Skip to content

Commit

Permalink
[data.search.aggs] Remove service getters from agg types (#61069) (#6…
Browse files Browse the repository at this point in the history
…1582)

* [data.search.aggs] Remove service getters from agg types

Part of #60333

* fix JEST

* fix karma:unit

* fix PR commnets

* fix PR comments

* try to fix ci

* fix CI

* fix karma:unit

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

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
alexwizp and elasticmachine authored Mar 27, 2020
1 parent 0ca9174 commit 4941c7e
Show file tree
Hide file tree
Showing 22 changed files with 702 additions and 616 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ class ReactVisController {

const I18nContext = getI18n().Context;

return new Promise(resolve => {
return new Promise((resolve, reject) => {
if (!this.vis.type || !this.vis.type.visConfig || !this.vis.type.visConfig.component) {
reject('Missing component for ReactVisType');
}

const Component = this.vis.type.visConfig.component;
const config = getUISettings();
render(
Expand Down
146 changes: 74 additions & 72 deletions src/legacy/ui/public/new_platform/new_platform.karma_mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,18 @@ const mockComponent = () => {
return null;
};

let refreshInterval = undefined;
let isTimeRangeSelectorEnabled = true;
let isAutoRefreshSelectorEnabled = true;

export const mockUiSettings = {
get: item => {
return mockUiSettings[item];
},
getUpdate$: () => ({
subscribe: sinon.fake(),
}),
isDefault: sinon.fake(),
'query:allowLeadingWildcards': true,
'query:queryString:options': {},
'courier:ignoreFilterIfFieldNotInIndex': true,
Expand All @@ -81,10 +86,77 @@ const mockCore = {
},
};

const querySetup = {
state$: mockObservable(),
filterManager: {
getFetches$: sinon.fake(),
getFilters: sinon.fake(),
getAppFilters: sinon.fake(),
getGlobalFilters: sinon.fake(),
removeFilter: sinon.fake(),
addFilters: sinon.fake(),
setFilters: sinon.fake(),
removeAll: sinon.fake(),
getUpdates$: mockObservable,
},
timefilter: {
timefilter: {
getFetch$: mockObservable,
getAutoRefreshFetch$: mockObservable,
getEnabledUpdated$: mockObservable,
getTimeUpdate$: mockObservable,
getRefreshIntervalUpdate$: mockObservable,
isTimeRangeSelectorEnabled: () => {
return isTimeRangeSelectorEnabled;
},
isAutoRefreshSelectorEnabled: () => {
return isAutoRefreshSelectorEnabled;
},
disableAutoRefreshSelector: () => {
isAutoRefreshSelectorEnabled = false;
},
enableAutoRefreshSelector: () => {
isAutoRefreshSelectorEnabled = true;
},
getRefreshInterval: () => {
return refreshInterval;
},
setRefreshInterval: interval => {
refreshInterval = interval;
},
enableTimeRangeSelector: () => {
isTimeRangeSelectorEnabled = true;
},
disableTimeRangeSelector: () => {
isTimeRangeSelectorEnabled = false;
},
getTime: sinon.fake(),
setTime: sinon.fake(),
getActiveBounds: sinon.fake(),
getBounds: sinon.fake(),
calculateBounds: sinon.fake(),
createFilter: sinon.fake(),
},
history: sinon.fake(),
},
savedQueries: {
saveQuery: sinon.fake(),
getAllSavedQueries: sinon.fake(),
findSavedQueries: sinon.fake(),
getSavedQuery: sinon.fake(),
deleteSavedQuery: sinon.fake(),
getSavedQueryCount: sinon.fake(),
},
};

const mockAggTypesRegistry = () => {
const registry = new AggTypesRegistry();
const registrySetup = registry.setup();
const aggTypes = getAggTypes({ uiSettings: mockCore.uiSettings });
const aggTypes = getAggTypes({
uiSettings: mockCore.uiSettings,
notifications: mockCore.notifications,
query: querySetup,
});
aggTypes.buckets.forEach(type => registrySetup.registerBucket(type));
aggTypes.metrics.forEach(type => registrySetup.registerMetric(type));

Expand All @@ -93,10 +165,6 @@ const mockAggTypesRegistry = () => {

const aggTypesRegistry = mockAggTypesRegistry();

let refreshInterval = undefined;
let isTimeRangeSelectorEnabled = true;
let isAutoRefreshSelectorEnabled = true;

export const npSetup = {
core: mockCore,
plugins: {
Expand Down Expand Up @@ -135,72 +203,7 @@ export const npSetup = {
addProvider: sinon.fake(),
getProvider: sinon.fake(),
},
query: {
state$: mockObservable(),
filterManager: {
getFetches$: sinon.fake(),
getFilters: sinon.fake(),
getAppFilters: sinon.fake(),
getGlobalFilters: sinon.fake(),
removeFilter: sinon.fake(),
addFilters: sinon.fake(),
setFilters: sinon.fake(),
removeAll: sinon.fake(),
getUpdates$: mockObservable,
},
timefilter: {
timefilter: {
getTime: sinon.fake(),
getRefreshInterval: sinon.fake(),
getTimeUpdate$: mockObservable,
getRefreshIntervalUpdate$: mockObservable,
getFetch$: mockObservable,
getAutoRefreshFetch$: mockObservable,
getEnabledUpdated$: mockObservable,
getTimeUpdate$: mockObservable,
getRefreshIntervalUpdate$: mockObservable,
isTimeRangeSelectorEnabled: () => {
return isTimeRangeSelectorEnabled;
},
isAutoRefreshSelectorEnabled: () => {
return isAutoRefreshSelectorEnabled;
},
disableAutoRefreshSelector: () => {
isAutoRefreshSelectorEnabled = false;
},
enableAutoRefreshSelector: () => {
isAutoRefreshSelectorEnabled = true;
},
getRefreshInterval: () => {
return refreshInterval;
},
setRefreshInterval: interval => {
refreshInterval = interval;
},
enableTimeRangeSelector: () => {
isTimeRangeSelectorEnabled = true;
},
disableTimeRangeSelector: () => {
isTimeRangeSelectorEnabled = false;
},
getTime: sinon.fake(),
setTime: sinon.fake(),
getActiveBounds: sinon.fake(),
getBounds: sinon.fake(),
calculateBounds: sinon.fake(),
createFilter: sinon.fake(),
},
history: sinon.fake(),
},
savedQueries: {
saveQuery: sinon.fake(),
getAllSavedQueries: sinon.fake(),
findSavedQueries: sinon.fake(),
getSavedQuery: sinon.fake(),
deleteSavedQuery: sinon.fake(),
getSavedQueryCount: sinon.fake(),
},
},
query: querySetup,
search: {
aggs: {
calculateAutoTimeExpression: sinon.fake(),
Expand Down Expand Up @@ -410,7 +413,6 @@ export const npStart = {
search: {
aggs: {
calculateAutoTimeExpression: sinon.fake(),
createAggConfigs: sinon.fake(),
createAggConfigs: (indexPattern, configStates = []) => {
return new AggConfigs(indexPattern, configStates, {
typesRegistry: aggTypesRegistry.start(),
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ export class DataPublicPlugin implements Plugin<DataPublicPluginSetup, DataPubli

return {
autocomplete: this.autocomplete.setup(core),
search: this.searchService.setup(core, this.packageInfo),
search: this.searchService.setup(core, {
packageInfo: this.packageInfo,
query: queryService,
}),
fieldFormats: this.fieldFormatsService.setup(core),
query: queryService,
};
Expand Down
92 changes: 48 additions & 44 deletions src/plugins/data/public/search/aggs/agg_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* under the License.
*/

import { IUiSettingsClient } from 'src/core/public';
import { IUiSettingsClient, NotificationsSetup } from 'src/core/public';
import { QuerySetup } from '../../query/query_service';

import { countMetricAgg } from './metrics/count';
import { avgMetricAgg } from './metrics/avg';
Expand All @@ -36,10 +37,10 @@ import { derivativeMetricAgg } from './metrics/derivative';
import { cumulativeSumMetricAgg } from './metrics/cumulative_sum';
import { movingAvgMetricAgg } from './metrics/moving_avg';
import { serialDiffMetricAgg } from './metrics/serial_diff';
import { dateHistogramBucketAgg } from './buckets/date_histogram';
import { histogramBucketAgg } from './buckets/histogram';
import { getDateHistogramBucketAgg } from './buckets/date_histogram';
import { getHistogramBucketAgg } from './buckets/histogram';
import { rangeBucketAgg } from './buckets/range';
import { dateRangeBucketAgg } from './buckets/date_range';
import { getDateRangeBucketAgg } from './buckets/date_range';
import { ipRangeBucketAgg } from './buckets/ip_range';
import { termsBucketAgg } from './buckets/terms';
import { filterBucketAgg } from './buckets/filter';
Expand All @@ -52,44 +53,47 @@ import { bucketAvgMetricAgg } from './metrics/bucket_avg';
import { bucketMinMetricAgg } from './metrics/bucket_min';
import { bucketMaxMetricAgg } from './metrics/bucket_max';

export function getAggTypes(deps: { uiSettings: IUiSettingsClient }) {
const { uiSettings } = deps;
return {
metrics: [
countMetricAgg,
avgMetricAgg,
sumMetricAgg,
medianMetricAgg,
minMetricAgg,
maxMetricAgg,
stdDeviationMetricAgg,
cardinalityMetricAgg,
percentilesMetricAgg,
percentileRanksMetricAgg,
topHitMetricAgg,
derivativeMetricAgg,
cumulativeSumMetricAgg,
movingAvgMetricAgg,
serialDiffMetricAgg,
bucketAvgMetricAgg,
bucketSumMetricAgg,
bucketMinMetricAgg,
bucketMaxMetricAgg,
geoBoundsMetricAgg,
geoCentroidMetricAgg,
],
buckets: [
dateHistogramBucketAgg,
histogramBucketAgg,
rangeBucketAgg,
dateRangeBucketAgg,
ipRangeBucketAgg,
termsBucketAgg,
filterBucketAgg,
getFiltersBucketAgg({ uiSettings }),
significantTermsBucketAgg,
geoHashBucketAgg,
geoTileBucketAgg,
],
};
export interface AggTypesDependencies {
notifications: NotificationsSetup;
uiSettings: IUiSettingsClient;
query: QuerySetup;
}

export const getAggTypes = ({ notifications, uiSettings, query }: AggTypesDependencies) => ({
metrics: [
countMetricAgg,
avgMetricAgg,
sumMetricAgg,
medianMetricAgg,
minMetricAgg,
maxMetricAgg,
stdDeviationMetricAgg,
cardinalityMetricAgg,
percentilesMetricAgg,
percentileRanksMetricAgg,
topHitMetricAgg,
derivativeMetricAgg,
cumulativeSumMetricAgg,
movingAvgMetricAgg,
serialDiffMetricAgg,
bucketAvgMetricAgg,
bucketSumMetricAgg,
bucketMinMetricAgg,
bucketMaxMetricAgg,
geoBoundsMetricAgg,
geoCentroidMetricAgg,
],
buckets: [
getDateHistogramBucketAgg({ uiSettings, query }),
getHistogramBucketAgg({ uiSettings, notifications }),
rangeBucketAgg,
getDateRangeBucketAgg({ uiSettings }),
ipRangeBucketAgg,
termsBucketAgg,
filterBucketAgg,
getFiltersBucketAgg({ uiSettings }),
significantTermsBucketAgg,
geoHashBucketAgg,
geoTileBucketAgg,
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,35 @@ import { createFilterDateHistogram } from './date_histogram';
import { intervalOptions } from '../_interval_options';
import { AggConfigs } from '../../agg_configs';
import { mockDataServices, mockAggTypesRegistry } from '../../test_helpers';
import { dateHistogramBucketAgg, IBucketDateHistogramAggConfig } from '../date_histogram';
import {
getDateHistogramBucketAgg,
DateHistogramBucketAggDependencies,
IBucketDateHistogramAggConfig,
} from '../date_histogram';
import { BUCKET_TYPES } from '../bucket_agg_types';
import { RangeFilter } from '../../../../../common';
import { coreMock } from '../../../../../../../core/public/mocks';
import { queryServiceMock } from '../../../../query/mocks';

describe('AggConfig Filters', () => {
describe('date_histogram', () => {
beforeEach(() => {
mockDataServices();
});

const typesRegistry = mockAggTypesRegistry([dateHistogramBucketAgg]);

let aggTypesDependencies: DateHistogramBucketAggDependencies;
let agg: IBucketDateHistogramAggConfig;
let filter: RangeFilter;
let bucketStart: any;
let field: any;

beforeEach(() => {
const { uiSettings } = coreMock.createSetup();

aggTypesDependencies = {
uiSettings,
query: queryServiceMock.createSetupContract(),
};

mockDataServices();
});

const init = (interval: string = 'auto', duration: any = moment.duration(15, 'minutes')) => {
field = {
name: 'date',
Expand All @@ -61,7 +73,7 @@ describe('AggConfig Filters', () => {
params: { field: field.name, interval, customInterval: '5d' },
},
],
{ typesRegistry }
{ typesRegistry: mockAggTypesRegistry([getDateHistogramBucketAgg(aggTypesDependencies)]) }
);
const bucketKey = 1422579600000;

Expand Down
Loading

0 comments on commit 4941c7e

Please sign in to comment.