[WIP] improved data loading #127
This commit is contained in:
parent
527d28ae39
commit
59f1183a50
@ -3,7 +3,7 @@ from cpl_core.configuration import ConfigurationABC
|
|||||||
from cpl_core.dependency_injection import ServiceProviderABC
|
from cpl_core.dependency_injection import ServiceProviderABC
|
||||||
from cpl_discord.service import DiscordBotServiceABC
|
from cpl_discord.service import DiscordBotServiceABC
|
||||||
|
|
||||||
from bot_core.configuration.bot_settings import BotSettings
|
from bot_core.configuration.bot_startup_settings import BotStartupSettings
|
||||||
|
|
||||||
|
|
||||||
class InitBotExtension(ApplicationExtensionABC):
|
class InitBotExtension(ApplicationExtensionABC):
|
||||||
@ -11,6 +11,6 @@ class InitBotExtension(ApplicationExtensionABC):
|
|||||||
ApplicationExtensionABC.__init__(self)
|
ApplicationExtensionABC.__init__(self)
|
||||||
|
|
||||||
async def run(self, config: ConfigurationABC, services: ServiceProviderABC):
|
async def run(self, config: ConfigurationABC, services: ServiceProviderABC):
|
||||||
settings = config.get_configuration(BotSettings)
|
settings: BotStartupSettings = config.get_configuration(BotStartupSettings)
|
||||||
|
|
||||||
bot: DiscordBotServiceABC = services.get_service(DiscordBotServiceABC, max_messages=settings.cache_max_messages)
|
bot: DiscordBotServiceABC = services.get_service(DiscordBotServiceABC, max_messages=settings.cache_max_messages)
|
||||||
|
@ -12,19 +12,13 @@ class BotSettings(ConfigurationModelABC):
|
|||||||
ConfigurationModelABC.__init__(self)
|
ConfigurationModelABC.__init__(self)
|
||||||
|
|
||||||
self._servers: List[ServerSettings] = List(ServerSettings)
|
self._servers: List[ServerSettings] = List(ServerSettings)
|
||||||
self._technicians: List[int] = List(int)
|
|
||||||
self._wait_for_restart = 2
|
self._wait_for_restart = 2
|
||||||
self._wait_for_shutdown = 2
|
self._wait_for_shutdown = 2
|
||||||
self._cache_max_messages = 1000
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def servers(self) -> List[ServerSettings]:
|
def servers(self) -> List[ServerSettings]:
|
||||||
return self._servers
|
return self._servers
|
||||||
|
|
||||||
@property
|
|
||||||
def technicians(self) -> List[int]:
|
|
||||||
return self._technicians
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def wait_for_restart(self) -> int:
|
def wait_for_restart(self) -> int:
|
||||||
return self._wait_for_restart
|
return self._wait_for_restart
|
||||||
@ -33,23 +27,13 @@ class BotSettings(ConfigurationModelABC):
|
|||||||
def wait_for_shutdown(self) -> int:
|
def wait_for_shutdown(self) -> int:
|
||||||
return self._wait_for_shutdown
|
return self._wait_for_shutdown
|
||||||
|
|
||||||
@property
|
|
||||||
def cache_max_messages(self) -> int:
|
|
||||||
return self._cache_max_messages
|
|
||||||
|
|
||||||
def from_dict(self, settings: dict):
|
def from_dict(self, settings: dict):
|
||||||
try:
|
try:
|
||||||
self._technicians = settings["Technicians"]
|
|
||||||
self._wait_for_restart = settings["WaitForRestart"]
|
self._wait_for_restart = settings["WaitForRestart"]
|
||||||
self._wait_for_shutdown = settings["WaitForShutdown"]
|
self._wait_for_shutdown = settings["WaitForShutdown"]
|
||||||
settings.pop("Technicians")
|
|
||||||
settings.pop("WaitForRestart")
|
settings.pop("WaitForRestart")
|
||||||
settings.pop("WaitForShutdown")
|
settings.pop("WaitForShutdown")
|
||||||
|
|
||||||
if "CacheMaxMessages" in settings:
|
|
||||||
self._cache_max_messages = settings["CacheMaxMessages"]
|
|
||||||
settings.pop("CacheMaxMessages")
|
|
||||||
|
|
||||||
servers = List(ServerSettings)
|
servers = List(ServerSettings)
|
||||||
for s in settings:
|
for s in settings:
|
||||||
st = ServerSettings()
|
st = ServerSettings()
|
||||||
|
29
kdb-bot/src/bot_core/configuration/bot_startup_settings.py
Normal file
29
kdb-bot/src/bot_core/configuration/bot_startup_settings.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import traceback
|
||||||
|
|
||||||
|
from cpl_core.configuration import ConfigurationModelABC
|
||||||
|
from cpl_core.console import Console
|
||||||
|
from cpl_query.extension import List
|
||||||
|
|
||||||
|
|
||||||
|
class BotStartupSettings(ConfigurationModelABC):
|
||||||
|
def __init__(self):
|
||||||
|
ConfigurationModelABC.__init__(self)
|
||||||
|
|
||||||
|
self._cache_max_messages = 1000
|
||||||
|
self._technicians: List[int] = List(int)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def cache_max_messages(self) -> int:
|
||||||
|
return self._cache_max_messages
|
||||||
|
|
||||||
|
@property
|
||||||
|
def technicians(self) -> List[int]:
|
||||||
|
return self._technicians
|
||||||
|
|
||||||
|
def from_dict(self, settings: dict):
|
||||||
|
try:
|
||||||
|
self._cache_max_messages = int(settings["CacheMaxMessages"])
|
||||||
|
self._technicians = settings["Technicians"]
|
||||||
|
except Exception as e:
|
||||||
|
Console.error(f"[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings")
|
||||||
|
Console.error(f"[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}")
|
@ -8,6 +8,8 @@ from bot_core.logging.database_logger import DatabaseLogger
|
|||||||
from bot_data.abc.data_seeder_abc import DataSeederABC
|
from bot_data.abc.data_seeder_abc import DataSeederABC
|
||||||
from bot_data.service.db_config import DBConfigService
|
from bot_data.service.db_config import DBConfigService
|
||||||
from bot_data.service.seeder_service import SeederService
|
from bot_data.service.seeder_service import SeederService
|
||||||
|
from modules.base.abc.base_helper_abc import BaseHelperABC
|
||||||
|
from modules.base.configuration.base_server_settings import BaseServerSettings
|
||||||
from modules.base.configuration.base_settings import BaseSettings
|
from modules.base.configuration.base_settings import BaseSettings
|
||||||
from modules.boot_log.configuration.boot_log_settings import BootLogSettings
|
from modules.boot_log.configuration.boot_log_settings import BootLogSettings
|
||||||
from modules.level.configuration.level_settings import LevelSettings
|
from modules.level.configuration.level_settings import LevelSettings
|
||||||
@ -23,6 +25,7 @@ class ConfigSeeder(DataSeederABC):
|
|||||||
bot: DiscordBotServiceABC,
|
bot: DiscordBotServiceABC,
|
||||||
db_context: DatabaseContextABC,
|
db_context: DatabaseContextABC,
|
||||||
db_config: DBConfigService,
|
db_config: DBConfigService,
|
||||||
|
base_helper: BaseHelperABC,
|
||||||
):
|
):
|
||||||
DataSeederABC.__init__(self)
|
DataSeederABC.__init__(self)
|
||||||
self._config = config
|
self._config = config
|
||||||
@ -32,8 +35,10 @@ class ConfigSeeder(DataSeederABC):
|
|||||||
self._bot = bot
|
self._bot = bot
|
||||||
self._db_context = db_context
|
self._db_context = db_context
|
||||||
self._db_config = db_config
|
self._db_config = db_config
|
||||||
|
self._base_helper = base_helper
|
||||||
|
|
||||||
async def seed(self):
|
async def seed(self):
|
||||||
|
self._logger.debug(__name__, f"Load config from database")
|
||||||
try:
|
try:
|
||||||
cfg = self._db_config.get_st_bot()
|
cfg = self._db_config.get_st_bot()
|
||||||
|
|
||||||
@ -69,3 +74,13 @@ class ConfigSeeder(DataSeederABC):
|
|||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._logger.fatal(__name__, "Cannot load config from db", e)
|
self._logger.fatal(__name__, "Cannot load config from db", e)
|
||||||
|
|
||||||
|
try:
|
||||||
|
for guild in self._bot.guilds:
|
||||||
|
settings: BaseServerSettings = self._base_helper.get_config(guild.id)
|
||||||
|
if settings is None:
|
||||||
|
self._db_config.create_config_for_guild(guild.id)
|
||||||
|
except Exception as e:
|
||||||
|
self._logger.fatal(__name__, "Cannot load or create guild config from db", e)
|
||||||
|
|
||||||
|
self._logger.info(__name__, f"Loaded config from database")
|
||||||
|
@ -300,6 +300,9 @@ class DatabaseOnReadyEvent(OnReadyABC):
|
|||||||
)
|
)
|
||||||
join.leaved_on = datetime.now()
|
join.leaved_on = datetime.now()
|
||||||
settings: BaseServerSettings = self._config.get_configuration(f"BaseServerSettings_{guild.id}")
|
settings: BaseServerSettings = self._config.get_configuration(f"BaseServerSettings_{guild.id}")
|
||||||
|
if settings is None:
|
||||||
|
self._logger.error(__name__, f"BaseServer settings for server {guild.id} not found!")
|
||||||
|
return
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(join.leaved_on - join.joined_on).total_seconds() / 60 / 60
|
(join.leaved_on - join.joined_on).total_seconds() / 60 / 60
|
||||||
|
@ -21,30 +21,17 @@ class ConfigMigration(MigrationABC):
|
|||||||
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||||
`WaitForRestart` BIGINT NOT NULL DEFAULT 8,
|
`WaitForRestart` BIGINT NOT NULL DEFAULT 8,
|
||||||
`WaitForShutdown` BIGINT NOT NULL DEFAULT 8,
|
`WaitForShutdown` BIGINT NOT NULL DEFAULT 8,
|
||||||
`CacheMaxMessages` BIGINT NOT NULL DEFAULT 1000000,
|
|
||||||
PRIMARY KEY(`Id`)
|
PRIMARY KEY(`Id`)
|
||||||
);
|
);
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
self._cursor.execute(
|
|
||||||
f"""
|
|
||||||
CREATE TABLE IF NOT EXISTS `ST_Technicians` (
|
|
||||||
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
|
||||||
`ST_BotId` BIGINT NOT NULL,
|
|
||||||
`DiscordId` BIGINT NOT NULL,
|
|
||||||
PRIMARY KEY(`Id`),
|
|
||||||
FOREIGN KEY (`ST_BotId`) REFERENCES `ST_Bot`(`Id`)
|
|
||||||
);
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
|
|
||||||
self._cursor.execute(
|
self._cursor.execute(
|
||||||
f"""
|
f"""
|
||||||
CREATE TABLE IF NOT EXISTS `ST_Servers` (
|
CREATE TABLE IF NOT EXISTS `ST_Servers` (
|
||||||
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||||
`ServerId` BIGINT NOT NULL,
|
`ServerId` BIGINT NOT NULL,
|
||||||
`MessageDeleteTimer` BIGINT NOT NULL,
|
`MessageDeleteTimer` BIGINT NOT NULL DEFAULT 4,
|
||||||
-- base
|
-- base
|
||||||
`MaxVoiceStateHours` BIGINT NOT NULL DEFAULT 6,
|
`MaxVoiceStateHours` BIGINT NOT NULL DEFAULT 6,
|
||||||
`XpPerMessage` BIGINT NOT NULL DEFAULT 1,
|
`XpPerMessage` BIGINT NOT NULL DEFAULT 1,
|
||||||
@ -125,7 +112,6 @@ class ConfigMigration(MigrationABC):
|
|||||||
def downgrade(self):
|
def downgrade(self):
|
||||||
self._logger.debug(__name__, "Running downgrade")
|
self._logger.debug(__name__, "Running downgrade")
|
||||||
self._cursor.execute("DROP TABLE `ST_Bot`;")
|
self._cursor.execute("DROP TABLE `ST_Bot`;")
|
||||||
self._cursor.execute("DROP TABLE `ST_Technicians`;")
|
|
||||||
self._cursor.execute("DROP TABLE `ST_Servers`;")
|
self._cursor.execute("DROP TABLE `ST_Servers`;")
|
||||||
self._cursor.execute("DROP TABLE `ST_Servers_AFKChannelIds`;")
|
self._cursor.execute("DROP TABLE `ST_Servers_AFKChannelIds`;")
|
||||||
self._cursor.execute("DROP TABLE `ST_Servers_PingURLs`;")
|
self._cursor.execute("DROP TABLE `ST_Servers_PingURLs`;")
|
||||||
|
@ -14,7 +14,7 @@ class DBConfigService:
|
|||||||
def get_st_bot(self) -> Optional[dict]:
|
def get_st_bot(self) -> Optional[dict]:
|
||||||
result = self._context.select(
|
result = self._context.select(
|
||||||
f"""
|
f"""
|
||||||
SELECT `Id`, `WaitForRestart`, `WaitForShutdown`, `CacheMaxMessages`
|
SELECT `Id`, `WaitForRestart`, `WaitForShutdown`
|
||||||
FROM ST_Bot
|
FROM ST_Bot
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
@ -25,22 +25,9 @@ class DBConfigService:
|
|||||||
# get first element
|
# get first element
|
||||||
result = result[0]
|
result = result[0]
|
||||||
|
|
||||||
technicians = self._context.select(
|
|
||||||
f"""
|
|
||||||
SELECT `DiscordId`
|
|
||||||
FROM ST_Technicians
|
|
||||||
WHERE ST_BotId = {result[0]};
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
technician_ids = []
|
|
||||||
for technician in technicians:
|
|
||||||
technician_ids.append(technician[0])
|
|
||||||
|
|
||||||
result_as_dict = {
|
result_as_dict = {
|
||||||
"WaitForRestart": result[1],
|
"WaitForRestart": result[1],
|
||||||
"WaitForShutdown": result[2],
|
"WaitForShutdown": result[2],
|
||||||
"CacheMaxMessages": result[3],
|
|
||||||
"Technicians": technician_ids,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
servers = self._context.select(
|
servers = self._context.select(
|
||||||
@ -138,3 +125,40 @@ class DBConfigService:
|
|||||||
settings = BotSettings()
|
settings = BotSettings()
|
||||||
settings.from_dict({})
|
settings.from_dict({})
|
||||||
return settings
|
return settings
|
||||||
|
|
||||||
|
def create_config_for_guild(self, guild_id: int):
|
||||||
|
server = self._context.select(
|
||||||
|
f"""
|
||||||
|
SELECT `ServerId`
|
||||||
|
FROM Servers
|
||||||
|
WHERE `DiscordServerId` = {guild_id};
|
||||||
|
"""
|
||||||
|
)[0]
|
||||||
|
|
||||||
|
servers = self._context.select(
|
||||||
|
f"""
|
||||||
|
SELECT *
|
||||||
|
FROM ST_Servers
|
||||||
|
WHERE `ServerId` = {server[0]}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(servers) > 0:
|
||||||
|
self._logger.fatal(__name__, f"Config for guild {guild_id} already exists")
|
||||||
|
return
|
||||||
|
|
||||||
|
self._context.cursor.execute(
|
||||||
|
f"""
|
||||||
|
INSERT INTO `ST_Servers` (`ServerId`)
|
||||||
|
VALUES
|
||||||
|
({server[0]});
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
st_server = self._context.select(
|
||||||
|
f"""
|
||||||
|
SELECT `Id`
|
||||||
|
FROM ST_Servers
|
||||||
|
WHERE `ServerId` = {server[0]};
|
||||||
|
"""
|
||||||
|
)[0]
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import discord
|
import discord
|
||||||
from cpl_core.logging import LoggerABC
|
|
||||||
from cpl_core.configuration import ConfigurationABC
|
from cpl_core.configuration import ConfigurationABC
|
||||||
|
from cpl_core.logging import LoggerABC
|
||||||
from cpl_discord.service import DiscordBotServiceABC
|
from cpl_discord.service import DiscordBotServiceABC
|
||||||
|
|
||||||
from bot_core.configuration.bot_settings import BotSettings
|
from bot_core.configuration.bot_startup_settings import BotStartupSettings
|
||||||
from modules.permission.abc.permission_service_abc import PermissionServiceABC
|
from modules.permission.abc.permission_service_abc import PermissionServiceABC
|
||||||
from modules.permission.configuration.permission_server_settings import (
|
from modules.permission.configuration.permission_server_settings import (
|
||||||
PermissionServerSettings,
|
PermissionServerSettings,
|
||||||
@ -16,7 +16,7 @@ class PermissionService(PermissionServiceABC):
|
|||||||
logger: LoggerABC,
|
logger: LoggerABC,
|
||||||
bot: DiscordBotServiceABC,
|
bot: DiscordBotServiceABC,
|
||||||
config: ConfigurationABC,
|
config: ConfigurationABC,
|
||||||
bot_settings: BotSettings,
|
bot_settings: BotStartupSettings,
|
||||||
):
|
):
|
||||||
PermissionServiceABC.__init__(self)
|
PermissionServiceABC.__init__(self)
|
||||||
self._logger = logger
|
self._logger = logger
|
||||||
|
Loading…
Reference in New Issue
Block a user