Explore: Cancel previous queries when a new query is run (#76674)

* Cancel streaming queries before running new queries

* Cancel previous queries when a new query is run

* Remove redundant dependency

---------

Co-authored-by: Kristina Durivage <kristina.durivage@grafana.com>
This commit is contained in:
Piotr Jamróz
2023-10-19 19:31:23 +02:00
committed by GitHub
parent 8f6a3c18c5
commit 6531cd94c4
2 changed files with 21 additions and 4 deletions

View File

@@ -242,6 +242,16 @@ describe('running queries', () => {
cleanSupplementaryQueryAction({ exploreId, type: SupplementaryQueryType.LogsSample }),
]);
});
it('should cancel running query when a new query is issued', async () => {
const initialState = {
...makeExplorePaneState(),
};
const dispatchedActions = await thunkTester(initialState)
.givenThunk(runQueries)
.whenThunkIsDispatched({ exploreId });
expect(dispatchedActions).toContainEqual(cancelQueriesAction({ exploreId }));
});
});
describe('changeQueries', () => {

View File

@@ -497,6 +497,8 @@ interface RunQueriesOptions {
export const runQueries = createAsyncThunk<void, RunQueriesOptions>(
'explore/runQueries',
async ({ exploreId, preserveCache }, { dispatch, getState }) => {
dispatch(cancelQueries(exploreId));
dispatch(updateTime({ exploreId }));
const correlations$ = getCorrelations(exploreId);
@@ -954,10 +956,15 @@ export const queryReducer = (state: ExploreItemState, action: AnyAction): Explor
return {
...state,
queryResponse: {
...state.queryResponse,
state: LoadingState.Done,
},
// mark existing request as done (may be incomplete)
...(state.queryResponse
? {
queryResponse: {
...state.queryResponse,
state: LoadingState.Done,
},
}
: {}),
};
}