Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[esaggs][inspector]: Refactor to prep for esaggs move to server. #83199

Merged
merged 7 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-embeddable-public](./kibana-plugin-plugins-embeddable-public.md) &gt; [Adapters](./kibana-plugin-plugins-embeddable-public.adapters.md) &gt; [data](./kibana-plugin-plugins-embeddable-public.adapters.data.md)

## Adapters.data property

<b>Signature:</b>

```typescript
data?: DataAdapter;
```
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ The interface that the adapters used to open an inspector have to fullfill.
```typescript
export interface Adapters
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [data](./kibana-plugin-plugins-embeddable-public.adapters.data.md) | <code>DataAdapter</code> | |
| [requests](./kibana-plugin-plugins-embeddable-public.adapters.requests.md) | <code>RequestAdapter</code> | |

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-embeddable-public](./kibana-plugin-plugins-embeddable-public.md) &gt; [Adapters](./kibana-plugin-plugins-embeddable-public.adapters.md) &gt; [requests](./kibana-plugin-plugins-embeddable-public.adapters.requests.md)

## Adapters.requests property

<b>Signature:</b>

```typescript
requests?: RequestAdapter;
```
4 changes: 2 additions & 2 deletions src/plugins/data/common/search/aggs/agg_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export interface AggTypeConfig<
aggConfigs: IAggConfigs,
aggConfig: TAggConfig,
searchSource: ISearchSource,
inspectorRequestAdapter: RequestAdapter,
inspectorRequestAdapter?: RequestAdapter,
abortSignal?: AbortSignal
) => Promise<any>;
getSerializedFormat?: (agg: TAggConfig) => SerializedFieldFormat;
Expand Down Expand Up @@ -189,7 +189,7 @@ export class AggType<
aggConfigs: IAggConfigs,
aggConfig: TAggConfig,
searchSource: ISearchSource,
inspectorRequestAdapter: RequestAdapter,
inspectorRequestAdapter?: RequestAdapter,
abortSignal?: AbortSignal
) => Promise<any>;
/**
Expand Down
42 changes: 24 additions & 18 deletions src/plugins/data/common/search/aggs/buckets/terms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import { noop } from 'lodash';
import { i18n } from '@kbn/i18n';
import type { RequestAdapter } from 'src/plugins/inspector/common';

import { BucketAggType, IBucketAggConfig } from './bucket_agg_type';
import { BUCKET_TYPES } from './bucket_agg_types';
Expand Down Expand Up @@ -111,27 +112,32 @@ export const getTermsBucketAgg = () =>

nestedSearchSource.setField('aggs', filterAgg);

const request = inspectorRequestAdapter.start(
i18n.translate('data.search.aggs.buckets.terms.otherBucketTitle', {
defaultMessage: 'Other bucket',
}),
{
description: i18n.translate('data.search.aggs.buckets.terms.otherBucketDescription', {
defaultMessage:
'This request counts the number of documents that fall ' +
'outside the criterion of the data buckets.',
let request: ReturnType<RequestAdapter['start']> | undefined;
if (inspectorRequestAdapter) {
request = inspectorRequestAdapter.start(
i18n.translate('data.search.aggs.buckets.terms.otherBucketTitle', {
defaultMessage: 'Other bucket',
}),
}
);
nestedSearchSource.getSearchRequestBody().then((body) => {
request.json(body);
});
request.stats(getRequestInspectorStats(nestedSearchSource));
{
description: i18n.translate('data.search.aggs.buckets.terms.otherBucketDescription', {
defaultMessage:
'This request counts the number of documents that fall ' +
'outside the criterion of the data buckets.',
}),
}
);
nestedSearchSource.getSearchRequestBody().then((body) => {
request!.json(body);
});
request.stats(getRequestInspectorStats(nestedSearchSource));
}

const response = await nestedSearchSource.fetch({ abortSignal });
request
.stats(getResponseInspectorStats(response, nestedSearchSource))
.ok({ json: response });
if (request) {
request
.stats(getResponseInspectorStats(response, nestedSearchSource))
.ok({ json: response });
}
resp = mergeOtherBucketAggResponse(aggConfigs, resp, response, aggConfig, filterAgg());
}
if (aggConfig.params.missingBucket) {
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/data/public/autocomplete/autocomplete_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { CoreSetup, PluginInitializerContext } from 'src/core/public';
import { TimefilterSetup } from '../query';
import { QuerySuggestionGetFn } from './providers/query_suggestion_provider';
import {
getEmptyValueSuggestions,
Expand Down Expand Up @@ -57,9 +58,9 @@ export class AutocompleteService {
private hasQuerySuggestions = (language: string) => this.querySuggestionProviders.has(language);

/** @public **/
public setup(core: CoreSetup) {
public setup(core: CoreSetup, { timefilter }: { timefilter: TimefilterSetup }) {
this.getValueSuggestions = this.autocompleteConfig.valueSuggestions.enabled
? setupValueSuggestionProvider(core)
? setupValueSuggestionProvider(core, { timefilter })
: getEmptyValueSuggestions;

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

import { stubIndexPattern, stubFields } from '../../stubs';
import { TimefilterSetup } from '../../query';
import { setupValueSuggestionProvider, ValueSuggestionsGetFn } from './value_suggestion_provider';
import { IUiSettingsClient, CoreSetup } from 'kibana/public';

jest.mock('../../services', () => ({
getQueryService: () => ({
timefilter: {
timefilter: {
createFilter: () => {
return {
time: 'fake',
};
},
getTime: () => {
return {
to: 'now',
from: 'now-15m',
};
},
},
},
}),
}));

describe('FieldSuggestions', () => {
let getValueSuggestions: ValueSuggestionsGetFn;
let http: any;
Expand All @@ -50,7 +31,23 @@ describe('FieldSuggestions', () => {
const uiSettings = { get: (key: string) => shouldSuggestValues } as IUiSettingsClient;
http = { fetch: jest.fn() };

getValueSuggestions = setupValueSuggestionProvider({ http, uiSettings } as CoreSetup);
getValueSuggestions = setupValueSuggestionProvider({ http, uiSettings } as CoreSetup, {
timefilter: ({
timefilter: {
createFilter: () => {
return {
time: 'fake',
};
},
getTime: () => {
return {
to: 'now',
from: 'now-15m',
};
},
},
} as unknown) as TimefilterSetup,
});
});

describe('with value suggestions disabled', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import dateMath from '@elastic/datemath';
import { memoize } from 'lodash';
import { CoreSetup } from 'src/core/public';
import { IIndexPattern, IFieldType, UI_SETTINGS, buildQueryFromFilters } from '../../../common';
import { getQueryService } from '../../services';
import { TimefilterSetup } from '../../query';

function resolver(title: string, field: IFieldType, query: string, filters: any[]) {
// Only cache results for a minute
Expand All @@ -40,8 +40,10 @@ interface ValueSuggestionsGetFnArgs {
signal?: AbortSignal;
}

const getAutocompleteTimefilter = (indexPattern: IIndexPattern) => {
const { timefilter } = getQueryService().timefilter;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I removed the getQueryService service getter, I realized it was still being used here. Rather than revert the change, I simply updated the autocomplete service since it was a straightforward fix.

const getAutocompleteTimefilter = (
{ timefilter }: TimefilterSetup,
indexPattern: IIndexPattern
) => {
const timeRange = timefilter.getTime();

// Use a rounded timerange so that memoizing works properly
Expand All @@ -54,7 +56,10 @@ const getAutocompleteTimefilter = (indexPattern: IIndexPattern) => {

export const getEmptyValueSuggestions = (() => Promise.resolve([])) as ValueSuggestionsGetFn;

export const setupValueSuggestionProvider = (core: CoreSetup): ValueSuggestionsGetFn => {
export const setupValueSuggestionProvider = (
core: CoreSetup,
{ timefilter }: { timefilter: TimefilterSetup }
): ValueSuggestionsGetFn => {
const requestSuggestions = memoize(
(index: string, field: IFieldType, query: string, filters: any = [], signal?: AbortSignal) =>
core.http.fetch(`/api/kibana/suggestions/values/${index}`, {
Expand Down Expand Up @@ -86,7 +91,9 @@ export const setupValueSuggestionProvider = (core: CoreSetup): ValueSuggestionsG
return [];
}

const timeFilter = useTimeRange ? getAutocompleteTimefilter(indexPattern) : undefined;
const timeFilter = useTimeRange
? getAutocompleteTimefilter(timefilter, indexPattern)
: undefined;
const filterQuery = timeFilter ? buildQueryFromFilters([timeFilter], indexPattern).filter : [];
const filters = [...(boolFilter ? boolFilter : []), ...filterQuery];
return await requestSuggestions(title, field, query, filters, signal);
Expand Down
11 changes: 0 additions & 11 deletions src/plugins/data/public/index_patterns/index_pattern.stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,9 @@
import sinon from 'sinon';

import { CoreSetup } from 'src/core/public';
import { FieldFormat as FieldFormatImpl } from '../../common/field_formats';
import { IFieldType, FieldSpec } from '../../common/index_patterns';
import { FieldFormatsStart } from '../field_formats';
import { IndexPattern, indexPatterns, KBN_FIELD_TYPES, fieldList } from '../';
import { getFieldFormatsRegistry } from '../test_utils';
import { setFieldFormats } from '../services';

setFieldFormats(({
getDefaultInstance: () =>
({
getConverterFor: () => (value: any) => value,
convert: (value: any) => JSON.stringify(value),
} as FieldFormatImpl),
} as unknown) as FieldFormatsStart);

export function getStubIndexPattern(
pattern: string,
Expand Down
24 changes: 17 additions & 7 deletions src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,14 @@ import {
UiSettingsPublicToCommon,
} from './index_patterns';
import {
setFieldFormats,
setIndexPatterns,
setNotifications,
setOverlays,
setQueryService,
setSearchService,
setUiSettings,
} from './services';
import { createSearchBar } from './ui/search_bar/create_search_bar';
import { esaggs } from './search/expressions';
import { getEsaggs } from './search/expressions';
import {
SELECT_RANGE_TRIGGER,
VALUE_CLICK_TRIGGER,
Expand Down Expand Up @@ -111,8 +109,22 @@ export class DataPublicPlugin
): DataPublicPluginSetup {
const startServices = createStartServicesGetter(core.getStartServices);

expressions.registerFunction(esaggs);
expressions.registerFunction(indexPatternLoad);
expressions.registerFunction(
getEsaggs({
getStartDependencies: async () => {
const [, , self] = await core.getStartServices();
const { fieldFormats, indexPatterns, query, search } = self;
return {
addFilters: query.filterManager.addFilters.bind(query.filterManager),
aggs: search.aggs,
deserializeFieldFormat: fieldFormats.deserialize.bind(fieldFormats),
indexPatterns,
searchSource: search.searchSource,
};
},
Comment on lines +115 to +125
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these service dependencies are now wired up in the top-level plugin.ts, which should make it easy to update when we register the function on the server.

Some of these dependencies may also be adjusted when we clean up the esaggs index patterns & aggs args in a follow up PR.

})
);

this.usageCollection = usageCollection;

Expand Down Expand Up @@ -145,7 +157,7 @@ export class DataPublicPlugin
});

return {
autocomplete: this.autocomplete.setup(core),
autocomplete: this.autocomplete.setup(core, { timefilter: queryService.timefilter }),
search: searchService,
fieldFormats: this.fieldFormatsService.setup(core),
query: queryService,
Expand All @@ -162,7 +174,6 @@ export class DataPublicPlugin
setUiSettings(uiSettings);

const fieldFormats = this.fieldFormatsService.start();
setFieldFormats(fieldFormats);

const indexPatterns = new IndexPatternsService({
uiSettings: new UiSettingsPublicToCommon(uiSettings),
Expand All @@ -186,7 +197,6 @@ export class DataPublicPlugin
savedObjectsClient: savedObjects.client,
uiSettings,
});
setQueryService(query);

const search = this.searchService.start(core, { fieldFormats, indexPatterns });
setSearchService(search);
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { EuiButtonEmptyProps } from '@elastic/eui';
import { EuiComboBoxProps } from '@elastic/eui';
import { EuiConfirmModalProps } from '@elastic/eui';
import { EuiGlobalToastListToast } from '@elastic/eui';
import { EventEmitter } from 'events';
import { ExclusiveUnion } from '@elastic/eui';
import { ExecutionContext } from 'src/plugins/expressions/common';
import { ExpressionAstFunction } from 'src/plugins/expressions/common';
Expand Down Expand Up @@ -66,7 +67,7 @@ import * as React_2 from 'react';
import { RecursiveReadonly } from '@kbn/utility-types';
import { Reporter } from '@kbn/analytics';
import { RequestAdapter } from 'src/plugins/inspector/common';
import { RequestStatistics } from 'src/plugins/inspector/common';
import { RequestStatistics as RequestStatistics_2 } from 'src/plugins/inspector/common';
import { Required } from '@kbn/utility-types';
import * as Rx from 'rxjs';
import { SavedObject } from 'src/core/server';
Expand Down
Loading