Skip to content

Commit a83455c

Browse files
alexwizpelasticmachineVladLasitsa
authored
[data.search.aggs] Remove service getters from agg types (#61628) (#62762)
* [data.search.aggs] Remove service getters from agg types Part of #60333 * new portion of changes * pass dependencies to MetricAgg Type through constructor * update docs * refactoring buckets * remove unused mockDataServices * Remove service getters from metrics * Some fixes * remove temporary code * moved notifications to the getInternalStartServices * fixed karma lock * update docs * Fixed tests * fix broken CI * fix PR comment * fix typo Co-authored-by: Elastic Machine <[email protected]> Co-authored-by: Uladzislau Lasitsa <[email protected]> Co-authored-by: Elastic Machine <[email protected]> Co-authored-by: Uladzislau Lasitsa <[email protected]>
1 parent 2a15721 commit a83455c

File tree

85 files changed

+2596
-1862
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+2596
-1862
lines changed

docs/development/plugins/data/public/kibana-plugin-plugins-data-public.search.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ search: {
1919
intervalOptions: ({
2020
display: string;
2121
val: string;
22-
enabled(agg: import("./search/aggs/buckets/_bucket_agg_type").IBucketAggConfig): boolean | "" | undefined;
22+
enabled(agg: import("./search/aggs/buckets/bucket_agg_type").IBucketAggConfig): boolean | "" | undefined;
2323
} | {
2424
display: string;
2525
val: string;

src/legacy/ui/public/new_platform/new_platform.karma_mock.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ const mockCoreStart = {
8888
get: sinon.fake.returns(''),
8989
},
9090
},
91+
notifications: {
92+
toasts: {},
93+
},
9194
i18n: {},
9295
overlays: {},
9396
savedObjects: {
@@ -164,8 +167,11 @@ const mockAggTypesRegistry = () => {
164167
const registrySetup = registry.setup();
165168
const aggTypes = getAggTypes({
166169
uiSettings: mockCoreSetup.uiSettings,
167-
notifications: mockCoreStart.notifications,
168170
query: querySetup,
171+
getInternalStartServices: () => ({
172+
fieldFormats: getFieldFormatsRegistry(mockCoreStart),
173+
notifications: mockCoreStart.notifications,
174+
}),
169175
});
170176
aggTypes.buckets.forEach(type => registrySetup.registerBucket(type));
171177
aggTypes.metrics.forEach(type => registrySetup.registerMetric(type));

src/plugins/data/common/field_formats/mocks.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,14 @@
1717
* under the License.
1818
*/
1919

20-
import { FieldFormat, IFieldFormatsRegistry } from '.';
21-
22-
const fieldFormatMock = ({
23-
convert: jest.fn(),
24-
getConverterFor: jest.fn(),
25-
getParamDefaults: jest.fn(),
26-
param: jest.fn(),
27-
params: jest.fn(),
28-
toJSON: jest.fn(),
29-
type: jest.fn(),
30-
setupContentType: jest.fn(),
31-
} as unknown) as FieldFormat;
20+
import { IFieldFormatsRegistry } from '.';
3221

3322
export const fieldFormatsMock: IFieldFormatsRegistry = {
3423
getByFieldType: jest.fn(),
3524
getDefaultConfig: jest.fn(),
36-
getDefaultInstance: jest.fn().mockImplementation(() => fieldFormatMock) as any,
25+
getDefaultInstance: jest.fn().mockImplementation(() => ({
26+
getConverterFor: jest.fn().mockImplementation(() => (t: string) => t),
27+
})) as any,
3728
getDefaultInstanceCacheResolver: jest.fn(),
3829
getDefaultInstancePlain: jest.fn(),
3930
getDefaultType: jest.fn(),
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { FieldFormatsStart, FieldFormatsSetup, FieldFormatsService } from '.';
21+
import { fieldFormatsMock } from '../../common/field_formats/mocks';
22+
23+
type FieldFormatsServiceClientContract = PublicMethodsOf<FieldFormatsService>;
24+
25+
const createSetupContractMock = () => fieldFormatsMock as FieldFormatsSetup;
26+
const createStartContractMock = () => fieldFormatsMock as FieldFormatsStart;
27+
28+
const createMock = () => {
29+
const mocked: jest.Mocked<FieldFormatsServiceClientContract> = {
30+
setup: jest.fn().mockReturnValue(createSetupContractMock()),
31+
start: jest.fn().mockReturnValue(createStartContractMock()),
32+
};
33+
34+
return mocked;
35+
};
36+
37+
export const fieldFormatsServiceMock = {
38+
create: createMock,
39+
createSetupContract: createSetupContractMock,
40+
createStartContract: createStartContractMock,
41+
};

src/plugins/data/public/mocks.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* under the License.
1818
*/
1919

20-
import { Plugin, DataPublicPluginSetup, DataPublicPluginStart, IndexPatternsContract } from '.';
21-
import { fieldFormatsMock } from '../common/field_formats/mocks';
20+
import { Plugin, IndexPatternsContract } from '.';
21+
import { fieldFormatsServiceMock } from './field_formats/mocks';
2222
import { searchSetupMock, searchStartMock } from './search/mocks';
2323
import { queryServiceMock } from './query/mocks';
2424

@@ -36,7 +36,7 @@ const createSetupContract = (): Setup => {
3636
return {
3737
autocomplete: autocompleteMock,
3838
search: searchSetupMock,
39-
fieldFormats: fieldFormatsMock as DataPublicPluginSetup['fieldFormats'],
39+
fieldFormats: fieldFormatsServiceMock.createSetupContract(),
4040
query: querySetupMock,
4141
};
4242
};
@@ -49,7 +49,7 @@ const createStartContract = (): Start => {
4949
},
5050
autocomplete: autocompleteMock,
5151
search: searchStartMock,
52-
fieldFormats: fieldFormatsMock as DataPublicPluginStart['fieldFormats'],
52+
fieldFormats: fieldFormatsServiceMock.createStartContract(),
5353
query: queryStartMock,
5454
ui: {
5555
IndexPatternSelect: jest.fn(),

src/plugins/data/public/plugin.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
DataPublicPluginStart,
3131
DataSetupDependencies,
3232
DataStartDependencies,
33+
GetInternalStartServicesFn,
3334
} from './types';
3435
import { AutocompleteService } from './autocomplete';
3536
import { SearchService } from './search/search_service';
@@ -47,6 +48,8 @@ import {
4748
setQueryService,
4849
setSearchService,
4950
setUiSettings,
51+
getFieldFormats,
52+
getNotifications,
5053
} from './services';
5154
import { createSearchBar } from './ui/search_bar/create_search_bar';
5255
import { esaggs } from './search/expressions';
@@ -100,6 +103,11 @@ export class DataPublicPlugin implements Plugin<DataPublicPluginSetup, DataPubli
100103

101104
expressions.registerFunction(esaggs);
102105

106+
const getInternalStartServices: GetInternalStartServicesFn = () => ({
107+
fieldFormats: getFieldFormats(),
108+
notifications: getNotifications(),
109+
});
110+
103111
const queryService = this.queryService.setup({
104112
uiSettings: core.uiSettings,
105113
storage: this.storage,
@@ -122,6 +130,7 @@ export class DataPublicPlugin implements Plugin<DataPublicPluginSetup, DataPubli
122130
return {
123131
autocomplete: this.autocomplete.setup(core),
124132
search: this.searchService.setup(core, {
133+
getInternalStartServices,
125134
packageInfo: this.packageInfo,
126135
query: queryService,
127136
}),

src/plugins/data/public/public.api.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import { Assign } from '@kbn/utility-types';
1212
import { Breadcrumb } from '@elastic/eui';
1313
import { Component } from 'react';
1414
import { CoreSetup } from 'src/core/public';
15-
import { CoreStart } from 'kibana/public';
16-
import { CoreStart as CoreStart_2 } from 'src/core/public';
15+
import { CoreStart } from 'src/core/public';
16+
import { CoreStart as CoreStart_2 } from 'kibana/public';
1717
import { EuiButtonEmptyProps } from '@elastic/eui';
1818
import { EuiComboBoxProps } from '@elastic/eui';
1919
import { EuiConfirmModalProps } from '@elastic/eui';
@@ -632,21 +632,21 @@ export type IAggType = AggType;
632632
// Warning: (ae-missing-release-tag) "IDataPluginServices" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
633633
//
634634
// @public (undocumented)
635-
export interface IDataPluginServices extends Partial<CoreStart_2> {
635+
export interface IDataPluginServices extends Partial<CoreStart> {
636636
// (undocumented)
637637
appName: string;
638638
// (undocumented)
639639
data: DataPublicPluginStart;
640640
// (undocumented)
641-
http: CoreStart_2['http'];
641+
http: CoreStart['http'];
642642
// (undocumented)
643-
notifications: CoreStart_2['notifications'];
643+
notifications: CoreStart['notifications'];
644644
// (undocumented)
645-
savedObjects: CoreStart_2['savedObjects'];
645+
savedObjects: CoreStart['savedObjects'];
646646
// (undocumented)
647647
storage: IStorageWrapper;
648648
// (undocumented)
649-
uiSettings: CoreStart_2['uiSettings'];
649+
uiSettings: CoreStart['uiSettings'];
650650
}
651651

652652
// Warning: (ae-missing-release-tag) "IEsSearchRequest" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
@@ -1104,7 +1104,7 @@ export type ISearch<T extends TStrategyTypes = typeof DEFAULT_SEARCH_STRATEGY> =
11041104
// @public (undocumented)
11051105
export interface ISearchContext {
11061106
// (undocumented)
1107-
core: CoreStart;
1107+
core: CoreStart_2;
11081108
// (undocumented)
11091109
getSearchStrategy: <T extends TStrategyTypes>(name: T) => TSearchStrategyProvider<T>;
11101110
}
@@ -1317,7 +1317,7 @@ export class Plugin implements Plugin_2<DataPublicPluginSetup, DataPublicPluginS
13171317
// Warning: (ae-forgotten-export) The symbol "DataStartDependencies" needs to be exported by the entry point index.d.ts
13181318
//
13191319
// (undocumented)
1320-
start(core: CoreStart_2, { uiActions }: DataStartDependencies): DataPublicPluginStart;
1320+
start(core: CoreStart, { uiActions }: DataStartDependencies): DataPublicPluginStart;
13211321
// (undocumented)
13221322
stop(): void;
13231323
}
@@ -1543,7 +1543,7 @@ export const search: {
15431543
intervalOptions: ({
15441544
display: string;
15451545
val: string;
1546-
enabled(agg: import("./search/aggs/buckets/_bucket_agg_type").IBucketAggConfig): boolean | "" | undefined;
1546+
enabled(agg: import("./search/aggs/buckets/bucket_agg_type").IBucketAggConfig): boolean | "" | undefined;
15471547
} | {
15481548
display: string;
15491549
val: string;

src/plugins/data/public/search/aggs/agg_config.test.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import { AggTypesRegistryStart } from './agg_types_registry';
2626
import { mockDataServices, mockAggTypesRegistry } from './test_helpers';
2727
import { Field as IndexPatternField, IndexPattern } from '../../index_patterns';
2828
import { stubIndexPatternWithFields } from '../../../public/stubs';
29-
import { dataPluginMock } from '../../../public/mocks';
30-
import { setFieldFormats } from '../../../public/services';
3129

3230
describe('AggConfig', () => {
3331
let indexPattern: IndexPattern;
@@ -400,13 +398,6 @@ describe('AggConfig', () => {
400398

401399
describe('#fieldFormatter - custom getFormat handler', () => {
402400
it('returns formatter from getFormat handler', () => {
403-
setFieldFormats({
404-
...dataPluginMock.createStartContract().fieldFormats,
405-
getDefaultInstance: jest.fn().mockImplementation(() => ({
406-
getConverterFor: jest.fn().mockImplementation(() => (t: string) => t),
407-
})) as any,
408-
});
409-
410401
const ac = new AggConfigs(indexPattern, [], { typesRegistry });
411402
const configStates = {
412403
enabled: true,
@@ -429,12 +420,6 @@ describe('AggConfig', () => {
429420
let aggConfig: AggConfig;
430421

431422
beforeEach(() => {
432-
setFieldFormats({
433-
...dataPluginMock.createStartContract().fieldFormats,
434-
getDefaultInstance: jest.fn().mockImplementation(() => ({
435-
getConverterFor: (t?: string) => t || identity,
436-
})) as any,
437-
});
438423
indexPattern.fields.getByName = name =>
439424
({
440425
format: {

src/plugins/data/public/search/aggs/agg_params.test.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,22 @@ import { BaseParamType } from './param_types/base';
2222
import { FieldParamType } from './param_types/field';
2323
import { OptionedParamType } from './param_types/optioned';
2424
import { AggParamType } from '../aggs/param_types/agg';
25+
import { fieldFormatsServiceMock } from '../../field_formats/mocks';
26+
import { notificationServiceMock } from '../../../../../../src/core/public/mocks';
27+
import { AggTypeDependencies } from './agg_type';
2528

2629
describe('AggParams class', () => {
30+
const aggTypesDependencies: AggTypeDependencies = {
31+
getInternalStartServices: () => ({
32+
fieldFormats: fieldFormatsServiceMock.createStartContract(),
33+
notifications: notificationServiceMock.createStartContract(),
34+
}),
35+
};
36+
2737
describe('constructor args', () => {
2838
it('accepts an array of param defs', () => {
2939
const params = [{ name: 'one' }, { name: 'two' }] as AggParamType[];
30-
const aggParams = initParams(params);
40+
const aggParams = initParams(params, aggTypesDependencies);
3141

3242
expect(aggParams).toHaveLength(params.length);
3343
expect(Array.isArray(aggParams)).toBeTruthy();
@@ -37,7 +47,7 @@ describe('AggParams class', () => {
3747
describe('AggParam creation', () => {
3848
it('Uses the FieldParamType class for params with the name "field"', () => {
3949
const params = [{ name: 'field', type: 'field' }] as AggParamType[];
40-
const aggParams = initParams(params);
50+
const aggParams = initParams(params, aggTypesDependencies);
4151

4252
expect(aggParams).toHaveLength(params.length);
4353
expect(aggParams[0] instanceof FieldParamType).toBeTruthy();
@@ -50,7 +60,7 @@ describe('AggParams class', () => {
5060
type: 'optioned',
5161
},
5262
] as AggParamType[];
53-
const aggParams = initParams(params);
63+
const aggParams = initParams(params, aggTypesDependencies);
5464

5565
expect(aggParams).toHaveLength(params.length);
5666
expect(aggParams[0] instanceof OptionedParamType).toBeTruthy();
@@ -72,7 +82,7 @@ describe('AggParams class', () => {
7282
},
7383
] as AggParamType[];
7484

75-
const aggParams = initParams(params);
85+
const aggParams = initParams(params, aggTypesDependencies);
7686

7787
expect(aggParams).toHaveLength(params.length);
7888

src/plugins/data/public/search/aggs/agg_params.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { BaseParamType } from './param_types/base';
2626

2727
import { AggConfig } from './agg_config';
2828
import { IAggConfigs } from './agg_configs';
29+
import { AggTypeDependencies } from './agg_type';
2930

3031
const paramTypeMap = {
3132
field: FieldParamType,
@@ -45,12 +46,13 @@ export interface AggParamOption {
4546
}
4647

4748
export const initParams = <TAggParam extends AggParamType = AggParamType>(
48-
params: TAggParam[]
49+
params: TAggParam[],
50+
{ getInternalStartServices }: AggTypeDependencies
4951
): TAggParam[] =>
5052
params.map((config: TAggParam) => {
5153
const Class = paramTypeMap[config.type] || paramTypeMap._default;
5254

53-
return new Class(config);
55+
return new Class(config, { getInternalStartServices });
5456
}) as TAggParam[];
5557

5658
/**

0 commit comments

Comments
 (0)