This repository has been 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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
91 additions
and
6 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
modules/analytics/db/migrations/20240819042229_init/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- CreateTable | ||
CREATE TABLE "Event" ( | ||
"id" UUID NOT NULL, | ||
"timestamp" TIMESTAMPTZ NOT NULL, | ||
"name" TEXT NOT NULL, | ||
"metadata" JSONB | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Event_id_key" ON "Event"("id"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "Event_name_time_idx" ON "Event"("name", "timestamp" DESC); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,9 @@ | |
"scripts": { | ||
"push_event": { | ||
"public": true | ||
}, | ||
"query_instant": { | ||
"public": true | ||
} | ||
}, | ||
"errors": {}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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 | ||
})) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { ScriptContext } from "../module.gen.ts"; | ||
|
||
let hasDefinitelyRun = false; | ||
export const createHypertable = async (ctx: ScriptContext) => { | ||
export const checkHypertable = async (ctx: ScriptContext) => { | ||
if (hasDefinitelyRun) return; | ||
|
||
await ctx.db.$queryRaw`SELECT create_hypertable('event', 'timestamp');`; | ||
// await ctx.db.$queryRaw`SELECT create_hypertable('event', 'timestamp');`; | ||
|
||
hasDefinitelyRun = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Filter } from "./types.ts"; | ||
|
||
export const stringifyFilters = (filters: Filter[]) => filters.map((filter: Filter) => { | ||
if ("greaterThan" in filter) return "(metadata->>'" + filter.greaterThan.key + "')::int" + " > " + filter.greaterThan.value; | ||
if ("lessThan" in filter) return "(metadata->>'" + filter.lessThan.key + "')::int" + " < " + filter.lessThan.value; | ||
if ("equals" in filter) return "(metadata->>'" + filter.equals.key + "')::int" + " = " + filter.equals.value; | ||
if ("notEquals" in filter) return "(metadata->>'" + filter.notEquals.key + "')::int" + " != " + filter.notEquals.value; | ||
if ("greaterThanOrEquals" in filter) return "(metadata->>'" + filter.greaterThanOrEquals.key + "')::int" + " >= " + filter.greaterThanOrEquals.value; | ||
if ("lessThanOrEquals" in filter) return "(metadata->>'" + filter.lessThanOrEquals.key + "')::int" + " <= " + filter.lessThanOrEquals.value; | ||
|
||
throw new Error("Unknown filter type"); | ||
}).join(' AND '); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export type AggregationMethod = { count: {} } | | ||
{ averageByKey: string } | | ||
{ sumByKey: string }; | ||
|
||
export type Filter = { greaterThan: { key: string, value: number } } | | ||
{ lessThan: { key: string, value: number } } | | ||
{ equals: { key: string, value: number } } | | ||
{ notEquals: { key: string, value: number } } | | ||
{ greaterThanOrEquals: { key: string, value: number } } | | ||
{ lessThanOrEquals: { key: string, value: number } }; |