From dcbb481b10dc057554f68e108e2b41149b9db1a4 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 4 Nov 2023 23:37:07 +0100 Subject: [PATCH] Added setting for max steam offer count --- bot/src/bot/config | 2 +- bot/src/bot/main.py | 2 +- .../db_history_scripts/config/technician.sql | 5 ++ .../max_steam_offer_count_migration.py | 51 +++++++++++++++++++ bot/src/bot_data/model/technician_config.py | 14 ++++- .../technician_config_repository_service.py | 5 +- .../startup_migration_extension.py | 2 + .../bot_graphql/graphql/technicianConfig.gql | 3 ++ .../mutations/technician_config_mutation.py | 3 ++ .../technician_config_history_query.py | 1 + .../queries/technician_config_query.py | 1 + .../special_offers/steam_offer_watcher.py | 6 ++- web/package.json | 2 +- .../models/config/technician-config.model.ts | 1 + web/src/app/models/graphql/mutations.model.ts | 4 +- web/src/app/models/graphql/queries.model.ts | 1 + .../settings/settings.component.html | 9 ++++ .../components/settings/settings.component.ts | 1 + web/src/assets/i18n/de.json | 1 + web/src/assets/i18n/en.json | 1 + 20 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 bot/src/bot_data/migration/max_steam_offer_count_migration.py rename bot/src/{bot => bot_data}/startup_migration_extension.py (95%) diff --git a/bot/src/bot/config b/bot/src/bot/config index 954fd9bb..990c7df3 160000 --- a/bot/src/bot/config +++ b/bot/src/bot/config @@ -1 +1 @@ -Subproject commit 954fd9bb341ff9ad66ffc9e293cbcde6ecb71326 +Subproject commit 990c7df3ff9c84085795282e7cb2eaeb36603200 diff --git a/bot/src/bot/main.py b/bot/src/bot/main.py index c004dd7d..1efe7d63 100644 --- a/bot/src/bot/main.py +++ b/bot/src/bot/main.py @@ -9,7 +9,7 @@ from bot.application import Application from bot.extension.init_bot_extension import InitBotExtension from bot.startup import Startup from bot.startup_discord_extension import StartupDiscordExtension -from bot.startup_migration_extension import StartupMigrationExtension +from bot_data.startup_migration_extension import StartupMigrationExtension from bot.startup_module_extension import StartupModuleExtension from bot.startup_settings_extension import StartupSettingsExtension from bot_api.app_api_extension import AppApiExtension 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 2256cbc8..1df922ee 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 @@ -5,6 +5,7 @@ CREATE TABLE IF NOT EXISTS `CFG_TechnicianHistory` `WaitForRestart` BIGINT NOT NULL DEFAULT 8, `WaitForShutdown` BIGINT NOT NULL DEFAULT 8, `CacheMaxMessages` BIGINT NOT NULL DEFAULT 1000000, + `MaxSteamOfferCount` BIGINT NOT NULL DEFAULT 250, `FeatureFlags` JSON NULL DEFAULT ('{}'), `Deleted` BOOL DEFAULT FALSE, `DateFrom` DATETIME(6) NOT NULL, @@ -23,6 +24,7 @@ BEGIN `WaitForRestart`, `WaitForShutdown`, `CacheMaxMessages`, + `MaxSteamOfferCount`, `FeatureFlags`, `DateFrom`, `DateTo`) @@ -31,6 +33,7 @@ BEGIN OLD.WaitForRestart, OLD.WaitForShutdown, OLD.CacheMaxMessages, + OLD.MaxSteamOfferCount, OLD.FeatureFlags, OLD.LastModifiedAt, CURRENT_TIMESTAMP(6)); @@ -48,6 +51,7 @@ BEGIN `WaitForRestart`, `WaitForShutdown`, `CacheMaxMessages`, + `MaxSteamOfferCount`, `FeatureFlags`, `Deleted`, `DateFrom`, @@ -57,6 +61,7 @@ BEGIN OLD.WaitForRestart, OLD.WaitForShutdown, OLD.CacheMaxMessages, + OLD.MaxSteamOfferCount, OLD.FeatureFlags, TRUE, OLD.LastModifiedAt, diff --git a/bot/src/bot_data/migration/max_steam_offer_count_migration.py b/bot/src/bot_data/migration/max_steam_offer_count_migration.py new file mode 100644 index 00000000..fe926c24 --- /dev/null +++ b/bot/src/bot_data/migration/max_steam_offer_count_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 MaxSteamOfferCountMigration(MigrationABC): + name = "1.2.0_MaxSteamOfferCountMigration" + + 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 MaxSteamOfferCount BIGINT NOT NULL DEFAULT 250 AFTER CacheMaxMessages; + """ + ) + ) + + self._cursor.execute( + str( + f""" + ALTER TABLE CFG_TechnicianHistory + ADD MaxSteamOfferCount BIGINT NOT NULL DEFAULT 250 AFTER CacheMaxMessages; + """ + ) + ) + self._exec(__file__, "config/technician.sql") + + def downgrade(self): + self._cursor.execute( + str( + f""" + ALTER TABLE CFG_Technician DROP COLUMN MaxSteamOfferCount; + """ + ) + ) + self._cursor.execute( + str( + f""" + ALTER TABLE CFG_TechnicianHistory DROP COLUMN MaxSteamOfferCount; + """ + ) + ) diff --git a/bot/src/bot_data/model/technician_config.py b/bot/src/bot_data/model/technician_config.py index 1390f4b8..313bbb95 100644 --- a/bot/src/bot_data/model/technician_config.py +++ b/bot/src/bot_data/model/technician_config.py @@ -15,6 +15,7 @@ class TechnicianConfig(TableABC, ConfigurationModelABC): wait_for_restart: int, wait_for_shutdown: int, cache_max_messages: int, + max_steam_offer_count: int, feature_flags: dict[FeatureFlagsEnum], technician_ids: List[int], ping_urls: List[str], @@ -27,6 +28,7 @@ class TechnicianConfig(TableABC, ConfigurationModelABC): self._wait_for_restart = wait_for_restart self._wait_for_shutdown = wait_for_shutdown self._cache_max_messages = cache_max_messages + self._max_steam_offer_count = max_steam_offer_count self._feature_flags = feature_flags self._technician_ids = technician_ids self._ping_urls = ping_urls @@ -71,6 +73,14 @@ class TechnicianConfig(TableABC, ConfigurationModelABC): def cache_max_messages(self, value: int): self._cache_max_messages = value + @property + def max_steam_offer_count(self) -> int: + return self._max_steam_offer_count + + @max_steam_offer_count.setter + def max_steam_offer_count(self, value: int): + self._max_steam_offer_count = value + @property def feature_flags(self) -> dict[FeatureFlagsEnum]: return self._feature_flags @@ -117,12 +127,13 @@ class TechnicianConfig(TableABC, ConfigurationModelABC): return str( f""" INSERT INTO `CFG_Technician` ( - `HelpCommandReferenceUrl`, `WaitForRestart`, `WaitForShutdown`, `CacheMaxMessages`, `FeatureFlags` + `HelpCommandReferenceUrl`, `WaitForRestart`, `WaitForShutdown`, `CacheMaxMessages`, `MaxSteamOfferCount`, `FeatureFlags` ) VALUES ( '{self._help_command_reference_url}', {self._wait_for_restart}, {self._wait_for_shutdown}, {self._cache_max_messages}, + {self._max_steam_offer_count}, '{json.dumps(self._feature_flags)}' ); """ @@ -137,6 +148,7 @@ class TechnicianConfig(TableABC, ConfigurationModelABC): `WaitForRestart` = {self._wait_for_restart}, `WaitForShutdown` = {self._wait_for_shutdown}, `CacheMaxMessages` = {self._cache_max_messages}, + `MaxSteamOfferCount` = {self._max_steam_offer_count}, `FeatureFlags` = '{json.dumps(self._feature_flags)}' WHERE `Id` = {self._id}; """ diff --git a/bot/src/bot_data/service/technician_config_repository_service.py b/bot/src/bot_data/service/technician_config_repository_service.py index 3500bbde..eb9293f7 100644 --- a/bot/src/bot_data/service/technician_config_repository_service.py +++ b/bot/src/bot_data/service/technician_config_repository_service.py @@ -46,11 +46,12 @@ class TechnicianConfigRepositoryService(TechnicianConfigRepositoryABC): result[2], result[3], result[4], - json.loads(result[5]), + result[5], + json.loads(result[6]), self._get_technician_ids(), self._get_technician_ping_urls(), - result[6], result[7], + result[8], id=result[0], ) diff --git a/bot/src/bot/startup_migration_extension.py b/bot/src/bot_data/startup_migration_extension.py similarity index 95% rename from bot/src/bot/startup_migration_extension.py rename to bot/src/bot_data/startup_migration_extension.py index 206262fb..8780d94f 100644 --- a/bot/src/bot/startup_migration_extension.py +++ b/bot/src/bot_data/startup_migration_extension.py @@ -20,6 +20,7 @@ from bot_data.migration.fix_updates_migration import FixUpdatesMigration from bot_data.migration.fix_user_history_migration import FixUserHistoryMigration from bot_data.migration.initial_migration import InitialMigration from bot_data.migration.level_migration import LevelMigration +from bot_data.migration.max_steam_offer_count_migration import MaxSteamOfferCountMigration from bot_data.migration.remove_stats_migration import RemoveStatsMigration from bot_data.migration.short_role_name_migration import ShortRoleNameMigration from bot_data.migration.short_role_name_only_highest_migration import ( @@ -68,3 +69,4 @@ class StartupMigrationExtension(StartupExtensionABC): services.add_transient(MigrationABC, FixUserHistoryMigration) # 10.10.2023 #401 - 1.2.0 services.add_transient(MigrationABC, BirthdayMigration) # 10.10.2023 #401 - 1.2.0 services.add_transient(MigrationABC, SteamSpecialOfferMigration) # 10.10.2023 #188 - 1.2.0 + services.add_transient(MigrationABC, MaxSteamOfferCountMigration) # 04.11.2023 #188 - 1.2.0 diff --git a/bot/src/bot_graphql/graphql/technicianConfig.gql b/bot/src/bot_graphql/graphql/technicianConfig.gql index dd91ceab..1c97490a 100644 --- a/bot/src/bot_graphql/graphql/technicianConfig.gql +++ b/bot/src/bot_graphql/graphql/technicianConfig.gql @@ -4,6 +4,7 @@ type TechnicianConfig implements TableWithHistoryQuery { waitForRestart: Int waitForShutdown: Int cacheMaxMessages: Int + maxSteamOfferCount: Int featureFlagCount: Int featureFlags: [FeatureFlag] pingURLs: [String] @@ -23,6 +24,7 @@ type TechnicianConfigHistory implements HistoryTableQuery { waitForRestart: Int waitForShutdown: Int cacheMaxMessages: Int + maxSteamOfferCount: Int featureFlagCount: Int featureFlags: [FeatureFlag] @@ -59,6 +61,7 @@ input TechnicianConfigInput { waitForRestart: Int waitForShutdown: Int cacheMaxMessages: Int + maxSteamOfferCount: Int 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 b520be37..28e947f9 100644 --- a/bot/src/bot_graphql/mutations/technician_config_mutation.py +++ b/bot/src/bot_graphql/mutations/technician_config_mutation.py @@ -59,6 +59,9 @@ class TechnicianConfigMutation(QueryABC): technician_config.cache_max_messages = ( input["cacheMaxMessages"] if "cacheMaxMessages" in input else technician_config.cache_max_messages ) + technician_config.max_steam_offer_count = ( + input["maxSteamOfferCount"] if "maxSteamOfferCount" in input else technician_config.max_steam_offer_count + ) old_feature_flags = technician_config.feature_flags technician_config.feature_flags = ( dict( diff --git a/bot/src/bot_graphql/queries/technician_config_history_query.py b/bot/src/bot_graphql/queries/technician_config_history_query.py index fb35ff2f..0db572bb 100644 --- a/bot/src/bot_graphql/queries/technician_config_history_query.py +++ b/bot/src/bot_graphql/queries/technician_config_history_query.py @@ -14,6 +14,7 @@ class TechnicianConfigHistoryQuery(HistoryQueryABC): self.set_field("waitForRestart", lambda config, *_: config.wait_for_restart) self.set_field("waitForShutdown", lambda config, *_: config.wait_for_shutdown) self.set_field("cacheMaxMessages", lambda config, *_: config.cache_max_messages) + self.set_field("maxSteamOfferCount", lambda config, *_: config.max_steam_offer_count) self.add_collection( "featureFlag", lambda config, *_: List( diff --git a/bot/src/bot_graphql/queries/technician_config_query.py b/bot/src/bot_graphql/queries/technician_config_query.py index d44d66ad..1fdb2643 100644 --- a/bot/src/bot_graphql/queries/technician_config_query.py +++ b/bot/src/bot_graphql/queries/technician_config_query.py @@ -27,6 +27,7 @@ class TechnicianConfigQuery(DataQueryWithHistoryABC): self.set_field("waitForRestart", lambda config, *_: config.wait_for_restart) self.set_field("waitForShutdown", lambda config, *_: config.wait_for_shutdown) self.set_field("cacheMaxMessages", lambda config, *_: config.cache_max_messages) + self.set_field("maxSteamOfferCount", lambda config, *_: config.max_steam_offer_count) self.add_collection( "featureFlag", lambda config, *_: List( diff --git a/bot/src/modules/special_offers/steam_offer_watcher.py b/bot/src/modules/special_offers/steam_offer_watcher.py index c5373249..d6dc3f29 100644 --- a/bot/src/modules/special_offers/steam_offer_watcher.py +++ b/bot/src/modules/special_offers/steam_offer_watcher.py @@ -21,6 +21,7 @@ from bot_data.abc.steam_special_offer_repository_abc import ( ) from bot_data.model.server_config import ServerConfig from bot_data.model.steam_special_offer import SteamSpecialOffer +from bot_data.model.technician_config import TechnicianConfig class SteamOfferWatcher(TaskABC): @@ -33,6 +34,7 @@ class SteamOfferWatcher(TaskABC): offers: SteamSpecialOfferRepositoryABC, message_service: MessageService, t: TranslatePipe, + tech_config: TechnicianConfig, ): TaskABC.__init__(self) @@ -43,6 +45,7 @@ class SteamOfferWatcher(TaskABC): self._bot = bot self._message_service = message_service self._t = t + self._tech_config = tech_config self._is_new = False self._urls = {} @@ -108,8 +111,7 @@ class SteamOfferWatcher(TaskABC): def _get_new_game_offers(self) -> List[SteamSpecialOffer]: new_offers = List(SteamSpecialOffer) - # sale_count = self._get_max_count() + 100 - sale_count = 150 + sale_count = self._tech_config.max_steam_offer_count # todo: let admins change the value self._logger.debug(__name__, f"Get special offers from 0 to {sale_count}") for i in range(0, sale_count, 100): diff --git a/web/package.json b/web/package.json index 524e7317..3ed303f5 100644 --- a/web/package.json +++ b/web/package.json @@ -53,4 +53,4 @@ "tslib": "^2.4.1", "typescript": "~4.9.5" } -} +} \ No newline at end of file diff --git a/web/src/app/models/config/technician-config.model.ts b/web/src/app/models/config/technician-config.model.ts index e70ae752..36be69b6 100644 --- a/web/src/app/models/config/technician-config.model.ts +++ b/web/src/app/models/config/technician-config.model.ts @@ -7,6 +7,7 @@ export interface TechnicianConfig extends DataWithHistory { waitForRestart?: number; waitForShutdown?: number; cacheMaxMessages?: number; + maxSteamOfferCount?: number; featureFlags: FeatureFlag[]; pingURLs: string[]; technicianIds: string[]; diff --git a/web/src/app/models/graphql/mutations.model.ts b/web/src/app/models/graphql/mutations.model.ts index 853d4178..9bffdc58 100644 --- a/web/src/app/models/graphql/mutations.model.ts +++ b/web/src/app/models/graphql/mutations.model.ts @@ -216,7 +216,7 @@ export class Mutations { `; static updateTechnicianConfig = ` - mutation updateTechnicianConfig($id: ID, $helpCommandReferenceUrl: String, $waitForRestart: Int, $waitForShutdown: Int, $cacheMaxMessages: Int, $featureFlags: [FeatureFlagInput], $pingURLs: [String], $technicianIds: [String]) { + mutation updateTechnicianConfig($id: ID, $helpCommandReferenceUrl: String, $waitForRestart: Int, $waitForShutdown: Int, $cacheMaxMessages: Int, $maxSteamOfferCount: Int, $featureFlags: [FeatureFlagInput], $pingURLs: [String], $technicianIds: [String]) { technicianConfig { updateTechnicianConfig(input: { id: $id, @@ -224,6 +224,7 @@ export class Mutations { waitForRestart: $waitForRestart, waitForShutdown: $waitForShutdown, cacheMaxMessages: $cacheMaxMessages, + maxSteamOfferCount: $maxSteamOfferCount, featureFlags: $featureFlags, pingURLs: $pingURLs, technicianIds: $technicianIds @@ -233,6 +234,7 @@ export class Mutations { waitForRestart waitForShutdown cacheMaxMessages + maxSteamOfferCount featureFlags { key value diff --git a/web/src/app/models/graphql/queries.model.ts b/web/src/app/models/graphql/queries.model.ts index 69cc22e6..f2a4f32a 100644 --- a/web/src/app/models/graphql/queries.model.ts +++ b/web/src/app/models/graphql/queries.model.ts @@ -497,6 +497,7 @@ export class Queries { waitForRestart waitForShutdown cacheMaxMessages + maxSteamOfferCount featureFlags { key value diff --git a/web/src/app/modules/admin/settings/components/settings/settings.component.html b/web/src/app/modules/admin/settings/components/settings/settings.component.html index 121cdc0f..c3370289 100644 --- a/web/src/app/modules/admin/settings/components/settings/settings.component.html +++ b/web/src/app/modules/admin/settings/components/settings/settings.component.html @@ -168,6 +168,15 @@ +
+
+
{{'admin.settings.bot.max_steam_offer_count' | translate}}:
+
+ +
+
+
+