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

[data.search.aggs]: Expression functions for metric agg types #64914

Merged
merged 16 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 10 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
5 changes: 5 additions & 0 deletions src/plugins/data/public/search/aggs/metrics/avg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ import { MetricAggType } from './metric_agg_type';
import { METRIC_TYPES } from './metric_agg_types';
import { KBN_FIELD_TYPES } from '../../../../common';
import { GetInternalStartServicesFn } from '../../../types';
import { BaseAggParams } from '../types';

const averageTitle = i18n.translate('data.search.aggs.metrics.averageTitle', {
defaultMessage: 'Average',
});

export interface AggParamsAvg extends BaseAggParams {
field: string;
}

export interface AvgMetricAggDependencies {
getInternalStartServices: GetInternalStartServicesFn;
}
Expand Down
63 changes: 63 additions & 0 deletions src/plugins/data/public/search/aggs/metrics/avg_fn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { functionWrapper } from '../test_helpers';
import { aggAvg } from './avg_fn';

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

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 {
"field": "machine.os.keyword",
"json": undefined,
},
"schema": undefined,
"type": "avg",
},
}
`);
});

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

expect(actual.value.params.json).toEqual({ foo: true });
expect(() => {
fn({
field: 'machine.os.keyword',
json: '/// intentionally malformed json ///',
});
}).toThrowErrorMatchingInlineSnapshot(`"Unable to parse json argument string"`);
});
});
});
89 changes: 89 additions & 0 deletions src/plugins/data/public/search/aggs/metrics/avg_fn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { i18n } from '@kbn/i18n';
import { ExpressionFunctionDefinition } from '../../../../../expressions/public';
import { AggExpressionType, AggExpressionFunctionArgs, METRIC_TYPES } from '../';
import { getParsedValue } from '../utils/get_parsed_value';

const fnName = 'aggAvg';

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

export const aggAvg = (): FunctionDefinition => ({
name: fnName,
help: i18n.translate('data.search.aggs.function.metrics.avg.help', {
defaultMessage: 'Generates a serialized agg config for a avg agg',
}),
type: 'agg_type',
args: {
id: {
types: ['string'],
help: i18n.translate('data.search.aggs.metrics.avg.id.help', {
defaultMessage: 'ID for this aggregation',
}),
},
enabled: {
types: ['boolean'],
default: true,
help: i18n.translate('data.search.aggs.metrics.avg.enabled.help', {
defaultMessage: 'Specifies whether this aggregation should be enabled',
}),
},
schema: {
types: ['string'],
help: i18n.translate('data.search.aggs.metrics.avg.schema.help', {
defaultMessage: 'Schema to use for this aggregation',
}),
},
field: {
types: ['string'],
required: true,
help: i18n.translate('data.search.aggs.metrics.avg.field.help', {
defaultMessage: 'Field to use for this aggregation',
}),
},
json: {
types: ['string'],
help: i18n.translate('data.search.aggs.metrics.avg.json.help', {
defaultMessage: 'Advanced json to include when the agg is sent to Elasticsearch',
}),
},
},
fn: (input, args) => {
const { id, enabled, schema, ...rest } = args;

return {
type: 'agg_type',
value: {
id,
enabled,
schema,
type: METRIC_TYPES.AVG,
params: {
...rest,
json: getParsedValue(args, 'json'),
},
},
};
},
});
6 changes: 6 additions & 0 deletions src/plugins/data/public/search/aggs/metrics/bucket_avg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ import { MetricAggType } from './metric_agg_type';
import { makeNestedLabel } from './lib/make_nested_label';
import { siblingPipelineAggHelper } from './lib/sibling_pipeline_agg_helper';
import { METRIC_TYPES } from './metric_agg_types';
import { AggConfigSerialized, BaseAggParams } from '../types';
import { GetInternalStartServicesFn } from '../../../types';

export interface AggParamsBucketAvg extends BaseAggParams {
customMetric?: AggConfigSerialized;
customBucket?: AggConfigSerialized;
}

export interface BucketAvgMetricAggDependencies {
getInternalStartServices: GetInternalStartServicesFn;
}
Expand Down
75 changes: 75 additions & 0 deletions src/plugins/data/public/search/aggs/metrics/bucket_avg_fn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { functionWrapper } from '../test_helpers';
import { aggBucketAvg } from './bucket_avg_fn';

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

test('handles customMetric and customBucket as a subexpression', () => {
const actual = fn({
customMetric: fn({}),
customBucket: fn({}),
});

expect(actual.value.params).toMatchInlineSnapshot(`
Object {
"customBucket": Object {
"enabled": true,
"id": undefined,
"params": Object {
"customBucket": undefined,
"customMetric": undefined,
"json": undefined,
},
"schema": undefined,
"type": "avg_bucket",
},
"customMetric": Object {
"enabled": true,
"id": undefined,
"params": Object {
"customBucket": undefined,
"customMetric": undefined,
"json": undefined,
},
"schema": undefined,
"type": "avg_bucket",
},
"json": undefined,
}
`);
});

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

expect(actual.value.params.json).toEqual({ foo: true });
expect(() => {
fn({
json: '/// intentionally malformed json ///',
});
}).toThrowErrorMatchingInlineSnapshot(`"Unable to parse json argument string"`);
});
});
});
106 changes: 106 additions & 0 deletions src/plugins/data/public/search/aggs/metrics/bucket_avg_fn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { i18n } from '@kbn/i18n';
import { Assign } from '@kbn/utility-types';
import { ExpressionFunctionDefinition } from '../../../../../expressions/public';
import { AggExpressionType, AggExpressionFunctionArgs, METRIC_TYPES } from '../';
import { getParsedValue } from '../utils/get_parsed_value';

const fnName = 'aggBucketAvg';

type Input = any;
type AggArgs = AggExpressionFunctionArgs<typeof METRIC_TYPES.AVG_BUCKET>;
type Arguments = Assign<
AggArgs,
{ customBucket?: AggExpressionType; customMetric?: AggExpressionType }
>;
type Output = AggExpressionType;
type FunctionDefinition = ExpressionFunctionDefinition<typeof fnName, Input, Arguments, Output>;

export const aggBucketAvg = (): FunctionDefinition => ({
name: fnName,
help: i18n.translate('data.search.aggs.function.metrics.bucket_avg.help', {
defaultMessage: 'Generates a serialized agg config for a bucket_avg agg',
}),
type: 'agg_type',
args: {
id: {
types: ['string'],
help: i18n.translate('data.search.aggs.metrics.bucket_avg.id.help', {
defaultMessage: 'ID for this aggregation',
}),
},
enabled: {
types: ['boolean'],
default: true,
help: i18n.translate('data.search.aggs.metrics.bucket_avg.enabled.help', {
defaultMessage: 'Specifies whether this aggregation should be enabled',
}),
},
schema: {
types: ['string'],
help: i18n.translate('data.search.aggs.metrics.bucket_avg.schema.help', {
defaultMessage: 'Schema to use for this aggregation',
}),
},
customBucket: {
types: ['agg_type'],
help: i18n.translate('data.search.aggs.metrics.bucket_avg.customBucket.help', {
defaultMessage: 'Agg config to use for building sibling pipeline aggregations',
}),
},
customMetric: {
types: ['agg_type'],
help: i18n.translate('data.search.aggs.metrics.bucket_avg.customMetric.help', {
defaultMessage: 'Agg config to use for building sibling pipeline aggregations',
}),
},
json: {
types: ['string'],
help: i18n.translate('data.search.aggs.metrics.bucket_avg.json.help', {
defaultMessage: 'Advanced json to include when the agg is sent to Elasticsearch',
}),
},
},
fn: (input, args) => {
const { id, enabled, schema, ...rest } = args;

// Need to spread this object to work around TS bug:
// https://github.com/microsoft/TypeScript/issues/15300#issuecomment-436793742
const customBucket = args.customBucket?.value ? { ...args.customBucket.value } : undefined;
const customMetric = args.customMetric?.value ? { ...args.customMetric.value } : undefined;

return {
type: 'agg_type',
value: {
id,
enabled,
schema,
type: METRIC_TYPES.AVG_BUCKET,
params: {
...rest,
customBucket,
customMetric,
json: getParsedValue(args, 'json'),
},
},
};
},
});
6 changes: 6 additions & 0 deletions src/plugins/data/public/search/aggs/metrics/bucket_max.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ import { MetricAggType } from './metric_agg_type';
import { makeNestedLabel } from './lib/make_nested_label';
import { siblingPipelineAggHelper } from './lib/sibling_pipeline_agg_helper';
import { METRIC_TYPES } from './metric_agg_types';
import { AggConfigSerialized, BaseAggParams } from '../types';
import { GetInternalStartServicesFn } from '../../../types';

export interface AggParamsBucketMax extends BaseAggParams {
customMetric?: AggConfigSerialized;
customBucket?: AggConfigSerialized;
}

export interface BucketMaxMetricAggDependencies {
getInternalStartServices: GetInternalStartServicesFn;
}
Expand Down
Loading