GraphClient.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "GraphClient.h"
  2. #include "Util/Util.h"
  3. #include <MySQLClient.h>
  4. #include <StringAlgorithm.h>
  5. #include <sstream>
  6. namespace DataStorageInterface {
  7. namespace DataInterface {
  8. GraphClient::GraphClient(MySQL::MySQLClient* pMySQLClient) :
  9. m_pMySQLClient(pMySQLClient)
  10. {
  11. }
  12. nlohmann::json GraphClient::GetGraphHeader(const std::vector<int>& dataIds, DataStorage::Timespan::type timespan)
  13. {
  14. int currentTimestamp = Util::GetTimestamp() + Util::GetUTCOffset();
  15. return GetGraphHeader(timespan, currentTimestamp);
  16. }
  17. nlohmann::json GraphClient::GetGraphData(int deviceId, const std::vector<int>& dataIds, DataStorage::Timespan::type timespan)
  18. {
  19. int currentTimestamp = Util::GetTimestamp() + Util::GetUTCOffset();
  20. nlohmann::json json = GetGraphHeader(timespan, currentTimestamp);
  21. std::string table;
  22. switch (timespan)
  23. {
  24. case DataStorage::Timespan::Day:
  25. table = "datalog"; // "datalog-daily"
  26. break;
  27. case DataStorage::Timespan::Week:
  28. table = "datalog-weekly";
  29. break;
  30. case DataStorage::Timespan::Month:
  31. table = "datalog-monthly";
  32. break;
  33. case DataStorage::Timespan::Year:
  34. table = "datalog-yearly";
  35. break;
  36. default:
  37. case DataStorage::Timespan::Unknown:
  38. return nlohmann::json();
  39. }
  40. nlohmann::json rootData = nlohmann::json::array();
  41. std::stringstream query;
  42. int startTimestamp = Util::GetTimestamp() - timespan;
  43. for (auto dataId : dataIds)
  44. {
  45. nlohmann::json data;
  46. nlohmann::json dataArray;
  47. query.str("");
  48. if (StringAlgorithm::iequals(table, "datalog"))
  49. query << "SELECT UNIX_TIMESTAMP(`d`.`timestamp`) AS timestamp, `d`.`value` AS 'min-value', `d`.`value` AS 'mean-value', `d`.`value` AS 'max-value' ";
  50. else
  51. query << "SELECT UNIX_TIMESTAMP(`d`.`timestamp`) AS timestamp, `d`.`min-value`, `d`.`mean-value`, `d`.`max-value` ";
  52. query << "FROM `" << table << "` AS `d` ";
  53. query << "WHERE `d`.`device_id` = '" << deviceId << "' ";
  54. query << "AND `d`.`data_id` = '" << dataId << "' ";
  55. query << "AND UNIX_TIMESTAMP(`d`.`timestamp`) > '" << startTimestamp << "';";
  56. auto resultSet = m_pMySQLClient->ExecuteQuery(query.str());
  57. int offset = Util::GetUTCOffset();
  58. int timestamp;
  59. double minValue, meanValue, maxValue;
  60. if (resultSet.RowsCount() > 0)
  61. {
  62. while (resultSet.Next())
  63. {
  64. timestamp = resultSet.Int("timestamp") + offset;
  65. minValue = resultSet.Double("min-value");
  66. meanValue = resultSet.Double("mean-value");
  67. maxValue = resultSet.Double("max-value");
  68. nlohmann::json entry = nlohmann::json::array();
  69. entry.push_back(timestamp);
  70. std::stringstream entryValue;
  71. if (StringAlgorithm::iequals(table, "datalog"))
  72. entryValue << std::fixed << meanValue;
  73. else
  74. entryValue << std::fixed << minValue << ", " << std::fixed << maxValue;
  75. entry.push_back(entryValue.str());
  76. dataArray.push_back(entry);
  77. }
  78. data["data"] = dataArray;
  79. rootData.push_back(data);
  80. }
  81. }
  82. json["data"] = rootData;
  83. return json;
  84. }
  85. nlohmann::json GraphClient::GetGraphHeader(DataStorage::Timespan::type timespan, std::time_t currentTimestamp)
  86. {
  87. int startTimestamp = currentTimestamp - timespan;
  88. nlohmann::json json;
  89. json["timespan"] = DataStorage::Conversions::Timespan(timespan);
  90. json["start"] = startTimestamp;
  91. json["end"] = currentTimestamp;
  92. return json;
  93. }
  94. } // namespace DataInterface
  95. } // namespace DataStorageInterface