Skip to content

Commit 32c99db

Browse files
[ML] Fix Anomaly Explorer charts time range to obey time picker range (#80317) (#80464)
* [ML] Fix Anomaly Explorer charts time range to obey time picker range * [ML] Fix explorer_charts_container_service Jest tests
1 parent dc7558d commit 32c99db

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

x-pack/plugins/ml/public/application/explorer/explorer_charts/explorer_charts_container_service.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { get, each, find, sortBy, map, reduce } from 'lodash';
1515

1616
import { buildConfig } from './explorer_chart_config_builder';
1717
import { chartLimits, getChartType } from '../../util/chart_utils';
18+
import { getTimefilter } from '../../util/dependency_cache';
1819

1920
import { getEntityFieldList } from '../../../../common/util/anomaly_utils';
2021
import {
@@ -50,8 +51,8 @@ const MAX_CHARTS_PER_ROW = 4;
5051
export const anomalyDataChange = function (
5152
chartsContainerWidth,
5253
anomalyRecords,
53-
earliestMs,
54-
latestMs,
54+
selectedEarliestMs,
55+
selectedLatestMs,
5556
severity = 0
5657
) {
5758
const data = getDefaultChartsData();
@@ -83,8 +84,8 @@ export const anomalyDataChange = function (
8384
const chartWidth = Math.floor(chartsContainerWidth / chartsPerRow);
8485
const { chartRange, tooManyBuckets } = calculateChartRange(
8586
seriesConfigs,
86-
earliestMs,
87-
latestMs,
87+
selectedEarliestMs,
88+
selectedLatestMs,
8889
chartWidth,
8990
recordsToPlot,
9091
data.timeFieldName
@@ -408,8 +409,8 @@ export const anomalyDataChange = function (
408409
chartData: processedData[i],
409410
plotEarliest: chartRange.min,
410411
plotLatest: chartRange.max,
411-
selectedEarliest: earliestMs,
412-
selectedLatest: latestMs,
412+
selectedEarliest: selectedEarliestMs,
413+
selectedLatest: selectedLatestMs,
413414
chartLimits: USE_OVERALL_CHART_LIMITS ? overallChartLimits : chartLimits(processedData[i]),
414415
}));
415416
explorerService.setCharts({ ...data });
@@ -561,19 +562,21 @@ function processRecordsForDisplay(anomalyRecords) {
561562

562563
function calculateChartRange(
563564
seriesConfigs,
564-
earliestMs,
565-
latestMs,
565+
selectedEarliestMs,
566+
selectedLatestMs,
566567
chartWidth,
567568
recordsToPlot,
568569
timeFieldName
569570
) {
570571
let tooManyBuckets = false;
571572
// Calculate the time range for the charts.
572573
// Fit in as many points in the available container width plotted at the job bucket span.
573-
const midpointMs = Math.ceil((earliestMs + latestMs) / 2);
574+
const midpointMs = Math.ceil((selectedEarliestMs + selectedLatestMs) / 2);
574575
const maxBucketSpanMs = Math.max.apply(null, map(seriesConfigs, 'bucketSpanSeconds')) * 1000;
575576

576-
const pointsToPlotFullSelection = Math.ceil((latestMs - earliestMs) / maxBucketSpanMs);
577+
const pointsToPlotFullSelection = Math.ceil(
578+
(selectedLatestMs - selectedEarliestMs) / maxBucketSpanMs
579+
);
577580

578581
// Optimally space points 5px apart.
579582
const optimumPointSpacing = 5;
@@ -583,9 +586,12 @@ function calculateChartRange(
583586
// at optimal point spacing.
584587
const plotPoints = Math.max(optimumNumPoints, pointsToPlotFullSelection);
585588
const halfPoints = Math.ceil(plotPoints / 2);
589+
const timefilter = getTimefilter();
590+
const bounds = timefilter.getActiveBounds();
591+
586592
let chartRange = {
587-
min: midpointMs - halfPoints * maxBucketSpanMs,
588-
max: midpointMs + halfPoints * maxBucketSpanMs,
593+
min: Math.max(midpointMs - halfPoints * maxBucketSpanMs, bounds.min.valueOf()),
594+
max: Math.min(midpointMs + halfPoints * maxBucketSpanMs, bounds.max.valueOf()),
589595
};
590596

591597
if (plotPoints > CHART_MAX_POINTS) {
@@ -615,8 +621,8 @@ function calculateChartRange(
615621

616622
if (maxMs - minMs < maxTimeSpan) {
617623
// Expand out to cover as much as the requested time span as possible.
618-
minMs = Math.max(earliestMs, minMs - maxTimeSpan);
619-
maxMs = Math.min(latestMs, maxMs + maxTimeSpan);
624+
minMs = Math.max(selectedEarliestMs, minMs - maxTimeSpan);
625+
maxMs = Math.min(selectedLatestMs, maxMs + maxTimeSpan);
620626
}
621627

622628
chartRange = { min: minMs, max: maxMs };

x-pack/plugins/ml/public/application/explorer/explorer_charts/explorer_charts_container_service.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import mockSeriesPromisesResponse from './__mocks__/mock_series_promises_respons
1515
//
1616
// 'call anomalyChangeListener with actual series config'
1717
// This test uses the standard mocks and uses the data as is provided via the mock files.
18-
// The mocked services check for values in the data (e.g. 'mock-job-id', 'farequore-2017')
18+
// The mocked services check for values in the data (e.g. 'mock-job-id', 'farequote-2017')
1919
// and return the mock data from the files.
2020
//
2121
// 'filtering should skip values of null'
@@ -88,14 +88,41 @@ jest.mock('../../util/string_utils', () => ({
8888
},
8989
}));
9090

91+
jest.mock('../../util/dependency_cache', () => {
92+
const dateMath = require('@elastic/datemath');
93+
let _time = undefined;
94+
const timefilter = {
95+
setTime: (time) => {
96+
_time = time;
97+
},
98+
getActiveBounds: () => {
99+
return {
100+
min: dateMath.parse(_time.from),
101+
max: dateMath.parse(_time.to),
102+
};
103+
},
104+
};
105+
return {
106+
getTimefilter: () => timefilter,
107+
};
108+
});
109+
91110
jest.mock('../explorer_dashboard_service', () => ({
92111
explorerService: {
93112
setCharts: jest.fn(),
94113
},
95114
}));
96115

116+
import moment from 'moment';
97117
import { anomalyDataChange, getDefaultChartsData } from './explorer_charts_container_service';
98118
import { explorerService } from '../explorer_dashboard_service';
119+
import { getTimefilter } from '../../util/dependency_cache';
120+
121+
const timefilter = getTimefilter();
122+
timefilter.setTime({
123+
from: moment(1486425600000).toISOString(), // Feb 07 2017
124+
to: moment(1486857600000).toISOString(), // Feb 12 2017
125+
});
99126

100127
describe('explorerChartsContainerService', () => {
101128
afterEach(() => {

0 commit comments

Comments
 (0)