diff --git a/bot/src/bot_graphql/graphql/server.gql b/bot/src/bot_graphql/graphql/server.gql index 1e80be20..be603f3a 100644 --- a/bot/src/bot_graphql/graphql/server.gql +++ b/bot/src/bot_graphql/graphql/server.gql @@ -38,6 +38,8 @@ type Server implements TableWithHistoryQuery { config: ServerConfig hasFeatureFlag(flag: String): FeatureFlag + statistic(date: String): ServerStatistic + createdAt: String modifiedAt: String 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/graphql_module.py b/bot/src/bot_graphql/graphql_module.py index e15e0c35..95d73f7b 100644 --- a/bot/src/bot_graphql/graphql_module.py +++ b/bot/src/bot_graphql/graphql_module.py @@ -57,6 +57,7 @@ from bot_graphql.queries.level_query import LevelQuery from bot_graphql.queries.server_config_query import ServerConfigQuery from bot_graphql.queries.server_history_query import ServerHistoryQuery from bot_graphql.queries.server_query import ServerQuery +from bot_graphql.queries.server_statistic_query import ServerStatisticQuery from bot_graphql.queries.short_role_name_history_query import ShortRoleNameHistoryQuery from bot_graphql.queries.short_role_name_query import ShortRoleNameQuery from bot_graphql.queries.technician_config_history_query import ( @@ -138,6 +139,7 @@ class GraphQLModule(ModuleABC): services.add_transient(QueryABC, ShortRoleNameQuery) services.add_transient(QueryABC, UserWarningHistoryQuery) services.add_transient(QueryABC, UserWarningQuery) + services.add_transient(QueryABC, ServerStatisticQuery) services.add_transient(QueryABC, DiscordQuery) services.add_transient(QueryABC, GuildQuery) 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 1398fa08..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,6 +108,7 @@ class ServerQuery(DataQueryWithHistoryABC): "hasFeatureFlag", lambda server, *_, **kwargs: self._resolve_has_feature_flag(server, *_, **kwargs), ) + 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 new file mode 100644 index 00000000..7fa4d9c1 --- /dev/null +++ b/bot/src/bot_graphql/queries/server_statistic_query.py @@ -0,0 +1,157 @@ +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 + + +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.set_field( + "achievementsAchieved", + self._resolve_achievements, + ) + + self.set_field( + "messageCount", + self._resolve_message_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_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.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) + + return datetime.datetime.strptime(kwargs["date"], "%d.%m.%Y").date() diff --git a/web/src/app/models/data/server.model.ts b/web/src/app/models/data/server.model.ts index 024a1a6e..9c9891c3 100644 --- a/web/src/app/models/data/server.model.ts +++ b/web/src/app/models/data/server.model.ts @@ -20,6 +20,7 @@ export interface Server extends Data { autoRoles?: AutoRole[]; clientCount?: number; clients?: Client[]; + statistic?: ServerStatistic; levelCount?: number; levels?: Level[]; userCount?: number; @@ -35,3 +36,16 @@ export interface ServerFilter { discordId?: String; name?: String; } + +export interface ServerStatistic { + achievementsAchieved?: Number + messageCount?: Number + userCount?: Number + activeUserCount?: Number + userJoinedVoiceChannelCount?: Number + userJoinedVoiceChannelOntime?: Number + userJoinedGameServerCount?: Number + userJoinedGameServerOntime?: Number + userWarningCount?: Number + activityScore?: Number +} diff --git a/web/src/app/models/graphql/queries.model.ts b/web/src/app/models/graphql/queries.model.ts index d16e94c1..1f6bb6a9 100644 --- a/web/src/app/models/graphql/queries.model.ts +++ b/web/src/app/models/graphql/queries.model.ts @@ -79,6 +79,18 @@ export class Queries { receivedCommandCount movedUsersCount } + statistic { + achievementsAchieved + messageCount + userCount + activeUserCount + userJoinedVoiceChannelCount + userJoinedVoiceChannelOntime + userJoinedGameServerCount + userJoinedGameServerOntime + userWarningCount + activityScore + } } } `; diff --git a/web/src/app/modules/view/server/server-dashboard/components/server-statistic/server-statistic.component.html b/web/src/app/modules/view/server/server-dashboard/components/server-statistic/server-statistic.component.html new file mode 100644 index 00000000..5721a2e8 --- /dev/null +++ b/web/src/app/modules/view/server/server-dashboard/components/server-statistic/server-statistic.component.html @@ -0,0 +1,35 @@ +