Добавил исходный код
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
# Модуль контроллеров -- обработчиков запросов
|
||||
project(la_controllers)
|
||||
|
||||
# Собираем исходные файлы
|
||||
file(GLOB_RECURSE SOURCES src/*.cpp)
|
||||
|
||||
# Создаём библиотеку
|
||||
add_library(${PROJECT_NAME} ${SOURCES})
|
||||
|
||||
# Указываем директории с заголовочными файлами
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC include)
|
||||
|
||||
# Подключаем зависимости
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
la_json
|
||||
la_storage
|
||||
la_utils
|
||||
)
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <controllers/controller_base.h>
|
||||
|
||||
namespace controllers::anomaly_logs {
|
||||
/// @brief Получение аномальных логов
|
||||
class Get : public ControllerBase {
|
||||
public:
|
||||
/// @brief Оператор вызова
|
||||
/// @param[in] parameters Параметры
|
||||
/// @returns Ответ
|
||||
json operator()(const json& parameters) const override;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <json/json.h>
|
||||
|
||||
namespace controllers {
|
||||
class ControllerBase {
|
||||
public:
|
||||
/// @brief Деструктор
|
||||
virtual ~ControllerBase() = default;
|
||||
|
||||
/// @brief Вызов логики контроллера
|
||||
/// @param[in] parameters Параметры
|
||||
/// @returns Ответ контроллера
|
||||
virtual json operator()(const json& parameters) const = 0;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <controllers/controller_base.h>
|
||||
|
||||
namespace controllers::logs_clusters {
|
||||
/// @brief Создание кластера логов
|
||||
class Create : public ControllerBase {
|
||||
public:
|
||||
/// @brief Оператор вызова
|
||||
/// @param[in] parameters Параметры
|
||||
/// @returns Ответ
|
||||
json operator()(const json& parameters) const override;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <controllers/controller_base.h>
|
||||
|
||||
namespace controllers::logs_clusters {
|
||||
/// @brief Получение кластеров логов
|
||||
class Get : public ControllerBase {
|
||||
public:
|
||||
/// @brief Оператор вызова
|
||||
/// @param[in] parameters Параметры
|
||||
/// @returns Ответ
|
||||
json operator()(const json& parameters) const override;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <controllers/controller_base.h>
|
||||
|
||||
namespace controllers::statistics {
|
||||
/// @brief Получение статистики
|
||||
class Get : public ControllerBase {
|
||||
public:
|
||||
/// @brief Оператор вызова
|
||||
/// @param[in] parameters Параметры
|
||||
/// @returns Ответ
|
||||
json operator()(const json& parameters) const override;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
namespace controllers::anomaly_logs::fields {
|
||||
/// @defgroup Поля запроса
|
||||
/// @{
|
||||
constexpr std::string_view last_id = "last_id";
|
||||
/// @}
|
||||
|
||||
/// @defgroup Поля ответа
|
||||
/// @{
|
||||
constexpr std::string_view id = "id";
|
||||
constexpr std::string_view class_id = "class_id";
|
||||
constexpr std::string_view data = "data";
|
||||
/// @}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include "query.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include <storage/postgresql/postgresql_storage.h>
|
||||
|
||||
namespace controllers::anomaly_logs::details {
|
||||
std::vector<AnomalyLog> GetAnomalyLogs(int64_t last_id) {
|
||||
std::ostringstream query;
|
||||
query << "select id, cluster_id, data from anomaly_logs where id > " << last_id << ";";
|
||||
|
||||
auto pqxx_result = PostgreSqlStorage::Get().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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace controllers::anomaly_logs::details {
|
||||
struct AnomalyLog {
|
||||
int64_t id;
|
||||
int64_t class_id;
|
||||
std::string data;
|
||||
};
|
||||
|
||||
std::vector<AnomalyLog> GetAnomalyLogs(int64_t last_id);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#include <controllers/anomaly_logs/get.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 anomaly_logs = details::GetAnomalyLogs(last_index);
|
||||
|
||||
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::data] = std::move(log.data);
|
||||
|
||||
result.push_back(std::move(item));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <controllers/logs_clusters/create.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]});
|
||||
|
||||
return {};
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
namespace controllers::logs_clusters::fields {
|
||||
/// @defgroup Поля ответа
|
||||
/// @{
|
||||
constexpr std::string_view id = "id";
|
||||
constexpr std::string_view name = "name";
|
||||
constexpr std::string_view description = "description";
|
||||
constexpr std::string_view recommendation = "recommendation";
|
||||
/// @}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "query.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include <storage/postgresql/postgresql_storage.h>
|
||||
|
||||
namespace controllers::logs_clusters::details {
|
||||
std::vector<LogsCluster> GetLogsClusters() {
|
||||
std::ostringstream query;
|
||||
query << "select id, name, description, recommendation from logs_clusters;";
|
||||
|
||||
auto pqxx_result = PostgreSqlStorage::Get().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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#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);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <controllers/logs_clusters/get.h>
|
||||
#include <utils/utils.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();
|
||||
|
||||
json result = json::array();
|
||||
for (auto& cluster : clusters) {
|
||||
json item = json::object();
|
||||
item[fields::id] = cluster.id;
|
||||
item[fields::name] = std::move(cluster.name);
|
||||
item[fields::description] = std::move(cluster.description);
|
||||
item[fields::recommendation] = std::move(cluster.recommendation);
|
||||
|
||||
result.push_back(std::move(item));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#include "query.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include <storage/postgresql/postgresql_storage.h>
|
||||
|
||||
namespace controllers::statistics::details {
|
||||
std::vector<Statistic> GetStatistics() {
|
||||
std::ostringstream query;
|
||||
query << "select name, value from statistics;";
|
||||
|
||||
auto pqxx_result = PostgreSqlStorage::Get().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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace controllers::statistics::details {
|
||||
struct Statistic {
|
||||
std::string name;
|
||||
size_t value;
|
||||
};
|
||||
|
||||
std::vector<Statistic> GetStatistics();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <controllers/statistics/get.h>
|
||||
|
||||
#include "details/query.h"
|
||||
|
||||
using namespace controllers::statistics;
|
||||
|
||||
json Get::operator()(const json& parameters) const {
|
||||
auto statistics = details::GetStatistics();
|
||||
|
||||
json result;
|
||||
for (auto& statistic : statistics) {
|
||||
result[std::move(statistic.name)] = statistic.value;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user