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] Add value count #136385

Merged
merged 16 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
2 changes: 2 additions & 0 deletions src/plugins/data/common/search/aggs/agg_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const getAggTypes = () => ({
{ name: METRIC_TYPES.MAX, fn: metrics.getMaxMetricAgg },
{ name: METRIC_TYPES.STD_DEV, fn: metrics.getStdDeviationMetricAgg },
{ name: METRIC_TYPES.CARDINALITY, fn: metrics.getCardinalityMetricAgg },
{ name: METRIC_TYPES.VALUE_COUNT, fn: metrics.getValueCountMetricAgg },
{ name: METRIC_TYPES.PERCENTILES, fn: metrics.getPercentilesMetricAgg },
{ name: METRIC_TYPES.PERCENTILE_RANKS, fn: metrics.getPercentileRanksMetricAgg },
{ name: METRIC_TYPES.TOP_HITS, fn: metrics.getTopHitMetricAgg },
Expand Down Expand Up @@ -97,6 +98,7 @@ export const getAggTypesFunctions = () => [
metrics.aggBucketSum,
metrics.aggFilteredMetric,
metrics.aggCardinality,
metrics.aggValueCount,
metrics.aggCount,
metrics.aggCumulativeSum,
metrics.aggDerivative,
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data/common/search/aggs/aggs_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ describe('Aggs service', () => {
"max",
"std_dev",
"cardinality",
"value_count",
"percentiles",
"percentile_ranks",
"top_hits",
Expand Down Expand Up @@ -136,6 +137,7 @@ describe('Aggs service', () => {
"max",
"std_dev",
"cardinality",
"value_count",
"percentiles",
"percentile_ranks",
"top_hits",
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data/common/search/aggs/metrics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export * from './filtered_metric_fn';
export * from './filtered_metric';
export * from './cardinality_fn';
export * from './cardinality';
export * from './value_count_fn';
export * from './value_count';
export * from './count';
export * from './count_fn';
export * from './cumulative_sum_fn';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ export class MetricAggType<TMetricAggConfig extends AggConfig = IMetricAggConfig
config.getValue ||
((agg, bucket) => {
// Metric types where an empty set equals `zero`
const isSettableToZero = [METRIC_TYPES.CARDINALITY, METRIC_TYPES.SUM].includes(
agg.type.name as METRIC_TYPES
);
const isSettableToZero = [
METRIC_TYPES.CARDINALITY,
METRIC_TYPES.VALUE_COUNT,
METRIC_TYPES.SUM,
].includes(agg.type.name as METRIC_TYPES);

// Return proper values when no buckets are present
// `Count` handles empty sets properly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export enum METRIC_TYPES {
AVG = 'avg',
FILTERED_METRIC = 'filtered_metric',
CARDINALITY = 'cardinality',
VALUE_COUNT = 'value_count',
AVG_BUCKET = 'avg_bucket',
MAX_BUCKET = 'max_bucket',
MIN_BUCKET = 'min_bucket',
Expand Down
48 changes: 48 additions & 0 deletions src/plugins/data/common/search/aggs/metrics/value_count.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 { i18n } from '@kbn/i18n';
import { aggValueCountFnName } from './value_count_fn';
import { MetricAggType, IMetricAggConfig } from './metric_agg_type';
import { METRIC_TYPES } from './metric_agg_types';
import { BaseAggParams } from '../types';

const valueCountTitle = i18n.translate('data.search.aggs.metrics.valueCountTitle', {
defaultMessage: 'Value Count',
});

export interface AggParamsValueCount extends BaseAggParams {
field: string;
emptyAsNull?: boolean;
}

export const getValueCountMetricAgg = () =>
new MetricAggType({
name: METRIC_TYPES.VALUE_COUNT,
valueType: 'number',
expressionName: aggValueCountFnName,
title: valueCountTitle,
enableEmptyAsNull: true,
makeLabel(aggConfig: IMetricAggConfig) {
return i18n.translate('data.search.aggs.metrics.valueCountLabel', {
defaultMessage: 'Value count of {field}',
values: { field: aggConfig.getFieldDisplayName() },
});
},
getSerializedFormat(agg) {
return {
id: 'number',
};
},
params: [
{
name: 'field',
type: 'field',
},
],
});
49 changes: 49 additions & 0 deletions src/plugins/data/common/search/aggs/metrics/value_count_fn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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 { functionWrapper } from '../test_helpers';
import { aggValueCount } from './value_count_fn';

describe('agg_expression_functions', () => {
describe('aggValueCount', () => {
const fn = functionWrapper(aggValueCount());

test('required args are provided', () => {
const actual = fn({
field: 'machine.os.keyword',
});
expect(actual).toMatchInlineSnapshot(`
Object {
"type": "agg_type",
"value": Object {
"enabled": true,
"id": undefined,
"params": Object {
"customLabel": undefined,
"emptyAsNull": undefined,
"field": "machine.os.keyword",
"json": undefined,
"timeShift": undefined,
},
"schema": undefined,
"type": "value_count",
},
}
`);
});

test('correctly parses json string argument', () => {
const actual = fn({
field: 'machine.os.keyword',
json: '{ "foo": true }',
});

expect(actual.value.params.json).toEqual('{ "foo": true }');
});
});
});
101 changes: 101 additions & 0 deletions src/plugins/data/common/search/aggs/metrics/value_count_fn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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 { i18n } from '@kbn/i18n';
import { ExpressionFunctionDefinition } from '@kbn/expressions-plugin/common';
import { AggExpressionType, AggExpressionFunctionArgs, METRIC_TYPES } from '..';

export const aggValueCountFnName = 'aggValueCount';

type Input = any;
type AggArgs = AggExpressionFunctionArgs<typeof METRIC_TYPES.VALUE_COUNT>;
type Output = AggExpressionType;
type FunctionDefinition = ExpressionFunctionDefinition<
typeof aggValueCountFnName,
Input,
AggArgs,
Output
>;

export const aggValueCount = (): FunctionDefinition => ({
name: aggValueCountFnName,
help: i18n.translate('data.search.aggs.function.metrics.valueCount.help', {
defaultMessage: 'Generates a serialized agg config for a value count agg',
}),
type: 'agg_type',
args: {
id: {
types: ['string'],
help: i18n.translate('data.search.aggs.metrics.value_count.id.help', {
defaultMessage: 'ID for this aggregation',
}),
},
enabled: {
types: ['boolean'],
default: true,
help: i18n.translate('data.search.aggs.metrics.value_count.enabled.help', {
defaultMessage: 'Specifies whether this aggregation should be enabled',
}),
},
schema: {
types: ['string'],
help: i18n.translate('data.search.aggs.metrics.value_count.schema.help', {
defaultMessage: 'Schema to use for this aggregation',
}),
},
field: {
types: ['string'],
required: true,
help: i18n.translate('data.search.aggs.metrics.value_count.field.help', {
defaultMessage: 'Field to use for this aggregation',
}),
},
json: {
types: ['string'],
help: i18n.translate('data.search.aggs.metrics.value_count.json.help', {
defaultMessage: 'Advanced json to include when the agg is sent to Elasticsearch',
}),
},
customLabel: {
types: ['string'],
help: i18n.translate('data.search.aggs.metrics.value_count.customLabel.help', {
defaultMessage: 'Represents a custom label for this aggregation',
}),
},
timeShift: {
types: ['string'],
help: i18n.translate('data.search.aggs.metrics.timeShift.help', {
defaultMessage:
'Shift the time range for the metric by a set time, for example 1h or 7d. "previous" will use the closest time range from the date histogram or time range filter.',
}),
},
emptyAsNull: {
types: ['boolean'],
help: i18n.translate('data.search.aggs.metrics.emptyAsNull.help', {
defaultMessage:
'If set to true, a missing value is treated as null in the resulting data table. If set to false, a "zero" is filled in',
}),
},
},
fn: (input, args) => {
const { id, enabled, schema, ...rest } = args;

return {
type: 'agg_type',
value: {
id,
enabled,
schema,
type: METRIC_TYPES.VALUE_COUNT,
params: {
...rest,
},
},
};
},
});
4 changes: 4 additions & 0 deletions src/plugins/data/common/search/aggs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
aggBucketMin,
aggBucketSum,
aggCardinality,
aggValueCount,
AggConfigs,
AggConfigSerialized,
aggCount,
Expand All @@ -41,6 +42,7 @@ import {
AggParamsBucketSum,
AggParamsFilteredMetric,
AggParamsCardinality,
AggParamsValueCount,
AggParamsCumulativeSum,
AggParamsDateHistogram,
AggParamsDateRange,
Expand Down Expand Up @@ -174,6 +176,7 @@ export interface AggParamsMapping {
[METRIC_TYPES.AVG]: AggParamsAvg;
[METRIC_TYPES.CARDINALITY]: AggParamsCardinality;
[METRIC_TYPES.COUNT]: AggParamsCount;
[METRIC_TYPES.VALUE_COUNT]: AggParamsValueCount;
[METRIC_TYPES.GEO_BOUNDS]: AggParamsGeoBounds;
[METRIC_TYPES.GEO_CENTROID]: AggParamsGeoCentroid;
[METRIC_TYPES.MAX]: AggParamsMax;
Expand Down Expand Up @@ -222,6 +225,7 @@ export interface AggFunctionsMapping {
aggBucketSum: ReturnType<typeof aggBucketSum>;
aggFilteredMetric: ReturnType<typeof aggFilteredMetric>;
aggCardinality: ReturnType<typeof aggCardinality>;
aggValueCount: ReturnType<typeof aggValueCount>;
aggCount: ReturnType<typeof aggCount>;
aggCumulativeSum: ReturnType<typeof aggCumulativeSum>;
aggDerivative: ReturnType<typeof aggDerivative>;
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/public/search/aggs/aggs_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('AggsService - public', () => {
service.setup(setupDeps);
const start = service.start(startDeps);
expect(start.types.getAll().buckets.length).toBe(16);
expect(start.types.getAll().metrics.length).toBe(25);
expect(start.types.getAll().metrics.length).toBe(26);
});

test('registers custom agg types', () => {
Expand All @@ -70,7 +70,7 @@ describe('AggsService - public', () => {
const start = service.start(startDeps);
expect(start.types.getAll().buckets.length).toBe(17);
expect(start.types.getAll().buckets.some(({ name }) => name === 'foo')).toBe(true);
expect(start.types.getAll().metrics.length).toBe(26);
expect(start.types.getAll().metrics.length).toBe(27);
expect(start.types.getAll().metrics.some(({ name }) => name === 'bar')).toBe(true);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { Filtering } from './filtering';
import { AdvancedOptions } from './advanced_options';
import { TimeShift } from './time_shift';
import type { LayerType } from '../../../common';
import { DOCUMENT_FIELD_NAME } from '../../../common';
import {
quickFunctionsName,
staticValueOperationName,
Expand All @@ -62,6 +63,7 @@ import { ParamEditorProps } from '../operations/definitions';
import { WrappingHelpPopover } from '../help_popover';
import { isColumn } from '../operations/definitions/helpers';
import { FieldChoiceWithOperationType } from './field_select';
import { documentField } from '../document_field';

export interface DimensionEditorProps extends IndexPatternDimensionEditorProps {
selectedColumn?: GenericIndexPatternColumn;
Expand Down Expand Up @@ -449,7 +451,8 @@ export function DimensionEditor(props: DimensionEditorProps) {
indexPattern: currentIndexPattern,
columnId,
op: operationType,
field: undefined,
// if document field can be used, default to it
field: possibleFields.has(DOCUMENT_FIELD_NAME) ? documentField : undefined,
visualizationGroups: dimensionGroups,
targetGroup: props.groupId,
});
Expand Down
Loading