-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ML] Add option for anomaly charts for metric detector should plot min, mean or max as appropriate #81662
[ML] Add option for anomaly charts for metric detector should plot min, mean or max as appropriate #81662
Changes from 26 commits
7a4e9a0
26aa395
ffce815
3c8972a
5fff206
eda2c30
d1071a3
85f1526
21bb6cb
a583988
e27e433
cb2429e
9f51223
759a3f8
19e20ee
ea8d71f
97e0fc3
f694868
09fe186
12b32ad
638ed89
3c02f4f
5350e3d
a7e2482
2f028cd
595b8b3
64ecedb
df26340
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,8 +24,9 @@ export const resultsApiProvider = (httpService: HttpService) => ({ | |
latestMs: number, | ||
dateFormatTz: string, | ||
maxRecords: number, | ||
maxExamples: number, | ||
influencersFilterQuery: any | ||
maxExamples?: number, | ||
influencersFilterQuery?: any, | ||
functionDescription?: string | undefined | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should be able to remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated here 64ecedb |
||
) { | ||
const body = JSON.stringify({ | ||
jobIds, | ||
|
@@ -39,6 +40,7 @@ export const resultsApiProvider = (httpService: HttpService) => ({ | |
maxRecords, | ||
maxExamples, | ||
influencersFilterQuery, | ||
functionDescription, | ||
}); | ||
|
||
return httpService.http$<any>({ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import { ML_MEDIAN_PERCENTS } from '../../../../common/util/job_utils'; | |
import { JobId } from '../../../../common/types/anomaly_detection_jobs'; | ||
import { MlApiServices } from '../ml_api_service'; | ||
import { CriteriaField } from './index'; | ||
import { aggregationTypeTransform } from '../../../../common/util/anomaly_utils'; | ||
|
||
interface ResultResponse { | ||
success: boolean; | ||
|
@@ -347,9 +348,10 @@ export function resultsServiceRxProvider(mlApiServices: MlApiServices) { | |
jobIds: string[], | ||
criteriaFields: CriteriaField[], | ||
threshold: any, | ||
earliestMs: number, | ||
latestMs: number, | ||
maxResults: number | undefined | ||
earliestMs: number | null, | ||
latestMs: number | null, | ||
maxResults: number | undefined, | ||
functionDescription?: string | undefined | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As noted above, you should be able to remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated here 64ecedb |
||
): Observable<RecordsForCriteria> { | ||
const obj: RecordsForCriteria = { success: true, records: [] }; | ||
|
||
|
@@ -400,6 +402,19 @@ export function resultsServiceRxProvider(mlApiServices: MlApiServices) { | |
}); | ||
}); | ||
|
||
if (functionDescription !== undefined) { | ||
const mlFunctionToPlotIfMetric = | ||
functionDescription !== undefined | ||
? aggregationTypeTransform.toML(functionDescription) | ||
: functionDescription; | ||
|
||
boolCriteria.push({ | ||
term: { | ||
function_description: mlFunctionToPlotIfMetric, | ||
}, | ||
}); | ||
} | ||
|
||
return mlApiServices.results | ||
.anomalySearch$( | ||
{ | ||
|
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'; |
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 = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it necessary to translate these since they relate to Elasticsearch aggregations? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since these are not strict terms that are excluded in the translation guide, I'll keep these translations here. |
||
{ | ||
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> | ||
); | ||
}; |
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, | ||
]; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* 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'; | ||
|
||
/** | ||
* 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 !== 'metric') return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated here 64ecedb |
||
|
||
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', | ||
}) | ||
); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
ML_JOB_AGGREGATION.METRIC
fromcommon/constants/aggregation_types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated here 64ecedb