58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
import { prisma } from '../database';
|
|
import { logger } from '../utils/logger';
|
|
|
|
export class ActivityTrackerService {
|
|
/**
|
|
* 메시지 활동을 기록합니다.
|
|
* @param guildId 서버 ID
|
|
*/
|
|
public static async recordActivity(guildId: string): Promise<void> {
|
|
try {
|
|
const now = new Date();
|
|
const hour = now.getUTCHours();
|
|
const dayOfWeek = now.getUTCDay();
|
|
|
|
// 이번 주의 시작일 (일요일 00:00:00)
|
|
const weekStart = new Date(now);
|
|
weekStart.setUTCDate(now.getUTCDate() - dayOfWeek);
|
|
weekStart.setUTCHours(0, 0, 0, 0);
|
|
|
|
await prisma.activityLog.upsert({
|
|
where: {
|
|
guildId_hour_dayOfWeek_weekStart: {
|
|
guildId,
|
|
hour,
|
|
dayOfWeek,
|
|
weekStart,
|
|
},
|
|
},
|
|
update: {
|
|
count: { increment: 1 },
|
|
},
|
|
create: {
|
|
guildId,
|
|
hour,
|
|
dayOfWeek,
|
|
weekStart,
|
|
count: 1,
|
|
},
|
|
});
|
|
} catch (err) {
|
|
logger.error(`Failed to record activity for guild ${guildId}:`, err);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 최근 활동 데이터를 기반으로 피크 시간대를 분석합니다. (FeverService에서 사용)
|
|
*/
|
|
public static async getPeakHour(guildId: string): Promise<number | null> {
|
|
const logs = await prisma.activityLog.findMany({
|
|
where: { guildId },
|
|
orderBy: { count: 'desc' },
|
|
take: 1
|
|
});
|
|
|
|
return logs.length > 0 ? logs[0].hour : null;
|
|
}
|
|
}
|