Обновление слоя хранилища
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
add_subdirectory(configuration)
|
|
||||||
add_subdirectory(controllers)
|
add_subdirectory(controllers)
|
||||||
add_subdirectory(json)
|
add_subdirectory(json)
|
||||||
add_subdirectory(server)
|
add_subdirectory(server)
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
# Модуль для работы с конфигурационными файлами
|
|
||||||
project(la_configuration)
|
|
||||||
|
|
||||||
# Собираем исходные файлы
|
|
||||||
file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
|
|
||||||
|
|
||||||
# Создаём библиотеку
|
|
||||||
add_library(${PROJECT_NAME} ${SOURCES})
|
|
||||||
|
|
||||||
# Указываем директории с заголовочными файлами
|
|
||||||
target_include_directories(${PROJECT_NAME} PUBLIC include)
|
|
||||||
|
|
||||||
# Подключаем зависимости
|
|
||||||
target_link_libraries(${PROJECT_NAME}
|
|
||||||
la_json
|
|
||||||
)
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include <json/json.h>
|
|
||||||
|
|
||||||
/// @brief Парсинг конфигурационного файла
|
|
||||||
/// @param[in] path_to_configuration Путь к конфигурационному файлу
|
|
||||||
/// @param[in] required_keys Список обязательных ключей в формате { "key1": ["subkey1", ...], "key2": ... }
|
|
||||||
/// @returns Конфигурация в формате json
|
|
||||||
json ParseConfiguration(const std::string& path_to_configuration, const json& required_keys);
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
# Модуль configuration
|
|
||||||
Конфигурационный файл должен иметь следующий формат:
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"key1": {
|
|
||||||
"subkey1": "value1",
|
|
||||||
"subkey2": "value2",
|
|
||||||
...
|
|
||||||
},
|
|
||||||
"key2": {
|
|
||||||
"subkey1": "value1",
|
|
||||||
...
|
|
||||||
},
|
|
||||||
...
|
|
||||||
}
|
|
||||||
```
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
#include <fstream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
#include <configuration/configuration.h>
|
|
||||||
|
|
||||||
json ParseConfiguration(const std::string& path_to_configuration, const json& required_keys) {
|
|
||||||
std::ifstream input(path_to_configuration);
|
|
||||||
auto configuration = json::parse(input);
|
|
||||||
|
|
||||||
for (const auto& [key, value] : required_keys.items()) {
|
|
||||||
// Проверяем наличие ключа на "нулевом" уровне
|
|
||||||
if (!configuration.contains(key)) {
|
|
||||||
std::ostringstream message;
|
|
||||||
message << "configuration doesn't contain required key '" << key << "'";
|
|
||||||
throw std::runtime_error(message.str());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Проверяем наличие вложенных ключей
|
|
||||||
auto configuration_value = configuration[key];
|
|
||||||
|
|
||||||
for (const auto& item : value) {
|
|
||||||
if (!configuration_value.contains(item)) {
|
|
||||||
std::ostringstream message;
|
|
||||||
message << "configuration doesn't contain required key '" << item << "' in '" << key << "'";
|
|
||||||
throw std::runtime_error(message.str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return configuration;
|
|
||||||
}
|
|
||||||
@@ -2,14 +2,16 @@
|
|||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#include <storage/postgresql/postgresql_storage.h>
|
#include <storage/transaction.h>
|
||||||
|
|
||||||
namespace controllers::anomaly_logs::details {
|
namespace controllers::anomaly_logs::details {
|
||||||
std::vector<AnomalyLog> GetAnomalyLogs(int64_t last_id) {
|
std::vector<AnomalyLog> GetAnomalyLogs(int64_t last_id) {
|
||||||
|
auto transaction = storage::Transaction::Create();
|
||||||
|
|
||||||
std::ostringstream query;
|
std::ostringstream query;
|
||||||
query << "select id, cluster_id, data from anomaly_logs where id > " << last_id << ";";
|
query << "select id, cluster_id, data from anomaly_logs where id > " << last_id << ";";
|
||||||
|
|
||||||
auto pqxx_result = PostgreSqlStorage::Get().Do(query.str());
|
auto pqxx_result = transaction.Do(query.str());
|
||||||
|
|
||||||
std::vector<AnomalyLog> result;
|
std::vector<AnomalyLog> result;
|
||||||
result.reserve(pqxx_result.size());
|
result.reserve(pqxx_result.size());
|
||||||
|
|||||||
@@ -5,9 +5,7 @@
|
|||||||
|
|
||||||
namespace controllers::anomaly_logs::details {
|
namespace controllers::anomaly_logs::details {
|
||||||
struct AnomalyLog {
|
struct AnomalyLog {
|
||||||
int64_t id;
|
|
||||||
int64_t class_id;
|
|
||||||
std::string data;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<AnomalyLog> GetAnomalyLogs(int64_t last_id);
|
std::vector<AnomalyLog> GetAnomalyLogs(int64_t last_id);
|
||||||
|
|||||||
@@ -2,14 +2,16 @@
|
|||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#include <storage/postgresql/postgresql_storage.h>
|
#include <storage/transaction.h>
|
||||||
|
|
||||||
namespace controllers::logs_clusters::details {
|
namespace controllers::logs_clusters::details {
|
||||||
std::vector<LogsCluster> GetLogsClusters() {
|
std::vector<LogsCluster> GetLogsClusters() {
|
||||||
|
auto transaction = storage::Transaction::Create();
|
||||||
|
|
||||||
std::ostringstream query;
|
std::ostringstream query;
|
||||||
query << "select id, name, description, recommendation from logs_clusters;";
|
query << "select id, name, description, recommendation from logs_clusters;";
|
||||||
|
|
||||||
auto pqxx_result = PostgreSqlStorage::Get().Do(query.str());
|
auto pqxx_result = transaction.Do(query.str());
|
||||||
|
|
||||||
std::vector<LogsCluster> result;
|
std::vector<LogsCluster> result;
|
||||||
result.reserve(pqxx_result.size());
|
result.reserve(pqxx_result.size());
|
||||||
|
|||||||
@@ -2,14 +2,16 @@
|
|||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#include <storage/postgresql/postgresql_storage.h>
|
#include <storage/transaction.h>
|
||||||
|
|
||||||
namespace controllers::statistics::details {
|
namespace controllers::statistics::details {
|
||||||
std::vector<Statistic> GetStatistics() {
|
std::vector<Statistic> GetStatistics() {
|
||||||
|
auto transaction = storage::Transaction::Create();
|
||||||
|
|
||||||
std::ostringstream query;
|
std::ostringstream query;
|
||||||
query << "select name, value from statistics;";
|
query << "select name, value from statistics;";
|
||||||
|
|
||||||
auto pqxx_result = PostgreSqlStorage::Get().Do(query.str());
|
auto pqxx_result = transaction.Do(query.str());
|
||||||
|
|
||||||
std::vector<Statistic> result;
|
std::vector<Statistic> result;
|
||||||
result.reserve(pqxx_result.size());
|
result.reserve(pqxx_result.size());
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#include "storage_wrapper.h"
|
||||||
|
|
||||||
|
#include <storage/storage.h>
|
||||||
|
|
||||||
|
using namespace server::details;
|
||||||
|
|
||||||
|
StorageWrapper::StorageWrapper(const userver::components::ComponentConfig& config,
|
||||||
|
const userver::components::ComponentContext& context)
|
||||||
|
: userver::components::LoggableComponentBase(config, context) {
|
||||||
|
auto settings = config[kName];
|
||||||
|
|
||||||
|
storage::ConnectionData connection_data;
|
||||||
|
connection_data.database = settings["database"].As<std::string>();
|
||||||
|
connection_data.username = settings["username"].As<std::string>();
|
||||||
|
connection_data.password = settings["password"].As<std::string>();
|
||||||
|
connection_data.host = settings["host"].As<std::string>();
|
||||||
|
connection_data.port = settings["port"].As<std::string>();
|
||||||
|
|
||||||
|
storage::Storage::Connect(connection_data);
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <userver/components/component.hpp>
|
||||||
|
#include <userver/components/loggable_component_base.hpp>
|
||||||
|
|
||||||
|
namespace server::details {
|
||||||
|
/// @brief Обёртка-компонент над хранилищем
|
||||||
|
class StorageWrapper : public userver::components::LoggableComponentBase {
|
||||||
|
public:
|
||||||
|
/// @brief Название компонента
|
||||||
|
static constexpr std::string_view kName = "storage-wrapper";
|
||||||
|
|
||||||
|
/// @brief Конструктор
|
||||||
|
/// @param[in] config Конфигурация
|
||||||
|
/// @param[in] context Контекст
|
||||||
|
StorageWrapper(const userver::components::ComponentConfig& config, const userver::components::ComponentContext& context);
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -11,42 +11,16 @@
|
|||||||
#include <controllers/statistics/get.h>
|
#include <controllers/statistics/get.h>
|
||||||
|
|
||||||
#include <configuration/configuration.h>
|
#include <configuration/configuration.h>
|
||||||
#include <json/json.h>
|
|
||||||
#include <server/logger.h>
|
#include <server/logger.h>
|
||||||
#include <storage/postgresql/postgresql_storage.h>
|
|
||||||
|
|
||||||
#include "details/controller_wrapper.h"
|
#include "details/controller_wrapper.h"
|
||||||
|
#include "details/storage_wrapper.h"
|
||||||
#include "details/details.h"
|
#include "details/details.h"
|
||||||
|
|
||||||
void Log(LogLevel level, const char* message) {
|
void Log(LogLevel level, const char* message) {
|
||||||
std::cout << message << std::endl;
|
std::cout << message << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectToStorage(const json& configuration) {
|
|
||||||
Logger::Get().Write(LogLevel::Info, "connecting to postgresql...");
|
|
||||||
|
|
||||||
const auto& parameters = configuration["storage"];
|
|
||||||
|
|
||||||
ConnectionData connection_data;
|
|
||||||
connection_data.database = parameters["database"].get<std::string>();
|
|
||||||
connection_data.username = parameters["username"].get<std::string>();
|
|
||||||
connection_data.password = parameters["password"].get<std::string>();
|
|
||||||
connection_data.host = parameters["host"].get<std::string>();
|
|
||||||
connection_data.port = std::to_string(parameters["port"].get<int>());
|
|
||||||
|
|
||||||
PostgreSqlStorage::Connect(connection_data);
|
|
||||||
|
|
||||||
Logger::Get().Write(LogLevel::Info, "connected to postgresql");
|
|
||||||
}
|
|
||||||
|
|
||||||
json ParseConfiguration(const std::string& path_to_configuration) {
|
|
||||||
json required_keys;
|
|
||||||
required_keys["server"] = { "path", "port" };
|
|
||||||
required_keys["storage"] = { "database", "username", "password", "host", "port" };
|
|
||||||
|
|
||||||
return ParseConfiguration(path_to_configuration, required_keys);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
std::cout << "usage: " << argv[0] << " <path-to-configuration>" << std::endl;
|
std::cout << "usage: " << argv[0] << " <path-to-configuration>" << std::endl;
|
||||||
@@ -66,6 +40,8 @@ int main(int argc, char** argv) {
|
|||||||
// static content
|
// static content
|
||||||
.Append<userver::components::FsCache>("fs-cache-main")
|
.Append<userver::components::FsCache>("fs-cache-main")
|
||||||
.Append<userver::server::handlers::HttpHandlerStatic>()
|
.Append<userver::server::handlers::HttpHandlerStatic>()
|
||||||
|
// storage
|
||||||
|
.Append<server::details::StorageWrapper>()
|
||||||
// anomaly logs
|
// anomaly logs
|
||||||
.Append<server::details::ControllerWrapper<controllers::anomaly_logs::Get>>()
|
.Append<server::details::ControllerWrapper<controllers::anomaly_logs::Get>>()
|
||||||
// clusters
|
// clusters
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#include <pqxx/pqxx>
|
|
||||||
|
|
||||||
struct ConnectionData {
|
|
||||||
std::string database, username, password, host, port;
|
|
||||||
};
|
|
||||||
|
|
||||||
class PostgreSqlStorage {
|
|
||||||
public:
|
|
||||||
static PostgreSqlStorage& Get();
|
|
||||||
static void Connect(const ConnectionData& connection_data);
|
|
||||||
pqxx::result Do(std::string_view query) const;
|
|
||||||
private:
|
|
||||||
std::unique_ptr<pqxx::connection> connection_;
|
|
||||||
};
|
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include <pqxx/pqxx>
|
||||||
|
|
||||||
|
#include <storage/transaction.h>
|
||||||
|
|
||||||
|
namespace storage {
|
||||||
|
/// @brief Данные подключения
|
||||||
|
struct ConnectionData {
|
||||||
|
std::string database, username, password, host, port;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Storage {
|
||||||
|
public:
|
||||||
|
static Storage& Get();
|
||||||
|
|
||||||
|
static void Connect(const ConnectionData& connection_data);
|
||||||
|
|
||||||
|
friend class Transaction;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unique_ptr<pqxx::connection> connection_;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#include <pqxx/pqxx>
|
||||||
|
|
||||||
|
namespace storage {
|
||||||
|
class Transaction {
|
||||||
|
public:
|
||||||
|
~Transaction();
|
||||||
|
|
||||||
|
Transaction(Transaction&&) = default;
|
||||||
|
Transaction& operator=(Transaction&&) = default;
|
||||||
|
|
||||||
|
static Transaction Create();
|
||||||
|
|
||||||
|
pqxx::result Do(std::string_view query) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Transaction() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unique_ptr<pqxx::work> transaction_;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
#include <sstream>
|
|
||||||
|
|
||||||
#include <storage/postgresql/postgresql_storage.h>
|
|
||||||
|
|
||||||
PostgreSqlStorage& PostgreSqlStorage::Get() {
|
|
||||||
static PostgreSqlStorage storage;
|
|
||||||
return storage;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PostgreSqlStorage::Connect(const ConnectionData& connection_data) {
|
|
||||||
std::ostringstream options;
|
|
||||||
options << "dbname = " << connection_data.database
|
|
||||||
<< " user = " << connection_data.username
|
|
||||||
<< " password = " << connection_data.password
|
|
||||||
<< " host = " << connection_data.host
|
|
||||||
<< " port = " << connection_data.port;
|
|
||||||
|
|
||||||
Get().connection_ = std::make_unique<pqxx::connection>(options.str());
|
|
||||||
}
|
|
||||||
|
|
||||||
pqxx::result PostgreSqlStorage::Do(std::string_view query) const {
|
|
||||||
// Создаём транзакцию
|
|
||||||
pqxx::work work(*connection_);
|
|
||||||
|
|
||||||
// Выполняем запрос
|
|
||||||
pqxx::result result = work.exec(query);
|
|
||||||
|
|
||||||
// Завершаем транзакцию
|
|
||||||
work.commit();
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
#include <storage/storage.h>
|
||||||
|
|
||||||
|
using namespace storage;
|
||||||
|
|
||||||
|
Storage& Storage::Get() {
|
||||||
|
static Storage storage;
|
||||||
|
return storage;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Storage::Connect(const ConnectionData& connection_data) {
|
||||||
|
std::ostringstream options;
|
||||||
|
options << "dbname = " << connection_data.database
|
||||||
|
<< " user = " << connection_data.username
|
||||||
|
<< " password = " << connection_data.password
|
||||||
|
<< " host = " << connection_data.host
|
||||||
|
<< " port = " << connection_data.port;
|
||||||
|
|
||||||
|
Get().connection_ = std::make_unique<pqxx::connection>(options.str());
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
#include <storage/storage.h>
|
||||||
|
#include <storage/transaction.h>
|
||||||
|
|
||||||
|
using namespace storage;
|
||||||
|
|
||||||
|
Transaction::~Transaction() {
|
||||||
|
transaction_->commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
Transaction Transaction::Create() {
|
||||||
|
auto& storage = storage::Storage::Get();
|
||||||
|
|
||||||
|
Transaction transaction;
|
||||||
|
transaction.transaction_ = std::make_unique<pqxx::work>(*storage.connection_);
|
||||||
|
|
||||||
|
return transaction;
|
||||||
|
}
|
||||||
|
|
||||||
|
pqxx::result Transaction::Do(std::string_view query) const {
|
||||||
|
return transaction_->exec(query);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user