QueryOptions: Handle invalid time shift values (#101670)

This commit is contained in:
Ivan Ortega Alba
2025-03-06 09:36:29 +01:00
committed by GitHub
parent d8da2061fc
commit 0e25c6857d
2 changed files with 30 additions and 0 deletions
@@ -111,6 +111,31 @@ describe('PanelTimeRange', () => {
expect(panelTime.state.value.from.format('Z')).toBe('+00:00'); // UTC
expect(panelTime.state.value.to.format('Z')).toBe('+00:00'); // UTC
});
it('should handle invalid time reference in timeShift', () => {
const panelTime = new PanelTimeRange({ timeShift: 'now-1d' });
buildAndActivateSceneFor(panelTime);
expect(panelTime.state.timeInfo).toBe('invalid timeshift');
// Should not be affected by invalid timeShift
expect(panelTime.state.from).toBe('now-6h');
expect(panelTime.state.to).toBe('now');
});
it('should handle invalid time reference in timeShift combined with timeFrom', () => {
const panelTime = new PanelTimeRange({
timeFrom: 'now-2h',
timeShift: 'now-1d',
});
buildAndActivateSceneFor(panelTime);
expect(panelTime.state.timeInfo).toBe('invalid timeshift');
// Should not be affected by invalid timeShift
expect(panelTime.state.from).toBe('now-2h');
expect(panelTime.state.to).toBe('now');
});
});
function buildAndActivateSceneFor(panelTime: PanelTimeRange) {
@@ -105,6 +105,11 @@ export class PanelTimeRange extends SceneTimeRangeTransformerBase<PanelTimeRange
const from = dateMath.parseDateMath(timeShift, newTimeData.timeRange.from, false)!;
const to = dateMath.parseDateMath(timeShift, newTimeData.timeRange.to, true)!;
if (!from || !to) {
newTimeData.timeInfo = 'invalid timeshift';
return newTimeData;
}
newTimeData.timeRange = { from, to, raw: { from, to } };
}