diff --git a/src/libs/logging.ts b/src/libs/logging.ts index 29e93e50d..2a2d23c7d 100644 --- a/src/libs/logging.ts +++ b/src/libs/logging.ts @@ -6,25 +6,35 @@ import { useMainVehicleStore } from '@/stores/mainVehicle' import { degrees } from './utils' +/** + * Format for a standalone cockpit log + */ +export interface CockpitStandardLogPoint { + /** + * Universal Linux epoch time (milliseconds since January 1st, 1970, UTC) + */ + epoch: number + /** + * Seconds passed since the beggining of the logging + */ + seconds: number + /** + * The actual vehicle data + */ + data: Record +} + +/** + * Format for a standalone cockpit log + */ +export type CockpitStandardLog = CockpitStandardLogPoint[] + /** * Manager logging vehicle data and others */ class DataLogger { currentLoggerInterval: ReturnType | undefined = undefined - currentCockpitLog: { - /** - * Universal Linux epoch time (milliseconds since January 1st, 1970, UTC) - */ - epoch: number - /** - * Seconds passed since the beggining of the logging - */ - seconds: number - /** - * The actual vehicle data - */ - data: Record - }[] = [] + currentCockpitLog: CockpitStandardLog = [] /** * Start an intervaled logging @@ -91,6 +101,19 @@ class DataLogger { logging(): boolean { return this.currentLoggerInterval !== undefined } + + /** + * Get desired part of a log based on timestamp + * @param {CockpitStandardLog} completeLog The log from which the slice should be taken from + * @param {Date} initialTime The timestamp from which the log should be started from + * @param {Date} finalTime The timestamp in which the log should be terminated + * @returns {CockpitStandardLog} The actual log + */ + getSlice(completeLog: CockpitStandardLog, initialTime: Date, finalTime: Date): CockpitStandardLog { + return completeLog + .filter((logPoint) => logPoint.epoch > initialTime.getTime() && logPoint.epoch < finalTime.getTime()) + .map((logPoint) => ({ ...logPoint, ...{ seconds: differenceInSeconds(new Date(logPoint.epoch), initialTime) } })) + } } export const datalogger = new DataLogger()