Skip to content

Commit

Permalink
Merge branch 'master' of github.com:elastic/kibana into dev/drilldown…
Browse files Browse the repository at this point in the history
…s/url-drilldown-helpers
  • Loading branch information
Dosant committed Oct 14, 2020
2 parents 4922834 + 2955584 commit 10c5017
Show file tree
Hide file tree
Showing 35 changed files with 610 additions and 194 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [AggConfig](./kibana-plugin-plugins-data-public.aggconfig.md) &gt; [getValueBucketPath](./kibana-plugin-plugins-data-public.aggconfig.getvaluebucketpath.md)

## AggConfig.getValueBucketPath() method

Returns the bucket path containing the main value the agg will produce (e.g. for sum of bytes it will point to the sum, for median it will point to the 50 percentile in the percentile multi value bucket)

<b>Signature:</b>

```typescript
getValueBucketPath(): string;
```
<b>Returns:</b>

`string`

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export declare class AggConfig
| [getResponseAggs()](./kibana-plugin-plugins-data-public.aggconfig.getresponseaggs.md) | | |
| [getTimeRange()](./kibana-plugin-plugins-data-public.aggconfig.gettimerange.md) | | |
| [getValue(bucket)](./kibana-plugin-plugins-data-public.aggconfig.getvalue.md) | | |
| [getValueBucketPath()](./kibana-plugin-plugins-data-public.aggconfig.getvaluebucketpath.md) | | Returns the bucket path containing the main value the agg will produce (e.g. for sum of bytes it will point to the sum, for median it will point to the 50 percentile in the percentile multi value bucket) |
| [isFilterable()](./kibana-plugin-plugins-data-public.aggconfig.isfilterable.md) | | |
| [makeLabel(percentageMode)](./kibana-plugin-plugins-data-public.aggconfig.makelabel.md) | | |
| [nextId(list)](./kibana-plugin-plugins-data-public.aggconfig.nextid.md) | <code>static</code> | Calculate the next id based on the ids in this list {<!-- -->array<!-- -->} list - a list of objects with id properties |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-expressions-public](./kibana-plugin-plugins-expressions-public.md) &gt; [Range](./kibana-plugin-plugins-expressions-public.range.md) &gt; [label](./kibana-plugin-plugins-expressions-public.range.label.md)

## Range.label property

<b>Signature:</b>

```typescript
label?: string;
```
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Range
| Property | Type | Description |
| --- | --- | --- |
| [from](./kibana-plugin-plugins-expressions-public.range.from.md) | <code>number</code> | |
| [label](./kibana-plugin-plugins-expressions-public.range.label.md) | <code>string</code> | |
| [to](./kibana-plugin-plugins-expressions-public.range.to.md) | <code>number</code> | |
| [type](./kibana-plugin-plugins-expressions-public.range.type.md) | <code>typeof name</code> | |

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-expressions-server](./kibana-plugin-plugins-expressions-server.md) &gt; [Range](./kibana-plugin-plugins-expressions-server.range.md) &gt; [label](./kibana-plugin-plugins-expressions-server.range.label.md)

## Range.label property

<b>Signature:</b>

```typescript
label?: string;
```
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Range
| Property | Type | Description |
| --- | --- | --- |
| [from](./kibana-plugin-plugins-expressions-server.range.from.md) | <code>number</code> | |
| [label](./kibana-plugin-plugins-expressions-server.range.label.md) | <code>string</code> | |
| [to](./kibana-plugin-plugins-expressions-server.range.to.md) | <code>number</code> | |
| [type](./kibana-plugin-plugins-expressions-server.range.type.md) | <code>typeof name</code> | |

9 changes: 9 additions & 0 deletions src/plugins/data/common/search/aggs/agg_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,15 @@ export class AggConfig {
return this.params.field;
}

/**
* Returns the bucket path containing the main value the agg will produce
* (e.g. for sum of bytes it will point to the sum, for median it will point
* to the 50 percentile in the percentile multi value bucket)
*/
getValueBucketPath() {
return this.type.getValueBucketPath(this);
}

makeLabel(percentageMode = false) {
if (this.params.customLabel) {
return this.params.customLabel;
Expand Down
9 changes: 9 additions & 0 deletions src/plugins/data/common/search/aggs/agg_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface AggTypeConfig<
getSerializedFormat?: (agg: TAggConfig) => SerializedFieldFormat;
getValue?: (agg: TAggConfig, bucket: any) => any;
getKey?: (bucket: any, key: any, agg: TAggConfig) => any;
getValueBucketPath?: (agg: TAggConfig) => string;
}

// TODO need to make a more explicit interface for this
Expand Down Expand Up @@ -210,6 +211,10 @@ export class AggType<
return this.params.find((p: TParam) => p.name === name);
};

getValueBucketPath = (agg: TAggConfig) => {
return agg.id;
};

/**
* Generic AggType Constructor
*
Expand All @@ -233,6 +238,10 @@ export class AggType<
this.createFilter = config.createFilter;
}

if (config.getValueBucketPath) {
this.getValueBucketPath = config.getValueBucketPath;
}

if (config.params && config.params.length && config.params[0] instanceof BaseParamType) {
this.params = config.params as TParam[];
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('AggConfig Filters', () => {
{
gte: 1024,
lt: 2048.0,
label: 'A custom label',
}
);

Expand All @@ -78,6 +79,7 @@ describe('AggConfig Filters', () => {
expect(filter.range).toHaveProperty('bytes');
expect(filter.range.bytes).toHaveProperty('gte', 1024.0);
expect(filter.range.bytes).toHaveProperty('lt', 2048.0);
expect(filter.range.bytes).not.toHaveProperty('label');
expect(filter.meta).toHaveProperty('formattedValue');
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { IBucketAggConfig } from '../bucket_agg_type';
export const createFilterRange = (
getFieldFormatsStart: AggTypesDependencies['getFieldFormatsStart']
) => {
return (aggConfig: IBucketAggConfig, params: any) => {
return (aggConfig: IBucketAggConfig, { label, ...params }: any) => {
const { deserialize } = getFieldFormatsStart();
return buildRangeFilter(
aggConfig.params.field,
Expand Down
9 changes: 7 additions & 2 deletions src/plugins/data/common/search/aggs/buckets/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface AggParamsRange extends BaseAggParams {
ranges?: Array<{
from: number;
to: number;
label?: string;
}>;
}

Expand Down Expand Up @@ -71,7 +72,7 @@ export const getRangeBucketAgg = ({ getFieldFormatsStart }: RangeBucketAggDepend

key = keys.get(id);
if (!key) {
key = new RangeKey(bucket);
key = new RangeKey(bucket, agg.params.ranges);
keys.set(id, key);
}

Expand Down Expand Up @@ -102,7 +103,11 @@ export const getRangeBucketAgg = ({ getFieldFormatsStart }: RangeBucketAggDepend
{ from: 1000, to: 2000 },
],
write(aggConfig, output) {
output.params.ranges = aggConfig.params.ranges;
output.params.ranges = (aggConfig.params as AggParamsRange).ranges?.map((range) => ({
to: range.to,
from: range.from,
}));

output.params.keyed = true;
},
},
Expand Down
24 changes: 23 additions & 1 deletion src/plugins/data/common/search/aggs/buckets/range_key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,36 @@

const id = Symbol('id');

type Ranges = Array<
Partial<{
from: string | number;
to: string | number;
label: string;
}>
>;

export class RangeKey {
[id]: string;
gte: string | number;
lt: string | number;
label?: string;

private findCustomLabel(
from: string | number | undefined | null,
to: string | number | undefined | null,
ranges?: Ranges
) {
return (ranges || []).find(
(range) =>
((from == null && range.from == null) || range.from === from) &&
((to == null && range.to == null) || range.to === to)
)?.label;
}

constructor(bucket: any) {
constructor(bucket: any, allRanges?: Ranges) {
this.gte = bucket.from == null ? -Infinity : bucket.from;
this.lt = bucket.to == null ? +Infinity : bucket.to;
this.label = this.findCustomLabel(bucket.from, bucket.to, allRanges);

this[id] = RangeKey.idBucket(bucket);
}
Expand Down
45 changes: 45 additions & 0 deletions src/plugins/data/common/search/aggs/buckets/terms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { AggConfigs } from '../agg_configs';
import { METRIC_TYPES } from '../metrics';
import { mockAggTypesRegistry } from '../test_helpers';
import { BUCKET_TYPES } from './bucket_agg_types';

Expand Down Expand Up @@ -133,5 +134,49 @@ describe('Terms Agg', () => {
expect(params.include).toStrictEqual([1.1, 2, 3.33]);
expect(params.exclude).toStrictEqual([4, 5.555, 6]);
});

test('uses correct bucket path for sorting by median', () => {
const indexPattern = {
id: '1234',
title: 'logstash-*',
fields: {
getByName: () => field,
filter: () => [field],
},
} as any;

const field = {
name: 'field',
indexPattern,
};

const aggConfigs = new AggConfigs(
indexPattern,
[
{
id: 'test',
params: {
field: {
name: 'string_field',
type: 'string',
},
orderAgg: {
type: METRIC_TYPES.MEDIAN,
params: {
field: {
name: 'number_field',
type: 'number',
},
},
},
},
type: BUCKET_TYPES.TERMS,
},
],
{ typesRegistry: mockAggTypesRegistry() }
);
const { [BUCKET_TYPES.TERMS]: params } = aggConfigs.aggs[0].toDsl();
expect(params.order).toEqual({ 'test-orderAgg.50': 'desc' });
});
});
});
5 changes: 2 additions & 3 deletions src/plugins/data/common/search/aggs/buckets/terms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import {
export const termsAggFilter = [
'!top_hits',
'!percentiles',
'!median',
'!std_dev',
'!derivative',
'!moving_avg',
Expand Down Expand Up @@ -198,14 +197,14 @@ export const getTermsBucketAgg = () =>
return;
}

const orderAggId = orderAgg.id;
const orderAggPath = orderAgg.getValueBucketPath();

if (orderAgg.parentId && aggs) {
orderAgg = aggs.byId(orderAgg.parentId);
}

output.subAggs = (output.subAggs || []).concat(orderAgg);
order[orderAggId] = dir;
order[orderAggPath] = dir;
},
},
{
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/data/common/search/aggs/metrics/median.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ describe('AggTypeMetricMedianProvider class', () => {
expect(dsl.median.percentiles.percents).toEqual([50]);
});

it('points to right value within multi metric for value bucket path', () => {
expect(aggConfigs.byId(METRIC_TYPES.MEDIAN)!.getValueBucketPath()).toEqual(
`${METRIC_TYPES.MEDIAN}.50`
);
});

it('converts the response', () => {
const agg = aggConfigs.getResponseAggs()[0];

Expand Down
3 changes: 3 additions & 0 deletions src/plugins/data/common/search/aggs/metrics/median.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export const getMedianMetricAgg = () => {
values: { field: aggConfig.getFieldDisplayName() },
});
},
getValueBucketPath(aggConfig) {
return `${aggConfig.id}.50`;
},
params: [
{
name: 'field',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ describe('getFormatWithAggs', () => {
expect(getFormat).toHaveBeenCalledTimes(1);
});

test('returns custom label for range if provided', () => {
const mapping = { id: 'range', params: {} };
const getFieldFormat = getFormatWithAggs(getFormat);
const format = getFieldFormat(mapping);

expect(format.convert({ gte: 1, lt: 20, label: 'custom' })).toBe('custom');
// underlying formatter is not called because custom label can be used directly
expect(getFormat).toHaveBeenCalledTimes(0);
});

test('creates custom format for terms', () => {
const mapping = {
id: 'terms',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export function getFormatWithAggs(getFieldFormat: GetFieldFormat): GetFieldForma
const customFormats: Record<string, () => IFieldFormat> = {
range: () => {
const RangeFormat = FieldFormat.from((range: any) => {
if (range.label) {
return range.label;
}
const nestedFormatter = params as SerializedFieldFormat;
const format = getFieldFormat({
id: nestedFormatter.id,
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export class AggConfig {
getTimeRange(): import("../../../public").TimeRange | undefined;
// (undocumented)
getValue(bucket: any): any;
getValueBucketPath(): string;
// (undocumented)
id: string;
// (undocumented)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface Range {
type: typeof name;
from: number;
to: number;
label?: string;
}

export const range: ExpressionTypeDefinition<typeof name, Range> = {
Expand All @@ -41,7 +42,7 @@ export const range: ExpressionTypeDefinition<typeof name, Range> = {
},
to: {
render: (value: Range): ExpressionValueRender<{ text: string }> => {
const text = `from ${value.from} to ${value.to}`;
const text = value?.label || `from ${value.from} to ${value.to}`;
return {
type: 'render',
as: 'text',
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/expressions/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,8 @@ export interface Range {
// (undocumented)
from: number;
// (undocumented)
label?: string;
// (undocumented)
to: number;
// Warning: (ae-forgotten-export) The symbol "name" needs to be exported by the entry point index.d.ts
//
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/expressions/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,8 @@ export interface Range {
// (undocumented)
from: number;
// (undocumented)
label?: string;
// (undocumented)
to: number;
// Warning: (ae-forgotten-export) The symbol "name" needs to be exported by the entry point index.d.ts
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('OrderAggParamEditor component', () => {

mount(<OrderByParamEditor {...props} />);

expect(setValue).toHaveBeenCalledWith('agg5');
expect(setValue).toHaveBeenCalledWith('agg3');
});

it('defaults to the _key metric if no agg is compatible', () => {
Expand Down
Loading

0 comments on commit 10c5017

Please sign in to comment.