Explore: Fix TimeSrv initializations (#73903)

Ensure TimeSrv is re-initializes
This commit is contained in:
Piotr Jamróz 2023-08-29 15:05:18 +02:00 committed by GitHub
parent 557b1654f9
commit 291ca54010
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -7,6 +7,13 @@ import { ExploreItemState } from 'app/types';
import { createDefaultInitialState } from './helpers'; import { createDefaultInitialState } from './helpers';
import { changeRangeAction, timeReducer, updateTime } from './time'; 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 = { const mockTemplateSrv = {
updateTimeRange: jest.fn(), updateTimeRange: jest.fn(),
}; };
@ -22,6 +29,8 @@ describe('Explore item reducer', () => {
const { dispatch } = configureStore(state); const { dispatch } = configureStore(state);
dispatch(updateTime({ exploreId: 'left' })); dispatch(updateTime({ exploreId: 'left' }));
expect(mockTemplateSrv.updateTimeRange).toBeCalledWith(state.explore.panes.left.range); expect(mockTemplateSrv.updateTimeRange).toBeCalledWith(state.explore.panes.left.range);
expect(mockTimeSrv.init).toBeCalled();
expect(mockTemplateSrv.updateTimeRange).toBeCalledWith(state.explore.panes.left.range);
}); });
}); });

View File

@ -5,6 +5,7 @@ import { getTemplateSrv } from '@grafana/runtime';
import { RefreshPicker } from '@grafana/ui'; import { RefreshPicker } from '@grafana/ui';
import { getTimeRange, refreshIntervalToSortOrder, stopQueryState } from 'app/core/utils/explore'; import { getTimeRange, refreshIntervalToSortOrder, stopQueryState } from 'app/core/utils/explore';
import { getShiftedTimeRange, getZoomedTimeRange } from 'app/core/utils/timePicker'; import { getShiftedTimeRange, getZoomedTimeRange } from 'app/core/utils/timePicker';
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { sortLogsResult } from 'app/features/logs/utils'; import { sortLogsResult } from 'app/features/logs/utils';
import { getFiscalYearStartMonth, getTimeZone } from 'app/features/profile/state/selectors'; import { getFiscalYearStartMonth, getTimeZone } from 'app/features/profile/state/selectors';
import { ExploreItemState, ThunkDispatch, ThunkResult } from 'app/types'; import { ExploreItemState, ThunkDispatch, ThunkResult } from 'app/types';
@ -21,6 +22,7 @@ export interface ChangeRangePayload {
range: TimeRange; range: TimeRange;
absoluteRange: AbsoluteTimeRange; absoluteRange: AbsoluteTimeRange;
} }
export const changeRangeAction = createAction<ChangeRangePayload>('explore/changeRange'); export const changeRangeAction = createAction<ChangeRangePayload>('explore/changeRange');
/** /**
@ -30,6 +32,7 @@ export interface ChangeRefreshIntervalPayload {
exploreId: string; exploreId: string;
refreshInterval: string; refreshInterval: string;
} }
export const changeRefreshInterval = createAction<ChangeRefreshIntervalPayload>('explore/changeRefreshInterval'); export const changeRefreshInterval = createAction<ChangeRefreshIntervalPayload>('explore/changeRefreshInterval');
export const updateTimeRange = (options: { export const updateTimeRange = (options: {
@ -78,6 +81,13 @@ export const updateTime = (config: {
const range = getTimeRange(timeZone, rawRange, fiscalYearStartMonth); const range = getTimeRange(timeZone, rawRange, fiscalYearStartMonth);
const absoluteRange: AbsoluteTimeRange = { from: range.from.valueOf(), to: range.to.valueOf() }; 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 // After re-initializing TimeSrv we need to update the time range in Template service for interpolation
// of __from and __to variables // of __from and __to variables
getTemplateSrv().updateTimeRange(range); getTemplateSrv().updateTimeRange(range);