This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Point.cxx
59 lines (48 loc) · 1.42 KB
/
Point.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
///
/// \author Adam Wegrzynek <adam.wegrzynek@cern.ch>
///
#include "Point.h"
#include <iostream>
#include <chrono>
#include <memory>
#include <sstream>
namespace influxdb
{
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
Point::Point(const std::string& measurement) :
mMeasurement(measurement), mTimestamp(Point::getCurrentTimestamp())
{
}
Point&& Point::addField(std::string_view name, std::variant<int, std::string, double> value)
{
std::stringstream convert;
if (!mFields.empty()) convert << ",";
convert << name << "=";
std::visit(overloaded {
[&convert](int value) { convert << value << 'i'; },
[&convert](double value) { convert << value; },
[&convert](const std::string& value) { convert << '"' << value << '"'; },
}, value);
mFields += convert.str();
return std::move(*this);
}
Point&& Point::addTag(std::string_view key, std::string_view value)
{
mTags += ",";
mTags += key;
mTags += "=";
mTags += value;
return std::move(*this);
}
auto Point::getCurrentTimestamp() -> decltype(std::chrono::system_clock::now())
{
return std::chrono::system_clock::now();
}
std::string Point::toLineProtocol() const
{
return mMeasurement + mTags + " " + mFields + " " + std::to_string(
std::chrono::duration_cast <std::chrono::nanoseconds>(mTimestamp.time_since_epoch()).count()
);
}
} // namespace influxdb