forked from
chadtmiller.com/slices-teal-relay
Teal.fm frontend powered by slices.network
tealfm-slices.wisp.place
tealfm
slices
1export interface DataPoint {
2 date: string;
3 count: number;
4}
5
6export function generateChartData(
7 plays: readonly { readonly playedTime?: string | null; readonly [key: string]: any }[],
8 days = 90
9): DataPoint[] {
10 const counts = new Map<string, number>();
11 const now = new Date();
12
13 // Initialize last N days with 0 counts
14 for (let i = days - 1; i >= 0; i--) {
15 const date = new Date(now);
16 date.setDate(date.getDate() - i);
17 date.setHours(0, 0, 0, 0);
18 const dateStr = date.toISOString().split("T")[0];
19 counts.set(dateStr, 0);
20 }
21
22 // Count plays per day
23 plays.forEach((play) => {
24 if (play?.playedTime) {
25 const date = new Date(play.playedTime);
26 date.setHours(0, 0, 0, 0);
27 const dateStr = date.toISOString().split("T")[0];
28 if (counts.has(dateStr)) {
29 counts.set(dateStr, (counts.get(dateStr) || 0) + 1);
30 }
31 }
32 });
33
34 return Array.from(counts.entries())
35 .map(([date, count]) => ({ date, count }))
36 .sort((a, b) => a.date.localeCompare(b.date));
37}