diff --git a/packages/core/src/session/stats.ts b/packages/core/src/session/stats.ts index 17a51576ec8e..e73eeba19fd1 100644 --- a/packages/core/src/session/stats.ts +++ b/packages/core/src/session/stats.ts @@ -86,7 +86,7 @@ export const get = Effect.fn("SessionStats.get")(function* (input: Input = {}) { const db = (yield* Database.Service).db const to = input.to ?? Date.now() if (input.from !== undefined && input.from >= to) return yield* new InvalidRangeError({ from: input.from, to }) - const project = input.projectID === undefined ? sql`` : sql`AND session.project_id = ${input.projectID}` + const project = input.projectID ? sql`AND session.project_id = ${input.projectID}` : sql`` const from = input.from ?? (yield* db @@ -282,7 +282,7 @@ export const get = Effect.fn("SessionStats.get")(function* (input: Input = {}) { const ids = yield* db .select({ id: SessionTable.id }) .from(SessionTable) - .where(input.projectID === undefined ? undefined : eq(SessionTable.project_id, input.projectID)) + .where(input.projectID ? eq(SessionTable.project_id, input.projectID) : undefined) .all() .pipe(Effect.orDie) const events = (yield* Effect.forEach( diff --git a/packages/core/test/session-stats.test.ts b/packages/core/test/session-stats.test.ts index c71d1fe86e81..5fe81d8cd810 100644 --- a/packages/core/test/session-stats.test.ts +++ b/packages/core/test/session-stats.test.ts @@ -235,6 +235,17 @@ describe("SessionStats", () => { const project = yield* SessionStats.get({ projectID, timezone: "UTC", tools: "none" }) expect(DateTime.toEpochMillis(project.range.from)).toBe(Date.UTC(2025, 11, 30, 10)) + // An empty project filter behaves like an omitted one. No explicit `from`, + // so this also covers the derived-range query that shares the same filter. + const unfiltered = yield* SessionStats.get({ to: Date.UTC(2026, 1, 1), timezone: "UTC", tools: "none" }) + const emptyProject = yield* SessionStats.get({ + to: Date.UTC(2026, 1, 1), + projectID: Project.ID.make(""), + timezone: "UTC", + tools: "none", + }) + expect(emptyProject).toEqual(unfiltered) + const error = yield* Effect.flip( SessionStats.get({ from: Date.UTC(2026, 1, 1), to: Date.UTC(2026, 0, 1), tools: "none" }), )