Skip to content

Commit

Permalink
[Enterprise Search] Create reusable EntSearchLogStream component (#10…
Browse files Browse the repository at this point in the history
…5804)

* Set up Kibana dependencies required by LogStream component

- notably, `infra` and `data` - @see https://github.com/elastic/kibana/blob/master/x-pack/plugins/infra/public/components/log_stream/log_stream.stories.mdx#prerequisites

- tsconfig.json note - I believe I missed kibana_react from when we previously started requiring it for KibanaPageTemplate. Because LogStream requires it for KibanaContextProvider anyway I decided to add as a reference just in case

* Set up log source configuration for ent search logs

@see https://github.com/elastic/kibana/blob/master/x-pack/plugins/infra/public/components/log_stream/log_stream.stories.mdx#with-a-source-configuration

or, monitoring plugin also has example usage

* Set up providers required by the LogStream component

@see https://github.com/elastic/kibana/blob/master/x-pack/plugins/infra/public/components/log_stream/log_stream.stories.mdx#prerequisites

Note: there's some overlap in KibanaContextProvider with KibanaLogic that may be worth investigating/DRYing out in the future

* Create reusable EntSearchLogStream component

- light wrapper over LogStream with certain prepopulated defaults

+ Update LogStreamProps from infra team to be exported publicly for reuse (eslint will error otherwise)

* Fix bad type export

- thanks @afgomez!!

* Fix failing security_only nav_links test

- which was caused by `spaces` being required by infra but optional for our plugin. I moved `spaces` to required by `enterprise_search for clarity.

- I'm still not sure I actually fixed the nav_links test correctly. I have almost no memory of adding those lines 12 months ago 🙈

* Fix spaces typing

- remove `?` notation now that it's a required and non-optional plugin
+ reorder required plugins slightly

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
Constance and kibanamachine authored Jul 20, 2021
1 parent a1f097e commit bc4928f
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 15 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/enterprise_search/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,5 @@ export const ERROR_CONNECTING_HEADER = 'x-ent-search-error-connecting';
export const READ_ONLY_MODE_HEADER = 'x-ent-search-read-only-mode';

export const ENTERPRISE_SEARCH_KIBANA_COOKIE = '_enterprise_search';

export const LOGS_SOURCE_ID = 'ent-search-logs';
4 changes: 2 additions & 2 deletions x-pack/plugins/enterprise_search/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"id": "enterpriseSearch",
"version": "kibana",
"kibanaVersion": "kibana",
"requiredPlugins": ["features", "licensing", "charts"],
"requiredPlugins": ["features", "spaces", "licensing", "data", "charts", "infra"],
"configPath": ["enterpriseSearch"],
"optionalPlugins": ["usageCollection", "security", "home", "spaces", "cloud"],
"optionalPlugins": ["usageCollection", "security", "home", "cloud"],
"server": true,
"ui": true,
"requiredBundles": ["home", "kibanaReact"],
Expand Down
18 changes: 12 additions & 6 deletions x-pack/plugins/enterprise_search/public/applications/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { Store } from 'redux';
import { I18nProvider } from '@kbn/i18n/react';

import { AppMountParameters, CoreStart } from '../../../../../src/core/public';
import { EuiThemeProvider } from '../../../../../src/plugins/kibana_react/common';
import { KibanaContextProvider } from '../../../../../src/plugins/kibana_react/public';
import { InitialAppData } from '../../common/types';
import { PluginsStart, ClientConfigType, ClientData } from '../plugin';

Expand Down Expand Up @@ -68,12 +70,16 @@ export const renderApp = (

ReactDOM.render(
<I18nProvider>
<Provider store={store}>
<Router history={params.history}>
<App {...initialData} />
<Toasts />
</Router>
</Provider>
<EuiThemeProvider>
<KibanaContextProvider services={{ ...core, ...plugins }}>
<Provider store={store}>
<Router history={params.history}>
<App {...initialData} />
<Toasts />
</Router>
</Provider>
</KibanaContextProvider>
</EuiThemeProvider>
</I18nProvider>,
params.element
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export { EntSearchLogStream } from './log_stream';
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';

import { shallow } from 'enzyme';

import { LogStream } from '../../../../../infra/public';

import { EntSearchLogStream } from './';

describe('EntSearchLogStream', () => {
const mockDateNow = jest.spyOn(global.Date, 'now').mockReturnValue(160000000);

describe('renders with default props', () => {
const wrapper = shallow(<EntSearchLogStream />);

it('renders a LogStream component', () => {
expect(wrapper.type()).toEqual(LogStream);
});

it('renders with the enterprise search log source ID', () => {
expect(wrapper.prop('sourceId')).toEqual('ent-search-logs');
});

it('renders with a default last-24-hours timestamp if no timestamp is passed', () => {
expect(wrapper.prop('startTimestamp')).toEqual(73600000);
expect(wrapper.prop('endTimestamp')).toEqual(160000000);
});
});

describe('renders custom props', () => {
it('overrides the default props', () => {
const wrapper = shallow(
<EntSearchLogStream sourceId="test" startTimestamp={1} endTimestamp={2} />
);

expect(wrapper.prop('sourceId')).toEqual('test');
expect(wrapper.prop('startTimestamp')).toEqual(1);
expect(wrapper.prop('endTimestamp')).toEqual(2);
});

it('allows passing a custom hoursAgo that modifies the default start timestamp', () => {
const wrapper = shallow(<EntSearchLogStream hoursAgo={1} />);

expect(wrapper.prop('startTimestamp')).toEqual(156400000);
expect(wrapper.prop('endTimestamp')).toEqual(160000000);
});

it('allows passing any prop that the LogStream component takes', () => {
const wrapper = shallow(
<EntSearchLogStream
height={500}
highlight="some-log-id"
columns={[
{ type: 'timestamp', header: 'Timestamp' },
{ type: 'field', field: 'log.level', header: 'Log level', width: 300 },
]}
filters={[]}
/>
);

expect(wrapper.prop('height')).toEqual(500);
expect(wrapper.prop('highlight')).toEqual('some-log-id');
expect(wrapper.prop('columns')).toBeTruthy();
expect(wrapper.prop('filters')).toEqual([]);
});
});

afterAll(() => mockDateNow.mockRestore());
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';

import { LogStream, LogStreamProps } from '../../../../../infra/public';

import { LOGS_SOURCE_ID } from '../../../../common/constants';

/*
* EnterpriseSearchLogStream is a light wrapper on top of infra's embeddable LogStream component.
* It prepopulates our log source ID (set in server/plugin.ts) and sets a basic 24-hours-ago
* default for timestamps. All other props get passed as-is to the underlying LogStream.
*
* Documentation links for reference:
* - https://github.com/elastic/kibana/blob/master/x-pack/plugins/infra/public/components/log_stream/log_stream.stories.mdx
* - Run `yarn storybook infra` for live docs
*/

interface Props extends Omit<LogStreamProps, 'startTimestamp' | 'endTimestamp'> {
startTimestamp?: LogStreamProps['startTimestamp'];
endTimestamp?: LogStreamProps['endTimestamp'];
hoursAgo?: number;
}

export const EntSearchLogStream: React.FC<Props> = ({
startTimestamp,
endTimestamp,
hoursAgo = 24,
...props
}) => {
if (!endTimestamp) endTimestamp = Date.now();
if (!startTimestamp) startTimestamp = endTimestamp - hoursAgo * 60 * 60 * 1000;

return (
<LogStream
sourceId={LOGS_SOURCE_ID}
startTimestamp={startTimestamp}
endTimestamp={endTimestamp}
{...props}
/>
);
};
2 changes: 2 additions & 0 deletions x-pack/plugins/enterprise_search/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
DEFAULT_APP_CATEGORIES,
} from '../../../../src/core/public';
import { ChartsPluginStart } from '../../../../src/plugins/charts/public';
import { DataPublicPluginStart } from '../../../../src/plugins/data/public';
import {
FeatureCatalogueCategory,
HomePublicPluginSetup,
Expand Down Expand Up @@ -49,6 +50,7 @@ export interface PluginsStart {
cloud?: CloudSetup;
licensing: LicensingPluginStart;
charts: ChartsPluginStart;
data: DataPublicPluginStart;
security?: SecurityPluginStart;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { callEnterpriseSearchConfigAPI } from './enterprise_search_config_api';
interface CheckAccess {
request: KibanaRequest;
security?: SecurityPluginSetup;
spaces?: SpacesPluginStart;
spaces: SpacesPluginStart;
config: ConfigType;
log: Logger;
}
Expand Down
19 changes: 17 additions & 2 deletions x-pack/plugins/enterprise_search/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import {
} from '../../../../src/core/server';
import { UsageCollectionSetup } from '../../../../src/plugins/usage_collection/server';
import { PluginSetupContract as FeaturesPluginSetup } from '../../features/server';
import { InfraPluginSetup } from '../../infra/server';
import { SecurityPluginSetup } from '../../security/server';
import { SpacesPluginStart } from '../../spaces/server';

import {
ENTERPRISE_SEARCH_PLUGIN,
APP_SEARCH_PLUGIN,
WORKPLACE_SEARCH_PLUGIN,
LOGS_SOURCE_ID,
} from '../common/constants';

import { registerTelemetryUsageCollector as registerASTelemetryUsageCollector } from './collectors/app_search/telemetry';
Expand Down Expand Up @@ -52,10 +54,11 @@ interface PluginsSetup {
usageCollection?: UsageCollectionSetup;
security?: SecurityPluginSetup;
features: FeaturesPluginSetup;
infra: InfraPluginSetup;
}

interface PluginsStart {
spaces?: SpacesPluginStart;
spaces: SpacesPluginStart;
}

export interface RouteDependencies {
Expand All @@ -77,7 +80,7 @@ export class EnterpriseSearchPlugin implements Plugin {

public setup(
{ capabilities, http, savedObjects, getStartServices }: CoreSetup<PluginsStart>,
{ usageCollection, security, features }: PluginsSetup
{ usageCollection, security, features, infra }: PluginsSetup
) {
const config = this.config;
const log = this.logger;
Expand Down Expand Up @@ -159,6 +162,18 @@ export class EnterpriseSearchPlugin implements Plugin {
}
});
registerTelemetryRoute({ ...dependencies, getSavedObjectsService: () => savedObjectsStarted });

/*
* Register logs source configuration, used by LogStream components
* @see https://github.com/elastic/kibana/blob/master/x-pack/plugins/infra/public/components/log_stream/log_stream.stories.mdx#with-a-source-configuration
*/
infra.defineInternalSourceConfiguration(LOGS_SOURCE_ID, {
name: 'Enterprise Search Logs',
logIndices: {
type: 'index_name',
indexName: '.ent-search-*',
},
});
}

public start() {}
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/enterprise_search/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
"references": [
{ "path": "../../../src/core/tsconfig.json" },
{ "path": "../../../src/plugins/charts/tsconfig.json" },
{ "path": "../../../src/plugins/data/tsconfig.json" },
{ "path": "../../../src/plugins/home/tsconfig.json" },
{ "path": "../../../src/plugins/kibana_react/tsconfig.json" },
{ "path": "../../../src/plugins/usage_collection/tsconfig.json" },
{ "path": "../cloud/tsconfig.json" },
{ "path": "../infra/tsconfig.json" },
{ "path": "../features/tsconfig.json" },
{ "path": "../licensing/tsconfig.json" },
{ "path": "../security/tsconfig.json" },
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/infra/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ export type InfraAppId = 'logs' | 'metrics';

// Shared components
export { LazyLogStreamWrapper as LogStream } from './components/log_stream/lazy_log_stream_wrapper';
export type { LogStreamProps } from './components/log_stream';
4 changes: 0 additions & 4 deletions x-pack/test/ui_capabilities/common/nav_links_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ export class NavLinksBuilder {
management: {
app: ['kibana:stack_management'],
},
// TODO: Temp until navLinkIds fix is merged in
appSearch: {
app: ['appSearch', 'workplaceSearch'],
},
kibana: {
app: ['kibana'],
},
Expand Down

0 comments on commit bc4928f

Please sign in to comment.