Skip to content

Commit 8737a34

Browse files
committed
feat: add event names fetcher
1 parent 7c3ac39 commit 8737a34

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

src/server/model/insights/warehouse.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import dayjs from 'dayjs';
1111
import { processGroupedTimeSeriesData } from './utils.js';
1212
import { env } from '../../utils/env.js';
1313
import { FilterInfoType, FilterInfoValue } from '@tianji/shared';
14+
import { compact, get } from 'lodash-es';
1415

1516
// MySQL date formats for different time units
1617
const 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+
}

src/server/trpc/routers/insights.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import { stringifyDateType } from '../../utils/common.js';
1111
import { queryEvents, queryInsight } from '../../model/insights/index.js';
1212
import { insightsSurveyBuiltinFields } from '../../model/insights/utils.js';
1313
import { uniq } from 'lodash-es';
14-
import { getWarehouseApplications } from '../../model/insights/warehouse.js';
14+
import {
15+
getWarehouseApplications,
16+
insightsWarehouseEvents,
17+
} from '../../model/insights/warehouse.js';
1518

1619
export const insightsRouter = router({
1720
query: workspaceProcedure
@@ -67,6 +70,14 @@ export const insightsRouter = router({
6770
return [];
6871
}
6972

73+
if (insightType === 'warehouse') {
74+
const events = await insightsWarehouseEvents(insightId);
75+
return events.map((item) => ({
76+
name: item,
77+
count: 0,
78+
}));
79+
}
80+
7081
return [];
7182
}),
7283
filterParams: workspaceProcedure

0 commit comments

Comments
 (0)