Skip to content

Commit

Permalink
[aggs] fix valueType for top hit, top metric and filtered metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
mbondyra committed Oct 25, 2023
1 parent 1fdcb41 commit 74c0d21
Show file tree
Hide file tree
Showing 19 changed files with 36 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/plugins/data/common/search/aggs/agg_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ export class AggConfig {
}

if (aggParam.deserialize) {
const isTyped = _.isFunction(aggParam.valueType);
const valueType = aggParam.getValueType?.(this);
const isTyped = _.isFunction(valueType);

const isType = isTyped && val instanceof aggParam.valueType;
const isType = isTyped && val instanceof valueType;
const isObject = !isTyped && _.isObject(val);
const isDeserialized = isType || isObject;

Expand Down
6 changes: 3 additions & 3 deletions src/plugins/data/common/search/aggs/agg_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface AggTypeConfig<
hasNoDsl?: boolean;
hasNoDslParams?: boolean;
params?: Array<Partial<TParam>>;
valueType?: DatatableColumnType;
getValueType?: (aggConfig: TAggConfig) => DatatableColumnType;
getRequestAggs?: ((aggConfig: TAggConfig) => TAggConfig[]) | (() => TAggConfig[] | void);
getResponseAggs?: ((aggConfig: TAggConfig) => TAggConfig[]) | (() => TAggConfig[] | void);
customLabels?: boolean;
Expand Down Expand Up @@ -105,7 +105,7 @@ export class AggType<
* The type the values produced by this agg will have in the final data table.
* If not specified, the type of the field is used.
*/
valueType?: DatatableColumnType;
getValueType?: (aggConfig: TAggConfig) => DatatableColumnType;
/**
* a function that will be called when this aggType is assigned to
* an aggConfig, and that aggConfig is being rendered (in a form, chart, etc.).
Expand Down Expand Up @@ -261,7 +261,7 @@ export class AggType<
this.dslName = config.dslName || config.name;
this.expressionName = config.expressionName;
this.title = config.title;
this.valueType = config.valueType;
this.getValueType = config.getValueType;
this.makeLabel = config.makeLabel || constant(this.name);
this.ordered = config.ordered;
this.hasNoDsl = !!config.hasNoDsl;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/search/aggs/metrics/avg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const getAvgMetricAgg = () => {
name: METRIC_TYPES.AVG,
expressionName: aggAvgFnName,
title: averageTitle,
valueType: 'number',
getValueType: () => 'number',
makeLabel: (aggConfig) => {
return i18n.translate('data.search.aggs.metrics.averageLabel', {
defaultMessage: 'Average {field}',
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/search/aggs/metrics/cardinality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface AggParamsCardinality extends BaseAggParams {
export const getCardinalityMetricAgg = () =>
new MetricAggType({
name: METRIC_TYPES.CARDINALITY,
valueType: 'number',
getValueType: () => 'number',
expressionName: aggCardinalityFnName,
title: uniqueCountTitle,
enableEmptyAsNull: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ export const getFilteredMetricAgg = ({ getConfig }: FiltersMetricAggDependencies
const customBucket = agg.getParam('customBucket');
return bucket && bucket[customBucket.id] && customMetric.getValue(bucket[customBucket.id]);
},
getValueType(agg) {
const customMetric = agg.getParam('customMetric');
return (
customMetric.type.getValueType?.(customMetric) ||
customMetric.params.field?.type ||
'number'
);
},
getValueBucketPath(agg) {
const customBucket = agg.getParam('customBucket');
const customMetric = agg.getParam('customMetric');
Expand Down
1 change: 0 additions & 1 deletion src/plugins/data/common/search/aggs/metrics/max.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const getMaxMetricAgg = () => {
name: METRIC_TYPES.MAX,
expressionName: aggMaxFnName,
title: maxTitle,
valueType: 'number',
makeLabel(aggConfig) {
return i18n.translate('data.search.aggs.metrics.maxLabel', {
defaultMessage: 'Max {field}',
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/search/aggs/metrics/median.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const getMedianMetricAgg = () => {
expressionName: aggMedianFnName,
dslName: 'percentiles',
title: medianTitle,
valueType: 'number',
getValueType: () => 'number',
makeLabel(aggConfig) {
return i18n.translate('data.search.aggs.metrics.medianLabel', {
defaultMessage: 'Median {field}',
Expand Down
1 change: 0 additions & 1 deletion src/plugins/data/common/search/aggs/metrics/min.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const getMinMetricAgg = () => {
name: METRIC_TYPES.MIN,
expressionName: aggMinFnName,
title: minTitle,
valueType: 'number',
makeLabel(aggConfig) {
return i18n.translate('data.search.aggs.metrics.minLabel', {
defaultMessage: 'Min {field}',
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/search/aggs/metrics/percentiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const getPercentilesMetricAgg = () => {
title: i18n.translate('data.search.aggs.metrics.percentilesTitle', {
defaultMessage: 'Percentiles',
}),
valueType: 'number',
getValueType: () => 'number',
makeLabel(agg) {
return i18n.translate('data.search.aggs.metrics.percentilesLabel', {
defaultMessage: 'Percentiles of {field}',
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/search/aggs/metrics/rate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const getRateMetricAgg = () => {
name: METRIC_TYPES.RATE,
expressionName: aggRateFnName,
title: rateTitle,
valueType: 'number',
getValueType: () => 'number',
makeLabel: (aggConfig) => {
return i18n.translate('data.search.aggs.metrics.rateLabel', {
defaultMessage: 'Rate of {field} per {unit}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const getSinglePercentileMetricAgg = () => {
expressionName: aggSinglePercentileFnName,
dslName: 'percentiles',
title: singlePercentileTitle,
valueType: 'number',
getValueType: () => 'number',
makeLabel(aggConfig) {
return i18n.translate('data.search.aggs.metrics.singlePercentileLabel', {
defaultMessage: 'Percentile {field}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const getSinglePercentileRankMetricAgg = () => {
expressionName: aggSinglePercentileRankFnName,
dslName: 'percentile_ranks',
title: singlePercentileTitle,
valueType: 'number',
getValueType: () => 'number',
makeLabel(aggConfig) {
return i18n.translate('data.search.aggs.metrics.singlePercentileRankLabel', {
defaultMessage: 'Percentile rank of {field}',
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/search/aggs/metrics/sum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const getSumMetricAgg = () => {
name: METRIC_TYPES.SUM,
expressionName: aggSumFnName,
title: sumTitle,
valueType: 'number',
getValueType: () => 'number',
enableEmptyAsNull: true,
makeLabel(aggConfig) {
return i18n.translate('data.search.aggs.metrics.sumLabel', {
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/data/common/search/aggs/metrics/top_hit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export const getTopHitMetricAgg = () => {
title: i18n.translate('data.search.aggs.metrics.topHitTitle', {
defaultMessage: 'Top Hit',
}),
getValueType: (aggConfig) => {
return aggConfig.getParam('field')?.type;
},
makeLabel(aggConfig) {
const lastPrefixLabel = i18n.translate('data.search.aggs.metrics.topHit.lastPrefixLabel', {
defaultMessage: 'Last',
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/data/common/search/aggs/metrics/top_metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export const getTopMetricsMetricAgg = () => {
title: i18n.translate('data.search.aggs.metrics.topMetricsTitle', {
defaultMessage: 'Top metrics',
}),
getValueType: (aggConfig) => {
return aggConfig.getParam('field')?.type;
},
makeLabel(aggConfig) {
const isDescOrder = aggConfig.getParam('sortOrder').value === 'desc';
const size = aggConfig.getParam('size');
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/search/aggs/metrics/value_count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface AggParamsValueCount extends BaseAggParams {
export const getValueCountMetricAgg = () =>
new MetricAggType({
name: METRIC_TYPES.VALUE_COUNT,
valueType: 'number',
getValueType: () => 'number',
expressionName: aggValueCountFnName,
title: valueCountTitle,
enableEmptyAsNull: true,
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/search/aggs/param_types/agg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ export class AggParamType<
}

this.makeAgg = config.makeAgg;
this.valueType = AggConfig;
this.getValueType = () => AggConfig;
}
}
6 changes: 3 additions & 3 deletions src/plugins/data/common/search/aggs/param_types/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ export class BaseParamType<TAggConfig extends IAggConfig = IAggConfig> {
deserialize: (value: any, aggConfig?: TAggConfig) => any;
toExpressionAst?: (value: any) => ExpressionAstExpression[] | ExpressionAstExpression | undefined;
options: any[];
valueType?: any;

getValueType: (aggConfig: IAggConfig) => any;
onChange?(agg: TAggConfig): void;
shouldShow?(agg: TAggConfig): boolean;

Expand Down Expand Up @@ -71,6 +70,7 @@ export class BaseParamType<TAggConfig extends IAggConfig = IAggConfig> {
this.options = config.options;
this.modifyAggConfigOnSearchRequestStart =
config.modifyAggConfigOnSearchRequestStart || function () {};
this.valueType = config.valueType || config.type;

this.getValueType = config.getValueType;
}
}
4 changes: 3 additions & 1 deletion src/plugins/data/common/search/tabify/response_writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ export class TabbedAggResponseWriter {
name: column.name,
meta: {
type:
column.aggConfig.type.valueType || column.aggConfig.params.field?.type || 'number',
column.aggConfig.type.getValueType?.(column.aggConfig) ||
column.aggConfig.params.field?.type ||
'number',
field: column.aggConfig.params.field?.name,
index: column.aggConfig.getIndexPattern()?.title,
params: column.aggConfig.toSerializedFieldFormat(),
Expand Down

0 comments on commit 74c0d21

Please sign in to comment.