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 @@ +
+
+ + +
+ +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
diff --git a/web/src/app/modules/view/server/server-dashboard/components/server-statistic/server-statistic.component.scss b/web/src/app/modules/view/server/server-dashboard/components/server-statistic/server-statistic.component.scss new file mode 100644 index 00000000..e69de29b diff --git a/web/src/app/modules/view/server/server-dashboard/components/server-statistic/server-statistic.component.spec.ts b/web/src/app/modules/view/server/server-dashboard/components/server-statistic/server-statistic.component.spec.ts new file mode 100644 index 00000000..d88c711b --- /dev/null +++ b/web/src/app/modules/view/server/server-dashboard/components/server-statistic/server-statistic.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ServerStatisticComponent } from './server-statistic.component'; + +describe('ServerStatisticComponent', () => { + let component: ServerStatisticComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [ ServerStatisticComponent ] + }) + .compileComponents(); + + fixture = TestBed.createComponent(ServerStatisticComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/web/src/app/modules/view/server/server-dashboard/components/server-statistic/server-statistic.component.ts b/web/src/app/modules/view/server/server-dashboard/components/server-statistic/server-statistic.component.ts new file mode 100644 index 00000000..369d0e9b --- /dev/null +++ b/web/src/app/modules/view/server/server-dashboard/components/server-statistic/server-statistic.component.ts @@ -0,0 +1,22 @@ +import { Component, Input } from "@angular/core"; +import { ServerStatistic } from "../../../../../../models/data/server.model"; + +@Component({ + selector: 'app-server-statistic', + templateUrl: './server-statistic.component.html', + styleUrls: ['./server-statistic.component.scss'] +}) +export class ServerStatisticComponent { + @Input() stats?: ServerStatistic = { + achievementsAchieved: 0, + messageCount: 0, + userCount: 0, + activeUserCount: 0, + userJoinedVoiceChannelCount: 0, + userJoinedVoiceChannelOntime: 0, + userJoinedGameServerCount: 0, + userJoinedGameServerOntime: 0, + userWarningCount: 0, + activityScore: 0, + }; +} diff --git a/web/src/app/modules/view/server/server-dashboard/server-dashboard.component.html b/web/src/app/modules/view/server/server-dashboard/server-dashboard.component.html index 69fe099e..dfd2c275 100644 --- a/web/src/app/modules/view/server/server-dashboard/server-dashboard.component.html +++ b/web/src/app/modules/view/server/server-dashboard/server-dashboard.component.html @@ -28,6 +28,13 @@ {{'view.dashboard.server.active_members' | translate}} {{server ? server.activeUserCount : ''}} + +
+
+ +
+ +
diff --git a/web/src/app/modules/view/server/server.module.ts b/web/src/app/modules/view/server/server.module.ts index 5ac78d1c..d1810268 100644 --- a/web/src/app/modules/view/server/server.module.ts +++ b/web/src/app/modules/view/server/server.module.ts @@ -6,6 +6,7 @@ import { SharedModule } from "../../shared/shared.module"; import { ProfileComponent } from "./profile/profile.component"; import { MembersComponent } from "./members/members.component"; import { ClientComponent } from "./server-dashboard/components/client/client.component"; +import { ServerStatisticComponent } from './server-dashboard/components/server-statistic/server-statistic.component'; @NgModule({ @@ -14,6 +15,7 @@ import { ClientComponent } from "./server-dashboard/components/client/client.com ProfileComponent, MembersComponent, ClientComponent, + ServerStatisticComponent, ], imports: [ CommonModule, diff --git a/web/src/assets/i18n/de.json b/web/src/assets/i18n/de.json index aec55e53..471a0893 100644 --- a/web/src/assets/i18n/de.json +++ b/web/src/assets/i18n/de.json @@ -454,13 +454,21 @@ } }, "dashboard": { + "achievements_achieved": "Errungenschaften vergeben", + "activity_score": "Aktivitätsbewertung", "deleted_message_count": "Gelöschte Nachrichten", "header": "Server dashboard", + "message_count": "Anzahl Nachrichten", "moved_users_count": "Verschobene Benutzer", "name": "Name", "received_command_count": "Empfangene Befehle", "received_message_count": "Empfangene Nachrichten", - "sent_message_count": "Gesendete Nachrichten" + "sent_message_count": "Gesendete Nachrichten", + "user_joined_game_server_count": "Anzahl Gameserver beitritte", + "user_joined_game_server_ontime": "Gameserver Ontime", + "user_joined_voice_channel_count": "Anzahl Sprachkanal beitritte", + "user_joined_voice_channel_ontime": "Sprachkanal Ontime", + "user_warning_count": "Anzahl Verwarnungen" }, "header": "Server", "levels": { diff --git a/web/src/assets/i18n/en.json b/web/src/assets/i18n/en.json index a236d8e3..539f3570 100644 --- a/web/src/assets/i18n/en.json +++ b/web/src/assets/i18n/en.json @@ -454,13 +454,21 @@ } }, "dashboard": { + "achievements_achieved": "Achievements achieved", + "activity_score": "Activity score", "deleted_message_count": "Deleted messages", "header": "Server dashboard", + "message_count": "Message count", "moved_users_count": "Moved users", "name": "Name", "received_command_count": "Received commands", "received_message_count": "Received messages", - "sent_message_count": "Sent messages" + "sent_message_count": "Sent messages", + "user_joined_game_server_count": "Joined Gameserver count", + "user_joined_game_server_ontime": "Gameserver ontime", + "user_joined_voice_channel_count": "Joined voice channel count", + "user_joined_voice_channel_ontime": "Joined voice channel ontime", + "user_warning_count": "User warning count" }, "header": "Server", "levels": {