database utils written

This commit is contained in:
2025-12-23 23:25:44 -06:00
parent 50cb5c6473
commit c3e0b7c27b
3 changed files with 102 additions and 0 deletions

56
src/database_utils.cpp Normal file
View File

@@ -0,0 +1,56 @@
#include "database_utils.hpp"
#include "clickhouse/block.h"
#include "clickhouse/client.h"
#include <cstdint>
#include <filesystem>
#include <string>
#include <variant>
namespace db_utils {
void ClientEventsBatch::insert_as_batch(std::shared_ptr<clickhouse::Client> 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<std::string, uint8_t> 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<std::string>(event_type)){
event_type_col->Append(std::get<std::string>(event_type));
}
else if(std::holds_alternative<uint8_t>(event_type)){
event_type_col->Append(std::get<uint8_t>(event_type));
}
}
void ChatEventsBatch::insert_as_batch(std::shared_ptr<clickhouse::Client> 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<clickhouse::Client> 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<std::string, uint8_t> event_type, float confidence){
user_id_col->Append(user_id); timestamp_col->Append(timestamp); confidence_col->Append(confidence);
if(std::holds_alternative<std::string>(event_type)){
event_type_col->Append(std::get<std::string>(event_type));
}
else if(std::holds_alternative<uint8_t>(event_type)){
event_type_col->Append(std::get<uint8_t>(event_type));
}
}
}

44
src/database_utils.hpp Normal file
View File

@@ -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 <cstdint>
#include <ctime>
#include <memory>
#include <string>
#include <variant>
#include <vector>
namespace db_utils {
struct ClientEventsBatch{
std::shared_ptr<clickhouse::ColumnString> user_id_col = std::make_shared<clickhouse::ColumnString>();
std::shared_ptr<clickhouse::ColumnDateTime> timestamp_col = std::make_shared<clickhouse::ColumnDateTime>();
std::shared_ptr<clickhouse::ColumnEnum8> event_type_col = std::make_shared<clickhouse::ColumnEnum8>(clickhouse::Type::CreateEnum8({ {"start", 1}, {"end", 2} }));
std::shared_ptr<clickhouse::ColumnString> log_source_col = std::make_shared<clickhouse::ColumnString>();
void insert_as_batch(std::shared_ptr<clickhouse::Client> client);
void add_row(std::string user_id, std::time_t timestamp, std::variant<std::string, uint8_t> event_type, std::string log_source);
};
struct ChatEventsBatch{
std::shared_ptr<clickhouse::ColumnString> user_id_col = std::make_shared<clickhouse::ColumnString>();
std::shared_ptr<clickhouse::ColumnDateTime> timestamp_col = std::make_shared<clickhouse::ColumnDateTime>();
std::shared_ptr<clickhouse::ColumnString> sender_col = std::make_shared<clickhouse::ColumnString>();
std::shared_ptr<clickhouse::ColumnString> message_col = std::make_shared<clickhouse::ColumnString>();
void insert_as_batch(std::shared_ptr<clickhouse::Client> client);
void add_row(std::string user_id, std::time_t timestamp, std::string sender, std::string message);
};
struct ActivityEventsBatch{
std::shared_ptr<clickhouse::ColumnString> user_id_col = std::make_shared<clickhouse::ColumnString>();
std::shared_ptr<clickhouse::ColumnDateTime> timestamp_col = std::make_shared<clickhouse::ColumnDateTime>();
std::shared_ptr<clickhouse::ColumnEnum8> event_type_col = std::make_shared<clickhouse::ColumnEnum8>(clickhouse::Type::CreateEnum8({ {"command", 1}, {"death", 2}, {"dimension", 3}, {"other", 4} }));
std::shared_ptr<clickhouse::ColumnFloat32> confidence_col = std::make_shared<clickhouse::ColumnFloat32>();
void insert_as_batch(std::shared_ptr<clickhouse::Client> client);
void add_row(std::string user_id, std::time_t timestamp, std::variant<std::string, uint8_t> event_type, float confidence);
};
}

View File

@@ -1,3 +1,4 @@
#include "spdlog/spdlog.h"
#include <string> #include <string>
#include <toml++/toml.h> #include <toml++/toml.h>
@@ -11,5 +12,6 @@ int main (int argc, char *argv[]) {
std::string clickhouse_db = config["clickhouse"]["db"].value_or(""); 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"); 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; return 0;
} }