| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #include "GraphClient.h"
- #include "Util/Util.h"
- #include <MySQLClient.h>
- #include <StringAlgorithm.h>
- #include <sstream>
- namespace DataStorageInterface {
- namespace DataInterface {
- GraphClient::GraphClient(MySQL::MySQLClient* pMySQLClient) :
- m_pMySQLClient(pMySQLClient)
- {
- }
- nlohmann::json GraphClient::GetGraphHeader(const std::vector<int>& dataIds, DataStorage::Timespan::type timespan)
- {
- int currentTimestamp = Util::GetTimestamp() + Util::GetUTCOffset();
- return GetGraphHeader(timespan, currentTimestamp);
- }
- nlohmann::json GraphClient::GetGraphData(int deviceId, const std::vector<int>& dataIds, DataStorage::Timespan::type timespan)
- {
- int currentTimestamp = Util::GetTimestamp() + Util::GetUTCOffset();
- nlohmann::json json = GetGraphHeader(timespan, currentTimestamp);
- std::string table;
- switch (timespan)
- {
- case DataStorage::Timespan::Day:
- table = "datalog"; // "datalog-daily"
- break;
- case DataStorage::Timespan::Week:
- table = "datalog-weekly";
- break;
- case DataStorage::Timespan::Month:
- table = "datalog-monthly";
- break;
- case DataStorage::Timespan::Year:
- table = "datalog-yearly";
- break;
- default:
- case DataStorage::Timespan::Unknown:
- return nlohmann::json();
- }
- nlohmann::json rootData = nlohmann::json::array();
- std::stringstream query;
- int startTimestamp = Util::GetTimestamp() - timespan;
- for (auto dataId : dataIds)
- {
- nlohmann::json data;
- nlohmann::json dataArray;
- query.str("");
- if (StringAlgorithm::iequals(table, "datalog"))
- query << "SELECT UNIX_TIMESTAMP(`d`.`timestamp`) AS timestamp, `d`.`value` AS 'min-value', `d`.`value` AS 'mean-value', `d`.`value` AS 'max-value' ";
- else
- query << "SELECT UNIX_TIMESTAMP(`d`.`timestamp`) AS timestamp, `d`.`min-value`, `d`.`mean-value`, `d`.`max-value` ";
- query << "FROM `" << table << "` AS `d` ";
- query << "WHERE `d`.`device_id` = '" << deviceId << "' ";
- query << "AND `d`.`data_id` = '" << dataId << "' ";
- query << "AND UNIX_TIMESTAMP(`d`.`timestamp`) > '" << startTimestamp << "';";
- auto resultSet = m_pMySQLClient->ExecuteQuery(query.str());
- int offset = Util::GetUTCOffset();
- int timestamp;
- double minValue, meanValue, maxValue;
- if (resultSet.RowsCount() > 0)
- {
- while (resultSet.Next())
- {
- timestamp = resultSet.Int("timestamp") + offset;
- minValue = resultSet.Double("min-value");
- meanValue = resultSet.Double("mean-value");
- maxValue = resultSet.Double("max-value");
- nlohmann::json entry = nlohmann::json::array();
- entry.push_back(timestamp);
- std::stringstream entryValue;
- if (StringAlgorithm::iequals(table, "datalog"))
- entryValue << std::fixed << meanValue;
- else
- entryValue << std::fixed << minValue << ", " << std::fixed << maxValue;
- entry.push_back(entryValue.str());
- dataArray.push_back(entry);
- }
- data["data"] = dataArray;
- rootData.push_back(data);
- }
- }
- json["data"] = rootData;
- return json;
- }
- nlohmann::json GraphClient::GetGraphHeader(DataStorage::Timespan::type timespan, std::time_t currentTimestamp)
- {
- int startTimestamp = currentTimestamp - timespan;
- nlohmann::json json;
- json["timespan"] = DataStorage::Conversions::Timespan(timespan);
- json["start"] = startTimestamp;
- json["end"] = currentTimestamp;
- return json;
- }
- } // namespace DataInterface
- } // namespace DataStorageInterface
|