2026-05-30 20:40:33 +07:00
|
|
|
import { executeAll, executeGet } from "../database/drizzle.js";
|
2026-05-29 19:37:08 +07:00
|
|
|
import { createChildLogger } from "../logger.js";
|
2026-05-30 21:12:25 +07:00
|
|
|
import { config } from "../config.js";
|
2026-05-29 19:37:08 +07:00
|
|
|
import type { MessageRecord } from "./types.js";
|
|
|
|
|
|
|
|
|
|
const logger = createChildLogger("analytics-store");
|
|
|
|
|
|
|
|
|
|
// ── Types ──────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export interface HourlyBucket {
|
|
|
|
|
hour: string;
|
|
|
|
|
count: number;
|
|
|
|
|
clean: number;
|
|
|
|
|
warned: number;
|
|
|
|
|
flagged: number;
|
|
|
|
|
error: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface TopicTrend {
|
|
|
|
|
topic: string;
|
|
|
|
|
count: number;
|
|
|
|
|
score: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface UserStat {
|
|
|
|
|
user_id: string;
|
|
|
|
|
username: string;
|
|
|
|
|
avatar_url: string | null;
|
|
|
|
|
message_count: number;
|
|
|
|
|
edited_count: number;
|
|
|
|
|
deleted_count: number;
|
|
|
|
|
flagged_count: number;
|
|
|
|
|
last_active: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ModerationBreakdown {
|
|
|
|
|
total: number;
|
|
|
|
|
clean: number;
|
|
|
|
|
warned: number;
|
|
|
|
|
flagged: number;
|
|
|
|
|
error: number;
|
|
|
|
|
pending: number;
|
|
|
|
|
average_score: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AnalyticsOverview {
|
|
|
|
|
period: { start: number; end: number };
|
|
|
|
|
messages: ModerationBreakdown;
|
|
|
|
|
hourly: HourlyBucket[];
|
|
|
|
|
topics: TopicTrend[];
|
|
|
|
|
top_users: UserStat[];
|
|
|
|
|
active_users_count: number;
|
|
|
|
|
total_channels: number;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
// ══════════════════════════════════════════════════════════════════════════
|
|
|
|
|
// GENERIC QUERY CACHE (reduces duplicate DB calls from 5s auto-refresh)
|
|
|
|
|
// ══════════════════════════════════════════════════════════════════════════
|
2026-05-30 17:28:27 +07:00
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
interface CacheEntry<T> {
|
|
|
|
|
data: T;
|
2026-05-30 17:28:27 +07:00
|
|
|
expiresAt: number;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
const queryCache = new Map<string, CacheEntry<any>>();
|
2026-05-30 17:28:27 +07:00
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
/** Default TTL for aggregate queries — 10s is long enough to prevent redundant
|
|
|
|
|
* calls from the 5s auto-refresh but short enough to feel real-time. */
|
|
|
|
|
const AGGREGATE_CACHE_TTL_MS = 10_000;
|
|
|
|
|
|
|
|
|
|
/** Topic extraction is expensive (JSON parsing). Cache longer. */
|
|
|
|
|
const TOPIC_CACHE_TTL_MS = 120_000;
|
|
|
|
|
|
|
|
|
|
function makeCacheKey(prefix: string, params: Record<string, any>): string {
|
|
|
|
|
return `${prefix}:${JSON.stringify(params)}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getCached<T>(key: string): T | undefined {
|
|
|
|
|
const entry = queryCache.get(key);
|
|
|
|
|
if (entry && entry.expiresAt > Date.now()) return entry.data;
|
|
|
|
|
if (entry) queryCache.delete(key); // expired
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setCache<T>(key: string, data: T, ttl: number): void {
|
|
|
|
|
queryCache.set(key, { data, expiresAt: Date.now() + ttl });
|
|
|
|
|
// Prune old entries if cache grows too large (>200 entries)
|
|
|
|
|
if (queryCache.size > 200) {
|
|
|
|
|
const now = Date.now();
|
|
|
|
|
for (const [k, v] of queryCache) {
|
|
|
|
|
if (v.expiresAt <= now) queryCache.delete(k);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-30 17:28:27 +07:00
|
|
|
}
|
|
|
|
|
|
2026-05-29 19:37:08 +07:00
|
|
|
// ── Hourly Message Stats ───────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export async function getHourlyStats(input: {
|
|
|
|
|
guildId: string;
|
|
|
|
|
channelId?: string;
|
|
|
|
|
hours?: number;
|
|
|
|
|
}): Promise<HourlyBucket[]> {
|
2026-05-30 20:40:33 +07:00
|
|
|
const { guildId, channelId, hours = 24 } = input;
|
|
|
|
|
const cacheKey = makeCacheKey("hourly", { guildId, channelId, hours });
|
|
|
|
|
const cached = getCached<HourlyBucket[]>(cacheKey);
|
|
|
|
|
if (cached) return cached;
|
|
|
|
|
|
2026-05-29 19:37:08 +07:00
|
|
|
try {
|
|
|
|
|
const since = Date.now() - hours * 3600_000;
|
2026-05-30 21:12:25 +07:00
|
|
|
const isPg = config.DATABASE_TYPE === "postgres";
|
|
|
|
|
|
|
|
|
|
const hourExpr = isPg
|
|
|
|
|
? `to_char(to_timestamp((created_at / 3600000) * 3600), 'YYYY-MM-DD HH24:MI:SS') as hour`
|
|
|
|
|
: `datetime((created_at / 3600000) * 3600, 'unixepoch') as hour`;
|
|
|
|
|
|
|
|
|
|
const rows = await executeAll(
|
2026-05-30 17:28:27 +07:00
|
|
|
`
|
|
|
|
|
SELECT
|
2026-05-30 21:12:25 +07:00
|
|
|
${hourExpr},
|
2026-05-30 17:28:27 +07:00
|
|
|
count(*) as count,
|
|
|
|
|
count(case when ai_status = 'clean' then 1 end) as clean,
|
|
|
|
|
count(case when ai_status = 'warn' then 1 end) as warned,
|
|
|
|
|
count(case when ai_status = 'flagged' then 1 end) as flagged,
|
|
|
|
|
count(case when ai_status = 'error' then 1 end) as error
|
|
|
|
|
FROM messages
|
|
|
|
|
WHERE guild_id = ?
|
|
|
|
|
AND created_at >= ?
|
|
|
|
|
AND deleted_at IS NULL
|
|
|
|
|
${channelId ? `AND (channel_id = ? OR thread_id = ?)` : ""}
|
|
|
|
|
GROUP BY (created_at / 3600000)
|
|
|
|
|
ORDER BY hour ASC
|
|
|
|
|
`,
|
|
|
|
|
channelId
|
|
|
|
|
? [guildId, since, channelId, channelId]
|
|
|
|
|
: [guildId, since],
|
|
|
|
|
);
|
2026-05-29 19:37:08 +07:00
|
|
|
|
2026-05-30 17:28:27 +07:00
|
|
|
// Initialize all hour buckets (fill gaps with zeros)
|
2026-05-29 19:37:08 +07:00
|
|
|
const buckets = new Map<
|
|
|
|
|
string,
|
2026-05-30 20:40:33 +07:00
|
|
|
{ count: number; clean: number; warned: number; flagged: number; error: number }
|
2026-05-29 19:37:08 +07:00
|
|
|
>();
|
|
|
|
|
|
|
|
|
|
for (let h = 0; h < hours; h++) {
|
|
|
|
|
const ts = new Date(since + h * 3600_000);
|
|
|
|
|
ts.setMinutes(0, 0, 0);
|
|
|
|
|
const key = ts.toISOString().slice(0, 13) + ":00:00Z";
|
|
|
|
|
buckets.set(key, { count: 0, clean: 0, warned: 0, flagged: 0, error: 0 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 21:12:25 +07:00
|
|
|
for (const row of rows) {
|
2026-05-30 17:28:27 +07:00
|
|
|
const d = new Date(row.hour.replace(" ", "T") + "Z");
|
2026-05-29 19:37:08 +07:00
|
|
|
const key = d.toISOString().slice(0, 13) + ":00:00Z";
|
|
|
|
|
const bucket = buckets.get(key);
|
|
|
|
|
if (!bucket) continue;
|
2026-05-30 17:28:27 +07:00
|
|
|
bucket.count = row.count;
|
|
|
|
|
bucket.clean = row.clean;
|
|
|
|
|
bucket.warned = row.warned;
|
|
|
|
|
bucket.flagged = row.flagged;
|
|
|
|
|
bucket.error = row.error;
|
2026-05-29 19:37:08 +07:00
|
|
|
}
|
|
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
const result = Array.from(buckets.entries())
|
2026-05-29 19:37:08 +07:00
|
|
|
.sort(([a], [b]) => a.localeCompare(b))
|
|
|
|
|
.map(([hour, data]) => ({ hour, ...data }));
|
2026-05-30 20:40:33 +07:00
|
|
|
|
|
|
|
|
setCache(cacheKey, result, AGGREGATE_CACHE_TTL_MS);
|
|
|
|
|
return result;
|
2026-05-29 19:37:08 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(
|
|
|
|
|
{ error: error instanceof Error ? error.message : String(error) },
|
|
|
|
|
"Failed to get hourly stats",
|
|
|
|
|
);
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Topic Trends ───────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
const STOP_WORDS = new Set([
|
2026-05-30 20:40:33 +07:00
|
|
|
"yang", "dan", "itu", "ini", "dengan", "akan", "pada", "dari", "di", "ke",
|
|
|
|
|
"untuk", "tidak", "ada", "juga", "sudah", "saya", "kamu", "dia", "mereka",
|
|
|
|
|
"kami", "aku", "lo", "lu", "gua", "gue", "org", "orang", "aja", "sama",
|
|
|
|
|
"kalo", "kalau", "bisa", "karena", "gak", "nggak", "ga", "tak", "belum",
|
|
|
|
|
"udah", "dah", "lah", "kah", "pun", "nih", "tuh", "deh", "dong", "si",
|
|
|
|
|
"nya", "kan", "ya", "yah", "yuk", "kok", "loh", "nah", "wow", "eh",
|
|
|
|
|
"the", "a", "an", "is", "are", "was", "were", "be", "been", "being",
|
|
|
|
|
"have", "has", "had", "having", "do", "does", "did", "doing",
|
|
|
|
|
"will", "would", "could", "should", "may", "might", "must", "shall",
|
|
|
|
|
"i", "you", "he", "she", "it", "we", "they",
|
|
|
|
|
"me", "him", "her", "us", "them",
|
|
|
|
|
"my", "your", "his", "its", "our", "their",
|
|
|
|
|
"and", "but", "or", "nor", "not", "so", "yet", "for", "if",
|
|
|
|
|
"to", "of", "in", "on", "at", "by", "as", "with",
|
|
|
|
|
"about", "just", "then", "now", "here", "there",
|
|
|
|
|
"when", "where", "why", "how",
|
|
|
|
|
"all", "both", "each", "few", "more", "most", "other",
|
|
|
|
|
"some", "such", "only", "own", "same", "too", "very",
|
|
|
|
|
"can", "go", "ok", "okay", "yeah", "yes", "no",
|
2026-05-29 19:37:08 +07:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
function extractTopics(messages: MessageRecord[], topN = 15): TopicTrend[] {
|
|
|
|
|
const topicScores = new Map<string, { count: number; score: number }>();
|
|
|
|
|
const wordFreq = new Map<string, number>();
|
|
|
|
|
const flaggedWordFreq = new Map<string, number>();
|
|
|
|
|
|
|
|
|
|
for (const msg of messages) {
|
|
|
|
|
if (msg.ai_analysis) {
|
|
|
|
|
try {
|
|
|
|
|
const analysis = JSON.parse(msg.ai_analysis);
|
|
|
|
|
const topics = analysis.topics;
|
|
|
|
|
if (topics && Array.isArray(topics)) {
|
|
|
|
|
for (const topic of topics) {
|
2026-05-30 15:55:01 +07:00
|
|
|
const key =
|
|
|
|
|
typeof topic === "string" ? topic : topic.name || topic.topic;
|
2026-05-29 19:37:08 +07:00
|
|
|
if (!key) continue;
|
|
|
|
|
const k = key.toLowerCase();
|
|
|
|
|
const score = msg.ai_moderation_score || 0;
|
|
|
|
|
const existing = topicScores.get(k);
|
2026-05-30 15:55:01 +07:00
|
|
|
if (existing) {
|
|
|
|
|
existing.count++;
|
|
|
|
|
existing.score += score;
|
|
|
|
|
} else {
|
|
|
|
|
topicScores.set(k, { count: 1, score });
|
|
|
|
|
}
|
2026-05-29 19:37:08 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (analysis.category) {
|
|
|
|
|
const cat = String(analysis.category).toLowerCase();
|
|
|
|
|
const existing = topicScores.get(cat);
|
2026-05-30 15:55:01 +07:00
|
|
|
if (existing) {
|
|
|
|
|
existing.count++;
|
|
|
|
|
existing.score += msg.ai_moderation_score || 0;
|
|
|
|
|
} else {
|
2026-05-30 20:40:33 +07:00
|
|
|
topicScores.set(cat, { count: 1, score: msg.ai_moderation_score || 0 });
|
2026-05-30 15:55:01 +07:00
|
|
|
}
|
2026-05-29 19:37:08 +07:00
|
|
|
}
|
2026-05-30 15:55:01 +07:00
|
|
|
} catch {
|
|
|
|
|
/* not valid JSON */
|
|
|
|
|
}
|
2026-05-29 19:37:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (msg.content) {
|
|
|
|
|
const words = msg.content
|
|
|
|
|
.toLowerCase()
|
|
|
|
|
.replace(/[^\w\s]/g, " ")
|
|
|
|
|
.split(/\s+/)
|
|
|
|
|
.filter((w) => w.length > 2 && !STOP_WORDS.has(w));
|
|
|
|
|
|
|
|
|
|
for (const word of words) {
|
|
|
|
|
wordFreq.set(word, (wordFreq.get(word) || 0) + 1);
|
|
|
|
|
if (msg.ai_status === "flagged" || msg.ai_status === "warn") {
|
|
|
|
|
flaggedWordFreq.set(word, (flaggedWordFreq.get(word) || 0) + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const results: TopicTrend[] = [];
|
|
|
|
|
for (const [topic, data] of topicScores) {
|
|
|
|
|
results.push({ topic, count: data.count, score: data.score });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sortedWords = Array.from(wordFreq.entries())
|
|
|
|
|
.sort(([, a], [, b]) => b - a)
|
|
|
|
|
.slice(0, topN);
|
|
|
|
|
|
|
|
|
|
for (const [word, count] of sortedWords) {
|
|
|
|
|
if (!topicScores.has(word)) {
|
2026-05-30 20:40:33 +07:00
|
|
|
results.push({ topic: word, count, score: flaggedWordFreq.get(word) || 0 });
|
2026-05-29 19:37:08 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results.sort((a, b) => b.count - a.count).slice(0, topN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getTopicTrends(input: {
|
|
|
|
|
guildId: string;
|
|
|
|
|
channelId?: string;
|
|
|
|
|
hours?: number;
|
|
|
|
|
}): Promise<TopicTrend[]> {
|
2026-05-30 17:28:27 +07:00
|
|
|
const { guildId, channelId, hours = 24 } = input;
|
2026-05-30 20:40:33 +07:00
|
|
|
const cacheKey = makeCacheKey("topics", { guildId, channelId, hours });
|
|
|
|
|
const cached = getCached<TopicTrend[]>(cacheKey);
|
|
|
|
|
if (cached) return cached;
|
2026-05-30 17:28:27 +07:00
|
|
|
|
2026-05-29 19:37:08 +07:00
|
|
|
try {
|
|
|
|
|
const since = Date.now() - hours * 3600_000;
|
|
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
// Only fetch messages that have ai_analysis (the ones that actually have topics)
|
|
|
|
|
// This dramatically reduces rows for large guilds
|
|
|
|
|
const rows = await executeAll(
|
2026-05-30 17:28:27 +07:00
|
|
|
`
|
|
|
|
|
SELECT
|
|
|
|
|
id, content, ai_status, ai_analysis, ai_moderation_score,
|
|
|
|
|
ai_moderation_flags, created_at
|
|
|
|
|
FROM messages
|
|
|
|
|
WHERE guild_id = ?
|
|
|
|
|
AND created_at >= ?
|
|
|
|
|
AND deleted_at IS NULL
|
2026-05-30 20:40:33 +07:00
|
|
|
AND ai_analysis IS NOT NULL
|
2026-05-30 17:28:27 +07:00
|
|
|
${channelId ? `AND (channel_id = ? OR thread_id = ?)` : ""}
|
|
|
|
|
ORDER BY created_at DESC
|
2026-05-30 20:40:33 +07:00
|
|
|
LIMIT 2000
|
2026-05-30 17:28:27 +07:00
|
|
|
`,
|
|
|
|
|
channelId
|
|
|
|
|
? [guildId, since, channelId, channelId]
|
|
|
|
|
: [guildId, since],
|
|
|
|
|
) as MessageRecord[];
|
2026-05-29 19:37:08 +07:00
|
|
|
|
2026-05-30 17:28:27 +07:00
|
|
|
const result = extractTopics(rows);
|
2026-05-30 20:40:33 +07:00
|
|
|
setCache(cacheKey, result, TOPIC_CACHE_TTL_MS);
|
2026-05-30 17:28:27 +07:00
|
|
|
return result;
|
2026-05-29 19:37:08 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(
|
|
|
|
|
{ error: error instanceof Error ? error.message : String(error) },
|
|
|
|
|
"Failed to get topic trends",
|
|
|
|
|
);
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── User Leaderboard ────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export async function getUserLeaderboard(input: {
|
|
|
|
|
guildId: string;
|
|
|
|
|
channelId?: string;
|
|
|
|
|
hours?: number;
|
|
|
|
|
limit?: number;
|
|
|
|
|
}): Promise<UserStat[]> {
|
2026-05-30 20:40:33 +07:00
|
|
|
const { guildId, channelId, hours = 24, limit = 20 } = input;
|
|
|
|
|
const cacheKey = makeCacheKey("leaderboard", { guildId, channelId, hours, limit });
|
|
|
|
|
const cached = getCached<UserStat[]>(cacheKey);
|
|
|
|
|
if (cached) return cached;
|
2026-05-29 19:37:08 +07:00
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
try {
|
|
|
|
|
const since = Date.now() - hours * 3600_000;
|
|
|
|
|
const rows = await executeAll(
|
2026-05-30 17:28:27 +07:00
|
|
|
`
|
|
|
|
|
SELECT
|
|
|
|
|
user_id,
|
|
|
|
|
username,
|
|
|
|
|
avatar_url,
|
|
|
|
|
count(*) as message_count,
|
|
|
|
|
count(case when type = 'edited' then 1 end) as edited_count,
|
|
|
|
|
count(case when type = 'deleted' then 1 end) as deleted_count,
|
|
|
|
|
count(case when ai_status in ('flagged', 'warn') then 1 end) as flagged_count,
|
|
|
|
|
max(created_at) as last_active
|
|
|
|
|
FROM messages
|
|
|
|
|
WHERE guild_id = ?
|
|
|
|
|
AND created_at >= ?
|
|
|
|
|
AND deleted_at IS NULL
|
|
|
|
|
${channelId ? `AND (channel_id = ? OR thread_id = ?)` : ""}
|
2026-05-30 21:12:25 +07:00
|
|
|
GROUP BY user_id, username, avatar_url
|
2026-05-30 17:28:27 +07:00
|
|
|
ORDER BY message_count DESC
|
|
|
|
|
LIMIT ?
|
|
|
|
|
`,
|
|
|
|
|
channelId
|
|
|
|
|
? [guildId, since, channelId, channelId, limit]
|
|
|
|
|
: [guildId, since, limit],
|
|
|
|
|
);
|
2026-05-29 19:37:08 +07:00
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
const result = rows as UserStat[];
|
|
|
|
|
setCache(cacheKey, result, AGGREGATE_CACHE_TTL_MS);
|
|
|
|
|
return result;
|
2026-05-29 19:37:08 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(
|
|
|
|
|
{ error: error instanceof Error ? error.message : String(error) },
|
|
|
|
|
"Failed to get user leaderboard",
|
|
|
|
|
);
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Moderation Stats ───────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export async function getModerationStats(input: {
|
|
|
|
|
guildId: string;
|
|
|
|
|
channelId?: string;
|
|
|
|
|
hours?: number;
|
|
|
|
|
}): Promise<ModerationBreakdown> {
|
2026-05-30 20:40:33 +07:00
|
|
|
const { guildId, channelId, hours = 24 } = input;
|
|
|
|
|
const cacheKey = makeCacheKey("modstats", { guildId, channelId, hours });
|
|
|
|
|
const cached = getCached<ModerationBreakdown>(cacheKey);
|
|
|
|
|
if (cached) return cached;
|
2026-05-29 19:37:08 +07:00
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
try {
|
|
|
|
|
const since = Date.now() - hours * 3600_000;
|
2026-05-30 21:12:25 +07:00
|
|
|
const isPg = config.DATABASE_TYPE === "postgres";
|
|
|
|
|
|
|
|
|
|
const avgScoreExpr = isPg
|
|
|
|
|
? `round(avg(ai_moderation_score)::numeric, 2)`
|
|
|
|
|
: `round(avg(ai_moderation_score), 2)`;
|
|
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
const row = await executeGet(
|
2026-05-30 17:28:27 +07:00
|
|
|
`
|
|
|
|
|
SELECT
|
|
|
|
|
count(*) as total,
|
|
|
|
|
count(case when ai_status = 'clean' then 1 end) as clean,
|
|
|
|
|
count(case when ai_status = 'warn' then 1 end) as warned,
|
|
|
|
|
count(case when ai_status = 'flagged' then 1 end) as flagged,
|
|
|
|
|
count(case when ai_status = 'error' then 1 end) as error,
|
|
|
|
|
count(case when ai_status = 'pending' or ai_status IS NULL then 1 end) as pending,
|
2026-05-30 21:12:25 +07:00
|
|
|
${avgScoreExpr} as average_score
|
2026-05-30 17:28:27 +07:00
|
|
|
FROM messages
|
|
|
|
|
WHERE guild_id = ?
|
|
|
|
|
AND created_at >= ?
|
|
|
|
|
AND deleted_at IS NULL
|
|
|
|
|
${channelId ? `AND (channel_id = ? OR thread_id = ?)` : ""}
|
|
|
|
|
`,
|
|
|
|
|
channelId
|
|
|
|
|
? [guildId, since, channelId, channelId]
|
|
|
|
|
: [guildId, since],
|
|
|
|
|
);
|
2026-05-29 19:37:08 +07:00
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
const result: ModerationBreakdown = row
|
|
|
|
|
? {
|
|
|
|
|
total: row.total ?? 0,
|
|
|
|
|
clean: row.clean ?? 0,
|
|
|
|
|
warned: row.warned ?? 0,
|
|
|
|
|
flagged: row.flagged ?? 0,
|
|
|
|
|
error: row.error ?? 0,
|
|
|
|
|
pending: row.pending ?? 0,
|
|
|
|
|
average_score: row.average_score ?? 0,
|
|
|
|
|
}
|
|
|
|
|
: { total: 0, clean: 0, warned: 0, flagged: 0, error: 0, pending: 0, average_score: 0 };
|
2026-05-29 19:37:08 +07:00
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
setCache(cacheKey, result, AGGREGATE_CACHE_TTL_MS);
|
|
|
|
|
return result;
|
2026-05-29 19:37:08 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(
|
|
|
|
|
{ error: error instanceof Error ? error.message : String(error) },
|
|
|
|
|
"Failed to get moderation stats",
|
|
|
|
|
);
|
2026-05-30 20:40:33 +07:00
|
|
|
return { total: 0, clean: 0, warned: 0, flagged: 0, error: 0, pending: 0, average_score: 0 };
|
2026-05-29 19:37:08 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Active Channels Count ──────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export async function getActiveChannelCount(input: {
|
|
|
|
|
guildId: string;
|
|
|
|
|
hours?: number;
|
|
|
|
|
}): Promise<number> {
|
2026-05-30 20:40:33 +07:00
|
|
|
const { guildId, hours = 24 } = input;
|
|
|
|
|
const cacheKey = makeCacheKey("channels", { guildId, hours });
|
|
|
|
|
const cached = getCached<number>(cacheKey);
|
|
|
|
|
if (cached !== undefined) return cached;
|
2026-05-29 19:37:08 +07:00
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
try {
|
|
|
|
|
const since = Date.now() - hours * 3600_000;
|
|
|
|
|
const row = await executeGet(
|
2026-05-30 17:28:27 +07:00
|
|
|
`
|
|
|
|
|
SELECT count(DISTINCT channel_id) as cnt
|
|
|
|
|
FROM messages
|
|
|
|
|
WHERE guild_id = ?
|
|
|
|
|
AND created_at >= ?
|
|
|
|
|
AND deleted_at IS NULL
|
|
|
|
|
`,
|
|
|
|
|
[guildId, since],
|
|
|
|
|
);
|
2026-05-29 19:37:08 +07:00
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
const result = row?.cnt ?? 0;
|
|
|
|
|
setCache(cacheKey, result, AGGREGATE_CACHE_TTL_MS);
|
|
|
|
|
return result;
|
2026-05-29 19:37:08 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(
|
|
|
|
|
{ error: error instanceof Error ? error.message : String(error) },
|
|
|
|
|
"Failed to get active channel count",
|
|
|
|
|
);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Top Violators ─────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export interface ViolatorStat {
|
|
|
|
|
user_id: string;
|
|
|
|
|
username: string;
|
|
|
|
|
avatar_url: string | null;
|
|
|
|
|
total_messages: number;
|
|
|
|
|
flagged_count: number;
|
|
|
|
|
warned_count: number;
|
2026-05-30 20:40:33 +07:00
|
|
|
violation_score: number;
|
|
|
|
|
worst_flags: string[];
|
2026-05-29 19:37:08 +07:00
|
|
|
last_violation: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getTopViolators(input: {
|
|
|
|
|
guildId: string;
|
|
|
|
|
channelId?: string;
|
|
|
|
|
hours?: number;
|
|
|
|
|
limit?: number;
|
|
|
|
|
}): Promise<ViolatorStat[]> {
|
2026-05-30 20:40:33 +07:00
|
|
|
const { guildId, channelId, hours = 24, limit = 20 } = input;
|
|
|
|
|
const cacheKey = makeCacheKey("violators", { guildId, channelId, hours, limit });
|
|
|
|
|
const cached = getCached<ViolatorStat[]>(cacheKey);
|
|
|
|
|
if (cached) return cached;
|
2026-05-29 19:37:08 +07:00
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
try {
|
|
|
|
|
const since = Date.now() - hours * 3600_000;
|
|
|
|
|
const rows = await executeAll(
|
2026-05-30 17:28:27 +07:00
|
|
|
`
|
|
|
|
|
SELECT
|
|
|
|
|
user_id,
|
|
|
|
|
username,
|
|
|
|
|
avatar_url,
|
|
|
|
|
count(*) as total_messages,
|
|
|
|
|
count(case when ai_status = 'flagged' then 1 end) as flagged_count,
|
|
|
|
|
count(case when ai_status = 'warn' then 1 end) as warned_count,
|
|
|
|
|
max(case when ai_status in ('flagged', 'warn') then created_at else 0 end) as last_violation
|
|
|
|
|
FROM messages
|
|
|
|
|
WHERE guild_id = ?
|
|
|
|
|
AND created_at >= ?
|
|
|
|
|
AND deleted_at IS NULL
|
|
|
|
|
${channelId ? `AND (channel_id = ? OR thread_id = ?)` : ""}
|
2026-05-30 21:12:25 +07:00
|
|
|
GROUP BY user_id, username, avatar_url
|
2026-05-30 21:29:16 +07:00
|
|
|
HAVING count(case when ai_status = 'flagged' then 1 end) > 0
|
|
|
|
|
OR count(case when ai_status = 'warn' then 1 end) > 0
|
|
|
|
|
ORDER BY (
|
|
|
|
|
count(case when ai_status = 'flagged' then 1 end) * 3
|
|
|
|
|
+ count(case when ai_status = 'warn' then 1 end)
|
|
|
|
|
) DESC
|
2026-05-30 17:28:27 +07:00
|
|
|
LIMIT ?
|
|
|
|
|
`,
|
|
|
|
|
channelId
|
|
|
|
|
? [guildId, since, channelId, channelId, limit]
|
|
|
|
|
: [guildId, since, limit],
|
|
|
|
|
);
|
2026-05-29 19:37:08 +07:00
|
|
|
|
2026-05-30 17:28:27 +07:00
|
|
|
const violators: ViolatorStat[] = rows.map((row: any) => ({
|
|
|
|
|
user_id: row.user_id,
|
|
|
|
|
username: row.username,
|
|
|
|
|
avatar_url: row.avatar_url,
|
|
|
|
|
total_messages: row.total_messages,
|
|
|
|
|
flagged_count: row.flagged_count,
|
|
|
|
|
warned_count: row.warned_count,
|
|
|
|
|
violation_score: row.flagged_count * 3 + row.warned_count,
|
2026-05-30 20:40:33 +07:00
|
|
|
worst_flags: [],
|
2026-05-30 17:28:27 +07:00
|
|
|
last_violation: row.last_violation,
|
|
|
|
|
}));
|
2026-05-29 19:37:08 +07:00
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
setCache(cacheKey, violators, AGGREGATE_CACHE_TTL_MS);
|
2026-05-30 17:28:27 +07:00
|
|
|
return violators;
|
2026-05-29 19:37:08 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(
|
|
|
|
|
{ error: error instanceof Error ? error.message : String(error) },
|
|
|
|
|
"Failed to get top violators",
|
|
|
|
|
);
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
// ── Cache Invalidation (called when new messages arrive) ───────────────
|
|
|
|
|
|
|
|
|
|
export function invalidateAnalyticsCache(guildId: string): void {
|
|
|
|
|
const now = Date.now();
|
|
|
|
|
const needle = `"${guildId}"`;
|
|
|
|
|
for (const [key, entry] of queryCache) {
|
|
|
|
|
if (key.includes(needle) && entry.expiresAt > now) {
|
|
|
|
|
entry.expiresAt = 0; // expire immediately
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-29 19:37:08 +07:00
|
|
|
// ── Combined Overview ──────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
export async function getAnalyticsOverview(input: {
|
|
|
|
|
guildId: string;
|
|
|
|
|
channelId?: string;
|
|
|
|
|
hours?: number;
|
|
|
|
|
}): Promise<AnalyticsOverview> {
|
|
|
|
|
const { guildId, hours = 24 } = input;
|
|
|
|
|
const now = Date.now();
|
|
|
|
|
const since = now - hours * 3600_000;
|
|
|
|
|
|
2026-05-30 20:40:33 +07:00
|
|
|
const [messages, hourly, topics, topUsers, totalChannels] = await Promise.all([
|
|
|
|
|
getModerationStats(input),
|
|
|
|
|
getHourlyStats(input),
|
|
|
|
|
getTopicTrends(input),
|
|
|
|
|
getUserLeaderboard(input),
|
|
|
|
|
getActiveChannelCount({ guildId, hours }),
|
|
|
|
|
]);
|
2026-05-29 19:37:08 +07:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
period: { start: since, end: now },
|
|
|
|
|
messages,
|
|
|
|
|
hourly,
|
|
|
|
|
topics,
|
|
|
|
|
top_users: topUsers,
|
|
|
|
|
active_users_count: topUsers.length,
|
|
|
|
|
total_channels: totalChannels,
|
|
|
|
|
};
|
|
|
|
|
}
|