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

[Logs + Metrics UI] Clean up async plugin initialization #67654

Merged
Merged
Show file tree
Hide file tree
Changes from 16 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
Expand Up @@ -3,13 +3,8 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { History } from 'history';

export * from './logs_router';
export * from './metrics_router';
import { Expressions } from './expression';

interface RouterProps {
history: History;
}

export type AppRouter = React.FC<RouterProps>;
// eslint-disable-next-line import/no-default-export
export default Expressions;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { i18n } from '@kbn/i18n';
import { isNumber } from 'lodash';
import {
MetricExpressionParams,
Comparator,
Expand Down Expand Up @@ -106,3 +105,5 @@ export function validateMetricThreshold({

return validationResult;
}

const isNumber = (value: unknown): value is number => typeof value === 'number';
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { i18n } from '@kbn/i18n';
import React from 'react';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { AlertTypeModel } from '../../../../triggers_actions_ui/public/types';
import { Expressions } from './components/expression';
import { validateMetricThreshold } from './components/validation';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { METRIC_THRESHOLD_ALERT_TYPE_ID } from '../../../server/lib/alerting/metric_threshold/types';
Expand All @@ -18,7 +18,7 @@ export function createMetricThresholdAlertType(): AlertTypeModel {
defaultMessage: 'Metric threshold',
}),
iconClass: 'bell',
alertParamsExpression: Expressions,
alertParamsExpression: React.lazy(() => import('./components/expression_lazy')),
validate: validateMetricThreshold,
defaultActionMessage: i18n.translate(
'xpack.infra.metrics.alerting.threshold.defaultActionMessage',
Expand Down
47 changes: 47 additions & 0 deletions x-pack/plugins/infra/public/apps/common_providers.tsx
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;
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import { CoreStart } from 'kibana/public';
import { ApolloClient } from 'apollo-client';
import {
useUiSetting$,
KibanaContextProvider,
} from '../../../../../src/plugins/kibana_react/public';
import { TriggersActionsProvider } from '../utils/triggers_actions_context';
import { ClientPluginDeps } from '../types';
import { TriggersAndActionsUIPublicPluginStart } from '../../../triggers_actions_ui/public';
import { ApolloClientContext } from '../utils/apollo_context';
import { EuiThemeProvider } from '../../../observability/public';
import { NavigationWarningPromptProvider } from '../utils/navigation_warning_prompt';

export const CommonInfraProviders: React.FC<{
apolloClient: ApolloClient<{}>;
triggersActionsUI: TriggersAndActionsUIPublicPluginStart;
}> = ({ apolloClient, children, triggersActionsUI }) => {
const [darkMode] = useUiSetting$<boolean>('theme:darkMode');

return (
<TriggersActionsProvider triggersActionsUI={triggersActionsUI}>
<ApolloClientContext.Provider value={apolloClient}>
<EuiThemeProvider darkMode={darkMode}>
<NavigationWarningPromptProvider>{children}</NavigationWarningPromptProvider>
</EuiThemeProvider>
</ApolloClientContext.Provider>
</TriggersActionsProvider>
);
};

export const CoreProviders: React.FC<{
core: CoreStart;
plugins: ClientPluginDeps;
}> = ({ children, core, plugins }) => {
return (
<KibanaContextProvider services={{ ...core, ...plugins }}>
<core.i18n.Context>{children}</core.i18n.Context>
</KibanaContextProvider>
);
};
13 changes: 13 additions & 0 deletions x-pack/plugins/infra/public/apps/common_styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export const CONTAINER_CLASSNAME = 'infra-container-element';

export const prepareMountElement = (element: HTMLElement) => {
// Ensure the element we're handed from application mounting is assigned a class
// for our index.scss styles to apply to.
element.classList.add(CONTAINER_CLASSNAME);
};
98 changes: 98 additions & 0 deletions x-pack/plugins/infra/public/apps/legacy_app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { EuiErrorBoundary } from '@elastic/eui';
import { createBrowserHistory, History } from 'history';
import { AppMountParameters } from 'kibana/public';
import React from 'react';
import ReactDOM from 'react-dom';
import { Route, RouteProps, Router, Switch } from 'react-router-dom';
import url from 'url';

// This exists purely to facilitate legacy app/infra URL redirects.
// It will be removed in 8.0.0.
export async function renderApp({ element }: AppMountParameters) {
const history = createBrowserHistory();

ReactDOM.render(<LegacyApp history={history} />, element);

return () => {
ReactDOM.unmountComponentAtNode(element);
};
}

const LegacyApp: React.FunctionComponent<{ history: History<unknown> }> = ({ history }) => {
return (
<EuiErrorBoundary>
<Router history={history}>
<Switch>
<Route
path={'/'}
render={({ location }: RouteProps) => {
if (!location) {
return null;
}

let nextPath = '';
let nextBasePath = '';
let nextSearch;

if (
location.hash.indexOf('#infrastructure') > -1 ||
location.hash.indexOf('#/infrastructure') > -1
) {
nextPath = location.hash.replace(
new RegExp(
'#infrastructure/|#/infrastructure/|#/infrastructure|#infrastructure',
'g'
),
''
);
nextBasePath = location.pathname.replace('app/infra', 'app/metrics');
} else if (
location.hash.indexOf('#logs') > -1 ||
location.hash.indexOf('#/logs') > -1
) {
nextPath = location.hash.replace(
new RegExp('#logs/|#/logs/|#/logs|#logs', 'g'),
''
);
nextBasePath = location.pathname.replace('app/infra', 'app/logs');
} else {
// This covers /app/infra and /app/infra/home (both of which used to render
// the metrics inventory page)
nextPath = 'inventory';
nextBasePath = location.pathname.replace('app/infra', 'app/metrics');
nextSearch = undefined;
}

// app/inra#infrastructure/metrics/:type/:node was changed to app/metrics/detail/:type/:node, this
weltenwort marked this conversation as resolved.
Show resolved Hide resolved
// accounts for that edge case
nextPath = nextPath.replace('metrics/', 'detail/');

// Query parameters (location.search) will arrive as part of location.hash and not location.search
const nextPathParts = nextPath.split('?');
nextPath = nextPathParts[0];
nextSearch = nextPathParts[1] ? nextPathParts[1] : undefined;

let nextUrl = url.format({
pathname: `${nextBasePath}/${nextPath}`,
hash: undefined,
search: nextSearch,
});

nextUrl = nextUrl.replace('//', '/');

window.location.href = nextUrl;

return null;
}}
/>
</Switch>
</Router>
</EuiErrorBoundary>
);
};
66 changes: 66 additions & 0 deletions x-pack/plugins/infra/public/apps/logs_app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { ApolloClient } from 'apollo-client';
import { History } from 'history';
import { CoreStart } from 'kibana/public';
import React from 'react';
import ReactDOM from 'react-dom';
import { Route, Router, Switch } from 'react-router-dom';
import { AppMountParameters } from '../../../../../src/core/public';
import '../index.scss';
import { NotFoundPage } from '../pages/404';
import { LinkToLogsPage } from '../pages/link_to/link_to_logs';
import { LogsPage } from '../pages/logs';
import { ClientPluginDeps } from '../types';
import { createApolloClient } from '../utils/apollo_client';
import { CommonInfraProviders, CoreProviders } from './common_providers';
import { prepareMountElement } from './common_styles';

export const renderApp = (
core: CoreStart,
plugins: ClientPluginDeps,
{ element, history }: AppMountParameters
) => {
const apolloClient = createApolloClient(core.http.fetch);
weltenwort marked this conversation as resolved.
Show resolved Hide resolved

prepareMountElement(element);

ReactDOM.render(
<LogsApp apolloClient={apolloClient} core={core} history={history} plugins={plugins} />,
element
);

return () => {
ReactDOM.unmountComponentAtNode(element);
};
};

const LogsApp: React.FC<{
apolloClient: ApolloClient<{}>;
core: CoreStart;
history: History<unknown>;
plugins: ClientPluginDeps;
}> = ({ apolloClient, core, history, plugins }) => {
const uiCapabilities = core.application.capabilities;

return (
<CoreProviders core={core} plugins={plugins}>
<CommonInfraProviders
apolloClient={apolloClient}
triggersActionsUI={plugins.triggers_actions_ui}
>
<Router history={history}>
<Switch>
<Route path="/link-to" component={LinkToLogsPage} />
{uiCapabilities?.logs?.show && <Route path="/" component={LogsPage} />}
<Route component={NotFoundPage} />
</Switch>
</Router>
</CommonInfraProviders>
</CoreProviders>
);
};
82 changes: 82 additions & 0 deletions x-pack/plugins/infra/public/apps/metrics_app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { ApolloClient } from 'apollo-client';
import { History } from 'history';
import { CoreStart } from 'kibana/public';
import React from 'react';
import ReactDOM from 'react-dom';
import { Route, Router, Switch } from 'react-router-dom';
import { AppMountParameters } from '../../../../../src/core/public';
import '../index.scss';
import { NotFoundPage } from '../pages/404';
import { LinkToMetricsPage } from '../pages/link_to/link_to_metrics';
import { InfrastructurePage } from '../pages/metrics';
import { MetricDetail } from '../pages/metrics/metric_detail';
import { ClientPluginDeps } from '../types';
import { createApolloClient } from '../utils/apollo_client';
import { RedirectWithQueryParams } from '../utils/redirect_with_query_params';
import { CommonInfraProviders, CoreProviders } from './common_providers';
import { prepareMountElement } from './common_styles';

export const renderApp = (
core: CoreStart,
plugins: ClientPluginDeps,
{ element, history }: AppMountParameters
) => {
const apolloClient = createApolloClient(core.http.fetch);

prepareMountElement(element);

ReactDOM.render(
<MetricsApp apolloClient={apolloClient} core={core} history={history} plugins={plugins} />,
element
);

return () => {
ReactDOM.unmountComponentAtNode(element);
};
};

const MetricsApp: React.FC<{
apolloClient: ApolloClient<{}>;
core: CoreStart;
history: History<unknown>;
plugins: ClientPluginDeps;
}> = ({ apolloClient, core, history, plugins }) => {
const uiCapabilities = core.application.capabilities;

return (
<CoreProviders core={core} plugins={plugins}>
<CommonInfraProviders
apolloClient={apolloClient}
triggersActionsUI={plugins.triggers_actions_ui}
>
<Router history={history}>
<Switch>
<Route path="/link-to" component={LinkToMetricsPage} />
{uiCapabilities?.infrastructure?.show && (
<RedirectWithQueryParams from="/" exact={true} to="/inventory" />
)}
{uiCapabilities?.infrastructure?.show && (
<RedirectWithQueryParams from="/snapshot" exact={true} to="/inventory" />
)}
{uiCapabilities?.infrastructure?.show && (
<RedirectWithQueryParams from="/metrics-explorer" exact={true} to="/explorer" />
)}
{uiCapabilities?.infrastructure?.show && (
<Route path="/detail/:type/:node" component={MetricDetail} />
)}
{uiCapabilities?.infrastructure?.show && (
<Route path="/" component={InfrastructurePage} />
)}
<Route component={NotFoundPage} />
</Switch>
</Router>
</CommonInfraProviders>
</CoreProviders>
);
};
Loading