55 */
66import { intersection } from 'lodash' ;
77import { leftJoin } from '../../../common/utils/left_join' ;
8- import { rangeFilter } from '../helpers/range_filter' ;
98import { Job as AnomalyDetectionJob } from '../../../../ml/server' ;
109import { PromiseReturnType } from '../../../typings/common' ;
1110import { IEnvOptions } from './get_service_map' ;
@@ -57,7 +56,6 @@ export async function getServiceAnomalies(
5756 ( apmJobCategory ) => apmJobCategory !== undefined
5857 ) as ApmMlJobCategory [ ] ;
5958 const apmJobIds = apmMlJobs . map ( ( job ) => job . job_id ) ;
60- const rangeQuery = { range : rangeFilter ( start , end , 'timestamp' ) } ;
6159 const params = {
6260 body : {
6361 size : 0 ,
@@ -70,7 +68,11 @@ export async function getServiceAnomalies(
7068 job_id : apmJobIds ,
7169 } ,
7270 } ,
73- rangeQuery ,
71+ {
72+ range : {
73+ timestamp : { gte : start , lte : end , format : 'epoch_millis' } ,
74+ } ,
75+ } ,
7476 ] ,
7577 } ,
7678 } ,
@@ -91,12 +93,23 @@ export async function getServiceAnomalies(
9193 } ,
9294 } ;
9395
94- const response = await ml . mlSystem . mlAnomalySearch ( params ) ;
95- const anomalyScores : Array < {
96- jobId : string ;
97- anomalyScore : number ;
98- timestamp : number ;
99- } > = response . aggregations . jobs . buckets . map ( ( jobBucket : any ) => {
96+ const response = ( await ml . mlSystem . mlAnomalySearch ( params ) ) as {
97+ aggregations : {
98+ jobs : {
99+ buckets : Array < {
100+ key : string ;
101+ top_score_hits : {
102+ hits : {
103+ hits : Array < {
104+ _source : { anomaly_score : number ; timestamp : number } ;
105+ } > ;
106+ } ;
107+ } ;
108+ } > ;
109+ } ;
110+ } ;
111+ } ;
112+ const anomalyScores = response . aggregations . jobs . buckets . map ( ( jobBucket ) => {
100113 const jobId = jobBucket . key ;
101114 const bucketSource = jobBucket . top_score_hits . hits . hits ?. [ 0 ] ?. _source ;
102115 return {
@@ -140,19 +153,13 @@ export async function getServiceAnomalies(
140153 }
141154 ) ;
142155
143- const anomalyModelValues = filterNonNullable (
156+ const anomalyModelValues = (
144157 await Promise . all ( anomalyModelValuePromises )
145- ) ;
158+ ) . filter ( < T > ( value : T | undefined ) : value is T => value !== undefined ) ;
146159
147160 return leftJoin (
148161 apmMlJobCategories ,
149162 'jobId' ,
150163 leftJoin ( anomalyScores , 'jobId' , anomalyModelValues )
151164 ) ;
152165}
153-
154- function filterNonNullable < T > ( array : T [ ] ) {
155- return array . filter (
156- ( element ) => element !== undefined || element !== null
157- ) as Array < NonNullable < T > > ;
158- }
0 commit comments