Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Added metrics improvement #36

Merged
merged 5 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ AMetrics_DB_Controller::AMetrics_DB_Controller(const ADB_Config& db_cfg) :
// ---------------------------------------------------------------------------------------------------------------------
void AMetrics_DB_Controller::Set_Metrics(const SMetrics& metrics)
{
Exec_Query(std::format(R"(INSERT INTO {} (start_req, profile_req, pr_game_req, math_game_req, help_req, about_project_req, definition_req, date) VALUES ('{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}'))", Table_Name, metrics.Start_Request_Count, metrics.Profile_Request_Count, metrics.Pr_Game_Request_Count, metrics.Math_Game_Request_Count, metrics.Help_Request_Count, metrics.About_Project_Request_Count, metrics.Definition_Request_Count, metrics.Get_Current_Date()));
Exec_Query(std::format(R"(INSERT INTO {} (start_req, profile_req, pr_game_req, math_game_req, help_req, about_project_req, definition_req, date, total_number_of_requests) VALUES ('{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}'))", Table_Name, metrics.Start_Request_Count, metrics.Profile_Request_Count, metrics.Pr_Game_Request_Count, metrics.Math_Game_Request_Count, metrics.Help_Request_Count, metrics.About_Project_Request_Count, metrics.Definition_Request_Count, metrics.Get_Current_Date(), metrics.Get_Total_Number_Of_Requests()));
}

// ---------------------------------------------------------------------------------------------------------------------
Expand All @@ -42,8 +42,15 @@ SMetrics AMetrics_DB_Controller::Get_Metrics(std::int64_t metrics_id)
}

// ---------------------------------------------------------------------------------------------------------------------
std::int64_t AMetrics_DB_Controller::Get_Available_Metrics_Count()
std::pair<std::int64_t, std::int64_t> AMetrics_DB_Controller::Get_Metrics_Range()
{
auto response{ Exec_Query(std::format(R"(SELECT MAX(id) FROM {})", Table_Name)) };
return response[0][0].as<std::int64_t>();
auto response{ Exec_Query(std::format(R"(SELECT MIN(id) AS min_val, MAX(id) AS max_val FROM {})", Table_Name)) };
return { response[0]["min_val"].as<std::int64_t>(), response[0]["max_val"].as<std::int64_t>() };
}

// ---------------------------------------------------------------------------------------------------------------------
SMetrics AMetrics_DB_Controller::Get_Best_Metrics()
{
auto response{ Exec_Query(std::format(R"(SELECT start_req, profile_req, pr_game_req, math_game_req, help_req, about_project_req, definition_req, date FROM {} WHERE total_number_of_requests = (SELECT MAX(total_number_of_requests) FROM {}))", Table_Name, Table_Name)) };
return SMetrics{ response[0][0].as<std::int64_t>(), response[0][1].as<std::int64_t>(), response[0][2].as<std::int64_t>(), response[0][3].as<std::int64_t>(), response[0][4].as<std::int64_t>(), response[0][5].as<std::int64_t>(), response[0][6].as<std::int64_t>(), response[0][7].as<std::string>() };
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#pragma once

#include <utility>

#include <controller/db/base_db/base_db_controller.hpp>
#include <model/metrics/metrics_model.hpp>

Expand All @@ -34,5 +36,8 @@ class AMetrics_DB_Controller : public ABase_DB_Controller

void Set_Metrics(const SMetrics& metrics);
SMetrics Get_Metrics(std::int64_t metrics_id);
std::int64_t Get_Available_Metrics_Count();

// return min id and max id
std::pair<std::int64_t, std::int64_t> Get_Metrics_Range();
SMetrics Get_Best_Metrics();
};
6 changes: 6 additions & 0 deletions source/business_logic/model/metrics/metrics_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ const std::string& SMetrics::Get_Current_Date() const
return Current_Date;
}

// ---------------------------------------------------------------------------------------------------------------------
std::int64_t SMetrics::Get_Total_Number_Of_Requests() const
{
return Start_Request_Count + Profile_Request_Count + Pr_Game_Request_Count + Math_Game_Request_Count + Help_Request_Count + About_Project_Request_Count + Definition_Request_Count;
}

// ---------------------------------------------------------------------------------------------------------------------
void SMetrics::Clear()
{
Expand Down
1 change: 1 addition & 0 deletions source/business_logic/model/metrics/metrics_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct SMetrics
void Set_Current_Date();
[[nodiscard]] const std::string& Get_Current_Date() const;

[[nodiscard]] std::int64_t Get_Total_Number_Of_Requests() const;
void Clear();

public:
Expand Down
21 changes: 18 additions & 3 deletions source/view/handlers/message_handler/message_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,39 @@ void AMessage_Handler::Bind_Commands()

TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Not_Found_Word_Definition_Msg());
});
TG_Bot.getEvents().onCommand(SMessage_Commands::Metrics_Count.data(), [this](TgBot::Message::Ptr message) -> void
TG_Bot.getEvents().onCommand(SMessage_Commands::Metrics_Range.data(), [this](TgBot::Message::Ptr message) -> void
{
if (!Is_Root_User(message->from->id))
{
return;
}

auto metrics_count{ Metrics_DB_Controller.Get_Available_Metrics_Count() };
TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Metrics_Count_Msg(metrics_count));
auto metrics_range{ Metrics_DB_Controller.Get_Metrics_Range() };
TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Metrics_Range_Msg(metrics_range));
});
TG_Bot.getEvents().onCommand(SMessage_Commands::Get_Metrics.data(), [this](TgBot::Message::Ptr message) -> void
{
if (!Is_Root_User(message->from->id))
{
return;
}

Cut_User_Input(message->text, SMessage_Commands::Get_Metrics.size());
std::int64_t metrics_id{ strtoll(message->text.c_str(), nullptr, 10) };

auto metrics{ Metrics_DB_Controller.Get_Metrics(metrics_id) };
TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Metrics_Msg(metrics));
});
TG_Bot.getEvents().onCommand(SMessage_Commands::Get_Best_Metric.data(), [this](TgBot::Message::Ptr message) -> void
{
if (!Is_Root_User(message->from->id))
{
return;
}

auto best_metric{ Metrics_DB_Controller.Get_Best_Metrics() };
TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Metrics_Msg(best_metric));
});
}

// ---------------------------------------------------------------------------------------------------------------------
Expand Down
5 changes: 3 additions & 2 deletions source/view/texts/message_commands/message_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ constinit std::string_view SMessage_Commands::Answer{ "answer" };
constinit std::string_view SMessage_Commands::Help{ "help" };
constinit std::string_view SMessage_Commands::About_Project{ "about_project" };
constinit std::string_view SMessage_Commands::Definiton{ "def" };
constinit std::string_view SMessage_Commands::Metrics_Count{ "metrics_count" };
constinit std::string_view SMessage_Commands::Get_Metrics{ "get_metrics" };
constinit std::string_view SMessage_Commands::Metrics_Range{"metrics_range" };
constinit std::string_view SMessage_Commands::Get_Metrics{ "get_metrics" };
constinit std::string_view SMessage_Commands::Get_Best_Metric{ "get_best_metric" };
3 changes: 2 additions & 1 deletion source/view/texts/message_commands/message_commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct SMessage_Commands
constinit static std::string_view Help;
constinit static std::string_view About_Project;
constinit static std::string_view Definiton;
constinit static std::string_view Metrics_Count;
constinit static std::string_view Metrics_Range;
constinit static std::string_view Get_Metrics;
constinit static std::string_view Get_Best_Metric;
};
7 changes: 4 additions & 3 deletions source/view/texts/message_reply/message_reply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,18 @@ std::string AMessage_Reply::Get_Not_Found_Word_Definition_Msg()
}

// ---------------------------------------------------------------------------------------------------------------------
std::string AMessage_Reply::Get_Metrics_Count_Msg(std::int64_t metrics_count)
std::string AMessage_Reply::Get_Metrics_Range_Msg(const std::pair<std::int64_t, std::int64_t>& metrics_range)
{
return std::format("Кількість доступних метрик: {}", metrics_count);
return std::format("Діапазон ID метрик: від {} до {}", metrics_range.first, metrics_range.second);
}

// ---------------------------------------------------------------------------------------------------------------------
std::string AMessage_Reply::Get_Metrics_Msg(const SMetrics& metrics)
{
return std::format("Метрики Arbuz-Bot\n\nДата: {}\n\nКількість реквестів на команди:\nStart: {}\nProfile: {}\nProgrammer Game: {}\nMath Game: {}\nHelp: {}\nAbout Project: {}\nDefinition: {}", metrics.Get_Current_Date(), metrics.Start_Request_Count, metrics.Profile_Request_Count, metrics.Pr_Game_Request_Count, metrics.Math_Game_Request_Count, metrics.Help_Request_Count, metrics.About_Project_Request_Count, metrics.Definition_Request_Count);
return std::format("Метрики Arbuz-Bot\n\nДата: {}\n\nЗагальна кількість реквестів: {}\n\nКількість реквестів на команди:\nStart: {}\nProfile: {}\nProgrammer Game: {}\nMath Game: {}\nHelp: {}\nAbout Project: {}\nDefinition: {}", metrics.Get_Current_Date(), metrics.Get_Total_Number_Of_Requests(), metrics.Start_Request_Count, metrics.Profile_Request_Count, metrics.Pr_Game_Request_Count, metrics.Math_Game_Request_Count, metrics.Help_Request_Count, metrics.About_Project_Request_Count, metrics.Definition_Request_Count);
}

// ---------------------------------------------------------------------------------------------------------------------
std::string AMessage_Reply::Get_Limit_Api_Requests_Msg()
{
return std::string{ "Нажаль ліміт запросів перевищено!\nСпробуйте пізніше)" };
Expand Down
3 changes: 2 additions & 1 deletion source/view/texts/message_reply/message_reply.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <string>
#include <cstdint>
#include <format>
#include <utility>

#include <emoji.hpp>

Expand All @@ -51,7 +52,7 @@ class AMessage_Reply
static std::string Get_Info_About_Project_Msg();
static std::string Get_Word_Definition_Msg(std::string_view primary_word, std::string_view definiton);
static std::string Get_Not_Found_Word_Definition_Msg();
static std::string Get_Metrics_Count_Msg(std::int64_t metrics_count);
static std::string Get_Metrics_Range_Msg(const std::pair<std::int64_t, std::int64_t>& metrics_range);
static std::string Get_Metrics_Msg(const SMetrics& metrics);
static std::string Get_Limit_Api_Requests_Msg();
};
Loading