|
1 | | -/** |
2 | | - * Transforms flat Tinybird rows into component-ready JSON |
3 | | - * e.g [{date: '2023-01-01', variation: 'A', val: 10}] |
4 | | - * -> [{date: '2023-01-01', A: 10}] |
5 | | - */ |
6 | | -export function pivotAnalyticsData< |
7 | | - T extends { date: string }, |
8 | | - P extends keyof T, |
9 | | - V extends keyof T, |
10 | | ->( |
11 | | - rows: T[], |
12 | | - pivotKey: P, |
13 | | - valueKey: V |
14 | | -): Array<{ date: string } & Record<string, T[V]>> { |
15 | | - if (!rows || rows.length === 0) { |
16 | | - return []; |
| 1 | +import { eachDayOfInterval, format, subDays } from "date-fns"; |
| 2 | + |
| 3 | +type AnalyticsRow = { |
| 4 | + date?: string | null; |
| 5 | + [key: string]: unknown; |
| 6 | +}; |
| 7 | + |
| 8 | +type ChartDataNode = { |
| 9 | + date: string; |
| 10 | + [key: string]: string | number; |
| 11 | +}; |
| 12 | + |
| 13 | +export function pivotAnalyticsData( |
| 14 | + rows: AnalyticsRow[], |
| 15 | + pivotKey: string, |
| 16 | + valueKey: string, |
| 17 | + days: number |
| 18 | +): ChartDataNode[] { |
| 19 | + const endDate = new Date(); |
| 20 | + const startDate = subDays(endDate, days - 1); |
| 21 | + const dateRange = eachDayOfInterval({ start: startDate, end: endDate }); |
| 22 | + |
| 23 | + const resultObj: Record<string, ChartDataNode> = {}; |
| 24 | + |
| 25 | + for (const day of dateRange) { |
| 26 | + const dateStr = format(day, "yyyy-MM-dd"); |
| 27 | + resultObj[dateStr] = { date: dateStr }; |
17 | 28 | } |
18 | 29 |
|
19 | | - const grouped = rows.reduce( |
20 | | - (acc, row) => { |
| 30 | + if (rows && Array.isArray(rows)) { |
| 31 | + for (const row of rows) { |
21 | 32 | const date = row.date; |
22 | 33 |
|
23 | | - // The value of the pivotKey (e.g., 'Variation A') becomes a property name |
24 | | - // We cast to string to ensure it's a valid object key |
25 | | - const dynamicKey = String(row[pivotKey]); |
26 | | - const value = row[valueKey]; |
| 34 | + if (date && resultObj[date]) { |
| 35 | + const key = String(row[pivotKey]); |
| 36 | + |
| 37 | + const value = Number(row[valueKey]) || 0; |
27 | 38 |
|
28 | | - if (!acc[date]) { |
29 | | - acc[date] = { date } as { date: string } & Record<string, T[V]>; |
| 39 | + const currentVal = (resultObj[date][key] as number) || 0; |
| 40 | + resultObj[date][key] = currentVal + value; |
30 | 41 | } |
| 42 | + } |
| 43 | + } |
31 | 44 |
|
32 | | - acc[date][dynamicKey] = value; |
33 | | - return acc; |
34 | | - }, |
35 | | - {} as Record<string, { date: string } & Record<string, T[V]>> |
| 45 | + return (Object.values(resultObj) as ChartDataNode[]).sort((a, b) => |
| 46 | + a.date.localeCompare(b.date) |
36 | 47 | ); |
| 48 | +} |
| 49 | + |
| 50 | +export function fillMissingDates( |
| 51 | + rows: AnalyticsRow[], |
| 52 | + days: number |
| 53 | +): Array<{ date: string; impressions: number }> { |
| 54 | + const endDate = new Date(); |
| 55 | + const startDate = subDays(endDate, days - 1); |
| 56 | + const dateRange = eachDayOfInterval({ start: startDate, end: endDate }); |
| 57 | + |
| 58 | + const resultObj: Record<string, { date: string; impressions: number }> = {}; |
| 59 | + |
| 60 | + for (const day of dateRange) { |
| 61 | + const dateStr = format(day, "yyyy-MM-dd"); |
| 62 | + resultObj[dateStr] = { date: dateStr, impressions: 0 }; |
| 63 | + } |
| 64 | + |
| 65 | + if (rows && Array.isArray(rows)) { |
| 66 | + for (const row of rows) { |
| 67 | + const date = row.date; |
| 68 | + if (date && resultObj[date]) { |
| 69 | + resultObj[date].impressions = Number(row.impressions) || 0; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
37 | 73 |
|
38 | | - return Object.values(grouped).sort((a, b) => a.date.localeCompare(b.date)); |
| 74 | + return Object.values(resultObj).sort((a, b) => a.date.localeCompare(b.date)); |
39 | 75 | } |
0 commit comments