Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include "influxlib.h"
  2. #include <variant>
  3. #include <string>
  4. #include <chrono>
  5. #include <memory>
  6. #include <sstream>
  7.  
  8. namespace influxLib
  9. {
  10. template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
  11. template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
  12.  
  13. InfluxLib::InfluxLib()
  14. {
  15.  
  16. }
  17.  
  18. Decorator::Decorator(const std::string aMeasurement):measurement(aMeasurement), timeStamp(Decorator::getCurrentTimestamp())
  19. {
  20.     mValue = {};
  21.     tags = {};
  22.     fields = {};
  23. }
  24.  
  25. Decorator&& Decorator::addField(std::string_view key, std::variant<int, long long int, std::string, double> value)
  26. {
  27.       std::stringstream fieldStream;
  28.       if (!fields.empty()) fieldStream << ",";
  29.  
  30.       fieldStream << key << "=";
  31.       std::visit(overloaded {
  32.         [&fieldStream](int value) { fieldStream << value << 'i'; },
  33.         [&fieldStream](long long int value) { fieldStream << value << 'i'; },
  34.         [&fieldStream](double value) { fieldStream << value; },
  35.         [&fieldStream](const std::string& value) { fieldStream << '"' << value << '"'; },
  36.         }, value);
  37.       fields += fieldStream.str();
  38.  
  39.       return std::move(*this);
  40. }
  41.  
  42. Decorator&& Decorator::addTag(std::string_view key, std::string_view value)
  43. {
  44.   std::stringstream tagStream;
  45.   tagStream<< ",";
  46.   tagStream<< key;
  47.   tagStream<< "=";
  48.   tagStream<< value;
  49.   fields += tagStream.str();
  50.  
  51.   return std::move(*this);
  52. }
  53.  
  54. Decorator &&Decorator::setTimeStamp(const std::chrono::system_clock::time_point &value)
  55. {
  56.     timeStamp = value;
  57.      return std::move(*this);
  58. }
  59.  
  60.  
  61. auto Decorator::getCurrentTimestamp()-> decltype(std::chrono::system_clock::now())
  62. {
  63.     return std::chrono::system_clock::now();
  64. }
  65.  
  66. std::string Decorator::toLineQuery() const
  67. {
  68.     return measurement + tags + " " + fields + " " + std::to_string(
  69.       std::chrono::duration_cast <std::chrono::nanoseconds>(timeStamp.time_since_epoch()).count());
  70. }
  71.  
  72.  
  73. std::string Decorator::getMeasurement() const
  74. {
  75.     return measurement;
  76. }
  77.  
  78.  
  79. void Decorator::setMeasurement(const std::string &value)
  80. {
  81.     measurement = value;
  82. }
  83.  
  84. std::chrono::time_point<std::chrono::system_clock> Decorator::getTimestamp() const
  85. {
  86.   return timeStamp;
  87. }
  88.  
  89. std::string Decorator::getFields() const
  90. {
  91.     return fields;
  92. }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement