Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix(mcp): a date-only end date includes that whole day (#546)

Carl-Gerhard Lindesvärd1236c048b1ab
diff --git a/packages/core/src/modules/chart/funnel.service.ts b/packages/core/src/modules/chart/funnel.service.ts
index 18790f038..6b283835f 100644
--- a/packages/core/src/modules/chart/funnel.service.ts
+++ b/packages/core/src/modules/chart/funnel.service.ts
@@ -1,6 +1,7 @@
import { ifNaN } from '@openpanel/shared';
import { last, reverse } from 'ramda';
import type { ServiceDeps, Services } from '../../services';
+import { toRangeBoundaryLiteral } from '../../shared/ch-dates';
import { getSettingsForProject } from '../organization/organization.service';
import type {
IChartBreakdown,
@@ -354,8 +355,8 @@ export async function getFunnelCore(
const result = await getFunnel(deps, {
projectId: input.projectId,
- startDate: input.startDate,
- endDate: input.endDate,
+ startDate: toRangeBoundaryLiteral(input.startDate, 'start'),
+ endDate: toRangeBoundaryLiteral(input.endDate, 'end'),
series: eventSeries,
breakdowns: [],
chartType: 'funnel',
diff --git a/packages/core/src/modules/chart/sankey.service.ts b/packages/core/src/modules/chart/sankey.service.ts
index c184a974f..ea960447f 100644
--- a/packages/core/src/modules/chart/sankey.service.ts
+++ b/packages/core/src/modules/chart/sankey.service.ts
@@ -1,6 +1,7 @@
import type { SqlFragment } from '@openpanel/db/src/clickhouse/sql';
import { TRPCBadRequestError } from '../../rpc/errors';
import type { ServiceDeps, Services } from '../../services';
+import { toRangeBoundaryLiteral } from '../../shared/ch-dates';
import { getSettingsForProject } from '../organization/organization.service';
import {
chartColors,
@@ -511,8 +512,8 @@ export async function getUserFlowCore(
const { timezone } = await getSettingsForProject(deps, input.projectId);
const result = await getSankey(deps, {
projectId: input.projectId,
- startDate: input.startDate,
- endDate: input.endDate,
+ startDate: toRangeBoundaryLiteral(input.startDate, 'start'),
+ endDate: toRangeBoundaryLiteral(input.endDate, 'end'),
steps: input.steps ?? DEFAULT_SANKEY_STEPS,
mode: input.mode,
startEvent: toChartEvent(input.startEvent),
diff --git a/packages/core/src/modules/event/event.service.ts b/packages/core/src/modules/event/event.service.ts
index 835b584a4..fdd182254 100644
--- a/packages/core/src/modules/event/event.service.ts
+++ b/packages/core/src/modules/event/event.service.ts
@@ -6,6 +6,7 @@ import { clone, mergeDeepRight, uniq } from 'ramda';
import { cacheablePerDeps } from '../../cacheable-per-deps';
import { chQuery } from '../../ch-query';
import type { ServiceDeps, Services } from '../../services';
+import { toRangeBoundaryLiteral } from '../../shared/ch-dates';
import { stripFixedStringPadding } from '../../shared/ch-fixed-string';
import { getEventFiltersWhereClause } from '../chart/src/filter-where';
import { buildFilterWhere } from '../chart/src/table-filter-where';
@@ -977,8 +978,8 @@ function queryEventsDateRange(
input.endDate
);
return {
- start: formatClickhouseDate(new Date(startDate)),
- end: formatClickhouseDate(new Date(endDate)),
+ start: toRangeBoundaryLiteral(startDate, 'start'),
+ end: toRangeBoundaryLiteral(endDate, 'end'),
};
}
diff --git a/packages/core/src/modules/mcp/src/integration/tools.test.ts b/packages/core/src/modules/mcp/src/integration/tools.test.ts
index 50a41f0b0..293b2798d 100644
--- a/packages/core/src/modules/mcp/src/integration/tools.test.ts
+++ b/packages/core/src/modules/mcp/src/integration/tools.test.ts
@@ -90,7 +90,7 @@ beforeAll(async () => {
);
await bootstrapTestDatabases();
await fixtures.setupPostgresFixtures(TEST_PROJECT_ID, TEST_ORG_ID);
- pinFixtureClock();
+ CHARLIE_DAY = fixtures.timeAgo(pinFixtureClock(), 5).slice(0, 10);
await fixtures.setupFixtures(TEST_PROJECT_ID);
const { testServiceDeps } = await import('../../../../../test/service-deps');
@@ -120,6 +120,9 @@ const CTX = {
/** What `createMcpServer` hands every tool at runtime. Built in `beforeAll`. */
let TOOLS: McpToolDeps;
+/** `YYYY-MM-DD` of charlie's first session, the only fixture events on that day. */
+let CHARLIE_DAY: string;
+
function makeServer() {
const handlers = new Map<string, (input: unknown) => Promise<unknown>>();
return {
@@ -706,6 +709,49 @@ describe('get_user_flow', () => {
});
});
+// A bare `endDate` names a whole day: asking for one day must not come back empty.
+describe('a date-only endDate includes that day', () => {
+ it('query_events returns the events of a single day', async () => {
+ const server = makeServer();
+ registerEventTools(server as any, TOOLS);
+ const res = await server.invoke('query_events', {
+ projectId: TEST_PROJECT_ID,
+ startDate: CHARLIE_DAY,
+ endDate: CHARLIE_DAY,
+ });
+ const events = rowsOf(res);
+ expect(events.length).toBe(5);
+ expect(events.every((e) => e.profile_id === FIXTURE.profiles.charlie)).toBe(
+ true
+ );
+ });
+
+ it('get_funnel counts a conversion on a single day', async () => {
+ const server = makeServer();
+ registerFunnelTools(server as any, TOOLS);
+ const res = await server.invoke('get_funnel', {
+ projectId: TEST_PROJECT_ID,
+ startDate: CHARLIE_DAY,
+ endDate: CHARLIE_DAY,
+ steps: ['session_start', 'purchase'],
+ });
+ expect(res.completedUsers).toBe(1);
+ });
+
+ it('get_user_flow finds the flow of a single day', async () => {
+ const server = makeServer();
+ registerUserFlowTools(server as any, TOOLS);
+ const res = await server.invoke('get_user_flow', {
+ projectId: TEST_PROJECT_ID,
+ startDate: CHARLIE_DAY,
+ endDate: CHARLIE_DAY,
+ startEvent: 'session_start',
+ mode: 'after',
+ });
+ expect(res.node_count).toBeGreaterThan(0);
+ });
+});
+
describe('get_rolling_active_users', () => {
it('returns DAU series (may be empty — dau_mv not auto-populated)', async () => {
const server = makeServer();