Skip to content

Commit 63b8c4a

Browse files
[TSVB] Fix std deviation band mode (#64413)
* Fix std_deviation_bands mode * Fix jest test Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 9f02c06 commit 63b8c4a

6 files changed

Lines changed: 70 additions & 111 deletions

File tree

src/plugins/vis_type_timeseries/public/application/visualizations/views/timeseries/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export const TimeSeries = ({
8787
const tooltipFormatter = decorateFormatter(xAxisFormatter);
8888
const uiSettings = getUISettings();
8989
const timeZone = getTimezone(uiSettings);
90-
const hasBarChart = series.some(({ bars }) => bars.show);
90+
const hasBarChart = series.some(({ bars }) => bars?.show);
9191

9292
// compute the theme based on the bg color
9393
const theme = getTheme(darkMode, backgroundColor);
@@ -180,7 +180,7 @@ export const TimeSeries = ({
180180
// Only use color mapping if there is no color from the server
181181
const finalColor = color ?? colors.mappedColors.mapping[label];
182182

183-
if (bars.show) {
183+
if (bars?.show) {
184184
return (
185185
<BarSeriesDecorator
186186
key={key}
@@ -205,7 +205,7 @@ export const TimeSeries = ({
205205
);
206206
}
207207

208-
if (lines.show) {
208+
if (lines?.show) {
209209
return (
210210
<AreaSeriesDecorator
211211
key={key}

src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/index.js

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,15 @@
1717
* under the License.
1818
*/
1919

20-
import { bucketTransform } from './bucket_transform';
21-
import { getAggValue } from './get_agg_value';
22-
import { getBucketSize } from './get_bucket_size';
23-
import { getBucketsPath } from './get_buckets_path';
24-
import { getDefaultDecoration } from './get_default_decoration';
25-
import { getLastMetric } from './get_last_metric';
26-
import { getSiblingAggValue } from './get_sibling_agg_value';
27-
import { getSplits } from './get_splits';
28-
import { getTimerange } from './get_timerange';
29-
import { mapBucket } from './map_bucket';
30-
import { parseSettings } from './parse_settings';
31-
20+
export { bucketTransform } from './bucket_transform';
21+
export { getAggValue } from './get_agg_value';
22+
export { getBucketSize } from './get_bucket_size';
23+
export { getBucketsPath } from './get_buckets_path';
24+
export { getDefaultDecoration } from './get_default_decoration';
25+
export { getLastMetric } from './get_last_metric';
26+
export { getSiblingAggValue } from './get_sibling_agg_value';
27+
export { getSplits } from './get_splits';
28+
export { getTimerange } from './get_timerange';
29+
export { mapBucket } from './map_bucket';
30+
export { parseSettings } from './parse_settings';
3231
export { overwrite } from './overwrite';
33-
34-
export const helpers = {
35-
bucketTransform,
36-
getAggValue,
37-
getBucketSize,
38-
getBucketsPath,
39-
getDefaultDecoration,
40-
getLastMetric,
41-
getSiblingAggValue,
42-
getSplits,
43-
getTimerange,
44-
mapBucket,
45-
parseSettings,
46-
};

src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/std_deviation_bands.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,37 @@
1717
* under the License.
1818
*/
1919

20-
import _ from 'lodash';
21-
import { getSplits } from '../../helpers/get_splits';
22-
import { getLastMetric } from '../../helpers/get_last_metric';
23-
import { mapBucket } from '../../helpers/map_bucket';
20+
import { getAggValue, getLastMetric, getSplits } from '../../helpers';
21+
import { METRIC_TYPES } from '../../../../../common/metric_types';
2422

2523
export function stdDeviationBands(resp, panel, series, meta) {
2624
return next => results => {
2725
const metric = getLastMetric(series);
28-
if (metric.type === 'std_deviation' && metric.mode === 'band') {
29-
getSplits(resp, panel, series, meta).forEach(split => {
30-
const upper = split.timeseries.buckets.map(
31-
mapBucket(_.assign({}, metric, { mode: 'upper' }))
32-
);
33-
const lower = split.timeseries.buckets.map(
34-
mapBucket(_.assign({}, metric, { mode: 'lower' }))
35-
);
36-
results.push({
37-
id: `${split.id}:upper`,
38-
label: split.label,
39-
color: split.color,
40-
lines: { show: true, fill: 0.5, lineWidth: 0 },
41-
points: { show: false },
42-
fillBetween: `${split.id}:lower`,
43-
data: upper,
44-
});
26+
if (metric.type === METRIC_TYPES.STD_DEVIATION && metric.mode === 'band') {
27+
getSplits(resp, panel, series, meta).forEach(({ id, color, label, timeseries }) => {
28+
const data = timeseries.buckets.map(bucket => [
29+
bucket.key,
30+
getAggValue(bucket, { ...metric, mode: 'upper' }),
31+
getAggValue(bucket, { ...metric, mode: 'lower' }),
32+
]);
33+
4534
results.push({
46-
id: `${split.id}:lower`,
47-
color: split.color,
48-
lines: { show: true, fill: false, lineWidth: 0 },
35+
id,
36+
label,
37+
color,
38+
data,
39+
lines: {
40+
show: series.chart_type === 'line',
41+
fill: 0.5,
42+
lineWidth: 0,
43+
mode: 'band',
44+
},
45+
bars: {
46+
show: series.chart_type === 'bar',
47+
fill: 0.5,
48+
mode: 'band',
49+
},
4950
points: { show: false },
50-
data: lower,
5151
});
5252
});
5353
}

src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/std_deviation_bands.test.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,29 +86,18 @@ describe('stdDeviationBands(resp, panel, series)', () => {
8686
test('creates a series', () => {
8787
const next = results => results;
8888
const results = stdDeviationBands(resp, panel, series)(next)([]);
89-
expect(results).toHaveLength(2);
89+
expect(results).toHaveLength(1);
9090

9191
expect(results[0]).toEqual({
92-
id: 'test:upper',
92+
id: 'test',
9393
label: 'Std. Deviation of cpu',
9494
color: 'rgb(255, 0, 0)',
95-
lines: { show: true, fill: 0.5, lineWidth: 0 },
96-
points: { show: false },
97-
fillBetween: 'test:lower',
98-
data: [
99-
[1, 3.2],
100-
[2, 3.5],
101-
],
102-
});
103-
104-
expect(results[1]).toEqual({
105-
id: 'test:lower',
106-
color: 'rgb(255, 0, 0)',
107-
lines: { show: true, fill: false, lineWidth: 0 },
95+
lines: { show: true, fill: 0.5, lineWidth: 0, mode: 'band' },
96+
bars: { show: false, fill: 0.5, mode: 'band' },
10897
points: { show: false },
10998
data: [
110-
[1, 0.2],
111-
[2, 0.5],
99+
[1, 3.2, 0.2],
100+
[2, 3.5, 0.5],
112101
],
113102
});
114103
});

src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/std_deviation_sibling.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,36 @@
1717
* under the License.
1818
*/
1919

20-
import _ from 'lodash';
21-
import { getSplits } from '../../helpers/get_splits';
22-
import { getLastMetric } from '../../helpers/get_last_metric';
23-
import { getSiblingAggValue } from '../../helpers/get_sibling_agg_value';
20+
import { getSplits, getLastMetric, getSiblingAggValue } from '../../helpers';
2421

2522
export function stdDeviationSibling(resp, panel, series, meta) {
2623
return next => results => {
2724
const metric = getLastMetric(series);
2825
if (metric.mode === 'band' && metric.type === 'std_deviation_bucket') {
2926
getSplits(resp, panel, series, meta).forEach(split => {
30-
const mapBucketByMode = mode => {
31-
return bucket => {
32-
return [bucket.key, getSiblingAggValue(split, _.assign({}, metric, { mode }))];
33-
};
34-
};
27+
const data = split.timeseries.buckets.map(bucket => [
28+
bucket.key,
29+
getSiblingAggValue(split, { ...metric, mode: 'upper' }),
30+
getSiblingAggValue(split, { ...metric, mode: 'lower' }),
31+
]);
3532

36-
const upperData = split.timeseries.buckets.map(mapBucketByMode('upper'));
37-
const lowerData = split.timeseries.buckets.map(mapBucketByMode('lower'));
38-
39-
results.push({
40-
id: `${split.id}:lower`,
41-
lines: { show: true, fill: false, lineWidth: 0 },
42-
points: { show: false },
43-
color: split.color,
44-
data: lowerData,
45-
});
4633
results.push({
47-
id: `${split.id}:upper`,
34+
id: split.id,
4835
label: split.label,
4936
color: split.color,
50-
lines: { show: true, fill: 0.5, lineWidth: 0 },
37+
lines: {
38+
show: series.chart_type === 'line',
39+
fill: 0.5,
40+
lineWidth: 0,
41+
mode: 'band',
42+
},
43+
bars: {
44+
show: series.chart_type === 'bar',
45+
fill: 0.5,
46+
mode: 'band',
47+
},
5148
points: { show: false },
52-
fillBetween: `${split.id}:lower`,
53-
data: upperData,
49+
data,
5450
});
5551
});
5652
}

src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/std_deviation_sibling.test.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,29 +86,18 @@ describe('stdDeviationSibling(resp, panel, series)', () => {
8686
test('creates a series', () => {
8787
const next = results => results;
8888
const results = stdDeviationSibling(resp, panel, series)(next)([]);
89-
expect(results).toHaveLength(2);
89+
expect(results).toHaveLength(1);
9090

9191
expect(results[0]).toEqual({
92-
id: 'test:lower',
92+
id: 'test',
9393
color: 'rgb(255, 0, 0)',
94-
lines: { show: true, fill: false, lineWidth: 0 },
95-
points: { show: false },
96-
data: [
97-
[1, 0.01],
98-
[2, 0.01],
99-
],
100-
});
101-
102-
expect(results[1]).toEqual({
103-
id: 'test:upper',
10494
label: 'Overall Std. Deviation of Average of cpu',
105-
color: 'rgb(255, 0, 0)',
106-
fillBetween: 'test:lower',
107-
lines: { show: true, fill: 0.5, lineWidth: 0 },
95+
lines: { show: true, fill: 0.5, lineWidth: 0, mode: 'band' },
96+
bars: { show: false, fill: 0.5, mode: 'band' },
10897
points: { show: false },
10998
data: [
110-
[1, 0.7],
111-
[2, 0.7],
99+
[1, 0.7, 0.01],
100+
[2, 0.7, 0.01],
112101
],
113102
});
114103
});

0 commit comments

Comments
 (0)