Skip to content

Commit

Permalink
[data.search.SearchSource] Extract dependencies and pass them in from…
Browse files Browse the repository at this point in the history
… the service definition. (#75368) (#75564)
  • Loading branch information
lukeelmers authored Aug 20, 2020
1 parent a618464 commit 900100d
Show file tree
Hide file tree
Showing 34 changed files with 188 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<b>Signature:</b>

```typescript
export declare type FieldFormatsGetConfigFn = <T = any>(key: string, defaultOverride?: T) => T;
export declare type FieldFormatsGetConfigFn = GetConfigFn;
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

## getSearchParamsFromRequest() function


<b>Signature:</b>

```typescript
export declare function getSearchParamsFromRequest(searchRequest: SearchRequest, dependencies: {
injectedMetadata: CoreStart['injectedMetadata'];
uiSettings: IUiSettingsClient;
esShardTimeout: number;
getConfig: GetConfigFn;
}): ISearchRequestParams;
```

Expand All @@ -18,7 +19,7 @@ export declare function getSearchParamsFromRequest(searchRequest: SearchRequest,
| Parameter | Type | Description |
| --- | --- | --- |
| searchRequest | <code>SearchRequest</code> | |
| dependencies | <code>{</code><br/><code> injectedMetadata: CoreStart['injectedMetadata'];</code><br/><code> uiSettings: IUiSettingsClient;</code><br/><code>}</code> | |
| dependencies | <code>{</code><br/><code> esShardTimeout: number;</code><br/><code> getConfig: GetConfigFn;</code><br/><code>}</code> | |

<b>Returns:</b>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<b>Signature:</b>

```typescript
export declare type FieldFormatsGetConfigFn = <T = any>(key: string, defaultOverride?: T) => T;
export declare type FieldFormatsGetConfigFn = GetConfigFn;
```
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 @@ -20,7 +20,8 @@
import { i18n } from '@kbn/i18n';
import { memoize, noop } from 'lodash';
import moment, { Moment } from 'moment';
import { FieldFormat, FIELD_FORMAT_IDS, KBN_FIELD_TYPES, TextContextTypeConvert } from '../../';
import { FieldFormat, FIELD_FORMAT_IDS, KBN_FIELD_TYPES } from '../../';
import { TextContextTypeConvert } from '../types';

/**
* Analyse the given moment.js format pattern for the fractional sec part (S,SS,SSS...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
import { baseFormatters } from './constants/base_formatters';
import { FieldFormat } from './field_format';
import { SerializedFieldFormat } from '../../../expressions/common/types';
import { ES_FIELD_TYPES, KBN_FIELD_TYPES } from '../types';
import { ES_FIELD_TYPES, KBN_FIELD_TYPES } from '../kbn_field_types/types';
import { UI_SETTINGS } from '../constants';

export class FieldFormatsRegistry {
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/data/common/field_formats/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* specific language governing permissions and limitations
* under the License.
*/

import { GetConfigFn } from '../types';
import { FieldFormat } from './field_format';
import { FieldFormatsRegistry } from './field_formats_registry';

Expand Down Expand Up @@ -72,7 +74,7 @@ export interface FieldFormatConfig {
es?: boolean;
}

export type FieldFormatsGetConfigFn = <T = any>(key: string, defaultOverride?: T) => T;
export type FieldFormatsGetConfigFn = GetConfigFn;

export type IFieldFormat = PublicMethodsOf<FieldFormat>;

Expand Down
3 changes: 2 additions & 1 deletion src/plugins/data/common/search/aggs/aggs_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import { ExpressionsServiceSetup } from 'src/plugins/expressions/common';
import { UI_SETTINGS } from '../../../common';
import { GetConfigFn } from '../../types';
import {
AggConfigs,
AggTypesRegistry,
Expand Down Expand Up @@ -48,7 +49,7 @@ export interface AggsCommonSetupDependencies {

/** @internal */
export interface AggsCommonStartDependencies {
getConfig: <T = any>(key: string) => T;
getConfig: GetConfigFn;
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/plugins/data/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@
export * from './query/types';
export * from './kbn_field_types/types';
export * from './index_patterns/types';
export { TextContextTypeConvert, IFieldFormatMetaParams } from './field_formats/types';

/**
* If a service is being shared on both the client and the server, and
* the client code requires synchronous access to uiSettings, both client
* and server should wrap the core uiSettings services in a function
* matching this signature.
*
* This matches the signature of the public `core.uiSettings.get`, and
* should only be used in scenarios where async access to uiSettings is
* not possible.
*/
export type GetConfigFn = <T = any>(key: string, defaultOverride?: T) => T;
8 changes: 2 additions & 6 deletions src/plugins/data/public/field_formats/converters/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@
import { i18n } from '@kbn/i18n';
import { memoize, noop } from 'lodash';
import moment from 'moment';
import {
FieldFormat,
KBN_FIELD_TYPES,
TextContextTypeConvert,
FIELD_FORMAT_IDS,
} from '../../../common';
import { FieldFormat, KBN_FIELD_TYPES, FIELD_FORMAT_IDS } from '../../../common';
import { TextContextTypeConvert } from '../../../common/field_formats/types';

export class DateFormat extends FieldFormat {
static id = FIELD_FORMAT_IDS.DATE;
Expand Down
4 changes: 0 additions & 4 deletions src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ import {
} from './index_patterns';
import {
setFieldFormats,
setHttp,
setIndexPatterns,
setInjectedMetadata,
setNotifications,
setOverlays,
setQueryService,
Expand Down Expand Up @@ -165,11 +163,9 @@ export class DataPublicPlugin

public start(core: CoreStart, { uiActions }: DataStartDependencies): DataPublicPluginStart {
const { uiSettings, http, notifications, savedObjects, overlays, application } = core;
setHttp(http);
setNotifications(notifications);
setOverlays(overlays);
setUiSettings(uiSettings);
setInjectedMetadata(core.injectedMetadata);

const fieldFormats = this.fieldFormatsService.start();
setFieldFormats(fieldFormats);
Expand Down
9 changes: 4 additions & 5 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import { InjectedIntl } from '@kbn/i18n/react';
import { ISearchSource as ISearchSource_2 } from 'src/plugins/data/public';
import { IStorageWrapper } from 'src/plugins/kibana_utils/public';
import { IUiSettingsClient } from 'src/core/public';
import { IUiSettingsClient as IUiSettingsClient_3 } from 'kibana/public';
import { KibanaClient } from '@elastic/elasticsearch/api/kibana';
import { KibanaConfigType } from 'src/core/server/kibana_config';
import { Location } from 'history';
Expand Down Expand Up @@ -594,10 +593,11 @@ export const fieldFormats: {
// @public (undocumented)
export type FieldFormatsContentType = 'html' | 'text';

// Warning: (ae-forgotten-export) The symbol "GetConfigFn" needs to be exported by the entry point index.d.ts
// Warning: (ae-missing-release-tag) "FieldFormatsGetConfigFn" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
export type FieldFormatsGetConfigFn = <T = any>(key: string, defaultOverride?: T) => T;
export type FieldFormatsGetConfigFn = GetConfigFn;

// Warning: (ae-missing-release-tag) "FieldList" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
Expand Down Expand Up @@ -727,12 +727,11 @@ export function getEsPreference(uiSettings: IUiSettingsClient_2, sessionId?: str
export const getKbnTypeNames: () => string[];

// Warning: (ae-forgotten-export) The symbol "ISearchRequestParams" needs to be exported by the entry point index.d.ts
// Warning: (ae-missing-release-tag) "getSearchParamsFromRequest" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
export function getSearchParamsFromRequest(searchRequest: SearchRequest, dependencies: {
injectedMetadata: CoreStart['injectedMetadata'];
uiSettings: IUiSettingsClient_3;
esShardTimeout: number;
getConfig: GetConfigFn;
}): ISearchRequestParams;

// Warning: (ae-missing-release-tag) "getTime" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Search Usage Collector', () => {
{} as any,
]);
mockUsageCollectionSetup = usageCollectionPluginMock.createSetupContract();
usageCollector = createUsageCollector(mockCoreSetup, mockUsageCollectionSetup);
usageCollector = createUsageCollector(mockCoreSetup.getStartServices, mockUsageCollectionSetup);
});

test('tracks query timeouts', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
*/

import { first } from 'rxjs/operators';
import { CoreSetup } from '../../../../../core/public';
import { StartServicesAccessor } from '../../../../../core/public';
import { METRIC_TYPE, UsageCollectionSetup } from '../../../../usage_collection/public';
import { SEARCH_EVENT_TYPE, SearchUsageCollector } from './types';

export const createUsageCollector = (
core: CoreSetup,
getStartServices: StartServicesAccessor,
usageCollection?: UsageCollectionSetup
): SearchUsageCollector => {
const getCurrentApp = async () => {
const [{ application }] = await core.getStartServices();
const [{ application }] = await getStartServices();
return application.currentAppId$.pipe(first()).toPromise();
};

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
35 changes: 18 additions & 17 deletions src/plugins/data/public/search/fetch/get_search_params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,52 @@
* under the License.
*/

import { IUiSettingsClient, CoreStart } 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;
}

export function getTimeout(esShardTimeout: number) {
return esShardTimeout > 0 ? `${esShardTimeout}ms` : undefined;
}

/** @public */
// TODO: Could provide this on runtime contract with dependencies
// already wired up.
export function getSearchParamsFromRequest(
searchRequest: SearchRequest,
dependencies: { injectedMetadata: CoreStart['injectedMetadata']; uiSettings: IUiSettingsClient }
dependencies: { esShardTimeout: number; getConfig: GetConfigFn }
): ISearchRequestParams {
const { injectedMetadata, uiSettings } = dependencies;
const esShardTimeout = injectedMetadata.getInjectedVar('esShardTimeout') as number;
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
Loading

0 comments on commit 900100d

Please sign in to comment.