1 | #include <Storages/System/StorageSystemGraphite.h> |
2 | #include <Storages/MergeTree/MergeTreeData.h> |
3 | #include <Interpreters/Context.h> |
4 | |
5 | |
6 | namespace DB |
7 | { |
8 | |
9 | NamesAndTypesList StorageSystemGraphite::getNamesAndTypes() |
10 | { |
11 | return { |
12 | {"config_name" , std::make_shared<DataTypeString>()}, |
13 | {"regexp" , std::make_shared<DataTypeString>()}, |
14 | {"function" , std::make_shared<DataTypeString>()}, |
15 | {"age" , std::make_shared<DataTypeUInt64>()}, |
16 | {"precision" , std::make_shared<DataTypeUInt64>()}, |
17 | {"priority" , std::make_shared<DataTypeUInt16>()}, |
18 | {"is_default" , std::make_shared<DataTypeUInt8>()}, |
19 | {"Tables.database" , std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>())}, |
20 | {"Tables.table" , std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>())}, |
21 | }; |
22 | } |
23 | |
24 | |
25 | /* |
26 | * Looking for (Replicated)*GraphiteMergeTree and get all configuration parameters for them |
27 | */ |
28 | StorageSystemGraphite::Configs StorageSystemGraphite::getConfigs(const Context & context) const |
29 | { |
30 | const Databases databases = context.getDatabases(); |
31 | Configs graphite_configs; |
32 | |
33 | for (const auto & db : databases) |
34 | { |
35 | /// Lazy database can not contain MergeTree tables |
36 | if (db.second->getEngineName() == "Lazy" ) |
37 | continue; |
38 | |
39 | for (auto iterator = db.second->getTablesIterator(context); iterator->isValid(); iterator->next()) |
40 | { |
41 | auto & table = iterator->table(); |
42 | |
43 | const MergeTreeData * table_data = dynamic_cast<const MergeTreeData *>(table.get()); |
44 | if (!table_data) |
45 | continue; |
46 | |
47 | if (table_data->merging_params.mode == MergeTreeData::MergingParams::Graphite) |
48 | { |
49 | const String & config_name = table_data->merging_params.graphite_params.config_name; |
50 | |
51 | if (!graphite_configs.count(config_name)) |
52 | { |
53 | Config new_config = |
54 | { |
55 | table_data->merging_params.graphite_params, |
56 | { table_data->getDatabaseName() }, |
57 | { table_data->getTableName() }, |
58 | }; |
59 | graphite_configs.emplace(config_name, new_config); |
60 | } |
61 | else |
62 | { |
63 | graphite_configs[config_name].databases.emplace_back(table_data->getDatabaseName()); |
64 | graphite_configs[config_name].tables.emplace_back(table_data->getTableName()); |
65 | } |
66 | } |
67 | } |
68 | } |
69 | |
70 | return graphite_configs; |
71 | } |
72 | |
73 | void StorageSystemGraphite::fillData(MutableColumns & res_columns, const Context & context, const SelectQueryInfo &) const |
74 | { |
75 | Configs graphite_configs = StorageSystemGraphite::getConfigs(context); |
76 | |
77 | for (const auto & config : graphite_configs) |
78 | { |
79 | UInt16 priority = 0; |
80 | for (const auto & pattern : config.second.graphite_params.patterns) |
81 | { |
82 | bool is_default = pattern.regexp == nullptr; |
83 | String regexp; |
84 | String function; |
85 | |
86 | if (is_default) |
87 | { |
88 | priority = std::numeric_limits<UInt16>::max(); |
89 | } |
90 | else |
91 | { |
92 | priority++; |
93 | regexp = pattern.regexp_str; |
94 | } |
95 | |
96 | if (pattern.function) |
97 | { |
98 | function = pattern.function->getName(); |
99 | } |
100 | |
101 | if (!pattern.retentions.empty()) |
102 | { |
103 | for (const auto & retention : pattern.retentions) |
104 | { |
105 | size_t i = 0; |
106 | res_columns[i++]->insert(config.first); |
107 | res_columns[i++]->insert(regexp); |
108 | res_columns[i++]->insert(function); |
109 | res_columns[i++]->insert(retention.age); |
110 | res_columns[i++]->insert(retention.precision); |
111 | res_columns[i++]->insert(priority); |
112 | res_columns[i++]->insert(is_default); |
113 | res_columns[i++]->insert(config.second.databases); |
114 | res_columns[i++]->insert(config.second.tables); |
115 | } |
116 | } |
117 | else |
118 | { |
119 | size_t i = 0; |
120 | res_columns[i++]->insert(config.first); |
121 | res_columns[i++]->insert(regexp); |
122 | res_columns[i++]->insert(function); |
123 | res_columns[i++]->insertDefault(); |
124 | res_columns[i++]->insertDefault(); |
125 | res_columns[i++]->insert(priority); |
126 | res_columns[i++]->insert(is_default); |
127 | res_columns[i++]->insert(config.second.databases); |
128 | res_columns[i++]->insert(config.second.tables); |
129 | } |
130 | } |
131 | } |
132 | } |
133 | |
134 | } |
135 | |