From bdf467c3973a7cdec3d627ec79c22eb3ec81bafe Mon Sep 17 00:00:00 2001 From: b1tflyyyy Date: Fri, 3 May 2024 14:32:37 -0400 Subject: [PATCH 1/9] update cmake --- source/business_logic/CMakeLists.txt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/source/business_logic/CMakeLists.txt b/source/business_logic/CMakeLists.txt index 517b939..52966b2 100644 --- a/source/business_logic/CMakeLists.txt +++ b/source/business_logic/CMakeLists.txt @@ -32,6 +32,8 @@ set(MODEL_HEADERS model/config/tg_config/tg_config_model.hpp model/math_problem_game/math_problem_game_expression_model.hpp model/config/english_words_info_api_config/english_words_info_api_config_model.hpp + model/metrics/metrics_model.hpp + model/config/tg_root_user_config/tg_root_user_config_model.hpp ) set(MODEL_SOURCES @@ -41,6 +43,8 @@ set(MODEL_SOURCES model/config/tg_config/tg_config_model.cpp model/math_problem_game/math_problem_game_expression_model.cpp model/config/english_words_info_api_config/english_words_info_api_config_model.cpp + model/metrics/metrics_model.cpp + model/config/tg_root_user_config/tg_root_user_config_model.cpp ) set(CONTROLLER_HEADERS @@ -56,6 +60,8 @@ set(CONTROLLER_HEADERS controller/api/base_api/base_api_controller.hpp controller/api/english_words_info_api/english_words_info_api_controller.hpp controller/config/english_words_info_api_config/english_words_info_api_config_controller.hpp + controller/db/metrics_db/metrics_db_controller.hpp + controller/config/tg_root_user_config/tg_root_user_config_controller.hpp ) set(CONTROLLER_SOURCES @@ -71,6 +77,8 @@ set(CONTROLLER_SOURCES controller/api/base_api/base_api_controller.cpp controller/api/english_words_info_api/english_words_info_api_controller.cpp controller/config/english_words_info_api_config/english_words_info_api_config_controller.cpp + controller/db/metrics_db/metrics_db_controller.cpp + controller/config/tg_root_user_config/tg_root_user_config_controller.cpp ) add_library(${LIBRARY_NAME} STATIC From 06e0bbd558338faf2b5950f24b531f06f1c08723 Mon Sep 17 00:00:00 2001 From: b1tflyyyy Date: Fri, 3 May 2024 14:35:38 -0400 Subject: [PATCH 2/9] added root user config --- .../tg_root_user_config_model.cpp | 35 +++++++++++++++++ .../tg_root_user_config_model.hpp | 38 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 source/business_logic/model/config/tg_root_user_config/tg_root_user_config_model.cpp create mode 100644 source/business_logic/model/config/tg_root_user_config/tg_root_user_config_model.hpp diff --git a/source/business_logic/model/config/tg_root_user_config/tg_root_user_config_model.cpp b/source/business_logic/model/config/tg_root_user_config/tg_root_user_config_model.cpp new file mode 100644 index 0000000..523712f --- /dev/null +++ b/source/business_logic/model/config/tg_root_user_config/tg_root_user_config_model.cpp @@ -0,0 +1,35 @@ +// MIT License + +// Copyright (c) 2024 The B1T Foundation + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + + +#include "tg_root_user_config_model.hpp" + +// --------------------------------------------------------------------------------------------------------------------- +ATG_Root_User_Config::ATG_Root_User_Config(std::int64_t root_id) : + Root_Id{ root_id } +{ } + +// --------------------------------------------------------------------------------------------------------------------- +std::int64_t ATG_Root_User_Config::Get_Root_Id() const +{ + return Root_Id; +} \ No newline at end of file diff --git a/source/business_logic/model/config/tg_root_user_config/tg_root_user_config_model.hpp b/source/business_logic/model/config/tg_root_user_config/tg_root_user_config_model.hpp new file mode 100644 index 0000000..2d42d8b --- /dev/null +++ b/source/business_logic/model/config/tg_root_user_config/tg_root_user_config_model.hpp @@ -0,0 +1,38 @@ +// MIT License + +// Copyright (c) 2024 The B1T Foundation + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + + +#pragma once + +#include + +class ATG_Root_User_Config +{ +public: + explicit ATG_Root_User_Config(std::int64_t root_id); + constexpr ~ATG_Root_User_Config() = default; + + [[nodiscard]] std::int64_t Get_Root_Id() const; + +private: + std::int64_t Root_Id; +}; \ No newline at end of file From e3855937a477232853bfec6bfad7b7d6cbe78119 Mon Sep 17 00:00:00 2001 From: b1tflyyyy Date: Fri, 3 May 2024 14:37:20 -0400 Subject: [PATCH 3/9] added root user config controller --- .../tg_root_user_config_controller.cpp | 36 ++++++++++++++++++ .../tg_root_user_config_controller.hpp | 38 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 source/business_logic/controller/config/tg_root_user_config/tg_root_user_config_controller.cpp create mode 100644 source/business_logic/controller/config/tg_root_user_config/tg_root_user_config_controller.hpp diff --git a/source/business_logic/controller/config/tg_root_user_config/tg_root_user_config_controller.cpp b/source/business_logic/controller/config/tg_root_user_config/tg_root_user_config_controller.cpp new file mode 100644 index 0000000..2f28254 --- /dev/null +++ b/source/business_logic/controller/config/tg_root_user_config/tg_root_user_config_controller.cpp @@ -0,0 +1,36 @@ +// MIT License + +// Copyright (c) 2024 The B1T Foundation + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + + +#include "tg_root_user_config_controller.hpp" + +// --------------------------------------------------------------------------------------------------------------------- +std::optional ATG_Root_User_Config_Controller::Load_Config() +{ + char* tg_root_user{ std::getenv("TG_ROOT_USER") }; + if (!tg_root_user) + { + return std::nullopt; + } + + return ATG_Root_User_Config{ strtoll(tg_root_user, nullptr, 10) }; +} \ No newline at end of file diff --git a/source/business_logic/controller/config/tg_root_user_config/tg_root_user_config_controller.hpp b/source/business_logic/controller/config/tg_root_user_config/tg_root_user_config_controller.hpp new file mode 100644 index 0000000..fab4e72 --- /dev/null +++ b/source/business_logic/controller/config/tg_root_user_config/tg_root_user_config_controller.hpp @@ -0,0 +1,38 @@ +// MIT License + +// Copyright (c) 2024 The B1T Foundation + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + + +#pragma once + +#include +#include + +#include + +class ATG_Root_User_Config_Controller +{ +public: + constexpr ATG_Root_User_Config_Controller() = default; + constexpr ~ATG_Root_User_Config_Controller() = default; + + static std::optional Load_Config(); +}; \ No newline at end of file From 606e9d0f83fa9304719f74ca2c12d644b6e2319c Mon Sep 17 00:00:00 2001 From: b1tflyyyy Date: Fri, 3 May 2024 14:38:00 -0400 Subject: [PATCH 4/9] added metrics model --- .../model/metrics/metrics_model.cpp | 60 +++++++++++++++++++ .../model/metrics/metrics_model.hpp | 55 +++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 source/business_logic/model/metrics/metrics_model.cpp create mode 100644 source/business_logic/model/metrics/metrics_model.hpp diff --git a/source/business_logic/model/metrics/metrics_model.cpp b/source/business_logic/model/metrics/metrics_model.cpp new file mode 100644 index 0000000..19e45a9 --- /dev/null +++ b/source/business_logic/model/metrics/metrics_model.cpp @@ -0,0 +1,60 @@ +// MIT License + +// Copyright (c) 2024 The B1T Foundation + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + + +#include "metrics_model.hpp" + +// --------------------------------------------------------------------------------------------------------------------- +SMetrics::SMetrics() : + Start_Request_Count{}, Profile_Request_Count{}, Pr_Game_Request_Count{}, Math_Game_Request_Count{}, Help_Request_Count{}, About_Project_Request_Count{}, Definition_Request_Count{}, Current_Date{} +{ } + +// --------------------------------------------------------------------------------------------------------------------- +SMetrics::SMetrics(std::int64_t start_request_count, std::int64_t profile_request_count, std::int64_t pr_game_request_count, std::int64_t math_game_request_count, std::int64_t help_request_count, std::int64_t about_project_request_count, std::int64_t definition_request_count, std::string current_date) : + Start_Request_Count{ start_request_count }, Profile_Request_Count{ profile_request_count }, Pr_Game_Request_Count{ pr_game_request_count }, Math_Game_Request_Count{ math_game_request_count }, Help_Request_Count{ help_request_count }, About_Project_Request_Count{ about_project_request_count }, Definition_Request_Count{ definition_request_count }, Current_Date{ std::move(current_date) } +{ } + +// --------------------------------------------------------------------------------------------------------------------- +void SMetrics::Set_Current_Date() +{ + std::chrono::system_clock::time_point time_point{ std::chrono::system_clock::now() }; + Current_Date = std::format("{:%Y.%m.%d %H:%M}", time_point); +} + +// --------------------------------------------------------------------------------------------------------------------- +const std::string& SMetrics::Get_Current_Date() const +{ + return Current_Date; +} + +// --------------------------------------------------------------------------------------------------------------------- +void SMetrics::Clear() +{ + Start_Request_Count = 0; + Profile_Request_Count = 0; + Pr_Game_Request_Count = 0; + Math_Game_Request_Count = 0; + Help_Request_Count = 0; + About_Project_Request_Count = 0; + Definition_Request_Count = 0; + Current_Date = " "; +} \ No newline at end of file diff --git a/source/business_logic/model/metrics/metrics_model.hpp b/source/business_logic/model/metrics/metrics_model.hpp new file mode 100644 index 0000000..9fbdc64 --- /dev/null +++ b/source/business_logic/model/metrics/metrics_model.hpp @@ -0,0 +1,55 @@ +// MIT License + +// Copyright (c) 2024 The B1T Foundation + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + + +#pragma once + +#include +#include +#include +#include + +struct SMetrics +{ +public: + explicit SMetrics(); + [[maybe_unused]] explicit SMetrics(std::int64_t start_request_count, std::int64_t profile_request_count, std::int64_t pr_game_request_count, std::int64_t math_game_request_count, std::int64_t help_request_count, std::int64_t about_project_request_count, std::int64_t definition_request_count, std::string current_date); + + ~SMetrics() = default; + + void Set_Current_Date(); + [[nodiscard]] const std::string& Get_Current_Date() const; + + void Clear(); + +public: + std::int64_t Start_Request_Count; + std::int64_t Profile_Request_Count; + std::int64_t Pr_Game_Request_Count; + std::int64_t Math_Game_Request_Count; + std::int64_t Help_Request_Count; + std::int64_t About_Project_Request_Count; + std::int64_t Definition_Request_Count; + +private: + std::string Current_Date; +}; \ No newline at end of file From 2056c84cb0e1e17295aaca13927c13e52f70ef58 Mon Sep 17 00:00:00 2001 From: b1tflyyyy Date: Fri, 3 May 2024 14:38:44 -0400 Subject: [PATCH 5/9] added metrics db controller --- .../db/metrics_db/metrics_db_controller.cpp | 49 +++++++++++++++++++ .../db/metrics_db/metrics_db_controller.hpp | 38 ++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 source/business_logic/controller/db/metrics_db/metrics_db_controller.cpp create mode 100644 source/business_logic/controller/db/metrics_db/metrics_db_controller.hpp diff --git a/source/business_logic/controller/db/metrics_db/metrics_db_controller.cpp b/source/business_logic/controller/db/metrics_db/metrics_db_controller.cpp new file mode 100644 index 0000000..5a38118 --- /dev/null +++ b/source/business_logic/controller/db/metrics_db/metrics_db_controller.cpp @@ -0,0 +1,49 @@ +// MIT License + +// Copyright (c) 2024 The B1T Foundation + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + + +#include "metrics_db_controller.hpp" + +// --------------------------------------------------------------------------------------------------------------------- +AMetrics_DB_Controller::AMetrics_DB_Controller(const ADB_Config& db_cfg) : + ABase_DB_Controller{ db_cfg, "metrics_data" } +{ } + +// --------------------------------------------------------------------------------------------------------------------- +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())); +} + +// --------------------------------------------------------------------------------------------------------------------- +SMetrics AMetrics_DB_Controller::Get_Metrics(std::int64_t metrics_id) +{ + 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 id = '{}')", Table_Name, metrics_id)) }; + return SMetrics{ response[0][0].as(), response[0][1].as(), response[0][2].as(), response[0][3].as(), response[0][4].as(), response[0][5].as(), response[0][6].as(), response[0][7].as() }; +} + +// --------------------------------------------------------------------------------------------------------------------- +std::int64_t AMetrics_DB_Controller::Get_Available_Metrics_Count() +{ + auto response{ Exec_Query(std::format(R"(SELECT MAX(id) FROM {})", Table_Name)) }; + return response[0][0].as(); +} \ No newline at end of file diff --git a/source/business_logic/controller/db/metrics_db/metrics_db_controller.hpp b/source/business_logic/controller/db/metrics_db/metrics_db_controller.hpp new file mode 100644 index 0000000..1c0256d --- /dev/null +++ b/source/business_logic/controller/db/metrics_db/metrics_db_controller.hpp @@ -0,0 +1,38 @@ +// MIT License + +// Copyright (c) 2024 The B1T Foundation + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + + +#pragma once + +#include +#include + +class AMetrics_DB_Controller : public ABase_DB_Controller +{ +public: + explicit AMetrics_DB_Controller(const ADB_Config& db_cfg); + ~AMetrics_DB_Controller() override = default; + + void Set_Metrics(const SMetrics& metrics); + SMetrics Get_Metrics(std::int64_t metrics_id); + std::int64_t Get_Available_Metrics_Count(); +}; \ No newline at end of file From 7f8f6ca981731e3d15baf65c3700689fec806075 Mon Sep 17 00:00:00 2001 From: b1tflyyyy Date: Fri, 3 May 2024 14:40:36 -0400 Subject: [PATCH 6/9] added new message commands --- source/view/texts/message_commands/message_commands.cpp | 4 +++- source/view/texts/message_commands/message_commands.hpp | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/source/view/texts/message_commands/message_commands.cpp b/source/view/texts/message_commands/message_commands.cpp index f356e57..e98a067 100644 --- a/source/view/texts/message_commands/message_commands.cpp +++ b/source/view/texts/message_commands/message_commands.cpp @@ -30,4 +30,6 @@ constinit std::string_view SMessage_Commands::Math_Game{ "math_game" }; 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" }; \ No newline at end of file +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" }; \ No newline at end of file diff --git a/source/view/texts/message_commands/message_commands.hpp b/source/view/texts/message_commands/message_commands.hpp index 346c56f..89837b6 100644 --- a/source/view/texts/message_commands/message_commands.hpp +++ b/source/view/texts/message_commands/message_commands.hpp @@ -35,4 +35,6 @@ 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 Get_Metrics; }; \ No newline at end of file From d7546874c010eecb412ffb1ec0a1719d5cb722eb Mon Sep 17 00:00:00 2001 From: b1tflyyyy Date: Fri, 3 May 2024 14:41:58 -0400 Subject: [PATCH 7/9] added new replies --- source/view/texts/message_reply/message_reply.cpp | 12 ++++++++++++ source/view/texts/message_reply/message_reply.hpp | 3 +++ 2 files changed, 15 insertions(+) diff --git a/source/view/texts/message_reply/message_reply.cpp b/source/view/texts/message_reply/message_reply.cpp index 35ee171..190c434 100644 --- a/source/view/texts/message_reply/message_reply.cpp +++ b/source/view/texts/message_reply/message_reply.cpp @@ -87,4 +87,16 @@ std::string AMessage_Reply::Get_Word_Definition(std::string_view primary_word, s std::string AMessage_Reply::Get_Not_Found_Word_Definition() { return std::format("Скоріш за все ти ввів дивне слово, я не можу знайти визначення для нього)"); +} + +// --------------------------------------------------------------------------------------------------------------------- +std::string AMessage_Reply::Get_Metrics_Count_Msg(std::int64_t metrics_count) +{ + return std::format("Кількість доступних метрик: {}", metrics_count); +} + +// --------------------------------------------------------------------------------------------------------------------- +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); } \ No newline at end of file diff --git a/source/view/texts/message_reply/message_reply.hpp b/source/view/texts/message_reply/message_reply.hpp index e7867da..d19ae33 100644 --- a/source/view/texts/message_reply/message_reply.hpp +++ b/source/view/texts/message_reply/message_reply.hpp @@ -32,6 +32,7 @@ #include #include #include +#include class AMessage_Reply { @@ -50,4 +51,6 @@ class AMessage_Reply static std::string Get_Info_About_Project(); static std::string Get_Word_Definition(std::string_view primary_word, std::string_view definiton); static std::string Get_Not_Found_Word_Definition(); + static std::string Get_Metrics_Count_Msg(std::int64_t metrics_count); + static std::string Get_Metrics_Msg(const SMetrics& metrics); }; \ No newline at end of file From 8cbd5d9fe45276465a6441fcdb151dcd98ffda6a Mon Sep 17 00:00:00 2001 From: b1tflyyyy Date: Fri, 3 May 2024 14:44:54 -0400 Subject: [PATCH 8/9] added mutex & metrics & new commands --- .../message_handler/message_handler.cpp | 72 ++++++++++++++++++- .../message_handler/message_handler.hpp | 16 ++++- 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/source/view/handlers/message_handler/message_handler.cpp b/source/view/handlers/message_handler/message_handler.cpp index 7be2683..2d5cb03 100644 --- a/source/view/handlers/message_handler/message_handler.cpp +++ b/source/view/handlers/message_handler/message_handler.cpp @@ -24,8 +24,11 @@ #include "message_handler.hpp" // --------------------------------------------------------------------------------------------------------------------- -AMessage_Handler::AMessage_Handler(TgBot::Bot& tg_bot, AUser_DB_Controller& user_db_controller, AState_DB_Controller& state_db_controller, ATask_DB_Controller& task_db_controller, AStats_DB_Controller& stats_db_controller, AEnglish_Words_Info_API_Controller& english_words_api_controller) : - TG_Bot{ tg_bot }, User_DB_Controller{ user_db_controller }, State_DB_Controller{ state_db_controller }, Task_DB_Controller{ task_db_controller }, Stats_DB_Controller{ stats_db_controller }, English_Words_API_Controller{ english_words_api_controller } +std::mutex AMessage_Handler::Mutex{ }; + +// --------------------------------------------------------------------------------------------------------------------- +AMessage_Handler::AMessage_Handler(TgBot::Bot& tg_bot, AUser_DB_Controller& user_db_controller, AState_DB_Controller& state_db_controller, ATask_DB_Controller& task_db_controller, AStats_DB_Controller& stats_db_controller, AEnglish_Words_Info_API_Controller& english_words_api_controller, AMetrics_DB_Controller& metrics_db_controller, ATG_Root_User_Config& tg_root_user_cfg) : + TG_Bot{ tg_bot }, User_DB_Controller{ user_db_controller }, State_DB_Controller{ state_db_controller }, Task_DB_Controller{ task_db_controller }, Stats_DB_Controller{ stats_db_controller }, English_Words_API_Controller{ english_words_api_controller }, Metrics_DB_Controller{ metrics_db_controller }, TG_Root_User_Cfg{ tg_root_user_cfg } { } // --------------------------------------------------------------------------------------------------------------------- @@ -33,11 +36,17 @@ void AMessage_Handler::Bind_Commands() { TG_Bot.getEvents().onCommand(SMessage_Commands::Start.data(), [this](TgBot::Message::Ptr message) -> void { + std::lock_guard locker(Mutex); + ++Metrics.Start_Request_Count; + Auto_Register(message->from->id, message->from->username, message->from->firstName); TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Hello_Msg(message->from->username)); }); TG_Bot.getEvents().onCommand(SMessage_Commands::Profile.data(), [this](TgBot::Message::Ptr message) -> void { + std::lock_guard locker(Mutex); + ++Metrics.Profile_Request_Count; + Auto_Register(message->from->id, message->from->username, message->from->firstName); auto user{ User_DB_Controller.Get_User(message->from->id) }; @@ -47,6 +56,9 @@ void AMessage_Handler::Bind_Commands() }); TG_Bot.getEvents().onCommand(SMessage_Commands::Programmer_Game.data(), [this](TgBot::Message::Ptr message) -> void { + std::lock_guard locker(Mutex); + ++Metrics.Pr_Game_Request_Count; + Auto_Register(message->from->id, message->from->username, message->from->firstName); if (Can_Use_Command(message, Time_To_Wait)) @@ -60,6 +72,9 @@ void AMessage_Handler::Bind_Commands() }); TG_Bot.getEvents().onCommand(SMessage_Commands::Math_Game.data(), [this](TgBot::Message::Ptr message) -> void { + std::lock_guard locker(Mutex); + ++Metrics.Math_Game_Request_Count; + Auto_Register(message->from->id, message->from->username, message->from->firstName); if (Can_Use_Command(message, Time_To_Wait)) @@ -77,14 +92,23 @@ void AMessage_Handler::Bind_Commands() }); TG_Bot.getEvents().onCommand(SMessage_Commands::Help.data(), [this](TgBot::Message::Ptr message) -> void { + std::lock_guard locker(Mutex); + + ++Metrics.Help_Request_Count; TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Help_Msg()); }); TG_Bot.getEvents().onCommand(SMessage_Commands::About_Project.data(), [this](TgBot::Message::Ptr message) -> void { + std::lock_guard locker(Mutex); + ++Metrics.About_Project_Request_Count; + TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Info_About_Project()); }); TG_Bot.getEvents().onCommand(SMessage_Commands::Definiton.data(), [this](TgBot::Message::Ptr message) -> void { + std::lock_guard locker(Mutex); + ++Metrics.Definition_Request_Count; + Cut_User_Input(message->text, SMessage_Commands::Definiton.size()); if (auto response{ English_Words_API_Controller.Get_Definition(message->text) }; response != std::nullopt) { @@ -94,6 +118,42 @@ void AMessage_Handler::Bind_Commands() TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Not_Found_Word_Definition()); }); + TG_Bot.getEvents().onCommand(SMessage_Commands::Metrics_Count.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)); + }); + TG_Bot.getEvents().onCommand(SMessage_Commands::Get_Metrics.data(), [this](TgBot::Message::Ptr message) -> void + { + 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)); + }); +} + +// --------------------------------------------------------------------------------------------------------------------- +void AMessage_Handler::Run_Metrics() +{ + using namespace std::chrono_literals; + + while (true) + { + Mutex.lock(); + + Metrics.Set_Current_Date(); + Metrics_DB_Controller.Set_Metrics(Metrics); + Metrics.Clear(); + + Mutex.unlock(); + std::this_thread::sleep_for(18h); + } } // --------------------------------------------------------------------------------------------------------------------- @@ -165,6 +225,12 @@ void AMessage_Handler::Handle_Answer(TgBot::Message::Ptr& message) // --------------------------------------------------------------------------------------------------------------------- void AMessage_Handler::Cut_User_Input(std::string& input_text, std::size_t command_size) { - input_text.erase(input_text.begin(), input_text.begin() + static_cast(command_size + 1)); // erase /some_command part, + 1 for '/' + input_text.erase(input_text.begin(), input_text.begin() + static_cast(command_size + 1)); // erase /some_command part, + 1 for '/' input_text.erase(std::remove(input_text.begin(), input_text.end(), ' '), input_text.end()); +} + +// --------------------------------------------------------------------------------------------------------------------- +bool AMessage_Handler::Is_Root_User(std::int64_t user_id) +{ + return user_id == TG_Root_User_Cfg.Get_Root_Id(); } \ No newline at end of file diff --git a/source/view/handlers/message_handler/message_handler.hpp b/source/view/handlers/message_handler/message_handler.hpp index 5a41071..107e3e2 100644 --- a/source/view/handlers/message_handler/message_handler.hpp +++ b/source/view/handlers/message_handler/message_handler.hpp @@ -23,18 +23,24 @@ #pragma once +#include +#include +#include + #include #include #include #include #include +#include #include #include #include #include +#include #include #include @@ -43,26 +49,34 @@ class AMessage_Handler { public: - explicit AMessage_Handler(TgBot::Bot& tg_bot, AUser_DB_Controller& user_db_controller, AState_DB_Controller& state_db_controller, ATask_DB_Controller& task_db_controller, AStats_DB_Controller& stats_db_controller, AEnglish_Words_Info_API_Controller& english_words_api_controller); + explicit AMessage_Handler(TgBot::Bot& tg_bot, AUser_DB_Controller& user_db_controller, AState_DB_Controller& state_db_controller, ATask_DB_Controller& task_db_controller, AStats_DB_Controller& stats_db_controller, AEnglish_Words_Info_API_Controller& english_words_api_controller, AMetrics_DB_Controller& metrics_db_controller, ATG_Root_User_Config& tg_root_user_cfg); constexpr ~AMessage_Handler() = default; void Bind_Commands(); + [[noreturn]] void Run_Metrics(); + private: void Auto_Register(std::int64_t user_id, std::string_view username, std::string_view first_name); bool Can_Use_Command(const TgBot::Message::Ptr& message, std::int64_t required_difference); void Handle_Answer(TgBot::Message::Ptr& message); void Cut_User_Input(std::string& input_text, std::size_t command_size); + bool Is_Root_User(std::int64_t user_id); private: constexpr static std::size_t Time_To_Wait{ 3 }; private: + static std::mutex Mutex; + TgBot::Bot& TG_Bot; + ATG_Root_User_Config& TG_Root_User_Cfg; + SMetrics Metrics; AUser_DB_Controller& User_DB_Controller; AState_DB_Controller& State_DB_Controller; ATask_DB_Controller& Task_DB_Controller; AStats_DB_Controller& Stats_DB_Controller; + AMetrics_DB_Controller& Metrics_DB_Controller; AEnglish_Words_Info_API_Controller& English_Words_API_Controller; }; \ No newline at end of file From b19056ba053279d1add7d087c2230df509bce203 Mon Sep 17 00:00:00 2001 From: b1tflyyyy Date: Fri, 3 May 2024 14:45:42 -0400 Subject: [PATCH 9/9] added threads & root user cfg --- source/view/main.cpp | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/source/view/main.cpp b/source/view/main.cpp index 0f9d27e..e6978ca 100644 --- a/source/view/main.cpp +++ b/source/view/main.cpp @@ -26,8 +26,9 @@ #include #include #include +#include +#include -#include #include int main() @@ -54,6 +55,13 @@ int main() ALogger_Utility::Error("Error reading english words api config data"); return -1; } + + auto tg_root_user_cfg{ ATG_Root_User_Config_Controller::Load_Config() }; + if (tg_root_user_cfg == std::nullopt) + { + ALogger_Utility::Error("Error reading tg root user cfg data"); + return -1; + } #endif TgBot::Bot tg_bot{ tg_cfg->Get_TG_Token().data() }; @@ -62,24 +70,35 @@ int main() AState_DB_Controller state_db_controller{ *db_cfg }; ATask_DB_Controller task_db_controller{ *db_cfg }; AStats_DB_Controller stats_db_controller{ *db_cfg }; + AMetrics_DB_Controller metrics_db_controller{ *db_cfg }; AEnglish_Words_Info_API_Controller english_words_api_controller{ *english_words_info_api_config }; - AMessage_Handler message_handler{ tg_bot, user_db_controller, state_db_controller, task_db_controller, stats_db_controller, english_words_api_controller }; + AMessage_Handler message_handler{ tg_bot, user_db_controller, state_db_controller, task_db_controller, stats_db_controller, english_words_api_controller, metrics_db_controller, *tg_root_user_cfg }; message_handler.Bind_Commands(); - try + std::thread metrics_thread([&message_handler]() -> void { - TgBot::TgLongPoll long_poll{ tg_bot }; + message_handler.Run_Metrics(); + }); - while(true) + std::thread bot_thread([&tg_bot]() -> void + { + try { - long_poll.start(); + TgBot::TgLongPoll long_poll{ tg_bot }; + while(true) + { + long_poll.start(); + } } - } - catch(TgBot::TgException& ex) - { - ALogger_Utility::Error(ex.what()); - } + catch(TgBot::TgException& ex) + { + ALogger_Utility::Error(ex.what()); + } + }); + + bot_thread.join(); + metrics_thread.join(); return 0; } \ No newline at end of file