explore: store failed queries too (#42516)

* explore: store failed queries too

* fixed test
This commit is contained in:
Gábor Farkas 2021-12-09 14:00:50 +01:00 committed by GitHub
parent 3232ae08c0
commit 096c248887
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 36 deletions

View File

@ -33,6 +33,7 @@ const defaultInitialState = {
eventBridge: {} as EventBusExtended,
queries: [] as DataQuery[],
range: testRange,
history: [],
refreshInterval: {
label: 'Off',
value: 0,

View File

@ -63,6 +63,7 @@ const defaultInitialState = {
eventBridge: { emit: () => {} } as any,
queries: [{ expr: 'test' }] as any[],
range: testRange,
history: [],
refreshInterval: {
label: 'Off',
value: 0,

View File

@ -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<HistoryItem<DataQuery>>,
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