diff --git a/public/app/features/explore/state/explorePane.test.ts b/public/app/features/explore/state/explorePane.test.ts index 8d997095489..81f1ef87e6a 100644 --- a/public/app/features/explore/state/explorePane.test.ts +++ b/public/app/features/explore/state/explorePane.test.ts @@ -33,6 +33,7 @@ const defaultInitialState = { eventBridge: {} as EventBusExtended, queries: [] as DataQuery[], range: testRange, + history: [], refreshInterval: { label: 'Off', value: 0, diff --git a/public/app/features/explore/state/query.test.ts b/public/app/features/explore/state/query.test.ts index f228a80ff7f..8a95b3ef9ee 100644 --- a/public/app/features/explore/state/query.test.ts +++ b/public/app/features/explore/state/query.test.ts @@ -63,6 +63,7 @@ const defaultInitialState = { eventBridge: { emit: () => {} } as any, queries: [{ expr: 'test' }] as any[], range: testRange, + history: [], refreshInterval: { label: 'Off', value: 0, diff --git a/public/app/features/explore/state/query.ts b/public/app/features/explore/state/query.ts index c1742588419..6107a17f8b3 100644 --- a/public/app/features/explore/state/query.ts +++ b/public/app/features/explore/state/query.ts @@ -7,6 +7,7 @@ import { DataQueryResponse, DataSourceApi, hasLogsVolumeSupport, + HistoryItem, LoadingState, PanelData, PanelEvents, @@ -25,8 +26,8 @@ import { updateHistory, } from 'app/core/utils/explore'; import { addToRichHistory } from 'app/core/utils/richHistory'; -import { ExploreItemState, ExplorePanelData, ThunkResult } from 'app/types'; -import { ExploreId, QueryOptions } from 'app/types/explore'; +import { ExploreItemState, ExplorePanelData, ThunkDispatch, ThunkResult } from 'app/types'; +import { ExploreId, ExploreState, QueryOptions } from 'app/types/explore'; import { getTimeZone } from 'app/features/profile/state/selectors'; import { getShiftedTimeRange } from 'app/core/utils/timePicker'; import { notifyApp } from '../../../core/actions'; @@ -299,6 +300,38 @@ export function modifyQueries( }; } +function handleHistory( + dispatch: ThunkDispatch, + state: ExploreState, + history: Array>, + datasource: DataSourceApi, + queries: DataQuery[], + exploreId: ExploreId +) { + const datasourceId = datasource.meta.id; + const nextHistory = updateHistory(history, datasourceId, queries); + const { richHistory: nextRichHistory, localStorageFull, limitExceeded } = addToRichHistory( + state.richHistory || [], + datasourceId, + datasource.name, + queries, + false, + '', + '', + !state.localStorageFull, + !state.richHistoryLimitExceededWarningShown + ); + dispatch(historyUpdatedAction({ exploreId, history: nextHistory })); + dispatch(richHistoryUpdatedAction({ richHistory: nextRichHistory })); + + if (localStorageFull) { + dispatch(localStorageFullAction()); + } + if (limitExceeded) { + dispatch(richHistoryLimitExceededAction()); + } +} + /** * Main action to run queries and dispatches sub-actions based on which result viewers are active */ @@ -315,7 +348,6 @@ export const runQueries = ( dispatch(clearCache(exploreId)); } - const { richHistory } = getState().explore; const exploreItemState = getState().explore[exploreId]!; const { datasourceInstance, @@ -325,7 +357,6 @@ export const runQueries = ( scanning, queryResponse, querySubscription, - history, refreshInterval, absoluteRange, cache, @@ -338,6 +369,12 @@ export const runQueries = ( datasource: query.datasource || datasourceInstance?.getRef(), })); + if (datasourceInstance != null) { + handleHistory(dispatch, getState().explore, exploreItemState.history, datasourceInstance, queries, exploreId); + } + + dispatch(stateSave({ replace: options?.replaceUrl })); + const cachedValue = getResultsFromCache(cache, absoluteRange); // If we have results saved in cache, we are going to use those results instead of running queries @@ -373,8 +410,6 @@ export const runQueries = ( stopQueryState(querySubscription); - const datasourceId = datasourceInstance?.meta.id; - const queryOptions: QueryOptions = { minInterval, // maxDataPoints is used in: @@ -387,11 +422,9 @@ export const runQueries = ( liveStreaming: live, }; - const datasourceName = datasourceInstance.name; const timeZone = getTimeZone(getState().user); const transaction = buildQueryTransaction(exploreId, queries, queryOptions, range, scanning, timeZone); - let querySaved = false; dispatch(changeLoadingStateAction({ exploreId, loadingState: LoadingState.Loading })); newQuerySub = runRequest(datasourceInstance, transaction.request) @@ -413,34 +446,6 @@ export const runQueries = ( ) .subscribe( (data) => { - if (data.state !== LoadingState.Loading && !data.error && !querySaved) { - // Side-effect: Saving history in localstorage - const nextHistory = updateHistory(history, datasourceId, queries); - const { richHistory: nextRichHistory, localStorageFull, limitExceeded } = addToRichHistory( - richHistory || [], - datasourceId, - datasourceName, - queries, - false, - '', - '', - !getState().explore.localStorageFull, - !getState().explore.richHistoryLimitExceededWarningShown - ); - dispatch(historyUpdatedAction({ exploreId, history: nextHistory })); - dispatch(richHistoryUpdatedAction({ richHistory: nextRichHistory })); - if (localStorageFull) { - dispatch(localStorageFullAction()); - } - if (limitExceeded) { - dispatch(richHistoryLimitExceededAction()); - } - - // We save queries to the URL here so that only successfully run queries change the URL. - dispatch(stateSave({ replace: options?.replaceUrl })); - querySaved = true; - } - dispatch(queryStreamUpdatedAction({ exploreId, response: data })); // Keep scanning for results if this was the last scanning transaction