( input: IReportInput, )
| 16 | * Normalize a chart input into a clean structure with dates and normalized series |
| 17 | */ |
| 18 | export async function normalize( |
| 19 | input: IReportInput, |
| 20 | ): Promise<IReportInputWithDates & { series: SeriesDefinition[] }> { |
| 21 | const { timezone } = await getSettingsForProject(input.projectId); |
| 22 | const { startDate, endDate } = getChartStartEndDate( |
| 23 | { |
| 24 | range: input.range, |
| 25 | startDate: input.startDate ?? undefined, |
| 26 | endDate: input.endDate ?? undefined, |
| 27 | }, |
| 28 | timezone, |
| 29 | ); |
| 30 | |
| 31 | // Get series from input (handles both 'series' and 'events' fields) |
| 32 | // The schema preprocessing should have already converted 'events' to 'series', but handle both for safety |
| 33 | const rawSeries = (input as any).series ?? (input as any).events ?? []; |
| 34 | |
| 35 | // Normalize each series item |
| 36 | const normalizedSeries: SeriesDefinition[] = rawSeries.map( |
| 37 | (item: any, index: number) => { |
| 38 | // If item already has type field, it's the new format |
| 39 | if (item && typeof item === 'object' && 'type' in item) { |
| 40 | return { |
| 41 | ...item, |
| 42 | id: item.id ?? alphabetIds[index] ?? `series-${index}`, |
| 43 | } as SeriesDefinition; |
| 44 | } |
| 45 | |
| 46 | // Old format without type field - assume it's an event |
| 47 | const event = item as Partial<IChartEvent>; |
| 48 | return { |
| 49 | type: 'event', |
| 50 | id: event.id ?? alphabetIds[index] ?? `series-${index}`, |
| 51 | name: event.name || 'unknown_event', |
| 52 | segment: event.segment ?? 'event', |
| 53 | filters: event.filters ?? [], |
| 54 | displayName: event.displayName, |
| 55 | property: event.property, |
| 56 | } as SeriesDefinition; |
| 57 | }, |
| 58 | ); |
| 59 | |
| 60 | return { |
| 61 | ...input, |
| 62 | series: mergeGlobalFilters(normalizedSeries, input.globalFilters), |
| 63 | startDate, |
| 64 | endDate, |
| 65 | }; |
| 66 | } |
| 67 |
no test coverage detected