Skip to content

Commit

Permalink
Switch all usages of uiSettings to use getConfig.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeelmers committed Aug 18, 2020
1 parent 9a4b945 commit 8456568
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 107 deletions.
2 changes: 0 additions & 2 deletions src/legacy/ui/public/new_platform/set_services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ interface NpStart {
export function setSetupServices(npSetup: NpSetup) {
// Services that need to be set in the legacy platform since the legacy data plugin
// which previously provided them has been removed.
dataServices.setInjectedMetadata(npSetup.core.injectedMetadata);
visualizationsServices.setUISettings(npSetup.core.uiSettings);
visualizationsServices.setUsageCollector(npSetup.plugins.usageCollection);
}

export function setStartServices(npStart: NpStart) {
// Services that need to be set in the legacy platform since the legacy data plugin
// which previously provided them has been removed.
dataServices.setHttp(npStart.core.http);
dataServices.setNotifications(npStart.core.notifications);
dataServices.setOverlays(npStart.core.overlays);
dataServices.setUiSettings(npStart.core.uiSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
*/

import { EsQueryConfig } from './build_es_query';
import { UI_SETTINGS } from '../../';
import { GetConfigFn, UI_SETTINGS } from '../../';

interface KibanaConfig {
get<T>(key: string): T;
get: GetConfigFn;
}

export function getEsQueryConfig(config: KibanaConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
*/

import { getSearchParams } from './get_search_params';
import { IUiSettingsClient } from 'kibana/public';
import { UI_SETTINGS } from '../../../common';
import { GetConfigFn, UI_SETTINGS } from '../../../common';

function getConfigStub(config: any = {}) {
return {
get: (key) => config[key],
} as IUiSettingsClient;
function getConfigStub(config: any = {}): GetConfigFn {
return (key) => config[key];
}

describe('getSearchParams', () => {
Expand Down
31 changes: 15 additions & 16 deletions src/plugins/data/public/search/fetch/get_search_params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,36 @@
* under the License.
*/

import { IUiSettingsClient } from 'kibana/public';
import { UI_SETTINGS, ISearchRequestParams } from '../../../common';
import { UI_SETTINGS, ISearchRequestParams, GetConfigFn } from '../../../common';
import { SearchRequest } from './types';

const sessionId = Date.now();

export function getSearchParams(config: IUiSettingsClient, esShardTimeout: number = 0) {
export function getSearchParams(getConfig: GetConfigFn, esShardTimeout: number = 0) {
return {
rest_total_hits_as_int: true,
ignore_unavailable: true,
ignore_throttled: getIgnoreThrottled(config),
max_concurrent_shard_requests: getMaxConcurrentShardRequests(config),
preference: getPreference(config),
ignore_throttled: getIgnoreThrottled(getConfig),
max_concurrent_shard_requests: getMaxConcurrentShardRequests(getConfig),
preference: getPreference(getConfig),
timeout: getTimeout(esShardTimeout),
};
}

export function getIgnoreThrottled(config: IUiSettingsClient) {
return !config.get(UI_SETTINGS.SEARCH_INCLUDE_FROZEN);
export function getIgnoreThrottled(getConfig: GetConfigFn) {
return !getConfig(UI_SETTINGS.SEARCH_INCLUDE_FROZEN);
}

export function getMaxConcurrentShardRequests(config: IUiSettingsClient) {
const maxConcurrentShardRequests = config.get(UI_SETTINGS.COURIER_MAX_CONCURRENT_SHARD_REQUESTS);
export function getMaxConcurrentShardRequests(getConfig: GetConfigFn) {
const maxConcurrentShardRequests = getConfig(UI_SETTINGS.COURIER_MAX_CONCURRENT_SHARD_REQUESTS);
return maxConcurrentShardRequests > 0 ? maxConcurrentShardRequests : undefined;
}

export function getPreference(config: IUiSettingsClient) {
const setRequestPreference = config.get(UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE);
export function getPreference(getConfig: GetConfigFn) {
const setRequestPreference = getConfig(UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE);
if (setRequestPreference === 'sessionId') return sessionId;
return setRequestPreference === 'custom'
? config.get(UI_SETTINGS.COURIER_CUSTOM_REQUEST_PREFERENCE)
? getConfig(UI_SETTINGS.COURIER_CUSTOM_REQUEST_PREFERENCE)
: undefined;
}

Expand All @@ -60,10 +59,10 @@ export function getTimeout(esShardTimeout: number) {
// already wired up.
export function getSearchParamsFromRequest(
searchRequest: SearchRequest,
dependencies: { esShardTimeout: number; uiSettings: IUiSettingsClient }
dependencies: { esShardTimeout: number; getConfig: GetConfigFn }
): ISearchRequestParams {
const { esShardTimeout, uiSettings } = dependencies;
const searchParams = getSearchParams(uiSettings, esShardTimeout);
const { esShardTimeout, getConfig } = dependencies;
const searchParams = getSearchParams(getConfig, esShardTimeout);

return {
index: searchRequest.index.title || searchRequest.index,
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/public/search/fetch/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { IUiSettingsClient } from '../../../../../core/public';
import { GetConfigFn } from '../../../common';
import { ISearchStartLegacy } from '../types';

export type SearchRequest = any;
Expand All @@ -30,7 +30,7 @@ export interface FetchOptions {

export interface FetchHandlers {
legacySearchService: ISearchStartLegacy;
config: IUiSettingsClient;
config: { get: GetConfigFn };
esShardTimeout: number;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function msearch({
index: index.title || index,
search_type: searchType,
ignore_unavailable: true,
preference: getPreference(config),
preference: getPreference(config.get),
};
const inlineBody = {
...body,
Expand All @@ -52,7 +52,7 @@ function msearch({
});

const searching = es.msearch({
...getMSearchParams(config),
...getMSearchParams(config.get),
body: `${inlineRequests.join('\n')}\n`,
});

Expand Down
39 changes: 18 additions & 21 deletions src/plugins/data/public/search/legacy/fetch_soon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@

import { fetchSoon } from './fetch_soon';
import { callClient } from './call_client';
import { IUiSettingsClient } from 'kibana/public';
import { FetchHandlers, FetchOptions } from '../fetch/types';
import { SearchRequest, SearchResponse } from '../index';
import { UI_SETTINGS } from '../../../common';
import { GetConfigFn, UI_SETTINGS } from '../../../common';

function getConfigStub(config: any = {}) {
return {
get: (key) => config[key],
} as IUiSettingsClient;
function getConfigStub(config: any = {}): GetConfigFn {
return (key) => config[key];
}

const mockResponses: Record<string, SearchResponse> = {
Expand Down Expand Up @@ -60,9 +57,9 @@ describe('fetchSoon', () => {
});

test('should execute asap if config is set to not batch searches', () => {
const config = getConfigStub({
[UI_SETTINGS.COURIER_BATCH_SEARCHES]: false,
});
const config = {
get: getConfigStub({ [UI_SETTINGS.COURIER_BATCH_SEARCHES]: false }),
};
const request = {};
const options = {};

Expand All @@ -72,9 +69,9 @@ describe('fetchSoon', () => {
});

test('should delay by 50ms if config is set to batch searches', () => {
const config = getConfigStub({
[UI_SETTINGS.COURIER_BATCH_SEARCHES]: true,
});
const config = {
get: getConfigStub({ [UI_SETTINGS.COURIER_BATCH_SEARCHES]: true }),
};
const request = {};
const options = {};

Expand All @@ -88,9 +85,9 @@ describe('fetchSoon', () => {
});

test('should send a batch of requests to callClient', () => {
const config = getConfigStub({
[UI_SETTINGS.COURIER_BATCH_SEARCHES]: true,
});
const config = {
get: getConfigStub({ [UI_SETTINGS.COURIER_BATCH_SEARCHES]: true }),
};
const requests = [{ foo: 1 }, { foo: 2 }];
const options = [{ bar: 1 }, { bar: 2 }];

Expand All @@ -105,9 +102,9 @@ describe('fetchSoon', () => {
});

test('should return the response to the corresponding call for multiple batched requests', async () => {
const config = getConfigStub({
[UI_SETTINGS.COURIER_BATCH_SEARCHES]: true,
});
const config = {
get: getConfigStub({ [UI_SETTINGS.COURIER_BATCH_SEARCHES]: true }),
};
const requests = [{ _mockResponseId: 'foo' }, { _mockResponseId: 'bar' }];

const promises = requests.map((request) => {
Expand All @@ -120,9 +117,9 @@ describe('fetchSoon', () => {
});

test('should wait for the previous batch to start before starting a new batch', () => {
const config = getConfigStub({
[UI_SETTINGS.COURIER_BATCH_SEARCHES]: true,
});
const config = {
get: getConfigStub({ [UI_SETTINGS.COURIER_BATCH_SEARCHES]: true }),
};
const firstBatch = [{ foo: 1 }, { foo: 2 }];
const secondBatch = [{ bar: 1 }, { bar: 2 }];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
*/

import { getMSearchParams } from './get_msearch_params';
import { IUiSettingsClient } from '../../../../../core/public';
import { UI_SETTINGS } from '../../../common';
import { GetConfigFn, UI_SETTINGS } from '../../../common';

function getConfigStub(config: any = {}) {
return {
get: (key) => config[key],
} as IUiSettingsClient;
function getConfigStub(config: any = {}): GetConfigFn {
return (key) => config[key];
}

describe('getMSearchParams', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/data/public/search/legacy/get_msearch_params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
* under the License.
*/

import { IUiSettingsClient } from 'kibana/public';
import { GetConfigFn } from '../../../common';
import { getIgnoreThrottled, getMaxConcurrentShardRequests } from '../fetch';

export function getMSearchParams(config: IUiSettingsClient) {
export function getMSearchParams(getConfig: GetConfigFn) {
return {
rest_total_hits_as_int: true,
ignore_throttled: getIgnoreThrottled(config),
max_concurrent_shard_requests: getMaxConcurrentShardRequests(config),
ignore_throttled: getIgnoreThrottled(getConfig),
max_concurrent_shard_requests: getMaxConcurrentShardRequests(getConfig),
};
}
2 changes: 1 addition & 1 deletion src/plugins/data/public/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
};

const searchSourceDependencies: SearchSourceDependencies = {
uiSettings,
getConfig: uiSettings.get.bind(uiSettings),
esShardTimeout: injectedMetadata.getInjectedVar('esShardTimeout') as number,
search,
legacySearch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,28 @@
* specific language governing permissions and limitations
* under the License.
*/

import { createSearchSource as createSearchSourceFactory } from './create_search_source';
import { SearchSourceDependencies } from './search_source';
import { IIndexPattern } from '../../../common/index_patterns';
import { IndexPatternsContract } from '../../index_patterns/index_patterns';
import { Filter } from '../../../common/es_query/filters';
import { coreMock } from '../../../../../core/public/mocks';
import { dataPluginMock } from '../../mocks';

describe('createSearchSource', () => {
const indexPatternMock: IIndexPattern = {} as IIndexPattern;
let indexPatternContractMock: jest.Mocked<IndexPatternsContract>;
let dependencies: any;
let dependencies: SearchSourceDependencies;
let createSearchSource: ReturnType<typeof createSearchSourceFactory>;

beforeEach(() => {
const core = coreMock.createStart();
const data = dataPluginMock.createStartContract();

dependencies = {
searchService: data.search,
uiSettings: core.uiSettings,
injectedMetadata: core.injectedMetadata,
getConfig: jest.fn(),
search: jest.fn(),
legacySearch: data.search.__LEGACY,
esShardTimeout: 30000,
};

indexPatternContractMock = ({
Expand Down
9 changes: 3 additions & 6 deletions src/plugins/data/public/search/search_source/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
* under the License.
*/

import {
injectedMetadataServiceMock,
uiSettingsServiceMock,
} from '../../../../../core/public/mocks';
import { uiSettingsServiceMock } from '../../../../../core/public/mocks';

import { ISearchSource, SearchSource } from './search_source';
import { SearchSourceFields } from './types';
Expand Down Expand Up @@ -54,13 +51,13 @@ export const searchSourceMock = {

export const createSearchSourceMock = (fields?: SearchSourceFields) =>
new SearchSource(fields, {
getConfig: uiSettingsServiceMock.createStartContract().get,
esShardTimeout: 30000,
search: jest.fn(),
legacySearch: {
esClient: {
search: jest.fn(),
msearch: jest.fn(),
},
},
uiSettings: uiSettingsServiceMock.createStartContract(),
injectedMetadata: injectedMetadataServiceMock.createStartContract(),
});
24 changes: 9 additions & 15 deletions src/plugins/data/public/search/search_source/search_source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
* specific language governing permissions and limitations
* under the License.
*/

import { Observable } from 'rxjs';
import { SearchSource } from './search_source';
import { GetConfigFn } from 'src/plugins/data/common';
import { SearchSource, SearchSourceDependencies } from './search_source';
import { IndexPattern, SortDirection } from '../..';
import { fetchSoon } from '../legacy';
import { IUiSettingsClient } from '../../../../../core/public';
import { dataPluginMock } from '../../../../data/public/mocks';
import { coreMock } from '../../../../../core/public/mocks';

jest.mock('../legacy', () => ({
fetchSoon: jest.fn().mockResolvedValue({}),
Expand Down Expand Up @@ -51,10 +51,9 @@ const indexPattern2 = ({

describe('SearchSource', () => {
let mockSearchMethod: any;
let searchSourceDependencies: any;
let searchSourceDependencies: SearchSourceDependencies;

beforeEach(() => {
const core = coreMock.createStart();
const data = dataPluginMock.createStartContract();

mockSearchMethod = jest.fn(() => {
Expand All @@ -69,10 +68,10 @@ describe('SearchSource', () => {
});

searchSourceDependencies = {
getConfig: jest.fn(),
search: mockSearchMethod,
legacySearch: data.search.__LEGACY,
injectedMetadata: core.injectedMetadata,
uiSettings: core.uiSettings,
esShardTimeout: 30000,
};
});

Expand Down Expand Up @@ -184,16 +183,11 @@ describe('SearchSource', () => {

describe('#legacy fetch()', () => {
beforeEach(() => {
const core = coreMock.createStart();

searchSourceDependencies = {
...searchSourceDependencies,
uiSettings: {
...core.uiSettings,
get: jest.fn(() => {
return true; // batchSearches = true
}),
} as IUiSettingsClient,
getConfig: jest.fn(() => {
return true; // batchSearches = true
}) as GetConfigFn,
};
});

Expand Down
Loading

0 comments on commit 8456568

Please sign in to comment.