Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
feat(OGBEE-81): Implement push_event and hypertable init (OGBEE-89)
Browse files Browse the repository at this point in the history
  • Loading branch information
ABCxFF committed Aug 25, 2024
1 parent 641611d commit ea5e446
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions modules/analytics/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"ABCxFF"
],
"scripts": {
"push_event": {
"public": true
}
},
"errors": {},
"dependencies": {}
Expand Down
31 changes: 31 additions & 0 deletions modules/analytics/scripts/push_event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ScriptContext } from "../module.gen.ts";
import { createHypertable } from "../utils/hypertable_init.ts";

export interface Request {
name: string,
metadata: any,
timestampOverride?: string,
}

export interface Response {
id: string,
timestamp: number,
}

export async function run(
ctx: ScriptContext,
req: Request,
): Promise<Response> {
createHypertable(ctx);
const timestamp = req.timestampOverride ? new Date(req.timestampOverride) : new Date();
const event = await ctx.db.event.create({
data: {
name: req.name,
timestamp,
metadata: req.metadata
}
});

return { id: event.id, timestamp: timestamp.getTime() };
}

10 changes: 10 additions & 0 deletions modules/analytics/utils/hypertable_init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ScriptContext } from "../module.gen.ts";

let hasDefinitelyRun = false;
export const createHypertable = async (ctx: ScriptContext) => {
if (hasDefinitelyRun) return;

await ctx.db.$queryRaw`SELECT create_hypertable('event', 'timestamp');`;

hasDefinitelyRun = true;
}

0 comments on commit ea5e446

Please sign in to comment.