GraphClient.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "GraphClient.h"
  2. #include <DateTime.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 = DateTime::GetTimestamp() + DateTime::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 = DateTime::GetTimestamp() + DateTime::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. case DataStorage::Timespan::_2_Year:
  35. case DataStorage::Timespan::_5_Year:
  36. table = "datalog-yearly";
  37. break;
  38. default:
  39. case DataStorage::Timespan::Unknown:
  40. return nlohmann::json();
  41. }
  42. nlohmann::json rootData = nlohmann::json::array();
  43. std::stringstream query;
  44. int startTimestamp = DateTime::GetTimestamp() - timespan;
  45. for (auto dataId : dataIds)
  46. {
  47. nlohmann::json data;
  48. nlohmann::json dataArray;
  49. query.str("");
  50. if (StringAlgorithm::iequals(table, "datalog"))
  51. query << "SELECT UNIX_TIMESTAMP(`d`.`timestamp`) AS timestamp, `d`.`value` AS 'min-value', `d`.`value` AS 'mean-value', `d`.`value` AS 'max-value' ";
  52. else
  53. query << "SELECT UNIX_TIMESTAMP(`d`.`timestamp`) AS timestamp, `d`.`min-value`, `d`.`mean-value`, `d`.`max-value` ";
  54. query << "FROM `" << table << "` AS `d` ";
  55. query << "WHERE `d`.`device_id` = '" << deviceId << "' ";
  56. query << "AND `d`.`data_id` = '" << dataId << "' ";
  57. query << "AND UNIX_TIMESTAMP(`d`.`timestamp`) > '" << startTimestamp << "';";
  58. auto resultSet = m_pMySQLClient->ExecuteQuery(query.str());
  59. int offset = DateTime::GetUTCOffset();
  60. int timestamp;
  61. double minValue, meanValue, maxValue;
  62. if (resultSet.RowsCount() > 0)
  63. {
  64. while (resultSet.Next())
  65. {
  66. timestamp = resultSet.Int("timestamp") + offset;
  67. minValue = resultSet.Double("min-value");
  68. meanValue = resultSet.Double("mean-value");
  69. maxValue = resultSet.Double("max-value");
  70. nlohmann::json entry = nlohmann::json::array();
  71. entry.push_back(timestamp);
  72. std::stringstream entryValue;
  73. if (StringAlgorithm::iequals(table, "datalog"))
  74. entryValue << std::fixed << meanValue;
  75. else
  76. entryValue << std::fixed << minValue << ", " << std::fixed << maxValue;
  77. entry.push_back(entryValue.str());
  78. dataArray.push_back(entry);
  79. }
  80. data["data"] = dataArray;
  81. rootData.push_back(data);
  82. }
  83. }
  84. json["data"] = rootData;
  85. return json;
  86. }
  87. nlohmann::json GraphClient::GetGraphHeader(DataStorage::Timespan::type timespan, std::time_t currentTimestamp)
  88. {
  89. int startTimestamp = currentTimestamp - timespan;
  90. nlohmann::json json;
  91. json["timespan"] = DataStorage::Conversions::Timespan(timespan);
  92. json["start"] = startTimestamp;
  93. json["end"] = currentTimestamp;
  94. return json;
  95. }
  96. } // namespace DataInterface
  97. } // namespace DataStorageInterface