Skip to content

Commit

Permalink
Migrate doc view part of discover (elastic#58094) (elastic#61033)
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 authored Mar 24, 2020
1 parent 89a76b0 commit 7d32617
Show file tree
Hide file tree
Showing 65 changed files with 606 additions and 168 deletions.
1 change: 1 addition & 0 deletions .i18nrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"common.ui": "src/legacy/ui",
"console": "src/plugins/console",
"core": "src/core",
"discover": "src/plugins/discover",
"dashboard": "src/plugins/dashboard",
"data": "src/plugins/data",
"embeddableApi": "src/plugins/embeddable",
Expand Down
6 changes: 3 additions & 3 deletions src/core/MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -1233,11 +1233,11 @@ This table shows where these uiExports have moved to in the New Platform. In mos
| `chromeNavControls` | [`core.chrome.navControls.register{Left,Right}`](/docs/development/core/public/kibana-plugin-public.chromenavcontrols.md) | |
| `contextMenuActions` | | Should be an API on the devTools plugin. |
| `devTools` | | |
| `docViews` | | |
| `docViews` | [`plugins.discover.docViews.addDocView`](./src/plugins/discover/public/doc_views) | Should be an API on the discover plugin. |
| `embeddableActions` | | Should be an API on the embeddables plugin. |
| `embeddableFactories` | | Should be an API on the embeddables plugin. |
| `fieldFormatEditors` | | |
| `fieldFormats` | [`plugins.data.fieldFormats`](./src/plugins/data/public/field_formats) | |
| `fieldFormatEditors` | | |
| `fieldFormats` | [`plugins.data.fieldFormats`](./src/plugins/data/public/field_formats) | |
| `hacks` | n/a | Just run the code in your plugin's `start` method. |
| `home` | [`plugins.home.featureCatalogue.register`](./src/plugins/home/public/feature_catalogue) | Must add `home` as a dependency in your kibana.json. |
| `indexManagement` | | Should be an API on the indexManagement plugin. |
Expand Down
14 changes: 8 additions & 6 deletions src/legacy/core_plugins/kibana/public/discover/build_services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ import {

import { DiscoverStartPlugins } from './plugin';
import { SharePluginStart } from '../../../../../plugins/share/public';
import { DocViewsRegistry } from './np_ready/doc_views/doc_views_registry';
import { ChartsPluginStart } from '../../../../../plugins/charts/public';
import { VisualizationsStart } from '../../../visualizations/public';
import { createSavedSearchesLoader, SavedSearch } from '../../../../../plugins/discover/public';
import {
createSavedSearchesLoader,
DocViewerComponent,
SavedSearch,
} from '../../../../../plugins/discover/public';

export interface DiscoverServices {
addBasePath: (path: string) => string;
Expand All @@ -47,7 +50,7 @@ export interface DiscoverServices {
core: CoreStart;
data: DataPublicPluginStart;
docLinks: DocLinksStart;
docViewsRegistry: DocViewsRegistry;
DocViewer: DocViewerComponent;
history: History;
theme: ChartsPluginStart['theme'];
filterManager: FilterManager;
Expand All @@ -64,8 +67,7 @@ export interface DiscoverServices {
}
export async function buildServices(
core: CoreStart,
plugins: DiscoverStartPlugins,
docViewsRegistry: DocViewsRegistry
plugins: DiscoverStartPlugins
): Promise<DiscoverServices> {
const services = {
savedObjectsClient: core.savedObjects.client,
Expand All @@ -81,7 +83,7 @@ export async function buildServices(
core,
data: plugins.data,
docLinks: core.docLinks,
docViewsRegistry,
DocViewer: plugins.discover.docViews.DocViewer,
history: createHashHistory(),
theme: plugins.charts.theme,
filterManager: plugins.data.query.filterManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export {
EsQuerySortValue,
SortDirection,
} from '../../../../../plugins/data/public';
export { ElasticSearchHit } from './np_ready/doc_views/doc_views_types';
export { getFormat } from 'ui/visualize/loader/pipeline_helpers/utilities';
// @ts-ignore
export { buildPointSeriesData } from 'ui/agg_response/point_series/point_series';
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { FieldName } from './field_name/field_name';
import { FieldName } from '../../../../../../../../plugins/discover/public';
import { getServices, wrapInI18nContext } from '../../../kibana_services';

export function FieldNameDirectiveProvider(reactDirective) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
* under the License.
*/

import { DocViewer } from '../components/doc_viewer/doc_viewer';
import React from 'react';
import { getServices } from '../../kibana_services';

export function createDocViewerDirective(reactDirective: any) {
return reactDirective(
DocViewer,
(props: any) => {
const { DocViewer } = getServices();
return <DocViewer {...props} />;
},
[
'hit',
['indexPattern', { watchDepth: 'reference' }],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
@import 'fetch_error/index';
@import 'field_chooser/index';
@import 'doc_viewer/index';
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,13 @@ import { ReactWrapper } from 'enzyme';
import { findTestSubject } from '@elastic/eui/lib/test';
import { Doc, DocProps } from './doc';

jest.mock('../doc_viewer/doc_viewer', () => ({
DocViewer: () => null,
}));

jest.mock('../../../kibana_services', () => {
return {
getServices: () => ({
metadata: {
branch: 'test',
},
getDocViewsSorted: () => {
return [];
},
DocViewer: () => null,
}),
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import React from 'react';
import { FormattedMessage } from '@kbn/i18n/react';
import { EuiCallOut, EuiLink, EuiLoadingSpinner, EuiPageContent } from '@elastic/eui';
import { IndexPatternsContract } from 'src/plugins/data/public';
import { DocViewer } from '../doc_viewer/doc_viewer';
import { ElasticRequestState, useEsDocSearch } from './use_es_doc_search';
import { ElasticSearchHit, getServices } from '../../../kibana_services';
import { getServices } from '../../../kibana_services';
import { ElasticSearchHit } from '../../../../../../../../plugins/discover/public';

export interface ElasticSearchResult {
hits: {
Expand Down Expand Up @@ -61,6 +61,7 @@ export interface DocProps {
}

export function Doc(props: DocProps) {
const { DocViewer } = getServices();
const [reqState, hit, indexPattern] = useEsDocSearch(props);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
* under the License.
*/
import { useEffect, useState } from 'react';
import { ElasticSearchHit, IndexPattern } from '../../../kibana_services';
import { IndexPattern } from '../../../kibana_services';
import { DocProps } from './doc';
import { ElasticSearchHit } from '../../../../../../../../plugins/discover/public';

export enum ElasticRequestState {
Loading,
Expand Down
46 changes: 8 additions & 38 deletions src/legacy/core_plugins/kibana/public/discover/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import { BehaviorSubject } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { i18n } from '@kbn/i18n';
import { AppMountParameters, CoreSetup, CoreStart, Plugin } from 'kibana/public';
import angular, { auto } from 'angular';
import { UiActionsSetup, UiActionsStart } from 'src/plugins/ui_actions/public';
Expand All @@ -41,33 +40,22 @@ import {
KibanaLegacySetup,
AngularRenderedAppUpdater,
} from '../../../../../plugins/kibana_legacy/public';
import { DocViewsRegistry } from './np_ready/doc_views/doc_views_registry';
import { DocViewInput, DocViewInputFn } from './np_ready/doc_views/doc_views_types';
import { DocViewTable } from './np_ready/components/table/table';
import { JsonCodeBlock } from './np_ready/components/json_code_block/json_code_block';
import { DiscoverSetup, DiscoverStart } from '../../../../../plugins/discover/public';
import { HomePublicPluginSetup } from '../../../../../plugins/home/public';
import {
VisualizationsStart,
VisualizationsSetup,
} from '../../../visualizations/public/np_ready/public';
import { createKbnUrlTracker } from '../../../../../plugins/kibana_utils/public';

/**
* These are the interfaces with your public contracts. You should export these
* for other plugins to use in _their_ `SetupDeps`/`StartDeps` interfaces.
* @public
*/
export interface DiscoverSetup {
addDocView(docViewRaw: DocViewInput | DocViewInputFn): void;
}
export type DiscoverStart = void;
export interface DiscoverSetupPlugins {
uiActions: UiActionsSetup;
embeddable: EmbeddableSetup;
kibanaLegacy: KibanaLegacySetup;
home: HomePublicPluginSetup;
visualizations: VisualizationsSetup;
data: DataPublicPluginSetup;
discover: DiscoverSetup;
}
export interface DiscoverStartPlugins {
uiActions: UiActionsStart;
Expand All @@ -78,6 +66,7 @@ export interface DiscoverStartPlugins {
share: SharePluginStart;
inspector: any;
visualizations: VisualizationsStart;
discover: DiscoverStart;
}
const innerAngularName = 'app/discover';
const embeddableAngularName = 'app/discoverEmbeddable';
Expand All @@ -87,10 +76,9 @@ const embeddableAngularName = 'app/discoverEmbeddable';
* There are 2 kinds of Angular bootstrapped for rendering, additionally to the main Angular
* Discover provides embeddables, those contain a slimmer Angular
*/
export class DiscoverPlugin implements Plugin<DiscoverSetup, DiscoverStart> {
export class DiscoverPlugin implements Plugin<void, void> {
private servicesInitialized: boolean = false;
private innerAngularInitialized: boolean = false;
private docViewsRegistry: DocViewsRegistry | null = null;
private embeddableInjector: auto.IInjectorService | null = null;
private getEmbeddableInjector: (() => Promise<auto.IInjectorService>) | null = null;
private appStateUpdater = new BehaviorSubject<AngularRenderedAppUpdater>(() => ({}));
Expand All @@ -103,7 +91,7 @@ export class DiscoverPlugin implements Plugin<DiscoverSetup, DiscoverStart> {
public initializeInnerAngular?: () => void;
public initializeServices?: () => Promise<{ core: CoreStart; plugins: DiscoverStartPlugins }>;

setup(core: CoreSetup<DiscoverStartPlugins>, plugins: DiscoverSetupPlugins): DiscoverSetup {
setup(core: CoreSetup<DiscoverStartPlugins>, plugins: DiscoverSetupPlugins) {
const { appMounted, appUnMounted, stop: stopUrlTracker } = createKbnUrlTracker({
baseUrl: core.http.basePath.prepend('/app/kibana'),
defaultSubUrl: '#/discover',
Expand All @@ -130,21 +118,7 @@ export class DiscoverPlugin implements Plugin<DiscoverSetup, DiscoverStart> {
};

this.getEmbeddableInjector = this.getInjector.bind(this);
this.docViewsRegistry = new DocViewsRegistry(this.getEmbeddableInjector);
this.docViewsRegistry.addDocView({
title: i18n.translate('kbn.discover.docViews.table.tableTitle', {
defaultMessage: 'Table',
}),
order: 10,
component: DocViewTable,
});
this.docViewsRegistry.addDocView({
title: i18n.translate('kbn.discover.docViews.json.jsonTitle', {
defaultMessage: 'JSON',
}),
order: 20,
component: JsonCodeBlock,
});
plugins.discover.docViews.setAngularInjectorGetter(this.getEmbeddableInjector);
plugins.kibanaLegacy.registerLegacyApp({
id: 'discover',
title: 'Discover',
Expand Down Expand Up @@ -172,14 +146,10 @@ export class DiscoverPlugin implements Plugin<DiscoverSetup, DiscoverStart> {
},
});
registerFeature(plugins.home);

this.registerEmbeddable(core, plugins);
return {
addDocView: this.docViewsRegistry.addDocView.bind(this.docViewsRegistry),
};
}

start(core: CoreStart, plugins: DiscoverStartPlugins): DiscoverStart {
start(core: CoreStart, plugins: DiscoverStartPlugins) {
// we need to register the application service at setup, but to render it
// there are some start dependencies necessary, for this reason
// initializeInnerAngular + initializeServices are assigned at start and used
Expand All @@ -198,7 +168,7 @@ export class DiscoverPlugin implements Plugin<DiscoverSetup, DiscoverStart> {
if (this.servicesInitialized) {
return { core, plugins };
}
const services = await buildServices(core, plugins, this.docViewsRegistry!);
const services = await buildServices(core, plugins);
setServices(services);
this.servicesInitialized = true;

Expand Down
11 changes: 11 additions & 0 deletions src/legacy/ui/public/new_platform/new_platform.karma_mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ export const npSetup = {
}),
},
},
discover: {
docViews: {
addDocView: sinon.fake(),
setAngularInjectorGetter: sinon.fake(),
},
},
visTypeVega: {
config: sinon.fake(),
},
Expand Down Expand Up @@ -459,6 +465,11 @@ export const npStart = {
useChartsTheme: sinon.fake(),
},
},
discover: {
docViews: {
DocViewer: () => null,
},
},
},
};

Expand Down
3 changes: 3 additions & 0 deletions src/legacy/ui/public/new_platform/new_platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import {
NavigationPublicPluginStart,
} from '../../../../plugins/navigation/public';
import { VisTypeVegaSetup } from '../../../../plugins/vis_type_vega/public';
import { DiscoverSetup, DiscoverStart } from '../../../../plugins/discover/public';

export interface PluginsSetup {
bfetch: BfetchPublicSetup;
Expand All @@ -83,6 +84,7 @@ export interface PluginsSetup {
advancedSettings: AdvancedSettingsSetup;
management: ManagementSetup;
visTypeVega: VisTypeVegaSetup;
discover: DiscoverSetup;
telemetry?: TelemetryPluginSetup;
}

Expand All @@ -100,6 +102,7 @@ export interface PluginsStart {
share: SharePluginStart;
management: ManagementStart;
advancedSettings: AdvancedSettingsStart;
discover: DiscoverStart;
telemetry?: TelemetryPluginStart;
}

Expand Down
6 changes: 6 additions & 0 deletions src/plugins/discover/kibana.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"id": "discover",
"version": "kibana",
"server": false,
"ui": true
}
1 change: 1 addition & 0 deletions src/plugins/discover/public/components/_index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import 'doc_viewer/index';
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,33 @@ import { mount, shallow } from 'enzyme';
import { DocViewer } from './doc_viewer';
// @ts-ignore
import { findTestSubject } from '@elastic/eui/lib/test';
import { getServices } from '../../../kibana_services';
import { getDocViewsRegistry } from '../../services';
import { DocViewRenderProps } from '../../doc_views/doc_views_types';

jest.mock('../../../kibana_services', () => {
jest.mock('../../services', () => {
let registry: any[] = [];
return {
getServices: () => ({
docViewsRegistry: {
addDocView(view: any) {
registry.push(view);
},
getDocViewsSorted() {
return registry;
},
getDocViewsRegistry: () => ({
addDocView(view: any) {
registry.push(view);
},
getDocViewsSorted() {
return registry;
},
resetRegistry: () => {
registry = [];
},
}),
formatMsg: (x: any) => String(x),
formatStack: (x: any) => String(x),
};
});

beforeEach(() => {
(getServices() as any).resetRegistry();
(getDocViewsRegistry() as any).resetRegistry();
jest.clearAllMocks();
});

test('Render <DocViewer/> with 3 different tabs', () => {
const registry = getServices().docViewsRegistry;
const registry = getDocViewsRegistry();
registry.addDocView({ order: 10, title: 'Render function', render: jest.fn() });
registry.addDocView({ order: 20, title: 'React component', component: () => <div>test</div> });
registry.addDocView({ order: 30, title: 'Invalid doc view' });
Expand All @@ -69,7 +65,7 @@ test('Render <DocViewer/> with 1 tab displaying error message', () => {
return null;
}

const registry = getServices().docViewsRegistry;
const registry = getDocViewsRegistry();
registry.addDocView({
order: 10,
title: 'React component',
Expand Down
Loading

0 comments on commit 7d32617

Please sign in to comment.