From d1ecfe96038b85bce1b898e90408a3a5e8b3e266 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 5 Nov 2023 15:34:17 +0100 Subject: [PATCH] Added activity score calculation #420 --- bot/src/bot_graphql/abc/query_abc.py | 12 +- bot/src/bot_graphql/graphql/server.gql | 8 - .../bot_graphql/graphql/serverStatistic.gql | 17 ++ .../bot_graphql/model/server_statistics.py | 7 + bot/src/bot_graphql/queries/server_query.py | 6 +- .../queries/server_statistic_query.py | 150 ++++++++++++++---- 6 files changed, 144 insertions(+), 56 deletions(-) create mode 100644 bot/src/bot_graphql/graphql/serverStatistic.gql create mode 100644 bot/src/bot_graphql/model/server_statistics.py diff --git a/bot/src/bot_graphql/abc/query_abc.py b/bot/src/bot_graphql/abc/query_abc.py index b141d89b..850fb22b 100644 --- a/bot/src/bot_graphql/abc/query_abc.py +++ b/bot/src/bot_graphql/abc/query_abc.py @@ -38,7 +38,7 @@ class QueryABC(ObjectType): def __init__(self, name: str): ObjectType.__init__(self, name) - def _get_wrapper(self, get_collection: Callable, filter_type: type = None): + def add_collection(self, name: str, get_collection: Callable, filter_type: type = None): def wrapper(*args, **kwargs): if filter_type is not None and "filter" in kwargs: kwargs["filter"] = FilterABC.get_collection_filter(filter_type, kwargs["filter"]) @@ -67,14 +67,8 @@ class QueryABC(ObjectType): return self._resolve_collection(collection, *args, **kwargs) - return wrapper - - def add_field(self, name: str, get_collection: Callable, filter_type: type = None): - self.set_field(name, self._get_wrapper(get_collection, filter_type)) - - def add_collection(self, name: str, get_collection: Callable, filter_type: type = None): - self.add_field(f"{name}s", get_collection, filter_type) - self.set_field(f"{name}Count", lambda *args: self._get_wrapper(get_collection, filter_type)(*args).count()) + self.set_field(f"{name}s", wrapper) + self.set_field(f"{name}Count", lambda *args: wrapper(*args).count()) def _can_user_see_element(self, user: AuthUser, element: T) -> bool: @ServiceProviderABC.inject diff --git a/bot/src/bot_graphql/graphql/server.gql b/bot/src/bot_graphql/graphql/server.gql index 38ee1dc8..be603f3a 100644 --- a/bot/src/bot_graphql/graphql/server.gql +++ b/bot/src/bot_graphql/graphql/server.gql @@ -61,12 +61,4 @@ input ServerFilter { id: ID discordId: String name: String -} - -type ServerStatistic { - userJoinedVoiceChannelCount: Int - userJoinedVoiceChannelOntime: Int - userJoinedGameServerCount: Int - userJoinedGameServerOntime: Int - messageCount: Int } \ No newline at end of file diff --git a/bot/src/bot_graphql/graphql/serverStatistic.gql b/bot/src/bot_graphql/graphql/serverStatistic.gql new file mode 100644 index 00000000..9f3b5c9f --- /dev/null +++ b/bot/src/bot_graphql/graphql/serverStatistic.gql @@ -0,0 +1,17 @@ +type ServerStatistic { + achievementsAchieved: Int + messageCount: Int + + userCount: Int + activeUserCount: Int + + userJoinedVoiceChannelCount: Int + userJoinedVoiceChannelOntime: Int + + userJoinedGameServerCount: Int + userJoinedGameServerOntime: Int + + userWarningCount: Int + + activityScore: Int +} \ No newline at end of file diff --git a/bot/src/bot_graphql/model/server_statistics.py b/bot/src/bot_graphql/model/server_statistics.py new file mode 100644 index 00000000..45ae580e --- /dev/null +++ b/bot/src/bot_graphql/model/server_statistics.py @@ -0,0 +1,7 @@ +from bot_data.model.server import Server + + +class ServerStatistics: + def __init__(self, server: Server, kwargs: dict): + self.server = server + self.kwargs = kwargs diff --git a/bot/src/bot_graphql/queries/server_query.py b/bot/src/bot_graphql/queries/server_query.py index b5203724..14033101 100644 --- a/bot/src/bot_graphql/queries/server_query.py +++ b/bot/src/bot_graphql/queries/server_query.py @@ -26,6 +26,7 @@ from bot_graphql.filter.client_filter import ClientFilter from bot_graphql.filter.level_filter import LevelFilter from bot_graphql.filter.short_role_name_filter import ShortRoleNameFilter from bot_graphql.filter.user_filter import UserFilter +from bot_graphql.model.server_statistics import ServerStatistics class ServerQuery(DataQueryWithHistoryABC): @@ -107,10 +108,7 @@ class ServerQuery(DataQueryWithHistoryABC): "hasFeatureFlag", lambda server, *_, **kwargs: self._resolve_has_feature_flag(server, *_, **kwargs), ) - self.set_field( - "statistic", - lambda server, *_, **kwargs: server, - ) + self.set_field("statistic", lambda server, *_, **kwargs: ServerStatistics(server, kwargs)) @staticmethod def resolve_id(server: Server, *_): diff --git a/bot/src/bot_graphql/queries/server_statistic_query.py b/bot/src/bot_graphql/queries/server_statistic_query.py index 7f01de89..7fa4d9c1 100644 --- a/bot/src/bot_graphql/queries/server_statistic_query.py +++ b/bot/src/bot_graphql/queries/server_statistic_query.py @@ -2,9 +2,12 @@ import datetime from cpl_core.configuration import ConfigurationABC +from bot_data.abc.achievement_repository_abc import AchievementRepositoryABC from bot_data.abc.user_joined_game_server_repository_abc import UserJoinedGameServerRepositoryABC from bot_data.abc.user_joined_voice_channel_repository_abc import UserJoinedVoiceChannelRepositoryABC from bot_data.abc.user_message_count_per_hour_repository_abc import UserMessageCountPerHourRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_data.abc.user_warnings_repository_abc import UserWarningsRepositoryABC from bot_data.model.server_config import ServerConfig from bot_graphql.abc.query_abc import QueryABC @@ -13,63 +16,140 @@ class ServerStatisticQuery(QueryABC): def __init__( self, config: ConfigurationABC, + users: UserRepositoryABC, user_joined_voice_channels: UserJoinedVoiceChannelRepositoryABC, user_joined_game_servers: UserJoinedGameServerRepositoryABC, user_messages: UserMessageCountPerHourRepositoryABC, + user_warnings: UserWarningsRepositoryABC, + achievements: AchievementRepositoryABC, ): QueryABC.__init__(self, "ServerStatistic") self._config = config - + self._users = users self._user_joined_voice_channels = user_joined_voice_channels self._user_joined_game_servers = user_joined_game_servers self._user_messages = user_messages + self._user_warnings = user_warnings + self._achievements = achievements - self.add_field("userJoinedVoiceChannelCount", self._resolve_voice_channel_count) - self.add_field("userJoinedVoiceChannelOntime", self._resolve_voice_channel_ontime) - self.add_field("userJoinedGameServerCount", self._resolve_game_server_count) - self.add_field("userJoinedGameServerOntime", self._resolve_game_server_ontime) - self.add_field( + self.set_field( + "achievementsAchieved", + self._resolve_achievements, + ) + + self.set_field( "messageCount", self._resolve_message_count, ) - def _resolve_voice_channel_count(self, server, *_, **kwargs): - return ( - self._user_joined_voice_channels.get_user_joined_voice_channels() - .where(lambda x: x.user.server.id == server.id and x.created_at.date() >= self._get_date(**kwargs)) - .count() + self.set_field("userCount", lambda server, *_: self._users.get_users_by_server_id(server.server.id).count()) + self.set_field("activeUserCount", self._resolve_active_count) + + self.set_field("userJoinedVoiceChannelCount", self._resolve_voice_channel_count) + self.set_field("userJoinedVoiceChannelOntime", self._resolve_voice_channel_ontime) + + self.set_field("userJoinedGameServerCount", self._resolve_game_server_count) + self.set_field("userJoinedGameServerOntime", self._resolve_game_server_ontime) + + self.set_field("userWarningCount", self._resolve_user_warning_count) + + self.set_field("activityScore", self._resolve_activity_score) + + def _resolve_activity_score(self, server, *_): + days = (datetime.date.today() - self._get_date(**server.kwargs)).days + return int( + ( + ( + self._resolve_achievements(server, *_) + + self._resolve_message_count(server, *_) + + self._resolve_voice_channel_count(server, *_) + + self._resolve_voice_channel_ontime(server, *_) + + self._resolve_game_server_count(server, *_) + + self._resolve_game_server_ontime(server, *_) + - self._resolve_user_warning_count(server, *_) + ) + / self._resolve_active_count(server, *_) + ) + / days + * 1000 ) - def _resolve_voice_channel_ontime(self, server, *_, **kwargs): - return ( - self._user_joined_voice_channels.get_user_joined_voice_channels() - .where(lambda x: x.user.server.id == server.id and x.created_at.date() >= self._get_date(**kwargs)) - .sum(lambda x: x.time) - ) - - def _resolve_game_server_count(self, server, *_, **kwargs): - return ( - self._user_joined_game_servers.get_user_joined_game_servers() - .where(lambda x: x.user.server.id == server.id and x.created_at.date() >= self._get_date(**kwargs)) - .count() - ) - - def _resolve_game_server_ontime(self, server, *_, **kwargs): - return ( - self._user_joined_game_servers.get_user_joined_game_servers() - .where(lambda x: x.user.server.id == server.id and x.created_at.date() >= self._get_date(**kwargs)) - .sum(lambda x: x.time) - ) - - def _resolve_message_count(self, server, *_, **kwargs): - settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{server.discord_id}") + def _resolve_message_count(self, server, *_): + settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{server.server.discord_id}") return ( self._user_messages.get_user_message_count_per_hours() - .where(lambda x: x.user.server.id == server.id and x.created_at.date() >= self._get_date(**kwargs)) + .where( + lambda x: x.user.server.id == server.server.id + and x.created_at.date() >= self._get_date(**server.kwargs) + ) .sum(lambda x: x.xp_count / settings.xp_per_message) ) + def _resolve_active_count(self, server, *_): + return self._users.get_users_by_server_id(server.server.id).where(lambda x: not x.left_server).count() + + def _resolve_voice_channel_count(self, server, *_): + return ( + self._user_joined_voice_channels.get_user_joined_voice_channels() + .where( + lambda x: x.user.server.id == server.server.id + and x.created_at.date() >= self._get_date(**server.kwargs) + ) + .count() + ) + + def _resolve_voice_channel_ontime(self, server, *_): + return ( + self._user_joined_voice_channels.get_user_joined_voice_channels() + .where( + lambda x: x.user.server.id == server.server.id + and x.created_at.date() >= self._get_date(**server.kwargs) + ) + .sum(lambda x: x.time) + ) + + def _resolve_game_server_count(self, server, *_): + return ( + self._user_joined_game_servers.get_user_joined_game_servers() + .where( + lambda x: x.user.server.id == server.server.id + and x.created_at.date() >= self._get_date(**server.kwargs) + ) + .count() + ) + + def _resolve_game_server_ontime(self, server, *_): + return ( + self._user_joined_game_servers.get_user_joined_game_servers() + .where( + lambda x: x.user.server.id == server.server.id + and x.created_at.date() >= self._get_date(**server.kwargs) + ) + .sum(lambda x: x.time) + ) + + def _resolve_user_warning_count(self, server, *_): + return ( + self._user_warnings.get_user_warnings() + .where( + lambda x: x.user.server.id == server.server.id + and x.created_at.date() >= self._get_date(**server.kwargs) + ) + .count() + ) + + def _resolve_achievements(self, server, *_): + return ( + self._achievements.get_achievements_by_server_id(server.server.id) + .select_many(lambda x: self._achievements.get_user_got_achievements_by_achievement_id(x.id)) + .where( + lambda x: x.user.server.id == server.server.id + and x.created_at.date() >= self._get_date(**server.kwargs) + ) + .count() + ) + def _get_date(self, **kwargs) -> datetime.date: if "date" not in kwargs: return datetime.date.today() - datetime.timedelta(days=7)