Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lens] Supports percentile_ranks aggregation #132430

Merged
merged 18 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,11 @@ export const getPercentileRanksMetricAgg = ({
getValue(agg, bucket) {
return getPercentileValue(agg, bucket) / 100;
},
getValueBucketPath(aggConfig) {
if (aggConfig.key) {
return `${aggConfig.id}`;
}
return `${aggConfig.id}.${aggConfig.params.values[0]}`;
},
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 { getPercentileValue } from './percentiles_get_value';
import type { IResponseAggConfig } from './lib/get_response_agg_config_class';

describe('getPercentileValue', () => {
test('should return the correct value for an IResponseAggConfig', () => {
const agg = {
id: '0.400',
key: 400,
parentId: '0',
} as IResponseAggConfig;
const bucket = {
'0': {
values: [
{
key: 400,
value: 24.21909648206358,
},
],
},
doc_count: 2356,
};
const value = getPercentileValue(agg, bucket);
expect(value).toEqual(24.21909648206358);
});

test('should return the correct value for an TAggConfig', () => {
const agg = {
id: '0-metric',
enabled: true,
type: 'percentile_ranks',
params: {
field: 'AvgTicketPrice',
values: [400],
},
schema: 'metric',
} as unknown as IResponseAggConfig;
const bucket = {
doc_count: 290,
'0-metric': {
values: [
{
key: 400,
value: 25.84782692356769,
},
],
},
};
const value = getPercentileValue(agg, bucket);
expect(value).toEqual(25.84782692356769);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ export const getPercentileValue = <TAggConfig extends IResponseAggConfig>(
agg: TAggConfig,
bucket: any
) => {
const { values } = bucket[agg.parentId] ?? {};
const { values } = bucket[agg.parentId ?? agg.id] ?? {};

const percentile: any = find(values, ({ key }) => key === agg.key);
const percentile: any = find(values, ({ key }) => {
if (agg.key) {
return key === agg.key;
}

return key === agg.params.values[0];
});

return percentile ? percentile.value : NaN;
};
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,48 @@ describe('getSeries', () => {
]);
});

test('should return the correct config for the percentile ranks aggregation', () => {
const metric = [
{
field: 'day_of_week_i',
id: 'id1',
type: 'percentile_rank',
values: ['1', '5', '7'],
colors: ['rgba(211,96,134,1)', 'rgba(155,33,230,1)', '#68BC00'],
},
] as Metric[];
const config = getSeries(metric, 1);
expect(config).toStrictEqual([
{
agg: 'percentile_ranks',
color: 'rgba(211,96,134,1)',
fieldName: 'day_of_week_i',
isFullReference: false,
params: {
value: '1',
},
},
{
agg: 'percentile_ranks',
color: 'rgba(155,33,230,1)',
fieldName: 'day_of_week_i',
isFullReference: false,
params: {
value: '5',
},
},
{
agg: 'percentile_ranks',
color: '#68BC00',
fieldName: 'day_of_week_i',
isFullReference: false,
params: {
value: '7',
},
},
]);
});

test('should return the correct formula config for a top_hit size 1 aggregation', () => {
const metric = [
{
Expand Down Expand Up @@ -461,4 +503,55 @@ describe('getSeries', () => {
},
]);
});

test('should return the correct formula for the math aggregation with percentile ranks as variables', () => {
const metric = [
{
field: 'day_of_week_i',
id: 'e72265d2-2106-4af9-b646-33afd9cddcad',
values: ['1', '5', '7'],
colors: ['rgba(211,96,134,1)', 'rgba(155,33,230,1)', '#68BC00'],
type: 'percentile_rank',
},
{
field: 'day_of_week_i',
id: '6280b080-7d1c-11ec-bfa7-3798d98f8341',
type: 'avg',
},
{
id: '23a05540-7d18-11ec-a589-45a3784fc1ce',
script: 'params.perc1 + params.perc5 + params.avg',
type: 'math',
variables: [
{
field: 'e72265d2-2106-4af9-b646-33afd9cddcad[1]',
id: '25840960-7d18-11ec-a589-45a3784fc1ce',
name: 'perc1',
},
{
field: 'e72265d2-2106-4af9-b646-33afd9cddcad[5]',
id: '2a440270-7d18-11ec-a589-45a3784fc1ce',
name: 'perc5',
},
{
field: '6280b080-7d1c-11ec-bfa7-3798d98f8341',
id: '64c82f80-7d1c-11ec-bfa7-3798d98f8341',
name: 'avg',
},
],
},
] as Metric[];
const config = getSeries(metric, 1);
expect(config).toStrictEqual([
{
agg: 'formula',
fieldName: 'document',
isFullReference: true,
params: {
formula:
'percentile_ranks(day_of_week_i, value=1) + percentile_ranks(day_of_week_i, value=5) + average(day_of_week_i)',
},
},
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { Metric } from '../../common/types';
import { SUPPORTED_METRICS } from './supported_metrics';
import {
getPercentilesSeries,
getPercentileRankSeries,
getFormulaSeries,
getParentPipelineSeries,
getSiblingPipelineSeriesFormula,
Expand Down Expand Up @@ -45,6 +46,19 @@ export const getSeries = (
}
break;
}
case 'percentile_rank': {
const values = metrics[metricIdx].values;
const colors = metrics[metricIdx].colors;
if (values?.length) {
const percentileRanksSeries = getPercentileRankSeries(
values,
colors,
fieldName
) as VisualizeEditorLayersContext['metrics'];
metricsArray = [...metricsArray, ...percentileRanksSeries];
}
break;
}
case 'math': {
// find the metric idx that has math expression
const mathMetricIdx = metrics.findIndex((metric) => metric.type === 'math');
Expand All @@ -69,7 +83,7 @@ export const getSeries = (
}

// should treat percentiles differently
if (currentMetric.type === 'percentile') {
if (currentMetric.type === 'percentile' || currentMetric.type === 'percentile_rank') {
variables.forEach((variable) => {
const [_, meta] = variable?.field?.split('[') ?? [];
const metaValue = Number(meta?.replace(']', ''));
Expand Down Expand Up @@ -100,7 +114,7 @@ export const getSeries = (
break;
}
case 'cumulative_sum': {
// percentile value is derived from the field Id. It has the format xxx-xxx-xxx-xxx[percentile]
// percentile and percentile_ranks value is derived from the field Id. It has the format xxx-xxx-xxx-xxx[percentile]
const [fieldId, meta] = metrics[metricIdx]?.field?.split('[') ?? [];
const subFunctionMetric = metrics.find((metric) => metric.id === fieldId);
if (!subFunctionMetric || subFunctionMetric.type === 'static') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('triggerTSVBtoLensConfiguration', () => {
...model.series[0],
metrics: [
{
type: 'percentile_rank',
type: 'std_deviation',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ This test is testing the scenario that a metric agg is not supported in Lens. As we support now the percentile_rank I changed it to std_deviation that is not supported

},
] as Series['metrics'],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
*/
import { METRIC_TYPES } from '@kbn/data-plugin/public';
import type { Metric, MetricType } from '../../common/types';
import { getPercentilesSeries, getParentPipelineSeries } from './metrics_helpers';
import {
getPercentilesSeries,
getPercentileRankSeries,
getParentPipelineSeries,
} from './metrics_helpers';

describe('getPercentilesSeries', () => {
test('should return correct config for multiple percentiles', () => {
Expand Down Expand Up @@ -78,6 +82,37 @@ describe('getPercentilesSeries', () => {
});
});

describe('getPercentileRankSeries', () => {
test('should return correct config for multiple percentile ranks', () => {
const values = ['1', '5', '7'] as Metric['values'];
const colors = ['#68BC00', 'rgba(0,63,188,1)', 'rgba(188,38,0,1)'] as Metric['colors'];
const config = getPercentileRankSeries(values, colors, 'day_of_week_i');
expect(config).toStrictEqual([
{
agg: 'percentile_ranks',
color: '#68BC00',
fieldName: 'day_of_week_i',
isFullReference: false,
params: { value: '1' },
},
{
agg: 'percentile_ranks',
color: 'rgba(0,63,188,1)',
fieldName: 'day_of_week_i',
isFullReference: false,
params: { value: '5' },
},
{
agg: 'percentile_ranks',
color: 'rgba(188,38,0,1)',
fieldName: 'day_of_week_i',
isFullReference: false,
params: { value: '7' },
},
]);
});
});

describe('getParentPipelineSeries', () => {
test('should return correct config for pipeline agg on percentiles', () => {
const metrics = [
Expand Down Expand Up @@ -124,6 +159,36 @@ describe('getParentPipelineSeries', () => {
]);
});

test('should return correct config for pipeline agg on percentile ranks', () => {
const metrics = [
{
field: 'AvgTicketPrice',
id: '04558549-f19f-4a87-9923-27df8b81af3e',
values: ['400', '500', '700'],
colors: ['rgba(211,96,134,1)', 'rgba(155,33,230,1)', '#68BC00'],
type: 'percentile_rank',
},
{
field: '04558549-f19f-4a87-9923-27df8b81af3e[400.0]',
id: '764f4110-7db9-11ec-9fdf-91a8881dd06b',
type: 'derivative',
unit: '',
},
] as Metric[];
const config = getParentPipelineSeries(METRIC_TYPES.DERIVATIVE, 1, metrics);
expect(config).toStrictEqual([
{
agg: 'differences',
fieldName: 'AvgTicketPrice',
isFullReference: true,
params: {
value: 400,
},
pipelineAggType: 'percentile_ranks',
},
]);
});

test('should return null config for pipeline agg on non-supported sub-aggregation', () => {
const metrics = [
{
Expand Down
Loading