-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathLineProtocol.cxx
170 lines (153 loc) · 6.75 KB
/
LineProtocol.cxx
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// MIT License
//
// Copyright (c) 2020-2025 offa
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "LineProtocol.h"
#include <iomanip>
#include <sstream>
namespace influxdb
{
namespace
{
template <class... Ts>
struct overloaded : Ts...
{
using Ts::operator()...;
};
template <class... Ts>
overloaded(Ts...) -> overloaded<Ts...>;
void appendIfNotEmpty(std::string& dest, const std::string& value, char separator)
{
if (!value.empty())
{
dest.append(std::string{separator}).append(value);
}
}
std::string escapeCharacters(std::string_view input, const std::string& escapedChars)
{
static const std::string escapeCharacter{"\\"};
std::string output;
output.reserve(input.size());
std::size_t searchStartPos{0};
// Find the first character that needs to be escaped
std::size_t escapedCharacterPos{input.find_first_of(escapedChars, searchStartPos)};
while (escapedCharacterPos != std::string::npos)
{
// Append the characters between the previous escaped character and the current one
output.append(input, searchStartPos, escapedCharacterPos - searchStartPos);
// Append the escape character and the character to be escaped
output.append(escapeCharacter).append(1, input[escapedCharacterPos]);
// Update the search start index to the character after the escaped character
searchStartPos = escapedCharacterPos + 1;
// Find the next character that needs to be escaped
escapedCharacterPos = input.find_first_of(escapedChars, searchStartPos);
}
// Append remaining characters after the final escaped character
output.append(input, searchStartPos);
return output;
}
std::string formatTags(const Point::TagSet& tagsDeque)
{
std::string tags;
bool addComma{false};
for (const auto& tag : tagsDeque)
{
if (addComma)
{
tags += ',';
}
tags += LineProtocol::EscapeStringElement(LineProtocol::ElementType::TagKey, tag.first);
tags += '=';
tags += LineProtocol::EscapeStringElement(LineProtocol::ElementType::TagValue, tag.second);
addComma = true;
}
return tags;
}
std::string formatFields(const Point::FieldSet& fieldsDeque)
{
std::stringstream convert;
convert << std::setprecision(Point::floatsPrecision) << std::fixed;
bool addComma{false};
for (const auto& field : fieldsDeque)
{
if (addComma)
{
convert << ',';
}
convert << LineProtocol::EscapeStringElement(LineProtocol::ElementType::FieldKey, field.first) << "=";
std::visit(overloaded{
[&convert](int v)
{ convert << v << 'i'; },
[&convert](long long int v)
{ convert << v << 'i'; },
[&convert](double v)
{ convert << v; },
[&convert](const std::string& v)
{ convert << '"' << LineProtocol::EscapeStringElement(LineProtocol::ElementType::FieldValue, v) << '"'; },
[&convert](bool v)
{ convert << (v ? "true" : "false"); },
[&convert](unsigned int v)
{ convert << v << 'u'; },
[&convert](unsigned long long int v)
{ convert << v << 'u'; },
},
field.second);
addComma = true;
}
return convert.str();
}
}
LineProtocol::LineProtocol()
: LineProtocol(std::string{})
{
}
LineProtocol::LineProtocol(const std::string& tags)
: globalTags(tags)
{
}
std::string LineProtocol::format(const Point& point) const
{
std::string line{LineProtocol::EscapeStringElement(LineProtocol::ElementType::Measurement, point.getName())};
appendIfNotEmpty(line, globalTags, ',');
appendIfNotEmpty(line, formatTags(point.getTagSet()), ',');
appendIfNotEmpty(line, formatFields(point.getFieldSet()), ' ');
return line.append(" ")
.append(std::to_string(std::chrono::duration_cast<std::chrono::nanoseconds>(point.getTimestamp().time_since_epoch()).count()));
}
std::string LineProtocol::EscapeStringElement(LineProtocol::ElementType type, std::string_view element)
{
// https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/#special-characters
static const std::string commaAndSpace{", "};
static const std::string commaEqualsAndSpace{",= "};
static const std::string doubleQuoteAndBackslash{R"("\)"};
switch (type)
{
case ElementType::Measurement:
return escapeCharacters(element, commaAndSpace);
case ElementType::TagKey:
case ElementType::TagValue:
case ElementType::FieldKey:
return escapeCharacters(element, commaEqualsAndSpace);
case ElementType::FieldValue:
return escapeCharacters(element, doubleQuoteAndBackslash);
}
return std::string{element};
}
}