Skip to content

Commit

Permalink
[Field formats] Correctly format numeric histograms outside Discover (e…
Browse files Browse the repository at this point in the history
…lastic#91576) (elastic#93486)

* [Field formats] Correctly format numeric histograms outside Discover

* Fix types

* Fix types

* Fix failures

* Fix merge issue

* Fix tests

* Fix i18n

* Fix i18n

* Add doc and fix integration

Co-authored-by: Kibana Machine <[email protected]>

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
Wylie Conlon and kibanamachine authored Mar 3, 2021
1 parent 5ef8a6f commit d740ddf
Show file tree
Hide file tree
Showing 32 changed files with 415 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ fieldFormats: {
UrlFormat: typeof UrlFormat;
StringFormat: typeof StringFormat;
TruncateFormat: typeof TruncateFormat;
HistogramFormat: typeof HistogramFormat;
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ fieldFormats: {
UrlFormat: typeof UrlFormat;
StringFormat: typeof StringFormat;
TruncateFormat: typeof TruncateFormat;
HistogramFormat: typeof HistogramFormat;
}
```
8 changes: 7 additions & 1 deletion docs/management/managing-fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,17 @@ include::field-formatters/string-formatter.asciidoc[]
[[field-formatters-numeric]]
=== Numeric field formatters

Numeric fields support the `Url`, `Bytes`, `Duration`, `Number`, `Percentage`, `String`, and `Color` formatters.
Numeric fields support the `Url`, `Bytes`, `Duration`, `Number`, `Percentage`, `Histogram`, `String`, and `Color` formatters.

The `Bytes`, `Number`, and `Percentage` formatters enable you to choose the display formats of numbers in this field using
the <<numeral, Elastic numeral pattern>> syntax that {kib} maintains.

The `Histogram` formatter is only used for the {ref}/histogram.html[histogram field type]. When using the `Histogram` formatter,
you can apply the `Number`, `Bytes`, or `Percentage` format to the aggregated data.

`Number`, and `Percentage` formatters enable you to choose the display formats of numbers in this field using
the <<numeral, Elastic numeral pattern>> syntax that {kib} maintains.

include::field-formatters/url-formatter.asciidoc[]

include::field-formatters/string-formatter.asciidoc[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
StringFormat,
TruncateFormat,
UrlFormat,
HistogramFormat,
} from '../converters';

export const baseFormatters: FieldFormatInstanceType[] = [
Expand All @@ -38,4 +39,5 @@ export const baseFormatters: FieldFormatInstanceType[] = [
StringFormat,
TruncateFormat,
UrlFormat,
HistogramFormat,
];
51 changes: 51 additions & 0 deletions src/plugins/data/common/field_formats/converters/histogram.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { i18n } from '@kbn/i18n';
import { FieldFormat } from '../field_format';
import { KBN_FIELD_TYPES } from '../../kbn_field_types/types';
import { TextContextTypeConvert, FIELD_FORMAT_IDS } from '../types';
import { BytesFormat } from './bytes';
import { NumberFormat } from './number';
import { PercentFormat } from './percent';

export class HistogramFormat extends FieldFormat {
static id = FIELD_FORMAT_IDS.HISTOGRAM;
static fieldType = KBN_FIELD_TYPES.HISTOGRAM;
static title = i18n.translate('data.fieldFormats.histogram.title', {
defaultMessage: 'Histogram',
});

id = HistogramFormat.id;
title = HistogramFormat.title;
allowsNumericalAggregations = true;

// Nested internal formatter
getParamDefaults() {
return {
id: 'number',
params: {},
};
}

textConvert: TextContextTypeConvert = (val) => {
if (typeof val === 'number') {
const subFormatId = this.param('id');
const SubFormat =
subFormatId === 'bytes'
? BytesFormat
: subFormatId === 'percent'
? PercentFormat
: NumberFormat;
const converter = new SubFormat(this.param('params'), this.getConfig);
return converter.textConvert(val);
} else {
return JSON.stringify(val);
}
};
}
1 change: 1 addition & 0 deletions src/plugins/data/common/field_formats/converters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export { ColorFormat } from './color';
export { TruncateFormat } from './truncate';
export { BoolFormat } from './boolean';
export { StaticLookupFormat } from './static_lookup';
export { HistogramFormat } from './histogram';
1 change: 1 addition & 0 deletions src/plugins/data/common/field_formats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {
UrlFormat,
StringFormat,
TruncateFormat,
HistogramFormat,
} from './converters';

export { getHighlightRequest } from './utils';
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/field_formats/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export enum FIELD_FORMAT_IDS {
STRING = 'string',
TRUNCATE = 'truncate',
URL = 'url',
HISTOGRAM = 'histogram',
}

export interface FieldFormatConfig {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/search/aggs/metrics/avg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const getAvgMetricAgg = () => {
name: METRIC_TYPES.AVG,
expressionName: aggAvgFnName,
title: averageTitle,
valueType: 'number',
makeLabel: (aggConfig) => {
return i18n.translate('data.search.aggs.metrics.averageLabel', {
defaultMessage: 'Average {field}',
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/search/aggs/metrics/max.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ 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
1 change: 1 addition & 0 deletions src/plugins/data/common/search/aggs/metrics/median.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const getMedianMetricAgg = () => {
expressionName: aggMedianFnName,
dslName: 'percentiles',
title: medianTitle,
valueType: 'number',
makeLabel(aggConfig) {
return i18n.translate('data.search.aggs.metrics.medianLabel', {
defaultMessage: 'Median {field}',
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/search/aggs/metrics/min.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ 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
1 change: 1 addition & 0 deletions src/plugins/data/common/search/aggs/metrics/percentiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const getPercentilesMetricAgg = () => {
title: i18n.translate('data.search.aggs.metrics.percentilesTitle', {
defaultMessage: 'Percentiles',
}),
valueType: 'number',
makeLabel(agg) {
return i18n.translate('data.search.aggs.metrics.percentilesLabel', {
defaultMessage: 'Percentiles of {field}',
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/search/aggs/metrics/sum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const getSumMetricAgg = () => {
name: METRIC_TYPES.SUM,
expressionName: aggSumFnName,
title: sumTitle,
valueType: 'number',
makeLabel(aggConfig) {
return i18n.translate('data.search.aggs.metrics.sumLabel', {
defaultMessage: 'Sum of {field}',
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ import {
UrlFormat,
StringFormat,
TruncateFormat,
HistogramFormat,
} from '../common/field_formats';

import { DateNanosFormat, DateFormat } from './field_formats';
Expand Down Expand Up @@ -188,6 +189,7 @@ export const fieldFormats = {
UrlFormat,
StringFormat,
TruncateFormat,
HistogramFormat,
};

export {
Expand Down
72 changes: 37 additions & 35 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,7 @@ export const fieldFormats: {
UrlFormat: typeof UrlFormat;
StringFormat: typeof StringFormat;
TruncateFormat: typeof TruncateFormat;
HistogramFormat: typeof HistogramFormat;
};

// @public (undocumented)
Expand Down Expand Up @@ -2646,41 +2647,42 @@ export const UI_SETTINGS: {
// src/plugins/data/public/index.ts:127:21 - (ae-forgotten-export) The symbol "getEsQueryConfig" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:127:21 - (ae-forgotten-export) The symbol "luceneStringToDsl" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:127:21 - (ae-forgotten-export) The symbol "decorateQuery" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:167:26 - (ae-forgotten-export) The symbol "FieldFormatsRegistry" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:167:26 - (ae-forgotten-export) The symbol "BoolFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:167:26 - (ae-forgotten-export) The symbol "BytesFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:167:26 - (ae-forgotten-export) The symbol "ColorFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:167:26 - (ae-forgotten-export) The symbol "DurationFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:167:26 - (ae-forgotten-export) The symbol "IpFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:167:26 - (ae-forgotten-export) The symbol "NumberFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:167:26 - (ae-forgotten-export) The symbol "PercentFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:167:26 - (ae-forgotten-export) The symbol "RelativeDateFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:167:26 - (ae-forgotten-export) The symbol "SourceFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:167:26 - (ae-forgotten-export) The symbol "StaticLookupFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:167:26 - (ae-forgotten-export) The symbol "UrlFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:167:26 - (ae-forgotten-export) The symbol "StringFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:167:26 - (ae-forgotten-export) The symbol "TruncateFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:209:23 - (ae-forgotten-export) The symbol "datatableToCSV" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:234:27 - (ae-forgotten-export) The symbol "isFilterable" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:234:27 - (ae-forgotten-export) The symbol "isNestedField" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:234:27 - (ae-forgotten-export) The symbol "validateIndexPattern" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:234:27 - (ae-forgotten-export) The symbol "flattenHitWrapper" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:234:27 - (ae-forgotten-export) The symbol "formatHitProvider" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:399:20 - (ae-forgotten-export) The symbol "getRequestInspectorStats" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:399:20 - (ae-forgotten-export) The symbol "getResponseInspectorStats" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:399:20 - (ae-forgotten-export) The symbol "tabifyAggResponse" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:399:20 - (ae-forgotten-export) The symbol "tabifyGetColumns" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:401:1 - (ae-forgotten-export) The symbol "CidrMask" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:402:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:411:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:412:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:413:1 - (ae-forgotten-export) The symbol "Ipv4Address" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:414:1 - (ae-forgotten-export) The symbol "isDateHistogramBucketAggConfig" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:418:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:419:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:422:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:423:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:426:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:168:26 - (ae-forgotten-export) The symbol "FieldFormatsRegistry" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:168:26 - (ae-forgotten-export) The symbol "BoolFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:168:26 - (ae-forgotten-export) The symbol "BytesFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:168:26 - (ae-forgotten-export) The symbol "ColorFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:168:26 - (ae-forgotten-export) The symbol "DurationFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:168:26 - (ae-forgotten-export) The symbol "IpFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:168:26 - (ae-forgotten-export) The symbol "NumberFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:168:26 - (ae-forgotten-export) The symbol "PercentFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:168:26 - (ae-forgotten-export) The symbol "RelativeDateFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:168:26 - (ae-forgotten-export) The symbol "SourceFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:168:26 - (ae-forgotten-export) The symbol "StaticLookupFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:168:26 - (ae-forgotten-export) The symbol "UrlFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:168:26 - (ae-forgotten-export) The symbol "StringFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:168:26 - (ae-forgotten-export) The symbol "TruncateFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:168:26 - (ae-forgotten-export) The symbol "HistogramFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:211:23 - (ae-forgotten-export) The symbol "datatableToCSV" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:236:27 - (ae-forgotten-export) The symbol "isFilterable" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:236:27 - (ae-forgotten-export) The symbol "isNestedField" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:236:27 - (ae-forgotten-export) The symbol "validateIndexPattern" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:236:27 - (ae-forgotten-export) The symbol "flattenHitWrapper" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:236:27 - (ae-forgotten-export) The symbol "formatHitProvider" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:401:20 - (ae-forgotten-export) The symbol "getRequestInspectorStats" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:401:20 - (ae-forgotten-export) The symbol "getResponseInspectorStats" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:401:20 - (ae-forgotten-export) The symbol "tabifyAggResponse" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:401:20 - (ae-forgotten-export) The symbol "tabifyGetColumns" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:403:1 - (ae-forgotten-export) The symbol "CidrMask" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:404:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:413:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:414:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:415:1 - (ae-forgotten-export) The symbol "Ipv4Address" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:416:1 - (ae-forgotten-export) The symbol "isDateHistogramBucketAggConfig" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:420:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:421:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:424:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:425:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:428:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/query/state_sync/connect_to_query_state.ts:34:5 - (ae-forgotten-export) The symbol "FilterStateStore" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/search/session/session_service.ts:42:5 - (ae-forgotten-export) The symbol "UrlGeneratorStateMapping" needs to be exported by the entry point index.d.ts

Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ import {
UrlFormat,
StringFormat,
TruncateFormat,
HistogramFormat,
} from '../common/field_formats';

export const fieldFormats = {
Expand All @@ -113,6 +114,7 @@ export const fieldFormats = {
UrlFormat,
StringFormat,
TruncateFormat,
HistogramFormat,
};

export { IFieldFormatsRegistry, FieldFormatsGetConfigFn, FieldFormatConfig } from '../common';
Expand Down
Loading

0 comments on commit d740ddf

Please sign in to comment.