diff --git a/src/database_utils.cpp b/src/database_utils.cpp new file mode 100644 index 0000000..8305e38 --- /dev/null +++ b/src/database_utils.cpp @@ -0,0 +1,56 @@ +#include "database_utils.hpp" +#include "clickhouse/block.h" +#include "clickhouse/client.h" +#include +#include +#include +#include + +namespace db_utils { + + void ClientEventsBatch::insert_as_batch(std::shared_ptr client){ + clickhouse::Block block = clickhouse::Block {}; + block.AppendColumn("user_id",user_id_col); + block.AppendColumn("timestamp", timestamp_col); + block.AppendColumn("event_type", event_type_col); + block.AppendColumn("log_source", log_source_col); + + client->Insert("mc_client_events", block); + } + + void ClientEventsBatch::add_row(std::string user_id, std::time_t timestamp, std::variant event_type, std::string log_source){ + user_id_col->Append(user_id); timestamp_col->Append(timestamp); log_source_col->Append(log_source); + if(std::holds_alternative(event_type)){ + event_type_col->Append(std::get(event_type)); + } + else if(std::holds_alternative(event_type)){ + event_type_col->Append(std::get(event_type)); + } + } + + void ChatEventsBatch::insert_as_batch(std::shared_ptr client){ + clickhouse::Block block = clickhouse::Block {}; + block.AppendColumn("user_id", user_id_col); block.AppendColumn("timestamp", timestamp_col); + block.AppendColumn("sender", sender_col); block.AppendColumn("message", message_col); + client->Insert("mc_chat_events", block); + } + void ChatEventsBatch::add_row(std::string user_id, std::time_t timestamp, std::string sender, std::string message){ + user_id_col->Append(user_id); timestamp_col->Append(timestamp); sender_col->Append(sender); message_col->Append(message); + } + void ActivityEventsBatch::insert_as_batch(std::shared_ptr client){ + clickhouse::Block block = clickhouse::Block {}; + block.AppendColumn("user_id", user_id_col); block.AppendColumn("timestamp", timestamp_col); + block.AppendColumn("confidence", confidence_col); + block.AppendColumn("event_type", event_type_col); + client->Insert("mc_activity_events", block); + } + void ActivityEventsBatch::add_row(std::string user_id, std::time_t timestamp, std::variant event_type, float confidence){ + user_id_col->Append(user_id); timestamp_col->Append(timestamp); confidence_col->Append(confidence); + if(std::holds_alternative(event_type)){ + event_type_col->Append(std::get(event_type)); + } + else if(std::holds_alternative(event_type)){ + event_type_col->Append(std::get(event_type)); + } + } +} diff --git a/src/database_utils.hpp b/src/database_utils.hpp new file mode 100644 index 0000000..b60487a --- /dev/null +++ b/src/database_utils.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include "clickhouse/client.h" +#include "clickhouse/columns/date.h" +#include "clickhouse/columns/enum.h" +#include "clickhouse/columns/numeric.h" +#include "clickhouse/columns/string.h" +#include "clickhouse/types/types.h" +#include +#include +#include +#include +#include +#include +namespace db_utils { + + struct ClientEventsBatch{ + std::shared_ptr user_id_col = std::make_shared(); + std::shared_ptr timestamp_col = std::make_shared(); + std::shared_ptr event_type_col = std::make_shared(clickhouse::Type::CreateEnum8({ {"start", 1}, {"end", 2} })); + std::shared_ptr log_source_col = std::make_shared(); + void insert_as_batch(std::shared_ptr client); + void add_row(std::string user_id, std::time_t timestamp, std::variant event_type, std::string log_source); + }; + + struct ChatEventsBatch{ + std::shared_ptr user_id_col = std::make_shared(); + std::shared_ptr timestamp_col = std::make_shared(); + std::shared_ptr sender_col = std::make_shared(); + std::shared_ptr message_col = std::make_shared(); + void insert_as_batch(std::shared_ptr client); + void add_row(std::string user_id, std::time_t timestamp, std::string sender, std::string message); + }; + + struct ActivityEventsBatch{ + std::shared_ptr user_id_col = std::make_shared(); + std::shared_ptr timestamp_col = std::make_shared(); + std::shared_ptr event_type_col = std::make_shared(clickhouse::Type::CreateEnum8({ {"command", 1}, {"death", 2}, {"dimension", 3}, {"other", 4} })); + + std::shared_ptr confidence_col = std::make_shared(); + void insert_as_batch(std::shared_ptr client); + void add_row(std::string user_id, std::time_t timestamp, std::variant event_type, float confidence); + }; +} diff --git a/src/main.cpp b/src/main.cpp index bb39370..c4f6a38 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#include "spdlog/spdlog.h" #include #include @@ -11,5 +12,6 @@ int main (int argc, char *argv[]) { std::string clickhouse_db = config["clickhouse"]["db"].value_or(""); if(clickhouse_username.empty() || clickouse_password.empty() || clickhouse_db.empty()) throw std::runtime_error("put the creds in the right place idiot"); + spdlog::info("Logging is functional"); return 0; }