2026-06-02 21:06:42 +07:00
|
|
|
import { ForbiddenError, ValidationError } from "@bete/shared/errors";
|
|
|
|
|
import { createChildLogger } from "@bete/shared/logger";
|
2026-06-03 18:20:27 +07:00
|
|
|
import { config } from "../../shared/config/index.js";
|
2026-06-01 21:44:29 +07:00
|
|
|
import { analyticsRepository } from "./analytics.repository.js";
|
|
|
|
|
import type { AnalyticsQuery } from "./analytics.schema.js";
|
|
|
|
|
|
|
|
|
|
const logger = createChildLogger("analytics.service");
|
|
|
|
|
|
|
|
|
|
export class AnalyticsService {
|
|
|
|
|
private assertMonitorGuild(guildId: string) {
|
|
|
|
|
if (!config.MONITOR_GUILD_ID) {
|
|
|
|
|
throw new ValidationError("MONITOR_GUILD_ID is not configured");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (guildId !== config.MONITOR_GUILD_ID) {
|
|
|
|
|
throw new ForbiddenError("Analytics are restricted to the monitor guild");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getOverview(query: AnalyticsQuery) {
|
|
|
|
|
this.assertMonitorGuild(query.guildId);
|
|
|
|
|
logger.debug({ query }, "Getting analytics overview");
|
|
|
|
|
return analyticsRepository.getOverview(
|
|
|
|
|
query.guildId,
|
|
|
|
|
query.channelId,
|
|
|
|
|
query.hours,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getDailyTrend(guildId: string, hours = 24) {
|
|
|
|
|
this.assertMonitorGuild(guildId);
|
|
|
|
|
logger.debug({ guildId, hours }, "Getting daily trend");
|
|
|
|
|
return analyticsRepository.getDailyTrend(guildId, hours);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-02 00:11:29 +07:00
|
|
|
async getHourlyStats(guildId: string, channelId?: string, hours = 24) {
|
2026-06-01 21:44:29 +07:00
|
|
|
this.assertMonitorGuild(guildId);
|
2026-06-02 00:11:29 +07:00
|
|
|
logger.debug({ guildId, channelId, hours }, "Getting hourly stats");
|
|
|
|
|
return analyticsRepository.getHourlyStats(guildId, channelId, hours);
|
2026-06-01 21:44:29 +07:00
|
|
|
}
|
|
|
|
|
|
2026-06-02 00:11:29 +07:00
|
|
|
async getTopViolators(
|
|
|
|
|
guildId: string,
|
|
|
|
|
channelId?: string,
|
|
|
|
|
hours = 24,
|
|
|
|
|
limit = 10,
|
|
|
|
|
) {
|
2026-06-01 21:44:29 +07:00
|
|
|
this.assertMonitorGuild(guildId);
|
2026-06-02 00:11:29 +07:00
|
|
|
logger.debug({ guildId, channelId, hours, limit }, "Getting top violators");
|
|
|
|
|
return analyticsRepository.getTopViolators(
|
|
|
|
|
guildId,
|
|
|
|
|
channelId,
|
|
|
|
|
hours,
|
|
|
|
|
limit,
|
|
|
|
|
);
|
2026-06-01 21:44:29 +07:00
|
|
|
}
|
|
|
|
|
|
2026-06-02 00:11:29 +07:00
|
|
|
async getUserLeaderboard(
|
|
|
|
|
guildId: string,
|
|
|
|
|
channelId?: string,
|
|
|
|
|
hours = 24,
|
|
|
|
|
limit = 10,
|
|
|
|
|
) {
|
2026-06-01 21:44:29 +07:00
|
|
|
this.assertMonitorGuild(guildId);
|
2026-06-02 00:11:29 +07:00
|
|
|
logger.debug(
|
|
|
|
|
{ guildId, channelId, hours, limit },
|
|
|
|
|
"Getting user leaderboard",
|
|
|
|
|
);
|
|
|
|
|
return analyticsRepository.getUserLeaderboard(
|
|
|
|
|
guildId,
|
|
|
|
|
channelId,
|
|
|
|
|
hours,
|
|
|
|
|
limit,
|
|
|
|
|
);
|
2026-06-01 21:44:29 +07:00
|
|
|
}
|
|
|
|
|
|
2026-06-02 00:11:29 +07:00
|
|
|
async getModerationStats(guildId: string, channelId?: string, hours = 24) {
|
2026-06-01 21:44:29 +07:00
|
|
|
this.assertMonitorGuild(guildId);
|
2026-06-02 00:11:29 +07:00
|
|
|
logger.debug({ guildId, channelId, hours }, "Getting moderation stats");
|
|
|
|
|
return analyticsRepository.getModerationStats(guildId, channelId, hours);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getHeatmap(guildId: string, channelId?: string, hours = 24) {
|
|
|
|
|
this.assertMonitorGuild(guildId);
|
|
|
|
|
logger.debug({ guildId, channelId, hours }, "Getting heatmap");
|
|
|
|
|
return analyticsRepository.getHeatmap(guildId, channelId, hours);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getTopics(guildId: string, channelId?: string, hours = 24) {
|
|
|
|
|
this.assertMonitorGuild(guildId);
|
|
|
|
|
logger.debug({ guildId, channelId, hours }, "Getting topics");
|
|
|
|
|
return analyticsRepository.getTopics(guildId, channelId, hours);
|
2026-06-01 21:44:29 +07:00
|
|
|
}
|
2026-06-03 18:16:55 +07:00
|
|
|
|
|
|
|
|
async getModerationActions(
|
|
|
|
|
guildId: string,
|
|
|
|
|
channelId?: string,
|
|
|
|
|
hours = 24,
|
|
|
|
|
limit = 20,
|
|
|
|
|
) {
|
|
|
|
|
this.assertMonitorGuild(guildId);
|
2026-06-03 18:20:27 +07:00
|
|
|
logger.debug(
|
|
|
|
|
{ guildId, channelId, hours, limit },
|
|
|
|
|
"Getting moderation actions",
|
|
|
|
|
);
|
2026-06-03 18:16:55 +07:00
|
|
|
return analyticsRepository.getModerationActions(
|
|
|
|
|
guildId,
|
|
|
|
|
channelId,
|
|
|
|
|
hours,
|
|
|
|
|
limit,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getAIStats(guildId: string, channelId?: string, hours = 24) {
|
|
|
|
|
this.assertMonitorGuild(guildId);
|
|
|
|
|
logger.debug({ guildId, channelId, hours }, "Getting AI stats");
|
|
|
|
|
return analyticsRepository.getAIStats(guildId, channelId, hours);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getAttachmentStats(guildId: string, channelId?: string, hours = 24) {
|
|
|
|
|
this.assertMonitorGuild(guildId);
|
|
|
|
|
logger.debug({ guildId, channelId, hours }, "Getting attachment stats");
|
|
|
|
|
return analyticsRepository.getAttachmentStats(guildId, channelId, hours);
|
|
|
|
|
}
|
2026-06-01 21:44:29 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const analyticsService = new AnalyticsService();
|