Skip to content

Commit

Permalink
[data.search.aggs] Remove service getters from agg types
Browse files Browse the repository at this point in the history
Part of #60333
  • Loading branch information
alexwizp committed Mar 27, 2020
1 parent 91c437d commit c193b74
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 12 deletions.
5 changes: 4 additions & 1 deletion src/legacy/ui/public/new_platform/new_platform.karma_mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,11 @@ const mockAggTypesRegistry = () => {
const registrySetup = registry.setup();
const aggTypes = getAggTypes({
uiSettings: mockCoreSetup.uiSettings,
notifications: mockCoreStart.notifications,
notifications: mockCoreSetup.notifications,
query: querySetup,
getInternalStartServices: () => ({
fieldFormats: getFieldFormatsRegistry(mockCoreStart),
}),
});
aggTypes.buckets.forEach(type => registrySetup.registerBucket(type));
aggTypes.metrics.forEach(type => registrySetup.registerMetric(type));
Expand Down
43 changes: 43 additions & 0 deletions src/plugins/data/public/field_formats/mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { FieldFormatsStart, FieldFormatsSetup, FieldFormatsService } from '.';
import { fieldFormatsMock } from '../../common/field_formats/mocks';

type FieldFormatsServiceClientContract = PublicMethodsOf<FieldFormatsService>;

const createSetupContractMock = () => fieldFormatsMock as FieldFormatsSetup;
const createStartContractMock = () => fieldFormatsMock as FieldFormatsStart;

const createMock = () => {
const mocked: jest.Mocked<FieldFormatsServiceClientContract> = {
setup: jest.fn(),
start: jest.fn(),
};

mocked.setup.mockReturnValue(createSetupContractMock());
mocked.start.mockReturnValue(createStartContractMock());
return mocked;
};

export const fieldFormatsServiceMock = {
create: createMock,
createSetupContract: createSetupContractMock,
createStartContract: createStartContractMock,
};
8 changes: 4 additions & 4 deletions src/plugins/data/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* under the License.
*/

import { Plugin, DataPublicPluginSetup, DataPublicPluginStart, IndexPatternsContract } from '.';
import { fieldFormatsMock } from '../common/field_formats/mocks';
import { Plugin, IndexPatternsContract } from '.';
import { fieldFormatsServiceMock } from './field_formats/mocks';
import { searchSetupMock, searchStartMock } from './search/mocks';
import { queryServiceMock } from './query/mocks';

Expand All @@ -36,7 +36,7 @@ const createSetupContract = (): Setup => {
return {
autocomplete: autocompleteMock,
search: searchSetupMock,
fieldFormats: fieldFormatsMock as DataPublicPluginSetup['fieldFormats'],
fieldFormats: fieldFormatsServiceMock.createSetupContract(),
query: querySetupMock,
};
};
Expand All @@ -49,7 +49,7 @@ const createStartContract = (): Start => {
},
autocomplete: autocompleteMock,
search: searchStartMock,
fieldFormats: fieldFormatsMock as DataPublicPluginStart['fieldFormats'],
fieldFormats: fieldFormatsServiceMock.createStartContract(),
query: queryStartMock,
ui: {
IndexPatternSelect: jest.fn(),
Expand Down
7 changes: 7 additions & 0 deletions src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
DataPublicPluginStart,
DataSetupDependencies,
DataStartDependencies,
getInternalStartServicesFn,
} from './types';
import { AutocompleteService } from './autocomplete';
import { SearchService } from './search/search_service';
Expand All @@ -47,6 +48,7 @@ import {
setQueryService,
setSearchService,
setUiSettings,
getFieldFormats,
} from './services';
import { createSearchBar } from './ui/search_bar/create_search_bar';
import { esaggs } from './search/expressions';
Expand Down Expand Up @@ -100,6 +102,10 @@ export class DataPublicPlugin implements Plugin<DataPublicPluginSetup, DataPubli

expressions.registerFunction(esaggs);

const getInternalStartServices: getInternalStartServicesFn = () => ({
fieldFormats: getFieldFormats(),
});

const queryService = this.queryService.setup({
uiSettings: core.uiSettings,
storage: this.storage,
Expand All @@ -122,6 +128,7 @@ export class DataPublicPlugin implements Plugin<DataPublicPluginSetup, DataPubli
return {
autocomplete: this.autocomplete.setup(core),
search: this.searchService.setup(core, {
getInternalStartServices,
packageInfo: this.packageInfo,
query: queryService,
}),
Expand Down
12 changes: 10 additions & 2 deletions src/plugins/data/public/search/aggs/agg_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,21 @@ import { bucketAvgMetricAgg } from './metrics/bucket_avg';
import { bucketMinMetricAgg } from './metrics/bucket_min';
import { bucketMaxMetricAgg } from './metrics/bucket_max';

import { getInternalStartServicesFn } from '../../types';

export interface AggTypesDependencies {
notifications: NotificationsSetup;
uiSettings: IUiSettingsClient;
query: QuerySetup;
getInternalStartServices: getInternalStartServicesFn;
}

export const getAggTypes = ({ notifications, uiSettings, query }: AggTypesDependencies) => ({
export const getAggTypes = ({
notifications,
uiSettings,
query,
getInternalStartServices,
}: AggTypesDependencies) => ({
metrics: [
countMetricAgg,
avgMetricAgg,
Expand Down Expand Up @@ -87,7 +95,7 @@ export const getAggTypes = ({ notifications, uiSettings, query }: AggTypesDepend
getDateHistogramBucketAgg({ uiSettings, query }),
getHistogramBucketAgg({ uiSettings, notifications }),
rangeBucketAgg,
getDateRangeBucketAgg({ uiSettings }),
getDateRangeBucketAgg({ uiSettings, getInternalStartServices }),
ipRangeBucketAgg,
termsBucketAgg,
filterBucketAgg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { mockAggTypesRegistry } from '../../test_helpers';
import { BUCKET_TYPES } from '../bucket_agg_types';
import { IBucketAggConfig } from '../_bucket_agg_type';
import { coreMock } from '../../../../../../../core/public/mocks';
import { fieldFormatsServiceMock } from '../../../../field_formats/mocks';

describe('AggConfig Filters', () => {
describe('Date range', () => {
Expand All @@ -37,6 +38,9 @@ describe('AggConfig Filters', () => {

aggTypesDependencies = {
uiSettings,
getInternalStartServices: () => ({
fieldFormats: fieldFormatsServiceMock.createStartContract(),
}),
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { getDateRangeBucketAgg, DateRangeBucketAggDependencies } from './date_ra
import { AggConfigs } from '../agg_configs';
import { mockAggTypesRegistry } from '../test_helpers';
import { BUCKET_TYPES } from './bucket_agg_types';
import { fieldFormatsServiceMock } from '../../../field_formats/mocks';

describe('date_range params', () => {
let aggTypesDependencies: DateRangeBucketAggDependencies;
Expand All @@ -31,6 +32,9 @@ describe('date_range params', () => {

aggTypesDependencies = {
uiSettings,
getInternalStartServices: () => ({
fieldFormats: fieldFormatsServiceMock.createStartContract(),
}),
};
});

Expand Down
12 changes: 8 additions & 4 deletions src/plugins/data/public/search/aggs/buckets/date_range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ import { createFilterDateRange } from './create_filter/date_range';
import { convertDateRangeToString, DateRangeKey } from './lib/date_range';

import { KBN_FIELD_TYPES, FieldFormat, TEXT_CONTEXT_TYPE } from '../../../../common';
import { getFieldFormats } from '../../../../public/services';
import { getInternalStartServicesFn } from '../../../types';

const dateRangeTitle = i18n.translate('data.search.aggs.buckets.dateRangeTitle', {
defaultMessage: 'Date Range',
});

export interface DateRangeBucketAggDependencies {
uiSettings: IUiSettingsClient;
getInternalStartServices: getInternalStartServicesFn;
}

export const getDateRangeBucketAgg = ({ uiSettings }: DateRangeBucketAggDependencies) =>
export const getDateRangeBucketAgg = ({
uiSettings,
getInternalStartServices,
}: DateRangeBucketAggDependencies) =>
new BucketAggType({
name: BUCKET_TYPES.DATE_RANGE,
title: dateRangeTitle,
Expand All @@ -47,11 +51,11 @@ export const getDateRangeBucketAgg = ({ uiSettings }: DateRangeBucketAggDependen
return { from, to };
},
getFormat(agg) {
const fieldFormatsService = getFieldFormats();
const { fieldFormats } = getInternalStartServices();

const formatter = agg.fieldOwnFormatter(
TEXT_CONTEXT_TYPE,
fieldFormatsService.getDefaultInstance(KBN_FIELD_TYPES.DATE)
fieldFormats.getDefaultInstance(KBN_FIELD_TYPES.DATE)
);
const DateRangeFormat = FieldFormat.from(function(range: DateRangeKey) {
return convertDateRangeToString(range, formatter);
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/data/public/search/aggs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ import { getAggTypes } from './index';
import { isBucketAggType } from './buckets/_bucket_agg_type';
import { isMetricAggType } from './metrics/metric_agg_type';
import { QueryStart } from '../../query';
import { FieldFormatsStart } from '../../field_formats';

describe('AggTypesComponent', () => {
const core = coreMock.createSetup();
const aggTypes = getAggTypes({
uiSettings: core.uiSettings,
notifications: core.notifications,
query: {} as QueryStart,
getInternalStartServices: () => ({
fieldFormats: {} as FieldFormatsStart,
}),
});

const { buckets, metrics } = aggTypes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { getAggTypes } from '../agg_types';
import { BucketAggType } from '../buckets/_bucket_agg_type';
import { MetricAggType } from '../metrics/metric_agg_type';
import { queryServiceMock } from '../../../query/mocks';
import { fieldFormatsServiceMock } from '../../../field_formats/mocks';

/**
* Testing utility which creates a new instance of AggTypesRegistry,
Expand Down Expand Up @@ -57,6 +58,9 @@ export function mockAggTypesRegistry<T extends BucketAggType<any> | MetricAggTyp
uiSettings: core.uiSettings,
notifications: core.notifications,
query: queryServiceMock.createSetupContract(),
getInternalStartServices: () => ({
fieldFormats: fieldFormatsServiceMock.createStartContract(),
}),
});

aggTypes.buckets.forEach(type => registrySetup.registerBucket(type));
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/data/public/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { getEsClient, LegacyApiCaller } from './es_client';
import { ES_SEARCH_STRATEGY, DEFAULT_SEARCH_STRATEGY } from '../../common/search';
import { esSearchStrategyProvider } from './es_search/es_search_strategy';
import { QuerySetup } from '../query/query_service';
import { getInternalStartServicesFn } from '../types';
import { SearchInterceptor } from './search_interceptor';
import {
getAggTypes,
Expand All @@ -44,6 +45,7 @@ import {
interface SearchServiceSetupDependencies {
packageInfo: PackageInfo;
query: QuerySetup;
getInternalStartServices: getInternalStartServicesFn;
}

/**
Expand Down Expand Up @@ -81,7 +83,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {

public setup(
core: CoreSetup,
{ packageInfo, query }: SearchServiceSetupDependencies
{ packageInfo, query, getInternalStartServices }: SearchServiceSetupDependencies
): ISearchSetup {
this.esClient = getEsClient(core.injectedMetadata, core.http, packageInfo);
this.registerSearchStrategyProvider(SYNC_SEARCH_STRATEGY, syncSearchStrategyProvider);
Expand All @@ -92,6 +94,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
query,
uiSettings: core.uiSettings,
notifications: core.notifications,
getInternalStartServices,
});

aggTypes.buckets.forEach(b => aggTypesSetup.registerBucket(b));
Expand Down
8 changes: 8 additions & 0 deletions src/plugins/data/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ export interface IDataPluginServices extends Partial<CoreStart> {
storage: IStorageWrapper;
data: DataPublicPluginStart;
}

/** @internal **/
export interface InternalStartServices {
fieldFormats: FieldFormatsStart;
}

/** @internal **/
export type getInternalStartServicesFn = () => InternalStartServices;

0 comments on commit c193b74

Please sign in to comment.