From bd1ca55211d03b14d95d0de2a349c98acce54f87 Mon Sep 17 00:00:00 2001 From: Oliver Gupte Date: Mon, 8 Jun 2020 12:39:40 -0700 Subject: [PATCH] Code and types cleanup --- .../lib/service_map/get_service_anomalies.ts | 41 +++++++++++-------- .../server/lib/service_map/get_service_map.ts | 9 ++-- 2 files changed, 28 insertions(+), 22 deletions(-) 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 f6176ec280a08..8259081709301 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 @@ -5,7 +5,6 @@ */ import { intersection } from 'lodash'; import { leftJoin } from '../../../common/utils/left_join'; -import { rangeFilter } from '../helpers/range_filter'; import { Job as AnomalyDetectionJob } from '../../../../ml/server'; import { PromiseReturnType } from '../../../typings/common'; import { IEnvOptions } from './get_service_map'; @@ -57,7 +56,6 @@ export async function getServiceAnomalies( (apmJobCategory) => apmJobCategory !== undefined ) as ApmMlJobCategory[]; const apmJobIds = apmMlJobs.map((job) => job.job_id); - const rangeQuery = { range: rangeFilter(start, end, 'timestamp') }; const params = { body: { size: 0, @@ -70,7 +68,11 @@ export async function getServiceAnomalies( job_id: apmJobIds, }, }, - rangeQuery, + { + range: { + timestamp: { gte: start, lte: end, format: 'epoch_millis' }, + }, + }, ], }, }, @@ -91,12 +93,23 @@ export async function getServiceAnomalies( }, }; - const response = await ml.mlSystem.mlAnomalySearch(params); - const anomalyScores: Array<{ - jobId: string; - anomalyScore: number; - timestamp: number; - }> = response.aggregations.jobs.buckets.map((jobBucket: any) => { + const response = (await ml.mlSystem.mlAnomalySearch(params)) as { + aggregations: { + jobs: { + buckets: Array<{ + key: string; + top_score_hits: { + hits: { + hits: Array<{ + _source: { anomaly_score: number; timestamp: number }; + }>; + }; + }; + }>; + }; + }; + }; + const anomalyScores = response.aggregations.jobs.buckets.map((jobBucket) => { const jobId = jobBucket.key; const bucketSource = jobBucket.top_score_hits.hits.hits?.[0]?._source; return { @@ -140,9 +153,9 @@ export async function getServiceAnomalies( } ); - const anomalyModelValues = filterNonNullable( + const anomalyModelValues = ( await Promise.all(anomalyModelValuePromises) - ); + ).filter((value: T | undefined): value is T => value !== undefined); return leftJoin( apmMlJobCategories, @@ -150,9 +163,3 @@ export async function getServiceAnomalies( leftJoin(anomalyScores, 'jobId', anomalyModelValues) ); } - -function filterNonNullable(array: T[]) { - return array.filter( - (element) => element !== undefined || element !== null - ) as Array>; -} diff --git a/x-pack/plugins/apm/server/lib/service_map/get_service_map.ts b/x-pack/plugins/apm/server/lib/service_map/get_service_map.ts index 425337c6d71d0..9f3ded82d7cbd 100644 --- a/x-pack/plugins/apm/server/lib/service_map/get_service_map.ts +++ b/x-pack/plugins/apm/server/lib/service_map/get_service_map.ts @@ -138,11 +138,10 @@ export type ServicesResponse = PromiseReturnType; export type ServiceMapAPIResponse = PromiseReturnType; export async function getServiceMap(options: IEnvOptions) { - const [connectionData, servicesData]: [ - // explicit types to avoid TS "excessively deep" error - ConnectionsResponse, - ServicesResponse - ] = await Promise.all([getConnectionData(options), getServicesData(options)]); + const [connectionData, servicesData] = await Promise.all([ + getConnectionData(options), + getServicesData(options), + ]); // Derive all related service names from connection and service data const allNodes = getAllNodes(servicesData, connectionData.connections);