1 | #include <Storages/System/StorageSystemQuotaUsage.h> |
2 | #include <DataTypes/DataTypeString.h> |
3 | #include <DataTypes/DataTypesNumber.h> |
4 | #include <DataTypes/DataTypeUUID.h> |
5 | #include <DataTypes/DataTypeDateTime.h> |
6 | #include <DataTypes/DataTypeNullable.h> |
7 | #include <Interpreters/Context.h> |
8 | #include <Access/AccessControlManager.h> |
9 | #include <Access/QuotaContext.h> |
10 | #include <ext/range.h> |
11 | |
12 | |
13 | namespace DB |
14 | { |
15 | NamesAndTypesList StorageSystemQuotaUsage::getNamesAndTypes() |
16 | { |
17 | NamesAndTypesList names_and_types{ |
18 | {"name" , std::make_shared<DataTypeString>()}, |
19 | {"id" , std::make_shared<DataTypeUUID>()}, |
20 | {"key" , std::make_shared<DataTypeString>()}, |
21 | {"duration" , std::make_shared<DataTypeNullable>(std::make_shared<DataTypeUInt64>())}, |
22 | {"end_of_interval" , std::make_shared<DataTypeNullable>(std::make_shared<DataTypeDateTime>())}}; |
23 | |
24 | for (auto resource_type : ext::range_with_static_cast<Quota::ResourceType>(Quota::MAX_RESOURCE_TYPE)) |
25 | { |
26 | DataTypePtr data_type; |
27 | if (resource_type == Quota::EXECUTION_TIME) |
28 | data_type = std::make_shared<DataTypeFloat64>(); |
29 | else |
30 | data_type = std::make_shared<DataTypeUInt64>(); |
31 | |
32 | String column_name = Quota::resourceTypeToColumnName(resource_type); |
33 | names_and_types.push_back({column_name, std::make_shared<DataTypeNullable>(data_type)}); |
34 | names_and_types.push_back({String("max_" ) + column_name, std::make_shared<DataTypeNullable>(data_type)}); |
35 | } |
36 | return names_and_types; |
37 | } |
38 | |
39 | |
40 | void StorageSystemQuotaUsage::fillData(MutableColumns & res_columns, const Context & context, const SelectQueryInfo &) const |
41 | { |
42 | const auto & access_control = context.getAccessControlManager(); |
43 | for (const auto & info : access_control.getQuotaUsageInfo()) |
44 | { |
45 | for (const auto & interval : info.intervals) |
46 | { |
47 | size_t i = 0; |
48 | res_columns[i++]->insert(info.quota_name); |
49 | res_columns[i++]->insert(info.quota_id); |
50 | res_columns[i++]->insert(info.quota_key); |
51 | res_columns[i++]->insert(std::chrono::seconds{interval.duration}.count()); |
52 | res_columns[i++]->insert(std::chrono::system_clock::to_time_t(interval.end_of_interval)); |
53 | for (auto resource_type : ext::range(Quota::MAX_RESOURCE_TYPE)) |
54 | { |
55 | if (resource_type == Quota::EXECUTION_TIME) |
56 | { |
57 | res_columns[i++]->insert(Quota::executionTimeToSeconds(interval.used[resource_type])); |
58 | res_columns[i++]->insert(Quota::executionTimeToSeconds(interval.max[resource_type])); |
59 | } |
60 | else |
61 | { |
62 | res_columns[i++]->insert(interval.used[resource_type]); |
63 | res_columns[i++]->insert(interval.max[resource_type]); |
64 | } |
65 | } |
66 | } |
67 | |
68 | if (info.intervals.empty()) |
69 | { |
70 | size_t i = 0; |
71 | res_columns[i++]->insert(info.quota_name); |
72 | res_columns[i++]->insert(info.quota_id); |
73 | res_columns[i++]->insert(info.quota_key); |
74 | for (size_t j = 0; j != Quota::MAX_RESOURCE_TYPE * 2 + 2; ++j) |
75 | res_columns[i++]->insertDefault(); |
76 | } |
77 | } |
78 | } |
79 | } |
80 | |