Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into feature/mappings-…
Browse files Browse the repository at this point in the history
…editor
  • Loading branch information
sebelga committed Oct 8, 2019
2 parents 9d34a5d + 0edc83a commit 3f0ca29
Show file tree
Hide file tree
Showing 112 changed files with 243,440 additions and 958 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"dependencies": {
"@babel/core": "^7.5.5",
"@babel/register": "^7.5.5",
"@elastic/charts": "^12.1.0",
"@elastic/charts": "^13.4.0",
"@elastic/datemath": "5.0.2",
"@elastic/eui": "14.4.0",
"@elastic/filesaver": "1.1.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ describe('metric agg make_nested_label', function () {
customMetric: {
makeLabel: () => { return metricLabel; }
}
},
getParam(key) {
return this.params[key];
}
};
}
Expand Down
10 changes: 8 additions & 2 deletions src/legacy/ui/public/agg_types/agg_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ export class AggConfig {
});
}

getParam(key: string): any {
return _.get(this.params, key);
}

write(aggs?: AggConfigs) {
return writeParams(this.type.params, this, aggs);
}
Expand All @@ -203,7 +207,9 @@ export class AggConfig {
}

createFilter(key: string, params = {}) {
if (!this.isFilterable()) {
const createFilter = this.type.createFilter;

if (!createFilter) {
throw new TypeError(`The "${this.type.title}" aggregation does not support filtering.`);
}

Expand All @@ -217,7 +223,7 @@ export class AggConfig {
throw new TypeError(message);
}

return this.type.createFilter(this, key, params);
return createFilter(this, key, params);
}

/**
Expand Down
34 changes: 20 additions & 14 deletions src/legacy/ui/public/agg_types/agg_params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,16 @@
* under the License.
*/

import { AggParamType } from 'ui/agg_types/param_types/agg';
import { AggParamType } from './param_types/agg';
import { FieldParamType } from './param_types/field';
import { OptionedParamType } from './param_types/optioned';
import { StringParamType } from './param_types/string';
import { JsonParamType } from './param_types/json';
import { BaseParamType } from './param_types/base';

import { AggConfig } from './agg_config';
import { AggConfigs } from './agg_configs';

export type AggParam = BaseParamType;

export interface AggParamOption {
val: string;
display: string;
enabled?(agg: AggConfig): boolean;
}

const paramTypeMap = {
field: FieldParamType,
optioned: OptionedParamType,
Expand All @@ -43,16 +36,29 @@ const paramTypeMap = {
_default: BaseParamType,
} as Record<string, any>;

interface AggParamConfig {
export type AggParam = BaseParamType;

export interface AggParamOption {
val: string;
display: string;
enabled?(agg: AggConfig): boolean;
}

export interface AggParamConfig {
type: string;
}

export const initParams = (params: AggParamConfig[]): AggParam[] => {
return params.map((config: AggParamConfig) => {
export const initParams = <
TAggParam extends AggParam = AggParam,
TAggParamConfig extends AggParamConfig = AggParamConfig
>(
params: TAggParamConfig[]
): TAggParam[] =>
params.map((config: TAggParamConfig) => {
const Class = paramTypeMap[config.type] || paramTypeMap._default;

return new Class(config);
}) as AggParam[];
};
}) as TAggParam[];

/**
* Reads an aggConfigs
Expand Down
72 changes: 38 additions & 34 deletions src/legacy/ui/public/agg_types/agg_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,52 @@
* under the License.
*/

import _ from 'lodash';
import { constant, noop, identity } from 'lodash';
import { i18n } from '@kbn/i18n';
import { BaseParamType } from 'ui/agg_types/param_types';
import { AggParam, initParams } from './agg_params';
// @ts-ignore
import { FieldFormat, fieldFormats } from '../registry/field_formats';
import { AggConfig } from '../vis';
import { AggConfigs } from './agg_configs';
import { SearchSource } from '../courier';
import { Adapters } from '../inspector';
import { BaseParamType } from './param_types/base';

export interface AggTypeConfig {
export interface AggTypeConfig<
TAggConfig extends AggConfig = AggConfig,
TParam extends AggParam = TAggConfig['params'][number]
> {
name: string;
type?: string;
title: string;
createFilter?: (aggConfig: TAggConfig, key: any, params?: any) => any;
type?: string;
dslName?: string;
makeLabel?: () => string;
makeLabel?: ((aggConfig: TAggConfig) => string) | (() => string);
ordered?: any;
hasNoDsl?: boolean;
createFilter: (aggConfig: AggConfig, key: any, params?: any) => any;
params?: AggParam[];
getRequestAggs?: () => AggConfig[];
getResponseAggs?: () => AggConfig[];
params?: Array<Partial<TParam>>;
getRequestAggs?: ((aggConfig: TAggConfig) => TAggConfig[]) | (() => TAggConfig[] | void);
getResponseAggs?: ((aggConfig: TAggConfig) => TAggConfig[]) | (() => TAggConfig[] | void);
customLabels?: boolean;
decorateAggConfig?: () => Record<string, any>;
postFlightRequest?: (
resp: any,
aggConfigs: AggConfigs,
aggConfig: AggConfig,
aggConfig: TAggConfig,
searchSource: SearchSource,
inspectorAdapters: Adapters
) => Promise<any>;
getFormat?: (agg: AggConfig) => FieldFormat;
getValue?: (agg: AggConfig, bucket: any) => any;
getKey?: (bucket: any, key: any, agg: AggConfig) => any;
getFormat?: (agg: TAggConfig) => FieldFormat;
getValue?: (agg: TAggConfig, bucket: any) => any;
getKey?: (bucket: any, key: any, agg: TAggConfig) => any;
}

const getFormat = (agg: AggConfig) => {
const field = agg.getField();
return field ? field.format : fieldFormats.getDefaultInstance('string');
};

const defaultGetValue = (agg: AggConfig, bucket: any) => {};

export class AggType {
export class AggType<TAggConfig extends AggConfig = AggConfig> {
/**
* the unique, unchanging, name that we have assigned this aggType
*
Expand Down Expand Up @@ -93,7 +94,7 @@ export class AggType {
* @param {AggConfig} aggConfig - an agg config of this type
* @returns {string} - label that can be used in the ui to describe the aggConfig
*/
makeLabel: (aggConfig?: AggConfig) => string;
makeLabel: ((aggConfig: TAggConfig) => string) | (() => string);
/**
* Describes if this aggType creates data that is ordered, and if that ordered data
* is some sort of time series.
Expand Down Expand Up @@ -123,14 +124,14 @@ export class AggType {
* @param {mixed} key The key for the bucket
* @returns {object} The filter
*/
createFilter: (aggConfig: AggConfig, key: any, params?: any) => any;
createFilter: ((aggConfig: TAggConfig, key: any, params?: any) => any) | undefined;
/**
* An instance of {{#crossLink "AggParams"}}{{/crossLink}}.
*
* @property params
* @type {AggParams}
*/
params: AggParam[];
params: TAggConfig['params'];
/**
* Designed for multi-value metric aggs, this method can return a
* set of AggConfigs that should replace this aggConfig in requests
Expand All @@ -140,7 +141,7 @@ export class AggType {
* that should replace this one,
* or undefined
*/
getRequestAggs: ((aggConfig?: AggConfig) => AggConfig[]) | (() => void);
getRequestAggs: ((aggConfig: TAggConfig) => TAggConfig[]) | (() => TAggConfig[] | void);
/**
* Designed for multi-value metric aggs, this method can return a
* set of AggConfigs that should replace this aggConfig in result sets
Expand All @@ -151,7 +152,7 @@ export class AggType {
* that should replace this one,
* or undefined
*/
getResponseAggs: ((aggConfig?: AggConfig) => AggConfig[]) | (() => void);
getResponseAggs: ((aggConfig: TAggConfig) => TAggConfig[]) | (() => TAggConfig[] | void);
/**
* A function that will be called each time an aggConfig of this type
* is created, giving the agg type a chance to modify the agg config
Expand All @@ -170,7 +171,7 @@ export class AggType {
postFlightRequest: (
resp: any,
aggConfigs: AggConfigs,
aggConfig: AggConfig,
aggConfig: TAggConfig,
searchSource: SearchSource,
inspectorAdapters: Adapters,
abortSignal?: AbortSignal
Expand All @@ -183,12 +184,12 @@ export class AggType {
* @param {agg} agg - the agg to pick a format for
* @return {FieldFormat}
*/
getFormat: (agg: AggConfig) => FieldFormat;
getFormat: (agg: TAggConfig) => FieldFormat;

getValue: (agg: AggConfig, bucket: any) => any;
getValue: (agg: TAggConfig, bucket: any) => any;

paramByName = (name: string) => {
return this.params.find(p => p.name === name);
return this.params.find((p: AggParam) => p.name === name);
};

/**
Expand All @@ -200,18 +201,21 @@ export class AggType {
* @private
* @param {object} config - used to set the properties of the AggType
*/
constructor(config: AggTypeConfig) {
constructor(config: AggTypeConfig<TAggConfig>) {
this.name = config.name;
this.type = config.type || 'metrics';
this.dslName = config.dslName || config.name;
this.title = config.title;
this.makeLabel = config.makeLabel || _.constant(this.name);
this.makeLabel = config.makeLabel || constant(this.name);
this.ordered = config.ordered;
this.hasNoDsl = !!config.hasNoDsl;
this.createFilter = config.createFilter;

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

if (config.params && config.params.length && config.params[0] instanceof BaseParamType) {
this.params = config.params;
this.params = config.params as TAggConfig['params'];
} else {
// always append the raw JSON param
const params: any[] = config.params ? [...config.params] : [];
Expand All @@ -229,18 +233,18 @@ export class AggType {
defaultMessage: 'Custom label',
}),
type: 'string',
write: _.noop,
write: noop,
});
}

this.params = initParams(params as any);
this.params = initParams(params);
}

this.getRequestAggs = config.getRequestAggs || (() => {});
this.getRequestAggs = config.getRequestAggs || noop;
this.getResponseAggs = config.getResponseAggs || (() => {});
this.decorateAggConfig = config.decorateAggConfig || (() => ({}));
this.postFlightRequest = config.postFlightRequest || _.identity;
this.postFlightRequest = config.postFlightRequest || identity;
this.getFormat = config.getFormat || getFormat;
this.getValue = config.getValue || defaultGetValue;
this.getValue = config.getValue || ((agg: TAggConfig, bucket: any) => {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,29 @@
* under the License.
*/

import { MetricAggType } from './metric_agg_type';
import { i18n } from '@kbn/i18n';
import { MetricAggType } from './metric_agg_type';
import { METRIC_TYPES } from './metric_agg_types';
import { KBN_FIELD_TYPES } from '../../../../../plugins/data/common';

const averageTitle = i18n.translate('common.ui.aggTypes.metrics.averageTitle', {
defaultMessage: 'Average',
});

export const avgMetricAgg = new MetricAggType({
name: 'avg',
title: i18n.translate('common.ui.aggTypes.metrics.averageTitle', {
defaultMessage: 'Average'
}),
makeLabel: function (aggConfig) {
name: METRIC_TYPES.AVG,
title: averageTitle,
makeLabel: aggConfig => {
return i18n.translate('common.ui.aggTypes.metrics.averageLabel', {
defaultMessage: 'Average {field}',
values: { field: aggConfig.getFieldDisplayName() }
values: { field: aggConfig.getFieldDisplayName() },
});
},
params: [
{
name: 'field',
type: 'field',
filterFieldTypes: 'number'
}
]
filterFieldTypes: KBN_FIELD_TYPES.NUMBER,
},
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,41 @@
* under the License.
*/

import { i18n } from '@kbn/i18n';
import { get } from 'lodash';

import { MetricAggType } from './metric_agg_type';
import { makeNestedLabel } from './lib/make_nested_label';
import { siblingPipelineAggHelper } from './lib/sibling_pipeline_agg_helper';
import { i18n } from '@kbn/i18n';
import { METRIC_TYPES } from './metric_agg_types';

const overallAverageLabel = i18n.translate('common.ui.aggTypes.metrics.overallAverageLabel', {
defaultMessage: 'overall average'
defaultMessage: 'overall average',
});

const averageBucketTitle = i18n.translate('common.ui.aggTypes.metrics.averageBucketTitle', {
defaultMessage: 'Average Bucket',
});

export const bucketAvgMetricAgg = new MetricAggType({
name: 'avg_bucket',
title: i18n.translate('common.ui.aggTypes.metrics.averageBucketTitle', {
defaultMessage: 'Average Bucket'
}),
name: METRIC_TYPES.AVG_BUCKET,
title: averageBucketTitle,
makeLabel: agg => makeNestedLabel(agg, overallAverageLabel),
subtype: siblingPipelineAggHelper.subtype,
params: [
...siblingPipelineAggHelper.params()
],
params: [...siblingPipelineAggHelper.params()],
getFormat: siblingPipelineAggHelper.getFormat,
getValue: function (agg, bucket) {
const customMetric = agg.params.customMetric;
getValue(agg, bucket) {
const customMetric = agg.getParam('customMetric');
const customBucket = agg.getParam('customBucket');
const scaleMetrics = customMetric.type && customMetric.type.isScalable();

let value = bucket[agg.id] && bucket[agg.id].value;
if (scaleMetrics && agg.params.customBucket.type.name === 'date_histogram') {
const aggInfo = agg.params.customBucket.write();

if (scaleMetrics && customBucket.type.name === 'date_histogram') {
const aggInfo = customBucket.write();

value *= get(aggInfo, 'bucketInterval.scale', 1);
}
return value;
}
},
});
Loading

0 comments on commit 3f0ca29

Please sign in to comment.