From 772bb6d8c5e5fca2c0bcff12cb9886233411b685 Mon Sep 17 00:00:00 2001 From: Luke Elmers Date: Thu, 5 Mar 2020 10:18:12 -0700 Subject: [PATCH] Remove schemas from agg configs (#58462) --- src/legacy/core_plugins/data/public/index.ts | 3 - .../public/search/aggs/agg_config.test.ts | 3 +- .../data/public/search/aggs/agg_config.ts | 61 ++------------ .../public/search/aggs/agg_configs.test.ts | 82 +------------------ .../data/public/search/aggs/agg_configs.ts | 47 +---------- .../data/public/search/aggs/buckets/terms.ts | 16 +--- .../aggs/filter/agg_type_filters.test.ts | 14 ++-- .../search/aggs/filter/agg_type_filters.ts | 15 +++- .../data/public/search/aggs/index.ts | 1 - .../metrics/lib/parent_pipeline_agg_helper.ts | 18 +--- .../lib/sibling_pipeline_agg_helper.ts | 27 +----- .../search/aggs/metrics/metric_agg_type.ts | 4 +- .../aggs/metrics/parent_pipeline.test.ts | 9 -- .../aggs/metrics/sibling_pipeline.test.ts | 9 -- .../public/search/aggs/metrics/top_hit.ts | 7 +- .../public/search/aggs/param_types/agg.ts | 5 ++ .../search/aggs/param_types/field.test.ts | 41 ---------- .../public/search/aggs/param_types/field.ts | 13 +-- .../data/public/search/aggs/types.ts | 1 - .../core_plugins/data/public/search/mocks.ts | 1 - .../data/public/search/search_service.ts | 1 - .../public/search/tabify/get_columns.test.ts | 36 +------- .../search/tabify/response_writer.test.ts | 12 +-- .../data/public/search/tabify/tabify.test.ts | 12 +-- .../discover/np_ready/angular/discover.js | 2 +- .../tile_map/public/tile_map_type.js | 2 +- .../__snapshots__/agg.test.tsx.snap | 21 +++-- .../public/components/agg.test.tsx | 14 ++-- .../public/components/agg.tsx | 13 +-- .../public/components/agg_add.tsx | 5 +- .../public/components/agg_common_props.ts | 4 +- .../public/components/agg_group.test.tsx | 13 ++- .../public/components/agg_group.tsx | 17 ++-- .../components/agg_group_helper.test.ts | 44 ++++++++-- .../public/components/agg_group_helper.tsx | 21 +++-- .../public/components/agg_param_props.ts | 2 + .../public/components/agg_params.test.tsx | 1 + .../public/components/agg_params.tsx | 32 +++++--- .../components/agg_params_helper.test.ts | 38 +++++---- .../public/components/agg_params_helper.ts | 39 +++++++-- .../public/components/controls/field.test.tsx | 1 + .../public/components/controls/order_agg.tsx | 4 + .../components/controls/percentiles.test.tsx | 1 + .../components/controls/rows_or_columns.tsx | 11 ++- .../public/components/controls/sub_agg.tsx | 4 + .../public/components/controls/sub_metric.tsx | 4 + .../public/components/controls/test_utils.ts | 1 + .../public/components/sidebar/data_tab.tsx | 6 +- .../public/components/sidebar/sidebar.tsx | 8 +- .../components/sidebar/state/actions.ts | 10 ++- .../components/sidebar/state/reducers.ts | 29 ++++--- .../vis_default_editor/public/index.ts | 1 + .../public/legacy_imports.ts | 2 - .../public}/schemas.ts | 15 +++- .../public/vis_type_agg_filter.ts | 4 +- .../public/table_vis_controller.test.ts | 30 +++---- .../components/options/gauge/style_panel.tsx | 3 +- .../public/legacy/build_pipeline.test.ts | 4 +- .../np_ready/public/legacy/build_pipeline.ts | 7 +- .../public/np_ready/public/vis_impl.d.ts | 4 + .../public/np_ready/public/vis_impl.js | 25 ++++-- src/legacy/ui/public/agg_types/index.ts | 5 +- .../translations/translations/ja-JP.json | 2 - .../translations/translations/zh-CN.json | 2 - 64 files changed, 357 insertions(+), 532 deletions(-) rename src/legacy/core_plugins/{data/public/search/aggs => vis_default_editor/public}/schemas.ts (84%) diff --git a/src/legacy/core_plugins/data/public/index.ts b/src/legacy/core_plugins/data/public/index.ts index ab3bc598bddd8..424e5ab0bf4d5 100644 --- a/src/legacy/core_plugins/data/public/index.ts +++ b/src/legacy/core_plugins/data/public/index.ts @@ -44,7 +44,6 @@ export { IFieldParamType, IMetricAggType, IpRangeKey, // only used in field formatter deserialization, which will live in data - ISchemas, OptionedParamEditorProps, // only type is used externally OptionedValueProp, // only type is used externally } from './search/types'; @@ -75,8 +74,6 @@ export { OptionedParamType, parentPipelineType, propFilter, - Schema, - Schemas, siblingPipelineType, termsAggFilter, toAbsoluteDates, diff --git a/src/legacy/core_plugins/data/public/search/aggs/agg_config.test.ts b/src/legacy/core_plugins/data/public/search/aggs/agg_config.test.ts index 4913499403c00..36d5451a4cd00 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/agg_config.test.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/agg_config.test.ts @@ -343,8 +343,7 @@ describe('AggConfig', () => { expect(typeof aggConfig.params).toBe('object'); expect(aggConfig.type).toBeInstanceOf(AggType); expect(aggConfig.type).toHaveProperty('name', 'date_histogram'); - expect(typeof aggConfig.schema).toBe('object'); - expect(aggConfig.schema).toHaveProperty('name', 'segment'); + expect(typeof aggConfig.schema).toBe('string'); const state = aggConfig.toJSON(); expect(state).toHaveProperty('id', '1'); diff --git a/src/legacy/core_plugins/data/public/search/aggs/agg_config.ts b/src/legacy/core_plugins/data/public/search/aggs/agg_config.ts index 1465731d5e82b..bf2d2f734c989 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/agg_config.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/agg_config.ts @@ -20,10 +20,8 @@ import _ from 'lodash'; import { i18n } from '@kbn/i18n'; import { IAggType } from './agg_type'; -import { AggGroupNames } from './agg_groups'; import { writeParams } from './agg_params'; import { IAggConfigs } from './agg_configs'; -import { Schema } from './schemas'; import { ISearchSource, FetchOptions, @@ -38,37 +36,9 @@ export interface AggConfigOptions { enabled?: boolean; id?: string; params?: Record; - schema?: string | Schema; + schema?: string; } -const unknownSchema: Schema = { - name: 'unknown', - title: 'Unknown', // only here for illustrative purposes - hideCustomLabel: true, - aggFilter: [], - min: 1, - max: 1, - params: [], - defaults: {}, - editor: false, - group: AggGroupNames.Metrics, - aggSettings: { - top_hits: { - allowStrings: true, - }, - }, -}; - -const getSchemaFromRegistry = (schemas: any, schema: string): Schema => { - let registeredSchema = schemas ? schemas.byName[schema] : null; - if (!registeredSchema) { - registeredSchema = Object.assign({}, unknownSchema); - registeredSchema.name = schema; - } - - return registeredSchema; -}; - /** * @name AggConfig * @@ -122,8 +92,8 @@ export class AggConfig { public params: any; public parent?: IAggConfigs; public brandNew?: boolean; + public schema?: string; - private __schema: Schema; private __type: IAggType; private __typeDecorations: any; private subAggs: AggConfig[] = []; @@ -141,14 +111,12 @@ export class AggConfig { this.setType(opts.type); if (opts.schema) { - this.setSchema(opts.schema); + this.schema = opts.schema; } // set the params to the values from opts, or just to the defaults this.setParams(opts.params || {}); - // @ts-ignore - this.__schema = this.__schema; // @ts-ignore this.__type = this.__type; } @@ -305,16 +273,13 @@ export class AggConfig { id: this.id, enabled: this.enabled, type: this.type && this.type.name, - schema: _.get(this, 'schema.name', this.schema), + schema: this.schema, params: outParams, }; } getAggParams() { - return [ - ...(_.has(this, 'type.params') ? this.type.params : []), - ...(_.has(this, 'schema.params') ? (this.schema as Schema).params : []), - ]; + return [...(_.has(this, 'type.params') ? this.type.params : [])]; } getRequestAggs() { @@ -434,9 +399,6 @@ export class AggConfig { // clear out the previous params except for a few special ones this.setParams({ - // split row/columns is "outside" of the agg, so don't reset it - row: this.params.row, - // almost every agg has fields, so we try to persist that when type changes field: availableFields.find((field: any) => field.name === this.getField()), }); @@ -445,17 +407,4 @@ export class AggConfig { public setType(type: IAggType) { this.type = type; } - - public get schema() { - return this.__schema; - } - - public set schema(schema) { - this.__schema = schema; - } - - public setSchema(schema: string | Schema) { - this.schema = - typeof schema === 'string' ? getSchemaFromRegistry(this.aggConfigs.schemas, schema) : schema; - } } diff --git a/src/legacy/core_plugins/data/public/search/aggs/agg_configs.test.ts b/src/legacy/core_plugins/data/public/search/aggs/agg_configs.test.ts index 49eed55f0233d..d69376b4026d9 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/agg_configs.test.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/agg_configs.test.ts @@ -21,8 +21,6 @@ import { indexBy } from 'lodash'; import { AggConfig } from './agg_config'; import { AggConfigs } from './agg_configs'; import { AggTypesRegistryStart } from './agg_types_registry'; -import { Schemas } from './schemas'; -import { AggGroupNames } from './agg_groups'; import { mockDataServices, mockAggTypesRegistry } from './test_helpers'; import { IndexPatternField, IndexPattern } from '../../../../../../plugins/data/public'; import { @@ -81,67 +79,6 @@ describe('AggConfigs', () => { expect(spy.mock.calls[0]).toEqual([configStates]); spy.mockRestore(); }); - - describe('defaults', () => { - const schemas = new Schemas([ - { - group: AggGroupNames.Metrics, - name: 'metric', - title: 'Simple', - min: 1, - max: 2, - defaults: [ - { schema: 'metric', type: 'count' }, - { schema: 'metric', type: 'avg' }, - { schema: 'metric', type: 'sum' }, - ], - }, - { - group: AggGroupNames.Buckets, - name: 'segment', - title: 'Example', - min: 0, - max: 1, - defaults: [ - { schema: 'segment', type: 'terms' }, - { schema: 'segment', type: 'filters' }, - ], - }, - ]); - - it('should only set the number of defaults defined by the max', () => { - const ac = new AggConfigs(indexPattern, [], { - schemas: schemas.all, - typesRegistry, - }); - expect(ac.bySchemaName('metric')).toHaveLength(2); - }); - - it('should set the defaults defined in the schema when none exist', () => { - const ac = new AggConfigs(indexPattern, [], { - schemas: schemas.all, - typesRegistry, - }); - expect(ac.aggs).toHaveLength(3); - }); - - it('should NOT set the defaults defined in the schema when some exist', () => { - const configStates = [ - { - enabled: true, - type: 'date_histogram', - params: {}, - schema: 'segment', - }, - ]; - const ac = new AggConfigs(indexPattern, configStates, { - schemas: schemas.all, - typesRegistry, - }); - expect(ac.aggs).toHaveLength(3); - expect(ac.bySchemaName('segment')[0].type.name).toEqual('date_histogram'); - }); - }); }); describe('#createAggConfig', () => { @@ -285,17 +222,6 @@ describe('AggConfigs', () => { }); describe('#toDsl', () => { - const schemas = new Schemas([ - { - group: AggGroupNames.Buckets, - name: 'segment', - }, - { - group: AggGroupNames.Buckets, - name: 'split', - }, - ]); - beforeEach(() => { indexPattern = stubIndexPattern as IndexPattern; indexPattern.fields.getByName = name => (name as unknown) as IndexPatternField; @@ -319,7 +245,6 @@ describe('AggConfigs', () => { const ac = new AggConfigs(indexPattern, configStates, { typesRegistry, - schemas: schemas.all, }); const aggInfos = ac.aggs.map(aggConfig => { @@ -390,11 +315,10 @@ describe('AggConfigs', () => { const ac = new AggConfigs(indexPattern, configStates, { typesRegistry, - schemas: schemas.all, }); const dsl = ac.toDsl(); const histo = ac.byName('date_histogram')[0]; - const metrics = ac.bySchemaGroup('metrics'); + const metrics = ac.bySchemaName('metrics'); expect(dsl).toHaveProperty(histo.id); expect(typeof dsl[histo.id]).toBe('object'); @@ -418,8 +342,8 @@ describe('AggConfigs', () => { const ac = new AggConfigs(indexPattern, configStates, { typesRegistry }); const topLevelDsl = ac.toDsl(true); - const buckets = ac.bySchemaGroup('buckets'); - const metrics = ac.bySchemaGroup('metrics'); + const buckets = ac.bySchemaName('buckets'); + const metrics = ac.bySchemaName('metrics'); (function checkLevel(dsl) { const bucket = buckets.shift(); diff --git a/src/legacy/core_plugins/data/public/search/aggs/agg_configs.ts b/src/legacy/core_plugins/data/public/search/aggs/agg_configs.ts index ab70e66b1e138..4a48f356d3f79 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/agg_configs.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/agg_configs.ts @@ -23,7 +23,6 @@ import { Assign } from '@kbn/utility-types'; import { AggConfig, AggConfigOptions, IAggConfig } from './agg_config'; import { IAggType } from './agg_type'; import { AggTypesRegistryStart } from './agg_types_registry'; -import { Schema } from './schemas'; import { AggGroupNames } from './agg_groups'; import { IndexPattern, @@ -32,8 +31,6 @@ import { TimeRange, } from '../../../../../../plugins/data/public'; -type Schemas = Record; - function removeParentAggs(obj: any) { for (const prop in obj) { if (prop === 'parentAggs') delete obj[prop]; @@ -51,7 +48,6 @@ function parseParentAggs(dslLvlCursor: any, dsl: any) { } export interface AggConfigsOptions { - schemas?: Schemas; typesRegistry: AggTypesRegistryStart; } @@ -73,7 +69,6 @@ export type IAggConfigs = AggConfigs; export class AggConfigs { public indexPattern: IndexPattern; - public schemas: any; public timeRange?: TimeRange; private readonly typesRegistry: AggTypesRegistryStart; @@ -90,37 +85,8 @@ export class AggConfigs { this.aggs = []; this.indexPattern = indexPattern; - this.schemas = opts.schemas; configStates.forEach((params: any) => this.createAggConfig(params)); - - if (this.schemas) { - this.initializeDefaultsFromSchemas(this.schemas); - } - } - - // do this wherever the schemas were passed in, & pass in state defaults instead - initializeDefaultsFromSchemas(schemas: Schemas) { - // Set the defaults for any schema which has them. If the defaults - // for some reason has more then the max only set the max number - // of defaults (not sure why a someone define more... - // but whatever). Also if a schema.name is already set then don't - // set anything. - _(schemas) - .filter((schema: Schema) => { - return Array.isArray(schema.defaults) && schema.defaults.length > 0; - }) - .each((schema: any) => { - if (!this.aggs.find((agg: AggConfig) => agg.schema && agg.schema.name === schema.name)) { - // the result here should be passable as a configState - const defaults = schema.defaults.slice(0, schema.max); - _.each(defaults, defaultState => { - const state = _.defaults({ id: AggConfig.nextId(this.aggs) }, defaultState); - this.createAggConfig(state as AggConfigOptions); - }); - } - }) - .commit(); } setTimeRange(timeRange: TimeRange) { @@ -148,7 +114,6 @@ export class AggConfigs { }; const aggConfigs = new AggConfigs(this.indexPattern, this.aggs.filter(filterAggs), { - schemas: this.schemas, typesRegistry: this.typesRegistry, }); @@ -271,23 +236,19 @@ export class AggConfigs { } byName(name: string) { - return this.aggs.filter(agg => agg.type.name === name); + return this.aggs.filter(agg => agg.type?.name === name); } byType(type: string) { - return this.aggs.filter(agg => agg.type.type === type); + return this.aggs.filter(agg => agg.type?.type === type); } byTypeName(type: string) { - return this.aggs.filter(agg => agg.type.name === type); + return this.byName(type); } bySchemaName(schema: string) { - return this.aggs.filter(agg => agg.schema && agg.schema.name === schema); - } - - bySchemaGroup(group: string) { - return this.aggs.filter(agg => agg.schema && agg.schema.group === group); + return this.aggs.filter(agg => agg.schema === schema); } getRequestAggs(): AggConfig[] { diff --git a/src/legacy/core_plugins/data/public/search/aggs/buckets/terms.ts b/src/legacy/core_plugins/data/public/search/aggs/buckets/terms.ts index 8fd95c86d8476..b387e9b7d306a 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/buckets/terms.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/buckets/terms.ts @@ -40,8 +40,6 @@ import { mergeOtherBucketAggResponse, updateMissingBucket, } from './_terms_other_bucket_helper'; -import { Schemas } from '../schemas'; -import { AggGroupNames } from '../agg_groups'; export const termsAggFilter = [ '!top_hits', @@ -58,17 +56,6 @@ export const termsAggFilter = [ '!sum_bucket', ]; -const [orderAggSchema] = new Schemas([ - { - group: AggGroupNames.None, - name: 'orderAgg', - // This string is never visible to the user so it doesn't need to be translated - title: 'Order Agg', - hideCustomLabel: true, - aggFilter: termsAggFilter, - }, -]).all; - const termsTitle = i18n.translate('data.search.aggs.buckets.termsTitle', { defaultMessage: 'Terms', }); @@ -158,10 +145,11 @@ export const termsBucketAgg = new BucketAggType({ { name: 'orderAgg', type: 'agg', + allowedAggs: termsAggFilter, default: null, makeAgg(termsAgg, state) { state = state || {}; - state.schema = orderAggSchema; + state.schema = 'orderAgg'; const orderAgg = termsAgg.aggConfigs.createAggConfig(state, { addToAggConfigs: false, }); diff --git a/src/legacy/core_plugins/data/public/search/aggs/filter/agg_type_filters.test.ts b/src/legacy/core_plugins/data/public/search/aggs/filter/agg_type_filters.test.ts index 0de1c31d02f96..90c29675c0db2 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/filter/agg_type_filters.test.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/filter/agg_type_filters.test.ts @@ -32,7 +32,7 @@ describe('AggTypeFilters', () => { it('should filter nothing without registered filters', async () => { const aggTypes = [{ name: 'count' }, { name: 'sum' }] as IAggType[]; - const filtered = registry.filter(aggTypes, indexPattern, aggConfig); + const filtered = registry.filter(aggTypes, indexPattern, aggConfig, []); expect(filtered).toEqual(aggTypes); }); @@ -40,23 +40,23 @@ describe('AggTypeFilters', () => { const aggTypes = [{ name: 'count' }, { name: 'sum' }] as IAggType[]; const filter = jest.fn(); registry.addFilter(filter); - registry.filter(aggTypes, indexPattern, aggConfig); - expect(filter).toHaveBeenCalledWith(aggTypes[0], indexPattern, aggConfig); - expect(filter).toHaveBeenCalledWith(aggTypes[1], indexPattern, aggConfig); + registry.filter(aggTypes, indexPattern, aggConfig, []); + expect(filter).toHaveBeenCalledWith(aggTypes[0], indexPattern, aggConfig, []); + expect(filter).toHaveBeenCalledWith(aggTypes[1], indexPattern, aggConfig, []); }); it('should allow registered filters to filter out aggTypes', async () => { const aggTypes = [{ name: 'count' }, { name: 'sum' }, { name: 'avg' }] as IAggType[]; - let filtered = registry.filter(aggTypes, indexPattern, aggConfig); + let filtered = registry.filter(aggTypes, indexPattern, aggConfig, []); expect(filtered).toEqual(aggTypes); registry.addFilter(() => true); registry.addFilter(aggType => aggType.name !== 'count'); - filtered = registry.filter(aggTypes, indexPattern, aggConfig); + filtered = registry.filter(aggTypes, indexPattern, aggConfig, []); expect(filtered).toEqual([aggTypes[1], aggTypes[2]]); registry.addFilter(aggType => aggType.name !== 'avg'); - filtered = registry.filter(aggTypes, indexPattern, aggConfig); + filtered = registry.filter(aggTypes, indexPattern, aggConfig, []); expect(filtered).toEqual([aggTypes[1]]); }); }); diff --git a/src/legacy/core_plugins/data/public/search/aggs/filter/agg_type_filters.ts b/src/legacy/core_plugins/data/public/search/aggs/filter/agg_type_filters.ts index 13a4cc0856b09..8da547e592af9 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/filter/agg_type_filters.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/filter/agg_type_filters.ts @@ -23,7 +23,8 @@ import { IAggConfig, IAggType } from '../types'; type AggTypeFilter = ( aggType: IAggType, indexPattern: IndexPattern, - aggConfig: IAggConfig + aggConfig: IAggConfig, + aggFilter: string[] ) => boolean; /** @@ -48,12 +49,20 @@ class AggTypeFilters { * @param aggTypes A list of aggTypes that will be filtered down by this registry. * @param indexPattern The indexPattern for which this list should be filtered down. * @param aggConfig The aggConfig for which the returning list will be used. + * @param schema * @return A filtered list of the passed aggTypes. */ - public filter(aggTypes: IAggType[], indexPattern: IndexPattern, aggConfig: IAggConfig) { + public filter( + aggTypes: IAggType[], + indexPattern: IndexPattern, + aggConfig: IAggConfig, + aggFilter: string[] + ) { const allFilters = Array.from(this.filters); const allowedAggTypes = aggTypes.filter(aggType => { - const isAggTypeAllowed = allFilters.every(filter => filter(aggType, indexPattern, aggConfig)); + const isAggTypeAllowed = allFilters.every(filter => + filter(aggType, indexPattern, aggConfig, aggFilter) + ); return isAggTypeAllowed; }); return allowedAggTypes; diff --git a/src/legacy/core_plugins/data/public/search/aggs/index.ts b/src/legacy/core_plugins/data/public/search/aggs/index.ts index be44e04a0129b..8d6fbeacd606a 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/index.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/index.ts @@ -56,7 +56,6 @@ export { OptionedParamType } from './param_types/optioned'; export { isValidJson, isValidInterval } from './utils'; export { BUCKET_TYPES } from './buckets/bucket_agg_types'; export { METRIC_TYPES } from './metrics/metric_agg_types'; -export { ISchemas, Schema, Schemas } from './schemas'; // types export { CreateAggConfigParams, IAggConfig, IAggConfigs } from './types'; diff --git a/src/legacy/core_plugins/data/public/search/aggs/metrics/lib/parent_pipeline_agg_helper.ts b/src/legacy/core_plugins/data/public/search/aggs/metrics/lib/parent_pipeline_agg_helper.ts index 88549ee3019ee..df4cbaf49c8b3 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/metrics/lib/parent_pipeline_agg_helper.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/metrics/lib/parent_pipeline_agg_helper.ts @@ -23,7 +23,7 @@ import { noop, identity } from 'lodash'; import { forwardModifyAggConfigOnSearchRequestStart } from './nested_agg_helpers'; import { IMetricAggConfig, MetricAggParam } from '../metric_agg_type'; import { parentPipelineAggWriter } from './parent_pipeline_agg_writer'; -import { Schemas } from '../../schemas'; + import { fieldFormats } from '../../../../../../../../plugins/data/public'; const metricAggFilter = [ @@ -36,20 +36,6 @@ const metricAggFilter = [ '!geo_centroid', ]; -const metricAggTitle = i18n.translate('data.search.aggs.metrics.metricAggTitle', { - defaultMessage: 'Metric agg', -}); - -const [metricAggSchema] = new Schemas([ - { - group: 'none', - name: 'metricAgg', - title: metricAggTitle, - hideCustomLabel: true, - aggFilter: metricAggFilter, - }, -]).all; - const parentPipelineType = i18n.translate( 'data.search.aggs.metrics.parentPipelineAggregationsSubtypeTitle', { @@ -69,9 +55,9 @@ const parentPipelineAggHelper = { { name: 'customMetric', type: 'agg', + allowedAggs: metricAggFilter, makeAgg(termsAgg, state: any) { state = state || { type: 'count' }; - state.schema = metricAggSchema; const metricAgg = termsAgg.aggConfigs.createAggConfig(state, { addToAggConfigs: false }); diff --git a/src/legacy/core_plugins/data/public/search/aggs/metrics/lib/sibling_pipeline_agg_helper.ts b/src/legacy/core_plugins/data/public/search/aggs/metrics/lib/sibling_pipeline_agg_helper.ts index 05e009cc9da30..33d6d72540868 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/metrics/lib/sibling_pipeline_agg_helper.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/metrics/lib/sibling_pipeline_agg_helper.ts @@ -22,7 +22,6 @@ import { i18n } from '@kbn/i18n'; import { siblingPipelineAggWriter } from './sibling_pipeline_agg_writer'; import { forwardModifyAggConfigOnSearchRequestStart } from './nested_agg_helpers'; import { IMetricAggConfig, MetricAggParam } from '../metric_agg_type'; -import { Schemas } from '../../schemas'; import { fieldFormats } from '../../../../../../../../plugins/data/public'; const metricAggFilter: string[] = [ @@ -44,28 +43,6 @@ const metricAggFilter: string[] = [ ]; const bucketAggFilter: string[] = []; -const [metricAggSchema] = new Schemas([ - { - group: 'none', - name: 'metricAgg', - title: i18n.translate('data.search.aggs.metrics.metricAggTitle', { - defaultMessage: 'Metric agg', - }), - aggFilter: metricAggFilter, - }, -]).all; - -const [bucketAggSchema] = new Schemas([ - { - group: 'none', - title: i18n.translate('data.search.aggs.metrics.bucketAggTitle', { - defaultMessage: 'Bucket agg', - }), - name: 'bucketAgg', - aggFilter: bucketAggFilter, - }, -]).all; - const siblingPipelineType = i18n.translate( 'data.search.aggs.metrics.siblingPipelineAggregationsSubtypeTitle', { @@ -80,10 +57,10 @@ const siblingPipelineAggHelper = { { name: 'customBucket', type: 'agg', + allowedAggs: bucketAggFilter, default: null, makeAgg(agg: IMetricAggConfig, state: any) { state = state || { type: 'date_histogram' }; - state.schema = bucketAggSchema; const orderAgg = agg.aggConfigs.createAggConfig(state, { addToAggConfigs: false }); orderAgg.id = agg.id + '-bucket'; @@ -97,10 +74,10 @@ const siblingPipelineAggHelper = { { name: 'customMetric', type: 'agg', + allowedAggs: metricAggFilter, default: null, makeAgg(agg: IMetricAggConfig, state: any) { state = state || { type: 'count' }; - state.schema = metricAggSchema; const orderAgg = agg.aggConfigs.createAggConfig(state, { addToAggConfigs: false }); orderAgg.id = agg.id + '-metric'; diff --git a/src/legacy/core_plugins/data/public/search/aggs/metrics/metric_agg_type.ts b/src/legacy/core_plugins/data/public/search/aggs/metrics/metric_agg_type.ts index 952dcc96de833..82b042a1e3378 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/metrics/metric_agg_type.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/metrics/metric_agg_type.ts @@ -21,11 +21,11 @@ import { i18n } from '@kbn/i18n'; import { AggType, AggTypeConfig } from '../agg_type'; import { AggParamType } from '../param_types/agg'; import { AggConfig } from '../agg_config'; -import { FilterFieldTypes } from '../param_types/field'; import { METRIC_TYPES } from './metric_agg_types'; import { KBN_FIELD_TYPES } from '../../../../../../../plugins/data/public'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { getFieldFormats } from '../../../../../../../plugins/data/public/services'; +import { FieldTypes } from '../param_types'; export interface IMetricAggConfig extends AggConfig { type: InstanceType; @@ -33,7 +33,7 @@ export interface IMetricAggConfig extends AggConfig { export interface MetricAggParam extends AggParamType { - filterFieldTypes?: FilterFieldTypes; + filterFieldTypes?: FieldTypes; onlyAggregatable?: boolean; } diff --git a/src/legacy/core_plugins/data/public/search/aggs/metrics/parent_pipeline.test.ts b/src/legacy/core_plugins/data/public/search/aggs/metrics/parent_pipeline.test.ts index 58b4ee530a8c2..02e63f653f94f 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/metrics/parent_pipeline.test.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/metrics/parent_pipeline.test.ts @@ -25,15 +25,6 @@ import { AggConfigs } from '../agg_configs'; import { mockDataServices, mockAggTypesRegistry } from '../test_helpers'; import { IMetricAggConfig, MetricAggType } from './metric_agg_type'; -jest.mock('../schemas', () => { - class MockedSchemas { - all = [{}]; - } - return { - Schemas: jest.fn().mockImplementation(() => new MockedSchemas()), - }; -}); - describe('parent pipeline aggs', function() { beforeEach(() => { mockDataServices(); diff --git a/src/legacy/core_plugins/data/public/search/aggs/metrics/sibling_pipeline.test.ts b/src/legacy/core_plugins/data/public/search/aggs/metrics/sibling_pipeline.test.ts index d3456bacceb6a..8389ed8262ce5 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/metrics/sibling_pipeline.test.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/metrics/sibling_pipeline.test.ts @@ -26,15 +26,6 @@ import { AggConfigs } from '../agg_configs'; import { IMetricAggConfig, MetricAggType } from './metric_agg_type'; import { mockDataServices, mockAggTypesRegistry } from '../test_helpers'; -jest.mock('../schemas', () => { - class MockedSchemas { - all = [{}]; - } - return { - Schemas: jest.fn().mockImplementation(() => new MockedSchemas()), - }; -}); - describe('sibling pipeline aggs', () => { beforeEach(() => { mockDataServices(); diff --git a/src/legacy/core_plugins/data/public/search/aggs/metrics/top_hit.ts b/src/legacy/core_plugins/data/public/search/aggs/metrics/top_hit.ts index 3112d882bb87e..c850eb4ff2220 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/metrics/top_hit.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/metrics/top_hit.ts @@ -63,10 +63,7 @@ export const topHitMetricAgg = new MetricAggType({ name: 'field', type: 'field', onlyAggregatable: false, - filterFieldTypes: (aggConfig: IMetricAggConfig) => - _.get(aggConfig.schema, 'aggSettings.top_hits.allowStrings', false) - ? '*' - : KBN_FIELD_TYPES.NUMBER, + filterFieldTypes: '*', write(agg, output) { const field = agg.getParam('field'); output.params = {}; @@ -133,7 +130,7 @@ export const topHitMetricAgg = new MetricAggType({ defaultMessage: 'Concatenate', }), isCompatible(aggConfig: IMetricAggConfig) { - return _.get(aggConfig.schema, 'aggSettings.top_hits.allowStrings', false); + return _.get(aggConfig.params, 'field.filterFieldTypes', '*') === '*'; }, disabled: true, value: 'concat', diff --git a/src/legacy/core_plugins/data/public/search/aggs/param_types/agg.ts b/src/legacy/core_plugins/data/public/search/aggs/param_types/agg.ts index d31abe64491d0..e5b53020c3159 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/param_types/agg.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/param_types/agg.ts @@ -24,10 +24,15 @@ export class AggParamType extends Ba TAggConfig > { makeAgg: (agg: TAggConfig, state?: any) => TAggConfig; + allowedAggs: string[] = []; constructor(config: Record) { super(config); + if (config.allowedAggs) { + this.allowedAggs = config.allowedAggs; + } + if (!config.write) { this.write = (aggConfig: TAggConfig, output: Record) => { if (aggConfig.params[this.name] && aggConfig.params[this.name].length) { diff --git a/src/legacy/core_plugins/data/public/search/aggs/param_types/field.test.ts b/src/legacy/core_plugins/data/public/search/aggs/param_types/field.test.ts index 7338c41f920d7..18b666f454664 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/param_types/field.test.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/param_types/field.test.ts @@ -17,13 +17,10 @@ * under the License. */ -import { get } from 'lodash'; import { BaseParamType } from './base'; import { FieldParamType } from './field'; import { ES_FIELD_TYPES, KBN_FIELD_TYPES } from '../../../../../../../plugins/data/public'; import { IAggConfig } from '../agg_config'; -import { IMetricAggConfig } from '../metrics/metric_agg_type'; -import { Schema } from '../schemas'; describe('Field', () => { const indexPattern = { @@ -105,43 +102,5 @@ describe('Field', () => { expect(fields.length).toBe(2); }); - - it('should return only numeric fields if filterFieldTypes was specified as a function', () => { - const aggParam = new FieldParamType({ - name: 'field', - type: 'field', - filterFieldTypes: (aggConfig: IMetricAggConfig) => - get(aggConfig.schema, 'aggSettings.top_hits.allowStrings', false) - ? '*' - : KBN_FIELD_TYPES.NUMBER, - }); - const fields = aggParam.getAvailableFields(agg); - - expect(fields.length).toBe(1); - expect(fields[0].type).toBe(KBN_FIELD_TYPES.NUMBER); - }); - - it('should return all fields if filterFieldTypes was specified as a function and aggSettings allow string type fields', () => { - const aggParam = new FieldParamType({ - name: 'field', - type: 'field', - filterFieldTypes: (aggConfig: IMetricAggConfig) => - get(aggConfig.schema, 'aggSettings.top_hits.allowStrings', false) - ? '*' - : KBN_FIELD_TYPES.NUMBER, - }); - - agg.schema = { - aggSettings: { - top_hits: { - allowStrings: true, - }, - }, - } as Schema; - - const fields = aggParam.getAvailableFields(agg); - - expect(fields.length).toBe(2); - }); }); }); diff --git a/src/legacy/core_plugins/data/public/search/aggs/param_types/field.ts b/src/legacy/core_plugins/data/public/search/aggs/param_types/field.ts index bb5707cbb482e..6882b8aa39e7e 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/param_types/field.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/param_types/field.ts @@ -18,12 +18,10 @@ */ import { i18n } from '@kbn/i18n'; -import { isFunction } from 'lodash'; import { IAggConfig } from '../agg_config'; import { SavedObjectNotFound } from '../../../../../../../plugins/kibana_utils/public'; import { BaseParamType } from './base'; import { propFilter } from '../filter'; -import { IMetricAggConfig } from '../metrics/metric_agg_type'; import { IndexPatternField, indexPatterns, @@ -34,15 +32,14 @@ import { getNotifications } from '../../../../../../../plugins/data/public/servi const filterByType = propFilter('type'); -type FieldTypes = KBN_FIELD_TYPES | KBN_FIELD_TYPES[] | '*'; -export type FilterFieldTypes = ((aggConfig: IMetricAggConfig) => FieldTypes) | FieldTypes; +export type FieldTypes = KBN_FIELD_TYPES | KBN_FIELD_TYPES[] | '*'; // TODO need to make a more explicit interface for this export type IFieldParamType = FieldParamType; export class FieldParamType extends BaseParamType { required = true; scriptable = true; - filterFieldTypes: FilterFieldTypes; + filterFieldTypes: FieldTypes; onlyAggregatable: boolean; constructor(config: Record) { @@ -127,12 +124,6 @@ export class FieldParamType extends BaseParamType { return false; } - if (isFunction(filterFieldTypes)) { - const filter = filterFieldTypes(aggConfig as IMetricAggConfig); - - return filterByType([field], filter).length !== 0; - } - return filterByType([field], filterFieldTypes).length !== 0; }); diff --git a/src/legacy/core_plugins/data/public/search/aggs/types.ts b/src/legacy/core_plugins/data/public/search/aggs/types.ts index 5d02f426b5896..069a933fd994a 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/types.ts +++ b/src/legacy/core_plugins/data/public/search/aggs/types.ts @@ -26,4 +26,3 @@ export { IMetricAggType } from './metrics/metric_agg_type'; export { DateRangeKey } from './buckets/date_range'; export { IpRangeKey } from './buckets/ip_range'; export { OptionedValueProp, OptionedParamEditorProps } from './param_types/optioned'; -export { ISchemas } from './schemas'; diff --git a/src/legacy/core_plugins/data/public/search/mocks.ts b/src/legacy/core_plugins/data/public/search/mocks.ts index 5629f597edff4..46c26dc8f1bd0 100644 --- a/src/legacy/core_plugins/data/public/search/mocks.ts +++ b/src/legacy/core_plugins/data/public/search/mocks.ts @@ -71,7 +71,6 @@ export const searchStartMock = (): SearchStart => ({ calculateAutoTimeExpression: getCalculateAutoTimeExpression(coreMock.createStart().uiSettings), createAggConfigs: jest.fn().mockImplementation((indexPattern, configStates = [], schemas) => { return new AggConfigs(indexPattern, configStates, { - schemas, typesRegistry: mockAggTypesRegistry(), }); }), diff --git a/src/legacy/core_plugins/data/public/search/search_service.ts b/src/legacy/core_plugins/data/public/search/search_service.ts index a38cc98c837ce..2d01ac446d951 100644 --- a/src/legacy/core_plugins/data/public/search/search_service.ts +++ b/src/legacy/core_plugins/data/public/search/search_service.ts @@ -99,7 +99,6 @@ export class SearchService { calculateAutoTimeExpression: getCalculateAutoTimeExpression(core.uiSettings), createAggConfigs: (indexPattern, configStates = [], schemas) => { return new AggConfigs(indexPattern, configStates, { - schemas, typesRegistry: aggTypesStart, }); }, diff --git a/src/legacy/core_plugins/data/public/search/tabify/get_columns.test.ts b/src/legacy/core_plugins/data/public/search/tabify/get_columns.test.ts index 6c5dc790ef976..b7dadc3f65d82 100644 --- a/src/legacy/core_plugins/data/public/search/tabify/get_columns.test.ts +++ b/src/legacy/core_plugins/data/public/search/tabify/get_columns.test.ts @@ -19,7 +19,7 @@ import { tabifyGetColumns } from './get_columns'; import { TabbedAggColumn } from './types'; -import { AggConfigs, AggGroupNames, Schemas } from '../aggs'; +import { AggConfigs } from '../aggs'; import { mockAggTypesRegistry, mockDataServices } from '../aggs/test_helpers'; describe('get columns', () => { @@ -45,26 +45,10 @@ describe('get columns', () => { return new AggConfigs(indexPattern, aggs, { typesRegistry, - schemas: new Schemas([ - { - group: AggGroupNames.Metrics, - name: 'metric', - min: 1, - defaults: [{ schema: 'metric', type: 'count' }], - }, - ]).all, }); }; - test('should inject a count metric if no aggs exist', () => { - const columns = tabifyGetColumns(createAggConfigs().aggs, true); - - expect(columns).toHaveLength(1); - expect(columns[0]).toHaveProperty('aggConfig'); - expect(columns[0].aggConfig.type).toHaveProperty('name', 'count'); - }); - - test('should inject a count metric if only buckets exist', () => { + test('should inject the metric after each bucket if the vis is hierarchical', () => { const columns = tabifyGetColumns( createAggConfigs([ { @@ -72,18 +56,6 @@ describe('get columns', () => { schema: 'segment', params: { field: '@timestamp', interval: '10s' }, }, - ]).aggs, - true - ); - - expect(columns).toHaveLength(2); - expect(columns[1]).toHaveProperty('aggConfig'); - expect(columns[1].aggConfig.type).toHaveProperty('name', 'count'); - }); - - test('should inject the metric after each bucket if the vis is hierarchical', () => { - const columns = tabifyGetColumns( - createAggConfigs([ { type: 'date_histogram', schema: 'segment', @@ -100,9 +72,7 @@ describe('get columns', () => { params: { field: '@timestamp', interval: '10s' }, }, { - type: 'date_histogram', - schema: 'segment', - params: { field: '@timestamp', interval: '10s' }, + type: 'count', }, ]).aggs, false diff --git a/src/legacy/core_plugins/data/public/search/tabify/response_writer.test.ts b/src/legacy/core_plugins/data/public/search/tabify/response_writer.test.ts index 94301eedac74a..91835bc948abb 100644 --- a/src/legacy/core_plugins/data/public/search/tabify/response_writer.test.ts +++ b/src/legacy/core_plugins/data/public/search/tabify/response_writer.test.ts @@ -18,7 +18,7 @@ */ import { TabbedAggResponseWriter } from './response_writer'; -import { AggConfigs, AggGroupNames, Schemas, BUCKET_TYPES } from '../aggs'; +import { AggConfigs, BUCKET_TYPES } from '../aggs'; import { mockDataServices, mockAggTypesRegistry } from '../aggs/test_helpers'; import { TabbedResponseWriterOptions } from './types'; @@ -39,6 +39,7 @@ describe('TabbedAggResponseWriter class', () => { field: 'geo.src', }, }, + { type: 'count' }, ]; const twoSplitsAggConfig = [ @@ -54,6 +55,7 @@ describe('TabbedAggResponseWriter class', () => { field: 'machine.os.raw', }, }, + { type: 'count' }, ]; const createResponseWritter = (aggs: any[] = [], opts?: Partial) => { @@ -73,14 +75,6 @@ describe('TabbedAggResponseWriter class', () => { return new TabbedAggResponseWriter( new AggConfigs(indexPattern, aggs, { typesRegistry, - schemas: new Schemas([ - { - group: AggGroupNames.Metrics, - name: 'metric', - min: 1, - defaults: [{ schema: 'metric', type: 'count' }], - }, - ]).all, }), { metricsAtAllLevels: false, diff --git a/src/legacy/core_plugins/data/public/search/tabify/tabify.test.ts b/src/legacy/core_plugins/data/public/search/tabify/tabify.test.ts index db4ad3bdea96b..7e7748c00ab43 100644 --- a/src/legacy/core_plugins/data/public/search/tabify/tabify.test.ts +++ b/src/legacy/core_plugins/data/public/search/tabify/tabify.test.ts @@ -19,7 +19,7 @@ import { IndexPattern } from '../../../../../../plugins/data/public'; import { tabifyAggResponse } from './tabify'; -import { IAggConfig, IAggConfigs, AggGroupNames, Schemas, AggConfigs } from '../aggs'; +import { IAggConfig, IAggConfigs, AggConfigs } from '../aggs'; import { mockAggTypesRegistry } from '../aggs/test_helpers'; import { metricOnly, threeTermBuckets } from 'fixtures/fake_hierarchical_data'; @@ -42,21 +42,13 @@ describe('tabifyAggResponse Integration', () => { return new AggConfigs(indexPattern, aggs, { typesRegistry, - schemas: new Schemas([ - { - group: AggGroupNames.Metrics, - name: 'metric', - min: 1, - defaults: [{ schema: 'metric', type: 'count' }], - }, - ]).all, }); }; const mockAggConfig = (agg: any): IAggConfig => (agg as unknown) as IAggConfig; test('transforms a simple response properly', () => { - const aggConfigs = createAggConfigs(); + const aggConfigs = createAggConfigs([{ type: 'count' } as any]); const resp = tabifyAggResponse(aggConfigs, metricOnly, { metricsAtAllLevels: true, diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js index 58da4f8eeddc3..fb4158a6e3e03 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js @@ -643,7 +643,7 @@ function discoverController( // no timefield, no vis, nothing to update if (!$scope.opts.timefield) return; - const buckets = $scope.vis.getAggConfig().bySchemaGroup('buckets'); + const buckets = $scope.vis.getAggConfig().byTypeName('buckets'); if (buckets && buckets.length === 1) { $scope.bucketInterval = buckets[0].buckets.getInterval(); diff --git a/src/legacy/core_plugins/tile_map/public/tile_map_type.js b/src/legacy/core_plugins/tile_map/public/tile_map_type.js index 80cec5b93f485..544b63abe82c7 100644 --- a/src/legacy/core_plugins/tile_map/public/tile_map_type.js +++ b/src/legacy/core_plugins/tile_map/public/tile_map_type.js @@ -137,7 +137,7 @@ export function createTileMapTypeDefinition(dependencies) { title: i18n.translate('tileMap.vis.map.editorConfig.schemas.geoCoordinatesTitle', { defaultMessage: 'Geo coordinates', }), - aggFilter: 'geohash_grid', + aggFilter: ['geohash_grid'], min: 1, max: 1, }, diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/__snapshots__/agg.test.tsx.snap b/src/legacy/core_plugins/vis_default_editor/public/components/__snapshots__/agg.test.tsx.snap index aed0285fd3405..ba5f2ae975cbe 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/__snapshots__/agg.test.tsx.snap +++ b/src/legacy/core_plugins/vis_default_editor/public/components/__snapshots__/agg.test.tsx.snap @@ -2,11 +2,11 @@ exports[`DefaultEditorAgg component should init with the default set of props 1`] = ` - Schema name + metric } @@ -45,9 +45,7 @@ exports[`DefaultEditorAgg component should init with the default set of props 1` "getIndexPattern": [Function], "id": "1", "params": Object {}, - "schema": Object { - "title": "Schema name", - }, + "schema": "metric", "title": "Metrics", } } @@ -58,10 +56,21 @@ exports[`DefaultEditorAgg component should init with the default set of props 1` indexPattern={Object {}} metricAggs={Array []} onAggTypeChange={[Function]} + schemas={ + Array [ + Object { + "name": "metric", + }, + ] + } setAggParamValue={[MockFunction]} setTouched={[Function]} setValidity={[Function]} - state={Object {}} + state={ + Object { + "params": Object {}, + } + } /> `; diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/agg.test.tsx b/src/legacy/core_plugins/vis_default_editor/public/components/agg.test.tsx index f5ce55e82967d..22e0ebb3d30dc 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/agg.test.tsx +++ b/src/legacy/core_plugins/vis_default_editor/public/components/agg.test.tsx @@ -28,6 +28,7 @@ import { IAggType, AggGroupNames } from '../legacy_imports'; import { DefaultEditorAgg, DefaultEditorAggProps } from './agg'; import { DefaultEditorAggParams } from './agg_params'; import { AGGS_ACTION_KEYS } from './agg_group_state'; +import { Schema } from '../schemas'; jest.mock('ui/new_platform'); @@ -55,7 +56,7 @@ describe('DefaultEditorAgg component', () => { id: '1', brandNew: true, getIndexPattern: () => ({} as IndexPattern), - schema: { title: 'Schema name' }, + schema: 'metric', title: 'Metrics', params: {}, } as any, @@ -69,13 +70,18 @@ describe('DefaultEditorAgg component', () => { isLastBucket: false, isRemovable: false, metricAggs: [], - state: {} as VisState, + state: { params: {} } as VisState, setAggParamValue, setStateParamValue, onAggTypeChange: () => {}, setAggsState, onToggleEnableAgg, removeAgg, + schemas: [ + { + name: 'metric', + } as Schema, + ], }; }); @@ -175,9 +181,7 @@ describe('DefaultEditorAgg component', () => { }); it('should add schema component', () => { - defaultProps.agg.schema = { - name: 'split', - } as any; + defaultProps.agg.schema = 'split'; const comp = mount(); expect(comp.find('RowsOrColumnsControl').exists()).toBeTruthy(); diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/agg.tsx b/src/legacy/core_plugins/vis_default_editor/public/components/agg.tsx index 5450c29450bac..30ccd4f0b6cae 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/agg.tsx +++ b/src/legacy/core_plugins/vis_default_editor/public/components/agg.tsx @@ -34,6 +34,7 @@ import { DefaultEditorAggCommonProps } from './agg_common_props'; import { AGGS_ACTION_KEYS, AggsAction } from './agg_group_state'; import { RowsOrColumnsControl } from './controls/rows_or_columns'; import { RadiusRatioOptionControl } from './controls/radius_ratio_option'; +import { getSchemaByName } from '../schemas'; export interface DefaultEditorAggProps extends DefaultEditorAggCommonProps { agg: IAggConfig; @@ -67,6 +68,7 @@ function DefaultEditorAgg({ onToggleEnableAgg, removeAgg, setAggsState, + schemas, }: DefaultEditorAggProps) { const [isEditorOpen, setIsEditorOpen] = useState((agg as any).brandNew); const [validState, setValidState] = useState(true); @@ -80,11 +82,11 @@ function DefaultEditorAgg({ let SchemaComponent; - if (agg.schema.name === 'split') { + if (agg.schema === 'split') { SchemaComponent = RowsOrColumnsControl; } - if (agg.schema.name === 'radius') { + if (agg.schema === 'radius') { SchemaComponent = RadiusRatioOptionControl; } @@ -255,10 +257,10 @@ function DefaultEditorAgg({ ); }; - + const schemaTitle = getSchemaByName(schemas, agg.schema).title; const buttonContent = ( <> - {agg.schema.title} {showDescription && {aggDescription}} + {schemaTitle || agg.schema} {showDescription && {aggDescription}} ); @@ -272,7 +274,7 @@ function DefaultEditorAgg({ className="visEditorSidebar__section visEditorSidebar__collapsible visEditorSidebar__collapsible--marginBottom" aria-label={i18n.translate('visDefaultEditor.agg.toggleEditorButtonAriaLabel', { defaultMessage: 'Toggle {schema} editor', - values: { schema: agg.schema.title }, + values: { schema: schemaTitle || agg.schema }, })} data-test-subj={`visEditorAggAccordion${agg.id}`} extraAction={renderAggButtons()} @@ -303,6 +305,7 @@ function DefaultEditorAgg({ onAggTypeChange={onAggTypeChange} setTouched={setTouched} setValidity={setValidity} + schemas={schemas} /> diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/agg_add.tsx b/src/legacy/core_plugins/vis_default_editor/public/components/agg_add.tsx index d8df5b315fca0..24cb83498d4d0 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/agg_add.tsx +++ b/src/legacy/core_plugins/vis_default_editor/public/components/agg_add.tsx @@ -29,7 +29,8 @@ import { } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; import { i18n } from '@kbn/i18n'; -import { IAggConfig, AggGroupNames, Schema } from '../legacy_imports'; +import { IAggConfig, AggGroupNames } from '../legacy_imports'; +import { Schema } from '../schemas'; interface DefaultEditorAggAddProps { group?: IAggConfig[]; @@ -72,7 +73,7 @@ function DefaultEditorAggAdd({ : i18n.translate('visDefaultEditor.aggAdd.metricLabel', { defaultMessage: 'metric' }); const isSchemaDisabled = (schema: Schema): boolean => { - const count = group.filter(agg => agg.schema.name === schema.name).length; + const count = group.filter(agg => agg.schema === schema.name).length; return count >= schema.max; }; diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/agg_common_props.ts b/src/legacy/core_plugins/vis_default_editor/public/components/agg_common_props.ts index 17d2c18d2532c..b43894e74689f 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/agg_common_props.ts +++ b/src/legacy/core_plugins/vis_default_editor/public/components/agg_common_props.ts @@ -18,7 +18,8 @@ */ import { VisState, VisParams } from 'src/legacy/core_plugins/visualizations/public'; -import { IAggType, IAggConfig, AggGroupNames, Schema } from '../legacy_imports'; +import { IAggType, IAggConfig, AggGroupNames } from '../legacy_imports'; +import { Schema } from '../schemas'; type AggId = IAggConfig['id']; type AggParams = IAggConfig['params']; @@ -44,4 +45,5 @@ export interface DefaultEditorAggCommonProps extends DefaultEditorCommonProps { setStateParamValue: (paramName: T, value: VisParams[T]) => void; onToggleEnableAgg: (aggId: AggId, isEnable: boolean) => void; removeAgg: (aggId: AggId) => void; + schemas: Schema[]; } diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/agg_group.test.tsx b/src/legacy/core_plugins/vis_default_editor/public/components/agg_group.test.tsx index c36c0176439f9..ec467480539ab 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/agg_group.test.tsx +++ b/src/legacy/core_plugins/vis_default_editor/public/components/agg_group.test.tsx @@ -21,10 +21,11 @@ import React from 'react'; import { mount, shallow } from 'enzyme'; import { act } from 'react-dom/test-utils'; import { VisState } from 'src/legacy/core_plugins/visualizations/public'; -import { IAggConfigs, IAggConfig, Schema } from '../legacy_imports'; +import { IAggConfigs, IAggConfig } from '../legacy_imports'; import { DefaultEditorAggGroup, DefaultEditorAggGroupProps } from './agg_group'; import { DefaultEditorAgg } from './agg'; import { DefaultEditorAggAdd } from './agg_add'; +import { Schema } from '../schemas'; jest.mock('@elastic/eui', () => ({ EuiTitle: 'eui-title', @@ -75,7 +76,7 @@ describe('DefaultEditorAgg component', () => { type: 'number', }, }, - schema: { group: 'metrics' }, + schema: 'metrics', } as IAggConfig, { id: '3', @@ -84,7 +85,7 @@ describe('DefaultEditorAgg component', () => { type: 'string', }, }, - schema: { group: 'metrics' }, + schema: 'metrics', } as IAggConfig, { id: '2', @@ -93,7 +94,7 @@ describe('DefaultEditorAgg component', () => { type: 'number', }, }, - schema: { group: 'buckets' }, + schema: 'buckets', } as IAggConfig, ], } as IAggConfigs; @@ -107,9 +108,13 @@ describe('DefaultEditorAgg component', () => { } as VisState, schemas: [ { + name: 'metrics', + group: 'metrics', max: 1, } as Schema, { + name: 'buckets', + group: 'buckets', max: 1, } as Schema, ], diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/agg_group.tsx b/src/legacy/core_plugins/vis_default_editor/public/components/agg_group.tsx index 768a9669025e4..a15a98d4983ce 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/agg_group.tsx +++ b/src/legacy/core_plugins/vis_default_editor/public/components/agg_group.tsx @@ -30,7 +30,7 @@ import { } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; -import { IAggConfig, aggGroupNamesMap, AggGroupNames, Schema } from '../legacy_imports'; +import { IAggConfig, aggGroupNamesMap, AggGroupNames } from '../legacy_imports'; import { DefaultEditorAgg } from './agg'; import { DefaultEditorAggAdd } from './agg_add'; import { AddSchema, ReorderAggs, DefaultEditorAggCommonProps } from './agg_common_props'; @@ -41,6 +41,7 @@ import { getEnabledMetricAggsCount, } from './agg_group_helper'; import { aggGroupReducer, initAggsState, AGGS_ACTION_KEYS } from './agg_group_state'; +import { Schema, getSchemasByGroup } from '../schemas'; export interface DefaultEditorAggGroupProps extends DefaultEditorAggCommonProps { schemas: Schema[]; @@ -69,9 +70,12 @@ function DefaultEditorAggGroup({ }: DefaultEditorAggGroupProps) { const groupNameLabel = (aggGroupNamesMap() as any)[groupName]; // e.g. buckets can have no aggs + const schemaNames = getSchemasByGroup(schemas, groupName).map(s => s.name); const group: IAggConfig[] = useMemo( - () => state.aggs.aggs.filter((agg: IAggConfig) => agg.schema.group === groupName) || [], - [groupName, state.aggs.aggs] + () => + state.aggs.aggs.filter((agg: IAggConfig) => agg.schema && schemaNames.includes(agg.schema)) || + [], + [state.aggs.aggs, schemaNames] ); const stats = { @@ -162,14 +166,14 @@ function DefaultEditorAggGroup({ 1} isLastBucket={groupName === AggGroupNames.Buckets && index === group.length - 1} - isRemovable={isAggRemovable(agg, group)} - isDisabled={agg.schema.name === 'metric' && isMetricAggregationDisabled} + isRemovable={isAggRemovable(agg, group, schemas)} + isDisabled={agg.schema === 'metric' && isMetricAggregationDisabled} lastParentPipelineAggTitle={lastParentPipelineAggTitle} metricAggs={metricAggs} state={state} @@ -179,6 +183,7 @@ function DefaultEditorAggGroup({ onToggleEnableAgg={onToggleEnableAgg} removeAgg={removeAgg} setAggsState={setAggsState} + schemas={schemas} /> )} diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/agg_group_helper.test.ts b/src/legacy/core_plugins/vis_default_editor/public/components/agg_group_helper.test.ts index b18e5af27f8b4..aebece29e7ae6 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/agg_group_helper.test.ts +++ b/src/legacy/core_plugins/vis_default_editor/public/components/agg_group_helper.test.ts @@ -25,9 +25,11 @@ import { getEnabledMetricAggsCount, } from './agg_group_helper'; import { AggsState } from './agg_group_state'; +import { Schema } from '../schemas'; describe('DefaultEditorGroup helpers', () => { let group: IAggConfig[]; + let schemas: Schema[]; beforeEach(() => { group = [ @@ -38,7 +40,7 @@ describe('DefaultEditorGroup helpers', () => { type: 'number', }, }, - schema: { name: 'metric', min: 1, mustBeFirst: true }, + schema: 'metric', } as IAggConfig, { id: '2', @@ -47,20 +49,45 @@ describe('DefaultEditorGroup helpers', () => { type: 'string', }, }, - schema: { name: 'metric', min: 2 }, + schema: 'metric2', } as IAggConfig, ]; + schemas = [ + { + name: 'metric', + title: 'Metric', + group: 'metrics', + min: 0, + max: 3, + aggFilter: [], + editor: false, + params: [], + defaults: null, + mustBeFirst: true, + }, + { + name: 'metric2', + title: 'Metric', + group: 'metrics', + min: 2, + max: 3, + aggFilter: [], + editor: false, + params: [], + defaults: null, + }, + ]; }); describe('isAggRemovable', () => { it('should return true when the number of aggs with the same schema is above the min', () => { - const isRemovable = isAggRemovable(group[0], group); + const isRemovable = isAggRemovable(group[0], group, schemas); expect(isRemovable).toBeTruthy(); }); it('should return false when the number of aggs with the same schema is not above the min', () => { - const isRemovable = isAggRemovable(group[1], group); + const isRemovable = isAggRemovable(group[1], group, schemas); expect(isRemovable).toBeFalsy(); }); @@ -77,6 +104,7 @@ describe('DefaultEditorGroup helpers', () => { it('should return 2 when there are multiple enabled aggs', () => { group[0].enabled = true; group[1].enabled = true; + group[1].schema = 'metric'; const enabledAggs = getEnabledMetricAggsCount(group); expect(enabledAggs).toBe(2); @@ -85,26 +113,26 @@ describe('DefaultEditorGroup helpers', () => { describe('calcAggIsTooLow', () => { it('should return false when agg.schema.mustBeFirst has falsy value', () => { - const isRemovable = calcAggIsTooLow(group[1], 0, group); + const isRemovable = calcAggIsTooLow(group[1], 0, group, schemas); expect(isRemovable).toBeFalsy(); }); it('should return false when there is no different schema', () => { group[1].schema = group[0].schema; - const isRemovable = calcAggIsTooLow(group[0], 0, group); + const isRemovable = calcAggIsTooLow(group[0], 0, group, schemas); expect(isRemovable).toBeFalsy(); }); it('should return false when different schema is not less than agg index', () => { - const isRemovable = calcAggIsTooLow(group[0], 0, group); + const isRemovable = calcAggIsTooLow(group[0], 0, group, schemas); expect(isRemovable).toBeFalsy(); }); it('should return true when agg index is greater than different schema index', () => { - const isRemovable = calcAggIsTooLow(group[0], 2, group); + const isRemovable = calcAggIsTooLow(group[0], 2, group, schemas); expect(isRemovable).toBeTruthy(); }); diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/agg_group_helper.tsx b/src/legacy/core_plugins/vis_default_editor/public/components/agg_group_helper.tsx index d2e8e5401c0f7..0a8c5c3077ada 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/agg_group_helper.tsx +++ b/src/legacy/core_plugins/vis_default_editor/public/components/agg_group_helper.tsx @@ -20,27 +20,34 @@ import { findIndex, isEmpty } from 'lodash'; import { IAggConfig } from '../legacy_imports'; import { AggsState } from './agg_group_state'; +import { Schema, getSchemaByName } from '../schemas'; -const isAggRemovable = (agg: IAggConfig, group: IAggConfig[]) => { +const isAggRemovable = (agg: IAggConfig, group: IAggConfig[], schemas: Schema[]) => { + const schema = getSchemaByName(schemas, agg.schema); const metricCount = group.reduce( - (count, aggregation: IAggConfig) => - aggregation.schema.name === agg.schema.name ? ++count : count, + (count, aggregation: IAggConfig) => (aggregation.schema === agg.schema ? ++count : count), 0 ); // make sure the the number of these aggs is above the min - return metricCount > agg.schema.min; + return metricCount > schema.min; }; const getEnabledMetricAggsCount = (group: IAggConfig[]) => { return group.reduce( (count, aggregation: IAggConfig) => - aggregation.schema.name === 'metric' && aggregation.enabled ? ++count : count, + aggregation.schema === 'metric' && aggregation.enabled ? ++count : count, 0 ); }; -const calcAggIsTooLow = (agg: IAggConfig, aggIndex: number, group: IAggConfig[]) => { - if (!agg.schema.mustBeFirst) { +const calcAggIsTooLow = ( + agg: IAggConfig, + aggIndex: number, + group: IAggConfig[], + schemas: Schema[] +) => { + const schema = getSchemaByName(schemas, agg.schema); + if (!schema.mustBeFirst) { return false; } diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/agg_param_props.ts b/src/legacy/core_plugins/vis_default_editor/public/components/agg_param_props.ts index 843cfddc07010..cdc5a4c8f8a77 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/agg_param_props.ts +++ b/src/legacy/core_plugins/vis_default_editor/public/components/agg_param_props.ts @@ -22,6 +22,7 @@ import { VisState } from 'src/legacy/core_plugins/visualizations/public'; import { IAggConfig, AggParam } from '../legacy_imports'; import { ComboBoxGroupedOptions } from '../utils'; import { EditorConfig } from './utils'; +import { Schema } from '../schemas'; // NOTE: we cannot export the interface with export { InterfaceName } // as there is currently a bug on babel typescript transform plugin for it @@ -38,6 +39,7 @@ export interface AggParamCommonProps { state: VisState; value?: T; metricAggs: IAggConfig[]; + schemas: Schema[]; } export interface AggParamEditorProps extends AggParamCommonProps { diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/agg_params.test.tsx b/src/legacy/core_plugins/vis_default_editor/public/components/agg_params.test.tsx index af851aa9b4418..d2821566fcb37 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/agg_params.test.tsx +++ b/src/legacy/core_plugins/vis_default_editor/public/components/agg_params.test.tsx @@ -105,6 +105,7 @@ describe('DefaultEditorAggParams component', () => { onAggTypeChange, setTouched, setValidity, + schemas: [], }; }); diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/agg_params.tsx b/src/legacy/core_plugins/vis_default_editor/public/components/agg_params.tsx index e9583ab4cec79..510c21af95da1 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/agg_params.tsx +++ b/src/legacy/core_plugins/vis_default_editor/public/components/agg_params.tsx @@ -40,6 +40,7 @@ import { } from './agg_params_state'; import { DefaultEditorCommonProps } from './agg_common_props'; import { EditorParamConfig, TimeIntervalParam, FixedParam, getEditorConfig } from './utils'; +import { Schema, getSchemaByName } from '../schemas'; const FIXED_VALUE_PROP = 'fixedValue'; const DEFAULT_PROP = 'default'; @@ -57,6 +58,9 @@ export interface DefaultEditorAggParamsProps extends DefaultEditorCommonProps { indexPattern: IndexPattern; setValidity: (isValid: boolean) => void; setTouched: (isTouched: boolean) => void; + schemas: Schema[]; + allowedAggs?: string[]; + hideCustomLabel?: boolean; } function DefaultEditorAggParams({ @@ -75,16 +79,22 @@ function DefaultEditorAggParams({ onAggTypeChange, setTouched, setValidity, + schemas, + allowedAggs = [], + hideCustomLabel = false, }: DefaultEditorAggParamsProps) { - const groupedAggTypeOptions = useMemo(() => getAggTypeOptions(agg, indexPattern, groupName), [ - agg, - indexPattern, - groupName, - ]); + const schema = getSchemaByName(schemas, agg.schema); + const { title } = schema; + const aggFilter = [...allowedAggs, ...(schema.aggFilter || [])]; + const groupedAggTypeOptions = useMemo( + () => getAggTypeOptions(agg, indexPattern, groupName, aggFilter), + [agg, indexPattern, groupName, aggFilter] + ); + const error = aggIsTooLow ? i18n.translate('visDefaultEditor.aggParams.errors.aggWrongRunOrderErrorMessage', { defaultMessage: '"{schema}" aggs must run before all other buckets!', - values: { schema: agg.schema.title }, + values: { schema: title }, }) : ''; const aggTypeName = agg.type?.name; @@ -94,12 +104,10 @@ function DefaultEditorAggParams({ aggTypeName, fieldName, ]); - const params = useMemo(() => getAggParamsToRender({ agg, editorConfig, metricAggs, state }), [ - agg, - editorConfig, - metricAggs, - state, - ]); + const params = useMemo( + () => getAggParamsToRender({ agg, editorConfig, metricAggs, state, schemas, hideCustomLabel }), + [agg, editorConfig, metricAggs, state, schemas, hideCustomLabel] + ); const allParams = [...params.basic, ...params.advanced]; const [paramsState, onChangeParamsState] = useReducer( aggParamsReducer, diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/agg_params_helper.test.ts b/src/legacy/core_plugins/vis_default_editor/public/components/agg_params_helper.test.ts index f3bee80baa1ba..047467750794b 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/agg_params_helper.test.ts +++ b/src/legacy/core_plugins/vis_default_editor/public/components/agg_params_helper.test.ts @@ -27,6 +27,7 @@ import { } from './agg_params_helper'; import { FieldParamEditor, OrderByParamEditor } from './controls'; import { EditorConfig } from './utils'; +import { Schema } from '../schemas'; jest.mock('../utils', () => ({ groupAndSortBy: jest.fn(() => ['indexedFields']), @@ -38,6 +39,15 @@ describe('DefaultEditorAggParams helpers', () => { describe('getAggParamsToRender', () => { let agg: IAggConfig; let editorConfig: EditorConfig; + const schemas: Schema[] = [ + { + name: 'metric', + } as Schema, + { + name: 'metric2', + hideCustomLabel: true, + } as Schema, + ]; const state = {} as VisState; const metricAggs: IAggConfig[] = []; const emptyParams = { @@ -50,16 +60,16 @@ describe('DefaultEditorAggParams helpers', () => { type: { params: [{ name: 'interval' }], }, - schema: {}, + schema: 'metric', } as IAggConfig; - const params = getAggParamsToRender({ agg, editorConfig, metricAggs, state }); + const params = getAggParamsToRender({ agg, editorConfig, metricAggs, state, schemas }); expect(params).toEqual(emptyParams); }); it('should not create any param if there is no agg type', () => { - agg = {} as IAggConfig; - const params = getAggParamsToRender({ agg, editorConfig, metricAggs, state }); + agg = { schema: 'metric' } as IAggConfig; + const params = getAggParamsToRender({ agg, editorConfig, metricAggs, state, schemas }); expect(params).toEqual(emptyParams); }); @@ -75,21 +85,19 @@ describe('DefaultEditorAggParams helpers', () => { hidden: true, }, }; - const params = getAggParamsToRender({ agg, editorConfig, metricAggs, state }); + const params = getAggParamsToRender({ agg, editorConfig, metricAggs, state, schemas }); expect(params).toEqual(emptyParams); }); it('should skip customLabel param if it is hidden', () => { - agg = { + agg = ({ type: { params: [{ name: 'customLabel' }], }, - schema: { - hideCustomLabel: true, - }, - } as IAggConfig; - const params = getAggParamsToRender({ agg, editorConfig, metricAggs, state }); + schema: 'metric2', + } as any) as IAggConfig; + const params = getAggParamsToRender({ agg, editorConfig, metricAggs, state, schemas }); expect(params).toEqual(emptyParams); }); @@ -116,7 +124,7 @@ describe('DefaultEditorAggParams helpers', () => { }, ], }, - schema: {}, + schema: 'metric', getIndexPattern: jest.fn(() => ({ fields: [ { name: '@timestamp', type: 'date' }, @@ -128,7 +136,7 @@ describe('DefaultEditorAggParams helpers', () => { field: 'field', }, } as any) as IAggConfig; - const params = getAggParamsToRender({ agg, editorConfig, metricAggs, state }); + const params = getAggParamsToRender({ agg, editorConfig, metricAggs, state, schemas }); expect(params).toEqual({ basic: [ @@ -140,6 +148,7 @@ describe('DefaultEditorAggParams helpers', () => { paramEditor: FieldParamEditor, metricAggs, state, + schemas, value: agg.params.field, }, { @@ -150,6 +159,7 @@ describe('DefaultEditorAggParams helpers', () => { paramEditor: OrderByParamEditor, metricAggs, state, + schemas, value: agg.params.orderBy, }, ], @@ -162,7 +172,7 @@ describe('DefaultEditorAggParams helpers', () => { describe('getAggTypeOptions', () => { it('should return agg type options grouped by subtype', () => { const indexPattern = {} as IndexPattern; - const aggs = getAggTypeOptions({} as IAggConfig, indexPattern, 'metrics'); + const aggs = getAggTypeOptions({} as IAggConfig, indexPattern, 'metrics', []); expect(aggs).toEqual(['indexedFields']); }); diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/agg_params_helper.ts b/src/legacy/core_plugins/vis_default_editor/public/components/agg_params_helper.ts index 0c0726ec67d50..520ff6ffc5ff5 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/agg_params_helper.ts +++ b/src/legacy/core_plugins/vis_default_editor/public/components/agg_params_helper.ts @@ -35,12 +35,15 @@ import { IAggType, } from '../legacy_imports'; import { EditorConfig } from './utils'; +import { Schema, getSchemaByName } from '../schemas'; interface ParamInstanceBase { agg: IAggConfig; editorConfig: EditorConfig; metricAggs: IAggConfig[]; state: VisState; + schemas: Schema[]; + hideCustomLabel?: boolean; } export interface ParamInstance extends ParamInstanceBase { @@ -50,7 +53,14 @@ export interface ParamInstance extends ParamInstanceBase { value: unknown; } -function getAggParamsToRender({ agg, editorConfig, metricAggs, state }: ParamInstanceBase) { +function getAggParamsToRender({ + agg, + editorConfig, + metricAggs, + state, + schemas, + hideCustomLabel, +}: ParamInstanceBase) { const params = { basic: [] as ParamInstance[], advanced: [] as ParamInstance[], @@ -63,19 +73,26 @@ function getAggParamsToRender({ agg, editorConfig, metricAggs, state }: ParamIns .filter((param: AggParam) => !get(editorConfig, [param.name, 'hidden'], false))) || []; + const schema = getSchemaByName(schemas, agg.schema); // build collection of agg params components paramsToRender.forEach((param: AggParam, index: number) => { let indexedFields: ComboBoxGroupedOptions = []; let fields: IndexPatternField[]; - if (agg.schema.hideCustomLabel && param.name === 'customLabel') { + if (hideCustomLabel && param.name === 'customLabel') { return; } // if field param exists, compute allowed fields if (param.type === 'field') { - const availableFields: IndexPatternField[] = (param as IFieldParamType).getAvailableFields( - agg - ); + let availableFields: IndexPatternField[] = (param as IFieldParamType).getAvailableFields(agg); + // should be refactored in the future to provide a more general way + // for visualization to override some agg config settings + if (agg.type.name === 'top_hits' && param.name === 'field') { + const allowStrings = _.get(schema, `aggSettings[${agg.type.name}].allowStrings`, false); + if (!allowStrings) { + availableFields = availableFields.filter(field => field.type === 'number'); + } + } fields = aggTypeFieldFilters.filter(availableFields, agg); indexedFields = groupAndSortBy(fields, 'type', 'name'); @@ -109,6 +126,8 @@ function getAggParamsToRender({ agg, editorConfig, metricAggs, state }: ParamIns metricAggs, state, value: agg.params[param.name], + schemas, + hideCustomLabel, }); } }); @@ -119,9 +138,15 @@ function getAggParamsToRender({ agg, editorConfig, metricAggs, state }: ParamIns function getAggTypeOptions( agg: IAggConfig, indexPattern: IndexPattern, - groupName: string + groupName: string, + allowedAggs: string[] ): ComboBoxGroupedOptions { - const aggTypeOptions = aggTypeFilters.filter((aggTypes as any)[groupName], indexPattern, agg); + const aggTypeOptions = aggTypeFilters.filter( + (aggTypes as any)[groupName], + indexPattern, + agg, + allowedAggs + ); return groupAndSortBy(aggTypeOptions as any[], 'subtype', 'title'); } diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/controls/field.test.tsx b/src/legacy/core_plugins/vis_default_editor/public/components/controls/field.test.tsx index 89d39a0605b60..36496c2800b64 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/controls/field.test.tsx +++ b/src/legacy/core_plugins/vis_default_editor/public/components/controls/field.test.tsx @@ -81,6 +81,7 @@ describe('FieldParamEditor component', () => { setTouched, state: {} as VisState, metricAggs: [] as IAggConfig[], + schemas: [], }; }); diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/controls/order_agg.tsx b/src/legacy/core_plugins/vis_default_editor/public/components/controls/order_agg.tsx index 10679b578d54e..8c020c668b3c6 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/controls/order_agg.tsx +++ b/src/legacy/core_plugins/vis_default_editor/public/components/controls/order_agg.tsx @@ -35,6 +35,7 @@ function OrderAggParamEditor({ setValue, setValidity, setTouched, + schemas, }: AggParamEditorProps) { const orderBy = agg.params.orderBy; @@ -64,6 +65,8 @@ function OrderAggParamEditor({ ); diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/controls/percentiles.test.tsx b/src/legacy/core_plugins/vis_default_editor/public/components/controls/percentiles.test.tsx index 020dbb351b497..0eaf9bcc987c1 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/controls/percentiles.test.tsx +++ b/src/legacy/core_plugins/vis_default_editor/public/components/controls/percentiles.test.tsx @@ -47,6 +47,7 @@ describe('PercentilesEditor component', () => { setTouched, state: {} as VisState, metricAggs: [] as IAggConfig[], + schemas: [], }; }); diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/controls/rows_or_columns.tsx b/src/legacy/core_plugins/vis_default_editor/public/components/controls/rows_or_columns.tsx index 65c7964709279..83a341e045b5c 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/controls/rows_or_columns.tsx +++ b/src/legacy/core_plugins/vis_default_editor/public/components/controls/rows_or_columns.tsx @@ -28,8 +28,11 @@ const PARAMS = { COLUMNS: 'visEditorSplitBy__false', }; -function RowsOrColumnsControl({ agg, setAggParamValue }: AggControlProps) { - const idSelected = `visEditorSplitBy__${agg.params.row}`; +function RowsOrColumnsControl({ editorStateParams, setStateParamValue }: AggControlProps) { + if (editorStateParams.row === undefined) { + setStateParamValue(PARAMS.NAME, true); + } + const idSelected = `visEditorSplitBy__${editorStateParams.row}`; const options = [ { id: PARAMS.ROWS, @@ -45,8 +48,8 @@ function RowsOrColumnsControl({ agg, setAggParamValue }: AggControlProps) { }, ]; const onChange = useCallback( - optionId => setAggParamValue(agg.id, PARAMS.NAME, optionId === PARAMS.ROWS), - [setAggParamValue] + optionId => setStateParamValue(PARAMS.NAME, optionId === PARAMS.ROWS), + [setStateParamValue] ); return ( diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/controls/sub_agg.tsx b/src/legacy/core_plugins/vis_default_editor/public/components/controls/sub_agg.tsx index 5dc28b59a52b3..5bc94bd4af226 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/controls/sub_agg.tsx +++ b/src/legacy/core_plugins/vis_default_editor/public/components/controls/sub_agg.tsx @@ -35,6 +35,7 @@ function SubAggParamEditor({ setValue, setValidity, setTouched, + schemas, }: AggParamEditorProps) { useEffect(() => { // we aren't creating a custom aggConfig @@ -61,6 +62,7 @@ function SubAggParamEditor({ ); diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/controls/sub_metric.tsx b/src/legacy/core_plugins/vis_default_editor/public/components/controls/sub_metric.tsx index 45ff0610d88ed..9d48b1c964a27 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/controls/sub_metric.tsx +++ b/src/legacy/core_plugins/vis_default_editor/public/components/controls/sub_metric.tsx @@ -35,6 +35,7 @@ function SubMetricParamEditor({ setValue, setValidity, setTouched, + schemas, }: AggParamEditorProps) { const metricTitle = i18n.translate('visDefaultEditor.controls.metrics.metricTitle', { defaultMessage: 'Metric', @@ -73,6 +74,7 @@ function SubMetricParamEditor({ ); diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/controls/test_utils.ts b/src/legacy/core_plugins/vis_default_editor/public/components/controls/test_utils.ts index 4280f85c901d7..8a21114999cd6 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/controls/test_utils.ts +++ b/src/legacy/core_plugins/vis_default_editor/public/components/controls/test_utils.ts @@ -29,4 +29,5 @@ export const aggParamCommonPropsMock = { metricAggs: [] as IAggConfig[], state: {} as VisState, showValidation: false, + schemas: [], }; diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/sidebar/data_tab.tsx b/src/legacy/core_plugins/vis_default_editor/public/components/sidebar/data_tab.tsx index efd17f02a0e09..1c1f9d57d8b90 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/sidebar/data_tab.tsx +++ b/src/legacy/core_plugins/vis_default_editor/public/components/sidebar/data_tab.tsx @@ -25,7 +25,6 @@ import { VisState } from 'src/legacy/core_plugins/visualizations/public'; import { IAggConfig, AggGroupNames, - ISchemas, parentPipelineType, IMetricAggType, } from '../../legacy_imports'; @@ -40,6 +39,7 @@ import { toggleEnabledAgg, } from './state'; import { AddSchema, ReorderAggs, DefaultEditorAggCommonProps } from '../agg_common_props'; +import { ISchemas } from '../../schemas'; export interface DefaultEditorDataTabProps { dispatch: React.Dispatch; @@ -76,8 +76,8 @@ function DefaultEditorDataTab({ const addSchema: AddSchema = useCallback(schema => dispatch(addNewAgg(schema)), [dispatch]); const onAggRemove: DefaultEditorAggCommonProps['removeAgg'] = useCallback( - aggId => dispatch(removeAgg(aggId)), - [dispatch] + aggId => dispatch(removeAgg(aggId, schemas.all || [])), + [dispatch, schemas] ); const onReorderAggs: ReorderAggs = useCallback((...props) => dispatch(reorderAggs(...props)), [ diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/sidebar/sidebar.tsx b/src/legacy/core_plugins/vis_default_editor/public/components/sidebar/sidebar.tsx index a70ffd3cd88e1..1efd8dae8178b 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/sidebar/sidebar.tsx +++ b/src/legacy/core_plugins/vis_default_editor/public/components/sidebar/sidebar.tsx @@ -31,6 +31,7 @@ import { DefaultEditorAggCommonProps } from '../agg_common_props'; import { SidebarTitle } from './sidebar_title'; import { PersistedState } from '../../../../../../plugins/visualizations/public'; import { SavedSearch } from '../../../../../../plugins/discover/public'; +import { getSchemasByGroup } from '../../schemas'; interface DefaultEditorSideBarProps { isCollapsed: boolean; @@ -57,9 +58,12 @@ function DefaultEditorSideBar({ const { formState, setTouched, setValidity, resetValidity } = useEditorFormState(); const responseAggs = useMemo(() => state.aggs.getResponseAggs(), [state.aggs]); + const metricSchemas = getSchemasByGroup(vis.type.schemas.all || [], AggGroupNames.Metrics).map( + s => s.name + ); const metricAggs = useMemo( - () => responseAggs.filter(agg => get(agg, 'schema.group') === AggGroupNames.Metrics), - [responseAggs] + () => responseAggs.filter(agg => metricSchemas.includes(get(agg, 'schema'))), + [responseAggs, metricSchemas] ); const hasHistogramAgg = useMemo(() => responseAggs.some(agg => agg.type.name === 'histogram'), [ responseAggs, diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/sidebar/state/actions.ts b/src/legacy/core_plugins/vis_default_editor/public/components/sidebar/state/actions.ts index 93fa1083bebf9..f9915bedc8878 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/sidebar/state/actions.ts +++ b/src/legacy/core_plugins/vis_default_editor/public/components/sidebar/state/actions.ts @@ -18,8 +18,9 @@ */ import { Vis, VisParams } from 'src/legacy/core_plugins/visualizations/public'; -import { IAggConfig, Schema } from '../../../legacy_imports'; +import { IAggConfig } from '../../../legacy_imports'; import { EditorStateActionTypes } from './constants'; +import { Schema } from '../../../schemas'; export interface ActionType { type: T; @@ -47,7 +48,7 @@ type SetStateParamValue = ActionTyp EditorStateActionTypes.SET_STATE_PARAM_VALUE, { paramName: T; value: AggParams[T] } >; -type RemoveAgg = ActionType; +type RemoveAgg = ActionType; type ReorderAggs = ActionType< EditorStateActionTypes.REORDER_AGGS, { sourceAgg: IAggConfig; destinationAgg: IAggConfig } @@ -85,7 +86,7 @@ export interface EditorActions { paramName: T, value: AggParams[T] ): SetStateParamValue; - removeAgg(aggId: AggId): RemoveAgg; + removeAgg(aggId: AggId, schemas: Schema[]): RemoveAgg; reorderAggs(sourceAgg: IAggConfig, destinationAgg: IAggConfig): ReorderAggs; toggleEnabledAgg(aggId: AggId, enabled: IAggConfig['enabled']): ToggleEnabledAgg; updateStateParams(params: VisParams): UpdateStateParams; @@ -128,10 +129,11 @@ const setStateParamValue: EditorActions['setStateParamValue'] = (paramName, valu }, }); -const removeAgg: EditorActions['removeAgg'] = aggId => ({ +const removeAgg: EditorActions['removeAgg'] = (aggId, schemas) => ({ type: EditorStateActionTypes.REMOVE_AGG, payload: { aggId, + schemas, }, }); diff --git a/src/legacy/core_plugins/vis_default_editor/public/components/sidebar/state/reducers.ts b/src/legacy/core_plugins/vis_default_editor/public/components/sidebar/state/reducers.ts index 6ae4e415f8caa..73675e75cbe36 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/components/sidebar/state/reducers.ts +++ b/src/legacy/core_plugins/vis_default_editor/public/components/sidebar/state/reducers.ts @@ -20,8 +20,7 @@ import { cloneDeep } from 'lodash'; import { Vis, VisState } from 'src/legacy/core_plugins/visualizations/public'; - -import { createAggConfigs, IAggConfig, AggGroupNames } from '../../../legacy_imports'; +import { createAggConfigs, AggGroupNames } from '../../../legacy_imports'; import { EditorStateActionTypes } from './constants'; import { getEnabledMetricAggsCount } from '../../agg_group_helper'; import { EditorAction } from './actions'; @@ -33,8 +32,12 @@ function initEditorState(vis: Vis) { function editorStateReducer(state: VisState, action: EditorAction): VisState { switch (action.type) { case EditorStateActionTypes.ADD_NEW_AGG: { - const payloadAggConfig = action.payload as IAggConfig; - const aggConfig = state.aggs.createAggConfig(payloadAggConfig, { + const { schema } = action.payload; + const defaultConfig = + !state.aggs.aggs.find(agg => agg.schema === schema.name) && schema.defaults + ? (schema as any).defaults.slice(0, schema.max) + : { schema: schema.name }; + const aggConfig = state.aggs.createAggConfig(defaultConfig, { addToAggConfigs: false, }); aggConfig.brandNew = true; @@ -42,7 +45,7 @@ function editorStateReducer(state: VisState, action: EditorAction): VisState { return { ...state, - aggs: createAggConfigs(state.aggs.indexPattern, newAggs, state.aggs.schemas), + aggs: createAggConfigs(state.aggs.indexPattern, newAggs), }; } @@ -65,7 +68,7 @@ function editorStateReducer(state: VisState, action: EditorAction): VisState { return { ...state, - aggs: createAggConfigs(state.aggs.indexPattern, newAggs, state.aggs.schemas), + aggs: createAggConfigs(state.aggs.indexPattern, newAggs), }; } @@ -90,7 +93,7 @@ function editorStateReducer(state: VisState, action: EditorAction): VisState { return { ...state, - aggs: createAggConfigs(state.aggs.indexPattern, newAggs, state.aggs.schemas), + aggs: createAggConfigs(state.aggs.indexPattern, newAggs), }; } @@ -108,10 +111,10 @@ function editorStateReducer(state: VisState, action: EditorAction): VisState { case EditorStateActionTypes.REMOVE_AGG: { let isMetric = false; - const newAggs = state.aggs.aggs.filter(({ id, schema }) => { if (id === action.payload.aggId) { - if (schema.group === AggGroupNames.Metrics) { + const schemaDef = action.payload.schemas.find(s => s.name === schema); + if (schemaDef && schemaDef.group === AggGroupNames.Metrics) { isMetric = true; } @@ -122,7 +125,7 @@ function editorStateReducer(state: VisState, action: EditorAction): VisState { }); if (isMetric && getEnabledMetricAggsCount(newAggs) === 0) { - const aggToEnable = newAggs.find(agg => agg.schema.name === 'metric'); + const aggToEnable = newAggs.find(agg => agg.schema === 'metric'); if (aggToEnable) { aggToEnable.enabled = true; @@ -131,7 +134,7 @@ function editorStateReducer(state: VisState, action: EditorAction): VisState { return { ...state, - aggs: createAggConfigs(state.aggs.indexPattern, newAggs, state.aggs.schemas), + aggs: createAggConfigs(state.aggs.indexPattern, newAggs), }; } @@ -143,7 +146,7 @@ function editorStateReducer(state: VisState, action: EditorAction): VisState { return { ...state, - aggs: createAggConfigs(state.aggs.indexPattern, newAggs, state.aggs.schemas), + aggs: createAggConfigs(state.aggs.indexPattern, newAggs), }; } @@ -165,7 +168,7 @@ function editorStateReducer(state: VisState, action: EditorAction): VisState { return { ...state, - aggs: createAggConfigs(state.aggs.indexPattern, newAggs, state.aggs.schemas), + aggs: createAggConfigs(state.aggs.indexPattern, newAggs), }; } diff --git a/src/legacy/core_plugins/vis_default_editor/public/index.ts b/src/legacy/core_plugins/vis_default_editor/public/index.ts index fa6c2ee6d5ec7..156d50f451b57 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/index.ts +++ b/src/legacy/core_plugins/vis_default_editor/public/index.ts @@ -23,3 +23,4 @@ export { RangesParamEditor, RangeValues } from './components/controls/ranges'; export * from './editor_size'; export * from './vis_options_props'; export * from './utils'; +export { ISchemas, Schemas, Schema } from './schemas'; diff --git a/src/legacy/core_plugins/vis_default_editor/public/legacy_imports.ts b/src/legacy/core_plugins/vis_default_editor/public/legacy_imports.ts index 8aed263c4e4d1..5c02b50286a95 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/legacy_imports.ts +++ b/src/legacy/core_plugins/vis_default_editor/public/legacy_imports.ts @@ -32,8 +32,6 @@ export { IFieldParamType, BUCKET_TYPES, METRIC_TYPES, - ISchemas, - Schema, termsAggFilter, } from 'ui/agg_types'; export { aggTypeFilters, propFilter } from 'ui/agg_types'; diff --git a/src/legacy/core_plugins/data/public/search/aggs/schemas.ts b/src/legacy/core_plugins/vis_default_editor/public/schemas.ts similarity index 84% rename from src/legacy/core_plugins/data/public/search/aggs/schemas.ts rename to src/legacy/core_plugins/vis_default_editor/public/schemas.ts index 1aa5ebe08656b..5849d9d80011e 100644 --- a/src/legacy/core_plugins/data/public/search/aggs/schemas.ts +++ b/src/legacy/core_plugins/vis_default_editor/public/schemas.ts @@ -22,16 +22,17 @@ import _ from 'lodash'; import { Optional } from '@kbn/utility-types'; import { IndexedArray } from 'ui/indexed_array'; -import { AggGroupNames } from './agg_groups'; -import { AggParam } from './agg_params'; +import { AggGroupNames } from '../../data/public/search/aggs/agg_groups'; +import { AggParam } from '../../data/public/search/aggs/agg_params'; export interface ISchemas { [AggGroupNames.Buckets]: Schema[]; [AggGroupNames.Metrics]: Schema[]; + all: Schema[]; } export interface Schema { - aggFilter: string | string[]; + aggFilter: string[]; editor: boolean | string; group: AggGroupNames; max: number; @@ -103,3 +104,11 @@ export class Schemas { .commit(); } } + +export const getSchemaByName = (schemas: Schema[], schemaName?: string) => { + return schemas.find(s => s.name === schemaName) || ({} as Schema); +}; + +export const getSchemasByGroup = (schemas: Schema[], schemaGroup?: string) => { + return schemas.filter(s => s.group === schemaGroup); +}; diff --git a/src/legacy/core_plugins/vis_default_editor/public/vis_type_agg_filter.ts b/src/legacy/core_plugins/vis_default_editor/public/vis_type_agg_filter.ts index 60b675f50a342..fcb06f73513b0 100644 --- a/src/legacy/core_plugins/vis_default_editor/public/vis_type_agg_filter.ts +++ b/src/legacy/core_plugins/vis_default_editor/public/vis_type_agg_filter.ts @@ -26,8 +26,8 @@ const filterByName = propFilter('name'); * and limits available aggregations based on that. */ aggTypeFilters.addFilter( - (aggType: IAggType, indexPatterns: IndexPattern, aggConfig: IAggConfig) => { - const doesSchemaAllowAggType = filterByName([aggType], aggConfig.schema.aggFilter).length !== 0; + (aggType: IAggType, indexPatterns: IndexPattern, aggConfig: IAggConfig, aggFilter: string[]) => { + const doesSchemaAllowAggType = filterByName([aggType], aggFilter).length !== 0; return doesSchemaAllowAggType; } ); diff --git a/src/legacy/core_plugins/vis_type_table/public/table_vis_controller.test.ts b/src/legacy/core_plugins/vis_type_table/public/table_vis_controller.test.ts index 736152c7014dc..6d4e94c6292a6 100644 --- a/src/legacy/core_plugins/vis_type_table/public/table_vis_controller.test.ts +++ b/src/legacy/core_plugins/vis_type_table/public/table_vis_controller.test.ts @@ -113,24 +113,20 @@ describe('Table Vis - Controller', () => { return ({ type: tableVisTypeDefinition, params: Object.assign({}, tableVisTypeDefinition.visConfig.defaults, params), - aggs: createAggConfigs( - stubIndexPattern, - [ - { type: 'count', schema: 'metric' }, - { - type: 'range', - schema: 'bucket', - params: { - field: 'bytes', - ranges: [ - { from: 0, to: 1000 }, - { from: 1000, to: 2000 }, - ], - }, + aggs: createAggConfigs(stubIndexPattern, [ + { type: 'count', schema: 'metric' }, + { + type: 'range', + schema: 'bucket', + params: { + field: 'bytes', + ranges: [ + { from: 0, to: 1000 }, + { from: 1000, to: 2000 }, + ], }, - ], - tableVisTypeDefinition.editorConfig.schemas.all - ), + }, + ]), } as unknown) as Vis; } diff --git a/src/legacy/core_plugins/vis_type_vislib/public/components/options/gauge/style_panel.tsx b/src/legacy/core_plugins/vis_type_vislib/public/components/options/gauge/style_panel.tsx index 9ed270257c559..4c936c93a4c8a 100644 --- a/src/legacy/core_plugins/vis_type_vislib/public/components/options/gauge/style_panel.tsx +++ b/src/legacy/core_plugins/vis_type_vislib/public/components/options/gauge/style_panel.tsx @@ -28,8 +28,7 @@ import { AggGroupNames } from '../../../legacy_imports'; function StylePanel({ aggs, setGaugeValue, stateParams, vis }: GaugeOptionsInternalProps) { const diasableAlignment = - aggs.bySchemaGroup(AggGroupNames.Metrics).length === 1 && - !aggs.bySchemaGroup(AggGroupNames.Buckets); + aggs.byType(AggGroupNames.Metrics).length === 1 && !aggs.byType(AggGroupNames.Buckets); return ( diff --git a/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/build_pipeline.test.ts b/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/build_pipeline.test.ts index afa638cdc5bf0..33b2da75b547e 100644 --- a/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/build_pipeline.test.ts +++ b/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/build_pipeline.test.ts @@ -383,9 +383,7 @@ describe('visualize loader pipeline helpers: build pipeline', () => { type: 'metrics', name: 'count', }, - schema: { - name: 'metric', - }, + schema: 'metric', params: {}, } as IAggConfig, ]; diff --git a/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/build_pipeline.ts b/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/build_pipeline.ts index 1339e1f2fdfe8..069b5814908a8 100644 --- a/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/build_pipeline.ts +++ b/src/legacy/core_plugins/visualizations/public/np_ready/public/legacy/build_pipeline.ts @@ -142,10 +142,7 @@ const getSchemas = ( const metrics = responseAggs.filter((agg: IAggConfig) => agg.type.type === 'metrics'); responseAggs.forEach((agg: IAggConfig) => { let skipMetrics = false; - let schemaName = agg.schema ? agg.schema.name || agg.schema : null; - if (typeof schemaName === 'object') { - schemaName = null; - } + let schemaName = agg.schema; if (!schemaName) { if (agg.type.name === 'geo_centroid') { schemaName = 'geo_centroid'; @@ -155,7 +152,7 @@ const getSchemas = ( } } if (schemaName === 'split') { - schemaName = `split_${agg.params.row ? 'row' : 'column'}`; + schemaName = `split_${vis.params.row ? 'row' : 'column'}`; skipMetrics = responseAggs.length - metrics.length > 1; } if (!schemas[schemaName]) { diff --git a/src/legacy/core_plugins/visualizations/public/np_ready/public/vis_impl.d.ts b/src/legacy/core_plugins/visualizations/public/np_ready/public/vis_impl.d.ts index 62b68082e21f8..0c4ea1572c4cd 100644 --- a/src/legacy/core_plugins/visualizations/public/np_ready/public/vis_impl.d.ts +++ b/src/legacy/core_plugins/visualizations/public/np_ready/public/vis_impl.d.ts @@ -20,6 +20,8 @@ import { Vis, VisState, VisParams } from './vis'; import { VisType } from './vis_types'; import { IIndexPattern } from '../../../../../../plugins/data/common'; +import { Schema } from '../../../../vis_default_editor/public'; +import { IAggConfig } from '../../../../data/public/search/aggs'; type InitVisStateType = | Partial @@ -44,6 +46,8 @@ export declare class VisImpl implements Vis { aggs: Array<{ [key: string]: any }>; }; + private initializeDefaultsFromSchemas(configStates: IAggConfig[], schemas: Schema[]); + // Since we haven't typed everything here yet, we basically "any" the rest // of that interface. This should be removed as soon as this type definition // has been completed. But that way we at least have typing for a couple of diff --git a/src/legacy/core_plugins/visualizations/public/np_ready/public/vis_impl.js b/src/legacy/core_plugins/visualizations/public/np_ready/public/vis_impl.js index d5e6412b6bdab..abd8f351ae94d 100644 --- a/src/legacy/core_plugins/visualizations/public/np_ready/public/vis_impl.js +++ b/src/legacy/core_plugins/visualizations/public/np_ready/public/vis_impl.js @@ -61,6 +61,23 @@ class VisImpl extends EventEmitter { }; } + initializeDefaultsFromSchemas(configStates, schemas) { + // Set the defaults for any schema which has them. If the defaults + // for some reason has more then the max only set the max number + // of defaults (not sure why a someone define more... + // but whatever). Also if a schema.name is already set then don't + // set anything. + const newConfigs = [...configStates]; + schemas + .filter(schema => Array.isArray(schema.defaults) && schema.defaults.length > 0) + .filter(schema => !configStates.find(agg => agg.schema && agg.schema === schema.name)) + .forEach(schema => { + const defaults = schema.defaults.slice(0, schema.max); + defaults.forEach(d => newConfigs.push(d)); + }); + return newConfigs; + } + setCurrentState(state) { this.title = state.title || ''; const type = state.type || this.type; @@ -82,11 +99,9 @@ class VisImpl extends EventEmitter { updateVisualizationConfig(state.params, this.params); if (state.aggs || !this.aggs) { - this.aggs = getAggs().createAggConfigs( - this.indexPattern, - state.aggs ? state.aggs.aggs || state.aggs : [], - this.type.schemas.all - ); + let configStates = state.aggs ? state.aggs.aggs || state.aggs : []; + configStates = this.initializeDefaultsFromSchemas(configStates, this.type.schemas.all || []); + this.aggs = getAggs().createAggConfigs(this.indexPattern, configStates); } } diff --git a/src/legacy/ui/public/agg_types/index.ts b/src/legacy/ui/public/agg_types/index.ts index 9773b11086b78..db64bd025b8cb 100644 --- a/src/legacy/ui/public/agg_types/index.ts +++ b/src/legacy/ui/public/agg_types/index.ts @@ -52,7 +52,6 @@ export { BUCKET_TYPES, DateRangeKey, IpRangeKey, - ISchemas, METRIC_TYPES, OptionedParamEditorProps, OptionedValueProp, @@ -78,8 +77,8 @@ export { OptionedParamType, parentPipelineType, propFilter, - Schema, - Schemas, siblingPipelineType, termsAggFilter, } from '../../../core_plugins/data/public'; + +export { ISchemas, Schemas, Schema } from '../../../core_plugins/vis_default_editor/public/schemas'; diff --git a/x-pack/plugins/translations/translations/ja-JP.json b/x-pack/plugins/translations/translations/ja-JP.json index d80fa4c80475c..252395f961ce0 100644 --- a/x-pack/plugins/translations/translations/ja-JP.json +++ b/x-pack/plugins/translations/translations/ja-JP.json @@ -299,7 +299,6 @@ "data.search.aggs.metrics.averageBucketTitle": "平均バケット", "data.search.aggs.metrics.averageLabel": "平均 {field}", "data.search.aggs.metrics.averageTitle": "平均", - "data.search.aggs.metrics.bucketAggTitle": "バケット集約", "data.search.aggs.metrics.countLabel": "カウント", "data.search.aggs.metrics.countTitle": "カウント", "data.search.aggs.metrics.cumulativeSumLabel": "累積合計", @@ -316,7 +315,6 @@ "data.search.aggs.metrics.medianLabel": "中央 {field}", "data.search.aggs.metrics.medianTitle": "中央", "data.search.aggs.metrics.metricAggregationsSubtypeTitle": "メトリック集約", - "data.search.aggs.metrics.metricAggTitle": "メトリック集約", "data.search.aggs.metrics.minBucketTitle": "最低バケット", "data.search.aggs.metrics.minLabel": "最低 {field}", "data.search.aggs.metrics.minTitle": "最低", diff --git a/x-pack/plugins/translations/translations/zh-CN.json b/x-pack/plugins/translations/translations/zh-CN.json index 5ca361a89f9b0..cc5ae17b92a33 100644 --- a/x-pack/plugins/translations/translations/zh-CN.json +++ b/x-pack/plugins/translations/translations/zh-CN.json @@ -299,7 +299,6 @@ "data.search.aggs.metrics.averageBucketTitle": "平均存储桶", "data.search.aggs.metrics.averageLabel": "{field}平均值", "data.search.aggs.metrics.averageTitle": "平均值", - "data.search.aggs.metrics.bucketAggTitle": "存储桶聚合", "data.search.aggs.metrics.countLabel": "计数", "data.search.aggs.metrics.countTitle": "计数", "data.search.aggs.metrics.cumulativeSumLabel": "累计和", @@ -316,7 +315,6 @@ "data.search.aggs.metrics.medianLabel": "{field}中值", "data.search.aggs.metrics.medianTitle": "中值", "data.search.aggs.metrics.metricAggregationsSubtypeTitle": "指标聚合", - "data.search.aggs.metrics.metricAggTitle": "指标聚合", "data.search.aggs.metrics.minBucketTitle": "最小存储桶", "data.search.aggs.metrics.minLabel": "{field}最小值", "data.search.aggs.metrics.minTitle": "最小值",