This repository was archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery_instant.ts
48 lines (41 loc) · 1.62 KB
/
query_instant.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { ScriptContext } from "../module.gen.ts";
import { checkHypertable } from "../utils/hypertable_init.ts";
import { stringifyFilters } from "../utils/stringify_filters.ts";
import { AggregationMethod, Filter } from "../utils/types.ts";
export interface Request {
event: string;
aggregate: AggregationMethod;
filters: Filter[]
groupBy: string[];
startAt: number;
stopAt: number;
}
export interface Response {
results: { groups: Record<string, any>, count: number}[]
}
export async function run(
ctx: ScriptContext,
req: Request,
): Promise<Response> {
checkHypertable(ctx);
const props = req.groupBy.map((col) => `metadata->>'${col}'`);
// A query that counts the amount of events in the database, per name (should return an array of counts per name)
// the name isn't an actual field but instead a value in the metadata field
const result = await ctx.db.$queryRawUnsafe(`
SELECT ${req.groupBy.map(col => `metadata->>'${col}' as _${col}`).join(', ')}, COUNT(*) as count
FROM "${ctx.dbSchema}"."Event"
WHERE name = '${req.event}'
AND timestamp >= '${new Date(req.startAt).toISOString()}'
AND timestamp <= '${new Date(req.stopAt).toISOString()}'
${req.filters.length ? " AND " + stringifyFilters(req.filters) : ""}
GROUP BY ${props.join(', ')}
ORDER BY ${props.join(', ')}
`) as any;
return {
results: result.map((e: any) => ({
// TODO: optimize
groups: props.reduce<Record<string, any>>((acc, k) => (acc[k] = e["_" + k], acc), {}),
count: e.count
}))
}
}