Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "influxlib.h"
- #include <variant>
- #include <string>
- #include <chrono>
- #include <memory>
- #include <sstream>
- namespace influxLib
- {
- template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
- template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
- InfluxLib::InfluxLib()
- {
- }
- Decorator::Decorator(const std::string aMeasurement):measurement(aMeasurement), timeStamp(Decorator::getCurrentTimestamp())
- {
- mValue = {};
- tags = {};
- fields = {};
- }
- Decorator&& Decorator::addField(std::string_view key, std::variant<int, long long int, std::string, double> value)
- {
- std::stringstream fieldStream;
- if (!fields.empty()) fieldStream << ",";
- fieldStream << key << "=";
- std::visit(overloaded {
- [&fieldStream](int value) { fieldStream << value << 'i'; },
- [&fieldStream](long long int value) { fieldStream << value << 'i'; },
- [&fieldStream](double value) { fieldStream << value; },
- [&fieldStream](const std::string& value) { fieldStream << '"' << value << '"'; },
- }, value);
- fields += fieldStream.str();
- return std::move(*this);
- }
- Decorator&& Decorator::addTag(std::string_view key, std::string_view value)
- {
- std::stringstream tagStream;
- tagStream<< ",";
- tagStream<< key;
- tagStream<< "=";
- tagStream<< value;
- fields += tagStream.str();
- return std::move(*this);
- }
- Decorator &&Decorator::setTimeStamp(const std::chrono::system_clock::time_point &value)
- {
- timeStamp = value;
- return std::move(*this);
- }
- auto Decorator::getCurrentTimestamp()-> decltype(std::chrono::system_clock::now())
- {
- return std::chrono::system_clock::now();
- }
- std::string Decorator::toLineQuery() const
- {
- return measurement + tags + " " + fields + " " + std::to_string(
- std::chrono::duration_cast <std::chrono::nanoseconds>(timeStamp.time_since_epoch()).count());
- }
- std::string Decorator::getMeasurement() const
- {
- return measurement;
- }
- void Decorator::setMeasurement(const std::string &value)
- {
- measurement = value;
- }
- std::chrono::time_point<std::chrono::system_clock> Decorator::getTimestamp() const
- {
- return timeStamp;
- }
- std::string Decorator::getFields() const
- {
- return fields;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement