-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[7.x] [ML] Add option for anomaly charts for metric detector should p…
- Loading branch information
Showing
21 changed files
with
333 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...ugins/ml/public/application/timeseriesexplorer/components/plot_function_controls/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
export { PlotByFunctionControls } from './plot_function_controls'; |
56 changes: 56 additions & 0 deletions
56
...plication/timeseriesexplorer/components/plot_function_controls/plot_function_controls.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import React from 'react'; | ||
import { EuiFlexItem, EuiFormRow, EuiSelect } from '@elastic/eui'; | ||
import { i18n } from '@kbn/i18n'; | ||
|
||
const plotByFunctionOptions = [ | ||
{ | ||
value: 'mean', | ||
text: i18n.translate('xpack.ml.timeSeriesExplorer.plotByAvgOptionLabel', { | ||
defaultMessage: 'mean', | ||
}), | ||
}, | ||
{ | ||
value: 'min', | ||
text: i18n.translate('xpack.ml.timeSeriesExplorer.plotByMinOptionLabel', { | ||
defaultMessage: 'min', | ||
}), | ||
}, | ||
{ | ||
value: 'max', | ||
text: i18n.translate('xpack.ml.timeSeriesExplorer.plotByMaxOptionLabel', { | ||
defaultMessage: 'max', | ||
}), | ||
}, | ||
]; | ||
export const PlotByFunctionControls = ({ | ||
functionDescription, | ||
setFunctionDescription, | ||
}: { | ||
functionDescription: undefined | string; | ||
setFunctionDescription: (func: string) => void; | ||
}) => { | ||
if (functionDescription === undefined) return null; | ||
return ( | ||
<EuiFlexItem grow={false}> | ||
<EuiFormRow | ||
label={i18n.translate('xpack.ml.timeSeriesExplorer.metricPlotByOption', { | ||
defaultMessage: 'Function', | ||
})} | ||
> | ||
<EuiSelect | ||
options={plotByFunctionOptions} | ||
value={functionDescription} | ||
onChange={(e) => setFunctionDescription(e.target.value)} | ||
aria-label={i18n.translate('xpack.ml.timeSeriesExplorer.metricPlotByOptionLabel', { | ||
defaultMessage: 'Pick function to plot by (min, max, or average) if metric function', | ||
})} | ||
/> | ||
</EuiFormRow> | ||
</EuiFlexItem> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
x-pack/plugins/ml/public/application/timeseriesexplorer/get_criteria_fields.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
/** | ||
* Updates criteria fields for API calls, e.g. getAnomaliesTableData | ||
* @param detectorIndex | ||
* @param entities | ||
*/ | ||
export const getCriteriaFields = (detectorIndex: number, entities: Record<string, any>) => { | ||
// Only filter on the entity if the field has a value. | ||
const nonBlankEntities = entities.filter( | ||
(entity: { fieldValue: any }) => entity.fieldValue !== null | ||
); | ||
return [ | ||
{ | ||
fieldName: 'detector_index', | ||
fieldValue: detectorIndex, | ||
}, | ||
...nonBlankEntities, | ||
]; | ||
}; |
62 changes: 62 additions & 0 deletions
62
x-pack/plugins/ml/public/application/timeseriesexplorer/get_function_description.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
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 { ML_JOB_AGGREGATION } from '../../../common/constants/aggregation_types'; | ||
|
||
/** | ||
* Get the function description from the record with the highest anomaly score | ||
*/ | ||
export const getFunctionDescription = async ( | ||
{ | ||
selectedDetectorIndex, | ||
selectedEntities, | ||
selectedJobId, | ||
selectedJob, | ||
}: { | ||
selectedDetectorIndex: number; | ||
selectedEntities: Record<string, any>; | ||
selectedJobId: string; | ||
selectedJob: CombinedJob; | ||
}, | ||
toastNotificationService: ToastNotificationService | ||
) => { | ||
// if the detector's function is metric, fetch the highest scoring anomaly record | ||
// and set to plot the function_description (avg/min/max) of that record by default | ||
if ( | ||
selectedJob?.analysis_config?.detectors[selectedDetectorIndex]?.function !== | ||
ML_JOB_AGGREGATION.METRIC | ||
) | ||
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; | ||
} | ||
} catch (error) { | ||
toastNotificationService.displayErrorToast( | ||
error, | ||
i18n.translate('xpack.ml.timeSeriesExplorer.highestAnomalyScoreErrorToastTitle', { | ||
defaultMessage: 'An error occurred getting record with the highest anomaly score', | ||
}) | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.