feat: resolve times on excel import, refs #508

This commit is contained in:
cv
2023-09-29 20:12:53 +02:00
parent a0d4f40bec
commit cf7f6bdbf7
6 changed files with 137 additions and 42 deletions
+2 -1
View File
@@ -1,12 +1,13 @@
// runtime utils
export { getFirst, getFirstEvent, getLastEvent, getNext, getPrevious } from './src/rundown-utils/rundownUtils.js';
export { validatePlayback } from './src/validate-action/validatePlayback.js';
export { validateTimes } from './src/validate-events/validateEvent.js';
export { calculateDuration } from './src/validate-events/validateEvent.js';
// rundown utils
export { sanitiseCue } from './src/cue-utils/cueUtils.js';
export { getCueCandidate } from './src/cue-utils/cueUtils.js';
export { generateId } from './src/generate-id/generateId.js';
export { calculateDuration } from './src/rundown-utils/rundownUtils.js';
export { swapOntimeEvents } from './src/rundown-utils/rundownUtils.js';
// format utils
@@ -1,7 +1,5 @@
import { isOntimeEvent, OntimeEvent, OntimeRundown, OntimeRundownEntry } from 'ontime-types';
import { dayInMs } from '../timeConstants.js';
/**
* Gets first event in rundown, if it exists
* @param {OntimeRundownEntry[]} rundown
@@ -113,20 +111,6 @@ export function getPreviousEvent(rundown: OntimeRundownEntry[], currentId: strin
return null;
}
/**
* @description calculates event duration considering midnight
* @param {number} timeStart
* @param {number} timeEnd
* @returns {number}
*/
export const calculateDuration = (timeStart: number, timeEnd: number): number => {
// Durations must be positive
if (timeEnd < timeStart) {
return timeEnd + dayInMs - timeStart;
}
return timeEnd - timeStart;
};
/**
* @description swaps two OntimeEvents in the rundown
* @param {OntimeRundown} rundown
@@ -1,7 +1,8 @@
import { EndAction, TimerType } from 'ontime-types';
import { expect } from 'vitest';
import { validateEndAction, validateTimerType } from './validateEvent';
import { dayInMs } from '../timeConstants.js';
import { validateEndAction, validateTimerType, validateTimes } from './validateEvent.js';
describe('validateEndAction()', () => {
it('recognises a string representation of an action', () => {
@@ -28,3 +29,61 @@ describe('validateTimerType()', () => {
expect(invalidType).toBe(TimerType.CountDown);
});
});
describe('validateTimes()', () => {
it('passes through a well defined time list', () => {
const { timeStart, timeEnd, duration } = validateTimes(5, 10, 5);
expect(timeStart).toBe(5);
expect(timeEnd).toBe(10);
expect(duration).toBe(5);
});
it('handles cases when no times are given', () => {
const { timeStart, timeEnd, duration } = validateTimes(null, undefined, null);
expect(timeStart).toBe(0);
expect(timeEnd).toBe(0);
expect(duration).toBe(0);
});
it('calculates duration', () => {
const { timeStart, timeEnd, duration } = validateTimes(5, 10);
expect(timeStart).toBe(5);
expect(timeEnd).toBe(10);
expect(duration).toBe(5);
});
it('calculates end time', () => {
const { timeStart, timeEnd, duration } = validateTimes(5, undefined, 10);
expect(timeStart).toBe(5);
expect(timeEnd).toBe(15);
expect(duration).toBe(10);
});
it('handles events that finish the day after', () => {
const { timeStart, timeEnd, duration } = validateTimes(100, 10);
expect(timeStart).toBe(100);
expect(timeEnd).toBe(10);
expect(duration).toBe(dayInMs - 90);
});
it('corrects time in case of conflicts', () => {
const { timeStart, timeEnd, duration } = validateTimes(5, 15, 15);
expect(timeStart).toBe(5);
expect(timeEnd).toBe(15);
expect(duration).toBe(10);
});
it('calculates start time', () => {
const { timeStart, timeEnd, duration } = validateTimes(undefined, 15, 10);
expect(timeStart).toBe(5);
expect(timeEnd).toBe(15);
expect(duration).toBe(10);
});
it('calculates start and end time', () => {
const { timeStart, timeEnd, duration } = validateTimes(undefined, undefined, 10);
expect(timeStart).toBe(0);
expect(timeEnd).toBe(10);
expect(duration).toBe(10);
});
});
@@ -1,5 +1,7 @@
import { EndAction, TimerType } from 'ontime-types';
import { dayInMs } from '../timeConstants.js';
export function validateEndAction(maybeAction: unknown, fallback = EndAction.None) {
if (typeof maybeAction !== 'string') {
return fallback;
@@ -23,3 +25,50 @@ export function validateTimerType(maybeTimerType: unknown, fallback = TimerType.
}
return fallback;
}
/**
* @description calculates event duration considering midnight
* @param {number} timeStart
* @param {number} timeEnd
* @returns {number}
*/
export const calculateDuration = (timeStart: number, timeEnd: number): number => {
// Durations must be positive
if (timeEnd < timeStart) {
return timeEnd + dayInMs - timeStart;
}
return timeEnd - timeStart;
};
export function validateTimes(_start?: number | null, _end?: number | null, _duration?: number | null) {
const timeStart = _start ?? 0;
const timeEnd = _end ?? 0;
const duration = _duration ?? 0;
if (_start != null && _end != null) {
// Case 1. if we have start and end, duration must be derived
return { timeStart, duration: calculateDuration(timeStart, timeEnd), timeEnd };
}
if (_start == null && _end == null) {
if (_duration == null) {
// Case 2. no valid times were given
return { timeStart, duration, timeEnd };
}
// Case 3. we have a duration and infer the rest
return { timeStart, duration: _duration, timeEnd: _duration };
}
if (_start != null) {
// Case 5. with only start, we can calculate the rest
return { timeStart, duration, timeEnd: timeStart + duration };
}
if (_end != null) {
// Case 6. with only end, we can calculate the rest
return { timeStart: timeEnd - duration, duration, timeEnd };
}
// we should have covered all cases
return { timeStart, duration, timeEnd };
}