import { WeeklyStats, DayData, Locations, DailyStats, LocationKey } from "../../types"; import { getStatsMock } from "./mock"; import getStorage from "./storage"; import { formatDate } from "./utils"; const storage = getStorage(); /** * Vypočte a vrátí statistiky jednotlivých možností pro předaný rozsah dat. * * @param startDate počáteční datum * @param endDate koncové datum * @returns statistiky pro zadaný rozsah dat */ export async function getStats(startDate: string, endDate: string): Promise { if (process.env.MOCK_DATA === 'true') { return getStatsMock(); } const start = new Date(startDate); const end = new Date(endDate); // Dočasná validace, aby to někdo ručně neshodil const daysDiff = ((end as any) - (start as any)) / (1000 * 60 * 60 * 24); if (daysDiff > 4) { throw Error('Neplatný rozsah'); } const result = []; for (const date = start; date <= end; date.setDate(date.getDate() + 1)) { const locationsStats: DailyStats = { // TODO vytáhnout do utils funkce date: `${String(date.getDate()).padStart(2, "0")}.${String(date.getMonth() + 1).padStart(2, "0")}.`, locations: {} } const data: DayData = await storage.getData(formatDate(date)); if (data?.choices) { Object.keys(data.choices).forEach(locationKey => { locationsStats.locations[locationKey as LocationKey] = Object.keys(data.choices[locationKey as LocationKey]!).length; }) } result.push(locationsStats); } return result as WeeklyStats; }