Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] decouple ML plugin from Map embeddable #182409

Merged
merged 7 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions x-pack/plugins/maps/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type {
} from './classes/tooltips/tooltip_property';

export type { MapsSetupApi, MapsStartApi } from './api';
export type { CreateLayerDescriptorParams } from './classes/sources/es_search_source/create_layer_descriptor';

export type { MapEmbeddable, MapEmbeddableInput, MapEmbeddableOutput } from './embeddable';
export { type MapApi, isMapApi } from './embeddable/map_api';
Expand Down

This file was deleted.

This file was deleted.

13 changes: 11 additions & 2 deletions x-pack/plugins/ml/public/application/explorer/anomalies_map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
htmlIdGenerator,
} from '@elastic/eui';
import type { VectorLayerDescriptor } from '@kbn/maps-plugin/common';
import { INITIAL_LOCATION } from '@kbn/maps-plugin/common';
import {
FIELD_ORIGIN,
LAYER_TYPE,
Expand All @@ -29,7 +30,6 @@ import type { EMSTermJoinConfig } from '@kbn/maps-plugin/public';
import { isDefined } from '@kbn/ml-is-defined';
import type { MlAnomaliesTableRecord } from '@kbn/ml-anomaly-utils';
import { useMlKibana } from '../contexts/kibana';
import { MlEmbeddedMapComponent } from '../components/ml_embedded_map';

const MAX_ENTITY_VALUES = 3;

Expand Down Expand Up @@ -253,7 +253,16 @@ export const AnomaliesMap: FC<Props> = ({ anomalies, jobIds }) => {
data-test-subj="mlAnomalyExplorerAnomaliesMap"
style={{ width: '100%', height: 300 }}
>
<MlEmbeddedMapComponent layerList={layerList} />
{mapsPlugin && (
<mapsPlugin.Map
layerList={layerList}
hideFilterActions={true}
mapSettings={{
initialLocation: INITIAL_LOCATION.AUTO_FIT_TO_BOUNDS,
autoFitToDataBounds: true,
}}
/>
)}
</div>
</EuiAccordion>
</EuiPanel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@

import React, { useState, useEffect } from 'react';
import type { LayerDescriptor } from '@kbn/maps-plugin/common';
import { INITIAL_LOCATION } from '@kbn/maps-plugin/common';
import type { Dictionary } from '../../../../common/types/common';
import { getMLAnomaliesActualLayer, getMLAnomaliesTypicalLayer } from './map_config';
import { MlEmbeddedMapComponent } from '../../components/ml_embedded_map';
import { useMlKibana } from '../../contexts/kibana';
interface Props {
seriesConfig: Dictionary<any>;
}

export function EmbeddedMapComponentWrapper({ seriesConfig }: Props) {
const {
services: { maps: mapsPlugin },
} = useMlKibana();

const [layerList, setLayerList] = useState<LayerDescriptor[]>([]);

useEffect(() => {
Expand All @@ -26,9 +31,16 @@ export function EmbeddedMapComponentWrapper({ seriesConfig }: Props) {
}
}, [seriesConfig]);

return (
return mapsPlugin ? (
<div data-test-subj="xpack.ml.explorer.embeddedMap" style={{ width: '100%', height: 300 }}>
<MlEmbeddedMapComponent layerList={layerList} />
<mapsPlugin.Map
layerList={layerList}
hideFilterActions={true}
mapSettings={{
initialLocation: INITIAL_LOCATION.AUTO_FIT_TO_BOUNDS,
autoFitToDataBounds: true,
}}
/>
</div>
);
) : null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import memoizeOne from 'memoize-one';
import { isEqual } from 'lodash';
import type { DataView } from '@kbn/data-views-plugin/common';
import type { ES_GEO_FIELD_TYPE, LayerDescriptor } from '@kbn/maps-plugin/common';
import type { MapsStartApi } from '@kbn/maps-plugin/public';
import type { CreateLayerDescriptorParams, MapsStartApi } from '@kbn/maps-plugin/public';
import type { Query } from '@kbn/es-query';
import type { Field, SplitField } from '@kbn/ml-anomaly-utils';
import { ChartLoader } from '../chart_loader';
Expand All @@ -30,7 +30,6 @@ export class MapLoader extends ChartLoader {
geoField: Field,
splitField: SplitField,
fieldValues: string[],
filters?: any[],
savedSearchQuery?: Query
) {
const layerList: LayerDescriptor[] = [];
Expand All @@ -41,11 +40,10 @@ export class MapLoader extends ChartLoader {
? `${splitField.name}:${fieldValues[0]} ${query ? `and ${query}` : ''}`
: `${query ? query : ''}`;

const params: any = {
const params: CreateLayerDescriptorParams = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 Nice to get rid of this any.

indexPatternId: this._dataView.id,
geoFieldName: geoField.name,
geoFieldType: geoField.type as unknown as ES_GEO_FIELD_TYPE,
filters: filters ?? [],
query: { query: queryString, language: 'kuery' },
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import type { FC } from 'react';
import React from 'react';
import { EuiFlexGrid, EuiFlexItem, EuiSpacer } from '@elastic/eui';
import type { LayerDescriptor } from '@kbn/maps-plugin/common';
import { INITIAL_LOCATION } from '@kbn/maps-plugin/common';
import type { Aggregation, Field, SplitField } from '@kbn/ml-anomaly-utils';
import { SplitCards, useAnimateSplit } from '../split_cards';
import { MlEmbeddedMapComponent } from '../../../../../../../components/ml_embedded_map';
import { useMlKibana } from '../../../../../../../contexts/kibana';
import { JOB_TYPE } from '../../../../../../../../../common/constants/new_job';
import { DetectorTitle } from '../detector_title';

Expand All @@ -31,6 +32,10 @@ export const GeoMapExamples: FC<Props> = ({
geoAgg,
layerList,
}) => {
const {
services: { maps: mapsPlugin },
} = useMlKibana();

const animateSplit = useAnimateSplit();

return (
Expand All @@ -46,9 +51,18 @@ export const GeoMapExamples: FC<Props> = ({
<>
{geoAgg && geoField ? <DetectorTitle index={0} agg={geoAgg} field={geoField} /> : null}
<EuiSpacer size="s" />
<span data-test-subj="mlGeoJobWizardMap" style={{ width: '100%', height: 400 }}>
<MlEmbeddedMapComponent layerList={layerList} />
</span>
{mapsPlugin && (
<span data-test-subj="mlGeoJobWizardMap" style={{ width: '100%', height: 400 }}>
<mapsPlugin.Map
layerList={layerList}
hideFilterActions={true}
mapSettings={{
initialLocation: INITIAL_LOCATION.AUTO_FIT_TO_BOUNDS,
autoFitToDataBounds: true,
}}
/>
</span>
)}
</>
</EuiFlexItem>
</EuiFlexGrid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const GeoDetector: FC<Props> = ({ setIsValid }) => {
const [layerList, setLayerList] = useState<LayerDescriptor[]>([]);

const {
services: { data, notifications: toasts },
services: { notifications: toasts },
} = useMlKibana();
const { mapLoader } = useContext(JobCreatorContext);

Expand Down Expand Up @@ -72,14 +72,12 @@ export const GeoDetector: FC<Props> = ({ setIsValid }) => {
useEffect(() => {
async function getMapLayersForGeoJob() {
if (jobCreator.geoField) {
const { filter, query } = jobCreator.savedSearchQuery ?? {};
const filters = [...data.query.filterManager.getFilters(), ...(filter ?? [])];
const { query } = jobCreator.savedSearchQuery ?? {};

const layers = await mapLoader.getMapLayersForGeoJob(
jobCreator.geoField,
jobCreator.splitField,
fieldValues,
filters,
query
);
setLayerList(layers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const GeoDetectorsSummary: FC = () => {
const splitField = jobCreator.splitField;

const {
services: { data, notifications },
services: { notifications },
} = useMlKibana();

// Load example field values for summary view
Expand Down Expand Up @@ -56,14 +56,12 @@ export const GeoDetectorsSummary: FC = () => {
useEffect(() => {
async function getMapLayersForGeoJob() {
if (geoField) {
const { filter, query } = jobCreator.savedSearchQuery ?? {};
const filters = [...data.query.filterManager.getFilters(), ...(filter ?? [])];
const { query } = jobCreator.savedSearchQuery ?? {};

const layers = await mapLoader.getMapLayersForGeoJob(
geoField,
splitField,
fieldValues,
filters,
query
);
setLayerList(layers);
Expand Down
2 changes: 1 addition & 1 deletion x-pack/test/functional/services/ml/job_wizard_geo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function MachineLearningJobWizardGeoProvider({ getService }: FtrProviderC
);

await testSubjects.existOrFail('mlGeoJobWizardMap');
await testSubjects.existOrFail('mlEmbeddedMapContent');
await testSubjects.existOrFail('mapContainer');
},
};
}