Skip to content

Commit

Permalink
Shim ui/autocomplete_providers into legacy data plugin (#46138) (#46479)
Browse files Browse the repository at this point in the history
* Shim ui/autocomplete_providers into legacy data plugin

Fix: #44949

* Fix PR commnets

* fix CI

* Add comment
  • Loading branch information
alexwizp authored Sep 24, 2019
1 parent 3b48e33 commit f497b61
Show file tree
Hide file tree
Showing 52 changed files with 221 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ export const mockPersistedLogFactory = jest.fn<jest.Mocked<typeof mockPersistedL
return mockPersistedLog;
});

export const mockGetAutocompleteSuggestions = jest.fn(() => Promise.resolve([]));
const mockAutocompleteProvider = jest.fn(() => mockGetAutocompleteSuggestions);
export const mockGetAutocompleteProvider = jest.fn(() => mockAutocompleteProvider);

export const mockFetchIndexPatterns = jest
.fn()
.mockReturnValue(Promise.resolve([mockIndexPattern]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ import React from 'react';
import { EuiFieldText, EuiOutsideClickDetector, PopoverAnchorPosition } from '@elastic/eui';

import { InjectedIntl, injectI18n } from '@kbn/i18n/react';
import {
AutocompleteSuggestion,
AutocompleteSuggestionType,
getAutocompleteProvider,
} from 'ui/autocomplete_providers';
import { debounce, compact, isEqual, omit } from 'lodash';
import { PersistedLog } from 'ui/persisted_log';
import { Storage } from 'ui/storage';
import { npStart } from 'ui/new_platform';
import {
UiSettingsClientContract,
SavedObjectsClientContract,
HttpServiceBase,
} from 'src/core/public';
import {
AutocompleteSuggestion,
AutocompleteSuggestionType,
} from '../../../../../../../plugins/data/public';
import { IndexPattern, StaticIndexPattern } from '../../../index_patterns';
import { Query } from '../index';
import { fromUser, matchPairs, toUser } from '../lib';
Expand All @@ -44,6 +44,11 @@ import { SuggestionsComponent } from './typeahead/suggestions_component';
import { getQueryLog } from '../lib/get_query_log';
import { fetchIndexPatterns } from '../lib/fetch_index_patterns';

// todo: related to https://github.com/elastic/kibana/pull/45762/files
// Will be refactored after merge of related PR
const getAutocompleteProvider = (language: string) =>
npStart.plugins.data.autocomplete.getProvider(language);

interface Props {
uiSettings: UiSettingsClientContract;
indexPatterns: Array<IndexPattern | string>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { mount, shallow } from 'enzyme';
import React from 'react';
import { AutocompleteSuggestion } from 'ui/autocomplete_providers';
import { AutocompleteSuggestion } from '../../../../../../../../plugins/data/public';
import { SuggestionComponent } from './suggestion_component';

const noop = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { EuiIcon } from '@elastic/eui';
import classNames from 'classnames';
import React, { FunctionComponent } from 'react';
import { AutocompleteSuggestion } from 'ui/autocomplete_providers';
import { AutocompleteSuggestion } from '../../../../../../../../plugins/data/public';

function getEuiIconType(type: string) {
switch (type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { mount, shallow } from 'enzyme';
import React from 'react';
import { AutocompleteSuggestion } from 'ui/autocomplete_providers';
import { AutocompleteSuggestion } from '../../../../../../../../plugins/data/public';
import { SuggestionComponent } from './suggestion_component';
import { SuggestionsComponent } from './suggestions_component';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { isEmpty } from 'lodash';
import React, { Component } from 'react';
import { AutocompleteSuggestion } from 'ui/autocomplete_providers';
import { AutocompleteSuggestion } from '../../../../../../../../plugins/data/public';
import { SuggestionComponent } from './suggestion_component';

interface Props {
Expand Down
1 change: 0 additions & 1 deletion src/legacy/core_plugins/kibana/public/kibana.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import 'uiExports/embeddableFactories';
import 'uiExports/embeddableActions';
import 'uiExports/inspectorViews';
import 'uiExports/search';
import 'uiExports/autocompleteProviders';
import 'uiExports/shareContextMenuExtensions';

import 'ui/autoload/all';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

import 'ui/registry/field_formats';
import 'uiExports/autocompleteProviders';
import 'uiExports/contextMenuActions';
import 'uiExports/devTools';
import 'uiExports/docViews';
Expand Down
1 change: 0 additions & 1 deletion src/legacy/ui/ui_exports/ui_export_types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export {
aliases,
visualize,
search,
autocompleteProviders,
shareContextMenuExtensions,
} from './ui_app_extensions';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const appExtension = wrap(
export const visTypes = appExtension;

export const visEditorTypes = appExtension;
export const autocompleteProviders = appExtension;
export const savedObjectTypes = appExtension;
export const embeddableActions = appExtension;
export const embeddableFactories = appExtension;
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/data/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@
* under the License.
*/

export * from './timefilter/types';
export * from './query/types';
export * from './query';
export * from './field_formats';
4 changes: 3 additions & 1 deletion src/plugins/data/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@
* under the License.
*/

export * from '.';
export * from './field_formats/types';
export * from './timefilter/types';
export * from './query/types';
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,25 @@
* specific language governing permissions and limitations
* under the License.
*/
import { AutocompleteProvider } from './types';

const autocompleteProviders = new Map();
export class AutocompleteProviderRegister {
private readonly registeredProviders: Map<string, AutocompleteProvider> = new Map();

export function addAutocompleteProvider(language, provider) {
autocompleteProviders.set(language, provider);
}
/** @public **/
public addProvider(language: string, provider: AutocompleteProvider): void {
if (language && provider) {
this.registeredProviders.set(language, provider);
}
}

/** @public **/
public getProvider(language: string): AutocompleteProvider | undefined {
return this.registeredProviders.get(language);
}

export function getAutocompleteProvider(language) {
return autocompleteProviders.get(language);
/** @internal **/
public clearProviders(): void {
this.registeredProviders.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
* under the License.
*/

/**
* WARNING: these typings are incomplete
*/
import { StaticIndexPattern } from 'ui/index_patterns';

/** @public **/
export type AutocompleteProvider = (args: {
config: {
get(configKey: string): any;
Expand All @@ -30,27 +28,26 @@ export type AutocompleteProvider = (args: {
boolFilter?: any;
}) => GetSuggestions;

/** @public **/
export type GetSuggestions = (args: {
query: string;
selectionStart: number;
selectionEnd: number;
}) => Promise<AutocompleteSuggestion[]>;

/** @public **/
export type AutocompleteSuggestionType =
| 'field'
| 'value'
| 'operator'
| 'conjunction'
| 'recentSearch';

/** @public **/
export interface AutocompleteSuggestion {
description?: string;
end: number;
start: number;
text: string;
type: AutocompleteSuggestionType;
}

export function addAutocompleteProvider(language: string, provider: AutocompleteProvider): void;

export function getAutocompleteProvider(language: string): AutocompleteProvider | undefined;
5 changes: 3 additions & 2 deletions src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
import { PluginInitializerContext } from '../../../core/public';
import { DataPublicPlugin } from './plugin';

export { RefreshInterval, TimeRange, Query } from './types';

export function plugin(initializerContext: PluginInitializerContext) {
return new DataPublicPlugin(initializerContext);
}

export { DataPublicPlugin as Plugin };

export * from '../common';

export * from './autocomplete_provider';
export * from './types';
15 changes: 13 additions & 2 deletions src/plugins/data/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,24 @@ import { Plugin } from '.';
export type Setup = jest.Mocked<ReturnType<Plugin['setup']>>;
export type Start = jest.Mocked<ReturnType<Plugin['start']>>;

const autocompleteMock: any = {
addProvider: jest.fn(),
getProvider: jest.fn(),
clearProviders: jest.fn(),
};

const createSetupContract = (): Setup => {
const setupContract: Setup = {};
const setupContract: Setup = {
autocomplete: autocompleteMock as Setup['autocomplete'],
};

return setupContract;
};

const createStartContract = (): Start => {
const startContract: Start = undefined;
const startContract: Start = {
autocomplete: autocompleteMock as Start['autocomplete'],
};
return startContract;
};

Expand Down
30 changes: 25 additions & 5 deletions src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,34 @@
*/

import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from '../../../core/public';
import { AutocompleteProviderRegister } from './autocomplete_provider';

export interface DataPublicPluginSetup {
autocomplete: Pick<AutocompleteProviderRegister, 'addProvider' | 'getProvider'>;
}

export interface DataPublicPluginStart {
autocomplete: Pick<AutocompleteProviderRegister, 'getProvider'>;
}

export class DataPublicPlugin implements Plugin<DataPublicPluginSetup, DataPublicPluginStart> {
private readonly autocomplete = new AutocompleteProviderRegister();

export class DataPublicPlugin implements Plugin<{}> {
constructor(initializerContext: PluginInitializerContext) {}

public setup(core: CoreSetup) {
return {};
public setup(core: CoreSetup): DataPublicPluginSetup {
return {
autocomplete: this.autocomplete,
};
}

public start(core: CoreStart): DataPublicPluginStart {
return {
autocomplete: this.autocomplete,
};
}

public start(core: CoreStart) {}
public stop() {}
public stop() {
this.autocomplete.clearProviders();
}
}
1 change: 1 addition & 0 deletions src/plugins/data/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
*/

export * from '../common/types';
export * from './autocomplete_provider/types';
4 changes: 2 additions & 2 deletions x-pack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { indexManagement } from './legacy/plugins/index_management';
import { indexLifecycleManagement } from './legacy/plugins/index_lifecycle_management';
import { consoleExtensions } from './legacy/plugins/console_extensions';
import { spaces } from './legacy/plugins/spaces';
import { kueryAutocomplete } from './legacy/plugins/kuery_autocomplete';
import { kueryAutocompleteInitializer } from './legacy/plugins/kuery_autocomplete';
import { canvas } from './legacy/plugins/canvas';
import { infra } from './legacy/plugins/infra';
import { taskManager } from './legacy/plugins/task_manager';
Expand Down Expand Up @@ -72,7 +72,7 @@ module.exports = function (kibana) {
indexManagement(kibana),
consoleExtensions(kibana),
indexLifecycleManagement(kibana),
kueryAutocomplete(kibana),
kueryAutocompleteInitializer(kibana),
infra(kibana),
taskManager(kibana),
rollup(kibana),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import { EuiCallOut } from '@elastic/eui';
import styled from 'styled-components';
import { FormattedMessage } from '@kbn/i18n/react';
import { i18n } from '@kbn/i18n';
import {
AutocompleteSuggestion,
getAutocompleteProvider
} from 'ui/autocomplete_providers';
import { npStart } from 'ui/new_platform';
import { StaticIndexPattern, getFromSavedObject } from 'ui/index_patterns';
import { fromKueryExpression, toElasticsearchQuery } from '@kbn/es-query';
import { fromQuery, toQuery } from '../Links/url_helpers';
Expand All @@ -29,11 +26,15 @@ import { useMatchedRoutes } from '../../../hooks/useMatchedRoutes';
import { RouteName } from '../../app/Main/route_config/route_names';
import { useKibanaCore } from '../../../../../observability/public';
import { getAPMIndexPattern } from '../../../services/rest/savedObjects';
import { AutocompleteSuggestion } from '../../../../../../../../src/plugins/data/public';

const Container = styled.div`
margin-bottom: 10px;
`;

const getAutocompleteProvider = (language: string) =>
npStart.plugins.data.autocomplete.getProvider(language);

interface State {
indexPattern: StaticIndexPattern | null;
suggestions: AutocompleteSuggestion[];
Expand Down
1 change: 0 additions & 1 deletion x-pack/legacy/plugins/apm/public/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import 'ui/autoload/styles';
import chrome from 'ui/chrome';
// @ts-ignore
import { uiModules } from 'ui/modules';
import 'uiExports/autocompleteProviders';
import { GlobalHelpExtension } from './components/app/GlobalHelpExtension';
import { plugin } from './new-platform';
import { REACT_APP_ROOT_ID } from './new-platform/plugin';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import {
import React from 'react';
import styled from 'styled-components';

// @ts-ignore
import { AutocompleteSuggestion } from 'ui/autocomplete_providers';
import { AutocompleteSuggestion } from '../../../../../../../src/plugins/data/public';

import { composeStateUpdaters } from '../../utils/typed_react';
import { SuggestionItem } from './suggestion_item';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { EuiIcon } from '@elastic/eui';
import { tint } from 'polished';
import React from 'react';
import styled from 'styled-components';
// @ts-ignore
import { AutocompleteSuggestion } from 'ui/autocomplete_providers';

import { AutocompleteSuggestion } from '../../../../../../../src/plugins/data/public';

interface SuggestionItemProps {
isSelected?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { EuiBasicTable, EuiFlexGroup, EuiFlexItem, EuiSpacer } from '@elastic/eu
import { i18n } from '@kbn/i18n';
import React from 'react';
import styled from 'styled-components';
import { AutocompleteSuggestion } from 'ui/autocomplete_providers';
import { AutocompleteSuggestion } from '../../../../../../../src/plugins/data/public';
import { TABLE_CONFIG } from '../../../common/constants';
import { AutocompleteField } from '../autocomplete_field/index';
import { ControlSchema } from './action_schema';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import React from 'react';

import { AutocompleteSuggestion } from 'ui/autocomplete_providers';
import { AutocompleteSuggestion } from '../../../../../../src/plugins/data/public';

import { FrontendLibs } from '../lib/types';
import { RendererFunction } from '../utils/typed_react';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { AutocompleteSuggestion } from 'ui/autocomplete_providers';
import { AutocompleteSuggestion } from '../../../../../../../../src/plugins/data/public';

export interface ElasticsearchAdapter {
convertKueryToEsQuery: (kuery: string) => Promise<string>;
Expand Down
Loading

0 comments on commit f497b61

Please sign in to comment.