Skip to content

Commit

Permalink
[TSVB] Metric count is depicted as - instead of 0
Browse files Browse the repository at this point in the history
  • Loading branch information
DianaDerevyankina committed Jun 29, 2021
1 parent 16ae487 commit 28be614
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]]);
});
});
Original file line number Diff line number Diff line change
@@ -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)]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}, {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down

0 comments on commit 28be614

Please sign in to comment.