This commit is contained in:
Carl-Gerhard Lindesvärd
2025-11-21 11:21:17 +01:00
parent dd71fd4e11
commit 06fb6c4f3c
40 changed files with 2944 additions and 1972 deletions

View File

@@ -12,8 +12,12 @@ import {
getEventsCountCached,
getSettingsForProject,
} from '@openpanel/db';
import { getChart } from '@openpanel/trpc/src/routers/chart.helpers';
import { zChartEvent, zChartInput } from '@openpanel/validation';
import { ChartEngine } from '@openpanel/db';
import {
zChartEvent,
zChartInput,
zChartInputBase,
} from '@openpanel/validation';
import { omit } from 'ramda';
async function getProjectId(
@@ -139,7 +143,7 @@ export async function events(
});
}
const chartSchemeFull = zChartInput
const chartSchemeFull = zChartInputBase
.pick({
breakdowns: true,
interval: true,
@@ -151,14 +155,27 @@ const chartSchemeFull = zChartInput
.extend({
project_id: z.string().optional(),
projectId: z.string().optional(),
events: z.array(
z.object({
name: z.string(),
filters: zChartEvent.shape.filters.optional(),
segment: zChartEvent.shape.segment.optional(),
property: zChartEvent.shape.property.optional(),
}),
),
series: z
.array(
z.object({
name: z.string(),
filters: zChartEvent.shape.filters.optional(),
segment: zChartEvent.shape.segment.optional(),
property: zChartEvent.shape.property.optional(),
}),
)
.optional(),
// Backward compatibility - events will be migrated to series via preprocessing
events: z
.array(
z.object({
name: z.string(),
filters: zChartEvent.shape.filters.optional(),
segment: zChartEvent.shape.segment.optional(),
property: zChartEvent.shape.property.optional(),
}),
)
.optional(),
});
export async function charts(
@@ -179,9 +196,17 @@ export async function charts(
const projectId = await getProjectId(request, reply);
const { timezone } = await getSettingsForProject(projectId);
const { events, ...rest } = query.data;
const { events, series, ...rest } = query.data;
return getChart({
// Use series if available, otherwise fall back to events (backward compat)
const eventSeries = (series ?? events ?? []).map((event: any) => ({
...event,
type: event.type ?? 'event',
segment: event.segment ?? 'event',
filters: event.filters ?? [],
}));
return ChartEngine.execute({
...rest,
startDate: rest.startDate
? DateTime.fromISO(rest.startDate)
@@ -194,11 +219,7 @@ export async function charts(
.toFormat('yyyy-MM-dd HH:mm:ss')
: undefined,
projectId,
events: events.map((event) => ({
...event,
segment: event.segment ?? 'event',
filters: event.filters ?? [],
})),
series: eventSeries,
chartType: 'linear',
metric: 'sum',
});

View File

@@ -7,8 +7,8 @@ import {
ch,
clix,
} from '@openpanel/db';
import { ChartEngine } from '@openpanel/db';
import { getCache } from '@openpanel/redis';
import { getChart } from '@openpanel/trpc/src/routers/chart.helpers';
import { zChartInputAI } from '@openpanel/validation';
import { tool } from 'ai';
import { z } from 'zod';