Explore: Add caching for queries run from logs navigation (#34297)

* WIP: Implement simple caching

* If results are cached, don't run new query and use those results

* Add duplicate key check

* Clean up

* Clean up

* Add tests for caching

* Remove unused variables

* Update public/app/features/explore/state/query.test.ts

Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com>

* Update public/app/features/explore/state/query.test.ts

Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com>

* Use decorateData to apply all decorators

* Remove unused variables

* Change loading stte to Done

* Clear cache when running query from navigation

Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com>
This commit is contained in:
Ivana Huckova
2021-05-24 13:56:48 +02:00
committed by GitHub
parent b5de6e7a1d
commit 247bdc2f9b
12 changed files with 363 additions and 103 deletions

View File

@@ -1,5 +1,7 @@
import {
addQueryRowAction,
addResultsToCache,
clearCache,
cancelQueries,
cancelQueriesAction,
queryReducer,
@@ -10,7 +12,17 @@ import {
} from './query';
import { ExploreId, ExploreItemState } from 'app/types';
import { interval, of } from 'rxjs';
import { ArrayVector, DataQueryResponse, DefaultTimeZone, MutableDataFrame, RawTimeRange, toUtc } from '@grafana/data';
import {
ArrayVector,
DataQueryResponse,
DefaultTimeZone,
MutableDataFrame,
RawTimeRange,
toUtc,
PanelData,
DataFrame,
LoadingState,
} from '@grafana/data';
import { thunkTester } from 'test/core/thunk/thunkTester';
import { makeExplorePaneState } from './utils';
import { reducerTester } from '../../../../test/core/redux/reducerTester';
@@ -50,6 +62,7 @@ const defaultInitialState = {
label: 'Off',
value: 0,
},
cache: [],
},
},
};
@@ -213,4 +226,95 @@ describe('reducer', () => {
});
});
});
describe('caching', () => {
it('should add response to cache', async () => {
const store = configureStore({
...(defaultInitialState as any),
explore: {
[ExploreId.left]: {
...defaultInitialState.explore[ExploreId.left],
queryResponse: {
series: [{ name: 'test name' }] as DataFrame[],
state: LoadingState.Done,
} as PanelData,
absoluteRange: { from: 1621348027000, to: 1621348050000 },
},
},
});
await store.dispatch(addResultsToCache(ExploreId.left));
expect(store.getState().explore[ExploreId.left].cache).toEqual([
{ key: 'from=1621348027000&to=1621348050000', value: { series: [{ name: 'test name' }], state: 'Done' } },
]);
});
it('should not add response to cache if response is still loading', async () => {
const store = configureStore({
...(defaultInitialState as any),
explore: {
[ExploreId.left]: {
...defaultInitialState.explore[ExploreId.left],
queryResponse: { series: [{ name: 'test name' }] as DataFrame[], state: LoadingState.Loading } as PanelData,
absoluteRange: { from: 1621348027000, to: 1621348050000 },
},
},
});
await store.dispatch(addResultsToCache(ExploreId.left));
expect(store.getState().explore[ExploreId.left].cache).toEqual([]);
});
it('should not add duplicate response to cache', async () => {
const store = configureStore({
...(defaultInitialState as any),
explore: {
[ExploreId.left]: {
...defaultInitialState.explore[ExploreId.left],
queryResponse: {
series: [{ name: 'test name' }] as DataFrame[],
state: LoadingState.Done,
} as PanelData,
absoluteRange: { from: 1621348027000, to: 1621348050000 },
cache: [
{
key: 'from=1621348027000&to=1621348050000',
value: { series: [{ name: 'old test name' }], state: LoadingState.Done },
},
],
},
},
});
await store.dispatch(addResultsToCache(ExploreId.left));
expect(store.getState().explore[ExploreId.left].cache).toHaveLength(1);
expect(store.getState().explore[ExploreId.left].cache).toEqual([
{ key: 'from=1621348027000&to=1621348050000', value: { series: [{ name: 'old test name' }], state: 'Done' } },
]);
});
it('should clear cache', async () => {
const store = configureStore({
...(defaultInitialState as any),
explore: {
[ExploreId.left]: {
...defaultInitialState.explore[ExploreId.left],
cache: [
{
key: 'from=1621348027000&to=1621348050000',
value: { series: [{ name: 'old test name' }], state: 'Done' },
},
],
},
},
});
await store.dispatch(clearCache(ExploreId.left));
expect(store.getState().explore[ExploreId.left].cache).toEqual([]);
});
});
});