diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.fieldformats.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.fieldformats.md index d39871b99f74..b51421741933 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.fieldformats.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.fieldformats.md @@ -10,7 +10,6 @@ fieldFormats: { FieldFormat: typeof FieldFormat; FieldFormatsRegistry: typeof FieldFormatsRegistry; - serialize: (agg: import("./search").AggConfig) => import("../../expressions").SerializedFieldFormat; DEFAULT_CONVERTER_COLOR: { range: string; regex: string; diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.fieldformats.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.fieldformats.md index 11f18a195d27..45fc1a608e8c 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.fieldformats.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.fieldformats.md @@ -10,7 +10,6 @@ fieldFormats: { FieldFormatsRegistry: typeof FieldFormatsRegistry; FieldFormat: typeof FieldFormat; - serializeFieldFormat: (agg: import("../public/search").AggConfig) => import("../../expressions").SerializedFieldFormat; BoolFormat: typeof BoolFormat; BytesFormat: typeof BytesFormat; ColorFormat: typeof ColorFormat; diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.plugin.setup.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.plugin.setup.md index 13c69d6bf754..a6fdfdf6891c 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.plugin.setup.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.plugin.setup.md @@ -10,7 +10,7 @@ setup(core: CoreSetup, { usageCollection }: DataPluginSetupDependencies): { search: ISearchSetup; fieldFormats: { - register: (customFieldFormat: import("../public").FieldFormatInstanceType) => number; + register: (customFieldFormat: import("../common").FieldFormatInstanceType) => number; }; }; ``` @@ -27,7 +27,7 @@ setup(core: CoreSetup, { usageCollection }: DataPluginS `{ search: ISearchSetup; fieldFormats: { - register: (customFieldFormat: import("../public").FieldFormatInstanceType) => number; + register: (customFieldFormat: import("../common").FieldFormatInstanceType) => number; }; }` diff --git a/src/legacy/ui/public/visualize/loader/pipeline_helpers/index.ts b/src/legacy/ui/public/visualize/loader/pipeline_helpers/index.ts deleted file mode 100644 index f9a2234d6e5a..000000000000 --- a/src/legacy/ui/public/visualize/loader/pipeline_helpers/index.ts +++ /dev/null @@ -1,21 +0,0 @@ -/* - * 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. - */ - -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -export { buildPipeline } from '../../../../../../plugins/visualizations/public/legacy/build_pipeline'; diff --git a/src/legacy/ui/public/visualize/loader/pipeline_helpers/utilities.ts b/src/legacy/ui/public/visualize/loader/pipeline_helpers/utilities.ts deleted file mode 100644 index cb25c66dfd2f..000000000000 --- a/src/legacy/ui/public/visualize/loader/pipeline_helpers/utilities.ts +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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 { npStart } from 'ui/new_platform'; -import { fieldFormats, IFieldFormat } from '../../../../../../plugins/data/public'; -import { SerializedFieldFormat } from '../../../../../../plugins/expressions/common/types'; - -type FormatFactory = (mapping?: SerializedFieldFormat) => IFieldFormat; - -const createFormat = fieldFormats.serialize; -const getFormat: FormatFactory = (mapping?) => { - return npStart.plugins.data.fieldFormats.deserialize(mapping as any); -}; - -export { getFormat, createFormat, FormatFactory }; diff --git a/src/plugins/data/common/field_formats/index.ts b/src/plugins/data/common/field_formats/index.ts index 5c67073c07dd..104ff030873a 100644 --- a/src/plugins/data/common/field_formats/index.ts +++ b/src/plugins/data/common/field_formats/index.ts @@ -40,7 +40,7 @@ export { TruncateFormat, } from './converters'; -export { getHighlightRequest, serializeFieldFormat } from './utils'; +export { getHighlightRequest } from './utils'; export { DEFAULT_CONVERTER_COLOR } from './constants/color_default'; export { FIELD_FORMAT_IDS } from './types'; diff --git a/src/plugins/data/common/field_formats/utils/index.ts b/src/plugins/data/common/field_formats/utils/index.ts index 3832c941ffad..eb020c17ca09 100644 --- a/src/plugins/data/common/field_formats/utils/index.ts +++ b/src/plugins/data/common/field_formats/utils/index.ts @@ -22,6 +22,5 @@ import { IFieldFormat } from '../index'; export { asPrettyString } from './as_pretty_string'; export { getHighlightHtml, getHighlightRequest } from './highlight'; -export { serializeFieldFormat } from './serialize'; export type FormatFactory = (mapping?: SerializedFieldFormat) => IFieldFormat; diff --git a/src/plugins/data/common/field_formats/utils/serialize.ts b/src/plugins/data/common/field_formats/utils/serialize.ts deleted file mode 100644 index 1092c90d1945..000000000000 --- a/src/plugins/data/common/field_formats/utils/serialize.ts +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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 { IAggConfig } from 'src/plugins/data/public'; -import { SerializedFieldFormat } from '../../../../expressions/common/types'; - -export const serializeFieldFormat = (agg: IAggConfig): SerializedFieldFormat => { - const format: SerializedFieldFormat = agg.params.field ? agg.params.field.format.toJSON() : {}; - const formats: Record SerializedFieldFormat> = { - date_range: () => ({ id: 'date_range', params: format }), - ip_range: () => ({ id: 'ip_range', params: format }), - percentile_ranks: () => ({ id: 'percent' }), - count: () => ({ id: 'number' }), - cardinality: () => ({ id: 'number' }), - date_histogram: () => ({ - id: 'date', - params: { - pattern: (agg as any).buckets.getScaledDateFormat(), - }, - }), - terms: () => ({ - id: 'terms', - params: { - id: format.id, - otherBucketLabel: agg.params.otherBucketLabel, - missingBucketLabel: agg.params.missingBucketLabel, - ...format.params, - }, - }), - range: () => ({ - id: 'range', - params: { id: format.id, ...format.params }, - }), - }; - - return formats[agg.type.name] ? formats[agg.type.name]() : format; -}; diff --git a/src/plugins/data/public/index.ts b/src/plugins/data/public/index.ts index 1554ac71f8c5..984ce18aa4d8 100644 --- a/src/plugins/data/public/index.ts +++ b/src/plugins/data/public/index.ts @@ -168,7 +168,6 @@ import { UrlFormat, StringFormat, TruncateFormat, - serializeFieldFormat, } from '../common/field_formats'; import { DateFormat } from './field_formats'; @@ -179,8 +178,6 @@ export const fieldFormats = { FieldFormat, FieldFormatsRegistry, // exported only for tests. Consider mock. - serialize: serializeFieldFormat, - DEFAULT_CONVERTER_COLOR, HTML_CONTEXT_TYPE, TEXT_CONTEXT_TYPE, diff --git a/src/plugins/data/public/public.api.md b/src/plugins/data/public/public.api.md index 23213d4d1165..31dc5b51a06f 100644 --- a/src/plugins/data/public/public.api.md +++ b/src/plugins/data/public/public.api.md @@ -154,6 +154,7 @@ import { SearchParams } from 'elasticsearch'; import { SearchResponse as SearchResponse_2 } from 'elasticsearch'; import { SearchShardsParams } from 'elasticsearch'; import { SearchTemplateParams } from 'elasticsearch'; +import { SerializedFieldFormat as SerializedFieldFormat_2 } from 'src/plugins/expressions/public'; import { SimpleSavedObject } from 'src/core/public'; import { SnapshotCreateParams } from 'elasticsearch'; import { SnapshotCreateRepositoryParams } from 'elasticsearch'; @@ -621,7 +622,6 @@ export type FieldFormatInstanceType = (new (params?: any, getConfig?: FieldForma export const fieldFormats: { FieldFormat: typeof FieldFormat; FieldFormatsRegistry: typeof FieldFormatsRegistry; - serialize: (agg: import("./search").AggConfig) => import("../../expressions").SerializedFieldFormat; DEFAULT_CONVERTER_COLOR: { range: string; regex: string; @@ -1954,41 +1954,41 @@ export const UI_SETTINGS: { // src/plugins/data/public/index.ts:136:21 - (ae-forgotten-export) The symbol "getEsQueryConfig" needs to be exported by the entry point index.d.ts // src/plugins/data/public/index.ts:136:21 - (ae-forgotten-export) The symbol "luceneStringToDsl" needs to be exported by the entry point index.d.ts // src/plugins/data/public/index.ts:136:21 - (ae-forgotten-export) The symbol "decorateQuery" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "FieldFormatsRegistry" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "BoolFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "BytesFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "ColorFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "DateNanosFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "DurationFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "IpFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "NumberFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "PercentFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "RelativeDateFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "SourceFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "StaticLookupFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "UrlFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "StringFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "TruncateFormat" 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 "getFromSavedObject" 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:376:20 - (ae-forgotten-export) The symbol "getRequestInspectorStats" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:376:20 - (ae-forgotten-export) The symbol "getResponseInspectorStats" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:376:20 - (ae-forgotten-export) The symbol "tabifyAggResponse" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:376:20 - (ae-forgotten-export) The symbol "tabifyGetColumns" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:378:1 - (ae-forgotten-export) The symbol "CidrMask" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:379:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:388:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:389:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:390:1 - (ae-forgotten-export) The symbol "isDateHistogramBucketAggConfig" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:394:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:395:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:398:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:399:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:402:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "FieldFormatsRegistry" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "BoolFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "BytesFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "ColorFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "DateNanosFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "DurationFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "IpFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "NumberFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "PercentFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "RelativeDateFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "SourceFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "StaticLookupFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "UrlFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "StringFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "TruncateFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:233:27 - (ae-forgotten-export) The symbol "isFilterable" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:233:27 - (ae-forgotten-export) The symbol "isNestedField" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:233:27 - (ae-forgotten-export) The symbol "validateIndexPattern" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:233:27 - (ae-forgotten-export) The symbol "getFromSavedObject" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:233:27 - (ae-forgotten-export) The symbol "flattenHitWrapper" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:233:27 - (ae-forgotten-export) The symbol "formatHitProvider" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:373:20 - (ae-forgotten-export) The symbol "getRequestInspectorStats" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:373:20 - (ae-forgotten-export) The symbol "getResponseInspectorStats" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:373:20 - (ae-forgotten-export) The symbol "tabifyAggResponse" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:373:20 - (ae-forgotten-export) The symbol "tabifyGetColumns" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:375:1 - (ae-forgotten-export) The symbol "CidrMask" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:376:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:385:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:386:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:387:1 - (ae-forgotten-export) The symbol "isDateHistogramBucketAggConfig" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:391:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:392:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:395:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:396:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:399: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:40:60 - (ae-forgotten-export) The symbol "FilterStateStore" needs to be exported by the entry point index.d.ts // src/plugins/data/public/types.ts:52:5 - (ae-forgotten-export) The symbol "createFiltersFromValueClickAction" needs to be exported by the entry point index.d.ts // src/plugins/data/public/types.ts:53:5 - (ae-forgotten-export) The symbol "createFiltersFromRangeSelectAction" needs to be exported by the entry point index.d.ts diff --git a/src/plugins/data/public/search/aggs/agg_config.test.ts b/src/plugins/data/public/search/aggs/agg_config.test.ts index 6a0dad07b69b..95e0b2cd2718 100644 --- a/src/plugins/data/public/search/aggs/agg_config.test.ts +++ b/src/plugins/data/public/search/aggs/agg_config.test.ts @@ -25,7 +25,11 @@ import { AggType } from './agg_type'; import { AggTypesRegistryStart } from './agg_types_registry'; import { mockDataServices, mockAggTypesRegistry } from './test_helpers'; import { MetricAggType } from './metrics/metric_agg_type'; -import { Field as IndexPatternField, IndexPattern } from '../../index_patterns'; +import { + Field as IndexPatternField, + IndexPattern, + IIndexPatternFieldList, +} from '../../index_patterns'; import { stubIndexPatternWithFields } from '../../../public/stubs'; import { FieldFormatsStart } from '../../field_formats'; import { fieldFormatsServiceMock } from '../../field_formats/mocks'; @@ -370,6 +374,109 @@ describe('AggConfig', () => { }); }); + describe('#toSerializedFieldFormat', () => { + beforeEach(() => { + indexPattern.fields.getByName = identity as IIndexPatternFieldList['getByName']; + }); + + it('works with aggs that have a special format type', () => { + const configStates = [ + { + type: 'count', + params: {}, + }, + { + type: 'date_histogram', + params: { field: '@timestamp' }, + }, + { + type: 'terms', + params: { field: 'machine.os.keyword' }, + }, + ]; + const ac = new AggConfigs(indexPattern, configStates, { typesRegistry, fieldFormats }); + + expect(ac.aggs.map((agg) => agg.toSerializedFieldFormat())).toMatchInlineSnapshot(` + Array [ + Object { + "id": "number", + }, + Object { + "id": "date", + "params": Object { + "pattern": "HH:mm:ss.SSS", + }, + }, + Object { + "id": "terms", + "params": Object { + "id": undefined, + "missingBucketLabel": "Missing", + "otherBucketLabel": "Other", + }, + }, + ] + `); + }); + + it('works with pipeline aggs', () => { + const configStates = [ + { + type: 'max_bucket', + params: { + customMetric: { + type: 'cardinality', + params: { + field: 'bytes', + }, + }, + }, + }, + { + type: 'cumulative_sum', + params: { + buckets_path: '1', + customMetric: { + type: 'cardinality', + params: { + field: 'bytes', + }, + }, + }, + }, + { + type: 'percentile_ranks', + id: 'myMetricAgg', + params: {}, + }, + { + type: 'cumulative_sum', + params: { + metricAgg: 'myMetricAgg', + }, + }, + ]; + const ac = new AggConfigs(indexPattern, configStates, { typesRegistry, fieldFormats }); + + expect(ac.aggs.map((agg) => agg.toSerializedFieldFormat())).toMatchInlineSnapshot(` + Array [ + Object { + "id": "number", + }, + Object { + "id": "number", + }, + Object { + "id": "percent", + }, + Object { + "id": "percent", + }, + ] + `); + }); + }); + describe('#toExpressionAst', () => { beforeEach(() => { fieldFormats.getDefaultInstance = (() => ({ diff --git a/src/plugins/data/public/search/aggs/agg_config.ts b/src/plugins/data/public/search/aggs/agg_config.ts index ee4116eefc0e..a2b74eca5847 100644 --- a/src/plugins/data/public/search/aggs/agg_config.ts +++ b/src/plugins/data/public/search/aggs/agg_config.ts @@ -20,7 +20,11 @@ import _ from 'lodash'; import { i18n } from '@kbn/i18n'; import { Assign, Ensure } from '@kbn/utility-types'; -import { ExpressionAstFunction, ExpressionAstArgument } from 'src/plugins/expressions/public'; +import { + ExpressionAstFunction, + ExpressionAstArgument, + SerializedFieldFormat, +} from 'src/plugins/expressions/public'; import { IAggType } from './agg_type'; import { writeParams } from './agg_params'; import { IAggConfigs } from './agg_configs'; @@ -42,7 +46,7 @@ export type AggConfigSerialized = Ensure< type: string; enabled?: boolean; id?: string; - params?: SerializableState; + params?: {} | SerializableState; schema?: string; }, SerializableState @@ -298,8 +302,8 @@ export class AggConfig { id: this.id, enabled: this.enabled, type: this.type && this.type.name, - schema: this.schema, params: outParams as SerializableState, + ...(this.schema && { schema: this.schema }), }; } @@ -310,6 +314,19 @@ export class AggConfig { return this.serialize(); } + /** + * Returns a serialized field format for the field used in this agg. + * This can be passed to fieldFormats.deserialize to get the field + * format instance. + * + * @public + */ + toSerializedFieldFormat(): + | {} + | Ensure, SerializableState> { + return this.type ? this.type.getSerializedFormat(this) : {}; + } + /** * @returns Returns an ExpressionAst representing the function for this agg type. */ diff --git a/src/plugins/data/public/search/aggs/agg_type.test.ts b/src/plugins/data/public/search/aggs/agg_type.test.ts index 18783bbd9a76..cc45b935d45b 100644 --- a/src/plugins/data/public/search/aggs/agg_type.test.ts +++ b/src/plugins/data/public/search/aggs/agg_type.test.ts @@ -199,5 +199,73 @@ describe('AggType Class', () => { expect(aggType.getFormat(aggConfig)).toBe('default'); }); }); + + describe('getSerializedFormat', () => { + test('returns the default serialized field format if it exists', () => { + const aggConfig = ({ + params: { + field: { + format: { + toJSON: () => ({ id: 'format' }), + }, + }, + }, + } as unknown) as IAggConfig; + const aggType = new AggType( + { + name: 'name', + title: 'title', + }, + dependencies + ); + expect(aggType.getSerializedFormat(aggConfig)).toMatchInlineSnapshot(` + Object { + "id": "format", + } + `); + }); + + test('returns an empty object if a field param does not exist', () => { + const aggConfig = ({ + params: {}, + } as unknown) as IAggConfig; + const aggType = new AggType( + { + name: 'name', + title: 'title', + }, + dependencies + ); + expect(aggType.getSerializedFormat(aggConfig)).toMatchInlineSnapshot(`Object {}`); + }); + + test('uses a custom getSerializedFormat function if defined', () => { + const aggConfig = ({ + params: { + field: { + format: { + toJSON: () => ({ id: 'format' }), + }, + }, + }, + } as unknown) as IAggConfig; + const getSerializedFormat = jest.fn().mockReturnValue({ id: 'hello' }); + const aggType = new AggType( + { + name: 'name', + title: 'title', + getSerializedFormat, + }, + dependencies + ); + const serialized = aggType.getSerializedFormat(aggConfig); + expect(getSerializedFormat).toHaveBeenCalledWith(aggConfig); + expect(serialized).toMatchInlineSnapshot(` + Object { + "id": "hello", + } + `); + }); + }); }); }); diff --git a/src/plugins/data/public/search/aggs/agg_type.ts b/src/plugins/data/public/search/aggs/agg_type.ts index fb0cb609a08c..e909cd8134e8 100644 --- a/src/plugins/data/public/search/aggs/agg_type.ts +++ b/src/plugins/data/public/search/aggs/agg_type.ts @@ -19,8 +19,10 @@ import { constant, noop, identity } from 'lodash'; import { i18n } from '@kbn/i18n'; -import { initParams } from './agg_params'; +import { SerializedFieldFormat } from 'src/plugins/expressions/public'; + +import { initParams } from './agg_params'; import { AggConfig } from './agg_config'; import { IAggConfigs } from './agg_configs'; import { Adapters } from '../../../../../plugins/inspector/public'; @@ -57,6 +59,7 @@ export interface AggTypeConfig< abortSignal?: AbortSignal ) => Promise; getFormat?: (agg: TAggConfig) => IFieldFormat; + getSerializedFormat?: (agg: TAggConfig) => SerializedFieldFormat; getValue?: (agg: TAggConfig, bucket: any) => any; getKey?: (bucket: any, key: any, agg: TAggConfig) => any; } @@ -204,6 +207,17 @@ export class AggType< */ getFormat: (agg: TAggConfig) => IFieldFormat; + /** + * Get the serialized format for the values produced by this agg type, + * overridden by several metrics that always output a simple number. + * You can pass this output to fieldFormatters.deserialize to get + * the formatter instance. + * + * @param {agg} agg - the agg to pick a format for + * @return {SerializedFieldFormat} + */ + getSerializedFormat: (agg: TAggConfig) => SerializedFieldFormat; + getValue: (agg: TAggConfig, bucket: any) => any; getKey?: (bucket: any, key: any, agg: TAggConfig) => any; @@ -277,6 +291,13 @@ export class AggType< return field ? field.format : fieldFormats.getDefaultInstance(KBN_FIELD_TYPES.STRING); }); + + this.getSerializedFormat = + config.getSerializedFormat || + ((agg: TAggConfig) => { + return agg.params.field ? agg.params.field.format.toJSON() : {}; + }); + this.getValue = config.getValue || ((agg: TAggConfig, bucket: any) => {}); } } diff --git a/src/plugins/data/public/search/aggs/buckets/date_histogram.ts b/src/plugins/data/public/search/aggs/buckets/date_histogram.ts index 8a5596f669cb..fc42d43b2fea 100644 --- a/src/plugins/data/public/search/aggs/buckets/date_histogram.ts +++ b/src/plugins/data/public/search/aggs/buckets/date_histogram.ts @@ -152,6 +152,14 @@ export const getDateHistogramBucketAgg = ({ (key: string) => uiSettings.get(key) ); }, + getSerializedFormat(agg) { + return { + id: 'date', + params: { + pattern: agg.buckets.getScaledDateFormat(), + }, + }; + }, params: [ { name: 'field', diff --git a/src/plugins/data/public/search/aggs/buckets/date_range.ts b/src/plugins/data/public/search/aggs/buckets/date_range.ts index 447347dbfbe1..3e14ab422ccb 100644 --- a/src/plugins/data/public/search/aggs/buckets/date_range.ts +++ b/src/plugins/data/public/search/aggs/buckets/date_range.ts @@ -70,6 +70,12 @@ export const getDateRangeBucketAgg = ({ }); return new DateRangeFormat(); }, + getSerializedFormat(agg) { + return { + id: 'date_range', + params: agg.params.field ? agg.params.field.format.toJSON() : {}, + }; + }, makeLabel(aggConfig) { return aggConfig.getFieldDisplayName() + ' date ranges'; }, diff --git a/src/plugins/data/public/search/aggs/buckets/ip_range.ts b/src/plugins/data/public/search/aggs/buckets/ip_range.ts index 10fdb2d93b56..b3e90bdf9b56 100644 --- a/src/plugins/data/public/search/aggs/buckets/ip_range.ts +++ b/src/plugins/data/public/search/aggs/buckets/ip_range.ts @@ -78,6 +78,12 @@ export const getIpRangeBucketAgg = ({ getInternalStartServices }: IpRangeBucketA }); return new IpRangeFormat(); }, + getSerializedFormat(agg) { + return { + id: 'ip_range', + params: agg.params.field ? agg.params.field.format.toJSON() : {}, + }; + }, makeLabel(aggConfig) { return i18n.translate('data.search.aggs.buckets.ipRangeLabel', { defaultMessage: '{fieldName} IP ranges', diff --git a/src/plugins/data/public/search/aggs/buckets/lib/time_buckets/time_buckets.ts b/src/plugins/data/public/search/aggs/buckets/lib/time_buckets/time_buckets.ts index b8d6586652d6..12197c85f4a9 100644 --- a/src/plugins/data/public/search/aggs/buckets/lib/time_buckets/time_buckets.ts +++ b/src/plugins/data/public/search/aggs/buckets/lib/time_buckets/time_buckets.ts @@ -48,7 +48,7 @@ function isValidMoment(m: any): boolean { return m && 'isValid' in m && m.isValid(); } -export interface TimeBucketsConfig { +export interface TimeBucketsConfig extends Record { 'histogram:maxBars': number; 'histogram:barTarget': number; dateFormat: string; diff --git a/src/plugins/data/public/search/aggs/buckets/range.test.ts b/src/plugins/data/public/search/aggs/buckets/range.test.ts index 4c2d3af1ab73..2b8a36f2fbdb 100644 --- a/src/plugins/data/public/search/aggs/buckets/range.test.ts +++ b/src/plugins/data/public/search/aggs/buckets/range.test.ts @@ -104,7 +104,7 @@ describe('Range Agg', () => { ); }; - describe('formating', () => { + describe('formatting', () => { test('formats bucket keys properly', () => { const aggConfigs = getAggConfigs(); const agg = aggConfigs.aggs[0]; @@ -115,4 +115,22 @@ describe('Range Agg', () => { expect(format(buckets[2])).toBe('≥ 2.5 KB and < +∞'); }); }); + + describe('getSerializedFormat', () => { + test('generates a serialized field format in the expected shape', () => { + const aggConfigs = getAggConfigs(); + const agg = aggConfigs.aggs[0]; + expect(agg.type.getSerializedFormat(agg)).toMatchInlineSnapshot(` + Object { + "id": "range", + "params": Object { + "id": "number", + "params": Object { + "pattern": "0,0.[000] b", + }, + }, + } + `); + }); + }); }); diff --git a/src/plugins/data/public/search/aggs/buckets/range.ts b/src/plugins/data/public/search/aggs/buckets/range.ts index 02aad3bd5fed..543e5d66b9fa 100644 --- a/src/plugins/data/public/search/aggs/buckets/range.ts +++ b/src/plugins/data/public/search/aggs/buckets/range.ts @@ -101,6 +101,16 @@ export const getRangeBucketAgg = ({ getInternalStartServices }: RangeBucketAggDe formats.set(agg, aggFormat); return aggFormat; }, + getSerializedFormat(agg) { + const format = agg.params.field ? agg.params.field.format.toJSON() : {}; + return { + id: 'range', + params: { + id: format.id, + params: format.params, + }, + }; + }, params: [ { name: 'field', diff --git a/src/plugins/data/public/search/aggs/buckets/terms.ts b/src/plugins/data/public/search/aggs/buckets/terms.ts index 45a76f08ddd1..1e8e9ab4ef9d 100644 --- a/src/plugins/data/public/search/aggs/buckets/terms.ts +++ b/src/plugins/data/public/search/aggs/buckets/terms.ts @@ -104,6 +104,18 @@ export const getTermsBucketAgg = ({ getInternalStartServices }: TermsBucketAggDe }, } as IFieldFormat; }, + getSerializedFormat(agg) { + const format = agg.params.field ? agg.params.field.format.toJSON() : {}; + return { + id: 'terms', + params: { + id: format.id, + otherBucketLabel: agg.params.otherBucketLabel, + missingBucketLabel: agg.params.missingBucketLabel, + ...format.params, + }, + }; + }, createFilter: createFilterTerms, postFlightRequest: async ( resp: any, diff --git a/src/plugins/data/public/search/aggs/metrics/bucket_avg.ts b/src/plugins/data/public/search/aggs/metrics/bucket_avg.ts index 927e9a7ae445..38312ec5cfa8 100644 --- a/src/plugins/data/public/search/aggs/metrics/bucket_avg.ts +++ b/src/plugins/data/public/search/aggs/metrics/bucket_avg.ts @@ -46,14 +46,17 @@ const averageBucketTitle = i18n.translate('data.search.aggs.metrics.averageBucke export const getBucketAvgMetricAgg = ({ getInternalStartServices, }: BucketAvgMetricAggDependencies) => { + const { subtype, params, getFormat, getSerializedFormat } = siblingPipelineAggHelper; + return new MetricAggType( { name: METRIC_TYPES.AVG_BUCKET, title: averageBucketTitle, makeLabel: (agg) => makeNestedLabel(agg, overallAverageLabel), - subtype: siblingPipelineAggHelper.subtype, - params: [...siblingPipelineAggHelper.params()], - getFormat: siblingPipelineAggHelper.getFormat, + subtype, + params: [...params()], + getFormat, + getSerializedFormat, getValue(agg, bucket) { const customMetric = agg.getParam('customMetric'); const customBucket = agg.getParam('customBucket'); diff --git a/src/plugins/data/public/search/aggs/metrics/bucket_max.ts b/src/plugins/data/public/search/aggs/metrics/bucket_max.ts index 2b171fcbd24f..e2c6a5105bac 100644 --- a/src/plugins/data/public/search/aggs/metrics/bucket_max.ts +++ b/src/plugins/data/public/search/aggs/metrics/bucket_max.ts @@ -45,14 +45,17 @@ const maxBucketTitle = i18n.translate('data.search.aggs.metrics.maxBucketTitle', export const getBucketMaxMetricAgg = ({ getInternalStartServices, }: BucketMaxMetricAggDependencies) => { + const { subtype, params, getFormat, getSerializedFormat } = siblingPipelineAggHelper; + return new MetricAggType( { name: METRIC_TYPES.MAX_BUCKET, title: maxBucketTitle, makeLabel: (agg) => makeNestedLabel(agg, overallMaxLabel), - subtype: siblingPipelineAggHelper.subtype, - params: [...siblingPipelineAggHelper.params()], - getFormat: siblingPipelineAggHelper.getFormat, + subtype, + params: [...params()], + getFormat, + getSerializedFormat, }, { getInternalStartServices, diff --git a/src/plugins/data/public/search/aggs/metrics/bucket_min.ts b/src/plugins/data/public/search/aggs/metrics/bucket_min.ts index e6a523eeea37..c46a3eb9425d 100644 --- a/src/plugins/data/public/search/aggs/metrics/bucket_min.ts +++ b/src/plugins/data/public/search/aggs/metrics/bucket_min.ts @@ -45,14 +45,17 @@ const minBucketTitle = i18n.translate('data.search.aggs.metrics.minBucketTitle', export const getBucketMinMetricAgg = ({ getInternalStartServices, }: BucketMinMetricAggDependencies) => { + const { subtype, params, getFormat, getSerializedFormat } = siblingPipelineAggHelper; + return new MetricAggType( { name: METRIC_TYPES.MIN_BUCKET, title: minBucketTitle, makeLabel: (agg) => makeNestedLabel(agg, overallMinLabel), - subtype: siblingPipelineAggHelper.subtype, - params: [...siblingPipelineAggHelper.params()], - getFormat: siblingPipelineAggHelper.getFormat, + subtype, + params: [...params()], + getFormat, + getSerializedFormat, }, { getInternalStartServices, diff --git a/src/plugins/data/public/search/aggs/metrics/bucket_sum.ts b/src/plugins/data/public/search/aggs/metrics/bucket_sum.ts index 71c88596ea56..57212ec9ff91 100644 --- a/src/plugins/data/public/search/aggs/metrics/bucket_sum.ts +++ b/src/plugins/data/public/search/aggs/metrics/bucket_sum.ts @@ -45,14 +45,17 @@ const sumBucketTitle = i18n.translate('data.search.aggs.metrics.sumBucketTitle', export const getBucketSumMetricAgg = ({ getInternalStartServices, }: BucketSumMetricAggDependencies) => { + const { subtype, params, getFormat, getSerializedFormat } = siblingPipelineAggHelper; + return new MetricAggType( { name: METRIC_TYPES.SUM_BUCKET, title: sumBucketTitle, makeLabel: (agg) => makeNestedLabel(agg, overallSumLabel), - subtype: siblingPipelineAggHelper.subtype, - params: [...siblingPipelineAggHelper.params()], - getFormat: siblingPipelineAggHelper.getFormat, + subtype, + params: [...params()], + getFormat, + getSerializedFormat, }, { getInternalStartServices, diff --git a/src/plugins/data/public/search/aggs/metrics/cardinality.ts b/src/plugins/data/public/search/aggs/metrics/cardinality.ts index 9ff3e84c38cd..2855cc1b6b16 100644 --- a/src/plugins/data/public/search/aggs/metrics/cardinality.ts +++ b/src/plugins/data/public/search/aggs/metrics/cardinality.ts @@ -54,6 +54,11 @@ export const getCardinalityMetricAgg = ({ return fieldFormats.getDefaultInstance(KBN_FIELD_TYPES.NUMBER); }, + getSerializedFormat(agg) { + return { + id: 'number', + }; + }, params: [ { name: 'field', diff --git a/src/plugins/data/public/search/aggs/metrics/count.ts b/src/plugins/data/public/search/aggs/metrics/count.ts index bd0b83798c7d..4c7b8139b016 100644 --- a/src/plugins/data/public/search/aggs/metrics/count.ts +++ b/src/plugins/data/public/search/aggs/metrics/count.ts @@ -45,6 +45,11 @@ export const getCountMetricAgg = ({ getInternalStartServices }: CountMetricAggDe return fieldFormats.getDefaultInstance(KBN_FIELD_TYPES.NUMBER); }, + getSerializedFormat(agg) { + return { + id: 'number', + }; + }, getValue(agg, bucket) { return bucket.doc_count; }, diff --git a/src/plugins/data/public/search/aggs/metrics/cumulative_sum.ts b/src/plugins/data/public/search/aggs/metrics/cumulative_sum.ts index 44bfca1b6fb8..c392f44a7961 100644 --- a/src/plugins/data/public/search/aggs/metrics/cumulative_sum.ts +++ b/src/plugins/data/public/search/aggs/metrics/cumulative_sum.ts @@ -46,14 +46,17 @@ const cumulativeSumTitle = i18n.translate('data.search.aggs.metrics.cumulativeSu export const getCumulativeSumMetricAgg = ({ getInternalStartServices, }: CumulativeSumMetricAggDependencies) => { + const { subtype, params, getFormat, getSerializedFormat } = parentPipelineAggHelper; + return new MetricAggType( { name: METRIC_TYPES.CUMULATIVE_SUM, title: cumulativeSumTitle, - subtype: parentPipelineAggHelper.subtype, makeLabel: (agg) => makeNestedLabel(agg, cumulativeSumLabel), - params: [...parentPipelineAggHelper.params()], - getFormat: parentPipelineAggHelper.getFormat, + subtype, + params: [...params()], + getFormat, + getSerializedFormat, }, { getInternalStartServices, diff --git a/src/plugins/data/public/search/aggs/metrics/derivative.ts b/src/plugins/data/public/search/aggs/metrics/derivative.ts index edb907ca4ed4..f3c1cc9bc297 100644 --- a/src/plugins/data/public/search/aggs/metrics/derivative.ts +++ b/src/plugins/data/public/search/aggs/metrics/derivative.ts @@ -46,16 +46,19 @@ const derivativeTitle = i18n.translate('data.search.aggs.metrics.derivativeTitle export const getDerivativeMetricAgg = ({ getInternalStartServices, }: DerivativeMetricAggDependencies) => { + const { subtype, params, getFormat, getSerializedFormat } = parentPipelineAggHelper; + return new MetricAggType( { name: METRIC_TYPES.DERIVATIVE, title: derivativeTitle, - subtype: parentPipelineAggHelper.subtype, makeLabel(agg) { return makeNestedLabel(agg, derivativeLabel); }, - params: [...parentPipelineAggHelper.params()], - getFormat: parentPipelineAggHelper.getFormat, + subtype, + params: [...params()], + getFormat, + getSerializedFormat, }, { getInternalStartServices, diff --git a/src/plugins/data/public/search/aggs/metrics/lib/parent_pipeline_agg_helper.ts b/src/plugins/data/public/search/aggs/metrics/lib/parent_pipeline_agg_helper.ts index 947394c97bdc..2a74a446ce84 100644 --- a/src/plugins/data/public/search/aggs/metrics/lib/parent_pipeline_agg_helper.ts +++ b/src/plugins/data/public/search/aggs/metrics/lib/parent_pipeline_agg_helper.ts @@ -84,4 +84,16 @@ export const parentPipelineAggHelper = { } return subAgg ? subAgg.type.getFormat(subAgg) : new (FieldFormat.from(identity))(); }, + + getSerializedFormat(agg: IMetricAggConfig) { + let subAgg; + const customMetric = agg.getParam('customMetric'); + + if (customMetric) { + subAgg = customMetric; + } else { + subAgg = agg.aggConfigs.byId(agg.getParam('metricAgg')); + } + return subAgg ? subAgg.type.getSerializedFormat(subAgg) : {}; + }, }; diff --git a/src/plugins/data/public/search/aggs/metrics/lib/sibling_pipeline_agg_helper.ts b/src/plugins/data/public/search/aggs/metrics/lib/sibling_pipeline_agg_helper.ts index cee7841a8c3b..8e3e0143bf91 100644 --- a/src/plugins/data/public/search/aggs/metrics/lib/sibling_pipeline_agg_helper.ts +++ b/src/plugins/data/public/search/aggs/metrics/lib/sibling_pipeline_agg_helper.ts @@ -93,4 +93,9 @@ export const siblingPipelineAggHelper = { ? customMetric.type.getFormat(customMetric) : new (FieldFormat.from(identity))(); }, + + getSerializedFormat(agg: IMetricAggConfig) { + const customMetric = agg.getParam('customMetric'); + return customMetric ? customMetric.type.getSerializedFormat(customMetric) : {}; + }, }; diff --git a/src/plugins/data/public/search/aggs/metrics/moving_avg.ts b/src/plugins/data/public/search/aggs/metrics/moving_avg.ts index 1173ae5358ee..abad2782f9d2 100644 --- a/src/plugins/data/public/search/aggs/metrics/moving_avg.ts +++ b/src/plugins/data/public/search/aggs/metrics/moving_avg.ts @@ -48,15 +48,19 @@ const movingAvgLabel = i18n.translate('data.search.aggs.metrics.movingAvgLabel', export const getMovingAvgMetricAgg = ({ getInternalStartServices, }: MovingAvgMetricAggDependencies) => { + const { subtype, params, getFormat, getSerializedFormat } = parentPipelineAggHelper; + return new MetricAggType( { name: METRIC_TYPES.MOVING_FN, dslName: 'moving_fn', title: movingAvgTitle, - subtype: parentPipelineAggHelper.subtype, makeLabel: (agg) => makeNestedLabel(agg, movingAvgLabel), + subtype, + getFormat, + getSerializedFormat, params: [ - ...parentPipelineAggHelper.params(), + ...params(), { name: 'window', default: 5, @@ -78,7 +82,6 @@ export const getMovingAvgMetricAgg = ({ */ return bucket[agg.id] ? bucket[agg.id].value : null; }, - getFormat: parentPipelineAggHelper.getFormat, }, { getInternalStartServices, diff --git a/src/plugins/data/public/search/aggs/metrics/percentile_ranks.ts b/src/plugins/data/public/search/aggs/metrics/percentile_ranks.ts index c8383f6bcc3d..c5aee380b977 100644 --- a/src/plugins/data/public/search/aggs/metrics/percentile_ranks.ts +++ b/src/plugins/data/public/search/aggs/metrics/percentile_ranks.ts @@ -103,6 +103,11 @@ export const getPercentileRanksMetricAgg = ({ fieldFormats.getDefaultInstance(KBN_FIELD_TYPES.NUMBER) ); }, + getSerializedFormat(agg) { + return { + id: 'percent', + }; + }, getValue(agg, bucket) { return getPercentileValue(agg, bucket) / 100; }, diff --git a/src/plugins/data/public/search/aggs/metrics/serial_diff.ts b/src/plugins/data/public/search/aggs/metrics/serial_diff.ts index 00bc631cefab..7cb1b194fed1 100644 --- a/src/plugins/data/public/search/aggs/metrics/serial_diff.ts +++ b/src/plugins/data/public/search/aggs/metrics/serial_diff.ts @@ -46,14 +46,17 @@ const serialDiffLabel = i18n.translate('data.search.aggs.metrics.serialDiffLabel export const getSerialDiffMetricAgg = ({ getInternalStartServices, }: SerialDiffMetricAggDependencies) => { + const { subtype, params, getFormat, getSerializedFormat } = parentPipelineAggHelper; + return new MetricAggType( { name: METRIC_TYPES.SERIAL_DIFF, title: serialDiffTitle, - subtype: parentPipelineAggHelper.subtype, makeLabel: (agg) => makeNestedLabel(agg, serialDiffLabel), - params: [...parentPipelineAggHelper.params()], - getFormat: parentPipelineAggHelper.getFormat, + subtype, + params: [...params()], + getFormat, + getSerializedFormat, }, { getInternalStartServices, diff --git a/src/plugins/data/public/search/aggs/test_helpers/mock_agg_types_registry.ts b/src/plugins/data/public/search/aggs/test_helpers/mock_agg_types_registry.ts index 5549ffd2583b..836aaad2cda0 100644 --- a/src/plugins/data/public/search/aggs/test_helpers/mock_agg_types_registry.ts +++ b/src/plugins/data/public/search/aggs/test_helpers/mock_agg_types_registry.ts @@ -25,6 +25,25 @@ import { MetricAggType } from '../metrics/metric_agg_type'; import { queryServiceMock } from '../../../query/mocks'; import { fieldFormatsServiceMock } from '../../../field_formats/mocks'; import { InternalStartServices } from '../../../types'; +import { TimeBucketsConfig } from '../buckets/lib/time_buckets/time_buckets'; + +// Mocked uiSettings shared among aggs unit tests +const mockUiSettings = jest.fn().mockImplementation((key: string) => { + const config: TimeBucketsConfig = { + 'histogram:maxBars': 4, + 'histogram:barTarget': 3, + dateFormat: 'YYYY-MM-DD', + 'dateFormat:scaled': [ + ['', 'HH:mm:ss.SSS'], + ['PT1S', 'HH:mm:ss'], + ['PT1M', 'HH:mm'], + ['PT1H', 'YYYY-MM-DD HH:mm'], + ['P1DT', 'YYYY-MM-DD'], + ['P1YT', 'YYYY'], + ], + }; + return config[key] ?? key; +}); /** * Testing utility which creates a new instance of AggTypesRegistry, @@ -54,7 +73,10 @@ export function mockAggTypesRegistry | MetricAggTyp }); } else { const coreSetup = coreMock.createSetup(); + coreSetup.uiSettings.get = mockUiSettings; + const coreStart = coreMock.createStart(); + coreSetup.uiSettings.get = mockUiSettings; const aggTypes = getAggTypes({ uiSettings: coreSetup.uiSettings, diff --git a/src/plugins/data/public/search/expressions/esaggs.ts b/src/plugins/data/public/search/expressions/esaggs.ts index 153eb7de6f2d..77475cd3ce88 100644 --- a/src/plugins/data/public/search/expressions/esaggs.ts +++ b/src/plugins/data/public/search/expressions/esaggs.ts @@ -32,14 +32,7 @@ import { Adapters } from '../../../../../plugins/inspector/public'; import { IAggConfigs } from '../aggs'; import { ISearchSource } from '../search_source'; import { tabifyAggResponse } from '../tabify'; -import { - Filter, - Query, - serializeFieldFormat, - TimeRange, - IIndexPattern, - isRangeFilter, -} from '../../../common'; +import { Filter, Query, TimeRange, IIndexPattern, isRangeFilter } from '../../../common'; import { FilterManager, calculateBounds, getTime } from '../../query'; import { getSearchService, getQueryService, getIndexPatterns } from '../../services'; import { buildTabularInspectorData } from './build_tabular_inspector_data'; @@ -313,7 +306,7 @@ export const esaggs = (): ExpressionFunctionDefinition import("../../expressions").SerializedFieldFormat; BoolFormat: typeof BoolFormat; BytesFormat: typeof BytesFormat; ColorFormat: typeof ColorFormat; @@ -631,7 +630,7 @@ export class Plugin implements Plugin_2 { setup(core: CoreSetup, { usageCollection }: DataPluginSetupDependencies): { search: ISearchSetup; fieldFormats: { - register: (customFieldFormat: import("../public").FieldFormatInstanceType) => number; + register: (customFieldFormat: import("../common").FieldFormatInstanceType) => number; }; }; // Warning: (ae-forgotten-export) The symbol "CoreStart" needs to be exported by the entry point index.d.ts @@ -774,30 +773,30 @@ export const UI_SETTINGS: { // src/plugins/data/server/index.ts:40:23 - (ae-forgotten-export) The symbol "buildFilter" needs to be exported by the entry point index.d.ts // src/plugins/data/server/index.ts:71:21 - (ae-forgotten-export) The symbol "getEsQueryConfig" needs to be exported by the entry point index.d.ts // src/plugins/data/server/index.ts:71:21 - (ae-forgotten-export) The symbol "buildEsQuery" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "FieldFormatsRegistry" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "FieldFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "BoolFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "BytesFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "ColorFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "DateNanosFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "DurationFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "IpFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "NumberFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "PercentFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "RelativeDateFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "SourceFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "StaticLookupFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "UrlFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "StringFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "TruncateFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:131:27 - (ae-forgotten-export) The symbol "isFilterable" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:131:27 - (ae-forgotten-export) The symbol "isNestedField" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:186:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:187:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:188:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:189:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:190:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:193:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "FieldFormatsRegistry" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "FieldFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "BoolFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "BytesFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "ColorFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "DateNanosFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "DurationFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "IpFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "NumberFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "PercentFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "RelativeDateFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "SourceFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "StaticLookupFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "UrlFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "StringFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "TruncateFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:129:27 - (ae-forgotten-export) The symbol "isFilterable" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:129:27 - (ae-forgotten-export) The symbol "isNestedField" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:184:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:185:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:186:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:187:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:188:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:191:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts // (No @packageDocumentation comment for this package) diff --git a/src/plugins/discover/public/application/angular/discover.js b/src/plugins/discover/public/application/angular/discover.js index 225f918c9f40..65868b0b7cd4 100644 --- a/src/plugins/discover/public/application/angular/discover.js +++ b/src/plugins/discover/public/application/angular/discover.js @@ -66,7 +66,6 @@ const { import { getRootBreadcrumbs, getSavedSearchBreadcrumbs } from '../helpers/breadcrumbs'; import { esFilters, - fieldFormats, indexPatterns as indexPatternsUtils, connectToQueryState, syncQueryStateWithUrl, @@ -851,7 +850,7 @@ function discoverController( x: { accessor: 0, label: agg.makeLabel(), - format: fieldFormats.serialize(agg), + format: agg.toSerializedFieldFormat(), params: { date: true, interval: moment.duration(esValue, esUnit), @@ -863,7 +862,7 @@ function discoverController( }, y: { accessor: 1, - format: fieldFormats.serialize(metric), + format: metric.toSerializedFieldFormat(), label: metric.makeLabel(), }, }; diff --git a/src/plugins/visualizations/public/legacy/build_pipeline.test.ts b/src/plugins/visualizations/public/legacy/build_pipeline.test.ts index 913058196380..9ecd321963e8 100644 --- a/src/plugins/visualizations/public/legacy/build_pipeline.test.ts +++ b/src/plugins/visualizations/public/legacy/build_pipeline.test.ts @@ -28,7 +28,7 @@ import { } from './build_pipeline'; import { Vis } from '..'; import { dataPluginMock } from '../../../../plugins/data/public/mocks'; -import { IAggConfig } from '../../../../plugins/data/public'; +import { IndexPattern, IAggConfigs } from '../../../../plugins/data/public'; describe('visualize loader pipeline helpers: build pipeline', () => { describe('prepareJson', () => { @@ -344,23 +344,20 @@ describe('visualize loader pipeline helpers: build pipeline', () => { describe('buildVislibDimensions', () => { const dataStart = dataPluginMock.createStartContract(); - let aggs: IAggConfig[]; + let aggs: IAggConfigs; let vis: Vis; let params: any; beforeEach(() => { - aggs = [ + aggs = dataStart.search.aggs.createAggConfigs({} as IndexPattern, [ { id: '0', enabled: true, - type: { - type: 'metrics', - name: 'count', - }, + type: 'count', schema: 'metric', params: {}, - } as IAggConfig, - ]; + }, + ]); params = { searchSource: null, @@ -393,11 +390,7 @@ describe('visualize loader pipeline helpers: build pipeline', () => { ], }, data: { - aggs: { - getResponseAggs: () => { - return aggs; - }, - } as any, + aggs, searchSource: {} as any, }, isHierarchical: () => { @@ -422,8 +415,13 @@ describe('visualize loader pipeline helpers: build pipeline', () => { }); it('with two numeric metrics, mixed normal and percent mode should have corresponding formatters', async () => { - const aggConfig = aggs[0]; - aggs = [{ ...aggConfig } as IAggConfig, { ...aggConfig, id: '5' } as IAggConfig]; + aggs.createAggConfig({ + id: '5', + enabled: true, + type: 'count', + schema: 'metric', + params: {}, + }); vis.params = { seriesParams: [ @@ -469,11 +467,7 @@ describe('visualize loader pipeline helpers: build pipeline', () => { }, params: { gauge: {} }, data: { - aggs: { - getResponseAggs: () => { - return aggs; - }, - } as any, + aggs, searchSource: {} as any, }, isHierarchical: () => { diff --git a/src/plugins/visualizations/public/legacy/build_pipeline.ts b/src/plugins/visualizations/public/legacy/build_pipeline.ts index 5d74cb3d3b1e..62ff1f83426b 100644 --- a/src/plugins/visualizations/public/legacy/build_pipeline.ts +++ b/src/plugins/visualizations/public/legacy/build_pipeline.ts @@ -20,12 +20,7 @@ import { get } from 'lodash'; import moment from 'moment'; import { SerializedFieldFormat } from '../../../../plugins/expressions/public'; -import { - IAggConfig, - fieldFormats, - search, - TimefilterContract, -} from '../../../../plugins/data/public'; +import { IAggConfig, search, TimefilterContract } from '../../../../plugins/data/public'; import { Vis, VisParams } from '../types'; const { isDateHistogramBucketAggConfig } = search.aggs; @@ -113,11 +108,9 @@ const getSchemas = ( 'max_bucket', ].includes(agg.type.name); - const format = fieldFormats.serialize( - hasSubAgg - ? agg.params.customMetric || agg.aggConfigs.getRequestAggById(agg.params.metricAgg) - : agg - ); + const formatAgg = hasSubAgg + ? agg.params.customMetric || agg.aggConfigs.getRequestAggById(agg.params.metricAgg) + : agg; const params: SchemaConfigParams = {}; @@ -130,7 +123,7 @@ const getSchemas = ( return { accessor, - format, + format: formatAgg.toSerializedFieldFormat(), params, label, aggType: agg.type.name, diff --git a/tsconfig.types.json b/tsconfig.types.json index fd3624dd8e31..2f5919e413e5 100644 --- a/tsconfig.types.json +++ b/tsconfig.types.json @@ -10,6 +10,8 @@ "include": [ "src/core/server/index.ts", "src/core/public/index.ts", + "src/plugins/data/server/index.ts", + "src/plugins/data/public/index.ts", "typings" ] }