kibana/x-pack/plugins/monitoring/public/legacy_shims.ts
Chris Roberson 06b1820df7
[Monitoring] Out of the box alerting (#68805)
* First draft, not quite working but a good start

* More working

* Support configuring throttle

* Get the other alerts working too

* More

* Separate into individual files

* Menu support as well as better integration in existing UIs

* Red borders!

* New overview style, and renamed alert

* more visual updates

* Update cpu usage and improve settings configuration in UI

* Convert cluster health and license expiration alert to use legacy data model

* Remove most of the custom UI and use the flyout

* Add the actual alerts

* Remove more code

* Fix formatting

* Fix up some errors

* Remove unnecessary code

* Updates

* add more links here

* Fix up linkage

* Added nodes changed alert

* Most of the version mismatch working

* Add kibana mismatch

* UI tweaks

* Add timestamp

* Support actions in the enable api

* Move this around

* Better support for changing legacy alerts

* Add missing files

* Update alerts

* Enable alerts whenever any page is visited in SM

* Tweaks

* Use more practical default

* Remove the buggy renderer and ensure setup mode can show all alerts

* Updates

* Remove unnecessary code

* Remove some dead code

* Cleanup

* Fix snapshot

* Fixes

* Fixes

* Fix test

* Add alerts to kibana and logstash listing pages

* Fix test

* Add disable/mute options

* Tweaks

* Fix linting

* Fix i18n

* Adding a couple tests

* Fix localization

* Use http

* Ensure we properly handle when an alert is resolved

* Fix tests

* Hide legacy alerts if not the right license

* Design tweaks

* Fix tests

* PR feedback

* Moar tests

* Fix i18n

* Ensure we have a control over the messaging

* Fix translations

* Tweaks

* More localization

* Copy changes

* Type
2020-07-14 17:50:22 -04:00

132 lines
4.7 KiB
TypeScript

/*
* 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 { CoreStart, HttpSetup, IUiSettingsClient } from 'kibana/public';
import angular from 'angular';
import { Observable } from 'rxjs';
import { HttpRequestInit } from '../../../../src/core/public';
import { MonitoringStartPluginDependencies } from './types';
import { TriggersAndActionsUIPublicPluginSetup } from '../../triggers_actions_ui/public';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { TypeRegistry } from '../../triggers_actions_ui/public/application/type_registry';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { ActionTypeModel, AlertTypeModel } from '../../triggers_actions_ui/public/types';
interface BreadcrumbItem {
['data-test-subj']?: string;
href?: string;
text: string;
}
export interface KFetchQuery {
[key: string]: string | number | boolean | undefined;
}
export interface KFetchOptions extends HttpRequestInit {
pathname: string;
query?: KFetchQuery;
asSystemRequest?: boolean;
}
export interface KFetchKibanaOptions {
prependBasePath?: boolean;
}
export interface IShims {
toastNotifications: CoreStart['notifications']['toasts'];
capabilities: CoreStart['application']['capabilities'];
getAngularInjector: () => angular.auto.IInjectorService;
getBasePath: () => string;
getInjected: (name: string, defaultValue?: unknown) => unknown;
breadcrumbs: {
set: (breadcrumbs: BreadcrumbItem[]) => void;
update: (breadcrumbs?: BreadcrumbItem[]) => void;
};
I18nContext: CoreStart['i18n']['Context'];
docLinks: CoreStart['docLinks'];
docTitle: CoreStart['chrome']['docTitle'];
timefilter: MonitoringStartPluginDependencies['data']['query']['timefilter']['timefilter'];
actionTypeRegistry: TypeRegistry<ActionTypeModel>;
alertTypeRegistry: TypeRegistry<AlertTypeModel>;
uiSettings: IUiSettingsClient;
http: HttpSetup;
kfetch: (
{ pathname, ...options }: KFetchOptions,
kfetchOptions?: KFetchKibanaOptions | undefined
) => Promise<any>;
isCloud: boolean;
triggersActionsUi: TriggersAndActionsUIPublicPluginSetup;
}
export class Legacy {
private static _shims: IShims;
public static init(
{ core, data, isCloud, triggersActionsUi }: MonitoringStartPluginDependencies,
ngInjector: angular.auto.IInjectorService
) {
this._shims = {
toastNotifications: core.notifications.toasts,
capabilities: core.application.capabilities,
getAngularInjector: (): angular.auto.IInjectorService => ngInjector,
getBasePath: (): string => core.http.basePath.get(),
getInjected: (name: string, defaultValue?: unknown): string | unknown =>
core.injectedMetadata.getInjectedVar(name, defaultValue),
breadcrumbs: {
set: (breadcrumbs: BreadcrumbItem[]) => this._shims.breadcrumbs.update(breadcrumbs),
update: (breadcrumbs?: BreadcrumbItem[]) => {
if (!breadcrumbs) {
const currentBreadcrumbs: Observable<any> & {
value?: BreadcrumbItem[];
} = core.chrome.getBreadcrumbs$()?.source;
breadcrumbs = currentBreadcrumbs?.value;
}
const globalStateStr = location.hash.split('?')[1];
if (
!breadcrumbs?.length ||
globalStateStr?.indexOf('_g') !== 0 ||
breadcrumbs[0].href?.split('?')[1] === globalStateStr
) {
return;
}
breadcrumbs.forEach((breadcrumb: BreadcrumbItem) => {
const breadcrumbHref = breadcrumb.href?.split('?')[0];
if (breadcrumbHref) {
breadcrumb.href = `${breadcrumbHref}?${globalStateStr}`;
}
});
core.chrome.setBreadcrumbs(breadcrumbs.slice(0));
},
},
I18nContext: core.i18n.Context,
docLinks: core.docLinks,
docTitle: core.chrome.docTitle,
timefilter: data.query.timefilter.timefilter,
actionTypeRegistry: triggersActionsUi?.actionTypeRegistry,
alertTypeRegistry: triggersActionsUi?.alertTypeRegistry,
uiSettings: core.uiSettings,
http: core.http,
kfetch: async (
{ pathname, ...options }: KFetchOptions,
kfetchOptions?: KFetchKibanaOptions
) =>
await core.http.fetch(pathname, {
prependBasePath: kfetchOptions?.prependBasePath,
...options,
}),
isCloud,
triggersActionsUi,
};
}
public static get shims(): Readonly<IShims> {
if (!Legacy._shims) {
throw new Error('Legacy needs to be initiated with Legacy.init(...) before use');
}
return Legacy._shims;
}
}