@@ -11,6 +11,7 @@ import dayjs from 'dayjs';
1111import { processGroupedTimeSeriesData } from './utils.js' ;
1212import { env } from '../../utils/env.js' ;
1313import { FilterInfoType , FilterInfoValue } from '@tianji/shared' ;
14+ import { compact , get } from 'lodash-es' ;
1415
1516// MySQL date formats for different time units
1617const MYSQL_DATE_FORMATS = {
@@ -66,21 +67,20 @@ export function getWarehouseApplications() {
6667 return applications ;
6768}
6869
69- export class WarehouseInsightsSqlBuilder extends InsightsSqlBuilder {
70- private connection : Pool | null = null ;
71-
72- private getConnection ( ) : Pool {
73- if ( ! env . insights . warehouse . enable || ! env . insights . warehouse . url ) {
74- throw new Error ( 'Warehouse is not enabled' ) ;
75- }
76-
77- if ( ! this . connection ) {
78- this . connection = createPool ( env . insights . warehouse . url ) ;
79- }
70+ let connection : Pool | null = null ;
71+ export function getWarehouseConnection ( ) {
72+ if ( ! env . insights . warehouse . enable || ! env . insights . warehouse . url ) {
73+ throw new Error ( 'Warehouse is not enabled' ) ;
74+ }
8075
81- return this . connection ;
76+ if ( ! connection ) {
77+ connection = createPool ( env . insights . warehouse . url ) ;
8278 }
8379
80+ return connection ;
81+ }
82+
83+ export class WarehouseInsightsSqlBuilder extends InsightsSqlBuilder {
8484 getApplication ( ) : WarehouseInsightsApplication {
8585 const name = this . query . insightId ;
8686
@@ -388,7 +388,7 @@ export class WarehouseInsightsSqlBuilder extends InsightsSqlBuilder {
388388 }
389389
390390 async executeQuery ( sql : Prisma . Sql ) : Promise < any [ ] > {
391- const connection = this . getConnection ( ) ;
391+ const connection = getWarehouseConnection ( ) ;
392392
393393 const [ rows ] = await connection . query (
394394 sql . sql . replaceAll ( '"' , '`' ) , // avoid mysql and pg sql syntax error about double quote
@@ -412,3 +412,31 @@ export async function insightsWarehouse(
412412
413413 return result ;
414414}
415+
416+ export async function insightsWarehouseEvents (
417+ applicationId : string
418+ ) : Promise < string [ ] > {
419+ const connection = getWarehouseConnection ( ) ;
420+ const eventTable = getWarehouseApplications ( ) . find (
421+ ( app ) => app . name === applicationId
422+ ) ?. eventTable ;
423+
424+ if ( ! eventTable ) {
425+ throw new Error ( `Event table not found for application ${ applicationId } ` ) ;
426+ }
427+
428+ const [ rows ] = await connection . query ( `
429+ SELECT DISTINCT event_name
430+ FROM (
431+ SELECT \`${ eventTable . eventNameField } \` as event_name, \`${ eventTable . dateBasedCreatedAtField ?? eventTable . createdAtField } \` as date
432+ FROM \`${ eventTable . name } \`
433+ ORDER BY \`${ eventTable . dateBasedCreatedAtField ?? eventTable . createdAtField } \` DESC
434+ ) t
435+ LIMIT 100;` ) ;
436+
437+ if ( ! Array . isArray ( rows ) ) {
438+ throw new Error ( `Invalid rows from warehouse` ) ;
439+ }
440+
441+ return compact ( rows . map ( ( row ) => get ( row , 'event_name' , '' ) ) ) ;
442+ }
0 commit comments