From e121d6724193fd765c79cc6fb03af95cb4377510 Mon Sep 17 00:00:00 2001 From: Melissa Alvarez Date: Wed, 19 Jan 2022 12:37:29 -0700 Subject: [PATCH] update naming and types --- x-pack/plugins/maps/common/index.ts | 1 + x-pack/plugins/ml/common/util/job_utils.ts | 9 +----- .../services/ml_api_service/jobs.ts | 2 +- .../ml/public/maps/anomaly_job_selector.tsx | 6 ++-- .../ml/public/maps/anomaly_layer_wizard.tsx | 2 +- .../maps/anomaly_layer_wizard_factory.tsx | 28 ++++++++++--------- .../plugins/ml/public/maps/anomaly_source.tsx | 22 ++++++--------- .../ml/public/maps/anomaly_source_field.ts | 12 ++------ x-pack/plugins/ml/public/maps/util.ts | 11 ++++---- .../ml/server/models/job_service/jobs.ts | 17 ++--------- .../plugins/ml/server/routes/job_service.ts | 4 +-- 11 files changed, 44 insertions(+), 70 deletions(-) diff --git a/x-pack/plugins/maps/common/index.ts b/x-pack/plugins/maps/common/index.ts index f3b8595efc8d197..3e26cdc82fbbba2 100644 --- a/x-pack/plugins/maps/common/index.ts +++ b/x-pack/plugins/maps/common/index.ts @@ -33,4 +33,5 @@ export type { TooltipFeature, VectorLayerDescriptor, VectorStyleDescriptor, + VectorSourceRequestMeta, } from './descriptor_types'; diff --git a/x-pack/plugins/ml/common/util/job_utils.ts b/x-pack/plugins/ml/common/util/job_utils.ts index 0eae428a51bf0fa..f1991118d4e3687 100644 --- a/x-pack/plugins/ml/common/util/job_utils.ts +++ b/x-pack/plugins/ml/common/util/job_utils.ts @@ -75,15 +75,8 @@ export function isMappableJob(job: CombinedJob, detectorIndex: number): boolean // Returns a boolean indicating whether the specified job is suitable for maps plugin. export function isJobWithGeoData(job: Job): boolean { - let isMappable = false; const { detectors } = job.analysis_config; - - detectors.forEach((detector) => { - if (detector.function === ML_JOB_AGGREGATION.LAT_LONG) { - isMappable = true; - } - }); - return isMappable; + return detectors.some((detector) => detector.function === ML_JOB_AGGREGATION.LAT_LONG); } /** diff --git a/x-pack/plugins/ml/public/application/services/ml_api_service/jobs.ts b/x-pack/plugins/ml/public/application/services/ml_api_service/jobs.ts index 41bf3fc918626b8..f1492074ed76222 100644 --- a/x-pack/plugins/ml/public/application/services/ml_api_service/jobs.ts +++ b/x-pack/plugins/ml/public/application/services/ml_api_service/jobs.ts @@ -45,7 +45,7 @@ export const jobsApiProvider = (httpService: HttpService) => ({ }); }, - jobsWithGeo() { + jobIdsWithGeo() { return httpService.http({ path: `${ML_BASE_PATH}/jobs/jobs_with_geo`, method: 'GET', diff --git a/x-pack/plugins/ml/public/maps/anomaly_job_selector.tsx b/x-pack/plugins/ml/public/maps/anomaly_job_selector.tsx index 17ef29b6b0adad4..aacc8e37d935bac 100644 --- a/x-pack/plugins/ml/public/maps/anomaly_job_selector.tsx +++ b/x-pack/plugins/ml/public/maps/anomaly_job_selector.tsx @@ -28,7 +28,7 @@ export class AnomalyJobSelector extends Component { state: State = {}; private async _loadJobs() { - const jobIdList = await this.props.mlJobsService.jobsWithGeo(); + const jobIdList = await this.props.mlJobsService.jobIdsWithGeo(); const options = jobIdList.map((jobId) => { return { label: jobId, value: jobId }; }); @@ -39,7 +39,7 @@ export class AnomalyJobSelector extends Component { } } - componentDidUpdate(prevProps: Readonly, prevState: Readonly, snapshot?: any): void { + componentDidUpdate(prevProps: Readonly, prevState: Readonly): void { this._loadJobs(); } @@ -68,7 +68,7 @@ export class AnomalyJobSelector extends Component { return ( diff --git a/x-pack/plugins/ml/public/maps/anomaly_layer_wizard.tsx b/x-pack/plugins/ml/public/maps/anomaly_layer_wizard.tsx index d96dcb1cb6f0b33..59343b33629dedb 100644 --- a/x-pack/plugins/ml/public/maps/anomaly_layer_wizard.tsx +++ b/x-pack/plugins/ml/public/maps/anomaly_layer_wizard.tsx @@ -10,7 +10,7 @@ import { LAYER_WIZARD_CATEGORY } from '../../../maps/common'; import type { LayerWizard } from '../../../maps/public'; export const anomalyLayerWizard: Partial = { - categories: [LAYER_WIZARD_CATEGORY.SOLUTIONS], + categories: [LAYER_WIZARD_CATEGORY.SOLUTIONS, LAYER_WIZARD_CATEGORY.ELASTICSEARCH], description: i18n.translate('xpack.ml.maps.anomalyLayerDescription', { defaultMessage: 'Create anomalies layers', }), diff --git a/x-pack/plugins/ml/public/maps/anomaly_layer_wizard_factory.tsx b/x-pack/plugins/ml/public/maps/anomaly_layer_wizard_factory.tsx index 8848e5a3f378a33..70a81c89816a2d1 100644 --- a/x-pack/plugins/ml/public/maps/anomaly_layer_wizard_factory.tsx +++ b/x-pack/plugins/ml/public/maps/anomaly_layer_wizard_factory.tsx @@ -6,7 +6,7 @@ */ import React from 'react'; -import uuid from 'uuid'; +import { htmlIdGenerator } from '@elastic/eui'; import type { StartServicesAccessor } from 'kibana/public'; import type { LayerWizard, RenderWizardArguments } from '../../../maps/public'; import { FIELD_ORIGIN, LAYER_TYPE, STYLE_TYPE } from '../../../maps/common'; @@ -23,6 +23,17 @@ import type { MlPluginStart, MlStartDependencies } from '../plugin'; import type { MlApiServices } from '../application/services/ml_api_service'; export const ML_ANOMALY = 'ML_ANOMALIES'; +const CUSTOM_COLOR_RAMP = { + type: STYLE_TYPE.DYNAMIC, + options: { + customColorRamp: SEVERITY_COLOR_RAMP, + field: { + name: 'record_score', + origin: FIELD_ORIGIN.SOURCE, + }, + useCustomColorRamp: true, + }, +}; export class AnomalyLayerWizardFactory { public readonly type = ML_ANOMALY; @@ -58,7 +69,7 @@ export class AnomalyLayerWizardFactory { } const anomalyLayerDescriptor: VectorLayerDescriptor = { - id: uuid(), + id: htmlIdGenerator()(), type: LAYER_TYPE.GEOJSON_VECTOR, sourceDescriptor: AnomalySource.createDescriptor({ jobId: sourceConfig.jobId, @@ -67,17 +78,8 @@ export class AnomalyLayerWizardFactory { style: { type: 'VECTOR', properties: { - fillColor: { - type: STYLE_TYPE.DYNAMIC, - options: { - customColorRamp: SEVERITY_COLOR_RAMP, - field: { - name: 'record_score', - origin: FIELD_ORIGIN.SOURCE, - }, - useCustomColorRamp: true, - }, - }, + fillColor: CUSTOM_COLOR_RAMP, + lineColor: CUSTOM_COLOR_RAMP, } as unknown as VectorStylePropertiesDescriptor, isTimeAware: false, }, diff --git a/x-pack/plugins/ml/public/maps/anomaly_source.tsx b/x-pack/plugins/ml/public/maps/anomaly_source.tsx index 9713e53a206eff8..414d206c496c5ba 100644 --- a/x-pack/plugins/ml/public/maps/anomaly_source.tsx +++ b/x-pack/plugins/ml/public/maps/anomaly_source.tsx @@ -7,7 +7,13 @@ import { i18n } from '@kbn/i18n'; import React, { ReactElement } from 'react'; -import { FieldFormatter, MAX_ZOOM, MIN_ZOOM, VECTOR_SHAPE_TYPE } from '../../../maps/common'; +import { + FieldFormatter, + MAX_ZOOM, + MIN_ZOOM, + VECTOR_SHAPE_TYPE, + VectorSourceRequestMeta, +} from '../../../maps/common'; import { AbstractSourceDescriptor, MapExtent } from '../../../maps/common/descriptor_types'; import { ITooltipProperty } from '../../../maps/public'; import { @@ -27,15 +33,6 @@ import { getResultsForJobId, MlAnomalyLayers } from './util'; import { UpdateAnomalySourceEditor } from './update_anomaly_source_editor'; import type { MlApiServices } from '../application/services/ml_api_service'; -export type SearchFilters = any & { - applyGlobalQuery: boolean; - applyGlobalTime: boolean; - fieldNames: string[]; - geogridPrecision?: number; - sourceQuery?: object; - sourceMeta: object; -}; - export interface AnomalySourceDescriptor extends AbstractSourceDescriptor { jobId: string; typicalActual: MlAnomalyLayers; @@ -46,7 +43,7 @@ export class AnomalySource implements IVectorSource { static canGetJobs: boolean; static createDescriptor(descriptor: Partial) { - if (!(typeof descriptor.jobId === 'string')) { + if (typeof descriptor.jobId !== 'string') { throw new Error('Job id is required for anomaly layer creation'); } @@ -65,7 +62,7 @@ export class AnomalySource implements IVectorSource { // TODO: implement query awareness async getGeoJsonWithMeta( layerName: string, - searchFilters: SearchFilters, + searchFilters: VectorSourceRequestMeta, registerCancelCallback: (callback: () => void) => void, isRequestStillActive: () => boolean ): Promise { @@ -213,7 +210,6 @@ export class AnomalySource implements IVectorSource { } async getLicensedFeatures(): Promise<[]> { - // return [{ name: 'layer from ML anomaly job', license: 'enterprise' }]; return []; } diff --git a/x-pack/plugins/ml/public/maps/anomaly_source_field.ts b/x-pack/plugins/ml/public/maps/anomaly_source_field.ts index c216a9707d40eac..ccc7ee38c507b28 100644 --- a/x-pack/plugins/ml/public/maps/anomaly_source_field.ts +++ b/x-pack/plugins/ml/public/maps/anomaly_source_field.ts @@ -6,7 +6,7 @@ */ // eslint-disable-next-line max-classes-per-file -import _ from 'lodash'; +import { escape } from 'lodash'; import { IField, IVectorSource } from '../../../maps/public'; import { FIELD_ORIGIN } from '../../../maps/common'; import { TileMetaFeature } from '../../../maps/common/descriptor_types'; @@ -24,13 +24,7 @@ export const ANOMALY_SOURCE_FIELDS: Record> = { }; export class AnomalySourceTooltipProperty implements ITooltipProperty { - private readonly _label: string; - private readonly _value: string; - - constructor(label: string, value: string) { - this._label = label; - this._value = value; - } + constructor(private readonly _label: string, private readonly _value: string) {} async getESFilters(): Promise { return []; @@ -70,7 +64,7 @@ export class AnomalySourceField implements IField { async createTooltipProperty(value: string | string[] | undefined): Promise { return new AnomalySourceTooltipProperty( await this.getLabel(), - _.escape(Array.isArray(value) ? value.join() : value ? value : '') + escape(Array.isArray(value) ? value.join() : value ? value : '') ); } diff --git a/x-pack/plugins/ml/public/maps/util.ts b/x-pack/plugins/ml/public/maps/util.ts index 88fe8edf564d8a3..91a8bd547e25036 100644 --- a/x-pack/plugins/ml/public/maps/util.ts +++ b/x-pack/plugins/ml/public/maps/util.ts @@ -5,12 +5,12 @@ * 2.0. */ -import { FeatureCollection, Feature } from 'geojson'; +import { FeatureCollection, Feature, Geometry } from 'geojson'; import { ESSearchResponse } from '../../../../../src/core/types/elasticsearch'; import { formatHumanReadableDateTimeSeconds } from '../../common/util/date_utils'; import type { MlApiServices } from '../application/services/ml_api_service'; import { MLAnomalyDoc } from '../../common/types/anomalies'; -import type { SearchFilters } from './anomaly_source'; +import { VectorSourceRequestMeta } from '../../../maps/common'; export type MlAnomalyLayers = 'typical' | 'actual' | 'connected'; @@ -18,7 +18,7 @@ export async function getResultsForJobId( mlResultsService: MlApiServices['results'], jobId: string, locationType: MlAnomalyLayers, - searchFilters: SearchFilters + searchFilters: VectorSourceRequestMeta ): Promise { const { timeFilters } = searchFilters; // Query to look for the highest scoring anomaly. @@ -98,9 +98,8 @@ export async function getResultsForJobId( }); } - // @ts-ignore - const features: Feature[] = hits!.map((result) => { - let geometry; + const features: Feature[] = hits.map((result) => { + let geometry: Geometry; if (locationType === 'typical' || locationType === 'actual') { geometry = { type: 'Point', diff --git a/x-pack/plugins/ml/server/models/job_service/jobs.ts b/x-pack/plugins/ml/server/models/job_service/jobs.ts index e1318d7d43c5cee..6e4700016d83b51 100644 --- a/x-pack/plugins/ml/server/models/job_service/jobs.ts +++ b/x-pack/plugins/ml/server/models/job_service/jobs.ts @@ -272,20 +272,9 @@ export function jobsProvider( return jobs; } - async function getJobsWithGeo(): Promise { + async function getJobIdsWithGeo(): Promise { const { body } = await mlClient.getJobs(); - - const geoJobs: string[] = []; - - if (body.count && body.count > 0) { - body.jobs.forEach((job) => { - if (isJobWithGeoData(job)) { - geoJobs.push(job.job_id); - } - }); - } - - return geoJobs; + return body.jobs.filter(isJobWithGeoData).map((job) => job.job_id); } async function jobsWithTimerange() { @@ -678,6 +667,6 @@ export function jobsProvider( getAllJobAndGroupIds, getLookBackProgress, bulkCreate, - getJobsWithGeo, + getJobIdsWithGeo, }; } diff --git a/x-pack/plugins/ml/server/routes/job_service.ts b/x-pack/plugins/ml/server/routes/job_service.ts index 0b13fdee2521c34..d44cd5f91355332 100644 --- a/x-pack/plugins/ml/server/routes/job_service.ts +++ b/x-pack/plugins/ml/server/routes/job_service.ts @@ -306,13 +306,13 @@ export function jobServiceRoutes({ router, routeGuard }: RouteInitialization) { }, routeGuard.fullLicenseAPIGuard(async ({ client, mlClient, response, context }) => { try { - const { getJobsWithGeo } = jobServiceProvider( + const { getJobIdsWithGeo } = jobServiceProvider( client, mlClient, context.alerting?.getRulesClient() ); - const resp = await getJobsWithGeo(); + const resp = await getJobIdsWithGeo(); return response.ok({ body: resp,