From e1258151dee7a18ec75c50f0d10ad716070de4f8 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 6 Nov 2023 18:02:04 +0100 Subject: [PATCH] Added maintenance to db and gql #424 --- bot/src/bot/application.py | 3 +- bot/src/bot/startup_settings_extension.py | 3 +- bot/src/bot_core/abc/task_abc.py | 3 +- .../core_extension_on_ready_event.py | 3 +- bot/src/bot_core/environment_variables.py | 1 + bot/src/bot_core/helper/event_checks.py | 3 +- .../bot_core/service/client_utils_service.py | 3 +- .../db_history_scripts/config/technician.sql | 5 ++ .../migration/maintenance_mode_migration.py | 51 +++++++++++++++++++ bot/src/bot_data/model/technician_config.py | 11 ++++ bot/src/bot_graphql/abc/query_abc.py | 7 +-- .../bot_graphql/graphql/technicianConfig.gql | 3 ++ .../mutations/technician_config_mutation.py | 3 ++ 13 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 bot/src/bot_core/environment_variables.py create mode 100644 bot/src/bot_data/migration/maintenance_mode_migration.py diff --git a/bot/src/bot/application.py b/bot/src/bot/application.py index a7366f22..aff7b618 100644 --- a/bot/src/bot/application.py +++ b/bot/src/bot/application.py @@ -11,6 +11,7 @@ from bot_api.api_thread import ApiThread from bot_core.abc.task_abc import TaskABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings +from bot_core.environment_variables import MAINTENANCE from bot_core.service.data_integrity_service import DataIntegrityService @@ -58,7 +59,7 @@ class Application(DiscordBotApplicationABC): return self._logger.info(__name__, f"Try to start {DiscordBotService.__name__}") - if not self._config.get_configuration("MAINTENANCE"): + if not self._config.get_configuration(MAINTENANCE): for task in self._tasks: await self._bot.add_cog(task) diff --git a/bot/src/bot/startup_settings_extension.py b/bot/src/bot/startup_settings_extension.py index 03896613..c792b077 100644 --- a/bot/src/bot/startup_settings_extension.py +++ b/bot/src/bot/startup_settings_extension.py @@ -8,6 +8,7 @@ from cpl_core.dependency_injection import ServiceCollectionABC from cpl_core.environment import ApplicationEnvironmentABC from bot_core.configuration.bot_logging_settings import BotLoggingSettings +from bot_core.environment_variables import MAINTENANCE class StartupSettingsExtension(StartupExtensionABC): @@ -20,7 +21,7 @@ class StartupSettingsExtension(StartupExtensionABC): configuration.add_environment_variables("SDB_") configuration.add_environment_variables("DISCORD_") configuration.add_configuration( - "MAINTENANCE", configuration.get_configuration("MAINTENANCE") in [True, "true", "True"] + MAINTENANCE, configuration.get_configuration(MAINTENANCE) in [True, "true", "True"] ) configuration.add_configuration( "MIGRATION_ONLY", configuration.get_configuration("MIGRATION_ONLY") in [True, "true", "True"] diff --git a/bot/src/bot_core/abc/task_abc.py b/bot/src/bot_core/abc/task_abc.py index 2bdee72e..5f578a99 100644 --- a/bot/src/bot_core/abc/task_abc.py +++ b/bot/src/bot_core/abc/task_abc.py @@ -6,6 +6,7 @@ from cpl_core.dependency_injection import ServiceProviderABC from cpl_discord.service import DiscordBotServiceABC from discord.ext import commands +from bot_core.environment_variables import MAINTENANCE from bot_core.logging.task_logger import TaskLogger @@ -16,7 +17,7 @@ class TaskABC(commands.Cog): @ServiceProviderABC.inject def _is_maintenance(self, config: ConfigurationABC) -> bool: - return config.get_configuration("MAINTENANCE") is True + return config.get_configuration(MAINTENANCE) is True @ServiceProviderABC.inject async def _wait_until_ready(self, config: ConfigurationABC, logger: TaskLogger, bot: DiscordBotServiceABC): diff --git a/bot/src/bot_core/core_extension/core_extension_on_ready_event.py b/bot/src/bot_core/core_extension/core_extension_on_ready_event.py index 88a938f5..1fa482a7 100644 --- a/bot/src/bot_core/core_extension/core_extension_on_ready_event.py +++ b/bot/src/bot_core/core_extension/core_extension_on_ready_event.py @@ -7,6 +7,7 @@ from cpl_discord.service import DiscordBotServiceABC from cpl_translation import TranslatePipe from bot_core.abc.client_utils_abc import ClientUtilsABC +from bot_core.environment_variables import MAINTENANCE class CoreExtensionOnReadyEvent(OnReadyABC): @@ -30,7 +31,7 @@ class CoreExtensionOnReadyEvent(OnReadyABC): async def on_ready(self): self._logger.debug(__name__, f"Module {type(self)} started") - if self._config.get_configuration("MAINTENANCE"): + if self._config.get_configuration(MAINTENANCE): await self._client_utils.presence_game("common.presence.maintenance") else: await self._client_utils.presence_game("common.presence.running") diff --git a/bot/src/bot_core/environment_variables.py b/bot/src/bot_core/environment_variables.py new file mode 100644 index 00000000..2cd1cadc --- /dev/null +++ b/bot/src/bot_core/environment_variables.py @@ -0,0 +1 @@ +MAINTENANCE = "MAINTENANCE" diff --git a/bot/src/bot_core/helper/event_checks.py b/bot/src/bot_core/helper/event_checks.py index c825b322..e94f10cf 100644 --- a/bot/src/bot_core/helper/event_checks.py +++ b/bot/src/bot_core/helper/event_checks.py @@ -24,12 +24,11 @@ class EventChecks: if not result: def empty(*args, **kwargs): - pass + return return empty return await func(*args, **kwargs) - # return commands.check(check_if_bot_is_ready) check_if_bot_is_ready.__name__ = func.__name__ sig = inspect.signature(func) check_if_bot_is_ready.__signature__ = sig.replace(parameters=tuple(sig.parameters.values())[1:]) diff --git a/bot/src/bot_core/service/client_utils_service.py b/bot/src/bot_core/service/client_utils_service.py index 32736c71..26fe6b13 100644 --- a/bot/src/bot_core/service/client_utils_service.py +++ b/bot/src/bot_core/service/client_utils_service.py @@ -16,6 +16,7 @@ from bot_core.abc.client_utils_abc import ClientUtilsABC from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings +from bot_core.environment_variables import MAINTENANCE from bot_data.abc.client_repository_abc import ClientRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.abc.user_joined_voice_channel_repository_abc import ( @@ -87,7 +88,7 @@ class ClientUtilsService(ClientUtilsABC): return client async def check_if_bot_is_ready_yet(self) -> bool: - if self._config.get_configuration("MAINTENANCE"): + if self._config.get_configuration(MAINTENANCE): self._logger.warn( __name__, f"Bot is in maintenance mode", diff --git a/bot/src/bot_data/migration/db_history_scripts/config/technician.sql b/bot/src/bot_data/migration/db_history_scripts/config/technician.sql index 1df922ee..3866f88f 100644 --- a/bot/src/bot_data/migration/db_history_scripts/config/technician.sql +++ b/bot/src/bot_data/migration/db_history_scripts/config/technician.sql @@ -6,6 +6,7 @@ CREATE TABLE IF NOT EXISTS `CFG_TechnicianHistory` `WaitForShutdown` BIGINT NOT NULL DEFAULT 8, `CacheMaxMessages` BIGINT NOT NULL DEFAULT 1000000, `MaxSteamOfferCount` BIGINT NOT NULL DEFAULT 250, + `Maintenance` BOOLEAN DEFAULT FALSE, `FeatureFlags` JSON NULL DEFAULT ('{}'), `Deleted` BOOL DEFAULT FALSE, `DateFrom` DATETIME(6) NOT NULL, @@ -25,6 +26,7 @@ BEGIN `WaitForShutdown`, `CacheMaxMessages`, `MaxSteamOfferCount`, + `Maintenance`, `FeatureFlags`, `DateFrom`, `DateTo`) @@ -34,6 +36,7 @@ BEGIN OLD.WaitForShutdown, OLD.CacheMaxMessages, OLD.MaxSteamOfferCount, + OLD.Maintenance, OLD.FeatureFlags, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)); @@ -52,6 +55,7 @@ BEGIN `WaitForShutdown`, `CacheMaxMessages`, `MaxSteamOfferCount`, + `Maintenance`, `FeatureFlags`, `Deleted`, `DateFrom`, @@ -62,6 +66,7 @@ BEGIN OLD.WaitForShutdown, OLD.CacheMaxMessages, OLD.MaxSteamOfferCount, + OLD.Maintenance, OLD.FeatureFlags, TRUE, OLD.LastModifiedAt, diff --git a/bot/src/bot_data/migration/maintenance_mode_migration.py b/bot/src/bot_data/migration/maintenance_mode_migration.py new file mode 100644 index 00000000..467c8604 --- /dev/null +++ b/bot/src/bot_data/migration/maintenance_mode_migration.py @@ -0,0 +1,51 @@ +from bot_core.logging.database_logger import DatabaseLogger +from bot_data.abc.migration_abc import MigrationABC +from bot_data.db_context import DBContext + + +class MaintenanceModeMigration(MigrationABC): + name = "1.2.0_MaintenanceModeMigration" + + def __init__(self, logger: DatabaseLogger, db: DBContext): + MigrationABC.__init__(self) + self._logger = logger + self._db = db + self._cursor = db.cursor + + def upgrade(self): + self._logger.debug(__name__, "Running upgrade") + + self._cursor.execute( + str( + f""" + ALTER TABLE CFG_Technician + ADD Maintenance BOOLEAN DEFAULT FALSE AFTER MaxSteamOfferCount; + """ + ) + ) + + self._cursor.execute( + str( + f""" + ALTER TABLE CFG_TechnicianHistory + ADD Maintenance BOOLEAN DEFAULT FALSE AFTER MaxSteamOfferCount; + """ + ) + ) + self._exec(__file__, "config/technician.sql") + + def downgrade(self): + self._cursor.execute( + str( + f""" + ALTER TABLE CFG_Technician DROP COLUMN Maintenance; + """ + ) + ) + self._cursor.execute( + str( + f""" + ALTER TABLE CFG_TechnicianHistory DROP COLUMN Maintenance; + """ + ) + ) diff --git a/bot/src/bot_data/model/technician_config.py b/bot/src/bot_data/model/technician_config.py index 313bbb95..e3c4f5f3 100644 --- a/bot/src/bot_data/model/technician_config.py +++ b/bot/src/bot_data/model/technician_config.py @@ -16,6 +16,7 @@ class TechnicianConfig(TableABC, ConfigurationModelABC): wait_for_shutdown: int, cache_max_messages: int, max_steam_offer_count: int, + maintenance: bool, feature_flags: dict[FeatureFlagsEnum], technician_ids: List[int], ping_urls: List[str], @@ -29,6 +30,8 @@ class TechnicianConfig(TableABC, ConfigurationModelABC): self._wait_for_shutdown = wait_for_shutdown self._cache_max_messages = cache_max_messages self._max_steam_offer_count = max_steam_offer_count + self._maintenance = maintenance + self._feature_flags = feature_flags self._technician_ids = technician_ids self._ping_urls = ping_urls @@ -105,6 +108,14 @@ class TechnicianConfig(TableABC, ConfigurationModelABC): def ping_urls(self, value: List[str]): self._ping_urls = value + @property + def maintenance(self) -> bool: + return self._maintenance + + @maintenance.setter + def maintenance(self, value: bool): + self._maintenance = value + @staticmethod def get_select_all_string() -> str: return str( diff --git a/bot/src/bot_graphql/abc/query_abc.py b/bot/src/bot_graphql/abc/query_abc.py index d4d44a90..506428fc 100644 --- a/bot/src/bot_graphql/abc/query_abc.py +++ b/bot/src/bot_graphql/abc/query_abc.py @@ -11,6 +11,7 @@ from bot_api.exception.service_error_code_enum import ServiceErrorCode from bot_api.exception.service_exception import ServiceException from bot_api.route.route import Route from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum +from bot_core.environment_variables import MAINTENANCE from bot_data.model.achievement import Achievement from bot_data.model.auth_role_enum import AuthRoleEnum from bot_data.model.auth_user import AuthUser @@ -93,7 +94,7 @@ class QueryABC(ObjectType): if permissions.is_member_technician(guild.get_member(u.discord_id)): return True - if config.get_configuration("MAINTENANCE"): + if config.get_configuration(MAINTENANCE): return False access = False @@ -242,11 +243,11 @@ class QueryABC(ObjectType): match permission: case UserRoleEnum.moderator: can_edit = permissions.is_member_moderator - if config.get_configuration("MAINTENANCE"): + if config.get_configuration(MAINTENANCE): can_edit = lambda x: False case UserRoleEnum.admin: can_edit = permissions.is_member_admin - if config.get_configuration("MAINTENANCE"): + if config.get_configuration(MAINTENANCE): can_edit = lambda x: False case UserRoleEnum.technician: can_edit = permissions.is_member_technician diff --git a/bot/src/bot_graphql/graphql/technicianConfig.gql b/bot/src/bot_graphql/graphql/technicianConfig.gql index 1c97490a..63cc1fb8 100644 --- a/bot/src/bot_graphql/graphql/technicianConfig.gql +++ b/bot/src/bot_graphql/graphql/technicianConfig.gql @@ -5,6 +5,7 @@ type TechnicianConfig implements TableWithHistoryQuery { waitForShutdown: Int cacheMaxMessages: Int maxSteamOfferCount: Int + maintenance: Boolean featureFlagCount: Int featureFlags: [FeatureFlag] pingURLs: [String] @@ -25,6 +26,7 @@ type TechnicianConfigHistory implements HistoryTableQuery { waitForShutdown: Int cacheMaxMessages: Int maxSteamOfferCount: Int + maintenance: Boolean featureFlagCount: Int featureFlags: [FeatureFlag] @@ -62,6 +64,7 @@ input TechnicianConfigInput { waitForShutdown: Int cacheMaxMessages: Int maxSteamOfferCount: Int + maintenance: Boolean featureFlags: [FeatureFlagInput] pingURLs: [String] technicianIds: [String] diff --git a/bot/src/bot_graphql/mutations/technician_config_mutation.py b/bot/src/bot_graphql/mutations/technician_config_mutation.py index 28e947f9..543a87b4 100644 --- a/bot/src/bot_graphql/mutations/technician_config_mutation.py +++ b/bot/src/bot_graphql/mutations/technician_config_mutation.py @@ -62,6 +62,9 @@ class TechnicianConfigMutation(QueryABC): technician_config.max_steam_offer_count = ( input["maxSteamOfferCount"] if "maxSteamOfferCount" in input else technician_config.max_steam_offer_count ) + technician_config.max_steam_offer_count = ( + input["maintenance"] if "maintenance" in input else technician_config.maintenance + ) old_feature_flags = technician_config.feature_flags technician_config.feature_flags = ( dict(