From 08b94cfe9f947f40dbdf43afe053b47d630153ea Mon Sep 17 00:00:00 2001 From: Oliver Gupte Date: Mon, 8 Jun 2020 08:55:36 -0700 Subject: [PATCH] - Use the anomalyDetectorsProvider jobs API rather than the search endpoint directly - Defines a specific return type for the ml jobs api - Update the empty anomaly data message --- .../components/app/ServiceMap/Popover/Contents.tsx | 3 +-- .../apm/server/lib/helpers/setup_request.ts | 14 ++------------ .../lib/service_map/get_service_anomalies.ts | 6 +++--- x-pack/plugins/ml/server/shared.ts | 2 +- .../shared_services/providers/anomaly_detectors.ts | 3 ++- 5 files changed, 9 insertions(+), 19 deletions(-) diff --git a/x-pack/plugins/apm/public/components/app/ServiceMap/Popover/Contents.tsx b/x-pack/plugins/apm/public/components/app/ServiceMap/Popover/Contents.tsx index f17e78914a1c9..259022bbee84e 100644 --- a/x-pack/plugins/apm/public/components/app/ServiceMap/Popover/Contents.tsx +++ b/x-pack/plugins/apm/public/components/app/ServiceMap/Popover/Contents.tsx @@ -119,8 +119,7 @@ const ANOMALY_DETECTION_DISABLED_TEXT = i18n.translate( const ANOMALY_DETECTION_NO_DATA_TEXT = i18n.translate( 'xpack.apm.serviceMap.anomalyDetectionPopoverNoData', { - defaultMessage: - 'No anomaly score found with the current filters. See details in the anomaly explorer:', + defaultMessage: `We couldn't find an anomaly score within the selected time range. See details in the anomaly explorer.`, } ); diff --git a/x-pack/plugins/apm/server/lib/helpers/setup_request.ts b/x-pack/plugins/apm/server/lib/helpers/setup_request.ts index afcf1265f2960..c41dff79a916a 100644 --- a/x-pack/plugins/apm/server/lib/helpers/setup_request.ts +++ b/x-pack/plugins/apm/server/lib/helpers/setup_request.ts @@ -19,7 +19,6 @@ import { APMRequestHandlerContext } from '../../routes/typings'; import { getESClient } from './es_client'; import { ProcessorEvent } from '../../../common/processor_event'; import { getDynamicIndexPattern } from '../index_pattern/get_dynamic_index_pattern'; -import { Job as AnomalyDetectionJob } from '../../../../ml/server'; function decodeUiFilters( indexPattern: IIndexPattern | undefined, @@ -115,17 +114,8 @@ function getMlSetup(context: APMRequestHandlerContext, request: KibanaRequest) { const ml = context.plugins.ml; const mlClient = ml.mlClient.asScoped(request).callAsCurrentUser; return { - ...ml.mlSystemProvider(mlClient, request), + mlSystem: ml.mlSystemProvider(mlClient, request), + anomalyDetectors: ml.anomalyDetectorsProvider(mlClient), mlClient, - /** - * https://www.elastic.co/guide/en/elasticsearch/reference/7.x/ml-get-job.html#ml-get-job-desc - * @param {string | string[]} [jobId] - job id, group name, or a wildcard, returns all jobs if nothing passed in - */ - mlJobs: async ( - jobId?: string | string[] - ): Promise => { - const mlJobsResponse = await mlClient('ml.jobs', { jobId }); - return mlJobsResponse.jobs; - }, }; } diff --git a/x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.ts b/x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.ts index 2a13606babfb5..f6176ec280a08 100644 --- a/x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.ts +++ b/x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.ts @@ -50,7 +50,7 @@ export async function getServiceAnomalies( return []; } - const apmMlJobs = await ml.mlJobs('apm'); + const { jobs: apmMlJobs } = await ml.anomalyDetectors.jobs('apm'); const apmMlJobCategories = apmMlJobs .map((job) => getApmMlJobCategory(job, serviceNames)) .filter( @@ -91,7 +91,7 @@ export async function getServiceAnomalies( }, }; - const response = await ml.mlAnomalySearch(params); + const response = await ml.mlSystem.mlAnomalySearch(params); const anomalyScores: Array<{ jobId: string; anomalyScore: number; @@ -109,7 +109,7 @@ export async function getServiceAnomalies( ({ jobId, timestamp }) => { return (async () => { try { - const modelPlotResponse = await ml.mlAnomalySearch({ + const modelPlotResponse = await ml.mlSystem.mlAnomalySearch({ body: { size: 1, query: { diff --git a/x-pack/plugins/ml/server/shared.ts b/x-pack/plugins/ml/server/shared.ts index 1b04a0802fae6..be27ee2d44a82 100644 --- a/x-pack/plugins/ml/server/shared.ts +++ b/x-pack/plugins/ml/server/shared.ts @@ -5,4 +5,4 @@ */ export * from '../common/types/anomalies'; -export { Job } from '../common/types/anomaly_detection_jobs'; +export * from '../common/types/anomaly_detection_jobs'; diff --git a/x-pack/plugins/ml/server/shared_services/providers/anomaly_detectors.ts b/x-pack/plugins/ml/server/shared_services/providers/anomaly_detectors.ts index 73696dfdeef86..880aebfde409c 100644 --- a/x-pack/plugins/ml/server/shared_services/providers/anomaly_detectors.ts +++ b/x-pack/plugins/ml/server/shared_services/providers/anomaly_detectors.ts @@ -6,12 +6,13 @@ import { APICaller } from 'kibana/server'; import { LicenseCheck } from '../license_checks'; +import { Job } from '../../../common/types/anomaly_detection_jobs'; export interface AnomalyDetectorsProvider { anomalyDetectorsProvider( callAsCurrentUser: APICaller ): { - jobs(jobId?: string): Promise; + jobs(jobId?: string): Promise<{ count: number; jobs: Job[] }>; }; }