[TSVB] Metric count is depicted as - instead of 0 (#103717)

* [TSVB] Metric count is depicted as - instead of 0

* Rename extractData to mapEmptyToZero and remove unnecessary intervalString undefined assignment in get_bucket_size.js

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Diana Derevyankina 2021-06-30 15:43:18 +03:00 committed by GitHub
parent 2bc801c8ef
commit 9f82e5ef74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 62 additions and 52 deletions

View file

@ -45,8 +45,6 @@ const calculateBucketData = (timeInterval, capabilities) => {
if (converted) {
intervalString = converted.value + converted.unit;
}
intervalString = undefined;
} else {
intervalString = '1ms';
}

View file

@ -15,6 +15,6 @@ 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 { mapEmptyToZero } from './map_empty_to_zero';
export { overwrite } from './overwrite';

View file

@ -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)];
}

View file

@ -6,39 +6,47 @@
* Side Public License, v 1.
*/
import { mapBucket } from './map_bucket';
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 bucket = {
key: 1234,
AVG: { value: 1 },
};
expect(mapBucket(metric)(bucket)).toEqual([1234, 1]);
const buckets = [
{
key: 1234,
AVG: { value: 1 },
},
];
expect(mapEmptyToZero(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(mapEmptyToZero(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(mapEmptyToZero(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(mapEmptyToZero(metric, buckets)).toEqual([[1234, 1]]);
});
});

View file

@ -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 mapEmptyToZero = (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)]);
};

View file

@ -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, mapEmptyToZero } 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] = mapEmptyToZero(m, split.timeseries.buckets);
}
return acc;
}, {});

View file

@ -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, mapEmptyToZero } 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 = mapEmptyToZero(metric, split.timeseries.buckets);
results.push({
id: `${split.id}`,
label: split.label,

View file

@ -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, mapEmptyToZero } 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 = mapEmptyToZero(metric, split.timeseries.buckets);
results.push({
id: split.id,
label: split.label,