Highly ambitious ATProtocol AppView service and sdks
at main 40 lines 1.0 kB view raw
1export interface DataPoint { 2 date: string; 3 count: number; 4} 5 6export function generateChartData( 7 plays: readonly { 8 readonly playedTime?: string | null; 9 readonly [key: string]: unknown; 10 }[], 11 days = 90, 12): DataPoint[] { 13 const counts = new Map<string, number>(); 14 const now = new Date(); 15 16 // Initialize last N days with 0 counts 17 for (let i = days - 1; i >= 0; i--) { 18 const date = new Date(now); 19 date.setDate(date.getDate() - i); 20 date.setHours(0, 0, 0, 0); 21 const dateStr = date.toISOString().split("T")[0]; 22 counts.set(dateStr, 0); 23 } 24 25 // Count plays per day 26 plays.forEach((play) => { 27 if (play?.playedTime) { 28 const date = new Date(play.playedTime); 29 date.setHours(0, 0, 0, 0); 30 const dateStr = date.toISOString().split("T")[0]; 31 if (counts.has(dateStr)) { 32 counts.set(dateStr, (counts.get(dateStr) || 0) + 1); 33 } 34 } 35 }); 36 37 return Array.from(counts.entries()) 38 .map(([date, count]) => ({ date, count })) 39 .sort((a, b) => a.date.localeCompare(b.date)); 40}