#ifndef JOURNALSERVICE_H #define JOURNALSERVICE_H #include "HttpClient.h" #include "logs_r.h" #include "utils.h" #include #include #include #include #include #include #include struct JournalSettings{ QMap> groupsWithSensors; qint64 start_time; qint64 end_time; bool cmp; QList status; JournalSettings(QMap> a, qint64 b, qint64 c, bool d, QList e) { groupsWithSensors = a; start_time = b; end_time = c; cmp = d; status = e; } }; class JournalService : public QObject { Q_OBJECT public: explicit JournalService(HttpClient *client, QObject *parent = nullptr) : QObject(parent) , httpClient(client) {} int getLogsAmount(const QMap> &groupWithSensors, const QList &threatLevels, qint64 start, qint64 end) { QJsonObject rootObj; rootObj["start_time"] = start; rootObj["end_time"] = end; QJsonArray threatLevelsArray; for (int level : threatLevels) { threatLevelsArray.append(level); } rootObj["threat_levels"] = threatLevelsArray; QJsonObject sensorsObj; for (auto it = groupWithSensors.constBegin(); it != groupWithSensors.constEnd(); ++it) { QJsonArray sensorArray; for (QString sensorId : it.value()) { sensorArray.append(sensorId); } sensorsObj[it.key()] = sensorArray; } rootObj["sensors"] = sensorsObj; QJsonDocument doc(rootObj); QByteArray requestData = doc.toJson(); QJsonObject responseJson = httpClient->post(utils::API_URL + "/logs/getLogsAmount", QJsonDocument::fromJson(requestData).object()); if (responseJson.contains("amount")) { return responseJson["amount"].toInt(); } return 0; } QList getLogs(const QMap> &groupWithSensors, const QList &threatLevels, bool cmp, qint64 start, qint64 end, int pageNum, int pageSize) { QJsonObject rootObj; QJsonArray threatLevelsArray; for (int level : threatLevels) { threatLevelsArray.append(level); } rootObj["threat_levels"] = threatLevelsArray; rootObj["start_time"] = start; rootObj["end_time"] = end; rootObj["page_number"] = pageNum; rootObj["page_size"] = pageSize; QJsonObject sensorsObj; for (auto it = groupWithSensors.constBegin(); it != groupWithSensors.constEnd(); ++it) { QJsonArray sensorArray; for (QString sensorId : it.value()) { sensorArray.append(sensorId); } sensorsObj[it.key()] = sensorArray; } rootObj["sensors"] = sensorsObj; QJsonArray orderArray; QJsonArray orderItem; cmp ? orderItem.append("threat_level") : orderItem.append("date"); orderItem.append("-"); orderArray.append(orderItem); rootObj["order"] = orderArray; QJsonDocument doc(rootObj); QByteArray requestData = doc.toJson(); QJsonObject responseJson = httpClient->post(utils::API_URL + "/logs/getLogs", QJsonDocument::fromJson(requestData).object()); QList logsList; if (responseJson.contains("logs") && responseJson["logs"].isArray()) { QJsonArray logsArray = responseJson["logs"].toArray(); for (const QJsonValue &logValue : logsArray) { if (logValue.isObject()) { LogsR log; log.parseResponse(logValue.toObject()); logsList.append(log); } } } return logsList; } private: HttpClient *httpClient; }; #endif // JOURNALSERVICE_H