From 28be61452feac741cef09a2b48af4690ff5f5e04 Mon Sep 17 00:00:00 2001 From: Diana Derevyankina Date: Tue, 29 Jun 2021 19:36:38 +0300 Subject: [PATCH 1/2] [TSVB] Metric count is depicted as - instead of 0 --- ...ap_bucket.test.js => extract_data.test.ts} | 50 +++++++++++-------- .../lib/vis_data/helpers/extract_data.ts | 25 ++++++++++ .../server/lib/vis_data/helpers/index.js | 2 +- .../server/lib/vis_data/helpers/map_bucket.js | 13 ----- .../response_processors/series/math.js | 7 +-- .../response_processors/series/std_metric.js | 7 +-- .../response_processors/table/std_metric.js | 6 +-- 7 files changed, 61 insertions(+), 49 deletions(-) rename src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/{map_bucket.test.js => extract_data.test.ts} (56%) create mode 100644 src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/extract_data.ts delete mode 100644 src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/map_bucket.js diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/map_bucket.test.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/extract_data.test.ts similarity index 56% rename from src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/map_bucket.test.js rename to src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/extract_data.test.ts index 63ba294b7dc46..581e67310fe8f 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/map_bucket.test.js +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/extract_data.test.ts @@ -6,39 +6,47 @@ * Side Public License, v 1. */ -import { mapBucket } from './map_bucket'; +import { extractData } from './extract_data'; describe('mapBucket(metric)', () => { test('returns bucket key and value for basic metric', () => { const metric = { id: 'AVG', type: 'avg' }; - const bucket = { - key: 1234, - AVG: { value: 1 }, - }; - expect(mapBucket(metric)(bucket)).toEqual([1234, 1]); + const buckets = [ + { + key: 1234, + AVG: { value: 1 }, + }, + ]; + expect(extractData(metric, buckets)).toEqual([[1234, 1]]); }); test('returns bucket key and value for std_deviation', () => { const metric = { id: 'STDDEV', type: 'std_deviation' }; - const bucket = { - key: 1234, - STDDEV: { std_deviation: 1 }, - }; - expect(mapBucket(metric)(bucket)).toEqual([1234, 1]); + const buckets = [ + { + key: 1234, + STDDEV: { std_deviation: 1 }, + }, + ]; + expect(extractData(metric, buckets)).toEqual([[1234, 1]]); }); test('returns bucket key and value for percentiles', () => { const metric = { id: 'PCT', type: 'percentile', percent: 50 }; - const bucket = { - key: 1234, - PCT: { values: { '50.0': 1 } }, - }; - expect(mapBucket(metric)(bucket)).toEqual([1234, 1]); + const buckets = [ + { + key: 1234, + PCT: { values: { '50.0': 1 } }, + }, + ]; + expect(extractData(metric, buckets)).toEqual([[1234, 1]]); }); test('returns bucket key and value for derivative', () => { const metric = { id: 'DERV', type: 'derivative', field: 'io', unit: '1s' }; - const bucket = { - key: 1234, - DERV: { value: 100, normalized_value: 1 }, - }; - expect(mapBucket(metric)(bucket)).toEqual([1234, 1]); + const buckets = [ + { + key: 1234, + DERV: { value: 100, normalized_value: 1 }, + }, + ]; + expect(extractData(metric, buckets)).toEqual([[1234, 1]]); }); }); diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/extract_data.ts b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/extract_data.ts new file mode 100644 index 0000000000000..f96321cb12bfb --- /dev/null +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/extract_data.ts @@ -0,0 +1,25 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +// @ts-expect-error not typed yet +import { getAggValue } from './get_agg_value'; +import { METRIC_TYPES } from '../../../../../data/common'; +import type { Metric } from '../../../../common/types'; + +export const extractData = (metric: Metric, buckets: any[]) => { + // Metric types where an empty set equals `zero` + const isSettableToZero = [ + METRIC_TYPES.COUNT, + METRIC_TYPES.CARDINALITY, + METRIC_TYPES.SUM, + ].includes(metric.type as METRIC_TYPES); + + return isSettableToZero && !buckets.length + ? [[undefined, 0]] + : buckets.map((bucket) => [bucket.key, getAggValue(bucket, metric)]); +}; diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/index.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/index.js index de93ff22fa598..ffa5f213e2cea 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/index.js +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/index.js @@ -7,6 +7,7 @@ */ export { bucketTransform } from './bucket_transform'; +export { extractData } from './extract_data'; export { getAggValue } from './get_agg_value'; export { getBucketSize } from './get_bucket_size'; export { getBucketsPath } from './get_buckets_path'; @@ -15,6 +16,5 @@ export { getLastMetric } from './get_last_metric'; export { getSiblingAggValue } from './get_sibling_agg_value'; export { getSplits } from './get_splits'; export { getTimerange } from './get_timerange'; -export { mapBucket } from './map_bucket'; export { parseSettings } from './parse_settings'; export { overwrite } from './overwrite'; diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/map_bucket.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/map_bucket.js deleted file mode 100644 index dd48aa3705d41..0000000000000 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/map_bucket.js +++ /dev/null @@ -1,13 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -import { getAggValue } from './get_agg_value'; - -export function mapBucket(metric) { - return (bucket) => [bucket.key, getAggValue(bucket, metric)]; -} diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/math.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/math.js index d3cff76524ee3..9ea34d1c16bde 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/math.js +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/math.js @@ -10,10 +10,7 @@ import { convertIntervalToUnit } from '../../helpers/unit_to_seconds'; const percentileValueMatch = /\[([0-9\.]+)\]$/; import { startsWith, flatten, values, first, last } from 'lodash'; -import { getDefaultDecoration } from '../../helpers/get_default_decoration'; -import { getSiblingAggValue } from '../../helpers/get_sibling_agg_value'; -import { getSplits } from '../../helpers/get_splits'; -import { mapBucket } from '../../helpers/map_bucket'; +import { getDefaultDecoration, getSiblingAggValue, getSplits, extractData } from '../../helpers'; import { evaluate } from '@kbn/tinymath'; export function mathAgg(resp, panel, series, meta, extractFields) { @@ -44,7 +41,7 @@ export function mathAgg(resp, panel, series, meta, extractFields) { } else { const percentileMatch = v.field.match(percentileValueMatch); const m = percentileMatch ? { ...metric, percent: percentileMatch[1] } : { ...metric }; - acc[v.name] = split.timeseries.buckets.map(mapBucket(m)); + acc[v.name] = extractData(m, split.timeseries.buckets); } return acc; }, {}); diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/std_metric.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/std_metric.js index b9a4139a72313..2114a460a5c6d 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/std_metric.js +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/std_metric.js @@ -6,10 +6,7 @@ * Side Public License, v 1. */ -import { getDefaultDecoration } from '../../helpers/get_default_decoration'; -import { getSplits } from '../../helpers/get_splits'; -import { getLastMetric } from '../../helpers/get_last_metric'; -import { mapBucket } from '../../helpers/map_bucket'; +import { getDefaultDecoration, getSplits, getLastMetric, extractData } from '../../helpers'; import { METRIC_TYPES } from '../../../../../common/enums'; export function stdMetric(resp, panel, series, meta, extractFields) { @@ -26,7 +23,7 @@ export function stdMetric(resp, panel, series, meta, extractFields) { const decoration = getDefaultDecoration(series); (await getSplits(resp, panel, series, meta, extractFields)).forEach((split) => { - const data = split.timeseries.buckets.map(mapBucket(metric)); + const data = extractData(metric, split.timeseries.buckets); results.push({ id: `${split.id}`, label: split.label, diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/table/std_metric.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/table/std_metric.js index a648258745a15..65a006da71555 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/table/std_metric.js +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/table/std_metric.js @@ -6,9 +6,7 @@ * Side Public License, v 1. */ -import { getSplits } from '../../helpers/get_splits'; -import { getLastMetric } from '../../helpers/get_last_metric'; -import { mapBucket } from '../../helpers/map_bucket'; +import { getSplits, getLastMetric, extractData } from '../../helpers'; import { METRIC_TYPES } from '../../../../../common/enums'; export function stdMetric(bucket, panel, series, meta, extractFields) { @@ -32,7 +30,7 @@ export function stdMetric(bucket, panel, series, meta, extractFields) { }; (await getSplits(fakeResp, panel, series, meta, extractFields)).forEach((split) => { - const data = split.timeseries.buckets.map(mapBucket(metric)); + const data = extractData(metric, split.timeseries.buckets); results.push({ id: split.id, label: split.label, From 9f90cc20a93cb790477d2832aaaa4a4da448cc05 Mon Sep 17 00:00:00 2001 From: Diana Derevyankina Date: Wed, 30 Jun 2021 13:40:38 +0300 Subject: [PATCH 2/2] Rename extractData to mapEmptyToZero and remove unnecessary intervalString undefined assignment in get_bucket_size.js --- .../server/lib/vis_data/helpers/get_bucket_size.js | 2 -- .../server/lib/vis_data/helpers/index.js | 2 +- ...xtract_data.test.ts => map_empty_to_zero.test.ts} | 12 ++++++------ .../{extract_data.ts => map_empty_to_zero.ts} | 2 +- .../lib/vis_data/response_processors/series/math.js | 4 ++-- .../response_processors/series/std_metric.js | 4 ++-- .../vis_data/response_processors/table/std_metric.js | 4 ++-- 7 files changed, 14 insertions(+), 16 deletions(-) rename src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/{extract_data.test.ts => map_empty_to_zero.test.ts} (77%) rename src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/{extract_data.ts => map_empty_to_zero.ts} (92%) diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/get_bucket_size.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/get_bucket_size.js index b9ce76f7176b4..ad20f434bedf5 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/get_bucket_size.js +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/get_bucket_size.js @@ -45,8 +45,6 @@ const calculateBucketData = (timeInterval, capabilities) => { if (converted) { intervalString = converted.value + converted.unit; } - - intervalString = undefined; } else { intervalString = '1ms'; } diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/index.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/index.js index ffa5f213e2cea..e2211e4843bd6 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/index.js +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/index.js @@ -7,7 +7,6 @@ */ export { bucketTransform } from './bucket_transform'; -export { extractData } from './extract_data'; export { getAggValue } from './get_agg_value'; export { getBucketSize } from './get_bucket_size'; export { getBucketsPath } from './get_buckets_path'; @@ -17,4 +16,5 @@ export { getSiblingAggValue } from './get_sibling_agg_value'; export { getSplits } from './get_splits'; export { getTimerange } from './get_timerange'; export { parseSettings } from './parse_settings'; +export { mapEmptyToZero } from './map_empty_to_zero'; export { overwrite } from './overwrite'; diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/extract_data.test.ts b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/map_empty_to_zero.test.ts similarity index 77% rename from src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/extract_data.test.ts rename to src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/map_empty_to_zero.test.ts index 581e67310fe8f..b5f1adc3f0202 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/extract_data.test.ts +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/map_empty_to_zero.test.ts @@ -6,9 +6,9 @@ * Side Public License, v 1. */ -import { extractData } from './extract_data'; +import { mapEmptyToZero } from './map_empty_to_zero'; -describe('mapBucket(metric)', () => { +describe('mapEmptyToZero(metric, buckets)', () => { test('returns bucket key and value for basic metric', () => { const metric = { id: 'AVG', type: 'avg' }; const buckets = [ @@ -17,7 +17,7 @@ describe('mapBucket(metric)', () => { AVG: { value: 1 }, }, ]; - expect(extractData(metric, buckets)).toEqual([[1234, 1]]); + expect(mapEmptyToZero(metric, buckets)).toEqual([[1234, 1]]); }); test('returns bucket key and value for std_deviation', () => { const metric = { id: 'STDDEV', type: 'std_deviation' }; @@ -27,7 +27,7 @@ describe('mapBucket(metric)', () => { STDDEV: { std_deviation: 1 }, }, ]; - expect(extractData(metric, buckets)).toEqual([[1234, 1]]); + expect(mapEmptyToZero(metric, buckets)).toEqual([[1234, 1]]); }); test('returns bucket key and value for percentiles', () => { const metric = { id: 'PCT', type: 'percentile', percent: 50 }; @@ -37,7 +37,7 @@ describe('mapBucket(metric)', () => { PCT: { values: { '50.0': 1 } }, }, ]; - expect(extractData(metric, buckets)).toEqual([[1234, 1]]); + expect(mapEmptyToZero(metric, buckets)).toEqual([[1234, 1]]); }); test('returns bucket key and value for derivative', () => { const metric = { id: 'DERV', type: 'derivative', field: 'io', unit: '1s' }; @@ -47,6 +47,6 @@ describe('mapBucket(metric)', () => { DERV: { value: 100, normalized_value: 1 }, }, ]; - expect(extractData(metric, buckets)).toEqual([[1234, 1]]); + expect(mapEmptyToZero(metric, buckets)).toEqual([[1234, 1]]); }); }); diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/extract_data.ts b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/map_empty_to_zero.ts similarity index 92% rename from src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/extract_data.ts rename to src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/map_empty_to_zero.ts index f96321cb12bfb..0490193a76e81 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/extract_data.ts +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/map_empty_to_zero.ts @@ -11,7 +11,7 @@ import { getAggValue } from './get_agg_value'; import { METRIC_TYPES } from '../../../../../data/common'; import type { Metric } from '../../../../common/types'; -export const extractData = (metric: Metric, buckets: any[]) => { +export const mapEmptyToZero = (metric: Metric, buckets: any[]) => { // Metric types where an empty set equals `zero` const isSettableToZero = [ METRIC_TYPES.COUNT, diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/math.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/math.js index 9ea34d1c16bde..a6addc8ba0e53 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/math.js +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/math.js @@ -10,7 +10,7 @@ import { convertIntervalToUnit } from '../../helpers/unit_to_seconds'; const percentileValueMatch = /\[([0-9\.]+)\]$/; import { startsWith, flatten, values, first, last } from 'lodash'; -import { getDefaultDecoration, getSiblingAggValue, getSplits, extractData } from '../../helpers'; +import { getDefaultDecoration, getSiblingAggValue, getSplits, mapEmptyToZero } from '../../helpers'; import { evaluate } from '@kbn/tinymath'; export function mathAgg(resp, panel, series, meta, extractFields) { @@ -41,7 +41,7 @@ export function mathAgg(resp, panel, series, meta, extractFields) { } else { const percentileMatch = v.field.match(percentileValueMatch); const m = percentileMatch ? { ...metric, percent: percentileMatch[1] } : { ...metric }; - acc[v.name] = extractData(m, split.timeseries.buckets); + acc[v.name] = mapEmptyToZero(m, split.timeseries.buckets); } return acc; }, {}); diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/std_metric.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/std_metric.js index 2114a460a5c6d..cc406041ad874 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/std_metric.js +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/series/std_metric.js @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { getDefaultDecoration, getSplits, getLastMetric, extractData } from '../../helpers'; +import { getDefaultDecoration, getSplits, getLastMetric, mapEmptyToZero } from '../../helpers'; import { METRIC_TYPES } from '../../../../../common/enums'; export function stdMetric(resp, panel, series, meta, extractFields) { @@ -23,7 +23,7 @@ export function stdMetric(resp, panel, series, meta, extractFields) { const decoration = getDefaultDecoration(series); (await getSplits(resp, panel, series, meta, extractFields)).forEach((split) => { - const data = extractData(metric, split.timeseries.buckets); + const data = mapEmptyToZero(metric, split.timeseries.buckets); results.push({ id: `${split.id}`, label: split.label, diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/table/std_metric.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/table/std_metric.js index 65a006da71555..140212c2ec907 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/table/std_metric.js +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/response_processors/table/std_metric.js @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { getSplits, getLastMetric, extractData } from '../../helpers'; +import { getSplits, getLastMetric, mapEmptyToZero } from '../../helpers'; import { METRIC_TYPES } from '../../../../../common/enums'; export function stdMetric(bucket, panel, series, meta, extractFields) { @@ -30,7 +30,7 @@ export function stdMetric(bucket, panel, series, meta, extractFields) { }; (await getSplits(fakeResp, panel, series, meta, extractFields)).forEach((split) => { - const data = extractData(metric, split.timeseries.buckets); + const data = mapEmptyToZero(metric, split.timeseries.buckets); results.push({ id: split.id, label: split.label,