From 92a03de073b91e73ef0b523c9c966f289a7f4d93 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Wed, 9 Jul 2025 14:37:56 +0100 Subject: [PATCH] Add report generator --- src/reportGen.ts | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/reportGen.ts diff --git a/src/reportGen.ts b/src/reportGen.ts new file mode 100644 index 0000000..6b1b4f1 --- /dev/null +++ b/src/reportGen.ts @@ -0,0 +1,55 @@ +interface ReportSummary { + totalReports: number; + firstTimestamp: Date; + lastTimestamp: Date; + reportsPerUnit: Record; +} + +interface Report { + unitNumber: string; + reported: string; + comments: string; + utcTimestamp: string; + faults: string[]; +} + +export async function organiseReports(): Promise { + try { + const res = await fetch('https://rep.fjla.uk/fetch'); + if (!res.ok) throw new Error(`Fetch failed: ${res.statusText}`); + + const reports: Report[] = await res.json(); + + if (reports.length === 0) { + return { + totalReports: 0, + firstTimestamp: new Date(0), // Epoch + lastTimestamp: new Date(0), + reportsPerUnit: {}, + }; + } + + // Sort by string utcTimestamp (lexical sort works fine for ISO strings) + reports.sort((a, b) => a.utcTimestamp.localeCompare(b.utcTimestamp)); + + const firstTimestamp = reports[0].utcTimestamp; + const lastTimestamp = reports[reports.length - 1].utcTimestamp; + const totalReports = reports.length; + + const reportsPerUnit: Record = {}; + for (const report of reports) { + const unit = report.unitNumber; + reportsPerUnit[unit] = (reportsPerUnit[unit] || 0) + 1; + } + + return { + totalReports, + firstTimestamp: new Date(firstTimestamp), + lastTimestamp: new Date(lastTimestamp), + reportsPerUnit, + }; + } catch (err) { + console.error('Error organising reports:', err); + throw err; + } +} \ No newline at end of file