mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-11 18:33:53 +00:00
fix: issue where a countToEnd would lead to incorrect expected times (#2149)
* fix: issue where a count-to-end would lead to incorrect expected times * fix: include add time in overtime when countToEnd * fix: ui and server use same calculation for expected end
This commit is contained in:
committed by
GitHub
parent
a8c611911d
commit
2a890cf2b3
@@ -1126,7 +1126,7 @@ describe('getRuntimeOffset()', () => {
|
||||
} as RuntimeState;
|
||||
|
||||
const { absolute } = getRuntimeOffset(state);
|
||||
expect(absolute).toBe(400000); // <--- offset is always the overtime
|
||||
expect(absolute).toBe(400000 - 200000); // <--- offset is always the overtime + added time
|
||||
});
|
||||
|
||||
it('handles time-to-end started after the end time', () => {
|
||||
|
||||
@@ -180,13 +180,13 @@ export function getRuntimeOffset(state: RuntimeState): { absolute: number; relat
|
||||
const pausedTime = state._timer.pausedAt === null ? 0 : clock - state._timer.pausedAt;
|
||||
|
||||
// absolute offset is difference between schedule and playback time
|
||||
const absolute = eventStartOffset + overtime + pausedTime + addedTime;
|
||||
// in case of count to end, the absolute offset is overtime and added time
|
||||
const absolute = countToEnd ? overtime + addedTime : eventStartOffset + overtime + pausedTime + addedTime;
|
||||
|
||||
// the relative offset is the same as the absolute but adjusted relative to the actual start time
|
||||
const relative = absolute + plannedStart - actualStart - _startDayOffset * dayInMs;
|
||||
|
||||
// in case of count to end, the absolute offset is just the overtime
|
||||
return countToEnd ? { absolute: overtime, relative } : { absolute, relative };
|
||||
return { absolute, relative };
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1036,4 +1036,167 @@ describe('loadGroupFlagAndEnd()', () => {
|
||||
eventNow: rundown.entries[0],
|
||||
});
|
||||
});
|
||||
|
||||
test('countToEnd entries decouple the link chain for subsequent events', () => {
|
||||
// Event 0 (loaded): no countToEnd, linkStart=true
|
||||
// Event 1: countToEnd=true → breaks the link chain
|
||||
// Event 2: linkStart=true → unlinked (chain broken by event 1)
|
||||
// Event 3: flag event → also unlinked
|
||||
const rundown = makeRundown({
|
||||
entries: {
|
||||
group: makeOntimeGroup({ id: 'group', entries: ['0', '1', '2'] }),
|
||||
0: makeOntimeEvent({
|
||||
id: '0',
|
||||
parent: 'group',
|
||||
timeStart: 0,
|
||||
duration: 3600000,
|
||||
countToEnd: false,
|
||||
linkStart: true,
|
||||
gap: 0,
|
||||
} as any),
|
||||
1: makeOntimeEvent({
|
||||
id: '1',
|
||||
parent: 'group',
|
||||
timeStart: 3600000,
|
||||
duration: 3600000,
|
||||
countToEnd: true,
|
||||
linkStart: true,
|
||||
gap: 0,
|
||||
} as any),
|
||||
2: makeOntimeEvent({
|
||||
id: '2',
|
||||
parent: 'group',
|
||||
timeStart: 7200000,
|
||||
duration: 3600000,
|
||||
linkStart: true,
|
||||
gap: 0,
|
||||
} as any),
|
||||
3: makeOntimeEvent({
|
||||
id: '3',
|
||||
parent: null,
|
||||
timeStart: 10800000,
|
||||
duration: 3600000,
|
||||
linkStart: true,
|
||||
gap: 0,
|
||||
} as any),
|
||||
},
|
||||
order: ['group', '0', '1', '2', '3'],
|
||||
});
|
||||
|
||||
const state = {
|
||||
groupNow: null,
|
||||
eventNow: rundown.entries[0],
|
||||
rundown: { actualGroupStart: null },
|
||||
} as RuntimeState;
|
||||
|
||||
const metadata = { playableEventOrder: ['0', '1', '2', '3'], flags: ['3'] } as RundownMetadata;
|
||||
|
||||
loadGroupFlagAndEnd(rundown, metadata, 0, state);
|
||||
|
||||
// _group is the last event in the group (event 2)
|
||||
// isLinkedToLoaded is false because event 1 (between loaded and group end) has countToEnd=true
|
||||
// accumulatedGap includes event 1's duration carried forward to event 2
|
||||
// (the countToEnd duration is applied to the next event, not the countToEnd event itself)
|
||||
expect(state._group).toMatchObject({
|
||||
event: rundown.entries[2],
|
||||
isLinkedToLoaded: false,
|
||||
accumulatedGap: 3600000,
|
||||
});
|
||||
|
||||
// _flag (event 3): isLinkedToLoaded is false because chain was broken by event 1
|
||||
// accumulatedGap is still 3600000 because event 2 is not countToEnd,
|
||||
// so previousWasCountToEnd is null and no further duration is carried forward
|
||||
expect(state._flag).toMatchObject({
|
||||
event: rundown.entries[3],
|
||||
isLinkedToLoaded: false,
|
||||
accumulatedGap: 3600000,
|
||||
});
|
||||
|
||||
// _end (event 3): also unlinked because the chain was broken at event 1
|
||||
expect(state._end).toMatchObject({
|
||||
event: rundown.entries[3],
|
||||
isLinkedToLoaded: false,
|
||||
});
|
||||
});
|
||||
|
||||
test('countToEnd in the middle of the chain breaks links for downstream events', () => {
|
||||
// Event 0 (loaded): no countToEnd, linkStart=true
|
||||
// Event 1: linkStart=true → linked to loaded
|
||||
// Event 2: countToEnd=true → breaks the chain (also last in group)
|
||||
// Event 3: linkStart=true → unlinked (chain broken by event 2)
|
||||
const rundown = makeRundown({
|
||||
entries: {
|
||||
group: makeOntimeGroup({ id: 'group', entries: ['0', '1', '2'] }),
|
||||
0: makeOntimeEvent({
|
||||
id: '0',
|
||||
parent: 'group',
|
||||
timeStart: 0,
|
||||
duration: 3600000,
|
||||
countToEnd: false,
|
||||
linkStart: true,
|
||||
gap: 0,
|
||||
} as any),
|
||||
1: makeOntimeEvent({
|
||||
id: '1',
|
||||
parent: 'group',
|
||||
timeStart: 3600000,
|
||||
duration: 3600000,
|
||||
linkStart: true,
|
||||
gap: 0,
|
||||
} as any),
|
||||
2: makeOntimeEvent({
|
||||
id: '2',
|
||||
parent: 'group',
|
||||
timeStart: 7200000,
|
||||
duration: 3600000,
|
||||
countToEnd: true,
|
||||
linkStart: true,
|
||||
gap: 0,
|
||||
} as any),
|
||||
3: makeOntimeEvent({
|
||||
id: '3',
|
||||
parent: null,
|
||||
timeStart: 10800000,
|
||||
duration: 3600000,
|
||||
linkStart: true,
|
||||
gap: 0,
|
||||
} as any),
|
||||
},
|
||||
order: ['group', '0', '1', '2', '3'],
|
||||
});
|
||||
|
||||
const state = {
|
||||
groupNow: null,
|
||||
eventNow: rundown.entries[0],
|
||||
rundown: { actualGroupStart: null },
|
||||
} as RuntimeState;
|
||||
|
||||
const metadata = { playableEventOrder: ['0', '1', '2', '3'], flags: ['3'] } as RundownMetadata;
|
||||
|
||||
loadGroupFlagAndEnd(rundown, metadata, 0, state);
|
||||
|
||||
// _group is the last event in the group (event 2)
|
||||
// isLinkedToLoaded is true because no preceding event had countToEnd
|
||||
// accumulatedGap is 0 because no preceding event was countToEnd
|
||||
// (the countToEnd duration is carried forward to the next event, not added to the countToEnd event itself)
|
||||
expect(state._group).toMatchObject({
|
||||
event: rundown.entries[2],
|
||||
isLinkedToLoaded: true,
|
||||
accumulatedGap: 0,
|
||||
});
|
||||
|
||||
// _flag (event 3): isLinkedToLoaded is false because chain was broken by count-to-end event 2
|
||||
// accumulatedGap includes event 2's duration carried forward
|
||||
expect(state._flag).toMatchObject({
|
||||
event: rundown.entries[3],
|
||||
isLinkedToLoaded: false,
|
||||
accumulatedGap: 3600000,
|
||||
});
|
||||
|
||||
// _end (event 3): unlinked because event 2 has countToEnd=true
|
||||
expect(state._end).toMatchObject({
|
||||
event: rundown.entries[3],
|
||||
isLinkedToLoaded: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -23,6 +23,7 @@ import {
|
||||
calculateDuration,
|
||||
checkIsNow,
|
||||
dayInMs,
|
||||
getExpectedEnd,
|
||||
getExpectedStart,
|
||||
getLastEventNormal,
|
||||
isPlaybackActive,
|
||||
@@ -831,7 +832,6 @@ function getExpectedTimes(state = runtimeState) {
|
||||
state.offset.expectedRundownEnd = null;
|
||||
state.offset.expectedGroupEnd = null;
|
||||
state.offset.expectedFlagStart = null;
|
||||
state.offset.expectedRundownEnd = null;
|
||||
|
||||
const { offset } = state;
|
||||
const { plannedStart, actualStart } = state.rundown;
|
||||
@@ -852,7 +852,7 @@ function getExpectedTimes(state = runtimeState) {
|
||||
plannedStart,
|
||||
actualStart,
|
||||
});
|
||||
state.offset.expectedGroupEnd = lastEventExpectedStart + lastEvent.duration;
|
||||
state.offset.expectedGroupEnd = getExpectedEnd(lastEvent, lastEventExpectedStart, state.rundown.currentDay!);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -884,7 +884,7 @@ function getExpectedTimes(state = runtimeState) {
|
||||
plannedStart,
|
||||
actualStart,
|
||||
});
|
||||
state.offset.expectedRundownEnd = expectedStart + event.duration;
|
||||
state.offset.expectedRundownEnd = getExpectedEnd(event, expectedStart, state.rundown.currentDay!);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -927,6 +927,7 @@ export function loadGroupFlagAndEnd(
|
||||
|
||||
let accumulatedGap = 0;
|
||||
let isLinkedToLoaded = true;
|
||||
let previousWasCountToEnd: Maybe<number> = null;
|
||||
|
||||
for (let idx = currentIndex; idx < playableEventOrder.length; idx++) {
|
||||
const entry = entries[playableEventOrder[idx]];
|
||||
@@ -934,8 +935,23 @@ export function loadGroupFlagAndEnd(
|
||||
if (isOntimeEvent(entry)) {
|
||||
if (idx !== currentIndex) {
|
||||
// we only accumulate data after the loaded event
|
||||
accumulatedGap += entry.gap;
|
||||
isLinkedToLoaded = isLinkedToLoaded && entry.linkStart;
|
||||
|
||||
if (previousWasCountToEnd !== null) {
|
||||
/** previous event was countToEnd: add its duration as a positive gap (it "gives back" time downstream)
|
||||
* and break the link to the loaded event since countToEnd events reset the schedule
|
||||
*/
|
||||
accumulatedGap += entry.gap + previousWasCountToEnd;
|
||||
isLinkedToLoaded = false;
|
||||
} else {
|
||||
accumulatedGap += entry.gap;
|
||||
isLinkedToLoaded = isLinkedToLoaded && entry.linkStart;
|
||||
}
|
||||
|
||||
if (entry.countToEnd) {
|
||||
previousWasCountToEnd = entry.duration;
|
||||
} else {
|
||||
previousWasCountToEnd = null;
|
||||
}
|
||||
|
||||
// and the loaded event is not allowed to be the next flag
|
||||
if (!foundFlag && metadata.flags.includes(entry.id)) {
|
||||
|
||||
Reference in New Issue
Block a user