From d28284c8f14a027f5ccb19a85b4dcf026ea00dae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Fern=C3=A1ndez?= Date: Wed, 13 Dec 2023 14:40:09 +0100 Subject: [PATCH] TimeRangePicker: do not swap time ranges when `to` is before `from` (#78915) --- .../DateTimePickers/TimeRangePicker.tsx | 5 ++++- .../features/dashboard/services/TimeSrv.test.ts | 12 ++++++------ .../app/features/dashboard/services/TimeSrv.ts | 16 ---------------- 3 files changed, 10 insertions(+), 23 deletions(-) diff --git a/packages/grafana-ui/src/components/DateTimePickers/TimeRangePicker.tsx b/packages/grafana-ui/src/components/DateTimePickers/TimeRangePicker.tsx index f64c4f426bf..d59f7b6ec85 100644 --- a/packages/grafana-ui/src/components/DateTimePickers/TimeRangePicker.tsx +++ b/packages/grafana-ui/src/components/DateTimePickers/TimeRangePicker.tsx @@ -111,6 +111,9 @@ export function TimeRangePicker(props: TimeRangePickerProps) { const variant = isSynced ? 'active' : isOnCanvas ? 'canvas' : 'default'; + const isFromAfterTo = value?.to?.isBefore(value.from); + const timePickerIcon = isFromAfterTo ? 'exclamation-triangle' : 'clock-nine'; + const currentTimeRange = formattedRange(value, timeZone); return ( @@ -138,7 +141,7 @@ export function TimeRangePicker(props: TimeRangePickerProps) { })} aria-controls="TimePickerContent" onClick={onToolbarButtonSwitch} - icon="clock-nine" + icon={timePickerIcon} isOpen={isOpen} variant={variant} > diff --git a/public/app/features/dashboard/services/TimeSrv.test.ts b/public/app/features/dashboard/services/TimeSrv.test.ts index aa067ae98ba..0fc4ba613ac 100644 --- a/public/app/features/dashboard/services/TimeSrv.test.ts +++ b/public/app/features/dashboard/services/TimeSrv.test.ts @@ -210,26 +210,26 @@ describe('timeSrv', () => { expect(time.to.valueOf()).toEqual(1410337650000); }); - it('corrects inverted from/to dates in ms', () => { + it('does not correct inverted from/to dates in ms', () => { locationService.push('/d/id?from=1621436828909&to=1621436818909'); timeSrv = new TimeSrv(new ContextSrvStub()); timeSrv.init(_dashboard); const time = timeSrv.timeRange(); - expect(time.from.valueOf()).toEqual(1621436818909); - expect(time.to.valueOf()).toEqual(1621436828909); + expect(time.from.valueOf()).toEqual(1621436828909); + expect(time.to.valueOf()).toEqual(1621436818909); }); - it('corrects inverted from/to dates as relative times', () => { + it('does not correct inverted from/to dates as relative times', () => { locationService.push('/d/id?from=now&to=now-1h'); timeSrv = new TimeSrv(new ContextSrvStub()); timeSrv.init(_dashboard); const time = timeSrv.timeRange(); - expect(time.raw.from).toBe('now-1h'); - expect(time.raw.to).toBe('now'); + expect(time.raw.from).toBe('now'); + expect(time.raw.to).toBe('now-1h'); }); }); }); diff --git a/public/app/features/dashboard/services/TimeSrv.ts b/public/app/features/dashboard/services/TimeSrv.ts index e2e2dd40a96..ff53e2ca189 100644 --- a/public/app/features/dashboard/services/TimeSrv.ts +++ b/public/app/features/dashboard/services/TimeSrv.ts @@ -70,22 +70,6 @@ export class TimeSrv { // remember time at load so we can go back to it this.timeAtLoad = cloneDeep(this.time); - const range = rangeUtil.convertRawToRange( - this.time, - this.timeModel?.getTimezone(), - this.timeModel?.fiscalYearStartMonth - ); - - if (range.to.isBefore(range.from)) { - this.setTime( - { - from: range.raw.to, - to: range.raw.from, - }, - false - ); - } - if (this.refresh) { this.setAutoRefresh(this.refresh); }