Files
ontime/apps/server/src/api-data/report/report.service.ts
T
Carlos Valente 1849b4d39f Deps migration (#1988)
* chore: migrate eslint to oxlint

* chore: migrate prettier to oxfmt

* chore: migrate typescript

* chore: toThrow should have a expected value

* chore: cast test value as Day

* chore: small title fix

* chore: mocks should be hoisted

* chore: incorrect async useage

* chore: test should be inside description

* chore: test sohuld include an expeced

* chore: oxfmt

---------

Co-authored-by: alex-Arc <omnivox@LAPTOP-RC5SNBVV.localdomain>
2026-03-08 16:22:12 +01:00

64 lines
1.5 KiB
TypeScript

import { OntimeEventReport, OntimeReport, RefetchKey, TimerLifeCycle } from 'ontime-types';
import { DeepReadonly } from 'ts-essentials';
import { sendRefetch } from '../../adapters/WebsocketAdapter.js';
import { RuntimeState } from '../../stores/runtimeState.js';
const report = new Map<string, OntimeEventReport>();
let formattedReport: OntimeReport | null = null;
/**
* generates a full report
* @returns full report
*/
export function generate(): OntimeReport {
if (formattedReport === null) {
formattedReport = Object.fromEntries(report);
}
return formattedReport;
}
/**
* clear report
* @param id optional id of a event report to clear
*/
export function clear(id?: string) {
formattedReport = null;
if (id) {
report.delete(id);
} else {
report.clear();
}
}
/**
* trigger report entry
* @param cycle
* @param state
* @returns
*/
export function triggerReportEntry(
cycle: TimerLifeCycle.onStart | TimerLifeCycle.onStop,
state: DeepReadonly<RuntimeState>,
) {
if (!state.eventNow?.id) {
return;
}
const eventId = state.eventNow.id;
if (cycle === TimerLifeCycle.onStart) {
report.set(eventId, { startedAt: state.timer.startedAt, endedAt: null });
formattedReport = null;
return;
}
if (cycle === TimerLifeCycle.onStop) {
const startedAt = report.get(eventId)?.startedAt ?? null;
report.set(eventId, { startedAt, endedAt: state.clock });
formattedReport = null;
sendRefetch(RefetchKey.Report);
}
}