From 291ca540104d1350d1b6894e32dfd1e7d68741ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Jamr=C3=B3z?= Date: Tue, 29 Aug 2023 15:05:18 +0200 Subject: [PATCH] Explore: Fix TimeSrv initializations (#73903) Ensure TimeSrv is re-initializes --- public/app/features/explore/state/time.test.ts | 9 +++++++++ public/app/features/explore/state/time.ts | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/public/app/features/explore/state/time.test.ts b/public/app/features/explore/state/time.test.ts index 55cf86ed393..09430fd0143 100644 --- a/public/app/features/explore/state/time.test.ts +++ b/public/app/features/explore/state/time.test.ts @@ -7,6 +7,13 @@ import { ExploreItemState } from 'app/types'; import { createDefaultInitialState } from './helpers'; import { changeRangeAction, timeReducer, updateTime } from './time'; +const mockTimeSrv = { + init: jest.fn(), +}; +jest.mock('app/features/dashboard/services/TimeSrv', () => ({ + ...jest.requireActual('app/features/dashboard/services/TimeSrv'), + getTimeSrv: () => mockTimeSrv, +})); const mockTemplateSrv = { updateTimeRange: jest.fn(), }; @@ -22,6 +29,8 @@ describe('Explore item reducer', () => { const { dispatch } = configureStore(state); dispatch(updateTime({ exploreId: 'left' })); expect(mockTemplateSrv.updateTimeRange).toBeCalledWith(state.explore.panes.left.range); + expect(mockTimeSrv.init).toBeCalled(); + expect(mockTemplateSrv.updateTimeRange).toBeCalledWith(state.explore.panes.left.range); }); }); diff --git a/public/app/features/explore/state/time.ts b/public/app/features/explore/state/time.ts index 85ebd464551..ddffb459ac8 100644 --- a/public/app/features/explore/state/time.ts +++ b/public/app/features/explore/state/time.ts @@ -5,6 +5,7 @@ import { getTemplateSrv } from '@grafana/runtime'; import { RefreshPicker } from '@grafana/ui'; import { getTimeRange, refreshIntervalToSortOrder, stopQueryState } from 'app/core/utils/explore'; import { getShiftedTimeRange, getZoomedTimeRange } from 'app/core/utils/timePicker'; +import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv'; import { sortLogsResult } from 'app/features/logs/utils'; import { getFiscalYearStartMonth, getTimeZone } from 'app/features/profile/state/selectors'; import { ExploreItemState, ThunkDispatch, ThunkResult } from 'app/types'; @@ -21,6 +22,7 @@ export interface ChangeRangePayload { range: TimeRange; absoluteRange: AbsoluteTimeRange; } + export const changeRangeAction = createAction('explore/changeRange'); /** @@ -30,6 +32,7 @@ export interface ChangeRefreshIntervalPayload { exploreId: string; refreshInterval: string; } + export const changeRefreshInterval = createAction('explore/changeRefreshInterval'); export const updateTimeRange = (options: { @@ -78,6 +81,13 @@ export const updateTime = (config: { const range = getTimeRange(timeZone, rawRange, fiscalYearStartMonth); const absoluteRange: AbsoluteTimeRange = { from: range.from.valueOf(), to: range.to.valueOf() }; + // @deprecated - set because some internal plugins read the range this way; please use QueryEditorProps.range instead + getTimeSrv().init({ + timepicker: {}, + getTimezone: () => timeZone, + timeRangeUpdated(timeRange) {}, + time: range.raw, + }); // After re-initializing TimeSrv we need to update the time range in Template service for interpolation // of __from and __to variables getTemplateSrv().updateTimeRange(range);