@@ -303,6 +303,20 @@ model ChatMessage {
303303// ============================================================================
304304
305305// Agent Events - Individual actions (TimescaleDB hypertable)
306+ //
307+ // NOTE: This table is converted to a TimescaleDB hypertable for time-series optimization.
308+ // The conversion is performed by scripts/enable-timescaledb.sql which:
309+ // - Creates a hypertable partitioned by timestamp (1-day chunks)
310+ // - Enables compression after 7 days (70-90% storage reduction)
311+ // - Sets up automatic retention policy (1 year)
312+ // - Creates continuous aggregates for hourly and daily statistics
313+ //
314+ // Performance characteristics:
315+ // - Write throughput: 50-100K events/sec
316+ // - Query latency: 30-50ms P95 for time-range queries
317+ // - Storage: 200-500 bytes per event after compression
318+ //
319+ // See: specs/20251031/001-database-architecture/README.md for details
306320model AgentEvent {
307321 id String @id @default (uuid () ) @db.Uuid
308322 timestamp DateTime @db.Timestamptz
@@ -333,13 +347,15 @@ model AgentEvent {
333347 session ChatSession @relation (fields : [sessionId ] , references : [sessionId ] , onDelete : Cascade )
334348 project Project @relation (fields : [projectId ] , references : [id ] , onDelete : Cascade )
335349
336- @@index ([timestamp (sort : Desc ) ] )
337- @@index ([sessionId ] )
338- @@index ([agentId ] )
339- @@index ([eventType ] )
340- @@index ([projectId ] )
341- @@index ([tags ] )
342- @@index ([severity ] )
350+ // Indexes optimized for TimescaleDB time-series queries
351+ @@index ([timestamp (sort : Desc ) ] ) // Primary time-series index
352+ @@index ([sessionId , timestamp (sort : Desc ) ] ) // Session timeline queries (composite)
353+ @@index ([projectId , timestamp (sort : Desc ) ] ) // Project timeline queries (composite)
354+ @@index ([agentId ] ) // Filter by agent type
355+ @@index ([eventType ] ) // Filter by event type
356+ @@index ([tags ] ) // Array index for tag filtering
357+ @@index ([severity ] ) // Filter by severity level
358+ @@index ([data ] ) // GIN index for JSONB field queries (created in migration)
343359 @@map (" agent_events " )
344360}
345361
0 commit comments