120 lines
3.4 KiB
C++
120 lines
3.4 KiB
C++
#include "GraphClient.h"
|
|
#include <DateTime.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 = DateTime::GetTimestamp() + DateTime::GetUTCOffset();
|
|
return GetGraphHeader(timespan, currentTimestamp);
|
|
}
|
|
|
|
nlohmann::json GraphClient::GetGraphData(int deviceId, const std::vector<int>& dataIds, DataStorage::Timespan::type timespan)
|
|
{
|
|
int currentTimestamp = DateTime::GetTimestamp() + DateTime::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:
|
|
case DataStorage::Timespan::_2_Year:
|
|
case DataStorage::Timespan::_5_Year:
|
|
table = "datalog-yearly";
|
|
break;
|
|
default:
|
|
case DataStorage::Timespan::Unknown:
|
|
return nlohmann::json();
|
|
}
|
|
|
|
nlohmann::json rootData = nlohmann::json::array();
|
|
std::stringstream query;
|
|
|
|
int startTimestamp = DateTime::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 = DateTime::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
|