Skip to content

Commit 47f3506

Browse files
committed
feat: add insightsWarehouseFilterParams function to fetch distinct event parameters
1 parent c2df195 commit 47f3506

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/server/model/insights/warehouse.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,31 @@ LIMIT 100;`);
440440

441441
return compact(rows.map((row) => get(row, 'event_name', '')));
442442
}
443+
444+
export async function insightsWarehouseFilterParams(
445+
applicationId: string
446+
): Promise<string[]> {
447+
const connection = getWarehouseConnection();
448+
const eventParametersTable = getWarehouseApplications().find(
449+
(app) => app.name === applicationId
450+
)?.eventParametersTable;
451+
452+
if (!eventParametersTable) {
453+
throw new Error(`Event table not found for application ${applicationId}`);
454+
}
455+
456+
const [rows] = await connection.query(`
457+
SELECT DISTINCT param_value
458+
FROM (
459+
SELECT \`${eventParametersTable.paramsNameField}\` as param_value, \`${eventParametersTable.dateBasedCreatedAtField ?? eventParametersTable.createdAtField}\` as date
460+
FROM \`${eventParametersTable.name}\`
461+
ORDER BY \`${eventParametersTable.dateBasedCreatedAtField ?? eventParametersTable.createdAtField}\` DESC
462+
) t
463+
LIMIT 100;`);
464+
465+
if (!Array.isArray(rows)) {
466+
throw new Error(`Invalid rows from warehouse`);
467+
}
468+
469+
return compact(rows.map((row) => get(row, 'param_value', '')));
470+
}

src/server/trpc/routers/insights.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { uniq } from 'lodash-es';
1414
import {
1515
getWarehouseApplications,
1616
insightsWarehouseEvents,
17+
insightsWarehouseFilterParams,
1718
} from '../../model/insights/warehouse.js';
1819

1920
export const insightsRouter = router({
@@ -139,6 +140,13 @@ export const insightsRouter = router({
139140
}));
140141

141142
return [...payloadFields, ...builtinFields];
143+
} else if (insightType === 'warehouse') {
144+
const params = await insightsWarehouseFilterParams(insightId);
145+
return params.map((item) => ({
146+
name: item,
147+
type: 'string',
148+
count: 0,
149+
}));
142150
}
143151

144152
return [];

0 commit comments

Comments
 (0)