-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Expand file tree
/
Copy pathAsynchronousMetricLog.cpp
More file actions
87 lines (76 loc) · 3 KB
/
Copy pathAsynchronousMetricLog.cpp
File metadata and controls
87 lines (76 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <DataTypes/DataTypeDate.h>
#include <DataTypes/DataTypeDateTime.h>
#include <DataTypes/DataTypeDateTime64.h>
#include <DataTypes/DataTypeLowCardinality.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypesNumber.h>
#include <Interpreters/AsynchronousMetricLog.h>
#include <Parsers/ExpressionElementParsers.h>
#include <Parsers/parseQuery.h>
#include <base/getFQDNOrHostName.h>
#include <Common/DateLUTImpl.h>
namespace DB
{
ColumnsDescription AsynchronousMetricLogElement::getColumnsDescription()
{
ParserCodec codec_parser;
return ColumnsDescription
{
{
"hostname",
std::make_shared<DataTypeLowCardinality>(std::make_shared<DataTypeString>()),
parseQuery(codec_parser, "(ZSTD(1))", 0, DBMS_DEFAULT_MAX_PARSER_DEPTH, DBMS_DEFAULT_MAX_PARSER_BACKTRACKS),
"Hostname of the server executing the query."
},
{
"event_date",
std::make_shared<DataTypeDate>(),
parseQuery(codec_parser, "(Delta(2), ZSTD(1))", 0, DBMS_DEFAULT_MAX_PARSER_DEPTH, DBMS_DEFAULT_MAX_PARSER_BACKTRACKS),
"Event date."
},
{
"event_time",
std::make_shared<DataTypeDateTime>(),
parseQuery(codec_parser, "(Delta(4), ZSTD(1))", 0, DBMS_DEFAULT_MAX_PARSER_DEPTH, DBMS_DEFAULT_MAX_PARSER_BACKTRACKS),
"Event time."
},
{
"metric",
std::make_shared<DataTypeLowCardinality>(std::make_shared<DataTypeString>()),
parseQuery(codec_parser, "(ZSTD(1))", 0, DBMS_DEFAULT_MAX_PARSER_DEPTH, DBMS_DEFAULT_MAX_PARSER_BACKTRACKS),
"Metric name."
},
{
"value",
std::make_shared<DataTypeFloat64>(),
parseQuery(codec_parser, "(ZSTD(3))", 0, DBMS_DEFAULT_MAX_PARSER_DEPTH, DBMS_DEFAULT_MAX_PARSER_BACKTRACKS),
"Metric value."
}
};
}
void AsynchronousMetricLogElement::appendToBlock(MutableColumns & columns) const
{
size_t column_idx = 0;
columns[column_idx++]->insert(getFQDNOrHostName());
columns[column_idx++]->insert(event_date);
columns[column_idx++]->insert(event_time);
columns[column_idx++]->insert(metric_name);
columns[column_idx++]->insert(value);
}
void AsynchronousMetricLog::addValues(const AsynchronousMetricValues & values)
{
AsynchronousMetricLogElement element;
element.event_time = time(nullptr);
element.event_date = static_cast<UInt16>(DateLUT::instance().toDayNum(element.event_time));
/// We will round the values to make them compress better in the table.
/// Note: as an alternative we can also use fixed point Decimal data type,
/// but we need to store up to UINT64_MAX sometimes.
static constexpr double precision = 1000.0;
for (const auto & [key, value] : values)
{
element.metric_name = key;
element.value = round(value.value * precision) / precision;
add([&](AsynchronousMetricLogElement & log_element) { log_element = element; });
}
}
}