Add report generator
This commit is contained in:
parent
2a6d6e0eb5
commit
92a03de073
55
src/reportGen.ts
Normal file
55
src/reportGen.ts
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
interface ReportSummary {
|
||||||
|
totalReports: number;
|
||||||
|
firstTimestamp: Date;
|
||||||
|
lastTimestamp: Date;
|
||||||
|
reportsPerUnit: Record<string, number>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Report {
|
||||||
|
unitNumber: string;
|
||||||
|
reported: string;
|
||||||
|
comments: string;
|
||||||
|
utcTimestamp: string;
|
||||||
|
faults: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function organiseReports(): Promise<ReportSummary> {
|
||||||
|
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<string, number> = {};
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user