@@ -6,9 +6,7 @@ import type {
66export type EventTypeLabel = "BASIC_USAGE" | "AI_TOKEN_USAGE" | "PAYMENT" ;
77
88export type EventTableName =
9- | "basic_usage_events"
10- | "ai_token_usage_events"
11- | "payment_events" ;
9+ "basic_usage_events" | "ai_token_usage_events" | "payment_events" ;
1210
1311const EVENT_TYPE_TO_TABLE : Record < EventTypeLabel , EventTableName > = {
1412 BASIC_USAGE : "basic_usage_events" ,
@@ -43,50 +41,33 @@ export const OPERATOR_SQL: Record<string, string> = {
4341 NEQ : "!=" ,
4442} ;
4543
46- interface EventTypeFilter {
47- operator : string ;
48- value : string ;
49- }
44+ function canTableMatch (
45+ group : QueryFilterGroup ,
46+ table : EventTableName
47+ ) : boolean {
48+ const tableEventType = TABLE_TO_EVENT_TYPE [ table ] ;
5049
51- function collectEventTypeFilters ( group : QueryFilterGroup ) : EventTypeFilter [ ] {
52- const filters : EventTypeFilter [ ] = [ ] ;
53- for ( const c of group . conditions ) {
50+ const conditionResults = group . conditions . map ( ( c ) => {
5451 if ( c . field === "eventType" ) {
55- filters . push ( { operator : c . operator , value : c . value } ) ;
52+ if ( c . operator === "EQ" ) return c . value === tableEventType ;
53+ if ( c . operator === "NEQ" ) return c . value !== tableEventType ;
54+ return true ;
5655 }
57- }
58- for ( const sub of group . groups ) {
59- filters . push ( ...collectEventTypeFilters ( sub ) ) ;
60- }
61- return filters ;
62- }
56+ return true ;
57+ } ) ;
6358
64- export function getTablesForRequest ( where : QueryFilterGroup ) : EventTableName [ ] {
65- const filters = collectEventTypeFilters ( where ) ;
66- if ( filters . length === 0 ) {
67- return [ ...ALL_TABLES ] ;
68- }
59+ const groupResults = group . groups . map ( ( g ) => canTableMatch ( g , table ) ) ;
60+ const allResults = [ ...conditionResults , ...groupResults ] ;
6961
70- const included = new Set < EventTableName > ( ) ;
71- const excluded = new Set < EventTableName > ( ) ;
62+ if ( allResults . length === 0 ) return true ;
7263
73- for ( const { operator, value } of filters ) {
74- if ( ! ( value in EVENT_TYPE_TO_TABLE ) ) continue ;
75- const table = EVENT_TYPE_TO_TABLE [ value as EventTypeLabel ] ;
76- if ( operator === "EQ" ) {
77- included . add ( table ) ;
78- } else if ( operator === "NEQ" ) {
79- excluded . add ( table ) ;
80- }
81- }
82-
83- if ( included . size > 0 ) {
84- return [ ...included ] ;
85- }
86-
87- if ( excluded . size > 0 ) {
88- return ALL_TABLES . filter ( ( t ) => ! excluded . has ( t ) ) ;
64+ if ( group . logical === "AND" ) {
65+ return allResults . every ( ( res ) => res ) ;
66+ } else {
67+ return allResults . some ( ( res ) => res ) ;
8968 }
69+ }
9070
91- return [ ] ;
71+ export function getTablesForRequest ( where : QueryFilterGroup ) : EventTableName [ ] {
72+ return ALL_TABLES . filter ( ( table ) => canTableMatch ( where , table ) ) ;
9273}
0 commit comments