Compare commits

...

2 Commits

Author SHA1 Message Date
Mars Ultor
c1e8ddb41a clickhouse connection operational 2025-12-23 23:32:40 -06:00
Mars Ultor
c3e0b7c27b database utils written 2025-12-23 23:25:44 -06:00
3 changed files with 112 additions and 1 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,15 +1,26 @@
#include "clickhouse/client.h"
#include "spdlog/spdlog.h"
#include <memory>
#include <string>
#include <sys/types.h>
#include <toml++/toml.h>
int main (int argc, char *argv[]) {
auto config = toml::parse_file("config.toml");
bool ingest_all_logs = config["all"].value_or(false);
std::string user_id = config["user_id"].value_or("kid");
std::string log_path = config["minecraft"]["logs_path"].value_or("");
if(log_path.empty()) throw std::runtime_error("your dumbass can't put in a path");
std::string clickhouse_username = config["clickhouse"]["username"].value_or("");
std::string clickouse_password = config["clickhouse"]["password"].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");
std::string clickhouse_host = config["clickhouse"]["host"].value_or("");
uint clickhouse_port = config["clickhouse"]["port"].value_or(9000);
if(clickhouse_username.empty() || clickouse_password.empty() || clickhouse_db.empty() || clickhouse_host.empty()) throw std::runtime_error("put the creds in the right place idiot");
spdlog::info("Logging is functional");
std::shared_ptr<clickhouse::Client> shared_client = std::make_shared<clickhouse::Client>(clickhouse::ClientOptions().SetPassword(clickouse_password).SetDefaultDatabase(clickhouse_db).SetUser(clickhouse_username).SetHost(clickhouse_host).SetPort(clickhouse_port));
spdlog::info("Server connection: {}", shared_client->GetServerInfo().display_name);
return 0;
}