package postgres import ( "context" "encoding/json" "fmt" "time" ) type EventRow struct { EventID string `json:"event_id"` TS time.Time `json:"ts"` Type string `json:"type"` Payload json.RawMessage `json:"payload"` } type EventsFilter struct { Limit int EventType string Framework string } func (d *DB) ListRecentEvents(ctx context.Context, f EventsFilter) ([]EventRow, error) { if f.Limit <= 0 { f.Limit = 100 } if f.Limit > 1000 { f.Limit = 1000 } query := "SELECT event_id, ts, type, payload FROM events WHERE 1=1" args := []any{} argN := 1 if f.EventType != "" { query += fmt.Sprintf(" AND type = $%d", argN) args = append(args, f.EventType) argN++ } if f.Framework != "" { query += fmt.Sprintf(" AND source_framework = $%d", argN) args = append(args, f.Framework) argN++ } query += fmt.Sprintf(" ORDER BY ts DESC LIMIT $%d", argN) args = append(args, f.Limit) rows, err := d.sql.QueryContext(ctx, query, args...) if err != nil { return nil, err } defer rows.Close() var out []EventRow for rows.Next() { var r EventRow if err := rows.Scan(&r.EventID, &r.TS, &r.Type, &r.Payload); err != nil { return nil, err } out = append(out, r) } return out, rows.Err() }