Improved data integrity service loop #446

This commit is contained in:
Sven Heidemann 2023-12-03 13:14:10 +01:00
parent 4233e089f8
commit 94732b7227
6 changed files with 225 additions and 348 deletions

View File

@ -1,5 +1,5 @@
from datetime import datetime from datetime import datetime
from typing import Callable from typing import Callable, Union
import discord import discord
from cpl_core.configuration import ConfigurationABC from cpl_core.configuration import ConfigurationABC
@ -25,6 +25,7 @@ from bot_data.abc.user_joined_voice_channel_repository_abc import (
from bot_data.abc.user_message_count_per_hour_repository_abc import ( from bot_data.abc.user_message_count_per_hour_repository_abc import (
UserMessageCountPerHourRepositoryABC, UserMessageCountPerHourRepositoryABC,
) )
from bot_data.model.auto_role_rule import AutoRoleRule
from bot_data.model.server_config import ServerConfig from bot_data.model.server_config import ServerConfig
from bot_data.model.user import User from bot_data.model.user import User
from bot_data.model.user_message_count_per_hour import UserMessageCountPerHour from bot_data.model.user_message_count_per_hour import UserMessageCountPerHour
@ -218,7 +219,7 @@ class ClientUtilsService(ClientUtilsABC):
guild: Guild = self._bot.guilds.where(lambda g: g == guild).single() guild: Guild = self._bot.guilds.where(lambda g: g == guild).single()
channel = guild.get_channel(discord_channel_id) channel = guild.get_channel(discord_channel_id)
message = await channel.fetch_message(discord_message_id) message = await channel.fetch_message(discord_message_id)
emoji = List(discord.Emoji, guild.emojis).where(lambda x: x.name == rule.emoji_name).single() emoji = List(discord.Emoji, [x for x in guild.emojis if x.name == rule.emoji_name]).single()
if emoji is None: if emoji is None:
self._logger.debug(__name__, f"Emoji {rule.emoji_name} not found") self._logger.debug(__name__, f"Emoji {rule.emoji_name} not found")

View File

@ -1,9 +1,8 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Union
import discord
from cpl_core.configuration import ConfigurationABC from cpl_core.configuration import ConfigurationABC
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
from cpl_discord.container import Member, Guild
from cpl_discord.service import DiscordBotServiceABC from cpl_discord.service import DiscordBotServiceABC
from bot_core.abc.client_utils_abc import ClientUtilsABC from bot_core.abc.client_utils_abc import ClientUtilsABC
@ -66,74 +65,76 @@ class DataIntegrityService:
self._is_for_shutdown = False self._is_for_shutdown = False
def _check_known_users(self): async def check_data_integrity(self, is_for_shutdown=False):
self._logger.debug(__name__, f"Start checking KnownUsers table, {len(self._bot.users)}") self._logger.info(__name__, f"Data integrity service started")
for u in self._bot.users: if is_for_shutdown != self._is_for_shutdown:
u: discord.User = u self._is_for_shutdown = is_for_shutdown
try: try:
if u.bot:
self._logger.trace(__name__, f"User {u.id} is ignored, because its a bot")
continue
user = self._known_users.find_user_by_discord_id(u.id)
if user is not None:
continue
self._logger.warn(__name__, f"Unknown user: {u.id}")
self._logger.debug(__name__, f"Add user: {u.id}")
self._known_users.add_user(KnownUser(u.id))
self._db_context.save_changes()
user = self._known_users.find_user_by_discord_id(u.id)
if user is None:
self._logger.fatal(__name__, f"Cannot add user: {u.id}")
self._logger.debug(__name__, f"Added user: {u.id}")
except Exception as e:
self._logger.error(__name__, f"Cannot get user", e)
def check_servers(self):
self._logger.debug(__name__, f"Start checking Servers table")
for g in self._bot.guilds: for g in self._bot.guilds:
g: discord.Guild = g self._logger.debug(__name__, f"Start check for server: {g.id}")
try: s = self._get_or_create_server(g)
server = self._servers.find_server_by_discord_id(g.id) self._logger.debug(__name__, f"Start check for clients")
if server is not None: self._check_clients(g.id, s)
for m in [m for m in g.members if not m.bot]:
await self._check_default_role(m)
self._check_known_user(m.id)
self._logger.debug(__name__, f"Start check for member: {g.id}@{m.id}")
u = self._get_or_create_user(s, m.id)
self._logger.debug(__name__, f"Start check for user joined server: {g.id}@{m.id}")
self._check_user_join(g, m, u)
self._logger.debug(__name__, f"Start check for user joined voice channels: {g.id}@{m.id}")
self._check_user_joined_vc(g.id, m, u)
self._logger.debug(__name__, f"Start check for user joined game servers: {g.id}@{m.id}")
self._check_user_joined_gs(g.id, m.id, u)
self._logger.debug(__name__, f"Start check for user got achievements: {g.id}@{m.id}")
await self._check_for_user_achievements(u)
for m in [m for m in g.members if m.bot]:
u = self._users.find_user_by_discord_id_and_server_id(m.id, s.id)
if u is None:
continue continue
self._logger.warn(__name__, f"Server not found in database: {g.id}") self._remove_bot(u)
self._logger.debug(__name__, f"Add server: {g.id}") self._logger.info(__name__, f"Data integrity service finished")
self._servers.add_server(Server(g.id)) except Exception as e:
self._logger.fatal(__name__, f"Checking data integrity failed", e)
def _get_or_create_server(self, guild: Guild) -> Server:
try:
server = self._servers.find_server_by_discord_id(guild.id)
if server is not None:
return server
self._logger.warn(__name__, f"Server not found in database: {guild.id}")
self._logger.debug(__name__, f"Add server: {guild.id}")
self._servers.add_server(Server(guild.id))
self._db_context.save_changes() self._db_context.save_changes()
server = self._servers.find_server_by_discord_id(g.id) server = self._servers.find_server_by_discord_id(guild.id)
if server is None: if server is None:
self._logger.fatal(__name__, f"Cannot add server: {g.id}") self._logger.fatal(__name__, f"Cannot add server: {guild.id}")
self._logger.debug(__name__, f"Added server: {g.id}") self._logger.trace(__name__, f"Added server: {guild.id}")
return server
except Exception as e: except Exception as e:
self._logger.error(__name__, f"Cannot get server", e) self._logger.error(__name__, f"Cannot get server", e)
results = self._servers.get_servers() def _check_clients(self, guild_id: int, server: Server):
if results is None or len(results) == 0:
self._logger.error(__name__, f"Table Servers is empty!")
def _check_clients(self):
self._logger.debug(__name__, f"Start checking Clients table")
for g in self._bot.guilds:
g: discord.Guild = g
try: try:
server: Server = self._servers.find_server_by_discord_id(g.id)
if server is None:
self._logger.fatal(__name__, f"Server not found in database: {g.id}")
client = self._clients.find_client_by_server_id(server.id) client = self._clients.find_client_by_server_id(server.id)
if client is not None: if client is not None:
continue return
self._logger.warn( self._logger.warn(
__name__, __name__,
f"Client for server {g.id} not found in database: {self._bot.user.id}", f"Client for server {guild_id} not found in database: {self._bot.user.id}",
) )
self._logger.debug(__name__, f"Add client: {self._bot.user.id}") self._logger.debug(__name__, f"Add client: {self._bot.user.id}")
self._clients.add_client(Client(self._bot.user.id, 0, 0, 0, 0, 0, server)) self._clients.add_client(Client(self._bot.user.id, 0, 0, 0, 0, 0, server))
@ -143,10 +144,10 @@ class DataIntegrityService:
if client is None: if client is None:
self._logger.fatal( self._logger.fatal(
__name__, __name__,
f"Cannot add client {self._bot.user.id} for server {g.id}", f"Cannot add client {self._bot.user.id} for server {guild_id}",
) )
self._logger.debug(__name__, f"Added client: {g.id}") self._logger.trace(__name__, f"Added client: {guild_id}")
except Exception as e: except Exception as e:
self._logger.error(__name__, f"Cannot get client", e) self._logger.error(__name__, f"Cannot get client", e)
@ -154,32 +155,37 @@ class DataIntegrityService:
if results is None or len(results) == 0: if results is None or len(results) == 0:
self._logger.error(__name__, f"Table Servers is empty!") self._logger.error(__name__, f"Table Servers is empty!")
def _check_users(self): def _check_known_user(self, member_id: int):
self._logger.debug(__name__, f"Start checking Users table")
for g in self._bot.guilds:
g: discord.Guild = g
try: try:
server = self._servers.find_server_by_discord_id(g.id) if self._known_users.find_user_by_discord_id(member_id) is not None:
if server is None: return
self._logger.fatal(__name__, f"Server not found in database: {g.id}")
for u in g.members: self._logger.warn(__name__, f"Unknown user: {member_id}")
u: Union[discord.Member, discord.User] = u self._logger.trace(__name__, f"Add known user: {member_id}")
if u.bot: self._known_users.add_user(KnownUser(member_id))
self._logger.trace(__name__, f"User {u.id} is ignored, because its a bot")
continue
user = self._users.find_user_by_discord_id_and_server_id(u.id, server.id)
if user is not None:
continue
self._logger.warn(__name__, f"User not found in database: {u.id}")
self._logger.debug(__name__, f"Add user: {u.id}")
self._users.add_user(User(u.id, 0, 0, 0, None, server))
self._db_context.save_changes() self._db_context.save_changes()
self._logger.debug(__name__, f"Added User: {u.id}") user = self._known_users.find_user_by_discord_id(member_id)
if user is None:
self._logger.fatal(__name__, f"Cannot add user: {member_id}")
self._logger.trace(__name__, f"Added known user: {member_id}")
except Exception as e:
self._logger.error(__name__, f"Cannot get user", e)
def _get_or_create_user(self, server: Server, member_id: int) -> User:
try:
user = self._users.find_user_by_discord_id_and_server_id(member_id, server.id)
if user is not None:
return user
self._logger.warn(__name__, f"User not found in database: {member_id}")
self._logger.debug(__name__, f"Add user: {member_id}")
self._users.add_user(User(member_id, 0, 0, 0, None, server))
self._db_context.save_changes()
self._logger.trace(__name__, f"Added User: {member_id}")
return self._users.get_user_by_discord_id_and_server_id(member_id, server.id)
except Exception as e: except Exception as e:
self._logger.error(__name__, f"Cannot get User", e) self._logger.error(__name__, f"Cannot get User", e)
@ -187,63 +193,34 @@ class DataIntegrityService:
if results is None or len(results) == 0: if results is None or len(results) == 0:
self._logger.error(__name__, f"Table Users is empty!") self._logger.error(__name__, f"Table Users is empty!")
def _check_user_joins(self): def _check_user_join(self, guild: Guild, member: Member, user: User):
self._logger.debug(__name__, f"Start checking UserJoinedServers table")
for guild in self._bot.guilds:
guild: discord.Guild = guild
server = self._servers.find_server_by_discord_id(guild.id)
if server is None:
self._logger.fatal(__name__, f"Server not found in database: {guild.id}")
try: try:
for u in guild.members:
u: discord.User = u
if u.bot:
self._logger.trace(__name__, f"User {u.id} is ignored, because its a bot")
continue
user = self._users.find_user_by_discord_id_and_server_id(u.id, server.id)
if user is None:
self._logger.fatal(__name__, f"User not found in database: {u.id}")
join = self._user_joins.find_active_user_joined_server_by_user_id(user.id) join = self._user_joins.find_active_user_joined_server_by_user_id(user.id)
if join is not None: if join is not None:
continue return
m: discord.Member = u
self._logger.warn( self._logger.warn(
__name__, __name__,
f"Active UserJoinedServer not found in database: {guild.id}:{u.id}@{m.joined_at}", f"Active UserJoinedServer not found in database: {guild.id}:{member.id}@{member.joined_at}",
) )
self._logger.debug( self._logger.debug(
__name__, __name__,
f"Add UserJoinedServer: {guild.id}:{u.id}@{m.joined_at}", f"Add UserJoinedServer: {guild.id}:{member.id}@{member.joined_at}",
)
self._user_joins.add_user_joined_server(
UserJoinedServer(user, self._dtp.transform(m.joined_at), None)
) )
self._user_joins.add_user_joined_server(UserJoinedServer(user, self._dtp.transform(member.joined_at), None))
self._db_context.save_changes() self._db_context.save_changes()
self._logger.debug(__name__, f"Added UserJoinedServer: {u.id}") self._logger.trace(__name__, f"Added UserJoinedServer: {member.id}")
except Exception as e: except Exception as e:
self._logger.error(__name__, f"Cannot get UserJoinedServer", e) self._logger.error(__name__, f"Cannot get UserJoinedServer", e)
results = self._users.get_users() try:
if results is None or len(results) == 0:
self._logger.error(__name__, f"Table Users is empty!")
joins = self._user_joins.get_user_joined_servers() joins = self._user_joins.get_user_joined_servers()
for join in joins: for join in [x for x in joins if x.user.server.discord_id == guild.id and x.leaved_on is None]:
join: UserJoinedServer = join
if join.user.server.discord_id != guild.id:
continue
if join.leaved_on is not None:
continue
dc_user = guild.get_member(join.user.discord_id) dc_user = guild.get_member(join.user.discord_id)
if dc_user is None: if dc_user is not None:
continue
self._logger.warn( self._logger.warn(
__name__, __name__,
f"User {join.user.discord_id} already left the server.", f"User {join.user.discord_id} already left the server.",
@ -252,42 +229,26 @@ class DataIntegrityService:
self._user_joins.update_user_joined_server(join) self._user_joins.update_user_joined_server(join)
self._db_context.save_changes() self._db_context.save_changes()
except Exception as e:
self._logger.error(__name__, f"Cannot update UserJoinedServer", e)
def _check_user_joins_vc(self): def _check_user_joined_vc(self, guild_id: int, member: Member, user: User):
self._logger.debug(__name__, f"Start checking UserJoinedVoiceChannel table") settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{guild_id}")
for guild in self._bot.guilds:
guild: discord.Guild = guild
settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{guild.id}")
server = self._servers.find_server_by_discord_id(guild.id)
if server is None:
self._logger.fatal(__name__, f"Server not found in database: {guild.id}")
try: try:
# close open voice states # close open voice states
for member in guild.members:
if member.bot:
self._logger.trace(__name__, f"User {member.id} is ignored, because its a bot")
continue
user = self._users.find_user_by_discord_id_and_server_id(member.id, server.id)
if user is None:
self._logger.fatal(__name__, f"User not found in database: {member.id}")
joins = self._user_joins_vc.find_active_user_joined_voice_channels_by_user_id(user.id) joins = self._user_joins_vc.find_active_user_joined_voice_channels_by_user_id(user.id)
if joins is None or len(joins) == 0: if joins is None or len(joins) == 0:
continue return
for join in joins: for join in joins:
self._logger.warn( self._logger.warn(
__name__, __name__,
f"Active UserJoinedVoiceChannel found in database: {guild.id}:{member.id}@{join.joined_on}", f"Active UserJoinedVoiceChannel found in database: {guild_id}:{member.id}@{join.joined_on}",
) )
join.leaved_on = datetime.now() join.leaved_on = datetime.now()
if ( if ((join.leaved_on - join.joined_on).total_seconds() / 60 / 60) > settings.max_voice_state_hours:
(join.leaved_on - join.joined_on).total_seconds() / 60 / 60
) > settings.max_voice_state_hours:
join.leaved_on = join.joined_on + timedelta(hours=settings.max_voice_state_hours) join.leaved_on = join.joined_on + timedelta(hours=settings.max_voice_state_hours)
self._user_joins_vc.update_user_joined_voice_channel(join) self._user_joins_vc.update_user_joined_voice_channel(join)
@ -301,59 +262,30 @@ class DataIntegrityService:
return return
# add open voice states # add open voice states
for member in guild.members:
if member.bot:
self._logger.trace(__name__, f"User {member.id} is ignored, because its a bot")
continue
if member.voice is None or member.voice.channel.id in settings.afk_channel_ids: if member.voice is None or member.voice.channel.id in settings.afk_channel_ids:
continue return
user = self._users.find_user_by_discord_id_and_server_id(member.id, server.id)
if user is None:
self._logger.fatal(__name__, f"User not found in database: {member.id}")
join = UserJoinedVoiceChannel(user, member.voice.channel.id, datetime.now()) join = UserJoinedVoiceChannel(user, member.voice.channel.id, datetime.now())
self._user_joins_vc.add_user_joined_voice_channel(join) self._user_joins_vc.add_user_joined_voice_channel(join)
self._db_context.save_changes() self._db_context.save_changes()
except Exception as e: except Exception as e:
self._logger.error(__name__, f"Cannot get UserJoinedVoiceChannel", e) self._logger.error(__name__, f"Cannot get UserJoinedVoiceChannel", e)
def _check_user_joined_gs(self): def _check_user_joined_gs(self, guild_id: int, member_id: int, user: User):
self._logger.debug(__name__, f"Start checking UserJoinedGameServer table")
for guild in self._bot.guilds:
guild: discord.Guild = guild
server = self._servers.find_server_by_discord_id(guild.id)
if server is None:
self._logger.fatal(__name__, f"Server not found in database: {guild.id}")
try: try:
for member in guild.members:
if member.bot:
self._logger.trace(__name__, f"User {member.id} is ignored, because its a bot")
continue
user = self._users.find_user_by_discord_id_and_server_id(member.id, server.id)
if user is None:
self._logger.fatal(__name__, f"User not found in database: {member.id}")
joins = self._user_joined_gs.find_active_user_joined_game_servers_by_user_id(user.id) joins = self._user_joined_gs.find_active_user_joined_game_servers_by_user_id(user.id)
if joins is None or len(joins) == 0: if joins is None or len(joins) == 0:
continue return
for join in joins: for join in joins:
self._logger.warn( self._logger.warn(
__name__, __name__,
f"Active UserJoinedGameServer found in database: {guild.id}:{member.id}@{join.joined_on}", f"Active UserJoinedGameServer found in database: {guild_id}:{member_id}@{join.joined_on}",
) )
join.leaved_on = datetime.now() join.leaved_on = datetime.now()
settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{guild.id}") settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{guild_id}")
if ( if join.time > settings.max_voice_state_hours:
(join.leaved_on - join.joined_on).total_seconds() / 60 / 60
) > settings.max_voice_state_hours:
join.leaved_on = join.joined_on + timedelta(hours=settings.max_voice_state_hours) join.leaved_on = join.joined_on + timedelta(hours=settings.max_voice_state_hours)
self._user_joined_gs.update_user_joined_game_server(join) self._user_joined_gs.update_user_joined_game_server(join)
@ -365,38 +297,19 @@ class DataIntegrityService:
except Exception as e: except Exception as e:
self._logger.error(__name__, f"Cannot get UserJoinedGameServer", e) self._logger.error(__name__, f"Cannot get UserJoinedGameServer", e)
async def _check_for_user_achievements(self): async def _check_for_user_achievements(self, user: User):
self._logger.debug(__name__, f"Start checking UserGotAchievement table") try:
for guild in self._bot.guilds:
server = self._servers.find_server_by_discord_id(guild.id)
if server is None:
self._logger.fatal(__name__, f"Server not found in database: {guild.id}")
for member in guild.members:
if member.bot:
self._logger.trace(__name__, f"User {member.id} is ignored, because its a bot")
continue
user = self._users.find_user_by_discord_id_and_server_id(member.id, server.id)
if user is None:
self._logger.fatal(__name__, f"User not found in database: {member.id}")
await self._achievements.validate_achievements_for_user(user) await self._achievements.validate_achievements_for_user(user)
except Exception as e:
self._logger.error(__name__, f"Cannot check UserGotAchievement for {user.id}", e)
async def _check_default_role(self): async def _check_default_role(self, member: Member):
for guild in self._bot.guilds:
for member in guild.members:
await self._client_utils.check_default_role(member) await self._client_utils.check_default_role(member)
def _check_for_bots(self): def _remove_bot(self, user: User):
for guild in self._bot.guilds: known_user = self._known_users.find_user_by_discord_id(user.discord_id)
server = self._servers.get_server_by_discord_id(guild.id) if known_user is not None:
self._known_users.delete_user(known_user)
for member in guild.members.where(lambda x: x.bot):
user = self._users.find_user_by_discord_id_and_server_id(member.id, server.id)
if user is None:
continue
for join in self._user_joins.get_user_joined_servers_by_user_id(user.id): for join in self._user_joins.get_user_joined_servers_by_user_id(user.id):
self._user_joins.delete_user_joined_server(join) self._user_joins.delete_user_joined_server(join)
@ -404,18 +317,3 @@ class DataIntegrityService:
self._user_joins_vc.delete_user_joined_voice_channel_by_user_id(user.id) self._user_joins_vc.delete_user_joined_voice_channel_by_user_id(user.id)
self._users.delete_user(user) self._users.delete_user(user)
self._db_context.save_changes() self._db_context.save_changes()
async def check_data_integrity(self, is_for_shutdown=False):
if is_for_shutdown != self._is_for_shutdown:
self._is_for_shutdown = is_for_shutdown
await self._check_default_role()
self._check_known_users()
self.check_servers()
self._check_clients()
self._check_users()
self._check_user_joins()
self._check_user_joins_vc()
self._check_user_joined_gs()
await self._check_for_user_achievements()
self._check_for_bots()

View File

@ -19,10 +19,6 @@ class UserJoinedServerRepositoryABC(ABC):
def get_user_joined_server_by_id(self, id: int) -> UserJoinedServer: def get_user_joined_server_by_id(self, id: int) -> UserJoinedServer:
pass pass
@abstractmethod
def get_user_joined_server_by_server_id(self, server_id: int) -> UserJoinedServer:
pass
@abstractmethod @abstractmethod
def get_user_joined_servers_by_user_id(self, user_id: int) -> list[UserJoinedServer]: def get_user_joined_servers_by_user_id(self, user_id: int) -> list[UserJoinedServer]:
pass pass

View File

@ -67,15 +67,6 @@ class UserJoinedServer(TableABC):
""" """
) )
@staticmethod
def get_select_by_server_id_string(id: int) -> str:
return str(
f"""
SELECT * FROM `UserJoinedServers`
WHERE `ServerId` = {id};
"""
)
@staticmethod @staticmethod
def get_select_by_user_id_string(id: int) -> str: def get_select_by_user_id_string(id: int) -> str:
return str( return str(

View File

@ -48,13 +48,6 @@ class UserJoinedServerRepositoryService(UserJoinedServerRepositoryABC):
) )
return self.__from_result(self._context.select(UserJoinedServer.get_select_by_id_string(id))[0]) return self.__from_result(self._context.select(UserJoinedServer.get_select_by_id_string(id))[0])
def get_user_joined_server_by_server_id(self, server_id: int) -> UserJoinedServer:
self._logger.trace(
__name__,
f"Send SQL command: {UserJoinedServer.get_select_by_server_id_string(server_id)}",
)
return self.__from_result(self._context.select(UserJoinedServer.get_select_by_id_string(server_id))[0])
def get_user_joined_servers_by_user_id(self, user_id: int) -> List[UserJoinedServer]: def get_user_joined_servers_by_user_id(self, user_id: int) -> List[UserJoinedServer]:
self._logger.trace( self._logger.trace(
__name__, __name__,

View File

@ -28,7 +28,5 @@ class ConfigOnReadyEvent(OnReadyABC):
self._data_integrity_service = data_integrity_service self._data_integrity_service = data_integrity_service
async def on_ready(self): async def on_ready(self):
self._data_integrity_service.check_servers()
for guild in self._bot.guilds: for guild in self._bot.guilds:
await self._config_service.reload_server_config(self._servers.get_server_by_discord_id(guild.id)) await self._config_service.reload_server_config(self._servers.get_server_by_discord_id(guild.id))