Skip to content

Commit

Permalink
sensor-logging: Fix subtitles to show data on the beggining and end o…
Browse files Browse the repository at this point in the history
…f the video
  • Loading branch information
rafaellehmkuhl committed Feb 5, 2025
1 parent 6b85045 commit 777f5c3
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions src/libs/sensors-logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,29 +427,51 @@ class DataLogger {
const keysLogPointsInRange = availableLogsKeys.filter((key) => {
const epochString = Number(key.split('=')[1])
const logPointDate = new Date(epochString)
return logPointDate >= initialTime && logPointDate <= finalTime
// Consider logs that start a little before the initial time and end a little after the final time, so we don't
// miss data on the start and end of the file.
const aLittleBitBeforeInitialTime = new Date(initialTime.getTime() - 10000)
const aLittleBitAfterFinalTime = new Date(finalTime.getTime() + 10000)
return logPointDate >= aLittleBitBeforeInitialTime && logPointDate <= aLittleBitAfterFinalTime
})

// Generate a object with the initial and final epoch of each log point
// To know the initial epoch, check the name of the key. To know the final epoch, check the epoch of the next point.
const infoLogPointsInRange = keysLogPointsInRange.map((key, index, array) => {
const epochString = Number(key.split('=')[1])
const initialDate = new Date(epochString)

let finalDate: Date | null = null
if (index !== array.length - 1) {
const nextEpochString = Number(array[index + 1].split('=')[1])
finalDate = new Date(nextEpochString)
}

return { key, initialDate, finalDate }
})

if (keysLogPointsInRange.length === 0) {
throw new Error('No log points found in the given range.')
}

const logPointsInRange: CockpitStandardLogPoint[] = []
for (const key of keysLogPointsInRange) {
const log = (await this.cockpitTemporaryLogsDB.getItem(key)) as CockpitStandardLogPoint
for (const info of infoLogPointsInRange) {
const log = (await this.cockpitTemporaryLogsDB.getItem(info.key)) as CockpitStandardLogPoint

// Only consider real log points(objects with an epoch and data property, and non-empty data)
if (log.epoch === undefined || log.data === undefined || Object.keys(log.data).length === 0) continue

// Exclude those log points that end before the initial time or start after the final time
if ((info.finalDate && info.finalDate < initialTime) || info.initialDate > finalTime) continue

logPointsInRange.push(log)
}

// Sort the log points by epoch, generate a final log file and put in in the local database
const sortedLogPoints = logPointsInRange.sort((a, b) => a.epoch - b.epoch)
const finalLog = sortedLogPoints.map((logPoint) => ({
...logPoint,
...{ seconds: differenceInSeconds(new Date(logPoint.epoch), initialTime) },
}))
const finalLog = sortedLogPoints.map((logPoint) => {
const seconds = Math.max(0, differenceInSeconds(new Date(logPoint.epoch), initialTime))
return { ...logPoint, ...{ seconds } }
})

await this.cockpitLogsDB.setItem(fileName, finalLog)

Expand Down Expand Up @@ -543,7 +565,7 @@ Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text`
const roundedMillisThisPoint = differenceInSeconds(new Date(logPoint.epoch), new Date(videoStartEpoch)) * 1000
const millisThisPoint = differenceInMilliseconds(new Date(logPoint.epoch), new Date(videoStartEpoch))
const remainingMillisThisPoint = millisThisPoint - roundedMillisThisPoint
const remainingCentisThisPoint = Math.floor(remainingMillisThisPoint / 10).toString().padStart(2, '0')
const remainingCentisThisPoint = Math.max(0, Math.floor(remainingMillisThisPoint / 10)).toString().padStart(2, '0')

const roundedMillisNextPoint = differenceInSeconds(new Date(log[index + 1].epoch), new Date(videoStartEpoch)) * 1000
const millisNextPoint = differenceInMilliseconds(new Date(log[index + 1].epoch), new Date(videoStartEpoch))
Expand Down

0 comments on commit 777f5c3

Please sign in to comment.