Skip to content
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

Merged
merged 28 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7a4e9a0
[ML] Add extra function info to marker tooltip if metric function
qn895 Oct 22, 2020
26aa395
[ML] Add dropdown to pick what function to plot by if metric
qn895 Oct 26, 2020
ffce815
[ML] Plot function associated with highest anomaly score by default
qn895 Oct 26, 2020
3c8972a
[ML] Clean up
qn895 Oct 26, 2020
5fff206
Merge remote-tracking branch 'upstream/master' into ml-metric-plotting
qn895 Oct 28, 2020
eda2c30
[ML] Add filter to table
qn895 Oct 28, 2020
d1071a3
[ML] Metric mini swimlane
qn895 Oct 29, 2020
85f1526
Merge remote upstream/master into ml-metric-plotting
qn895 Nov 2, 2020
21bb6cb
[ML] Refactor
qn895 Nov 2, 2020
a583988
[ML] Delete residual merge code
qn895 Nov 3, 2020
e27e433
Merge remote-tracking branch 'upstream/master' into ml-metric-plotting
qn895 Nov 3, 2020
cb2429e
[ML] Rename actualPlotFunction, fix mean and avg name mismatch
qn895 Nov 3, 2020
9f51223
[ML] Have dropdown use mean instead of avg to match terminology
qn895 Nov 3, 2020
759a3f8
Merge remote-tracking branch 'upstream/master' into ml-metric-plotting
qn895 Nov 3, 2020
19e20ee
[ML] Refactor getFunctionDescription
qn895 Nov 3, 2020
ea8d71f
[ML] Fix anomaly point plotting incorrectly for metric partitioned
qn895 Nov 3, 2020
97e0fc3
Merge remote-tracking branch 'upstream/master' into ml-metric-plotting
qn895 Nov 3, 2020
f694868
[ML] Fix typescript
qn895 Nov 3, 2020
09fe186
[ML] Remove metricActualPlotFunction because we no longer need it
qn895 Nov 3, 2020
12b32ad
Merge remote-tracking branch 'upstream/master' into ml-metric-plotting
qn895 Nov 4, 2020
638ed89
[ML] Fix bounds issue caused by value in form of array
qn895 Nov 4, 2020
3c02f4f
[ML] Fix anomalies not showing up in mini swimlane
qn895 Nov 4, 2020
5350e3d
[ML] Use toastNotificationService from kibana context
qn895 Nov 4, 2020
a7e2482
[ML] Clean up
qn895 Nov 4, 2020
2f028cd
Merge upstream/master into ml-metric-plotting
qn895 Nov 4, 2020
595b8b3
[ML] Minor fixes & clean up
qn895 Nov 4, 2020
64ecedb
[ML] Use constant metric, revert code removal, update function descri…
qn895 Nov 9, 2020
df26340
Merge remote-tracking branch 'upstream/master' into ml-metric-plotting
qn895 Nov 9, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { parseInterval } from '../../../../common/util/parse_interval';
import { getEntityFieldList } from '../../../../common/util/anomaly_utils';
import { buildConfigFromDetector } from '../../util/chart_config_builder';
import { mlJobService } from '../../services/job_service';
import { mlFunctionToESAggregation } from '../../../../common/util/job_utils';

// Builds the chart configuration for the provided anomaly record, returning
// an object with properties used for the display (series function and field, aggregation interval etc),
Expand Down Expand Up @@ -48,6 +49,10 @@ export function buildConfig(record) {
// define the metric series to be plotted.
config.entityFields = getEntityFieldList(record);

if (record.function === 'metric') {
Copy link
Contributor

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 from common/constants/aggregation_types

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated here 64ecedb

config.metricFunction = mlFunctionToESAggregation(record.function_description);
}

// Build the tooltip data for the chart info icon, showing further details on what is being plotted.
let functionLabel = config.metricFunction;
if (config.metricFieldName !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,22 @@ export class ExplorerChartSingleMetric extends React.Component {
});
}

if (marker.metricActualPlotFunction !== undefined) {
tooltipData.push({
label: i18n.translate(
'xpack.ml.explorer.singleMetricChart.metricActualPlotFunctionLabel',
{
defaultMessage: 'function',
}
),
value: marker.metricActualPlotFunction,
seriesIdentifier: {
key: seriesKey,
},
valueAccessor: 'metricActualPlotFunction',
});
}

tooltipService.show(tooltipData, circle, {
x: LINE_CHART_ANOMALY_RADIUS * 3,
y: LINE_CHART_ANOMALY_RADIUS * 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ const ML_TIME_FIELD_NAME = 'timestamp';
const USE_OVERALL_CHART_LIMITS = false;
const MAX_CHARTS_PER_ROW = 4;

// callback(getDefaultChartsData());

export const anomalyDataChange = function (
chartsContainerWidth,
anomalyRecords,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ export const resultsApiProvider = (httpService: HttpService) => ({
latestMs: number,
dateFormatTz: string,
maxRecords: number,
maxExamples: number,
influencersFilterQuery: any
maxExamples?: number,
influencersFilterQuery?: any,
actualPlotFunction?: string | undefined
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be renamed to functionDescription to match the name of the property in the records?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated here cb2429e

) {
const body = JSON.stringify({
jobIds,
Expand All @@ -39,6 +40,7 @@ export const resultsApiProvider = (httpService: HttpService) => ({
maxRecords,
maxExamples,
influencersFilterQuery,
actualPlotFunction,
});

return httpService.http$<any>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export function resultsServiceProvider(
criteriaFields: any[],
earliestMs: number,
latestMs: number,
intervalMs: number
intervalMs: number,
actualPlotFunctionIfMetric?: string
): Promise<any>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,14 @@ export function resultsServiceProvider(mlApiServices) {
// criteria, time range, and aggregation interval.
// criteriaFields parameter must be an array, with each object in the array having 'fieldName'
// 'fieldValue' properties.
getRecordMaxScoreByTime(jobId, criteriaFields, earliestMs, latestMs, intervalMs) {
getRecordMaxScoreByTime(
jobId,
criteriaFields,
earliestMs,
latestMs,
intervalMs,
actualPlotFunctionIfMetric
) {
return new Promise((resolve, reject) => {
const obj = {
success: true,
Expand Down Expand Up @@ -1300,6 +1307,14 @@ export function resultsServiceProvider(mlApiServices) {
},
});
});
if (actualPlotFunctionIfMetric !== undefined && actualPlotFunctionIfMetric !== 'unknown') {
mustCriteria.push({
term: {
function_description:
actualPlotFunctionIfMetric === 'avg' ? 'mean' : actualPlotFunctionIfMetric,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use the enums out of common/constants/aggregation_types here?

},
});
}

mlApiServices.results
.anomalySearch({
Expand Down
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,54 @@
/*
* 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 = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to translate these since they relate to Elasticsearch aggregations?

Copy link
Member Author

Choose a reason for hiding this comment

The 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: 'avg',
text: i18n.translate('xpack.ml.timeSeriesExplorer.plotByAvgOptionLabel', {
defaultMessage: 'average',
}),
},
{
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 = ({
actualPlotFunction,
setPlotByFunction,
}: {
actualPlotFunction: undefined | string;
setPlotByFunction: (func: string) => void;
}) => {
if (actualPlotFunction === 'unknown' || !actualPlotFunction) return null;
return (
<EuiFlexItem style={{ textAlign: 'right' }} grow={false}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to avoid inline styles, you can use EuiText with textAlign prop

Copy link
Member Author

@qn895 qn895 Nov 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized this style wasn't necessary so I removed it here a7e2482

<EuiFormRow
label={i18n.translate('xpack.ml.timeSeriesExplorer.metricPlotByOption', {
defaultMessage: 'Function',
})}
>
<EuiSelect
options={plotByFunctionOptions}
value={actualPlotFunction ?? 'avg'}
onChange={(e) => setPlotByFunction(e.target.value)}
aria-label="Pick function to plot by (min, max, or average) if metric function"
darnautov marked this conversation as resolved.
Show resolved Hide resolved
/>
</EuiFormRow>
</EuiFlexItem>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,22 @@ class TimeseriesChartIntl extends Component {
});
}

if (marker.metricFunction) {
tooltipData.push({
label: i18n.translate(
'xpack.ml.timeSeriesExplorer.timeSeriesChart.metricActualPlotFunctionLabel',
{
defaultMessage: 'function',
}
),
value: marker.metricFunction,
seriesIdentifier: {
key: seriesKey,
},
valueAccessor: 'metric_function',
});
}

if (modelPlotEnabled === false) {
// Show actual/typical when available except for rare detectors.
// Rare detectors always have 1 as actual and the probability as typical.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 { mlJobService } from '../services/job_service';
export const UNKNOWN_METRIC_PLOT_FUNCTION = 'unknown';

export const getPlotByOptions = (
selectedJobId: string,
selectedDetectorIndex: number,
currentActualPlotFunction: string | undefined,
defaultFunctionToPlotIfMetric: string | undefined
) => {
// only load the options to view chart by 'avg', 'min', or 'max'
// if the original function is 'metric'
const selectedJob = mlJobService.getJob(selectedJobId);

if (selectedJob === undefined) return;

const detectors = selectedJob.analysis_config.detectors;

if (detectors && detectors[selectedDetectorIndex]?.function) {
if (detectors[selectedDetectorIndex]?.function === 'metric') {
if (currentActualPlotFunction === undefined) {
// here we just know the detector is a metric function, but we don't know what to plot yet
// need to find the highest scoring anomaly record in order to pick the default view
return defaultFunctionToPlotIfMetric || UNKNOWN_METRIC_PLOT_FUNCTION;
}
} else {
return undefined;
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ function getMetricData(
entityFields: EntityField[],
earliestMs: number,
latestMs: number,
intervalMs: number
intervalMs: number,
esMetricFunction?: string
): Observable<ModelPlotOutput> {
if (
isModelPlotChartableForDetector(job, detectorIndex) &&
Expand Down Expand Up @@ -88,7 +89,7 @@ function getMetricData(
chartConfig.datafeedConfig.indices,
entityFields,
chartConfig.datafeedConfig.query,
chartConfig.metricFunction,
esMetricFunction ?? chartConfig.metricFunction,
chartConfig.metricFieldName,
chartConfig.timeField,
earliestMs,
Expand Down
Loading