Skip to content

Commit

Permalink
[Maps] use stored map buffer to generate queries for search sessions (e…
Browse files Browse the repository at this point in the history
…lastic#91148)

* [Maps] use stored map buffer to generate queries for search sessions

* getDataFilters unit test

* tslint

* update setQuery unit tests

* only set searchSessionMapBuffer when search session isRestore

* tslint

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
nreese and kibanamachine committed Feb 17, 2021
1 parent 86739f3 commit 8a8017d
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 38 deletions.
3 changes: 3 additions & 0 deletions x-pack/plugins/maps/public/actions/map_actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ describe('map_actions', () => {
require('../selectors/map_selectors').getSearchSessionId = () => {
return searchSessionId;
};
require('../selectors/map_selectors').getSearchSessionMapBuffer = () => {
return undefined;
};
require('../selectors/map_selectors').getMapSettings = () => {
return {
autoFitToDataBounds: false,
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/maps/public/actions/map_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getTimeFilters,
getLayerList,
getSearchSessionId,
getSearchSessionMapBuffer,
} from '../selectors/map_selectors';
import {
CLEAR_GOTO,
Expand Down Expand Up @@ -229,12 +230,14 @@ export function setQuery({
filters = [],
forceRefresh = false,
searchSessionId,
searchSessionMapBuffer,
}: {
filters?: Filter[];
query?: Query;
timeFilters?: TimeRange;
forceRefresh?: boolean;
searchSessionId?: string;
searchSessionMapBuffer?: MapExtent;
}) {
return async (
dispatch: ThunkDispatch<MapStoreState, void, AnyAction>,
Expand All @@ -255,13 +258,15 @@ export function setQuery({
},
filters: filters ? filters : getFilters(getState()),
searchSessionId,
searchSessionMapBuffer,
};

const prevQueryContext = {
timeFilters: getTimeFilters(getState()),
query: getQuery(getState()),
filters: getFilters(getState()),
searchSessionId: getSearchSessionId(getState()),
searchSessionMapBuffer: getSearchSessionMapBuffer(getState()),
};

if (_.isEqual(nextQueryContext, prevQueryContext)) {
Expand Down
50 changes: 17 additions & 33 deletions x-pack/plugins/maps/public/embeddable/map_embeddable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
} from '../reducers/non_serializable_instances';
import {
getMapCenter,
getMapBuffer,
getMapZoom,
getHiddenLayerIds,
getQueryableUniqueIndexPatternIds,
Expand Down Expand Up @@ -151,11 +152,7 @@ export class MapEmbeddable
store.dispatch(disableScrollZoom());

this._dispatchSetQuery({
query: this.input.query,
timeRange: this.input.timeRange,
filters: this.input.filters,
forceRefresh: false,
searchSessionId: this.input.searchSessionId,
});
if (this.input.refreshConfig) {
this._dispatchSetRefreshConfig(this.input.refreshConfig);
Expand Down Expand Up @@ -230,11 +227,7 @@ export class MapEmbeddable
this.input.searchSessionId !== this._prevSearchSessionId
) {
this._dispatchSetQuery({
query: this.input.query,
timeRange: this.input.timeRange,
filters: this.input.filters,
forceRefresh: false,
searchSessionId: this.input.searchSessionId,
});
}

Expand All @@ -258,30 +251,24 @@ export class MapEmbeddable
}
}

_dispatchSetQuery({
query,
timeRange,
filters = [],
forceRefresh,
searchSessionId,
}: {
query?: Query;
timeRange?: TimeRange;
filters?: Filter[];
forceRefresh: boolean;
searchSessionId?: string;
}) {
this._prevTimeRange = timeRange;
this._prevQuery = query;
this._prevFilters = filters;
this._prevSearchSessionId = searchSessionId;
_dispatchSetQuery({ forceRefresh }: { forceRefresh: boolean }) {
this._prevTimeRange = this.input.timeRange;
this._prevQuery = this.input.query;
this._prevFilters = this.input.filters;
this._prevSearchSessionId = this.input.searchSessionId;
const enabledFilters = this.input.filters
? this.input.filters.filter((filter) => !filter.meta.disabled)
: [];
this._savedMap.getStore().dispatch<any>(
setQuery({
filters: filters.filter((filter) => !filter.meta.disabled),
query,
timeFilters: timeRange,
filters: enabledFilters,
query: this.input.query,
timeFilters: this.input.timeRange,
forceRefresh,
searchSessionId,
searchSessionId: this.input.searchSessionId,
searchSessionMapBuffer: getIsRestore(this.input.searchSessionId)
? this.input.mapBuffer
: undefined,
})
);
}
Expand Down Expand Up @@ -432,11 +419,7 @@ export class MapEmbeddable

reload() {
this._dispatchSetQuery({
query: this.input.query,
timeRange: this.input.timeRange,
filters: this.input.filters,
forceRefresh: true,
searchSessionId: this.input.searchSessionId,
});
}

Expand All @@ -457,6 +440,7 @@ export class MapEmbeddable
lon: center.lon,
zoom,
},
mapBuffer: getMapBuffer(this._savedMap.getStore().getState()),
});
}

Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/maps/public/embeddable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
SavedObjectEmbeddableInput,
} from '../../../../../src/plugins/embeddable/public';
import { RefreshInterval, Query, Filter, TimeRange } from '../../../../../src/plugins/data/common';
import { MapCenterAndZoom } from '../../common/descriptor_types';
import { MapCenterAndZoom, MapExtent } from '../../common/descriptor_types';
import { MapSavedObjectAttributes } from '../../common/map_saved_object_type';
import { MapSettings } from '../reducers/map';

Expand All @@ -25,6 +25,7 @@ interface MapEmbeddableState {
isLayerTOCOpen?: boolean;
openTOCDetails?: string[];
mapCenter?: MapCenterAndZoom;
mapBuffer?: MapExtent;
mapSettings?: Partial<MapSettings>;
hiddenLayers?: string[];
hideFilterActions?: boolean;
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/maps/public/reducers/map.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type MapContext = {
refreshTimerLastTriggeredAt?: string;
drawState?: DrawState;
searchSessionId?: string;
searchSessionMapBuffer?: MapExtent;
};

export type MapSettings = {
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/maps/public/reducers/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export function map(state = DEFAULT_MAP_STATE, action) {
};
return { ...state, mapState: { ...state.mapState, ...newMapState } };
case SET_QUERY:
const { query, timeFilters, filters, searchSessionId } = action;
const { query, timeFilters, filters, searchSessionId, searchSessionMapBuffer } = action;
return {
...state,
mapState: {
Expand All @@ -250,6 +250,7 @@ export function map(state = DEFAULT_MAP_STATE, action) {
timeFilters,
filters,
searchSessionId,
searchSessionMapBuffer,
},
};
case SET_REFRESH_CONFIG:
Expand Down
60 changes: 59 additions & 1 deletion x-pack/plugins/maps/public/selectors/map_selectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,67 @@ jest.mock('../kibana_services', () => ({
}));

import { DEFAULT_MAP_STORE_STATE } from '../reducers/store';
import { areLayersLoaded, getTimeFilters } from './map_selectors';
import { areLayersLoaded, getDataFilters, getTimeFilters } from './map_selectors';
import { LayerDescriptor } from '../../common/descriptor_types';
import { ILayer } from '../classes/layers/layer';
import { Filter } from '../../../../../src/plugins/data/public';

describe('getDataFilters', () => {
const mapExtent = {
maxLat: 1,
maxLon: 1,
minLat: 0,
minLon: 0,
};
const mapBuffer = {
maxLat: 1.5,
maxLon: 1.5,
minLat: -0.5,
minLon: -0.5,
};
const mapZoom = 4;
const timeFilters = { to: '2001-01-01', from: '2001-12-31' };
const refreshTimerLastTriggeredAt = '2001-01-01T00:00:00';
const query = undefined;
const filters: Filter[] = [];
const searchSessionId = '12345';
const searchSessionMapBuffer = {
maxLat: 1.25,
maxLon: 1.25,
minLat: -0.25,
minLon: -0.25,
};

test('should set buffer as searchSessionMapBuffer when using searchSessionId', () => {
const dataFilters = getDataFilters.resultFunc(
mapExtent,
mapBuffer,
mapZoom,
timeFilters,
refreshTimerLastTriggeredAt,
query,
filters,
searchSessionId,
searchSessionMapBuffer
);
expect(dataFilters.buffer).toEqual(searchSessionMapBuffer);
});

test('should fall back to screen buffer when using searchSessionId and searchSessionMapBuffer is not provided', () => {
const dataFilters = getDataFilters.resultFunc(
mapExtent,
mapBuffer,
mapZoom,
timeFilters,
refreshTimerLastTriggeredAt,
query,
filters,
searchSessionId,
undefined
);
expect(dataFilters.buffer).toEqual(mapBuffer);
});
});

describe('getTimeFilters', () => {
test('should return timeFilters when contained in state', () => {
Expand Down
9 changes: 7 additions & 2 deletions x-pack/plugins/maps/public/selectors/map_selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ export const getFilters = ({ map }: MapStoreState): Filter[] => map.mapState.fil
export const getSearchSessionId = ({ map }: MapStoreState): string | undefined =>
map.mapState.searchSessionId;

export const getSearchSessionMapBuffer = ({ map }: MapStoreState): MapExtent | undefined =>
map.mapState.searchSessionMapBuffer;

export const isUsingSearch = (state: MapStoreState): boolean => {
const filters = getFilters(state).filter((filter) => !filter.meta.disabled);
const queryString = _.get(getQuery(state), 'query', '');
Expand Down Expand Up @@ -235,6 +238,7 @@ export const getDataFilters = createSelector(
getQuery,
getFilters,
getSearchSessionId,
getSearchSessionMapBuffer,
(
mapExtent,
mapBuffer,
Expand All @@ -243,11 +247,12 @@ export const getDataFilters = createSelector(
refreshTimerLastTriggeredAt,
query,
filters,
searchSessionId
searchSessionId,
searchSessionMapBuffer
) => {
return {
extent: mapExtent,
buffer: mapBuffer,
buffer: searchSessionId && searchSessionMapBuffer ? searchSessionMapBuffer : mapBuffer,
zoom: mapZoom,
timeFilters,
refreshTimerLastTriggeredAt,
Expand Down

0 comments on commit 8a8017d

Please sign in to comment.