From 71f1f972c9226e4efbf49b72419fa2a5a3877463 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 6 Aug 2023 10:24:05 +0200 Subject: [PATCH] Added technician config seeder #127 --- kdb-bot/src/bot_data/abc/data_seeder_abc.py | 2 +- .../abc/technician_config_repository_abc.py | 4 ++ kdb-bot/src/bot_data/data_module.py | 6 +++ .../src/bot_data/model/technician_config.py | 2 +- .../bot_data/model/technician_id_config.py | 2 +- .../model/technician_ping_url_config.py | 2 +- .../technician_config_repository_service.py | 6 +++ .../service/technician_config_seeder.py | 48 +++++++++++++++++++ 8 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 kdb-bot/src/bot_data/service/technician_config_seeder.py diff --git a/kdb-bot/src/bot_data/abc/data_seeder_abc.py b/kdb-bot/src/bot_data/abc/data_seeder_abc.py index e49dc486..1907f25e 100644 --- a/kdb-bot/src/bot_data/abc/data_seeder_abc.py +++ b/kdb-bot/src/bot_data/abc/data_seeder_abc.py @@ -7,5 +7,5 @@ class DataSeederABC(ABC): pass @abstractmethod - def seed(self): + async def seed(self): pass diff --git a/kdb-bot/src/bot_data/abc/technician_config_repository_abc.py b/kdb-bot/src/bot_data/abc/technician_config_repository_abc.py index 1751cbe9..25c88d96 100644 --- a/kdb-bot/src/bot_data/abc/technician_config_repository_abc.py +++ b/kdb-bot/src/bot_data/abc/technician_config_repository_abc.py @@ -10,6 +10,10 @@ class TechnicianConfigRepositoryABC(ABC): def __init__(self): pass + @abstractmethod + def does_technician_config_exists(self) -> bool: + pass + @abstractmethod def get_technician_config(self) -> TechnicianConfig: pass diff --git a/kdb-bot/src/bot_data/data_module.py b/kdb-bot/src/bot_data/data_module.py index fd571abb..0ef436a2 100644 --- a/kdb-bot/src/bot_data/data_module.py +++ b/kdb-bot/src/bot_data/data_module.py @@ -10,10 +10,12 @@ from bot_data.abc.api_key_repository_abc import ApiKeyRepositoryABC from bot_data.abc.auth_user_repository_abc import AuthUserRepositoryABC from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.client_repository_abc import ClientRepositoryABC +from bot_data.abc.data_seeder_abc import DataSeederABC from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC from bot_data.abc.level_repository_abc import LevelRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.technician_config_repository_abc import TechnicianConfigRepositoryABC from bot_data.abc.user_game_ident_repository_abc import UserGameIdentRepositoryABC from bot_data.abc.user_joined_game_server_repository_abc import UserJoinedGameServerRepositoryABC from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC @@ -36,6 +38,8 @@ from bot_data.service.known_user_repository_service import KnownUserRepositorySe from bot_data.service.level_repository_service import LevelRepositoryService from bot_data.service.seeder_service import SeederService from bot_data.service.server_repository_service import ServerRepositoryService +from bot_data.service.technician_config_repository_service import TechnicianConfigRepositoryService +from bot_data.service.technician_config_seeder import TechnicianConfigSeeder from bot_data.service.user_game_ident_repository_service import UserGameIdentRepositoryService from bot_data.service.user_joined_game_server_repository_service import UserJoinedGameServerRepositoryService from bot_data.service.user_joined_server_repository_service import ( @@ -80,5 +84,7 @@ class DataModule(ModuleABC): services.add_transient(GameServerRepositoryABC, GameServerRepositoryService) services.add_transient(UserGameIdentRepositoryABC, UserGameIdentRepositoryService) services.add_transient(AchievementRepositoryABC, AchievementRepositoryService) + services.add_transient(TechnicianConfigRepositoryABC, TechnicianConfigRepositoryService) services.add_transient(SeederService) + services.add_transient(DataSeederABC, TechnicianConfigSeeder) diff --git a/kdb-bot/src/bot_data/model/technician_config.py b/kdb-bot/src/bot_data/model/technician_config.py index b90cb493..302d8c1e 100644 --- a/kdb-bot/src/bot_data/model/technician_config.py +++ b/kdb-bot/src/bot_data/model/technician_config.py @@ -104,7 +104,7 @@ class TechnicianConfig(TableABC): '{self._help_command_reference_url}', {self._wait_for_restart}, {self._wait_for_shutdown}, - {self._cache_max_messages}, + {self._cache_max_messages} ); """ ) diff --git a/kdb-bot/src/bot_data/model/technician_id_config.py b/kdb-bot/src/bot_data/model/technician_id_config.py index ed9c51cc..fcf0d83d 100644 --- a/kdb-bot/src/bot_data/model/technician_id_config.py +++ b/kdb-bot/src/bot_data/model/technician_id_config.py @@ -50,7 +50,7 @@ class TechnicianIdConfig(TableABC): INSERT INTO `CFG_TechnicianIds` ( `TechnicianId` ) VALUES ( - '{self._technician_id}', + '{self._technician_id}' ); """ ) diff --git a/kdb-bot/src/bot_data/model/technician_ping_url_config.py b/kdb-bot/src/bot_data/model/technician_ping_url_config.py index b9504f5c..69d57965 100644 --- a/kdb-bot/src/bot_data/model/technician_ping_url_config.py +++ b/kdb-bot/src/bot_data/model/technician_ping_url_config.py @@ -50,7 +50,7 @@ class TechnicianPingUrlConfig(TableABC): INSERT INTO `CFG_TechnicianPingUrls` ( `URL` ) VALUES ( - '{self._ping_url}', + '{self._ping_url}' ); """ ) diff --git a/kdb-bot/src/bot_data/service/technician_config_repository_service.py b/kdb-bot/src/bot_data/service/technician_config_repository_service.py index bd3728d5..3e1354cb 100644 --- a/kdb-bot/src/bot_data/service/technician_config_repository_service.py +++ b/kdb-bot/src/bot_data/service/technician_config_repository_service.py @@ -48,6 +48,12 @@ class TechnicianConfigRepositoryService(TechnicianConfigRepositoryABC): id=result[0], ) + def does_technician_config_exists(self) -> bool: + self._logger.trace(__name__, f"Send SQL command: {TechnicianConfig.get_select_all_string()}") + result = self._context.select(TechnicianConfig.get_select_all_string()) + + return len(result) > 0 + def get_technician_config(self) -> TechnicianConfig: self._logger.trace(__name__, f"Send SQL command: {TechnicianConfig.get_select_all_string()}") result = self._context.select(TechnicianConfig.get_select_all_string())[0] diff --git a/kdb-bot/src/bot_data/service/technician_config_seeder.py b/kdb-bot/src/bot_data/service/technician_config_seeder.py new file mode 100644 index 00000000..44ed44e6 --- /dev/null +++ b/kdb-bot/src/bot_data/service/technician_config_seeder.py @@ -0,0 +1,48 @@ +from cpl_core.database.context import DatabaseContextABC +from cpl_query.extension import List + +from bot_core.logging.database_logger import DatabaseLogger +from bot_data.abc.data_seeder_abc import DataSeederABC +from bot_data.abc.technician_config_repository_abc import TechnicianConfigRepositoryABC +from bot_data.model.technician_config import TechnicianConfig +from bot_data.model.technician_id_config import TechnicianIdConfig +from bot_data.model.technician_ping_url_config import TechnicianPingUrlConfig + + +class TechnicianConfigSeeder(DataSeederABC): + def __init__( + self, + logger: DatabaseLogger, + technician_config: TechnicianConfigRepositoryABC, + db: DatabaseContextABC, + ): + DataSeederABC.__init__(self) + + self._logger = logger + self._technician_config = technician_config + self._db = db + + async def seed(self): + try: + if not self._technician_config.does_technician_config_exists(): + config = TechnicianConfig( + "https://git.sh-edraft.de/sh-edraft.de/kd_discord_bot/wiki/Befehle", + 8, + 8, + 1000000, + List(int, [240160344557879316]), + List(str, ["www.google.com", "www.sh-edraft.de", "www.keksdose-gaming.de"]), + ) + + self._technician_config.add_technician_config(config) + for technician in config.technician_ids: + self._technician_config.add_technician_id_config(TechnicianIdConfig(technician)) + + for url in config.ping_urls: + self._technician_config.add_technician_ping_url_config(TechnicianPingUrlConfig(url)) + + self._db.save_changes() + self._logger.debug(__name__, "Seeded technician config") + + except Exception as e: + self._logger.error(__name__, f"Seeding technician config failed", e)