@@ -3,7 +3,7 @@ import type { Kysely } from 'kysely';
33import { DbDeps } from '@/infra/db/db.dep' ;
44import type { DatabaseScheme } from '@/infra/db/db-scheme' ;
55import type { EventRow , NewEventRow } from '@/infra/db/events.table' ;
6- import type { Event , EventGeo , NewEvent } from '../domain/entity/event.entity' ;
6+ import type { Event , EventGeo , EventMetric , NewEvent } from '../domain/entity/event.entity' ;
77import type { EventSources } from '../domain/event.enums' ;
88import type {
99 IEventRepository ,
@@ -53,6 +53,31 @@ export class EventRepository implements IEventRepository {
5353 return rows . map ( ( row ) => toEvent ( row ) ) ;
5454 }
5555
56+ public async listEventMetrics ( params : ListEventsParams ) : Promise < EventMetric [ ] > {
57+ const limit = params . limit ?? 50 ;
58+
59+ let query = this . db
60+ . selectFrom ( 'events' )
61+ . select ( [ 'id' , 'source' , 'kind' , 'occurred_at' , 'level' ] )
62+ . orderBy ( 'fetched_at' , 'desc' )
63+ . limit ( limit ) ;
64+
65+ if ( params . kind !== undefined ) {
66+ query = query . where ( 'kind' , '=' , params . kind ) ;
67+ }
68+
69+ if ( params . source ) {
70+ query = query . where ( 'source' , '=' , params . source ) ;
71+ }
72+
73+ if ( params . since !== undefined ) {
74+ query = query . where ( 'fetched_at' , '>=' , params . since ) ;
75+ }
76+
77+ const rows = await query . execute ( ) ;
78+ return rows . map ( ( row ) => toEventMetric ( row ) ) ;
79+ }
80+
5681 public async listEventsAfterId ( params : ListEventsAfterIdParams ) : Promise < Event [ ] > {
5782 const limit = params . limit ?? 500 ;
5883
@@ -163,6 +188,18 @@ function toEvent(row: EventRow): Event {
163188 } ;
164189}
165190
191+ type EventMetricRow = Pick < EventRow , 'id' | 'source' | 'kind' | 'occurred_at' | 'level' > ;
192+
193+ function toEventMetric ( row : EventMetricRow ) : EventMetric {
194+ return {
195+ id : row . id ,
196+ source : row . source ,
197+ kind : row . kind ,
198+ occurredAt : normalizeTimestamp ( row . occurred_at ) ,
199+ level : row . level ,
200+ } ;
201+ }
202+
166203function toGeo ( lat : number | null , lng : number | null ) : EventGeo | null {
167204 if ( lat === null || lng === null ) {
168205 return null ;
0 commit comments