Добавил слой моделей данных и обновил контроллеры

This commit is contained in:
2024-02-23 21:51:57 +03:00
parent 64f9157e85
commit c6d6be9fff
27 changed files with 371 additions and 181 deletions
+2 -1
View File
@@ -11,7 +11,8 @@ add_library(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PUBLIC include)
# Подключаем зависимости
target_link_libraries(${PROJECT_NAME}
target_link_libraries(${PROJECT_NAME}
la_data_models
la_json
la_storage
la_utils
@@ -11,7 +11,7 @@ namespace controllers::anomaly_logs::fields {
/// @defgroup Поля ответа
/// @{
constexpr std::string_view id = "id";
constexpr std::string_view class_id = "class_id";
constexpr std::string_view cluster_id = "cluster_id";
constexpr std::string_view data = "data";
/// @}
}
@@ -1,31 +0,0 @@
#include "query.h"
#include <sstream>
#include <storage/transaction.h>
namespace controllers::anomaly_logs::details {
std::vector<AnomalyLog> GetAnomalyLogs(int64_t last_id) {
auto transaction = storage::Transaction::Create();
std::ostringstream query;
query << "select id, cluster_id, data from anomaly_logs where id > " << last_id << ";";
auto pqxx_result = transaction.Do(query.str());
std::vector<AnomalyLog> result;
result.reserve(pqxx_result.size());
for (const auto& row : pqxx_result) {
AnomalyLog anomaly_log{
row[0].as<int64_t>(),
row[1].as<int64_t>(),
row[2].as<std::string>(),
};
result.push_back(std::move(anomaly_log));
}
return result;
}
}
@@ -1,12 +0,0 @@
#pragma once
#include <string>
#include <vector>
namespace controllers::anomaly_logs::details {
struct AnomalyLog {
};
std::vector<AnomalyLog> GetAnomalyLogs(int64_t last_id);
}
@@ -1,23 +1,31 @@
#include <controllers/anomaly_logs/get.h>
#include <data_models/anomaly_logs.h>
#include <storage/transaction.h>
#include <utils/utils.h>
#include "details/fields.h"
#include "details/query.h"
using namespace controllers::anomaly_logs;
json Get::operator()(const json& parameters) const {
// Проверяем наличие необходимых полей
utils::CheckForRequiredFields(parameters, { fields::last_id });
auto last_index = parameters[fields::last_id].get<int64_t>();
// Создаём транзакцию
auto transaction = storage::Transaction::Create();
auto anomaly_logs = details::GetAnomalyLogs(last_index);
// Создаём модель
data_models::AnomalyLogsModel anomaly_logs_model(transaction);
// Запрашиваем логи
auto anomaly_logs = anomaly_logs_model.GetAll(parameters[fields::last_id]);
// Формируем ответ
json result = json::array();
for (auto& log : anomaly_logs) {
json item = json::object();
item[fields::id] = log.id;
item[fields::class_id] = log.class_id;
item[fields::cluster_id] = log.cluster_id;
item[fields::data] = std::move(log.data);
result.push_back(std::move(item));
@@ -1,15 +1,35 @@
#include <controllers/logs_clusters/create.h>
#include <data_models/logs_clusters.h>
#include <storage/transaction.h>
#include <utils/utils.h>
#include "details/fields.h"
#include "details/query.h"
using namespace controllers::logs_clusters;
json Create::operator()(const json& parameters) const {
// Проверяем наличие необходимых полей
utils::CheckForRequiredFields(parameters, { fields::id, fields::name, fields::description, fields::recommendation });
details::CreateLogsCluster({parameters[fields::id], parameters[fields::name], parameters[fields::description], parameters[fields::recommendation]});
// Создаём транзакцию
auto transaction = storage::Transaction::Create();
// Создаём модель
data_models::LogsClustersModel logs_clusters_model(transaction);
// Проверяем, что кластер с указанным id не существует
int64_t id = parameters[fields::id];
if (logs_clusters_model.Has(id)) {
throw std::runtime_error("Cluster with specified id already exists");
}
// Создаём кластер
logs_clusters_model.Create({
id,
parameters[fields::name],
parameters[fields::description],
parameters[fields::recommendation]
});
return {};
}
@@ -1,44 +0,0 @@
#include "query.h"
#include <sstream>
#include <storage/transaction.h>
namespace controllers::logs_clusters::details {
std::vector<LogsCluster> GetLogsClusters() {
auto transaction = storage::Transaction::Create();
std::ostringstream query;
query << "select id, name, description, recommendation from logs_clusters;";
auto pqxx_result = transaction.Do(query.str());
std::vector<LogsCluster> result;
result.reserve(pqxx_result.size());
for (const auto& row : pqxx_result) {
LogsCluster logs_cluster{
row[0].as<int64_t>(),
row[1].as<std::string>(),
row[2].as<std::string>(),
row[3].as<std::string>(),
};
result.push_back(std::move(logs_cluster));
}
return result;
}
void CreateLogsCluster(const LogsCluster& cluster) {
std::ostringstream query;
query << "insert into logs_clusters (id, name, description, recommendation) values ("
<< cluster.id << ", "
<< "'" << cluster.name << "', "
<< "'" << cluster.description << "', "
<< "'" << cluster.recommendation << "'"
<< ");";
PostgreSqlStorage::Get().Do(query.str());
}
}
@@ -1,17 +0,0 @@
#pragma once
#include <string>
#include <vector>
namespace controllers::logs_clusters::details {
struct LogsCluster {
int64_t id;
std::string name;
std::string description;
std::string recommendation;
};
std::vector<LogsCluster> GetLogsClusters();
void CreateLogsCluster(const LogsCluster& cluster);
}
@@ -1,14 +1,22 @@
#include <controllers/logs_clusters/get.h>
#include <utils/utils.h>
#include <data_models/logs_clusters.h>
#include <storage/transaction.h>
#include "details/fields.h"
#include "details/query.h"
using namespace controllers::logs_clusters;
json Get::operator()(const json& parameters) const {
auto clusters = details::GetLogsClusters();
// Создаём транзакцию
auto transaction = storage::Transaction::Create();
// Создаём модель
data_models::LogsClustersModel logs_clusters_model(transaction);
// Запрашиваем кластеры
auto clusters = logs_clusters_model.GetAll();
// Формируем ответ
json result = json::array();
for (auto& cluster : clusters) {
json item = json::object();
@@ -1,26 +0,0 @@
#include "query.h"
#include <sstream>
#include <storage/transaction.h>
namespace controllers::statistics::details {
std::vector<Statistic> GetStatistics() {
auto transaction = storage::Transaction::Create();
std::ostringstream query;
query << "select name, value from statistics;";
auto pqxx_result = transaction.Do(query.str());
std::vector<Statistic> result;
result.reserve(pqxx_result.size());
for (const auto& row : pqxx_result) {
Statistic statistic{ row[0].as<std::string>(), row[1].as<size_t>() };
result.push_back(std::move(statistic));
}
return result;
}
}
@@ -1,13 +0,0 @@
#pragma once
#include <string>
#include <vector>
namespace controllers::statistics::details {
struct Statistic {
std::string name;
size_t value;
};
std::vector<Statistic> GetStatistics();
}
@@ -1,12 +1,20 @@
#include <controllers/statistics/get.h>
#include "details/query.h"
#include <data_models/statistics.h>
#include <storage/transaction.h>
using namespace controllers::statistics;
json Get::operator()(const json& parameters) const {
auto statistics = details::GetStatistics();
// Создаём транзакцию
auto transaction = storage::Transaction::Create();
// Создаём модель
data_models::StatisticsModel statistics_model(transaction);
// Запрашиваем статистику
auto statistics = statistics_model.GetAll();
// Формируем ответ
json result;
for (auto& statistic : statistics) {
result[std::move(statistic.name)] = statistic.value;