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 bucket agg types #64772

Merged
merged 21 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
77eaef7
[data.search.aggs]: Expression functions for bucket agg types - range…
alexwizp Apr 29, 2020
3e64a00
new portion of changes
alexwizp Apr 30, 2020
59dbde7
Merge remote-tracking branch 'upstream/master' into 63762
alexwizp Apr 30, 2020
4e7b330
add geo_tile_fn
alexwizp Apr 30, 2020
21dd3cc
add geo_hash_fn
alexwizp Apr 30, 2020
a19709d
Update src/plugins/data/public/search/aggs/buckets/filter_fn.ts
alexwizp May 1, 2020
1247040
Update src/plugins/data/public/search/aggs/buckets/geo_tile_fn.ts
alexwizp May 1, 2020
54ba370
Update src/plugins/data/public/search/aggs/buckets/geo_hash_fn.ts
alexwizp May 1, 2020
7af5fec
Update src/plugins/data/public/search/aggs/buckets/ip_range_fn.ts
alexwizp May 1, 2020
3e176e1
Update src/plugins/data/public/search/aggs/buckets/geo_hash_fn.ts
alexwizp May 1, 2020
0589ebd
Update src/plugins/data/public/search/aggs/buckets/geo_hash_fn.ts
alexwizp May 1, 2020
4e783da
Update src/plugins/data/public/search/aggs/buckets/geo_hash_fn.ts
alexwizp May 1, 2020
57dca84
Merge remote-tracking branch 'upstream/master' into 63762
alexwizp May 4, 2020
4c8aca9
create BaseAggParams
alexwizp May 4, 2020
910c7b9
add filters_fn
alexwizp May 4, 2020
7c6a589
add histogram / date_histogram expression functions
alexwizp May 4, 2020
999b3f7
cleanup
alexwizp May 4, 2020
da334ae
terms - order should be optional
alexwizp May 4, 2020
f524122
add custom label params
alexwizp May 4, 2020
ece4919
Merge remote-tracking branch 'upstream/master' into 63762
alexwizp May 4, 2020
24b3db5
Merge branch 'master' into 63762
elasticmachine May 5, 2020
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 @@ -7,5 +7,5 @@
<b>Signature:</b>

```typescript
from: number;
from: number | string;
```
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export interface DateRangeKey

| Property | Type | Description |
| --- | --- | --- |
| [from](./kibana-plugin-plugins-data-public.daterangekey.from.md) | <code>number</code> | |
| [to](./kibana-plugin-plugins-data-public.daterangekey.to.md) | <code>number</code> | |
| [from](./kibana-plugin-plugins-data-public.daterangekey.from.md) | <code>number &#124; string</code> | |
| [to](./kibana-plugin-plugins-data-public.daterangekey.to.md) | <code>number &#124; string</code> | |

Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<b>Signature:</b>

```typescript
to: number;
to: number | string;
```
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,5 @@
<b>Signature:</b>

```typescript
export declare type IpRangeKey = {
type: 'mask';
mask: string;
} | {
type: 'range';
from: string;
to: string;
};
export declare type IpRangeKey = CidrMaskIpRangeAggKey | RangeIpRangeAggKey;
```
16 changes: 6 additions & 10 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Component } from 'react';
import { CoreSetup } from 'src/core/public';
import { CoreStart } from 'kibana/public';
import { CoreStart as CoreStart_2 } from 'src/core/public';
import { Ensure } from '@kbn/utility-types';
import { EuiButtonEmptyProps } from '@elastic/eui';
import { EuiComboBoxProps } from '@elastic/eui';
import { EuiConfirmModalProps } from '@elastic/eui';
Expand Down Expand Up @@ -271,9 +272,9 @@ export interface DataPublicPluginStart {
// @public (undocumented)
export interface DateRangeKey {
// (undocumented)
from: number;
from: number | string;
// (undocumented)
to: number;
to: number | string;
}

// @public (undocumented)
Expand Down Expand Up @@ -1101,17 +1102,12 @@ export type InputTimeRange = TimeRange | {
to: Moment;
};

// Warning: (ae-forgotten-export) The symbol "CidrMaskIpRangeAggKey" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "RangeIpRangeAggKey" needs to be exported by the entry point index.d.ts
// Warning: (ae-missing-release-tag) "IpRangeKey" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
export type IpRangeKey = {
type: 'mask';
mask: string;
} | {
type: 'range';
from: string;
to: string;
};
export type IpRangeKey = CidrMaskIpRangeAggKey | RangeIpRangeAggKey;

// Warning: (ae-missing-release-tag) "IRequestTypesMap" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
Expand Down
23 changes: 14 additions & 9 deletions src/plugins/data/public/search/aggs/agg_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import _ from 'lodash';
import { i18n } from '@kbn/i18n';
import { Assign } from '@kbn/utility-types';
import { Assign, Ensure } from '@kbn/utility-types';
import { ExpressionAstFunction, ExpressionAstArgument } from 'src/plugins/expressions/public';
import { IAggType } from './agg_type';
import { writeParams } from './agg_params';
Expand All @@ -31,17 +31,22 @@ import { FieldFormatsStart } from '../../field_formats';

type State = string | number | boolean | null | undefined | SerializableState;

interface SerializableState {
/** @internal **/
export interface SerializableState {
[key: string]: State | State[];
}

export interface AggConfigSerialized {
type: string;
enabled?: boolean;
id?: string;
params?: SerializableState;
schema?: string;
}
/** @internal **/
export type AggConfigSerialized = Ensure<
lukeelmers marked this conversation as resolved.
Show resolved Hide resolved
{
type: string;
enabled?: boolean;
id?: string;
params?: SerializableState;
schema?: string;
},
SerializableState
>;

export interface AggConfigDependencies {
fieldFormats: FieldFormatsStart;
Expand Down
19 changes: 18 additions & 1 deletion src/plugins/data/public/search/aggs/agg_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,23 @@ export const getAggTypes = ({
],
});

/** Buckets: **/
import { aggFilter } from './buckets/filter_fn';
import { aggSignificantTerms } from './buckets/significant_terms_fn';
import { aggIpRange } from './buckets/ip_range_fn';
import { aggDateRange } from './buckets/date_range_fn';
import { aggRange } from './buckets/range_fn';
import { aggGeoTile } from './buckets/geo_tile_fn';
import { aggGeoHash } from './buckets/geo_hash_fn';
import { aggTerms } from './buckets/terms_fn';

export const getAggTypesFunctions = () => [aggTerms];
export const getAggTypesFunctions = () => [
aggFilter,
aggSignificantTerms,
aggIpRange,
aggDateRange,
aggRange,
aggGeoTile,
aggGeoHash,
aggTerms,
];
7 changes: 7 additions & 0 deletions src/plugins/data/public/search/aggs/buckets/date_range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export interface DateRangeBucketAggDependencies {
getInternalStartServices: GetInternalStartServicesFn;
}

export interface AggParamsDateRange {
field: string;
alexwizp marked this conversation as resolved.
Show resolved Hide resolved
ranges?: DateRangeKey[];
time_zone?: string;
json?: string;
}

export const getDateRangeBucketAgg = ({
uiSettings,
getInternalStartServices,
Expand Down
101 changes: 101 additions & 0 deletions src/plugins/data/public/search/aggs/buckets/date_range_fn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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 { aggDateRange } from './date_range_fn';

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

test('fills in defaults when only required args are provided', () => {
const actual = fn({
field: 'date_field',
});
expect(actual).toMatchInlineSnapshot(`
Object {
"type": "agg_type",
"value": Object {
"enabled": true,
"id": undefined,
"params": Object {
"field": "date_field",
"json": undefined,
"ranges": undefined,
"time_zone": undefined,
},
"schema": undefined,
"type": "date_range",
},
}
`);
});

test('includes optional params when they are provided', () => {
const actual = fn({
field: 'date_field',
time_zone: 'UTC +3',
ranges: JSON.stringify([
{ from: 'now-1w/w', to: 'now' },
{ from: 1588163532470, to: 1588163532481 },
]),
});

expect(actual.value).toMatchInlineSnapshot(`
Object {
"enabled": true,
"id": undefined,
"params": Object {
"field": "date_field",
"json": undefined,
"ranges": Array [
Object {
"from": "now-1w/w",
"to": "now",
},
Object {
"from": 1588163532470,
"to": 1588163532481,
},
],
"time_zone": "UTC +3",
},
"schema": undefined,
"type": "date_range",
}
`);
});

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

expect(actual.value.params.json).toEqual({ foo: true });

expect(() => {
fn({
field: 'date_field',
json: '/// intentionally malformed json ///',
});
}).toThrowErrorMatchingInlineSnapshot(`"Unable to parse json argument string"`);
});
});
});
108 changes: 108 additions & 0 deletions src/plugins/data/public/search/aggs/buckets/date_range_fn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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, BUCKET_TYPES } from '../';
import { getParsedValue } from '../utils/get_parsed_value';

const fnName = 'aggDateRange';

type Input = any;
type AggArgs = AggExpressionFunctionArgs<typeof BUCKET_TYPES.DATE_RANGE>;

type Arguments = Assign<AggArgs, { ranges?: string }>;

type Output = AggExpressionType;
type FunctionDefinition = ExpressionFunctionDefinition<typeof fnName, Input, Arguments, Output>;

export const aggDateRange = (): FunctionDefinition => ({
name: fnName,
help: i18n.translate('data.search.aggs.function.buckets.dateRange.help', {
defaultMessage: 'Generates a serialized agg config for a Date Range agg',
}),
type: 'agg_type',
args: {
id: {
types: ['string'],
help: i18n.translate('data.search.aggs.buckets.dateRange.id.help', {
defaultMessage: 'ID for this aggregation',
}),
},
enabled: {
types: ['boolean'],
default: true,
help: i18n.translate('data.search.aggs.buckets.dateRange.enabled.help', {
defaultMessage: 'Specifies whether this aggregation should be enabled',
}),
},
schema: {
types: ['string'],
help: i18n.translate('data.search.aggs.buckets.dateRange.schema.help', {
defaultMessage: 'Schema to use for this aggregation',
}),
},
field: {
types: ['string'],
required: true,
help: i18n.translate('data.search.aggs.buckets.dateRange.field.help', {
defaultMessage: 'Field to use for this aggregation',
}),
},
ranges: {
types: ['string'],
required: false,
help: i18n.translate('data.search.aggs.buckets.dateRange.ranges.help', {
defaultMessage: 'Serialized ranges to use for this aggregation.',
}),
alexwizp marked this conversation as resolved.
Show resolved Hide resolved
},
time_zone: {
types: ['string'],
required: false,
help: i18n.translate('data.search.aggs.buckets.dateRange.timeZone.help', {
defaultMessage: 'Time zone to use for this aggregation.',
}),
},
json: {
types: ['string'],
help: i18n.translate('data.search.aggs.buckets.dateRange.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: BUCKET_TYPES.DATE_RANGE,
params: {
...rest,
json: getParsedValue(args, 'json'),
ranges: getParsedValue(args, 'ranges'),
},
},
};
},
});
7 changes: 7 additions & 0 deletions src/plugins/data/public/search/aggs/buckets/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { i18n } from '@kbn/i18n';
import { BucketAggType } from './bucket_agg_type';
import { BUCKET_TYPES } from './bucket_agg_types';
import { GetInternalStartServicesFn } from '../../../types';
import { GeoBoundingBox } from './lib/geo_point';

const filterTitle = i18n.translate('data.search.aggs.buckets.filterTitle', {
defaultMessage: 'Filter',
Expand All @@ -30,6 +31,12 @@ export interface FilterBucketAggDependencies {
getInternalStartServices: GetInternalStartServicesFn;
}

export interface AggParamsFilter {
field: string;
lukeelmers marked this conversation as resolved.
Show resolved Hide resolved
geo_bounding_box?: GeoBoundingBox;
json?: string;
}

export const getFilterBucketAgg = ({ getInternalStartServices }: FilterBucketAggDependencies) =>
new BucketAggType(
{
Expand Down
Loading