Implements comprehensive LLM usage tracking with analytics endpoints. Implementation: - Added LlmUsageLog model to Prisma schema - Created llm-usage module with service, controller, and DTOs - Added tracking for token usage, costs, and durations - Implemented analytics aggregation by provider, model, and task type - Added filtering by workspace, provider, model, user, and date range Testing: - 20 unit tests with 90.8% coverage (exceeds 85% requirement) - Tests for service and controller with full error handling - Tests use Vitest following project conventions API Endpoints: - GET /api/llm-usage/analytics - Aggregated usage analytics - GET /api/llm-usage/by-workspace/:workspaceId - Workspace usage logs - GET /api/llm-usage/by-workspace/:workspaceId/provider/:provider - Provider logs - GET /api/llm-usage/by-workspace/:workspaceId/model/:model - Model logs Database: - LlmUsageLog table with indexes for efficient queries - Relations to User, Workspace, and LlmProviderInstance - Ready for migration with: pnpm prisma migrate dev Refs #309 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
112 lines
3.7 KiB
TypeScript
112 lines
3.7 KiB
TypeScript
import { Module } from "@nestjs/common";
|
|
import { APP_INTERCEPTOR, APP_GUARD } from "@nestjs/core";
|
|
import { ThrottlerModule } from "@nestjs/throttler";
|
|
import { BullModule } from "@nestjs/bullmq";
|
|
import { ThrottlerValkeyStorageService, ThrottlerApiKeyGuard } from "./common/throttler";
|
|
import { CsrfGuard } from "./common/guards/csrf.guard";
|
|
import { AppController } from "./app.controller";
|
|
import { AppService } from "./app.service";
|
|
import { CsrfController } from "./common/controllers/csrf.controller";
|
|
import { PrismaModule } from "./prisma/prisma.module";
|
|
import { DatabaseModule } from "./database/database.module";
|
|
import { AuthModule } from "./auth/auth.module";
|
|
import { ActivityModule } from "./activity/activity.module";
|
|
import { TasksModule } from "./tasks/tasks.module";
|
|
import { EventsModule } from "./events/events.module";
|
|
import { ProjectsModule } from "./projects/projects.module";
|
|
import { DomainsModule } from "./domains/domains.module";
|
|
import { IdeasModule } from "./ideas/ideas.module";
|
|
import { WidgetsModule } from "./widgets/widgets.module";
|
|
import { LayoutsModule } from "./layouts/layouts.module";
|
|
import { KnowledgeModule } from "./knowledge/knowledge.module";
|
|
import { UsersModule } from "./users/users.module";
|
|
import { WebSocketModule } from "./websocket/websocket.module";
|
|
import { LlmModule } from "./llm/llm.module";
|
|
import { LlmUsageModule } from "./llm-usage/llm-usage.module";
|
|
import { BrainModule } from "./brain/brain.module";
|
|
import { CronModule } from "./cron/cron.module";
|
|
import { AgentTasksModule } from "./agent-tasks/agent-tasks.module";
|
|
import { ValkeyModule } from "./valkey/valkey.module";
|
|
import { BullMqModule } from "./bullmq/bullmq.module";
|
|
import { StitcherModule } from "./stitcher/stitcher.module";
|
|
import { TelemetryModule, TelemetryInterceptor } from "./telemetry";
|
|
import { RunnerJobsModule } from "./runner-jobs/runner-jobs.module";
|
|
import { JobEventsModule } from "./job-events/job-events.module";
|
|
import { JobStepsModule } from "./job-steps/job-steps.module";
|
|
import { CoordinatorIntegrationModule } from "./coordinator-integration/coordinator-integration.module";
|
|
import { FederationModule } from "./federation/federation.module";
|
|
|
|
@Module({
|
|
imports: [
|
|
// Rate limiting configuration
|
|
ThrottlerModule.forRootAsync({
|
|
useFactory: () => {
|
|
const ttl = parseInt(process.env.RATE_LIMIT_TTL ?? "60", 10) * 1000; // Convert to milliseconds
|
|
const limit = parseInt(process.env.RATE_LIMIT_GLOBAL_LIMIT ?? "100", 10);
|
|
|
|
return {
|
|
throttlers: [
|
|
{
|
|
ttl,
|
|
limit,
|
|
},
|
|
],
|
|
storage: new ThrottlerValkeyStorageService(),
|
|
};
|
|
},
|
|
}),
|
|
// BullMQ job queue configuration
|
|
BullModule.forRoot({
|
|
connection: {
|
|
host: process.env.VALKEY_HOST ?? "localhost",
|
|
port: parseInt(process.env.VALKEY_PORT ?? "6379", 10),
|
|
},
|
|
}),
|
|
TelemetryModule,
|
|
PrismaModule,
|
|
DatabaseModule,
|
|
ValkeyModule,
|
|
BullMqModule,
|
|
StitcherModule,
|
|
AuthModule,
|
|
ActivityModule,
|
|
TasksModule,
|
|
EventsModule,
|
|
ProjectsModule,
|
|
DomainsModule,
|
|
IdeasModule,
|
|
WidgetsModule,
|
|
LayoutsModule,
|
|
KnowledgeModule,
|
|
UsersModule,
|
|
WebSocketModule,
|
|
LlmModule,
|
|
LlmUsageModule,
|
|
BrainModule,
|
|
CronModule,
|
|
AgentTasksModule,
|
|
RunnerJobsModule,
|
|
JobEventsModule,
|
|
JobStepsModule,
|
|
CoordinatorIntegrationModule,
|
|
FederationModule,
|
|
],
|
|
controllers: [AppController, CsrfController],
|
|
providers: [
|
|
AppService,
|
|
{
|
|
provide: APP_INTERCEPTOR,
|
|
useClass: TelemetryInterceptor,
|
|
},
|
|
{
|
|
provide: APP_GUARD,
|
|
useClass: ThrottlerApiKeyGuard,
|
|
},
|
|
{
|
|
provide: APP_GUARD,
|
|
useClass: CsrfGuard,
|
|
},
|
|
],
|
|
})
|
|
export class AppModule {}
|