mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-19 22:24:11 +00:00
refactor(rundown): simplify gap and overlap accumulation
The gap/overlap detection is already a merge-intervals sweep: `latestEvent` is the running max end and `getTimeFrom` is `start - maxEnd`. Making that explicit collapses the branching around it. - getTimeFrom: the three branches all returned the same subtraction - totalDuration: the gap === 0 / gap > 0 / gap < 0 cases are all Math.max(duration + gap, 0), since duration is never negative Same behaviour, no signature changes. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016XSraCPafWbAg35gAFrtgH
This commit is contained in:
@@ -152,13 +152,10 @@ export function computeScopedRundown(
|
|||||||
|
|
||||||
const timeFromPrevious: number = getTimeFrom(currentEntry, lastEntry);
|
const timeFromPrevious: number = getTimeFrom(currentEntry, lastEntry);
|
||||||
|
|
||||||
if (timeFromPrevious === 0) {
|
// each event only contributes the time it pushes past the latest event so far
|
||||||
totalDuration += currentEntry.duration;
|
// NOTE: timeFromPrevious is negative on overlap, so adding it removes the overlapping part
|
||||||
} else if (timeFromPrevious > 0) {
|
totalDuration += Math.max(currentEntry.duration + timeFromPrevious, 0);
|
||||||
totalDuration += timeFromPrevious + currentEntry.duration;
|
|
||||||
} else if (timeFromPrevious < 0) {
|
|
||||||
totalDuration += Math.max(currentEntry.duration + timeFromPrevious, 0);
|
|
||||||
}
|
|
||||||
if (isNewLatest(currentEntry, lastEntry)) {
|
if (isNewLatest(currentEntry, lastEntry)) {
|
||||||
lastEntry = currentEntry;
|
lastEntry = currentEntry;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -309,18 +309,15 @@ function processEntry<T extends OntimeEntry>(
|
|||||||
|
|
||||||
entry.gap = getTimeFrom(entry, rundownMetadata.latestEvent);
|
entry.gap = getTimeFrom(entry, rundownMetadata.latestEvent);
|
||||||
|
|
||||||
if (entry.gap === 0) {
|
/**
|
||||||
// event starts on previous finish, we add its duration
|
* The rundown duration is the union of all event intervals, so each event
|
||||||
rundownMetadata.totalDuration += entry.duration;
|
* only contributes the time it pushes past the latest event so far.
|
||||||
} else if (entry.gap > 0) {
|
* - gap >= 0: the event contributes its gap plus its full duration
|
||||||
// event has a gap, we add the gap and the duration
|
* - gap < 0: the event overlaps, only the part after the latest end counts
|
||||||
rundownMetadata.totalDuration += entry.gap + entry.duration;
|
* (clamped at 0 for events which are fully contained in a previous one)
|
||||||
} else {
|
* NOTE: gap is negative on overlap, so adding it removes the overlapping part
|
||||||
// there is an overlap, we remove the overlap from the duration
|
*/
|
||||||
// ensuring that the sum is not negative (ie: fully overlapped events)
|
rundownMetadata.totalDuration += Math.max(entry.duration + entry.gap, 0);
|
||||||
// NOTE: we add the gap since it is a negative number
|
|
||||||
rundownMetadata.totalDuration += Math.max(entry.duration + entry.gap, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// remove eventual gaps from the accumulated delay
|
// remove eventual gaps from the accumulated delay
|
||||||
// we only affect positive delays (time forwards)
|
// we only affect positive delays (time forwards)
|
||||||
|
|||||||
@@ -17,17 +17,11 @@ export function getTimeFrom(
|
|||||||
const normalisedCurrentStart = current.timeStart + current.dayOffset * dayInMs;
|
const normalisedCurrentStart = current.timeStart + current.dayOffset * dayInMs;
|
||||||
const normalisedPreviousEnd = previous.timeStart + previous.duration + previous.dayOffset * dayInMs;
|
const normalisedPreviousEnd = previous.timeStart + previous.duration + previous.dayOffset * dayInMs;
|
||||||
|
|
||||||
// event is linked to previous
|
/**
|
||||||
if (normalisedCurrentStart === normalisedPreviousEnd) {
|
* The distance between the current start and the previous end
|
||||||
return 0;
|
* - positive: there is a gap between the events
|
||||||
}
|
* - zero: the current event starts on the previous end
|
||||||
|
* - negative: the events overlap
|
||||||
// event has a gap from previous
|
*/
|
||||||
if (normalisedCurrentStart > normalisedPreviousEnd) {
|
|
||||||
// time from previous is difference between start and previous end
|
|
||||||
return normalisedCurrentStart - normalisedPreviousEnd;
|
|
||||||
}
|
|
||||||
|
|
||||||
// event overlaps with previous
|
|
||||||
return normalisedCurrentStart - normalisedPreviousEnd;
|
return normalisedCurrentStart - normalisedPreviousEnd;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user