Skip to content

Commit

Permalink
fix: Convert writing events to a raw query (#99)
Browse files Browse the repository at this point in the history
Due to drizzle-team/drizzle-orm#1061 the
events lose the ms precision on the timestamp. This PR converts the
query to a raw query, so the precision is preserved.
  • Loading branch information
nadeesha authored Jan 31, 2024
1 parent 2948d1e commit 627c8b9
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions control-plane/src/modules/observability/events.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { sql } from "drizzle-orm";
import { ulid } from "ulid";
import * as data from "../data";

Expand Down Expand Up @@ -66,17 +67,20 @@ class EventWriterBuffer {

private async writeEvents(events: InsertableEvent[], attempt = 0) {
try {
const result = await data.db.insert(data.events).values(
events.map((e) => ({
id: ulid(),
cluster_id: e.clusterId,
type: e.type,
job_id: e.jobId,
machine_id: e.machineId,
created_at: e.createdAt,
meta: e.meta,
service: e.service,
})),
if (events.length === 0) {
return;
}

const values = events.map(
(e) =>
sql`(${ulid()}, ${e.clusterId}, ${e.type}, ${e.jobId ?? null}, ${e.machineId ?? null}, ${e.createdAt}, ${e.meta ?? null}, ${e.service ?? null})`,
);

const result = await data.db.execute(
sql`
INSERT INTO events (id, cluster_id, type, job_id, machine_id, created_at, meta, service)
VALUES ${sql.join(values, sql`,`)};
`,
);

console.log("Wrote events", {
Expand Down

0 comments on commit 627c8b9

Please sign in to comment.