mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-15 12:23:51 +00:00
1849b4d39f
* 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>
64 lines
1.5 KiB
TypeScript
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);
|
|
}
|
|
}
|