Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/webapp/app/v3/eventRepository/common.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function extractContextFromCarrier(carrier: Record<string, unknown>) {
}

export function getNowInNanoseconds(): bigint {
return BigInt(new Date().getTime() * 1_000_000);
return convertDateToNanoseconds(new Date());
}

export function getDateFromNanoseconds(nanoseconds: bigint): Date {
Expand All @@ -39,7 +39,7 @@ export function calculateDurationFromStart(
) {
const $endtime = typeof endTime === "string" ? new Date(endTime) : endTime;

const duration = Number(BigInt($endtime.getTime() * 1_000_000) - startTime);
const duration = Number(convertDateToNanoseconds($endtime) - startTime);

if (minimumDuration && duration < minimumDuration) {
return minimumDuration;
Expand Down
7 changes: 3 additions & 4 deletions apps/webapp/app/v3/eventRepository/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { FEATURE_FLAG } from "../featureFlags";
import { flag } from "../featureFlags.server";
import { getTaskEventStore } from "../taskEventStore.server";
import { clickhouseFactory } from "~/services/clickhouse/clickhouseFactoryInstance.server";
import { convertDateToNanoseconds } from "./common.server";

export const EVENT_STORE_TYPES = {
POSTGRES: "postgres",
CLICKHOUSE: "clickhouse",
POSTGRES: "postgres", CLICKHOUSE: "clickhouse",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Object literal breaks formatter check

The EVENT_STORE_TYPES literal puts POSTGRES and CLICKHOUSE on one line with a double space. oxfmt reformats this, so the code-quality CI check fails on the diff.

Suggested change
POSTGRES: "postgres", CLICKHOUSE: "clickhouse",
POSTGRES: "postgres",
CLICKHOUSE: "clickhouse",
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

CLICKHOUSE_V2: "clickhouse_v2",
} as const;

Expand Down Expand Up @@ -208,10 +208,9 @@ async function recordRunEvent(
runId: foundRun.friendlyId,
...attributes,
},
startTime: BigInt((startTime?.getTime() ?? Date.now()) * 1_000_000),
startTime: convertDateToNanoseconds(startTime ?? new Date()),
...optionsRest,
});

return {
success: true,
};
Expand Down
7 changes: 5 additions & 2 deletions apps/webapp/app/v3/runEngineHandlers.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import { MetadataTooLargeError } from "~/utils/packets";
import { QueueSizeLimitExceededError } from "~/v3/services/common.server";
import { TriggerTaskService } from "~/v3/services/triggerTask.server";
import { tracer } from "~/v3/tracer.server";
import { createExceptionPropertiesFromError } from "./eventRepository/common.server";
import {
convertDateToNanoseconds,
createExceptionPropertiesFromError,
} from "./eventRepository/common.server";
import { getEventRepositoryForStore, recordRunDebugLog } from "./eventRepository/index.server";
import { roomFromFriendlyRunId, socketIo } from "./handleSocketIo.server";
import { engine } from "./runEngine.server";
Expand Down Expand Up @@ -555,7 +558,7 @@ export function registerRunEngineEventBusHandlers() {
);

await eventRepository.recordEvent(retryMessage, {
startTime: BigInt(time.getTime() * 1000000),
startTime: convertDateToNanoseconds(time),
taskSlug: run.taskIdentifier,
environment,
attributes: {
Expand Down
31 changes: 31 additions & 0 deletions apps/webapp/test/eventRepositoryNanoseconds.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import {
calculateDurationFromStart,
convertDateToNanoseconds,
getNowInNanoseconds,
} from "~/v3/eventRepository/common.server";

const EPOCH_MS = 1_782_994_600_413;

describe("event repository nanosecond conversion", () => {
afterEach(() => {
vi.useRealTimers();
});

it("converts a date to nanoseconds without losing precision", () => {
expect(convertDateToNanoseconds(new Date(EPOCH_MS))).toBe(1_782_994_600_413_000_000n);
});

it("returns the current time in exact nanoseconds", () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(EPOCH_MS));

expect(getNowInNanoseconds()).toBe(1_782_994_600_413_000_000n);
});

it("calculates an exact duration from a nanosecond start time", () => {
const startTime = convertDateToNanoseconds(new Date(EPOCH_MS));

expect(calculateDurationFromStart(startTime, new Date(EPOCH_MS + 2))).toBe(2_000_000);
});
});