Skip to content

Commit

Permalink
[ML] Fix Anomaly Explorer & Single Metric Viewer not plotting correct…
Browse files Browse the repository at this point in the history
…ly for detectors with non zero count function (#87903)
  • Loading branch information
qn895 authored Jan 13, 2021
1 parent badd2b3 commit 6e3a06b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export const anomalyDataChange = function (
datafeedQuery,
config.metricFunction,
config.metricFieldName,
config.summaryCountFieldName,
config.timeField,
range.min,
range.max,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { CriteriaField } from './index';
import { findAggField } from '../../../../common/util/validation_utils';
import { getDatafeedAggregations } from '../../../../common/util/datafeed_utils';
import { aggregationTypeTransform } from '../../../../common/util/anomaly_utils';
import { ES_AGGREGATION } from '../../../../common/constants/aggregation_types';

interface ResultResponse {
success: boolean;
Expand Down Expand Up @@ -68,6 +69,7 @@ export function resultsServiceRxProvider(mlApiServices: MlApiServices) {
query: object | undefined,
metricFunction: string, // ES aggregation name
metricFieldName: string,
summaryCountFieldName: string | undefined,
timeFieldName: string,
earliestMs: number,
latestMs: number,
Expand Down Expand Up @@ -153,9 +155,8 @@ export function resultsServiceRxProvider(mlApiServices: MlApiServices) {
body.query.bool.minimum_should_match = shouldCriteria.length / 2;
}

body.aggs.byTime.aggs = {};
if (metricFieldName !== undefined && metricFieldName !== '') {
body.aggs.byTime.aggs = {};

const metricAgg: any = {
[metricFunction]: {},
};
Expand Down Expand Up @@ -186,8 +187,23 @@ export function resultsServiceRxProvider(mlApiServices: MlApiServices) {
} else {
body.aggs.byTime.aggs.metric = metricAgg;
}
} else {
// if metricFieldName is not defined, it's probably a variation of the non zero count function
// refer to buildConfigFromDetector
if (summaryCountFieldName !== undefined && metricFunction === ES_AGGREGATION.CARDINALITY) {
// if so, check if summaryCountFieldName is an aggregation field
if (typeof aggFields === 'object' && Object.keys(aggFields).length > 0) {
// first item under aggregations can be any name, not necessarily 'buckets'
const accessor = Object.keys(aggFields)[0];
const tempAggs = { ...(aggFields[accessor].aggs ?? aggFields[accessor].aggregations) };
const foundCardinalityField = findAggField(tempAggs, summaryCountFieldName);
if (foundCardinalityField !== undefined) {
tempAggs.metric = foundCardinalityField;
}
body.aggs.byTime.aggs = tempAggs;
}
}
}

return mlApiServices.esSearch$({ index, body }).pipe(
map((resp: any) => {
const obj: MetricData = { success: true, results: {} };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ function getMetricData(
chartConfig.datafeedConfig.query,
esMetricFunction ?? chartConfig.metricFunction,
chartConfig.metricFieldName,
chartConfig.summaryCountFieldName,
chartConfig.timeField,
earliestMs,
latestMs,
Expand Down
14 changes: 11 additions & 3 deletions x-pack/plugins/ml/public/application/util/chart_config_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function buildConfigFromDetector(job, detectorIndex) {
timeField: job.data_description.time_field,
interval: job.analysis_config.bucket_span,
datafeedConfig: job.datafeed_config,
summaryCountFieldName: job.analysis_config.summary_count_field_name,
};

if (detector.field_name !== undefined) {
Expand Down Expand Up @@ -63,10 +64,17 @@ export function buildConfigFromDetector(job, detectorIndex) {
'field',
]);
}

if (detector.function === ML_JOB_AGGREGATION.NON_ZERO_COUNT && cardinalityField !== undefined) {
if (
(detector.function === ML_JOB_AGGREGATION.NON_ZERO_COUNT ||
detector.function === ML_JOB_AGGREGATION.LOW_NON_ZERO_COUNT ||
detector.function === ML_JOB_AGGREGATION.HIGH_NON_ZERO_COUNT ||
detector.function === ML_JOB_AGGREGATION.COUNT ||
detector.function === ML_JOB_AGGREGATION.HIGH_COUNT ||
detector.function === ML_JOB_AGGREGATION.LOW_COUNT) &&
cardinalityField !== undefined
) {
config.metricFunction = ES_AGGREGATION.CARDINALITY;
config.metricFieldName = cardinalityField;
config.metricFieldName = undefined;
} else {
// For count detectors using summary_count_field, plot sum(summary_count_field_name)
config.metricFunction = ES_AGGREGATION.SUM;
Expand Down

0 comments on commit 6e3a06b

Please sign in to comment.