fix: auto-aggregate AI costs hourly instead of manual-only
Some checks failed
Deploy API Server / build-and-deploy (push) Failing after 33s
Some checks failed
Deploy API Server / build-and-deploy (push) Failing after 33s
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
eb62868e8f
commit
14eaad53c3
@ -1,12 +1,24 @@
|
|||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
|
||||||
import { PrismaService } from '../../infrastructure/database/prisma.service';
|
import { PrismaService } from '../../infrastructure/database/prisma.service';
|
||||||
|
|
||||||
|
const AGGREGATE_INTERVAL_MS = 60 * 60 * 1000; // hourly
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CostAggregationService {
|
export class CostAggregationService implements OnModuleInit, OnModuleDestroy {
|
||||||
private readonly logger = new Logger(CostAggregationService.name);
|
private readonly logger = new Logger(CostAggregationService.name);
|
||||||
|
private timer: ReturnType<typeof setInterval> | null = null;
|
||||||
|
|
||||||
constructor(private readonly prisma: PrismaService) {}
|
constructor(private readonly prisma: PrismaService) {}
|
||||||
|
|
||||||
|
async onModuleInit() {
|
||||||
|
await this.aggregateToday();
|
||||||
|
this.timer = setInterval(() => this.aggregateToday(), AGGREGATE_INTERVAL_MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
onModuleDestroy() {
|
||||||
|
if (this.timer) clearInterval(this.timer);
|
||||||
|
}
|
||||||
|
|
||||||
/** Aggregate today's AiUsageLog into CostDailySummary */
|
/** Aggregate today's AiUsageLog into CostDailySummary */
|
||||||
async aggregateToday(): Promise<void> {
|
async aggregateToday(): Promise<void> {
|
||||||
const today = new Date(); today.setHours(0, 0, 0, 0);
|
const today = new Date(); today.setHours(0, 0, 0, 0);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user