import { i18n } from '@kbn/i18n' ; import { mlResultsService } from '../services/results_service' ; import { ToastNotificationService } from '../services/toast_notification_service' ; import { getControlsForDetector } from './get_controls_for_detector' ; import { getCriteriaFields } from './get_criteria_fields' ; import { CombinedJob } from '../../../common/types/anomaly_detection_jobs' ; import { ES_AGGREGATION , ML_JOB_AGGREGATION } from '../../../common/constants/aggregation_types' ; import { getViewableDetectors } from './timeseriesexplorer_utils/get_viewable_detectors' ; export function isMetricDetector ( selectedJob , selectedDetectorIndex ) { const detectors = getViewableDetectors ( selectedJob ) ; if ( Array . isArray ( detectors ) && detectors . length >= selectedDetectorIndex ) { const detector = selectedJob . analysis_config . detectors [ selectedDetectorIndex ] ; if ( detector ?. function === ML_JOB_AGGREGATION . METRIC ) { return true ; } } return false ; } /**
 * Get the function description from the record with the highest anomaly score
 */ export const getFunctionDescription = async ( { selectedDetectorIndex , selectedEntities , selectedJobId , selectedJob , } , toastNotificationService ) => { if ( ! isMetricDetector ( selectedJob , selectedDetectorIndex ) ) return ; const entityControls = getControlsForDetector ( selectedDetectorIndex , selectedEntities , selectedJobId ) ; const criteriaFields = getCriteriaFields ( selectedDetectorIndex , entityControls ) ; try { const resp = await mlResultsService . getRecordsForCriteria ( [ selectedJob . job_id ] , criteriaFields , 0 , null , null , 1 ) . toPromise ( ) ; if ( Array . isArray ( resp ?. records ) && resp . records . length === 1 ) { const highestScoringAnomaly = resp . records [ 0 ] ; return highestScoringAnomaly ?. function_description ; } return ES_AGGREGATION . AVG ; } catch ( error ) { toastNotificationService . displayErrorToast ( error , i18n . translate ( 'xpack.ml.timeSeriesExplorer.highestAnomalyScoreErrorToastTitle' , { defaultMessage : 'An error occurred getting record with the highest anomaly score' , } ) ) ; } } ;