[ML] Anomaly Detection geo wizard: ensure map examples reflect saved search (#149291)

## Summary

Fixes https://github.com/elastic/kibana/issues/149290

Ensure the map examples reflect the saved search query.

<img width="1179" alt="image"
src="https://user-images.githubusercontent.com/6446462/213820351-00cbca76-cafc-4820-a2df-333805ee9cde.png">


### Checklist

Delete any items that are not applicable to this PR.

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] Any UI touched in this PR is usable by keyboard only (learn more
about [keyboard accessibility](https://webaim.org/techniques/keyboard/))
- [ ] Any UI touched in this PR does not create any new axe failures
(run axe in browser:
[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),
[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This renders correctly on smaller devices using a responsive
layout. (You can test this [in your
browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))
- [ ] This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Melissa Alvarez 2023-01-26 11:35:38 -07:00 committed by GitHub
parent 80b78d94cc
commit 1496173cdf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 8 deletions

View file

@ -8,6 +8,7 @@
import { BehaviorSubject } from 'rxjs';
import { cloneDeep } from 'lodash';
import { ES_FIELD_TYPES } from '@kbn/field-types';
import type { Query } from '@kbn/es-query';
import type { DataView } from '@kbn/data-views-plugin/public';
import { addExcludeFrozenToQuery } from '@kbn/ml-query-utils';
import { SavedSearchSavedObject } from '../../../../../../common/types/kibana';
@ -18,6 +19,7 @@ import {
aggregations,
mlOnlyAggregations,
} from '../../../../../../common/constants/aggregation_types';
import { getQueryFromSavedSearchObject } from '../../../../util/index_utils';
import {
Job,
Datafeed,
@ -105,9 +107,14 @@ export class JobCreator {
return this._type;
}
public get savedSearch(): SavedSearchSavedObject | null {
return this._savedSearch;
}
public get dataView(): DataView {
return this._indexPattern;
}
public get dataViewId(): string | undefined {
return this._indexPattern.id;
}
@ -148,6 +155,10 @@ export class JobCreator {
this._fields.length = 0;
}
public get savedSearchQuery(): { query: Query; filter: any[] } | null {
return this._savedSearch ? getQueryFromSavedSearchObject(this._savedSearch) : null;
}
public get detectors(): Detector[] {
return this._detectors;
}

View file

@ -10,6 +10,7 @@ import { isEqual } from 'lodash';
import type { DataView } from '@kbn/data-views-plugin/common';
import { ES_GEO_FIELD_TYPE, LayerDescriptor } from '@kbn/maps-plugin/common';
import type { MapsStartApi } from '@kbn/maps-plugin/public';
import type { Query } from '@kbn/es-query';
import { ChartLoader } from '../chart_loader';
import { Field, SplitField } from '../../../../../../common/types/fields';
const eq = (newArgs: any[], lastArgs: any[]) => isEqual(newArgs, lastArgs);
@ -29,18 +30,23 @@ export class MapLoader extends ChartLoader {
geoField: Field,
splitField: SplitField,
fieldValues: string[],
filters?: any[]
filters?: any[],
savedSearchQuery?: Query
) {
const layerList: LayerDescriptor[] = [];
if (this._dataView.id !== undefined && geoField) {
const { query } = savedSearchQuery ?? {};
const queryString =
fieldValues.length && splitField
? `${splitField.name}:${fieldValues[0]} ${query ? `and ${query}` : ''}`
: `${query ? query : ''}`;
const params: any = {
indexPatternId: this._dataView.id,
geoFieldName: geoField.name,
geoFieldType: geoField.type as unknown as ES_GEO_FIELD_TYPE,
filters: filters ?? [],
...(fieldValues.length && splitField
? { query: { query: `${splitField.name}:${fieldValues[0]}`, language: 'kuery' } }
: {}),
query: { query: queryString, language: 'kuery' },
};
const searchLayerDescriptor = this._getMapData ? await this._getMapData(params) : null;

View file

@ -71,12 +71,15 @@ export const GeoDetector: FC<Props> = ({ setIsValid }) => {
useEffect(() => {
async function getMapLayersForGeoJob() {
if (jobCreator.geoField) {
const filters = data.query.filterManager.getFilters() ?? [];
const { filter, query } = jobCreator.savedSearchQuery ?? {};
const filters = [...data.query.filterManager.getFilters(), ...(filter ?? [])];
const layers = await mapLoader.getMapLayersForGeoJob(
jobCreator.geoField,
jobCreator.splitField,
fieldValues,
filters
filters,
query
);
setLayerList(layers);
}

View file

@ -55,12 +55,15 @@ export const GeoDetectorsSummary: FC = () => {
useEffect(() => {
async function getMapLayersForGeoJob() {
if (geoField) {
const filters = data.query.filterManager.getFilters() ?? [];
const { filter, query } = jobCreator.savedSearchQuery ?? {};
const filters = [...data.query.filterManager.getFilters(), ...(filter ?? [])];
const layers = await mapLoader.getMapLayersForGeoJob(
geoField,
splitField,
fieldValues,
filters
filters,
query
);
setLayerList(layers);
}