Add support to OREF (-o flag)

This commit is contained in:
Dima yawaflua Andreev
2024-11-14 20:22:44 +02:00
parent 27632fa5c6
commit 89781660c3
12 changed files with 504 additions and 57 deletions

View File

@@ -0,0 +1,69 @@
//
// Created by yawaflua on 01/11/2024.
//
#include <string>
#include <nlohmann/json.hpp>
namespace tzeva_adom {
class AlertMessage {
public:
std::string heb;
std::string eng;
std::string rus;
std::string arb;
int catId;
int matrixCatId;
std::string hebTitle;
std::string engTitle;
std::string rusTitle;
std::string arbTitle;
// AlertMessage(
// const std::string& heb, const std::string& eng, const std::string& rus, const std::string& arb,
// int catId, int matrixCatId,
// const std::string& hebTitle, const std::string& engTitle,
// const std::string& rusTitle, const std::string& arbTitle
// )
// : heb(heb), eng(eng), rus(rus), arb(arb),
// catId(catId), matrixCatId(matrixCatId),
// hebTitle(hebTitle), engTitle(engTitle),
// rusTitle(rusTitle), arbTitle(arbTitle) {}
AlertMessage() = default;
virtual ~AlertMessage() = default;
// JSON сериализация
friend void to_json(nlohmann::json& j, const AlertMessage& message) {
j = nlohmann::json{
{"heb", message.heb},
{"eng", message.eng},
{"rus", message.rus},
{"arb", message.arb},
{"catId", message.catId},
{"matrixCatId", message.matrixCatId},
{"hebTitle", message.hebTitle},
{"engTitle", message.engTitle},
{"rusTitle", message.rusTitle},
{"arbTitle", message.arbTitle}
};
}
// JSON десериализация
friend void from_json(const nlohmann::json& j, AlertMessage& message) {
j.at("heb").get_to(message.heb);
j.at("eng").get_to(message.eng);
j.at("rus").get_to(message.rus);
j.at("arb").get_to(message.arb);
j.at("catId").get_to(message.catId);
j.at("matrixCatId").get_to(message.matrixCatId);
j.at("hebTitle").get_to(message.hebTitle);
j.at("engTitle").get_to(message.engTitle);
j.at("rusTitle").get_to(message.rusTitle);
j.at("arbTitle").get_to(message.arbTitle);
}
using AlertingMessages = std::vector<AlertMessage>;
};
}

View File

@@ -11,8 +11,11 @@
#include <boost/optional.hpp>
#include <stdexcept>
#include <fmt/format.h>
#include <regex>
#include <nlohmann/json.hpp>
#include "models/Interfaces/IAlertResponse.cpp"
#include "models/Interfaces/IAlert.cpp"
namespace tzeva_adom {
using nlohmann::json;
@@ -31,9 +34,16 @@ namespace tzeva_adom {
}
#endif
class Alert {
class Alert : public IAlert{
public:
Alert() = default;
Alert(
const int64_t time,
const std::vector<std::string>& cities,
int64_t threat,
bool is_drill
) :
time(time), cities(cities), threat(threat), is_drill(is_drill) {};
virtual ~Alert() = default;
private:
@@ -48,10 +58,12 @@ namespace tzeva_adom {
void set_time(const int64_t & value) { this->time = value; }
const std::vector<std::string> & get_cities() const { return cities; }
std::vector<std::string> get_cities() override { return cities; }
std::vector<std::string> & get_mutable_cities() { return cities; }
void set_cities(const std::vector<std::string> & value) { this->cities = value; }
const int64_t & get_threat() const { return threat; }
int get_threat() override { return threat; }
int64_t & get_mutable_threat() { return threat; }
void set_threat(const int64_t & value) { this->threat = value; }
@@ -60,9 +72,25 @@ namespace tzeva_adom {
void set_is_drill(const bool & value) { this->is_drill = value; }
};
class AlertResponseElement {
class AlertResponseElement : public IAlertResponse {
public:
AlertResponseElement() = default;
AlertResponseElement(
const int id,
const nlohmann::json description,
const nlohmann::json alerts)
: id(id), description(description) {
for (const auto& alertJson : alerts) {
int64_t time = alertJson["time"];
std::vector<std::string> cities = alertJson["cities"].get<std::vector<std::string>>();
int64_t threat = alertJson["threat"];
bool is_drill = alertJson["isDrill"];
Alert _alert = *std::make_unique<Alert>(time, cities, threat, is_drill).get();
this->alerts.push_back(_alert);
}
}
virtual ~AlertResponseElement() = default;
private:
@@ -71,10 +99,19 @@ namespace tzeva_adom {
std::vector<Alert> alerts;
public:
const int64_t & get_id() const { return id; }
int get_id() override { return id; }
const int64_t &get_id() const { return id; }
int64_t & get_mutable_id() { return id; }
void set_id(const int64_t & value) { this->id = value; }
int get_threat() override {
int i = 0;
for (auto alert: alerts) {
i += alert.get_threat();
}
return i / alerts.size();
}
const nlohmann::json & get_description() const { return description; }
nlohmann::json & get_mutable_description() { return description; }
void set_description(const nlohmann::json & value) { this->description = value; }
@@ -82,6 +119,26 @@ namespace tzeva_adom {
const std::vector<Alert> & get_alerts() const { return alerts; }
std::vector<Alert> & get_mutable_alerts() { return alerts; }
void set_alerts(const std::vector<Alert> & value) { this->alerts = value; }
std::string get_cities() override {
return fmt::format("{}", fmt::join(get_cities_arr(), ", "));
}
std::vector<std::string> get_cities_arr() override {
std::vector<std::string> cities_to_return = {} ;
for (auto alert : alerts) {
cities_to_return.push_back(fmt::format("{}", fmt::join(alert.get_cities(), "")));
}
return cities_to_return;
}
std::vector<IAlert> get_alerts() override {
std::vector<IAlert> alertsToReturn {};
for (IAlert alert : alerts) {
alertsToReturn.insert(alertsToReturn.begin(), alert);
}
return alertsToReturn;
};
};
using AlertResponse = std::vector<AlertResponseElement>;

View File

@@ -0,0 +1,16 @@
//
// Created by yawaflua on 30/10/2024.
//
#pragma once
#include <vector>
#include <string>
namespace tzeva_adom {
class IAlert {
public:
virtual ~IAlert() = default;
virtual std::vector<std::string> get_cities() {};
virtual int get_threat() {};
};
}

View File

@@ -0,0 +1,24 @@
//
// Created by yawaflua on 30/10/2024.
//
#pragma once
#include <string>
#include <vector>
#include "IAlert.cpp"
namespace tzeva_adom {
class IAlertResponse
{
public:
// IAlertResponse() = default;
virtual ~IAlertResponse() = default;
virtual int get_id() = 0;
virtual int get_threat() = 0;
virtual std::string get_cities() = 0;
virtual std::vector<std::string> get_cities_arr() = 0;
virtual std::vector<IAlert> get_alerts() = 0;
};
}

View File

@@ -0,0 +1,125 @@
//
// Created by yawaflua on 30/10/2024.
//
#pragma once
#ifndef OREFALERTRESPONSE
#define OREFALERTRESPONSE
#include "models/Interfaces/IAlertResponse.cpp"
#include "utils/OREFToTzevaAdom.cpp"
#include <nlohmann/json.hpp>
namespace tzeva_adom{
class OrefAlert : public IAlert {
public:
OrefAlert() = default;
//virtual ~OrefAlert() = default;
void set_values(int _threat, std::vector<std::string> _cities) {
this->cities = _cities;
this->threat = _threat;
}
std::vector<std::string> get_cities() override { return cities; }
int get_threat() override { return threat; }
private:
int threat;
std::vector<std::string> cities;
};
class OrefAlertResponse : public IAlertResponse {
public:
OrefAlertResponse(const std::string& alertDate, const std::string& title,
const std::string& data, int64_t threat)
: alert_date(alertDate), title(title), data(data), threat(threat) {}
virtual ~OrefAlertResponse() = default;
private:
std::string alert_date;
std::string title;
std::string data;
int64_t threat;
public:
inline void set_data(std::string alert, std::string title_provided, std::string data_provided, int threat_provided) {
alert_date = alert;
title = title_provided;
data = data_provided;
threat = threat_provided;
}
const std::string & get_alert_date() const { return alert_date; }
std::string & get_mutable_alert_date() { return alert_date; }
void set_alert_date(const std::string & value) { this->alert_date = value; }
const std::string & get_title() const { return title; }
std::string & get_mutable_title() { return title; }
void set_title(const std::string & value) { this->title = value; }
const std::string & get_data() const { return data; }
std::string & get_mutable_data() { return data; }
void set_data(const std::string & value) { this->data = value; }
const int64_t &get_threat() const { return threat; }
int get_threat() override { return oref_threat_to_tzeva_adom(threat); }
int64_t & get_mutable_threat() { return threat; }
void set_threat(const int64_t & value) { this->threat = value; }
int get_id() override {
std::tm t = {};
std::istringstream ss(alert_date);
if (ss >> std::get_time(&t, "%Y-%m-%d %H:%M:%S"))
{
return std::mktime(&t) % 1000000;
}
};
std::string get_cities() override {return data; };
std::vector<std::string> get_cities_arr() override { return std::vector {data}; }
std::vector<IAlert> get_alerts() override {
auto orefAlertReturn = new OrefAlert();
orefAlertReturn->set_values(threat, get_cities_arr());
IAlert iAlertToReturn = static_cast<IAlert>(std::ref(*orefAlertReturn));
return std::vector {iAlertToReturn};
};
};
}
namespace tzeva_adom {
// void from_json(const nlohmann::json & j, OrefAlertResponse & x);
// void to_json(nlohmann::json & j, const OrefAlertResponse & x);
inline void to_json(nlohmann::json& j, const OrefAlertResponse& alert) {
j = nlohmann::json{
{"alert_date", alert.get_alert_date()},
{"title", alert.get_title()},
{"data", alert.get_data()},
{"threat", alert.get_threat()}
};
}
inline void from_json(const nlohmann::json& j, OrefAlertResponse& alert) {
if (j.contains("alert_date")) {
alert.set_alert_date(j.at("alert_date").get<std::string>());
}
if (j.contains("title")) {
alert.set_title(j.at("title").get<std::string>());
}
if (j.contains("data")) {
alert.set_data(j.at("data").get<std::string>());
}
if (j.contains("threat")) {
alert.set_threat(j.at("threat").get<int64_t>());
}
}
}
#endif