-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[data.search.aggs]: Expression functions for metric agg types (#64914)
- Loading branch information
1 parent
a467fe5
commit 2ccc397
Showing
65 changed files
with
4,105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/plugins/data/public/search/aggs/metrics/avg_fn.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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 { | ||
"customLabel": undefined, | ||
"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"`); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* 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', | ||
}), | ||
}, | ||
customLabel: { | ||
types: ['string'], | ||
help: i18n.translate('data.search.aggs.metrics.avg.customLabel.help', { | ||
defaultMessage: 'Represents a custom label for this aggregation', | ||
}), | ||
}, | ||
}, | ||
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'), | ||
}, | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/plugins/data/public/search/aggs/metrics/bucket_avg_fn.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* 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, | ||
"customLabel": undefined, | ||
"customMetric": undefined, | ||
"json": undefined, | ||
}, | ||
"schema": undefined, | ||
"type": "avg_bucket", | ||
}, | ||
"customLabel": undefined, | ||
"customMetric": Object { | ||
"enabled": true, | ||
"id": undefined, | ||
"params": Object { | ||
"customBucket": undefined, | ||
"customLabel": 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"`); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.