diff --git a/docs/development/core/public/kibana-plugin-core-public.coresetup.doclinks.md b/docs/development/core/public/kibana-plugin-core-public.coresetup.doclinks.md deleted file mode 100644 index b239319c427fe4..00000000000000 --- a/docs/development/core/public/kibana-plugin-core-public.coresetup.doclinks.md +++ /dev/null @@ -1,13 +0,0 @@ - - -[Home](./index.md) > [kibana-plugin-core-public](./kibana-plugin-core-public.md) > [CoreSetup](./kibana-plugin-core-public.coresetup.md) > [docLinks](./kibana-plugin-core-public.coresetup.doclinks.md) - -## CoreSetup.docLinks property - -[DocLinksSetup](./kibana-plugin-core-public.doclinkssetup.md) - -Signature: - -```typescript -docLinks: DocLinksSetup; -``` diff --git a/docs/development/core/public/kibana-plugin-core-public.coresetup.md b/docs/development/core/public/kibana-plugin-core-public.coresetup.md index 4f981b5a40139f..870fa33dce9000 100644 --- a/docs/development/core/public/kibana-plugin-core-public.coresetup.md +++ b/docs/development/core/public/kibana-plugin-core-public.coresetup.md @@ -18,7 +18,6 @@ export interface CoreSetupApplicationSetup | [ApplicationSetup](./kibana-plugin-core-public.applicationsetup.md) | | [context](./kibana-plugin-core-public.coresetup.context.md) | ContextSetup | [ContextSetup](./kibana-plugin-core-public.contextsetup.md) | -| [docLinks](./kibana-plugin-core-public.coresetup.doclinks.md) | DocLinksSetup | [DocLinksSetup](./kibana-plugin-core-public.doclinkssetup.md) | | [fatalErrors](./kibana-plugin-core-public.coresetup.fatalerrors.md) | FatalErrorsSetup | [FatalErrorsSetup](./kibana-plugin-core-public.fatalerrorssetup.md) | | [getStartServices](./kibana-plugin-core-public.coresetup.getstartservices.md) | StartServicesAccessor<TPluginsStart, TStart> | [StartServicesAccessor](./kibana-plugin-core-public.startservicesaccessor.md) | | [http](./kibana-plugin-core-public.coresetup.http.md) | HttpSetup | [HttpSetup](./kibana-plugin-core-public.httpsetup.md) | diff --git a/docs/development/core/public/kibana-plugin-core-public.doclinkssetup.doc_link_version.md b/docs/development/core/public/kibana-plugin-core-public.doclinkssetup.doc_link_version.md deleted file mode 100644 index c8d13bab92b058..00000000000000 --- a/docs/development/core/public/kibana-plugin-core-public.doclinkssetup.doc_link_version.md +++ /dev/null @@ -1,11 +0,0 @@ - - -[Home](./index.md) > [kibana-plugin-core-public](./kibana-plugin-core-public.md) > [DocLinksSetup](./kibana-plugin-core-public.doclinkssetup.md) > [DOC\_LINK\_VERSION](./kibana-plugin-core-public.doclinkssetup.doc_link_version.md) - -## DocLinksSetup.DOC\_LINK\_VERSION property - -Signature: - -```typescript -readonly DOC_LINK_VERSION: string; -``` diff --git a/docs/development/core/public/kibana-plugin-core-public.doclinkssetup.elastic_website_url.md b/docs/development/core/public/kibana-plugin-core-public.doclinkssetup.elastic_website_url.md deleted file mode 100644 index d8493148bae107..00000000000000 --- a/docs/development/core/public/kibana-plugin-core-public.doclinkssetup.elastic_website_url.md +++ /dev/null @@ -1,11 +0,0 @@ - - -[Home](./index.md) > [kibana-plugin-core-public](./kibana-plugin-core-public.md) > [DocLinksSetup](./kibana-plugin-core-public.doclinkssetup.md) > [ELASTIC\_WEBSITE\_URL](./kibana-plugin-core-public.doclinkssetup.elastic_website_url.md) - -## DocLinksSetup.ELASTIC\_WEBSITE\_URL property - -Signature: - -```typescript -readonly ELASTIC_WEBSITE_URL: string; -``` diff --git a/docs/development/core/public/kibana-plugin-core-public.doclinkssetup.md b/docs/development/core/public/kibana-plugin-core-public.doclinkssetup.md deleted file mode 100644 index 9e7938bd9c8507..00000000000000 --- a/docs/development/core/public/kibana-plugin-core-public.doclinkssetup.md +++ /dev/null @@ -1,21 +0,0 @@ - - -[Home](./index.md) > [kibana-plugin-core-public](./kibana-plugin-core-public.md) > [DocLinksSetup](./kibana-plugin-core-public.doclinkssetup.md) - -## DocLinksSetup interface - - -Signature: - -```typescript -export interface DocLinksSetup -``` - -## Properties - -| Property | Type | Description | -| --- | --- | --- | -| [DOC\_LINK\_VERSION](./kibana-plugin-core-public.doclinkssetup.doc_link_version.md) | string | | -| [ELASTIC\_WEBSITE\_URL](./kibana-plugin-core-public.doclinkssetup.elastic_website_url.md) | string | | -| [links](./kibana-plugin-core-public.doclinkssetup.links.md) | {
readonly dashboard: {
readonly drilldowns: string;
};
readonly filebeat: {
readonly base: string;
readonly installation: string;
readonly configuration: string;
readonly elasticsearchOutput: string;
readonly startup: string;
readonly exportedFields: string;
};
readonly auditbeat: {
readonly base: string;
};
readonly metricbeat: {
readonly base: string;
};
readonly heartbeat: {
readonly base: string;
};
readonly logstash: {
readonly base: string;
};
readonly functionbeat: {
readonly base: string;
};
readonly winlogbeat: {
readonly base: string;
};
readonly aggs: {
readonly date_histogram: string;
readonly date_range: string;
readonly filter: string;
readonly filters: string;
readonly geohash_grid: string;
readonly histogram: string;
readonly ip_range: string;
readonly range: string;
readonly significant_terms: string;
readonly terms: string;
readonly avg: string;
readonly avg_bucket: string;
readonly max_bucket: string;
readonly min_bucket: string;
readonly sum_bucket: string;
readonly cardinality: string;
readonly count: string;
readonly cumulative_sum: string;
readonly derivative: string;
readonly geo_bounds: string;
readonly geo_centroid: string;
readonly max: string;
readonly median: string;
readonly min: string;
readonly moving_avg: string;
readonly percentile_ranks: string;
readonly serial_diff: string;
readonly std_dev: string;
readonly sum: string;
readonly top_hits: string;
};
readonly scriptedFields: {
readonly scriptFields: string;
readonly scriptAggs: string;
readonly painless: string;
readonly painlessApi: string;
readonly painlessSyntax: string;
readonly luceneExpressions: string;
};
readonly indexPatterns: {
readonly loadingData: string;
readonly introduction: string;
};
readonly kibana: string;
readonly siem: {
readonly guide: string;
readonly gettingStarted: string;
};
readonly query: {
readonly luceneQuerySyntax: string;
readonly queryDsl: string;
readonly kueryQuerySyntax: string;
};
readonly date: {
readonly dateMath: string;
};
readonly management: Record<string, string>;
} | | - diff --git a/docs/development/core/public/kibana-plugin-core-public.doclinksstart.doc_link_version.md b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.doc_link_version.md new file mode 100644 index 00000000000000..8140b3fcf380f9 --- /dev/null +++ b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.doc_link_version.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-core-public](./kibana-plugin-core-public.md) > [DocLinksStart](./kibana-plugin-core-public.doclinksstart.md) > [DOC\_LINK\_VERSION](./kibana-plugin-core-public.doclinksstart.doc_link_version.md) + +## DocLinksStart.DOC\_LINK\_VERSION property + +Signature: + +```typescript +readonly DOC_LINK_VERSION: string; +``` diff --git a/docs/development/core/public/kibana-plugin-core-public.doclinksstart.elastic_website_url.md b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.elastic_website_url.md new file mode 100644 index 00000000000000..af770ed3055aad --- /dev/null +++ b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.elastic_website_url.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-core-public](./kibana-plugin-core-public.md) > [DocLinksStart](./kibana-plugin-core-public.doclinksstart.md) > [ELASTIC\_WEBSITE\_URL](./kibana-plugin-core-public.doclinksstart.elastic_website_url.md) + +## DocLinksStart.ELASTIC\_WEBSITE\_URL property + +Signature: + +```typescript +readonly ELASTIC_WEBSITE_URL: string; +``` diff --git a/docs/development/core/public/kibana-plugin-core-public.doclinkssetup.links.md b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.links.md similarity index 94% rename from docs/development/core/public/kibana-plugin-core-public.doclinkssetup.links.md rename to docs/development/core/public/kibana-plugin-core-public.doclinksstart.links.md index 80e2702451d861..a03b1b74fc1ac2 100644 --- a/docs/development/core/public/kibana-plugin-core-public.doclinkssetup.links.md +++ b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.links.md @@ -1,8 +1,8 @@ -[Home](./index.md) > [kibana-plugin-core-public](./kibana-plugin-core-public.md) > [DocLinksSetup](./kibana-plugin-core-public.doclinkssetup.md) > [links](./kibana-plugin-core-public.doclinkssetup.links.md) +[Home](./index.md) > [kibana-plugin-core-public](./kibana-plugin-core-public.md) > [DocLinksStart](./kibana-plugin-core-public.doclinksstart.md) > [links](./kibana-plugin-core-public.doclinksstart.links.md) -## DocLinksSetup.links property +## DocLinksStart.links property Signature: diff --git a/docs/development/core/public/kibana-plugin-core-public.doclinksstart.md b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.md index af2a41b691727d..8f739950d249b9 100644 --- a/docs/development/core/public/kibana-plugin-core-public.doclinksstart.md +++ b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.md @@ -2,11 +2,20 @@ [Home](./index.md) > [kibana-plugin-core-public](./kibana-plugin-core-public.md) > [DocLinksStart](./kibana-plugin-core-public.doclinksstart.md) -## DocLinksStart type +## DocLinksStart interface Signature: ```typescript -export declare type DocLinksStart = DocLinksSetup; +export interface DocLinksStart ``` + +## Properties + +| Property | Type | Description | +| --- | --- | --- | +| [DOC\_LINK\_VERSION](./kibana-plugin-core-public.doclinksstart.doc_link_version.md) | string | | +| [ELASTIC\_WEBSITE\_URL](./kibana-plugin-core-public.doclinksstart.elastic_website_url.md) | string | | +| [links](./kibana-plugin-core-public.doclinksstart.links.md) | {
readonly dashboard: {
readonly drilldowns: string;
};
readonly filebeat: {
readonly base: string;
readonly installation: string;
readonly configuration: string;
readonly elasticsearchOutput: string;
readonly startup: string;
readonly exportedFields: string;
};
readonly auditbeat: {
readonly base: string;
};
readonly metricbeat: {
readonly base: string;
};
readonly heartbeat: {
readonly base: string;
};
readonly logstash: {
readonly base: string;
};
readonly functionbeat: {
readonly base: string;
};
readonly winlogbeat: {
readonly base: string;
};
readonly aggs: {
readonly date_histogram: string;
readonly date_range: string;
readonly filter: string;
readonly filters: string;
readonly geohash_grid: string;
readonly histogram: string;
readonly ip_range: string;
readonly range: string;
readonly significant_terms: string;
readonly terms: string;
readonly avg: string;
readonly avg_bucket: string;
readonly max_bucket: string;
readonly min_bucket: string;
readonly sum_bucket: string;
readonly cardinality: string;
readonly count: string;
readonly cumulative_sum: string;
readonly derivative: string;
readonly geo_bounds: string;
readonly geo_centroid: string;
readonly max: string;
readonly median: string;
readonly min: string;
readonly moving_avg: string;
readonly percentile_ranks: string;
readonly serial_diff: string;
readonly std_dev: string;
readonly sum: string;
readonly top_hits: string;
};
readonly scriptedFields: {
readonly scriptFields: string;
readonly scriptAggs: string;
readonly painless: string;
readonly painlessApi: string;
readonly painlessSyntax: string;
readonly luceneExpressions: string;
};
readonly indexPatterns: {
readonly loadingData: string;
readonly introduction: string;
};
readonly kibana: string;
readonly siem: {
readonly guide: string;
readonly gettingStarted: string;
};
readonly query: {
readonly luceneQuerySyntax: string;
readonly queryDsl: string;
readonly kueryQuerySyntax: string;
};
readonly date: {
readonly dateMath: string;
};
readonly management: Record<string, string>;
} | | + diff --git a/docs/development/core/public/kibana-plugin-core-public.md b/docs/development/core/public/kibana-plugin-core-public.md index dda6b6ac0c60a3..b0612ff4d5b65f 100644 --- a/docs/development/core/public/kibana-plugin-core-public.md +++ b/docs/development/core/public/kibana-plugin-core-public.md @@ -65,7 +65,7 @@ The plugin integrates with the core system via lifecycle events: `setup` | [ContextSetup](./kibana-plugin-core-public.contextsetup.md) | An object that handles registration of context providers and configuring handlers with context. | | [CoreSetup](./kibana-plugin-core-public.coresetup.md) | Core services exposed to the Plugin setup lifecycle | | [CoreStart](./kibana-plugin-core-public.corestart.md) | Core services exposed to the Plugin start lifecycle | -| [DocLinksSetup](./kibana-plugin-core-public.doclinkssetup.md) | | +| [DocLinksStart](./kibana-plugin-core-public.doclinksstart.md) | | | [EnvironmentMode](./kibana-plugin-core-public.environmentmode.md) | | | [ErrorToastOptions](./kibana-plugin-core-public.errortoastoptions.md) | Options available for [IToasts](./kibana-plugin-core-public.itoasts.md) error APIs. | | [FatalErrorInfo](./kibana-plugin-core-public.fatalerrorinfo.md) | Represents the message and stack of a fatal Error | @@ -157,7 +157,6 @@ The plugin integrates with the core system via lifecycle events: `setup` | [ChromeHelpExtensionMenuGitHubLink](./kibana-plugin-core-public.chromehelpextensionmenugithublink.md) | | | [ChromeHelpExtensionMenuLink](./kibana-plugin-core-public.chromehelpextensionmenulink.md) | | | [ChromeNavLinkUpdateableFields](./kibana-plugin-core-public.chromenavlinkupdateablefields.md) | | -| [DocLinksStart](./kibana-plugin-core-public.doclinksstart.md) | | | [FatalErrorsStart](./kibana-plugin-core-public.fatalerrorsstart.md) | FatalErrors stop the Kibana Public Core and displays a fatal error screen with details about the Kibana build and the error. | | [Freezable](./kibana-plugin-core-public.freezable.md) | | | [HandlerContextType](./kibana-plugin-core-public.handlercontexttype.md) | Extracts the type of the first argument of a [HandlerFunction](./kibana-plugin-core-public.handlerfunction.md) to represent the type of the context. | diff --git a/docs/development/core/server/kibana-plugin-core-server.appenderconfigtype.md b/docs/development/core/server/kibana-plugin-core-server.appenderconfigtype.md new file mode 100644 index 00000000000000..9c70e658014b3a --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.appenderconfigtype.md @@ -0,0 +1,12 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [AppenderConfigType](./kibana-plugin-core-server.appenderconfigtype.md) + +## AppenderConfigType type + + +Signature: + +```typescript +export declare type AppenderConfigType = TypeOf; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.coresetup.logging.md b/docs/development/core/server/kibana-plugin-core-server.coresetup.logging.md new file mode 100644 index 00000000000000..12fe49e65d9cad --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.coresetup.logging.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [CoreSetup](./kibana-plugin-core-server.coresetup.md) > [logging](./kibana-plugin-core-server.coresetup.logging.md) + +## CoreSetup.logging property + +[LoggingServiceSetup](./kibana-plugin-core-server.loggingservicesetup.md) + +Signature: + +```typescript +logging: LoggingServiceSetup; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.coresetup.md b/docs/development/core/server/kibana-plugin-core-server.coresetup.md index 30c054345928bf..e9ed5b830b6918 100644 --- a/docs/development/core/server/kibana-plugin-core-server.coresetup.md +++ b/docs/development/core/server/kibana-plugin-core-server.coresetup.md @@ -21,6 +21,7 @@ export interface CoreSetupElasticsearchServiceSetup | [ElasticsearchServiceSetup](./kibana-plugin-core-server.elasticsearchservicesetup.md) | | [getStartServices](./kibana-plugin-core-server.coresetup.getstartservices.md) | StartServicesAccessor<TPluginsStart, TStart> | [StartServicesAccessor](./kibana-plugin-core-server.startservicesaccessor.md) | | [http](./kibana-plugin-core-server.coresetup.http.md) | HttpServiceSetup & {
resources: HttpResources;
} | [HttpServiceSetup](./kibana-plugin-core-server.httpservicesetup.md) | +| [logging](./kibana-plugin-core-server.coresetup.logging.md) | LoggingServiceSetup | [LoggingServiceSetup](./kibana-plugin-core-server.loggingservicesetup.md) | | [metrics](./kibana-plugin-core-server.coresetup.metrics.md) | MetricsServiceSetup | [MetricsServiceSetup](./kibana-plugin-core-server.metricsservicesetup.md) | | [savedObjects](./kibana-plugin-core-server.coresetup.savedobjects.md) | SavedObjectsServiceSetup | [SavedObjectsServiceSetup](./kibana-plugin-core-server.savedobjectsservicesetup.md) | | [status](./kibana-plugin-core-server.coresetup.status.md) | StatusServiceSetup | [StatusServiceSetup](./kibana-plugin-core-server.statusservicesetup.md) | diff --git a/docs/development/core/server/kibana-plugin-core-server.loggerconfigtype.md b/docs/development/core/server/kibana-plugin-core-server.loggerconfigtype.md new file mode 100644 index 00000000000000..c389b7e6279954 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.loggerconfigtype.md @@ -0,0 +1,12 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [LoggerConfigType](./kibana-plugin-core-server.loggerconfigtype.md) + +## LoggerConfigType type + + +Signature: + +```typescript +export declare type LoggerConfigType = TypeOf; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.loggercontextconfiginput.appenders.md b/docs/development/core/server/kibana-plugin-core-server.loggercontextconfiginput.appenders.md new file mode 100644 index 00000000000000..486a5543473ea6 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.loggercontextconfiginput.appenders.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [LoggerContextConfigInput](./kibana-plugin-core-server.loggercontextconfiginput.md) > [appenders](./kibana-plugin-core-server.loggercontextconfiginput.appenders.md) + +## LoggerContextConfigInput.appenders property + +Signature: + +```typescript +appenders?: Record | Map; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.loggercontextconfiginput.loggers.md b/docs/development/core/server/kibana-plugin-core-server.loggercontextconfiginput.loggers.md new file mode 100644 index 00000000000000..64d31f7d55045b --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.loggercontextconfiginput.loggers.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [LoggerContextConfigInput](./kibana-plugin-core-server.loggercontextconfiginput.md) > [loggers](./kibana-plugin-core-server.loggercontextconfiginput.loggers.md) + +## LoggerContextConfigInput.loggers property + +Signature: + +```typescript +loggers?: LoggerConfigType[]; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.loggercontextconfiginput.md b/docs/development/core/server/kibana-plugin-core-server.loggercontextconfiginput.md new file mode 100644 index 00000000000000..fb6922d839cb85 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.loggercontextconfiginput.md @@ -0,0 +1,20 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [LoggerContextConfigInput](./kibana-plugin-core-server.loggercontextconfiginput.md) + +## LoggerContextConfigInput interface + + +Signature: + +```typescript +export interface LoggerContextConfigInput +``` + +## Properties + +| Property | Type | Description | +| --- | --- | --- | +| [appenders](./kibana-plugin-core-server.loggercontextconfiginput.appenders.md) | Record<string, AppenderConfigType> | Map<string, AppenderConfigType> | | +| [loggers](./kibana-plugin-core-server.loggercontextconfiginput.loggers.md) | LoggerConfigType[] | | + diff --git a/docs/development/core/server/kibana-plugin-core-server.loggingservicesetup.configure.md b/docs/development/core/server/kibana-plugin-core-server.loggingservicesetup.configure.md new file mode 100644 index 00000000000000..04a3cf9aff6448 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.loggingservicesetup.configure.md @@ -0,0 +1,42 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [LoggingServiceSetup](./kibana-plugin-core-server.loggingservicesetup.md) > [configure](./kibana-plugin-core-server.loggingservicesetup.configure.md) + +## LoggingServiceSetup.configure() method + +Customizes the logging config for the plugin's context. + +Signature: + +```typescript +configure(config$: Observable): void; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| config$ | Observable<LoggerContextConfigInput> | | + +Returns: + +`void` + +## Remarks + +Assumes that that the `context` property of the individual `logger` items emitted by `config$` are relative to the plugin's logging context (defaults to `plugins.`). + +## Example + +Customize the configuration for the plugins.data.search context. + +```ts +core.logging.configure( + of({ + appenders: new Map(), + loggers: [{ context: 'search', appenders: ['default'] }] + }) +) + +``` + diff --git a/docs/development/core/server/kibana-plugin-core-server.loggingservicesetup.md b/docs/development/core/server/kibana-plugin-core-server.loggingservicesetup.md new file mode 100644 index 00000000000000..010438ce28803a --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.loggingservicesetup.md @@ -0,0 +1,20 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [LoggingServiceSetup](./kibana-plugin-core-server.loggingservicesetup.md) + +## LoggingServiceSetup interface + +Provides APIs to plugins for customizing the plugin's logger. + +Signature: + +```typescript +export interface LoggingServiceSetup +``` + +## Methods + +| Method | Description | +| --- | --- | +| [configure(config$)](./kibana-plugin-core-server.loggingservicesetup.configure.md) | Customizes the logging config for the plugin's context. | + diff --git a/docs/development/core/server/kibana-plugin-core-server.md b/docs/development/core/server/kibana-plugin-core-server.md index 0f1bbbe7176e50..1a03ac5ee3d1ad 100644 --- a/docs/development/core/server/kibana-plugin-core-server.md +++ b/docs/development/core/server/kibana-plugin-core-server.md @@ -108,7 +108,9 @@ The plugin integrates with the core system via lifecycle events: `setup` | [LegacyServiceSetupDeps](./kibana-plugin-core-server.legacyservicesetupdeps.md) | | | [LegacyServiceStartDeps](./kibana-plugin-core-server.legacyservicestartdeps.md) | | | [Logger](./kibana-plugin-core-server.logger.md) | Logger exposes all the necessary methods to log any type of information and this is the interface used by the logging consumers including plugins. | +| [LoggerContextConfigInput](./kibana-plugin-core-server.loggercontextconfiginput.md) | | | [LoggerFactory](./kibana-plugin-core-server.loggerfactory.md) | The single purpose of LoggerFactory interface is to define a way to retrieve a context-based logger instance. | +| [LoggingServiceSetup](./kibana-plugin-core-server.loggingservicesetup.md) | Provides APIs to plugins for customizing the plugin's logger. | | [LogMeta](./kibana-plugin-core-server.logmeta.md) | Contextual metadata | | [MetricsServiceSetup](./kibana-plugin-core-server.metricsservicesetup.md) | APIs to retrieves metrics gathered and exposed by the core platform. | | [NodesVersionCompatibility](./kibana-plugin-core-server.nodesversioncompatibility.md) | | @@ -209,6 +211,7 @@ The plugin integrates with the core system via lifecycle events: `setup` | Type Alias | Description | | --- | --- | +| [AppenderConfigType](./kibana-plugin-core-server.appenderconfigtype.md) | | | [AuthenticationHandler](./kibana-plugin-core-server.authenticationhandler.md) | See [AuthToolkit](./kibana-plugin-core-server.authtoolkit.md). | | [AuthHeaders](./kibana-plugin-core-server.authheaders.md) | Auth Headers map | | [AuthResult](./kibana-plugin-core-server.authresult.md) | | @@ -242,6 +245,7 @@ The plugin integrates with the core system via lifecycle events: `setup` | [KibanaResponseFactory](./kibana-plugin-core-server.kibanaresponsefactory.md) | Creates an object containing request response payload, HTTP headers, error details, and other data transmitted to the client. | | [KnownHeaders](./kibana-plugin-core-server.knownheaders.md) | Set of well-known HTTP headers. | | [LifecycleResponseFactory](./kibana-plugin-core-server.lifecycleresponsefactory.md) | Creates an object containing redirection or error response with error details, HTTP headers, and other data transmitted to the client. | +| [LoggerConfigType](./kibana-plugin-core-server.loggerconfigtype.md) | | | [MIGRATION\_ASSISTANCE\_INDEX\_ACTION](./kibana-plugin-core-server.migration_assistance_index_action.md) | | | [MIGRATION\_DEPRECATION\_LEVEL](./kibana-plugin-core-server.migration_deprecation_level.md) | | | [MutatingOperationRefreshSetting](./kibana-plugin-core-server.mutatingoperationrefreshsetting.md) | Elasticsearch Refresh setting for mutating operation | diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.fieldformats.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.fieldformats.md index d39871b99f7447..b51421741933a8 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.fieldformats.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.fieldformats.md @@ -10,7 +10,6 @@ fieldFormats: { FieldFormat: typeof FieldFormat; FieldFormatsRegistry: typeof FieldFormatsRegistry; - serialize: (agg: import("./search").AggConfig) => import("../../expressions").SerializedFieldFormat; DEFAULT_CONVERTER_COLOR: { range: string; regex: string; diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.fieldformats.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.fieldformats.md index 11f18a195d2716..45fc1a608e8ca3 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.fieldformats.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.fieldformats.md @@ -10,7 +10,6 @@ fieldFormats: { FieldFormatsRegistry: typeof FieldFormatsRegistry; FieldFormat: typeof FieldFormat; - serializeFieldFormat: (agg: import("../public/search").AggConfig) => import("../../expressions").SerializedFieldFormat; BoolFormat: typeof BoolFormat; BytesFormat: typeof BytesFormat; ColorFormat: typeof ColorFormat; diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.plugin.setup.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.plugin.setup.md index 13c69d6bf7548e..a6fdfdf6891c86 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.plugin.setup.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.plugin.setup.md @@ -10,7 +10,7 @@ setup(core: CoreSetup, { usageCollection }: DataPluginSetupDependencies): { search: ISearchSetup; fieldFormats: { - register: (customFieldFormat: import("../public").FieldFormatInstanceType) => number; + register: (customFieldFormat: import("../common").FieldFormatInstanceType) => number; }; }; ``` @@ -27,7 +27,7 @@ setup(core: CoreSetup, { usageCollection }: DataPluginS `{ search: ISearchSetup; fieldFormats: { - register: (customFieldFormat: import("../public").FieldFormatInstanceType) => number; + register: (customFieldFormat: import("../common").FieldFormatInstanceType) => number; }; }` diff --git a/packages/kbn-storybook/lib/webpack.dll.config.js b/packages/kbn-storybook/lib/webpack.dll.config.js index bc871fab471b28..534f503e2956a7 100644 --- a/packages/kbn-storybook/lib/webpack.dll.config.js +++ b/packages/kbn-storybook/lib/webpack.dll.config.js @@ -73,7 +73,6 @@ module.exports = { 'rxjs', 'sinon', 'tinycolor2', - './src/legacy/ui/public/styles/font_awesome.less', './src/legacy/ui/public/styles/bootstrap/bootstrap_light.less', ], plugins: [ diff --git a/src/core/public/chrome/chrome_service.mock.ts b/src/core/public/chrome/chrome_service.mock.ts index 4a79dd8869c1c6..c9a05ff4e08fe1 100644 --- a/src/core/public/chrome/chrome_service.mock.ts +++ b/src/core/public/chrome/chrome_service.mock.ts @@ -74,6 +74,8 @@ const createStartContractMock = () => { setHelpSupportUrl: jest.fn(), getIsNavDrawerLocked$: jest.fn(), getNavType$: jest.fn(), + getCustomNavLink$: jest.fn(), + setCustomNavLink: jest.fn(), }; startContract.navLinks.getAll.mockReturnValue([]); startContract.getBrand$.mockReturnValue(new BehaviorSubject({} as ChromeBrand)); @@ -81,6 +83,7 @@ const createStartContractMock = () => { startContract.getApplicationClasses$.mockReturnValue(new BehaviorSubject(['class-name'])); startContract.getBadge$.mockReturnValue(new BehaviorSubject({} as ChromeBadge)); startContract.getBreadcrumbs$.mockReturnValue(new BehaviorSubject([{} as ChromeBreadcrumb])); + startContract.getCustomNavLink$.mockReturnValue(new BehaviorSubject(undefined)); startContract.getHelpExtension$.mockReturnValue(new BehaviorSubject(undefined)); startContract.getIsNavDrawerLocked$.mockReturnValue(new BehaviorSubject(false)); startContract.getNavType$.mockReturnValue(new BehaviorSubject('modern' as NavType)); diff --git a/src/core/public/chrome/chrome_service.test.ts b/src/core/public/chrome/chrome_service.test.ts index e39733cc10de70..8dc81dceaccd61 100644 --- a/src/core/public/chrome/chrome_service.test.ts +++ b/src/core/public/chrome/chrome_service.test.ts @@ -363,6 +363,27 @@ describe('start', () => { }); }); + describe('custom nav link', () => { + it('updates/emits the current custom nav link', async () => { + const { chrome, service } = await start(); + const promise = chrome.getCustomNavLink$().pipe(toArray()).toPromise(); + + chrome.setCustomNavLink({ title: 'Manage cloud deployment' }); + chrome.setCustomNavLink(undefined); + service.stop(); + + await expect(promise).resolves.toMatchInlineSnapshot(` + Array [ + undefined, + Object { + "title": "Manage cloud deployment", + }, + undefined, + ] + `); + }); + }); + describe('help extension', () => { it('updates/emits the current help extension', async () => { const { chrome, service } = await start(); diff --git a/src/core/public/chrome/chrome_service.tsx b/src/core/public/chrome/chrome_service.tsx index 67cd43f0647e43..0fe3c1f083cf08 100644 --- a/src/core/public/chrome/chrome_service.tsx +++ b/src/core/public/chrome/chrome_service.tsx @@ -34,7 +34,7 @@ import { IUiSettingsClient } from '../ui_settings'; import { KIBANA_ASK_ELASTIC_LINK } from './constants'; import { ChromeDocTitle, DocTitleService } from './doc_title'; import { ChromeNavControls, NavControlsService } from './nav_controls'; -import { ChromeNavLinks, NavLinksService } from './nav_links'; +import { ChromeNavLinks, NavLinksService, ChromeNavLink } from './nav_links'; import { ChromeRecentlyAccessed, RecentlyAccessedService } from './recently_accessed'; import { Header } from './ui'; import { NavType } from './ui/header'; @@ -148,6 +148,7 @@ export class ChromeService { const helpExtension$ = new BehaviorSubject(undefined); const breadcrumbs$ = new BehaviorSubject([]); const badge$ = new BehaviorSubject(undefined); + const customNavLink$ = new BehaviorSubject(undefined); const helpSupportUrl$ = new BehaviorSubject(KIBANA_ASK_ELASTIC_LINK); const isNavDrawerLocked$ = new BehaviorSubject(localStorage.getItem(IS_LOCKED_KEY) === 'true'); @@ -221,6 +222,7 @@ export class ChromeService { badge$={badge$.pipe(takeUntil(this.stop$))} basePath={http.basePath} breadcrumbs$={breadcrumbs$.pipe(takeUntil(this.stop$))} + customNavLink$={customNavLink$.pipe(takeUntil(this.stop$))} kibanaDocLink={docLinks.links.kibana} forceAppSwitcherNavigation$={navLinks.getForceAppSwitcherNavigation$()} helpExtension$={helpExtension$.pipe(takeUntil(this.stop$))} @@ -297,6 +299,12 @@ export class ChromeService { getIsNavDrawerLocked$: () => getIsNavDrawerLocked$, getNavType$: () => getNavType$, + + getCustomNavLink$: () => customNavLink$.pipe(takeUntil(this.stop$)), + + setCustomNavLink: (customNavLink?: ChromeNavLink) => { + customNavLink$.next(customNavLink); + }, }; } @@ -423,6 +431,16 @@ export interface ChromeStart { */ setBreadcrumbs(newBreadcrumbs: ChromeBreadcrumb[]): void; + /** + * Get an observable of the current custom nav link + */ + getCustomNavLink$(): Observable | undefined>; + + /** + * Override the current set of custom nav link + */ + setCustomNavLink(newCustomNavLink?: Partial): void; + /** * Get an observable of the current custom help conttent */ diff --git a/src/core/public/chrome/ui/header/__snapshots__/collapsible_nav.test.tsx.snap b/src/core/public/chrome/ui/header/__snapshots__/collapsible_nav.test.tsx.snap index 9239811df20653..9fee7b50f371b2 100644 --- a/src/core/public/chrome/ui/header/__snapshots__/collapsible_nav.test.tsx.snap +++ b/src/core/public/chrome/ui/header/__snapshots__/collapsible_nav.test.tsx.snap @@ -61,6 +61,64 @@ exports[`CollapsibleNav renders links grouped by category 1`] = ` } } closeNav={[Function]} + customNavLink$={ + BehaviorSubject { + "_isScalar": false, + "_value": Object { + "baseUrl": "/", + "category": undefined, + "data-test-subj": "Custom link", + "href": "Custom link", + "id": "Custom link", + "isActive": true, + "legacy": false, + "title": "Custom link", + }, + "closed": false, + "hasError": false, + "isStopped": false, + "observers": Array [ + Subscriber { + "_parentOrParents": null, + "_subscriptions": Array [ + SubjectSubscription { + "_parentOrParents": [Circular], + "_subscriptions": null, + "closed": false, + "subject": [Circular], + "subscriber": [Circular], + }, + ], + "closed": false, + "destination": SafeSubscriber { + "_complete": undefined, + "_context": [Circular], + "_error": undefined, + "_next": [Function], + "_parentOrParents": null, + "_parentSubscriber": [Circular], + "_subscriptions": null, + "closed": false, + "destination": Object { + "closed": true, + "complete": [Function], + "error": [Function], + "next": [Function], + }, + "isStopped": false, + "syncErrorThrowable": false, + "syncErrorThrown": false, + "syncErrorValue": null, + }, + "isStopped": false, + "syncErrorThrowable": true, + "syncErrorThrown": false, + "syncErrorValue": null, + }, + ], + "thrownError": null, + } + } homeHref="/" id="collapsibe-nav" isLocked={false} @@ -408,6 +466,46 @@ exports[`CollapsibleNav renders links grouped by category 1`] = ` data-test-subj="collapsibleNav" id="collapsibe-nav" > +
+
+ +
+
+
+
+
+ +
+
+
+ +
+ +
+
+ + + +
+
+
+
+
+ +
+
+
+
+
+
    +
  • + +
  • +
+
+
+
+
+
+
+
+
    +
  • + +
  • +
+
+
+
+
+ +
+ +
+
+ +
    + +
  • + +
  • +
    +
+
+
+
+
+
+
+ +
+
{}, closeNav: () => {}, navigateToApp: () => Promise.resolve(), + customNavLink$: new BehaviorSubject(undefined), }; } @@ -120,12 +121,14 @@ describe('CollapsibleNav', () => { mockRecentNavLink({ label: 'recent 1' }), mockRecentNavLink({ label: 'recent 2' }), ]; + const customNavLink = mockLink({ title: 'Custom link' }); const component = mount( ); expect(component).toMatchSnapshot(); diff --git a/src/core/public/chrome/ui/header/collapsible_nav.tsx b/src/core/public/chrome/ui/header/collapsible_nav.tsx index 9494e22920de81..07541b1adff16c 100644 --- a/src/core/public/chrome/ui/header/collapsible_nav.tsx +++ b/src/core/public/chrome/ui/header/collapsible_nav.tsx @@ -30,7 +30,7 @@ import { } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import { groupBy, sortBy } from 'lodash'; -import React, { useRef } from 'react'; +import React, { Fragment, useRef } from 'react'; import { useObservable } from 'react-use'; import * as Rx from 'rxjs'; import { ChromeNavLink, ChromeRecentlyAccessedHistoryItem } from '../..'; @@ -88,6 +88,7 @@ interface Props { onIsLockedUpdate: OnIsLockedUpdate; closeNav: () => void; navigateToApp: InternalApplicationStart['navigateToApp']; + customNavLink$: Rx.Observable; } export function CollapsibleNav({ @@ -105,6 +106,7 @@ export function CollapsibleNav({ }: Props) { const navLinks = useObservable(observables.navLinks$, []).filter((link) => !link.hidden); const recentlyAccessed = useObservable(observables.recentlyAccessed$, []); + const customNavLink = useObservable(observables.customNavLink$, undefined); const appId = useObservable(observables.appId$, ''); const lockRef = useRef(null); const groupedNavLinks = groupBy(navLinks, (link) => link?.category?.id); @@ -134,6 +136,38 @@ export function CollapsibleNav({ isDocked={isLocked} onClose={closeNav} > + {customNavLink && ( + + + + + + + + + + )} + {/* Pinned items */} { const navLinks$ = new BehaviorSubject([ { id: 'kibana', title: 'kibana', baseUrl: '', legacy: false }, ]); + const customNavLink$ = new BehaviorSubject({ + id: 'cloud-deployment-link', + title: 'Manage cloud deployment', + baseUrl: '', + legacy: false, + }); const recentlyAccessed$ = new BehaviorSubject([ { link: '', label: 'dashboard', id: 'dashboard' }, ]); @@ -87,6 +94,7 @@ describe('Header', () => { recentlyAccessed$={recentlyAccessed$} isLocked$={isLocked$} navType$={navType$} + customNavLink$={customNavLink$} /> ); expect(component).toMatchSnapshot(); diff --git a/src/core/public/chrome/ui/header/header.tsx b/src/core/public/chrome/ui/header/header.tsx index d24b342e0386bc..3da3caaaa4a4f8 100644 --- a/src/core/public/chrome/ui/header/header.tsx +++ b/src/core/public/chrome/ui/header/header.tsx @@ -58,6 +58,7 @@ export interface HeaderProps { appTitle$: Observable; badge$: Observable; breadcrumbs$: Observable; + customNavLink$: Observable; homeHref: string; isVisible$: Observable; kibanaDocLink: string; @@ -203,6 +204,7 @@ export function Header({ toggleCollapsibleNavRef.current.focus(); } }} + customNavLink$={observables.customNavLink$} /> ) : ( // TODO #64541 diff --git a/src/core/public/chrome/ui/header/nav_link.tsx b/src/core/public/chrome/ui/header/nav_link.tsx index 969b6728e0263f..6b5cecd138376b 100644 --- a/src/core/public/chrome/ui/header/nav_link.tsx +++ b/src/core/public/chrome/ui/header/nav_link.tsx @@ -35,11 +35,12 @@ function LinkIcon({ url }: { url: string }) { interface Props { link: ChromeNavLink; legacyMode: boolean; - appId: string | undefined; + appId?: string; basePath?: HttpStart['basePath']; dataTestSubj: string; onClick?: Function; navigateToApp: CoreStart['application']['navigateToApp']; + externalLink?: boolean; } // TODO #64541 @@ -54,6 +55,7 @@ export function createEuiListItem({ onClick = () => {}, navigateToApp, dataTestSubj, + externalLink = false, }: Props) { const { legacy, active, id, title, disabled, euiIconType, icon, tooltip } = link; let { href } = link; @@ -69,6 +71,7 @@ export function createEuiListItem({ onClick(event: React.MouseEvent) { onClick(); if ( + !externalLink && // ignore external links !legacyMode && // ignore when in legacy mode !legacy && // ignore links to legacy apps !event.defaultPrevented && // onClick prevented default diff --git a/src/core/public/core_system.ts b/src/core/public/core_system.ts index d6172b77d3ca53..00fabc2b6f2f1c 100644 --- a/src/core/public/core_system.ts +++ b/src/core/public/core_system.ts @@ -163,7 +163,7 @@ export class CoreSystem { i18n: this.i18n.getContext(), }); await this.integrations.setup(); - const docLinks = this.docLinks.setup({ injectedMetadata }); + this.docLinks.setup(); const http = this.http.setup({ injectedMetadata, fatalErrors: this.fatalErrorsSetup }); const uiSettings = this.uiSettings.setup({ http, injectedMetadata }); const notifications = this.notifications.setup({ uiSettings }); @@ -185,7 +185,6 @@ export class CoreSystem { const core: InternalCoreSetup = { application, context, - docLinks, fatalErrors: this.fatalErrorsSetup, http, injectedMetadata, @@ -217,7 +216,7 @@ export class CoreSystem { try { const injectedMetadata = await this.injectedMetadata.start(); const uiSettings = await this.uiSettings.start(); - const docLinks = this.docLinks.start(); + const docLinks = this.docLinks.start({ injectedMetadata }); const http = await this.http.start(); const savedObjects = await this.savedObjects.start({ http }); const i18n = await this.i18n.start(); diff --git a/src/core/public/doc_links/doc_links_service.mock.ts b/src/core/public/doc_links/doc_links_service.mock.ts index 9edcf2e3c79901..105c13f96cef65 100644 --- a/src/core/public/doc_links/doc_links_service.mock.ts +++ b/src/core/public/doc_links/doc_links_service.mock.ts @@ -18,25 +18,23 @@ */ import { injectedMetadataServiceMock } from '../injected_metadata/injected_metadata_service.mock'; -import { DocLinksService, DocLinksSetup, DocLinksStart } from './doc_links_service'; +import { DocLinksService, DocLinksStart } from './doc_links_service'; -const createSetupContractMock = (): DocLinksSetup => { +const createStartContractMock = (): DocLinksStart => { // This service is so simple that we actually use the real implementation const injectedMetadata = injectedMetadataServiceMock.createStartContract(); injectedMetadata.getKibanaBranch.mockReturnValue('mocked-test-branch'); - return new DocLinksService().setup({ injectedMetadata }); + return new DocLinksService().start({ injectedMetadata }); }; -const createStartContractMock: () => DocLinksStart = createSetupContractMock; - type DocLinksServiceContract = PublicMethodsOf; const createMock = (): jest.Mocked => ({ - setup: jest.fn().mockReturnValue(createSetupContractMock()), + setup: jest.fn().mockReturnValue(undefined), start: jest.fn().mockReturnValue(createStartContractMock()), }); export const docLinksServiceMock = { create: createMock, - createSetupContract: createSetupContractMock, + createSetupContract: () => jest.fn(), createStartContract: createStartContractMock, }; diff --git a/src/core/public/doc_links/doc_links_service.test.ts b/src/core/public/doc_links/doc_links_service.test.ts index 4c5d6bcde8b773..c430ae7655040d 100644 --- a/src/core/public/doc_links/doc_links_service.test.ts +++ b/src/core/public/doc_links/doc_links_service.test.ts @@ -20,33 +20,15 @@ import { DocLinksService } from './doc_links_service'; import { injectedMetadataServiceMock } from '../injected_metadata/injected_metadata_service.mock'; -describe('DocLinksService#setup()', () => { +describe('DocLinksService#start()', () => { it('templates the doc links with the branch information from injectedMetadata', () => { const injectedMetadata = injectedMetadataServiceMock.createStartContract(); injectedMetadata.getKibanaBranch.mockReturnValue('test-branch'); const service = new DocLinksService(); - const setup = service.setup({ injectedMetadata }); - expect(setup.DOC_LINK_VERSION).toEqual('test-branch'); - expect(setup.links.kibana).toEqual( + const api = service.start({ injectedMetadata }); + expect(api.DOC_LINK_VERSION).toEqual('test-branch'); + expect(api.links.kibana).toEqual( 'https://www.elastic.co/guide/en/kibana/test-branch/index.html' ); }); }); - -describe('DocLinksService#start()', () => { - it('returns the same data as setup', () => { - const injectedMetadata = injectedMetadataServiceMock.createStartContract(); - injectedMetadata.getKibanaBranch.mockReturnValue('test-branch'); - const service = new DocLinksService(); - const setup = service.setup({ injectedMetadata }); - const start = service.start(); - expect(setup).toEqual(start); - }); - - it('must be called after setup', () => { - const service = new DocLinksService(); - expect(() => { - service.start(); - }).toThrowErrorMatchingInlineSnapshot(`"DocLinksService#setup() must be called first!"`); - }); -}); diff --git a/src/core/public/doc_links/doc_links_service.ts b/src/core/public/doc_links/doc_links_service.ts index f2bc90a5b08d48..0662586797164d 100644 --- a/src/core/public/doc_links/doc_links_service.ts +++ b/src/core/public/doc_links/doc_links_service.ts @@ -20,20 +20,19 @@ import { InjectedMetadataSetup } from '../injected_metadata'; import { deepFreeze } from '../../utils'; -interface SetupDeps { +interface StartDeps { injectedMetadata: InjectedMetadataSetup; } /** @internal */ export class DocLinksService { - private service?: DocLinksSetup; - - public setup({ injectedMetadata }: SetupDeps): DocLinksSetup { + public setup() {} + public start({ injectedMetadata }: StartDeps): DocLinksStart { const DOC_LINK_VERSION = injectedMetadata.getKibanaBranch(); const ELASTIC_WEBSITE_URL = 'https://www.elastic.co/'; const ELASTICSEARCH_DOCS = `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/reference/${DOC_LINK_VERSION}/`; - this.service = deepFreeze({ + return deepFreeze({ DOC_LINK_VERSION, ELASTIC_WEBSITE_URL, links: { @@ -129,21 +128,11 @@ export class DocLinksService { }, }, }); - - return this.service; - } - - public start(): DocLinksStart { - if (!this.service) { - throw new Error(`DocLinksService#setup() must be called first!`); - } - - return this.service; } } /** @public */ -export interface DocLinksSetup { +export interface DocLinksStart { readonly DOC_LINK_VERSION: string; readonly ELASTIC_WEBSITE_URL: string; readonly links: { @@ -236,6 +225,3 @@ export interface DocLinksSetup { readonly management: Record; }; } - -/** @public */ -export type DocLinksStart = DocLinksSetup; diff --git a/src/core/public/doc_links/index.ts b/src/core/public/doc_links/index.ts index fbfa9db5635ddc..fe49d4a7c6a583 100644 --- a/src/core/public/doc_links/index.ts +++ b/src/core/public/doc_links/index.ts @@ -17,4 +17,4 @@ * under the License. */ -export { DocLinksService, DocLinksSetup, DocLinksStart } from './doc_links_service'; +export { DocLinksService, DocLinksStart } from './doc_links_service'; diff --git a/src/core/public/index.ts b/src/core/public/index.ts index 99b75f85340f31..41af0f1b8395fe 100644 --- a/src/core/public/index.ts +++ b/src/core/public/index.ts @@ -67,7 +67,7 @@ import { OverlayStart } from './overlays'; import { Plugin, PluginInitializer, PluginInitializerContext, PluginOpaqueId } from './plugins'; import { UiSettingsState, IUiSettingsClient } from './ui_settings'; import { ApplicationSetup, Capabilities, ApplicationStart } from './application'; -import { DocLinksSetup, DocLinksStart } from './doc_links'; +import { DocLinksStart } from './doc_links'; import { SavedObjectsStart } from './saved_objects'; export { PackageInfo, EnvironmentMode } from '../server/types'; import { @@ -216,8 +216,6 @@ export interface CoreSetup { mockSetupDeps = { application: applicationServiceMock.createInternalSetupContract(), context: contextServiceMock.createSetupContract(), - docLinks: docLinksServiceMock.createSetupContract(), fatalErrors: fatalErrorsServiceMock.createSetupContract(), http: httpServiceMock.createSetupContract(), injectedMetadata: pick(injectedMetadataServiceMock.createStartContract(), 'getInjectedVar'), diff --git a/src/core/public/public.api.md b/src/core/public/public.api.md index 7970d9f3f86bb2..bc11ab57b3ea1b 100644 --- a/src/core/public/public.api.md +++ b/src/core/public/public.api.md @@ -466,6 +466,7 @@ export interface ChromeStart { getBadge$(): Observable; getBrand$(): Observable; getBreadcrumbs$(): Observable; + getCustomNavLink$(): Observable | undefined>; getHelpExtension$(): Observable; getIsNavDrawerLocked$(): Observable; getIsVisible$(): Observable; @@ -478,6 +479,7 @@ export interface ChromeStart { setBadge(badge?: ChromeBadge): void; setBrand(brand: ChromeBrand): void; setBreadcrumbs(newBreadcrumbs: ChromeBreadcrumb[]): void; + setCustomNavLink(newCustomNavLink?: Partial): void; setHelpExtension(helpExtension?: ChromeHelpExtension): void; setHelpSupportUrl(url: string): void; setIsVisible(isVisible: boolean): void; @@ -508,8 +510,6 @@ export interface CoreSetup; @@ -600,7 +600,7 @@ export const DEFAULT_APP_CATEGORIES: Readonly<{ }>; // @public (undocumented) -export interface DocLinksSetup { +export interface DocLinksStart { // (undocumented) readonly DOC_LINK_VERSION: string; // (undocumented) @@ -697,9 +697,6 @@ export interface DocLinksSetup { }; } -// @public (undocumented) -export type DocLinksStart = DocLinksSetup; - // @public (undocumented) export interface EnvironmentMode { // (undocumented) @@ -1594,6 +1591,6 @@ export interface UserProvidedValues { // Warnings were encountered during analysis: // -// src/core/public/core_system.ts:216:21 - (ae-forgotten-export) The symbol "InternalApplicationStart" needs to be exported by the entry point index.d.ts +// src/core/public/core_system.ts:215:21 - (ae-forgotten-export) The symbol "InternalApplicationStart" needs to be exported by the entry point index.d.ts ``` diff --git a/src/core/server/capabilities/integration_tests/capabilities_service.test.ts b/src/core/server/capabilities/integration_tests/capabilities_service.test.ts index 6be9846f5a86a5..b4d620965b0471 100644 --- a/src/core/server/capabilities/integration_tests/capabilities_service.test.ts +++ b/src/core/server/capabilities/integration_tests/capabilities_service.test.ts @@ -20,7 +20,7 @@ import supertest from 'supertest'; import { HttpService, InternalHttpServiceSetup } from '../../http'; import { contextServiceMock } from '../../context/context_service.mock'; -import { loggingServiceMock } from '../../logging/logging_service.mock'; +import { loggingSystemMock } from '../../logging/logging_system.mock'; import { Env } from '../../config'; import { getEnvOptions } from '../../config/__mocks__/env'; import { CapabilitiesService, CapabilitiesSetup } from '..'; @@ -44,7 +44,7 @@ describe('CapabilitiesService', () => { service = new CapabilitiesService({ coreId, env, - logger: loggingServiceMock.create(), + logger: loggingSystemMock.create(), configService: {} as any, }); serviceSetup = await service.setup({ http: httpSetup }); diff --git a/src/core/server/config/config_service.test.ts b/src/core/server/config/config_service.test.ts index 5f28fca1371b08..236cf6579d7c80 100644 --- a/src/core/server/config/config_service.test.ts +++ b/src/core/server/config/config_service.test.ts @@ -28,12 +28,12 @@ import { rawConfigServiceMock } from './raw_config_service.mock'; import { schema } from '@kbn/config-schema'; import { ConfigService, Env } from '.'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; import { getEnvOptions } from './__mocks__/env'; const emptyArgv = getEnvOptions(); const defaultEnv = new Env('/kibana', emptyArgv); -const logger = loggingServiceMock.create(); +const logger = loggingSystemMock.create(); const getRawConfigProvider = (rawConfig: Record) => rawConfigServiceMock.create({ rawConfig }); @@ -443,9 +443,9 @@ test('logs deprecation warning during validation', async () => { return config; }); - loggingServiceMock.clear(logger); + loggingSystemMock.clear(logger); await configService.validate(); - expect(loggingServiceMock.collect(logger).warn).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).warn).toMatchInlineSnapshot(` Array [ Array [ "some deprecation message", diff --git a/src/core/server/config/integration_tests/config_deprecation.test.mocks.ts b/src/core/server/config/integration_tests/config_deprecation.test.mocks.ts index 58b2da926b7c3b..1d42c7667a34d9 100644 --- a/src/core/server/config/integration_tests/config_deprecation.test.mocks.ts +++ b/src/core/server/config/integration_tests/config_deprecation.test.mocks.ts @@ -17,9 +17,9 @@ * under the License. */ -import { loggingServiceMock } from '../../logging/logging_service.mock'; -export const mockLoggingService = loggingServiceMock.create(); -mockLoggingService.asLoggerFactory.mockImplementation(() => mockLoggingService); -jest.doMock('../../logging/logging_service', () => ({ - LoggingService: jest.fn(() => mockLoggingService), +import { loggingSystemMock } from '../../logging/logging_system.mock'; +export const mockLoggingSystem = loggingSystemMock.create(); +mockLoggingSystem.asLoggerFactory.mockImplementation(() => mockLoggingSystem); +jest.doMock('../../logging/logging_system', () => ({ + LoggingSystem: jest.fn(() => mockLoggingSystem), })); diff --git a/src/core/server/config/integration_tests/config_deprecation.test.ts b/src/core/server/config/integration_tests/config_deprecation.test.ts index 3523b074ea5b42..56385f3b171c93 100644 --- a/src/core/server/config/integration_tests/config_deprecation.test.ts +++ b/src/core/server/config/integration_tests/config_deprecation.test.ts @@ -17,8 +17,8 @@ * under the License. */ -import { mockLoggingService } from './config_deprecation.test.mocks'; -import { loggingServiceMock } from '../../logging/logging_service.mock'; +import { mockLoggingSystem } from './config_deprecation.test.mocks'; +import { loggingSystemMock } from '../../logging/logging_system.mock'; import * as kbnTestServer from '../../../../test_utils/kbn_server'; describe('configuration deprecations', () => { @@ -35,7 +35,7 @@ describe('configuration deprecations', () => { await root.setup(); - const logs = loggingServiceMock.collect(mockLoggingService); + const logs = loggingSystemMock.collect(mockLoggingSystem); const warnings = logs.warn.flatMap((i) => i); expect(warnings).not.toContain( '"optimize.lazy" is deprecated and has been replaced by "optimize.watch"' @@ -55,7 +55,7 @@ describe('configuration deprecations', () => { await root.setup(); - const logs = loggingServiceMock.collect(mockLoggingService); + const logs = loggingSystemMock.collect(mockLoggingSystem); const warnings = logs.warn.flatMap((i) => i); expect(warnings).toContain( '"optimize.lazy" is deprecated and has been replaced by "optimize.watch"' diff --git a/src/core/server/core_context.mock.ts b/src/core/server/core_context.mock.ts index d287348e19079f..f870d30528df42 100644 --- a/src/core/server/core_context.mock.ts +++ b/src/core/server/core_context.mock.ts @@ -20,17 +20,17 @@ import { CoreContext } from './core_context'; import { getEnvOptions } from './config/__mocks__/env'; import { Env, IConfigService } from './config'; -import { loggingServiceMock } from './logging/logging_service.mock'; +import { loggingSystemMock } from './logging/logging_system.mock'; import { configServiceMock } from './config/config_service.mock'; -import { ILoggingService } from './logging'; +import { ILoggingSystem } from './logging'; function create({ env = Env.createDefault(getEnvOptions()), - logger = loggingServiceMock.create(), + logger = loggingSystemMock.create(), configService = configServiceMock.create(), }: { env?: Env; - logger?: jest.Mocked; + logger?: jest.Mocked; configService?: jest.Mocked; } = {}): DeeplyMockedKeys { return { coreId: Symbol(), env, logger, configService }; diff --git a/src/core/server/elasticsearch/cluster_client.test.ts b/src/core/server/elasticsearch/cluster_client.test.ts index db277fa0e06074..820272bdf14b81 100644 --- a/src/core/server/elasticsearch/cluster_client.test.ts +++ b/src/core/server/elasticsearch/cluster_client.test.ts @@ -28,11 +28,11 @@ import { import { errors } from 'elasticsearch'; import { get } from 'lodash'; import { Logger } from '../logging'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; import { httpServerMock } from '../http/http_server.mocks'; import { ClusterClient } from './cluster_client'; -const logger = loggingServiceMock.create(); +const logger = loggingSystemMock.create(); afterEach(() => jest.clearAllMocks()); test('#constructor creates client with parsed config', () => { diff --git a/src/core/server/elasticsearch/elasticsearch_client_config.test.ts b/src/core/server/elasticsearch/elasticsearch_client_config.test.ts index 20c10459e0e8a4..77d1e41c9ad833 100644 --- a/src/core/server/elasticsearch/elasticsearch_client_config.test.ts +++ b/src/core/server/elasticsearch/elasticsearch_client_config.test.ts @@ -18,12 +18,12 @@ */ import { duration } from 'moment'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; import { ElasticsearchClientConfig, parseElasticsearchClientConfig, } from './elasticsearch_client_config'; -const logger = loggingServiceMock.create(); +const logger = loggingSystemMock.create(); afterEach(() => jest.clearAllMocks()); test('parses minimally specified config', () => { @@ -360,7 +360,7 @@ describe('#log', () => { expect(typeof esLogger.close).toBe('function'); - expect(loggingServiceMock.collect(logger)).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger)).toMatchInlineSnapshot(` Object { "debug": Array [], "error": Array [ @@ -406,7 +406,7 @@ Object { expect(typeof esLogger.close).toBe('function'); - expect(loggingServiceMock.collect(logger)).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger)).toMatchInlineSnapshot(` Object { "debug": Array [ Array [ diff --git a/src/core/server/elasticsearch/elasticsearch_service.test.ts b/src/core/server/elasticsearch/elasticsearch_service.test.ts index 8bf0df74186a99..0a7068903e15c2 100644 --- a/src/core/server/elasticsearch/elasticsearch_service.test.ts +++ b/src/core/server/elasticsearch/elasticsearch_service.test.ts @@ -26,7 +26,7 @@ import { Env } from '../config'; import { getEnvOptions } from '../config/__mocks__/env'; import { CoreContext } from '../core_context'; import { configServiceMock } from '../config/config_service.mock'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; import { httpServiceMock } from '../http/http_service.mock'; import { ElasticsearchConfig } from './elasticsearch_config'; import { ElasticsearchService } from './elasticsearch_service'; @@ -55,7 +55,7 @@ configService.atPath.mockReturnValue( let env: Env; let coreContext: CoreContext; -const logger = loggingServiceMock.create(); +const logger = loggingSystemMock.create(); beforeEach(() => { env = Env.createDefault(getEnvOptions()); diff --git a/src/core/server/elasticsearch/retry_call_cluster.test.ts b/src/core/server/elasticsearch/retry_call_cluster.test.ts index 8be138e6752d2e..18ffa95048c4d9 100644 --- a/src/core/server/elasticsearch/retry_call_cluster.test.ts +++ b/src/core/server/elasticsearch/retry_call_cluster.test.ts @@ -19,7 +19,7 @@ import * as legacyElasticsearch from 'elasticsearch'; import { retryCallCluster, migrationsRetryCallCluster } from './retry_call_cluster'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; describe('retryCallCluster', () => { it('retries ES API calls that rejects with NoConnections', () => { @@ -69,10 +69,10 @@ describe('migrationsRetryCallCluster', () => { 'Gone', ]; - const mockLogger = loggingServiceMock.create(); + const mockLogger = loggingSystemMock.create(); beforeEach(() => { - loggingServiceMock.clear(mockLogger); + loggingSystemMock.clear(mockLogger); }); errors.forEach((errorName) => { @@ -133,7 +133,7 @@ describe('migrationsRetryCallCluster', () => { callEsApi.mockResolvedValueOnce('done'); const retried = migrationsRetryCallCluster(callEsApi, mockLogger.get('mock log'), 1); await retried('endpoint'); - expect(loggingServiceMock.collect(mockLogger).warn).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(mockLogger).warn).toMatchInlineSnapshot(` Array [ Array [ "Unable to connect to Elasticsearch. Error: No Living connections", diff --git a/src/core/server/elasticsearch/version_check/ensure_es_version.test.ts b/src/core/server/elasticsearch/version_check/ensure_es_version.test.ts index a2090ed111ca11..3d1218d4a8e8b9 100644 --- a/src/core/server/elasticsearch/version_check/ensure_es_version.test.ts +++ b/src/core/server/elasticsearch/version_check/ensure_es_version.test.ts @@ -17,12 +17,12 @@ * under the License. */ import { mapNodesVersionCompatibility, pollEsNodesVersion, NodesInfo } from './ensure_es_version'; -import { loggingServiceMock } from '../../logging/logging_service.mock'; +import { loggingSystemMock } from '../../logging/logging_system.mock'; import { take, delay } from 'rxjs/operators'; import { TestScheduler } from 'rxjs/testing'; import { of } from 'rxjs'; -const mockLoggerFactory = loggingServiceMock.create(); +const mockLoggerFactory = loggingSystemMock.create(); const mockLogger = mockLoggerFactory.get('mock logger'); const KIBANA_VERSION = '5.1.0'; diff --git a/src/core/server/http/__snapshots__/http_config.test.ts.snap b/src/core/server/http/__snapshots__/http_config.test.ts.snap index 07c153a7a8a200..d48ead3cec8e13 100644 --- a/src/core/server/http/__snapshots__/http_config.test.ts.snap +++ b/src/core/server/http/__snapshots__/http_config.test.ts.snap @@ -83,6 +83,8 @@ Object { exports[`throws if basepath appends a slash 1`] = `"[basePath]: must start with a slash, don't end with one"`; +exports[`throws if basepath is an empty string 1`] = `"[basePath]: must start with a slash, don't end with one"`; + exports[`throws if basepath is missing prepended slash 1`] = `"[basePath]: must start with a slash, don't end with one"`; exports[`throws if basepath is not specified, but rewriteBasePath is set 1`] = `"cannot use [rewriteBasePath] when [basePath] is not specified"`; diff --git a/src/core/server/http/cookie_session_storage.test.ts b/src/core/server/http/cookie_session_storage.test.ts index 3afe5e0c4dfc7b..1fb2b5693bb614 100644 --- a/src/core/server/http/cookie_session_storage.test.ts +++ b/src/core/server/http/cookie_session_storage.test.ts @@ -29,14 +29,14 @@ import { Env } from '../config'; import { getEnvOptions } from '../config/__mocks__/env'; import { configServiceMock } from '../config/config_service.mock'; import { contextServiceMock } from '../context/context_service.mock'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; import { httpServerMock } from './http_server.mocks'; import { createCookieSessionStorageFactory } from './cookie_session_storage'; let server: HttpService; -let logger: ReturnType; +let logger: ReturnType; let env: Env; let coreContext: CoreContext; const configService = configServiceMock.create(); @@ -67,7 +67,7 @@ configService.atPath.mockReturnValue( ); beforeEach(() => { - logger = loggingServiceMock.create(); + logger = loggingSystemMock.create(); env = Env.createDefault(getEnvOptions()); coreContext = { coreId: Symbol(), env, logger, configService: configService as any }; @@ -324,7 +324,7 @@ describe('Cookie based SessionStorage', () => { expect(mockServer.auth.test).toBeCalledTimes(1); expect(mockServer.auth.test).toHaveBeenCalledWith('security-cookie', mockRequest); - expect(loggingServiceMock.collect(logger).warn).toEqual([ + expect(loggingSystemMock.collect(logger).warn).toEqual([ ['Found 2 auth sessions when we were only expecting 1.'], ]); }); @@ -381,7 +381,7 @@ describe('Cookie based SessionStorage', () => { const session = await factory.asScoped(KibanaRequest.from(mockRequest)).get(); expect(session).toBe(null); - expect(loggingServiceMock.collect(logger).debug).toEqual([['Error: Invalid cookie.']]); + expect(loggingSystemMock.collect(logger).debug).toEqual([['Error: Invalid cookie.']]); }); }); diff --git a/src/core/server/http/http_config.test.ts b/src/core/server/http/http_config.test.ts index eaf66219d08dc4..0698f118be03fa 100644 --- a/src/core/server/http/http_config.test.ts +++ b/src/core/server/http/http_config.test.ts @@ -78,6 +78,14 @@ test('throws if basepath appends a slash', () => { expect(() => httpSchema.validate(obj)).toThrowErrorMatchingSnapshot(); }); +test('throws if basepath is an empty string', () => { + const httpSchema = config.schema; + const obj = { + basePath: '', + }; + expect(() => httpSchema.validate(obj)).toThrowErrorMatchingSnapshot(); +}); + test('throws if basepath is not specified, but rewriteBasePath is set', () => { const httpSchema = config.schema; const obj = { diff --git a/src/core/server/http/http_config.ts b/src/core/server/http/http_config.ts index 289b6539fd7625..83a2e712b424fd 100644 --- a/src/core/server/http/http_config.ts +++ b/src/core/server/http/http_config.ts @@ -23,7 +23,7 @@ import { hostname } from 'os'; import { CspConfigType, CspConfig, ICspConfig } from '../csp'; import { SslConfig, sslSchema } from './ssl_config'; -const validBasePathRegex = /(^$|^\/.*[^\/]$)/; +const validBasePathRegex = /^\/.*[^\/]$/; const uuidRegexp = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i; const match = (regex: RegExp, errorMsg: string) => (str: string) => diff --git a/src/core/server/http/http_server.test.ts b/src/core/server/http/http_server.test.ts index 9a5deb9b455627..4520851bb460c7 100644 --- a/src/core/server/http/http_server.test.ts +++ b/src/core/server/http/http_server.test.ts @@ -31,7 +31,7 @@ import { RouteValidationResultFactory, RouteValidationFunction, } from './router'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; import { HttpServer } from './http_server'; import { Readable } from 'stream'; import { RequestHandlerContext } from 'kibana/server'; @@ -48,7 +48,7 @@ let server: HttpServer; let config: HttpConfig; let configWithSSL: HttpConfig; -const loggingService = loggingServiceMock.create(); +const loggingService = loggingSystemMock.create(); const logger = loggingService.get(); const enhanceWithContext = (fn: (...args: any[]) => any) => fn.bind(null, {}); @@ -97,7 +97,7 @@ test('log listening address after started', async () => { await server.start(); expect(server.isListening()).toBe(true); - expect(loggingServiceMock.collect(loggingService).info).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(loggingService).info).toMatchInlineSnapshot(` Array [ Array [ "http server running at http://127.0.0.1:10002", @@ -113,7 +113,7 @@ test('log listening address after started when configured with BasePath and rewr await server.start(); expect(server.isListening()).toBe(true); - expect(loggingServiceMock.collect(loggingService).info).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(loggingService).info).toMatchInlineSnapshot(` Array [ Array [ "http server running at http://127.0.0.1:10002", @@ -129,7 +129,7 @@ test('log listening address after started when configured with BasePath and rewr await server.start(); expect(server.isListening()).toBe(true); - expect(loggingServiceMock.collect(loggingService).info).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(loggingService).info).toMatchInlineSnapshot(` Array [ Array [ "http server running at http://127.0.0.1:10002/bar", diff --git a/src/core/server/http/http_service.test.ts b/src/core/server/http/http_service.test.ts index 8b500caf217dc5..3d759b427d9fb0 100644 --- a/src/core/server/http/http_service.test.ts +++ b/src/core/server/http/http_service.test.ts @@ -25,12 +25,12 @@ import { HttpService } from '.'; import { HttpConfigType, config } from './http_config'; import { httpServerMock } from './http_server.mocks'; import { ConfigService, Env } from '../config'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; import { contextServiceMock } from '../context/context_service.mock'; import { getEnvOptions } from '../config/__mocks__/env'; import { config as cspConfig } from '../csp'; -const logger = loggingServiceMock.create(); +const logger = loggingSystemMock.create(); const env = Env.createDefault(getEnvOptions()); const coreId = Symbol(); @@ -159,7 +159,7 @@ test('logs error if already set up', async () => { await service.setup(setupDeps); - expect(loggingServiceMock.collect(logger).warn).toMatchSnapshot(); + expect(loggingSystemMock.collect(logger).warn).toMatchSnapshot(); }); test('stops http server', async () => { diff --git a/src/core/server/http/http_tools.test.ts b/src/core/server/http/http_tools.test.ts index 7d5a7277a767a9..f09d862f9edace 100644 --- a/src/core/server/http/http_tools.test.ts +++ b/src/core/server/http/http_tools.test.ts @@ -34,7 +34,7 @@ import { defaultValidationErrorHandler, HapiValidationError, getServerOptions } import { HttpServer } from './http_server'; import { HttpConfig, config } from './http_config'; import { Router } from './router'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; import { ByteSizeValue } from '@kbn/config-schema'; const emptyOutput = { @@ -77,7 +77,7 @@ describe('defaultValidationErrorHandler', () => { }); describe('timeouts', () => { - const logger = loggingServiceMock.create(); + const logger = loggingSystemMock.create(); const server = new HttpServer(logger, 'foo'); const enhanceWithContext = (fn: (...args: any[]) => any) => fn.bind(null, {}); diff --git a/src/core/server/http/https_redirect_server.test.ts b/src/core/server/http/https_redirect_server.test.ts index a7d3cbe41aa3d4..f35456f01c19bb 100644 --- a/src/core/server/http/https_redirect_server.test.ts +++ b/src/core/server/http/https_redirect_server.test.ts @@ -27,7 +27,7 @@ import supertest from 'supertest'; import { ByteSizeValue } from '@kbn/config-schema'; import { HttpConfig } from '.'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; import { HttpsRedirectServer } from './https_redirect_server'; const chance = new Chance(); @@ -50,7 +50,7 @@ beforeEach(() => { }, } as HttpConfig; - server = new HttpsRedirectServer(loggingServiceMock.create().get()); + server = new HttpsRedirectServer(loggingSystemMock.create().get()); }); afterEach(async () => { diff --git a/src/core/server/http/integration_tests/lifecycle.test.ts b/src/core/server/http/integration_tests/lifecycle.test.ts index 73ed4e5de4b046..879cbc689f8e79 100644 --- a/src/core/server/http/integration_tests/lifecycle.test.ts +++ b/src/core/server/http/integration_tests/lifecycle.test.ts @@ -24,12 +24,12 @@ import { ensureRawRequest } from '../router'; import { HttpService } from '../http_service'; import { contextServiceMock } from '../../context/context_service.mock'; -import { loggingServiceMock } from '../../logging/logging_service.mock'; +import { loggingSystemMock } from '../../logging/logging_system.mock'; import { createHttpServer } from '../test_utils'; let server: HttpService; -let logger: ReturnType; +let logger: ReturnType; const contextSetup = contextServiceMock.createSetupContract(); @@ -38,7 +38,7 @@ const setupDeps = { }; beforeEach(() => { - logger = loggingServiceMock.create(); + logger = loggingSystemMock.create(); server = createHttpServer({ logger }); }); @@ -167,7 +167,7 @@ describe('OnPreAuth', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('An internal server error occurred.'); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: reason], @@ -188,7 +188,7 @@ describe('OnPreAuth', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('An internal server error occurred.'); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: Unexpected result from OnPreAuth. Expected OnPreAuthResult or KibanaResponse, but given: [object Object].], @@ -301,7 +301,7 @@ describe('OnPostAuth', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('An internal server error occurred.'); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: reason], @@ -321,7 +321,7 @@ describe('OnPostAuth', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('An internal server error occurred.'); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: Unexpected result from OnPostAuth. Expected OnPostAuthResult or KibanaResponse, but given: [object Object].], @@ -506,7 +506,7 @@ describe('Auth', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('An internal server error occurred.'); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: reason], @@ -703,7 +703,7 @@ describe('Auth', () => { const response = await supertest(innerServer.listener).get('/').expect(200); expect(response.header['www-authenticate']).toBe('from auth interceptor'); - expect(loggingServiceMock.collect(logger).warn).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).warn).toMatchInlineSnapshot(` Array [ Array [ "onPreResponseHandler rewrote a response header [www-authenticate].", @@ -736,7 +736,7 @@ describe('Auth', () => { const response = await supertest(innerServer.listener).get('/').expect(400); expect(response.header['www-authenticate']).toBe('from auth interceptor'); - expect(loggingServiceMock.collect(logger).warn).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).warn).toMatchInlineSnapshot(` Array [ Array [ "onPreResponseHandler rewrote a response header [www-authenticate].", @@ -798,7 +798,7 @@ describe('Auth', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('An internal server error occurred.'); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: reason], @@ -818,7 +818,7 @@ describe('Auth', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('An internal server error occurred.'); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: Unexpected result from OnPostAuth. Expected OnPostAuthResult or KibanaResponse, but given: [object Object].], @@ -929,7 +929,7 @@ describe('OnPreResponse', () => { await supertest(innerServer.listener).get('/').expect(200); - expect(loggingServiceMock.collect(logger).warn).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).warn).toMatchInlineSnapshot(` Array [ Array [ "onPreResponseHandler rewrote a response header [x-kibana-header].", @@ -953,7 +953,7 @@ describe('OnPreResponse', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('An internal server error occurred.'); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: reason], @@ -975,7 +975,7 @@ describe('OnPreResponse', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('An internal server error occurred.'); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: Unexpected result from OnPreResponse. Expected OnPreResponseResult, but given: [object Object].], diff --git a/src/core/server/http/integration_tests/request.test.ts b/src/core/server/http/integration_tests/request.test.ts index d33757273042b4..2d018f7f464b5d 100644 --- a/src/core/server/http/integration_tests/request.test.ts +++ b/src/core/server/http/integration_tests/request.test.ts @@ -21,12 +21,12 @@ import supertest from 'supertest'; import { HttpService } from '../http_service'; import { contextServiceMock } from '../../context/context_service.mock'; -import { loggingServiceMock } from '../../logging/logging_service.mock'; +import { loggingSystemMock } from '../../logging/logging_system.mock'; import { createHttpServer } from '../test_utils'; let server: HttpService; -let logger: ReturnType; +let logger: ReturnType; const contextSetup = contextServiceMock.createSetupContract(); const setupDeps = { @@ -34,7 +34,7 @@ const setupDeps = { }; beforeEach(() => { - logger = loggingServiceMock.create(); + logger = loggingSystemMock.create(); server = createHttpServer({ logger }); }); diff --git a/src/core/server/http/integration_tests/router.test.ts b/src/core/server/http/integration_tests/router.test.ts index 8f3799b12eccb6..bb36fefa96611e 100644 --- a/src/core/server/http/integration_tests/router.test.ts +++ b/src/core/server/http/integration_tests/router.test.ts @@ -24,12 +24,12 @@ import { schema } from '@kbn/config-schema'; import { HttpService } from '../http_service'; import { contextServiceMock } from '../../context/context_service.mock'; -import { loggingServiceMock } from '../../logging/logging_service.mock'; +import { loggingSystemMock } from '../../logging/logging_system.mock'; import { createHttpServer } from '../test_utils'; let server: HttpService; -let logger: ReturnType; +let logger: ReturnType; const contextSetup = contextServiceMock.createSetupContract(); const setupDeps = { @@ -37,7 +37,7 @@ const setupDeps = { }; beforeEach(() => { - logger = loggingServiceMock.create(); + logger = loggingSystemMock.create(); server = createHttpServer({ logger }); }); @@ -347,7 +347,7 @@ describe('Handler', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('An internal server error occurred.'); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: unexpected error], @@ -368,7 +368,7 @@ describe('Handler', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('An internal server error occurred.'); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: Unauthorized], @@ -387,7 +387,7 @@ describe('Handler', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('An internal server error occurred.'); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: Unexpected result from Route Handler. Expected KibanaResponse, but given: string.], @@ -763,7 +763,7 @@ describe('Response factory', () => { await supertest(innerServer.listener).get('/').expect(500); // error happens within hapi when route handler already finished execution. - expect(loggingServiceMock.collect(logger).error).toHaveLength(0); + expect(loggingSystemMock.collect(logger).error).toHaveLength(0); }); it('200 OK with body', async () => { @@ -855,7 +855,7 @@ describe('Response factory', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('An internal server error occurred.'); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: expected 'location' header to be set], @@ -1261,7 +1261,7 @@ describe('Response factory', () => { message: 'An internal server error occurred.', statusCode: 500, }); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: Unexpected Http status code. Expected from 400 to 599, but given: 200], @@ -1330,7 +1330,7 @@ describe('Response factory', () => { await supertest(innerServer.listener).get('/').expect(500); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: expected 'location' header to be set], @@ -1445,7 +1445,7 @@ describe('Response factory', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('reason'); - expect(loggingServiceMock.collect(logger).error).toHaveLength(0); + expect(loggingSystemMock.collect(logger).error).toHaveLength(0); }); it('throws an error if not valid error is provided', async () => { @@ -1464,7 +1464,7 @@ describe('Response factory', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('An internal server error occurred.'); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: expected error message to be provided], @@ -1488,7 +1488,7 @@ describe('Response factory', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('An internal server error occurred.'); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: expected error message to be provided], @@ -1511,7 +1511,7 @@ describe('Response factory', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('An internal server error occurred.'); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: options.statusCode is expected to be set. given options: undefined], @@ -1534,7 +1534,7 @@ describe('Response factory', () => { const result = await supertest(innerServer.listener).get('/').expect(500); expect(result.body.message).toBe('An internal server error occurred.'); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: Unexpected Http status code. Expected from 100 to 599, but given: 20.], diff --git a/src/core/server/http/router/router.test.ts b/src/core/server/http/router/router.test.ts index 9655e2153b863e..fa38c7bd6b336d 100644 --- a/src/core/server/http/router/router.test.ts +++ b/src/core/server/http/router/router.test.ts @@ -18,10 +18,10 @@ */ import { Router } from './router'; -import { loggingServiceMock } from '../../logging/logging_service.mock'; +import { loggingSystemMock } from '../../logging/logging_system.mock'; import { schema } from '@kbn/config-schema'; -const logger = loggingServiceMock.create().get(); +const logger = loggingSystemMock.create().get(); const enhanceWithContext = (fn: (...args: any[]) => any) => fn.bind(null, {}); describe('Router', () => { diff --git a/src/core/server/http/test_utils.ts b/src/core/server/http/test_utils.ts index 0e639aa72a8254..bda66e1de8168f 100644 --- a/src/core/server/http/test_utils.ts +++ b/src/core/server/http/test_utils.ts @@ -24,12 +24,12 @@ import { getEnvOptions } from '../config/__mocks__/env'; import { HttpService } from './http_service'; import { CoreContext } from '../core_context'; import { configServiceMock } from '../config/config_service.mock'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; const coreId = Symbol('core'); const env = Env.createDefault(getEnvOptions()); -const logger = loggingServiceMock.create(); +const logger = loggingSystemMock.create(); const configService = configServiceMock.create(); configService.atPath.mockReturnValue( diff --git a/src/core/server/index.ts b/src/core/server/index.ts index 0da7e5d66cf2a7..e0afd5e57f0416 100644 --- a/src/core/server/index.ts +++ b/src/core/server/index.ts @@ -62,6 +62,12 @@ import { CapabilitiesSetup, CapabilitiesStart } from './capabilities'; import { UuidServiceSetup } from './uuid'; import { MetricsServiceSetup } from './metrics'; import { StatusServiceSetup } from './status'; +import { + LoggingServiceSetup, + appendersSchema, + loggerContextConfigSchema, + loggerSchema, +} from './logging'; export { bootstrap } from './bootstrap'; export { Capabilities, CapabilitiesProvider, CapabilitiesSwitcher } from './capabilities'; @@ -187,7 +193,17 @@ export { } from './http_resources'; export { IRenderOptions } from './rendering'; -export { Logger, LoggerFactory, LogMeta, LogRecord, LogLevel } from './logging'; +export { + Logger, + LoggerFactory, + LogMeta, + LogRecord, + LogLevel, + LoggingServiceSetup, + LoggerContextConfigInput, + LoggerConfigType, + AppenderConfigType, +} from './logging'; export { DiscoveredPlugin, @@ -385,6 +401,8 @@ export interface CoreSetup = KbnServer as any; @@ -64,7 +65,7 @@ let setupDeps: LegacyServiceSetupDeps; let startDeps: LegacyServiceStartDeps; -const logger = loggingServiceMock.create(); +const logger = loggingSystemMock.create(); let configService: ReturnType; let uuidSetup: ReturnType; @@ -100,6 +101,7 @@ beforeEach(() => { metrics: metricsServiceMock.createInternalSetupContract(), uuid: uuidSetup, status: statusServiceMock.createInternalSetupContract(), + logging: loggingServiceMock.createInternalSetupContract(), }, plugins: { 'plugin-id': 'plugin-value' }, uiPlugins: { @@ -281,7 +283,7 @@ describe('once LegacyService is set up with connection info', () => { const [mockKbnServer] = MockKbnServer.mock.instances as Array>; expect(mockKbnServer.applyLoggingConfiguration).not.toHaveBeenCalled(); - expect(loggingServiceMock.collect(logger).error).toEqual([]); + expect(loggingSystemMock.collect(logger).error).toEqual([]); const configError = new Error('something went wrong'); mockKbnServer.applyLoggingConfiguration.mockImplementation(() => { @@ -290,7 +292,7 @@ describe('once LegacyService is set up with connection info', () => { config$.next(new ObjectToConfigAdapter({ logging: { verbose: true } })); - expect(loggingServiceMock.collect(logger).error).toEqual([[configError]]); + expect(loggingSystemMock.collect(logger).error).toEqual([[configError]]); }); test('logs error if config service fails.', async () => { @@ -306,13 +308,13 @@ describe('once LegacyService is set up with connection info', () => { const [mockKbnServer] = MockKbnServer.mock.instances; expect(mockKbnServer.applyLoggingConfiguration).not.toHaveBeenCalled(); - expect(loggingServiceMock.collect(logger).error).toEqual([]); + expect(loggingSystemMock.collect(logger).error).toEqual([]); const configError = new Error('something went wrong'); config$.error(configError); expect(mockKbnServer.applyLoggingConfiguration).not.toHaveBeenCalled(); - expect(loggingServiceMock.collect(logger).error).toEqual([[configError]]); + expect(loggingSystemMock.collect(logger).error).toEqual([[configError]]); }); }); diff --git a/src/core/server/legacy/legacy_service.ts b/src/core/server/legacy/legacy_service.ts index cfc53b10d91f0a..be737f6593c025 100644 --- a/src/core/server/legacy/legacy_service.ts +++ b/src/core/server/legacy/legacy_service.ts @@ -309,6 +309,9 @@ export class LegacyService implements CoreService { csp: setupDeps.core.http.csp, getServerInfo: setupDeps.core.http.getServerInfo, }, + logging: { + configure: (config$) => setupDeps.core.logging.configure([], config$), + }, metrics: { getOpsMetrics$: setupDeps.core.metrics.getOpsMetrics$, }, diff --git a/src/core/server/logging/__snapshots__/logging_service.test.ts.snap b/src/core/server/logging/__snapshots__/logging_system.test.ts.snap similarity index 100% rename from src/core/server/logging/__snapshots__/logging_service.test.ts.snap rename to src/core/server/logging/__snapshots__/logging_system.test.ts.snap diff --git a/src/core/server/logging/appenders/appenders.ts b/src/core/server/logging/appenders/appenders.ts index 3aa86495e4d825..3b90a10a1a76c3 100644 --- a/src/core/server/logging/appenders/appenders.ts +++ b/src/core/server/logging/appenders/appenders.ts @@ -26,13 +26,19 @@ import { LogRecord } from '../log_record'; import { ConsoleAppender } from './console/console_appender'; import { FileAppender } from './file/file_appender'; -const appendersSchema = schema.oneOf([ +/** + * Config schema for validting the shape of the `appenders` key in in {@link LoggerContextConfigType} or + * {@link LoggingConfigType}. + * + * @public + */ +export const appendersSchema = schema.oneOf([ ConsoleAppender.configSchema, FileAppender.configSchema, LegacyAppender.configSchema, ]); -/** @internal */ +/** @public */ export type AppenderConfigType = TypeOf; /** diff --git a/src/core/server/logging/index.ts b/src/core/server/logging/index.ts index fd35ed39092b31..94719720302817 100644 --- a/src/core/server/logging/index.ts +++ b/src/core/server/logging/index.ts @@ -21,7 +21,18 @@ export { Logger, LogMeta } from './logger'; export { LoggerFactory } from './logger_factory'; export { LogRecord } from './log_record'; export { LogLevel } from './log_level'; -/** @internal */ -export { config, LoggingConfigType } from './logging_config'; -/** @internal */ -export { LoggingService, ILoggingService } from './logging_service'; +export { + config, + LoggingConfigType, + LoggerContextConfigInput, + LoggerConfigType, + loggerContextConfigSchema, + loggerSchema, +} from './logging_config'; +export { LoggingSystem, ILoggingSystem } from './logging_system'; +export { + InternalLoggingServiceSetup, + LoggingServiceSetup, + LoggingService, +} from './logging_service'; +export { appendersSchema, AppenderConfigType } from './appenders/appenders'; diff --git a/src/core/server/logging/logging_config.test.ts b/src/core/server/logging/logging_config.test.ts index 75f571d34c25c5..e2ce3e1983aa14 100644 --- a/src/core/server/logging/logging_config.test.ts +++ b/src/core/server/logging/logging_config.test.ts @@ -171,3 +171,127 @@ test('fails if loggers use unknown appenders.', () => { expect(() => new LoggingConfig(validateConfig)).toThrowErrorMatchingSnapshot(); }); + +describe('extend', () => { + it('adds new appenders', () => { + const configValue = new LoggingConfig( + config.schema.validate({ + appenders: { + file1: { + kind: 'file', + layout: { kind: 'pattern' }, + path: 'path', + }, + }, + }) + ); + + const mergedConfigValue = configValue.extend( + config.schema.validate({ + appenders: { + file2: { + kind: 'file', + layout: { kind: 'pattern' }, + path: 'path', + }, + }, + }) + ); + + expect([...mergedConfigValue.appenders.keys()]).toEqual([ + 'default', + 'console', + 'file1', + 'file2', + ]); + }); + + it('overrides appenders', () => { + const configValue = new LoggingConfig( + config.schema.validate({ + appenders: { + file1: { + kind: 'file', + layout: { kind: 'pattern' }, + path: 'path', + }, + }, + }) + ); + + const mergedConfigValue = configValue.extend( + config.schema.validate({ + appenders: { + file1: { + kind: 'file', + layout: { kind: 'json' }, + path: 'updatedPath', + }, + }, + }) + ); + + expect(mergedConfigValue.appenders.get('file1')).toEqual({ + kind: 'file', + layout: { kind: 'json' }, + path: 'updatedPath', + }); + }); + + it('adds new loggers', () => { + const configValue = new LoggingConfig( + config.schema.validate({ + loggers: [ + { + context: 'plugins', + level: 'warn', + }, + ], + }) + ); + + const mergedConfigValue = configValue.extend( + config.schema.validate({ + loggers: [ + { + context: 'plugins.pid', + level: 'trace', + }, + ], + }) + ); + + expect([...mergedConfigValue.loggers.keys()]).toEqual(['root', 'plugins', 'plugins.pid']); + }); + + it('overrides loggers', () => { + const configValue = new LoggingConfig( + config.schema.validate({ + loggers: [ + { + context: 'plugins', + level: 'warn', + }, + ], + }) + ); + + const mergedConfigValue = configValue.extend( + config.schema.validate({ + loggers: [ + { + appenders: ['console'], + context: 'plugins', + level: 'trace', + }, + ], + }) + ); + + expect(mergedConfigValue.loggers.get('plugins')).toEqual({ + appenders: ['console'], + context: 'plugins', + level: 'trace', + }); + }); +}); diff --git a/src/core/server/logging/logging_config.ts b/src/core/server/logging/logging_config.ts index 772909ce584e51..a6aafabeb970cf 100644 --- a/src/core/server/logging/logging_config.ts +++ b/src/core/server/logging/logging_config.ts @@ -39,7 +39,7 @@ const ROOT_CONTEXT_NAME = 'root'; */ const DEFAULT_APPENDER_NAME = 'default'; -const createLevelSchema = schema.oneOf( +const levelSchema = schema.oneOf( [ schema.literal('all'), schema.literal('fatal'), @@ -55,21 +55,26 @@ const createLevelSchema = schema.oneOf( } ); -const createLoggerSchema = schema.object({ +/** + * Config schema for validating the `loggers` key in {@link LoggerContextConfigType} or {@link LoggingConfigType}. + * + * @public + */ +export const loggerSchema = schema.object({ appenders: schema.arrayOf(schema.string(), { defaultValue: [] }), context: schema.string(), - level: createLevelSchema, + level: levelSchema, }); -/** @internal */ -export type LoggerConfigType = TypeOf; +/** @public */ +export type LoggerConfigType = TypeOf; export const config = { path: 'logging', schema: schema.object({ appenders: schema.mapOf(schema.string(), Appenders.configSchema, { defaultValue: new Map(), }), - loggers: schema.arrayOf(createLoggerSchema, { + loggers: schema.arrayOf(loggerSchema, { defaultValue: [], }), root: schema.object( @@ -78,7 +83,7 @@ export const config = { defaultValue: [DEFAULT_APPENDER_NAME], minSize: 1, }), - level: createLevelSchema, + level: levelSchema, }, { validate(rawConfig) { @@ -93,6 +98,29 @@ export const config = { export type LoggingConfigType = TypeOf; +/** + * Config schema for validating the inputs to the {@link LoggingServiceStart.configure} API. + * See {@link LoggerContextConfigType}. + * + * @public + */ +export const loggerContextConfigSchema = schema.object({ + appenders: schema.mapOf(schema.string(), Appenders.configSchema, { + defaultValue: new Map(), + }), + + loggers: schema.arrayOf(loggerSchema, { defaultValue: [] }), +}); + +/** @public */ +export type LoggerContextConfigType = TypeOf; +/** @public */ +export interface LoggerContextConfigInput { + // config-schema knows how to handle either Maps or Records + appenders?: Record | Map; + loggers?: LoggerConfigType[]; +} + /** * Describes the config used to fully setup logging subsystem. * @internal @@ -147,11 +175,35 @@ export class LoggingConfig { */ public readonly loggers: Map = new Map(); - constructor(configType: LoggingConfigType) { + constructor(private readonly configType: LoggingConfigType) { this.fillAppendersConfig(configType); this.fillLoggersConfig(configType); } + /** + * Returns a new LoggingConfig that merges the existing config with the specified config. + * + * @remarks + * Does not support merging the `root` config property. + * + * @param contextConfig + */ + public extend(contextConfig: LoggerContextConfigType) { + // Use a Map to de-dupe any loggers for the same context. contextConfig overrides existing config. + const mergedLoggers = new Map([ + ...this.configType.loggers.map((l) => [l.context, l] as [string, LoggerConfigType]), + ...contextConfig.loggers.map((l) => [l.context, l] as [string, LoggerConfigType]), + ]); + + const mergedConfig: LoggingConfigType = { + appenders: new Map([...this.configType.appenders, ...contextConfig.appenders]), + loggers: [...mergedLoggers.values()], + root: this.configType.root, + }; + + return new LoggingConfig(mergedConfig); + } + private fillAppendersConfig(loggingConfig: LoggingConfigType) { for (const [appenderKey, appenderSchema] of loggingConfig.appenders) { this.appenders.set(appenderKey, appenderSchema); diff --git a/src/core/server/logging/logging_service.mock.ts b/src/core/server/logging/logging_service.mock.ts index 15d66c2e8535ca..21edbe670eaecd 100644 --- a/src/core/server/logging/logging_service.mock.ts +++ b/src/core/server/logging/logging_service.mock.ts @@ -17,67 +17,35 @@ * under the License. */ -// Test helpers to simplify mocking logs and collecting all their outputs -import { ILoggingService } from './logging_service'; -import { LoggerFactory } from './logger_factory'; -import { loggerMock, MockedLogger } from './logger.mock'; - -const createLoggingServiceMock = () => { - const mockLog = loggerMock.create(); - - mockLog.get.mockImplementation((...context) => ({ - ...mockLog, - context, - })); - - const mocked: jest.Mocked = { - get: jest.fn(), - asLoggerFactory: jest.fn(), - upgrade: jest.fn(), +import { + LoggingService, + LoggingServiceSetup, + InternalLoggingServiceSetup, +} from './logging_service'; + +const createInternalSetupMock = (): jest.Mocked => ({ + configure: jest.fn(), +}); + +const createSetupMock = (): jest.Mocked => ({ + configure: jest.fn(), +}); + +type LoggingServiceContract = PublicMethodsOf; +const createMock = (): jest.Mocked => { + const service: jest.Mocked = { + setup: jest.fn(), + start: jest.fn(), stop: jest.fn(), }; - mocked.get.mockImplementation((...context) => ({ - ...mockLog, - context, - })); - mocked.asLoggerFactory.mockImplementation(() => mocked); - mocked.stop.mockResolvedValue(); - return mocked; -}; - -const collectLoggingServiceMock = (loggerFactory: LoggerFactory) => { - const mockLog = loggerFactory.get() as MockedLogger; - return { - debug: mockLog.debug.mock.calls, - error: mockLog.error.mock.calls, - fatal: mockLog.fatal.mock.calls, - info: mockLog.info.mock.calls, - log: mockLog.log.mock.calls, - trace: mockLog.trace.mock.calls, - warn: mockLog.warn.mock.calls, - }; -}; -const clearLoggingServiceMock = (loggerFactory: LoggerFactory) => { - const mockedLoggerFactory = (loggerFactory as unknown) as jest.Mocked; - mockedLoggerFactory.get.mockClear(); - mockedLoggerFactory.asLoggerFactory.mockClear(); - mockedLoggerFactory.upgrade.mockClear(); - mockedLoggerFactory.stop.mockClear(); + service.setup.mockReturnValue(createInternalSetupMock()); - const mockLog = loggerFactory.get() as MockedLogger; - mockLog.debug.mockClear(); - mockLog.info.mockClear(); - mockLog.warn.mockClear(); - mockLog.error.mockClear(); - mockLog.trace.mockClear(); - mockLog.fatal.mockClear(); - mockLog.log.mockClear(); + return service; }; export const loggingServiceMock = { - create: createLoggingServiceMock, - collect: collectLoggingServiceMock, - clear: clearLoggingServiceMock, - createLogger: loggerMock.create, + create: createMock, + createSetupContract: createSetupMock, + createInternalSetupContract: createInternalSetupMock, }; diff --git a/src/core/server/logging/logging_service.test.ts b/src/core/server/logging/logging_service.test.ts index 1e6c253c56c7b1..5107db77304fcc 100644 --- a/src/core/server/logging/logging_service.test.ts +++ b/src/core/server/logging/logging_service.test.ts @@ -16,167 +16,85 @@ * specific language governing permissions and limitations * under the License. */ - -const mockStreamWrite = jest.fn(); -jest.mock('fs', () => ({ - constants: {}, - createWriteStream: jest.fn(() => ({ write: mockStreamWrite })), -})); - -const dynamicProps = { pid: expect.any(Number) }; - -jest.mock('../../../legacy/server/logging/rotate', () => ({ - setupLoggingRotate: jest.fn().mockImplementation(() => Promise.resolve({})), -})); - -const timestamp = new Date(Date.UTC(2012, 1, 1, 14, 33, 22, 11)); -let mockConsoleLog: jest.SpyInstance; - -import { createWriteStream } from 'fs'; -const mockCreateWriteStream = (createWriteStream as unknown) as jest.Mock; - -import { LoggingService, config } from '.'; - -let service: LoggingService; -beforeEach(() => { - mockConsoleLog = jest.spyOn(global.console, 'log').mockReturnValue(undefined); - jest.spyOn(global, 'Date').mockImplementation(() => timestamp); - service = new LoggingService(); -}); - -afterEach(() => { - jest.restoreAllMocks(); - mockCreateWriteStream.mockClear(); - mockStreamWrite.mockClear(); -}); - -test('uses default memory buffer logger until config is provided', () => { - const bufferAppendSpy = jest.spyOn((service as any).bufferAppender, 'append'); - - const logger = service.get('test', 'context'); - logger.trace('trace message'); - - // We shouldn't create new buffer appender for another context. - const anotherLogger = service.get('test', 'context2'); - anotherLogger.fatal('fatal message', { some: 'value' }); - - expect(bufferAppendSpy).toHaveBeenCalledTimes(2); - expect(bufferAppendSpy.mock.calls[0][0]).toMatchSnapshot(dynamicProps); - expect(bufferAppendSpy.mock.calls[1][0]).toMatchSnapshot(dynamicProps); -}); - -test('flushes memory buffer logger and switches to real logger once config is provided', () => { - const logger = service.get('test', 'context'); - - logger.trace('buffered trace message'); - logger.info('buffered info message', { some: 'value' }); - logger.fatal('buffered fatal message'); - - const bufferAppendSpy = jest.spyOn((service as any).bufferAppender, 'append'); - - // Switch to console appender with `info` level, so that `trace` message won't go through. - service.upgrade( - config.schema.validate({ - appenders: { default: { kind: 'console', layout: { kind: 'json' } } }, - root: { level: 'info' }, - }) - ); - - expect(JSON.parse(mockConsoleLog.mock.calls[0][0])).toMatchSnapshot( - dynamicProps, - 'buffered messages' - ); - mockConsoleLog.mockClear(); - - // Now message should go straight to thew newly configured appender, not buffered one. - logger.info('some new info message'); - expect(JSON.parse(mockConsoleLog.mock.calls[0][0])).toMatchSnapshot(dynamicProps, 'new messages'); - expect(bufferAppendSpy).not.toHaveBeenCalled(); -}); - -test('appends records via multiple appenders.', () => { - const loggerWithoutConfig = service.get('some-context'); - const testsLogger = service.get('tests'); - const testsChildLogger = service.get('tests', 'child'); - - loggerWithoutConfig.info('You know, just for your info.'); - testsLogger.warn('Config is not ready!'); - testsChildLogger.error('Too bad that config is not ready :/'); - testsChildLogger.info('Just some info that should not be logged.'); - - expect(mockConsoleLog).not.toHaveBeenCalled(); - expect(mockCreateWriteStream).not.toHaveBeenCalled(); - - service.upgrade( - config.schema.validate({ - appenders: { - default: { kind: 'console', layout: { kind: 'pattern' } }, - file: { kind: 'file', layout: { kind: 'pattern' }, path: 'path' }, - }, - loggers: [ - { appenders: ['file'], context: 'tests', level: 'warn' }, - { context: 'tests.child', level: 'error' }, - ], - }) - ); - - // Now all logs should added to configured appenders. - expect(mockConsoleLog).toHaveBeenCalledTimes(1); - expect(mockConsoleLog.mock.calls[0][0]).toMatchSnapshot('console logs'); - - expect(mockStreamWrite).toHaveBeenCalledTimes(2); - expect(mockStreamWrite.mock.calls[0][0]).toMatchSnapshot('file logs'); - expect(mockStreamWrite.mock.calls[1][0]).toMatchSnapshot('file logs'); -}); - -test('uses `root` logger if context is not specified.', () => { - service.upgrade( - config.schema.validate({ - appenders: { default: { kind: 'console', layout: { kind: 'pattern' } } }, - }) - ); - - const rootLogger = service.get(); - rootLogger.info('This message goes to a root context.'); - - expect(mockConsoleLog.mock.calls).toMatchSnapshot(); -}); - -test('`stop()` disposes all appenders.', async () => { - service.upgrade( - config.schema.validate({ - appenders: { default: { kind: 'console', layout: { kind: 'json' } } }, - root: { level: 'info' }, - }) - ); - - const bufferDisposeSpy = jest.spyOn((service as any).bufferAppender, 'dispose'); - const consoleDisposeSpy = jest.spyOn((service as any).appenders.get('default'), 'dispose'); - - await service.stop(); - - expect(bufferDisposeSpy).toHaveBeenCalledTimes(1); - expect(consoleDisposeSpy).toHaveBeenCalledTimes(1); -}); - -test('asLoggerFactory() only allows to create new loggers.', () => { - const logger = service.asLoggerFactory().get('test', 'context'); - - service.upgrade( - config.schema.validate({ - appenders: { default: { kind: 'console', layout: { kind: 'json' } } }, - root: { level: 'all' }, - }) - ); - - logger.trace('buffered trace message'); - logger.info('buffered info message', { some: 'value' }); - logger.fatal('buffered fatal message'); - - expect(Object.keys(service.asLoggerFactory())).toEqual(['get']); - - expect(mockConsoleLog).toHaveBeenCalledTimes(3); - expect(JSON.parse(mockConsoleLog.mock.calls[0][0])).toMatchSnapshot(dynamicProps); - expect(JSON.parse(mockConsoleLog.mock.calls[1][0])).toMatchSnapshot(dynamicProps); - expect(JSON.parse(mockConsoleLog.mock.calls[2][0])).toMatchSnapshot(dynamicProps); +import { of, Subject } from 'rxjs'; + +import { LoggingService, InternalLoggingServiceSetup } from './logging_service'; +import { loggingSystemMock } from './logging_system.mock'; +import { LoggerContextConfigType } from './logging_config'; + +describe('LoggingService', () => { + let loggingSystem: ReturnType; + let service: LoggingService; + let setup: InternalLoggingServiceSetup; + + beforeEach(() => { + loggingSystem = loggingSystemMock.create(); + service = new LoggingService({ logger: loggingSystem.asLoggerFactory() } as any); + setup = service.setup({ loggingSystem }); + }); + afterEach(() => { + service.stop(); + }); + + describe('setup', () => { + it('forwards configuration changes to logging system', () => { + const config1: LoggerContextConfigType = { + appenders: new Map(), + loggers: [{ context: 'subcontext', appenders: ['console'], level: 'warn' }], + }; + const config2: LoggerContextConfigType = { + appenders: new Map(), + loggers: [{ context: 'subcontext', appenders: ['default'], level: 'all' }], + }; + + setup.configure(['test', 'context'], of(config1, config2)); + expect(loggingSystem.setContextConfig).toHaveBeenNthCalledWith( + 1, + ['test', 'context'], + config1 + ); + expect(loggingSystem.setContextConfig).toHaveBeenNthCalledWith( + 2, + ['test', 'context'], + config2 + ); + }); + + it('stops forwarding first observable when called a second time', () => { + const updates$ = new Subject(); + const config1: LoggerContextConfigType = { + appenders: new Map(), + loggers: [{ context: 'subcontext', appenders: ['console'], level: 'warn' }], + }; + const config2: LoggerContextConfigType = { + appenders: new Map(), + loggers: [{ context: 'subcontext', appenders: ['default'], level: 'all' }], + }; + + setup.configure(['test', 'context'], updates$); + setup.configure(['test', 'context'], of(config1)); + updates$.next(config2); + expect(loggingSystem.setContextConfig).toHaveBeenNthCalledWith( + 1, + ['test', 'context'], + config1 + ); + expect(loggingSystem.setContextConfig).not.toHaveBeenCalledWith(['test', 'context'], config2); + }); + }); + + describe('stop', () => { + it('stops forwarding updates to logging system', () => { + const updates$ = new Subject(); + const config1: LoggerContextConfigType = { + appenders: new Map(), + loggers: [{ context: 'subcontext', appenders: ['console'], level: 'warn' }], + }; + + setup.configure(['test', 'context'], updates$); + service.stop(); + updates$.next(config1); + expect(loggingSystem.setContextConfig).not.toHaveBeenCalledWith(['test', 'context'], config1); + }); + }); }); diff --git a/src/core/server/logging/logging_service.ts b/src/core/server/logging/logging_service.ts index 2e6f8957241228..09051f8f077023 100644 --- a/src/core/server/logging/logging_service.ts +++ b/src/core/server/logging/logging_service.ts @@ -16,112 +16,88 @@ * specific language governing permissions and limitations * under the License. */ -import { Appenders, DisposableAppender } from './appenders/appenders'; -import { BufferAppender } from './appenders/buffer/buffer_appender'; -import { LogLevel } from './log_level'; -import { BaseLogger, Logger } from './logger'; -import { LoggerAdapter } from './logger_adapter'; -import { LoggerFactory } from './logger_factory'; -import { LoggingConfigType, LoggerConfigType, LoggingConfig } from './logging_config'; -export type ILoggingService = PublicMethodsOf; +import { Observable, Subscription } from 'rxjs'; +import { CoreService } from '../../types'; +import { LoggingConfig, LoggerContextConfigInput } from './logging_config'; +import { ILoggingSystem } from './logging_system'; +import { Logger } from './logger'; +import { CoreContext } from '../core_context'; + /** - * Service that is responsible for maintaining loggers and logger appenders. - * @internal + * Provides APIs to plugins for customizing the plugin's logger. + * @public */ -export class LoggingService implements LoggerFactory { - private config?: LoggingConfig; - private readonly appenders: Map = new Map(); - private readonly bufferAppender = new BufferAppender(); - private readonly loggers: Map = new Map(); - - public get(...contextParts: string[]): Logger { - const context = LoggingConfig.getLoggerContext(contextParts); - if (!this.loggers.has(context)) { - this.loggers.set(context, new LoggerAdapter(this.createLogger(context, this.config))); - } - return this.loggers.get(context)!; - } - - /** - * Safe wrapper that allows passing logging service as immutable LoggerFactory. - */ - public asLoggerFactory(): LoggerFactory { - return { get: (...contextParts: string[]) => this.get(...contextParts) }; - } - +export interface LoggingServiceSetup { /** - * Updates all current active loggers with the new config values. - * @param rawConfig New config instance. + * Customizes the logging config for the plugin's context. + * + * @remarks + * Assumes that that the `context` property of the individual `logger` items emitted by `config$` + * are relative to the plugin's logging context (defaults to `plugins.`). + * + * @example + * Customize the configuration for the plugins.data.search context. + * ```ts + * core.logging.configure( + * of({ + * appenders: new Map(), + * loggers: [{ context: 'search', appenders: ['default'] }] + * }) + * ) + * ``` + * + * @param config$ */ - public upgrade(rawConfig: LoggingConfigType) { - const config = new LoggingConfig(rawConfig); - // Config update is asynchronous and may require some time to complete, so we should invalidate - // config so that new loggers will be using BufferAppender until newly configured appenders are ready. - this.config = undefined; - - // Appenders must be reset, so we first dispose of the current ones, then - // build up a new set of appenders. - for (const appender of this.appenders.values()) { - appender.dispose(); - } - this.appenders.clear(); + configure(config$: Observable): void; +} - for (const [appenderKey, appenderConfig] of config.appenders) { - this.appenders.set(appenderKey, Appenders.create(appenderConfig)); - } +/** @internal */ +export interface InternalLoggingServiceSetup { + configure(contextParts: string[], config$: Observable): void; +} - for (const [loggerKey, loggerAdapter] of this.loggers) { - loggerAdapter.updateLogger(this.createLogger(loggerKey, config)); - } +interface SetupDeps { + loggingSystem: ILoggingSystem; +} - this.config = config; +/** @internal */ +export class LoggingService implements CoreService { + private readonly subscriptions = new Map(); + private readonly log: Logger; - // Re-log all buffered log records with newly configured appenders. - for (const logRecord of this.bufferAppender.flush()) { - this.get(logRecord.context).log(logRecord); - } + constructor(coreContext: CoreContext) { + this.log = coreContext.logger.get('logging'); } - /** - * Disposes all loggers (closes log files, clears buffers etc.). Service is not usable after - * calling of this method until new config is provided via `upgrade` method. - * @returns Promise that is resolved once all loggers are successfully disposed. - */ - public async stop() { - for (const appender of this.appenders.values()) { - await appender.dispose(); - } - - await this.bufferAppender.dispose(); - - this.appenders.clear(); - this.loggers.clear(); + public setup({ loggingSystem }: SetupDeps) { + return { + configure: (contextParts: string[], config$: Observable) => { + const contextName = LoggingConfig.getLoggerContext(contextParts); + this.log.debug(`Setting custom config for context [${contextName}]`); + + const existingSubscription = this.subscriptions.get(contextName); + if (existingSubscription) { + existingSubscription.unsubscribe(); + } + + // Might be fancier way to do this with rxjs, but this works and is simple to understand + this.subscriptions.set( + contextName, + config$.subscribe((config) => { + this.log.debug(`Updating logging config for context [${contextName}]`); + loggingSystem.setContextConfig(contextParts, config); + }) + ); + }, + }; } - private createLogger(context: string, config: LoggingConfig | undefined) { - if (config === undefined) { - // If we don't have config yet, use `buffered` appender that will store all logged messages in the memory - // until the config is ready. - return new BaseLogger(context, LogLevel.All, [this.bufferAppender], this.asLoggerFactory()); - } - - const { level, appenders } = this.getLoggerConfigByContext(config, context); - const loggerLevel = LogLevel.fromId(level); - const loggerAppenders = appenders.map((appenderKey) => this.appenders.get(appenderKey)!); + public start() {} - return new BaseLogger(context, loggerLevel, loggerAppenders, this.asLoggerFactory()); - } - - private getLoggerConfigByContext(config: LoggingConfig, context: string): LoggerConfigType { - const loggerConfig = config.loggers.get(context); - if (loggerConfig !== undefined) { - return loggerConfig; + public stop() { + for (const [, subscription] of this.subscriptions) { + subscription.unsubscribe(); } - - // If we don't have configuration for the specified context and it's the "nested" one (eg. `foo.bar.baz`), - // let's move up to the parent context (eg. `foo.bar`) and check if it has config we can rely on. Otherwise - // we fallback to the `root` context that should always be defined (enforced by configuration schema). - return this.getLoggerConfigByContext(config, LoggingConfig.getParentLoggerContext(context)); } } diff --git a/src/core/server/logging/logging_system.mock.ts b/src/core/server/logging/logging_system.mock.ts new file mode 100644 index 00000000000000..ac1e9b5196002e --- /dev/null +++ b/src/core/server/logging/logging_system.mock.ts @@ -0,0 +1,84 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// Test helpers to simplify mocking logs and collecting all their outputs +import { ILoggingSystem } from './logging_system'; +import { LoggerFactory } from './logger_factory'; +import { loggerMock, MockedLogger } from './logger.mock'; + +const createLoggingSystemMock = () => { + const mockLog = loggerMock.create(); + + mockLog.get.mockImplementation((...context) => ({ + ...mockLog, + context, + })); + + const mocked: jest.Mocked = { + get: jest.fn(), + asLoggerFactory: jest.fn(), + setContextConfig: jest.fn(), + upgrade: jest.fn(), + stop: jest.fn(), + }; + mocked.get.mockImplementation((...context) => ({ + ...mockLog, + context, + })); + mocked.asLoggerFactory.mockImplementation(() => mocked); + mocked.stop.mockResolvedValue(); + return mocked; +}; + +const collectLoggingSystemMock = (loggerFactory: LoggerFactory) => { + const mockLog = loggerFactory.get() as MockedLogger; + return { + debug: mockLog.debug.mock.calls, + error: mockLog.error.mock.calls, + fatal: mockLog.fatal.mock.calls, + info: mockLog.info.mock.calls, + log: mockLog.log.mock.calls, + trace: mockLog.trace.mock.calls, + warn: mockLog.warn.mock.calls, + }; +}; + +const clearLoggingSystemMock = (loggerFactory: LoggerFactory) => { + const mockedLoggerFactory = (loggerFactory as unknown) as jest.Mocked; + mockedLoggerFactory.get.mockClear(); + mockedLoggerFactory.asLoggerFactory.mockClear(); + mockedLoggerFactory.upgrade.mockClear(); + mockedLoggerFactory.stop.mockClear(); + + const mockLog = loggerFactory.get() as MockedLogger; + mockLog.debug.mockClear(); + mockLog.info.mockClear(); + mockLog.warn.mockClear(); + mockLog.error.mockClear(); + mockLog.trace.mockClear(); + mockLog.fatal.mockClear(); + mockLog.log.mockClear(); +}; + +export const loggingSystemMock = { + create: createLoggingSystemMock, + collect: collectLoggingSystemMock, + clear: clearLoggingSystemMock, + createLogger: loggerMock.create, +}; diff --git a/src/core/server/logging/logging_system.test.ts b/src/core/server/logging/logging_system.test.ts new file mode 100644 index 00000000000000..f73e40fe320dc3 --- /dev/null +++ b/src/core/server/logging/logging_system.test.ts @@ -0,0 +1,348 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +const mockStreamWrite = jest.fn(); +jest.mock('fs', () => ({ + constants: {}, + createWriteStream: jest.fn(() => ({ write: mockStreamWrite })), +})); + +const dynamicProps = { pid: expect.any(Number) }; + +jest.mock('../../../legacy/server/logging/rotate', () => ({ + setupLoggingRotate: jest.fn().mockImplementation(() => Promise.resolve({})), +})); + +const timestamp = new Date(Date.UTC(2012, 1, 1, 14, 33, 22, 11)); +let mockConsoleLog: jest.SpyInstance; + +import { createWriteStream } from 'fs'; +const mockCreateWriteStream = (createWriteStream as unknown) as jest.Mock; + +import { LoggingSystem, config } from '.'; + +let system: LoggingSystem; +beforeEach(() => { + mockConsoleLog = jest.spyOn(global.console, 'log').mockReturnValue(undefined); + jest.spyOn(global, 'Date').mockImplementation(() => timestamp); + system = new LoggingSystem(); +}); + +afterEach(() => { + jest.restoreAllMocks(); + mockCreateWriteStream.mockClear(); + mockStreamWrite.mockClear(); +}); + +test('uses default memory buffer logger until config is provided', () => { + const bufferAppendSpy = jest.spyOn((system as any).bufferAppender, 'append'); + + const logger = system.get('test', 'context'); + logger.trace('trace message'); + + // We shouldn't create new buffer appender for another context. + const anotherLogger = system.get('test', 'context2'); + anotherLogger.fatal('fatal message', { some: 'value' }); + + expect(bufferAppendSpy).toHaveBeenCalledTimes(2); + expect(bufferAppendSpy.mock.calls[0][0]).toMatchSnapshot(dynamicProps); + expect(bufferAppendSpy.mock.calls[1][0]).toMatchSnapshot(dynamicProps); +}); + +test('flushes memory buffer logger and switches to real logger once config is provided', () => { + const logger = system.get('test', 'context'); + + logger.trace('buffered trace message'); + logger.info('buffered info message', { some: 'value' }); + logger.fatal('buffered fatal message'); + + const bufferAppendSpy = jest.spyOn((system as any).bufferAppender, 'append'); + + // Switch to console appender with `info` level, so that `trace` message won't go through. + system.upgrade( + config.schema.validate({ + appenders: { default: { kind: 'console', layout: { kind: 'json' } } }, + root: { level: 'info' }, + }) + ); + + expect(JSON.parse(mockConsoleLog.mock.calls[0][0])).toMatchSnapshot( + dynamicProps, + 'buffered messages' + ); + mockConsoleLog.mockClear(); + + // Now message should go straight to thew newly configured appender, not buffered one. + logger.info('some new info message'); + expect(JSON.parse(mockConsoleLog.mock.calls[0][0])).toMatchSnapshot(dynamicProps, 'new messages'); + expect(bufferAppendSpy).not.toHaveBeenCalled(); +}); + +test('appends records via multiple appenders.', () => { + const loggerWithoutConfig = system.get('some-context'); + const testsLogger = system.get('tests'); + const testsChildLogger = system.get('tests', 'child'); + + loggerWithoutConfig.info('You know, just for your info.'); + testsLogger.warn('Config is not ready!'); + testsChildLogger.error('Too bad that config is not ready :/'); + testsChildLogger.info('Just some info that should not be logged.'); + + expect(mockConsoleLog).not.toHaveBeenCalled(); + expect(mockCreateWriteStream).not.toHaveBeenCalled(); + + system.upgrade( + config.schema.validate({ + appenders: { + default: { kind: 'console', layout: { kind: 'pattern' } }, + file: { kind: 'file', layout: { kind: 'pattern' }, path: 'path' }, + }, + loggers: [ + { appenders: ['file'], context: 'tests', level: 'warn' }, + { context: 'tests.child', level: 'error' }, + ], + }) + ); + + // Now all logs should added to configured appenders. + expect(mockConsoleLog).toHaveBeenCalledTimes(1); + expect(mockConsoleLog.mock.calls[0][0]).toMatchSnapshot('console logs'); + + expect(mockStreamWrite).toHaveBeenCalledTimes(2); + expect(mockStreamWrite.mock.calls[0][0]).toMatchSnapshot('file logs'); + expect(mockStreamWrite.mock.calls[1][0]).toMatchSnapshot('file logs'); +}); + +test('uses `root` logger if context is not specified.', () => { + system.upgrade( + config.schema.validate({ + appenders: { default: { kind: 'console', layout: { kind: 'pattern' } } }, + }) + ); + + const rootLogger = system.get(); + rootLogger.info('This message goes to a root context.'); + + expect(mockConsoleLog.mock.calls).toMatchSnapshot(); +}); + +test('`stop()` disposes all appenders.', async () => { + system.upgrade( + config.schema.validate({ + appenders: { default: { kind: 'console', layout: { kind: 'json' } } }, + root: { level: 'info' }, + }) + ); + + const bufferDisposeSpy = jest.spyOn((system as any).bufferAppender, 'dispose'); + const consoleDisposeSpy = jest.spyOn((system as any).appenders.get('default'), 'dispose'); + + await system.stop(); + + expect(bufferDisposeSpy).toHaveBeenCalledTimes(1); + expect(consoleDisposeSpy).toHaveBeenCalledTimes(1); +}); + +test('asLoggerFactory() only allows to create new loggers.', () => { + const logger = system.asLoggerFactory().get('test', 'context'); + + system.upgrade( + config.schema.validate({ + appenders: { default: { kind: 'console', layout: { kind: 'json' } } }, + root: { level: 'all' }, + }) + ); + + logger.trace('buffered trace message'); + logger.info('buffered info message', { some: 'value' }); + logger.fatal('buffered fatal message'); + + expect(Object.keys(system.asLoggerFactory())).toEqual(['get']); + + expect(mockConsoleLog).toHaveBeenCalledTimes(3); + expect(JSON.parse(mockConsoleLog.mock.calls[0][0])).toMatchSnapshot(dynamicProps); + expect(JSON.parse(mockConsoleLog.mock.calls[1][0])).toMatchSnapshot(dynamicProps); + expect(JSON.parse(mockConsoleLog.mock.calls[2][0])).toMatchSnapshot(dynamicProps); +}); + +test('setContextConfig() updates config with relative contexts', () => { + const testsLogger = system.get('tests'); + const testsChildLogger = system.get('tests', 'child'); + const testsGrandchildLogger = system.get('tests', 'child', 'grandchild'); + + system.upgrade( + config.schema.validate({ + appenders: { default: { kind: 'console', layout: { kind: 'json' } } }, + root: { level: 'info' }, + }) + ); + + system.setContextConfig(['tests', 'child'], { + appenders: new Map([ + [ + 'custom', + { kind: 'console', layout: { kind: 'pattern', pattern: '[%level][%logger] %message' } }, + ], + ]), + loggers: [{ context: 'grandchild', appenders: ['default', 'custom'], level: 'debug' }], + }); + + testsLogger.warn('tests log to default!'); + testsChildLogger.error('tests.child log to default!'); + testsGrandchildLogger.debug('tests.child.grandchild log to default and custom!'); + + expect(mockConsoleLog).toHaveBeenCalledTimes(4); + // Parent contexts are unaffected + expect(JSON.parse(mockConsoleLog.mock.calls[0][0])).toMatchObject({ + context: 'tests', + message: 'tests log to default!', + level: 'WARN', + }); + expect(JSON.parse(mockConsoleLog.mock.calls[1][0])).toMatchObject({ + context: 'tests.child', + message: 'tests.child log to default!', + level: 'ERROR', + }); + // Customized context is logged in both appender formats + expect(JSON.parse(mockConsoleLog.mock.calls[2][0])).toMatchObject({ + context: 'tests.child.grandchild', + message: 'tests.child.grandchild log to default and custom!', + level: 'DEBUG', + }); + expect(mockConsoleLog.mock.calls[3][0]).toMatchInlineSnapshot( + `"[DEBUG][tests.child.grandchild] tests.child.grandchild log to default and custom!"` + ); +}); + +test('custom context configs are applied on subsequent calls to update()', () => { + system.setContextConfig(['tests', 'child'], { + appenders: new Map([ + [ + 'custom', + { kind: 'console', layout: { kind: 'pattern', pattern: '[%level][%logger] %message' } }, + ], + ]), + loggers: [{ context: 'grandchild', appenders: ['default', 'custom'], level: 'debug' }], + }); + + // Calling upgrade after setContextConfig should not throw away the context-specific config + system.upgrade( + config.schema.validate({ + appenders: { default: { kind: 'console', layout: { kind: 'json' } } }, + root: { level: 'info' }, + }) + ); + + system + .get('tests', 'child', 'grandchild') + .debug('tests.child.grandchild log to default and custom!'); + + // Customized context is logged in both appender formats still + expect(mockConsoleLog).toHaveBeenCalledTimes(2); + expect(JSON.parse(mockConsoleLog.mock.calls[0][0])).toMatchObject({ + context: 'tests.child.grandchild', + message: 'tests.child.grandchild log to default and custom!', + level: 'DEBUG', + }); + expect(mockConsoleLog.mock.calls[1][0]).toMatchInlineSnapshot( + `"[DEBUG][tests.child.grandchild] tests.child.grandchild log to default and custom!"` + ); +}); + +test('subsequent calls to setContextConfig() for the same context override the previous config', () => { + system.upgrade( + config.schema.validate({ + appenders: { default: { kind: 'console', layout: { kind: 'json' } } }, + root: { level: 'info' }, + }) + ); + + system.setContextConfig(['tests', 'child'], { + appenders: new Map([ + [ + 'custom', + { kind: 'console', layout: { kind: 'pattern', pattern: '[%level][%logger] %message' } }, + ], + ]), + loggers: [{ context: 'grandchild', appenders: ['default', 'custom'], level: 'debug' }], + }); + + // Call again, this time with level: 'warn' and a different pattern + system.setContextConfig(['tests', 'child'], { + appenders: new Map([ + [ + 'custom', + { + kind: 'console', + layout: { kind: 'pattern', pattern: '[%level][%logger] second pattern! %message' }, + }, + ], + ]), + loggers: [{ context: 'grandchild', appenders: ['default', 'custom'], level: 'warn' }], + }); + + const logger = system.get('tests', 'child', 'grandchild'); + logger.debug('this should not show anywhere!'); + logger.warn('tests.child.grandchild log to default and custom!'); + + // Only the warn log should have been logged + expect(mockConsoleLog).toHaveBeenCalledTimes(2); + expect(JSON.parse(mockConsoleLog.mock.calls[0][0])).toMatchObject({ + context: 'tests.child.grandchild', + message: 'tests.child.grandchild log to default and custom!', + level: 'WARN', + }); + expect(mockConsoleLog.mock.calls[1][0]).toMatchInlineSnapshot( + `"[WARN ][tests.child.grandchild] second pattern! tests.child.grandchild log to default and custom!"` + ); +}); + +test('subsequent calls to setContextConfig() for the same context can disable the previous config', () => { + system.upgrade( + config.schema.validate({ + appenders: { default: { kind: 'console', layout: { kind: 'json' } } }, + root: { level: 'info' }, + }) + ); + + system.setContextConfig(['tests', 'child'], { + appenders: new Map([ + [ + 'custom', + { kind: 'console', layout: { kind: 'pattern', pattern: '[%level][%logger] %message' } }, + ], + ]), + loggers: [{ context: 'grandchild', appenders: ['default', 'custom'], level: 'debug' }], + }); + + // Call again, this time no customizations (effectively disabling) + system.setContextConfig(['tests', 'child'], {}); + + const logger = system.get('tests', 'child', 'grandchild'); + logger.debug('this should not show anywhere!'); + logger.warn('tests.child.grandchild log to default!'); + + // Only the warn log should have been logged once on the default appender + expect(mockConsoleLog).toHaveBeenCalledTimes(1); + expect(JSON.parse(mockConsoleLog.mock.calls[0][0])).toMatchObject({ + context: 'tests.child.grandchild', + message: 'tests.child.grandchild log to default!', + level: 'WARN', + }); +}); diff --git a/src/core/server/logging/logging_system.ts b/src/core/server/logging/logging_system.ts new file mode 100644 index 00000000000000..0bab9534d2d053 --- /dev/null +++ b/src/core/server/logging/logging_system.ts @@ -0,0 +1,185 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { Appenders, DisposableAppender } from './appenders/appenders'; +import { BufferAppender } from './appenders/buffer/buffer_appender'; +import { LogLevel } from './log_level'; +import { BaseLogger, Logger } from './logger'; +import { LoggerAdapter } from './logger_adapter'; +import { LoggerFactory } from './logger_factory'; +import { + LoggingConfigType, + LoggerConfigType, + LoggingConfig, + LoggerContextConfigType, + LoggerContextConfigInput, + loggerContextConfigSchema, +} from './logging_config'; + +export type ILoggingSystem = PublicMethodsOf; + +/** + * System that is responsible for maintaining loggers and logger appenders. + * @internal + */ +export class LoggingSystem implements LoggerFactory { + /** The configuration set by the user. */ + private baseConfig?: LoggingConfig; + /** The fully computed configuration extended by context-specific configurations set programmatically */ + private computedConfig?: LoggingConfig; + private readonly appenders: Map = new Map(); + private readonly bufferAppender = new BufferAppender(); + private readonly loggers: Map = new Map(); + private readonly contextConfigs = new Map(); + + public get(...contextParts: string[]): Logger { + const context = LoggingConfig.getLoggerContext(contextParts); + if (!this.loggers.has(context)) { + this.loggers.set(context, new LoggerAdapter(this.createLogger(context, this.computedConfig))); + } + return this.loggers.get(context)!; + } + + /** + * Safe wrapper that allows passing logging service as immutable LoggerFactory. + */ + public asLoggerFactory(): LoggerFactory { + return { get: (...contextParts: string[]) => this.get(...contextParts) }; + } + + /** + * Updates all current active loggers with the new config values. + * @param rawConfig New config instance. + */ + public upgrade(rawConfig: LoggingConfigType) { + const config = new LoggingConfig(rawConfig)!; + this.applyBaseConfig(config); + } + + /** + * Customizes the logging config for a specific context. + * + * @remarks + * Assumes that that the `context` property of the individual items in `rawConfig.loggers` + * are relative to the `baseContextParts`. + * + * @example + * Customize the configuration for the plugins.data.search context. + * ```ts + * loggingSystem.setContextConfig( + * ['plugins', 'data'], + * { + * loggers: [{ context: 'search', appenders: ['default'] }] + * } + * ) + * ``` + * + * @param baseContextParts + * @param rawConfig + */ + public setContextConfig(baseContextParts: string[], rawConfig: LoggerContextConfigInput) { + const context = LoggingConfig.getLoggerContext(baseContextParts); + const contextConfig = loggerContextConfigSchema.validate(rawConfig); + this.contextConfigs.set(context, { + ...contextConfig, + // Automatically prepend the base context to the logger sub-contexts + loggers: contextConfig.loggers.map((l) => ({ + ...l, + context: LoggingConfig.getLoggerContext([context, l.context]), + })), + }); + + // If we already have a base config, apply the config. If not, custom context configs + // will be picked up on next call to `upgrade`. + if (this.baseConfig) { + this.applyBaseConfig(this.baseConfig); + } + } + + /** + * Disposes all loggers (closes log files, clears buffers etc.). Service is not usable after + * calling of this method until new config is provided via `upgrade` method. + * @returns Promise that is resolved once all loggers are successfully disposed. + */ + public async stop() { + await Promise.all([...this.appenders.values()].map((a) => a.dispose())); + + await this.bufferAppender.dispose(); + + this.appenders.clear(); + this.loggers.clear(); + } + + private createLogger(context: string, config: LoggingConfig | undefined) { + if (config === undefined) { + // If we don't have config yet, use `buffered` appender that will store all logged messages in the memory + // until the config is ready. + return new BaseLogger(context, LogLevel.All, [this.bufferAppender], this.asLoggerFactory()); + } + + const { level, appenders } = this.getLoggerConfigByContext(config, context); + const loggerLevel = LogLevel.fromId(level); + const loggerAppenders = appenders.map((appenderKey) => this.appenders.get(appenderKey)!); + + return new BaseLogger(context, loggerLevel, loggerAppenders, this.asLoggerFactory()); + } + + private getLoggerConfigByContext(config: LoggingConfig, context: string): LoggerConfigType { + const loggerConfig = config.loggers.get(context); + if (loggerConfig !== undefined) { + return loggerConfig; + } + + // If we don't have configuration for the specified context and it's the "nested" one (eg. `foo.bar.baz`), + // let's move up to the parent context (eg. `foo.bar`) and check if it has config we can rely on. Otherwise + // we fallback to the `root` context that should always be defined (enforced by configuration schema). + return this.getLoggerConfigByContext(config, LoggingConfig.getParentLoggerContext(context)); + } + + private applyBaseConfig(newBaseConfig: LoggingConfig) { + const computedConfig = [...this.contextConfigs.values()].reduce( + (baseConfig, contextConfig) => baseConfig.extend(contextConfig), + newBaseConfig + ); + + // Appenders must be reset, so we first dispose of the current ones, then + // build up a new set of appenders. + for (const appender of this.appenders.values()) { + appender.dispose(); + } + this.appenders.clear(); + + for (const [appenderKey, appenderConfig] of computedConfig.appenders) { + this.appenders.set(appenderKey, Appenders.create(appenderConfig)); + } + + for (const [loggerKey, loggerAdapter] of this.loggers) { + loggerAdapter.updateLogger(this.createLogger(loggerKey, computedConfig)); + } + + // We keep a reference to the base config so we can properly extend it + // on each config change. + this.baseConfig = newBaseConfig; + this.computedConfig = computedConfig; + + // Re-log all buffered log records with newly configured appenders. + for (const logRecord of this.bufferAppender.flush()) { + this.get(logRecord.context).log(logRecord); + } + } +} diff --git a/src/core/server/mocks.ts b/src/core/server/mocks.ts index f3ae5462f16316..0770e8843e2f63 100644 --- a/src/core/server/mocks.ts +++ b/src/core/server/mocks.ts @@ -19,6 +19,7 @@ import { of } from 'rxjs'; import { duration } from 'moment'; import { PluginInitializerContext, CoreSetup, CoreStart, StartServicesAccessor } from '.'; +import { loggingSystemMock } from './logging/logging_system.mock'; import { loggingServiceMock } from './logging/logging_service.mock'; import { elasticsearchServiceMock } from './elasticsearch/elasticsearch_service.mock'; import { httpServiceMock } from './http/http_service.mock'; @@ -42,7 +43,7 @@ export { sessionStorageMock } from './http/cookie_session_storage.mocks'; export { configServiceMock } from './config/config_service.mock'; export { elasticsearchServiceMock } from './elasticsearch/elasticsearch_service.mock'; export { httpServiceMock } from './http/http_service.mock'; -export { loggingServiceMock } from './logging/logging_service.mock'; +export { loggingSystemMock } from './logging/logging_system.mock'; export { savedObjectsRepositoryMock } from './saved_objects/service/lib/repository.mock'; export { savedObjectsServiceMock } from './saved_objects/saved_objects_service.mock'; export { typeRegistryMock as savedObjectsTypeRegistryMock } from './saved_objects/saved_objects_type_registry.mock'; @@ -78,7 +79,7 @@ export function pluginInitializerContextConfigMock(config: T) { function pluginInitializerContextMock(config: T = {} as T) { const mock: PluginInitializerContext = { opaqueId: Symbol(), - logger: loggingServiceMock.create(), + logger: loggingSystemMock.create(), env: { mode: { dev: true, @@ -130,6 +131,7 @@ function createCoreSetupMock({ metrics: metricsServiceMock.createSetupContract(), uiSettings: uiSettingsMock, uuid: uuidServiceMock.createSetupContract(), + logging: loggingServiceMock.createSetupContract(), getStartServices: jest .fn, object, any]>, []>() .mockResolvedValue([createCoreStartMock(), pluginStartDeps, pluginStartContract]), @@ -163,6 +165,7 @@ function createInternalCoreSetupMock() { httpResources: httpResourcesMock.createSetupContract(), rendering: renderingMock.createSetupContract(), uiSettings: uiSettingsServiceMock.createSetupContract(), + logging: loggingServiceMock.createInternalSetupContract(), }; return setupDeps; } diff --git a/src/core/server/plugins/discovery/plugin_manifest_parser.test.ts b/src/core/server/plugins/discovery/plugin_manifest_parser.test.ts index 979accb1f769ef..5ffdef88104c83 100644 --- a/src/core/server/plugins/discovery/plugin_manifest_parser.test.ts +++ b/src/core/server/plugins/discovery/plugin_manifest_parser.test.ts @@ -20,12 +20,12 @@ import { PluginDiscoveryErrorType } from './plugin_discovery_error'; import { mockReadFile } from './plugin_manifest_parser.test.mocks'; -import { loggingServiceMock } from '../../logging/logging_service.mock'; +import { loggingSystemMock } from '../../logging/logging_system.mock'; import { resolve } from 'path'; import { parseManifest } from './plugin_manifest_parser'; -const logger = loggingServiceMock.createLogger(); +const logger = loggingSystemMock.createLogger(); const pluginPath = resolve('path', 'existent-dir'); const pluginManifestPath = resolve(pluginPath, 'kibana.json'); const packageInfo = { @@ -105,9 +105,9 @@ test('logs warning if pluginId is not in camelCase format', async () => { cb(null, Buffer.from(JSON.stringify({ id: 'some_name', version: 'kibana', server: true }))); }); - expect(loggingServiceMock.collect(logger).warn).toHaveLength(0); + expect(loggingSystemMock.collect(logger).warn).toHaveLength(0); await parseManifest(pluginPath, packageInfo, logger); - expect(loggingServiceMock.collect(logger).warn).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).warn).toMatchInlineSnapshot(` Array [ Array [ "Expect plugin \\"id\\" in camelCase, but found: some_name", diff --git a/src/core/server/plugins/discovery/plugins_discovery.test.ts b/src/core/server/plugins/discovery/plugins_discovery.test.ts index 73f274957cbc46..1c42f5dcfc7a70 100644 --- a/src/core/server/plugins/discovery/plugins_discovery.test.ts +++ b/src/core/server/plugins/discovery/plugins_discovery.test.ts @@ -19,7 +19,7 @@ import { mockPackage, mockReaddir, mockReadFile, mockStat } from './plugins_discovery.test.mocks'; import { rawConfigServiceMock } from '../../config/raw_config_service.mock'; -import { loggingServiceMock } from '../../logging/logging_service.mock'; +import { loggingSystemMock } from '../../logging/logging_system.mock'; import { resolve } from 'path'; import { first, map, toArray } from 'rxjs/operators'; @@ -37,7 +37,7 @@ const TEST_PLUGIN_SEARCH_PATHS = { }; const TEST_EXTRA_PLUGIN_PATH = resolve(process.cwd(), 'my-extra-plugin'); -const logger = loggingServiceMock.create(); +const logger = loggingSystemMock.create(); beforeEach(() => { mockReaddir.mockImplementation((path, cb) => { @@ -221,7 +221,7 @@ test('logs a warning about --plugin-path when used in development', async () => logger, }); - expect(loggingServiceMock.collect(logger).warn).toEqual([ + expect(loggingSystemMock.collect(logger).warn).toEqual([ [ `Explicit plugin paths [${TEST_EXTRA_PLUGIN_PATH}] should only be used in development. Relative imports may not work properly in production.`, ], @@ -263,5 +263,5 @@ test('does not log a warning about --plugin-path when used in production', async logger, }); - expect(loggingServiceMock.collect(logger).warn).toEqual([]); + expect(loggingSystemMock.collect(logger).warn).toEqual([]); }); diff --git a/src/core/server/plugins/integration_tests/plugins_service.test.ts b/src/core/server/plugins/integration_tests/plugins_service.test.ts index 04f570cca489bd..e676c789449caf 100644 --- a/src/core/server/plugins/integration_tests/plugins_service.test.ts +++ b/src/core/server/plugins/integration_tests/plugins_service.test.ts @@ -27,13 +27,13 @@ import { getEnvOptions } from '../../config/__mocks__/env'; import { BehaviorSubject, from } from 'rxjs'; import { rawConfigServiceMock } from '../../config/raw_config_service.mock'; import { config } from '../plugins_config'; -import { loggingServiceMock } from '../../logging/logging_service.mock'; +import { loggingSystemMock } from '../../logging/logging_system.mock'; import { coreMock } from '../../mocks'; import { Plugin } from '../types'; import { PluginWrapper } from '../plugin'; describe('PluginsService', () => { - const logger = loggingServiceMock.create(); + const logger = loggingSystemMock.create(); let pluginsService: PluginsService; const createPlugin = ( diff --git a/src/core/server/plugins/plugin.test.ts b/src/core/server/plugins/plugin.test.ts index 8d82d96f949c71..ec0a3986b48775 100644 --- a/src/core/server/plugins/plugin.test.ts +++ b/src/core/server/plugins/plugin.test.ts @@ -26,14 +26,14 @@ import { getEnvOptions } from '../config/__mocks__/env'; import { CoreContext } from '../core_context'; import { coreMock } from '../mocks'; import { configServiceMock } from '../config/config_service.mock'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; import { PluginWrapper } from './plugin'; import { PluginManifest } from './types'; import { createPluginInitializerContext, createPluginSetupContext } from './plugin_context'; const mockPluginInitializer = jest.fn(); -const logger = loggingServiceMock.create(); +const logger = loggingSystemMock.create(); jest.doMock( join('plugin-with-initializer-path', 'server'), () => ({ plugin: mockPluginInitializer }), diff --git a/src/core/server/plugins/plugin.ts b/src/core/server/plugins/plugin.ts index d7cfaa14d23434..2e5881c6518439 100644 --- a/src/core/server/plugins/plugin.ts +++ b/src/core/server/plugins/plugin.ts @@ -95,8 +95,6 @@ export class PluginWrapper< public async setup(setupContext: CoreSetup, plugins: TPluginsSetup) { this.instance = this.createPluginInstance(); - this.log.debug('Setting up plugin'); - return this.instance.setup(setupContext, plugins); } @@ -112,8 +110,6 @@ export class PluginWrapper< throw new Error(`Plugin "${this.name}" can't be started since it isn't set up.`); } - this.log.debug('Starting plugin'); - const startContract = await this.instance.start(startContext, plugins); this.startDependencies$.next([startContext, plugins, startContract]); return startContract; @@ -127,8 +123,6 @@ export class PluginWrapper< throw new Error(`Plugin "${this.name}" can't be stopped since it isn't set up.`); } - this.log.info('Stopping plugin'); - if (typeof this.instance.stop === 'function') { await this.instance.stop(); } diff --git a/src/core/server/plugins/plugin_context.test.ts b/src/core/server/plugins/plugin_context.test.ts index 54350d96984b41..69b354661abc9d 100644 --- a/src/core/server/plugins/plugin_context.test.ts +++ b/src/core/server/plugins/plugin_context.test.ts @@ -22,14 +22,14 @@ import { first } from 'rxjs/operators'; import { createPluginInitializerContext } from './plugin_context'; import { CoreContext } from '../core_context'; import { Env } from '../config'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; import { rawConfigServiceMock } from '../config/raw_config_service.mock'; import { getEnvOptions } from '../config/__mocks__/env'; import { PluginManifest } from './types'; import { Server } from '../server'; import { fromRoot } from '../utils'; -const logger = loggingServiceMock.create(); +const logger = loggingSystemMock.create(); let coreId: symbol; let env: Env; diff --git a/src/core/server/plugins/plugin_context.ts b/src/core/server/plugins/plugin_context.ts index 31e36db49223a7..32bc8dc088cad1 100644 --- a/src/core/server/plugins/plugin_context.ts +++ b/src/core/server/plugins/plugin_context.ts @@ -166,6 +166,9 @@ export function createPluginSetupContext( csp: deps.http.csp, getServerInfo: deps.http.getServerInfo, }, + logging: { + configure: (config$) => deps.logging.configure(['plugins', plugin.name], config$), + }, metrics: { getOpsMetrics$: deps.metrics.getOpsMetrics$, }, diff --git a/src/core/server/plugins/plugins_service.test.ts b/src/core/server/plugins/plugins_service.test.ts index 6f8d15838641f9..c277dc85e5e048 100644 --- a/src/core/server/plugins/plugins_service.test.ts +++ b/src/core/server/plugins/plugins_service.test.ts @@ -28,7 +28,7 @@ import { ConfigPath, ConfigService, Env } from '../config'; import { rawConfigServiceMock } from '../config/raw_config_service.mock'; import { getEnvOptions } from '../config/__mocks__/env'; import { coreMock } from '../mocks'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; import { PluginDiscoveryError } from './discovery'; import { PluginWrapper } from './plugin'; import { PluginsService } from './plugins_service'; @@ -47,7 +47,7 @@ let env: Env; let mockPluginSystem: jest.Mocked; const setupDeps = coreMock.createInternalSetup(); -const logger = loggingServiceMock.create(); +const logger = loggingSystemMock.create(); expect.addSnapshotSerializer(createAbsolutePathSerializer()); @@ -138,7 +138,7 @@ describe('PluginsService', () => { [Error: Failed to initialize plugins: Invalid JSON (invalid-manifest, path-1)] `); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: Invalid JSON (invalid-manifest, path-1)], @@ -159,7 +159,7 @@ describe('PluginsService', () => { [Error: Failed to initialize plugins: Incompatible version (incompatible-version, path-3)] `); - expect(loggingServiceMock.collect(logger).error).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(` Array [ Array [ [Error: Incompatible version (incompatible-version, path-3)], @@ -238,7 +238,7 @@ describe('PluginsService', () => { expect(mockPluginSystem.setupPlugins).toHaveBeenCalledTimes(1); expect(mockPluginSystem.setupPlugins).toHaveBeenCalledWith(setupDeps); - expect(loggingServiceMock.collect(logger).info).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).info).toMatchInlineSnapshot(` Array [ Array [ "Plugin \\"explicitly-disabled-plugin\\" is disabled.", @@ -360,7 +360,7 @@ describe('PluginsService', () => { { coreId, env, logger, configService } ); - const logs = loggingServiceMock.collect(logger); + const logs = loggingSystemMock.collect(logger); expect(logs.info).toHaveLength(0); expect(logs.error).toHaveLength(0); }); diff --git a/src/core/server/plugins/plugins_system.test.ts b/src/core/server/plugins/plugins_system.test.ts index 70983e4fd087b5..a40df70228ff3c 100644 --- a/src/core/server/plugins/plugins_system.test.ts +++ b/src/core/server/plugins/plugins_system.test.ts @@ -28,7 +28,7 @@ import { Env } from '../config'; import { getEnvOptions } from '../config/__mocks__/env'; import { CoreContext } from '../core_context'; import { configServiceMock } from '../config/config_service.mock'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; import { PluginWrapper } from './plugin'; import { PluginName } from './types'; @@ -36,7 +36,7 @@ import { PluginsSystem } from './plugins_system'; import { coreMock } from '../mocks'; import { Logger } from '../logging'; -const logger = loggingServiceMock.create(); +const logger = loggingSystemMock.create(); function createPlugin( id: string, { diff --git a/src/core/server/root/index.test.mocks.ts b/src/core/server/root/index.test.mocks.ts index 1d3add66d7c22c..ef4a40fa3db2d0 100644 --- a/src/core/server/root/index.test.mocks.ts +++ b/src/core/server/root/index.test.mocks.ts @@ -17,10 +17,10 @@ * under the License. */ -import { loggingServiceMock } from '../logging/logging_service.mock'; -export const logger = loggingServiceMock.create(); -jest.doMock('../logging/logging_service', () => ({ - LoggingService: jest.fn(() => logger), +import { loggingSystemMock } from '../logging/logging_system.mock'; +export const logger = loggingSystemMock.create(); +jest.doMock('../logging/logging_system', () => ({ + LoggingSystem: jest.fn(() => logger), })); import { configServiceMock } from '../config/config_service.mock'; diff --git a/src/core/server/root/index.ts b/src/core/server/root/index.ts index d6d0c641e00b09..5e9722de03dee0 100644 --- a/src/core/server/root/index.ts +++ b/src/core/server/root/index.ts @@ -21,7 +21,7 @@ import { ConnectableObservable, Subscription } from 'rxjs'; import { first, map, publishReplay, switchMap, tap } from 'rxjs/operators'; import { Env, RawConfigurationProvider } from '../config'; -import { Logger, LoggerFactory, LoggingConfigType, LoggingService } from '../logging'; +import { Logger, LoggerFactory, LoggingConfigType, LoggingSystem } from '../logging'; import { Server } from '../server'; /** @@ -30,7 +30,7 @@ import { Server } from '../server'; export class Root { public readonly logger: LoggerFactory; private readonly log: Logger; - private readonly loggingService: LoggingService; + private readonly loggingSystem: LoggingSystem; private readonly server: Server; private loggingConfigSubscription?: Subscription; @@ -39,10 +39,10 @@ export class Root { env: Env, private readonly onShutdown?: (reason?: Error | string) => void ) { - this.loggingService = new LoggingService(); - this.logger = this.loggingService.asLoggerFactory(); + this.loggingSystem = new LoggingSystem(); + this.logger = this.loggingSystem.asLoggerFactory(); this.log = this.logger.get('root'); - this.server = new Server(rawConfigProvider, env, this.logger); + this.server = new Server(rawConfigProvider, env, this.loggingSystem); } public async setup() { @@ -86,7 +86,7 @@ export class Root { this.loggingConfigSubscription.unsubscribe(); this.loggingConfigSubscription = undefined; } - await this.loggingService.stop(); + await this.loggingSystem.stop(); if (this.onShutdown !== undefined) { this.onShutdown(reason); @@ -99,7 +99,7 @@ export class Root { const update$ = configService.getConfig$().pipe( // always read the logging config when the underlying config object is re-read switchMap(() => configService.atPath('logging')), - map((config) => this.loggingService.upgrade(config)), + map((config) => this.loggingSystem.upgrade(config)), // This specifically console.logs because we were not able to configure the logger. // eslint-disable-next-line no-console tap({ error: (err) => console.error('Configuring logger failed:', err) }), diff --git a/src/core/server/saved_objects/migrations/core/document_migrator.test.ts b/src/core/server/saved_objects/migrations/core/document_migrator.test.ts index a3647103225240..6287d47f99f623 100644 --- a/src/core/server/saved_objects/migrations/core/document_migrator.test.ts +++ b/src/core/server/saved_objects/migrations/core/document_migrator.test.ts @@ -20,11 +20,11 @@ import _ from 'lodash'; import { SavedObjectUnsanitizedDoc } from '../../serialization'; import { DocumentMigrator } from './document_migrator'; -import { loggingServiceMock } from '../../../logging/logging_service.mock'; +import { loggingSystemMock } from '../../../logging/logging_system.mock'; import { SavedObjectsType } from '../../types'; import { SavedObjectTypeRegistry } from '../../saved_objects_type_registry'; -const mockLoggerFactory = loggingServiceMock.create(); +const mockLoggerFactory = loggingSystemMock.create(); const mockLogger = mockLoggerFactory.get('mock logger'); const createRegistry = (...types: Array>) => { @@ -572,7 +572,7 @@ describe('DocumentMigrator', () => { expect('Did not throw').toEqual('But it should have!'); } catch (error) { expect(error.message).toMatch(/Dang diggity!/); - const warning = loggingServiceMock.collect(mockLoggerFactory).warn[0][0]; + const warning = loggingSystemMock.collect(mockLoggerFactory).warn[0][0]; expect(warning).toContain(JSON.stringify(failedDoc)); expect(warning).toContain('dog:1.2.3'); } @@ -601,8 +601,8 @@ describe('DocumentMigrator', () => { migrationVersion: {}, }; migrator.migrate(doc); - expect(loggingServiceMock.collect(mockLoggerFactory).info[0][0]).toEqual(logTestMsg); - expect(loggingServiceMock.collect(mockLoggerFactory).warn[1][0]).toEqual(logTestMsg); + expect(loggingSystemMock.collect(mockLoggerFactory).info[0][0]).toEqual(logTestMsg); + expect(loggingSystemMock.collect(mockLoggerFactory).warn[1][0]).toEqual(logTestMsg); }); test('extracts the latest migration version info', () => { diff --git a/src/core/server/saved_objects/migrations/core/index_migrator.test.ts b/src/core/server/saved_objects/migrations/core/index_migrator.test.ts index 392089c69f5a08..86c79cbfb58249 100644 --- a/src/core/server/saved_objects/migrations/core/index_migrator.test.ts +++ b/src/core/server/saved_objects/migrations/core/index_migrator.test.ts @@ -21,7 +21,7 @@ import _ from 'lodash'; import { SavedObjectUnsanitizedDoc, SavedObjectsSerializer } from '../../serialization'; import { SavedObjectTypeRegistry } from '../../saved_objects_type_registry'; import { IndexMigrator } from './index_migrator'; -import { loggingServiceMock } from '../../../logging/logging_service.mock'; +import { loggingSystemMock } from '../../../logging/logging_system.mock'; describe('IndexMigrator', () => { let testOpts: any; @@ -31,7 +31,7 @@ describe('IndexMigrator', () => { batchSize: 10, callCluster: jest.fn(), index: '.kibana', - log: loggingServiceMock.create().get(), + log: loggingSystemMock.create().get(), mappingProperties: {}, pollInterval: 1, scrollDuration: '1m', diff --git a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts index 7a5c044924d0ee..01b0d1cd0ba3af 100644 --- a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts +++ b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts @@ -19,7 +19,7 @@ import { take } from 'rxjs/operators'; import { KibanaMigratorOptions, KibanaMigrator } from './kibana_migrator'; -import { loggingServiceMock } from '../../../logging/logging_service.mock'; +import { loggingSystemMock } from '../../../logging/logging_system.mock'; import { SavedObjectTypeRegistry } from '../../saved_objects_type_registry'; import { SavedObjectsType } from '../../types'; @@ -110,7 +110,7 @@ describe('KibanaMigrator', () => { function mockOptions(): KibanaMigratorOptions { const callCluster = jest.fn(); return { - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), kibanaVersion: '8.2.3', savedObjectValidations: {}, typeRegistry: createRegistry([ diff --git a/src/core/server/saved_objects/routes/integration_tests/log_legacy_import.test.ts b/src/core/server/saved_objects/routes/integration_tests/log_legacy_import.test.ts index 0fe07245dda202..8d021580da36c7 100644 --- a/src/core/server/saved_objects/routes/integration_tests/log_legacy_import.test.ts +++ b/src/core/server/saved_objects/routes/integration_tests/log_legacy_import.test.ts @@ -20,7 +20,7 @@ import supertest from 'supertest'; import { UnwrapPromise } from '@kbn/utility-types'; import { registerLogLegacyImportRoute } from '../log_legacy_import'; -import { loggingServiceMock } from '../../../logging/logging_service.mock'; +import { loggingSystemMock } from '../../../logging/logging_system.mock'; import { setupServer } from '../test_utils'; type setupServerReturn = UnwrapPromise>; @@ -28,11 +28,11 @@ type setupServerReturn = UnwrapPromise>; describe('POST /api/saved_objects/_log_legacy_import', () => { let server: setupServerReturn['server']; let httpSetup: setupServerReturn['httpSetup']; - let logger: ReturnType; + let logger: ReturnType; beforeEach(async () => { ({ server, httpSetup } = await setupServer()); - logger = loggingServiceMock.createLogger(); + logger = loggingSystemMock.createLogger(); const router = httpSetup.createRouter('/api/saved_objects/'); registerLogLegacyImportRoute(router, logger); @@ -50,7 +50,7 @@ describe('POST /api/saved_objects/_log_legacy_import', () => { .expect(200); expect(result.body).toEqual({ success: true }); - expect(loggingServiceMock.collect(logger).warn).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).warn).toMatchInlineSnapshot(` Array [ Array [ "Importing saved objects from a .json file has been deprecated", diff --git a/src/core/server/server.api.md b/src/core/server/server.api.md index 9dc3ac9b94d96d..4d6316fceb5682 100644 --- a/src/core/server/server.api.md +++ b/src/core/server/server.api.md @@ -388,6 +388,11 @@ export interface APICaller { (endpoint: string, clientParams?: Record, options?: CallAPIOptions): Promise; } +// Warning: (ae-forgotten-export) The symbol "appendersSchema" needs to be exported by the entry point index.d.ts +// +// @public (undocumented) +export type AppenderConfigType = TypeOf; + // @public export function assertNever(x: never): never; @@ -574,6 +579,72 @@ export const config: { ignoreVersionMismatch: import("@kbn/config-schema/target/types/types").ConditionalType; }>; }; + logging: { + appenders: import("@kbn/config-schema").Type | Readonly<{ + pattern?: string | undefined; + highlight?: boolean | undefined; + } & { + kind: "pattern"; + }>; + kind: "console"; + }> | Readonly<{} & { + path: string; + layout: Readonly<{} & { + kind: "json"; + }> | Readonly<{ + pattern?: string | undefined; + highlight?: boolean | undefined; + } & { + kind: "pattern"; + }>; + kind: "file"; + }> | Readonly<{ + legacyLoggingConfig?: any; + } & { + kind: "legacy-appender"; + }>>; + loggers: import("@kbn/config-schema").ObjectType<{ + appenders: import("@kbn/config-schema").Type; + context: import("@kbn/config-schema").Type; + level: import("@kbn/config-schema").Type; + }>; + loggerContext: import("@kbn/config-schema").ObjectType<{ + appenders: import("@kbn/config-schema").Type | Readonly<{ + pattern?: string | undefined; + highlight?: boolean | undefined; + } & { + kind: "pattern"; + }>; + kind: "console"; + }> | Readonly<{} & { + path: string; + layout: Readonly<{} & { + kind: "json"; + }> | Readonly<{ + pattern?: string | undefined; + highlight?: boolean | undefined; + } & { + kind: "pattern"; + }>; + kind: "file"; + }> | Readonly<{ + legacyLoggingConfig?: any; + } & { + kind: "legacy-appender"; + }>>>; + loggers: import("@kbn/config-schema").Type[]>; + }>; + }; }; // @public @@ -639,6 +710,8 @@ export interface CoreSetup; + +// @public (undocumented) +export interface LoggerContextConfigInput { + // (undocumented) + appenders?: Record | Map; + // (undocumented) + loggers?: LoggerConfigType[]; +} + // @public export interface LoggerFactory { get(...contextParts: string[]): Logger; } +// @public +export interface LoggingServiceSetup { + configure(config$: Observable): void; +} + // @internal export class LogLevel { // (undocumented) diff --git a/src/core/server/server.test.mocks.ts b/src/core/server/server.test.mocks.ts index 5d535c98457249..e5e710d54e04b7 100644 --- a/src/core/server/server.test.mocks.ts +++ b/src/core/server/server.test.mocks.ts @@ -91,3 +91,9 @@ export const mockStatusService = statusServiceMock.create(); jest.doMock('./status/status_service', () => ({ StatusService: jest.fn(() => mockStatusService), })); + +import { loggingServiceMock } from './logging/logging_service.mock'; +export const mockLoggingService = loggingServiceMock.create(); +jest.doMock('./logging/logging_service', () => ({ + LoggingService: jest.fn(() => mockLoggingService), +})); diff --git a/src/core/server/server.test.ts b/src/core/server/server.test.ts index 1e3e1638cf2a04..1f507a85d3ddf0 100644 --- a/src/core/server/server.test.ts +++ b/src/core/server/server.test.ts @@ -30,6 +30,7 @@ import { mockRenderingService, mockMetricsService, mockStatusService, + mockLoggingService, } from './server.test.mocks'; import { BehaviorSubject } from 'rxjs'; @@ -37,11 +38,11 @@ import { Env } from './config'; import { Server } from './server'; import { getEnvOptions } from './config/__mocks__/env'; -import { loggingServiceMock } from './logging/logging_service.mock'; +import { loggingSystemMock } from './logging/logging_system.mock'; import { rawConfigServiceMock } from './config/raw_config_service.mock'; const env = new Env('.', getEnvOptions()); -const logger = loggingServiceMock.create(); +const logger = loggingSystemMock.create(); const rawConfigService = rawConfigServiceMock.create({}); beforeEach(() => { @@ -68,6 +69,7 @@ test('sets up services on "setup"', async () => { expect(mockRenderingService.setup).not.toHaveBeenCalled(); expect(mockMetricsService.setup).not.toHaveBeenCalled(); expect(mockStatusService.setup).not.toHaveBeenCalled(); + expect(mockLoggingService.setup).not.toHaveBeenCalled(); await server.setup(); @@ -80,6 +82,7 @@ test('sets up services on "setup"', async () => { expect(mockRenderingService.setup).toHaveBeenCalledTimes(1); expect(mockMetricsService.setup).toHaveBeenCalledTimes(1); expect(mockStatusService.setup).toHaveBeenCalledTimes(1); + expect(mockLoggingService.setup).toHaveBeenCalledTimes(1); }); test('injects legacy dependency to context#setup()', async () => { @@ -151,6 +154,7 @@ test('stops services on "stop"', async () => { expect(mockUiSettingsService.stop).not.toHaveBeenCalled(); expect(mockMetricsService.stop).not.toHaveBeenCalled(); expect(mockStatusService.stop).not.toHaveBeenCalled(); + expect(mockLoggingService.stop).not.toHaveBeenCalled(); await server.stop(); @@ -162,6 +166,7 @@ test('stops services on "stop"', async () => { expect(mockUiSettingsService.stop).toHaveBeenCalledTimes(1); expect(mockMetricsService.stop).toHaveBeenCalledTimes(1); expect(mockStatusService.stop).toHaveBeenCalledTimes(1); + expect(mockLoggingService.stop).toHaveBeenCalledTimes(1); }); test(`doesn't setup core services if config validation fails`, async () => { @@ -179,6 +184,7 @@ test(`doesn't setup core services if config validation fails`, async () => { expect(mockRenderingService.setup).not.toHaveBeenCalled(); expect(mockMetricsService.setup).not.toHaveBeenCalled(); expect(mockStatusService.setup).not.toHaveBeenCalled(); + expect(mockLoggingService.setup).not.toHaveBeenCalled(); }); test(`doesn't setup core services if legacy config validation fails`, async () => { @@ -200,4 +206,5 @@ test(`doesn't setup core services if legacy config validation fails`, async () = expect(mockUiSettingsService.setup).not.toHaveBeenCalled(); expect(mockMetricsService.setup).not.toHaveBeenCalled(); expect(mockStatusService.setup).not.toHaveBeenCalled(); + expect(mockLoggingService.setup).not.toHaveBeenCalled(); }); diff --git a/src/core/server/server.ts b/src/core/server/server.ts index ae1a02cf71b886..3bbcd0e37e142a 100644 --- a/src/core/server/server.ts +++ b/src/core/server/server.ts @@ -32,7 +32,7 @@ import { HttpService } from './http'; import { HttpResourcesService } from './http_resources'; import { RenderingService } from './rendering'; import { LegacyService, ensureValidConfiguration } from './legacy'; -import { Logger, LoggerFactory } from './logging'; +import { Logger, LoggerFactory, LoggingService, ILoggingSystem } from './logging'; import { UiSettingsService } from './ui_settings'; import { PluginsService, config as pluginsConfig } from './plugins'; import { SavedObjectsService } from '../server/saved_objects'; @@ -74,20 +74,23 @@ export class Server { private readonly metrics: MetricsService; private readonly httpResources: HttpResourcesService; private readonly status: StatusService; + private readonly logging: LoggingService; private readonly coreApp: CoreApp; #pluginsInitialized?: boolean; private coreStart?: InternalCoreStart; + private readonly logger: LoggerFactory; constructor( rawConfigProvider: RawConfigurationProvider, public readonly env: Env, - private readonly logger: LoggerFactory + private readonly loggingSystem: ILoggingSystem ) { + this.logger = this.loggingSystem.asLoggerFactory(); this.log = this.logger.get('server'); - this.configService = new ConfigService(rawConfigProvider, env, logger); + this.configService = new ConfigService(rawConfigProvider, env, this.logger); - const core = { coreId, configService: this.configService, env, logger }; + const core = { coreId, configService: this.configService, env, logger: this.logger }; this.context = new ContextService(core); this.http = new HttpService(core); this.rendering = new RenderingService(core); @@ -102,6 +105,7 @@ export class Server { this.status = new StatusService(core); this.coreApp = new CoreApp(core); this.httpResources = new HttpResourcesService(core); + this.logging = new LoggingService(core); } public async setup() { @@ -164,6 +168,10 @@ export class Server { savedObjects: savedObjectsSetup, }); + const loggingSetup = this.logging.setup({ + loggingSystem: this.loggingSystem, + }); + const coreSetup: InternalCoreSetup = { capabilities: capabilitiesSetup, context: contextServiceSetup, @@ -176,6 +184,7 @@ export class Server { metrics: metricsSetup, rendering: renderingSetup, httpResources: httpResourcesSetup, + logging: loggingSetup, }; const pluginsSetup = await this.plugins.setup(coreSetup); @@ -244,6 +253,7 @@ export class Server { await this.rendering.stop(); await this.metrics.stop(); await this.status.stop(); + await this.logging.stop(); } private registerCoreContext(coreSetup: InternalCoreSetup) { diff --git a/src/core/server/ui_settings/create_or_upgrade_saved_config/create_or_upgrade_saved_config.test.ts b/src/core/server/ui_settings/create_or_upgrade_saved_config/create_or_upgrade_saved_config.test.ts index 9c5a0625e8fd0c..10a30db038174d 100644 --- a/src/core/server/ui_settings/create_or_upgrade_saved_config/create_or_upgrade_saved_config.test.ts +++ b/src/core/server/ui_settings/create_or_upgrade_saved_config/create_or_upgrade_saved_config.test.ts @@ -21,7 +21,7 @@ import Chance from 'chance'; import { SavedObjectsErrorHelpers } from '../../saved_objects'; import { savedObjectsClientMock } from '../../saved_objects/service/saved_objects_client.mock'; -import { loggingServiceMock } from '../../logging/logging_service.mock'; +import { loggingSystemMock } from '../../logging/logging_system.mock'; import { getUpgradeableConfigMock } from './get_upgradeable_config.test.mock'; import { createOrUpgradeSavedConfig } from './create_or_upgrade_saved_config'; @@ -35,7 +35,7 @@ describe('uiSettings/createOrUpgradeSavedConfig', function () { const buildNum = chance.integer({ min: 1000, max: 5000 }); function setup() { - const logger = loggingServiceMock.create(); + const logger = loggingSystemMock.create(); const getUpgradeableConfig = getUpgradeableConfigMock; const savedObjectsClient = savedObjectsClientMock.create(); savedObjectsClient.create.mockImplementation( @@ -137,7 +137,7 @@ describe('uiSettings/createOrUpgradeSavedConfig', function () { }); await run(); - expect(loggingServiceMock.collect(logger).debug).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).debug).toMatchInlineSnapshot(` Array [ Array [ "Upgrade config from 4.0.0 to 4.0.1", @@ -169,7 +169,7 @@ describe('uiSettings/createOrUpgradeSavedConfig', function () { expect(error.message).toBe('foo'); } - expect(loggingServiceMock.collect(logger).debug).toHaveLength(0); + expect(loggingSystemMock.collect(logger).debug).toHaveLength(0); }); }); diff --git a/src/core/server/ui_settings/create_or_upgrade_saved_config/integration_tests/create_or_upgrade.test.ts b/src/core/server/ui_settings/create_or_upgrade_saved_config/integration_tests/create_or_upgrade.test.ts index adf36e4491b795..d2e31dad58e55e 100644 --- a/src/core/server/ui_settings/create_or_upgrade_saved_config/integration_tests/create_or_upgrade.test.ts +++ b/src/core/server/ui_settings/create_or_upgrade_saved_config/integration_tests/create_or_upgrade.test.ts @@ -26,10 +26,10 @@ import { TestUtils, } from '../../../../../test_utils/kbn_server'; import { createOrUpgradeSavedConfig } from '../create_or_upgrade_saved_config'; -import { loggingServiceMock } from '../../../logging/logging_service.mock'; +import { loggingSystemMock } from '../../../logging/logging_system.mock'; import { httpServerMock } from '../../../http/http_server.mocks'; -const logger = loggingServiceMock.create().get(); +const logger = loggingSystemMock.create().get(); describe('createOrUpgradeSavedConfig()', () => { let savedObjectsClient: SavedObjectsClientContract; let servers: TestUtils; diff --git a/src/core/server/ui_settings/ui_settings_client.test.ts b/src/core/server/ui_settings/ui_settings_client.test.ts index 4ce33eed267a34..a38fb2ab7e06c6 100644 --- a/src/core/server/ui_settings/ui_settings_client.test.ts +++ b/src/core/server/ui_settings/ui_settings_client.test.ts @@ -20,7 +20,7 @@ import Chance from 'chance'; import { schema } from '@kbn/config-schema'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; import { createOrUpgradeSavedConfigMock } from './create_or_upgrade_saved_config/create_or_upgrade_saved_config.test.mock'; import { SavedObjectsClient } from '../saved_objects'; @@ -28,7 +28,7 @@ import { savedObjectsClientMock } from '../saved_objects/service/saved_objects_c import { UiSettingsClient } from './ui_settings_client'; import { CannotOverrideError } from './ui_settings_errors'; -const logger = loggingServiceMock.create().get(); +const logger = loggingSystemMock.create().get(); const TYPE = 'config'; const ID = 'kibana-version'; @@ -375,7 +375,7 @@ describe('ui settings', () => { }, }); - expect(loggingServiceMock.collect(logger).warn).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).warn).toMatchInlineSnapshot(` Array [ Array [ "Ignore invalid UiSettings value. Error: [validation [id]]: expected value of type [number] but got [string].", @@ -517,7 +517,7 @@ describe('ui settings', () => { user: 'foo', }); - expect(loggingServiceMock.collect(logger).warn).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).warn).toMatchInlineSnapshot(` Array [ Array [ "Ignore invalid UiSettings value. Error: [validation [id]]: expected value of type [number] but got [string].", @@ -645,7 +645,7 @@ describe('ui settings', () => { expect(await uiSettings.get('id')).toBe(42); - expect(loggingServiceMock.collect(logger).warn).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).warn).toMatchInlineSnapshot(` Array [ Array [ "Ignore invalid UiSettings value. Error: [validation [id]]: expected value of type [number] but got [string].", diff --git a/src/core/server/uuid/resolve_uuid.test.ts b/src/core/server/uuid/resolve_uuid.test.ts index eab027b532ddbc..1a873a1cea0cfe 100644 --- a/src/core/server/uuid/resolve_uuid.test.ts +++ b/src/core/server/uuid/resolve_uuid.test.ts @@ -21,7 +21,7 @@ import { join } from 'path'; import { readFile, writeFile } from './fs'; import { resolveInstanceUuid, UUID_7_6_0_BUG } from './resolve_uuid'; import { configServiceMock } from '../config/config_service.mock'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; import { BehaviorSubject } from 'rxjs'; import { Logger } from '../logging'; @@ -93,7 +93,7 @@ describe('resolveInstanceUuid', () => { mockReadFile({ uuid: DEFAULT_FILE_UUID }); mockWriteFile(); configService = getConfigService(DEFAULT_CONFIG_UUID); - logger = loggingServiceMock.create().get() as any; + logger = loggingSystemMock.create().get() as any; }); describe('when file is present and config property is set', () => { diff --git a/src/core/server/uuid/uuid_service.test.ts b/src/core/server/uuid/uuid_service.test.ts index a61061ff842630..092216303080e3 100644 --- a/src/core/server/uuid/uuid_service.test.ts +++ b/src/core/server/uuid/uuid_service.test.ts @@ -21,7 +21,7 @@ import { UuidService } from './uuid_service'; import { resolveInstanceUuid } from './resolve_uuid'; import { CoreContext } from '../core_context'; -import { loggingServiceMock } from '../logging/logging_service.mock'; +import { loggingSystemMock } from '../logging/logging_system.mock'; import { mockCoreContext } from '../core_context.mock'; import { Env } from '../config'; import { getEnvOptions } from '../config/__mocks__/env'; @@ -31,12 +31,12 @@ jest.mock('./resolve_uuid', () => ({ })); describe('UuidService', () => { - let logger: ReturnType; + let logger: ReturnType; let coreContext: CoreContext; beforeEach(() => { jest.clearAllMocks(); - logger = loggingServiceMock.create(); + logger = loggingSystemMock.create(); coreContext = mockCoreContext.create({ logger }); }); diff --git a/src/legacy/core_plugins/kibana/index.js b/src/legacy/core_plugins/kibana/index.js index ae613e0e809048..9648ff29a95e72 100644 --- a/src/legacy/core_plugins/kibana/index.js +++ b/src/legacy/core_plugins/kibana/index.js @@ -21,13 +21,8 @@ import Fs from 'fs'; import { resolve } from 'path'; import { promisify } from 'util'; -import { importApi } from './server/routes/api/import'; -import { exportApi } from './server/routes/api/export'; import { getUiSettingDefaults } from './server/ui_setting_defaults'; import { registerCspCollector } from './server/lib/csp_usage_collector'; -import { injectVars } from './inject_vars'; - -import { kbnBaseUrl } from '../../../plugins/kibana_legacy/server'; const mkdirAsync = promisify(Fs.mkdir); @@ -45,35 +40,7 @@ export default function (kibana) { }, uiExports: { - app: { - id: 'kibana', - title: 'Kibana', - listed: false, - main: 'plugins/kibana/kibana', - }, styleSheetPaths: resolve(__dirname, 'public/index.scss'), - links: [], - - injectDefaultVars(server, options) { - const mapConfig = server.config().get('map'); - const tilemap = mapConfig.tilemap; - - return { - kbnIndex: options.index, - kbnBaseUrl, - - // required on all pages due to hacks that use these values - mapConfig, - tilemapsConfig: { - deprecated: { - // If url is set, old settings must be used for backward compatibility - isOverridden: typeof tilemap.url === 'string' && tilemap.url !== '', - config: tilemap, - }, - }, - }; - }, - uiSettingDefaults: getUiSettingDefaults(), }, @@ -91,11 +58,7 @@ export default function (kibana) { init: async function (server) { const { usageCollection } = server.newPlatform.setup.plugins; - // routes - importApi(server); - exportApi(server); registerCspCollector(usageCollection, server); - server.injectUiAppVars('kibana', () => injectVars(server)); }, }); } diff --git a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_table/legacy.ts b/src/legacy/core_plugins/kibana/public/__tests__/vis_type_table/legacy.ts index c6467a5beae686..216afe5920408a 100644 --- a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_table/legacy.ts +++ b/src/legacy/core_plugins/kibana/public/__tests__/vis_type_table/legacy.ts @@ -35,4 +35,5 @@ const pluginInstance = new TableVisPlugin({} as PluginInitializerContext); export const setup = pluginInstance.setup(npSetup.core, plugins); export const start = pluginInstance.start(npStart.core, { data: npStart.plugins.data, + kibanaLegacy: npStart.plugins.kibanaLegacy, }); diff --git a/src/legacy/core_plugins/kibana/public/index.scss b/src/legacy/core_plugins/kibana/public/index.scss index 56a2543dbca788..e9810a747c8c78 100644 --- a/src/legacy/core_plugins/kibana/public/index.scss +++ b/src/legacy/core_plugins/kibana/public/index.scss @@ -7,12 +7,3 @@ // Public UI styles @import 'src/legacy/ui/public/index'; -// Has to come after visualize because of some -// bad cascading in the Editor layout -@import '../../../../plugins/maps_legacy/public/index'; - -// Management styles -@import './management/index'; - -// Local application mount wrapper styles -@import 'local_application_service/index'; diff --git a/src/legacy/core_plugins/kibana/public/kibana.js b/src/legacy/core_plugins/kibana/public/kibana.js deleted file mode 100644 index 51dedcc629c768..00000000000000 --- a/src/legacy/core_plugins/kibana/public/kibana.js +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -// autoloading - -// preloading (for faster webpack builds) -import routes from 'ui/routes'; -import { npSetup } from 'ui/new_platform'; - -// import the uiExports that we want to "use" -import 'uiExports/savedObjectTypes'; -import 'uiExports/fieldFormatEditors'; -import 'uiExports/navbarExtensions'; -import 'uiExports/contextMenuActions'; -import 'uiExports/managementSections'; -import 'uiExports/indexManagement'; -import 'uiExports/embeddableFactories'; -import 'uiExports/embeddableActions'; -import 'uiExports/inspectorViews'; -import 'uiExports/search'; -import 'uiExports/shareContextMenuExtensions'; -import 'uiExports/interpreter'; - -import 'ui/autoload/all'; - -import { localApplicationService } from './local_application_service'; - -npSetup.plugins.kibanaLegacy.registerLegacyAppAlias('doc', 'discover', { keepPrefix: true }); -npSetup.plugins.kibanaLegacy.registerLegacyAppAlias('context', 'discover', { keepPrefix: true }); - -npSetup.plugins.kibanaLegacy.forwardApp('management', 'management', (path) => { - return path.replace('/management', ''); -}); - -localApplicationService.attachToAngular(routes); - -routes.enable(); - -const { config } = npSetup.plugins.kibanaLegacy; - -routes.otherwise({ - redirectTo: `/${config.defaultAppId || 'discover'}`, -}); diff --git a/src/legacy/core_plugins/kibana/public/local_application_service/_index.scss b/src/legacy/core_plugins/kibana/public/local_application_service/_index.scss deleted file mode 100644 index 12cc1444101e71..00000000000000 --- a/src/legacy/core_plugins/kibana/public/local_application_service/_index.scss +++ /dev/null @@ -1 +0,0 @@ -@import 'local_application_service'; diff --git a/src/legacy/core_plugins/kibana/public/local_application_service/_local_application_service.scss b/src/legacy/core_plugins/kibana/public/local_application_service/_local_application_service.scss deleted file mode 100644 index 33a6100c439759..00000000000000 --- a/src/legacy/core_plugins/kibana/public/local_application_service/_local_application_service.scss +++ /dev/null @@ -1,5 +0,0 @@ -.kbnLocalApplicationWrapper { - display: flex; - flex-direction: column; - flex-grow: 1; -} diff --git a/src/legacy/core_plugins/kibana/public/local_application_service/local_application_service.ts b/src/legacy/core_plugins/kibana/public/local_application_service/local_application_service.ts deleted file mode 100644 index 59e5238578d25d..00000000000000 --- a/src/legacy/core_plugins/kibana/public/local_application_service/local_application_service.ts +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { App, AppUnmount, AppMountDeprecated } from 'kibana/public'; -import { UIRoutes } from 'ui/routes'; -import { ILocationService, IScope } from 'angular'; -import { npStart } from 'ui/new_platform'; -import { htmlIdGenerator } from '@elastic/eui'; - -const matchAllWithPrefix = (prefixOrApp: string | App) => - `/${typeof prefixOrApp === 'string' ? prefixOrApp : prefixOrApp.id}/:tail*?`; - -/** - * To be able to migrate and shim parts of the Kibana app plugin - * while still running some parts of it in the legacy world, this - * service emulates the core application service while using the global - * angular router to switch between apps without page reload. - * - * The id of the apps is used as prefix of the route - when switching between - * to apps, the current application is unmounted. - * - * This service becomes unnecessary once the platform provides a central - * router that handles switching between applications without page reload. - */ -export class LocalApplicationService { - private idGenerator = htmlIdGenerator('kibanaAppLocalApp'); - - /** - * Wires up listeners to handle mounting and unmounting of apps to - * the legacy angular route manager. Once all apps within the Kibana - * plugin are using the local route manager, this implementation can - * be switched to a more lightweight implementation. - * - * @param angularRouteManager The current `ui/routes` instance - */ - attachToAngular(angularRouteManager: UIRoutes) { - npStart.plugins.kibanaLegacy.getApps().forEach((app) => { - const wrapperElementId = this.idGenerator(); - angularRouteManager.when(matchAllWithPrefix(app), { - outerAngularWrapperRoute: true, - reloadOnSearch: false, - reloadOnUrl: false, - template: `
`, - controller($scope: IScope) { - const element = document.getElementById(wrapperElementId)!; - let unmountHandler: AppUnmount | null = null; - let isUnmounted = false; - $scope.$on('$destroy', () => { - if (unmountHandler) { - unmountHandler(); - } - isUnmounted = true; - }); - (async () => { - const params = { - element, - appBasePath: '', - onAppLeave: () => undefined, - // TODO: adapt to use Core's ScopedHistory - history: {} as any, - }; - unmountHandler = isAppMountDeprecated(app.mount) - ? await app.mount({ core: npStart.core }, params) - : await app.mount(params); - // immediately unmount app if scope got destroyed in the meantime - if (isUnmounted) { - unmountHandler(); - } - })(); - }, - }); - - if (app.updater$) { - app.updater$.subscribe((updater) => { - const updatedFields = updater(app); - if (updatedFields && updatedFields.activeUrl) { - npStart.core.chrome.navLinks.update(app.navLinkId || app.id, { - url: updatedFields.activeUrl, - }); - } - }); - } - }); - - npStart.plugins.kibanaLegacy.getForwards().forEach((forwardDefinition) => { - angularRouteManager.when(matchAllWithPrefix(forwardDefinition.legacyAppId), { - outerAngularWrapperRoute: true, - reloadOnSearch: false, - reloadOnUrl: false, - template: '', - controller($location: ILocationService) { - const newPath = forwardDefinition.rewritePath($location.url()); - window.location.replace( - npStart.core.http.basePath.prepend(`/app/${forwardDefinition.newAppId}${newPath}`) - ); - }, - }); - }); - - npStart.plugins.kibanaLegacy - .getLegacyAppAliases() - .forEach(({ legacyAppId, newAppId, keepPrefix }) => { - angularRouteManager.when(matchAllWithPrefix(legacyAppId), { - resolveRedirectTo: ($location: ILocationService) => { - const url = $location.url(); - return `/${newAppId}${keepPrefix ? url : url.replace(legacyAppId, '')}`; - }, - }); - }); - } -} - -export const localApplicationService = new LocalApplicationService(); - -function isAppMountDeprecated(mount: (...args: any[]) => any): mount is AppMountDeprecated { - // Mount functions with two arguments are assumed to expect deprecated `context` object. - return mount.length === 2; -} diff --git a/src/legacy/core_plugins/kibana/public/management/index.scss b/src/legacy/core_plugins/kibana/public/management/index.scss deleted file mode 100644 index fb267b714f1c96..00000000000000 --- a/src/legacy/core_plugins/kibana/public/management/index.scss +++ /dev/null @@ -1,13 +0,0 @@ -// This file is imported into src/core_plugings/kibana/publix/index.scss - -// Prefix all styles with "dsh" to avoid conflicts. -// Examples -// mgtChart -// mgtChart__legend -// mgtChart__legend--small -// mgtChart__legend-isLoading - -// Core -@import '../../../../../plugins/advanced_settings/public/index'; - -@import 'sections/index_patterns/index'; diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/index.scss b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/index.scss deleted file mode 100644 index c5cf844ebdc342..00000000000000 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/index.scss +++ /dev/null @@ -1,25 +0,0 @@ -#indexPatternListReact { - display: flex; - - .indexPatternList__headerWrapper { - padding-bottom: $euiSizeS; - } - - .euiButtonEmpty__content { - justify-content: left; - padding: 0; - - span { - text-overflow: ellipsis; - overflow: hidden; - } - } - - .indexPatternListPrompt__descList { - text-align: left; - } -} - -.indexPatternList__badge { - margin-left: $euiSizeS; -} diff --git a/src/legacy/core_plugins/kibana/server/lib/__tests__/relationships.js b/src/legacy/core_plugins/kibana/server/lib/__tests__/relationships.js deleted file mode 100644 index 4df0e7a140205e..00000000000000 --- a/src/legacy/core_plugins/kibana/server/lib/__tests__/relationships.js +++ /dev/null @@ -1,645 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import expect from '@kbn/expect'; -import { findRelationships } from '../management/saved_objects/relationships'; - -function getManagementaMock(savedObjectSchemas) { - return { - isImportAndExportable(type) { - return ( - !savedObjectSchemas[type] || savedObjectSchemas[type].isImportableAndExportable !== false - ); - }, - getDefaultSearchField(type) { - return savedObjectSchemas[type] && savedObjectSchemas[type].defaultSearchField; - }, - getIcon(type) { - return savedObjectSchemas[type] && savedObjectSchemas[type].icon; - }, - getTitle(savedObject) { - const { type } = savedObject; - const getTitle = savedObjectSchemas[type] && savedObjectSchemas[type].getTitle; - if (getTitle) { - return getTitle(savedObject); - } - }, - getEditUrl(savedObject) { - const { type } = savedObject; - const getEditUrl = savedObjectSchemas[type] && savedObjectSchemas[type].getEditUrl; - if (getEditUrl) { - return getEditUrl(savedObject); - } - }, - getInAppUrl(savedObject) { - const { type } = savedObject; - const getInAppUrl = savedObjectSchemas[type] && savedObjectSchemas[type].getInAppUrl; - if (getInAppUrl) { - return getInAppUrl(savedObject); - } - }, - }; -} - -const savedObjectsManagement = getManagementaMock({ - 'index-pattern': { - icon: 'indexPatternApp', - defaultSearchField: 'title', - getTitle(obj) { - return obj.attributes.title; - }, - getEditUrl(obj) { - return `/management/kibana/indexPatterns/patterns/${encodeURIComponent(obj.id)}`; - }, - getInAppUrl(obj) { - return { - path: `/app/management/kibana/indexPatterns/patterns/${encodeURIComponent(obj.id)}`, - uiCapabilitiesPath: 'management.kibana.index_patterns', - }; - }, - }, - visualization: { - icon: 'visualizeApp', - defaultSearchField: 'title', - getTitle(obj) { - return obj.attributes.title; - }, - getEditUrl(obj) { - return `/management/kibana/objects/savedVisualizations/${encodeURIComponent(obj.id)}`; - }, - getInAppUrl(obj) { - return { - path: `/app/visualize#/edit/${encodeURIComponent(obj.id)}`, - uiCapabilitiesPath: 'visualize.show', - }; - }, - }, - search: { - icon: 'search', - defaultSearchField: 'title', - getTitle(obj) { - return obj.attributes.title; - }, - getEditUrl(obj) { - return `/management/kibana/objects/savedSearches/${encodeURIComponent(obj.id)}`; - }, - getInAppUrl(obj) { - return { - path: `/app/discover#//${encodeURIComponent(obj.id)}`, - uiCapabilitiesPath: 'discover.show', - }; - }, - }, - dashboard: { - icon: 'dashboardApp', - defaultSearchField: 'title', - getTitle(obj) { - return obj.attributes.title; - }, - getEditUrl(obj) { - return `/management/kibana/objects/savedDashboards/${encodeURIComponent(obj.id)}`; - }, - getInAppUrl(obj) { - return { - path: `/app/kibana#/dashboard/${encodeURIComponent(obj.id)}`, - uiCapabilitiesPath: 'dashboard.show', - }; - }, - }, -}); - -describe('findRelationships', () => { - it('should find relationships for dashboards', async () => { - const type = 'dashboard'; - const id = 'foo'; - const size = 10; - - const savedObjectsClient = { - get: () => ({ - attributes: { - panelsJSON: JSON.stringify([ - { panelRefName: 'panel_0' }, - { panelRefName: 'panel_1' }, - { panelRefName: 'panel_2' }, - ]), - }, - references: [ - { - name: 'panel_0', - type: 'visualization', - id: '1', - }, - { - name: 'panel_1', - type: 'visualization', - id: '2', - }, - { - name: 'panel_2', - type: 'visualization', - id: '3', - }, - ], - }), - bulkGet: () => ({ saved_objects: [] }), - find: () => ({ - saved_objects: [ - { - id: '1', - type: 'visualization', - attributes: { - title: 'Foo', - }, - }, - { - id: '2', - type: 'visualization', - attributes: { - title: 'Bar', - }, - }, - { - id: '3', - type: 'visualization', - attributes: { - title: 'FooBar', - }, - }, - ], - }), - }; - const result = await findRelationships(type, id, { - size, - savedObjectsClient, - savedObjectsManagement, - savedObjectTypes: ['dashboard', 'visualization', 'search', 'index-pattern'], - }); - expect(result).to.eql([ - { - id: '1', - type: 'visualization', - relationship: 'parent', - meta: { - icon: 'visualizeApp', - title: 'Foo', - editUrl: '/management/kibana/objects/savedVisualizations/1', - inAppUrl: { - path: '/app/visualize#/edit/1', - uiCapabilitiesPath: 'visualize.show', - }, - }, - }, - { - id: '2', - type: 'visualization', - relationship: 'parent', - meta: { - icon: 'visualizeApp', - title: 'Bar', - editUrl: '/management/kibana/objects/savedVisualizations/2', - inAppUrl: { - path: '/app/visualize#/edit/2', - uiCapabilitiesPath: 'visualize.show', - }, - }, - }, - { - id: '3', - type: 'visualization', - relationship: 'parent', - meta: { - icon: 'visualizeApp', - title: 'FooBar', - editUrl: '/management/kibana/objects/savedVisualizations/3', - inAppUrl: { - path: '/app/visualize#/edit/3', - uiCapabilitiesPath: 'visualize.show', - }, - }, - }, - ]); - }); - - it('should find relationships for visualizations', async () => { - const type = 'visualization'; - const id = 'foo'; - const size = 10; - - const savedObjectsClient = { - get: () => ({ - attributes: { - kibanaSavedObjectMeta: { - searchSourceJSON: JSON.stringify({ - indexRefName: 'kibanaSavedObjectMeta.searchSourceJSON.index', - }), - }, - }, - references: [ - { - name: 'kibanaSavedObjectMeta.searchSourceJSON.index', - type: 'index-pattern', - id: '1', - }, - ], - }), - bulkGet: () => ({ - saved_objects: [ - { - id: '1', - type: 'index-pattern', - attributes: { - title: 'My Index Pattern', - }, - }, - ], - }), - find: () => ({ - saved_objects: [ - { - id: '1', - type: 'dashboard', - attributes: { - title: 'My Dashboard', - panelsJSON: JSON.stringify([ - { - type: 'visualization', - id, - }, - { - type: 'visualization', - id: 'foobar', - }, - ]), - }, - }, - { - id: '2', - type: 'dashboard', - attributes: { - title: 'Your Dashboard', - panelsJSON: JSON.stringify([ - { - type: 'visualization', - id, - }, - { - type: 'visualization', - id: 'foobar', - }, - ]), - }, - }, - ], - }), - }; - - const result = await findRelationships(type, id, { - size, - savedObjectsClient, - savedObjectsManagement, - savedObjectTypes: ['dashboard', 'visualization', 'search', 'index-pattern'], - }); - expect(result).to.eql([ - { - id: '1', - type: 'index-pattern', - relationship: 'child', - meta: { - icon: 'indexPatternApp', - title: 'My Index Pattern', - editUrl: '/management/kibana/indexPatterns/patterns/1', - inAppUrl: { - path: '/app/management/kibana/indexPatterns/patterns/1', - uiCapabilitiesPath: 'management.kibana.index_patterns', - }, - }, - }, - { - id: '1', - type: 'dashboard', - relationship: 'parent', - meta: { - icon: 'dashboardApp', - title: 'My Dashboard', - editUrl: '/management/kibana/objects/savedDashboards/1', - inAppUrl: { - path: '/app/kibana#/dashboard/1', - uiCapabilitiesPath: 'dashboard.show', - }, - }, - }, - { - id: '2', - type: 'dashboard', - relationship: 'parent', - meta: { - icon: 'dashboardApp', - title: 'Your Dashboard', - editUrl: '/management/kibana/objects/savedDashboards/2', - inAppUrl: { - path: '/app/kibana#/dashboard/2', - uiCapabilitiesPath: 'dashboard.show', - }, - }, - }, - ]); - }); - - it('should find relationships for saved searches', async () => { - const type = 'search'; - const id = 'foo'; - const size = 10; - - const savedObjectsClient = { - get: () => ({ - id: '1', - type: 'search', - attributes: { - kibanaSavedObjectMeta: { - searchSourceJSON: JSON.stringify({ - indexRefName: 'kibanaSavedObjectMeta.searchSourceJSON.index', - }), - }, - }, - references: [ - { - name: 'kibanaSavedObjectMeta.searchSourceJSON.index', - type: 'index-pattern', - id: '1', - }, - ], - }), - bulkGet: () => ({ - saved_objects: [ - { - id: '1', - type: 'index-pattern', - attributes: { - title: 'My Index Pattern', - }, - }, - ], - }), - find: () => ({ - saved_objects: [ - { - id: '1', - type: 'visualization', - attributes: { - title: 'Foo', - }, - }, - { - id: '2', - type: 'visualization', - attributes: { - title: 'Bar', - }, - }, - { - id: '3', - type: 'visualization', - attributes: { - title: 'FooBar', - }, - }, - ], - }), - }; - - const result = await findRelationships(type, id, { - size, - savedObjectsClient, - savedObjectsManagement, - savedObjectTypes: ['dashboard', 'visualization', 'search', 'index-pattern'], - }); - expect(result).to.eql([ - { - id: '1', - type: 'index-pattern', - relationship: 'child', - meta: { - icon: 'indexPatternApp', - title: 'My Index Pattern', - editUrl: '/management/kibana/indexPatterns/patterns/1', - inAppUrl: { - path: '/app/management/kibana/indexPatterns/patterns/1', - uiCapabilitiesPath: 'management.kibana.index_patterns', - }, - }, - }, - { - id: '1', - type: 'visualization', - relationship: 'parent', - meta: { - icon: 'visualizeApp', - title: 'Foo', - editUrl: '/management/kibana/objects/savedVisualizations/1', - inAppUrl: { - path: '/app/visualize#/edit/1', - uiCapabilitiesPath: 'visualize.show', - }, - }, - }, - { - id: '2', - type: 'visualization', - relationship: 'parent', - meta: { - icon: 'visualizeApp', - title: 'Bar', - editUrl: '/management/kibana/objects/savedVisualizations/2', - inAppUrl: { - path: '/app/visualize#/edit/2', - uiCapabilitiesPath: 'visualize.show', - }, - }, - }, - { - id: '3', - type: 'visualization', - relationship: 'parent', - meta: { - icon: 'visualizeApp', - title: 'FooBar', - editUrl: '/management/kibana/objects/savedVisualizations/3', - inAppUrl: { - path: '/app/visualize#/edit/3', - uiCapabilitiesPath: 'visualize.show', - }, - }, - }, - ]); - }); - - it('should find relationships for index patterns', async () => { - const type = 'index-pattern'; - const id = 'foo'; - const size = 10; - - const savedObjectsClient = { - get: () => ({ - id: '1', - type: 'index-pattern', - attributes: { - title: 'My Index Pattern', - }, - }), - find: () => ({ - saved_objects: [ - { - id: '1', - type: 'visualization', - attributes: { - title: 'Foo', - kibanaSavedObjectMeta: { - searchSourceJSON: JSON.stringify({ - index: 'foo', - }), - }, - }, - }, - { - id: '2', - type: 'visualization', - attributes: { - title: 'Bar', - kibanaSavedObjectMeta: { - searchSourceJSON: JSON.stringify({ - index: 'foo', - }), - }, - }, - }, - { - id: '3', - type: 'visualization', - attributes: { - title: 'FooBar', - kibanaSavedObjectMeta: { - searchSourceJSON: JSON.stringify({ - index: 'foo2', - }), - }, - }, - }, - { - id: '1', - type: 'search', - attributes: { - title: 'My Saved Search', - }, - }, - ], - }), - }; - - const result = await findRelationships(type, id, { - size, - savedObjectsClient, - savedObjectsManagement, - savedObjectTypes: ['dashboard', 'visualization', 'search', 'index-pattern'], - }); - expect(result).to.eql([ - { - id: '1', - type: 'visualization', - relationship: 'parent', - meta: { - icon: 'visualizeApp', - title: 'Foo', - editUrl: '/management/kibana/objects/savedVisualizations/1', - inAppUrl: { - path: '/app/visualize#/edit/1', - uiCapabilitiesPath: 'visualize.show', - }, - }, - }, - { - id: '2', - type: 'visualization', - relationship: 'parent', - meta: { - icon: 'visualizeApp', - title: 'Bar', - editUrl: '/management/kibana/objects/savedVisualizations/2', - inAppUrl: { - path: '/app/visualize#/edit/2', - uiCapabilitiesPath: 'visualize.show', - }, - }, - }, - { - id: '3', - type: 'visualization', - relationship: 'parent', - meta: { - icon: 'visualizeApp', - title: 'FooBar', - editUrl: '/management/kibana/objects/savedVisualizations/3', - inAppUrl: { - path: '/app/visualize#/edit/3', - uiCapabilitiesPath: 'visualize.show', - }, - }, - }, - { - id: '1', - type: 'search', - relationship: 'parent', - meta: { - icon: 'search', - title: 'My Saved Search', - editUrl: '/management/kibana/objects/savedSearches/1', - inAppUrl: { - path: '/app/discover#//1', - uiCapabilitiesPath: 'discover.show', - }, - }, - }, - ]); - }); - - it('should return an empty object for non related objects', async () => { - const type = 'invalid'; - const id = 'foo'; - const size = 10; - - const savedObjectsClient = { - get: () => ({ - id: '1', - type: 'index-pattern', - attributes: { - title: 'My Index Pattern', - }, - references: [], - }), - find: () => ({ saved_objects: [] }), - }; - - const result = await findRelationships(type, id, { - size, - savedObjectsClient, - savedObjectsManagement, - savedObjectTypes: ['dashboard', 'visualization', 'search', 'index-pattern'], - }); - expect(result).to.eql({}); - }); -}); diff --git a/src/legacy/core_plugins/kibana/server/lib/import/import_dashboards.test.js b/src/legacy/core_plugins/kibana/server/lib/import/import_dashboards.test.js deleted file mode 100644 index 13e04e1e9e16e2..00000000000000 --- a/src/legacy/core_plugins/kibana/server/lib/import/import_dashboards.test.js +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { importDashboards } from './import_dashboards'; -import sinon from 'sinon'; - -describe('importDashboards(req)', () => { - let req; - let bulkCreateStub; - beforeEach(() => { - bulkCreateStub = sinon.stub().returns(Promise.resolve({ saved_objects: [] })); - req = { - query: {}, - payload: { - version: '6.0.0', - objects: [ - { id: 'dashboard-01', type: 'dashboard', attributes: { panelJSON: '{}' } }, - { id: 'panel-01', type: 'visualization', attributes: { visState: '{}' } }, - ], - }, - getSavedObjectsClient() { - return { - bulkCreate: bulkCreateStub, - }; - }, - }; - }); - - test('should call bulkCreate with each asset', () => { - return importDashboards(req).then(() => { - expect(bulkCreateStub.calledOnce).toEqual(true); - expect(bulkCreateStub.args[0][0]).toEqual([ - { - id: 'dashboard-01', - type: 'dashboard', - attributes: { panelJSON: '{}' }, - migrationVersion: {}, - }, - { - id: 'panel-01', - type: 'visualization', - attributes: { visState: '{}' }, - migrationVersion: {}, - }, - ]); - }); - }); - - test('should call bulkCreate with overwrite true if force is truthy', () => { - req.query = { force: 'true' }; - return importDashboards(req).then(() => { - expect(bulkCreateStub.calledOnce).toEqual(true); - expect(bulkCreateStub.args[0][1]).toEqual({ overwrite: true }); - }); - }); - - test('should exclude types based on exclude argument', () => { - req.query = { exclude: 'visualization' }; - return importDashboards(req).then(() => { - expect(bulkCreateStub.calledOnce).toEqual(true); - expect(bulkCreateStub.args[0][0]).toEqual([ - { - id: 'dashboard-01', - type: 'dashboard', - attributes: { panelJSON: '{}' }, - migrationVersion: {}, - }, - ]); - }); - }); -}); diff --git a/src/legacy/core_plugins/kibana/server/lib/management/saved_objects/inject_meta_attributes.test.js b/src/legacy/core_plugins/kibana/server/lib/management/saved_objects/inject_meta_attributes.test.js deleted file mode 100644 index b98160346011ae..00000000000000 --- a/src/legacy/core_plugins/kibana/server/lib/management/saved_objects/inject_meta_attributes.test.js +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { injectMetaAttributes } from './inject_meta_attributes'; - -function getManagementMock(savedObjectSchemas) { - return { - isImportAndExportable(type) { - return ( - !savedObjectSchemas[type] || savedObjectSchemas[type].isImportableAndExportable !== false - ); - }, - getDefaultSearchField(type) { - return savedObjectSchemas[type] && savedObjectSchemas[type].defaultSearchField; - }, - getIcon(type) { - return savedObjectSchemas[type] && savedObjectSchemas[type].icon; - }, - getTitle(savedObject) { - const { type } = savedObject; - const getTitle = savedObjectSchemas[type] && savedObjectSchemas[type].getTitle; - if (getTitle) { - return getTitle(savedObject); - } - }, - getEditUrl(savedObject) { - const { type } = savedObject; - const getEditUrl = savedObjectSchemas[type] && savedObjectSchemas[type].getEditUrl; - if (getEditUrl) { - return getEditUrl(savedObject); - } - }, - getInAppUrl(savedObject) { - const { type } = savedObject; - const getInAppUrl = savedObjectSchemas[type] && savedObjectSchemas[type].getInAppUrl; - if (getInAppUrl) { - return getInAppUrl(savedObject); - } - }, - }; -} - -test('works when no schema is defined for the type', () => { - const savedObject = { type: 'a' }; - const savedObjectsManagement = getManagementMock({}); - const result = injectMetaAttributes(savedObject, savedObjectsManagement); - expect(result).toEqual({ type: 'a', meta: {} }); -}); - -test('inject icon into meta attribute', () => { - const savedObject = { - type: 'a', - }; - const savedObjectsManagement = getManagementMock({ - a: { - icon: 'my-icon', - }, - }); - const result = injectMetaAttributes(savedObject, savedObjectsManagement); - expect(result).toEqual({ - type: 'a', - meta: { - icon: 'my-icon', - }, - }); -}); - -test('injects title into meta attribute', () => { - const savedObject = { - type: 'a', - }; - const savedObjectsManagement = getManagementMock({ - a: { - getTitle() { - return 'my-title'; - }, - }, - }); - const result = injectMetaAttributes(savedObject, savedObjectsManagement); - expect(result).toEqual({ - type: 'a', - meta: { - title: 'my-title', - }, - }); -}); - -test('injects editUrl into meta attribute', () => { - const savedObject = { - type: 'a', - }; - const savedObjectsManagement = getManagementMock({ - a: { - getEditUrl() { - return 'my-edit-url'; - }, - }, - }); - const result = injectMetaAttributes(savedObject, savedObjectsManagement); - expect(result).toEqual({ - type: 'a', - meta: { - editUrl: 'my-edit-url', - }, - }); -}); - -test('injects inAppUrl meta attribute', () => { - const savedObject = { - type: 'a', - }; - const savedObjectsManagement = getManagementMock({ - a: { - getInAppUrl() { - return { - path: 'my-in-app-url', - uiCapabilitiesPath: 'ui.path', - }; - }, - }, - }); - const result = injectMetaAttributes(savedObject, savedObjectsManagement); - expect(result).toEqual({ - type: 'a', - meta: { - inAppUrl: { - path: 'my-in-app-url', - uiCapabilitiesPath: 'ui.path', - }, - }, - }); -}); diff --git a/src/legacy/core_plugins/kibana/server/lib/management/saved_objects/relationships.js b/src/legacy/core_plugins/kibana/server/lib/management/saved_objects/relationships.js deleted file mode 100644 index e0a6c574b7ad87..00000000000000 --- a/src/legacy/core_plugins/kibana/server/lib/management/saved_objects/relationships.js +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { pick } from 'lodash'; -import { injectMetaAttributes } from './inject_meta_attributes'; - -export async function findRelationships(type, id, options = {}) { - const { size, savedObjectsClient, savedObjectTypes, savedObjectsManagement } = options; - - const { references = [] } = await savedObjectsClient.get(type, id); - - // Use a map to avoid duplicates, it does happen but have a different "name" in the reference - const referencedToBulkGetOpts = new Map( - references.map(({ type, id }) => [`${type}:${id}`, { id, type }]) - ); - - const [referencedObjects, referencedResponse] = await Promise.all([ - referencedToBulkGetOpts.size > 0 - ? savedObjectsClient.bulkGet([...referencedToBulkGetOpts.values()]) - : Promise.resolve({ saved_objects: [] }), - savedObjectsClient.find({ - hasReference: { type, id }, - perPage: size, - type: savedObjectTypes, - }), - ]); - - return [].concat( - referencedObjects.saved_objects - .map((obj) => injectMetaAttributes(obj, savedObjectsManagement)) - .map(extractCommonProperties) - .map((obj) => ({ - ...obj, - relationship: 'child', - })), - referencedResponse.saved_objects - .map((obj) => injectMetaAttributes(obj, savedObjectsManagement)) - .map(extractCommonProperties) - .map((obj) => ({ - ...obj, - relationship: 'parent', - })) - ); -} - -function extractCommonProperties(savedObject) { - return pick(savedObject, ['id', 'type', 'meta']); -} diff --git a/src/legacy/core_plugins/kibana/server/routes/api/export/index.js b/src/legacy/core_plugins/kibana/server/routes/api/export/index.js deleted file mode 100644 index ef556ed53f4fce..00000000000000 --- a/src/legacy/core_plugins/kibana/server/routes/api/export/index.js +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import Joi from 'joi'; -import moment from 'moment'; - -import { exportDashboards } from '../../../lib/export/export_dashboards'; - -export function exportApi(server) { - server.route({ - path: '/api/kibana/dashboards/export', - config: { - validate: { - query: Joi.object().keys({ - dashboard: Joi.alternatives() - .try(Joi.string(), Joi.array().items(Joi.string())) - .required(), - }), - }, - tags: ['api'], - }, - method: ['GET'], - handler: async (req, h) => { - const currentDate = moment.utc(); - return exportDashboards(req).then((resp) => { - const json = JSON.stringify(resp, null, ' '); - const filename = `kibana-dashboards.${currentDate.format('YYYY-MM-DD-HH-mm-ss')}.json`; - return h - .response(json) - .header('Content-Disposition', `attachment; filename="${filename}"`) - .header('Content-Type', 'application/json') - .header('Content-Length', Buffer.byteLength(json, 'utf8')); - }); - }, - }); -} diff --git a/src/legacy/core_plugins/timelion/public/legacy.ts b/src/legacy/core_plugins/timelion/public/legacy.ts index acb95e80fe18c0..7980291e2d4628 100644 --- a/src/legacy/core_plugins/timelion/public/legacy.ts +++ b/src/legacy/core_plugins/timelion/public/legacy.ts @@ -18,7 +18,7 @@ */ import { PluginInitializerContext } from 'kibana/public'; -import { npSetup } from 'ui/new_platform'; +import { npSetup, npStart } from 'ui/new_platform'; import { plugin } from '.'; import { TimelionPluginSetupDependencies } from './plugin'; import { LegacyDependenciesPlugin } from './shim'; @@ -32,4 +32,4 @@ const setupPlugins: Readonly = { const pluginInstance = plugin({} as PluginInitializerContext); export const setup = pluginInstance.setup(npSetup.core, setupPlugins); -export const start = pluginInstance.start(); +export const start = pluginInstance.start(npStart.core, npStart.plugins); diff --git a/src/legacy/core_plugins/timelion/public/plugin.ts b/src/legacy/core_plugins/timelion/public/plugin.ts index 8b021cda4bfb0b..1f837303a2b3df 100644 --- a/src/legacy/core_plugins/timelion/public/plugin.ts +++ b/src/legacy/core_plugins/timelion/public/plugin.ts @@ -16,10 +16,17 @@ * specific language governing permissions and limitations * under the License. */ -import { CoreSetup, Plugin, PluginInitializerContext, IUiSettingsClient } from 'kibana/public'; +import { + CoreSetup, + Plugin, + PluginInitializerContext, + IUiSettingsClient, + CoreStart, +} from 'kibana/public'; import { getTimeChart } from './panels/timechart/timechart'; import { Panel } from './panels/panel'; import { LegacyDependenciesPlugin, LegacyDependenciesPluginSetup } from './shim'; +import { KibanaLegacyStart } from '../../../../plugins/kibana_legacy/public'; /** @internal */ export interface TimelionVisualizationDependencies extends LegacyDependenciesPluginSetup { @@ -59,7 +66,9 @@ export class TimelionPlugin implements Plugin, void> { dependencies.timelionPanels.set(timeChartPanel.name, timeChartPanel); } - public start() {} + public start(core: CoreStart, { kibanaLegacy }: { kibanaLegacy: KibanaLegacyStart }) { + kibanaLegacy.loadFontAwesome(); + } public stop(): void {} } diff --git a/src/legacy/ui/public/autoload/all.js b/src/legacy/ui/public/autoload/all.js index 5c95afb7a0628d..be9b29aa944c95 100644 --- a/src/legacy/ui/public/autoload/all.js +++ b/src/legacy/ui/public/autoload/all.js @@ -20,4 +20,3 @@ import './accessibility'; import './modules'; import './settings'; -import './styles'; diff --git a/src/legacy/ui/public/autoload/styles.js b/src/legacy/ui/public/autoload/styles.js deleted file mode 100644 index c623acca07b015..00000000000000 --- a/src/legacy/ui/public/autoload/styles.js +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import 'ui/styles/font_awesome.less'; diff --git a/src/legacy/ui/public/directives/__tests__/input_focus.js b/src/legacy/ui/public/directives/__tests__/input_focus.js index 45b1821cbfd217..a9cd9b3c87974c 100644 --- a/src/legacy/ui/public/directives/__tests__/input_focus.js +++ b/src/legacy/ui/public/directives/__tests__/input_focus.js @@ -21,6 +21,7 @@ import expect from '@kbn/expect'; import ngMock from 'ng_mock'; import $ from 'jquery'; import '../input_focus'; +import uiRoutes from 'ui/routes'; describe('Input focus directive', function () { let $compile; @@ -32,6 +33,8 @@ describe('Input focus directive', function () { let selectedText; const inputValue = 'Input Text Value'; + uiRoutes.enable(); + beforeEach(ngMock.module('kibana')); beforeEach( ngMock.inject(function (_$compile_, _$rootScope_, _$timeout_) { diff --git a/src/legacy/ui/public/new_platform/new_platform.karma_mock.js b/src/legacy/ui/public/new_platform/new_platform.karma_mock.js index d98770842a0f09..35380ada51a0ad 100644 --- a/src/legacy/ui/public/new_platform/new_platform.karma_mock.js +++ b/src/legacy/ui/public/new_platform/new_platform.karma_mock.js @@ -263,7 +263,6 @@ export const npSetup = { }, kibanaLegacy: { registerLegacyApp: () => {}, - registerLegacyAppAlias: () => {}, forwardApp: () => {}, config: { defaultAppId: 'home', @@ -379,9 +378,8 @@ export const npStart = { registerType: sinon.fake(), }, kibanaLegacy: { - getApps: () => [], getForwards: () => [], - getLegacyAppAliases: () => [], + loadFontAwesome: () => {}, config: { defaultAppId: 'home', }, diff --git a/src/legacy/ui/public/styles/font_awesome.less b/src/legacy/ui/public/styles/font_awesome.less deleted file mode 100644 index 428e3c6b83f89f..00000000000000 --- a/src/legacy/ui/public/styles/font_awesome.less +++ /dev/null @@ -1,10 +0,0 @@ -// Needs to remain a LESS file to point to the correct path for the fonts themeselves -@import "~font-awesome/less/font-awesome"; - -// new file icon -.@{fa-css-prefix}-file-new-o:before { content: @fa-var-file-o; } -.@{fa-css-prefix}-file-new-o:after { content: @fa-var-plus; position: relative; margin-left: -1.0em; font-size: 0.5em; } - -// alias for alert types - allows class="fa fa-{{alertType}}" -.fa-success:before { content: @fa-var-check; } -.fa-danger:before { content: @fa-var-exclamation-circle; } diff --git a/src/legacy/ui/public/visualize/loader/pipeline_helpers/utilities.ts b/src/legacy/ui/public/visualize/loader/pipeline_helpers/utilities.ts deleted file mode 100644 index cb25c66dfd2fe6..00000000000000 --- a/src/legacy/ui/public/visualize/loader/pipeline_helpers/utilities.ts +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { npStart } from 'ui/new_platform'; -import { fieldFormats, IFieldFormat } from '../../../../../../plugins/data/public'; -import { SerializedFieldFormat } from '../../../../../../plugins/expressions/common/types'; - -type FormatFactory = (mapping?: SerializedFieldFormat) => IFieldFormat; - -const createFormat = fieldFormats.serialize; -const getFormat: FormatFactory = (mapping?) => { - return npStart.plugins.data.fieldFormats.deserialize(mapping as any); -}; - -export { getFormat, createFormat, FormatFactory }; diff --git a/src/legacy/ui/ui_render/ui_render_mixin.js b/src/legacy/ui/ui_render/ui_render_mixin.js index b4f82552972401..0cfcb91aa94efe 100644 --- a/src/legacy/ui/ui_render/ui_render_mixin.js +++ b/src/legacy/ui/ui_render/ui_render_mixin.js @@ -130,7 +130,6 @@ export function uiRenderMixin(kbnServer, server, config) { `${basePath}/node_modules/@kbn/ui-framework/dist/kui_light.css`, `${regularBundlePath}/light_theme.style.css`, ]), - `${regularBundlePath}/commons.style.css`, ...(isCore ? [] : [ @@ -155,13 +154,7 @@ export function uiRenderMixin(kbnServer, server, config) { (filename) => `${regularBundlePath}/kbn-ui-shared-deps/${filename}` ), `${regularBundlePath}/kbn-ui-shared-deps/${UiSharedDeps.jsFilename}`, - ...(isCore - ? [] - : [ - `${dllBundlePath}/vendors_runtime.bundle.dll.js`, - ...dllJsChunks, - `${regularBundlePath}/commons.bundle.js`, - ]), + ...(isCore ? [] : [`${dllBundlePath}/vendors_runtime.bundle.dll.js`, ...dllJsChunks]), `${regularBundlePath}/core/core.entry.js`, ...kpPluginIds.map( diff --git a/src/optimize/base_optimizer.js b/src/optimize/base_optimizer.js index 12131b89e03c10..55752db55e28a4 100644 --- a/src/optimize/base_optimizer.js +++ b/src/optimize/base_optimizer.js @@ -266,13 +266,6 @@ export default class BaseOptimizer { optimization: { splitChunks: { cacheGroups: { - commons: { - name: 'commons', - chunks: (chunk) => - chunk.canBeInitial() && chunk.name !== 'light_theme' && chunk.name !== 'dark_theme', - minChunks: 2, - reuseExistingChunk: true, - }, light_theme: { name: 'light_theme', test: (m) => diff --git a/src/plugins/advanced_settings/public/management_app/advanced_settings.scss b/src/plugins/advanced_settings/public/management_app/_advanced_settings.scss similarity index 100% rename from src/plugins/advanced_settings/public/management_app/advanced_settings.scss rename to src/plugins/advanced_settings/public/management_app/_advanced_settings.scss diff --git a/src/plugins/advanced_settings/public/management_app/_index.scss b/src/plugins/advanced_settings/public/management_app/index.scss similarity index 100% rename from src/plugins/advanced_settings/public/management_app/_index.scss rename to src/plugins/advanced_settings/public/management_app/index.scss diff --git a/src/plugins/advanced_settings/public/management_app/mount_management_section.tsx b/src/plugins/advanced_settings/public/management_app/mount_management_section.tsx index b4779d051ab027..ab348451b1eef8 100644 --- a/src/plugins/advanced_settings/public/management_app/mount_management_section.tsx +++ b/src/plugins/advanced_settings/public/management_app/mount_management_section.tsx @@ -29,6 +29,8 @@ import { AdvancedSettings } from './advanced_settings'; import { ManagementAppMountParams } from '../../../management/public'; import { ComponentRegistry } from '../types'; +import './index.scss'; + const title = i18n.translate('advancedSettings.advancedSettingsLabel', { defaultMessage: 'Advanced Settings', }); diff --git a/src/plugins/dashboard/public/application/application.ts b/src/plugins/dashboard/public/application/application.ts index 543450916c5056..08eeb19dcda930 100644 --- a/src/plugins/dashboard/public/application/application.ts +++ b/src/plugins/dashboard/public/application/application.ts @@ -68,11 +68,12 @@ export interface RenderDeps { embeddable: EmbeddableStart; localStorage: Storage; share?: SharePluginStart; - config: KibanaLegacyStart['config']; usageCollection?: UsageCollectionSetup; navigateToDefaultApp: KibanaLegacyStart['navigateToDefaultApp']; + navigateToLegacyKibanaUrl: KibanaLegacyStart['navigateToLegacyKibanaUrl']; scopedHistory: () => ScopedHistory; savedObjects: SavedObjectsStart; + restorePreviousUrl: () => void; } let angularModuleInstance: IModule | null = null; diff --git a/src/plugins/dashboard/public/application/legacy_app.js b/src/plugins/dashboard/public/application/legacy_app.js index 2d99a2c6a22533..8b8fdcb7a76acc 100644 --- a/src/plugins/dashboard/public/application/legacy_app.js +++ b/src/plugins/dashboard/public/application/legacy_app.js @@ -242,9 +242,17 @@ export function initDashboardApp(app, deps) { }, }) .otherwise({ - template: '', - controller: function () { - deps.navigateToDefaultApp(); + resolveRedirectTo: function ($rootScope) { + const path = window.location.hash.substr(1); + deps.restorePreviousUrl(); + $rootScope.$applyAsync(() => { + const { navigated } = deps.navigateToLegacyKibanaUrl(path); + if (!navigated) { + deps.navigateToDefaultApp(); + } + }); + // prevent angular from completing the navigation + return new Promise(() => {}); }, }); }); diff --git a/src/plugins/dashboard/public/plugin.tsx b/src/plugins/dashboard/public/plugin.tsx index 5e2cb609653964..041a02a251e8ab 100644 --- a/src/plugins/dashboard/public/plugin.tsx +++ b/src/plugins/dashboard/public/plugin.tsx @@ -215,7 +215,13 @@ export class DashboardPlugin const placeholderFactory = new PlaceholderEmbeddableFactory(); embeddable.registerEmbeddableFactory(placeholderFactory.type, placeholderFactory); - const { appMounted, appUnMounted, stop: stopUrlTracker, getActiveUrl } = createKbnUrlTracker({ + const { + appMounted, + appUnMounted, + stop: stopUrlTracker, + getActiveUrl, + restorePreviousUrl, + } = createKbnUrlTracker({ baseUrl: core.http.basePath.prepend('/app/dashboards'), defaultSubUrl: `#${DashboardConstants.LANDING_PAGE_PATH}`, storageKey: `lastUrl:${core.http.basePath.get()}:dashboard`, @@ -260,7 +266,7 @@ export class DashboardPlugin navigation, share: shareStart, data: dataStart, - kibanaLegacy: { dashboardConfig, navigateToDefaultApp }, + kibanaLegacy: { dashboardConfig, navigateToDefaultApp, navigateToLegacyKibanaUrl }, savedObjects, } = pluginsStart; @@ -269,6 +275,7 @@ export class DashboardPlugin core: coreStart, dashboardConfig, navigateToDefaultApp, + navigateToLegacyKibanaUrl, navigation, share: shareStart, data: dataStart, @@ -277,7 +284,6 @@ export class DashboardPlugin chrome: coreStart.chrome, addBasePath: coreStart.http.basePath.prepend, uiSettings: coreStart.uiSettings, - config: kibanaLegacy.config, savedQueryService: dataStart.query.savedQueries, embeddable: embeddableStart, dashboardCapabilities: coreStart.application.capabilities.dashboard, @@ -289,6 +295,7 @@ export class DashboardPlugin usageCollection, scopedHistory: () => this.currentHistory!, savedObjects, + restorePreviousUrl, }; // make sure the index pattern list is up to date await dataStart.indexPatterns.clearCache(); @@ -305,6 +312,15 @@ export class DashboardPlugin initAngularBootstrap(); core.application.register(app); + kibanaLegacy.forwardApp( + DashboardConstants.DASHBOARDS_ID, + DashboardConstants.DASHBOARDS_ID, + (path) => { + const [, tail] = /(\?.*)/.exec(path) || []; + // carry over query if it exists + return `#/list${tail || ''}`; + } + ); kibanaLegacy.forwardApp( DashboardConstants.DASHBOARD_ID, DashboardConstants.DASHBOARDS_ID, @@ -322,15 +338,6 @@ export class DashboardPlugin return `#/view/${id}${tail || ''}`; } ); - kibanaLegacy.forwardApp( - DashboardConstants.DASHBOARDS_ID, - DashboardConstants.DASHBOARDS_ID, - (path) => { - const [, tail] = /(\?.*)/.exec(path) || []; - // carry over query if it exists - return `#/list${tail || ''}`; - } - ); if (home) { home.featureCatalogue.register({ diff --git a/src/plugins/data/common/field_formats/index.ts b/src/plugins/data/common/field_formats/index.ts index 5c67073c07dd54..104ff030873aa6 100644 --- a/src/plugins/data/common/field_formats/index.ts +++ b/src/plugins/data/common/field_formats/index.ts @@ -40,7 +40,7 @@ export { TruncateFormat, } from './converters'; -export { getHighlightRequest, serializeFieldFormat } from './utils'; +export { getHighlightRequest } from './utils'; export { DEFAULT_CONVERTER_COLOR } from './constants/color_default'; export { FIELD_FORMAT_IDS } from './types'; diff --git a/src/plugins/data/common/field_formats/utils/index.ts b/src/plugins/data/common/field_formats/utils/index.ts index 3832c941ffad75..eb020c17ca09c9 100644 --- a/src/plugins/data/common/field_formats/utils/index.ts +++ b/src/plugins/data/common/field_formats/utils/index.ts @@ -22,6 +22,5 @@ import { IFieldFormat } from '../index'; export { asPrettyString } from './as_pretty_string'; export { getHighlightHtml, getHighlightRequest } from './highlight'; -export { serializeFieldFormat } from './serialize'; export type FormatFactory = (mapping?: SerializedFieldFormat) => IFieldFormat; diff --git a/src/plugins/data/common/field_formats/utils/serialize.ts b/src/plugins/data/common/field_formats/utils/serialize.ts deleted file mode 100644 index 1092c90d19451a..00000000000000 --- a/src/plugins/data/common/field_formats/utils/serialize.ts +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { IAggConfig } from 'src/plugins/data/public'; -import { SerializedFieldFormat } from '../../../../expressions/common/types'; - -export const serializeFieldFormat = (agg: IAggConfig): SerializedFieldFormat => { - const format: SerializedFieldFormat = agg.params.field ? agg.params.field.format.toJSON() : {}; - const formats: Record SerializedFieldFormat> = { - date_range: () => ({ id: 'date_range', params: format }), - ip_range: () => ({ id: 'ip_range', params: format }), - percentile_ranks: () => ({ id: 'percent' }), - count: () => ({ id: 'number' }), - cardinality: () => ({ id: 'number' }), - date_histogram: () => ({ - id: 'date', - params: { - pattern: (agg as any).buckets.getScaledDateFormat(), - }, - }), - terms: () => ({ - id: 'terms', - params: { - id: format.id, - otherBucketLabel: agg.params.otherBucketLabel, - missingBucketLabel: agg.params.missingBucketLabel, - ...format.params, - }, - }), - range: () => ({ - id: 'range', - params: { id: format.id, ...format.params }, - }), - }; - - return formats[agg.type.name] ? formats[agg.type.name]() : format; -}; diff --git a/src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts b/src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts index 666d99362ce803..cd39a965ae6fce 100644 --- a/src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts +++ b/src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts @@ -198,7 +198,7 @@ export class IndexPattern implements IIndexPattern { private updateFromElasticSearch(response: any, forceFieldRefresh: boolean = false) { if (!response.found) { - throw new SavedObjectNotFound(type, this.id, 'kibana#/management/kibana/indexPatterns'); + throw new SavedObjectNotFound(type, this.id, 'management/kibana/indexPatterns'); } _.forOwn(this.mapping, (fieldMapping: FieldMappingSpec, name: string | undefined) => { diff --git a/src/plugins/data/public/field_formats/utils/deserialize.ts b/src/plugins/data/public/field_formats/utils/deserialize.ts index d9c713c8b1eb4b..26baa5fdeb1e4b 100644 --- a/src/plugins/data/public/field_formats/utils/deserialize.ts +++ b/src/plugins/data/public/field_formats/utils/deserialize.ts @@ -18,107 +18,42 @@ */ import { identity } from 'lodash'; -import { i18n } from '@kbn/i18n'; -import { convertDateRangeToString, DateRangeKey } from '../../search/aggs/buckets/lib/date_range'; -import { convertIPRangeToString, IpRangeKey } from '../../search/aggs/buckets/lib/ip_range'; + import { SerializedFieldFormat } from '../../../../expressions/common/types'; -import { FieldFormatId, FieldFormatsContentType, IFieldFormat } from '../..'; + import { FieldFormat } from '../../../common'; -import { DataPublicPluginStart } from '../../../public'; -import { getUiSettings } from '../../../public/services'; import { FormatFactory } from '../../../common/field_formats/utils'; - -interface TermsFieldFormatParams { - otherBucketLabel: string; - missingBucketLabel: string; - id: string; -} - -function isTermsFieldFormat( - serializedFieldFormat: SerializedFieldFormat -): serializedFieldFormat is SerializedFieldFormat { - return serializedFieldFormat.id === 'terms'; -} +import { DataPublicPluginStart, IFieldFormat } from '../../../public'; +import { getUiSettings } from '../../../public/services'; +import { getFormatWithAggs } from '../../search/aggs/utils'; const getConfig = (key: string, defaultOverride?: any): any => getUiSettings().get(key, defaultOverride); const DefaultFieldFormat = FieldFormat.from(identity); -const getFieldFormat = ( - fieldFormatsService: DataPublicPluginStart['fieldFormats'], - id?: FieldFormatId, - params: object = {} -): IFieldFormat => { - if (id) { - const Format = fieldFormatsService.getType(id); - - if (Format) { - return new Format(params, getConfig); - } - } - - return new DefaultFieldFormat(); -}; - export const deserializeFieldFormat: FormatFactory = function ( this: DataPublicPluginStart['fieldFormats'], - mapping?: SerializedFieldFormat + serializedFieldFormat?: SerializedFieldFormat ) { - if (!mapping) { + if (!serializedFieldFormat) { return new DefaultFieldFormat(); } - const { id } = mapping; - if (id === 'range') { - const RangeFormat = FieldFormat.from((range: any) => { - const nestedFormatter = mapping.params as SerializedFieldFormat; - const format = getFieldFormat(this, nestedFormatter.id, nestedFormatter.params); - const gte = '\u2265'; - const lt = '\u003c'; - return i18n.translate('data.aggTypes.buckets.ranges.rangesFormatMessage', { - defaultMessage: '{gte} {from} and {lt} {to}', - values: { - gte, - from: format.convert(range.gte), - lt, - to: format.convert(range.lt), - }, - }); - }); - return new RangeFormat(); - } else if (id === 'date_range') { - const nestedFormatter = mapping.params as SerializedFieldFormat; - const DateRangeFormat = FieldFormat.from((range: DateRangeKey) => { - const format = getFieldFormat(this, nestedFormatter.id, nestedFormatter.params); - return convertDateRangeToString(range, format.convert.bind(format)); - }); - return new DateRangeFormat(); - } else if (id === 'ip_range') { - const nestedFormatter = mapping.params as SerializedFieldFormat; - const IpRangeFormat = FieldFormat.from((range: IpRangeKey) => { - const format = getFieldFormat(this, nestedFormatter.id, nestedFormatter.params); - return convertIPRangeToString(range, format.convert.bind(format)); - }); - return new IpRangeFormat(); - } else if (isTermsFieldFormat(mapping) && mapping.params) { - const { params } = mapping; - const convert = (val: string, type: FieldFormatsContentType) => { - const format = getFieldFormat(this, params.id, mapping.params); - if (val === '__other__') { - return params.otherBucketLabel; - } - if (val === '__missing__') { - return params.missingBucketLabel; + const getFormat = (mapping: SerializedFieldFormat): IFieldFormat => { + const { id, params = {} } = mapping; + if (id) { + const Format = this.getType(id); + + if (Format) { + return new Format(params, getConfig); } + } + + return new DefaultFieldFormat(); + }; - return format.convert(val, type); - }; + // decorate getFormat to handle custom types created by aggs + const getFieldFormat = getFormatWithAggs(getFormat); - return { - convert, - getConverterFor: (type: FieldFormatsContentType) => (val: string) => convert(val, type), - } as IFieldFormat; - } else { - return getFieldFormat(this, id, mapping.params); - } + return getFieldFormat(serializedFieldFormat); }; diff --git a/src/plugins/data/public/index.ts b/src/plugins/data/public/index.ts index 1554ac71f8c552..984ce18aa4d839 100644 --- a/src/plugins/data/public/index.ts +++ b/src/plugins/data/public/index.ts @@ -168,7 +168,6 @@ import { UrlFormat, StringFormat, TruncateFormat, - serializeFieldFormat, } from '../common/field_formats'; import { DateFormat } from './field_formats'; @@ -179,8 +178,6 @@ export const fieldFormats = { FieldFormat, FieldFormatsRegistry, // exported only for tests. Consider mock. - serialize: serializeFieldFormat, - DEFAULT_CONVERTER_COLOR, HTML_CONTEXT_TYPE, TEXT_CONTEXT_TYPE, diff --git a/src/plugins/data/public/public.api.md b/src/plugins/data/public/public.api.md index 23213d4d1165a6..31dc5b51a06f56 100644 --- a/src/plugins/data/public/public.api.md +++ b/src/plugins/data/public/public.api.md @@ -154,6 +154,7 @@ import { SearchParams } from 'elasticsearch'; import { SearchResponse as SearchResponse_2 } from 'elasticsearch'; import { SearchShardsParams } from 'elasticsearch'; import { SearchTemplateParams } from 'elasticsearch'; +import { SerializedFieldFormat as SerializedFieldFormat_2 } from 'src/plugins/expressions/public'; import { SimpleSavedObject } from 'src/core/public'; import { SnapshotCreateParams } from 'elasticsearch'; import { SnapshotCreateRepositoryParams } from 'elasticsearch'; @@ -621,7 +622,6 @@ export type FieldFormatInstanceType = (new (params?: any, getConfig?: FieldForma export const fieldFormats: { FieldFormat: typeof FieldFormat; FieldFormatsRegistry: typeof FieldFormatsRegistry; - serialize: (agg: import("./search").AggConfig) => import("../../expressions").SerializedFieldFormat; DEFAULT_CONVERTER_COLOR: { range: string; regex: string; @@ -1954,41 +1954,41 @@ export const UI_SETTINGS: { // src/plugins/data/public/index.ts:136:21 - (ae-forgotten-export) The symbol "getEsQueryConfig" needs to be exported by the entry point index.d.ts // src/plugins/data/public/index.ts:136:21 - (ae-forgotten-export) The symbol "luceneStringToDsl" needs to be exported by the entry point index.d.ts // src/plugins/data/public/index.ts:136:21 - (ae-forgotten-export) The symbol "decorateQuery" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "FieldFormatsRegistry" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "BoolFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "BytesFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "ColorFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "DateNanosFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "DurationFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "IpFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "NumberFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "PercentFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "RelativeDateFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "SourceFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "StaticLookupFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "UrlFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "StringFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "TruncateFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:236:27 - (ae-forgotten-export) The symbol "isFilterable" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:236:27 - (ae-forgotten-export) The symbol "isNestedField" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:236:27 - (ae-forgotten-export) The symbol "validateIndexPattern" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:236:27 - (ae-forgotten-export) The symbol "getFromSavedObject" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:236:27 - (ae-forgotten-export) The symbol "flattenHitWrapper" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:236:27 - (ae-forgotten-export) The symbol "formatHitProvider" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:376:20 - (ae-forgotten-export) The symbol "getRequestInspectorStats" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:376:20 - (ae-forgotten-export) The symbol "getResponseInspectorStats" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:376:20 - (ae-forgotten-export) The symbol "tabifyAggResponse" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:376:20 - (ae-forgotten-export) The symbol "tabifyGetColumns" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:378:1 - (ae-forgotten-export) The symbol "CidrMask" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:379:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:388:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:389:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:390:1 - (ae-forgotten-export) The symbol "isDateHistogramBucketAggConfig" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:394:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:395:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:398:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:399:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:402:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "FieldFormatsRegistry" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "BoolFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "BytesFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "ColorFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "DateNanosFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "DurationFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "IpFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "NumberFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "PercentFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "RelativeDateFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "SourceFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "StaticLookupFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "UrlFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "StringFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:177:26 - (ae-forgotten-export) The symbol "TruncateFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:233:27 - (ae-forgotten-export) The symbol "isFilterable" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:233:27 - (ae-forgotten-export) The symbol "isNestedField" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:233:27 - (ae-forgotten-export) The symbol "validateIndexPattern" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:233:27 - (ae-forgotten-export) The symbol "getFromSavedObject" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:233:27 - (ae-forgotten-export) The symbol "flattenHitWrapper" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:233:27 - (ae-forgotten-export) The symbol "formatHitProvider" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:373:20 - (ae-forgotten-export) The symbol "getRequestInspectorStats" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:373:20 - (ae-forgotten-export) The symbol "getResponseInspectorStats" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:373:20 - (ae-forgotten-export) The symbol "tabifyAggResponse" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:373:20 - (ae-forgotten-export) The symbol "tabifyGetColumns" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:375:1 - (ae-forgotten-export) The symbol "CidrMask" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:376:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:385:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:386:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:387:1 - (ae-forgotten-export) The symbol "isDateHistogramBucketAggConfig" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:391:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:392:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:395:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:396:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:399:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts // src/plugins/data/public/query/state_sync/connect_to_query_state.ts:40:60 - (ae-forgotten-export) The symbol "FilterStateStore" needs to be exported by the entry point index.d.ts // src/plugins/data/public/types.ts:52:5 - (ae-forgotten-export) The symbol "createFiltersFromValueClickAction" needs to be exported by the entry point index.d.ts // src/plugins/data/public/types.ts:53:5 - (ae-forgotten-export) The symbol "createFiltersFromRangeSelectAction" needs to be exported by the entry point index.d.ts diff --git a/src/plugins/data/public/search/aggs/agg_config.test.ts b/src/plugins/data/public/search/aggs/agg_config.test.ts index 6a0dad07b69bb5..95e0b2cd27186b 100644 --- a/src/plugins/data/public/search/aggs/agg_config.test.ts +++ b/src/plugins/data/public/search/aggs/agg_config.test.ts @@ -25,7 +25,11 @@ import { AggType } from './agg_type'; import { AggTypesRegistryStart } from './agg_types_registry'; import { mockDataServices, mockAggTypesRegistry } from './test_helpers'; import { MetricAggType } from './metrics/metric_agg_type'; -import { Field as IndexPatternField, IndexPattern } from '../../index_patterns'; +import { + Field as IndexPatternField, + IndexPattern, + IIndexPatternFieldList, +} from '../../index_patterns'; import { stubIndexPatternWithFields } from '../../../public/stubs'; import { FieldFormatsStart } from '../../field_formats'; import { fieldFormatsServiceMock } from '../../field_formats/mocks'; @@ -370,6 +374,109 @@ describe('AggConfig', () => { }); }); + describe('#toSerializedFieldFormat', () => { + beforeEach(() => { + indexPattern.fields.getByName = identity as IIndexPatternFieldList['getByName']; + }); + + it('works with aggs that have a special format type', () => { + const configStates = [ + { + type: 'count', + params: {}, + }, + { + type: 'date_histogram', + params: { field: '@timestamp' }, + }, + { + type: 'terms', + params: { field: 'machine.os.keyword' }, + }, + ]; + const ac = new AggConfigs(indexPattern, configStates, { typesRegistry, fieldFormats }); + + expect(ac.aggs.map((agg) => agg.toSerializedFieldFormat())).toMatchInlineSnapshot(` + Array [ + Object { + "id": "number", + }, + Object { + "id": "date", + "params": Object { + "pattern": "HH:mm:ss.SSS", + }, + }, + Object { + "id": "terms", + "params": Object { + "id": undefined, + "missingBucketLabel": "Missing", + "otherBucketLabel": "Other", + }, + }, + ] + `); + }); + + it('works with pipeline aggs', () => { + const configStates = [ + { + type: 'max_bucket', + params: { + customMetric: { + type: 'cardinality', + params: { + field: 'bytes', + }, + }, + }, + }, + { + type: 'cumulative_sum', + params: { + buckets_path: '1', + customMetric: { + type: 'cardinality', + params: { + field: 'bytes', + }, + }, + }, + }, + { + type: 'percentile_ranks', + id: 'myMetricAgg', + params: {}, + }, + { + type: 'cumulative_sum', + params: { + metricAgg: 'myMetricAgg', + }, + }, + ]; + const ac = new AggConfigs(indexPattern, configStates, { typesRegistry, fieldFormats }); + + expect(ac.aggs.map((agg) => agg.toSerializedFieldFormat())).toMatchInlineSnapshot(` + Array [ + Object { + "id": "number", + }, + Object { + "id": "number", + }, + Object { + "id": "percent", + }, + Object { + "id": "percent", + }, + ] + `); + }); + }); + describe('#toExpressionAst', () => { beforeEach(() => { fieldFormats.getDefaultInstance = (() => ({ diff --git a/src/plugins/data/public/search/aggs/agg_config.ts b/src/plugins/data/public/search/aggs/agg_config.ts index ee4116eefc0e27..a2b74eca584766 100644 --- a/src/plugins/data/public/search/aggs/agg_config.ts +++ b/src/plugins/data/public/search/aggs/agg_config.ts @@ -20,7 +20,11 @@ import _ from 'lodash'; import { i18n } from '@kbn/i18n'; import { Assign, Ensure } from '@kbn/utility-types'; -import { ExpressionAstFunction, ExpressionAstArgument } from 'src/plugins/expressions/public'; +import { + ExpressionAstFunction, + ExpressionAstArgument, + SerializedFieldFormat, +} from 'src/plugins/expressions/public'; import { IAggType } from './agg_type'; import { writeParams } from './agg_params'; import { IAggConfigs } from './agg_configs'; @@ -42,7 +46,7 @@ export type AggConfigSerialized = Ensure< type: string; enabled?: boolean; id?: string; - params?: SerializableState; + params?: {} | SerializableState; schema?: string; }, SerializableState @@ -298,8 +302,8 @@ export class AggConfig { id: this.id, enabled: this.enabled, type: this.type && this.type.name, - schema: this.schema, params: outParams as SerializableState, + ...(this.schema && { schema: this.schema }), }; } @@ -310,6 +314,19 @@ export class AggConfig { return this.serialize(); } + /** + * Returns a serialized field format for the field used in this agg. + * This can be passed to fieldFormats.deserialize to get the field + * format instance. + * + * @public + */ + toSerializedFieldFormat(): + | {} + | Ensure, SerializableState> { + return this.type ? this.type.getSerializedFormat(this) : {}; + } + /** * @returns Returns an ExpressionAst representing the function for this agg type. */ diff --git a/src/plugins/data/public/search/aggs/agg_type.test.ts b/src/plugins/data/public/search/aggs/agg_type.test.ts index 18783bbd9a7605..cc45b935d45b54 100644 --- a/src/plugins/data/public/search/aggs/agg_type.test.ts +++ b/src/plugins/data/public/search/aggs/agg_type.test.ts @@ -199,5 +199,73 @@ describe('AggType Class', () => { expect(aggType.getFormat(aggConfig)).toBe('default'); }); }); + + describe('getSerializedFormat', () => { + test('returns the default serialized field format if it exists', () => { + const aggConfig = ({ + params: { + field: { + format: { + toJSON: () => ({ id: 'format' }), + }, + }, + }, + } as unknown) as IAggConfig; + const aggType = new AggType( + { + name: 'name', + title: 'title', + }, + dependencies + ); + expect(aggType.getSerializedFormat(aggConfig)).toMatchInlineSnapshot(` + Object { + "id": "format", + } + `); + }); + + test('returns an empty object if a field param does not exist', () => { + const aggConfig = ({ + params: {}, + } as unknown) as IAggConfig; + const aggType = new AggType( + { + name: 'name', + title: 'title', + }, + dependencies + ); + expect(aggType.getSerializedFormat(aggConfig)).toMatchInlineSnapshot(`Object {}`); + }); + + test('uses a custom getSerializedFormat function if defined', () => { + const aggConfig = ({ + params: { + field: { + format: { + toJSON: () => ({ id: 'format' }), + }, + }, + }, + } as unknown) as IAggConfig; + const getSerializedFormat = jest.fn().mockReturnValue({ id: 'hello' }); + const aggType = new AggType( + { + name: 'name', + title: 'title', + getSerializedFormat, + }, + dependencies + ); + const serialized = aggType.getSerializedFormat(aggConfig); + expect(getSerializedFormat).toHaveBeenCalledWith(aggConfig); + expect(serialized).toMatchInlineSnapshot(` + Object { + "id": "hello", + } + `); + }); + }); }); }); diff --git a/src/plugins/data/public/search/aggs/agg_type.ts b/src/plugins/data/public/search/aggs/agg_type.ts index fb0cb609a08cfe..e909cd8134e83f 100644 --- a/src/plugins/data/public/search/aggs/agg_type.ts +++ b/src/plugins/data/public/search/aggs/agg_type.ts @@ -19,8 +19,10 @@ import { constant, noop, identity } from 'lodash'; import { i18n } from '@kbn/i18n'; -import { initParams } from './agg_params'; +import { SerializedFieldFormat } from 'src/plugins/expressions/public'; + +import { initParams } from './agg_params'; import { AggConfig } from './agg_config'; import { IAggConfigs } from './agg_configs'; import { Adapters } from '../../../../../plugins/inspector/public'; @@ -57,6 +59,7 @@ export interface AggTypeConfig< abortSignal?: AbortSignal ) => Promise; getFormat?: (agg: TAggConfig) => IFieldFormat; + getSerializedFormat?: (agg: TAggConfig) => SerializedFieldFormat; getValue?: (agg: TAggConfig, bucket: any) => any; getKey?: (bucket: any, key: any, agg: TAggConfig) => any; } @@ -204,6 +207,17 @@ export class AggType< */ getFormat: (agg: TAggConfig) => IFieldFormat; + /** + * Get the serialized format for the values produced by this agg type, + * overridden by several metrics that always output a simple number. + * You can pass this output to fieldFormatters.deserialize to get + * the formatter instance. + * + * @param {agg} agg - the agg to pick a format for + * @return {SerializedFieldFormat} + */ + getSerializedFormat: (agg: TAggConfig) => SerializedFieldFormat; + getValue: (agg: TAggConfig, bucket: any) => any; getKey?: (bucket: any, key: any, agg: TAggConfig) => any; @@ -277,6 +291,13 @@ export class AggType< return field ? field.format : fieldFormats.getDefaultInstance(KBN_FIELD_TYPES.STRING); }); + + this.getSerializedFormat = + config.getSerializedFormat || + ((agg: TAggConfig) => { + return agg.params.field ? agg.params.field.format.toJSON() : {}; + }); + this.getValue = config.getValue || ((agg: TAggConfig, bucket: any) => {}); } } diff --git a/src/plugins/data/public/search/aggs/buckets/date_histogram.ts b/src/plugins/data/public/search/aggs/buckets/date_histogram.ts index 8a5596f669cb78..fc42d43b2fea81 100644 --- a/src/plugins/data/public/search/aggs/buckets/date_histogram.ts +++ b/src/plugins/data/public/search/aggs/buckets/date_histogram.ts @@ -152,6 +152,14 @@ export const getDateHistogramBucketAgg = ({ (key: string) => uiSettings.get(key) ); }, + getSerializedFormat(agg) { + return { + id: 'date', + params: { + pattern: agg.buckets.getScaledDateFormat(), + }, + }; + }, params: [ { name: 'field', diff --git a/src/plugins/data/public/search/aggs/buckets/date_range.ts b/src/plugins/data/public/search/aggs/buckets/date_range.ts index 447347dbfbe109..3e14ab422ccbee 100644 --- a/src/plugins/data/public/search/aggs/buckets/date_range.ts +++ b/src/plugins/data/public/search/aggs/buckets/date_range.ts @@ -70,6 +70,12 @@ export const getDateRangeBucketAgg = ({ }); return new DateRangeFormat(); }, + getSerializedFormat(agg) { + return { + id: 'date_range', + params: agg.params.field ? agg.params.field.format.toJSON() : {}, + }; + }, makeLabel(aggConfig) { return aggConfig.getFieldDisplayName() + ' date ranges'; }, diff --git a/src/plugins/data/public/search/aggs/buckets/ip_range.ts b/src/plugins/data/public/search/aggs/buckets/ip_range.ts index 10fdb2d93b56ea..b3e90bdf9b56a1 100644 --- a/src/plugins/data/public/search/aggs/buckets/ip_range.ts +++ b/src/plugins/data/public/search/aggs/buckets/ip_range.ts @@ -78,6 +78,12 @@ export const getIpRangeBucketAgg = ({ getInternalStartServices }: IpRangeBucketA }); return new IpRangeFormat(); }, + getSerializedFormat(agg) { + return { + id: 'ip_range', + params: agg.params.field ? agg.params.field.format.toJSON() : {}, + }; + }, makeLabel(aggConfig) { return i18n.translate('data.search.aggs.buckets.ipRangeLabel', { defaultMessage: '{fieldName} IP ranges', diff --git a/src/plugins/data/public/search/aggs/buckets/lib/time_buckets/time_buckets.ts b/src/plugins/data/public/search/aggs/buckets/lib/time_buckets/time_buckets.ts index b8d6586652d6b1..12197c85f4a961 100644 --- a/src/plugins/data/public/search/aggs/buckets/lib/time_buckets/time_buckets.ts +++ b/src/plugins/data/public/search/aggs/buckets/lib/time_buckets/time_buckets.ts @@ -48,7 +48,7 @@ function isValidMoment(m: any): boolean { return m && 'isValid' in m && m.isValid(); } -export interface TimeBucketsConfig { +export interface TimeBucketsConfig extends Record { 'histogram:maxBars': number; 'histogram:barTarget': number; dateFormat: string; diff --git a/src/plugins/data/public/search/aggs/buckets/range.test.ts b/src/plugins/data/public/search/aggs/buckets/range.test.ts index 4c2d3af1ab7347..2b8a36f2fbdbcb 100644 --- a/src/plugins/data/public/search/aggs/buckets/range.test.ts +++ b/src/plugins/data/public/search/aggs/buckets/range.test.ts @@ -104,7 +104,7 @@ describe('Range Agg', () => { ); }; - describe('formating', () => { + describe('formatting', () => { test('formats bucket keys properly', () => { const aggConfigs = getAggConfigs(); const agg = aggConfigs.aggs[0]; @@ -115,4 +115,22 @@ describe('Range Agg', () => { expect(format(buckets[2])).toBe('≥ 2.5 KB and < +∞'); }); }); + + describe('getSerializedFormat', () => { + test('generates a serialized field format in the expected shape', () => { + const aggConfigs = getAggConfigs(); + const agg = aggConfigs.aggs[0]; + expect(agg.type.getSerializedFormat(agg)).toMatchInlineSnapshot(` + Object { + "id": "range", + "params": Object { + "id": "number", + "params": Object { + "pattern": "0,0.[000] b", + }, + }, + } + `); + }); + }); }); diff --git a/src/plugins/data/public/search/aggs/buckets/range.ts b/src/plugins/data/public/search/aggs/buckets/range.ts index 02aad3bd5fed12..543e5d66b9fa81 100644 --- a/src/plugins/data/public/search/aggs/buckets/range.ts +++ b/src/plugins/data/public/search/aggs/buckets/range.ts @@ -101,6 +101,16 @@ export const getRangeBucketAgg = ({ getInternalStartServices }: RangeBucketAggDe formats.set(agg, aggFormat); return aggFormat; }, + getSerializedFormat(agg) { + const format = agg.params.field ? agg.params.field.format.toJSON() : {}; + return { + id: 'range', + params: { + id: format.id, + params: format.params, + }, + }; + }, params: [ { name: 'field', diff --git a/src/plugins/data/public/search/aggs/buckets/terms.ts b/src/plugins/data/public/search/aggs/buckets/terms.ts index 45a76f08ddd13d..1e8e9ab4ef9d09 100644 --- a/src/plugins/data/public/search/aggs/buckets/terms.ts +++ b/src/plugins/data/public/search/aggs/buckets/terms.ts @@ -104,6 +104,18 @@ export const getTermsBucketAgg = ({ getInternalStartServices }: TermsBucketAggDe }, } as IFieldFormat; }, + getSerializedFormat(agg) { + const format = agg.params.field ? agg.params.field.format.toJSON() : {}; + return { + id: 'terms', + params: { + id: format.id, + otherBucketLabel: agg.params.otherBucketLabel, + missingBucketLabel: agg.params.missingBucketLabel, + ...format.params, + }, + }; + }, createFilter: createFilterTerms, postFlightRequest: async ( resp: any, diff --git a/src/plugins/data/public/search/aggs/metrics/bucket_avg.ts b/src/plugins/data/public/search/aggs/metrics/bucket_avg.ts index 927e9a7ae44586..38312ec5cfa81c 100644 --- a/src/plugins/data/public/search/aggs/metrics/bucket_avg.ts +++ b/src/plugins/data/public/search/aggs/metrics/bucket_avg.ts @@ -46,14 +46,17 @@ const averageBucketTitle = i18n.translate('data.search.aggs.metrics.averageBucke export const getBucketAvgMetricAgg = ({ getInternalStartServices, }: BucketAvgMetricAggDependencies) => { + const { subtype, params, getFormat, getSerializedFormat } = siblingPipelineAggHelper; + return new MetricAggType( { name: METRIC_TYPES.AVG_BUCKET, title: averageBucketTitle, makeLabel: (agg) => makeNestedLabel(agg, overallAverageLabel), - subtype: siblingPipelineAggHelper.subtype, - params: [...siblingPipelineAggHelper.params()], - getFormat: siblingPipelineAggHelper.getFormat, + subtype, + params: [...params()], + getFormat, + getSerializedFormat, getValue(agg, bucket) { const customMetric = agg.getParam('customMetric'); const customBucket = agg.getParam('customBucket'); diff --git a/src/plugins/data/public/search/aggs/metrics/bucket_max.ts b/src/plugins/data/public/search/aggs/metrics/bucket_max.ts index 2b171fcbd24fd2..e2c6a5105bac66 100644 --- a/src/plugins/data/public/search/aggs/metrics/bucket_max.ts +++ b/src/plugins/data/public/search/aggs/metrics/bucket_max.ts @@ -45,14 +45,17 @@ const maxBucketTitle = i18n.translate('data.search.aggs.metrics.maxBucketTitle', export const getBucketMaxMetricAgg = ({ getInternalStartServices, }: BucketMaxMetricAggDependencies) => { + const { subtype, params, getFormat, getSerializedFormat } = siblingPipelineAggHelper; + return new MetricAggType( { name: METRIC_TYPES.MAX_BUCKET, title: maxBucketTitle, makeLabel: (agg) => makeNestedLabel(agg, overallMaxLabel), - subtype: siblingPipelineAggHelper.subtype, - params: [...siblingPipelineAggHelper.params()], - getFormat: siblingPipelineAggHelper.getFormat, + subtype, + params: [...params()], + getFormat, + getSerializedFormat, }, { getInternalStartServices, diff --git a/src/plugins/data/public/search/aggs/metrics/bucket_min.ts b/src/plugins/data/public/search/aggs/metrics/bucket_min.ts index e6a523eeea374b..c46a3eb9425d1f 100644 --- a/src/plugins/data/public/search/aggs/metrics/bucket_min.ts +++ b/src/plugins/data/public/search/aggs/metrics/bucket_min.ts @@ -45,14 +45,17 @@ const minBucketTitle = i18n.translate('data.search.aggs.metrics.minBucketTitle', export const getBucketMinMetricAgg = ({ getInternalStartServices, }: BucketMinMetricAggDependencies) => { + const { subtype, params, getFormat, getSerializedFormat } = siblingPipelineAggHelper; + return new MetricAggType( { name: METRIC_TYPES.MIN_BUCKET, title: minBucketTitle, makeLabel: (agg) => makeNestedLabel(agg, overallMinLabel), - subtype: siblingPipelineAggHelper.subtype, - params: [...siblingPipelineAggHelper.params()], - getFormat: siblingPipelineAggHelper.getFormat, + subtype, + params: [...params()], + getFormat, + getSerializedFormat, }, { getInternalStartServices, diff --git a/src/plugins/data/public/search/aggs/metrics/bucket_sum.ts b/src/plugins/data/public/search/aggs/metrics/bucket_sum.ts index 71c88596ea5693..57212ec9ff91b2 100644 --- a/src/plugins/data/public/search/aggs/metrics/bucket_sum.ts +++ b/src/plugins/data/public/search/aggs/metrics/bucket_sum.ts @@ -45,14 +45,17 @@ const sumBucketTitle = i18n.translate('data.search.aggs.metrics.sumBucketTitle', export const getBucketSumMetricAgg = ({ getInternalStartServices, }: BucketSumMetricAggDependencies) => { + const { subtype, params, getFormat, getSerializedFormat } = siblingPipelineAggHelper; + return new MetricAggType( { name: METRIC_TYPES.SUM_BUCKET, title: sumBucketTitle, makeLabel: (agg) => makeNestedLabel(agg, overallSumLabel), - subtype: siblingPipelineAggHelper.subtype, - params: [...siblingPipelineAggHelper.params()], - getFormat: siblingPipelineAggHelper.getFormat, + subtype, + params: [...params()], + getFormat, + getSerializedFormat, }, { getInternalStartServices, diff --git a/src/plugins/data/public/search/aggs/metrics/cardinality.ts b/src/plugins/data/public/search/aggs/metrics/cardinality.ts index 9ff3e84c38cd8f..2855cc1b6b16eb 100644 --- a/src/plugins/data/public/search/aggs/metrics/cardinality.ts +++ b/src/plugins/data/public/search/aggs/metrics/cardinality.ts @@ -54,6 +54,11 @@ export const getCardinalityMetricAgg = ({ return fieldFormats.getDefaultInstance(KBN_FIELD_TYPES.NUMBER); }, + getSerializedFormat(agg) { + return { + id: 'number', + }; + }, params: [ { name: 'field', diff --git a/src/plugins/data/public/search/aggs/metrics/count.ts b/src/plugins/data/public/search/aggs/metrics/count.ts index bd0b83798c7db6..4c7b8139b01628 100644 --- a/src/plugins/data/public/search/aggs/metrics/count.ts +++ b/src/plugins/data/public/search/aggs/metrics/count.ts @@ -45,6 +45,11 @@ export const getCountMetricAgg = ({ getInternalStartServices }: CountMetricAggDe return fieldFormats.getDefaultInstance(KBN_FIELD_TYPES.NUMBER); }, + getSerializedFormat(agg) { + return { + id: 'number', + }; + }, getValue(agg, bucket) { return bucket.doc_count; }, diff --git a/src/plugins/data/public/search/aggs/metrics/cumulative_sum.ts b/src/plugins/data/public/search/aggs/metrics/cumulative_sum.ts index 44bfca1b6fb87c..c392f44a7961ed 100644 --- a/src/plugins/data/public/search/aggs/metrics/cumulative_sum.ts +++ b/src/plugins/data/public/search/aggs/metrics/cumulative_sum.ts @@ -46,14 +46,17 @@ const cumulativeSumTitle = i18n.translate('data.search.aggs.metrics.cumulativeSu export const getCumulativeSumMetricAgg = ({ getInternalStartServices, }: CumulativeSumMetricAggDependencies) => { + const { subtype, params, getFormat, getSerializedFormat } = parentPipelineAggHelper; + return new MetricAggType( { name: METRIC_TYPES.CUMULATIVE_SUM, title: cumulativeSumTitle, - subtype: parentPipelineAggHelper.subtype, makeLabel: (agg) => makeNestedLabel(agg, cumulativeSumLabel), - params: [...parentPipelineAggHelper.params()], - getFormat: parentPipelineAggHelper.getFormat, + subtype, + params: [...params()], + getFormat, + getSerializedFormat, }, { getInternalStartServices, diff --git a/src/plugins/data/public/search/aggs/metrics/derivative.ts b/src/plugins/data/public/search/aggs/metrics/derivative.ts index edb907ca4ed416..f3c1cc9bc29773 100644 --- a/src/plugins/data/public/search/aggs/metrics/derivative.ts +++ b/src/plugins/data/public/search/aggs/metrics/derivative.ts @@ -46,16 +46,19 @@ const derivativeTitle = i18n.translate('data.search.aggs.metrics.derivativeTitle export const getDerivativeMetricAgg = ({ getInternalStartServices, }: DerivativeMetricAggDependencies) => { + const { subtype, params, getFormat, getSerializedFormat } = parentPipelineAggHelper; + return new MetricAggType( { name: METRIC_TYPES.DERIVATIVE, title: derivativeTitle, - subtype: parentPipelineAggHelper.subtype, makeLabel(agg) { return makeNestedLabel(agg, derivativeLabel); }, - params: [...parentPipelineAggHelper.params()], - getFormat: parentPipelineAggHelper.getFormat, + subtype, + params: [...params()], + getFormat, + getSerializedFormat, }, { getInternalStartServices, diff --git a/src/plugins/data/public/search/aggs/metrics/lib/parent_pipeline_agg_helper.ts b/src/plugins/data/public/search/aggs/metrics/lib/parent_pipeline_agg_helper.ts index 947394c97bdcd2..2a74a446ce84e0 100644 --- a/src/plugins/data/public/search/aggs/metrics/lib/parent_pipeline_agg_helper.ts +++ b/src/plugins/data/public/search/aggs/metrics/lib/parent_pipeline_agg_helper.ts @@ -84,4 +84,16 @@ export const parentPipelineAggHelper = { } return subAgg ? subAgg.type.getFormat(subAgg) : new (FieldFormat.from(identity))(); }, + + getSerializedFormat(agg: IMetricAggConfig) { + let subAgg; + const customMetric = agg.getParam('customMetric'); + + if (customMetric) { + subAgg = customMetric; + } else { + subAgg = agg.aggConfigs.byId(agg.getParam('metricAgg')); + } + return subAgg ? subAgg.type.getSerializedFormat(subAgg) : {}; + }, }; diff --git a/src/plugins/data/public/search/aggs/metrics/lib/sibling_pipeline_agg_helper.ts b/src/plugins/data/public/search/aggs/metrics/lib/sibling_pipeline_agg_helper.ts index cee7841a8c3b98..8e3e0143bf9154 100644 --- a/src/plugins/data/public/search/aggs/metrics/lib/sibling_pipeline_agg_helper.ts +++ b/src/plugins/data/public/search/aggs/metrics/lib/sibling_pipeline_agg_helper.ts @@ -93,4 +93,9 @@ export const siblingPipelineAggHelper = { ? customMetric.type.getFormat(customMetric) : new (FieldFormat.from(identity))(); }, + + getSerializedFormat(agg: IMetricAggConfig) { + const customMetric = agg.getParam('customMetric'); + return customMetric ? customMetric.type.getSerializedFormat(customMetric) : {}; + }, }; diff --git a/src/plugins/data/public/search/aggs/metrics/moving_avg.ts b/src/plugins/data/public/search/aggs/metrics/moving_avg.ts index 1173ae5358ee70..abad2782f9d201 100644 --- a/src/plugins/data/public/search/aggs/metrics/moving_avg.ts +++ b/src/plugins/data/public/search/aggs/metrics/moving_avg.ts @@ -48,15 +48,19 @@ const movingAvgLabel = i18n.translate('data.search.aggs.metrics.movingAvgLabel', export const getMovingAvgMetricAgg = ({ getInternalStartServices, }: MovingAvgMetricAggDependencies) => { + const { subtype, params, getFormat, getSerializedFormat } = parentPipelineAggHelper; + return new MetricAggType( { name: METRIC_TYPES.MOVING_FN, dslName: 'moving_fn', title: movingAvgTitle, - subtype: parentPipelineAggHelper.subtype, makeLabel: (agg) => makeNestedLabel(agg, movingAvgLabel), + subtype, + getFormat, + getSerializedFormat, params: [ - ...parentPipelineAggHelper.params(), + ...params(), { name: 'window', default: 5, @@ -78,7 +82,6 @@ export const getMovingAvgMetricAgg = ({ */ return bucket[agg.id] ? bucket[agg.id].value : null; }, - getFormat: parentPipelineAggHelper.getFormat, }, { getInternalStartServices, diff --git a/src/plugins/data/public/search/aggs/metrics/percentile_ranks.ts b/src/plugins/data/public/search/aggs/metrics/percentile_ranks.ts index c8383f6bcc3d9c..c5aee380b97762 100644 --- a/src/plugins/data/public/search/aggs/metrics/percentile_ranks.ts +++ b/src/plugins/data/public/search/aggs/metrics/percentile_ranks.ts @@ -103,6 +103,11 @@ export const getPercentileRanksMetricAgg = ({ fieldFormats.getDefaultInstance(KBN_FIELD_TYPES.NUMBER) ); }, + getSerializedFormat(agg) { + return { + id: 'percent', + }; + }, getValue(agg, bucket) { return getPercentileValue(agg, bucket) / 100; }, diff --git a/src/plugins/data/public/search/aggs/metrics/serial_diff.ts b/src/plugins/data/public/search/aggs/metrics/serial_diff.ts index 00bc631cefab81..7cb1b194fed1c1 100644 --- a/src/plugins/data/public/search/aggs/metrics/serial_diff.ts +++ b/src/plugins/data/public/search/aggs/metrics/serial_diff.ts @@ -46,14 +46,17 @@ const serialDiffLabel = i18n.translate('data.search.aggs.metrics.serialDiffLabel export const getSerialDiffMetricAgg = ({ getInternalStartServices, }: SerialDiffMetricAggDependencies) => { + const { subtype, params, getFormat, getSerializedFormat } = parentPipelineAggHelper; + return new MetricAggType( { name: METRIC_TYPES.SERIAL_DIFF, title: serialDiffTitle, - subtype: parentPipelineAggHelper.subtype, makeLabel: (agg) => makeNestedLabel(agg, serialDiffLabel), - params: [...parentPipelineAggHelper.params()], - getFormat: parentPipelineAggHelper.getFormat, + subtype, + params: [...params()], + getFormat, + getSerializedFormat, }, { getInternalStartServices, diff --git a/src/plugins/data/public/search/aggs/test_helpers/mock_agg_types_registry.ts b/src/plugins/data/public/search/aggs/test_helpers/mock_agg_types_registry.ts index 5549ffd2583b18..836aaad2cda0c3 100644 --- a/src/plugins/data/public/search/aggs/test_helpers/mock_agg_types_registry.ts +++ b/src/plugins/data/public/search/aggs/test_helpers/mock_agg_types_registry.ts @@ -25,6 +25,25 @@ import { MetricAggType } from '../metrics/metric_agg_type'; import { queryServiceMock } from '../../../query/mocks'; import { fieldFormatsServiceMock } from '../../../field_formats/mocks'; import { InternalStartServices } from '../../../types'; +import { TimeBucketsConfig } from '../buckets/lib/time_buckets/time_buckets'; + +// Mocked uiSettings shared among aggs unit tests +const mockUiSettings = jest.fn().mockImplementation((key: string) => { + const config: TimeBucketsConfig = { + 'histogram:maxBars': 4, + 'histogram:barTarget': 3, + dateFormat: 'YYYY-MM-DD', + 'dateFormat:scaled': [ + ['', 'HH:mm:ss.SSS'], + ['PT1S', 'HH:mm:ss'], + ['PT1M', 'HH:mm'], + ['PT1H', 'YYYY-MM-DD HH:mm'], + ['P1DT', 'YYYY-MM-DD'], + ['P1YT', 'YYYY'], + ], + }; + return config[key] ?? key; +}); /** * Testing utility which creates a new instance of AggTypesRegistry, @@ -54,7 +73,10 @@ export function mockAggTypesRegistry | MetricAggTyp }); } else { const coreSetup = coreMock.createSetup(); + coreSetup.uiSettings.get = mockUiSettings; + const coreStart = coreMock.createStart(); + coreSetup.uiSettings.get = mockUiSettings; const aggTypes = getAggTypes({ uiSettings: coreSetup.uiSettings, diff --git a/src/plugins/data/public/search/aggs/utils/get_format_with_aggs.test.ts b/src/plugins/data/public/search/aggs/utils/get_format_with_aggs.test.ts new file mode 100644 index 00000000000000..3b440bc50c93b8 --- /dev/null +++ b/src/plugins/data/public/search/aggs/utils/get_format_with_aggs.test.ts @@ -0,0 +1,99 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { identity } from 'lodash'; + +import { SerializedFieldFormat } from '../../../../../expressions/common/types'; +import { FieldFormat } from '../../../../common'; +import { IFieldFormat } from '../../../../public'; + +import { getFormatWithAggs } from './get_format_with_aggs'; + +describe('getFormatWithAggs', () => { + let getFormat: jest.MockedFunction<(mapping: SerializedFieldFormat) => IFieldFormat>; + + beforeEach(() => { + getFormat = jest.fn().mockImplementation(() => { + const DefaultFieldFormat = FieldFormat.from(identity); + return new DefaultFieldFormat(); + }); + }); + + test('calls provided getFormat if no matching aggs exist', () => { + const mapping = { id: 'foo', params: {} }; + const getFieldFormat = getFormatWithAggs(getFormat); + getFieldFormat(mapping); + + expect(getFormat).toHaveBeenCalledTimes(1); + expect(getFormat).toHaveBeenCalledWith(mapping); + }); + + test('creates custom format for date_range', () => { + const mapping = { id: 'date_range', params: {} }; + const getFieldFormat = getFormatWithAggs(getFormat); + const format = getFieldFormat(mapping); + + expect(format.convert({ from: '2020-05-01', to: '2020-06-01' })).toBe( + '2020-05-01 to 2020-06-01' + ); + expect(format.convert({ to: '2020-06-01' })).toBe('Before 2020-06-01'); + expect(format.convert({ from: '2020-06-01' })).toBe('After 2020-06-01'); + expect(getFormat).toHaveBeenCalledTimes(3); + }); + + test('creates custom format for ip_range', () => { + const mapping = { id: 'ip_range', params: {} }; + const getFieldFormat = getFormatWithAggs(getFormat); + const format = getFieldFormat(mapping); + + expect(format.convert({ type: 'range', from: '10.0.0.1', to: '10.0.0.10' })).toBe( + '10.0.0.1 to 10.0.0.10' + ); + expect(format.convert({ type: 'range', to: '10.0.0.10' })).toBe('-Infinity to 10.0.0.10'); + expect(format.convert({ type: 'range', from: '10.0.0.10' })).toBe('10.0.0.10 to Infinity'); + format.convert({ type: 'mask', mask: '10.0.0.1/24' }); + expect(getFormat).toHaveBeenCalledTimes(4); + }); + + test('creates custom format for range', () => { + const mapping = { id: 'range', params: {} }; + const getFieldFormat = getFormatWithAggs(getFormat); + const format = getFieldFormat(mapping); + + expect(format.convert({ gte: 1, lt: 20 })).toBe('≥ 1 and < 20'); + expect(getFormat).toHaveBeenCalledTimes(1); + }); + + test('creates custom format for terms', () => { + const mapping = { + id: 'terms', + params: { + otherBucketLabel: 'other bucket', + missingBucketLabel: 'missing bucket', + }, + }; + const getFieldFormat = getFormatWithAggs(getFormat); + const format = getFieldFormat(mapping); + + expect(format.convert('machine.os.keyword')).toBe('machine.os.keyword'); + expect(format.convert('__other__')).toBe(mapping.params.otherBucketLabel); + expect(format.convert('__missing__')).toBe(mapping.params.missingBucketLabel); + expect(getFormat).toHaveBeenCalledTimes(3); + }); +}); diff --git a/src/plugins/data/public/search/aggs/utils/get_format_with_aggs.ts b/src/plugins/data/public/search/aggs/utils/get_format_with_aggs.ts new file mode 100644 index 00000000000000..e0db249c7cf865 --- /dev/null +++ b/src/plugins/data/public/search/aggs/utils/get_format_with_aggs.ts @@ -0,0 +1,116 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { i18n } from '@kbn/i18n'; + +import { SerializedFieldFormat } from '../../../../../expressions/common/types'; +import { FieldFormat } from '../../../../common'; +import { FieldFormatsContentType, IFieldFormat } from '../../../../public'; +import { convertDateRangeToString, DateRangeKey } from '../buckets/lib/date_range'; +import { convertIPRangeToString, IpRangeKey } from '../buckets/lib/ip_range'; + +type GetFieldFormat = (mapping: SerializedFieldFormat) => IFieldFormat; + +/** + * Certain aggs have custom field formats that are not part of the field formats + * registry. This function will take the `getFormat` function which is used inside + * `deserializeFieldFormat` and decorate it with the additional custom formats + * that the field formats service doesn't know anything about. + * + * This function is internal to the data plugin, and only exists for use inside + * the field formats service. + * + * @internal + */ +export function getFormatWithAggs(getFieldFormat: GetFieldFormat): GetFieldFormat { + return (mapping) => { + const { id, params = {} } = mapping; + + const customFormats: Record IFieldFormat> = { + range: () => { + const RangeFormat = FieldFormat.from((range: any) => { + const nestedFormatter = params as SerializedFieldFormat; + const format = getFieldFormat({ + id: nestedFormatter.id, + params: nestedFormatter.params, + }); + const gte = '\u2265'; + const lt = '\u003c'; + return i18n.translate('data.aggTypes.buckets.ranges.rangesFormatMessage', { + defaultMessage: '{gte} {from} and {lt} {to}', + values: { + gte, + from: format.convert(range.gte), + lt, + to: format.convert(range.lt), + }, + }); + }); + return new RangeFormat(); + }, + date_range: () => { + const nestedFormatter = params as SerializedFieldFormat; + const DateRangeFormat = FieldFormat.from((range: DateRangeKey) => { + const format = getFieldFormat({ + id: nestedFormatter.id, + params: nestedFormatter.params, + }); + return convertDateRangeToString(range, format.convert.bind(format)); + }); + return new DateRangeFormat(); + }, + ip_range: () => { + const nestedFormatter = params as SerializedFieldFormat; + const IpRangeFormat = FieldFormat.from((range: IpRangeKey) => { + const format = getFieldFormat({ + id: nestedFormatter.id, + params: nestedFormatter.params, + }); + return convertIPRangeToString(range, format.convert.bind(format)); + }); + return new IpRangeFormat(); + }, + terms: () => { + const convert = (val: string, type: FieldFormatsContentType) => { + const format = getFieldFormat({ id: params.id, params }); + + if (val === '__other__') { + return params.otherBucketLabel; + } + if (val === '__missing__') { + return params.missingBucketLabel; + } + + return format.convert(val, type); + }; + + return { + convert, + getConverterFor: (type: FieldFormatsContentType) => (val: string) => convert(val, type), + } as IFieldFormat; + }, + }; + + if (!id || !(id in customFormats)) { + return getFieldFormat(mapping); + } + + return customFormats[id](); + }; +} diff --git a/src/plugins/data/public/search/aggs/utils/index.ts b/src/plugins/data/public/search/aggs/utils/index.ts index 169d872b17d3aa..5a889ee9ead9d9 100644 --- a/src/plugins/data/public/search/aggs/utils/index.ts +++ b/src/plugins/data/public/search/aggs/utils/index.ts @@ -18,5 +18,6 @@ */ export * from './calculate_auto_time_expression'; +export * from './get_format_with_aggs'; export * from './prop_filter'; export * from './to_angular_json'; diff --git a/src/plugins/data/public/search/expressions/esaggs.ts b/src/plugins/data/public/search/expressions/esaggs.ts index 153eb7de6f2de4..77475cd3ce88b9 100644 --- a/src/plugins/data/public/search/expressions/esaggs.ts +++ b/src/plugins/data/public/search/expressions/esaggs.ts @@ -32,14 +32,7 @@ import { Adapters } from '../../../../../plugins/inspector/public'; import { IAggConfigs } from '../aggs'; import { ISearchSource } from '../search_source'; import { tabifyAggResponse } from '../tabify'; -import { - Filter, - Query, - serializeFieldFormat, - TimeRange, - IIndexPattern, - isRangeFilter, -} from '../../../common'; +import { Filter, Query, TimeRange, IIndexPattern, isRangeFilter } from '../../../common'; import { FilterManager, calculateBounds, getTime } from '../../query'; import { getSearchService, getQueryService, getIndexPatterns } from '../../services'; import { buildTabularInspectorData } from './build_tabular_inspector_data'; @@ -313,7 +306,7 @@ export const esaggs = (): ExpressionFunctionDefinition import("../../expressions").SerializedFieldFormat; BoolFormat: typeof BoolFormat; BytesFormat: typeof BytesFormat; ColorFormat: typeof ColorFormat; @@ -631,7 +630,7 @@ export class Plugin implements Plugin_2 { setup(core: CoreSetup, { usageCollection }: DataPluginSetupDependencies): { search: ISearchSetup; fieldFormats: { - register: (customFieldFormat: import("../public").FieldFormatInstanceType) => number; + register: (customFieldFormat: import("../common").FieldFormatInstanceType) => number; }; }; // Warning: (ae-forgotten-export) The symbol "CoreStart" needs to be exported by the entry point index.d.ts @@ -774,30 +773,30 @@ export const UI_SETTINGS: { // src/plugins/data/server/index.ts:40:23 - (ae-forgotten-export) The symbol "buildFilter" needs to be exported by the entry point index.d.ts // src/plugins/data/server/index.ts:71:21 - (ae-forgotten-export) The symbol "getEsQueryConfig" needs to be exported by the entry point index.d.ts // src/plugins/data/server/index.ts:71:21 - (ae-forgotten-export) The symbol "buildEsQuery" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "FieldFormatsRegistry" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "FieldFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "BoolFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "BytesFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "ColorFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "DateNanosFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "DurationFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "IpFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "NumberFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "PercentFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "RelativeDateFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "SourceFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "StaticLookupFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "UrlFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "StringFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:103:26 - (ae-forgotten-export) The symbol "TruncateFormat" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:131:27 - (ae-forgotten-export) The symbol "isFilterable" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:131:27 - (ae-forgotten-export) The symbol "isNestedField" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:186:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:187:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:188:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:189:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:190:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:193:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "FieldFormatsRegistry" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "FieldFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "BoolFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "BytesFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "ColorFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "DateNanosFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "DurationFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "IpFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "NumberFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "PercentFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "RelativeDateFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "SourceFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "StaticLookupFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "UrlFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "StringFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:102:26 - (ae-forgotten-export) The symbol "TruncateFormat" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:129:27 - (ae-forgotten-export) The symbol "isFilterable" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:129:27 - (ae-forgotten-export) The symbol "isNestedField" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:184:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:185:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:186:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:187:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:188:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:191:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts // (No @packageDocumentation comment for this package) diff --git a/src/plugins/discover/public/application/_hacks.scss b/src/plugins/discover/public/application/_hacks.scss index baf27bb9f82da1..9bbe9cd14fd913 100644 --- a/src/plugins/discover/public/application/_hacks.scss +++ b/src/plugins/discover/public/application/_hacks.scss @@ -2,5 +2,3 @@ .tab-discover { overflow: hidden; } - - diff --git a/src/plugins/discover/public/application/angular/discover.js b/src/plugins/discover/public/application/angular/discover.js index 8ff5af1e3a7672..65868b0b7cd46a 100644 --- a/src/plugins/discover/public/application/angular/discover.js +++ b/src/plugins/discover/public/application/angular/discover.js @@ -66,7 +66,6 @@ const { import { getRootBreadcrumbs, getSavedSearchBreadcrumbs } from '../helpers/breadcrumbs'; import { esFilters, - fieldFormats, indexPatterns as indexPatternsUtils, connectToQueryState, syncQueryStateWithUrl, @@ -114,7 +113,7 @@ app.config(($routeProvider) => { }; }, }; - $routeProvider.when('/:id?', { + const discoverRoute = { ...defaults, template: indexTemplate, reloadOnSearch: false, @@ -177,7 +176,10 @@ app.config(($routeProvider) => { }); }, }, - }); + }; + + $routeProvider.when('/view/:id?', discoverRoute); + $routeProvider.when('/', discoverRoute); }); app.directive('discoverApp', function () { @@ -415,7 +417,7 @@ function discoverController( testId: 'discoverOpenButton', run: () => { showOpenSearchPanel({ - makeUrl: (searchId) => `#/${encodeURIComponent(searchId)}`, + makeUrl: (searchId) => `#/view/${encodeURIComponent(searchId)}`, I18nContext: core.i18n.Context, }); }, @@ -747,7 +749,7 @@ function discoverController( }); if (savedSearch.id !== $route.current.params.id) { - history.push(`/${encodeURIComponent(savedSearch.id)}`); + history.push(`/view/${encodeURIComponent(savedSearch.id)}`); } else { // Update defaults so that "reload saved query" functions correctly setAppState(getStateDefaults()); @@ -848,7 +850,7 @@ function discoverController( x: { accessor: 0, label: agg.makeLabel(), - format: fieldFormats.serialize(agg), + format: agg.toSerializedFieldFormat(), params: { date: true, interval: moment.duration(esValue, esUnit), @@ -860,7 +862,7 @@ function discoverController( }, y: { accessor: 1, - format: fieldFormats.serialize(metric), + format: metric.toSerializedFieldFormat(), label: metric.makeLabel(), }, }; @@ -926,7 +928,9 @@ function discoverController( }; $scope.resetQuery = function () { - history.push(`/${encodeURIComponent($route.current.params.id)}`); + history.push( + $route.current.params.id ? `/view/${encodeURIComponent($route.current.params.id)}` : '/' + ); $route.reload(); }; diff --git a/src/plugins/discover/public/application/angular/doc_table/components/table_row.ts b/src/plugins/discover/public/application/angular/doc_table/components/table_row.ts index b1e6d27d766563..2fb6fb1e3a3079 100644 --- a/src/plugins/discover/public/application/angular/doc_table/components/table_row.ts +++ b/src/plugins/discover/public/application/angular/doc_table/components/table_row.ts @@ -144,8 +144,7 @@ export function createTableRowDirective($compile: ng.ICompileService, $httpParam cellTemplate({ timefield: true, formatted: _displayField(row, indexPattern.timeFieldName), - filterable: - mapping(indexPattern.timeFieldName).filterable && _.isFunction($scope.filter), + filterable: mapping(indexPattern.timeFieldName).filterable && $scope.filter, column: indexPattern.timeFieldName, }) ); @@ -156,7 +155,7 @@ export function createTableRowDirective($compile: ng.ICompileService, $httpParam $scope.flattenedRow[column] !== undefined && mapping(column) && mapping(column).filterable && - _.isFunction($scope.filter); + $scope.filter; newHtmls.push( cellTemplate({ diff --git a/src/plugins/discover/public/application/angular/index.ts b/src/plugins/discover/public/application/angular/index.ts index f3fd3fb6622ee8..20a22d4ae634d7 100644 --- a/src/plugins/discover/public/application/angular/index.ts +++ b/src/plugins/discover/public/application/angular/index.ts @@ -25,4 +25,5 @@ import './discover'; import './doc'; import './context'; import './doc_viewer'; +import './redirect'; import './directives'; diff --git a/src/legacy/core_plugins/kibana/server/routes/api/import/index.js b/src/plugins/discover/public/application/angular/redirect.ts similarity index 55% rename from src/legacy/core_plugins/kibana/server/routes/api/import/index.js rename to src/plugins/discover/public/application/angular/redirect.ts index b7efb7da3c5a92..bfa2f07f852e93 100644 --- a/src/legacy/core_plugins/kibana/server/routes/api/import/index.js +++ b/src/plugins/discover/public/application/angular/redirect.ts @@ -16,30 +16,22 @@ * specific language governing permissions and limitations * under the License. */ +import { getAngularModule, getServices, getUrlTracker } from '../../kibana_services'; -import Joi from 'joi'; -import { importDashboards } from '../../../lib/import/import_dashboards'; - -export function importApi(server) { - server.route({ - path: '/api/kibana/dashboards/import', - method: ['POST'], - config: { - validate: { - payload: Joi.object().keys({ - objects: Joi.array(), - version: Joi.string(), - }), - query: Joi.object().keys({ - force: Joi.boolean().default(false), - exclude: [Joi.string(), Joi.array().items(Joi.string())], - }), - }, - tags: ['api'], - }, - - handler: async (req) => { - return await importDashboards(req); +getAngularModule().config(($routeProvider: any) => { + $routeProvider.otherwise({ + resolveRedirectTo: ($rootScope: any) => { + const path = window.location.hash.substr(1); + getUrlTracker().restorePreviousUrl(); + $rootScope.$applyAsync(() => { + const { kibanaLegacy } = getServices(); + const { navigated } = kibanaLegacy.navigateToLegacyKibanaUrl(path); + if (!navigated) { + kibanaLegacy.navigateToDefaultApp(); + } + }); + // prevent angular from completing the navigation + return new Promise(() => {}); }, }); -} +}); diff --git a/src/plugins/discover/public/application/application.ts b/src/plugins/discover/public/application/application.ts index 8167d4f9031956..b49cefd2fcb16f 100644 --- a/src/plugins/discover/public/application/application.ts +++ b/src/plugins/discover/public/application/application.ts @@ -19,11 +19,14 @@ import './index.scss'; import angular from 'angular'; +import { getServices } from '../kibana_services'; /** * Here's where Discover's inner angular is mounted and rendered */ export async function renderApp(moduleName: string, element: HTMLElement) { + // do not wait for fontawesome + getServices().kibanaLegacy.loadFontAwesome(); await import('./angular'); const $injector = mountDiscoverApp(moduleName, element); return () => $injector.get('$rootScope').$destroy(); diff --git a/src/plugins/discover/public/build_services.ts b/src/plugins/discover/public/build_services.ts index 6d3e0b55140ba0..75c83e30d80ad3 100644 --- a/src/plugins/discover/public/build_services.ts +++ b/src/plugins/discover/public/build_services.ts @@ -42,6 +42,7 @@ import { SavedObjectKibanaServices } from 'src/plugins/saved_objects/public'; import { DiscoverStartPlugins } from './plugin'; import { createSavedSearchesLoader, SavedSearch } from './saved_searches'; import { getHistory } from './kibana_services'; +import { KibanaLegacyStart } from '../../kibana_legacy/public'; export interface DiscoverServices { addBasePath: (path: string) => string; @@ -57,6 +58,7 @@ export interface DiscoverServices { inspector: InspectorPublicPluginStart; metadata: { branch: string }; share?: SharePluginStart; + kibanaLegacy: KibanaLegacyStart; timefilter: TimefilterContract; toastNotifications: ToastsStart; getSavedSearchById: (id: string) => Promise; @@ -97,6 +99,7 @@ export async function buildServices( branch: context.env.packageInfo.branch, }, share: plugins.share, + kibanaLegacy: plugins.kibanaLegacy, timefilter: plugins.data.query.timefilter.timefilter, toastNotifications: core.notifications.toasts, uiSettings: core.uiSettings, diff --git a/src/plugins/discover/public/kibana_services.ts b/src/plugins/discover/public/kibana_services.ts index bbd0357f41ed4e..cca63cd880b600 100644 --- a/src/plugins/discover/public/kibana_services.ts +++ b/src/plugins/discover/public/kibana_services.ts @@ -53,6 +53,7 @@ export function setServices(newServices: any) { export const [getUrlTracker, setUrlTracker] = createGetterSetter<{ setTrackedUrl: (url: string) => void; + restorePreviousUrl: () => void; }>('urlTracker'); export const [getDocViewsRegistry, setDocViewsRegistry] = createGetterSetter( diff --git a/src/plugins/discover/public/plugin.ts b/src/plugins/discover/public/plugin.ts index 091288e3e65aa4..ba97efa55068d7 100644 --- a/src/plugins/discover/public/plugin.ts +++ b/src/plugins/discover/public/plugin.ts @@ -36,7 +36,7 @@ import { ChartsPluginStart } from 'src/plugins/charts/public'; import { NavigationPublicPluginStart as NavigationStart } from 'src/plugins/navigation/public'; import { SharePluginStart, SharePluginSetup, UrlGeneratorContract } from 'src/plugins/share/public'; import { VisualizationsStart, VisualizationsSetup } from 'src/plugins/visualizations/public'; -import { KibanaLegacySetup } from 'src/plugins/kibana_legacy/public'; +import { KibanaLegacySetup, KibanaLegacyStart } from 'src/plugins/kibana_legacy/public'; import { HomePublicPluginSetup } from 'src/plugins/home/public'; import { Start as InspectorPublicPluginStart } from 'src/plugins/inspector/public'; import { DataPublicPluginStart, DataPublicPluginSetup, esFilters } from '../../data/public'; @@ -55,6 +55,7 @@ import { setServices, setScopedHistory, getScopedHistory, + getServices, } from './kibana_services'; import { createSavedSearchesLoader } from './saved_searches'; import { registerFeature } from './register_feature'; @@ -130,6 +131,7 @@ export interface DiscoverStartPlugins { charts: ChartsPluginStart; data: DataPublicPluginStart; share?: SharePluginStart; + kibanaLegacy: KibanaLegacyStart; inspector: InspectorPublicPluginStart; visualizations: VisualizationsStart; } @@ -195,6 +197,7 @@ export class DiscoverPlugin appUnMounted, stop: stopUrlTracker, setActiveUrl: setTrackedUrl, + restorePreviousUrl, } = createKbnUrlTracker({ // we pass getter here instead of plain `history`, // so history is lazily created (when app is mounted) @@ -220,7 +223,7 @@ export class DiscoverPlugin }, ], }); - setUrlTracker({ setTrackedUrl }); + setUrlTracker({ setTrackedUrl, restorePreviousUrl }); this.stopUrlTracking = () => { stopUrlTracker(); }; @@ -260,7 +263,19 @@ export class DiscoverPlugin }, }); - plugins.kibanaLegacy.forwardApp('discover', 'discover'); + plugins.kibanaLegacy.forwardApp('doc', 'discover', (path) => { + return `#${path}`; + }); + plugins.kibanaLegacy.forwardApp('context', 'discover', (path) => { + return `#${path}`; + }); + plugins.kibanaLegacy.forwardApp('discover', 'discover', (path) => { + const [, id, tail] = /discover\/([^\?]+)(.*)/.exec(path) || []; + if (!id) { + return `#${path.replace('/discover', '') || '/'}`; + } + return `#/view/${id}${tail || ''}`; + }); if (plugins.home) { registerFeature(plugins.home); @@ -356,6 +371,7 @@ export class DiscoverPlugin throw Error('Discover plugin getEmbeddableInjector: initializeServices is undefined'); } const { core, plugins } = await this.initializeServices(); + getServices().kibanaLegacy.loadFontAwesome(); const { getInnerAngularModuleEmbeddable } = await import('./get_inner_angular'); getInnerAngularModuleEmbeddable( embeddableAngularName, diff --git a/src/plugins/discover/public/saved_searches/_saved_search.ts b/src/plugins/discover/public/saved_searches/_saved_search.ts index 9eda4f6ce9d167..2b8574a8fa1183 100644 --- a/src/plugins/discover/public/saved_searches/_saved_search.ts +++ b/src/plugins/discover/public/saved_searches/_saved_search.ts @@ -66,7 +66,7 @@ export function createSavedSearchClass(services: SavedObjectKibanaServices) { }); this.showInRecentlyAccessed = true; this.id = id; - this.getFullPath = () => `/app/discover#/${String(id)}`; + this.getFullPath = () => `/app/discover#/view/${String(id)}`; } } diff --git a/src/plugins/discover/public/saved_searches/saved_searches.ts b/src/plugins/discover/public/saved_searches/saved_searches.ts index 1d1d4f17742a2e..09be10b1374943 100644 --- a/src/plugins/discover/public/saved_searches/saved_searches.ts +++ b/src/plugins/discover/public/saved_searches/saved_searches.ts @@ -34,7 +34,7 @@ export function createSavedSearchesLoader(services: SavedObjectKibanaServices) { nouns: 'saved searches', }; - savedSearchLoader.urlFor = (id: string) => `#/${encodeURIComponent(id)}`; + savedSearchLoader.urlFor = (id: string) => (id ? `#/view/${encodeURIComponent(id)}` : '#/'); return savedSearchLoader; } diff --git a/src/plugins/expressions/common/types/common.ts b/src/plugins/expressions/common/types/common.ts index f532f9708940ec..040979e4264b54 100644 --- a/src/plugins/expressions/common/types/common.ts +++ b/src/plugins/expressions/common/types/common.ts @@ -61,7 +61,7 @@ export type UnmappedTypeStrings = 'date' | 'filter'; * Is used to carry information about how to format data in * a data table as part of the column definition. */ -export interface SerializedFieldFormat { +export interface SerializedFieldFormat> { id?: string; params?: TParams; } diff --git a/src/plugins/kibana_legacy/public/font_awesome/font_awesome.scss b/src/plugins/kibana_legacy/public/font_awesome/font_awesome.scss new file mode 100644 index 00000000000000..876a920269c499 --- /dev/null +++ b/src/plugins/kibana_legacy/public/font_awesome/font_awesome.scss @@ -0,0 +1,23 @@ +@font-face { + font-family: 'FontAwesome'; + src: url('~font-awesome/fonts/fontawesome-webfont.eot?v=4.7.0'); + src: url('~font-awesome/fonts/fontawesome-webfont.eot?#iefix&v=4.7.0') format('embedded-opentype'), + url('~font-awesome/fonts/fontawesome-webfont.woff2?v=4.7.0') format('woff2'), + url('~font-awesome/fonts/fontawesome-webfont.woff?v=4.7.0') format('woff'), + url('~font-awesome/fonts/fontawesome-webfont.ttf?v=4.7.0') format('truetype'), + url('~font-awesome/fonts/fontawesome-webfont.svg?v=4.7.0#fontawesomeregular') format('svg'); + font-weight: normal; + font-style: normal; +} + +@import "font-awesome/scss/variables"; +@import "font-awesome/scss/core"; +@import "font-awesome/scss/icons"; + +// new file icon +.#{$fa-css-prefix}-file-new-o:before { content: $fa-var-file-o; } +.#{$fa-css-prefix}-file-new-o:after { content: $fa-var-plus; position: relative; margin-left: -1.0em; font-size: 0.5em; } + +// alias for alert types - allows class="fa fa-{{alertType}}" +.fa-success:before { content: $fa-var-check; } +.fa-danger:before { content: $fa-var-exclamation-circle; } diff --git a/src/plugins/advanced_settings/public/_index.scss b/src/plugins/kibana_legacy/public/font_awesome/index.ts similarity index 95% rename from src/plugins/advanced_settings/public/_index.scss rename to src/plugins/kibana_legacy/public/font_awesome/index.ts index d13c37bff32d00..318d44a3abfefe 100644 --- a/src/plugins/advanced_settings/public/_index.scss +++ b/src/plugins/kibana_legacy/public/font_awesome/index.ts @@ -17,4 +17,4 @@ * under the License. */ -@import './management_app/index'; +import './font_awesome.scss'; diff --git a/src/plugins/kibana_legacy/public/forward_app/forward_app.ts b/src/plugins/kibana_legacy/public/forward_app/forward_app.ts index a5bccfc7d725de..89018df1ca7e17 100644 --- a/src/plugins/kibana_legacy/public/forward_app/forward_app.ts +++ b/src/plugins/kibana_legacy/public/forward_app/forward_app.ts @@ -22,10 +22,8 @@ import { AppNavLinkStatus } from '../../../../core/public'; import { navigateToLegacyKibanaUrl } from './navigate_to_legacy_kibana_url'; import { ForwardDefinition } from '../plugin'; -export const createLegacyUrlForwardApp = ( - core: CoreSetup<{}, { getForwards: () => ForwardDefinition[] }> -): App => ({ - id: 'url_migrate', +export const createLegacyUrlForwardApp = (core: CoreSetup, forwards: ForwardDefinition[]): App => ({ + id: 'kibana', chromeless: true, title: 'Legacy URL migration', navLinkStatus: AppNavLinkStatus.hidden, @@ -33,7 +31,7 @@ export const createLegacyUrlForwardApp = ( const hash = params.history.location.hash.substr(1); if (!hash) { - throw new Error('Could not forward URL'); + core.fatalErrors.add('Could not forward URL'); } const [ @@ -41,11 +39,13 @@ export const createLegacyUrlForwardApp = ( application, http: { basePath }, }, - , - { getForwards }, ] = await core.getStartServices(); - navigateToLegacyKibanaUrl(hash, getForwards(), basePath, application, window.location); + const result = await navigateToLegacyKibanaUrl(hash, forwards, basePath, application); + + if (!result.navigated) { + core.fatalErrors.add('Could not forward URL'); + } return () => {}; }, diff --git a/src/plugins/kibana_legacy/public/forward_app/navigate_to_legacy_kibana_url.test.ts b/src/plugins/kibana_legacy/public/forward_app/navigate_to_legacy_kibana_url.test.ts index de8fa9de7241eb..30583aa95fc8c6 100644 --- a/src/plugins/kibana_legacy/public/forward_app/navigate_to_legacy_kibana_url.test.ts +++ b/src/plugins/kibana_legacy/public/forward_app/navigate_to_legacy_kibana_url.test.ts @@ -25,7 +25,6 @@ import { coreMock } from '../../../../core/public/mocks'; describe('migrate legacy kibana urls', () => { let forwardDefinitions: ForwardDefinition[]; let coreStart: CoreStart; - let locationMock: Location; beforeEach(() => { coreStart = coreMock.createStart({ basePath: '/base/path' }); @@ -36,34 +35,33 @@ describe('migrate legacy kibana urls', () => { rewritePath: jest.fn(() => '/new/path'), }, ]; - locationMock = { href: '' } as Location; }); - it('should redirect to kibana if no forward definition is found', () => { - navigateToLegacyKibanaUrl( + it('should do nothing if no forward definition is found', () => { + const result = navigateToLegacyKibanaUrl( '/myOtherApp/deep/path', forwardDefinitions, coreStart.http.basePath, - coreStart.application, - locationMock + coreStart.application ); - expect(locationMock.href).toEqual('/base/path/app/kibana#/myOtherApp/deep/path'); + expect(result).toEqual({ navigated: false }); + expect(coreStart.application.navigateToApp).not.toHaveBeenCalled(); }); it('should call navigateToApp with migrated URL', () => { - navigateToLegacyKibanaUrl( + const result = navigateToLegacyKibanaUrl( '/myApp/deep/path', forwardDefinitions, coreStart.http.basePath, - coreStart.application, - locationMock + coreStart.application ); expect(coreStart.application.navigateToApp).toHaveBeenCalledWith('updatedApp', { path: '/new/path', + replace: true, }); expect(forwardDefinitions[0].rewritePath).toHaveBeenCalledWith('/myApp/deep/path'); - expect(locationMock.href).toEqual(''); + expect(result).toEqual({ navigated: true }); }); }); diff --git a/src/plugins/kibana_legacy/public/forward_app/navigate_to_legacy_kibana_url.ts b/src/plugins/kibana_legacy/public/forward_app/navigate_to_legacy_kibana_url.ts index a6aee351fde529..1df991f66747ce 100644 --- a/src/plugins/kibana_legacy/public/forward_app/navigate_to_legacy_kibana_url.ts +++ b/src/plugins/kibana_legacy/public/forward_app/navigate_to_legacy_kibana_url.ts @@ -19,30 +19,26 @@ import { ApplicationStart, IBasePath } from 'kibana/public'; import { ForwardDefinition } from '../index'; +import { normalizePath } from '../utils/normalize_path'; export const navigateToLegacyKibanaUrl = ( path: string, forwards: ForwardDefinition[], basePath: IBasePath, - application: ApplicationStart, - location: Location -) => { - // navigate to the respective path in the legacy kibana plugin by default (for unmigrated plugins) - let targetAppId = 'kibana'; - let targetAppPath = path; + application: ApplicationStart +): { navigated: boolean } => { + const normalizedPath = normalizePath(path); // try to find an existing redirect for the target path if possible // this avoids having to load the legacy app just to get redirected to a core application again afterwards - const relevantForward = forwards.find((forward) => path.startsWith(`/${forward.legacyAppId}`)); - if (relevantForward) { - targetAppPath = relevantForward.rewritePath(path); - targetAppId = relevantForward.newAppId; - } - - if (targetAppId === 'kibana') { - // exception for kibana app because redirect won't work right otherwise - location.href = basePath.prepend(`/app/kibana#${targetAppPath}`); - } else { - application.navigateToApp(targetAppId, { path: targetAppPath }); + const relevantForward = forwards.find((forward) => + normalizedPath.startsWith(`/${forward.legacyAppId}`) + ); + if (!relevantForward) { + return { navigated: false }; } + const targetAppPath = relevantForward.rewritePath(normalizedPath); + const targetAppId = relevantForward.newAppId; + application.navigateToApp(targetAppId, { path: targetAppPath, replace: true }); + return { navigated: true }; }; diff --git a/src/plugins/kibana_legacy/public/mocks.ts b/src/plugins/kibana_legacy/public/mocks.ts index 5bdc76d44e4bf6..a3cdb2106523c7 100644 --- a/src/plugins/kibana_legacy/public/mocks.ts +++ b/src/plugins/kibana_legacy/public/mocks.ts @@ -17,7 +17,6 @@ * under the License. */ -import { EnvironmentMode, PackageInfo } from 'kibana/server'; import { KibanaLegacyPlugin } from './plugin'; export type Setup = jest.Mocked>; @@ -25,20 +24,9 @@ export type Start = jest.Mocked>; const createSetupContract = (): Setup => ({ forwardApp: jest.fn(), - registerLegacyAppAlias: jest.fn(), - registerLegacyApp: jest.fn(), - config: { - defaultAppId: 'home', - }, - env: {} as { - mode: Readonly; - packageInfo: Readonly; - }, }); const createStartContract = (): Start => ({ - getApps: jest.fn(), - getLegacyAppAliases: jest.fn(), getForwards: jest.fn(), config: { defaultAppId: 'home', @@ -48,6 +36,8 @@ const createStartContract = (): Start => ({ getHideWriteControls: jest.fn(), }, navigateToDefaultApp: jest.fn(), + navigateToLegacyKibanaUrl: jest.fn(), + loadFontAwesome: jest.fn(), }); export const kibanaLegacyPluginMock = { diff --git a/src/plugins/kibana_legacy/public/navigate_to_default_app.ts b/src/plugins/kibana_legacy/public/navigate_to_default_app.ts index 80b8343d3b229a..cea901e9ba6b41 100644 --- a/src/plugins/kibana_legacy/public/navigate_to_default_app.ts +++ b/src/plugins/kibana_legacy/public/navigate_to_default_app.ts @@ -43,12 +43,7 @@ export function navigateToDefaultApp( // when the correct app is already loaded, just set the hash to the right value // otherwise use navigateToApp (or setting href in case of kibana app) if (currentAppId !== targetAppId) { - if (targetAppId === 'kibana') { - // exception for kibana app because redirect won't work right otherwise - window.location.href = basePath.prepend(`/app/kibana${targetAppPath}`); - } else { - application.navigateToApp(targetAppId, { path: targetAppPath }); - } + application.navigateToApp(targetAppId, { path: targetAppPath, replace: true }); } else if (overwriteHash) { window.location.hash = targetAppPath; } diff --git a/src/plugins/kibana_legacy/public/plugin.ts b/src/plugins/kibana_legacy/public/plugin.ts index c1a93f180db6fe..59ce88c07f4f48 100644 --- a/src/plugins/kibana_legacy/public/plugin.ts +++ b/src/plugins/kibana_legacy/public/plugin.ts @@ -17,26 +17,14 @@ * under the License. */ -import { - App, - AppBase, - PluginInitializerContext, - AppUpdatableFields, - CoreStart, - CoreSetup, -} from 'kibana/public'; -import { Observable, Subscription } from 'rxjs'; +import { PluginInitializerContext, CoreStart, CoreSetup } from 'kibana/public'; +import { Subscription } from 'rxjs'; import { ConfigSchema } from '../config'; import { getDashboardConfig } from './dashboard_config'; import { navigateToDefaultApp } from './navigate_to_default_app'; import { createLegacyUrlForwardApp } from './forward_app'; import { injectHeaderStyle } from './utils/inject_header_style'; - -interface LegacyAppAliasDefinition { - legacyAppId: string; - newAppId: string; - keepPrefix: boolean; -} +import { navigateToLegacyKibanaUrl } from './forward_app/navigate_to_legacy_kibana_url'; export interface ForwardDefinition { legacyAppId: string; @@ -44,27 +32,7 @@ export interface ForwardDefinition { rewritePath: (legacyPath: string) => string; } -export type AngularRenderedAppUpdater = ( - app: AppBase -) => Partial | undefined; - -export interface AngularRenderedApp extends App { - /** - * Angular rendered apps are able to update the active url in the nav link (which is currently not - * possible for actual NP apps). When regular applications have the same functionality, this type - * override can be removed. - */ - updater$?: Observable; - /** - * If the active url is updated via the updater$ subject, the app id is assumed to be identical with - * the nav link id. If this is not the case, it is possible to provide another nav link id here. - */ - navLinkId?: string; -} - export class KibanaLegacyPlugin { - private apps: AngularRenderedApp[] = []; - private legacyAppAliases: LegacyAppAliasDefinition[] = []; private forwardDefinitions: ForwardDefinition[] = []; private currentAppId: string | undefined; private currentAppIdSubscription: Subscription | undefined; @@ -72,57 +40,8 @@ export class KibanaLegacyPlugin { constructor(private readonly initializerContext: PluginInitializerContext) {} public setup(core: CoreSetup<{}, KibanaLegacyStart>) { - core.application.register(createLegacyUrlForwardApp(core)); + core.application.register(createLegacyUrlForwardApp(core, this.forwardDefinitions)); return { - /** - * @deprecated - * Register an app to be managed by the application service. - * This method works exactly as `core.application.register`. - * - * When an app is mounted, it is responsible for routing. The app - * won't be mounted again if the route changes within the prefix - * of the app (its id). It is fine to use whatever means for handling - * routing within the app. - * - * When switching to a URL outside of the current prefix, the app router - * shouldn't do anything because it doesn't own the routing anymore - - * the local application service takes over routing again, - * unmounts the current app and mounts the next app. - * - * @param app The app descriptor - */ - registerLegacyApp: (app: AngularRenderedApp) => { - this.apps.push(app); - }, - - /** - * @deprecated - * Forwards every URL starting with `legacyAppId` to the same URL starting - * with `newAppId` - e.g. `/legacy/my/legacy/path?q=123` gets forwarded to - * `/newApp/my/legacy/path?q=123`. - * - * When setting the `keepPrefix` option, the new app id is simply prepended. - * The example above would become `/newApp/legacy/my/legacy/path?q=123`. - * - * This method can be used to provide backwards compatibility for URLs when - * renaming or nesting plugins. For route changes after the prefix, please - * use the routing mechanism of your app. - * - * This method just redirects URLs within the legacy `kibana` app. - * - * @param legacyAppId The name of the old app to forward URLs from - * @param newAppId The name of the new app that handles the URLs now - * @param options Whether the prefix of the old app is kept to nest the legacy - * path into the new path - */ - registerLegacyAppAlias: ( - legacyAppId: string, - newAppId: string, - options: { keepPrefix: boolean } = { keepPrefix: false } - ) => { - this.legacyAppAliases.push({ legacyAppId, newAppId, ...options }); - }, - /** * Forwards URLs within the legacy `kibana` app to a new platform application. * @@ -164,18 +83,6 @@ export class KibanaLegacyPlugin { rewritePath: rewritePath || ((path) => `#${path.replace(`/${legacyAppId}`, '') || '/'}`), }); }, - - /** - * @deprecated - * The `defaultAppId` config key is temporarily exposed to be used in the legacy platform. - * As this setting is going away, no new code should depend on it. - */ - config: this.initializerContext.config.get(), - /** - * @deprecated - * Temporarily exposing the NP env to simulate initializer contexts in the LP. - */ - env: this.initializerContext.env, }; } @@ -186,21 +93,9 @@ export class KibanaLegacyPlugin { injectHeaderStyle(uiSettings); return { /** + * Used to power dashboard mode. Should be removed when dashboard mode is removed eventually. * @deprecated - * Just exported for wiring up with legacy platform, should not be used. - */ - getApps: () => this.apps, - /** - * @deprecated - * Just exported for wiring up with legacy platform, should not be used. - */ - getLegacyAppAliases: () => this.legacyAppAliases, - /** - * @deprecated - * Just exported for wiring up with legacy platform, should not be used. */ - getForwards: () => this.forwardDefinitions, - config: this.initializerContext.config.get(), dashboardConfig: getDashboardConfig(!application.capabilities.dashboard.showWriteControls), /** * Navigates to the app defined as kibana.defaultAppId. @@ -218,6 +113,32 @@ export class KibanaLegacyPlugin { overwriteHash ); }, + /** + * Resolves the provided hash using the registered forwards and navigates to the target app. + * If a navigation happened, `{ navigated: true }` will be returned. + * If no matching forward is found, `{ navigated: false }` will be returned. + * @param hash + */ + navigateToLegacyKibanaUrl: (hash: string) => { + return navigateToLegacyKibanaUrl(hash, this.forwardDefinitions, basePath, application); + }, + /** + * Loads the font-awesome icon font. Should be removed once the last consumer has migrated to EUI + * @deprecated + */ + loadFontAwesome: async () => { + await import('./font_awesome'); + }, + /** + * @deprecated + * Just exported for wiring up with legacy platform, should not be used. + */ + getForwards: () => this.forwardDefinitions, + /** + * @deprecated + * Just exported for wiring up with dashboard mode, should not be used. + */ + config: this.initializerContext.config.get(), }; } diff --git a/src/plugins/kibana_legacy/public/utils/index.ts b/src/plugins/kibana_legacy/public/utils/index.ts index 339079d3ac3523..e7dd55ec5582b4 100644 --- a/src/plugins/kibana_legacy/public/utils/index.ts +++ b/src/plugins/kibana_legacy/public/utils/index.ts @@ -19,6 +19,7 @@ export * from './migrate_legacy_query'; export * from './system_api'; +export * from './normalize_path'; // @ts-ignore export { KbnAccessibleClickProvider } from './kbn_accessible_click'; // @ts-ignore diff --git a/src/legacy/core_plugins/kibana/inject_vars.js b/src/plugins/kibana_legacy/public/utils/normalize_path.ts similarity index 73% rename from src/legacy/core_plugins/kibana/inject_vars.js rename to src/plugins/kibana_legacy/public/utils/normalize_path.ts index c3b906ee842e33..ece6c89cb7cdd5 100644 --- a/src/legacy/core_plugins/kibana/inject_vars.js +++ b/src/plugins/kibana_legacy/public/utils/normalize_path.ts @@ -17,11 +17,11 @@ * under the License. */ -export function injectVars(server) { - const serverConfig = server.config(); +import { normalize } from 'path'; - return { - autocompleteTerminateAfter: serverConfig.get('kibana.autocompleteTerminateAfter'), - autocompleteTimeout: serverConfig.get('kibana.autocompleteTimeout'), - }; +export function normalizePath(path: string) { + // resolve ../ within the path + const normalizedPath = normalize(path); + // strip any leading slashes and dots and replace with single leading slash + return normalizedPath.replace(/(\.?\.?\/?)*/, '/'); } diff --git a/src/plugins/kibana_utils/public/state_management/url/kbn_url_tracker.ts b/src/plugins/kibana_utils/public/state_management/url/kbn_url_tracker.ts index d9ff3ef36abafb..ce00b2bf68d932 100644 --- a/src/plugins/kibana_utils/public/state_management/url/kbn_url_tracker.ts +++ b/src/plugins/kibana_utils/public/state_management/url/kbn_url_tracker.ts @@ -38,6 +38,10 @@ export interface KbnUrlTracker { stop: () => void; setActiveUrl: (newUrl: string) => void; getActiveUrl: () => string; + /** + * Resets internal state to the last active url, discarding the most recent change + */ + restorePreviousUrl: () => void; } /** @@ -122,6 +126,8 @@ export function createKbnUrlTracker({ }): KbnUrlTracker { const storageInstance = storage || sessionStorage; + // local state storing previous active url to make restore possible + let previousActiveUrl: string = ''; // local state storing current listeners and active url let activeUrl: string = ''; let unsubscribeURLHistory: UnregisterCallback | undefined; @@ -157,6 +163,7 @@ export function createKbnUrlTracker({ toastNotifications.addDanger(e.message); } + previousActiveUrl = activeUrl; activeUrl = getActiveSubUrl(urlWithStates || urlWithHashes); storageInstance.setItem(storageKey, activeUrl); } @@ -183,6 +190,7 @@ export function createKbnUrlTracker({ { useHash: false }, baseUrl + (activeUrl || defaultSubUrl) ); + previousActiveUrl = activeUrl; // remove baseUrl prefix (just storing the sub url part) activeUrl = getActiveSubUrl(updatedUrl); storageInstance.setItem(storageKey, activeUrl); @@ -198,6 +206,7 @@ export function createKbnUrlTracker({ const storedUrl = storageInstance.getItem(storageKey); if (storedUrl) { activeUrl = storedUrl; + previousActiveUrl = storedUrl; setNavLink(storedUrl); } @@ -217,5 +226,8 @@ export function createKbnUrlTracker({ getActiveUrl() { return activeUrl; }, + restorePreviousUrl() { + activeUrl = previousActiveUrl; + }, }; } diff --git a/src/plugins/legacy_export/kibana.json b/src/plugins/legacy_export/kibana.json new file mode 100644 index 00000000000000..f382cab1e655d9 --- /dev/null +++ b/src/plugins/legacy_export/kibana.json @@ -0,0 +1,6 @@ +{ + "id": "legacyExport", + "version": "kibana", + "server": true, + "ui": false +} diff --git a/src/legacy/ui/public/visualize/loader/pipeline_helpers/index.ts b/src/plugins/legacy_export/server/index.ts similarity index 80% rename from src/legacy/ui/public/visualize/loader/pipeline_helpers/index.ts rename to src/plugins/legacy_export/server/index.ts index f9a2234d6e5a46..52d4da168b71dd 100644 --- a/src/legacy/ui/public/visualize/loader/pipeline_helpers/index.ts +++ b/src/plugins/legacy_export/server/index.ts @@ -17,5 +17,7 @@ * under the License. */ -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -export { buildPipeline } from '../../../../../../plugins/visualizations/public/legacy/build_pipeline'; +import { PluginInitializer } from 'src/core/server'; +import { LegacyExportPlugin } from './plugin'; + +export const plugin: PluginInitializer<{}, {}> = (context) => new LegacyExportPlugin(context); diff --git a/src/legacy/core_plugins/kibana/server/lib/export/collect_references_deep.test.ts b/src/plugins/legacy_export/server/lib/export/collect_references_deep.test.ts similarity index 98% rename from src/legacy/core_plugins/kibana/server/lib/export/collect_references_deep.test.ts rename to src/plugins/legacy_export/server/lib/export/collect_references_deep.test.ts index d1be3d64fdb3fc..603afe364aba2c 100644 --- a/src/legacy/core_plugins/kibana/server/lib/export/collect_references_deep.test.ts +++ b/src/plugins/legacy_export/server/lib/export/collect_references_deep.test.ts @@ -18,8 +18,8 @@ */ import { SavedObject, SavedObjectAttributes } from 'src/core/server'; +import { savedObjectsClientMock } from '../../../../../core/server/mocks'; import { collectReferencesDeep } from './collect_references_deep'; -import { savedObjectsClientMock } from '../../../../../../core/server/mocks'; const data: Array> = [ { diff --git a/src/legacy/core_plugins/kibana/server/lib/export/collect_references_deep.ts b/src/plugins/legacy_export/server/lib/export/collect_references_deep.ts similarity index 98% rename from src/legacy/core_plugins/kibana/server/lib/export/collect_references_deep.ts rename to src/plugins/legacy_export/server/lib/export/collect_references_deep.ts index e44db901a0cb80..8e8ae1332d74b6 100644 --- a/src/legacy/core_plugins/kibana/server/lib/export/collect_references_deep.ts +++ b/src/plugins/legacy_export/server/lib/export/collect_references_deep.ts @@ -29,7 +29,7 @@ interface ObjectsToCollect { export async function collectReferencesDeep( savedObjectClient: SavedObjectsClientContract, objects: ObjectsToCollect[] -) { +): Promise { let result: SavedObject[] = []; const queue = [...objects]; while (queue.length !== 0) { diff --git a/src/legacy/core_plugins/kibana/server/lib/export/export_dashboards.js b/src/plugins/legacy_export/server/lib/export/export_dashboards.ts similarity index 80% rename from src/legacy/core_plugins/kibana/server/lib/export/export_dashboards.js rename to src/plugins/legacy_export/server/lib/export/export_dashboards.ts index 913ebff588f84e..4b2e548f3f7fdf 100644 --- a/src/legacy/core_plugins/kibana/server/lib/export/export_dashboards.js +++ b/src/plugins/legacy_export/server/lib/export/export_dashboards.ts @@ -17,19 +17,19 @@ * under the License. */ -import _ from 'lodash'; +import { SavedObjectsClientContract } from 'src/core/server'; import { collectReferencesDeep } from './collect_references_deep'; -export async function exportDashboards(req) { - const ids = _.flatten([req.query.dashboard]); - const config = req.server.config(); - - const savedObjectsClient = req.getSavedObjectsClient(); +export async function exportDashboards( + ids: string[], + savedObjectsClient: SavedObjectsClientContract, + kibanaVersion: string +) { const objectsToExport = ids.map((id) => ({ id, type: 'dashboard' })); const objects = await collectReferencesDeep(savedObjectsClient, objectsToExport); return { - version: config.get('pkg.version'), + version: kibanaVersion, objects, }; } diff --git a/src/plugins/legacy_export/server/lib/import/import_dashboards.test.ts b/src/plugins/legacy_export/server/lib/import/import_dashboards.test.ts new file mode 100644 index 00000000000000..9d4dbb60679461 --- /dev/null +++ b/src/plugins/legacy_export/server/lib/import/import_dashboards.test.ts @@ -0,0 +1,92 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { savedObjectsClientMock } from '../../../../../core/server/mocks'; +import { SavedObject } from '../../../../../core/server'; +import { importDashboards } from './import_dashboards'; + +describe('importDashboards(req)', () => { + let savedObjectClient: ReturnType; + let importedObjects: SavedObject[]; + + beforeEach(() => { + savedObjectClient = savedObjectsClientMock.create(); + savedObjectClient.bulkCreate.mockResolvedValue({ saved_objects: [] }); + + importedObjects = [ + { id: 'dashboard-01', type: 'dashboard', attributes: { panelJSON: '{}' }, references: [] }, + { id: 'panel-01', type: 'visualization', attributes: { visState: '{}' }, references: [] }, + ]; + }); + + test('should call bulkCreate with each asset', async () => { + await importDashboards(savedObjectClient, importedObjects, { overwrite: false, exclude: [] }); + + expect(savedObjectClient.bulkCreate).toHaveBeenCalledTimes(1); + expect(savedObjectClient.bulkCreate).toHaveBeenCalledWith( + [ + { + id: 'dashboard-01', + type: 'dashboard', + attributes: { panelJSON: '{}' }, + references: [], + migrationVersion: {}, + }, + { + id: 'panel-01', + type: 'visualization', + attributes: { visState: '{}' }, + references: [], + migrationVersion: {}, + }, + ], + { overwrite: false } + ); + }); + + test('should call bulkCreate with overwrite true if force is truthy', async () => { + await importDashboards(savedObjectClient, importedObjects, { overwrite: true, exclude: [] }); + + expect(savedObjectClient.bulkCreate).toHaveBeenCalledTimes(1); + expect(savedObjectClient.bulkCreate).toHaveBeenCalledWith(expect.any(Array), { + overwrite: true, + }); + }); + + test('should exclude types based on exclude argument', async () => { + await importDashboards(savedObjectClient, importedObjects, { + overwrite: false, + exclude: ['visualization'], + }); + + expect(savedObjectClient.bulkCreate).toHaveBeenCalledTimes(1); + expect(savedObjectClient.bulkCreate).toHaveBeenCalledWith( + [ + { + id: 'dashboard-01', + type: 'dashboard', + attributes: { panelJSON: '{}' }, + references: [], + migrationVersion: {}, + }, + ], + { overwrite: false } + ); + }); +}); diff --git a/src/legacy/core_plugins/kibana/server/lib/import/import_dashboards.js b/src/plugins/legacy_export/server/lib/import/import_dashboards.ts similarity index 81% rename from src/legacy/core_plugins/kibana/server/lib/import/import_dashboards.js rename to src/plugins/legacy_export/server/lib/import/import_dashboards.ts index 7c28b184144f1a..7b7562aecd7bd8 100644 --- a/src/legacy/core_plugins/kibana/server/lib/import/import_dashboards.js +++ b/src/plugins/legacy_export/server/lib/import/import_dashboards.ts @@ -17,21 +17,19 @@ * under the License. */ -import { flatten } from 'lodash'; - -export async function importDashboards(req) { - const { payload } = req; - const overwrite = 'force' in req.query && req.query.force !== false; - const exclude = flatten([req.query.exclude]); - - const savedObjectsClient = req.getSavedObjectsClient(); +import { SavedObject, SavedObjectsClientContract } from 'src/core/server'; +export async function importDashboards( + savedObjectsClient: SavedObjectsClientContract, + objects: SavedObject[], + { overwrite, exclude }: { overwrite: boolean; exclude: string[] } +) { // The server assumes that documents with no migrationVersion are up to date. // That assumption enables Kibana and other API consumers to not have to build // up migrationVersion prior to creating new objects. But it means that imports // need to set migrationVersion to something other than undefined, so that imported // docs are not seen as automatically up-to-date. - const docs = payload.objects + const docs = objects .filter((item) => !exclude.includes(item.type)) .map((doc) => ({ ...doc, migrationVersion: doc.migrationVersion || {} })); diff --git a/src/legacy/core_plugins/kibana/public/local_application_service/index.ts b/src/plugins/legacy_export/server/lib/index.ts similarity index 86% rename from src/legacy/core_plugins/kibana/public/local_application_service/index.ts rename to src/plugins/legacy_export/server/lib/index.ts index 2128355ca906ad..ceabdc76322e99 100644 --- a/src/legacy/core_plugins/kibana/public/local_application_service/index.ts +++ b/src/plugins/legacy_export/server/lib/index.ts @@ -17,4 +17,5 @@ * under the License. */ -export * from './local_application_service'; +export { exportDashboards } from './export/export_dashboards'; +export { importDashboards } from './import/import_dashboards'; diff --git a/src/legacy/core_plugins/kibana/server/lib/management/saved_objects/inject_meta_attributes.js b/src/plugins/legacy_export/server/plugin.ts similarity index 59% rename from src/legacy/core_plugins/kibana/server/lib/management/saved_objects/inject_meta_attributes.js rename to src/plugins/legacy_export/server/plugin.ts index 25f6341cf25da9..22c7c1a05dddbb 100644 --- a/src/legacy/core_plugins/kibana/server/lib/management/saved_objects/inject_meta_attributes.js +++ b/src/plugins/legacy_export/server/plugin.ts @@ -17,17 +17,26 @@ * under the License. */ -export function injectMetaAttributes(savedObject, savedObjectsManagement) { - const result = { - ...savedObject, - meta: savedObject.meta || {}, - }; +import { Plugin, CoreSetup, PluginInitializerContext } from 'kibana/server'; +import { registerRoutes } from './routes'; - // Add extra meta information - result.meta.icon = savedObjectsManagement.getIcon(savedObject.type); - result.meta.title = savedObjectsManagement.getTitle(savedObject); - result.meta.editUrl = savedObjectsManagement.getEditUrl(savedObject); - result.meta.inAppUrl = savedObjectsManagement.getInAppUrl(savedObject); +export class LegacyExportPlugin implements Plugin<{}, {}> { + private readonly kibanaVersion: string; - return result; + constructor(context: PluginInitializerContext) { + this.kibanaVersion = context.env.packageInfo.version; + } + + public setup({ http }: CoreSetup) { + const router = http.createRouter(); + registerRoutes(router, this.kibanaVersion); + + return {}; + } + + public start() { + return {}; + } + + public stop() {} } diff --git a/src/plugins/legacy_export/server/routes/export.ts b/src/plugins/legacy_export/server/routes/export.ts new file mode 100644 index 00000000000000..2064302eca432e --- /dev/null +++ b/src/plugins/legacy_export/server/routes/export.ts @@ -0,0 +1,56 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import moment from 'moment'; +import { schema } from '@kbn/config-schema'; +import { IRouter } from 'src/core/server'; +import { exportDashboards } from '../lib'; + +export const registerExportRoute = (router: IRouter, kibanaVersion: string) => { + router.get( + { + path: '/api/kibana/dashboards/export', + validate: { + query: schema.object({ + dashboard: schema.oneOf([schema.string(), schema.arrayOf(schema.string())]), + }), + }, + options: { + tags: ['api'], + }, + }, + async (ctx, req, res) => { + const ids = Array.isArray(req.query.dashboard) ? req.query.dashboard : [req.query.dashboard]; + const { client } = ctx.core.savedObjects; + + const exported = await exportDashboards(ids, client, kibanaVersion); + const filename = `kibana-dashboards.${moment.utc().format('YYYY-MM-DD-HH-mm-ss')}.json`; + const body = JSON.stringify(exported, null, ' '); + + return res.ok({ + body, + headers: { + 'Content-Disposition': `attachment; filename="${filename}"`, + 'Content-Type': 'application/json', + 'Content-Length': `${Buffer.byteLength(body, 'utf8')}`, + }, + }); + } + ); +}; diff --git a/src/plugins/legacy_export/server/routes/import.ts b/src/plugins/legacy_export/server/routes/import.ts new file mode 100644 index 00000000000000..cf6f28683be176 --- /dev/null +++ b/src/plugins/legacy_export/server/routes/import.ts @@ -0,0 +1,57 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { schema } from '@kbn/config-schema'; +import { IRouter, SavedObject } from 'src/core/server'; +import { importDashboards } from '../lib'; + +export const registerImportRoute = (router: IRouter) => { + router.post( + { + path: '/api/kibana/dashboards/import', + validate: { + body: schema.object({ + objects: schema.arrayOf(schema.recordOf(schema.string(), schema.any())), + version: schema.string(), + }), + query: schema.object({ + force: schema.boolean({ defaultValue: false }), + exclude: schema.oneOf([schema.string(), schema.arrayOf(schema.string())], { + defaultValue: [], + }), + }), + }, + options: { + tags: ['api'], + }, + }, + async (ctx, req, res) => { + const { client } = ctx.core.savedObjects; + const objects = req.body.objects as SavedObject[]; + const { force, exclude } = req.query; + const result = await importDashboards(client, objects, { + overwrite: force, + exclude: Array.isArray(exclude) ? exclude : [exclude], + }); + return res.ok({ + body: result, + }); + } + ); +}; diff --git a/src/plugins/legacy_export/server/routes/index.ts b/src/plugins/legacy_export/server/routes/index.ts new file mode 100644 index 00000000000000..7b9de7f016b6ba --- /dev/null +++ b/src/plugins/legacy_export/server/routes/index.ts @@ -0,0 +1,27 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { IRouter } from 'src/core/server'; +import { registerImportRoute } from './import'; +import { registerExportRoute } from './export'; + +export const registerRoutes = (router: IRouter, kibanaVersion: string) => { + registerExportRoute(router, kibanaVersion); + registerImportRoute(router); +}; diff --git a/src/plugins/maps_legacy/public/_index.scss b/src/plugins/maps_legacy/public/_index.scss deleted file mode 100644 index 28cf4289bb0481..00000000000000 --- a/src/plugins/maps_legacy/public/_index.scss +++ /dev/null @@ -1 +0,0 @@ -@import './map/index'; diff --git a/src/plugins/maps_legacy/public/index.ts b/src/plugins/maps_legacy/public/index.ts index a7f5427909334a..cbe8b9213d577d 100644 --- a/src/plugins/maps_legacy/public/index.ts +++ b/src/plugins/maps_legacy/public/index.ts @@ -44,6 +44,8 @@ import { // @ts-ignore import { mapTooltipProvider } from './tooltip_provider'; +import './map/index.scss'; + export interface MapsLegacyConfigType { regionmap: any; emsTileLayerId: string; diff --git a/src/plugins/maps_legacy/public/map/_index.scss b/src/plugins/maps_legacy/public/map/index.scss similarity index 100% rename from src/plugins/maps_legacy/public/map/_index.scss rename to src/plugins/maps_legacy/public/map/index.scss diff --git a/src/plugins/region_map/kibana.json b/src/plugins/region_map/kibana.json index 3a6f64e92bcba6..ac7e1f8659d66d 100644 --- a/src/plugins/region_map/kibana.json +++ b/src/plugins/region_map/kibana.json @@ -9,6 +9,7 @@ "visualizations", "expressions", "mapsLegacy", + "kibanaLegacy", "data" ] } diff --git a/src/plugins/region_map/public/kibana_services.ts b/src/plugins/region_map/public/kibana_services.ts index 1ef58c69c5bef4..8367325c7415b8 100644 --- a/src/plugins/region_map/public/kibana_services.ts +++ b/src/plugins/region_map/public/kibana_services.ts @@ -20,6 +20,7 @@ import { NotificationsStart } from 'kibana/public'; import { createGetterSetter } from '../../kibana_utils/public'; import { DataPublicPluginStart } from '../../data/public'; +import { KibanaLegacyStart } from '../../kibana_legacy/public'; export const [getFormatService, setFormatService] = createGetterSetter< DataPublicPluginStart['fieldFormats'] @@ -28,3 +29,7 @@ export const [getFormatService, setFormatService] = createGetterSetter< export const [getNotifications, setNotifications] = createGetterSetter( 'Notifications' ); + +export const [getKibanaLegacy, setKibanaLegacy] = createGetterSetter( + 'KibanaLegacy' +); diff --git a/src/plugins/region_map/public/plugin.ts b/src/plugins/region_map/public/plugin.ts index 09a13fbe9774e7..6b31de758a4caa 100644 --- a/src/plugins/region_map/public/plugin.ts +++ b/src/plugins/region_map/public/plugin.ts @@ -31,10 +31,11 @@ import { createRegionMapFn } from './region_map_fn'; // @ts-ignore import { createRegionMapTypeDefinition } from './region_map_type'; import { getBaseMapsVis, IServiceSettings, MapsLegacyPluginSetup } from '../../maps_legacy/public'; -import { setFormatService, setNotifications } from './kibana_services'; +import { setFormatService, setNotifications, setKibanaLegacy } from './kibana_services'; import { DataPublicPluginStart } from '../../data/public'; import { RegionMapsConfigType } from './index'; import { ConfigSchema } from '../../maps_legacy/config'; +import { KibanaLegacyStart } from '../../kibana_legacy/public'; /** @private */ interface RegionMapVisualizationDependencies { @@ -55,6 +56,7 @@ export interface RegionMapPluginSetupDependencies { export interface RegionMapPluginStartDependencies { data: DataPublicPluginStart; notifications: NotificationsStart; + kibanaLegacy: KibanaLegacyStart; } /** @internal */ @@ -107,8 +109,9 @@ export class RegionMapPlugin implements Plugin { const ID = 'bf00ad16941fc51420f91a93428b27a0'; @@ -35,7 +35,7 @@ describe('shortUrlLookupProvider', () => { savedObjects = savedObjectsClientMock.create(); savedObjects.create.mockResolvedValue({ id: ID } as SavedObject); deps = { savedObjects }; - shortUrl = shortUrlLookupProvider({ logger: loggingServiceMock.create().get() }); + shortUrl = shortUrlLookupProvider({ logger: loggingSystemMock.create().get() }); }); describe('generateUrlId', () => { diff --git a/src/plugins/share/server/saved_objects/kibana_app_migration.ts b/src/plugins/share/server/saved_objects/kibana_app_migration.ts deleted file mode 100644 index 413b48d7fa6de7..00000000000000 --- a/src/plugins/share/server/saved_objects/kibana_app_migration.ts +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { SavedObjectMigrationFn } from 'kibana/server'; - -/** - * To avoid loading the client twice for old short urls pointing to the /app/kibana app, - * this PR rewrites them to point to the new platform app url_migrate instead. This app will - * migrate the url on the fly and redirect the user to the actual new location of the short url - * without loading the page again. - * @param doc - */ -export const migrateLegacyKibanaAppShortUrls: SavedObjectMigrationFn = (doc) => ({ - ...doc, - attributes: { - ...doc.attributes, - url: - typeof doc.attributes.url === 'string' && doc.attributes.url.startsWith('/app/kibana') - ? doc.attributes.url.replace('/app/kibana', '/app/url_migrate') - : doc.attributes.url, - }, -}); diff --git a/src/plugins/share/server/saved_objects/url.ts b/src/plugins/share/server/saved_objects/url.ts index 3103777179741d..c76c21993a13f1 100644 --- a/src/plugins/share/server/saved_objects/url.ts +++ b/src/plugins/share/server/saved_objects/url.ts @@ -16,9 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -import { SavedObjectMigrationFn, SavedObjectsType } from 'kibana/server'; -import { flow } from 'lodash'; -import { migrateLegacyKibanaAppShortUrls } from './kibana_app_migration'; +import { SavedObjectsType } from 'kibana/server'; export const url: SavedObjectsType = { name: 'url', @@ -32,9 +30,6 @@ export const url: SavedObjectsType = { return `/goto/${encodeURIComponent(obj.id)}`; }, }, - migrations: { - '7.9.0': flow(migrateLegacyKibanaAppShortUrls), - }, mappings: { properties: { accessCount: { diff --git a/src/plugins/tile_map/kibana.json b/src/plugins/tile_map/kibana.json index 71ae0bb29d17f1..bb8ef5a2465494 100644 --- a/src/plugins/tile_map/kibana.json +++ b/src/plugins/tile_map/kibana.json @@ -9,6 +9,7 @@ "visualizations", "expressions", "mapsLegacy", + "kibanaLegacy", "data" ] } diff --git a/src/plugins/tile_map/public/plugin.ts b/src/plugins/tile_map/public/plugin.ts index e55f7189929dfc..20a45c586074a5 100644 --- a/src/plugins/tile_map/public/plugin.ts +++ b/src/plugins/tile_map/public/plugin.ts @@ -35,6 +35,8 @@ import { createTileMapTypeDefinition } from './tile_map_type'; import { getBaseMapsVis, MapsLegacyPluginSetup } from '../../maps_legacy/public'; import { DataPublicPluginStart } from '../../data/public'; import { setFormatService, setQueryService } from './services'; +import { setKibanaLegacy } from './services'; +import { KibanaLegacyStart } from '../../kibana_legacy/public'; export interface TileMapConfigType { tilemap: any; @@ -58,6 +60,7 @@ export interface TileMapPluginSetupDependencies { /** @internal */ export interface TileMapPluginStartDependencies { data: DataPublicPluginStart; + kibanaLegacy: KibanaLegacyStart; } export interface TileMapPluginSetup { @@ -96,9 +99,10 @@ export class TileMapPlugin implements Plugin('Query'); + +export const [getKibanaLegacy, setKibanaLegacy] = createGetterSetter( + 'KibanaLegacy' +); diff --git a/src/plugins/tile_map/public/tile_map_visualization.js b/src/plugins/tile_map/public/tile_map_visualization.js index 1f4e5f09a9aa45..2ebb76d05c2195 100644 --- a/src/plugins/tile_map/public/tile_map_visualization.js +++ b/src/plugins/tile_map/public/tile_map_visualization.js @@ -19,7 +19,7 @@ import { get } from 'lodash'; import { GeohashLayer } from './geohash_layer'; -import { getFormatService, getQueryService } from './services'; +import { getFormatService, getQueryService, getKibanaLegacy } from './services'; import { scaleBounds, geoContains, mapTooltipProvider } from '../../maps_legacy/public'; import { tooltipFormatter } from './tooltip_formatter'; @@ -60,6 +60,11 @@ export const createTileMapVisualization = (dependencies) => { this.vis.eventsSubject.next(updateVarsObject); }; + async render(esResponse, visParams) { + getKibanaLegacy().loadFontAwesome(); + await super.render(esResponse, visParams); + } + async _makeKibanaMap() { await super._makeKibanaMap(); diff --git a/src/plugins/usage_collection/server/collector/collector_set.test.ts b/src/plugins/usage_collection/server/collector/collector_set.test.ts index ced5206cee318d..50919ecb3d83f8 100644 --- a/src/plugins/usage_collection/server/collector/collector_set.test.ts +++ b/src/plugins/usage_collection/server/collector/collector_set.test.ts @@ -21,9 +21,9 @@ import { noop } from 'lodash'; import { Collector } from './collector'; import { CollectorSet } from './collector_set'; import { UsageCollector } from './usage_collector'; -import { loggingServiceMock } from '../../../../core/server/mocks'; +import { loggingSystemMock } from '../../../../core/server/mocks'; -const logger = loggingServiceMock.createLogger(); +const logger = loggingSystemMock.createLogger(); const loggerSpies = { debug: jest.spyOn(logger, 'debug'), diff --git a/src/plugins/usage_collection/server/mocks.ts b/src/plugins/usage_collection/server/mocks.ts index ca3710c62cd893..e1f13304165a19 100644 --- a/src/plugins/usage_collection/server/mocks.ts +++ b/src/plugins/usage_collection/server/mocks.ts @@ -17,14 +17,14 @@ * under the License. */ -import { loggingServiceMock } from '../../../core/server/mocks'; +import { loggingSystemMock } from '../../../core/server/mocks'; import { UsageCollectionSetup } from './plugin'; import { CollectorSet } from './collector'; const createSetupContract = () => { return { ...new CollectorSet({ - logger: loggingServiceMock.createLogger(), + logger: loggingSystemMock.createLogger(), maximumWaitTimeForAllCollectorsInS: 1, }), } as UsageCollectionSetup; diff --git a/src/plugins/vis_type_table/kibana.json b/src/plugins/vis_type_table/kibana.json index bb0f6478a42408..ed098d71614033 100644 --- a/src/plugins/vis_type_table/kibana.json +++ b/src/plugins/vis_type_table/kibana.json @@ -6,6 +6,7 @@ "requiredPlugins": [ "expressions", "visualizations", - "data" + "data", + "kibanaLegacy" ] } diff --git a/src/plugins/vis_type_table/public/plugin.ts b/src/plugins/vis_type_table/public/plugin.ts index a41d939523bcca..28f823df79d919 100644 --- a/src/plugins/vis_type_table/public/plugin.ts +++ b/src/plugins/vis_type_table/public/plugin.ts @@ -23,7 +23,8 @@ import { VisualizationsSetup } from '../../visualizations/public'; import { createTableVisFn } from './table_vis_fn'; import { getTableVisTypeDefinition } from './table_vis_type'; import { DataPublicPluginStart } from '../../data/public'; -import { setFormatService } from './services'; +import { setFormatService, setKibanaLegacy } from './services'; +import { KibanaLegacyStart } from '../../kibana_legacy/public'; /** @internal */ export interface TablePluginSetupDependencies { @@ -34,6 +35,7 @@ export interface TablePluginSetupDependencies { /** @internal */ export interface TablePluginStartDependencies { data: DataPublicPluginStart; + kibanaLegacy: KibanaLegacyStart; } /** @internal */ @@ -55,7 +57,8 @@ export class TableVisPlugin implements Plugin, void> { ); } - public start(core: CoreStart, { data }: TablePluginStartDependencies) { + public start(core: CoreStart, { data, kibanaLegacy }: TablePluginStartDependencies) { setFormatService(data.fieldFormats); + setKibanaLegacy(kibanaLegacy); } } diff --git a/src/plugins/vis_type_table/public/services.ts b/src/plugins/vis_type_table/public/services.ts index 3aaffe75e27f14..b4f996f078f6be 100644 --- a/src/plugins/vis_type_table/public/services.ts +++ b/src/plugins/vis_type_table/public/services.ts @@ -19,7 +19,12 @@ import { createGetterSetter } from '../../kibana_utils/public'; import { DataPublicPluginStart } from '../../data/public'; +import { KibanaLegacyStart } from '../../kibana_legacy/public'; export const [getFormatService, setFormatService] = createGetterSetter< DataPublicPluginStart['fieldFormats'] >('table data.fieldFormats'); + +export const [getKibanaLegacy, setKibanaLegacy] = createGetterSetter( + 'table kibanaLegacy' +); diff --git a/src/plugins/vis_type_table/public/vis_controller.ts b/src/plugins/vis_type_table/public/vis_controller.ts index d49dd32c8c89c3..a5086e0c9a2d80 100644 --- a/src/plugins/vis_type_table/public/vis_controller.ts +++ b/src/plugins/vis_type_table/public/vis_controller.ts @@ -22,6 +22,7 @@ import $ from 'jquery'; import { VisParams, ExprVis } from '../../visualizations/public'; import { getAngularModule } from './get_inner_angular'; +import { getKibanaLegacy } from './services'; import { initTableVisLegacyModule } from './table_vis_legacy_module'; const innerAngularName = 'kibana/table_vis'; @@ -64,6 +65,7 @@ export function getTableVisualizationControllerClass( } async render(esResponse: object, visParams: VisParams) { + getKibanaLegacy().loadFontAwesome(); await this.initLocalAngular(); return new Promise(async (resolve, reject) => { diff --git a/src/plugins/vis_type_vislib/kibana.json b/src/plugins/vis_type_vislib/kibana.json index 5b3088b399ebff..cad0ebe01494a0 100644 --- a/src/plugins/vis_type_vislib/kibana.json +++ b/src/plugins/vis_type_vislib/kibana.json @@ -3,6 +3,6 @@ "version": "kibana", "server": true, "ui": true, - "requiredPlugins": ["charts", "data", "expressions", "visualizations"], + "requiredPlugins": ["charts", "data", "expressions", "visualizations", "kibanaLegacy"], "optionalPlugins": ["visTypeXy"] } diff --git a/src/plugins/vis_type_vislib/public/plugin.ts b/src/plugins/vis_type_vislib/public/plugin.ts index 19bbf89ee0243e..c6a6b6f82592b5 100644 --- a/src/plugins/vis_type_vislib/public/plugin.ts +++ b/src/plugins/vis_type_vislib/public/plugin.ts @@ -44,7 +44,8 @@ import { } from './vis_type_vislib_vis_types'; import { ChartsPluginSetup } from '../../charts/public'; import { DataPublicPluginStart } from '../../data/public'; -import { setFormatService, setDataActions } from './services'; +import { setFormatService, setDataActions, setKibanaLegacy } from './services'; +import { KibanaLegacyStart } from '../../kibana_legacy/public'; export interface VisTypeVislibDependencies { uiSettings: IUiSettingsClient; @@ -62,6 +63,7 @@ export interface VisTypeVislibPluginSetupDependencies { /** @internal */ export interface VisTypeVislibPluginStartDependencies { data: DataPublicPluginStart; + kibanaLegacy: KibanaLegacyStart; } type VisTypeVislibCoreSetup = CoreSetup; @@ -109,8 +111,9 @@ export class VisTypeVislibPlugin implements Plugin { ); } - public start(core: CoreStart, { data }: VisTypeVislibPluginStartDependencies) { + public start(core: CoreStart, { data, kibanaLegacy }: VisTypeVislibPluginStartDependencies) { setFormatService(data.fieldFormats); setDataActions(data.actions); + setKibanaLegacy(kibanaLegacy); } } diff --git a/src/plugins/vis_type_vislib/public/services.ts b/src/plugins/vis_type_vislib/public/services.ts index 633fae9c7f2a61..7257b98f2e9f5b 100644 --- a/src/plugins/vis_type_vislib/public/services.ts +++ b/src/plugins/vis_type_vislib/public/services.ts @@ -19,6 +19,7 @@ import { createGetterSetter } from '../../kibana_utils/public'; import { DataPublicPluginStart } from '../../data/public'; +import { KibanaLegacyStart } from '../../kibana_legacy/public'; export const [getDataActions, setDataActions] = createGetterSetter< DataPublicPluginStart['actions'] @@ -27,3 +28,7 @@ export const [getDataActions, setDataActions] = createGetterSetter< export const [getFormatService, setFormatService] = createGetterSetter< DataPublicPluginStart['fieldFormats'] >('vislib data.fieldFormats'); + +export const [getKibanaLegacy, setKibanaLegacy] = createGetterSetter( + 'vislib kibanalegacy' +); diff --git a/src/plugins/vis_type_vislib/public/vis_controller.tsx b/src/plugins/vis_type_vislib/public/vis_controller.tsx index 262290b071abce..86ef98de045d7c 100644 --- a/src/plugins/vis_type_vislib/public/vis_controller.tsx +++ b/src/plugins/vis_type_vislib/public/vis_controller.tsx @@ -27,6 +27,7 @@ import { VisTypeVislibDependencies } from './plugin'; import { mountReactNode } from '../../../core/public/utils'; import { VisLegend, CUSTOM_LEGEND_VIS_TYPES } from './vislib/components/legend'; import { VisParams, ExprVis } from '../../visualizations/public'; +import { getKibanaLegacy } from './services'; const legendClassName = { top: 'visLib--legend-top', @@ -72,6 +73,8 @@ export const createVislibVisController = (deps: VisTypeVislibDependencies) => { this.destroy(); } + getKibanaLegacy().loadFontAwesome(); + return new Promise(async (resolve) => { if (this.el.clientWidth === 0 || this.el.clientHeight === 0) { return resolve(); diff --git a/src/plugins/visualizations/public/legacy/build_pipeline.test.ts b/src/plugins/visualizations/public/legacy/build_pipeline.test.ts index 9130581963800b..9ecd321963e8a4 100644 --- a/src/plugins/visualizations/public/legacy/build_pipeline.test.ts +++ b/src/plugins/visualizations/public/legacy/build_pipeline.test.ts @@ -28,7 +28,7 @@ import { } from './build_pipeline'; import { Vis } from '..'; import { dataPluginMock } from '../../../../plugins/data/public/mocks'; -import { IAggConfig } from '../../../../plugins/data/public'; +import { IndexPattern, IAggConfigs } from '../../../../plugins/data/public'; describe('visualize loader pipeline helpers: build pipeline', () => { describe('prepareJson', () => { @@ -344,23 +344,20 @@ describe('visualize loader pipeline helpers: build pipeline', () => { describe('buildVislibDimensions', () => { const dataStart = dataPluginMock.createStartContract(); - let aggs: IAggConfig[]; + let aggs: IAggConfigs; let vis: Vis; let params: any; beforeEach(() => { - aggs = [ + aggs = dataStart.search.aggs.createAggConfigs({} as IndexPattern, [ { id: '0', enabled: true, - type: { - type: 'metrics', - name: 'count', - }, + type: 'count', schema: 'metric', params: {}, - } as IAggConfig, - ]; + }, + ]); params = { searchSource: null, @@ -393,11 +390,7 @@ describe('visualize loader pipeline helpers: build pipeline', () => { ], }, data: { - aggs: { - getResponseAggs: () => { - return aggs; - }, - } as any, + aggs, searchSource: {} as any, }, isHierarchical: () => { @@ -422,8 +415,13 @@ describe('visualize loader pipeline helpers: build pipeline', () => { }); it('with two numeric metrics, mixed normal and percent mode should have corresponding formatters', async () => { - const aggConfig = aggs[0]; - aggs = [{ ...aggConfig } as IAggConfig, { ...aggConfig, id: '5' } as IAggConfig]; + aggs.createAggConfig({ + id: '5', + enabled: true, + type: 'count', + schema: 'metric', + params: {}, + }); vis.params = { seriesParams: [ @@ -469,11 +467,7 @@ describe('visualize loader pipeline helpers: build pipeline', () => { }, params: { gauge: {} }, data: { - aggs: { - getResponseAggs: () => { - return aggs; - }, - } as any, + aggs, searchSource: {} as any, }, isHierarchical: () => { diff --git a/src/plugins/visualizations/public/legacy/build_pipeline.ts b/src/plugins/visualizations/public/legacy/build_pipeline.ts index 5d74cb3d3b1e5d..62ff1f83426b9c 100644 --- a/src/plugins/visualizations/public/legacy/build_pipeline.ts +++ b/src/plugins/visualizations/public/legacy/build_pipeline.ts @@ -20,12 +20,7 @@ import { get } from 'lodash'; import moment from 'moment'; import { SerializedFieldFormat } from '../../../../plugins/expressions/public'; -import { - IAggConfig, - fieldFormats, - search, - TimefilterContract, -} from '../../../../plugins/data/public'; +import { IAggConfig, search, TimefilterContract } from '../../../../plugins/data/public'; import { Vis, VisParams } from '../types'; const { isDateHistogramBucketAggConfig } = search.aggs; @@ -113,11 +108,9 @@ const getSchemas = ( 'max_bucket', ].includes(agg.type.name); - const format = fieldFormats.serialize( - hasSubAgg - ? agg.params.customMetric || agg.aggConfigs.getRequestAggById(agg.params.metricAgg) - : agg - ); + const formatAgg = hasSubAgg + ? agg.params.customMetric || agg.aggConfigs.getRequestAggById(agg.params.metricAgg) + : agg; const params: SchemaConfigParams = {}; @@ -130,7 +123,7 @@ const getSchemas = ( return { accessor, - format, + format: formatAgg.toSerializedFieldFormat(), params, label, aggType: agg.type.name, diff --git a/src/plugins/visualize/public/application/legacy_app.js b/src/plugins/visualize/public/application/legacy_app.js index 42e8b07ee63103..452118f8097da0 100644 --- a/src/plugins/visualize/public/application/legacy_app.js +++ b/src/plugins/visualize/public/application/legacy_app.js @@ -244,9 +244,17 @@ export function initVisualizeApp(app, deps) { }, }) .otherwise({ - template: '', - controller: function () { - deps.kibanaLegacy.navigateToDefaultApp(); + resolveRedirectTo: function ($rootScope) { + const path = window.location.hash.substr(1); + deps.restorePreviousUrl(); + $rootScope.$applyAsync(() => { + const { navigated } = deps.kibanaLegacy.navigateToLegacyKibanaUrl(path); + if (!navigated) { + deps.kibanaLegacy.navigateToDefaultApp(); + } + }); + // prevent angular from completing the navigation + return new Promise(() => {}); }, }); }); diff --git a/src/plugins/visualize/public/kibana_services.ts b/src/plugins/visualize/public/kibana_services.ts index d954a3f4925ac0..f1e6b0b37d55b7 100644 --- a/src/plugins/visualize/public/kibana_services.ts +++ b/src/plugins/visualize/public/kibana_services.ts @@ -55,6 +55,7 @@ export interface VisualizeKibanaServices { embeddable: EmbeddableStart; I18nContext: I18nStart['Context']; setActiveUrl: (newUrl: string) => void; + restorePreviousUrl: () => void; createVisEmbeddableFromObject: VisualizationsStart['__LEGACY']['createVisEmbeddableFromObject']; scopedHistory: () => ScopedHistory; savedObjects: SavedObjectsStart; diff --git a/src/plugins/visualize/public/plugin.ts b/src/plugins/visualize/public/plugin.ts index 3bd111084e34b0..bec082642d3d46 100644 --- a/src/plugins/visualize/public/plugin.ts +++ b/src/plugins/visualize/public/plugin.ts @@ -73,7 +73,13 @@ export class VisualizePlugin core: CoreSetup, { home, kibanaLegacy, data }: VisualizePluginSetupDependencies ) { - const { appMounted, appUnMounted, stop: stopUrlTracker, setActiveUrl } = createKbnUrlTracker({ + const { + appMounted, + appUnMounted, + stop: stopUrlTracker, + setActiveUrl, + restorePreviousUrl, + } = createKbnUrlTracker({ baseUrl: core.http.basePath.prepend('/app/visualize'), defaultSubUrl: '#/', storageKey: `lastUrl:${core.http.basePath.get()}:visualize`, @@ -136,6 +142,7 @@ export class VisualizePlugin pluginsStart.visualizations.__LEGACY.createVisEmbeddableFromObject, scopedHistory: () => this.currentHistory!, savedObjects: pluginsStart.savedObjects, + restorePreviousUrl, }; setServices(deps); diff --git a/test/api_integration/apis/saved_objects_management/find.ts b/test/api_integration/apis/saved_objects_management/find.ts index 4d9f1c1658139e..235d789a388df2 100644 --- a/test/api_integration/apis/saved_objects_management/find.ts +++ b/test/api_integration/apis/saved_objects_management/find.ts @@ -221,7 +221,7 @@ export default function ({ getService }: FtrProviderContext) { editUrl: '/management/kibana/objects/savedSearches/960372e0-3224-11e8-a572-ffca06da1357', inAppUrl: { - path: '/app/discover#/960372e0-3224-11e8-a572-ffca06da1357', + path: '/app/discover#/view/960372e0-3224-11e8-a572-ffca06da1357', uiCapabilitiesPath: 'discover.show', }, }); diff --git a/test/api_integration/apis/saved_objects_management/relationships.ts b/test/api_integration/apis/saved_objects_management/relationships.ts index 1db4df181e0e98..225fc5456e7436 100644 --- a/test/api_integration/apis/saved_objects_management/relationships.ts +++ b/test/api_integration/apis/saved_objects_management/relationships.ts @@ -283,7 +283,7 @@ export default function ({ getService }: FtrProviderContext) { editUrl: '/management/kibana/objects/savedSearches/960372e0-3224-11e8-a572-ffca06da1357', inAppUrl: { - path: '/app/discover#/960372e0-3224-11e8-a572-ffca06da1357', + path: '/app/discover#/view/960372e0-3224-11e8-a572-ffca06da1357', uiCapabilitiesPath: 'discover.show', }, }, @@ -323,7 +323,7 @@ export default function ({ getService }: FtrProviderContext) { editUrl: '/management/kibana/objects/savedSearches/960372e0-3224-11e8-a572-ffca06da1357', inAppUrl: { - path: '/app/discover#/960372e0-3224-11e8-a572-ffca06da1357', + path: '/app/discover#/view/960372e0-3224-11e8-a572-ffca06da1357', uiCapabilitiesPath: 'discover.show', }, }, @@ -366,7 +366,7 @@ export default function ({ getService }: FtrProviderContext) { editUrl: '/management/kibana/objects/savedSearches/960372e0-3224-11e8-a572-ffca06da1357', inAppUrl: { - path: '/app/discover#/960372e0-3224-11e8-a572-ffca06da1357', + path: '/app/discover#/view/960372e0-3224-11e8-a572-ffca06da1357', uiCapabilitiesPath: 'discover.show', }, }, @@ -406,7 +406,7 @@ export default function ({ getService }: FtrProviderContext) { editUrl: '/management/kibana/objects/savedSearches/960372e0-3224-11e8-a572-ffca06da1357', inAppUrl: { - path: '/app/discover#/960372e0-3224-11e8-a572-ffca06da1357', + path: '/app/discover#/view/960372e0-3224-11e8-a572-ffca06da1357', uiCapabilitiesPath: 'discover.show', }, }, diff --git a/test/functional/apps/bundles/index.js b/test/functional/apps/bundles/index.js index ead6412564751f..f106ea895c2e6c 100644 --- a/test/functional/apps/bundles/index.js +++ b/test/functional/apps/bundles/index.js @@ -65,7 +65,7 @@ export default function ({ getService }) { it('returns gzip files when no brotli version exists', () => supertest - .get(`/${buildNum}/bundles/commons.style.css`) // legacy optimizer does not create brotli outputs + .get(`/${buildNum}/bundles/light_theme.style.css`) // legacy optimizer does not create brotli outputs .set('Accept-Encoding', 'gzip, br') .expect(200) .expect('Content-Encoding', 'gzip')); diff --git a/test/functional/apps/dashboard/index.js b/test/functional/apps/dashboard/index.js index a8d0e03c9421e8..1e310c1ddd2684 100644 --- a/test/functional/apps/dashboard/index.js +++ b/test/functional/apps/dashboard/index.js @@ -58,6 +58,7 @@ export default function ({ getService, loadTestFile }) { loadTestFile(require.resolve('./embed_mode')); loadTestFile(require.resolve('./dashboard_back_button')); loadTestFile(require.resolve('./dashboard_error_handling')); + loadTestFile(require.resolve('./legacy_urls')); // Note: This one must be last because it unloads some data for one of its tests! // No, this isn't ideal, but loading/unloading takes so much time and these are all bunched diff --git a/test/functional/apps/dashboard/legacy_urls.ts b/test/functional/apps/dashboard/legacy_urls.ts new file mode 100644 index 00000000000000..e606649c1df9f5 --- /dev/null +++ b/test/functional/apps/dashboard/legacy_urls.ts @@ -0,0 +1,111 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { FtrProviderContext } from '../../ftr_provider_context'; + +export default function ({ getService, getPageObjects }: FtrProviderContext) { + const PageObjects = getPageObjects([ + 'dashboard', + 'header', + 'common', + 'timePicker', + 'visualize', + 'visEditor', + ]); + const pieChart = getService('pieChart'); + const browser = getService('browser'); + const find = getService('find'); + const log = getService('log'); + const dashboardAddPanel = getService('dashboardAddPanel'); + const listingTable = getService('listingTable'); + const esArchiver = getService('esArchiver'); + + let kibanaLegacyBaseUrl: string; + let kibanaVisualizeBaseUrl: string; + let testDashboardId: string; + + describe('legacy urls', function describeIndexTests() { + before(async function () { + await esArchiver.load('dashboard/current/kibana'); + await PageObjects.common.navigateToApp('dashboard'); + await PageObjects.dashboard.clickNewDashboard(); + await dashboardAddPanel.addVisualization('Rendering-Test:-animal-sounds-pie'); + await PageObjects.dashboard.saveDashboard('legacyTest', { waitDialogIsClosed: true }); + await PageObjects.header.waitUntilLoadingHasFinished(); + const currentUrl = await browser.getCurrentUrl(); + await log.debug(`Current url is ${currentUrl}`); + testDashboardId = /#\/view\/(.+)\?/.exec(currentUrl)![1]; + kibanaLegacyBaseUrl = + currentUrl.substring(0, currentUrl.indexOf('/app/dashboards')) + '/app/kibana'; + kibanaVisualizeBaseUrl = + currentUrl.substring(0, currentUrl.indexOf('/app/dashboards')) + '/app/visualize'; + await log.debug(`id is ${testDashboardId}`); + }); + + after(async function () { + await PageObjects.dashboard.gotoDashboardLandingPage(); + await listingTable.deleteItem('legacyTest', testDashboardId); + }); + + describe('kibana link redirect', () => { + it('redirects from old kibana app URL', async () => { + const url = `${kibanaLegacyBaseUrl}#/dashboard/${testDashboardId}`; + await browser.get(url, true); + await PageObjects.header.waitUntilLoadingHasFinished(); + await PageObjects.timePicker.setDefaultDataRange(); + + await PageObjects.dashboard.waitForRenderComplete(); + await pieChart.expectPieSliceCount(5); + }); + + it('redirects from legacy hash in wrong app', async () => { + const url = `${kibanaVisualizeBaseUrl}#/dashboard/${testDashboardId}`; + await browser.get(url, true); + await PageObjects.header.waitUntilLoadingHasFinished(); + await PageObjects.timePicker.setDefaultDataRange(); + + await PageObjects.dashboard.waitForRenderComplete(); + await pieChart.expectPieSliceCount(5); + }); + + it('resolves markdown link', async () => { + await PageObjects.visualize.navigateToNewVisualization(); + await PageObjects.visualize.clickMarkdownWidget(); + await PageObjects.visEditor.setMarkdownTxt(`[abc](#/dashboard/${testDashboardId})`); + await PageObjects.visEditor.clickGo(); + (await find.byLinkText('abc')).click(); + + await PageObjects.header.waitUntilLoadingHasFinished(); + await PageObjects.timePicker.setDefaultDataRange(); + + await PageObjects.dashboard.waitForRenderComplete(); + await pieChart.expectPieSliceCount(5); + }); + + it('back button works', async () => { + // back to default time range + await browser.goBack(); + // back to last app + await browser.goBack(); + await PageObjects.visEditor.expectMarkdownTextArea(); + await browser.goForward(); + }); + }); + }); +} diff --git a/test/functional/apps/discover/_shared_links.js b/test/functional/apps/discover/_shared_links.js index 38d8812fa3103b..5c6a70450a0aa1 100644 --- a/test/functional/apps/discover/_shared_links.js +++ b/test/functional/apps/discover/_shared_links.js @@ -109,7 +109,7 @@ export default function ({ getService, getPageObjects }) { const expectedUrl = baseUrl + '/app/discover#' + - '/ab12e3c0-f231-11e6-9486-733b1ac9221a' + + '/view/ab12e3c0-f231-11e6-9486-733b1ac9221a' + '?_g=(filters%3A!()%2CrefreshInterval%3A(pause%3A!t%2Cvalue%3A0)' + "%2Ctime%3A(from%3A'2015-09-19T06%3A31%3A44.000Z'%2C" + "to%3A'2015-09-23T18%3A31%3A44.000Z'))"; diff --git a/test/functional/page_objects/common_page.ts b/test/functional/page_objects/common_page.ts index d08e88ecf47ea9..236b2fb9f2f1e3 100644 --- a/test/functional/page_objects/common_page.ts +++ b/test/functional/page_objects/common_page.ts @@ -378,14 +378,12 @@ export function CommonPageProvider({ getService, getPageObjects }: FtrProviderCo async isChromeVisible() { const globalNavShown = await globalNav.exists(); - const topNavShown = await testSubjects.exists('top-nav'); - return globalNavShown && topNavShown; + return globalNavShown; } async isChromeHidden() { const globalNavShown = await globalNav.exists(); - const topNavShown = await testSubjects.exists('top-nav'); - return !globalNavShown && !topNavShown; + return !globalNavShown; } async waitForTopNavToBeVisible() { diff --git a/test/functional/page_objects/visualize_editor_page.ts b/test/functional/page_objects/visualize_editor_page.ts index c4c7c2aaffabd9..9fcb38efce0db9 100644 --- a/test/functional/page_objects/visualize_editor_page.ts +++ b/test/functional/page_objects/visualize_editor_page.ts @@ -230,6 +230,10 @@ export function VisualizeEditorPageProvider({ getService, getPageObjects }: FtrP await testSubjects.click('dropPartialBucketsCheckbox'); } + public async expectMarkdownTextArea() { + await testSubjects.existOrFail('markdownTextarea'); + } + public async setMarkdownTxt(markdownTxt: string) { const input = await testSubjects.find('markdownTextarea'); await input.clearValue(); diff --git a/test/plugin_functional/plugins/core_logging/kibana.json b/test/plugin_functional/plugins/core_logging/kibana.json new file mode 100644 index 00000000000000..3289c2c627b9a7 --- /dev/null +++ b/test/plugin_functional/plugins/core_logging/kibana.json @@ -0,0 +1,7 @@ +{ + "id": "core_logging", + "version": "0.0.1", + "kibanaVersion": "kibana", + "configPath": ["core_logging"], + "server": true +} diff --git a/test/plugin_functional/plugins/core_logging/server/.gitignore b/test/plugin_functional/plugins/core_logging/server/.gitignore new file mode 100644 index 00000000000000..9a3d2811791937 --- /dev/null +++ b/test/plugin_functional/plugins/core_logging/server/.gitignore @@ -0,0 +1 @@ +/*debug.log diff --git a/src/legacy/core_plugins/kibana/public/index.ts b/test/plugin_functional/plugins/core_logging/server/index.ts similarity index 78% rename from src/legacy/core_plugins/kibana/public/index.ts rename to test/plugin_functional/plugins/core_logging/server/index.ts index 6b1b7f0d249ff4..ca1d9da95b495c 100644 --- a/src/legacy/core_plugins/kibana/public/index.ts +++ b/test/plugin_functional/plugins/core_logging/server/index.ts @@ -17,7 +17,7 @@ * under the License. */ -export { - ProcessedImportResponse, - processImportResponse, -} from '../../../../plugins/saved_objects_management/public/lib'; // eslint-disable-line @kbn/eslint/no-restricted-paths +import type { PluginInitializerContext } from '../../../../../src/core/server'; +import { CoreLoggingPlugin } from './plugin'; + +export const plugin = (init: PluginInitializerContext) => new CoreLoggingPlugin(init); diff --git a/test/plugin_functional/plugins/core_logging/server/plugin.ts b/test/plugin_functional/plugins/core_logging/server/plugin.ts new file mode 100644 index 00000000000000..a7820a0f675250 --- /dev/null +++ b/test/plugin_functional/plugins/core_logging/server/plugin.ts @@ -0,0 +1,118 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { resolve } from 'path'; +import { Subject } from 'rxjs'; +import { schema } from '@kbn/config-schema'; +import type { + PluginInitializerContext, + Plugin, + CoreSetup, + LoggerContextConfigInput, + Logger, +} from '../../../../../src/core/server'; + +const CUSTOM_LOGGING_CONFIG: LoggerContextConfigInput = { + appenders: { + customJsonFile: { + kind: 'file', + path: resolve(__dirname, 'json_debug.log'), // use 'debug.log' suffix so file watcher does not restart server + layout: { + kind: 'json', + }, + }, + customPatternFile: { + kind: 'file', + path: resolve(__dirname, 'pattern_debug.log'), + layout: { + kind: 'pattern', + pattern: 'CUSTOM - PATTERN [%logger][%level] %message', + }, + }, + }, + + loggers: [ + { context: 'debug_json', appenders: ['customJsonFile'], level: 'debug' }, + { context: 'debug_pattern', appenders: ['customPatternFile'], level: 'debug' }, + { context: 'info_json', appenders: ['customJsonFile'], level: 'info' }, + { context: 'info_pattern', appenders: ['customPatternFile'], level: 'info' }, + { context: 'all', appenders: ['customJsonFile', 'customPatternFile'], level: 'debug' }, + ], +}; + +export class CoreLoggingPlugin implements Plugin { + private readonly logger: Logger; + + constructor(init: PluginInitializerContext) { + this.logger = init.logger.get(); + } + + public setup(core: CoreSetup) { + const loggingConfig$ = new Subject(); + core.logging.configure(loggingConfig$); + + const router = core.http.createRouter(); + + // Expose a route that allows our test suite to write logs as this plugin + router.post( + { + path: '/internal/core-logging/write-log', + validate: { + body: schema.object({ + level: schema.oneOf([schema.literal('debug'), schema.literal('info')]), + message: schema.string(), + context: schema.arrayOf(schema.string()), + }), + }, + }, + (ctx, req, res) => { + const { level, message, context } = req.body; + const logger = this.logger.get(...context); + + if (level === 'debug') { + logger.debug(message); + } else if (level === 'info') { + logger.info(message); + } + + return res.ok(); + } + ); + + // Expose a route to toggle on and off the custom config + router.post( + { + path: '/internal/core-logging/update-config', + validate: { body: schema.object({ enableCustomConfig: schema.boolean() }) }, + }, + (ctx, req, res) => { + if (req.body.enableCustomConfig) { + loggingConfig$.next(CUSTOM_LOGGING_CONFIG); + } else { + loggingConfig$.next({}); + } + + return res.ok({ body: `Updated config: ${req.body.enableCustomConfig}` }); + } + ); + } + + public start() {} + public stop() {} +} diff --git a/test/plugin_functional/plugins/core_logging/tsconfig.json b/test/plugin_functional/plugins/core_logging/tsconfig.json new file mode 100644 index 00000000000000..7389eb6ce159be --- /dev/null +++ b/test/plugin_functional/plugins/core_logging/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "../../../../tsconfig.json", + "compilerOptions": { + "outDir": "./target", + "skipLibCheck": true + }, + "include": [ + "index.ts", + "server/**/*.ts", + "../../../../typings/**/*", + ], + "exclude": [] +} diff --git a/test/plugin_functional/test_suites/core_plugins/index.ts b/test/plugin_functional/test_suites/core_plugins/index.ts index 8f54ec6c0f4cd9..8f7c2267d34b44 100644 --- a/test/plugin_functional/test_suites/core_plugins/index.ts +++ b/test/plugin_functional/test_suites/core_plugins/index.ts @@ -30,5 +30,6 @@ export default function ({ loadTestFile }: PluginFunctionalProviderContext) { loadTestFile(require.resolve('./application_leave_confirm')); loadTestFile(require.resolve('./application_status')); loadTestFile(require.resolve('./rendering')); + loadTestFile(require.resolve('./logging')); }); } diff --git a/test/plugin_functional/test_suites/core_plugins/logging.ts b/test/plugin_functional/test_suites/core_plugins/logging.ts new file mode 100644 index 00000000000000..9fdaa6ce834ea5 --- /dev/null +++ b/test/plugin_functional/test_suites/core_plugins/logging.ts @@ -0,0 +1,146 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { resolve } from 'path'; +import fs from 'fs'; +import expect from '@kbn/expect'; +import { PluginFunctionalProviderContext } from '../../services'; + +// eslint-disable-next-line import/no-default-export +export default function ({ getService }: PluginFunctionalProviderContext) { + const supertest = getService('supertest'); + + describe('plugin logging', function describeIndexTests() { + const LOG_FILE_DIRECTORY = resolve(__dirname, '..', '..', 'plugins', 'core_logging', 'server'); + const JSON_FILE_PATH = resolve(LOG_FILE_DIRECTORY, 'json_debug.log'); + const PATTERN_FILE_PATH = resolve(LOG_FILE_DIRECTORY, 'pattern_debug.log'); + + beforeEach(async () => { + // "touch" each file to ensure it exists and is empty before each test + await fs.promises.writeFile(JSON_FILE_PATH, ''); + await fs.promises.writeFile(PATTERN_FILE_PATH, ''); + }); + + async function readLines(path: string) { + const contents = await fs.promises.readFile(path, { encoding: 'utf8' }); + return contents.trim().split('\n'); + } + + async function readJsonLines() { + return (await readLines(JSON_FILE_PATH)) + .filter((line) => line.length > 0) + .map((line) => JSON.parse(line)) + .map(({ level, message, context }) => ({ level, message, context })); + } + + function writeLog(context: string[], level: string, message: string) { + return supertest + .post('/internal/core-logging/write-log') + .set('kbn-xsrf', 'anything') + .send({ context, level, message }) + .expect(200); + } + + function setContextConfig(enable: boolean) { + return supertest + .post('/internal/core-logging/update-config') + .set('kbn-xsrf', 'anything') + .send({ enableCustomConfig: enable }) + .expect(200); + } + + it('does not write to custom appenders when not configured', async () => { + await setContextConfig(false); + await writeLog(['debug_json'], 'info', 'i go to the default appender!'); + expect(await readJsonLines()).to.eql([]); + }); + + it('writes debug_json context to custom JSON appender', async () => { + await setContextConfig(true); + await writeLog(['debug_json'], 'debug', 'log1'); + await writeLog(['debug_json'], 'info', 'log2'); + expect(await readJsonLines()).to.eql([ + { + level: 'DEBUG', + context: 'plugins.core_logging.debug_json', + message: 'log1', + }, + { + level: 'INFO', + context: 'plugins.core_logging.debug_json', + message: 'log2', + }, + ]); + }); + + it('writes info_json context to custom JSON appender', async () => { + await setContextConfig(true); + await writeLog(['info_json'], 'debug', 'i should not be logged!'); + await writeLog(['info_json'], 'info', 'log2'); + expect(await readJsonLines()).to.eql([ + { + level: 'INFO', + context: 'plugins.core_logging.info_json', + message: 'log2', + }, + ]); + }); + + it('writes debug_pattern context to custom pattern appender', async () => { + await setContextConfig(true); + await writeLog(['debug_pattern'], 'debug', 'log1'); + await writeLog(['debug_pattern'], 'info', 'log2'); + expect(await readLines(PATTERN_FILE_PATH)).to.eql([ + 'CUSTOM - PATTERN [plugins.core_logging.debug_pattern][DEBUG] log1', + 'CUSTOM - PATTERN [plugins.core_logging.debug_pattern][INFO ] log2', + ]); + }); + + it('writes info_pattern context to custom pattern appender', async () => { + await setContextConfig(true); + await writeLog(['info_pattern'], 'debug', 'i should not be logged!'); + await writeLog(['info_pattern'], 'info', 'log2'); + expect(await readLines(PATTERN_FILE_PATH)).to.eql([ + 'CUSTOM - PATTERN [plugins.core_logging.info_pattern][INFO ] log2', + ]); + }); + + it('writes all context to both appenders', async () => { + await setContextConfig(true); + await writeLog(['all'], 'debug', 'log1'); + await writeLog(['all'], 'info', 'log2'); + expect(await readJsonLines()).to.eql([ + { + level: 'DEBUG', + context: 'plugins.core_logging.all', + message: 'log1', + }, + { + level: 'INFO', + context: 'plugins.core_logging.all', + message: 'log2', + }, + ]); + expect(await readLines(PATTERN_FILE_PATH)).to.eql([ + 'CUSTOM - PATTERN [plugins.core_logging.all][DEBUG] log1', + 'CUSTOM - PATTERN [plugins.core_logging.all][INFO ] log2', + ]); + }); + }); +} diff --git a/test/scripts/jenkins_security_solution_cypress.sh b/test/scripts/jenkins_security_solution_cypress.sh index 23b83cf946d491..8aa3425be0beb2 100644 --- a/test/scripts/jenkins_security_solution_cypress.sh +++ b/test/scripts/jenkins_security_solution_cypress.sh @@ -11,11 +11,16 @@ export KIBANA_INSTALL_DIR="$destDir" echo " -> Running security solution cypress tests" cd "$XPACK_DIR" -checks-reporter-with-killswitch "Security solution Cypress Tests" \ - node scripts/functional_tests \ - --debug --bail \ - --kibana-install-dir "$KIBANA_INSTALL_DIR" \ - --config test/security_solution_cypress/config.ts +# Failures across multiple suites, skipping all +# https://github.com/elastic/kibana/issues/69847 +# https://github.com/elastic/kibana/issues/69848 +# https://github.com/elastic/kibana/issues/69849 + +# checks-reporter-with-killswitch "Security solution Cypress Tests" \ +# node scripts/functional_tests \ +# --debug --bail \ +# --kibana-install-dir "$KIBANA_INSTALL_DIR" \ +# --config test/security_solution_cypress/config.ts echo "" echo "" diff --git a/tsconfig.types.json b/tsconfig.types.json index fd3624dd8e31bf..2f5919e413e514 100644 --- a/tsconfig.types.json +++ b/tsconfig.types.json @@ -10,6 +10,8 @@ "include": [ "src/core/server/index.ts", "src/core/public/index.ts", + "src/plugins/data/server/index.ts", + "src/plugins/data/public/index.ts", "typings" ] } diff --git a/x-pack/.i18nrc.json b/x-pack/.i18nrc.json index 36cfdf904d6d43..596ba17d343c0c 100644 --- a/x-pack/.i18nrc.json +++ b/x-pack/.i18nrc.json @@ -8,6 +8,7 @@ "xpack.apm": ["legacy/plugins/apm", "plugins/apm"], "xpack.beatsManagement": ["legacy/plugins/beats_management", "plugins/beats_management"], "xpack.canvas": "plugins/canvas", + "xpack.cloud": "plugins/cloud", "xpack.dashboard": "plugins/dashboard_enhanced", "xpack.discover": "plugins/discover_enhanced", "xpack.crossClusterReplication": "plugins/cross_cluster_replication", diff --git a/x-pack/dev-tools/jest/setup/polyfills.js b/x-pack/dev-tools/jest/setup/polyfills.js index 5ecee2e3ad0d35..822802f3dacb79 100644 --- a/x-pack/dev-tools/jest/setup/polyfills.js +++ b/x-pack/dev-tools/jest/setup/polyfills.js @@ -17,5 +17,7 @@ const MutationObserver = require('mutation-observer'); Object.defineProperty(window, 'MutationObserver', { value: MutationObserver }); require('whatwg-fetch'); -const URL = { createObjectURL: () => '' }; -Object.defineProperty(window, 'URL', { value: URL }); + +if (!global.URL.hasOwnProperty('createObjectURL')) { + Object.defineProperty(global.URL, 'createObjectURL', { value: () => '' }); +} diff --git a/x-pack/plugins/actions/server/builtin_action_types/index.test.ts b/x-pack/plugins/actions/server/builtin_action_types/index.test.ts index 459ffd7667f155..21efc05d49c38a 100644 --- a/x-pack/plugins/actions/server/builtin_action_types/index.test.ts +++ b/x-pack/plugins/actions/server/builtin_action_types/index.test.ts @@ -9,7 +9,7 @@ import { ActionTypeRegistry } from '../action_type_registry'; import { taskManagerMock } from '../../../task_manager/server/task_manager.mock'; import { registerBuiltInActionTypes } from './index'; import { Logger } from '../../../../../src/core/server'; -import { loggingServiceMock } from '../../../../../src/core/server/mocks'; +import { loggingSystemMock } from '../../../../../src/core/server/mocks'; import { actionsConfigMock } from '../actions_config.mock'; import { licenseStateMock } from '../lib/license_state.mock'; @@ -19,7 +19,7 @@ export function createActionTypeRegistry(): { logger: jest.Mocked; actionTypeRegistry: ActionTypeRegistry; } { - const logger = loggingServiceMock.create().get() as jest.Mocked; + const logger = loggingSystemMock.create().get() as jest.Mocked; const actionTypeRegistry = new ActionTypeRegistry({ taskManager: taskManagerMock.setup(), taskRunnerFactory: new TaskRunnerFactory( diff --git a/x-pack/plugins/actions/server/builtin_action_types/lib/send_email.test.ts b/x-pack/plugins/actions/server/builtin_action_types/lib/send_email.test.ts index a02f79a49e8e24..3514bd4257b0f6 100644 --- a/x-pack/plugins/actions/server/builtin_action_types/lib/send_email.test.ts +++ b/x-pack/plugins/actions/server/builtin_action_types/lib/send_email.test.ts @@ -10,14 +10,14 @@ jest.mock('nodemailer', () => ({ import { Logger } from '../../../../../../src/core/server'; import { sendEmail } from './send_email'; -import { loggingServiceMock } from '../../../../../../src/core/server/mocks'; +import { loggingSystemMock } from '../../../../../../src/core/server/mocks'; import nodemailer from 'nodemailer'; const createTransportMock = nodemailer.createTransport as jest.Mock; const sendMailMockResult = { result: 'does not matter' }; const sendMailMock = jest.fn(); -const mockLogger = loggingServiceMock.create().get() as jest.Mocked; +const mockLogger = loggingSystemMock.create().get() as jest.Mocked; describe('send_email module', () => { beforeEach(() => { diff --git a/x-pack/plugins/actions/server/lib/action_executor.test.ts b/x-pack/plugins/actions/server/lib/action_executor.test.ts index 88216c4bf13adc..c8e6669275e117 100644 --- a/x-pack/plugins/actions/server/lib/action_executor.test.ts +++ b/x-pack/plugins/actions/server/lib/action_executor.test.ts @@ -9,7 +9,7 @@ import { schema } from '@kbn/config-schema'; import { ActionExecutor } from './action_executor'; import { actionTypeRegistryMock } from '../action_type_registry.mock'; import { encryptedSavedObjectsMock } from '../../../encrypted_saved_objects/server/mocks'; -import { loggingServiceMock, savedObjectsClientMock } from '../../../../../src/core/server/mocks'; +import { loggingSystemMock, savedObjectsClientMock } from '../../../../../src/core/server/mocks'; import { eventLoggerMock } from '../../../event_log/server/mocks'; import { spacesServiceMock } from '../../../spaces/server/spaces_service/spaces_service.mock'; import { ActionType } from '../types'; @@ -31,7 +31,7 @@ const executeParams = { const spacesMock = spacesServiceMock.createSetupContract(); actionExecutor.initialize({ - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), spaces: spacesMock, getServices: () => services, getScopedSavedObjectsClient: () => savedObjectsClientWithHidden, @@ -266,7 +266,7 @@ test('should not throws an error if actionType is preconfigured', async () => { test('throws an error when passing isESOUsingEphemeralEncryptionKey with value of true', async () => { const customActionExecutor = new ActionExecutor({ isESOUsingEphemeralEncryptionKey: true }); customActionExecutor.initialize({ - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), spaces: spacesMock, getScopedSavedObjectsClient: () => savedObjectsClientWithHidden, getServices: () => services, diff --git a/x-pack/plugins/actions/server/lib/task_runner_factory.test.ts b/x-pack/plugins/actions/server/lib/task_runner_factory.test.ts index 3339416a1112d1..06cb84ad79a891 100644 --- a/x-pack/plugins/actions/server/lib/task_runner_factory.test.ts +++ b/x-pack/plugins/actions/server/lib/task_runner_factory.test.ts @@ -12,7 +12,7 @@ import { TaskRunnerFactory } from './task_runner_factory'; import { actionTypeRegistryMock } from '../action_type_registry.mock'; import { actionExecutorMock } from './action_executor.mock'; import { encryptedSavedObjectsMock } from '../../../encrypted_saved_objects/server/mocks'; -import { savedObjectsClientMock, loggingServiceMock } from 'src/core/server/mocks'; +import { savedObjectsClientMock, loggingSystemMock } from 'src/core/server/mocks'; import { eventLoggerMock } from '../../../event_log/server/mocks'; import { ActionTypeDisabledError } from './errors'; @@ -56,7 +56,7 @@ const services = { savedObjectsClient: savedObjectsClientMock.create(), }; const actionExecutorInitializerParams = { - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), getServices: jest.fn().mockReturnValue(services), actionTypeRegistry, getScopedSavedObjectsClient: () => savedObjectsClientMock.create(), @@ -67,7 +67,7 @@ const actionExecutorInitializerParams = { const taskRunnerFactoryInitializerParams = { spaceIdToNamespace, actionTypeRegistry, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), encryptedSavedObjectsClient: mockedEncryptedSavedObjectsClient, getBasePath: jest.fn().mockReturnValue(undefined), getScopedSavedObjectsClient: jest.fn().mockReturnValue(services.savedObjectsClient), diff --git a/x-pack/plugins/alerting_builtins/server/alert_types/index_threshold/alert_type.test.ts b/x-pack/plugins/alerting_builtins/server/alert_types/index_threshold/alert_type.test.ts index 315e4800d4c733..d3583fd4cdb0b5 100644 --- a/x-pack/plugins/alerting_builtins/server/alert_types/index_threshold/alert_type.test.ts +++ b/x-pack/plugins/alerting_builtins/server/alert_types/index_threshold/alert_type.test.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { loggingServiceMock } from '../../../../../../src/core/server/mocks'; +import { loggingSystemMock } from '../../../../../../src/core/server/mocks'; import { getAlertType } from './alert_type'; import { Params } from './alert_type_params'; @@ -13,7 +13,7 @@ describe('alertType', () => { indexThreshold: { timeSeriesQuery: jest.fn(), }, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), }; const alertType = getAlertType(service); diff --git a/x-pack/plugins/alerting_builtins/server/alert_types/index_threshold/lib/time_series_query.test.ts b/x-pack/plugins/alerting_builtins/server/alert_types/index_threshold/lib/time_series_query.test.ts index d40df4c91998ff..0565a8634fc71c 100644 --- a/x-pack/plugins/alerting_builtins/server/alert_types/index_threshold/lib/time_series_query.test.ts +++ b/x-pack/plugins/alerting_builtins/server/alert_types/index_threshold/lib/time_series_query.test.ts @@ -6,7 +6,7 @@ // test error conditions of calling timeSeriesQuery - postive results tested in FT -import { loggingServiceMock } from '../../../../../../../src/core/server/mocks'; +import { loggingSystemMock } from '../../../../../../../src/core/server/mocks'; import { coreMock } from '../../../../../../../src/core/server/mocks'; import { AlertingBuiltinsPlugin } from '../../../plugin'; import { TimeSeriesQueryParameters, TimeSeriesResult, TimeSeriesQuery } from './time_series_query'; @@ -44,7 +44,7 @@ describe('timeSeriesQuery', () => { mockCallCluster.mockReset(); params = { - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), callCluster: mockCallCluster, query: DefaultQueryParams, }; diff --git a/x-pack/plugins/alerts/server/alerts_client.test.ts b/x-pack/plugins/alerts/server/alerts_client.test.ts index f494f1358980d1..d69d04f71ce9ec 100644 --- a/x-pack/plugins/alerts/server/alerts_client.test.ts +++ b/x-pack/plugins/alerts/server/alerts_client.test.ts @@ -6,7 +6,7 @@ import uuid from 'uuid'; import { schema } from '@kbn/config-schema'; import { AlertsClient, CreateOptions } from './alerts_client'; -import { savedObjectsClientMock, loggingServiceMock } from '../../../../src/core/server/mocks'; +import { savedObjectsClientMock, loggingSystemMock } from '../../../../src/core/server/mocks'; import { taskManagerMock } from '../../task_manager/server/task_manager.mock'; import { alertTypeRegistryMock } from './alert_type_registry.mock'; import { TaskStatus } from '../../task_manager/server'; @@ -29,7 +29,7 @@ const alertsClientParams = { getUserName: jest.fn(), createAPIKey: jest.fn(), invalidateAPIKey: jest.fn(), - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), encryptedSavedObjectsClient: encryptedSavedObjects, getActionsClient: jest.fn(), }; diff --git a/x-pack/plugins/alerts/server/alerts_client_factory.test.ts b/x-pack/plugins/alerts/server/alerts_client_factory.test.ts index a2d64c94ce007c..128d54c10b66a4 100644 --- a/x-pack/plugins/alerts/server/alerts_client_factory.test.ts +++ b/x-pack/plugins/alerts/server/alerts_client_factory.test.ts @@ -9,7 +9,7 @@ import { AlertsClientFactory, AlertsClientFactoryOpts } from './alerts_client_fa import { alertTypeRegistryMock } from './alert_type_registry.mock'; import { taskManagerMock } from '../../task_manager/server/task_manager.mock'; import { KibanaRequest } from '../../../../src/core/server'; -import { loggingServiceMock, savedObjectsClientMock } from '../../../../src/core/server/mocks'; +import { loggingSystemMock, savedObjectsClientMock } from '../../../../src/core/server/mocks'; import { encryptedSavedObjectsMock } from '../../encrypted_saved_objects/server/mocks'; import { AuthenticatedUser } from '../../../plugins/security/common/model'; import { securityMock } from '../../security/server/mocks'; @@ -20,7 +20,7 @@ jest.mock('./alerts_client'); const savedObjectsClient = savedObjectsClientMock.create(); const securityPluginSetup = securityMock.createSetup(); const alertsClientFactoryParams: jest.Mocked = { - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), taskManager: taskManagerMock.start(), alertTypeRegistry: alertTypeRegistryMock.create(), getSpaceId: jest.fn(), diff --git a/x-pack/plugins/alerts/server/task_runner/create_execution_handler.test.ts b/x-pack/plugins/alerts/server/task_runner/create_execution_handler.test.ts index dd5a9f531bd58b..3b1948c5e7ad7d 100644 --- a/x-pack/plugins/alerts/server/task_runner/create_execution_handler.test.ts +++ b/x-pack/plugins/alerts/server/task_runner/create_execution_handler.test.ts @@ -6,7 +6,7 @@ import { AlertType } from '../types'; import { createExecutionHandler } from './create_execution_handler'; -import { loggingServiceMock } from '../../../../../src/core/server/mocks'; +import { loggingSystemMock } from '../../../../../src/core/server/mocks'; import { actionsMock, actionsClientMock } from '../../../actions/server/mocks'; import { eventLoggerMock } from '../../../event_log/server/event_logger.mock'; import { KibanaRequest } from 'kibana/server'; @@ -34,7 +34,7 @@ const createExecutionHandlerParams = { spaceIdToNamespace: jest.fn().mockReturnValue(undefined), getBasePath: jest.fn().mockReturnValue(undefined), alertType, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), eventLogger: eventLoggerMock.create(), actions: [ { diff --git a/x-pack/plugins/alerts/server/task_runner/task_runner.test.ts b/x-pack/plugins/alerts/server/task_runner/task_runner.test.ts index 690971bc870062..7a031c6671fd07 100644 --- a/x-pack/plugins/alerts/server/task_runner/task_runner.test.ts +++ b/x-pack/plugins/alerts/server/task_runner/task_runner.test.ts @@ -11,7 +11,7 @@ import { ConcreteTaskInstance, TaskStatus } from '../../../task_manager/server'; import { TaskRunnerContext } from './task_runner_factory'; import { TaskRunner } from './task_runner'; import { encryptedSavedObjectsMock } from '../../../encrypted_saved_objects/server/mocks'; -import { loggingServiceMock } from '../../../../../src/core/server/mocks'; +import { loggingSystemMock } from '../../../../../src/core/server/mocks'; import { PluginStartContract as ActionsPluginStart } from '../../../actions/server'; import { actionsMock, actionsClientMock } from '../../../actions/server/mocks'; import { alertsMock } from '../mocks'; @@ -66,7 +66,7 @@ describe('Task Runner', () => { getServices: jest.fn().mockReturnValue(services), actionsPlugin: actionsMock.createStart(), encryptedSavedObjectsClient, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), spaceIdToNamespace: jest.fn().mockReturnValue(undefined), getBasePath: jest.fn().mockReturnValue(undefined), eventLogger: eventLoggerMock.create(), diff --git a/x-pack/plugins/alerts/server/task_runner/task_runner_factory.test.ts b/x-pack/plugins/alerts/server/task_runner/task_runner_factory.test.ts index 7d9710d8a3e082..8f3e44b1cf42df 100644 --- a/x-pack/plugins/alerts/server/task_runner/task_runner_factory.test.ts +++ b/x-pack/plugins/alerts/server/task_runner/task_runner_factory.test.ts @@ -8,7 +8,7 @@ import sinon from 'sinon'; import { ConcreteTaskInstance, TaskStatus } from '../../../task_manager/server'; import { TaskRunnerContext, TaskRunnerFactory } from './task_runner_factory'; import { encryptedSavedObjectsMock } from '../../../encrypted_saved_objects/server/mocks'; -import { loggingServiceMock } from '../../../../../src/core/server/mocks'; +import { loggingSystemMock } from '../../../../../src/core/server/mocks'; import { actionsMock } from '../../../actions/server/mocks'; import { alertsMock } from '../mocks'; import { eventLoggerMock } from '../../../event_log/server/event_logger.mock'; @@ -57,7 +57,7 @@ describe('Task Runner Factory', () => { getServices: jest.fn().mockReturnValue(services), actionsPlugin: actionsMock.createStart(), encryptedSavedObjectsClient: encryptedSavedObjectsPlugin.getClient(), - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), spaceIdToNamespace: jest.fn().mockReturnValue(undefined), getBasePath: jest.fn().mockReturnValue(undefined), eventLogger: eventLoggerMock.create(), diff --git a/x-pack/plugins/apm/common/ml_job_constants.test.ts b/x-pack/plugins/apm/common/ml_job_constants.test.ts index 45bb7133e852ef..96e3ba826d2010 100644 --- a/x-pack/plugins/apm/common/ml_job_constants.test.ts +++ b/x-pack/plugins/apm/common/ml_job_constants.test.ts @@ -4,45 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { - getMlJobId, - getMlPrefix, - getMlJobServiceName, - getSeverity, - severity, -} from './ml_job_constants'; +import { getSeverity, severity } from './ml_job_constants'; describe('ml_job_constants', () => { - it('getMlPrefix', () => { - expect(getMlPrefix('myServiceName')).toBe('myservicename-'); - expect(getMlPrefix('myServiceName', 'myTransactionType')).toBe( - 'myservicename-mytransactiontype-' - ); - }); - - it('getMlJobId', () => { - expect(getMlJobId('myServiceName')).toBe( - 'myservicename-high_mean_response_time' - ); - expect(getMlJobId('myServiceName', 'myTransactionType')).toBe( - 'myservicename-mytransactiontype-high_mean_response_time' - ); - expect(getMlJobId('my service name')).toBe( - 'my_service_name-high_mean_response_time' - ); - expect(getMlJobId('my service name', 'my transaction type')).toBe( - 'my_service_name-my_transaction_type-high_mean_response_time' - ); - }); - - describe('getMlJobServiceName', () => { - it('extracts the service name from a job id', () => { - expect( - getMlJobServiceName('opbeans-node-request-high_mean_response_time') - ).toEqual('opbeans-node'); - }); - }); - describe('getSeverity', () => { describe('when score is undefined', () => { it('returns undefined', () => { diff --git a/x-pack/plugins/apm/common/ml_job_constants.ts b/x-pack/plugins/apm/common/ml_job_constants.ts index f9b0119d8a107e..b8c2546bd0c84a 100644 --- a/x-pack/plugins/apm/common/ml_job_constants.ts +++ b/x-pack/plugins/apm/common/ml_job_constants.ts @@ -11,25 +11,6 @@ export enum severity { warning = 'warning', } -export const APM_ML_JOB_GROUP_NAME = 'apm'; - -export function getMlPrefix(serviceName: string, transactionType?: string) { - const maybeTransactionType = transactionType ? `${transactionType}-` : ''; - return encodeForMlApi(`${serviceName}-${maybeTransactionType}`); -} - -export function getMlJobId(serviceName: string, transactionType?: string) { - return `${getMlPrefix(serviceName, transactionType)}high_mean_response_time`; -} - -export function getMlJobServiceName(jobId: string) { - return jobId.split('-').slice(0, -2).join('-'); -} - -export function encodeForMlApi(value: string) { - return value.replace(/\s+/g, '_').toLowerCase(); -} - export function getSeverity(score?: number) { if (typeof score !== 'number') { return undefined; diff --git a/x-pack/plugins/apm/common/service_map.ts b/x-pack/plugins/apm/common/service_map.ts index 7d7a7811eeba2c..43f3585d0ebb2e 100644 --- a/x-pack/plugins/apm/common/service_map.ts +++ b/x-pack/plugins/apm/common/service_map.ts @@ -34,16 +34,6 @@ export interface Connection { destination: ConnectionNode; } -export interface ServiceAnomaly { - anomaly_score: number; - anomaly_severity: string; - actual_value: number; - typical_value: number; - ml_job_id: string; -} - -export type ServiceNode = ConnectionNode & Partial; - export interface ServiceNodeMetrics { avgMemoryUsage: number | null; avgCpuUsage: number | null; diff --git a/x-pack/plugins/apm/public/components/app/ServiceDetails/ServiceIntegrations/MachineLearningFlyout/TransactionSelect.tsx b/x-pack/plugins/apm/public/components/app/ServiceDetails/ServiceIntegrations/MachineLearningFlyout/TransactionSelect.tsx deleted file mode 100644 index 42f7246b6ea359..00000000000000 --- a/x-pack/plugins/apm/public/components/app/ServiceDetails/ServiceIntegrations/MachineLearningFlyout/TransactionSelect.tsx +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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 { - EuiFlexGroup, - EuiFlexItem, - EuiFormRow, - EuiSuperSelect, - EuiText, -} from '@elastic/eui'; -import { i18n } from '@kbn/i18n'; -import React from 'react'; - -interface TransactionSelectProps { - transactionTypes: string[]; - onChange: (value: string) => void; - selectedTransactionType: string; -} - -export function TransactionSelect({ - transactionTypes, - onChange, - selectedTransactionType, -}: TransactionSelectProps) { - return ( - - { - return { - value: transactionType, - inputDisplay: transactionType, - dropdownDisplay: ( - - - {transactionType} - - - ), - }; - })} - /> - - ); -} diff --git a/x-pack/plugins/apm/public/components/app/ServiceDetails/ServiceIntegrations/MachineLearningFlyout/index.tsx b/x-pack/plugins/apm/public/components/app/ServiceDetails/ServiceIntegrations/MachineLearningFlyout/index.tsx deleted file mode 100644 index 91778b2940c6b5..00000000000000 --- a/x-pack/plugins/apm/public/components/app/ServiceDetails/ServiceIntegrations/MachineLearningFlyout/index.tsx +++ /dev/null @@ -1,167 +0,0 @@ -/* - * 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 { i18n } from '@kbn/i18n'; -import React, { Component } from 'react'; -import { toMountPoint } from '../../../../../../../../../src/plugins/kibana_react/public'; -import { startMLJob, MLError } from '../../../../../services/rest/ml'; -import { IUrlParams } from '../../../../../context/UrlParamsContext/types'; -import { MLJobLink } from '../../../../shared/Links/MachineLearningLinks/MLJobLink'; -import { MachineLearningFlyoutView } from './view'; -import { ApmPluginContext } from '../../../../../context/ApmPluginContext'; - -interface Props { - isOpen: boolean; - onClose: () => void; - urlParams: IUrlParams; -} - -interface State { - isCreatingJob: boolean; -} - -export class MachineLearningFlyout extends Component { - static contextType = ApmPluginContext; - - public state: State = { - isCreatingJob: false, - }; - - public onClickCreate = async ({ - transactionType, - }: { - transactionType: string; - }) => { - this.setState({ isCreatingJob: true }); - try { - const { http } = this.context.core; - const { serviceName } = this.props.urlParams; - if (!serviceName) { - throw new Error('Service name is required to create this ML job'); - } - const res = await startMLJob({ http, serviceName, transactionType }); - const didSucceed = res.datafeeds[0].success && res.jobs[0].success; - if (!didSucceed) { - throw new Error('Creating ML job failed'); - } - this.addSuccessToast({ transactionType }); - } catch (e) { - this.addErrorToast(e as MLError); - } - - this.setState({ isCreatingJob: false }); - this.props.onClose(); - }; - - public addErrorToast = (error: MLError) => { - const { core } = this.context; - - const { urlParams } = this.props; - const { serviceName } = urlParams; - - if (!serviceName) { - return; - } - - const errorDescription = error?.body?.message; - const errorText = errorDescription - ? `${error.message}: ${errorDescription}` - : error.message; - - core.notifications.toasts.addWarning({ - title: i18n.translate( - 'xpack.apm.serviceDetails.enableAnomalyDetectionPanel.jobCreationFailedNotificationTitle', - { - defaultMessage: 'Job creation failed', - } - ), - text: toMountPoint( - <> -

{errorText}

-

- {i18n.translate( - 'xpack.apm.serviceDetails.enableAnomalyDetectionPanel.jobCreationFailedNotificationText', - { - defaultMessage: - 'Your current license may not allow for creating machine learning jobs, or this job may already exist.', - } - )} -

- - ), - }); - }; - - public addSuccessToast = ({ - transactionType, - }: { - transactionType: string; - }) => { - const { core } = this.context; - const { urlParams } = this.props; - const { serviceName } = urlParams; - - if (!serviceName) { - return; - } - - core.notifications.toasts.addSuccess({ - title: i18n.translate( - 'xpack.apm.serviceDetails.enableAnomalyDetectionPanel.jobCreatedNotificationTitle', - { - defaultMessage: 'Job successfully created', - } - ), - text: toMountPoint( -

- {i18n.translate( - 'xpack.apm.serviceDetails.enableAnomalyDetectionPanel.jobCreatedNotificationText', - { - defaultMessage: - 'The analysis is now running for {serviceName} ({transactionType}). It might take a while before results are added to the response times graph.', - values: { - serviceName, - transactionType, - }, - } - )}{' '} - - - {i18n.translate( - 'xpack.apm.serviceDetails.enableAnomalyDetectionPanel.jobCreatedNotificationText.viewJobLinkText', - { - defaultMessage: 'View job', - } - )} - - -

- ), - }); - }; - - public render() { - const { isOpen, onClose, urlParams } = this.props; - const { serviceName } = urlParams; - const { isCreatingJob } = this.state; - - if (!isOpen || !serviceName) { - return null; - } - - return ( - - ); - } -} diff --git a/x-pack/plugins/apm/public/components/app/ServiceDetails/ServiceIntegrations/MachineLearningFlyout/view.tsx b/x-pack/plugins/apm/public/components/app/ServiceDetails/ServiceIntegrations/MachineLearningFlyout/view.tsx deleted file mode 100644 index 72e8193ba2de27..00000000000000 --- a/x-pack/plugins/apm/public/components/app/ServiceDetails/ServiceIntegrations/MachineLearningFlyout/view.tsx +++ /dev/null @@ -1,264 +0,0 @@ -/* - * 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 { - EuiButton, - EuiCallOut, - EuiFlexGroup, - EuiFlexItem, - EuiFlyout, - EuiFlyoutBody, - EuiFlyoutFooter, - EuiFlyoutHeader, - EuiFormRow, - EuiSpacer, - EuiText, - EuiTitle, -} from '@elastic/eui'; -import { i18n } from '@kbn/i18n'; -import { FormattedMessage } from '@kbn/i18n/react'; -import React, { useState, useEffect } from 'react'; -import { isEmpty } from 'lodash'; -import { FETCH_STATUS, useFetcher } from '../../../../../hooks/useFetcher'; -import { getHasMLJob } from '../../../../../services/rest/ml'; -import { MLJobLink } from '../../../../shared/Links/MachineLearningLinks/MLJobLink'; -import { MLLink } from '../../../../shared/Links/MachineLearningLinks/MLLink'; -import { TransactionSelect } from './TransactionSelect'; -import { IUrlParams } from '../../../../../context/UrlParamsContext/types'; -import { useServiceTransactionTypes } from '../../../../../hooks/useServiceTransactionTypes'; -import { useApmPluginContext } from '../../../../../hooks/useApmPluginContext'; - -interface Props { - isCreatingJob: boolean; - onClickCreate: ({ transactionType }: { transactionType: string }) => void; - onClose: () => void; - urlParams: IUrlParams; -} - -export function MachineLearningFlyoutView({ - isCreatingJob, - onClickCreate, - onClose, - urlParams, -}: Props) { - const { serviceName } = urlParams; - const transactionTypes = useServiceTransactionTypes(urlParams); - - const [selectedTransactionType, setSelectedTransactionType] = useState< - string | undefined - >(undefined); - - const { http } = useApmPluginContext().core; - - const { data: hasMLJob, status } = useFetcher( - () => { - if (serviceName && selectedTransactionType) { - return getHasMLJob({ - serviceName, - transactionType: selectedTransactionType, - http, - }); - } - }, - [serviceName, selectedTransactionType, http], - { showToastOnError: false } - ); - - // update selectedTransactionType when list of transaction types has loaded - useEffect(() => { - setSelectedTransactionType(transactionTypes[0]); - }, [transactionTypes]); - - if (!serviceName || !selectedTransactionType || isEmpty(transactionTypes)) { - return null; - } - - const isLoadingMLJob = status === FETCH_STATUS.LOADING; - const isMlAvailable = status !== FETCH_STATUS.FAILURE; - - return ( - - - -

- {i18n.translate( - 'xpack.apm.serviceDetails.enableAnomalyDetectionPanel.enableAnomalyDetectionTitle', - { - defaultMessage: 'Enable anomaly detection', - } - )} -

-
- -
- - {!isMlAvailable && ( -
- -

- {i18n.translate( - 'xpack.apm.serviceDetails.enableAnomalyDetectionPanel.callout.mlNotAvailableDescription', - { - defaultMessage: - 'Unable to connect to Machine learning. Make sure it is enabled in Kibana to use anomaly detection.', - } - )} -

-
- -
- )} - {hasMLJob && ( -
- -

- {i18n.translate( - 'xpack.apm.serviceDetails.enableAnomalyDetectionPanel.callout.jobExistsDescription', - { - defaultMessage: - 'There is currently a job running for {serviceName} ({transactionType}).', - values: { - serviceName, - transactionType: selectedTransactionType, - }, - } - )}{' '} - - {i18n.translate( - 'xpack.apm.serviceDetails.enableAnomalyDetectionPanel.callout.jobExistsDescription.viewJobLinkText', - { - defaultMessage: 'View existing job', - } - )} - -

-
- -
- )} - -

- - {i18n.translate( - 'xpack.apm.serviceDetails.enableAnomalyDetectionPanel.createMLJobDescription.transactionDurationGraphText', - { - defaultMessage: 'transaction duration', - } - )} - - ), - serviceMapAnnotationText: ( - - {i18n.translate( - 'xpack.apm.serviceDetails.enableAnomalyDetectionPanel.createMLJobDescription.serviceMapAnnotationText', - { - defaultMessage: 'service maps', - } - )} - - ), - }} - /> -

-

- - {i18n.translate( - 'xpack.apm.serviceDetails.enableAnomalyDetectionPanel.manageMLJobDescription.mlJobsPageLinkText', - { - defaultMessage: 'Machine Learning Job Management page', - } - )} - - ), - }} - />{' '} - - {i18n.translate( - 'xpack.apm.serviceDetails.enableAnomalyDetectionPanel.manageMLJobDescription.noteText', - { - defaultMessage: - 'Note: It might take a few minutes for the job to begin calculating results.', - } - )} - -

-
- - -
- - - - {transactionTypes.length > 1 ? ( - { - setSelectedTransactionType(value); - }} - /> - ) : null} - - - - - onClickCreate({ transactionType: selectedTransactionType }) - } - fill - disabled={isCreatingJob || hasMLJob || isLoadingMLJob} - > - {i18n.translate( - 'xpack.apm.serviceDetails.enableAnomalyDetectionPanel.createNewJobButtonLabel', - { - defaultMessage: 'Create job', - } - )} - - - - - -
- ); -} diff --git a/x-pack/plugins/apm/public/components/app/ServiceDetails/ServiceIntegrations/index.tsx b/x-pack/plugins/apm/public/components/app/ServiceDetails/ServiceIntegrations/index.tsx index 321617ed8496af..0a7dcbd0be3dfc 100644 --- a/x-pack/plugins/apm/public/components/app/ServiceDetails/ServiceIntegrations/index.tsx +++ b/x-pack/plugins/apm/public/components/app/ServiceDetails/ServiceIntegrations/index.tsx @@ -4,18 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ -import { - EuiButtonEmpty, - EuiContextMenu, - EuiContextMenuPanelItemDescriptor, - EuiPopover, -} from '@elastic/eui'; +import { EuiButtonEmpty, EuiContextMenu, EuiPopover } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; -import { memoize } from 'lodash'; -import React, { Fragment } from 'react'; +import React from 'react'; import { IUrlParams } from '../../../../context/UrlParamsContext/types'; -import { LicenseContext } from '../../../../context/LicenseContext'; -import { MachineLearningFlyout } from './MachineLearningFlyout'; import { WatcherFlyout } from './WatcherFlyout'; import { ApmPluginContext } from '../../../../context/ApmPluginContext'; @@ -26,7 +18,7 @@ interface State { isPopoverOpen: boolean; activeFlyout: FlyoutName; } -type FlyoutName = null | 'ML' | 'Watcher'; +type FlyoutName = null | 'Watcher'; export class ServiceIntegrations extends React.Component { static contextType = ApmPluginContext; @@ -34,38 +26,6 @@ export class ServiceIntegrations extends React.Component { public state: State = { isPopoverOpen: false, activeFlyout: null }; - public getPanelItems = memoize((mlAvailable: boolean | undefined) => { - let panelItems: EuiContextMenuPanelItemDescriptor[] = []; - if (mlAvailable) { - panelItems = panelItems.concat(this.getMLPanelItems()); - } - return panelItems.concat(this.getWatcherPanelItems()); - }); - - public getMLPanelItems = () => { - return [ - { - name: i18n.translate( - 'xpack.apm.serviceDetails.integrationsMenu.enableMLAnomalyDetectionButtonLabel', - { - defaultMessage: 'Enable ML anomaly detection', - } - ), - icon: 'machineLearningApp', - toolTipContent: i18n.translate( - 'xpack.apm.serviceDetails.integrationsMenu.enableMLAnomalyDetectionButtonTooltip', - { - defaultMessage: 'Set up a machine learning job for this service', - } - ), - onClick: () => { - this.closePopover(); - this.openFlyout('ML'); - }, - }, - ]; - }; - public getWatcherPanelItems = () => { const { core } = this.context; @@ -132,42 +92,31 @@ export class ServiceIntegrations extends React.Component { ); return ( - - {(license) => ( - - - - - - - - )} - + <> + + + + + ); } } diff --git a/x-pack/plugins/apm/public/components/app/ServiceMap/Popover/Contents.tsx b/x-pack/plugins/apm/public/components/app/ServiceMap/Popover/Contents.tsx index ff68288916af47..78779bdcc2052e 100644 --- a/x-pack/plugins/apm/public/components/app/ServiceMap/Popover/Contents.tsx +++ b/x-pack/plugins/apm/public/components/app/ServiceMap/Popover/Contents.tsx @@ -15,8 +15,6 @@ import React, { MouseEvent } from 'react'; import { Buttons } from './Buttons'; import { Info } from './Info'; import { ServiceMetricFetcher } from './ServiceMetricFetcher'; -import { AnomalyDetection } from './anomaly_detection'; -import { ServiceNode } from '../../../../../common/service_map'; import { popoverMinWidth } from '../cytoscapeOptions'; interface ContentsProps { @@ -70,12 +68,13 @@ export function Contents({ - {isService && ( + {/* //TODO [APM ML] add service health stats here: + isService && ( - + - )} + )*/} {isService ? ( diff --git a/x-pack/plugins/apm/public/components/app/ServiceMap/Popover/anomaly_detection.tsx b/x-pack/plugins/apm/public/components/app/ServiceMap/Popover/anomaly_detection.tsx deleted file mode 100644 index 531bbb139d58b6..00000000000000 --- a/x-pack/plugins/apm/public/components/app/ServiceMap/Popover/anomaly_detection.tsx +++ /dev/null @@ -1,157 +0,0 @@ -/* - * 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 { i18n } from '@kbn/i18n'; -import React from 'react'; -import styled from 'styled-components'; -import { - EuiFlexGroup, - EuiFlexItem, - EuiTitle, - EuiIconTip, - EuiHealth, -} from '@elastic/eui'; -import { useTheme } from '../../../../hooks/useTheme'; -import { fontSize, px } from '../../../../style/variables'; -import { asInteger } from '../../../../utils/formatters'; -import { MLJobLink } from '../../../shared/Links/MachineLearningLinks/MLJobLink'; -import { getSeverityColor, popoverMinWidth } from '../cytoscapeOptions'; -import { getMetricChangeDescription } from '../../../../../../ml/public'; -import { ServiceNode } from '../../../../../common/service_map'; - -const HealthStatusTitle = styled(EuiTitle)` - display: inline; - text-transform: uppercase; -`; - -const VerticallyCentered = styled.div` - display: flex; - align-items: center; -`; - -const SubduedText = styled.span` - color: ${({ theme }) => theme.eui.euiTextSubduedColor}; -`; - -const EnableText = styled.section` - color: ${({ theme }) => theme.eui.euiTextSubduedColor}; - line-height: 1.4; - font-size: ${fontSize}; - width: ${px(popoverMinWidth)}; -`; - -export const ContentLine = styled.section` - line-height: 2; -`; - -interface AnomalyDetectionProps { - serviceNodeData: cytoscape.NodeDataDefinition & ServiceNode; -} - -export function AnomalyDetection({ serviceNodeData }: AnomalyDetectionProps) { - const theme = useTheme(); - const anomalySeverity = serviceNodeData.anomaly_severity; - const anomalyScore = serviceNodeData.anomaly_score; - const actualValue = serviceNodeData.actual_value; - const typicalValue = serviceNodeData.typical_value; - const mlJobId = serviceNodeData.ml_job_id; - const hasAnomalyDetectionScore = - anomalySeverity !== undefined && anomalyScore !== undefined; - const anomalyDescription = - hasAnomalyDetectionScore && - actualValue !== undefined && - typicalValue !== undefined - ? getMetricChangeDescription(actualValue, typicalValue).message - : null; - - return ( - <> -
- -

{ANOMALY_DETECTION_TITLE}

-
-   - - {!mlJobId && {ANOMALY_DETECTION_DISABLED_TEXT}} -
- {hasAnomalyDetectionScore && ( - - - - - - {ANOMALY_DETECTION_SCORE_METRIC} - - - -
- {getDisplayedAnomalyScore(anomalyScore as number)} - {anomalyDescription && ( -  ({anomalyDescription}) - )} -
-
-
-
- )} - {mlJobId && !hasAnomalyDetectionScore && ( - {ANOMALY_DETECTION_NO_DATA_TEXT} - )} - {mlJobId && ( - - - {ANOMALY_DETECTION_LINK} - - - )} - - ); -} - -function getDisplayedAnomalyScore(score: number) { - if (score > 0 && score < 1) { - return '< 1'; - } - return asInteger(score); -} - -const ANOMALY_DETECTION_TITLE = i18n.translate( - 'xpack.apm.serviceMap.anomalyDetectionPopoverTitle', - { defaultMessage: 'Anomaly Detection' } -); - -const ANOMALY_DETECTION_TOOLTIP = i18n.translate( - 'xpack.apm.serviceMap.anomalyDetectionPopoverTooltip', - { - defaultMessage: - 'Service health indicators are powered by the anomaly detection feature in machine learning', - } -); - -const ANOMALY_DETECTION_SCORE_METRIC = i18n.translate( - 'xpack.apm.serviceMap.anomalyDetectionPopoverScoreMetric', - { defaultMessage: 'Score (max.)' } -); - -const ANOMALY_DETECTION_LINK = i18n.translate( - 'xpack.apm.serviceMap.anomalyDetectionPopoverLink', - { defaultMessage: 'View anomalies' } -); - -const ANOMALY_DETECTION_DISABLED_TEXT = i18n.translate( - 'xpack.apm.serviceMap.anomalyDetectionPopoverDisabled', - { - defaultMessage: - 'Display service health indicators by enabling anomaly detection from the Integrations menu in the Service details view.', - } -); - -const ANOMALY_DETECTION_NO_DATA_TEXT = i18n.translate( - 'xpack.apm.serviceMap.anomalyDetectionPopoverNoData', - { - defaultMessage: `We couldn't find an anomaly score within the selected time range. See details in the anomaly explorer.`, - } -); diff --git a/x-pack/plugins/apm/public/components/app/ServiceMap/__stories__/Cytoscape.stories.tsx b/x-pack/plugins/apm/public/components/app/ServiceMap/__stories__/Cytoscape.stories.tsx index 28cb7a6f9d291d..aee392b53298a5 100644 --- a/x-pack/plugins/apm/public/components/app/ServiceMap/__stories__/Cytoscape.stories.tsx +++ b/x-pack/plugins/apm/public/components/app/ServiceMap/__stories__/Cytoscape.stories.tsx @@ -10,60 +10,63 @@ import cytoscape from 'cytoscape'; import React from 'react'; import { Cytoscape } from '../Cytoscape'; import { iconForNode } from '../icons'; +import { EuiThemeProvider } from '../../../../../../observability/public'; -storiesOf('app/ServiceMap/Cytoscape', module).add( - 'example', - () => { - const elements: cytoscape.ElementDefinition[] = [ - { - data: { - id: 'opbeans-python', - 'service.name': 'opbeans-python', - 'agent.name': 'python', - }, - }, - { - data: { - id: 'opbeans-node', - 'service.name': 'opbeans-node', - 'agent.name': 'nodejs', - }, - }, - { - data: { - id: 'opbeans-ruby', - 'service.name': 'opbeans-ruby', - 'agent.name': 'ruby', - }, - }, - { data: { source: 'opbeans-python', target: 'opbeans-node' } }, - { - data: { - bidirectional: true, - source: 'opbeans-python', - target: 'opbeans-ruby', - }, - }, - ]; - const height = 300; - const width = 1340; - const serviceName = 'opbeans-python'; - return ( - - ); - }, - { - info: { - propTables: false, - source: false, +storiesOf('app/ServiceMap/Cytoscape', module) + .addDecorator((storyFn) => {storyFn()}) + .add( + 'example', + () => { + const elements: cytoscape.ElementDefinition[] = [ + { + data: { + id: 'opbeans-python', + 'service.name': 'opbeans-python', + 'agent.name': 'python', + }, + }, + { + data: { + id: 'opbeans-node', + 'service.name': 'opbeans-node', + 'agent.name': 'nodejs', + }, + }, + { + data: { + id: 'opbeans-ruby', + 'service.name': 'opbeans-ruby', + 'agent.name': 'ruby', + }, + }, + { data: { source: 'opbeans-python', target: 'opbeans-node' } }, + { + data: { + bidirectional: true, + source: 'opbeans-python', + target: 'opbeans-ruby', + }, + }, + ]; + const height = 300; + const width = 1340; + const serviceName = 'opbeans-python'; + return ( + + ); }, - } -); + { + info: { + propTables: false, + source: false, + }, + } + ); storiesOf('app/ServiceMap/Cytoscape', module).add( 'node icons', diff --git a/x-pack/plugins/apm/public/components/app/ServiceMap/__stories__/CytoscapeExampleData.stories.tsx b/x-pack/plugins/apm/public/components/app/ServiceMap/__stories__/CytoscapeExampleData.stories.tsx index 3aced1b33dcacb..44278b2846128a 100644 --- a/x-pack/plugins/apm/public/components/app/ServiceMap/__stories__/CytoscapeExampleData.stories.tsx +++ b/x-pack/plugins/apm/public/components/app/ServiceMap/__stories__/CytoscapeExampleData.stories.tsx @@ -6,23 +6,25 @@ /* eslint-disable no-console */ import { + EuiButton, + EuiCodeEditor, + EuiFieldNumber, + EuiFilePicker, EuiFlexGroup, EuiFlexItem, - EuiButton, EuiForm, - EuiFieldNumber, - EuiToolTip, - EuiCodeEditor, EuiSpacer, - EuiFilePicker, + EuiToolTip, } from '@elastic/eui'; import { storiesOf } from '@storybook/react'; -import React, { useState, useEffect } from 'react'; +import React, { useEffect, useState } from 'react'; +import { EuiThemeProvider } from '../../../../../../observability/public'; import { Cytoscape } from '../Cytoscape'; -import { generateServiceMapElements } from './generate_service_map_elements'; -import exampleResponseOpbeansBeats from './example_response_opbeans_beats.json'; import exampleResponseHipsterStore from './example_response_hipster_store.json'; +import exampleResponseOpbeansBeats from './example_response_opbeans_beats.json'; import exampleResponseTodo from './example_response_todo.json'; +import exampleResponseOneDomainManyIPs from './example_response_one_domain_many_ips.json'; +import { generateServiceMapElements } from './generate_service_map_elements'; const STORYBOOK_PATH = 'app/ServiceMap/Cytoscape/Example data'; @@ -34,151 +36,155 @@ function setSessionJson(json: string) { window.sessionStorage.setItem(SESSION_STORAGE_KEY, json); } -storiesOf(STORYBOOK_PATH, module).add( - 'Generate map', - () => { - const [size, setSize] = useState(10); - const [json, setJson] = useState(''); - const [elements, setElements] = useState( - generateServiceMapElements(size) - ); - - return ( -
- - - { - setElements(generateServiceMapElements(size)); - setJson(''); - }} - > - Generate service map - - - - - setSize(e.target.valueAsNumber)} - /> - - - - { - setJson(JSON.stringify({ elements }, null, 2)); - }} - > - Get JSON - - - - - - - {json && ( - - )} -
- ); - }, - { - info: { propTables: false, source: false }, - } -); - -storiesOf(STORYBOOK_PATH, module).add( - 'Map from JSON', - () => { - const [json, setJson] = useState( - getSessionJson() || JSON.stringify(exampleResponseTodo, null, 2) - ); - const [error, setError] = useState(); - - const [elements, setElements] = useState([]); - useEffect(() => { - try { - setElements(JSON.parse(json).elements); - } catch (e) { - setError(e.message); - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); - - return ( -
- - +storiesOf(STORYBOOK_PATH, module) + .addDecorator((storyFn) => {storyFn()}) + .add( + 'Generate map', + () => { + const [size, setSize] = useState(10); + const [json, setJson] = useState(''); + const [elements, setElements] = useState( + generateServiceMapElements(size) + ); + + return ( +
- { - setJson(value); + { + setElements(generateServiceMapElements(size)); + setJson(''); }} - /> + > + Generate service map + - - { - const item = event?.item(0); - - if (item) { - const f = new FileReader(); - f.onload = (onloadEvent) => { - const result = onloadEvent?.target?.result; - if (typeof result === 'string') { - setJson(result); - } - }; - f.readAsText(item); - } - }} + + setSize(e.target.valueAsNumber)} /> - - { - try { - setElements(JSON.parse(json).elements); - setSessionJson(json); - setError(undefined); - } catch (e) { - setError(e.message); - } - }} - > - Render JSON - - + + + + { + setJson(JSON.stringify({ elements }, null, 2)); + }} + > + Get JSON + - -
- ); - }, - { - info: { - propTables: false, - source: false, - text: ` + + + + {json && ( + + )} +
+ ); + }, + { + info: { propTables: false, source: false }, + } + ); + +storiesOf(STORYBOOK_PATH, module) + .addDecorator((storyFn) => {storyFn()}) + .add( + 'Map from JSON', + () => { + const [json, setJson] = useState( + getSessionJson() || JSON.stringify(exampleResponseTodo, null, 2) + ); + const [error, setError] = useState(); + + const [elements, setElements] = useState([]); + useEffect(() => { + try { + setElements(JSON.parse(json).elements); + } catch (e) { + setError(e.message); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + return ( +
+ + + + + { + setJson(value); + }} + /> + + + + { + const item = event?.item(0); + + if (item) { + const f = new FileReader(); + f.onload = (onloadEvent) => { + const result = onloadEvent?.target?.result; + if (typeof result === 'string') { + setJson(result); + } + }; + f.readAsText(item); + } + }} + /> + + { + try { + setElements(JSON.parse(json).elements); + setSessionJson(json); + setError(undefined); + } catch (e) { + setError(e.message); + } + }} + > + Render JSON + + + + + +
+ ); + }, + { + info: { + propTables: false, + source: false, + text: ` Enter JSON map data into the text box or upload a file and click "Render JSON" to see the results. You can enable a download button on the service map by putting \`\`\` @@ -186,60 +192,86 @@ storiesOf(STORYBOOK_PATH, module).add( \`\`\` into the JavaScript console and reloading the page.`, + }, + } + ); + +storiesOf(STORYBOOK_PATH, module) + .addDecorator((storyFn) => {storyFn()}) + .add( + 'Todo app', + () => { + return ( +
+ +
+ ); + }, + { + info: { propTables: false, source: false }, + } + ); + +storiesOf(STORYBOOK_PATH, module) + .addDecorator((storyFn) => {storyFn()}) + .add( + 'Opbeans + beats', + () => { + return ( +
+ +
+ ); + }, + { + info: { propTables: false, source: false }, + } + ); + +storiesOf(STORYBOOK_PATH, module) + .addDecorator((storyFn) => {storyFn()}) + .add( + 'Hipster store', + () => { + return ( +
+ +
+ ); + }, + { + info: { propTables: false, source: false }, + } + ); + +storiesOf(STORYBOOK_PATH, module) + .addDecorator((storyFn) => {storyFn()}) + .add( + 'Node resolves one domain name to many IPs', + () => { + return ( +
+ +
+ ); }, - } -); - -storiesOf(STORYBOOK_PATH, module).add( - 'Todo app', - () => { - return ( -
- -
- ); - }, - { - info: { propTables: false, source: false }, - } -); - -storiesOf(STORYBOOK_PATH, module).add( - 'Opbeans + beats', - () => { - return ( -
- -
- ); - }, - { - info: { propTables: false, source: false }, - } -); - -storiesOf(STORYBOOK_PATH, module).add( - 'Hipster store', - () => { - return ( -
- -
- ); - }, - { - info: { propTables: false, source: false }, - } -); + { + info: { propTables: false, source: false }, + } + ); diff --git a/x-pack/plugins/apm/public/components/app/ServiceMap/__stories__/example_response_one_domain_many_ips.json b/x-pack/plugins/apm/public/components/app/ServiceMap/__stories__/example_response_one_domain_many_ips.json new file mode 100644 index 00000000000000..f9b8a273d8577c --- /dev/null +++ b/x-pack/plugins/apm/public/components/app/ServiceMap/__stories__/example_response_one_domain_many_ips.json @@ -0,0 +1,2122 @@ +{ + "elements": [ + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.99:80", + "id": "artifact_api~>192.0.2.99:80", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "http", + "span.destination.service.resource": "192.0.2.99:80", + "span.type": "external", + "id": ">192.0.2.99:80", + "label": ">192.0.2.99:80" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.47:443", + "id": "artifact_api~>192.0.2.47:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.47:443", + "span.type": "external", + "id": ">192.0.2.47:443", + "label": ">192.0.2.47:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.13:443", + "id": "artifact_api~>192.0.2.13:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.13:443", + "span.type": "external", + "id": ">192.0.2.13:443", + "label": ">192.0.2.13:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.106:443", + "id": "artifact_api~>192.0.2.106:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.106:443", + "span.type": "external", + "id": ">192.0.2.106:443", + "label": ">192.0.2.106:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.83:443", + "id": "artifact_api~>192.0.2.83:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.83:443", + "span.type": "external", + "id": ">192.0.2.83:443", + "label": ">192.0.2.83:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.111:443", + "id": "artifact_api~>192.0.2.111:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.111:443", + "span.type": "external", + "id": ">192.0.2.111:443", + "label": ">192.0.2.111:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.189:443", + "id": "artifact_api~>192.0.2.189:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.189:443", + "span.type": "external", + "id": ">192.0.2.189:443", + "label": ">192.0.2.189:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.148:443", + "id": "artifact_api~>192.0.2.148:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.148:443", + "span.type": "external", + "id": ">192.0.2.148:443", + "label": ">192.0.2.148:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.39:443", + "id": "artifact_api~>192.0.2.39:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.39:443", + "span.type": "external", + "id": ">192.0.2.39:443", + "label": ">192.0.2.39:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.42:443", + "id": "artifact_api~>192.0.2.42:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.42:443", + "span.type": "external", + "id": ">192.0.2.42:443", + "label": ">192.0.2.42:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.240:443", + "id": "artifact_api~>192.0.2.240:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.240:443", + "span.type": "external", + "id": ">192.0.2.240:443", + "label": ">192.0.2.240:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.156:443", + "id": "artifact_api~>192.0.2.156:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.156:443", + "span.type": "external", + "id": ">192.0.2.156:443", + "label": ">192.0.2.156:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.245:443", + "id": "artifact_api~>192.0.2.245:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.245:443", + "span.type": "external", + "id": ">192.0.2.245:443", + "label": ">192.0.2.245:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.198:443", + "id": "artifact_api~>192.0.2.198:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.198:443", + "span.type": "external", + "id": ">192.0.2.198:443", + "label": ">192.0.2.198:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.77:443", + "id": "artifact_api~>192.0.2.77:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.77:443", + "span.type": "external", + "id": ">192.0.2.77:443", + "label": ">192.0.2.77:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.8:443", + "id": "artifact_api~>192.0.2.8:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.8:443", + "span.type": "external", + "id": ">192.0.2.8:443", + "label": ">192.0.2.8:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.69:443", + "id": "artifact_api~>192.0.2.69:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.69:443", + "span.type": "external", + "id": ">192.0.2.69:443", + "label": ">192.0.2.69:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.5:443", + "id": "artifact_api~>192.0.2.5:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.5:443", + "span.type": "external", + "id": ">192.0.2.5:443", + "label": ">192.0.2.5:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.139:443", + "id": "artifact_api~>192.0.2.139:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.139:443", + "span.type": "external", + "id": ">192.0.2.139:443", + "label": ">192.0.2.139:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.113:443", + "id": "artifact_api~>192.0.2.113:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.113:443", + "span.type": "external", + "id": ">192.0.2.113:443", + "label": ">192.0.2.113:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.2:443", + "id": "artifact_api~>192.0.2.2:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.2:443", + "span.type": "external", + "id": ">192.0.2.2:443", + "label": ">192.0.2.2:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.213:443", + "id": "artifact_api~>192.0.2.213:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.213:443", + "span.type": "external", + "id": ">192.0.2.213:443", + "label": ">192.0.2.213:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.153:443", + "id": "artifact_api~>192.0.2.153:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.153:443", + "span.type": "external", + "id": ">192.0.2.153:443", + "label": ">192.0.2.153:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.36:443", + "id": "artifact_api~>192.0.2.36:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.36:443", + "span.type": "external", + "id": ">192.0.2.36:443", + "label": ">192.0.2.36:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.164:443", + "id": "artifact_api~>192.0.2.164:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.164:443", + "span.type": "external", + "id": ">192.0.2.164:443", + "label": ">192.0.2.164:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.190:443", + "id": "artifact_api~>192.0.2.190:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.190:443", + "span.type": "external", + "id": ">192.0.2.190:443", + "label": ">192.0.2.190:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.9:443", + "id": "artifact_api~>192.0.2.9:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.9:443", + "span.type": "external", + "id": ">192.0.2.9:443", + "label": ">192.0.2.9:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.210:443", + "id": "artifact_api~>192.0.2.210:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.210:443", + "span.type": "external", + "id": ">192.0.2.210:443", + "label": ">192.0.2.210:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.21:443", + "id": "artifact_api~>192.0.2.21:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.21:443", + "span.type": "external", + "id": ">192.0.2.21:443", + "label": ">192.0.2.21:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.176:443", + "id": "artifact_api~>192.0.2.176:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.176:443", + "span.type": "external", + "id": ">192.0.2.176:443", + "label": ">192.0.2.176:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.81:443", + "id": "artifact_api~>192.0.2.81:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.81:443", + "span.type": "external", + "id": ">192.0.2.81:443", + "label": ">192.0.2.81:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.118:443", + "id": "artifact_api~>192.0.2.118:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.118:443", + "span.type": "external", + "id": ">192.0.2.118:443", + "label": ">192.0.2.118:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.103:443", + "id": "artifact_api~>192.0.2.103:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.103:443", + "span.type": "external", + "id": ">192.0.2.103:443", + "label": ">192.0.2.103:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.3:443", + "id": "artifact_api~>192.0.2.3:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.3:443", + "span.type": "external", + "id": ">192.0.2.3:443", + "label": ">192.0.2.3:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.135:443", + "id": "artifact_api~>192.0.2.135:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.135:443", + "span.type": "external", + "id": ">192.0.2.135:443", + "label": ">192.0.2.135:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.26:443", + "id": "artifact_api~>192.0.2.26:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.26:443", + "span.type": "external", + "id": ">192.0.2.26:443", + "label": ">192.0.2.26:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.185:443", + "id": "artifact_api~>192.0.2.185:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.185:443", + "span.type": "external", + "id": ">192.0.2.185:443", + "label": ">192.0.2.185:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.173:443", + "id": "artifact_api~>192.0.2.173:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.173:443", + "span.type": "external", + "id": ">192.0.2.173:443", + "label": ">192.0.2.173:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.45:443", + "id": "artifact_api~>192.0.2.45:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.45:443", + "span.type": "external", + "id": ">192.0.2.45:443", + "label": ">192.0.2.45:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.144:443", + "id": "artifact_api~>192.0.2.144:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.144:443", + "span.type": "external", + "id": ">192.0.2.144:443", + "label": ">192.0.2.144:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.165:443", + "id": "artifact_api~>192.0.2.165:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.165:443", + "span.type": "external", + "id": ">192.0.2.165:443", + "label": ">192.0.2.165:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.119:443", + "id": "artifact_api~>192.0.2.119:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.119:443", + "span.type": "external", + "id": ">192.0.2.119:443", + "label": ">192.0.2.119:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.186:443", + "id": "artifact_api~>192.0.2.186:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.186:443", + "span.type": "external", + "id": ">192.0.2.186:443", + "label": ">192.0.2.186:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.54:443", + "id": "artifact_api~>192.0.2.54:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.54:443", + "span.type": "external", + "id": ">192.0.2.54:443", + "label": ">192.0.2.54:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.23:443", + "id": "artifact_api~>192.0.2.23:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.23:443", + "span.type": "external", + "id": ">192.0.2.23:443", + "label": ">192.0.2.23:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.34:443", + "id": "artifact_api~>192.0.2.34:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.34:443", + "span.type": "external", + "id": ">192.0.2.34:443", + "label": ">192.0.2.34:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.169:443", + "id": "artifact_api~>192.0.2.169:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.169:443", + "span.type": "external", + "id": ">192.0.2.169:443", + "label": ">192.0.2.169:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.226:443", + "id": "artifact_api~>192.0.2.226:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.226:443", + "span.type": "external", + "id": ">192.0.2.226:443", + "label": ">192.0.2.226:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.82:443", + "id": "artifact_api~>192.0.2.82:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.82:443", + "span.type": "external", + "id": ">192.0.2.82:443", + "label": ">192.0.2.82:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.132:443", + "id": "artifact_api~>192.0.2.132:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.132:443", + "span.type": "external", + "id": ">192.0.2.132:443", + "label": ">192.0.2.132:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.78:443", + "id": "artifact_api~>192.0.2.78:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.78:443", + "span.type": "external", + "id": ">192.0.2.78:443", + "label": ">192.0.2.78:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.71:443", + "id": "artifact_api~>192.0.2.71:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.71:443", + "span.type": "external", + "id": ">192.0.2.71:443", + "label": ">192.0.2.71:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.48:443", + "id": "artifact_api~>192.0.2.48:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.48:443", + "span.type": "external", + "id": ">192.0.2.48:443", + "label": ">192.0.2.48:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.107:443", + "id": "artifact_api~>192.0.2.107:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.107:443", + "span.type": "external", + "id": ">192.0.2.107:443", + "label": ">192.0.2.107:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.239:443", + "id": "artifact_api~>192.0.2.239:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.239:443", + "span.type": "external", + "id": ">192.0.2.239:443", + "label": ">192.0.2.239:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.209:443", + "id": "artifact_api~>192.0.2.209:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.209:443", + "span.type": "external", + "id": ">192.0.2.209:443", + "label": ">192.0.2.209:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.248:443", + "id": "artifact_api~>192.0.2.248:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.248:443", + "span.type": "external", + "id": ">192.0.2.248:443", + "label": ">192.0.2.248:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.18:443", + "id": "artifact_api~>192.0.2.18:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.18:443", + "span.type": "external", + "id": ">192.0.2.18:443", + "label": ">192.0.2.18:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.228:443", + "id": "artifact_api~>192.0.2.228:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.228:443", + "span.type": "external", + "id": ">192.0.2.228:443", + "label": ">192.0.2.228:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.145:443", + "id": "artifact_api~>192.0.2.145:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.145:443", + "span.type": "external", + "id": ">192.0.2.145:443", + "label": ">192.0.2.145:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.25:443", + "id": "artifact_api~>192.0.2.25:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.25:443", + "span.type": "external", + "id": ">192.0.2.25:443", + "label": ">192.0.2.25:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.162:443", + "id": "artifact_api~>192.0.2.162:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.162:443", + "span.type": "external", + "id": ">192.0.2.162:443", + "label": ">192.0.2.162:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.202:443", + "id": "artifact_api~>192.0.2.202:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.202:443", + "span.type": "external", + "id": ">192.0.2.202:443", + "label": ">192.0.2.202:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.60:443", + "id": "artifact_api~>192.0.2.60:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.60:443", + "span.type": "external", + "id": ">192.0.2.60:443", + "label": ">192.0.2.60:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.59:443", + "id": "artifact_api~>192.0.2.59:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.59:443", + "span.type": "external", + "id": ">192.0.2.59:443", + "label": ">192.0.2.59:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.114:443", + "id": "artifact_api~>192.0.2.114:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.114:443", + "span.type": "external", + "id": ">192.0.2.114:443", + "label": ">192.0.2.114:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.215:443", + "id": "artifact_api~>192.0.2.215:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.215:443", + "span.type": "external", + "id": ">192.0.2.215:443", + "label": ">192.0.2.215:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.238:443", + "id": "artifact_api~>192.0.2.238:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.238:443", + "span.type": "external", + "id": ">192.0.2.238:443", + "label": ">192.0.2.238:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.160:443", + "id": "artifact_api~>192.0.2.160:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.160:443", + "span.type": "external", + "id": ">192.0.2.160:443", + "label": ">192.0.2.160:443" + } + } + }, + { + "data": { + "source": "artifact_api", + "target": ">192.0.2.70:443", + "id": "artifact_api~>192.0.2.70:443", + "sourceData": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + }, + "targetData": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.70:443", + "span.type": "external", + "id": ">192.0.2.70:443", + "label": ">192.0.2.70:443" + } + } + }, + { + "data": { + "id": "artifact_api", + "service.environment": "development", + "service.name": "artifact_api", + "agent.name": "nodejs", + "service.framework.name": "express" + } + }, + { + "data": { + "span.subtype": "http", + "span.destination.service.resource": "192.0.2.99:80", + "span.type": "external", + "id": ">192.0.2.99:80", + "label": ">192.0.2.99:80" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.186:443", + "span.type": "external", + "id": ">192.0.2.186:443", + "label": ">192.0.2.186:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.78:443", + "span.type": "external", + "id": ">192.0.2.78:443", + "label": ">192.0.2.78:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.226:443", + "span.type": "external", + "id": ">192.0.2.226:443", + "label": ">192.0.2.226:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.245:443", + "span.type": "external", + "id": ">192.0.2.245:443", + "label": ">192.0.2.245:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.77:443", + "span.type": "external", + "id": ">192.0.2.77:443", + "label": ">192.0.2.77:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.2:443", + "span.type": "external", + "id": ">192.0.2.2:443", + "label": ">192.0.2.2:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.198:443", + "span.type": "external", + "id": ">192.0.2.198:443", + "label": ">192.0.2.198:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.113:443", + "span.type": "external", + "id": ">192.0.2.113:443", + "label": ">192.0.2.113:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.39:443", + "span.type": "external", + "id": ">192.0.2.39:443", + "label": ">192.0.2.39:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.83:443", + "span.type": "external", + "id": ">192.0.2.83:443", + "label": ">192.0.2.83:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.5:443", + "span.type": "external", + "id": ">192.0.2.5:443", + "label": ">192.0.2.5:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.165:443", + "span.type": "external", + "id": ">192.0.2.165:443", + "label": ">192.0.2.165:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.156:443", + "span.type": "external", + "id": ">192.0.2.156:443", + "label": ">192.0.2.156:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.132:443", + "span.type": "external", + "id": ">192.0.2.132:443", + "label": ">192.0.2.132:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.240:443", + "span.type": "external", + "id": ">192.0.2.240:443", + "label": ">192.0.2.240:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.54:443", + "span.type": "external", + "id": ">192.0.2.54:443", + "label": ">192.0.2.54:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.213:443", + "span.type": "external", + "id": ">192.0.2.213:443", + "label": ">192.0.2.213:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.81:443", + "span.type": "external", + "id": ">192.0.2.81:443", + "label": ">192.0.2.81:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.176:443", + "span.type": "external", + "id": ">192.0.2.176:443", + "label": ">192.0.2.176:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.82:443", + "span.type": "external", + "id": ">192.0.2.82:443", + "label": ">192.0.2.82:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.23:443", + "span.type": "external", + "id": ">192.0.2.23:443", + "label": ">192.0.2.23:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.189:443", + "span.type": "external", + "id": ">192.0.2.189:443", + "label": ">192.0.2.189:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.190:443", + "span.type": "external", + "id": ">192.0.2.190:443", + "label": ">192.0.2.190:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.119:443", + "span.type": "external", + "id": ">192.0.2.119:443", + "label": ">192.0.2.119:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.169:443", + "span.type": "external", + "id": ">192.0.2.169:443", + "label": ">192.0.2.169:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.210:443", + "span.type": "external", + "id": ">192.0.2.210:443", + "label": ">192.0.2.210:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.148:443", + "span.type": "external", + "id": ">192.0.2.148:443", + "label": ">192.0.2.148:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.26:443", + "span.type": "external", + "id": ">192.0.2.26:443", + "label": ">192.0.2.26:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.139:443", + "span.type": "external", + "id": ">192.0.2.139:443", + "label": ">192.0.2.139:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.111:443", + "span.type": "external", + "id": ">192.0.2.111:443", + "label": ">192.0.2.111:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.13:443", + "span.type": "external", + "id": ">192.0.2.13:443", + "label": ">192.0.2.13:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.36:443", + "span.type": "external", + "id": ">192.0.2.36:443", + "label": ">192.0.2.36:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.69:443", + "span.type": "external", + "id": ">192.0.2.69:443", + "label": ">192.0.2.69:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.173:443", + "span.type": "external", + "id": ">192.0.2.173:443", + "label": ">192.0.2.173:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.144:443", + "span.type": "external", + "id": ">192.0.2.144:443", + "label": ">192.0.2.144:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.135:443", + "span.type": "external", + "id": ">192.0.2.135:443", + "label": ">192.0.2.135:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.21:443", + "span.type": "external", + "id": ">192.0.2.21:443", + "label": ">192.0.2.21:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.118:443", + "span.type": "external", + "id": ">192.0.2.118:443", + "label": ">192.0.2.118:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.42:443", + "span.type": "external", + "id": ">192.0.2.42:443", + "label": ">192.0.2.42:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.106:443", + "span.type": "external", + "id": ">192.0.2.106:443", + "label": ">192.0.2.106:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.3:443", + "span.type": "external", + "id": ">192.0.2.3:443", + "label": ">192.0.2.3:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.34:443", + "span.type": "external", + "id": ">192.0.2.34:443", + "label": ">192.0.2.34:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.185:443", + "span.type": "external", + "id": ">192.0.2.185:443", + "label": ">192.0.2.185:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.153:443", + "span.type": "external", + "id": ">192.0.2.153:443", + "label": ">192.0.2.153:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.9:443", + "span.type": "external", + "id": ">192.0.2.9:443", + "label": ">192.0.2.9:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.164:443", + "span.type": "external", + "id": ">192.0.2.164:443", + "label": ">192.0.2.164:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.47:443", + "span.type": "external", + "id": ">192.0.2.47:443", + "label": ">192.0.2.47:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.45:443", + "span.type": "external", + "id": ">192.0.2.45:443", + "label": ">192.0.2.45:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.8:443", + "span.type": "external", + "id": ">192.0.2.8:443", + "label": ">192.0.2.8:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.103:443", + "span.type": "external", + "id": ">192.0.2.103:443", + "label": ">192.0.2.103:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.60:443", + "span.type": "external", + "id": ">192.0.2.60:443", + "label": ">192.0.2.60:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.202:443", + "span.type": "external", + "id": ">192.0.2.202:443", + "label": ">192.0.2.202:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.70:443", + "span.type": "external", + "id": ">192.0.2.70:443", + "label": ">192.0.2.70:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.114:443", + "span.type": "external", + "id": ">192.0.2.114:443", + "label": ">192.0.2.114:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.25:443", + "span.type": "external", + "id": ">192.0.2.25:443", + "label": ">192.0.2.25:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.209:443", + "span.type": "external", + "id": ">192.0.2.209:443", + "label": ">192.0.2.209:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.248:443", + "span.type": "external", + "id": ">192.0.2.248:443", + "label": ">192.0.2.248:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.18:443", + "span.type": "external", + "id": ">192.0.2.18:443", + "label": ">192.0.2.18:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.107:443", + "span.type": "external", + "id": ">192.0.2.107:443", + "label": ">192.0.2.107:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.160:443", + "span.type": "external", + "id": ">192.0.2.160:443", + "label": ">192.0.2.160:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.228:443", + "span.type": "external", + "id": ">192.0.2.228:443", + "label": ">192.0.2.228:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.215:443", + "span.type": "external", + "id": ">192.0.2.215:443", + "label": ">192.0.2.215:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.162:443", + "span.type": "external", + "id": ">192.0.2.162:443", + "label": ">192.0.2.162:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.238:443", + "span.type": "external", + "id": ">192.0.2.238:443", + "label": ">192.0.2.238:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.145:443", + "span.type": "external", + "id": ">192.0.2.145:443", + "label": ">192.0.2.145:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.239:443", + "span.type": "external", + "id": ">192.0.2.239:443", + "label": ">192.0.2.239:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.59:443", + "span.type": "external", + "id": ">192.0.2.59:443", + "label": ">192.0.2.59:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.71:443", + "span.type": "external", + "id": ">192.0.2.71:443", + "label": ">192.0.2.71:443" + } + }, + { + "data": { + "span.subtype": "https", + "span.destination.service.resource": "192.0.2.48:443", + "span.type": "external", + "id": ">192.0.2.48:443", + "label": ">192.0.2.48:443" + } + }, + { + "data": { + "service.name": "graphics-worker", + "agent.name": "nodejs", + "service.environment": null, + "service.framework.name": null, + "id": "graphics-worker" + } + } + ] +} diff --git a/x-pack/plugins/apm/public/components/app/Settings/AgentConfigurations/AgentConfigurationCreateEdit/index.stories.tsx b/x-pack/plugins/apm/public/components/app/Settings/AgentConfigurations/AgentConfigurationCreateEdit/index.stories.tsx index 7c69fb28d668fd..920ef39e84ca32 100644 --- a/x-pack/plugins/apm/public/components/app/Settings/AgentConfigurations/AgentConfigurationCreateEdit/index.stories.tsx +++ b/x-pack/plugins/apm/public/components/app/Settings/AgentConfigurations/AgentConfigurationCreateEdit/index.stories.tsx @@ -21,13 +21,13 @@ import { ApmPluginContext, ApmPluginContextValue, } from '../../../../../context/ApmPluginContext'; +import { EuiThemeProvider } from '../../../../../../../observability/public'; storiesOf( 'app/Settings/AgentConfigurations/AgentConfigurationCreateEdit', module -).add( - 'with config', - () => { +) + .addDecorator((storyFn) => { const httpMock = {}; // mock @@ -40,10 +40,21 @@ storiesOf( }, }, }; + return ( - + + + {storyFn()} + + + ); + }) + .add( + 'with config', + () => { + return ( - - ); - }, - { - info: { - source: false, + ); }, - } -); + { + info: { + source: false, + }, + } + ); diff --git a/x-pack/plugins/apm/public/components/app/TransactionOverview/index.tsx b/x-pack/plugins/apm/public/components/app/TransactionOverview/index.tsx index 9018fbb2bc410b..fc5347d081316a 100644 --- a/x-pack/plugins/apm/public/components/app/TransactionOverview/index.tsx +++ b/x-pack/plugins/apm/public/components/app/TransactionOverview/index.tsx @@ -22,8 +22,6 @@ import { TransactionCharts } from '../../shared/charts/TransactionCharts'; import { TransactionBreakdown } from '../../shared/TransactionBreakdown'; import { TransactionList } from './List'; import { useRedirect } from './useRedirect'; -import { useFetcher } from '../../../hooks/useFetcher'; -import { getHasMLJob } from '../../../services/rest/ml'; import { history } from '../../../utils/history'; import { useLocation } from '../../../hooks/useLocation'; import { ChartsSyncContextProvider } from '../../../context/ChartsSyncContext'; @@ -34,7 +32,6 @@ import { PROJECTION } from '../../../../common/projections/typings'; import { useUrlParams } from '../../../hooks/useUrlParams'; import { useServiceTransactionTypes } from '../../../hooks/useServiceTransactionTypes'; import { TransactionTypeFilter } from '../../shared/LocalUIFilters/TransactionTypeFilter'; -import { useApmPluginContext } from '../../../hooks/useApmPluginContext'; function getRedirectLocation({ urlParams, @@ -86,18 +83,6 @@ export function TransactionOverview() { status: transactionListStatus, } = useTransactionList(urlParams); - const { http } = useApmPluginContext().core; - - const { data: hasMLJob = false } = useFetcher( - () => { - if (serviceName && transactionType) { - return getHasMLJob({ serviceName, transactionType, http }); - } - }, - [http, serviceName, transactionType], - { showToastOnError: false } - ); - const localFiltersConfig: React.ComponentProps = useMemo( () => ({ filterNames: [ @@ -140,7 +125,8 @@ export function TransactionOverview() { { - it('should produce the correct URL with serviceName', async () => { - const href = await getRenderedHref( - () => ( - - ), - { search: '?rangeFrom=now/w&rangeTo=now-4h' } as Location - ); - - expect(href).toEqual( - `/basepath/app/ml#/timeseriesexplorer?_g=(ml:(jobIds:!(myservicename-mytransactiontype-high_mean_response_time)),refreshInterval:(pause:true,value:'0'),time:(from:now%2Fw,to:now-4h))` - ); - }); it('should produce the correct URL with jobId', async () => { const href = await getRenderedHref( () => ( diff --git a/x-pack/plugins/apm/public/components/shared/Links/MachineLearningLinks/MLJobLink.tsx b/x-pack/plugins/apm/public/components/shared/Links/MachineLearningLinks/MLJobLink.tsx index 346748964d529b..1e1f9ea5f23b72 100644 --- a/x-pack/plugins/apm/public/components/shared/Links/MachineLearningLinks/MLJobLink.tsx +++ b/x-pack/plugins/apm/public/components/shared/Links/MachineLearningLinks/MLJobLink.tsx @@ -5,28 +5,16 @@ */ import React from 'react'; -import { getMlJobId } from '../../../../../common/ml_job_constants'; import { MLLink } from './MLLink'; -interface PropsServiceName { - serviceName: string; - transactionType?: string; -} -interface PropsJobId { +interface Props { jobId: string; -} - -type Props = (PropsServiceName | PropsJobId) & { external?: boolean; -}; +} export const MLJobLink: React.FC = (props) => { - const jobId = - 'jobId' in props - ? props.jobId - : getMlJobId(props.serviceName, props.transactionType); const query = { - ml: { jobIds: [jobId] }, + ml: { jobIds: [props.jobId] }, }; return ( diff --git a/x-pack/plugins/apm/public/components/shared/charts/TransactionCharts/index.tsx b/x-pack/plugins/apm/public/components/shared/charts/TransactionCharts/index.tsx index 4821e06419e341..00ff6f9969725a 100644 --- a/x-pack/plugins/apm/public/components/shared/charts/TransactionCharts/index.tsx +++ b/x-pack/plugins/apm/public/components/shared/charts/TransactionCharts/index.tsx @@ -101,11 +101,13 @@ export class TransactionCharts extends Component { return null; } - const { serviceName, transactionType, kuery } = this.props.urlParams; + const { serviceName, kuery } = this.props.urlParams; if (!serviceName) { return null; } + const linkedJobId = ''; // TODO [APM ML] link to ML job id for the selected environment + const hasKuery = !isEmpty(kuery); const icon = hasKuery ? ( { } )}{' '} - - View Job - + View Job ); diff --git a/x-pack/plugins/apm/public/services/rest/ml.ts b/x-pack/plugins/apm/public/services/rest/ml.ts deleted file mode 100644 index 47032501d9fbe1..00000000000000 --- a/x-pack/plugins/apm/public/services/rest/ml.ts +++ /dev/null @@ -1,123 +0,0 @@ -/* - * 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 { HttpSetup } from 'kibana/public'; -import { - PROCESSOR_EVENT, - SERVICE_NAME, - TRANSACTION_TYPE, -} from '../../../common/elasticsearch_fieldnames'; -import { - APM_ML_JOB_GROUP_NAME, - getMlJobId, - getMlPrefix, - encodeForMlApi, -} from '../../../common/ml_job_constants'; -import { callApi } from './callApi'; -import { ESFilter } from '../../../typings/elasticsearch'; -import { callApmApi } from './createCallApmApi'; - -interface MlResponseItem { - id: string; - success: boolean; - error?: { - msg: string; - body: string; - path: string; - response: string; - statusCode: number; - }; -} - -interface StartedMLJobApiResponse { - datafeeds: MlResponseItem[]; - jobs: MlResponseItem[]; -} - -async function getTransactionIndices() { - const indices = await callApmApi({ - method: 'GET', - pathname: `/api/apm/settings/apm-indices`, - }); - return indices['apm_oss.transactionIndices']; -} - -export async function startMLJob({ - serviceName, - transactionType, - http, -}: { - serviceName: string; - transactionType: string; - http: HttpSetup; -}) { - const transactionIndices = await getTransactionIndices(); - const groups = [ - APM_ML_JOB_GROUP_NAME, - encodeForMlApi(serviceName), - encodeForMlApi(transactionType), - ]; - const filter: ESFilter[] = [ - { term: { [SERVICE_NAME]: serviceName } }, - { term: { [PROCESSOR_EVENT]: 'transaction' } }, - { term: { [TRANSACTION_TYPE]: transactionType } }, - ]; - return callApi(http, { - method: 'POST', - pathname: `/api/ml/modules/setup/apm_transaction`, - body: { - prefix: getMlPrefix(serviceName, transactionType), - groups, - indexPatternName: transactionIndices, - startDatafeed: true, - query: { - bool: { - filter, - }, - }, - }, - }); -} - -// https://www.elastic.co/guide/en/elasticsearch/reference/6.5/ml-get-job.html -export interface MLJobApiResponse { - count: number; - jobs: Array<{ - job_id: string; - }>; -} - -export type MLError = Error & { body?: { message?: string } }; - -export async function getHasMLJob({ - serviceName, - transactionType, - http, -}: { - serviceName: string; - transactionType: string; - http: HttpSetup; -}) { - try { - await callApi(http, { - method: 'GET', - pathname: `/api/ml/anomaly_detectors/${getMlJobId( - serviceName, - transactionType - )}`, - }); - return true; - } catch (error) { - if ( - error?.body?.statusCode === 404 && - error?.body?.attributes?.body?.error?.type === - 'resource_not_found_exception' - ) { - return false; // false only if ML api responds with resource_not_found_exception - } - throw error; - } -} diff --git a/x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.test.ts b/x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.test.ts deleted file mode 100644 index aefd074c373f95..00000000000000 --- a/x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 { getApmMlJobCategory } from './get_service_anomalies'; -import { Job as AnomalyDetectionJob } from '../../../../ml/server'; - -describe('getApmMlJobCategory', () => { - it('should match service names with different casings', () => { - const mlJob = { - job_id: 'testservice-request-high_mean_response_time', - groups: ['apm', 'testservice', 'request'], - } as AnomalyDetectionJob; - const serviceNames = ['testService']; - const apmMlJobCategory = getApmMlJobCategory(mlJob, serviceNames); - - expect(apmMlJobCategory).toEqual({ - jobId: 'testservice-request-high_mean_response_time', - serviceName: 'testService', - transactionType: 'request', - }); - }); - - it('should match service names with spaces', () => { - const mlJob = { - job_id: 'test_service-request-high_mean_response_time', - groups: ['apm', 'test_service', 'request'], - } as AnomalyDetectionJob; - const serviceNames = ['Test Service']; - const apmMlJobCategory = getApmMlJobCategory(mlJob, serviceNames); - - expect(apmMlJobCategory).toEqual({ - jobId: 'test_service-request-high_mean_response_time', - serviceName: 'Test Service', - transactionType: 'request', - }); - }); -}); diff --git a/x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.ts b/x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.ts deleted file mode 100644 index 900141e9040ae1..00000000000000 --- a/x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* - * 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 { intersection } from 'lodash'; -import { leftJoin } from '../../../common/utils/left_join'; -import { Job as AnomalyDetectionJob } from '../../../../ml/server'; -import { PromiseReturnType } from '../../../typings/common'; -import { IEnvOptions } from './get_service_map'; -import { Setup } from '../helpers/setup_request'; -import { - APM_ML_JOB_GROUP_NAME, - encodeForMlApi, -} from '../../../common/ml_job_constants'; - -async function getApmAnomalyDetectionJobs( - setup: Setup -): Promise { - const { ml } = setup; - - if (!ml) { - return []; - } - try { - const { jobs } = await ml.anomalyDetectors.jobs(APM_ML_JOB_GROUP_NAME); - return jobs; - } catch (error) { - if (error.statusCode === 404) { - return []; - } - throw error; - } -} - -type ApmMlJobCategory = NonNullable>; - -export const getApmMlJobCategory = ( - mlJob: AnomalyDetectionJob, - serviceNames: string[] -) => { - const serviceByGroupNameMap = new Map( - serviceNames.map((serviceName) => [ - encodeForMlApi(serviceName), - serviceName, - ]) - ); - if (!mlJob.groups.includes(APM_ML_JOB_GROUP_NAME)) { - // ML job missing "apm" group name - return; - } - const apmJobGroups = mlJob.groups.filter( - (groupName) => groupName !== APM_ML_JOB_GROUP_NAME - ); - const apmJobServiceNames = apmJobGroups.map( - (groupName) => serviceByGroupNameMap.get(groupName) || groupName - ); - const [serviceName] = intersection(apmJobServiceNames, serviceNames); - if (!serviceName) { - // APM ML job service was not found - return; - } - const serviceGroupName = encodeForMlApi(serviceName); - const [transactionType] = apmJobGroups.filter( - (groupName) => groupName !== serviceGroupName - ); - if (!transactionType) { - // APM ML job transaction type was not found. - return; - } - return { jobId: mlJob.job_id, serviceName, transactionType }; -}; - -export type ServiceAnomalies = PromiseReturnType; - -export async function getServiceAnomalies( - options: IEnvOptions, - serviceNames: string[] -) { - const { start, end, ml } = options.setup; - - if (!ml || serviceNames.length === 0) { - return []; - } - - const apmMlJobs = await getApmAnomalyDetectionJobs(options.setup); - if (apmMlJobs.length === 0) { - return []; - } - const apmMlJobCategories = apmMlJobs - .map((job) => getApmMlJobCategory(job, serviceNames)) - .filter( - (apmJobCategory) => apmJobCategory !== undefined - ) as ApmMlJobCategory[]; - const apmJobIds = apmMlJobs.map((job) => job.job_id); - const params = { - body: { - size: 0, - query: { - bool: { - filter: [ - { term: { result_type: 'record' } }, - { - terms: { - job_id: apmJobIds, - }, - }, - { - range: { - timestamp: { gte: start, lte: end, format: 'epoch_millis' }, - }, - }, - ], - }, - }, - aggs: { - jobs: { - terms: { field: 'job_id', size: apmJobIds.length }, - aggs: { - top_score_hits: { - top_hits: { - sort: [{ record_score: { order: 'desc' as const } }], - _source: ['record_score', 'timestamp', 'typical', 'actual'], - size: 1, - }, - }, - }, - }, - }, - }, - }; - - const response = (await ml.mlSystem.mlAnomalySearch(params)) as { - aggregations: { - jobs: { - buckets: Array<{ - key: string; - top_score_hits: { - hits: { - hits: Array<{ - _source: { - record_score: number; - timestamp: number; - typical: number[]; - actual: number[]; - }; - }>; - }; - }; - }>; - }; - }; - }; - const anomalyScores = response.aggregations.jobs.buckets.map((jobBucket) => { - const jobId = jobBucket.key; - const bucketSource = jobBucket.top_score_hits.hits.hits?.[0]?._source; - return { - jobId, - anomalyScore: bucketSource.record_score, - timestamp: bucketSource.timestamp, - typical: bucketSource.typical[0], - actual: bucketSource.actual[0], - }; - }); - return leftJoin(apmMlJobCategories, 'jobId', anomalyScores); -} diff --git a/x-pack/plugins/apm/server/lib/service_map/get_service_map.ts b/x-pack/plugins/apm/server/lib/service_map/get_service_map.ts index 9f3ded82d7cbd8..4d488cd1a55096 100644 --- a/x-pack/plugins/apm/server/lib/service_map/get_service_map.ts +++ b/x-pack/plugins/apm/server/lib/service_map/get_service_map.ts @@ -13,14 +13,9 @@ import { getServicesProjection } from '../../../common/projections/services'; import { mergeProjection } from '../../../common/projections/util/merge_projection'; import { PromiseReturnType } from '../../../typings/common'; import { Setup, SetupTimeRange } from '../helpers/setup_request'; -import { - transformServiceMapResponses, - getAllNodes, - getServiceNodes, -} from './transform_service_map_responses'; +import { transformServiceMapResponses } from './transform_service_map_responses'; import { getServiceMapFromTraceIds } from './get_service_map_from_trace_ids'; import { getTraceSampleIds } from './get_trace_sample_ids'; -import { getServiceAnomalies, ServiceAnomalies } from './get_service_anomalies'; export interface IEnvOptions { setup: Setup & SetupTimeRange; @@ -132,7 +127,6 @@ async function getServicesData(options: IEnvOptions) { ); } -export { ServiceAnomalies }; export type ConnectionsResponse = PromiseReturnType; export type ServicesResponse = PromiseReturnType; export type ServiceMapAPIResponse = PromiseReturnType; @@ -143,19 +137,8 @@ export async function getServiceMap(options: IEnvOptions) { getServicesData(options), ]); - // Derive all related service names from connection and service data - const allNodes = getAllNodes(servicesData, connectionData.connections); - const serviceNodes = getServiceNodes(allNodes); - const serviceNames = serviceNodes.map( - (serviceData) => serviceData[SERVICE_NAME] - ); - - // Get related service anomalies - const serviceAnomalies = await getServiceAnomalies(options, serviceNames); - return transformServiceMapResponses({ ...connectionData, - anomalies: serviceAnomalies, services: servicesData, }); } diff --git a/x-pack/plugins/apm/server/lib/service_map/ml_helpers.test.ts b/x-pack/plugins/apm/server/lib/service_map/ml_helpers.test.ts deleted file mode 100644 index f07b575cc0a35a..00000000000000 --- a/x-pack/plugins/apm/server/lib/service_map/ml_helpers.test.ts +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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 { ServiceAnomalies } from './get_service_map'; -import { addAnomaliesDataToNodes } from './ml_helpers'; - -describe('addAnomaliesDataToNodes', () => { - it('adds anomalies to nodes', () => { - const nodes = [ - { - 'service.name': 'opbeans-ruby', - 'agent.name': 'ruby', - 'service.environment': null, - }, - { - 'service.name': 'opbeans-java', - 'agent.name': 'java', - 'service.environment': null, - }, - ]; - - const serviceAnomalies: ServiceAnomalies = [ - { - jobId: 'opbeans-ruby-request-high_mean_response_time', - serviceName: 'opbeans-ruby', - transactionType: 'request', - anomalyScore: 50, - timestamp: 1591351200000, - actual: 2000, - typical: 1000, - }, - { - jobId: 'opbeans-java-request-high_mean_response_time', - serviceName: 'opbeans-java', - transactionType: 'request', - anomalyScore: 100, - timestamp: 1591351200000, - actual: 9000, - typical: 3000, - }, - ]; - - const result = [ - { - 'service.name': 'opbeans-ruby', - 'agent.name': 'ruby', - 'service.environment': null, - anomaly_score: 50, - anomaly_severity: 'major', - actual_value: 2000, - typical_value: 1000, - ml_job_id: 'opbeans-ruby-request-high_mean_response_time', - }, - { - 'service.name': 'opbeans-java', - 'agent.name': 'java', - 'service.environment': null, - anomaly_score: 100, - anomaly_severity: 'critical', - actual_value: 9000, - typical_value: 3000, - ml_job_id: 'opbeans-java-request-high_mean_response_time', - }, - ]; - - expect( - addAnomaliesDataToNodes( - nodes, - (serviceAnomalies as unknown) as ServiceAnomalies - ) - ).toEqual(result); - }); -}); diff --git a/x-pack/plugins/apm/server/lib/service_map/ml_helpers.ts b/x-pack/plugins/apm/server/lib/service_map/ml_helpers.ts deleted file mode 100644 index 8162417616b6cc..00000000000000 --- a/x-pack/plugins/apm/server/lib/service_map/ml_helpers.ts +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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 { SERVICE_NAME } from '../../../common/elasticsearch_fieldnames'; -import { getSeverity } from '../../../common/ml_job_constants'; -import { ConnectionNode, ServiceNode } from '../../../common/service_map'; -import { ServiceAnomalies } from './get_service_map'; - -export function addAnomaliesDataToNodes( - nodes: ConnectionNode[], - serviceAnomalies: ServiceAnomalies -) { - const anomaliesMap = serviceAnomalies.reduce( - (acc, anomalyJob) => { - const serviceAnomaly: typeof acc[string] | undefined = - acc[anomalyJob.serviceName]; - const hasAnomalyJob = serviceAnomaly !== undefined; - const hasAnomalyScore = serviceAnomaly?.anomaly_score !== undefined; - const hasNewAnomalyScore = anomalyJob.anomalyScore !== undefined; - const hasNewMaxAnomalyScore = - hasNewAnomalyScore && - (!hasAnomalyScore || - (anomalyJob?.anomalyScore ?? 0) > - (serviceAnomaly?.anomaly_score ?? 0)); - - if (!hasAnomalyJob || hasNewMaxAnomalyScore) { - acc[anomalyJob.serviceName] = { - anomaly_score: anomalyJob.anomalyScore, - actual_value: anomalyJob.actual, - typical_value: anomalyJob.typical, - ml_job_id: anomalyJob.jobId, - }; - } - - return acc; - }, - {} as { - [serviceName: string]: { - anomaly_score?: number; - actual_value?: number; - typical_value?: number; - ml_job_id: string; - }; - } - ); - - const servicesDataWithAnomalies: ServiceNode[] = nodes.map((service) => { - const serviceAnomaly = anomaliesMap[service[SERVICE_NAME]]; - if (serviceAnomaly) { - const anomalyScore = serviceAnomaly.anomaly_score; - return { - ...service, - anomaly_score: anomalyScore, - anomaly_severity: getSeverity(anomalyScore), - actual_value: serviceAnomaly.actual_value, - typical_value: serviceAnomaly.typical_value, - ml_job_id: serviceAnomaly.ml_job_id, - }; - } - return service; - }); - - return servicesDataWithAnomalies; -} diff --git a/x-pack/plugins/apm/server/lib/service_map/transform_service_map_responses.test.ts b/x-pack/plugins/apm/server/lib/service_map/transform_service_map_responses.test.ts index 6c9880c2dc4dfb..1e26634bdf0f12 100644 --- a/x-pack/plugins/apm/server/lib/service_map/transform_service_map_responses.test.ts +++ b/x-pack/plugins/apm/server/lib/service_map/transform_service_map_responses.test.ts @@ -12,7 +12,6 @@ import { SPAN_SUBTYPE, SPAN_TYPE, } from '../../../common/elasticsearch_fieldnames'; -import { ServiceAnomalies } from './get_service_map'; import { transformServiceMapResponses, ServiceMapResponse, @@ -36,12 +35,9 @@ const javaService = { [AGENT_NAME]: 'java', }; -const serviceAnomalies: ServiceAnomalies = []; - describe('transformServiceMapResponses', () => { it('maps external destinations to internal services', () => { const response: ServiceMapResponse = { - anomalies: serviceAnomalies, services: [nodejsService, javaService], discoveredServices: [ { @@ -73,7 +69,6 @@ describe('transformServiceMapResponses', () => { it('collapses external destinations based on span.destination.resource.name', () => { const response: ServiceMapResponse = { - anomalies: serviceAnomalies, services: [nodejsService, javaService], discoveredServices: [ { @@ -109,7 +104,6 @@ describe('transformServiceMapResponses', () => { it('picks the first span.type/subtype in an alphabetically sorted list', () => { const response: ServiceMapResponse = { - anomalies: serviceAnomalies, services: [javaService], discoveredServices: [], connections: [ @@ -148,7 +142,6 @@ describe('transformServiceMapResponses', () => { it('processes connections without a matching "service" aggregation', () => { const response: ServiceMapResponse = { - anomalies: serviceAnomalies, services: [javaService], discoveredServices: [], connections: [ diff --git a/x-pack/plugins/apm/server/lib/service_map/transform_service_map_responses.ts b/x-pack/plugins/apm/server/lib/service_map/transform_service_map_responses.ts index 53abf54cbcf313..835c00b8df239e 100644 --- a/x-pack/plugins/apm/server/lib/service_map/transform_service_map_responses.ts +++ b/x-pack/plugins/apm/server/lib/service_map/transform_service_map_responses.ts @@ -17,12 +17,7 @@ import { ServiceConnectionNode, ExternalConnectionNode, } from '../../../common/service_map'; -import { - ConnectionsResponse, - ServicesResponse, - ServiceAnomalies, -} from './get_service_map'; -import { addAnomaliesDataToNodes } from './ml_helpers'; +import { ConnectionsResponse, ServicesResponse } from './get_service_map'; function getConnectionNodeId(node: ConnectionNode): string { if ('span.destination.service.resource' in node) { @@ -67,12 +62,11 @@ export function getServiceNodes(allNodes: ConnectionNode[]) { } export type ServiceMapResponse = ConnectionsResponse & { - anomalies: ServiceAnomalies; services: ServicesResponse; }; export function transformServiceMapResponses(response: ServiceMapResponse) { - const { anomalies, discoveredServices, services, connections } = response; + const { discoveredServices, services, connections } = response; const allNodes = getAllNodes(services, connections); const serviceNodes = getServiceNodes(allNodes); @@ -214,18 +208,10 @@ export function transformServiceMapResponses(response: ServiceMapResponse) { return prev.concat(connection); }, []); - // Add anomlies data - const dedupedNodesWithAnomliesData = addAnomaliesDataToNodes( - dedupedNodes, - anomalies - ); - // Put everything together in elements, with everything in the "data" property - const elements = [...dedupedConnections, ...dedupedNodesWithAnomliesData].map( - (element) => ({ - data: element, - }) - ); + const elements = [...dedupedConnections, ...dedupedNodes].map((element) => ({ + data: element, + })); return { elements }; } diff --git a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/__snapshots__/fetcher.test.ts.snap b/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/__snapshots__/fetcher.test.ts.snap deleted file mode 100644 index cf3fdac221b597..00000000000000 --- a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/__snapshots__/fetcher.test.ts.snap +++ /dev/null @@ -1,68 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`anomalyAggsFetcher when ES returns valid response should call client with correct query 1`] = ` -Array [ - Array [ - Object { - "body": Object { - "aggs": Object { - "ml_avg_response_times": Object { - "aggs": Object { - "anomaly_score": Object { - "max": Object { - "field": "anomaly_score", - }, - }, - "lower": Object { - "min": Object { - "field": "model_lower", - }, - }, - "upper": Object { - "max": Object { - "field": "model_upper", - }, - }, - }, - "date_histogram": Object { - "extended_bounds": Object { - "max": 200000, - "min": 90000, - }, - "field": "timestamp", - "fixed_interval": "myInterval", - "min_doc_count": 0, - }, - }, - }, - "query": Object { - "bool": Object { - "filter": Array [ - Object { - "term": Object { - "job_id": "myservicename-mytransactiontype-high_mean_response_time", - }, - }, - Object { - "exists": Object { - "field": "bucket_span", - }, - }, - Object { - "range": Object { - "timestamp": Object { - "format": "epoch_millis", - "gte": 90000, - "lte": 200000, - }, - }, - }, - ], - }, - }, - "size": 0, - }, - }, - ], -] -`; diff --git a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/__snapshots__/index.test.ts.snap b/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/__snapshots__/index.test.ts.snap deleted file mode 100644 index 971fa3b92cc83f..00000000000000 --- a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/__snapshots__/index.test.ts.snap +++ /dev/null @@ -1,38 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`getAnomalySeries should match snapshot 1`] = ` -Object { - "anomalyBoundaries": Array [ - Object { - "x": 5000, - "y": 200, - "y0": 20, - }, - Object { - "x": 15000, - "y": 100, - "y0": 20, - }, - Object { - "x": 25000, - "y": 50, - "y0": 10, - }, - Object { - "x": 30000, - "y": 50, - "y0": 10, - }, - ], - "anomalyScore": Array [ - Object { - "x": 25000, - "x0": 15000, - }, - Object { - "x": 35000, - "x0": 25000, - }, - ], -} -`; diff --git a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/__snapshots__/transform.test.ts.snap b/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/__snapshots__/transform.test.ts.snap deleted file mode 100644 index 8cf471cb34ed26..00000000000000 --- a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/__snapshots__/transform.test.ts.snap +++ /dev/null @@ -1,33 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`anomalySeriesTransform should match snapshot 1`] = ` -Object { - "anomalyBoundaries": Array [ - Object { - "x": 10000, - "y": 200, - "y0": 20, - }, - Object { - "x": 15000, - "y": 100, - "y0": 20, - }, - Object { - "x": 25000, - "y": 50, - "y0": 10, - }, - ], - "anomalyScore": Array [ - Object { - "x": 25000, - "x0": 15000, - }, - Object { - "x": 25000, - "x0": 25000, - }, - ], -} -`; diff --git a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/fetcher.test.ts b/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/fetcher.test.ts deleted file mode 100644 index 313cf818a322da..00000000000000 --- a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/fetcher.test.ts +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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 { anomalySeriesFetcher, ESResponse } from './fetcher'; - -describe('anomalyAggsFetcher', () => { - describe('when ES returns valid response', () => { - let response: ESResponse | undefined; - let clientSpy: jest.Mock; - - beforeEach(async () => { - clientSpy = jest.fn().mockReturnValue('ES Response'); - response = await anomalySeriesFetcher({ - serviceName: 'myServiceName', - transactionType: 'myTransactionType', - intervalString: 'myInterval', - mlBucketSize: 10, - setup: { - ml: { - mlSystem: { - mlAnomalySearch: clientSpy, - }, - } as any, - start: 100000, - end: 200000, - } as any, - }); - }); - - it('should call client with correct query', () => { - expect(clientSpy.mock.calls).toMatchSnapshot(); - }); - - it('should return correct response', () => { - expect(response).toBe('ES Response'); - }); - }); - - it('should swallow HTTP errors', () => { - const httpError = new Error('anomaly lookup failed') as any; - httpError.statusCode = 418; - const failedRequestSpy = jest.fn(() => Promise.reject(httpError)); - - return expect( - anomalySeriesFetcher({ - setup: { - ml: { - mlSystem: { - mlAnomalySearch: failedRequestSpy, - }, - } as any, - }, - } as any) - ).resolves.toEqual(undefined); - }); - - it('should throw other errors', () => { - const otherError = new Error('anomaly lookup ASPLODED') as any; - const failedRequestSpy = jest.fn(() => Promise.reject(otherError)); - - return expect( - anomalySeriesFetcher({ - setup: { - ml: { - mlSystem: { - mlAnomalySearch: failedRequestSpy, - }, - } as any, - }, - } as any) - ).rejects.toThrow(otherError); - }); -}); diff --git a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/fetcher.ts b/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/fetcher.ts deleted file mode 100644 index 8ee078de7f3ce1..00000000000000 --- a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/fetcher.ts +++ /dev/null @@ -1,90 +0,0 @@ -/* - * 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 { getMlJobId } from '../../../../../common/ml_job_constants'; -import { PromiseReturnType } from '../../../../../../observability/typings/common'; -import { Setup, SetupTimeRange } from '../../../helpers/setup_request'; - -export type ESResponse = Exclude< - PromiseReturnType, - undefined ->; - -export async function anomalySeriesFetcher({ - serviceName, - transactionType, - intervalString, - mlBucketSize, - setup, -}: { - serviceName: string; - transactionType: string; - intervalString: string; - mlBucketSize: number; - setup: Setup & SetupTimeRange; -}) { - const { ml, start, end } = setup; - if (!ml) { - return; - } - - // move the start back with one bucket size, to ensure to get anomaly data in the beginning - // this is required because ML has a minimum bucket size (default is 900s) so if our buckets are smaller, we might have several null buckets in the beginning - const newStart = start - mlBucketSize * 1000; - const jobId = getMlJobId(serviceName, transactionType); - - const params = { - body: { - size: 0, - query: { - bool: { - filter: [ - { term: { job_id: jobId } }, - { exists: { field: 'bucket_span' } }, - { - range: { - timestamp: { - gte: newStart, - lte: end, - format: 'epoch_millis', - }, - }, - }, - ], - }, - }, - aggs: { - ml_avg_response_times: { - date_histogram: { - field: 'timestamp', - fixed_interval: intervalString, - min_doc_count: 0, - extended_bounds: { - min: newStart, - max: end, - }, - }, - aggs: { - anomaly_score: { max: { field: 'anomaly_score' } }, - lower: { min: { field: 'model_lower' } }, - upper: { max: { field: 'model_upper' } }, - }, - }, - }, - }, - }; - - try { - const response = await ml.mlSystem.mlAnomalySearch(params); - return response; - } catch (err) { - const isHttpError = 'statusCode' in err; - if (isHttpError) { - return; - } - throw err; - } -} diff --git a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/get_ml_bucket_size.ts b/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/get_ml_bucket_size.ts deleted file mode 100644 index d649bfb1927390..00000000000000 --- a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/get_ml_bucket_size.ts +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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 { getMlJobId } from '../../../../../common/ml_job_constants'; -import { Setup, SetupTimeRange } from '../../../helpers/setup_request'; - -interface IOptions { - serviceName: string; - transactionType: string; - setup: Setup & SetupTimeRange; -} - -interface ESResponse { - bucket_span: number; -} - -export async function getMlBucketSize({ - serviceName, - transactionType, - setup, -}: IOptions): Promise { - const { ml, start, end } = setup; - if (!ml) { - return 0; - } - const jobId = getMlJobId(serviceName, transactionType); - - const params = { - body: { - _source: 'bucket_span', - size: 1, - query: { - bool: { - filter: [ - { term: { job_id: jobId } }, - { exists: { field: 'bucket_span' } }, - { - range: { - timestamp: { - gte: start, - lte: end, - format: 'epoch_millis', - }, - }, - }, - ], - }, - }, - }, - }; - - try { - const resp = await ml.mlSystem.mlAnomalySearch(params); - return resp.hits.hits[0]?._source.bucket_span || 0; - } catch (err) { - const isHttpError = 'statusCode' in err; - if (isHttpError) { - return 0; - } - throw err; - } -} diff --git a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/index.test.ts b/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/index.test.ts deleted file mode 100644 index fb87f1b5707d14..00000000000000 --- a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/index.test.ts +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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 { getAnomalySeries } from '.'; -import { mlAnomalyResponse } from './mock_responses/ml_anomaly_response'; -import { mlBucketSpanResponse } from './mock_responses/ml_bucket_span_response'; -import { PromiseReturnType } from '../../../../../../observability/typings/common'; -import { APMConfig } from '../../../..'; - -describe('getAnomalySeries', () => { - let avgAnomalies: PromiseReturnType; - beforeEach(async () => { - const clientSpy = jest - .fn() - .mockResolvedValueOnce(mlBucketSpanResponse) - .mockResolvedValueOnce(mlAnomalyResponse); - - avgAnomalies = await getAnomalySeries({ - serviceName: 'myServiceName', - transactionType: 'myTransactionType', - transactionName: undefined, - timeSeriesDates: [100, 100000], - setup: { - start: 0, - end: 500000, - client: { search: () => {} } as any, - internalClient: { search: () => {} } as any, - config: new Proxy( - {}, - { - get: () => 'myIndex', - } - ) as APMConfig, - uiFiltersES: [], - indices: { - 'apm_oss.sourcemapIndices': 'myIndex', - 'apm_oss.errorIndices': 'myIndex', - 'apm_oss.onboardingIndices': 'myIndex', - 'apm_oss.spanIndices': 'myIndex', - 'apm_oss.transactionIndices': 'myIndex', - 'apm_oss.metricsIndices': 'myIndex', - apmAgentConfigurationIndex: 'myIndex', - apmCustomLinkIndex: 'myIndex', - }, - dynamicIndexPattern: null as any, - ml: { - mlSystem: { - mlAnomalySearch: clientSpy, - mlCapabilities: async () => ({ isPlatinumOrTrialLicense: true }), - }, - } as any, - }, - }); - }); - - it('should remove buckets lower than threshold and outside date range from anomalyScore', () => { - expect(avgAnomalies!.anomalyScore).toEqual([ - { x0: 15000, x: 25000 }, - { x0: 25000, x: 35000 }, - ]); - }); - - it('should remove buckets outside date range from anomalyBoundaries', () => { - expect( - avgAnomalies!.anomalyBoundaries!.filter( - (bucket) => bucket.x < 100 || bucket.x > 100000 - ).length - ).toBe(0); - }); - - it('should remove buckets with null from anomalyBoundaries', () => { - expect( - avgAnomalies!.anomalyBoundaries!.filter((p) => p.y === null).length - ).toBe(0); - }); - - it('should match snapshot', async () => { - expect(avgAnomalies).toMatchSnapshot(); - }); -}); diff --git a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/index.ts b/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/index.ts index 6f44cfa1df9f06..b2d11f2ffe19a6 100644 --- a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/index.ts +++ b/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/index.ts @@ -4,15 +4,17 @@ * you may not use this file except in compliance with the Elastic License. */ -import { getBucketSize } from '../../../helpers/get_bucket_size'; import { Setup, SetupTimeRange, SetupUIFilters, } from '../../../helpers/setup_request'; -import { anomalySeriesFetcher } from './fetcher'; -import { getMlBucketSize } from './get_ml_bucket_size'; -import { anomalySeriesTransform } from './transform'; +import { Coordinate, RectCoordinate } from '../../../../../typings/timeseries'; + +interface AnomalyTimeseries { + anomalyBoundaries: Coordinate[]; + anomalyScore: RectCoordinate[]; +} export async function getAnomalySeries({ serviceName, @@ -26,7 +28,7 @@ export async function getAnomalySeries({ transactionName: string | undefined; timeSeriesDates: number[]; setup: Setup & SetupTimeRange & SetupUIFilters; -}) { +}): Promise { // don't fetch anomalies for transaction details page if (transactionName) { return; @@ -53,29 +55,6 @@ export async function getAnomalySeries({ return; } - const mlBucketSize = await getMlBucketSize({ - serviceName, - transactionType, - setup, - }); - - const { start, end } = setup; - const { intervalString, bucketSize } = getBucketSize(start, end, 'auto'); - - const esResponse = await anomalySeriesFetcher({ - serviceName, - transactionType, - intervalString, - mlBucketSize, - setup, - }); - - return esResponse - ? anomalySeriesTransform( - esResponse, - mlBucketSize, - bucketSize, - timeSeriesDates - ) - : undefined; + // TODO [APM ML] return a series of anomaly scores, upper & lower bounds for the given timeSeriesDates + return; } diff --git a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/mock_responses/ml_anomaly_response.ts b/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/mock_responses/ml_anomaly_response.ts deleted file mode 100644 index 523161ec102759..00000000000000 --- a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/mock_responses/ml_anomaly_response.ts +++ /dev/null @@ -1,127 +0,0 @@ -/* - * 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 { ESResponse } from '../fetcher'; - -export const mlAnomalyResponse: ESResponse = ({ - took: 3, - timed_out: false, - _shards: { - total: 5, - successful: 5, - skipped: 0, - failed: 0, - }, - hits: { - total: 10, - max_score: 0, - hits: [], - }, - aggregations: { - ml_avg_response_times: { - buckets: [ - { - key_as_string: '2018-07-02T09:16:40.000Z', - key: 0, - doc_count: 0, - anomaly_score: { - value: null, - }, - upper: { - value: 200, - }, - lower: { - value: 20, - }, - }, - { - key_as_string: '2018-07-02T09:25:00.000Z', - key: 5000, - doc_count: 4, - anomaly_score: { - value: null, - }, - upper: { - value: null, - }, - lower: { - value: null, - }, - }, - { - key_as_string: '2018-07-02T09:33:20.000Z', - key: 10000, - doc_count: 0, - anomaly_score: { - value: null, - }, - upper: { - value: null, - }, - lower: { - value: null, - }, - }, - { - key_as_string: '2018-07-02T09:41:40.000Z', - key: 15000, - doc_count: 2, - anomaly_score: { - value: 90, - }, - upper: { - value: 100, - }, - lower: { - value: 20, - }, - }, - { - key_as_string: '2018-07-02T09:50:00.000Z', - key: 20000, - doc_count: 0, - anomaly_score: { - value: null, - }, - upper: { - value: null, - }, - lower: { - value: null, - }, - }, - { - key_as_string: '2018-07-02T09:58:20.000Z', - key: 25000, - doc_count: 2, - anomaly_score: { - value: 100, - }, - upper: { - value: 50, - }, - lower: { - value: 10, - }, - }, - { - key_as_string: '2018-07-02T10:15:00.000Z', - key: 30000, - doc_count: 2, - anomaly_score: { - value: 0, - }, - upper: { - value: null, - }, - lower: { - value: null, - }, - }, - ], - }, - }, -} as unknown) as ESResponse; diff --git a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/mock_responses/ml_bucket_span_response.ts b/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/mock_responses/ml_bucket_span_response.ts deleted file mode 100644 index 3689529a07c4a9..00000000000000 --- a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/mock_responses/ml_bucket_span_response.ts +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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 mlBucketSpanResponse = { - took: 1, - timed_out: false, - _shards: { - total: 1, - successful: 1, - skipped: 0, - failed: 0, - }, - hits: { - total: 192, - max_score: 1.0, - hits: [ - { - _index: '.ml-anomalies-shared', - _id: - 'opbeans-go-request-high_mean_response_time_model_plot_1542636000000_900_0_29791_0', - _score: 1.0, - _source: { - bucket_span: 10, - }, - }, - ], - }, -}; diff --git a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/transform.test.ts b/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/transform.test.ts deleted file mode 100644 index eb94c83e92576d..00000000000000 --- a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/transform.test.ts +++ /dev/null @@ -1,303 +0,0 @@ -/* - * 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 { ESResponse } from './fetcher'; -import { mlAnomalyResponse } from './mock_responses/ml_anomaly_response'; -import { anomalySeriesTransform, replaceFirstAndLastBucket } from './transform'; - -describe('anomalySeriesTransform', () => { - it('should match snapshot', () => { - const getMlBucketSize = 10; - const bucketSize = 5; - const timeSeriesDates = [10000, 25000]; - const anomalySeries = anomalySeriesTransform( - mlAnomalyResponse, - getMlBucketSize, - bucketSize, - timeSeriesDates - ); - expect(anomalySeries).toMatchSnapshot(); - }); - - describe('anomalyScoreSeries', () => { - it('should only returns bucket within range and above threshold', () => { - const esResponse = getESResponse([ - { - key: 0, - anomaly_score: { value: 90 }, - }, - { - key: 5000, - anomaly_score: { value: 0 }, - }, - { - key: 10000, - anomaly_score: { value: 90 }, - }, - { - key: 15000, - anomaly_score: { value: 0 }, - }, - { - key: 20000, - anomaly_score: { value: 90 }, - }, - ]); - - const getMlBucketSize = 5; - const bucketSize = 5; - const timeSeriesDates = [5000, 15000]; - const anomalySeries = anomalySeriesTransform( - esResponse, - getMlBucketSize, - bucketSize, - timeSeriesDates - ); - - const buckets = anomalySeries!.anomalyScore; - expect(buckets).toEqual([{ x0: 10000, x: 15000 }]); - }); - - it('should decrease the x-value to avoid going beyond last date', () => { - const esResponse = getESResponse([ - { - key: 0, - anomaly_score: { value: 0 }, - }, - { - key: 5000, - anomaly_score: { value: 90 }, - }, - ]); - - const getMlBucketSize = 10; - const bucketSize = 5; - const timeSeriesDates = [0, 10000]; - const anomalySeries = anomalySeriesTransform( - esResponse, - getMlBucketSize, - bucketSize, - timeSeriesDates - ); - - const buckets = anomalySeries!.anomalyScore; - expect(buckets).toEqual([{ x0: 5000, x: 10000 }]); - }); - }); - - describe('anomalyBoundariesSeries', () => { - it('should trim buckets to time range', () => { - const esResponse = getESResponse([ - { - key: 0, - upper: { value: 15 }, - lower: { value: 10 }, - }, - { - key: 5000, - upper: { value: 25 }, - lower: { value: 20 }, - }, - { - key: 10000, - upper: { value: 35 }, - lower: { value: 30 }, - }, - { - key: 15000, - upper: { value: 45 }, - lower: { value: 40 }, - }, - ]); - - const mlBucketSize = 10; - const bucketSize = 5; - const timeSeriesDates = [5000, 10000]; - const anomalySeries = anomalySeriesTransform( - esResponse, - mlBucketSize, - bucketSize, - timeSeriesDates - ); - - const buckets = anomalySeries!.anomalyBoundaries; - expect(buckets).toEqual([ - { x: 5000, y: 25, y0: 20 }, - { x: 10000, y: 35, y0: 30 }, - ]); - }); - - it('should replace first bucket in range', () => { - const esResponse = getESResponse([ - { - key: 0, - anomaly_score: { value: 0 }, - upper: { value: 15 }, - lower: { value: 10 }, - }, - { - key: 5000, - anomaly_score: { value: 0 }, - upper: { value: null }, - lower: { value: null }, - }, - { - key: 10000, - anomaly_score: { value: 0 }, - upper: { value: 25 }, - lower: { value: 20 }, - }, - ]); - - const getMlBucketSize = 10; - const bucketSize = 5; - const timeSeriesDates = [5000, 10000]; - const anomalySeries = anomalySeriesTransform( - esResponse, - getMlBucketSize, - bucketSize, - timeSeriesDates - ); - - const buckets = anomalySeries!.anomalyBoundaries; - expect(buckets).toEqual([ - { x: 5000, y: 15, y0: 10 }, - { x: 10000, y: 25, y0: 20 }, - ]); - }); - - it('should replace last bucket in range', () => { - const esResponse = getESResponse([ - { - key: 0, - anomaly_score: { value: 0 }, - upper: { value: 15 }, - lower: { value: 10 }, - }, - { - key: 5000, - anomaly_score: { value: 0 }, - upper: { value: null }, - lower: { value: null }, - }, - { - key: 10000, - anomaly_score: { value: 0 }, - upper: { value: null }, - lower: { value: null }, - }, - ]); - - const getMlBucketSize = 10; - const bucketSize = 5; - const timeSeriesDates = [5000, 10000]; - const anomalySeries = anomalySeriesTransform( - esResponse, - getMlBucketSize, - bucketSize, - timeSeriesDates - ); - - const buckets = anomalySeries!.anomalyBoundaries; - expect(buckets).toEqual([ - { x: 5000, y: 15, y0: 10 }, - { x: 10000, y: 15, y0: 10 }, - ]); - }); - }); -}); - -describe('replaceFirstAndLastBucket', () => { - it('should extend the first bucket', () => { - const buckets = [ - { - x: 0, - lower: 10, - upper: 20, - }, - { - x: 5, - lower: null, - upper: null, - }, - { - x: 10, - lower: null, - upper: null, - }, - { - x: 15, - lower: 30, - upper: 40, - }, - ]; - - const timeSeriesDates = [10, 15]; - expect(replaceFirstAndLastBucket(buckets as any, timeSeriesDates)).toEqual([ - { x: 10, lower: 10, upper: 20 }, - { x: 15, lower: 30, upper: 40 }, - ]); - }); - - it('should extend the last bucket', () => { - const buckets = [ - { - x: 10, - lower: 30, - upper: 40, - }, - { - x: 15, - lower: null, - upper: null, - }, - { - x: 20, - lower: null, - upper: null, - }, - ] as any; - - const timeSeriesDates = [10, 15, 20]; - expect(replaceFirstAndLastBucket(buckets, timeSeriesDates)).toEqual([ - { x: 10, lower: 30, upper: 40 }, - { x: 15, lower: null, upper: null }, - { x: 20, lower: 30, upper: 40 }, - ]); - }); -}); - -function getESResponse(buckets: any): ESResponse { - return ({ - took: 3, - timed_out: false, - _shards: { - total: 5, - successful: 5, - skipped: 0, - failed: 0, - }, - hits: { - total: 10, - max_score: 0, - hits: [], - }, - aggregations: { - ml_avg_response_times: { - buckets: buckets.map((bucket: any) => { - return { - ...bucket, - lower: { value: bucket?.lower?.value || null }, - upper: { value: bucket?.upper?.value || null }, - anomaly_score: { - value: bucket?.anomaly_score?.value || null, - }, - }; - }), - }, - }, - } as unknown) as ESResponse; -} diff --git a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/transform.ts b/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/transform.ts deleted file mode 100644 index 454a6add3e2562..00000000000000 --- a/x-pack/plugins/apm/server/lib/transactions/charts/get_anomaly_data/transform.ts +++ /dev/null @@ -1,126 +0,0 @@ -/* - * 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 { first, last } from 'lodash'; -import { Coordinate, RectCoordinate } from '../../../../../typings/timeseries'; -import { ESResponse } from './fetcher'; - -type IBucket = ReturnType; -function getBucket( - bucket: Required< - ESResponse - >['aggregations']['ml_avg_response_times']['buckets'][0] -) { - return { - x: bucket.key, - anomalyScore: bucket.anomaly_score.value, - lower: bucket.lower.value, - upper: bucket.upper.value, - }; -} - -export type AnomalyTimeSeriesResponse = ReturnType< - typeof anomalySeriesTransform ->; -export function anomalySeriesTransform( - response: ESResponse, - mlBucketSize: number, - bucketSize: number, - timeSeriesDates: number[] -) { - const buckets = - response.aggregations?.ml_avg_response_times.buckets.map(getBucket) || []; - - const bucketSizeInMillis = Math.max(bucketSize, mlBucketSize) * 1000; - - return { - anomalyScore: getAnomalyScoreDataPoints( - buckets, - timeSeriesDates, - bucketSizeInMillis - ), - anomalyBoundaries: getAnomalyBoundaryDataPoints(buckets, timeSeriesDates), - }; -} - -export function getAnomalyScoreDataPoints( - buckets: IBucket[], - timeSeriesDates: number[], - bucketSizeInMillis: number -): RectCoordinate[] { - const ANOMALY_THRESHOLD = 75; - const firstDate = first(timeSeriesDates); - const lastDate = last(timeSeriesDates); - - return buckets - .filter( - (bucket) => - bucket.anomalyScore !== null && bucket.anomalyScore > ANOMALY_THRESHOLD - ) - .filter(isInDateRange(firstDate, lastDate)) - .map((bucket) => { - return { - x0: bucket.x, - x: Math.min(bucket.x + bucketSizeInMillis, lastDate), // don't go beyond last date - }; - }); -} - -export function getAnomalyBoundaryDataPoints( - buckets: IBucket[], - timeSeriesDates: number[] -): Coordinate[] { - return replaceFirstAndLastBucket(buckets, timeSeriesDates) - .filter((bucket) => bucket.lower !== null) - .map((bucket) => { - return { - x: bucket.x, - y0: bucket.lower, - y: bucket.upper, - }; - }); -} - -export function replaceFirstAndLastBucket( - buckets: IBucket[], - timeSeriesDates: number[] -) { - const firstDate = first(timeSeriesDates); - const lastDate = last(timeSeriesDates); - - const preBucketWithValue = buckets - .filter((p) => p.x <= firstDate) - .reverse() - .find((p) => p.lower !== null); - - const bucketsInRange = buckets.filter(isInDateRange(firstDate, lastDate)); - - // replace first bucket if it is null - const firstBucket = first(bucketsInRange); - if (preBucketWithValue && firstBucket && firstBucket.lower === null) { - firstBucket.lower = preBucketWithValue.lower; - firstBucket.upper = preBucketWithValue.upper; - } - - const lastBucketWithValue = [...buckets] - .reverse() - .find((p) => p.lower !== null); - - // replace last bucket if it is null - const lastBucket = last(bucketsInRange); - if (lastBucketWithValue && lastBucket && lastBucket.lower === null) { - lastBucket.lower = lastBucketWithValue.lower; - lastBucket.upper = lastBucketWithValue.upper; - } - - return bucketsInRange; -} - -// anomaly time series contain one or more buckets extra in the beginning -// these extra buckets should be removed -function isInDateRange(firstDate: number, lastDate: number) { - return (p: IBucket) => p.x >= firstDate && p.x <= lastDate; -} diff --git a/x-pack/plugins/canvas/.storybook/config.js b/x-pack/plugins/canvas/.storybook/config.js index c808a672711aba..04b4e2a8e7b4b0 100644 --- a/x-pack/plugins/canvas/.storybook/config.js +++ b/x-pack/plugins/canvas/.storybook/config.js @@ -59,6 +59,9 @@ function loadStories() { // Find all files ending in *.examples.ts const req = require.context('./..', true, /.(stories|examples).tsx$/); req.keys().forEach(filename => req(filename)); + + // Import Canvas CSS + require('../public/style/index.scss') } // Set up the Storybook environment with custom settings. diff --git a/x-pack/plugins/canvas/.storybook/webpack.config.js b/x-pack/plugins/canvas/.storybook/webpack.config.js index 4d83a3d4fa70f9..45a5303d8b0db1 100644 --- a/x-pack/plugins/canvas/.storybook/webpack.config.js +++ b/x-pack/plugins/canvas/.storybook/webpack.config.js @@ -199,6 +199,7 @@ module.exports = async ({ config }) => { config.resolve.alias['ui/url/absolute_to_parsed_url'] = path.resolve(__dirname, '../tasks/mocks/uiAbsoluteToParsedUrl'); config.resolve.alias['ui/chrome'] = path.resolve(__dirname, '../tasks/mocks/uiChrome'); config.resolve.alias.ui = path.resolve(KIBANA_ROOT, 'src/legacy/ui/public'); + config.resolve.alias['src/legacy/ui/public/styles/styling_constants'] = path.resolve(KIBANA_ROOT, 'src/legacy/ui/public/styles/_styling_constants.scss'); config.resolve.alias.ng_mock$ = path.resolve(KIBANA_ROOT, 'src/test_utils/public/ng_mock'); return config; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/palette.test.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/palette.test.js index af03297ad666ba..01cabd171c2fed 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/palette.test.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/palette.test.js @@ -5,7 +5,7 @@ */ import { functionWrapper } from '../../../__tests__/helpers/function_wrapper'; -import { palettes } from '../../../common/lib/palettes'; +import { paulTor14 } from '../../../common/lib/palettes'; import { palette } from './palette'; describe('palette', () => { @@ -25,7 +25,7 @@ describe('palette', () => { it('defaults to pault_tor_14 colors', () => { const result = fn(null); - expect(result.colors).toEqual(palettes.paul_tor_14.colors); + expect(result.colors).toEqual(paulTor14.colors); }); }); @@ -47,17 +47,17 @@ describe('palette', () => { describe('reverse', () => { it('reverses order of the colors', () => { const result = fn(null, { reverse: true }); - expect(result.colors).toEqual(palettes.paul_tor_14.colors.reverse()); + expect(result.colors).toEqual(paulTor14.colors.reverse()); }); it('keeps the original order of the colors', () => { const result = fn(null, { reverse: false }); - expect(result.colors).toEqual(palettes.paul_tor_14.colors); + expect(result.colors).toEqual(paulTor14.colors); }); it(`defaults to 'false`, () => { const result = fn(null); - expect(result.colors).toEqual(palettes.paul_tor_14.colors); + expect(result.colors).toEqual(paulTor14.colors); }); }); }); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/palette.ts b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/palette.ts index f27abe261e2e20..50d62a19b23612 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/palette.ts +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/palette.ts @@ -5,8 +5,7 @@ */ import { ExpressionFunctionDefinition } from 'src/plugins/expressions/common'; -// @ts-expect-error untyped local -import { palettes } from '../../../common/lib/palettes'; +import { paulTor14 } from '../../../common/lib/palettes'; import { getFunctionHelp } from '../../../i18n'; interface Arguments { @@ -52,7 +51,7 @@ export function palette(): ExpressionFunctionDefinition<'palette', null, Argumen }, fn: (input, args) => { const { color, reverse, gradient } = args; - const colors = ([] as string[]).concat(color || palettes.paul_tor_14.colors); + const colors = ([] as string[]).concat(color || paulTor14.colors); return { type: 'palette', diff --git a/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/__examples__/__snapshots__/palette.stories.storyshot b/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/__examples__/__snapshots__/palette.stories.storyshot new file mode 100644 index 00000000000000..385b16d3d8e8e1 --- /dev/null +++ b/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/__examples__/__snapshots__/palette.stories.storyshot @@ -0,0 +1,86 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Storyshots arguments/Palette default 1`] = ` +
+
+
+ +
+
+ + Select an option: +
+ , is selected + + +
+ + +
+
+
+
+
+`; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/__examples__/palette.stories.tsx b/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/__examples__/palette.stories.tsx new file mode 100644 index 00000000000000..6bc285a3d66d29 --- /dev/null +++ b/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/__examples__/palette.stories.tsx @@ -0,0 +1,32 @@ +/* + * 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 { storiesOf } from '@storybook/react'; +import React from 'react'; +import { action } from '@storybook/addon-actions'; +import { PaletteArgInput } from '../palette'; +import { paulTor14 } from '../../../../common/lib/palettes'; + +storiesOf('arguments/Palette', module).add('default', () => ( +
+ +
+)); diff --git a/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/index.ts b/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/index.ts index 94a9cf28aef69d..ddf428d884917c 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/index.ts +++ b/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/index.ts @@ -15,7 +15,6 @@ import { imageUpload } from './image_upload'; // @ts-expect-error untyped local import { number } from './number'; import { numberFormatInitializer } from './number_format'; -// @ts-expect-error untyped local import { palette } from './palette'; // @ts-expect-error untyped local import { percentage } from './percentage'; diff --git a/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/palette.js b/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/palette.tsx similarity index 58% rename from x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/palette.js rename to x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/palette.tsx index eddaa20a4800ee..a33d000a1f6563 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/palette.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/uis/arguments/palette.tsx @@ -4,45 +4,63 @@ * you may not use this file except in compliance with the Elastic License. */ -import React from 'react'; +import React, { FC } from 'react'; import PropTypes from 'prop-types'; import { get } from 'lodash'; import { getType } from '@kbn/interpreter/common'; +import { ExpressionAstFunction, ExpressionAstExpression } from 'src/plugins/expressions'; import { PalettePicker } from '../../../public/components/palette_picker'; import { templateFromReactComponent } from '../../../public/lib/template_from_react_component'; import { ArgumentStrings } from '../../../i18n'; +import { identifyPalette, ColorPalette } from '../../../common/lib'; const { Palette: strings } = ArgumentStrings; -const PaletteArgInput = ({ onValueChange, argValue, renderError }) => { - // Why is this neccesary? Does the dialog really need to know what parameter it is setting? - - const throwNotParsed = () => renderError(); +interface Props { + onValueChange: (value: ExpressionAstExpression) => void; + argValue: ExpressionAstExpression; + renderError: () => void; + argId?: string; +} +export const PaletteArgInput: FC = ({ onValueChange, argId, argValue, renderError }) => { // TODO: This is weird, its basically a reimplementation of what the interpretter would return. - // Probably a better way todo this, and maybe a better way to handle template stype objects in general? - function astToPalette({ chain }) { + // Probably a better way todo this, and maybe a better way to handle template type objects in general? + const astToPalette = ({ chain }: { chain: ExpressionAstFunction[] }): ColorPalette | null => { if (chain.length !== 1 || chain[0].function !== 'palette') { - throwNotParsed(); + renderError(); + return null; } + try { const colors = chain[0].arguments._.map((astObj) => { if (getType(astObj) !== 'string') { - throwNotParsed(); + renderError(); } return astObj; - }); + }) as string[]; - const gradient = get(chain[0].arguments.gradient, '[0]'); + const gradient = get(chain[0].arguments.gradient, '[0]'); + const palette = identifyPalette({ colors, gradient }); - return { colors, gradient }; + if (palette) { + return palette; + } + + return ({ + id: 'custom', + label: strings.getCustomPaletteLabel(), + colors, + gradient, + } as any) as ColorPalette; } catch (e) { - throwNotParsed(); + renderError(); } - } + return null; + }; - function handleChange(palette) { - const astObj = { + const handleChange = (palette: ColorPalette): void => { + const astObj: ExpressionAstExpression = { type: 'expression', chain: [ { @@ -57,16 +75,20 @@ const PaletteArgInput = ({ onValueChange, argValue, renderError }) => { }; onValueChange(astObj); - } + }; const palette = astToPalette(argValue); - return ( - - ); + if (!palette) { + renderError(); + return null; + } + + return ; }; PaletteArgInput.propTypes = { + argId: PropTypes.string, onValueChange: PropTypes.func.isRequired, argValue: PropTypes.any.isRequired, renderError: PropTypes.func, diff --git a/x-pack/plugins/canvas/common/lib/index.ts b/x-pack/plugins/canvas/common/lib/index.ts index 4cb3cbbb9b4e6d..6bd7e0bc9948fd 100644 --- a/x-pack/plugins/canvas/common/lib/index.ts +++ b/x-pack/plugins/canvas/common/lib/index.ts @@ -26,7 +26,6 @@ export * from './hex_to_rgb'; export * from './httpurl'; // @ts-expect-error missing local definition export * from './missing_asset'; -// @ts-expect-error missing local definition export * from './palettes'; export * from './pivot_object_array'; // @ts-expect-error missing local definition diff --git a/x-pack/plugins/canvas/common/lib/palettes.js b/x-pack/plugins/canvas/common/lib/palettes.js deleted file mode 100644 index 3fe977ec3862c4..00000000000000 --- a/x-pack/plugins/canvas/common/lib/palettes.js +++ /dev/null @@ -1,152 +0,0 @@ -/* - * 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. - */ - -/* - This should be pluggable -*/ - -export const palettes = { - paul_tor_14: { - colors: [ - '#882E72', - '#B178A6', - '#D6C1DE', - '#1965B0', - '#5289C7', - '#7BAFDE', - '#4EB265', - '#90C987', - '#CAE0AB', - '#F7EE55', - '#F6C141', - '#F1932D', - '#E8601C', - '#DC050C', - ], - gradient: false, - }, - paul_tor_21: { - colors: [ - '#771155', - '#AA4488', - '#CC99BB', - '#114477', - '#4477AA', - '#77AADD', - '#117777', - '#44AAAA', - '#77CCCC', - '#117744', - '#44AA77', - '#88CCAA', - '#777711', - '#AAAA44', - '#DDDD77', - '#774411', - '#AA7744', - '#DDAA77', - '#771122', - '#AA4455', - '#DD7788', - ], - gradient: false, - }, - earth_tones: { - colors: [ - '#842113', - '#984d23', - '#32221c', - '#739379', - '#dab150', - '#4d2521', - '#716c49', - '#bb3918', - '#7e5436', - '#c27c34', - '#72392e', - '#8f8b7e', - ], - gradient: false, - }, - canvas: { - colors: [ - '#01A4A4', - '#CC6666', - '#D0D102', - '#616161', - '#00A1CB', - '#32742C', - '#F18D05', - '#113F8C', - '#61AE24', - '#D70060', - ], - gradient: false, - }, - color_blind: { - colors: [ - '#1ea593', - '#2b70f7', - '#ce0060', - '#38007e', - '#fca5d3', - '#f37020', - '#e49e29', - '#b0916f', - '#7b000b', - '#34130c', - ], - gradient: false, - }, - elastic_teal: { - colors: ['#C5FAF4', '#0F6259'], - gradient: true, - }, - elastic_blue: { - colors: ['#7ECAE3', '#003A4D'], - gradient: true, - }, - elastic_yellow: { - colors: ['#FFE674', '#4D3F00'], - gradient: true, - }, - elastic_pink: { - colors: ['#FEA8D5', '#531E3A'], - gradient: true, - }, - elastic_green: { - colors: ['#D3FB71', '#131A00'], - gradient: true, - }, - elastic_orange: { - colors: ['#FFC68A', '#7B3F00'], - gradient: true, - }, - elastic_purple: { - colors: ['#CCC7DF', '#130351'], - gradient: true, - }, - green_blue_red: { - colors: ['#D3FB71', '#7ECAE3', '#f03b20'], - gradient: true, - }, - yellow_green: { - colors: ['#f7fcb9', '#addd8e', '#31a354'], - gradient: true, - }, - yellow_blue: { - colors: ['#edf8b1', '#7fcdbb', '#2c7fb8'], - gradient: true, - }, - yellow_red: { - colors: ['#ffeda0', '#feb24c', '#f03b20'], - gradient: true, - }, - instagram: { - colors: ['#833ab4', '#fd1d1d', '#fcb045'], - gradient: true, - }, -}; diff --git a/x-pack/plugins/canvas/common/lib/palettes.ts b/x-pack/plugins/canvas/common/lib/palettes.ts new file mode 100644 index 00000000000000..1469ba63967c0e --- /dev/null +++ b/x-pack/plugins/canvas/common/lib/palettes.ts @@ -0,0 +1,263 @@ +/* + * 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 { isEqual } from 'lodash'; +import { LibStrings } from '../../i18n'; + +const { Palettes: strings } = LibStrings; + +/** + * This type contains a unions of all supported palette ids. + */ +export type PaletteID = typeof palettes[number]['id']; + +/** + * An interface representing a color palette in Canvas, with a textual label and a set of + * hex values. + */ +export interface ColorPalette { + id: PaletteID; + label: string; + colors: string[]; + gradient: boolean; +} + +// This function allows one to create a strongly-typed palette for inclusion in +// the palette collection. As a result, the values and labels are known to the +// type system, preventing one from specifying a non-existent palette at build +// time. +function createPalette< + RawPalette extends { + id: RawPaletteID; + }, + RawPaletteID extends string +>(palette: RawPalette) { + return palette; +} + +/** + * Return a palette given a set of colors and gradient. Returns undefined if the + * palette doesn't match. + */ +export const identifyPalette = ( + input: Pick +): ColorPalette | undefined => { + return palettes.find((palette) => { + const { colors, gradient } = palette; + return gradient === input.gradient && isEqual(colors, input.colors); + }); +}; + +export const paulTor14 = createPalette({ + id: 'paul_tor_14', + label: 'Paul Tor 14', + colors: [ + '#882E72', + '#B178A6', + '#D6C1DE', + '#1965B0', + '#5289C7', + '#7BAFDE', + '#4EB265', + '#90C987', + '#CAE0AB', + '#F7EE55', + '#F6C141', + '#F1932D', + '#E8601C', + '#DC050C', + ], + gradient: false, +}); + +export const paulTor21 = createPalette({ + id: 'paul_tor_21', + label: 'Paul Tor 21', + colors: [ + '#771155', + '#AA4488', + '#CC99BB', + '#114477', + '#4477AA', + '#77AADD', + '#117777', + '#44AAAA', + '#77CCCC', + '#117744', + '#44AA77', + '#88CCAA', + '#777711', + '#AAAA44', + '#DDDD77', + '#774411', + '#AA7744', + '#DDAA77', + '#771122', + '#AA4455', + '#DD7788', + ], + gradient: false, +}); + +export const earthTones = createPalette({ + id: 'earth_tones', + label: strings.getEarthTones(), + colors: [ + '#842113', + '#984d23', + '#32221c', + '#739379', + '#dab150', + '#4d2521', + '#716c49', + '#bb3918', + '#7e5436', + '#c27c34', + '#72392e', + '#8f8b7e', + ], + gradient: false, +}); + +export const canvas = createPalette({ + id: 'canvas', + label: strings.getCanvas(), + colors: [ + '#01A4A4', + '#CC6666', + '#D0D102', + '#616161', + '#00A1CB', + '#32742C', + '#F18D05', + '#113F8C', + '#61AE24', + '#D70060', + ], + gradient: false, +}); + +export const colorBlind = createPalette({ + id: 'color_blind', + label: strings.getColorBlind(), + colors: [ + '#1ea593', + '#2b70f7', + '#ce0060', + '#38007e', + '#fca5d3', + '#f37020', + '#e49e29', + '#b0916f', + '#7b000b', + '#34130c', + ], + gradient: false, +}); + +export const elasticTeal = createPalette({ + id: 'elastic_teal', + label: strings.getElasticTeal(), + colors: ['#7ECAE3', '#003A4D'], + gradient: true, +}); + +export const elasticBlue = createPalette({ + id: 'elastic_blue', + label: strings.getElasticBlue(), + colors: ['#C5FAF4', '#0F6259'], + gradient: true, +}); + +export const elasticYellow = createPalette({ + id: 'elastic_yellow', + label: strings.getElasticYellow(), + colors: ['#FFE674', '#4D3F00'], + gradient: true, +}); + +export const elasticPink = createPalette({ + id: 'elastic_pink', + label: strings.getElasticPink(), + colors: ['#FEA8D5', '#531E3A'], + gradient: true, +}); + +export const elasticGreen = createPalette({ + id: 'elastic_green', + label: strings.getElasticGreen(), + colors: ['#D3FB71', '#131A00'], + gradient: true, +}); + +export const elasticOrange = createPalette({ + id: 'elastic_orange', + label: strings.getElasticOrange(), + colors: ['#FFC68A', '#7B3F00'], + gradient: true, +}); + +export const elasticPurple = createPalette({ + id: 'elastic_purple', + label: strings.getElasticPurple(), + colors: ['#CCC7DF', '#130351'], + gradient: true, +}); + +export const greenBlueRed = createPalette({ + id: 'green_blue_red', + label: strings.getGreenBlueRed(), + colors: ['#D3FB71', '#7ECAE3', '#f03b20'], + gradient: true, +}); + +export const yellowGreen = createPalette({ + id: 'yellow_green', + label: strings.getYellowGreen(), + colors: ['#f7fcb9', '#addd8e', '#31a354'], + gradient: true, +}); + +export const yellowBlue = createPalette({ + id: 'yellow_blue', + label: strings.getYellowBlue(), + colors: ['#edf8b1', '#7fcdbb', '#2c7fb8'], + gradient: true, +}); + +export const yellowRed = createPalette({ + id: 'yellow_red', + label: strings.getYellowRed(), + colors: ['#ffeda0', '#feb24c', '#f03b20'], + gradient: true, +}); + +export const instagram = createPalette({ + id: 'instagram', + label: strings.getInstagram(), + colors: ['#833ab4', '#fd1d1d', '#fcb045'], + gradient: true, +}); + +export const palettes = [ + paulTor14, + paulTor21, + earthTones, + canvas, + colorBlind, + elasticTeal, + elasticBlue, + elasticYellow, + elasticPink, + elasticGreen, + elasticOrange, + elasticPurple, + greenBlueRed, + yellowGreen, + yellowBlue, + yellowRed, + instagram, +]; diff --git a/x-pack/plugins/canvas/i18n/components.ts b/x-pack/plugins/canvas/i18n/components.ts index de16bc2101e8cf..0b512c80b209ba 100644 --- a/x-pack/plugins/canvas/i18n/components.ts +++ b/x-pack/plugins/canvas/i18n/components.ts @@ -586,6 +586,16 @@ export const ComponentStrings = { defaultMessage: 'Delete', }), }, + PalettePicker: { + getEmptyPaletteLabel: () => + i18n.translate('xpack.canvas.palettePicker.emptyPaletteLabel', { + defaultMessage: 'None', + }), + getNoPaletteFoundErrorTitle: () => + i18n.translate('xpack.canvas.palettePicker.noPaletteFoundErrorTitle', { + defaultMessage: 'Color palette not found', + }), + }, SavedElementsModal: { getAddNewElementDescription: () => i18n.translate('xpack.canvas.savedElementsModal.addNewElementDescription', { diff --git a/x-pack/plugins/canvas/i18n/constants.ts b/x-pack/plugins/canvas/i18n/constants.ts index 099effc697fc56..af82d0afc7e9ff 100644 --- a/x-pack/plugins/canvas/i18n/constants.ts +++ b/x-pack/plugins/canvas/i18n/constants.ts @@ -20,6 +20,7 @@ export const FONT_FAMILY = '`font-family`'; export const FONT_WEIGHT = '`font-weight`'; export const HEX = 'HEX'; export const HTML = 'HTML'; +export const INSTAGRAM = 'Instagram'; export const ISO8601 = 'ISO8601'; export const JS = 'JavaScript'; export const JSON = 'JSON'; diff --git a/x-pack/plugins/canvas/i18n/index.ts b/x-pack/plugins/canvas/i18n/index.ts index 864311d34aca08..3bf1fa077130cd 100644 --- a/x-pack/plugins/canvas/i18n/index.ts +++ b/x-pack/plugins/canvas/i18n/index.ts @@ -11,6 +11,7 @@ export * from './errors'; export * from './expression_types'; export * from './elements'; export * from './functions'; +export * from './lib'; export * from './renderers'; export * from './shortcuts'; export * from './tags'; diff --git a/x-pack/plugins/canvas/i18n/lib.ts b/x-pack/plugins/canvas/i18n/lib.ts new file mode 100644 index 00000000000000..eca6dc44354a27 --- /dev/null +++ b/x-pack/plugins/canvas/i18n/lib.ts @@ -0,0 +1,92 @@ +/* + * 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 { i18n } from '@kbn/i18n'; +import { CANVAS, INSTAGRAM } from './constants'; + +export const LibStrings = { + Palettes: { + getEarthTones: () => + i18n.translate('xpack.canvas.lib.palettes.earthTonesLabel', { + defaultMessage: 'Earth Tones', + }), + getCanvas: () => + i18n.translate('xpack.canvas.lib.palettes.canvasLabel', { + defaultMessage: '{CANVAS}', + values: { + CANVAS, + }, + }), + + getColorBlind: () => + i18n.translate('xpack.canvas.lib.palettes.colorBlindLabel', { + defaultMessage: 'Color Blind', + }), + + getElasticTeal: () => + i18n.translate('xpack.canvas.lib.palettes.elasticTealLabel', { + defaultMessage: 'Elastic Teal', + }), + + getElasticBlue: () => + i18n.translate('xpack.canvas.lib.palettes.elasticBlueLabel', { + defaultMessage: 'Elastic Blue', + }), + + getElasticYellow: () => + i18n.translate('xpack.canvas.lib.palettes.elasticYellowLabel', { + defaultMessage: 'Elastic Yellow', + }), + + getElasticPink: () => + i18n.translate('xpack.canvas.lib.palettes.elasticPinkLabel', { + defaultMessage: 'Elastic Pink', + }), + + getElasticGreen: () => + i18n.translate('xpack.canvas.lib.palettes.elasticGreenLabel', { + defaultMessage: 'Elastic Green', + }), + + getElasticOrange: () => + i18n.translate('xpack.canvas.lib.palettes.elasticOrangeLabel', { + defaultMessage: 'Elastic Orange', + }), + + getElasticPurple: () => + i18n.translate('xpack.canvas.lib.palettes.elasticPurpleLabel', { + defaultMessage: 'Elastic Purple', + }), + + getGreenBlueRed: () => + i18n.translate('xpack.canvas.lib.palettes.greenBlueRedLabel', { + defaultMessage: 'Green, Blue, Red', + }), + + getYellowGreen: () => + i18n.translate('xpack.canvas.lib.palettes.yellowGreenLabel', { + defaultMessage: 'Yellow, Green', + }), + + getYellowBlue: () => + i18n.translate('xpack.canvas.lib.palettes.yellowBlueLabel', { + defaultMessage: 'Yellow, Blue', + }), + + getYellowRed: () => + i18n.translate('xpack.canvas.lib.palettes.yellowRedLabel', { + defaultMessage: 'Yellow, Red', + }), + + getInstagram: () => + i18n.translate('xpack.canvas.lib.palettes.instagramLabel', { + defaultMessage: '{INSTAGRAM}', + values: { + INSTAGRAM, + }, + }), + }, +}; diff --git a/x-pack/plugins/canvas/i18n/ui.ts b/x-pack/plugins/canvas/i18n/ui.ts index f69f9e747ab902..bc282db203be2e 100644 --- a/x-pack/plugins/canvas/i18n/ui.ts +++ b/x-pack/plugins/canvas/i18n/ui.ts @@ -232,7 +232,11 @@ export const ArgumentStrings = { }), getHelp: () => i18n.translate('xpack.canvas.uis.arguments.paletteLabel', { - defaultMessage: 'Choose a color palette', + defaultMessage: 'The collection of colors used to render the element', + }), + getCustomPaletteLabel: () => + i18n.translate('xpack.canvas.uis.arguments.customPaletteLabel', { + defaultMessage: 'Custom', }), }, Percentage: { diff --git a/x-pack/plugins/canvas/public/components/element_content/element_content.js b/x-pack/plugins/canvas/public/components/element_content/element_content.js index 114a457d167e77..e2c1a61c348d13 100644 --- a/x-pack/plugins/canvas/public/components/element_content/element_content.js +++ b/x-pack/plugins/canvas/public/components/element_content/element_content.js @@ -12,6 +12,7 @@ import { getType } from '@kbn/interpreter/common'; import { Loading } from '../loading'; import { RenderWithFn } from '../render_with_fn'; import { ElementShareContainer } from '../element_share_container'; +import { assignHandlers } from '../../lib/create_handlers'; import { InvalidExpression } from './invalid_expression'; import { InvalidElementType } from './invalid_element_type'; @@ -46,7 +47,7 @@ const branches = [ export const ElementContent = compose( pure, ...branches -)(({ renderable, renderFunction, size, handlers }) => { +)(({ renderable, renderFunction, width, height, handlers }) => { const { getFilter, setFilter, @@ -62,7 +63,7 @@ export const ElementContent = compose(
diff --git a/x-pack/plugins/canvas/public/components/element_wrapper/element_wrapper.js b/x-pack/plugins/canvas/public/components/element_wrapper/element_wrapper.js index 845fc5927d8397..de7748413b718a 100644 --- a/x-pack/plugins/canvas/public/components/element_wrapper/element_wrapper.js +++ b/x-pack/plugins/canvas/public/components/element_wrapper/element_wrapper.js @@ -14,7 +14,13 @@ export const ElementWrapper = (props) => { return ( - + ); }; diff --git a/x-pack/plugins/canvas/public/components/element_wrapper/index.js b/x-pack/plugins/canvas/public/components/element_wrapper/index.js index 390c349ab2ee64..6fc582bfee4446 100644 --- a/x-pack/plugins/canvas/public/components/element_wrapper/index.js +++ b/x-pack/plugins/canvas/public/components/element_wrapper/index.js @@ -10,12 +10,12 @@ import { compose, withPropsOnChange, mapProps } from 'recompose'; import isEqual from 'react-fast-compare'; import { getResolvedArgs, getSelectedPage } from '../../state/selectors/workpad'; import { getState, getValue } from '../../lib/resolved_arg'; +import { createDispatchedHandlerFactory } from '../../lib/create_handlers'; import { ElementWrapper as Component } from './element_wrapper'; -import { createHandlers as createHandlersWithDispatch } from './lib/handlers'; function selectorFactory(dispatch) { let result = {}; - const createHandlers = createHandlersWithDispatch(dispatch); + const createHandlers = createDispatchedHandlerFactory(dispatch); return (nextState, nextOwnProps) => { const { element, ...restOwnProps } = nextOwnProps; diff --git a/x-pack/plugins/canvas/public/components/element_wrapper/lib/handlers.js b/x-pack/plugins/canvas/public/components/element_wrapper/lib/handlers.js deleted file mode 100644 index 33e8eacd902dda..00000000000000 --- a/x-pack/plugins/canvas/public/components/element_wrapper/lib/handlers.js +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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 { isEqual } from 'lodash'; -import { setFilter } from '../../../state/actions/elements'; -import { - updateEmbeddableExpression, - fetchEmbeddableRenderable, -} from '../../../state/actions/embeddable'; - -export const createHandlers = (dispatch) => { - let isComplete = false; - let oldElement; - let completeFn = () => {}; - - return (element) => { - // reset isComplete when element changes - if (!isEqual(oldElement, element)) { - isComplete = false; - oldElement = element; - } - - return { - setFilter(text) { - dispatch(setFilter(text, element.id, true)); - }, - - getFilter() { - return element.filter; - }, - - onComplete(fn) { - completeFn = fn; - }, - - getElementId: () => element.id, - - onEmbeddableInputChange(embeddableExpression) { - dispatch(updateEmbeddableExpression({ elementId: element.id, embeddableExpression })); - }, - - onEmbeddableDestroyed() { - dispatch(fetchEmbeddableRenderable(element.id)); - }, - - done() { - // don't emit if the element is already done - if (isComplete) { - return; - } - - isComplete = true; - completeFn(); - }, - }; - }; -}; diff --git a/x-pack/plugins/canvas/public/components/palette_picker/__examples__/__snapshots__/palette_picker.stories.storyshot b/x-pack/plugins/canvas/public/components/palette_picker/__examples__/__snapshots__/palette_picker.stories.storyshot new file mode 100644 index 00000000000000..d3809b4c3979f1 --- /dev/null +++ b/x-pack/plugins/canvas/public/components/palette_picker/__examples__/__snapshots__/palette_picker.stories.storyshot @@ -0,0 +1,237 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Storyshots components/Color/PalettePicker clearable 1`] = ` +
+
+
+ +
+
+ + Select an option: None, is selected + + +
+ + +
+
+
+
+
+`; + +exports[`Storyshots components/Color/PalettePicker default 1`] = ` +
+
+
+ +
+
+ + Select an option: +
+ , is selected + + +
+ + +
+
+
+
+
+`; + +exports[`Storyshots components/Color/PalettePicker interactive 1`] = ` +
+
+
+ +
+
+ + Select an option: +
+ , is selected + + +
+ + +
+
+
+
+
+`; diff --git a/x-pack/plugins/canvas/public/components/palette_picker/__examples__/palette_picker.stories.tsx b/x-pack/plugins/canvas/public/components/palette_picker/__examples__/palette_picker.stories.tsx new file mode 100644 index 00000000000000..b1ae860e80efb7 --- /dev/null +++ b/x-pack/plugins/canvas/public/components/palette_picker/__examples__/palette_picker.stories.tsx @@ -0,0 +1,25 @@ +/* + * 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, { FC, useState } from 'react'; +import { action } from '@storybook/addon-actions'; +import { storiesOf } from '@storybook/react'; +import { PalettePicker } from '../palette_picker'; + +import { paulTor14, ColorPalette } from '../../../../common/lib/palettes'; + +const Interactive: FC = () => { + const [palette, setPalette] = useState(paulTor14); + return ; +}; + +storiesOf('components/Color/PalettePicker', module) + .addDecorator((fn) =>
{fn()}
) + .add('default', () => ) + .add('clearable', () => ( + + )) + .add('interactive', () => ); diff --git a/x-pack/plugins/canvas/public/components/positionable/index.js b/x-pack/plugins/canvas/public/components/palette_picker/index.ts similarity index 63% rename from x-pack/plugins/canvas/public/components/positionable/index.js rename to x-pack/plugins/canvas/public/components/palette_picker/index.ts index e5c3c32acb0241..840600698c5a42 100644 --- a/x-pack/plugins/canvas/public/components/positionable/index.js +++ b/x-pack/plugins/canvas/public/components/palette_picker/index.ts @@ -4,7 +4,4 @@ * you may not use this file except in compliance with the Elastic License. */ -import { pure } from 'recompose'; -import { Positionable as Component } from './positionable'; - -export const Positionable = pure(Component); +export { PalettePicker } from './palette_picker'; diff --git a/x-pack/plugins/canvas/public/components/palette_picker/palette_picker.js b/x-pack/plugins/canvas/public/components/palette_picker/palette_picker.js deleted file mode 100644 index ca2a499feb84cb..00000000000000 --- a/x-pack/plugins/canvas/public/components/palette_picker/palette_picker.js +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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 PropTypes from 'prop-types'; -import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui'; -import { map } from 'lodash'; -import { Popover } from '../popover'; -import { PaletteSwatch } from '../palette_swatch'; -import { palettes } from '../../../common/lib/palettes'; - -export const PalettePicker = ({ onChange, value, anchorPosition, ariaLabel }) => { - const button = (handleClick) => ( - - ); - - return ( - - {() => ( -
- {map(palettes, (palette, name) => ( - - ))} -
- )} -
- ); -}; - -PalettePicker.propTypes = { - value: PropTypes.object, - onChange: PropTypes.func, - anchorPosition: PropTypes.string, -}; diff --git a/x-pack/plugins/canvas/public/components/palette_picker/palette_picker.scss b/x-pack/plugins/canvas/public/components/palette_picker/palette_picker.scss deleted file mode 100644 index f837d47682f61f..00000000000000 --- a/x-pack/plugins/canvas/public/components/palette_picker/palette_picker.scss +++ /dev/null @@ -1,42 +0,0 @@ -.canvasPalettePicker { - display: inline-block; - width: 100%; -} - -.canvasPalettePicker__swatches { - @include euiScrollBar; - - width: 280px; - height: 250px; - overflow-y: scroll; -} - -.canvasPalettePicker__swatchesPanel { - padding: $euiSizeS 0 !important; // sass-lint:disable-line no-important -} - -.canvasPalettePicker__swatch { - padding: $euiSizeS $euiSize; - - &:hover, - &:focus { - text-decoration: underline; - background-color: $euiColorLightestShade; - - .canvasPaletteSwatch, - .canvasPaletteSwatch__background { - transform: scaleY(2); - } - - .canvasPalettePicker__label { - color: $euiTextColor; - } - } -} - -.canvasPalettePicker__label { - font-size: $euiFontSizeXS; - text-transform: capitalize; - text-align: left; - color: $euiColorDarkShade; -} diff --git a/x-pack/plugins/canvas/public/components/palette_picker/palette_picker.tsx b/x-pack/plugins/canvas/public/components/palette_picker/palette_picker.tsx new file mode 100644 index 00000000000000..dec09a5335d950 --- /dev/null +++ b/x-pack/plugins/canvas/public/components/palette_picker/palette_picker.tsx @@ -0,0 +1,92 @@ +/* + * 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, { FC } from 'react'; +import PropTypes from 'prop-types'; +import { EuiColorPalettePicker, EuiColorPalettePickerPaletteProps } from '@elastic/eui'; +import { palettes, ColorPalette } from '../../../common/lib/palettes'; +import { ComponentStrings } from '../../../i18n'; + +const { PalettePicker: strings } = ComponentStrings; + +interface RequiredProps { + id?: string; + onChange?: (palette: ColorPalette) => void; + palette: ColorPalette; + clearable?: false; +} + +interface ClearableProps { + id?: string; + onChange?: (palette: ColorPalette | null) => void; + palette: ColorPalette | null; + clearable: true; +} + +type Props = RequiredProps | ClearableProps; + +export const PalettePicker: FC = (props) => { + const colorPalettes: EuiColorPalettePickerPaletteProps[] = palettes.map((item) => ({ + value: item.id, + title: item.label, + type: item.gradient ? 'gradient' : 'fixed', + palette: item.colors, + })); + + if (props.clearable) { + const { palette, onChange = () => {} } = props; + + colorPalettes.unshift({ + value: 'clear', + title: strings.getEmptyPaletteLabel(), + type: 'text', + }); + + const onPickerChange = (value: string) => { + const canvasPalette = palettes.find((item) => item.id === value); + onChange(canvasPalette || null); + }; + + return ( + + ); + } + + const { palette, onChange = () => {} } = props; + + const onPickerChange = (value: string) => { + const canvasPalette = palettes.find((item) => item.id === value); + + if (!canvasPalette) { + throw new Error(strings.getNoPaletteFoundErrorTitle()); + } + + onChange(canvasPalette); + }; + + return ( + + ); +}; + +PalettePicker.propTypes = { + id: PropTypes.string, + palette: PropTypes.object, + onChange: PropTypes.func, + clearable: PropTypes.bool, +}; diff --git a/x-pack/plugins/canvas/public/components/palette_swatch/palette_swatch.js b/x-pack/plugins/canvas/public/components/palette_swatch/palette_swatch.js deleted file mode 100644 index 71d16260e00c73..00000000000000 --- a/x-pack/plugins/canvas/public/components/palette_swatch/palette_swatch.js +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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 PropTypes from 'prop-types'; - -export const PaletteSwatch = ({ colors, gradient }) => { - let colorBoxes; - - if (!gradient) { - colorBoxes = colors.map((color) => ( -
- )); - } else { - colorBoxes = [ -
, - ]; - } - - return ( -
-
-
{colorBoxes}
-
- ); -}; - -PaletteSwatch.propTypes = { - colors: PropTypes.array, - gradient: PropTypes.bool, -}; diff --git a/x-pack/plugins/canvas/public/components/palette_swatch/palette_swatch.scss b/x-pack/plugins/canvas/public/components/palette_swatch/palette_swatch.scss deleted file mode 100644 index b57c520a5b07f2..00000000000000 --- a/x-pack/plugins/canvas/public/components/palette_swatch/palette_swatch.scss +++ /dev/null @@ -1,35 +0,0 @@ -.canvasPaletteSwatch { - display: inline-block; - position: relative; - height: $euiSizeXS; - width: 100%; - overflow: hidden; - text-align: left; - transform: scaleY(1); - transition: transform $euiAnimSlightResistance $euiAnimSpeedExtraFast; - - .canvasPaletteSwatch__background { - position: absolute; - height: $euiSizeXS; - top: 0; - left: 0; - width: 100%; - transform: scaleY(1); - transition: transform $euiAnimSlightResistance $euiAnimSpeedExtraFast; - } - - .canvasPaletteSwatch__foreground { - position: absolute; - height: 100%; // TODO: No idea why this can't be 25, but it leaves a 1px white spot in the palettePicker if its 25 - top: 0; - left: 0; - white-space: nowrap; - width: 100%; - display: flex; - } - - .canvasPaletteSwatch__box { - display: inline-block; - width: 100%; - } -} diff --git a/x-pack/plugins/canvas/public/components/palette_picker/index.js b/x-pack/plugins/canvas/public/components/positionable/index.ts similarity index 62% rename from x-pack/plugins/canvas/public/components/palette_picker/index.js rename to x-pack/plugins/canvas/public/components/positionable/index.ts index 33d1d227771839..964e2ee41df75b 100644 --- a/x-pack/plugins/canvas/public/components/palette_picker/index.js +++ b/x-pack/plugins/canvas/public/components/positionable/index.ts @@ -4,8 +4,4 @@ * you may not use this file except in compliance with the Elastic License. */ -import { pure } from 'recompose'; - -import { PalettePicker as Component } from './palette_picker'; - -export const PalettePicker = pure(Component); +export { Positionable } from './positionable'; diff --git a/x-pack/plugins/canvas/public/components/positionable/positionable.js b/x-pack/plugins/canvas/public/components/positionable/positionable.js deleted file mode 100644 index 9898f50cbb0f0c..00000000000000 --- a/x-pack/plugins/canvas/public/components/positionable/positionable.js +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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 PropTypes from 'prop-types'; -import { matrixToCSS } from '../../lib/dom'; - -export const Positionable = ({ children, transformMatrix, width, height }) => { - // Throw if there is more than one child - React.Children.only(children); - // This could probably be made nicer by having just one child - const wrappedChildren = React.Children.map(children, (child) => { - const newStyle = { - width, - height, - marginLeft: -width / 2, - marginTop: -height / 2, - position: 'absolute', - transform: matrixToCSS(transformMatrix.map((n, i) => (i < 12 ? n : Math.round(n)))), - }; - - const stepChild = React.cloneElement(child, { size: { width, height } }); - return ( -
- {stepChild} -
- ); - }); - - return wrappedChildren; -}; - -Positionable.propTypes = { - onChange: PropTypes.func, - children: PropTypes.element.isRequired, - transformMatrix: PropTypes.arrayOf(PropTypes.number).isRequired, - width: PropTypes.number.isRequired, - height: PropTypes.number.isRequired, -}; diff --git a/x-pack/plugins/canvas/public/components/positionable/positionable.tsx b/x-pack/plugins/canvas/public/components/positionable/positionable.tsx new file mode 100644 index 00000000000000..3344398b001988 --- /dev/null +++ b/x-pack/plugins/canvas/public/components/positionable/positionable.tsx @@ -0,0 +1,48 @@ +/* + * 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, { FC, ReactElement, CSSProperties } from 'react'; +import PropTypes from 'prop-types'; +import { matrixToCSS } from '../../lib/dom'; +import { TransformMatrix3d } from '../../lib/aeroelastic'; + +interface Props { + children: ReactElement; + transformMatrix: TransformMatrix3d; + height: number; + width: number; +} + +export const Positionable: FC = ({ children, transformMatrix, width, height }) => { + // Throw if there is more than one child + const childNode = React.Children.only(children); + + const matrix = (transformMatrix.map((n, i) => + i < 12 ? n : Math.round(n) + ) as any) as TransformMatrix3d; + + const newStyle: CSSProperties = { + width, + height, + marginLeft: -width / 2, + marginTop: -height / 2, + position: 'absolute', + transform: matrixToCSS(matrix), + }; + + return ( +
+ {childNode} +
+ ); +}; + +Positionable.propTypes = { + children: PropTypes.element.isRequired, + transformMatrix: PropTypes.arrayOf(PropTypes.number).isRequired, + width: PropTypes.number.isRequired, + height: PropTypes.number.isRequired, +}; diff --git a/x-pack/plugins/canvas/public/components/render_to_dom/index.js b/x-pack/plugins/canvas/public/components/render_to_dom/index.js deleted file mode 100644 index e8a3f8cd8c93b7..00000000000000 --- a/x-pack/plugins/canvas/public/components/render_to_dom/index.js +++ /dev/null @@ -1,12 +0,0 @@ -/* - * 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 { compose, withState } from 'recompose'; -import { RenderToDom as Component } from './render_to_dom'; - -export const RenderToDom = compose( - withState('domNode', 'setDomNode') // Still don't like this, seems to be the only way todo it. -)(Component); diff --git a/x-pack/plugins/canvas/public/components/palette_swatch/index.js b/x-pack/plugins/canvas/public/components/render_to_dom/index.ts similarity index 62% rename from x-pack/plugins/canvas/public/components/palette_swatch/index.js rename to x-pack/plugins/canvas/public/components/render_to_dom/index.ts index 2be37a8338b2b3..43a5dad059c955 100644 --- a/x-pack/plugins/canvas/public/components/palette_swatch/index.js +++ b/x-pack/plugins/canvas/public/components/render_to_dom/index.ts @@ -4,8 +4,4 @@ * you may not use this file except in compliance with the Elastic License. */ -import { pure } from 'recompose'; - -import { PaletteSwatch as Component } from './palette_swatch'; - -export const PaletteSwatch = pure(Component); +export { RenderToDom } from './render_to_dom'; diff --git a/x-pack/plugins/canvas/public/components/render_to_dom/render_to_dom.js b/x-pack/plugins/canvas/public/components/render_to_dom/render_to_dom.js deleted file mode 100644 index db393a8dde4f9a..00000000000000 --- a/x-pack/plugins/canvas/public/components/render_to_dom/render_to_dom.js +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 PropTypes from 'prop-types'; - -export class RenderToDom extends React.Component { - static propTypes = { - domNode: PropTypes.object, - setDomNode: PropTypes.func.isRequired, - render: PropTypes.func.isRequired, - style: PropTypes.object, - }; - - shouldComponentUpdate(nextProps) { - return this.props.domNode !== nextProps.domNode; - } - - componentDidUpdate() { - // Calls render function once we have the reference to the DOM element to render into - if (this.props.domNode) { - this.props.render(this.props.domNode); - } - } - - render() { - const { domNode, setDomNode, style } = this.props; - const linkRef = (refNode) => { - if (!domNode && refNode) { - // Initialize the domNode property. This should only happen once, even if config changes. - setDomNode(refNode); - } - }; - - return
; - } -} diff --git a/x-pack/plugins/canvas/public/components/render_to_dom/render_to_dom.tsx b/x-pack/plugins/canvas/public/components/render_to_dom/render_to_dom.tsx new file mode 100644 index 00000000000000..a37c0fc096e574 --- /dev/null +++ b/x-pack/plugins/canvas/public/components/render_to_dom/render_to_dom.tsx @@ -0,0 +1,27 @@ +/* + * 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, { useCallback, FC } from 'react'; +import CSS from 'csstype'; + +interface Props { + render: (element: HTMLElement) => void; + style?: CSS.Properties; +} + +export const RenderToDom: FC = ({ render, style }) => { + // https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node + const ref = useCallback( + (node: HTMLDivElement) => { + if (node !== null) { + render(node); + } + }, + [render] + ); + + return
; +}; diff --git a/x-pack/plugins/canvas/public/components/render_with_fn/index.js b/x-pack/plugins/canvas/public/components/render_with_fn/index.js deleted file mode 100644 index 37c49624a39407..00000000000000 --- a/x-pack/plugins/canvas/public/components/render_with_fn/index.js +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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 { compose, withProps, withPropsOnChange } from 'recompose'; -import PropTypes from 'prop-types'; -import isEqual from 'react-fast-compare'; -import { withKibana } from '../../../../../../src/plugins/kibana_react/public'; -import { RenderWithFn as Component } from './render_with_fn'; -import { ElementHandlers } from './lib/handlers'; - -export const RenderWithFn = compose( - withPropsOnChange( - // rebuild elementHandlers when handlers object changes - (props, nextProps) => !isEqual(props.handlers, nextProps.handlers), - ({ handlers }) => ({ - handlers: Object.assign(new ElementHandlers(), handlers), - }) - ), - withKibana, - withProps((props) => ({ - onError: props.kibana.services.canvas.notify.error, - })) -)(Component); - -RenderWithFn.propTypes = { - handlers: PropTypes.object, -}; diff --git a/x-pack/plugins/canvas/public/components/render_with_fn/index.ts b/x-pack/plugins/canvas/public/components/render_with_fn/index.ts new file mode 100644 index 00000000000000..4bfef734d34f4c --- /dev/null +++ b/x-pack/plugins/canvas/public/components/render_with_fn/index.ts @@ -0,0 +1,7 @@ +/* + * 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 { RenderWithFn } from './render_with_fn'; diff --git a/x-pack/plugins/canvas/public/components/render_with_fn/render_with_fn.js b/x-pack/plugins/canvas/public/components/render_with_fn/render_with_fn.js deleted file mode 100644 index 763cbd5e53eb18..00000000000000 --- a/x-pack/plugins/canvas/public/components/render_with_fn/render_with_fn.js +++ /dev/null @@ -1,157 +0,0 @@ -/* - * 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 PropTypes from 'prop-types'; -import { isEqual, cloneDeep } from 'lodash'; -import { RenderToDom } from '../render_to_dom'; -import { ErrorStrings } from '../../../i18n'; - -const { RenderWithFn: strings } = ErrorStrings; - -export class RenderWithFn extends React.Component { - static propTypes = { - name: PropTypes.string.isRequired, - renderFn: PropTypes.func.isRequired, - reuseNode: PropTypes.bool, - handlers: PropTypes.shape({ - // element handlers, see components/element_wrapper/lib/handlers.js - setFilter: PropTypes.func.isRequired, - getFilter: PropTypes.func.isRequired, - done: PropTypes.func.isRequired, - // render handlers, see lib/handlers.js - resize: PropTypes.func.isRequired, - onResize: PropTypes.func.isRequired, - destroy: PropTypes.func.isRequired, - onDestroy: PropTypes.func.isRequired, - }), - config: PropTypes.object, - size: PropTypes.object.isRequired, - onError: PropTypes.func.isRequired, - }; - - static defaultProps = { - reuseNode: false, - }; - - componentDidMount() { - this.firstRender = true; - this.renderTarget = null; - } - - UNSAFE_componentWillReceiveProps({ renderFn }) { - const newRenderFunction = renderFn !== this.props.renderFn; - - if (newRenderFunction) { - this._resetRenderTarget(this._domNode); - } - } - - shouldComponentUpdate(prevProps) { - return !isEqual(this.props.size, prevProps.size) || this._shouldFullRerender(prevProps); - } - - componentDidUpdate(prevProps) { - const { handlers, size } = this.props; - // Config changes - if (this._shouldFullRerender(prevProps)) { - // This should be the only place you call renderFn besides the first time - this._callRenderFn(); - } - - // Size changes - if (!isEqual(size, prevProps.size)) { - return handlers.resize(size); - } - } - - componentWillUnmount() { - this.props.handlers.destroy(); - } - - _domNode = null; - - _callRenderFn = () => { - const { handlers, config, renderFn, reuseNode, name: functionName } = this.props; - // TODO: We should wait until handlers.done() is called before replacing the element content? - if (!reuseNode || !this.renderTarget) { - this._resetRenderTarget(this._domNode); - } - // else if (!firstRender) handlers.destroy(); - - const renderConfig = cloneDeep(config); - - // TODO: this is hacky, but it works. it stops Kibana from blowing up when a render throws - try { - renderFn(this.renderTarget, renderConfig, handlers); - this.firstRender = false; - } catch (err) { - console.error('renderFn threw', err); - this.props.onError(err, { title: strings.getRenderErrorMessage(functionName) }); - } - }; - - _resetRenderTarget = (domNode) => { - const { handlers } = this.props; - - if (!domNode) { - throw new Error('RenderWithFn can not reset undefined target node'); - } - - // call destroy on existing element - if (!this.firstRender) { - handlers.destroy(); - } - - while (domNode.firstChild) { - domNode.removeChild(domNode.firstChild); - } - - this.firstRender = true; - this.renderTarget = this._createRenderTarget(); - domNode.appendChild(this.renderTarget); - }; - - _createRenderTarget = () => { - const div = document.createElement('div'); - div.style.width = '100%'; - div.style.height = '100%'; - return div; - }; - - _shouldFullRerender = (prevProps) => { - // required to stop re-renders on element move, anything that should - // cause a re-render needs to be checked here - // TODO: fix props passed in to remove this check - return ( - this.props.handlers !== prevProps.handlers || - !isEqual(this.props.config, prevProps.config) || - !isEqual(this.props.renderFn.toString(), prevProps.renderFn.toString()) - ); - }; - - destroy = () => { - this.props.handlers.destroy(); - }; - - render() { - // NOTE: the data-shared-* attributes here are used for reporting - return ( -
- { - this._domNode = domNode; - this._callRenderFn(); - }} - /> -
- ); - } -} diff --git a/x-pack/plugins/canvas/public/components/render_with_fn/render_with_fn.tsx b/x-pack/plugins/canvas/public/components/render_with_fn/render_with_fn.tsx new file mode 100644 index 00000000000000..bc51128cf0c876 --- /dev/null +++ b/x-pack/plugins/canvas/public/components/render_with_fn/render_with_fn.tsx @@ -0,0 +1,117 @@ +/* + * 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, { useState, useEffect, useRef, FC, useCallback } from 'react'; +import { useDebounce } from 'react-use'; + +import { useKibana } from '../../../../../../src/plugins/kibana_react/public'; +import { RenderToDom } from '../render_to_dom'; +import { ErrorStrings } from '../../../i18n'; +import { RendererHandlers } from '../../../types'; + +const { RenderWithFn: strings } = ErrorStrings; + +interface Props { + name: string; + renderFn: ( + domNode: HTMLElement, + config: Record, + handlers: RendererHandlers + ) => void | Promise; + reuseNode: boolean; + handlers: RendererHandlers; + config: Record; + height: number; + width: number; +} + +const style = { height: '100%', width: '100%' }; + +export const RenderWithFn: FC = ({ + name: functionName, + renderFn, + reuseNode = false, + handlers: incomingHandlers, + config, + width, + height, +}) => { + const { services } = useKibana(); + const onError = services.canvas.notify.error; + + const [domNode, setDomNode] = useState(null); + + // Tells us if the component is attempting to re-render into a previously-populated render target. + const firstRender = useRef(true); + // A reference to the node appended to the provided DOM node which is created and optionally replaced. + const renderTarget = useRef(null); + // A reference to the handlers, as the renderFn may mutate them, (via onXYZ functions) + const handlers = useRef(incomingHandlers); + + // Reset the render target, the node appended to the DOM node provided by RenderToDOM. + const resetRenderTarget = useCallback(() => { + if (!domNode) { + return; + } + + if (!firstRender) { + handlers.current.destroy(); + } + + while (domNode.firstChild) { + domNode.removeChild(domNode.firstChild); + } + + const div = document.createElement('div'); + div.style.width = '100%'; + div.style.height = '100%'; + domNode.appendChild(div); + + renderTarget.current = div; + firstRender.current = true; + }, [domNode]); + + useDebounce(() => handlers.current.resize({ height, width }), 150, [height, width]); + + useEffect( + () => () => { + handlers.current.destroy(); + }, + [] + ); + + const render = useCallback(() => { + renderFn(renderTarget.current!, config, handlers.current); + }, [renderTarget, config, renderFn]); + + useEffect(() => { + if (!domNode) { + return; + } + + if (!reuseNode || !renderTarget.current) { + resetRenderTarget(); + } + + try { + render(); + firstRender.current = false; + } catch (err) { + onError(err, { title: strings.getRenderErrorMessage(functionName) }); + } + }, [domNode, functionName, onError, render, resetRenderTarget, reuseNode]); + + return ( +
+ { + setDomNode(node); + }} + /> +
+ ); +}; diff --git a/x-pack/plugins/canvas/public/lib/create_handlers.ts b/x-pack/plugins/canvas/public/lib/create_handlers.ts new file mode 100644 index 00000000000000..4e0c7b217d5b71 --- /dev/null +++ b/x-pack/plugins/canvas/public/lib/create_handlers.ts @@ -0,0 +1,96 @@ +/* + * 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 { isEqual } from 'lodash'; +// @ts-ignore untyped local +import { setFilter } from '../state/actions/elements'; +import { updateEmbeddableExpression, fetchEmbeddableRenderable } from '../state/actions/embeddable'; +import { RendererHandlers, CanvasElement } from '../../types'; + +// This class creates stub handlers to ensure every element and renderer fulfills the contract. +// TODO: consider warning if these methods are invoked but not implemented by the renderer...? + +export const createHandlers = (): RendererHandlers => ({ + destroy() {}, + done() {}, + event() {}, + getElementId() { + return ''; + }, + getFilter() { + return ''; + }, + onComplete(fn: () => void) { + this.done = fn; + }, + onDestroy(fn: () => void) { + this.destroy = fn; + }, + // TODO: these functions do not match the `onXYZ` and `xyz` pattern elsewhere. + onEmbeddableDestroyed() {}, + onEmbeddableInputChange() {}, + onResize(fn: (size: { height: number; width: number }) => void) { + this.resize = fn; + }, + reload() {}, + resize(_size: { height: number; width: number }) {}, + setFilter() {}, + update() {}, +}); + +export const assignHandlers = (handlers: Partial = {}): RendererHandlers => + Object.assign(createHandlers(), handlers); + +// TODO: this is a legacy approach we should unravel in the near future. +export const createDispatchedHandlerFactory = ( + dispatch: (action: any) => void +): ((element: CanvasElement) => RendererHandlers) => { + let isComplete = false; + let oldElement: CanvasElement | undefined; + let completeFn = () => {}; + + return (element: CanvasElement) => { + // reset isComplete when element changes + if (!isEqual(oldElement, element)) { + isComplete = false; + oldElement = element; + } + + return assignHandlers({ + setFilter(text: string) { + dispatch(setFilter(text, element.id, true)); + }, + + getFilter() { + return element.filter; + }, + + onComplete(fn: () => void) { + completeFn = fn; + }, + + getElementId: () => element.id, + + onEmbeddableInputChange(embeddableExpression: string) { + dispatch(updateEmbeddableExpression({ elementId: element.id, embeddableExpression })); + }, + + onEmbeddableDestroyed() { + dispatch(fetchEmbeddableRenderable(element.id)); + }, + + done() { + // don't emit if the element is already done + if (isComplete) { + return; + } + + isComplete = true; + completeFn(); + }, + }); + }; +}; diff --git a/x-pack/plugins/canvas/public/style/index.scss b/x-pack/plugins/canvas/public/style/index.scss index 7b4e1271cca1df..78a34a58f5f782 100644 --- a/x-pack/plugins/canvas/public/style/index.scss +++ b/x-pack/plugins/canvas/public/style/index.scss @@ -39,8 +39,6 @@ @import '../components/loading/loading'; @import '../components/navbar/navbar'; @import '../components/page_manager/page_manager'; -@import '../components/palette_picker/palette_picker'; -@import '../components/palette_swatch/palette_swatch'; @import '../components/positionable/positionable'; @import '../components/rotation_handle/rotation_handle'; @import '../components/shape_preview/shape_preview'; diff --git a/x-pack/plugins/canvas/server/routes/custom_elements/create.test.ts b/x-pack/plugins/canvas/server/routes/custom_elements/create.test.ts index db0417434227c4..290175d9062ea8 100644 --- a/x-pack/plugins/canvas/server/routes/custom_elements/create.test.ts +++ b/x-pack/plugins/canvas/server/routes/custom_elements/create.test.ts @@ -9,7 +9,7 @@ import { savedObjectsClientMock, httpServiceMock, httpServerMock, - loggingServiceMock, + loggingSystemMock, } from 'src/core/server/mocks'; import { CUSTOM_ELEMENT_TYPE } from '../../../common/lib/constants'; import { initializeCreateCustomElementRoute } from './create'; @@ -41,7 +41,7 @@ describe('POST custom element', () => { const router = httpService.createRouter(); initializeCreateCustomElementRoute({ router, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), }); routeHandler = router.post.mock.calls[0][1]; diff --git a/x-pack/plugins/canvas/server/routes/custom_elements/delete.test.ts b/x-pack/plugins/canvas/server/routes/custom_elements/delete.test.ts index 98b26ec368ab1e..62ce4b9c3593ce 100644 --- a/x-pack/plugins/canvas/server/routes/custom_elements/delete.test.ts +++ b/x-pack/plugins/canvas/server/routes/custom_elements/delete.test.ts @@ -11,7 +11,7 @@ import { savedObjectsClientMock, httpServiceMock, httpServerMock, - loggingServiceMock, + loggingSystemMock, } from 'src/core/server/mocks'; const mockRouteContext = ({ @@ -30,7 +30,7 @@ describe('DELETE custom element', () => { const router = httpService.createRouter(); initializeDeleteCustomElementRoute({ router, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), }); routeHandler = router.delete.mock.calls[0][1]; diff --git a/x-pack/plugins/canvas/server/routes/custom_elements/find.test.ts b/x-pack/plugins/canvas/server/routes/custom_elements/find.test.ts index dead9ded8a14af..d42c97b62e0f39 100644 --- a/x-pack/plugins/canvas/server/routes/custom_elements/find.test.ts +++ b/x-pack/plugins/canvas/server/routes/custom_elements/find.test.ts @@ -10,7 +10,7 @@ import { savedObjectsClientMock, httpServiceMock, httpServerMock, - loggingServiceMock, + loggingSystemMock, } from 'src/core/server/mocks'; const mockRouteContext = ({ @@ -29,7 +29,7 @@ describe('Find custom element', () => { const router = httpService.createRouter(); initializeFindCustomElementsRoute({ router, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), }); routeHandler = router.get.mock.calls[0][1]; diff --git a/x-pack/plugins/canvas/server/routes/custom_elements/get.test.ts b/x-pack/plugins/canvas/server/routes/custom_elements/get.test.ts index 09b620aeff9bb1..7b4d0eba374199 100644 --- a/x-pack/plugins/canvas/server/routes/custom_elements/get.test.ts +++ b/x-pack/plugins/canvas/server/routes/custom_elements/get.test.ts @@ -11,7 +11,7 @@ import { savedObjectsClientMock, httpServiceMock, httpServerMock, - loggingServiceMock, + loggingSystemMock, } from 'src/core/server/mocks'; const mockRouteContext = ({ @@ -30,7 +30,7 @@ describe('GET custom element', () => { const router = httpService.createRouter(); initializeGetCustomElementRoute({ router, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), }); routeHandler = router.get.mock.calls[0][1]; diff --git a/x-pack/plugins/canvas/server/routes/custom_elements/update.test.ts b/x-pack/plugins/canvas/server/routes/custom_elements/update.test.ts index 19477458bacb5a..0f954904355ae5 100644 --- a/x-pack/plugins/canvas/server/routes/custom_elements/update.test.ts +++ b/x-pack/plugins/canvas/server/routes/custom_elements/update.test.ts @@ -13,7 +13,7 @@ import { savedObjectsClientMock, httpServiceMock, httpServerMock, - loggingServiceMock, + loggingSystemMock, } from 'src/core/server/mocks'; import { okResponse } from '../ok_response'; @@ -55,7 +55,7 @@ describe('PUT custom element', () => { const router = httpService.createRouter(); initializeUpdateCustomElementRoute({ router, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), }); routeHandler = router.put.mock.calls[0][1]; diff --git a/x-pack/plugins/canvas/server/routes/es_fields/es_fields.test.ts b/x-pack/plugins/canvas/server/routes/es_fields/es_fields.test.ts index 93fdb4304acc6d..c1918feb7f4ec3 100644 --- a/x-pack/plugins/canvas/server/routes/es_fields/es_fields.test.ts +++ b/x-pack/plugins/canvas/server/routes/es_fields/es_fields.test.ts @@ -9,7 +9,7 @@ import { kibanaResponseFactory, RequestHandlerContext, RequestHandler } from 'sr import { httpServiceMock, httpServerMock, - loggingServiceMock, + loggingSystemMock, elasticsearchServiceMock, } from 'src/core/server/mocks'; @@ -29,7 +29,7 @@ describe('Retrieve ES Fields', () => { const router = httpService.createRouter(); initializeESFieldsRoute({ router, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), }); routeHandler = router.get.mock.calls[0][1]; diff --git a/x-pack/plugins/canvas/server/routes/shareables/download.test.ts b/x-pack/plugins/canvas/server/routes/shareables/download.test.ts index 75eeb46c890d5d..0267a695ae9fe3 100644 --- a/x-pack/plugins/canvas/server/routes/shareables/download.test.ts +++ b/x-pack/plugins/canvas/server/routes/shareables/download.test.ts @@ -8,7 +8,7 @@ jest.mock('fs'); import fs from 'fs'; import { kibanaResponseFactory, RequestHandlerContext, RequestHandler } from 'src/core/server'; -import { httpServiceMock, httpServerMock, loggingServiceMock } from 'src/core/server/mocks'; +import { httpServiceMock, httpServerMock, loggingSystemMock } from 'src/core/server/mocks'; import { initializeDownloadShareableWorkpadRoute } from './download'; const mockRouteContext = {} as RequestHandlerContext; @@ -23,7 +23,7 @@ describe('Download Canvas shareables runtime', () => { const router = httpService.createRouter(); initializeDownloadShareableWorkpadRoute({ router, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), }); routeHandler = router.get.mock.calls[0][1]; diff --git a/x-pack/plugins/canvas/server/routes/shareables/zip.test.ts b/x-pack/plugins/canvas/server/routes/shareables/zip.test.ts index 5a2d122c2754be..29dcb4268e6184 100644 --- a/x-pack/plugins/canvas/server/routes/shareables/zip.test.ts +++ b/x-pack/plugins/canvas/server/routes/shareables/zip.test.ts @@ -8,7 +8,7 @@ jest.mock('archiver'); const archiver = require('archiver') as jest.Mock; import { kibanaResponseFactory, RequestHandlerContext, RequestHandler } from 'src/core/server'; -import { httpServiceMock, httpServerMock, loggingServiceMock } from 'src/core/server/mocks'; +import { httpServiceMock, httpServerMock, loggingSystemMock } from 'src/core/server/mocks'; import { initializeZipShareableWorkpadRoute } from './zip'; import { API_ROUTE_SHAREABLE_ZIP } from '../../../common/lib'; import { @@ -29,7 +29,7 @@ describe('Zips Canvas shareables runtime together with workpad', () => { const router = httpService.createRouter(); initializeZipShareableWorkpadRoute({ router, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), }); routeHandler = router.post.mock.calls[0][1]; diff --git a/x-pack/plugins/canvas/server/routes/workpad/create.test.ts b/x-pack/plugins/canvas/server/routes/workpad/create.test.ts index 2ed63e7397108a..9cadb50b9a506c 100644 --- a/x-pack/plugins/canvas/server/routes/workpad/create.test.ts +++ b/x-pack/plugins/canvas/server/routes/workpad/create.test.ts @@ -9,7 +9,7 @@ import { savedObjectsClientMock, httpServiceMock, httpServerMock, - loggingServiceMock, + loggingSystemMock, } from 'src/core/server/mocks'; import { CANVAS_TYPE } from '../../../common/lib/constants'; import { initializeCreateWorkpadRoute } from './create'; @@ -41,7 +41,7 @@ describe('POST workpad', () => { const router = httpService.createRouter(); initializeCreateWorkpadRoute({ router, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), }); routeHandler = router.post.mock.calls[0][1]; diff --git a/x-pack/plugins/canvas/server/routes/workpad/delete.test.ts b/x-pack/plugins/canvas/server/routes/workpad/delete.test.ts index 712ff294003829..32ce30325b60ad 100644 --- a/x-pack/plugins/canvas/server/routes/workpad/delete.test.ts +++ b/x-pack/plugins/canvas/server/routes/workpad/delete.test.ts @@ -11,7 +11,7 @@ import { savedObjectsClientMock, httpServiceMock, httpServerMock, - loggingServiceMock, + loggingSystemMock, } from 'src/core/server/mocks'; const mockRouteContext = ({ @@ -30,7 +30,7 @@ describe('DELETE workpad', () => { const router = httpService.createRouter(); initializeDeleteWorkpadRoute({ router, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), }); routeHandler = router.delete.mock.calls[0][1]; diff --git a/x-pack/plugins/canvas/server/routes/workpad/find.test.ts b/x-pack/plugins/canvas/server/routes/workpad/find.test.ts index e2dd8552379b7a..a87cf7be57d811 100644 --- a/x-pack/plugins/canvas/server/routes/workpad/find.test.ts +++ b/x-pack/plugins/canvas/server/routes/workpad/find.test.ts @@ -10,7 +10,7 @@ import { savedObjectsClientMock, httpServiceMock, httpServerMock, - loggingServiceMock, + loggingSystemMock, } from 'src/core/server/mocks'; const mockRouteContext = ({ @@ -29,7 +29,7 @@ describe('Find workpad', () => { const router = httpService.createRouter(); initializeFindWorkpadsRoute({ router, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), }); routeHandler = router.get.mock.calls[0][1]; diff --git a/x-pack/plugins/canvas/server/routes/workpad/get.test.ts b/x-pack/plugins/canvas/server/routes/workpad/get.test.ts index 9ecd9ceefed8d0..8cc190dc6231cc 100644 --- a/x-pack/plugins/canvas/server/routes/workpad/get.test.ts +++ b/x-pack/plugins/canvas/server/routes/workpad/get.test.ts @@ -11,7 +11,7 @@ import { savedObjectsClientMock, httpServiceMock, httpServerMock, - loggingServiceMock, + loggingSystemMock, } from 'src/core/server/mocks'; import { workpadWithGroupAsElement } from '../../../__tests__/fixtures/workpads'; import { CanvasWorkpad } from '../../../types'; @@ -32,7 +32,7 @@ describe('GET workpad', () => { const router = httpService.createRouter(); initializeGetWorkpadRoute({ router, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), }); routeHandler = router.get.mock.calls[0][1]; diff --git a/x-pack/plugins/canvas/server/routes/workpad/update.test.ts b/x-pack/plugins/canvas/server/routes/workpad/update.test.ts index 36ea984447d8ac..6d7ea06852a5e5 100644 --- a/x-pack/plugins/canvas/server/routes/workpad/update.test.ts +++ b/x-pack/plugins/canvas/server/routes/workpad/update.test.ts @@ -12,7 +12,7 @@ import { savedObjectsClientMock, httpServiceMock, httpServerMock, - loggingServiceMock, + loggingSystemMock, } from 'src/core/server/mocks'; import { workpads } from '../../../__tests__/fixtures/workpads'; import { okResponse } from '../ok_response'; @@ -42,7 +42,7 @@ describe('PUT workpad', () => { const router = httpService.createRouter(); initializeUpdateWorkpadRoute({ router, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), }); routeHandler = router.put.mock.calls[0][1]; @@ -156,7 +156,7 @@ describe('update assets', () => { const router = httpService.createRouter(); initializeUpdateWorkpadAssetsRoute({ router, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), }); routeHandler = router.put.mock.calls[0][1]; diff --git a/x-pack/plugins/canvas/shareable_runtime/components/rendered_element.tsx b/x-pack/plugins/canvas/shareable_runtime/components/rendered_element.tsx index 5741f5f2d698c3..6bcc0db92f1ccd 100644 --- a/x-pack/plugins/canvas/shareable_runtime/components/rendered_element.tsx +++ b/x-pack/plugins/canvas/shareable_runtime/components/rendered_element.tsx @@ -7,13 +7,13 @@ import React, { FC, PureComponent } from 'react'; // @ts-expect-error untyped library import Style from 'style-it'; -// @ts-expect-error untyped local import { Positionable } from '../../public/components/positionable/positionable'; // @ts-expect-error untyped local import { elementToShape } from '../../public/components/workpad_page/utils'; import { CanvasRenderedElement } from '../types'; import { CanvasShareableContext, useCanvasShareableState } from '../context'; import { RendererSpec } from '../../types'; +import { createHandlers } from '../../public/lib/create_handlers'; import css from './rendered_element.module.scss'; @@ -62,17 +62,7 @@ export class RenderedElementComponent extends PureComponent { } try { - // TODO: These are stubbed, but may need implementation. - fn.render(this.ref.current, value.value, { - done: () => {}, - onDestroy: () => {}, - onResize: () => {}, - getElementId: () => '', - setFilter: () => {}, - getFilter: () => '', - onEmbeddableInputChange: () => {}, - onEmbeddableDestroyed: () => {}, - }); + fn.render(this.ref.current, value.value, createHandlers()); } catch (e) { // eslint-disable-next-line no-console console.log(as, e.message); diff --git a/x-pack/plugins/canvas/types/renderers.ts b/x-pack/plugins/canvas/types/renderers.ts index 2564b045d1cf75..772a16aa94c605 100644 --- a/x-pack/plugins/canvas/types/renderers.ts +++ b/x-pack/plugins/canvas/types/renderers.ts @@ -4,25 +4,29 @@ * you may not use this file except in compliance with the Elastic License. */ -type GenericCallback = (callback: () => void) => void; +import { IInterpreterRenderHandlers } from 'src/plugins/expressions'; -export interface RendererHandlers { - /** Handler to invoke when an element has finished rendering */ - done: () => void; +type GenericRendererCallback = (callback: () => void) => void; + +export interface RendererHandlers extends IInterpreterRenderHandlers { + /** Handler to invoke when an element should be destroyed. */ + destroy: () => void; /** Get the id of the element being rendered. Can be used as a unique ID in a render function */ getElementId: () => string; - /** Handler to invoke when an element is deleted or changes to a different render type */ - onDestroy: GenericCallback; - /** Handler to invoke when an element's dimensions have changed*/ - onResize: GenericCallback; /** Retrieves the value of the filter property on the element object persisted on the workpad */ getFilter: () => string; - /** Sets the value of the filter property on the element object persisted on the workpad */ - setFilter: (filter: string) => void; - /** Handler to invoke when the input to a function has changed internally */ - onEmbeddableInputChange: (expression: string) => void; + /** Handler to invoke when a renderer is considered complete */ + onComplete: (fn: () => void) => void; /** Handler to invoke when a rendered embeddable is destroyed */ onEmbeddableDestroyed: () => void; + /** Handler to invoke when the input to a function has changed internally */ + onEmbeddableInputChange: (expression: string) => void; + /** Handler to invoke when an element's dimensions have changed*/ + onResize: GenericRendererCallback; + /** Handler to invoke when an element should be resized. */ + resize: (size: { height: number; width: number }) => void; + /** Sets the value of the filter property on the element object persisted on the workpad */ + setFilter: (filter: string) => void; } export interface RendererSpec { diff --git a/x-pack/plugins/case/server/routes/api/__fixtures__/mock_router.ts b/x-pack/plugins/case/server/routes/api/__fixtures__/mock_router.ts index e00c1c111b41b2..8fde66ea820194 100644 --- a/x-pack/plugins/case/server/routes/api/__fixtures__/mock_router.ts +++ b/x-pack/plugins/case/server/routes/api/__fixtures__/mock_router.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { loggingServiceMock, httpServiceMock } from '../../../../../../../src/core/server/mocks'; +import { loggingSystemMock, httpServiceMock } from '../../../../../../../src/core/server/mocks'; import { CaseService, CaseConfigureService } from '../../../services'; import { authenticationMock } from '../__fixtures__'; import { RouteDeps } from '../types'; @@ -17,7 +17,7 @@ export const createRoute = async ( const httpService = httpServiceMock.createSetupContract(); const router = httpService.createRouter(); - const log = loggingServiceMock.create().get('case'); + const log = loggingSystemMock.create().get('case'); const caseServicePlugin = new CaseService(log); const caseConfigureServicePlugin = new CaseConfigureService(log); diff --git a/x-pack/plugins/cloud/public/plugin.ts b/x-pack/plugins/cloud/public/plugin.ts index 62e21392f71105..1c3a770da79f55 100644 --- a/x-pack/plugins/cloud/public/plugin.ts +++ b/x-pack/plugins/cloud/public/plugin.ts @@ -5,6 +5,7 @@ */ import { CoreSetup, CoreStart, Plugin, PluginInitializerContext } from 'src/core/public'; +import { i18n } from '@kbn/i18n'; import { getIsCloudEnabled } from '../common/is_cloud_enabled'; import { ELASTIC_SUPPORT_LINK } from '../common/constants'; import { HomePublicPluginSetup } from '../../../../src/plugins/home/public'; @@ -12,6 +13,7 @@ import { HomePublicPluginSetup } from '../../../../src/plugins/home/public'; interface CloudConfigType { id?: string; resetPasswordUrl?: string; + deploymentUrl?: string; } interface CloudSetupDependencies { @@ -24,10 +26,14 @@ export interface CloudSetup { } export class CloudPlugin implements Plugin { - constructor(private readonly initializerContext: PluginInitializerContext) {} + private config!: CloudConfigType; + + constructor(private readonly initializerContext: PluginInitializerContext) { + this.config = this.initializerContext.config.get(); + } public async setup(core: CoreSetup, { home }: CloudSetupDependencies) { - const { id, resetPasswordUrl } = this.initializerContext.config.get(); + const { id, resetPasswordUrl } = this.config; const isCloudEnabled = getIsCloudEnabled(id); if (home) { @@ -44,6 +50,16 @@ export class CloudPlugin implements Plugin { } public start(coreStart: CoreStart) { + const { deploymentUrl } = this.config; coreStart.chrome.setHelpSupportUrl(ELASTIC_SUPPORT_LINK); + if (deploymentUrl) { + coreStart.chrome.setCustomNavLink({ + title: i18n.translate('xpack.cloud.deploymentLinkLabel', { + defaultMessage: 'Manage this deployment', + }), + euiIconType: 'arrowLeft', + href: deploymentUrl, + }); + } } } diff --git a/x-pack/plugins/cloud/server/config.ts b/x-pack/plugins/cloud/server/config.ts index d899b45aebdfe7..ff8a2c5acdf9ab 100644 --- a/x-pack/plugins/cloud/server/config.ts +++ b/x-pack/plugins/cloud/server/config.ts @@ -22,6 +22,7 @@ const configSchema = schema.object({ id: schema.maybe(schema.string()), apm: schema.maybe(apmConfigSchema), resetPasswordUrl: schema.maybe(schema.string()), + deploymentUrl: schema.maybe(schema.string()), }); export type CloudConfigType = TypeOf; @@ -30,6 +31,7 @@ export const config: PluginConfigDescriptor = { exposeToBrowser: { id: true, resetPasswordUrl: true, + deploymentUrl: true, }, schema: configSchema, }; diff --git a/x-pack/plugins/encrypted_saved_objects/server/config.test.ts b/x-pack/plugins/encrypted_saved_objects/server/config.test.ts index db07f0f9ce2c0a..3f8074eb15c0c3 100644 --- a/x-pack/plugins/encrypted_saved_objects/server/config.test.ts +++ b/x-pack/plugins/encrypted_saved_objects/server/config.test.ts @@ -7,7 +7,7 @@ jest.mock('crypto', () => ({ randomBytes: jest.fn() })); import { first } from 'rxjs/operators'; -import { loggingServiceMock, coreMock } from 'src/core/server/mocks'; +import { loggingSystemMock, coreMock } from 'src/core/server/mocks'; import { createConfig$, ConfigSchema } from './config'; describe('config schema', () => { @@ -60,7 +60,7 @@ describe('createConfig$()', () => { usingEphemeralEncryptionKey: true, }); - expect(loggingServiceMock.collect(contextMock.logger).warn).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(contextMock.logger).warn).toMatchInlineSnapshot(` Array [ Array [ "Generating a random key for xpack.encryptedSavedObjects.encryptionKey. To be able to decrypt encrypted saved objects attributes after restart, please set xpack.encryptedSavedObjects.encryptionKey in kibana.yml", @@ -79,6 +79,6 @@ describe('createConfig$()', () => { usingEphemeralEncryptionKey: false, }); - expect(loggingServiceMock.collect(contextMock.logger).warn).toEqual([]); + expect(loggingSystemMock.collect(contextMock.logger).warn).toEqual([]); }); }); diff --git a/x-pack/plugins/encrypted_saved_objects/server/crypto/encrypted_saved_objects_service.test.ts b/x-pack/plugins/encrypted_saved_objects/server/crypto/encrypted_saved_objects_service.test.ts index 6ece9d1be8ec8b..db7c96f83dff25 100644 --- a/x-pack/plugins/encrypted_saved_objects/server/crypto/encrypted_saved_objects_service.test.ts +++ b/x-pack/plugins/encrypted_saved_objects/server/crypto/encrypted_saved_objects_service.test.ts @@ -12,7 +12,7 @@ import { EncryptedSavedObjectsAuditLogger } from '../audit'; import { EncryptedSavedObjectsService } from './encrypted_saved_objects_service'; import { EncryptionError } from './encryption_error'; -import { loggingServiceMock } from 'src/core/server/mocks'; +import { loggingSystemMock } from 'src/core/server/mocks'; import { encryptedSavedObjectsAuditLoggerMock } from '../audit/index.mock'; let service: EncryptedSavedObjectsService; @@ -28,7 +28,7 @@ beforeEach(() => { service = new EncryptedSavedObjectsService( 'encryption-key-abc', - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), mockAuditLogger ); }); @@ -222,7 +222,7 @@ describe('#encryptAttributes', () => { service = new EncryptedSavedObjectsService( 'encryption-key-abc', - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), mockAuditLogger ); }); @@ -916,7 +916,7 @@ describe('#decryptAttributes', () => { it('fails if encrypted with another encryption key', async () => { service = new EncryptedSavedObjectsService( 'encryption-key-abc*', - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), mockAuditLogger ); diff --git a/x-pack/plugins/event_log/server/es/cluster_client_adapter.test.ts b/x-pack/plugins/event_log/server/es/cluster_client_adapter.test.ts index ada86adf84cfd2..459a2cc65671e6 100644 --- a/x-pack/plugins/event_log/server/es/cluster_client_adapter.test.ts +++ b/x-pack/plugins/event_log/server/es/cluster_client_adapter.test.ts @@ -5,7 +5,7 @@ */ import { ClusterClient, Logger } from '../../../../../src/core/server'; -import { elasticsearchServiceMock, loggingServiceMock } from '../../../../../src/core/server/mocks'; +import { elasticsearchServiceMock, loggingSystemMock } from '../../../../../src/core/server/mocks'; import { ClusterClientAdapter, IClusterClientAdapter } from './cluster_client_adapter'; import moment from 'moment'; import { findOptionsSchema } from '../event_log_client'; @@ -17,7 +17,7 @@ let clusterClient: EsClusterClient; let clusterClientAdapter: IClusterClientAdapter; beforeEach(() => { - logger = loggingServiceMock.createLogger(); + logger = loggingSystemMock.createLogger(); clusterClient = elasticsearchServiceMock.createClusterClient(); clusterClientAdapter = new ClusterClientAdapter({ logger, diff --git a/x-pack/plugins/event_log/server/es/context.mock.ts b/x-pack/plugins/event_log/server/es/context.mock.ts index c15fee803fb71e..0c9f7b29b64119 100644 --- a/x-pack/plugins/event_log/server/es/context.mock.ts +++ b/x-pack/plugins/event_log/server/es/context.mock.ts @@ -7,14 +7,14 @@ import { EsContext } from './context'; import { namesMock } from './names.mock'; import { IClusterClientAdapter } from './cluster_client_adapter'; -import { loggingServiceMock } from '../../../../../src/core/server/mocks'; +import { loggingSystemMock } from '../../../../../src/core/server/mocks'; import { clusterClientAdapterMock } from './cluster_client_adapter.mock'; const createContextMock = () => { const mock: jest.Mocked & { esAdapter: jest.Mocked; } = { - logger: loggingServiceMock.createLogger(), + logger: loggingSystemMock.createLogger(), esNames: namesMock.create(), initialize: jest.fn(), waitTillReady: jest.fn(), diff --git a/x-pack/plugins/event_log/server/es/context.test.ts b/x-pack/plugins/event_log/server/es/context.test.ts index 09fe676a5762ed..6f9ee5875ddb73 100644 --- a/x-pack/plugins/event_log/server/es/context.test.ts +++ b/x-pack/plugins/event_log/server/es/context.test.ts @@ -6,7 +6,7 @@ import { createEsContext } from './context'; import { ClusterClient, Logger } from '../../../../../src/core/server'; -import { elasticsearchServiceMock, loggingServiceMock } from '../../../../../src/core/server/mocks'; +import { elasticsearchServiceMock, loggingSystemMock } from '../../../../../src/core/server/mocks'; jest.mock('../lib/../../../../package.json', () => ({ version: '1.2.3', })); @@ -16,7 +16,7 @@ let logger: Logger; let clusterClient: EsClusterClient; beforeEach(() => { - logger = loggingServiceMock.createLogger(); + logger = loggingSystemMock.createLogger(); clusterClient = elasticsearchServiceMock.createClusterClient(); }); diff --git a/x-pack/plugins/event_log/server/event_log_service.test.ts b/x-pack/plugins/event_log/server/event_log_service.test.ts index 43883ea4e384ce..2cf68592f2fa17 100644 --- a/x-pack/plugins/event_log/server/event_log_service.test.ts +++ b/x-pack/plugins/event_log/server/event_log_service.test.ts @@ -7,9 +7,9 @@ import { IEventLogConfig } from './types'; import { EventLogService } from './event_log_service'; import { contextMock } from './es/context.mock'; -import { loggingServiceMock } from 'src/core/server/mocks'; +import { loggingSystemMock } from 'src/core/server/mocks'; -const loggingService = loggingServiceMock.create(); +const loggingService = loggingSystemMock.create(); const systemLogger = loggingService.get(); describe('EventLogService', () => { diff --git a/x-pack/plugins/event_log/server/event_logger.test.ts b/x-pack/plugins/event_log/server/event_logger.test.ts index 2bda194a65d133..d4d3df3ef8267c 100644 --- a/x-pack/plugins/event_log/server/event_logger.test.ts +++ b/x-pack/plugins/event_log/server/event_logger.test.ts @@ -9,20 +9,20 @@ import { ECS_VERSION } from './types'; import { EventLogService } from './event_log_service'; import { EsContext } from './es/context'; import { contextMock } from './es/context.mock'; -import { loggingServiceMock } from 'src/core/server/mocks'; +import { loggingSystemMock } from 'src/core/server/mocks'; import { delay } from './lib/delay'; import { EVENT_LOGGED_PREFIX } from './event_logger'; const KIBANA_SERVER_UUID = '424-24-2424'; describe('EventLogger', () => { - let systemLogger: ReturnType; + let systemLogger: ReturnType; let esContext: EsContext; let service: IEventLogService; let eventLogger: IEventLogger; beforeEach(() => { - systemLogger = loggingServiceMock.createLogger(); + systemLogger = loggingSystemMock.createLogger(); esContext = contextMock.create(); service = new EventLogService({ esContext, @@ -183,7 +183,7 @@ describe('EventLogger', () => { // return the next logged event; throw if not an event async function waitForLogEvent( - mockLogger: ReturnType, + mockLogger: ReturnType, waitSeconds: number = 1 ): Promise { const result = await waitForLog(mockLogger, waitSeconds); @@ -193,7 +193,7 @@ async function waitForLogEvent( // return the next logged message; throw if it is an event async function waitForLogMessage( - mockLogger: ReturnType, + mockLogger: ReturnType, waitSeconds: number = 1 ): Promise { const result = await waitForLog(mockLogger, waitSeconds); @@ -203,7 +203,7 @@ async function waitForLogMessage( // return the next logged message, if it's an event log entry, parse it async function waitForLog( - mockLogger: ReturnType, + mockLogger: ReturnType, waitSeconds: number = 1 ): Promise { const intervals = 4; diff --git a/x-pack/plugins/event_log/server/lib/bounded_queue.test.ts b/x-pack/plugins/event_log/server/lib/bounded_queue.test.ts index dd6d15a6e48431..b30d83f24f261a 100644 --- a/x-pack/plugins/event_log/server/lib/bounded_queue.test.ts +++ b/x-pack/plugins/event_log/server/lib/bounded_queue.test.ts @@ -5,9 +5,9 @@ */ import { createBoundedQueue } from './bounded_queue'; -import { loggingServiceMock } from 'src/core/server/mocks'; +import { loggingSystemMock } from 'src/core/server/mocks'; -const loggingService = loggingServiceMock.create(); +const loggingService = loggingSystemMock.create(); const logger = loggingService.get(); describe('basic', () => { diff --git a/x-pack/plugins/graph/kibana.json b/x-pack/plugins/graph/kibana.json index 4cae14f8939b20..ebe18dba2b58c8 100644 --- a/x-pack/plugins/graph/kibana.json +++ b/x-pack/plugins/graph/kibana.json @@ -4,7 +4,7 @@ "kibanaVersion": "kibana", "server": true, "ui": true, - "requiredPlugins": ["licensing", "data", "navigation", "savedObjects"], + "requiredPlugins": ["licensing", "data", "navigation", "savedObjects", "kibanaLegacy"], "optionalPlugins": ["home", "features"], "configPath": ["xpack", "graph"] } diff --git a/x-pack/plugins/graph/public/application.ts b/x-pack/plugins/graph/public/application.ts index b46bc88500e0a6..0969b80bc38b0b 100644 --- a/x-pack/plugins/graph/public/application.ts +++ b/x-pack/plugins/graph/public/application.ts @@ -35,6 +35,7 @@ import { configureAppAngularModule, createTopNavDirective, createTopNavHelper, + KibanaLegacyStart, } from '../../../../src/plugins/kibana_legacy/public'; import './index.scss'; @@ -67,9 +68,11 @@ export interface GraphDependencies { graphSavePolicy: string; overlays: OverlayStart; savedObjects: SavedObjectsStart; + kibanaLegacy: KibanaLegacyStart; } -export const renderApp = ({ appBasePath, element, ...deps }: GraphDependencies) => { +export const renderApp = ({ appBasePath, element, kibanaLegacy, ...deps }: GraphDependencies) => { + kibanaLegacy.loadFontAwesome(); const graphAngularModule = createLocalAngularModule(deps.navigation); configureAppAngularModule( graphAngularModule, diff --git a/x-pack/plugins/graph/public/plugin.ts b/x-pack/plugins/graph/public/plugin.ts index e97735c50388f0..5b2566ffab7c02 100644 --- a/x-pack/plugins/graph/public/plugin.ts +++ b/x-pack/plugins/graph/public/plugin.ts @@ -10,7 +10,10 @@ import { AppMountParameters, Plugin } from 'src/core/public'; import { PluginInitializerContext } from 'kibana/public'; import { Storage } from '../../../../src/plugins/kibana_utils/public'; -import { initAngularBootstrap } from '../../../../src/plugins/kibana_legacy/public'; +import { + initAngularBootstrap, + KibanaLegacyStart, +} from '../../../../src/plugins/kibana_legacy/public'; import { NavigationPublicPluginStart as NavigationStart } from '../../../../src/plugins/navigation/public'; import { DataPublicPluginStart } from '../../../../src/plugins/data/public'; @@ -34,6 +37,7 @@ export interface GraphPluginStartDependencies { navigation: NavigationStart; data: DataPublicPluginStart; savedObjects: SavedObjectsStart; + kibanaLegacy: KibanaLegacyStart; } export class GraphPlugin @@ -85,6 +89,7 @@ export class GraphPlugin core: coreStart, navigation: pluginsStart.navigation, data: pluginsStart.data, + kibanaLegacy: pluginsStart.kibanaLegacy, savedObjectsClient: coreStart.savedObjects.client, addBasePath: core.http.basePath.prepend, getBasePath: core.http.basePath.get, diff --git a/x-pack/plugins/index_management/__jest__/client_integration/helpers/http_requests.ts b/x-pack/plugins/index_management/__jest__/client_integration/helpers/http_requests.ts index 56d76da522ac22..907c749f8ec0b0 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/helpers/http_requests.ts +++ b/x-pack/plugins/index_management/__jest__/client_integration/helpers/http_requests.ts @@ -35,6 +35,22 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => { ]); }; + const setLoadDataStreamResponse = (response: HttpResponse = []) => { + server.respondWith('GET', `${API_BASE_PATH}/data_streams/:id`, [ + 200, + { 'Content-Type': 'application/json' }, + JSON.stringify(response), + ]); + }; + + const setDeleteDataStreamResponse = (response: HttpResponse = []) => { + server.respondWith('POST', `${API_BASE_PATH}/delete_data_streams`, [ + 200, + { 'Content-Type': 'application/json' }, + JSON.stringify(response), + ]); + }; + const setDeleteTemplateResponse = (response: HttpResponse = []) => { server.respondWith('POST', `${API_BASE_PATH}/delete_index_templates`, [ 200, @@ -80,6 +96,8 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => { setLoadTemplatesResponse, setLoadIndicesResponse, setLoadDataStreamsResponse, + setLoadDataStreamResponse, + setDeleteDataStreamResponse, setDeleteTemplateResponse, setLoadTemplateResponse, setCreateTemplateResponse, diff --git a/x-pack/plugins/index_management/__jest__/client_integration/helpers/setup_environment.tsx b/x-pack/plugins/index_management/__jest__/client_integration/helpers/setup_environment.tsx index 0a49191fdb1496..d85db94d4a970c 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/helpers/setup_environment.tsx +++ b/x-pack/plugins/index_management/__jest__/client_integration/helpers/setup_environment.tsx @@ -8,6 +8,7 @@ import React from 'react'; import axios from 'axios'; import axiosXhrAdapter from 'axios/lib/adapters/xhr'; +import { merge } from 'lodash'; import { notificationServiceMock, @@ -33,7 +34,7 @@ export const services = { services.uiMetricService.setup({ reportUiStats() {} } as any); setExtensionsService(services.extensionsService); setUiMetricService(services.uiMetricService); -const appDependencies = { services, core: {}, plugins: {} } as any; +const appDependencies = { services, core: { getUrlForApp: () => {} }, plugins: {} } as any; export const setupEnvironment = () => { // Mock initialization of services @@ -51,8 +52,13 @@ export const setupEnvironment = () => { }; }; -export const WithAppDependencies = (Comp: any) => (props: any) => ( - - - -); +export const WithAppDependencies = (Comp: any, overridingDependencies: any = {}) => ( + props: any +) => { + const mergedDependencies = merge({}, appDependencies, overridingDependencies); + return ( + + + + ); +}; diff --git a/x-pack/plugins/index_management/__jest__/client_integration/home/data_streams_tab.helpers.ts b/x-pack/plugins/index_management/__jest__/client_integration/home/data_streams_tab.helpers.ts index ef6aca44a1754c..ecea230ecab85e 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/home/data_streams_tab.helpers.ts +++ b/x-pack/plugins/index_management/__jest__/client_integration/home/data_streams_tab.helpers.ts @@ -5,6 +5,7 @@ */ import { act } from 'react-dom/test-utils'; +import { ReactWrapper } from 'enzyme'; import { registerTestBed, @@ -17,27 +18,38 @@ import { IndexManagementHome } from '../../../public/application/sections/home'; import { indexManagementStore } from '../../../public/application/store'; // eslint-disable-line @kbn/eslint/no-restricted-paths import { WithAppDependencies, services, TestSubjects } from '../helpers'; -const testBedConfig: TestBedConfig = { - store: () => indexManagementStore(services as any), - memoryRouter: { - initialEntries: [`/indices`], - componentRoutePath: `/:section(indices|data_streams|templates)`, - }, - doMountAsync: true, -}; - -const initTestBed = registerTestBed(WithAppDependencies(IndexManagementHome), testBedConfig); - export interface DataStreamsTabTestBed extends TestBed { actions: { goToDataStreamsList: () => void; clickEmptyPromptIndexTemplateLink: () => void; clickReloadButton: () => void; + clickNameAt: (index: number) => void; clickIndicesAt: (index: number) => void; + clickDeletActionAt: (index: number) => void; + clickConfirmDelete: () => void; + clickDeletDataStreamButton: () => void; }; + findDeleteActionAt: (index: number) => ReactWrapper; + findDeleteConfirmationModal: () => ReactWrapper; + findDetailPanel: () => ReactWrapper; + findDetailPanelTitle: () => string; + findEmptyPromptIndexTemplateLink: () => ReactWrapper; } -export const setup = async (): Promise => { +export const setup = async (overridingDependencies: any = {}): Promise => { + const testBedConfig: TestBedConfig = { + store: () => indexManagementStore(services as any), + memoryRouter: { + initialEntries: [`/indices`], + componentRoutePath: `/:section(indices|data_streams|templates)`, + }, + doMountAsync: true, + }; + + const initTestBed = registerTestBed( + WithAppDependencies(IndexManagementHome, overridingDependencies), + testBedConfig + ); const testBed = await initTestBed(); /** @@ -48,15 +60,17 @@ export const setup = async (): Promise => { testBed.find('data_streamsTab').simulate('click'); }; - const clickEmptyPromptIndexTemplateLink = async () => { - const { find, component, router } = testBed; - + const findEmptyPromptIndexTemplateLink = () => { + const { find } = testBed; const templateLink = find('dataStreamsEmptyPromptTemplateLink'); + return templateLink; + }; + const clickEmptyPromptIndexTemplateLink = async () => { + const { component, router } = testBed; await act(async () => { - router.navigateTo(templateLink.props().href!); + router.navigateTo(findEmptyPromptIndexTemplateLink().props().href!); }); - component.update(); }; @@ -65,10 +79,15 @@ export const setup = async (): Promise => { find('reloadButton').simulate('click'); }; - const clickIndicesAt = async (index: number) => { - const { component, table, router } = testBed; + const findTestSubjectAt = (testSubject: string, index: number) => { + const { table } = testBed; const { rows } = table.getMetaData('dataStreamTable'); - const indicesLink = findTestSubject(rows[index].reactWrapper, 'indicesLink'); + return findTestSubject(rows[index].reactWrapper, testSubject); + }; + + const clickIndicesAt = async (index: number) => { + const { component, router } = testBed; + const indicesLink = findTestSubjectAt('indicesLink', index); await act(async () => { router.navigateTo(indicesLink.props().href!); @@ -77,20 +96,77 @@ export const setup = async (): Promise => { component.update(); }; + const clickNameAt = async (index: number) => { + const { component, router } = testBed; + const nameLink = findTestSubjectAt('nameLink', index); + + await act(async () => { + router.navigateTo(nameLink.props().href!); + }); + + component.update(); + }; + + const findDeleteActionAt = findTestSubjectAt.bind(null, 'deleteDataStream'); + + const clickDeletActionAt = (index: number) => { + findDeleteActionAt(index).simulate('click'); + }; + + const findDeleteConfirmationModal = () => { + const { find } = testBed; + return find('deleteDataStreamsConfirmation'); + }; + + const clickConfirmDelete = async () => { + const modal = document.body.querySelector('[data-test-subj="deleteDataStreamsConfirmation"]'); + const confirmButton: HTMLButtonElement | null = modal!.querySelector( + '[data-test-subj="confirmModalConfirmButton"]' + ); + + await act(async () => { + confirmButton!.click(); + }); + }; + + const clickDeletDataStreamButton = () => { + const { find } = testBed; + find('deleteDataStreamButton').simulate('click'); + }; + + const findDetailPanel = () => { + const { find } = testBed; + return find('dataStreamDetailPanel'); + }; + + const findDetailPanelTitle = () => { + const { find } = testBed; + return find('dataStreamDetailPanelTitle').text(); + }; + return { ...testBed, actions: { goToDataStreamsList, clickEmptyPromptIndexTemplateLink, clickReloadButton, + clickNameAt, clickIndicesAt, + clickDeletActionAt, + clickConfirmDelete, + clickDeletDataStreamButton, }, + findDeleteActionAt, + findDeleteConfirmationModal, + findDetailPanel, + findDetailPanelTitle, + findEmptyPromptIndexTemplateLink, }; }; export const createDataStreamPayload = (name: string): DataStream => ({ name, - timeStampField: '@timestamp', + timeStampField: { name: '@timestamp', mapping: { type: 'date' } }, indices: [ { name: 'indexName', diff --git a/x-pack/plugins/index_management/__jest__/client_integration/home/data_streams_tab.test.ts b/x-pack/plugins/index_management/__jest__/client_integration/home/data_streams_tab.test.ts index efe2e2d0c74aee..dfcbb518694662 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/home/data_streams_tab.test.ts +++ b/x-pack/plugins/index_management/__jest__/client_integration/home/data_streams_tab.test.ts @@ -19,61 +19,38 @@ describe('Data Streams tab', () => { server.restore(); }); - beforeEach(async () => { - httpRequestsMockHelpers.setLoadIndicesResponse([ - { - health: '', - status: '', - primary: '', - replica: '', - documents: '', - documents_deleted: '', - size: '', - primary_size: '', - name: 'data-stream-index', - data_stream: 'dataStream1', - }, - { - health: 'green', - status: 'open', - primary: 1, - replica: 1, - documents: 10000, - documents_deleted: 100, - size: '156kb', - primary_size: '156kb', - name: 'non-data-stream-index', - }, - ]); - - await act(async () => { - testBed = await setup(); - }); - }); - describe('when there are no data streams', () => { beforeEach(async () => { - const { actions, component } = testBed; - + httpRequestsMockHelpers.setLoadIndicesResponse([]); httpRequestsMockHelpers.setLoadDataStreamsResponse([]); httpRequestsMockHelpers.setLoadTemplatesResponse({ templates: [], legacyTemplates: [] }); + }); + + test('displays an empty prompt', async () => { + testBed = await setup(); await act(async () => { - actions.goToDataStreamsList(); + testBed.actions.goToDataStreamsList(); }); + const { exists, component } = testBed; component.update(); - }); - - test('displays an empty prompt', async () => { - const { exists } = testBed; expect(exists('sectionLoading')).toBe(false); expect(exists('emptyPrompt')).toBe(true); }); - test('goes to index templates tab when "Get started" link is clicked', async () => { - const { actions, exists } = testBed; + test('when Ingest Manager is disabled, goes to index templates tab when "Get started" link is clicked', async () => { + testBed = await setup({ + plugins: {}, + }); + + await act(async () => { + testBed.actions.goToDataStreamsList(); + }); + + const { actions, exists, component } = testBed; + component.update(); await act(async () => { actions.clickEmptyPromptIndexTemplateLink(); @@ -81,32 +58,77 @@ describe('Data Streams tab', () => { expect(exists('templateList')).toBe(true); }); + + test('when Ingest Manager is enabled, links to Ingest Manager', async () => { + testBed = await setup({ + plugins: { ingestManager: { hi: 'ok' } }, + }); + + await act(async () => { + testBed.actions.goToDataStreamsList(); + }); + + const { findEmptyPromptIndexTemplateLink, component } = testBed; + component.update(); + + // Assert against the text because the href won't be available, due to dependency upon our core mock. + expect(findEmptyPromptIndexTemplateLink().text()).toBe('Ingest Manager'); + }); }); describe('when there are data streams', () => { beforeEach(async () => { - const { actions, component } = testBed; + httpRequestsMockHelpers.setLoadIndicesResponse([ + { + health: '', + status: '', + primary: '', + replica: '', + documents: '', + documents_deleted: '', + size: '', + primary_size: '', + name: 'data-stream-index', + data_stream: 'dataStream1', + }, + { + health: 'green', + status: 'open', + primary: 1, + replica: 1, + documents: 10000, + documents_deleted: 100, + size: '156kb', + primary_size: '156kb', + name: 'non-data-stream-index', + }, + ]); + + const dataStreamForDetailPanel = createDataStreamPayload('dataStream1'); httpRequestsMockHelpers.setLoadDataStreamsResponse([ - createDataStreamPayload('dataStream1'), + dataStreamForDetailPanel, createDataStreamPayload('dataStream2'), ]); + httpRequestsMockHelpers.setLoadDataStreamResponse(dataStreamForDetailPanel); + + testBed = await setup(); + await act(async () => { - actions.goToDataStreamsList(); + testBed.actions.goToDataStreamsList(); }); - component.update(); + testBed.component.update(); }); test('lists them in the table', async () => { const { table } = testBed; - const { tableCellsValues } = table.getMetaData('dataStreamTable'); expect(tableCellsValues).toEqual([ - ['dataStream1', '1', '@timestamp', '1'], - ['dataStream2', '1', '@timestamp', '1'], + ['', 'dataStream1', '1', ''], + ['', 'dataStream2', '1', ''], ]); }); @@ -126,12 +148,90 @@ describe('Data Streams tab', () => { test('clicking the indices count navigates to the backing indices', async () => { const { table, actions } = testBed; - await actions.clickIndicesAt(0); - expect(table.getMetaData('indexTable').tableCellsValues).toEqual([ ['', '', '', '', '', '', '', 'dataStream1'], ]); }); + + describe('row actions', () => { + test('can delete', () => { + const { findDeleteActionAt } = testBed; + const deleteAction = findDeleteActionAt(0); + expect(deleteAction.length).toBe(1); + }); + }); + + describe('deleting a data stream', () => { + test('shows a confirmation modal', async () => { + const { + actions: { clickDeletActionAt }, + findDeleteConfirmationModal, + } = testBed; + clickDeletActionAt(0); + const confirmationModal = findDeleteConfirmationModal(); + expect(confirmationModal).toBeDefined(); + }); + + test('sends a request to the Delete API', async () => { + const { + actions: { clickDeletActionAt, clickConfirmDelete }, + } = testBed; + clickDeletActionAt(0); + + httpRequestsMockHelpers.setDeleteDataStreamResponse({ + results: { + dataStreamsDeleted: ['dataStream1'], + errors: [], + }, + }); + + await clickConfirmDelete(); + + const { method, url, requestBody } = server.requests[server.requests.length - 1]; + + expect(method).toBe('POST'); + expect(url).toBe(`${API_BASE_PATH}/delete_data_streams`); + expect(JSON.parse(JSON.parse(requestBody).body)).toEqual({ + dataStreams: ['dataStream1'], + }); + }); + }); + + describe('detail panel', () => { + test('opens when the data stream name in the table is clicked', async () => { + const { actions, findDetailPanel, findDetailPanelTitle } = testBed; + await actions.clickNameAt(0); + expect(findDetailPanel().length).toBe(1); + expect(findDetailPanelTitle()).toBe('dataStream1'); + }); + + test('deletes the data stream when delete button is clicked', async () => { + const { + actions: { clickNameAt, clickDeletDataStreamButton, clickConfirmDelete }, + } = testBed; + + await clickNameAt(0); + + clickDeletDataStreamButton(); + + httpRequestsMockHelpers.setDeleteDataStreamResponse({ + results: { + dataStreamsDeleted: ['dataStream1'], + errors: [], + }, + }); + + await clickConfirmDelete(); + + const { method, url, requestBody } = server.requests[server.requests.length - 1]; + + expect(method).toBe('POST'); + expect(url).toBe(`${API_BASE_PATH}/delete_data_streams`); + expect(JSON.parse(JSON.parse(requestBody).body)).toEqual({ + dataStreams: ['dataStream1'], + }); + }); + }); }); }); diff --git a/x-pack/plugins/index_management/__jest__/client_integration/home/indices_tab.helpers.ts b/x-pack/plugins/index_management/__jest__/client_integration/home/indices_tab.helpers.ts index f00348aacbf085..11ea29fd9b78c6 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/home/indices_tab.helpers.ts +++ b/x-pack/plugins/index_management/__jest__/client_integration/home/indices_tab.helpers.ts @@ -5,6 +5,7 @@ */ import { act } from 'react-dom/test-utils'; +import { ReactWrapper } from 'enzyme'; import { registerTestBed, @@ -34,6 +35,8 @@ export interface IndicesTestBed extends TestBed { clickIncludeHiddenIndicesToggle: () => void; clickDataStreamAt: (index: number) => void; }; + findDataStreamDetailPanel: () => ReactWrapper; + findDataStreamDetailPanelTitle: () => string; } export const setup = async (): Promise => { @@ -77,6 +80,16 @@ export const setup = async (): Promise => { component.update(); }; + const findDataStreamDetailPanel = () => { + const { find } = testBed; + return find('dataStreamDetailPanel'); + }; + + const findDataStreamDetailPanelTitle = () => { + const { find } = testBed; + return find('dataStreamDetailPanelTitle').text(); + }; + return { ...testBed, actions: { @@ -85,5 +98,7 @@ export const setup = async (): Promise => { clickIncludeHiddenIndicesToggle, clickDataStreamAt, }, + findDataStreamDetailPanel, + findDataStreamDetailPanelTitle, }; }; diff --git a/x-pack/plugins/index_management/__jest__/client_integration/home/indices_tab.test.ts b/x-pack/plugins/index_management/__jest__/client_integration/home/indices_tab.test.ts index c2d955bb4dfce8..3d6d94d1658559 100644 --- a/x-pack/plugins/index_management/__jest__/client_integration/home/indices_tab.test.ts +++ b/x-pack/plugins/index_management/__jest__/client_integration/home/indices_tab.test.ts @@ -70,10 +70,10 @@ describe('', () => { }, ]); - httpRequestsMockHelpers.setLoadDataStreamsResponse([ - createDataStreamPayload('dataStream1'), - createDataStreamPayload('dataStream2'), - ]); + // The detail panel should still appear even if there are no data streams. + httpRequestsMockHelpers.setLoadDataStreamsResponse([]); + + httpRequestsMockHelpers.setLoadDataStreamResponse(createDataStreamPayload('dataStream1')); testBed = await setup(); @@ -86,13 +86,16 @@ describe('', () => { }); test('navigates to the data stream in the Data Streams tab', async () => { - const { table, actions } = testBed; + const { + findDataStreamDetailPanel, + findDataStreamDetailPanelTitle, + actions: { clickDataStreamAt }, + } = testBed; - await actions.clickDataStreamAt(0); + await clickDataStreamAt(0); - expect(table.getMetaData('dataStreamTable').tableCellsValues).toEqual([ - ['dataStream1', '1', '@timestamp', '1'], - ]); + expect(findDataStreamDetailPanel().length).toBe(1); + expect(findDataStreamDetailPanelTitle()).toBe('dataStream1'); }); }); diff --git a/x-pack/plugins/index_management/common/lib/data_stream_serialization.ts b/x-pack/plugins/index_management/common/lib/data_stream_serialization.ts index 9d267210a6b318..51528ed9856ce9 100644 --- a/x-pack/plugins/index_management/common/lib/data_stream_serialization.ts +++ b/x-pack/plugins/index_management/common/lib/data_stream_serialization.ts @@ -6,8 +6,10 @@ import { DataStream, DataStreamFromEs } from '../types'; -export function deserializeDataStreamList(dataStreamsFromEs: DataStreamFromEs[]): DataStream[] { - return dataStreamsFromEs.map(({ name, timestamp_field, indices, generation }) => ({ +export function deserializeDataStream(dataStreamFromEs: DataStreamFromEs): DataStream { + const { name, timestamp_field, indices, generation } = dataStreamFromEs; + + return { name, timeStampField: timestamp_field, indices: indices.map( @@ -17,5 +19,9 @@ export function deserializeDataStreamList(dataStreamsFromEs: DataStreamFromEs[]) }) ), generation, - })); + }; +} + +export function deserializeDataStreamList(dataStreamsFromEs: DataStreamFromEs[]): DataStream[] { + return dataStreamsFromEs.map((dataStream) => deserializeDataStream(dataStream)); } diff --git a/x-pack/plugins/index_management/common/lib/index.ts b/x-pack/plugins/index_management/common/lib/index.ts index fce4d8ccc2502b..4e76a40ced5240 100644 --- a/x-pack/plugins/index_management/common/lib/index.ts +++ b/x-pack/plugins/index_management/common/lib/index.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -export { deserializeDataStreamList } from './data_stream_serialization'; +export { deserializeDataStream, deserializeDataStreamList } from './data_stream_serialization'; export { deserializeLegacyTemplateList, diff --git a/x-pack/plugins/index_management/common/types/data_streams.ts b/x-pack/plugins/index_management/common/types/data_streams.ts index 5b743296d868bd..772ed43459bcf7 100644 --- a/x-pack/plugins/index_management/common/types/data_streams.ts +++ b/x-pack/plugins/index_management/common/types/data_streams.ts @@ -4,9 +4,18 @@ * you may not use this file except in compliance with the Elastic License. */ +interface TimestampFieldFromEs { + name: string; + mapping: { + type: string; + }; +} + +type TimestampField = TimestampFieldFromEs; + export interface DataStreamFromEs { name: string; - timestamp_field: string; + timestamp_field: TimestampFieldFromEs; indices: DataStreamIndexFromEs[]; generation: number; } @@ -18,7 +27,7 @@ export interface DataStreamIndexFromEs { export interface DataStream { name: string; - timeStampField: string; + timeStampField: TimestampField; indices: DataStreamIndex[]; generation: number; } diff --git a/x-pack/plugins/index_management/kibana.json b/x-pack/plugins/index_management/kibana.json index 2e0fa04337b400..40ecb26e8f0c96 100644 --- a/x-pack/plugins/index_management/kibana.json +++ b/x-pack/plugins/index_management/kibana.json @@ -10,7 +10,8 @@ ], "optionalPlugins": [ "security", - "usageCollection" + "usageCollection", + "ingestManager" ], "configPath": ["xpack", "index_management"] } diff --git a/x-pack/plugins/index_management/public/application/app_context.tsx b/x-pack/plugins/index_management/public/application/app_context.tsx index 84938de4169417..c8219071203736 100644 --- a/x-pack/plugins/index_management/public/application/app_context.tsx +++ b/x-pack/plugins/index_management/public/application/app_context.tsx @@ -6,9 +6,10 @@ import React, { createContext, useContext } from 'react'; import { ScopedHistory } from 'kibana/public'; -import { CoreStart } from '../../../../../src/core/public'; +import { UsageCollectionSetup } from 'src/plugins/usage_collection/public'; -import { UsageCollectionSetup } from '../../../../../src/plugins/usage_collection/public'; +import { CoreStart } from '../../../../../src/core/public'; +import { IngestManagerSetup } from '../../../ingest_manager/public'; import { IndexMgmtMetricsType } from '../types'; import { UiMetricService, NotificationService, HttpService } from './services'; import { ExtensionsService } from '../services'; @@ -22,6 +23,7 @@ export interface AppDependencies { }; plugins: { usageCollection: UsageCollectionSetup; + ingestManager?: IngestManagerSetup; }; services: { uiMetricService: UiMetricService; diff --git a/x-pack/plugins/index_management/public/application/components/component_templates/component_templates_context.tsx b/x-pack/plugins/index_management/public/application/components/component_templates/component_templates_context.tsx index e8116409def4b8..c78d24f126e297 100644 --- a/x-pack/plugins/index_management/public/application/components/component_templates/component_templates_context.tsx +++ b/x-pack/plugins/index_management/public/application/components/component_templates/component_templates_context.tsx @@ -5,7 +5,7 @@ */ import React, { createContext, useContext } from 'react'; -import { HttpSetup, DocLinksSetup, NotificationsSetup } from 'src/core/public'; +import { HttpSetup, DocLinksStart, NotificationsSetup } from 'src/core/public'; import { getApi, getUseRequest, getSendRequest, getDocumentation } from './lib'; @@ -15,7 +15,7 @@ interface Props { httpClient: HttpSetup; apiBasePath: string; trackMetric: (type: 'loaded' | 'click' | 'count', eventName: string) => void; - docLinks: DocLinksSetup; + docLinks: DocLinksStart; toasts: NotificationsSetup['toasts']; } diff --git a/x-pack/plugins/index_management/public/application/components/component_templates/lib/documentation.ts b/x-pack/plugins/index_management/public/application/components/component_templates/lib/documentation.ts index dc27dadf0b8073..9d20ae9d2ec76a 100644 --- a/x-pack/plugins/index_management/public/application/components/component_templates/lib/documentation.ts +++ b/x-pack/plugins/index_management/public/application/components/component_templates/lib/documentation.ts @@ -4,9 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { DocLinksSetup } from 'src/core/public'; +import { DocLinksStart } from 'src/core/public'; -export const getDocumentation = ({ ELASTIC_WEBSITE_URL, DOC_LINK_VERSION }: DocLinksSetup) => { +export const getDocumentation = ({ ELASTIC_WEBSITE_URL, DOC_LINK_VERSION }: DocLinksStart) => { const docsBase = `${ELASTIC_WEBSITE_URL}guide/en`; const esDocsBase = `${docsBase}/elasticsearch/reference/${DOC_LINK_VERSION}`; diff --git a/x-pack/plugins/index_management/public/application/components/shared/components/index.ts b/x-pack/plugins/index_management/public/application/components/shared/components/index.ts index e1700ad6a632d2..b67a9c355e723d 100644 --- a/x-pack/plugins/index_management/public/application/components/shared/components/index.ts +++ b/x-pack/plugins/index_management/public/application/components/shared/components/index.ts @@ -6,4 +6,9 @@ export { TabAliases, TabMappings, TabSettings } from './details_panel'; -export { StepAliases, StepMappings, StepSettings } from './wizard_steps'; +export { + StepAliasesContainer, + StepMappingsContainer, + StepSettingsContainer, + CommonWizardSteps, +} from './wizard_steps'; diff --git a/x-pack/plugins/index_management/public/application/components/shared/components/wizard_steps/index.ts b/x-pack/plugins/index_management/public/application/components/shared/components/wizard_steps/index.ts index 90ce6227c09c88..ea554ca269d8bd 100644 --- a/x-pack/plugins/index_management/public/application/components/shared/components/wizard_steps/index.ts +++ b/x-pack/plugins/index_management/public/application/components/shared/components/wizard_steps/index.ts @@ -4,6 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -export { StepAliases } from './step_aliases'; -export { StepMappings } from './step_mappings'; -export { StepSettings } from './step_settings'; +export { StepAliasesContainer } from './step_aliases_container'; +export { StepMappingsContainer } from './step_mappings_container'; +export { StepSettingsContainer } from './step_settings_container'; + +export { CommonWizardSteps } from './types'; diff --git a/x-pack/plugins/index_management/public/application/components/shared/components/wizard_steps/step_aliases_container.tsx b/x-pack/plugins/index_management/public/application/components/shared/components/wizard_steps/step_aliases_container.tsx new file mode 100644 index 00000000000000..a5953ea00a1063 --- /dev/null +++ b/x-pack/plugins/index_management/public/application/components/shared/components/wizard_steps/step_aliases_container.tsx @@ -0,0 +1,22 @@ +/* + * 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 { Forms } from '../../../../../shared_imports'; +import { CommonWizardSteps } from './types'; +import { StepAliases } from './step_aliases'; + +interface Props { + esDocsBase: string; +} + +export const StepAliasesContainer: React.FunctionComponent = ({ esDocsBase }) => { + const { defaultValue, updateContent } = Forms.useContent('aliases'); + + return ( + + ); +}; diff --git a/x-pack/plugins/index_management/public/application/components/template_form/steps/step_mappings_container.tsx b/x-pack/plugins/index_management/public/application/components/shared/components/wizard_steps/step_mappings_container.tsx similarity index 57% rename from x-pack/plugins/index_management/public/application/components/template_form/steps/step_mappings_container.tsx rename to x-pack/plugins/index_management/public/application/components/shared/components/wizard_steps/step_mappings_container.tsx index 80c0d1d4df4890..34e05d88c651d7 100644 --- a/x-pack/plugins/index_management/public/application/components/template_form/steps/step_mappings_container.tsx +++ b/x-pack/plugins/index_management/public/application/components/shared/components/wizard_steps/step_mappings_container.tsx @@ -5,20 +5,23 @@ */ import React from 'react'; -import { Forms } from '../../../../shared_imports'; -import { documentationService } from '../../../services/documentation'; -import { StepMappings } from '../../shared'; -import { WizardContent } from '../template_form'; +import { Forms } from '../../../../../shared_imports'; +import { CommonWizardSteps } from './types'; +import { StepMappings } from './step_mappings'; -export const StepMappingsContainer = () => { - const { defaultValue, updateContent, getData } = Forms.useContent('mappings'); +interface Props { + esDocsBase: string; +} + +export const StepMappingsContainer: React.FunctionComponent = ({ esDocsBase }) => { + const { defaultValue, updateContent, getData } = Forms.useContent('mappings'); return ( ); }; diff --git a/x-pack/plugins/index_management/public/application/components/shared/components/wizard_steps/step_settings_container.tsx b/x-pack/plugins/index_management/public/application/components/shared/components/wizard_steps/step_settings_container.tsx new file mode 100644 index 00000000000000..c540ddceb95c2f --- /dev/null +++ b/x-pack/plugins/index_management/public/application/components/shared/components/wizard_steps/step_settings_container.tsx @@ -0,0 +1,22 @@ +/* + * 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 { Forms } from '../../../../../shared_imports'; +import { CommonWizardSteps } from './types'; +import { StepSettings } from './step_settings'; + +interface Props { + esDocsBase: string; +} + +export const StepSettingsContainer = React.memo(({ esDocsBase }: Props) => { + const { defaultValue, updateContent } = Forms.useContent('settings'); + + return ( + + ); +}); diff --git a/x-pack/plugins/index_management/public/application/components/shared/components/wizard_steps/types.ts b/x-pack/plugins/index_management/public/application/components/shared/components/wizard_steps/types.ts new file mode 100644 index 00000000000000..f8088e2b6e058b --- /dev/null +++ b/x-pack/plugins/index_management/public/application/components/shared/components/wizard_steps/types.ts @@ -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. + */ + +import { Mappings, IndexSettings, Aliases } from '../../../../../../common'; + +export interface CommonWizardSteps { + settings?: IndexSettings; + mappings?: Mappings; + aliases?: Aliases; +} diff --git a/x-pack/plugins/index_management/public/application/components/shared/index.ts b/x-pack/plugins/index_management/public/application/components/shared/index.ts index 5ec1f717102709..897e86c99eca0b 100644 --- a/x-pack/plugins/index_management/public/application/components/shared/index.ts +++ b/x-pack/plugins/index_management/public/application/components/shared/index.ts @@ -8,7 +8,8 @@ export { TabAliases, TabMappings, TabSettings, - StepAliases, - StepMappings, - StepSettings, + StepAliasesContainer, + StepMappingsContainer, + StepSettingsContainer, + CommonWizardSteps, } from './components'; diff --git a/x-pack/plugins/index_management/public/application/components/template_form/steps/index.ts b/x-pack/plugins/index_management/public/application/components/template_form/steps/index.ts index 95d1222ad2cc9a..b7e3e36e61814d 100644 --- a/x-pack/plugins/index_management/public/application/components/template_form/steps/index.ts +++ b/x-pack/plugins/index_management/public/application/components/template_form/steps/index.ts @@ -5,7 +5,4 @@ */ export { StepLogisticsContainer } from './step_logistics_container'; -export { StepAliasesContainer } from './step_aliases_container'; -export { StepMappingsContainer } from './step_mappings_container'; -export { StepSettingsContainer } from './step_settings_container'; export { StepReviewContainer } from './step_review_container'; diff --git a/x-pack/plugins/index_management/public/application/components/template_form/steps/step_aliases_container.tsx b/x-pack/plugins/index_management/public/application/components/template_form/steps/step_aliases_container.tsx deleted file mode 100644 index a0e0c59be6622e..00000000000000 --- a/x-pack/plugins/index_management/public/application/components/template_form/steps/step_aliases_container.tsx +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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 { Forms } from '../../../../shared_imports'; -import { documentationService } from '../../../services/documentation'; -import { StepAliases } from '../../shared'; -import { WizardContent } from '../template_form'; - -export const StepAliasesContainer = () => { - const { defaultValue, updateContent } = Forms.useContent('aliases'); - - return ( - - ); -}; diff --git a/x-pack/plugins/index_management/public/application/components/template_form/steps/step_settings_container.tsx b/x-pack/plugins/index_management/public/application/components/template_form/steps/step_settings_container.tsx deleted file mode 100644 index b79c6804d382b3..00000000000000 --- a/x-pack/plugins/index_management/public/application/components/template_form/steps/step_settings_container.tsx +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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 { Forms } from '../../../../shared_imports'; -import { documentationService } from '../../../services/documentation'; -import { StepSettings } from '../../shared'; -import { WizardContent } from '../template_form'; - -export const StepSettingsContainer = React.memo(() => { - const { defaultValue, updateContent } = Forms.useContent('settings'); - - return ( - - ); -}); diff --git a/x-pack/plugins/index_management/public/application/components/template_form/template_form.tsx b/x-pack/plugins/index_management/public/application/components/template_form/template_form.tsx index 9e6d49faac5630..8a2c991aea8d07 100644 --- a/x-pack/plugins/index_management/public/application/components/template_form/template_form.tsx +++ b/x-pack/plugins/index_management/public/application/components/template_form/template_form.tsx @@ -11,13 +11,14 @@ import { EuiSpacer } from '@elastic/eui'; import { TemplateDeserialized, CREATE_LEGACY_TEMPLATE_BY_DEFAULT } from '../../../../common'; import { serializers, Forms } from '../../../shared_imports'; import { SectionError } from '../section_error'; +import { StepLogisticsContainer, StepReviewContainer } from './steps'; import { - StepLogisticsContainer, + CommonWizardSteps, StepSettingsContainer, StepMappingsContainer, StepAliasesContainer, - StepReviewContainer, -} from './steps'; +} from '../shared'; +import { documentationService } from '../../services/documentation'; const { stripEmptyFields } = serializers; const { FormWizard, FormWizardStep } = Forms; @@ -31,11 +32,8 @@ interface Props { isEditing?: boolean; } -export interface WizardContent { +export interface WizardContent extends CommonWizardSteps { logistics: Omit; - settings: TemplateDeserialized['template']['settings']; - mappings: TemplateDeserialized['template']['mappings']; - aliases: TemplateDeserialized['template']['aliases']; } export type WizardSection = keyof WizardContent | 'review'; @@ -183,15 +181,15 @@ export const TemplateForm = ({ - + - + - + diff --git a/x-pack/plugins/index_management/public/application/mount_management_section.ts b/x-pack/plugins/index_management/public/application/mount_management_section.ts index e8b6f200fb349f..258f32865720af 100644 --- a/x-pack/plugins/index_management/public/application/mount_management_section.ts +++ b/x-pack/plugins/index_management/public/application/mount_management_section.ts @@ -8,6 +8,7 @@ import { CoreSetup } from 'src/core/public'; import { ManagementAppMountParams } from 'src/plugins/management/public/'; import { UsageCollectionSetup } from 'src/plugins/usage_collection/public'; +import { IngestManagerSetup } from '../../../ingest_manager/public'; import { ExtensionsService } from '../services'; import { IndexMgmtMetricsType } from '../types'; import { AppDependencies } from './app_context'; @@ -28,7 +29,8 @@ export async function mountManagementSection( coreSetup: CoreSetup, usageCollection: UsageCollectionSetup, services: InternalServices, - params: ManagementAppMountParams + params: ManagementAppMountParams, + ingestManager?: IngestManagerSetup ) { const { element, setBreadcrumbs, history } = params; const [core] = await coreSetup.getStartServices(); @@ -44,6 +46,7 @@ export async function mountManagementSection( }, plugins: { usageCollection, + ingestManager, }, services, history, diff --git a/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/data_stream_detail_panel/data_stream_detail_panel.tsx b/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/data_stream_detail_panel/data_stream_detail_panel.tsx index a6c8b83a05f989..577f04a4a7efd1 100644 --- a/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/data_stream_detail_panel/data_stream_detail_panel.tsx +++ b/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/data_stream_detail_panel/data_stream_detail_panel.tsx @@ -4,9 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ -import React, { Fragment } from 'react'; +import React, { useState } from 'react'; import { FormattedMessage } from '@kbn/i18n/react'; import { + EuiButton, EuiFlyout, EuiFlyoutHeader, EuiTitle, @@ -15,14 +16,18 @@ import { EuiFlexGroup, EuiFlexItem, EuiButtonEmpty, + EuiDescriptionList, + EuiDescriptionListTitle, + EuiDescriptionListDescription, } from '@elastic/eui'; import { SectionLoading, SectionError, Error } from '../../../../components'; import { useLoadDataStream } from '../../../../services/api'; +import { DeleteDataStreamConfirmationModal } from '../delete_data_stream_confirmation_modal'; interface Props { dataStreamName: string; - onClose: () => void; + onClose: (shouldReload?: boolean) => void; } /** @@ -36,6 +41,8 @@ export const DataStreamDetailPanel: React.FunctionComponent = ({ }) => { const { error, data: dataStream, isLoading } = useLoadDataStream(dataStreamName); + const [isDeleting, setIsDeleting] = useState(false); + let content; if (isLoading) { @@ -61,44 +68,97 @@ export const DataStreamDetailPanel: React.FunctionComponent = ({ /> ); } else if (dataStream) { - content = {JSON.stringify(dataStream)}; + const { timeStampField, generation } = dataStream; + + content = ( + + + + + + {timeStampField.name} + + + + + + {generation} + + ); } return ( - - - -

- {dataStreamName} -

-
-
- - {content} - - - - - - - - - - -
+ <> + {isDeleting ? ( + { + if (data && data.hasDeletedDataStreams) { + onClose(true); + } else { + setIsDeleting(false); + } + }} + dataStreams={[dataStreamName]} + /> + ) : null} + + + + +

+ {dataStreamName} +

+
+
+ + {content} + + + + + onClose()} + data-test-subj="closeDetailsButton" + > + + + + + {!isLoading && !error ? ( + + setIsDeleting(true)} + data-test-subj="deleteDataStreamButton" + > + + + + ) : null} + + +
+ ); }; diff --git a/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/data_stream_list.tsx b/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/data_stream_list.tsx index 951c4a0d7f3c31..bad008b665cfbf 100644 --- a/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/data_stream_list.tsx +++ b/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/data_stream_list.tsx @@ -12,9 +12,13 @@ import { EuiTitle, EuiText, EuiSpacer, EuiEmptyPrompt, EuiLink } from '@elastic/ import { ScopedHistory } from 'kibana/public'; import { reactRouterNavigate } from '../../../../shared_imports'; +import { useAppContext } from '../../../app_context'; import { SectionError, SectionLoading, Error } from '../../../components'; import { useLoadDataStreams } from '../../../services/api'; +import { decodePathFromReactRouter } from '../../../services/routing'; +import { Section } from '../../home'; import { DataStreamTable } from './data_stream_table'; +import { DataStreamDetailPanel } from './data_stream_detail_panel'; interface MatchParams { dataStreamName?: string; @@ -26,6 +30,11 @@ export const DataStreamList: React.FunctionComponent { + const { + core: { getUrlForApp }, + plugins: { ingestManager }, + } = useAppContext(); + const { error, isLoading, data: dataStreams, sendRequest: reload } = useLoadDataStreams(); let content; @@ -67,22 +76,52 @@ export const DataStreamList: React.FunctionComponent - {i18n.translate('xpack.idxMgmt.dataStreamList.emptyPrompt.getStartedLink', { - defaultMessage: 'composable index template', - })} - - ), - }} + defaultMessage="Data streams represent collections of time series indices." /> + {' ' /* We need this space to separate these two sentences. */} + {ingestManager ? ( + + {i18n.translate( + 'xpack.idxMgmt.dataStreamList.emptyPrompt.noDataStreamsCtaIngestManagerLink', + { + defaultMessage: 'Ingest Manager', + } + )} + + ), + }} + /> + ) : ( + + {i18n.translate( + 'xpack.idxMgmt.dataStreamList.emptyPrompt.noDataStreamsCtaIndexTemplateLink', + { + defaultMessage: 'composable index template', + } + )} + + ), + }} + /> + )}

} data-test-subj="emptyPrompt" @@ -104,24 +143,38 @@ export const DataStreamList: React.FunctionComponent - - {/* TODO: Implement this once we have something to put in here, e.g. storage size, docs count */} - {/* dataStreamName && ( - { - history.push('/data_streams'); - }} - /> - )*/} ); } - return
{content}
; + return ( +
+ {content} + + {/* + If the user has been deep-linked, they'll expect to see the detail panel because it reflects + the URL state, even if there are no data streams or if there was an error loading them. + */} + {dataStreamName && ( + { + history.push(`/${Section.DataStreams}`); + + // If the data stream was deleted, we need to refresh the list. + if (shouldReload) { + reload(); + } + }} + /> + )} +
+ ); }; diff --git a/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/data_stream_table/data_stream_table.tsx b/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/data_stream_table/data_stream_table.tsx index 54b215e561b462..d01d8fa03a3fae 100644 --- a/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/data_stream_table/data_stream_table.tsx +++ b/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/data_stream_table/data_stream_table.tsx @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import React from 'react'; +import React, { useState } from 'react'; import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; import { EuiInMemoryTable, EuiBasicTableColumn, EuiButton, EuiLink } from '@elastic/eui'; @@ -13,6 +13,8 @@ import { ScopedHistory } from 'kibana/public'; import { DataStream } from '../../../../../../common/types'; import { reactRouterNavigate } from '../../../../../shared_imports'; import { encodePathForReactRouter } from '../../../../services/routing'; +import { Section } from '../../../home'; +import { DeleteDataStreamConfirmationModal } from '../delete_data_stream_confirmation_modal'; interface Props { dataStreams?: DataStream[]; @@ -27,6 +29,9 @@ export const DataStreamTable: React.FunctionComponent = ({ history, filters, }) => { + const [selection, setSelection] = useState([]); + const [dataStreamsToDelete, setDataStreamsToDelete] = useState([]); + const columns: Array> = [ { field: 'name', @@ -35,7 +40,19 @@ export const DataStreamTable: React.FunctionComponent = ({ }), truncateText: true, sortable: true, - // TODO: Render as a link to open the detail panel + render: (name: DataStream['name'], item: DataStream) => { + return ( + /* eslint-disable-next-line @elastic/eui/href-or-on-click */ + + {name} + + ); + }, }, { field: 'indices', @@ -59,20 +76,27 @@ export const DataStreamTable: React.FunctionComponent = ({ ), }, { - field: 'timeStampField', - name: i18n.translate('xpack.idxMgmt.dataStreamList.table.timeStampFieldColumnTitle', { - defaultMessage: 'Timestamp field', + name: i18n.translate('xpack.idxMgmt.dataStreamList.table.actionColumnTitle', { + defaultMessage: 'Actions', }), - truncateText: true, - sortable: true, - }, - { - field: 'generation', - name: i18n.translate('xpack.idxMgmt.dataStreamList.table.generationFieldColumnTitle', { - defaultMessage: 'Generation', - }), - truncateText: true, - sortable: true, + actions: [ + { + name: i18n.translate('xpack.idxMgmt.dataStreamList.table.actionDeleteText', { + defaultMessage: 'Delete', + }), + description: i18n.translate('xpack.idxMgmt.dataStreamList.table.actionDeleteDecription', { + defaultMessage: 'Delete this data stream', + }), + icon: 'trash', + color: 'danger', + type: 'icon', + onClick: ({ name }: DataStream) => { + setDataStreamsToDelete([name]); + }, + isPrimary: true, + 'data-test-subj': 'deleteDataStream', + }, + ], }, ]; @@ -88,12 +112,29 @@ export const DataStreamTable: React.FunctionComponent = ({ }, } as const; + const selectionConfig = { + onSelectionChange: setSelection, + }; + const searchConfig = { query: filters, box: { incremental: true, }, - toolsLeft: undefined /* TODO: Actions menu */, + toolsLeft: + selection.length > 0 ? ( + setDataStreamsToDelete(selection.map(({ name }: DataStream) => name))} + color="danger" + > + + + ) : undefined, toolsRight: [ = ({ return ( <> + {dataStreamsToDelete && dataStreamsToDelete.length > 0 ? ( + { + if (data && data.hasDeletedDataStreams) { + reload(); + } else { + setDataStreamsToDelete([]); + } + }} + dataStreams={dataStreamsToDelete} + /> + ) : null} = ({ search={searchConfig} sorting={sorting} isSelectable={true} + selection={selectionConfig} pagination={pagination} rowProps={() => ({ 'data-test-subj': 'row', diff --git a/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/delete_data_stream_confirmation_modal/delete_data_stream_confirmation_modal.tsx b/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/delete_data_stream_confirmation_modal/delete_data_stream_confirmation_modal.tsx new file mode 100644 index 00000000000000..fc8e41aa634b47 --- /dev/null +++ b/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/delete_data_stream_confirmation_modal/delete_data_stream_confirmation_modal.tsx @@ -0,0 +1,149 @@ +/* + * 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, { Fragment } from 'react'; +import { EuiCallOut, EuiConfirmModal, EuiOverlayMask, EuiSpacer } from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; +import { FormattedMessage } from '@kbn/i18n/react'; + +import { deleteDataStreams } from '../../../../services/api'; +import { notificationService } from '../../../../services/notification'; + +interface Props { + dataStreams: string[]; + onClose: (data?: { hasDeletedDataStreams: boolean }) => void; +} + +export const DeleteDataStreamConfirmationModal: React.FunctionComponent = ({ + dataStreams, + onClose, +}: { + dataStreams: string[]; + onClose: (data?: { hasDeletedDataStreams: boolean }) => void; +}) => { + const dataStreamsCount = dataStreams.length; + + const handleDeleteDataStreams = () => { + deleteDataStreams(dataStreams).then(({ data: { dataStreamsDeleted, errors }, error }) => { + const hasDeletedDataStreams = dataStreamsDeleted && dataStreamsDeleted.length; + + if (hasDeletedDataStreams) { + const successMessage = + dataStreamsDeleted.length === 1 + ? i18n.translate( + 'xpack.idxMgmt.deleteDataStreamsConfirmationModal.successDeleteSingleNotificationMessageText', + { + defaultMessage: "Deleted data stream '{dataStreamName}'", + values: { dataStreamName: dataStreams[0] }, + } + ) + : i18n.translate( + 'xpack.idxMgmt.deleteDataStreamsConfirmationModal.successDeleteMultipleNotificationMessageText', + { + defaultMessage: + 'Deleted {numSuccesses, plural, one {# data stream} other {# data streams}}', + values: { numSuccesses: dataStreamsDeleted.length }, + } + ); + + onClose({ hasDeletedDataStreams }); + notificationService.showSuccessToast(successMessage); + } + + if (error || (errors && errors.length)) { + const hasMultipleErrors = + (errors && errors.length > 1) || (error && dataStreams.length > 1); + + const errorMessage = hasMultipleErrors + ? i18n.translate( + 'xpack.idxMgmt.deleteDataStreamsConfirmationModal.multipleErrorsNotificationMessageText', + { + defaultMessage: 'Error deleting {count} data streams', + values: { + count: (errors && errors.length) || dataStreams.length, + }, + } + ) + : i18n.translate( + 'xpack.idxMgmt.deleteDataStreamsConfirmationModal.errorNotificationMessageText', + { + defaultMessage: "Error deleting data stream '{name}'", + values: { name: (errors && errors[0].name) || dataStreams[0] }, + } + ); + + notificationService.showDangerToast(errorMessage); + } + }); + }; + + return ( + + + } + onCancel={() => onClose()} + onConfirm={handleDeleteDataStreams} + cancelButtonText={ + + } + confirmButtonText={ + + } + > + + + } + color="danger" + iconType="alert" + > +

+ +

+
+ + + +

+ +

+ +
    + {dataStreams.map((name) => ( +
  • {name}
  • + ))} +
+
+
+
+ ); +}; diff --git a/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/delete_data_stream_confirmation_modal/index.ts b/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/delete_data_stream_confirmation_modal/index.ts new file mode 100644 index 00000000000000..eaa4a8fc2de02b --- /dev/null +++ b/x-pack/plugins/index_management/public/application/sections/home/data_stream_list/delete_data_stream_confirmation_modal/index.ts @@ -0,0 +1,7 @@ +/* + * 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 { DeleteDataStreamConfirmationModal } from './delete_data_stream_confirmation_modal'; diff --git a/x-pack/plugins/index_management/public/application/services/api.ts b/x-pack/plugins/index_management/public/application/services/api.ts index 5ad84395d24c2b..d7874ec2dcf325 100644 --- a/x-pack/plugins/index_management/public/application/services/api.ts +++ b/x-pack/plugins/index_management/public/application/services/api.ts @@ -53,14 +53,21 @@ export function useLoadDataStreams() { }); } -// TODO: Implement this API endpoint once we have content to surface in the detail panel. export function useLoadDataStream(name: string) { - return useRequest({ - path: `${API_BASE_PATH}/data_stream/${encodeURIComponent(name)}`, + return useRequest({ + path: `${API_BASE_PATH}/data_streams/${encodeURIComponent(name)}`, method: 'get', }); } +export async function deleteDataStreams(dataStreams: string[]) { + return sendRequest({ + path: `${API_BASE_PATH}/delete_data_streams`, + method: 'post', + body: { dataStreams }, + }); +} + export async function loadIndices() { const response = await httpService.httpClient.get(`${API_BASE_PATH}/indices`); return response.data ? response.data : response; diff --git a/x-pack/plugins/index_management/public/plugin.ts b/x-pack/plugins/index_management/public/plugin.ts index 94d9bccdc63caa..aec25ee3247d69 100644 --- a/x-pack/plugins/index_management/public/plugin.ts +++ b/x-pack/plugins/index_management/public/plugin.ts @@ -8,6 +8,8 @@ import { i18n } from '@kbn/i18n'; import { CoreSetup } from '../../../../src/core/public'; import { UsageCollectionSetup } from '../../../../src/plugins/usage_collection/public'; import { ManagementSetup, ManagementSectionId } from '../../../../src/plugins/management/public'; + +import { IngestManagerSetup } from '../../ingest_manager/public'; import { UIM_APP_NAME, PLUGIN } from '../common/constants'; import { httpService } from './application/services/http'; @@ -25,6 +27,7 @@ export interface IndexManagementPluginSetup { } interface PluginsDependencies { + ingestManager?: IngestManagerSetup; usageCollection: UsageCollectionSetup; management: ManagementSetup; } @@ -42,7 +45,7 @@ export class IndexMgmtUIPlugin { public setup(coreSetup: CoreSetup, plugins: PluginsDependencies): IndexManagementPluginSetup { const { http, notifications } = coreSetup; - const { usageCollection, management } = plugins; + const { ingestManager, usageCollection, management } = plugins; httpService.setup(http); notificationService.setup(notifications); @@ -60,7 +63,7 @@ export class IndexMgmtUIPlugin { uiMetricService: this.uiMetricService, extensionsService: this.extensionsService, }; - return mountManagementSection(coreSetup, usageCollection, services, params); + return mountManagementSection(coreSetup, usageCollection, services, params, ingestManager); }, }); diff --git a/x-pack/plugins/index_management/server/client/elasticsearch.ts b/x-pack/plugins/index_management/server/client/elasticsearch.ts index 6b1bf47512b211..6c0fbe3dd6a652 100644 --- a/x-pack/plugins/index_management/server/client/elasticsearch.ts +++ b/x-pack/plugins/index_management/server/client/elasticsearch.ts @@ -20,6 +20,20 @@ export const elasticsearchJsPlugin = (Client: any, config: any, components: any) method: 'GET', }); + dataManagement.getDataStream = ca({ + urls: [ + { + fmt: '/_data_stream/<%=name%>', + req: { + name: { + type: 'string', + }, + }, + }, + ], + method: 'GET', + }); + // We don't allow the user to create a data stream in the UI or API. We're just adding this here // to enable the API integration tests. dataManagement.createDataStream = ca({ diff --git a/x-pack/plugins/index_management/server/routes/api/data_streams/index.ts b/x-pack/plugins/index_management/server/routes/api/data_streams/index.ts index 56c514e30f2427..4aaf2b1bc5ed56 100644 --- a/x-pack/plugins/index_management/server/routes/api/data_streams/index.ts +++ b/x-pack/plugins/index_management/server/routes/api/data_streams/index.ts @@ -6,8 +6,11 @@ import { RouteDependencies } from '../../../types'; -import { registerGetAllRoute } from './register_get_route'; +import { registerGetOneRoute, registerGetAllRoute } from './register_get_route'; +import { registerDeleteRoute } from './register_delete_route'; export function registerDataStreamRoutes(dependencies: RouteDependencies) { + registerGetOneRoute(dependencies); registerGetAllRoute(dependencies); + registerDeleteRoute(dependencies); } diff --git a/x-pack/plugins/index_management/server/routes/api/data_streams/register_delete_route.ts b/x-pack/plugins/index_management/server/routes/api/data_streams/register_delete_route.ts new file mode 100644 index 00000000000000..45b185bcd053b5 --- /dev/null +++ b/x-pack/plugins/index_management/server/routes/api/data_streams/register_delete_route.ts @@ -0,0 +1,52 @@ +/* + * 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 { schema, TypeOf } from '@kbn/config-schema'; + +import { RouteDependencies } from '../../../types'; +import { addBasePath } from '../index'; +import { wrapEsError } from '../../helpers'; + +const bodySchema = schema.object({ + dataStreams: schema.arrayOf(schema.string()), +}); + +export function registerDeleteRoute({ router, license }: RouteDependencies) { + router.post( + { + path: addBasePath('/delete_data_streams'), + validate: { body: bodySchema }, + }, + license.guardApiRoute(async (ctx, req, res) => { + const { callAsCurrentUser } = ctx.dataManagement!.client; + const { dataStreams } = req.body as TypeOf; + + const response: { dataStreamsDeleted: string[]; errors: any[] } = { + dataStreamsDeleted: [], + errors: [], + }; + + await Promise.all( + dataStreams.map(async (name: string) => { + try { + await callAsCurrentUser('dataManagement.deleteDataStream', { + name, + }); + + return response.dataStreamsDeleted.push(name); + } catch (e) { + return response.errors.push({ + name, + error: wrapEsError(e), + }); + } + }) + ); + + return res.ok({ body: response }); + }) + ); +} diff --git a/x-pack/plugins/index_management/server/routes/api/data_streams/register_get_route.ts b/x-pack/plugins/index_management/server/routes/api/data_streams/register_get_route.ts index 9128556130bf45..5f4e625348333d 100644 --- a/x-pack/plugins/index_management/server/routes/api/data_streams/register_get_route.ts +++ b/x-pack/plugins/index_management/server/routes/api/data_streams/register_get_route.ts @@ -4,7 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { deserializeDataStreamList } from '../../../../common/lib'; +import { schema, TypeOf } from '@kbn/config-schema'; + +import { deserializeDataStream, deserializeDataStreamList } from '../../../../common/lib'; import { RouteDependencies } from '../../../types'; import { addBasePath } from '../index'; @@ -32,3 +34,40 @@ export function registerGetAllRoute({ router, license, lib: { isEsError } }: Rou }) ); } + +export function registerGetOneRoute({ router, license, lib: { isEsError } }: RouteDependencies) { + const paramsSchema = schema.object({ + name: schema.string(), + }); + + router.get( + { + path: addBasePath('/data_streams/{name}'), + validate: { params: paramsSchema }, + }, + license.guardApiRoute(async (ctx, req, res) => { + const { name } = req.params as TypeOf; + const { callAsCurrentUser } = ctx.dataManagement!.client; + + try { + const dataStream = await callAsCurrentUser('dataManagement.getDataStream', { name }); + + if (dataStream[0]) { + const body = deserializeDataStream(dataStream[0]); + return res.ok({ body }); + } + + return res.notFound(); + } catch (e) { + if (isEsError(e)) { + return res.customError({ + statusCode: e.statusCode, + body: e, + }); + } + // Case: default + return res.internalError({ body: e }); + } + }) + ); +} diff --git a/x-pack/plugins/infra/common/alerting/metrics/types.ts b/x-pack/plugins/infra/common/alerting/metrics/types.ts index a6184080cb7746..0c1e5090def914 100644 --- a/x-pack/plugins/infra/common/alerting/metrics/types.ts +++ b/x-pack/plugins/infra/common/alerting/metrics/types.ts @@ -5,6 +5,7 @@ */ import * as rt from 'io-ts'; +import { ItemTypeRT } from '../../inventory_models/types'; // TODO: Have threshold and inventory alerts import these types from this file instead of from their // local directories @@ -39,7 +40,16 @@ const baseAlertRequestParamsRT = rt.intersection([ sourceId: rt.string, }), rt.type({ - lookback: rt.union([rt.literal('h'), rt.literal('d'), rt.literal('w'), rt.literal('M')]), + lookback: rt.union([ + rt.literal('ms'), + rt.literal('s'), + rt.literal('m'), + rt.literal('h'), + rt.literal('d'), + rt.literal('w'), + rt.literal('M'), + rt.literal('y'), + ]), criteria: rt.array(rt.any), alertInterval: rt.string, }), @@ -61,10 +71,13 @@ export type MetricThresholdAlertPreviewRequestParams = rt.TypeOf< const inventoryAlertPreviewRequestParamsRT = rt.intersection([ baseAlertRequestParamsRT, rt.type({ - nodeType: rt.string, + nodeType: ItemTypeRT, alertType: rt.literal(METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID), }), ]); +export type InventoryAlertPreviewRequestParams = rt.TypeOf< + typeof inventoryAlertPreviewRequestParamsRT +>; export const alertPreviewRequestParamsRT = rt.union([ metricThresholdAlertPreviewRequestParamsRT, @@ -80,3 +93,6 @@ export const alertPreviewSuccessResponsePayloadRT = rt.type({ tooManyBuckets: rt.number, }), }); +export type AlertPreviewSuccessResponsePayload = rt.TypeOf< + typeof alertPreviewSuccessResponsePayloadRT +>; diff --git a/x-pack/plugins/infra/public/alerting/common/get_alert_preview.ts b/x-pack/plugins/infra/public/alerting/common/get_alert_preview.ts new file mode 100644 index 00000000000000..0db1cd57e093f1 --- /dev/null +++ b/x-pack/plugins/infra/public/alerting/common/get_alert_preview.ts @@ -0,0 +1,51 @@ +/* + * 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 * as rt from 'io-ts'; +import { HttpSetup } from 'src/core/public'; +import { + INFRA_ALERT_PREVIEW_PATH, + METRIC_THRESHOLD_ALERT_TYPE_ID, + METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID, + alertPreviewRequestParamsRT, + alertPreviewSuccessResponsePayloadRT, +} from '../../../common/alerting/metrics'; + +async function getAlertPreview({ + fetch, + params, + alertType, +}: { + fetch: HttpSetup['fetch']; + params: rt.TypeOf; + alertType: + | typeof METRIC_THRESHOLD_ALERT_TYPE_ID + | typeof METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID; +}): Promise> { + return await fetch(`${INFRA_ALERT_PREVIEW_PATH}`, { + method: 'POST', + body: JSON.stringify({ + ...params, + alertType, + }), + }); +} + +export const getMetricThresholdAlertPreview = ({ + fetch, + params, +}: { + fetch: HttpSetup['fetch']; + params: rt.TypeOf; +}) => getAlertPreview({ fetch, params, alertType: METRIC_THRESHOLD_ALERT_TYPE_ID }); + +export const getInventoryAlertPreview = ({ + fetch, + params, +}: { + fetch: HttpSetup['fetch']; + params: rt.TypeOf; +}) => getAlertPreview({ fetch, params, alertType: METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID }); diff --git a/x-pack/plugins/infra/public/alerting/common/index.ts b/x-pack/plugins/infra/public/alerting/common/index.ts new file mode 100644 index 00000000000000..33f9c856e71666 --- /dev/null +++ b/x-pack/plugins/infra/public/alerting/common/index.ts @@ -0,0 +1,55 @@ +/* + * 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 { i18n } from '@kbn/i18n'; + +export * from './get_alert_preview'; + +export const previewOptions = [ + { + value: 'h', + text: i18n.translate('xpack.infra.metrics.alertFlyout.lastHourLabel', { + defaultMessage: 'Last hour', + }), + shortText: i18n.translate('xpack.infra.metrics.alertFlyout.hourLabel', { + defaultMessage: 'hour', + }), + }, + { + value: 'd', + text: i18n.translate('xpack.infra.metrics.alertFlyout.lastDayLabel', { + defaultMessage: 'Last day', + }), + shortText: i18n.translate('xpack.infra.metrics.alertFlyout.dayLabel', { + defaultMessage: 'day', + }), + }, + { + value: 'w', + text: i18n.translate('xpack.infra.metrics.alertFlyout.lastWeekLabel', { + defaultMessage: 'Last week', + }), + shortText: i18n.translate('xpack.infra.metrics.alertFlyout.weekLabel', { + defaultMessage: 'week', + }), + }, + { + value: 'M', + text: i18n.translate('xpack.infra.metrics.alertFlyout.lastMonthLabel', { + defaultMessage: 'Last month', + }), + shortText: i18n.translate('xpack.infra.metrics.alertFlyout.monthLabel', { + defaultMessage: 'month', + }), + }, +]; + +export const firedTimeLabel = i18n.translate('xpack.infra.metrics.alertFlyout.firedTime', { + defaultMessage: 'time', +}); +export const firedTimesLabel = i18n.translate('xpack.infra.metrics.alertFlyout.firedTimes', { + defaultMessage: 'times', +}); diff --git a/x-pack/plugins/infra/public/components/alerting/inventory/alert_dropdown.tsx b/x-pack/plugins/infra/public/alerting/inventory/components/alert_dropdown.tsx similarity index 100% rename from x-pack/plugins/infra/public/components/alerting/inventory/alert_dropdown.tsx rename to x-pack/plugins/infra/public/alerting/inventory/components/alert_dropdown.tsx diff --git a/x-pack/plugins/infra/public/components/alerting/inventory/alert_flyout.tsx b/x-pack/plugins/infra/public/alerting/inventory/components/alert_flyout.tsx similarity index 100% rename from x-pack/plugins/infra/public/components/alerting/inventory/alert_flyout.tsx rename to x-pack/plugins/infra/public/alerting/inventory/components/alert_flyout.tsx diff --git a/x-pack/plugins/infra/public/components/alerting/inventory/expression.tsx b/x-pack/plugins/infra/public/alerting/inventory/components/expression.tsx similarity index 73% rename from x-pack/plugins/infra/public/components/alerting/inventory/expression.tsx rename to x-pack/plugins/infra/public/alerting/inventory/components/expression.tsx index ce14897991e60b..ef73d6ff96e412 100644 --- a/x-pack/plugins/infra/public/components/alerting/inventory/expression.tsx +++ b/x-pack/plugins/infra/public/alerting/inventory/components/expression.tsx @@ -4,7 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -import { debounce } from 'lodash'; +import { debounce, pick } from 'lodash'; +import { Unit } from '@elastic/datemath'; import React, { useCallback, useMemo, useEffect, useState, ChangeEvent } from 'react'; import { EuiFlexGroup, @@ -15,9 +16,20 @@ import { EuiFormRow, EuiButtonEmpty, EuiFieldSearch, + EuiSelect, + EuiButton, } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; import { i18n } from '@kbn/i18n'; +import { + previewOptions, + firedTimeLabel, + firedTimesLabel, + getInventoryAlertPreview as getAlertPreview, +} from '../../../alerting/common'; +import { AlertPreviewSuccessResponsePayload } from '../../../../common/alerting/metrics/types'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths +import { getIntervalInSeconds } from '../../../../server/utils/get_interval_in_seconds'; import { Comparator, // eslint-disable-next-line @kbn/eslint/no-restricted-paths @@ -52,6 +64,8 @@ import { NodeTypeExpression } from './node_type'; import { InfraWaffleMapOptions } from '../../../lib/lib'; import { convertKueryToElasticSearchQuery } from '../../../utils/kuery'; +import { validateMetricThreshold } from './validation'; + const FILTER_TYPING_DEBOUNCE_MS = 500; interface AlertContextMeta { @@ -65,18 +79,16 @@ interface Props { alertParams: { criteria: InventoryMetricConditions[]; nodeType: InventoryItemType; - groupBy?: string; filterQuery?: string; filterQueryText?: string; sourceId?: string; }; + alertInterval: string; alertsContext: AlertsContextValue; setAlertParams(key: string, value: any): void; setAlertProperty(key: string, value: any): void; } -type TimeUnit = 's' | 'm' | 'h' | 'd'; - const defaultExpression = { metric: 'cpu' as SnapshotMetricType, comparator: Comparator.GT, @@ -86,7 +98,7 @@ const defaultExpression = { } as InventoryMetricConditions; export const Expressions: React.FC = (props) => { - const { setAlertParams, alertParams, errors, alertsContext } = props; + const { setAlertParams, alertParams, errors, alertsContext, alertInterval } = props; const { source, createDerivedIndexPattern } = useSourceViaHttp({ sourceId: 'default', type: 'metrics', @@ -94,7 +106,32 @@ export const Expressions: React.FC = (props) => { toastWarning: alertsContext.toastNotifications.addWarning, }); const [timeSize, setTimeSize] = useState(1); - const [timeUnit, setTimeUnit] = useState('m'); + const [timeUnit, setTimeUnit] = useState('m'); + + const [previewLookbackInterval, setPreviewLookbackInterval] = useState('h'); + const [isPreviewLoading, setIsPreviewLoading] = useState(false); + const [previewError, setPreviewError] = useState(false); + const [previewResult, setPreviewResult] = useState( + null + ); + + const previewIntervalError = useMemo(() => { + const intervalInSeconds = getIntervalInSeconds(alertInterval); + const lookbackInSeconds = getIntervalInSeconds(`1${previewLookbackInterval}`); + if (intervalInSeconds >= lookbackInSeconds) { + return true; + } + return false; + }, [previewLookbackInterval, alertInterval]); + + const isPreviewDisabled = useMemo(() => { + if (previewIntervalError) return true; + const validationResult = validateMetricThreshold({ criteria: alertParams.criteria } as any); + const hasValidationErrors = Object.values(validationResult.errors).some((result) => + Object.values(result).some((arr) => Array.isArray(arr) && arr.length) + ); + return hasValidationErrors; + }, [alertParams.criteria, previewIntervalError]); const derivedIndexPattern = useMemo(() => createDerivedIndexPattern('metrics'), [ createDerivedIndexPattern, @@ -173,7 +210,7 @@ export const Expressions: React.FC = (props) => { ...c, timeUnit: tu, })); - setTimeUnit(tu as TimeUnit); + setTimeUnit(tu as Unit); setAlertParams('criteria', criteria); }, [alertParams.criteria, setAlertParams] @@ -216,6 +253,33 @@ export const Expressions: React.FC = (props) => { } }, [alertsContext.metadata, derivedIndexPattern, setAlertParams]); + const onSelectPreviewLookbackInterval = useCallback((e) => { + setPreviewLookbackInterval(e.target.value); + setPreviewResult(null); + }, []); + + const onClickPreview = useCallback(async () => { + setIsPreviewLoading(true); + setPreviewResult(null); + setPreviewError(false); + try { + const result = await getAlertPreview({ + fetch: alertsContext.http.fetch, + params: { + ...pick(alertParams, 'criteria', 'nodeType'), + sourceId: alertParams.sourceId, + lookback: previewLookbackInterval as Unit, + alertInterval, + }, + }); + setPreviewResult(result); + } catch (e) { + setPreviewError(true); + } finally { + setIsPreviewLoading(false); + } + }, [alertParams, alertInterval, alertsContext, previewLookbackInterval]); + useEffect(() => { const md = alertsContext.metadata; if (!alertParams.nodeType) { @@ -332,6 +396,91 @@ export const Expressions: React.FC = (props) => { + + <> + + + + + + + {i18n.translate('xpack.infra.metrics.alertFlyout.testAlertTrigger', { + defaultMessage: 'Test alert trigger', + })} + + + + + {previewResult && ( + <> + + + {previewResult.resultTotals.fired}, + lookback: previewOptions.find((e) => e.value === previewLookbackInterval) + ?.shortText, + }} + />{' '} + {previewResult.numberOfGroups}, + groupName: alertParams.nodeType, + plural: previewResult.numberOfGroups !== 1 ? 's' : '', + }} + /> + + + )} + {previewIntervalError && ( + <> + + + check every, + }} + /> + + + )} + {previewError && ( + <> + + + + + + )} + + + ); }; diff --git a/x-pack/plugins/infra/public/components/alerting/inventory/metric.tsx b/x-pack/plugins/infra/public/alerting/inventory/components/metric.tsx similarity index 100% rename from x-pack/plugins/infra/public/components/alerting/inventory/metric.tsx rename to x-pack/plugins/infra/public/alerting/inventory/components/metric.tsx diff --git a/x-pack/plugins/infra/public/components/alerting/inventory/node_type.tsx b/x-pack/plugins/infra/public/alerting/inventory/components/node_type.tsx similarity index 100% rename from x-pack/plugins/infra/public/components/alerting/inventory/node_type.tsx rename to x-pack/plugins/infra/public/alerting/inventory/components/node_type.tsx diff --git a/x-pack/plugins/infra/public/components/alerting/inventory/validation.tsx b/x-pack/plugins/infra/public/alerting/inventory/components/validation.tsx similarity index 100% rename from x-pack/plugins/infra/public/components/alerting/inventory/validation.tsx rename to x-pack/plugins/infra/public/alerting/inventory/components/validation.tsx diff --git a/x-pack/plugins/infra/public/components/alerting/inventory/metric_inventory_threshold_alert_type.ts b/x-pack/plugins/infra/public/alerting/inventory/index.ts similarity index 73% rename from x-pack/plugins/infra/public/components/alerting/inventory/metric_inventory_threshold_alert_type.ts rename to x-pack/plugins/infra/public/alerting/inventory/index.ts index 0cb564ec2194e2..7503e5673fcd96 100644 --- a/x-pack/plugins/infra/public/components/alerting/inventory/metric_inventory_threshold_alert_type.ts +++ b/x-pack/plugins/infra/public/alerting/inventory/index.ts @@ -6,19 +6,20 @@ 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 { validateMetricThreshold } from './validation'; +import { METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID } from '../../../server/lib/alerting/inventory_metric_threshold/types'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths +import { AlertTypeModel } from '../../../../triggers_actions_ui/public/types'; +import { validateMetricThreshold } from './components/validation'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID } from '../../../../server/lib/alerting/inventory_metric_threshold/types'; -export function getInventoryMetricAlertType(): AlertTypeModel { +export function createInventoryMetricAlertType(): AlertTypeModel { return { id: METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID, name: i18n.translate('xpack.infra.metrics.inventory.alertFlyout.alertName', { defaultMessage: 'Inventory', }), iconClass: 'bell', - alertParamsExpression: React.lazy(() => import('./expression')), + alertParamsExpression: React.lazy(() => import('./components/expression')), validate: validateMetricThreshold, defaultActionMessage: i18n.translate( 'xpack.infra.metrics.alerting.inventory.threshold.defaultActionMessage', diff --git a/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.tsx b/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.tsx index febf849ccc9438..3c3351f4ddd76d 100644 --- a/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.tsx +++ b/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.tsx @@ -5,8 +5,8 @@ */ import { debounce, pick } from 'lodash'; +import { Unit } from '@elastic/datemath'; import * as rt from 'io-ts'; -import { HttpSetup } from 'src/core/public'; import React, { ChangeEvent, useCallback, useMemo, useEffect, useState } from 'react'; import { EuiSpacer, @@ -24,15 +24,18 @@ import { } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; import { i18n } from '@kbn/i18n'; +import { + previewOptions, + firedTimeLabel, + firedTimesLabel, + getMetricThresholdAlertPreview as getAlertPreview, +} from '../../common'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { getIntervalInSeconds } from '../../../../server/utils/get_interval_in_seconds'; import { Comparator, Aggregators, - INFRA_ALERT_PREVIEW_PATH, - alertPreviewRequestParamsRT, alertPreviewSuccessResponsePayloadRT, - METRIC_THRESHOLD_ALERT_TYPE_ID, } from '../../../../common/alerting/metrics'; import { ForLastExpression, @@ -79,22 +82,6 @@ const defaultExpression = { timeUnit: 'm', } as MetricExpression; -async function getAlertPreview({ - fetch, - params, -}: { - fetch: HttpSetup['fetch']; - params: rt.TypeOf; -}): Promise> { - return await fetch(`${INFRA_ALERT_PREVIEW_PATH}`, { - method: 'POST', - body: JSON.stringify({ - ...params, - alertType: METRIC_THRESHOLD_ALERT_TYPE_ID, - }), - }); -} - export const Expressions: React.FC = (props) => { const { setAlertParams, alertParams, errors, alertsContext, alertInterval } = props; const { source, createDerivedIndexPattern } = useSourceViaHttp({ @@ -275,7 +262,7 @@ export const Expressions: React.FC = (props) => { params: { ...pick(alertParams, 'criteria', 'groupBy', 'filterQuery'), sourceId: alertParams.sourceId, - lookback: previewLookbackInterval as 'h' | 'd' | 'w' | 'M', + lookback: previewLookbackInterval as Unit, alertInterval, }, }); @@ -319,11 +306,12 @@ export const Expressions: React.FC = (props) => { }, [previewLookbackInterval, alertInterval]); const isPreviewDisabled = useMemo(() => { + if (previewIntervalError) return true; const validationResult = validateMetricThreshold({ criteria: alertParams.criteria } as any); const hasValidationErrors = Object.values(validationResult.errors).some((result) => Object.values(result).some((arr) => Array.isArray(arr) && arr.length) ); - return hasValidationErrors || previewIntervalError; + return hasValidationErrors; }, [alertParams.criteria, previewIntervalError]); return ( @@ -600,52 +588,6 @@ export const Expressions: React.FC = (props) => { ); }; -const previewOptions = [ - { - value: 'h', - text: i18n.translate('xpack.infra.metrics.alertFlyout.lastHourLabel', { - defaultMessage: 'Last hour', - }), - shortText: i18n.translate('xpack.infra.metrics.alertFlyout.hourLabel', { - defaultMessage: 'hour', - }), - }, - { - value: 'd', - text: i18n.translate('xpack.infra.metrics.alertFlyout.lastDayLabel', { - defaultMessage: 'Last day', - }), - shortText: i18n.translate('xpack.infra.metrics.alertFlyout.dayLabel', { - defaultMessage: 'day', - }), - }, - { - value: 'w', - text: i18n.translate('xpack.infra.metrics.alertFlyout.lastWeekLabel', { - defaultMessage: 'Last week', - }), - shortText: i18n.translate('xpack.infra.metrics.alertFlyout.weekLabel', { - defaultMessage: 'week', - }), - }, - { - value: 'M', - text: i18n.translate('xpack.infra.metrics.alertFlyout.lastMonthLabel', { - defaultMessage: 'Last month', - }), - shortText: i18n.translate('xpack.infra.metrics.alertFlyout.monthLabel', { - defaultMessage: 'month', - }), - }, -]; - -const firedTimeLabel = i18n.translate('xpack.infra.metrics.alertFlyout.firedTime', { - defaultMessage: 'time', -}); -const firedTimesLabel = i18n.translate('xpack.infra.metrics.alertFlyout.firedTimes', { - defaultMessage: 'times', -}); - // required for dynamic import // eslint-disable-next-line import/no-default-export export default Expressions; diff --git a/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_chart.test.tsx b/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_chart.test.tsx index 0b43883728a6f7..1ca7f7bff83ede 100644 --- a/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_chart.test.tsx +++ b/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_chart.test.tsx @@ -27,6 +27,7 @@ describe('ExpressionChart', () => { groupBy?: string ) { const mocks = coreMock.createSetup(); + const startMocks = coreMock.createStart(); const [ { application: { capabilities }, @@ -38,7 +39,7 @@ describe('ExpressionChart', () => { toastNotifications: mocks.notifications.toasts, actionTypeRegistry: actionTypeRegistryMock.create() as any, alertTypeRegistry: alertTypeRegistryMock.create() as any, - docLinks: mocks.docLinks, + docLinks: startMocks.docLinks, capabilities: { ...capabilities, actions: { diff --git a/x-pack/plugins/infra/public/pages/metrics/index.tsx b/x-pack/plugins/infra/public/pages/metrics/index.tsx index 05296fbf6b0a32..ab7f41e3066b8c 100644 --- a/x-pack/plugins/infra/public/pages/metrics/index.tsx +++ b/x-pack/plugins/infra/public/pages/metrics/index.tsx @@ -29,7 +29,7 @@ import { WaffleOptionsProvider } from './inventory_view/hooks/use_waffle_options import { WaffleTimeProvider } from './inventory_view/hooks/use_waffle_time'; import { WaffleFiltersProvider } from './inventory_view/hooks/use_waffle_filters'; -import { InventoryAlertDropdown } from '../../components/alerting/inventory/alert_dropdown'; +import { InventoryAlertDropdown } from '../../alerting/inventory/components/alert_dropdown'; import { MetricsAlertDropdown } from '../../alerting/metric_threshold/components/alert_dropdown'; const ADD_DATA_LABEL = i18n.translate('xpack.infra.metricsHeaderAddDataButtonLabel', { diff --git a/x-pack/plugins/infra/public/pages/metrics/inventory_view/components/waffle/node_context_menu.tsx b/x-pack/plugins/infra/public/pages/metrics/inventory_view/components/waffle/node_context_menu.tsx index 3441b6bf2c1b9e..d9132615213836 100644 --- a/x-pack/plugins/infra/public/pages/metrics/inventory_view/components/waffle/node_context_menu.tsx +++ b/x-pack/plugins/infra/public/pages/metrics/inventory_view/components/waffle/node_context_menu.tsx @@ -9,7 +9,7 @@ import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; import React, { useMemo, useState } from 'react'; -import { AlertFlyout } from '../../../../../components/alerting/inventory/alert_flyout'; +import { AlertFlyout } from '../../../../../alerting/inventory/components/alert_flyout'; import { InfraWaffleMapNode, InfraWaffleMapOptions } from '../../../../../lib/lib'; import { getNodeDetailUrl, getNodeLogsUrl } from '../../../../link_to'; import { createUptimeLink } from '../../lib/create_uptime_link'; diff --git a/x-pack/plugins/infra/public/plugin.ts b/x-pack/plugins/infra/public/plugin.ts index b3765db43335a3..496e788efc060f 100644 --- a/x-pack/plugins/infra/public/plugin.ts +++ b/x-pack/plugins/infra/public/plugin.ts @@ -13,7 +13,7 @@ import { } from 'kibana/public'; import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/public'; import { createMetricThresholdAlertType } from './alerting/metric_threshold'; -import { getInventoryMetricAlertType } from './components/alerting/inventory/metric_inventory_threshold_alert_type'; +import { createInventoryMetricAlertType } from './alerting/inventory'; import { getAlertType as getLogsAlertType } from './components/alerting/logs/log_threshold_alert_type'; import { registerStartSingleton } from './legacy_singletons'; import { registerFeatures } from './register_feature'; @@ -29,7 +29,7 @@ export class Plugin setup(core: CoreSetup, pluginsSetup: ClientPluginsSetup) { registerFeatures(pluginsSetup.home); - pluginsSetup.triggers_actions_ui.alertTypeRegistry.register(getInventoryMetricAlertType()); + pluginsSetup.triggers_actions_ui.alertTypeRegistry.register(createInventoryMetricAlertType()); pluginsSetup.triggers_actions_ui.alertTypeRegistry.register(getLogsAlertType()); pluginsSetup.triggers_actions_ui.alertTypeRegistry.register(createMetricThresholdAlertType()); diff --git a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts new file mode 100644 index 00000000000000..c55f50e229b698 --- /dev/null +++ b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts @@ -0,0 +1,136 @@ +/* + * 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 { mapValues, last } from 'lodash'; +import moment from 'moment'; +import { + InfraDatabaseSearchResponse, + CallWithRequestParams, +} from '../../adapters/framework/adapter_types'; +import { Comparator, InventoryMetricConditions } from './types'; +import { AlertServices } from '../../../../../alerts/server'; +import { InfraSnapshot } from '../../snapshot'; +import { parseFilterQuery } from '../../../utils/serialized_query'; +import { InventoryItemType, SnapshotMetricType } from '../../../../common/inventory_models/types'; +import { InfraTimerangeInput } from '../../../../common/http_api/snapshot_api'; +import { InfraSourceConfiguration } from '../../sources'; + +interface ConditionResult { + shouldFire: boolean | boolean[]; + currentValue?: number | null; + metric: string; + isNoData: boolean; + isError: boolean; +} + +export const evaluateCondition = async ( + condition: InventoryMetricConditions, + nodeType: InventoryItemType, + sourceConfiguration: InfraSourceConfiguration, + callCluster: AlertServices['callCluster'], + filterQuery?: string, + lookbackSize?: number +): Promise> => { + const { comparator, metric } = condition; + let { threshold } = condition; + + const timerange = { + to: Date.now(), + from: moment().subtract(condition.timeSize, condition.timeUnit).toDate().getTime(), + interval: condition.timeUnit, + } as InfraTimerangeInput; + if (lookbackSize) { + timerange.lookbackSize = lookbackSize; + } + + const currentValues = await getData( + callCluster, + nodeType, + metric, + timerange, + sourceConfiguration, + filterQuery + ); + + threshold = threshold.map((n) => convertMetricValue(metric, n)); + + const comparisonFunction = comparatorMap[comparator]; + + return mapValues(currentValues, (value) => ({ + shouldFire: + value !== undefined && + value !== null && + (Array.isArray(value) + ? value.map((v) => comparisonFunction(Number(v), threshold)) + : comparisonFunction(value, threshold)), + metric, + isNoData: value === null, + isError: value === undefined, + ...(!Array.isArray(value) ? { currentValue: value } : {}), + })); +}; + +const getData = async ( + callCluster: AlertServices['callCluster'], + nodeType: InventoryItemType, + metric: SnapshotMetricType, + timerange: InfraTimerangeInput, + sourceConfiguration: InfraSourceConfiguration, + filterQuery?: string +) => { + const snapshot = new InfraSnapshot(); + const esClient = ( + options: CallWithRequestParams + ): Promise> => callCluster('search', options); + + const options = { + filterQuery: parseFilterQuery(filterQuery), + nodeType, + groupBy: [], + sourceConfiguration, + metric: { type: metric }, + timerange, + includeTimeseries: Boolean(timerange.lookbackSize), + }; + + const { nodes } = await snapshot.getNodes(esClient, options); + + return nodes.reduce((acc, n) => { + const nodePathItem = last(n.path); + if (n.metric?.value && n.metric?.timeseries) { + const { timeseries } = n.metric; + const values = timeseries.rows.map((row) => row.metric_0) as Array; + acc[nodePathItem.label] = values; + } else { + acc[nodePathItem.label] = n.metric && n.metric.value; + } + return acc; + }, {} as Record | undefined | null>); +}; + +const comparatorMap = { + [Comparator.BETWEEN]: (value: number, [a, b]: number[]) => + value >= Math.min(a, b) && value <= Math.max(a, b), + // `threshold` is always an array of numbers in case the BETWEEN comparator is + // used; all other compartors will just destructure the first value in the array + [Comparator.GT]: (a: number, [b]: number[]) => a > b, + [Comparator.LT]: (a: number, [b]: number[]) => a < b, + [Comparator.OUTSIDE_RANGE]: (value: number, [a, b]: number[]) => value < a || value > b, + [Comparator.GT_OR_EQ]: (a: number, [b]: number[]) => a >= b, + [Comparator.LT_OR_EQ]: (a: number, [b]: number[]) => a <= b, +}; + +// Some metrics in the UI are in a different unit that what we store in ES. +const convertMetricValue = (metric: SnapshotMetricType, value: number) => { + if (converters[metric]) { + return converters[metric](value); + } else { + return value; + } +}; +const converters: Record number> = { + cpu: (n) => Number(n) / 100, + memory: (n) => Number(n) / 100, +}; diff --git a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/inventory_metric_threshold_executor.ts b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/inventory_metric_threshold_executor.ts index 5a34a6665e781d..99e653b2d67894 100644 --- a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/inventory_metric_threshold_executor.ts +++ b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/inventory_metric_threshold_executor.ts @@ -3,27 +3,18 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { mapValues, last, get } from 'lodash'; +import { first, get } from 'lodash'; import { i18n } from '@kbn/i18n'; -import moment from 'moment'; -import { - InfraDatabaseSearchResponse, - CallWithRequestParams, -} from '../../adapters/framework/adapter_types'; -import { Comparator, AlertStates, InventoryMetricConditions } from './types'; -import { AlertServices, AlertExecutorOptions } from '../../../../../alerts/server'; -import { InfraSnapshot } from '../../snapshot'; -import { parseFilterQuery } from '../../../utils/serialized_query'; +import { AlertStates, InventoryMetricConditions } from './types'; +import { AlertExecutorOptions } from '../../../../../alerts/server'; import { InventoryItemType, SnapshotMetricType } from '../../../../common/inventory_models/types'; -import { InfraTimerangeInput } from '../../../../common/http_api/snapshot_api'; -import { InfraSourceConfiguration } from '../../sources'; import { InfraBackendLibs } from '../../infra_types'; import { METRIC_FORMATTERS } from '../../../../common/formatters/snapshot_metric_formats'; import { createFormatter } from '../../../../common/formatters'; +import { evaluateCondition } from './evaluate_condition'; interface InventoryMetricThresholdParams { criteria: InventoryMetricConditions[]; - groupBy: string | undefined; filterQuery: string | undefined; nodeType: InventoryItemType; sourceId?: string; @@ -41,11 +32,13 @@ export const createInventoryMetricThresholdExecutor = ( ); const results = await Promise.all( - criteria.map((c) => evaluateCondtion(c, nodeType, source.configuration, services, filterQuery)) + criteria.map((c) => + evaluateCondition(c, nodeType, source.configuration, services.callCluster, filterQuery) + ) ); - const invenotryItems = Object.keys(results[0]); - for (const item of invenotryItems) { + const inventoryItems = Object.keys(first(results)); + for (const item of inventoryItems) { const alertInstance = services.alertInstanceFactory(`${alertId}-${item}`); // AND logic; all criteria must be across the threshold const shouldAlertFire = results.every((result) => result[item].shouldFire); @@ -79,93 +72,6 @@ export const createInventoryMetricThresholdExecutor = ( } }; -interface ConditionResult { - shouldFire: boolean; - currentValue?: number | null; - isNoData: boolean; - isError: boolean; -} - -const evaluateCondtion = async ( - condition: InventoryMetricConditions, - nodeType: InventoryItemType, - sourceConfiguration: InfraSourceConfiguration, - services: AlertServices, - filterQuery?: string -): Promise> => { - const { comparator, metric } = condition; - let { threshold } = condition; - - const currentValues = await getData( - services, - nodeType, - metric, - { - to: Date.now(), - from: moment().subtract(condition.timeSize, condition.timeUnit).toDate().getTime(), - interval: condition.timeUnit, - }, - sourceConfiguration, - filterQuery - ); - - threshold = threshold.map((n) => convertMetricValue(metric, n)); - - const comparisonFunction = comparatorMap[comparator]; - - return mapValues(currentValues, (value) => ({ - shouldFire: value !== undefined && value !== null && comparisonFunction(value, threshold), - metric, - currentValue: value, - isNoData: value === null, - isError: value === undefined, - })); -}; - -const getData = async ( - services: AlertServices, - nodeType: InventoryItemType, - metric: SnapshotMetricType, - timerange: InfraTimerangeInput, - sourceConfiguration: InfraSourceConfiguration, - filterQuery?: string -) => { - const snapshot = new InfraSnapshot(); - const esClient = ( - options: CallWithRequestParams - ): Promise> => - services.callCluster('search', options); - - const options = { - filterQuery: parseFilterQuery(filterQuery), - nodeType, - groupBy: [], - sourceConfiguration, - metric: { type: metric }, - timerange, - }; - - const { nodes } = await snapshot.getNodes(esClient, options); - - return nodes.reduce((acc, n) => { - const nodePathItem = last(n.path); - acc[nodePathItem.label] = n.metric && n.metric.value; - return acc; - }, {} as Record); -}; - -const comparatorMap = { - [Comparator.BETWEEN]: (value: number, [a, b]: number[]) => - value >= Math.min(a, b) && value <= Math.max(a, b), - // `threshold` is always an array of numbers in case the BETWEEN comparator is - // used; all other compartors will just destructure the first value in the array - [Comparator.GT]: (a: number, [b]: number[]) => a > b, - [Comparator.LT]: (a: number, [b]: number[]) => a < b, - [Comparator.OUTSIDE_RANGE]: (value: number, [a, b]: number[]) => value < a || value > b, - [Comparator.GT_OR_EQ]: (a: number, [b]: number[]) => a >= b, - [Comparator.LT_OR_EQ]: (a: number, [b]: number[]) => a <= b, -}; - const mapToConditionsLookup = ( list: any[], mapFn: (value: any, index: number, array: any[]) => unknown @@ -184,19 +90,6 @@ export const FIRED_ACTIONS = { }), }; -// Some metrics in the UI are in a different unit that what we store in ES. -const convertMetricValue = (metric: SnapshotMetricType, value: number) => { - if (converters[metric]) { - return converters[metric](value); - } else { - return value; - } -}; -const converters: Record number> = { - cpu: (n) => Number(n) / 100, - memory: (n) => Number(n) / 100, -}; - const formatMetric = (metric: SnapshotMetricType, value: number) => { // if (SnapshotCustomMetricInputRT.is(metric)) { // const formatter = createFormatterForMetric(metric); diff --git a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/preview_inventory_metric_threshold_alert.ts b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/preview_inventory_metric_threshold_alert.ts new file mode 100644 index 00000000000000..6e8c624e61c49a --- /dev/null +++ b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/preview_inventory_metric_threshold_alert.ts @@ -0,0 +1,83 @@ +/* + * 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 { Unit } from '@elastic/datemath'; +import { first } from 'lodash'; +import { InventoryMetricConditions } from './types'; +import { IScopedClusterClient } from '../../../../../../../src/core/server'; +import { InfraSource } from '../../../../common/http_api/source_api'; +import { getIntervalInSeconds } from '../../../utils/get_interval_in_seconds'; +import { InventoryItemType } from '../../../../common/inventory_models/types'; +import { evaluateCondition } from './evaluate_condition'; + +interface InventoryMetricThresholdParams { + criteria: InventoryMetricConditions[]; + filterQuery: string | undefined; + nodeType: InventoryItemType; + sourceId?: string; +} + +interface PreviewInventoryMetricThresholdAlertParams { + callCluster: IScopedClusterClient['callAsCurrentUser']; + params: InventoryMetricThresholdParams; + config: InfraSource['configuration']; + lookback: Unit; + alertInterval: string; +} + +export const previewInventoryMetricThresholdAlert = async ({ + callCluster, + params, + config, + lookback, + alertInterval, +}: PreviewInventoryMetricThresholdAlertParams) => { + const { criteria, filterQuery, nodeType } = params as InventoryMetricThresholdParams; + + const { timeSize, timeUnit } = criteria[0]; + const bucketInterval = `${timeSize}${timeUnit}`; + const bucketIntervalInSeconds = getIntervalInSeconds(bucketInterval); + + const lookbackInterval = `1${lookback}`; + const lookbackIntervalInSeconds = getIntervalInSeconds(lookbackInterval); + const lookbackSize = Math.ceil(lookbackIntervalInSeconds / bucketIntervalInSeconds); + + const alertIntervalInSeconds = getIntervalInSeconds(alertInterval); + const alertResultsPerExecution = alertIntervalInSeconds / bucketIntervalInSeconds; + + const results = await Promise.all( + criteria.map((c) => + evaluateCondition(c, nodeType, config, callCluster, filterQuery, lookbackSize) + ) + ); + + const inventoryItems = Object.keys(first(results)); + const previewResults = inventoryItems.map((item) => { + const isNoData = results.some((result) => result[item].isNoData); + if (isNoData) { + return null; + } + const isError = results.some((result) => result[item].isError); + if (isError) { + return undefined; + } + + const numberOfResultBuckets = lookbackSize; + const numberOfExecutionBuckets = Math.floor(numberOfResultBuckets / alertResultsPerExecution); + return [...Array(numberOfExecutionBuckets)].reduce( + (totalFired, _, i) => + totalFired + + (results.every((result) => { + const shouldFire = result[item].shouldFire as boolean[]; + return shouldFire[Math.floor(i * alertResultsPerExecution)]; + }) + ? 1 + : 0), + 0 + ); + }); + + return previewResults; +}; diff --git a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/types.ts b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/types.ts index 73ee1ab6b76159..ec1caad30a4d73 100644 --- a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/types.ts +++ b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/types.ts @@ -3,6 +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 { Unit } from '@elastic/datemath'; import { SnapshotMetricType } from '../../../../common/inventory_models/types'; export const METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID = 'metrics.alert.inventory.threshold'; @@ -23,12 +24,10 @@ export enum AlertStates { ERROR, } -export type TimeUnit = 's' | 'm' | 'h' | 'd'; - export interface InventoryMetricConditions { metric: SnapshotMetricType; timeSize: number; - timeUnit: TimeUnit; + timeUnit: Unit; sourceId?: string; threshold: number[]; comparator: Comparator; diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/preview_metric_threshold_alert.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/preview_metric_threshold_alert.ts index 7aa8367f7678ca..52637d52175a42 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/preview_metric_threshold_alert.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/preview_metric_threshold_alert.ts @@ -5,6 +5,7 @@ */ import { first, zip } from 'lodash'; +import { Unit } from '@elastic/datemath'; import { TOO_MANY_BUCKETS_PREVIEW_EXCEPTION, isTooManyBucketsPreviewException, @@ -25,7 +26,7 @@ interface PreviewMetricThresholdAlertParams { filterQuery: string | undefined; }; config: InfraSource['configuration']; - lookback: 'h' | 'd' | 'w' | 'M'; + lookback: Unit; alertInterval: string; end?: number; overrideLookbackIntervalInSeconds?: number; diff --git a/x-pack/plugins/infra/server/routes/alerting/preview.ts b/x-pack/plugins/infra/server/routes/alerting/preview.ts index f4eed041481f6a..d11425a4f4cb07 100644 --- a/x-pack/plugins/infra/server/routes/alerting/preview.ts +++ b/x-pack/plugins/infra/server/routes/alerting/preview.ts @@ -12,8 +12,10 @@ import { alertPreviewRequestParamsRT, alertPreviewSuccessResponsePayloadRT, MetricThresholdAlertPreviewRequestParams, + InventoryAlertPreviewRequestParams, } from '../../../common/alerting/metrics'; import { createValidationFunction } from '../../../common/runtime_types'; +import { previewInventoryMetricThresholdAlert } from '../../lib/alerting/inventory_metric_threshold/preview_inventory_metric_threshold_alert'; import { previewMetricThresholdAlert } from '../../lib/alerting/metric_threshold/preview_metric_threshold_alert'; import { InfraBackendLibs } from '../../lib/infra_types'; @@ -76,8 +78,35 @@ export const initAlertPreviewRoute = ({ framework, sources }: InfraBackendLibs) }); } case METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID: { - // TODO: Add inventory preview functionality - return response.ok({}); + const { nodeType } = request.body as InventoryAlertPreviewRequestParams; + const previewResult = await previewInventoryMetricThresholdAlert({ + callCluster, + params: { criteria, filterQuery, nodeType }, + lookback, + config: source.configuration, + alertInterval, + }); + + const numberOfGroups = previewResult.length; + const resultTotals = previewResult.reduce( + (totals, groupResult) => { + if (groupResult === null) return { ...totals, noData: totals.noData + 1 }; + if (isNaN(groupResult)) return { ...totals, error: totals.error + 1 }; + return { ...totals, fired: totals.fired + groupResult }; + }, + { + fired: 0, + noData: 0, + error: 0, + } + ); + + return response.ok({ + body: alertPreviewSuccessResponsePayloadRT.encode({ + numberOfGroups, + resultTotals, + }), + }); } default: throw new Error('Unknown alert type'); diff --git a/x-pack/plugins/ingest_manager/common/types/rest_spec/index.ts b/x-pack/plugins/ingest_manager/common/types/rest_spec/index.ts index eb212050ef53ef..294e10aabe4efc 100644 --- a/x-pack/plugins/ingest_manager/common/types/rest_spec/index.ts +++ b/x-pack/plugins/ingest_manager/common/types/rest_spec/index.ts @@ -12,6 +12,7 @@ export * from './fleet_setup'; export * from './epm'; export * from './enrollment_api_key'; export * from './install_script'; +export * from './ingest_setup'; export * from './output'; export * from './settings'; export * from './app'; diff --git a/x-pack/plugins/canvas/public/components/render_with_fn/lib/handlers.js b/x-pack/plugins/ingest_manager/common/types/rest_spec/ingest_setup.ts similarity index 61% rename from x-pack/plugins/canvas/public/components/render_with_fn/lib/handlers.js rename to x-pack/plugins/ingest_manager/common/types/rest_spec/ingest_setup.ts index 9e5032efa97e26..17f4023fc8bead 100644 --- a/x-pack/plugins/canvas/public/components/render_with_fn/lib/handlers.js +++ b/x-pack/plugins/ingest_manager/common/types/rest_spec/ingest_setup.ts @@ -4,16 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -export class ElementHandlers { - resize() {} - - destroy() {} - - onResize(fn) { - this.resize = fn; - } - - onDestroy(fn) { - this.destroy = fn; - } +export interface PostIngestSetupResponse { + isInitialized: boolean; } diff --git a/x-pack/plugins/ingest_manager/public/index.ts b/x-pack/plugins/ingest_manager/public/index.ts index 9f4893ac6e499f..ac56349b30c13e 100644 --- a/x-pack/plugins/ingest_manager/public/index.ts +++ b/x-pack/plugins/ingest_manager/public/index.ts @@ -6,7 +6,7 @@ import { PluginInitializerContext } from 'src/core/public'; import { IngestManagerPlugin } from './plugin'; -export { IngestManagerStart } from './plugin'; +export { IngestManagerSetup, IngestManagerStart } from './plugin'; export const plugin = (initializerContext: PluginInitializerContext) => { return new IngestManagerPlugin(initializerContext); diff --git a/x-pack/plugins/ingest_manager/public/plugin.ts b/x-pack/plugins/ingest_manager/public/plugin.ts index 3eb2fad339b7d0..4a10a26151e780 100644 --- a/x-pack/plugins/ingest_manager/public/plugin.ts +++ b/x-pack/plugins/ingest_manager/public/plugin.ts @@ -14,7 +14,7 @@ import { i18n } from '@kbn/i18n'; import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/public'; import { DataPublicPluginSetup, DataPublicPluginStart } from '../../../../src/plugins/data/public'; import { LicensingPluginSetup } from '../../licensing/public'; -import { PLUGIN_ID } from '../common/constants'; +import { PLUGIN_ID, CheckPermissionsResponse, PostIngestSetupResponse } from '../common'; import { IngestManagerConfigType } from '../common/types'; import { setupRouteService, appRoutesService } from '../common'; @@ -22,16 +22,17 @@ import { registerDatasource } from './applications/ingest_manager/sections/agent export { IngestManagerConfigType } from '../common/types'; -export type IngestManagerSetup = void; +// We need to provide an object instead of void so that dependent plugins know when Ingest Manager +// is disabled. +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface IngestManagerSetup {} + /** * Describes public IngestManager plugin contract returned at the `start` stage. */ export interface IngestManagerStart { registerDatasource: typeof registerDatasource; - success: boolean; - error?: { - message: string; - }; + success: Promise; } export interface IngestManagerSetupDeps { @@ -75,24 +76,34 @@ export class IngestManagerPlugin }; }, }); + + return {}; } public async start(core: CoreStart): Promise { + let successPromise: IngestManagerStart['success']; try { - const permissionsResponse = await core.http.get(appRoutesService.getCheckPermissionsPath()); - if (permissionsResponse.success) { - const { isInitialized: success } = await core.http.post(setupRouteService.getSetupPath()); - return { success, registerDatasource }; + const permissionsResponse = await core.http.get( + appRoutesService.getCheckPermissionsPath() + ); + + if (permissionsResponse?.success) { + successPromise = core.http + .post(setupRouteService.getSetupPath()) + .then(({ isInitialized }) => + isInitialized ? Promise.resolve(true) : Promise.reject(new Error('Unknown setup error')) + ); } else { - throw new Error(permissionsResponse.error); + throw new Error(permissionsResponse?.error || 'Unknown permissions error'); } } catch (error) { - return { - success: false, - error: { message: error.body?.message || 'Unknown error' }, - registerDatasource, - }; + successPromise = Promise.reject(error); } + + return { + success: successPromise, + registerDatasource, + }; } public stop() {} diff --git a/x-pack/plugins/ingest_manager/server/index.ts b/x-pack/plugins/ingest_manager/server/index.ts index f6b2d7ccc6d480..1e9011c9dfe4f7 100644 --- a/x-pack/plugins/ingest_manager/server/index.ts +++ b/x-pack/plugins/ingest_manager/server/index.ts @@ -11,6 +11,7 @@ export { IngestManagerSetupContract, IngestManagerSetupDeps, IngestManagerStartContract, + ExternalCallback, } from './plugin'; export const config = { @@ -42,6 +43,8 @@ export const config = { export type IngestManagerConfigType = TypeOf; +export { DatasourceServiceInterface } from './services/datasource'; + export const plugin = (initializerContext: PluginInitializerContext) => { return new IngestManagerPlugin(initializerContext); }; diff --git a/x-pack/plugins/ingest_manager/server/mocks.ts b/x-pack/plugins/ingest_manager/server/mocks.ts new file mode 100644 index 00000000000000..3bdef14dc85a01 --- /dev/null +++ b/x-pack/plugins/ingest_manager/server/mocks.ts @@ -0,0 +1,36 @@ +/* + * 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 { loggingSystemMock, savedObjectsServiceMock } from 'src/core/server/mocks'; +import { IngestManagerAppContext } from './plugin'; +import { encryptedSavedObjectsMock } from '../../encrypted_saved_objects/server/mocks'; +import { securityMock } from '../../security/server/mocks'; +import { DatasourceServiceInterface } from './services/datasource'; + +export const createAppContextStartContractMock = (): IngestManagerAppContext => { + return { + encryptedSavedObjectsStart: encryptedSavedObjectsMock.createStart(), + savedObjects: savedObjectsServiceMock.createStartContract(), + security: securityMock.createSetup(), + logger: loggingSystemMock.create().get(), + isProductionMode: true, + kibanaVersion: '8.0.0', + }; +}; + +export const createDatasourceServiceMock = () => { + return { + assignPackageStream: jest.fn(), + buildDatasourceFromPackage: jest.fn(), + bulkCreate: jest.fn(), + create: jest.fn(), + delete: jest.fn(), + get: jest.fn(), + getByIDs: jest.fn(), + list: jest.fn(), + update: jest.fn(), + } as jest.Mocked; +}; diff --git a/x-pack/plugins/ingest_manager/server/plugin.ts b/x-pack/plugins/ingest_manager/server/plugin.ts index fb1c218e1545b4..fcdb6387fed3ae 100644 --- a/x-pack/plugins/ingest_manager/server/plugin.ts +++ b/x-pack/plugins/ingest_manager/server/plugin.ts @@ -45,15 +45,16 @@ import { registerSettingsRoutes, registerAppRoutes, } from './routes'; -import { IngestManagerConfigType } from '../common'; +import { IngestManagerConfigType, NewDatasource } from '../common'; import { appContextService, licenseService, ESIndexPatternSavedObjectService, ESIndexPatternService, AgentService, + datasourceService, } from './services'; -import { getAgentStatusById } from './services/agents'; +import { getAgentStatusById, authenticateAgentWithAccessToken } from './services/agents'; import { CloudSetup } from '../../cloud/server'; import { agentCheckinState } from './services/agents/checkin/state'; @@ -92,12 +93,31 @@ const allSavedObjectTypes = [ ENROLLMENT_API_KEYS_SAVED_OBJECT_TYPE, ]; +/** + * Callbacks supported by the Ingest plugin + */ +export type ExternalCallback = [ + 'datasourceCreate', + (newDatasource: NewDatasource) => Promise +]; + +export type ExternalCallbacksStorage = Map>; + /** * Describes public IngestManager plugin contract returned at the `startup` stage. */ export interface IngestManagerStartContract { esIndexPatternService: ESIndexPatternService; agentService: AgentService; + /** + * Services for Ingest's Datasources + */ + datasourceService: typeof datasourceService; + /** + * Register callbacks for inclusion in ingest API processing + * @param args + */ + registerExternalCallback: (...args: ExternalCallback) => void; } export class IngestManagerPlugin @@ -236,6 +256,11 @@ export class IngestManagerPlugin esIndexPatternService: new ESIndexPatternSavedObjectService(), agentService: { getAgentStatusById, + authenticateAgentWithAccessToken, + }, + datasourceService, + registerExternalCallback: (...args: ExternalCallback) => { + return appContextService.addExternalCallback(...args); }, }; } diff --git a/x-pack/plugins/ingest_manager/server/routes/agent/acks_handlers.test.ts b/x-pack/plugins/ingest_manager/server/routes/agent/acks_handlers.test.ts index 84923d5c336642..aaed189ae3dddf 100644 --- a/x-pack/plugins/ingest_manager/server/routes/agent/acks_handlers.test.ts +++ b/x-pack/plugins/ingest_manager/server/routes/agent/acks_handlers.test.ts @@ -77,7 +77,7 @@ describe('test acks handlers', () => { id: 'action1', }, ]), - getAgentByAccessAPIKeyId: jest.fn().mockReturnValueOnce({ + authenticateAgentWithAccessToken: jest.fn().mockReturnValueOnce({ id: 'agent', }), getSavedObjectsClientContract: jest.fn().mockReturnValueOnce(mockSavedObjectsClient), diff --git a/x-pack/plugins/ingest_manager/server/routes/agent/acks_handlers.ts b/x-pack/plugins/ingest_manager/server/routes/agent/acks_handlers.ts index 83d894295c3126..0b719d8a67df74 100644 --- a/x-pack/plugins/ingest_manager/server/routes/agent/acks_handlers.ts +++ b/x-pack/plugins/ingest_manager/server/routes/agent/acks_handlers.ts @@ -9,7 +9,6 @@ import { RequestHandler } from 'kibana/server'; import { TypeOf } from '@kbn/config-schema'; import { PostAgentAcksRequestSchema } from '../../types/rest_spec'; -import * as APIKeyService from '../../services/api_keys'; import { AcksService } from '../../services/agents'; import { AgentEvent } from '../../../common/types/models'; import { PostAgentAcksResponse } from '../../../common/types/rest_spec'; @@ -24,8 +23,7 @@ export const postAgentAcksHandlerBuilder = function ( return async (context, request, response) => { try { const soClient = ackService.getSavedObjectsClientContract(request); - const res = APIKeyService.parseApiKeyFromHeaders(request.headers); - const agent = await ackService.getAgentByAccessAPIKeyId(soClient, res.apiKeyId as string); + const agent = await ackService.authenticateAgentWithAccessToken(soClient, request); const agentEvents = request.body.events as AgentEvent[]; // validate that all events are for the authorized agent obtained from the api key diff --git a/x-pack/plugins/ingest_manager/server/routes/agent/handlers.ts b/x-pack/plugins/ingest_manager/server/routes/agent/handlers.ts index 0d1c77b8d697fd..d31498599a2b65 100644 --- a/x-pack/plugins/ingest_manager/server/routes/agent/handlers.ts +++ b/x-pack/plugins/ingest_manager/server/routes/agent/handlers.ts @@ -171,8 +171,7 @@ export const postAgentCheckinHandler: RequestHandler< > = async (context, request, response) => { try { const soClient = appContextService.getInternalUserSOClient(request); - const res = APIKeyService.parseApiKeyFromHeaders(request.headers); - const agent = await AgentService.getAgentByAccessAPIKeyId(soClient, res.apiKeyId); + const agent = await AgentService.authenticateAgentWithAccessToken(soClient, request); const abortController = new AbortController(); request.events.aborted$.subscribe(() => { abortController.abort(); diff --git a/x-pack/plugins/ingest_manager/server/routes/agent/index.ts b/x-pack/plugins/ingest_manager/server/routes/agent/index.ts index 87eee4622c80b6..eaab46c7b455c3 100644 --- a/x-pack/plugins/ingest_manager/server/routes/agent/index.ts +++ b/x-pack/plugins/ingest_manager/server/routes/agent/index.ts @@ -109,7 +109,7 @@ export const registerRoutes = (router: IRouter) => { }, postAgentAcksHandlerBuilder({ acknowledgeAgentActions: AgentService.acknowledgeAgentActions, - getAgentByAccessAPIKeyId: AgentService.getAgentByAccessAPIKeyId, + authenticateAgentWithAccessToken: AgentService.authenticateAgentWithAccessToken, getSavedObjectsClientContract: appContextService.getInternalUserSOClient.bind( appContextService ), diff --git a/x-pack/plugins/ingest_manager/server/routes/datasource/datasource_handlers.test.ts b/x-pack/plugins/ingest_manager/server/routes/datasource/datasource_handlers.test.ts new file mode 100644 index 00000000000000..07cbeb8b2cec56 --- /dev/null +++ b/x-pack/plugins/ingest_manager/server/routes/datasource/datasource_handlers.test.ts @@ -0,0 +1,332 @@ +/* + * 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 { httpServerMock, httpServiceMock } from 'src/core/server/mocks'; +import { IRouter, KibanaRequest, Logger, RequestHandler, RouteConfig } from 'kibana/server'; +import { registerRoutes } from './index'; +import { DATASOURCE_API_ROUTES } from '../../../common/constants'; +import { xpackMocks } from '../../../../../mocks'; +import { appContextService } from '../../services'; +import { createAppContextStartContractMock } from '../../mocks'; +import { DatasourceServiceInterface, ExternalCallback } from '../..'; +import { CreateDatasourceRequestSchema } from '../../types/rest_spec'; +import { datasourceService } from '../../services'; + +const datasourceServiceMock = datasourceService as jest.Mocked; + +jest.mock('../../services/datasource', (): { + datasourceService: jest.Mocked; +} => { + return { + datasourceService: { + assignPackageStream: jest.fn((packageInfo, dataInputs) => Promise.resolve(dataInputs)), + buildDatasourceFromPackage: jest.fn(), + bulkCreate: jest.fn(), + create: jest.fn((soClient, newData) => + Promise.resolve({ + ...newData, + id: '1', + revision: 1, + updated_at: new Date().toISOString(), + updated_by: 'elastic', + created_at: new Date().toISOString(), + created_by: 'elastic', + }) + ), + delete: jest.fn(), + get: jest.fn(), + getByIDs: jest.fn(), + list: jest.fn(), + update: jest.fn(), + }, + }; +}); + +jest.mock('../../services/epm/packages', () => { + return { + ensureInstalledPackage: jest.fn(() => Promise.resolve()), + getPackageInfo: jest.fn(() => Promise.resolve()), + }; +}); + +describe('When calling datasource', () => { + let routerMock: jest.Mocked; + let routeHandler: RequestHandler; + let routeConfig: RouteConfig; + let context: ReturnType; + let response: ReturnType; + + beforeAll(() => { + routerMock = httpServiceMock.createRouter(); + registerRoutes(routerMock); + }); + + beforeEach(() => { + appContextService.start(createAppContextStartContractMock()); + context = xpackMocks.createRequestHandlerContext(); + response = httpServerMock.createResponseFactory(); + }); + + afterEach(() => { + jest.clearAllMocks(); + appContextService.stop(); + }); + + describe('create api handler', () => { + const getCreateKibanaRequest = ( + newData?: typeof CreateDatasourceRequestSchema.body + ): KibanaRequest => { + return httpServerMock.createKibanaRequest< + undefined, + undefined, + typeof CreateDatasourceRequestSchema.body + >({ + path: routeConfig.path, + method: 'post', + body: newData || { + name: 'endpoint-1', + description: '', + config_id: 'a5ca00c0-b30c-11ea-9732-1bb05811278c', + enabled: true, + output_id: '', + inputs: [], + namespace: 'default', + package: { name: 'endpoint', title: 'Elastic Endpoint', version: '0.5.0' }, + }, + }); + }; + + // Set the routeConfig and routeHandler to the Create API + beforeAll(() => { + [routeConfig, routeHandler] = routerMock.post.mock.calls.find(([{ path }]) => + path.startsWith(DATASOURCE_API_ROUTES.CREATE_PATTERN) + )!; + }); + + describe('and external callbacks are registered', () => { + const callbackCallingOrder: string[] = []; + + // Callback one adds an input that includes a `config` property + const callbackOne: ExternalCallback[1] = jest.fn(async (ds) => { + callbackCallingOrder.push('one'); + const newDs = { + ...ds, + inputs: [ + { + type: 'endpoint', + enabled: true, + streams: [], + config: { + one: { + value: 'inserted by callbackOne', + }, + }, + }, + ], + }; + return newDs; + }); + + // Callback two adds an additional `input[0].config` property + const callbackTwo: ExternalCallback[1] = jest.fn(async (ds) => { + callbackCallingOrder.push('two'); + const newDs = { + ...ds, + inputs: [ + { + ...ds.inputs[0], + config: { + ...ds.inputs[0].config, + two: { + value: 'inserted by callbackTwo', + }, + }, + }, + ], + }; + return newDs; + }); + + beforeEach(() => { + appContextService.addExternalCallback('datasourceCreate', callbackOne); + appContextService.addExternalCallback('datasourceCreate', callbackTwo); + }); + + afterEach(() => (callbackCallingOrder.length = 0)); + + it('should call external callbacks in expected order', async () => { + const request = getCreateKibanaRequest(); + await routeHandler(context, request, response); + expect(response.ok).toHaveBeenCalled(); + expect(callbackCallingOrder).toEqual(['one', 'two']); + }); + + it('should feed datasource returned by last callback', async () => { + const request = getCreateKibanaRequest(); + await routeHandler(context, request, response); + expect(response.ok).toHaveBeenCalled(); + expect(callbackOne).toHaveBeenCalledWith({ + config_id: 'a5ca00c0-b30c-11ea-9732-1bb05811278c', + description: '', + enabled: true, + inputs: [], + name: 'endpoint-1', + namespace: 'default', + output_id: '', + package: { + name: 'endpoint', + title: 'Elastic Endpoint', + version: '0.5.0', + }, + }); + expect(callbackTwo).toHaveBeenCalledWith({ + config_id: 'a5ca00c0-b30c-11ea-9732-1bb05811278c', + description: '', + enabled: true, + inputs: [ + { + type: 'endpoint', + enabled: true, + streams: [], + config: { + one: { + value: 'inserted by callbackOne', + }, + }, + }, + ], + name: 'endpoint-1', + namespace: 'default', + output_id: '', + package: { + name: 'endpoint', + title: 'Elastic Endpoint', + version: '0.5.0', + }, + }); + }); + + it('should create with data from callback', async () => { + const request = getCreateKibanaRequest(); + await routeHandler(context, request, response); + expect(response.ok).toHaveBeenCalled(); + expect(datasourceServiceMock.create.mock.calls[0][1]).toEqual({ + config_id: 'a5ca00c0-b30c-11ea-9732-1bb05811278c', + description: '', + enabled: true, + inputs: [ + { + config: { + one: { + value: 'inserted by callbackOne', + }, + two: { + value: 'inserted by callbackTwo', + }, + }, + enabled: true, + streams: [], + type: 'endpoint', + }, + ], + name: 'endpoint-1', + namespace: 'default', + output_id: '', + package: { + name: 'endpoint', + title: 'Elastic Endpoint', + version: '0.5.0', + }, + }); + }); + + describe('and a callback throws an exception', () => { + const callbackThree: ExternalCallback[1] = jest.fn(async (ds) => { + callbackCallingOrder.push('three'); + throw new Error('callbackThree threw error on purpose'); + }); + + const callbackFour: ExternalCallback[1] = jest.fn(async (ds) => { + callbackCallingOrder.push('four'); + return { + ...ds, + inputs: [ + { + ...ds.inputs[0], + config: { + ...ds.inputs[0].config, + four: { + value: 'inserted by callbackFour', + }, + }, + }, + ], + }; + }); + + beforeEach(() => { + appContextService.addExternalCallback('datasourceCreate', callbackThree); + appContextService.addExternalCallback('datasourceCreate', callbackFour); + }); + + it('should skip over callback exceptions and still execute other callbacks', async () => { + const request = getCreateKibanaRequest(); + await routeHandler(context, request, response); + expect(response.ok).toHaveBeenCalled(); + expect(callbackCallingOrder).toEqual(['one', 'two', 'three', 'four']); + }); + + it('should log errors', async () => { + const errorLogger = (appContextService.getLogger() as jest.Mocked).error; + const request = getCreateKibanaRequest(); + await routeHandler(context, request, response); + expect(response.ok).toHaveBeenCalled(); + expect(errorLogger.mock.calls).toEqual([ + ['An external registered [datasourceCreate] callback failed when executed'], + [new Error('callbackThree threw error on purpose')], + ]); + }); + + it('should create datasource with last successful returned datasource', async () => { + const request = getCreateKibanaRequest(); + await routeHandler(context, request, response); + expect(response.ok).toHaveBeenCalled(); + expect(datasourceServiceMock.create.mock.calls[0][1]).toEqual({ + config_id: 'a5ca00c0-b30c-11ea-9732-1bb05811278c', + description: '', + enabled: true, + inputs: [ + { + config: { + one: { + value: 'inserted by callbackOne', + }, + two: { + value: 'inserted by callbackTwo', + }, + four: { + value: 'inserted by callbackFour', + }, + }, + enabled: true, + streams: [], + type: 'endpoint', + }, + ], + name: 'endpoint-1', + namespace: 'default', + output_id: '', + package: { + name: 'endpoint', + title: 'Elastic Endpoint', + version: '0.5.0', + }, + }); + }); + }); + }); + }); +}); diff --git a/x-pack/plugins/ingest_manager/server/routes/datasource/handlers.ts b/x-pack/plugins/ingest_manager/server/routes/datasource/handlers.ts index 09daec3370400c..4f83d24a846ea7 100644 --- a/x-pack/plugins/ingest_manager/server/routes/datasource/handlers.ts +++ b/x-pack/plugins/ingest_manager/server/routes/datasource/handlers.ts @@ -14,6 +14,7 @@ import { CreateDatasourceRequestSchema, UpdateDatasourceRequestSchema, DeleteDatasourcesRequestSchema, + NewDatasource, } from '../../types'; import { CreateDatasourceResponse, DeleteDatasourcesResponse } from '../../../common'; @@ -76,23 +77,50 @@ export const createDatasourceHandler: RequestHandler< const soClient = context.core.savedObjects.client; const callCluster = context.core.elasticsearch.legacy.client.callAsCurrentUser; const user = (await appContextService.getSecurity()?.authc.getCurrentUser(request)) || undefined; - const newData = { ...request.body }; + const logger = appContextService.getLogger(); + let newData = { ...request.body }; try { + // If we have external callbacks, then process those now before creating the actual datasource + const externalCallbacks = appContextService.getExternalCallbacks('datasourceCreate'); + if (externalCallbacks && externalCallbacks.size > 0) { + let updatedNewData: NewDatasource = newData; + + for (const callback of externalCallbacks) { + try { + // ensure that the returned value by the callback passes schema validation + updatedNewData = CreateDatasourceRequestSchema.body.validate( + await callback(updatedNewData) + ); + } catch (error) { + // Log the error, but keep going and process the other callbacks + logger.error('An external registered [datasourceCreate] callback failed when executed'); + logger.error(error); + } + } + + // The type `NewDatasource` and the `DatasourceBaseSchema` are incompatible. + // `NewDatasrouce` defines `namespace` as optional string, which means that `undefined` is a + // valid value, however, the schema defines it as string with a minimum length of 1. + // Here, we need to cast the value back to the schema type and ignore the TS error. + // @ts-ignore + newData = updatedNewData as typeof CreateDatasourceRequestSchema.body; + } + // Make sure the datasource package is installed - if (request.body.package?.name) { + if (newData.package?.name) { await ensureInstalledPackage({ savedObjectsClient: soClient, - pkgName: request.body.package.name, + pkgName: newData.package.name, callCluster, }); const pkgInfo = await getPackageInfo({ savedObjectsClient: soClient, - pkgName: request.body.package.name, - pkgVersion: request.body.package.version, + pkgName: newData.package.name, + pkgVersion: newData.package.version, }); newData.inputs = (await datasourceService.assignPackageStream( pkgInfo, - request.body.inputs + newData.inputs )) as TypeOf['inputs']; } @@ -103,6 +131,7 @@ export const createDatasourceHandler: RequestHandler< body, }); } catch (e) { + logger.error(e); return response.customError({ statusCode: 500, body: { message: e.message }, diff --git a/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts b/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts index 98083434173908..1daa63800f4ee5 100644 --- a/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts +++ b/x-pack/plugins/ingest_manager/server/routes/setup/handlers.ts @@ -6,7 +6,7 @@ import { RequestHandler } from 'src/core/server'; import { TypeOf } from '@kbn/config-schema'; import { outputService, appContextService } from '../../services'; -import { GetFleetStatusResponse } from '../../../common'; +import { GetFleetStatusResponse, PostIngestSetupResponse } from '../../../common'; import { setupIngestManager, setupFleet } from '../../services/setup'; import { PostFleetSetupRequestSchema } from '../../types'; import { IngestManagerError, getHTTPResponseCode } from '../../errors'; @@ -83,9 +83,10 @@ export const ingestManagerSetupHandler: RequestHandler = async (context, request const callCluster = context.core.elasticsearch.legacy.client.callAsCurrentUser; const logger = appContextService.getLogger(); try { + const body: PostIngestSetupResponse = { isInitialized: true }; await setupIngestManager(soClient, callCluster); return response.ok({ - body: { isInitialized: true }, + body, }); } catch (e) { if (e instanceof IngestManagerError) { diff --git a/x-pack/plugins/ingest_manager/server/services/agents/acks.ts b/x-pack/plugins/ingest_manager/server/services/agents/acks.ts index 81ba9754e8aa48..a1b48a879bb890 100644 --- a/x-pack/plugins/ingest_manager/server/services/agents/acks.ts +++ b/x-pack/plugins/ingest_manager/server/services/agents/acks.ts @@ -140,9 +140,9 @@ export interface AcksService { actionIds: AgentEvent[] ) => Promise; - getAgentByAccessAPIKeyId: ( + authenticateAgentWithAccessToken: ( soClient: SavedObjectsClientContract, - accessAPIKeyId: string + request: KibanaRequest ) => Promise; getSavedObjectsClientContract: (kibanaRequest: KibanaRequest) => SavedObjectsClientContract; diff --git a/x-pack/plugins/ingest_manager/server/services/agents/authenticate.test.ts b/x-pack/plugins/ingest_manager/server/services/agents/authenticate.test.ts new file mode 100644 index 00000000000000..b56ca4ca8cc177 --- /dev/null +++ b/x-pack/plugins/ingest_manager/server/services/agents/authenticate.test.ts @@ -0,0 +1,154 @@ +/* + * 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 { KibanaRequest } from 'kibana/server'; +import { savedObjectsClientMock } from 'src/core/server/mocks'; + +import { authenticateAgentWithAccessToken } from './authenticate'; + +describe('test agent autenticate services', () => { + it('should succeed with a valid API key and an active agent', async () => { + const mockSavedObjectsClient = savedObjectsClientMock.create(); + mockSavedObjectsClient.find.mockReturnValue( + Promise.resolve({ + page: 1, + per_page: 100, + total: 1, + saved_objects: [ + { + id: 'agent1', + type: 'agent', + references: [], + score: 0, + attributes: { + active: true, + access_api_key_id: 'pedTuHIBTEDt93wW0Fhr', + }, + }, + ], + }) + ); + await authenticateAgentWithAccessToken(mockSavedObjectsClient, { + auth: { isAuthenticated: true }, + headers: { + authorization: 'ApiKey cGVkVHVISUJURUR0OTN3VzBGaHI6TnU1U0JtbHJSeC12Rm9qQWpoSHlUZw==', + }, + } as KibanaRequest); + }); + + it('should throw if the request is not authenticated', async () => { + const mockSavedObjectsClient = savedObjectsClientMock.create(); + mockSavedObjectsClient.find.mockReturnValue( + Promise.resolve({ + page: 1, + per_page: 100, + total: 1, + saved_objects: [ + { + id: 'agent1', + type: 'agent', + references: [], + score: 0, + attributes: { + active: true, + access_api_key_id: 'pedTuHIBTEDt93wW0Fhr', + }, + }, + ], + }) + ); + expect( + authenticateAgentWithAccessToken(mockSavedObjectsClient, { + auth: { isAuthenticated: false }, + headers: { + authorization: 'ApiKey cGVkVHVISUJURUR0OTN3VzBGaHI6TnU1U0JtbHJSeC12Rm9qQWpoSHlUZw==', + }, + } as KibanaRequest) + ).rejects.toThrow(/Request not authenticated/); + }); + + it('should throw if the ApiKey headers is malformed', async () => { + const mockSavedObjectsClient = savedObjectsClientMock.create(); + mockSavedObjectsClient.find.mockReturnValue( + Promise.resolve({ + page: 1, + per_page: 100, + total: 1, + saved_objects: [ + { + id: 'agent1', + type: 'agent', + references: [], + score: 0, + attributes: { + active: false, + access_api_key_id: 'pedTuHIBTEDt93wW0Fhr', + }, + }, + ], + }) + ); + expect( + authenticateAgentWithAccessToken(mockSavedObjectsClient, { + auth: { isAuthenticated: true }, + headers: { + authorization: 'aaaa', + }, + } as KibanaRequest) + ).rejects.toThrow(/Authorization header is malformed/); + }); + + it('should throw if the agent is not active', async () => { + const mockSavedObjectsClient = savedObjectsClientMock.create(); + mockSavedObjectsClient.find.mockReturnValue( + Promise.resolve({ + page: 1, + per_page: 100, + total: 1, + saved_objects: [ + { + id: 'agent1', + type: 'agent', + references: [], + score: 0, + attributes: { + active: false, + access_api_key_id: 'pedTuHIBTEDt93wW0Fhr', + }, + }, + ], + }) + ); + expect( + authenticateAgentWithAccessToken(mockSavedObjectsClient, { + auth: { isAuthenticated: true }, + headers: { + authorization: 'ApiKey cGVkVHVISUJURUR0OTN3VzBGaHI6TnU1U0JtbHJSeC12Rm9qQWpoSHlUZw==', + }, + } as KibanaRequest) + ).rejects.toThrow(/Agent inactive/); + }); + + it('should throw if there is no agent matching the API key', async () => { + const mockSavedObjectsClient = savedObjectsClientMock.create(); + mockSavedObjectsClient.find.mockReturnValue( + Promise.resolve({ + page: 1, + per_page: 100, + total: 1, + saved_objects: [], + }) + ); + expect( + authenticateAgentWithAccessToken(mockSavedObjectsClient, { + auth: { isAuthenticated: true }, + headers: { + authorization: 'ApiKey cGVkVHVISUJURUR0OTN3VzBGaHI6TnU1U0JtbHJSeC12Rm9qQWpoSHlUZw==', + }, + } as KibanaRequest) + ).rejects.toThrow(/Agent not found/); + }); +}); diff --git a/x-pack/plugins/ingest_manager/server/services/agents/authenticate.ts b/x-pack/plugins/ingest_manager/server/services/agents/authenticate.ts new file mode 100644 index 00000000000000..2515a02da4e781 --- /dev/null +++ b/x-pack/plugins/ingest_manager/server/services/agents/authenticate.ts @@ -0,0 +1,30 @@ +/* + * 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 Boom from 'boom'; +import { KibanaRequest, SavedObjectsClientContract } from 'src/core/server'; +import { Agent } from '../../types'; +import * as APIKeyService from '../api_keys'; +import { getAgentByAccessAPIKeyId } from './crud'; + +export async function authenticateAgentWithAccessToken( + soClient: SavedObjectsClientContract, + request: KibanaRequest +): Promise { + if (!request.auth.isAuthenticated) { + throw Boom.unauthorized('Request not authenticated'); + } + let res: { apiKey: string; apiKeyId: string }; + try { + res = APIKeyService.parseApiKeyFromHeaders(request.headers); + } catch (err) { + throw Boom.unauthorized(err.message); + } + + const agent = await getAgentByAccessAPIKeyId(soClient, res.apiKeyId); + + return agent; +} diff --git a/x-pack/plugins/ingest_manager/server/services/agents/index.ts b/x-pack/plugins/ingest_manager/server/services/agents/index.ts index 257091af0ebd0a..400c099af4e936 100644 --- a/x-pack/plugins/ingest_manager/server/services/agents/index.ts +++ b/x-pack/plugins/ingest_manager/server/services/agents/index.ts @@ -14,3 +14,4 @@ export * from './crud'; export * from './update'; export * from './actions'; export * from './reassign'; +export * from './authenticate'; diff --git a/x-pack/plugins/ingest_manager/server/services/app_context.ts b/x-pack/plugins/ingest_manager/server/services/app_context.ts index 5ed6f7c5e54d18..4d109b73d12d90 100644 --- a/x-pack/plugins/ingest_manager/server/services/app_context.ts +++ b/x-pack/plugins/ingest_manager/server/services/app_context.ts @@ -12,7 +12,7 @@ import { } from '../../../encrypted_saved_objects/server'; import { SecurityPluginSetup } from '../../../security/server'; import { IngestManagerConfigType } from '../../common'; -import { IngestManagerAppContext } from '../plugin'; +import { ExternalCallback, ExternalCallbacksStorage, IngestManagerAppContext } from '../plugin'; import { CloudSetup } from '../../../cloud/server'; class AppContextService { @@ -27,6 +27,7 @@ class AppContextService { private cloud?: CloudSetup; private logger: Logger | undefined; private httpSetup?: HttpServiceSetup; + private externalCallbacks: ExternalCallbacksStorage = new Map(); public async start(appContext: IngestManagerAppContext) { this.encryptedSavedObjects = appContext.encryptedSavedObjectsStart?.getClient(); @@ -47,7 +48,9 @@ class AppContextService { } } - public stop() {} + public stop() { + this.externalCallbacks.clear(); + } public getEncryptedSavedObjects() { if (!this.encryptedSavedObjects) { @@ -121,6 +124,19 @@ class AppContextService { } return this.kibanaVersion; } + + public addExternalCallback(type: ExternalCallback[0], callback: ExternalCallback[1]) { + if (!this.externalCallbacks.has(type)) { + this.externalCallbacks.set(type, new Set()); + } + this.externalCallbacks.get(type)!.add(callback); + } + + public getExternalCallbacks(type: ExternalCallback[0]) { + if (this.externalCallbacks) { + return this.externalCallbacks.get(type); + } + } } export const appContextService = new AppContextService(); diff --git a/x-pack/plugins/ingest_manager/server/services/datasource.ts b/x-pack/plugins/ingest_manager/server/services/datasource.ts index 3ad94ea8191d4d..f3f460d2a74206 100644 --- a/x-pack/plugins/ingest_manager/server/services/datasource.ts +++ b/x-pack/plugins/ingest_manager/server/services/datasource.ts @@ -307,4 +307,5 @@ async function _assignPackageStreamToStream( return { ...stream }; } +export type DatasourceServiceInterface = DatasourceService; export const datasourceService = new DatasourceService(); diff --git a/x-pack/plugins/licensing/server/plugin.test.ts b/x-pack/plugins/licensing/server/plugin.test.ts index 3e31eae9453833..9d76472b51cd26 100644 --- a/x-pack/plugins/licensing/server/plugin.test.ts +++ b/x-pack/plugins/licensing/server/plugin.test.ts @@ -12,7 +12,7 @@ import { LicensingPlugin } from './plugin'; import { coreMock, elasticsearchServiceMock, - loggingServiceMock, + loggingSystemMock, } from '../../../../src/core/server/mocks'; import { IClusterClient } from '../../../../src/core/server/'; @@ -173,7 +173,7 @@ describe('licensing plugin', () => { await flushPromises(); - const loggedMessages = loggingServiceMock.collect(pluginInitContextMock.logger).debug; + const loggedMessages = loggingSystemMock.collect(pluginInitContextMock.logger).debug; expect( loggedMessages.some(([message]) => diff --git a/x-pack/plugins/maps/common/constants.ts b/x-pack/plugins/maps/common/constants.ts index be3de22fa011e8..1d795c370dc00b 100644 --- a/x-pack/plugins/maps/common/constants.ts +++ b/x-pack/plugins/maps/common/constants.ts @@ -25,7 +25,7 @@ export const EMS_TILES_VECTOR_TILE_PATH = 'vector/tile'; export const MAP_SAVED_OBJECT_TYPE = 'map'; export const APP_ID = 'maps'; export const APP_ICON = 'gisApp'; -export const TELEMETRY_TYPE = 'maps-telemetry'; +export const TELEMETRY_TYPE = APP_ID; export const MAP_APP_PATH = `app/${APP_ID}`; export const GIS_API_PATH = `api/${APP_ID}`; diff --git a/x-pack/plugins/maps/server/maps_telemetry/maps_telemetry.ts b/x-pack/plugins/maps/server/maps_telemetry/maps_telemetry.ts index 463d3f3b3939d9..0e29eca2446422 100644 --- a/x-pack/plugins/maps/server/maps_telemetry/maps_telemetry.ts +++ b/x-pack/plugins/maps/server/maps_telemetry/maps_telemetry.ts @@ -11,12 +11,7 @@ import { SavedObjectAttribute, } from 'kibana/server'; import { IFieldType, IIndexPattern } from 'src/plugins/data/public'; -import { - SOURCE_TYPES, - ES_GEO_FIELD_TYPE, - MAP_SAVED_OBJECT_TYPE, - TELEMETRY_TYPE, -} from '../../common/constants'; +import { SOURCE_TYPES, ES_GEO_FIELD_TYPE, MAP_SAVED_OBJECT_TYPE } from '../../common/constants'; import { LayerDescriptor } from '../../common/descriptor_types'; import { MapSavedObject } from '../../common/map_saved_object_type'; // @ts-ignore @@ -186,9 +181,5 @@ export async function getMapsTelemetry(config: MapsConfigType) { const settings: SavedObjectAttribute = { showMapVisualizationTypes: config.showMapVisualizationTypes, }; - const mapsTelemetry = buildMapsTelemetry({ mapSavedObjects, indexPatternSavedObjects, settings }); - return await savedObjectsClient.create(TELEMETRY_TYPE, mapsTelemetry, { - id: TELEMETRY_TYPE, - overwrite: true, - }); + return buildMapsTelemetry({ mapSavedObjects, indexPatternSavedObjects, settings }); } diff --git a/x-pack/plugins/maps/server/saved_objects/maps_telemetry.ts b/x-pack/plugins/maps/server/saved_objects/maps_telemetry.ts index 2512bf3094bcfb..ad0b17af36ddab 100644 --- a/x-pack/plugins/maps/server/saved_objects/maps_telemetry.ts +++ b/x-pack/plugins/maps/server/saved_objects/maps_telemetry.ts @@ -6,7 +6,7 @@ import { SavedObjectsType } from 'src/core/server'; export const mapsTelemetrySavedObjects: SavedObjectsType = { - name: 'maps-telemetry', + name: 'maps', hidden: false, namespaceType: 'agnostic', mappings: { diff --git a/x-pack/plugins/ml/common/util/job_utils.ts b/x-pack/plugins/ml/common/util/job_utils.ts index 1fef0e6e2ecba9..7ea4ceccf578d5 100644 --- a/x-pack/plugins/ml/common/util/job_utils.ts +++ b/x-pack/plugins/ml/common/util/job_utils.ts @@ -6,6 +6,7 @@ import _ from 'lodash'; import semver from 'semver'; +import { Duration } from 'moment'; // @ts-ignore import numeral from '@elastic/numeral'; @@ -433,7 +434,7 @@ export function basicJobValidation( messages.push({ id: 'bucket_span_empty' }); valid = false; } else { - if (isValidTimeFormat(job.analysis_config.bucket_span)) { + if (isValidTimeInterval(job.analysis_config.bucket_span)) { messages.push({ id: 'bucket_span_valid', bucketSpan: job.analysis_config.bucket_span, @@ -490,14 +491,14 @@ export function basicDatafeedValidation(datafeed: Datafeed): ValidationResults { if (datafeed) { let queryDelayMessage = { id: 'query_delay_valid' }; - if (isValidTimeFormat(datafeed.query_delay) === false) { + if (isValidTimeInterval(datafeed.query_delay) === false) { queryDelayMessage = { id: 'query_delay_invalid' }; valid = false; } messages.push(queryDelayMessage); let frequencyMessage = { id: 'frequency_valid' }; - if (isValidTimeFormat(datafeed.frequency) === false) { + if (isValidTimeInterval(datafeed.frequency) === false) { frequencyMessage = { id: 'frequency_invalid' }; valid = false; } @@ -591,12 +592,33 @@ export function validateGroupNames(job: Job): ValidationResults { }; } -function isValidTimeFormat(value: string | undefined): boolean { +/** + * Parses the supplied string to a time interval suitable for use in an ML anomaly + * detection job or datafeed. + * @param value the string to parse + * @return {Duration} the parsed interval, or null if it does not represent a valid + * time interval. + */ +export function parseTimeIntervalForJob(value: string | undefined): Duration | null { + if (value === undefined) { + return null; + } + + // Must be a valid interval, greater than zero, + // and if specified in ms must be a multiple of 1000ms. + const interval = parseInterval(value, true); + return interval !== null && interval.asMilliseconds() !== 0 && interval.milliseconds() === 0 + ? interval + : null; +} + +// Checks that the value for a field which represents a time interval, +// such as a job bucket span or datafeed query delay, is valid. +function isValidTimeInterval(value: string | undefined): boolean { if (value === undefined) { return true; } - const interval = parseInterval(value); - return interval !== null && interval.asMilliseconds() !== 0; + return parseTimeIntervalForJob(value) !== null; } // Returns the latest of the last source data and last processed bucket timestamp, diff --git a/x-pack/plugins/ml/common/util/parse_interval.test.ts b/x-pack/plugins/ml/common/util/parse_interval.test.ts index 1717b2f0dd80be..be7ca2d55eecf1 100644 --- a/x-pack/plugins/ml/common/util/parse_interval.test.ts +++ b/x-pack/plugins/ml/common/util/parse_interval.test.ts @@ -7,7 +7,7 @@ import { parseInterval } from './parse_interval'; describe('ML parse interval util', () => { - test('correctly parses an interval containing unit and value', () => { + test('should correctly parse an interval containing a valid unit and value', () => { expect(parseInterval('1d')!.as('d')).toBe(1); expect(parseInterval('2y')!.as('y')).toBe(2); expect(parseInterval('5M')!.as('M')).toBe(5); @@ -20,15 +20,25 @@ describe('ML parse interval util', () => { expect(parseInterval('0s')!.as('h')).toBe(0); }); - test('correctly handles zero value intervals', () => { + test('should correctly handle zero value intervals', () => { expect(parseInterval('0h')!.as('h')).toBe(0); expect(parseInterval('0d')).toBe(null); }); - test('returns null for an invalid interval', () => { + test('should return null for an invalid interval', () => { expect(parseInterval('')).toBe(null); expect(parseInterval('234asdf')).toBe(null); expect(parseInterval('m')).toBe(null); expect(parseInterval('1.5h')).toBe(null); }); + + test('should correctly check for whether the interval units are valid Elasticsearch time units', () => { + expect(parseInterval('100s', true)!.as('s')).toBe(100); + expect(parseInterval('5m', true)!.as('m')).toBe(5); + expect(parseInterval('24h', true)!.as('h')).toBe(24); + expect(parseInterval('7d', true)!.as('d')).toBe(7); + expect(parseInterval('1w', true)).toBe(null); + expect(parseInterval('1M', true)).toBe(null); + expect(parseInterval('1y', true)).toBe(null); + }); }); diff --git a/x-pack/plugins/ml/common/util/parse_interval.ts b/x-pack/plugins/ml/common/util/parse_interval.ts index 0f348f43d47b30..da6cd9db67792e 100644 --- a/x-pack/plugins/ml/common/util/parse_interval.ts +++ b/x-pack/plugins/ml/common/util/parse_interval.ts @@ -16,7 +16,15 @@ const INTERVAL_STRING_RE = new RegExp('^([0-9]*)\\s*(' + dateMath.units.join('|' // for units of hour or less. const SUPPORT_ZERO_DURATION_UNITS: SupportedUnits[] = ['ms', 's', 'm', 'h']; +// List of time units which are supported for use in Elasticsearch durations +// (such as anomaly detection job bucket spans) +// See https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#time-units +const SUPPORT_ES_DURATION_UNITS: SupportedUnits[] = ['ms', 's', 'm', 'h', 'd']; + // Parses an interval String, such as 7d, 1h or 30m to a moment duration. +// Optionally carries out an additional check that the interval is supported as a +// time unit by Elasticsearch, as units greater than 'd' for example cannot be used +// for anomaly detection job bucket spans. // Differs from the Kibana ui/utils/parse_interval in the following ways: // 1. A value-less interval such as 'm' is not allowed - in line with the ML back-end // not accepting such interval Strings for the bucket span of a job. @@ -25,7 +33,7 @@ const SUPPORT_ZERO_DURATION_UNITS: SupportedUnits[] = ['ms', 's', 'm', 'h']; // to work with units less than 'day'. // 3. Fractional intervals e.g. 1.5h or 4.5d are not allowed, in line with the behaviour // of the Elasticsearch date histogram aggregation. -export function parseInterval(interval: string): Duration | null { +export function parseInterval(interval: string, checkValidEsUnit = false): Duration | null { const matches = String(interval).trim().match(INTERVAL_STRING_RE); if (!Array.isArray(matches) || matches.length < 3) { return null; @@ -36,8 +44,13 @@ export function parseInterval(interval: string): Duration | null { const unit = matches[2] as SupportedUnits; // In line with moment.js, only allow zero value intervals when the unit is less than 'day'. - // And check for isNaN as e.g. valueless 'm' will pass the regex test. - if (isNaN(value) || (value < 1 && SUPPORT_ZERO_DURATION_UNITS.indexOf(unit) === -1)) { + // And check for isNaN as e.g. valueless 'm' will pass the regex test, + // plus an optional check that the unit is not w/M/y which are not fully supported by ES. + if ( + isNaN(value) || + (value < 1 && SUPPORT_ZERO_DURATION_UNITS.indexOf(unit) === -1) || + (checkValidEsUnit === true && SUPPORT_ES_DURATION_UNITS.indexOf(unit) === -1) + ) { return null; } diff --git a/x-pack/plugins/ml/kibana.json b/x-pack/plugins/ml/kibana.json index e9d4aff3484b1c..f93e7bc19f9603 100644 --- a/x-pack/plugins/ml/kibana.json +++ b/x-pack/plugins/ml/kibana.json @@ -15,7 +15,8 @@ "usageCollection", "share", "embeddable", - "uiActions" + "uiActions", + "kibanaLegacy" ], "optionalPlugins": [ "security", diff --git a/x-pack/plugins/ml/public/application/app.tsx b/x-pack/plugins/ml/public/application/app.tsx index b871d857f7fded..3df176ff25cb41 100644 --- a/x-pack/plugins/ml/public/application/app.tsx +++ b/x-pack/plugins/ml/public/application/app.tsx @@ -78,6 +78,8 @@ export const renderApp = ( urlGenerators: deps.share.urlGenerators, }); + deps.kibanaLegacy.loadFontAwesome(); + const mlLicense = setLicenseCache(deps.licensing); appMountParams.onAppLeave((actions) => actions.default()); diff --git a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_creation/components/create_step/create_step.tsx b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_creation/components/create_step/create_step.tsx index 8d51848a25f500..0d1690cf179463 100644 --- a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_creation/components/create_step/create_step.tsx +++ b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_creation/components/create_step/create_step.tsx @@ -19,6 +19,7 @@ import { CreateAnalyticsFormProps } from '../../../analytics_management/hooks/us import { Messages } from '../shared'; import { ANALYTICS_STEPS } from '../../page'; import { BackToListPanel } from '../back_to_list_panel'; +import { ProgressStats } from './progress_stats'; interface Props extends CreateAnalyticsFormProps { step: ANALYTICS_STEPS; @@ -27,8 +28,10 @@ interface Props extends CreateAnalyticsFormProps { export const CreateStep: FC = ({ actions, state, step }) => { const { createAnalyticsJob, startAnalyticsJob } = actions; const { isAdvancedEditorValidJson, isJobCreated, isJobStarted, isValid, requestMessages } = state; + const { jobId } = state.form; const [checked, setChecked] = useState(true); + const [showProgress, setShowProgress] = useState(false); if (step !== ANALYTICS_STEPS.CREATE) return null; @@ -36,6 +39,7 @@ export const CreateStep: FC = ({ actions, state, step }) => { await createAnalyticsJob(); if (checked) { + setShowProgress(true); startAnalyticsJob(); } }; @@ -82,6 +86,7 @@ export const CreateStep: FC = ({ actions, state, step }) => { )} + {isJobCreated === true && showProgress && } {isJobCreated === true && } ); diff --git a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_creation/components/create_step/progress_stats.tsx b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_creation/components/create_step/progress_stats.tsx new file mode 100644 index 00000000000000..8cee63d3c4c841 --- /dev/null +++ b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_creation/components/create_step/progress_stats.tsx @@ -0,0 +1,110 @@ +/* + * 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, { FC, useState, useEffect } from 'react'; +import { EuiFlexGroup, EuiFlexItem, EuiProgress, EuiSpacer, EuiText } from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; +import { useMlKibana } from '../../../../../contexts/kibana'; +import { getDataFrameAnalyticsProgressPhase } from '../../../analytics_management/components/analytics_list/common'; +import { isGetDataFrameAnalyticsStatsResponseOk } from '../../../analytics_management/services/analytics_service/get_analytics'; +import { ml } from '../../../../../services/ml_api_service'; +import { DataFrameAnalyticsId } from '../../../../common/analytics'; + +export const PROGRESS_REFRESH_INTERVAL_MS = 1000; + +export const ProgressStats: FC<{ jobId: DataFrameAnalyticsId }> = ({ jobId }) => { + const [initialized, setInitialized] = useState(false); + const [currentProgress, setCurrentProgress] = useState< + | { + currentPhase: number; + progress: number; + totalPhases: number; + } + | undefined + >(undefined); + + const { + services: { notifications }, + } = useMlKibana(); + + useEffect(() => { + setInitialized(true); + }, []); + + useEffect(() => { + const interval = setInterval(async () => { + try { + const analyticsStats = await ml.dataFrameAnalytics.getDataFrameAnalyticsStats(jobId); + const jobStats = isGetDataFrameAnalyticsStatsResponseOk(analyticsStats) + ? analyticsStats.data_frame_analytics[0] + : undefined; + + if (jobStats !== undefined) { + const progressStats = getDataFrameAnalyticsProgressPhase(jobStats); + setCurrentProgress(progressStats); + if ( + progressStats.currentPhase === progressStats.totalPhases && + progressStats.progress === 100 + ) { + clearInterval(interval); + } + } else { + clearInterval(interval); + } + } catch (e) { + notifications.toasts.addDanger( + i18n.translate('xpack.ml.dataframe.analytics.create.analyticsProgressErrorMessage', { + defaultMessage: 'An error occurred getting progress stats for analytics job {jobId}', + values: { jobId }, + }) + ); + clearInterval(interval); + } + }, PROGRESS_REFRESH_INTERVAL_MS); + + return () => clearInterval(interval); + }, [initialized]); + + if (currentProgress === undefined) return null; + + return ( + <> + + + + {i18n.translate('xpack.ml.dataframe.analytics.create.analyticsProgressTitle', { + defaultMessage: 'Progress', + })} + + + + + + + + {i18n.translate('xpack.ml.dataframe.analytics.create.analyticsProgressPhaseTitle', { + defaultMessage: 'Phase', + })}{' '} + {currentProgress.currentPhase}/{currentProgress.totalPhases} + + + + + + + + {`${currentProgress.progress}%`} + + + + ); +}; diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/job_creator.ts b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/job_creator.ts index 89a0c458287373..d8c4dab150fb51 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/job_creator.ts +++ b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/job_creator.ts @@ -155,7 +155,7 @@ export class JobCreator { } protected _setBucketSpanMs(bucketSpan: BucketSpan) { - const bs = parseInterval(bucketSpan); + const bs = parseInterval(bucketSpan, true); this._bucketSpanMs = bs === null ? 0 : bs.asMilliseconds(); } diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/single_metric_job_creator.ts b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/single_metric_job_creator.ts index febfc5ca3eb9e5..e884da5470cc5a 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/single_metric_job_creator.ts +++ b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/single_metric_job_creator.ts @@ -76,7 +76,7 @@ export class SingleMetricJobCreator extends JobCreator { const functionName = this._aggs[0].dslName; const timeField = this._job_config.data_description.time_field; - const duration = parseInterval(this._job_config.analysis_config.bucket_span); + const duration = parseInterval(this._job_config.analysis_config.bucket_span, true); if (duration === null) { return; } diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_validator/util.ts b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_validator/util.ts index d5cc1cf535a787..b97841542f76af 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_validator/util.ts +++ b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_validator/util.ts @@ -142,7 +142,7 @@ export function populateValidationMessages( basicValidations.bucketSpan.message = msg; } else if (validationResults.contains('bucket_span_invalid')) { basicValidations.bucketSpan.valid = false; - basicValidations.bucketSpan.message = invalidTimeFormatMessage( + basicValidations.bucketSpan.message = invalidTimeIntervalMessage( jobConfig.analysis_config.bucket_span ); } @@ -163,12 +163,12 @@ export function populateValidationMessages( if (validationResults.contains('query_delay_invalid')) { basicValidations.queryDelay.valid = false; - basicValidations.queryDelay.message = invalidTimeFormatMessage(datafeedConfig.query_delay); + basicValidations.queryDelay.message = invalidTimeIntervalMessage(datafeedConfig.query_delay); } if (validationResults.contains('frequency_invalid')) { basicValidations.frequency.valid = false; - basicValidations.frequency.message = invalidTimeFormatMessage(datafeedConfig.frequency); + basicValidations.frequency.message = invalidTimeIntervalMessage(datafeedConfig.frequency); } } @@ -202,16 +202,18 @@ export function checkForExistingJobAndGroupIds( }; } -function invalidTimeFormatMessage(value: string | undefined) { +function invalidTimeIntervalMessage(value: string | undefined) { return i18n.translate( 'xpack.ml.newJob.wizard.validateJob.frequencyInvalidTimeIntervalFormatErrorMessage', { defaultMessage: - '{value} is not a valid time interval format e.g. {tenMinutes}, {oneHour}. It also needs to be higher than zero.', + '{value} is not a valid time interval format e.g. {thirtySeconds}, {tenMinutes}, {oneHour}, {sevenDays}. It also needs to be higher than zero.', values: { value, + thirtySeconds: '30s', tenMinutes: '10m', oneHour: '1h', + sevenDays: '7d', }, } ); diff --git a/x-pack/plugins/ml/public/application/util/time_buckets.js b/x-pack/plugins/ml/public/application/util/time_buckets.js index 1915a4ce6516bc..19d499faf6c8dd 100644 --- a/x-pack/plugins/ml/public/application/util/time_buckets.js +++ b/x-pack/plugins/ml/public/application/util/time_buckets.js @@ -14,7 +14,11 @@ import { getFieldFormats, getUiSettings } from './dependency_cache'; import { FIELD_FORMAT_IDS, UI_SETTINGS } from '../../../../../../src/plugins/data/public'; const unitsDesc = dateMath.unitsDesc; -const largeMax = unitsDesc.indexOf('w'); // Multiple units of week or longer converted to days for ES intervals. + +// Index of the list of time interval units at which larger units (i.e. weeks, months, years) need +// need to be converted to multiples of the largest unit supported in ES aggregation intervals (i.e. days). +// Note that similarly the largest interval supported for ML bucket spans is 'd'. +const timeUnitsMaxSupportedIndex = unitsDesc.indexOf('w'); const calcAuto = timeBucketsCalcAutoIntervalProvider(); @@ -383,9 +387,11 @@ export function calcEsInterval(duration) { const val = duration.as(unit); // find a unit that rounds neatly if (val >= 1 && Math.floor(val) === val) { - // if the unit is "large", like years, but isn't set to 1, ES will throw an error. + // Apart from for date histograms, ES only supports time units up to 'd', + // meaning we can't for example use 'w' for job bucket spans. + // See https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#time-units // So keep going until we get out of the "large" units. - if (i <= largeMax && val !== 1) { + if (i <= timeUnitsMaxSupportedIndex) { continue; } diff --git a/x-pack/plugins/ml/public/application/util/time_buckets.test.js b/x-pack/plugins/ml/public/application/util/time_buckets.test.js index 250c7255f5b99f..6ebd518841bd1d 100644 --- a/x-pack/plugins/ml/public/application/util/time_buckets.test.js +++ b/x-pack/plugins/ml/public/application/util/time_buckets.test.js @@ -232,14 +232,14 @@ describe('ML - time buckets', () => { expression: '3d', }); expect(calcEsInterval(moment.duration(7, 'd'))).toEqual({ - value: 1, - unit: 'w', - expression: '1w', + value: 7, + unit: 'd', + expression: '7d', }); expect(calcEsInterval(moment.duration(1, 'w'))).toEqual({ - value: 1, - unit: 'w', - expression: '1w', + value: 7, + unit: 'd', + expression: '7d', }); expect(calcEsInterval(moment.duration(4, 'w'))).toEqual({ value: 28, @@ -247,19 +247,19 @@ describe('ML - time buckets', () => { expression: '28d', }); expect(calcEsInterval(moment.duration(1, 'M'))).toEqual({ - value: 1, - unit: 'M', - expression: '1M', + value: 30, + unit: 'd', + expression: '30d', }); expect(calcEsInterval(moment.duration(12, 'M'))).toEqual({ - value: 1, - unit: 'y', - expression: '1y', + value: 365, + unit: 'd', + expression: '365d', }); expect(calcEsInterval(moment.duration(1, 'y'))).toEqual({ - value: 1, - unit: 'y', - expression: '1y', + value: 365, + unit: 'd', + expression: '365d', }); }); }); diff --git a/x-pack/plugins/ml/public/plugin.ts b/x-pack/plugins/ml/public/plugin.ts index be2ebb3caa4161..7f7544a44efa7f 100644 --- a/x-pack/plugins/ml/public/plugin.ts +++ b/x-pack/plugins/ml/public/plugin.ts @@ -30,10 +30,12 @@ import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/public'; import { registerEmbeddables } from './embeddables'; import { UiActionsSetup } from '../../../../src/plugins/ui_actions/public'; import { registerMlUiActions } from './ui_actions'; +import { KibanaLegacyStart } from '../../../../src/plugins/kibana_legacy/public'; export interface MlStartDependencies { data: DataPublicPluginStart; share: SharePluginStart; + kibanaLegacy: KibanaLegacyStart; } export interface MlSetupDependencies { security?: SecurityPluginSetup; @@ -70,6 +72,7 @@ export class MlPlugin implements Plugin { { data: pluginsStart.data, share: pluginsStart.share, + kibanaLegacy: pluginsStart.kibanaLegacy, security: pluginsSetup.security, licensing: pluginsSetup.licensing, management: pluginsSetup.management, diff --git a/x-pack/plugins/ml/server/models/job_validation/job_validation.test.ts b/x-pack/plugins/ml/server/models/job_validation/job_validation.test.ts index f9999a06f38ed0..0aae4388e73992 100644 --- a/x-pack/plugins/ml/server/models/job_validation/job_validation.test.ts +++ b/x-pack/plugins/ml/server/models/job_validation/job_validation.test.ts @@ -133,11 +133,11 @@ describe('ML - validateJob', () => { }); }; it('invalid bucket span formats', () => { - const invalidBucketSpanFormats = ['a', '10', '$']; + const invalidBucketSpanFormats = ['a', '10', '$', '500ms', '1w', '2M', '1y']; return bucketSpanFormatTests(invalidBucketSpanFormats, 'bucket_span_invalid'); }); it('valid bucket span formats', () => { - const validBucketSpanFormats = ['1s', '4h', '10d', '6w', '2m', '3y']; + const validBucketSpanFormats = ['5000ms', '1s', '2m', '4h', '10d']; return bucketSpanFormatTests(validBucketSpanFormats, 'bucket_span_valid'); }); diff --git a/x-pack/plugins/ml/server/models/job_validation/validate_bucket_span.js b/x-pack/plugins/ml/server/models/job_validation/validate_bucket_span.js index 46d05d3cf76376..7dc2ad7ff3b8f3 100644 --- a/x-pack/plugins/ml/server/models/job_validation/validate_bucket_span.js +++ b/x-pack/plugins/ml/server/models/job_validation/validate_bucket_span.js @@ -5,9 +5,8 @@ */ import { estimateBucketSpanFactory } from '../../models/bucket_span_estimator'; -import { mlFunctionToESAggregation } from '../../../common/util/job_utils'; +import { mlFunctionToESAggregation, parseTimeIntervalForJob } from '../../../common/util/job_utils'; import { SKIP_BUCKET_SPAN_ESTIMATION } from '../../../common/constants/validation'; -import { parseInterval } from '../../../common/util/parse_interval'; import { validateJobObject } from './validate_job_object'; @@ -65,8 +64,11 @@ export async function validateBucketSpan( } const messages = []; - const parsedBucketSpan = parseInterval(job.analysis_config.bucket_span); - if (parsedBucketSpan === null || parsedBucketSpan.asMilliseconds() === 0) { + + // Bucket span must be a valid interval, greater than 0, + // and if specified in ms must be a multiple of 1000ms + const parsedBucketSpan = parseTimeIntervalForJob(job.analysis_config.bucket_span); + if (parsedBucketSpan === null) { messages.push({ id: 'bucket_span_invalid' }); return messages; } diff --git a/x-pack/plugins/ml/server/models/job_validation/validate_time_range.ts b/x-pack/plugins/ml/server/models/job_validation/validate_time_range.ts index be6c9a7157aeb6..f60ca66b092f91 100644 --- a/x-pack/plugins/ml/server/models/job_validation/validate_time_range.ts +++ b/x-pack/plugins/ml/server/models/job_validation/validate_time_range.ts @@ -78,7 +78,7 @@ export async function validateTimeRange( } // check for minimum time range (25 buckets or 2 hours, whichever is longer) - const interval = parseInterval(job.analysis_config.bucket_span); + const interval = parseInterval(job.analysis_config.bucket_span, true); if (interval === null) { messages.push({ id: 'bucket_span_invalid' }); } else { diff --git a/x-pack/plugins/monitoring/public/angular/index.ts b/x-pack/plugins/monitoring/public/angular/index.ts index 3f2a51a898d1f3..69d97a5e3bdc35 100644 --- a/x-pack/plugins/monitoring/public/angular/index.ts +++ b/x-pack/plugins/monitoring/public/angular/index.ts @@ -25,12 +25,22 @@ export class AngularApp { isCloud, pluginInitializerContext, externalConfig, + kibanaLegacy, } = deps; const app: IModule = localAppModule(deps); app.run(($injector: angular.auto.IInjectorService) => { this.injector = $injector; Legacy.init( - { core, element, data, navigation, isCloud, pluginInitializerContext, externalConfig }, + { + core, + element, + data, + navigation, + isCloud, + pluginInitializerContext, + externalConfig, + kibanaLegacy, + }, this.injector ); }); diff --git a/x-pack/plugins/monitoring/public/components/logstash/listing/listing.js b/x-pack/plugins/monitoring/public/components/logstash/listing/listing.js index 8e2c43e44ee118..78eb982a95dd7e 100644 --- a/x-pack/plugins/monitoring/public/components/logstash/listing/listing.js +++ b/x-pack/plugins/monitoring/public/components/logstash/listing/listing.js @@ -62,7 +62,7 @@ export class Listing extends PureComponent { return (
- + {name}
diff --git a/x-pack/plugins/monitoring/public/components/logstash/pipeline_listing/pipeline_listing.js b/x-pack/plugins/monitoring/public/components/logstash/pipeline_listing/pipeline_listing.js index 1b22bc6823bb83..4cacf91913ab94 100644 --- a/x-pack/plugins/monitoring/public/components/logstash/pipeline_listing/pipeline_listing.js +++ b/x-pack/plugins/monitoring/public/components/logstash/pipeline_listing/pipeline_listing.js @@ -46,7 +46,7 @@ export class PipelineListing extends Component { field: 'id', sortable: true, render: (id) => ( - + {id} ), diff --git a/x-pack/plugins/monitoring/public/directives/elasticsearch/ml_job_listing/index.js b/x-pack/plugins/monitoring/public/directives/elasticsearch/ml_job_listing/index.js index bef0fce4cd088b..ec325673ddfda0 100644 --- a/x-pack/plugins/monitoring/public/directives/elasticsearch/ml_job_listing/index.js +++ b/x-pack/plugins/monitoring/public/directives/elasticsearch/ml_job_listing/index.js @@ -72,7 +72,9 @@ const getColumns = () => [ render: (name, node) => { if (node) { return ( - {name} + + {name} + ); } diff --git a/x-pack/plugins/monitoring/public/directives/main/index.js b/x-pack/plugins/monitoring/public/directives/main/index.js index 97ec66c9b3415b..eda32cd39c0d0b 100644 --- a/x-pack/plugins/monitoring/public/directives/main/index.js +++ b/x-pack/plugins/monitoring/public/directives/main/index.js @@ -133,7 +133,7 @@ export class MonitoringMainController { this.pipelineHashShort = shortenPipelineHash(this.pipelineHash); this.onChangePipelineHash = () => { window.location.hash = getSafeForExternalLink( - `/logstash/pipelines/${this.pipelineId}/${this.pipelineHash}` + `#/logstash/pipelines/${this.pipelineId}/${this.pipelineHash}` ); }; } diff --git a/x-pack/plugins/monitoring/public/plugin.ts b/x-pack/plugins/monitoring/public/plugin.ts index 24383028e558c9..de8c8d59b78bff 100644 --- a/x-pack/plugins/monitoring/public/plugin.ts +++ b/x-pack/plugins/monitoring/public/plugin.ts @@ -70,6 +70,7 @@ export class MonitoringPlugin const { AngularApp } = await import('./angular'); const deps: MonitoringPluginDependencies = { navigation: pluginsStart.navigation, + kibanaLegacy: pluginsStart.kibanaLegacy, element: params.element, core: coreStart, data: pluginsStart.data, @@ -78,6 +79,7 @@ export class MonitoringPlugin externalConfig: this.getExternalConfig(), }; + pluginsStart.kibanaLegacy.loadFontAwesome(); this.setInitialTimefilter(deps); this.overrideAlertingEmailDefaults(deps); diff --git a/x-pack/plugins/monitoring/public/types.ts b/x-pack/plugins/monitoring/public/types.ts index b8c854f4e7ee0d..6266755a041206 100644 --- a/x-pack/plugins/monitoring/public/types.ts +++ b/x-pack/plugins/monitoring/public/types.ts @@ -7,6 +7,7 @@ import { PluginInitializerContext, CoreStart } from 'kibana/public'; import { NavigationPublicPluginStart as NavigationStart } from '../../../../src/plugins/navigation/public'; import { DataPublicPluginStart } from '../../../../src/plugins/data/public'; +import { KibanaLegacyStart } from '../../../../src/plugins/kibana_legacy/public'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths export { MonitoringConfig } from '../server'; @@ -14,6 +15,7 @@ export { MonitoringConfig } from '../server'; export interface MonitoringPluginDependencies { navigation: NavigationStart; data: DataPublicPluginStart; + kibanaLegacy: KibanaLegacyStart; element: HTMLElement; core: CoreStart; isCloud: boolean; diff --git a/x-pack/plugins/observability/public/pages/home/index.tsx b/x-pack/plugins/observability/public/pages/home/index.tsx index 696361393ef82d..91e7e2759b8244 100644 --- a/x-pack/plugins/observability/public/pages/home/index.tsx +++ b/x-pack/plugins/observability/public/pages/home/index.tsx @@ -92,7 +92,7 @@ export const Home = () => {

{i18n.translate('xpack.observability.home.sectionTitle', { - defaultMessage: 'Observability built on the Elastic Stack', + defaultMessage: 'Unified visibility across your entire ecosystem', })}

@@ -100,7 +100,7 @@ export const Home = () => { {i18n.translate('xpack.observability.home.sectionsubtitle', { defaultMessage: - 'Bring your logs, metrics, and APM traces together at scale in a single stack so you can monitor and react to events happening anywhere in your environment.', + 'Monitor, analyze, and react to events happening anywhere in your environment by bringing logs, metrics, and traces together at scale in a single stack.', })} diff --git a/x-pack/plugins/observability/public/pages/home/section.ts b/x-pack/plugins/observability/public/pages/home/section.ts index a2b82c31bf2ab0..d33571a16ccb75 100644 --- a/x-pack/plugins/observability/public/pages/home/section.ts +++ b/x-pack/plugins/observability/public/pages/home/section.ts @@ -23,7 +23,7 @@ export const appsSection: ISection[] = [ icon: 'logoLogging', description: i18n.translate('xpack.observability.section.apps.logs.description', { defaultMessage: - 'The Elastic Stack (sometimes known as the ELK Stack) is the most popular open source logging platform.', + 'Centralize logs from any source. Search, tail, automate anomaly detection, and visualize trends so you can take action quicker.', }), }, { @@ -34,7 +34,7 @@ export const appsSection: ISection[] = [ icon: 'logoAPM', description: i18n.translate('xpack.observability.section.apps.apm.description', { defaultMessage: - 'See exactly where your application is spending time so you can quickly fix issues and feel good about the code you push.', + 'Trace transactions through a distributed architecture and map your services’ interactions to easily spot performance bottlenecks.', }), }, { @@ -45,7 +45,7 @@ export const appsSection: ISection[] = [ icon: 'logoMetrics', description: i18n.translate('xpack.observability.section.apps.metrics.description', { defaultMessage: - 'Already using the Elastic Stack for logs? Add metrics in just a few steps and correlate metrics and logs in one place.', + 'Analyze metrics from your infrastructure, apps, and services. Discover trends, forecast behavior, get alerts on anomalies, and more.', }), }, { @@ -56,7 +56,7 @@ export const appsSection: ISection[] = [ icon: 'logoUptime', description: i18n.translate('xpack.observability.section.apps.uptime.description', { defaultMessage: - 'React to availability issues across your apps and services before they affect users.', + 'Proactively monitor the availability of your sites and services. Receive alerts and resolve issues faster to optimize your users’ experience.', }), }, ]; diff --git a/x-pack/plugins/observability/public/typings/fetch_data_response/index.d.ts b/x-pack/plugins/observability/public/typings/fetch_data_response/index.d.ts index 30ecb24a58a5a5..06e86d1096cfc4 100644 --- a/x-pack/plugins/observability/public/typings/fetch_data_response/index.d.ts +++ b/x-pack/plugins/observability/public/typings/fetch_data_response/index.d.ts @@ -4,17 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -interface Percentage { - label: string; - pct: number; - color?: string; -} -interface Bytes { - label: string; - bytes: number; - color?: string; -} -interface Numeral { +interface Stat { + type: 'number' | 'percent' | 'bytesPerSecond'; label: string; value: number; color?: string; @@ -37,18 +28,18 @@ export interface FetchDataResponse { } export interface LogsFetchDataResponse extends FetchDataResponse { - stats: Record; + stats: Record; series: Record; } export interface MetricsFetchDataResponse extends FetchDataResponse { stats: { - hosts: Numeral; - cpu: Percentage; - memory: Percentage; - disk: Percentage; - inboundTraffic: Bytes; - outboundTraffic: Bytes; + hosts: Stat; + cpu: Stat; + memory: Stat; + disk: Stat; + inboundTraffic: Stat; + outboundTraffic: Stat; }; series: { inboundTraffic: Series; @@ -58,9 +49,9 @@ export interface MetricsFetchDataResponse extends FetchDataResponse { export interface UptimeFetchDataResponse extends FetchDataResponse { stats: { - monitors: Numeral; - up: Numeral; - down: Numeral; + monitors: Stat; + up: Stat; + down: Stat; }; series: { up: Series; @@ -70,8 +61,8 @@ export interface UptimeFetchDataResponse extends FetchDataResponse { export interface ApmFetchDataResponse extends FetchDataResponse { stats: { - services: Numeral; - transactions: Numeral; + services: Stat; + transactions: Stat; }; series: { transactions: Series; diff --git a/x-pack/plugins/reporting/server/config/schema.test.ts b/x-pack/plugins/reporting/server/config/schema.test.ts index 41285c2bfa133a..ddd5491b661bc4 100644 --- a/x-pack/plugins/reporting/server/config/schema.test.ts +++ b/x-pack/plugins/reporting/server/config/schema.test.ts @@ -112,6 +112,8 @@ describe('Reporting Config Schema', () => { .encryptionKey ).toBe('qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq'); + expect(ConfigSchema.validate({ encryptionKey: 'weaksauce' }).encryptionKey).toBe('weaksauce'); + // disableSandbox expect( ConfigSchema.validate({ capture: { browser: { chromium: { disableSandbox: true } } } }) diff --git a/x-pack/plugins/reporting/server/config/schema.ts b/x-pack/plugins/reporting/server/config/schema.ts index b1234a6ddf0b66..2f77aff0020d53 100644 --- a/x-pack/plugins/reporting/server/config/schema.ts +++ b/x-pack/plugins/reporting/server/config/schema.ts @@ -136,8 +136,8 @@ const CsvSchema = schema.object({ const EncryptionKeySchema = schema.conditional( schema.contextRef('dist'), true, - schema.maybe(schema.string({ minLength: 32 })), // default value is dynamic in createConfig$ - schema.string({ minLength: 32, defaultValue: 'a'.repeat(32) }) + schema.maybe(schema.string()), // default value is dynamic in createConfig$ + schema.string({ defaultValue: 'a'.repeat(32) }) ); const RolesSchema = schema.object({ diff --git a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/observable.test.ts b/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/observable.test.ts index 2ddb4a5d5b9943..b00233137943de 100644 --- a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/observable.test.ts +++ b/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/observable.test.ts @@ -17,7 +17,7 @@ jest.mock('../../../../browsers/chromium/puppeteer', () => ({ import * as Rx from 'rxjs'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { loggingServiceMock } from '../../../../../../../../src/core/server/mocks'; +import { loggingSystemMock } from '../../../../../../../../src/core/server/mocks'; import { HeadlessChromiumDriver } from '../../../../browsers'; import { LevelLogger } from '../../../../lib'; import { createMockBrowserDriverFactory, createMockLayoutInstance } from '../../../../test_helpers'; @@ -28,7 +28,7 @@ import { screenshotsObservableFactory } from './observable'; /* * Mocks */ -const mockLogger = jest.fn(loggingServiceMock.create); +const mockLogger = jest.fn(loggingSystemMock.create); const logger = new LevelLogger(mockLogger()); const mockConfig = { timeouts: { openUrl: 13 } } as CaptureConfig; diff --git a/x-pack/plugins/security/common/is_internal_url.test.ts b/x-pack/plugins/security/common/is_internal_url.test.ts new file mode 100644 index 00000000000000..7e9f63f069fd05 --- /dev/null +++ b/x-pack/plugins/security/common/is_internal_url.test.ts @@ -0,0 +1,88 @@ +/* + * 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 { isInternalURL } from './is_internal_url'; + +describe('isInternalURL', () => { + describe('with basePath defined', () => { + const basePath = '/iqf'; + + it('should return `true `if URL includes hash fragment', () => { + const href = `${basePath}/app/kibana#/discover/New-Saved-Search`; + expect(isInternalURL(href, basePath)).toBe(true); + }); + + it('should return `false` if URL includes a protocol/hostname', () => { + const href = `https://example.com${basePath}/app/kibana`; + expect(isInternalURL(href, basePath)).toBe(false); + }); + + it('should return `false` if URL includes a port', () => { + const href = `http://localhost:5601${basePath}/app/kibana`; + expect(isInternalURL(href, basePath)).toBe(false); + }); + + it('should return `false` if URL does not specify protocol', () => { + const hrefWithTwoSlashes = `/${basePath}/app/kibana`; + expect(isInternalURL(hrefWithTwoSlashes)).toBe(false); + + const hrefWithThreeSlashes = `//${basePath}/app/kibana`; + expect(isInternalURL(hrefWithThreeSlashes)).toBe(false); + }); + + it('should return `true` if URL starts with a basepath', () => { + for (const href of [basePath, `${basePath}/`, `${basePath}/login`, `${basePath}/login/`]) { + expect(isInternalURL(href, basePath)).toBe(true); + } + }); + + it('should return `false` if URL does not start with basePath', () => { + for (const href of [ + '/notbasepath/app/kibana', + `${basePath}_/login`, + basePath.slice(1), + `${basePath.slice(1)}/app/kibana`, + ]) { + expect(isInternalURL(href, basePath)).toBe(false); + } + }); + + it('should return `true` if relative path does not escape base path', () => { + const href = `${basePath}/app/kibana/../../management`; + expect(isInternalURL(href, basePath)).toBe(true); + }); + + it('should return `false` if relative path escapes base path', () => { + const href = `${basePath}/app/kibana/../../../management`; + expect(isInternalURL(href, basePath)).toBe(false); + }); + }); + + describe('without basePath defined', () => { + it('should return `true `if URL includes hash fragment', () => { + const href = '/app/kibana#/discover/New-Saved-Search'; + expect(isInternalURL(href)).toBe(true); + }); + + it('should return `false` if URL includes a protocol/hostname', () => { + const href = 'https://example.com/app/kibana'; + expect(isInternalURL(href)).toBe(false); + }); + + it('should return `false` if URL includes a port', () => { + const href = 'http://localhost:5601/app/kibana'; + expect(isInternalURL(href)).toBe(false); + }); + + it('should return `false` if URL does not specify protocol', () => { + const hrefWithTwoSlashes = `//app/kibana`; + expect(isInternalURL(hrefWithTwoSlashes)).toBe(false); + + const hrefWithThreeSlashes = `///app/kibana`; + expect(isInternalURL(hrefWithThreeSlashes)).toBe(false); + }); + }); +}); diff --git a/x-pack/plugins/security/common/is_internal_url.ts b/x-pack/plugins/security/common/is_internal_url.ts new file mode 100644 index 00000000000000..e83839bf7d34bc --- /dev/null +++ b/x-pack/plugins/security/common/is_internal_url.ts @@ -0,0 +1,38 @@ +/* + * 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 { parse } from 'url'; + +export function isInternalURL(url: string, basePath = '') { + const { protocol, hostname, port, pathname } = parse( + url, + false /* parseQueryString */, + true /* slashesDenoteHost */ + ); + + // We should explicitly compare `protocol`, `port` and `hostname` to null to make sure these are not + // detected in the URL at all. For example `hostname` can be empty string for Node URL parser, but + // browser (because of various bwc reasons) processes URL differently (e.g. `///abc.com` - for browser + // hostname is `abc.com`, but for Node hostname is an empty string i.e. everything between schema (`//`) + // and the first slash that belongs to path. + if (protocol !== null || hostname !== null || port !== null) { + return false; + } + + if (basePath) { + // Now we need to normalize URL to make sure any relative path segments (`..`) cannot escape expected + // base path. We can rely on `URL` with a localhost to automatically "normalize" the URL. + const normalizedPathname = new URL(String(pathname), 'https://localhost').pathname; + return ( + // Normalized pathname can add a leading slash, but we should also make sure it's included in + // the original URL too + pathname?.startsWith('/') && + (normalizedPathname === basePath || normalizedPathname.startsWith(`${basePath}/`)) + ); + } + + return true; +} diff --git a/x-pack/plugins/security/common/parse_next.ts b/x-pack/plugins/security/common/parse_next.ts index 7cbe335825a5a7..7ce0de05ad526d 100644 --- a/x-pack/plugins/security/common/parse_next.ts +++ b/x-pack/plugins/security/common/parse_next.ts @@ -5,6 +5,7 @@ */ import { parse } from 'url'; +import { isInternalURL } from './is_internal_url'; export function parseNext(href: string, basePath = '') { const { query, hash } = parse(href, true); @@ -20,23 +21,8 @@ export function parseNext(href: string, basePath = '') { } // validate that `next` is not attempting a redirect to somewhere - // outside of this Kibana install - const { protocol, hostname, port, pathname } = parse( - next, - false /* parseQueryString */, - true /* slashesDenoteHost */ - ); - - // We should explicitly compare `protocol`, `port` and `hostname` to null to make sure these are not - // detected in the URL at all. For example `hostname` can be empty string for Node URL parser, but - // browser (because of various bwc reasons) processes URL differently (e.g. `///abc.com` - for browser - // hostname is `abc.com`, but for Node hostname is an empty string i.e. everything between schema (`//`) - // and the first slash that belongs to path. - if (protocol !== null || hostname !== null || port !== null) { - return `${basePath}/`; - } - - if (!String(pathname).startsWith(basePath)) { + // outside of this Kibana install. + if (!isInternalURL(next, basePath)) { return `${basePath}/`; } diff --git a/x-pack/plugins/security/server/audit/audit_service.test.ts b/x-pack/plugins/security/server/audit/audit_service.test.ts index 94a2ada8df1da9..b2d866d07ff891 100644 --- a/x-pack/plugins/security/server/audit/audit_service.test.ts +++ b/x-pack/plugins/security/server/audit/audit_service.test.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ import { AuditService } from './audit_service'; -import { loggingServiceMock } from 'src/core/server/mocks'; +import { loggingSystemMock } from 'src/core/server/mocks'; import { licenseMock } from '../../common/licensing/index.mock'; import { ConfigSchema, ConfigType } from '../config'; import { SecurityLicenseFeatures } from '../../common/licensing'; @@ -20,7 +20,7 @@ const config = createConfig({ describe('#setup', () => { it('returns the expected contract', () => { - const logger = loggingServiceMock.createLogger(); + const logger = loggingSystemMock.createLogger(); const auditService = new AuditService(logger); const license = licenseMock.create(); expect(auditService.setup({ license, config })).toMatchInlineSnapshot(` @@ -34,7 +34,7 @@ describe('#setup', () => { test(`calls the underlying logger with the provided message and requisite tags`, () => { const pluginId = 'foo'; - const logger = loggingServiceMock.createLogger(); + const logger = loggingSystemMock.createLogger(); const license = licenseMock.create(); license.features$ = new BehaviorSubject({ allowAuditLogging: true, @@ -58,7 +58,7 @@ test(`calls the underlying logger with the provided message and requisite tags`, test(`calls the underlying logger with the provided metadata`, () => { const pluginId = 'foo'; - const logger = loggingServiceMock.createLogger(); + const logger = loggingSystemMock.createLogger(); const license = licenseMock.create(); license.features$ = new BehaviorSubject({ allowAuditLogging: true, @@ -90,7 +90,7 @@ test(`calls the underlying logger with the provided metadata`, () => { test(`does not call the underlying logger if license does not support audit logging`, () => { const pluginId = 'foo'; - const logger = loggingServiceMock.createLogger(); + const logger = loggingSystemMock.createLogger(); const license = licenseMock.create(); license.features$ = new BehaviorSubject({ allowAuditLogging: false, @@ -110,7 +110,7 @@ test(`does not call the underlying logger if license does not support audit logg test(`does not call the underlying logger if security audit logging is not enabled`, () => { const pluginId = 'foo'; - const logger = loggingServiceMock.createLogger(); + const logger = loggingSystemMock.createLogger(); const license = licenseMock.create(); license.features$ = new BehaviorSubject({ allowAuditLogging: true, @@ -135,7 +135,7 @@ test(`does not call the underlying logger if security audit logging is not enabl test(`calls the underlying logger after license upgrade`, () => { const pluginId = 'foo'; - const logger = loggingServiceMock.createLogger(); + const logger = loggingSystemMock.createLogger(); const license = licenseMock.create(); const features$ = new BehaviorSubject({ diff --git a/x-pack/plugins/security/server/authentication/api_keys.test.ts b/x-pack/plugins/security/server/authentication/api_keys.test.ts index 9f2a628b575d57..ad55f15545bd9f 100644 --- a/x-pack/plugins/security/server/authentication/api_keys.test.ts +++ b/x-pack/plugins/security/server/authentication/api_keys.test.ts @@ -10,7 +10,7 @@ import { APIKeys } from './api_keys'; import { httpServerMock, - loggingServiceMock, + loggingSystemMock, elasticsearchServiceMock, } from '../../../../../src/core/server/mocks'; import { licenseMock } from '../../common/licensing/index.mock'; @@ -35,7 +35,7 @@ describe('API Keys', () => { apiKeys = new APIKeys({ clusterClient: mockClusterClient, - logger: loggingServiceMock.create().get('api-keys'), + logger: loggingSystemMock.create().get('api-keys'), license: mockLicense, }); }); diff --git a/x-pack/plugins/security/server/authentication/authenticator.test.ts b/x-pack/plugins/security/server/authentication/authenticator.test.ts index 60d0521a2947e7..3b77ea32481731 100644 --- a/x-pack/plugins/security/server/authentication/authenticator.test.ts +++ b/x-pack/plugins/security/server/authentication/authenticator.test.ts @@ -14,7 +14,7 @@ import { duration, Duration } from 'moment'; import { SessionStorage } from '../../../../../src/core/server'; import { - loggingServiceMock, + loggingSystemMock, httpServiceMock, httpServerMock, elasticsearchServiceMock, @@ -48,10 +48,10 @@ function getMockOptions({ clusterClient: elasticsearchServiceMock.createClusterClient(), basePath: httpServiceMock.createSetupContract().basePath, license: licenseMock.create(), - loggers: loggingServiceMock.create(), + loggers: loggingSystemMock.create(), config: createConfig( ConfigSchema.validate({ session, authc: { selector, providers, http } }), - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), { isTLSEnabled: false } ), sessionStorageFactory: sessionStorageMock.createFactory(), @@ -112,6 +112,33 @@ describe('Authenticator', () => { ).toThrowError('Provider name "__http__" is reserved.'); }); + it('properly sets `loggedOut` URL.', () => { + const basicAuthenticationProviderMock = jest.requireMock('./providers/basic') + .BasicAuthenticationProvider; + + basicAuthenticationProviderMock.mockClear(); + new Authenticator(getMockOptions()); + expect(basicAuthenticationProviderMock).toHaveBeenCalledWith( + expect.objectContaining({ + urls: { + loggedOut: '/mock-server-basepath/security/logged_out', + }, + }), + expect.anything() + ); + + basicAuthenticationProviderMock.mockClear(); + new Authenticator(getMockOptions({ selector: { enabled: true } })); + expect(basicAuthenticationProviderMock).toHaveBeenCalledWith( + expect.objectContaining({ + urls: { + loggedOut: `/mock-server-basepath/login?msg=LOGGED_OUT`, + }, + }), + expect.anything() + ); + }); + describe('HTTP authentication provider', () => { beforeEach(() => { jest diff --git a/x-pack/plugins/security/server/authentication/authenticator.ts b/x-pack/plugins/security/server/authentication/authenticator.ts index ac5c2a72b9667c..70f4063878aa89 100644 --- a/x-pack/plugins/security/server/authentication/authenticator.ts +++ b/x-pack/plugins/security/server/authentication/authenticator.ts @@ -242,6 +242,11 @@ export class Authenticator { client: this.options.clusterClient, logger: this.options.loggers.get('tokens'), }), + urls: { + loggedOut: options.config.authc.selector.enabled + ? `${options.basePath.serverBasePath}/login?msg=LOGGED_OUT` + : `${options.basePath.serverBasePath}/security/logged_out`, + }, }; this.providers = new Map( diff --git a/x-pack/plugins/security/server/authentication/index.test.ts b/x-pack/plugins/security/server/authentication/index.test.ts index c7323509c00d68..0acd4fa7fae406 100644 --- a/x-pack/plugins/security/server/authentication/index.test.ts +++ b/x-pack/plugins/security/server/authentication/index.test.ts @@ -12,7 +12,7 @@ jest.mock('./authenticator'); import Boom from 'boom'; import { - loggingServiceMock, + loggingSystemMock, coreMock, httpServerMock, httpServiceMock, @@ -66,12 +66,12 @@ describe('setupAuthentication()', () => { secureCookies: true, cookieName: 'my-sid-cookie', }), - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), { isTLSEnabled: false } ), clusterClient: elasticsearchServiceMock.createClusterClient(), license: licenseMock.create(), - loggers: loggingServiceMock.create(), + loggers: loggingSystemMock.create(), getFeatureUsageService: jest .fn() .mockReturnValue(securityFeatureUsageServiceMock.createStartContract()), @@ -221,7 +221,7 @@ describe('setupAuthentication()', () => { expect(mockAuthToolkit.authenticated).not.toHaveBeenCalled(); expect(mockAuthToolkit.redirected).not.toHaveBeenCalled(); - expect(loggingServiceMock.collect(mockSetupAuthenticationParams.loggers).error) + expect(loggingSystemMock.collect(mockSetupAuthenticationParams.loggers).error) .toMatchInlineSnapshot(` Array [ Array [ diff --git a/x-pack/plugins/security/server/authentication/providers/base.mock.ts b/x-pack/plugins/security/server/authentication/providers/base.mock.ts index 1dcd2885f66dc9..7c71348bb8ca0f 100644 --- a/x-pack/plugins/security/server/authentication/providers/base.mock.ts +++ b/x-pack/plugins/security/server/authentication/providers/base.mock.ts @@ -5,7 +5,7 @@ */ import { - loggingServiceMock, + loggingSystemMock, httpServiceMock, elasticsearchServiceMock, } from '../../../../../../src/core/server/mocks'; @@ -15,14 +15,14 @@ export type MockAuthenticationProviderOptions = ReturnType< >; export function mockAuthenticationProviderOptions(options?: { name: string }) { - const basePath = httpServiceMock.createSetupContract().basePath; - basePath.get.mockReturnValue('/base-path'); - return { client: elasticsearchServiceMock.createClusterClient(), - logger: loggingServiceMock.create().get(), - basePath, + logger: loggingSystemMock.create().get(), + basePath: httpServiceMock.createBasePath(), tokens: { refresh: jest.fn(), invalidate: jest.fn() }, name: options?.name ?? 'basic1', + urls: { + loggedOut: '/mock-server-basepath/security/logged_out', + }, }; } diff --git a/x-pack/plugins/security/server/authentication/providers/base.ts b/x-pack/plugins/security/server/authentication/providers/base.ts index d2d2e82951a3e8..32ea41802d31bb 100644 --- a/x-pack/plugins/security/server/authentication/providers/base.ts +++ b/x-pack/plugins/security/server/authentication/providers/base.ts @@ -26,6 +26,9 @@ export interface AuthenticationProviderOptions { client: IClusterClient; logger: Logger; tokens: PublicMethodsOf; + urls: { + loggedOut: string; + }; } /** diff --git a/x-pack/plugins/security/server/authentication/providers/basic.test.ts b/x-pack/plugins/security/server/authentication/providers/basic.test.ts index 97ca4e46d3eb59..ee6a12e36df05f 100644 --- a/x-pack/plugins/security/server/authentication/providers/basic.test.ts +++ b/x-pack/plugins/security/server/authentication/providers/basic.test.ts @@ -107,7 +107,7 @@ describe('BasicAuthenticationProvider', () => { ) ).resolves.toEqual( AuthenticationResult.redirectTo( - '/base-path/login?next=%2Fbase-path%2Fs%2Ffoo%2Fsome-path%20%23%20that%20needs%20to%20be%20encoded' + '/mock-server-basepath/login?next=%2Fmock-server-basepath%2Fs%2Ffoo%2Fsome-path%20%23%20that%20needs%20to%20be%20encoded' ) ); }); @@ -186,7 +186,7 @@ describe('BasicAuthenticationProvider', () => { it('always redirects to the login page.', async () => { await expect(provider.logout(httpServerMock.createKibanaRequest(), {})).resolves.toEqual( - DeauthenticationResult.redirectTo('/base-path/login?msg=LOGGED_OUT') + DeauthenticationResult.redirectTo('/mock-server-basepath/login?msg=LOGGED_OUT') ); }); @@ -199,7 +199,9 @@ describe('BasicAuthenticationProvider', () => { {} ) ).resolves.toEqual( - DeauthenticationResult.redirectTo('/base-path/login?next=%2Fapp%2Fml&msg=SESSION_EXPIRED') + DeauthenticationResult.redirectTo( + '/mock-server-basepath/login?next=%2Fapp%2Fml&msg=SESSION_EXPIRED' + ) ); }); }); diff --git a/x-pack/plugins/security/server/authentication/providers/kerberos.test.ts b/x-pack/plugins/security/server/authentication/providers/kerberos.test.ts index ca80761ee140c5..ebf1341127e5f0 100644 --- a/x-pack/plugins/security/server/authentication/providers/kerberos.test.ts +++ b/x-pack/plugins/security/server/authentication/providers/kerberos.test.ts @@ -518,7 +518,7 @@ describe('KerberosAuthenticationProvider', () => { expect(mockOptions.tokens.invalidate).toHaveBeenCalledWith(tokenPair); }); - it('redirects to `/logged_out` page if tokens are invalidated successfully.', async () => { + it('redirects to `loggedOut` URL if tokens are invalidated successfully.', async () => { const request = httpServerMock.createKibanaRequest(); const tokenPair = { accessToken: 'some-valid-token', @@ -528,7 +528,7 @@ describe('KerberosAuthenticationProvider', () => { mockOptions.tokens.invalidate.mockResolvedValue(undefined); await expect(provider.logout(request, tokenPair)).resolves.toEqual( - DeauthenticationResult.redirectTo('/mock-server-basepath/security/logged_out') + DeauthenticationResult.redirectTo(mockOptions.urls.loggedOut) ); expect(mockOptions.tokens.invalidate).toHaveBeenCalledTimes(1); diff --git a/x-pack/plugins/security/server/authentication/providers/kerberos.ts b/x-pack/plugins/security/server/authentication/providers/kerberos.ts index 2540c21210bd50..66a0ce22d3d19f 100644 --- a/x-pack/plugins/security/server/authentication/providers/kerberos.ts +++ b/x-pack/plugins/security/server/authentication/providers/kerberos.ts @@ -114,9 +114,7 @@ export class KerberosAuthenticationProvider extends BaseAuthenticationProvider { return DeauthenticationResult.failed(err); } - return DeauthenticationResult.redirectTo( - `${this.options.basePath.serverBasePath}/security/logged_out` - ); + return DeauthenticationResult.redirectTo(this.options.urls.loggedOut); } /** diff --git a/x-pack/plugins/security/server/authentication/providers/oidc.test.ts b/x-pack/plugins/security/server/authentication/providers/oidc.test.ts index 2d42d90ab60b8e..74344e8ae3edad 100644 --- a/x-pack/plugins/security/server/authentication/providers/oidc.test.ts +++ b/x-pack/plugins/security/server/authentication/providers/oidc.test.ts @@ -353,7 +353,7 @@ describe('OIDCAuthenticationProvider', () => { state: { state: 'statevalue', nonce: 'noncevalue', - nextURL: '/base-path/s/foo/some-path', + nextURL: '/mock-server-basepath/s/foo/some-path', realm: 'oidc1', }, } @@ -575,7 +575,7 @@ describe('OIDCAuthenticationProvider', () => { state: { state: 'statevalue', nonce: 'noncevalue', - nextURL: '/base-path/s/foo/some-path', + nextURL: '/mock-server-basepath/s/foo/some-path', realm: 'oidc1', }, } @@ -702,7 +702,7 @@ describe('OIDCAuthenticationProvider', () => { }); }); - it('redirects to /logged_out if `redirect` field in OpenID Connect logout response is null.', async () => { + it('redirects to `loggedOut` URL if `redirect` field in OpenID Connect logout response is null.', async () => { const request = httpServerMock.createKibanaRequest(); const accessToken = 'x-oidc-token'; const refreshToken = 'x-oidc-refresh-token'; @@ -711,9 +711,7 @@ describe('OIDCAuthenticationProvider', () => { await expect( provider.logout(request, { accessToken, refreshToken, realm: 'oidc1' }) - ).resolves.toEqual( - DeauthenticationResult.redirectTo('/mock-server-basepath/security/logged_out') - ); + ).resolves.toEqual(DeauthenticationResult.redirectTo(mockOptions.urls.loggedOut)); expect(mockOptions.client.callAsInternalUser).toHaveBeenCalledTimes(1); expect(mockOptions.client.callAsInternalUser).toHaveBeenCalledWith('shield.oidcLogout', { diff --git a/x-pack/plugins/security/server/authentication/providers/oidc.ts b/x-pack/plugins/security/server/authentication/providers/oidc.ts index f8e6ac0f9b5d08..ac7374401f99a1 100644 --- a/x-pack/plugins/security/server/authentication/providers/oidc.ts +++ b/x-pack/plugins/security/server/authentication/providers/oidc.ts @@ -433,9 +433,7 @@ export class OIDCAuthenticationProvider extends BaseAuthenticationProvider { return DeauthenticationResult.redirectTo(redirect); } - return DeauthenticationResult.redirectTo( - `${this.options.basePath.serverBasePath}/security/logged_out` - ); + return DeauthenticationResult.redirectTo(this.options.urls.loggedOut); } catch (err) { this.logger.debug(`Failed to deauthenticate user: ${err.message}`); return DeauthenticationResult.failed(err); diff --git a/x-pack/plugins/security/server/authentication/providers/pki.test.ts b/x-pack/plugins/security/server/authentication/providers/pki.test.ts index 28db64edd9e328..a1279c9b9ca7f8 100644 --- a/x-pack/plugins/security/server/authentication/providers/pki.test.ts +++ b/x-pack/plugins/security/server/authentication/providers/pki.test.ts @@ -547,14 +547,14 @@ describe('PKIAuthenticationProvider', () => { expect(mockOptions.tokens.invalidate).toHaveBeenCalledWith({ accessToken: 'foo' }); }); - it('redirects to `/logged_out` page if access token is invalidated successfully.', async () => { + it('redirects to `loggedOut` URL if access token is invalidated successfully.', async () => { const request = httpServerMock.createKibanaRequest(); const state = { accessToken: 'foo', peerCertificateFingerprint256: '2A:7A:C2:DD' }; mockOptions.tokens.invalidate.mockResolvedValue(undefined); await expect(provider.logout(request, state)).resolves.toEqual( - DeauthenticationResult.redirectTo('/mock-server-basepath/security/logged_out') + DeauthenticationResult.redirectTo(mockOptions.urls.loggedOut) ); expect(mockOptions.tokens.invalidate).toHaveBeenCalledTimes(1); diff --git a/x-pack/plugins/security/server/authentication/providers/pki.ts b/x-pack/plugins/security/server/authentication/providers/pki.ts index 243e5415ad2c2c..164a9516f06958 100644 --- a/x-pack/plugins/security/server/authentication/providers/pki.ts +++ b/x-pack/plugins/security/server/authentication/providers/pki.ts @@ -119,9 +119,7 @@ export class PKIAuthenticationProvider extends BaseAuthenticationProvider { return DeauthenticationResult.failed(err); } - return DeauthenticationResult.redirectTo( - `${this.options.basePath.serverBasePath}/security/logged_out` - ); + return DeauthenticationResult.redirectTo(this.options.urls.loggedOut); } /** diff --git a/x-pack/plugins/security/server/authentication/providers/saml.test.ts b/x-pack/plugins/security/server/authentication/providers/saml.test.ts index 461ad3e38eca51..f7adaa24e9dbbd 100644 --- a/x-pack/plugins/security/server/authentication/providers/saml.test.ts +++ b/x-pack/plugins/security/server/authentication/providers/saml.test.ts @@ -110,6 +110,51 @@ describe('SAMLAuthenticationProvider', () => { ); }); + it('gets token and redirects user to the requested URL if SAML Response is valid ignoring Relay State.', async () => { + const request = httpServerMock.createKibanaRequest(); + + mockOptions.client.callAsInternalUser.mockResolvedValue({ + username: 'user', + access_token: 'some-token', + refresh_token: 'some-refresh-token', + }); + + provider = new SAMLAuthenticationProvider(mockOptions, { + realm: 'test-realm', + maxRedirectURLSize: new ByteSizeValue(100), + useRelayStateDeepLink: true, + }); + await expect( + provider.login( + request, + { + type: SAMLLogin.LoginWithSAMLResponse, + samlResponse: 'saml-response-xml', + relayState: `${mockOptions.basePath.serverBasePath}/app/some-app#some-deep-link`, + }, + { + requestId: 'some-request-id', + redirectURL: '/test-base-path/some-path#some-app', + realm: 'test-realm', + } + ) + ).resolves.toEqual( + AuthenticationResult.redirectTo('/test-base-path/some-path#some-app', { + state: { + username: 'user', + accessToken: 'some-token', + refreshToken: 'some-refresh-token', + realm: 'test-realm', + }, + }) + ); + + expect(mockOptions.client.callAsInternalUser).toHaveBeenCalledWith( + 'shield.samlAuthenticate', + { body: { ids: ['some-request-id'], content: 'saml-response-xml', realm: 'test-realm' } } + ); + }); + it('fails if SAML Response payload is presented but state does not contain SAML Request token.', async () => { const request = httpServerMock.createKibanaRequest(); @@ -163,7 +208,46 @@ describe('SAMLAuthenticationProvider', () => { { requestId: 'some-request-id', redirectURL: '', realm: 'test-realm' } ) ).resolves.toEqual( - AuthenticationResult.redirectTo('/base-path/', { + AuthenticationResult.redirectTo('/mock-server-basepath/', { + state: { + accessToken: 'user-initiated-login-token', + refreshToken: 'user-initiated-login-refresh-token', + realm: 'test-realm', + }, + }) + ); + + expect(mockOptions.client.callAsInternalUser).toHaveBeenCalledWith( + 'shield.samlAuthenticate', + { body: { ids: ['some-request-id'], content: 'saml-response-xml', realm: 'test-realm' } } + ); + }); + + it('redirects to the default location if state contains empty redirect URL ignoring Relay State.', async () => { + const request = httpServerMock.createKibanaRequest(); + + mockOptions.client.callAsInternalUser.mockResolvedValue({ + access_token: 'user-initiated-login-token', + refresh_token: 'user-initiated-login-refresh-token', + }); + + provider = new SAMLAuthenticationProvider(mockOptions, { + realm: 'test-realm', + maxRedirectURLSize: new ByteSizeValue(100), + useRelayStateDeepLink: true, + }); + await expect( + provider.login( + request, + { + type: SAMLLogin.LoginWithSAMLResponse, + samlResponse: 'saml-response-xml', + relayState: `${mockOptions.basePath.serverBasePath}/app/some-app#some-deep-link`, + }, + { requestId: 'some-request-id', redirectURL: '', realm: 'test-realm' } + ) + ).resolves.toEqual( + AuthenticationResult.redirectTo('/mock-server-basepath/', { state: { accessToken: 'user-initiated-login-token', refreshToken: 'user-initiated-login-refresh-token', @@ -192,7 +276,7 @@ describe('SAMLAuthenticationProvider', () => { samlResponse: 'saml-response-xml', }) ).resolves.toEqual( - AuthenticationResult.redirectTo('/base-path/', { + AuthenticationResult.redirectTo('/mock-server-basepath/', { state: { accessToken: 'idp-initiated-login-token', refreshToken: 'idp-initiated-login-refresh-token', @@ -231,6 +315,133 @@ describe('SAMLAuthenticationProvider', () => { ); }); + describe('IdP initiated login', () => { + beforeEach(() => { + mockOptions.basePath.get.mockReturnValue(mockOptions.basePath.serverBasePath); + + const mockScopedClusterClient = elasticsearchServiceMock.createScopedClusterClient(); + mockScopedClusterClient.callAsCurrentUser.mockImplementation(() => + Promise.resolve(mockAuthenticatedUser()) + ); + mockOptions.client.asScoped.mockReturnValue(mockScopedClusterClient); + + mockOptions.client.callAsInternalUser.mockResolvedValue({ + username: 'user', + access_token: 'valid-token', + refresh_token: 'valid-refresh-token', + }); + + provider = new SAMLAuthenticationProvider(mockOptions, { + realm: 'test-realm', + maxRedirectURLSize: new ByteSizeValue(100), + useRelayStateDeepLink: true, + }); + }); + + it('redirects to the home page if `useRelayStateDeepLink` is set to `false`.', async () => { + provider = new SAMLAuthenticationProvider(mockOptions, { + realm: 'test-realm', + maxRedirectURLSize: new ByteSizeValue(100), + useRelayStateDeepLink: false, + }); + + await expect( + provider.login(httpServerMock.createKibanaRequest({ headers: {} }), { + type: SAMLLogin.LoginWithSAMLResponse, + samlResponse: 'saml-response-xml', + relayState: `${mockOptions.basePath.serverBasePath}/app/some-app#some-deep-link`, + }) + ).resolves.toEqual( + AuthenticationResult.redirectTo(`${mockOptions.basePath.serverBasePath}/`, { + state: { + username: 'user', + accessToken: 'valid-token', + refreshToken: 'valid-refresh-token', + realm: 'test-realm', + }, + }) + ); + }); + + it('redirects to the home page if `relayState` is not specified.', async () => { + await expect( + provider.login(httpServerMock.createKibanaRequest({ headers: {} }), { + type: SAMLLogin.LoginWithSAMLResponse, + samlResponse: 'saml-response-xml', + }) + ).resolves.toEqual( + AuthenticationResult.redirectTo(`${mockOptions.basePath.serverBasePath}/`, { + state: { + username: 'user', + accessToken: 'valid-token', + refreshToken: 'valid-refresh-token', + realm: 'test-realm', + }, + }) + ); + }); + + it('redirects to the home page if `relayState` includes external URL', async () => { + await expect( + provider.login(httpServerMock.createKibanaRequest({ headers: {} }), { + type: SAMLLogin.LoginWithSAMLResponse, + samlResponse: 'saml-response-xml', + relayState: `https://evil.com${mockOptions.basePath.serverBasePath}/app/some-app#some-deep-link`, + }) + ).resolves.toEqual( + AuthenticationResult.redirectTo(`${mockOptions.basePath.serverBasePath}/`, { + state: { + username: 'user', + accessToken: 'valid-token', + refreshToken: 'valid-refresh-token', + realm: 'test-realm', + }, + }) + ); + }); + + it('redirects to the home page if `relayState` includes URL that starts with double slashes', async () => { + await expect( + provider.login(httpServerMock.createKibanaRequest({ headers: {} }), { + type: SAMLLogin.LoginWithSAMLResponse, + samlResponse: 'saml-response-xml', + relayState: `//${mockOptions.basePath.serverBasePath}/app/some-app#some-deep-link`, + }) + ).resolves.toEqual( + AuthenticationResult.redirectTo(`${mockOptions.basePath.serverBasePath}/`, { + state: { + username: 'user', + accessToken: 'valid-token', + refreshToken: 'valid-refresh-token', + realm: 'test-realm', + }, + }) + ); + }); + + it('redirects to the URL from the relay state.', async () => { + await expect( + provider.login(httpServerMock.createKibanaRequest({ headers: {} }), { + type: SAMLLogin.LoginWithSAMLResponse, + samlResponse: 'saml-response-xml', + relayState: `${mockOptions.basePath.serverBasePath}/app/some-app#some-deep-link`, + }) + ).resolves.toEqual( + AuthenticationResult.redirectTo( + `${mockOptions.basePath.serverBasePath}/app/some-app#some-deep-link`, + { + state: { + username: 'user', + accessToken: 'valid-token', + refreshToken: 'valid-refresh-token', + realm: 'test-realm', + }, + } + ) + ); + }); + }); + describe('IdP initiated login with existing session', () => { it('returns `notHandled` if new SAML Response is rejected.', async () => { const request = httpServerMock.createKibanaRequest({ headers: {} }); @@ -351,7 +562,72 @@ describe('SAMLAuthenticationProvider', () => { state ) ).resolves.toEqual( - AuthenticationResult.redirectTo('/base-path/', { + AuthenticationResult.redirectTo('/mock-server-basepath/', { + state: { + username: 'user', + accessToken: 'new-valid-token', + refreshToken: 'new-valid-refresh-token', + realm: 'test-realm', + }, + }) + ); + + expectAuthenticateCall(mockOptions.client, { headers: { authorization } }); + + expect(mockOptions.client.callAsInternalUser).toHaveBeenCalledWith( + 'shield.samlAuthenticate', + { + body: { ids: [], content: 'saml-response-xml', realm: 'test-realm' }, + } + ); + + expect(mockOptions.tokens.invalidate).toHaveBeenCalledTimes(1); + expect(mockOptions.tokens.invalidate).toHaveBeenCalledWith({ + accessToken: state.accessToken, + refreshToken: state.refreshToken, + }); + }); + + it(`redirects to the URL from relay state if new SAML Response is for the same user if ${description}.`, async () => { + const request = httpServerMock.createKibanaRequest({ headers: {} }); + const state = { + username: 'user', + accessToken: 'existing-token', + refreshToken: 'existing-refresh-token', + realm: 'test-realm', + }; + const authorization = `Bearer ${state.accessToken}`; + + const mockScopedClusterClient = elasticsearchServiceMock.createScopedClusterClient(); + mockScopedClusterClient.callAsCurrentUser.mockImplementation(() => response); + mockOptions.client.asScoped.mockReturnValue(mockScopedClusterClient); + + mockOptions.client.callAsInternalUser.mockResolvedValue({ + username: 'user', + access_token: 'new-valid-token', + refresh_token: 'new-valid-refresh-token', + }); + + mockOptions.tokens.invalidate.mockResolvedValue(undefined); + + provider = new SAMLAuthenticationProvider(mockOptions, { + realm: 'test-realm', + maxRedirectURLSize: new ByteSizeValue(100), + useRelayStateDeepLink: true, + }); + + await expect( + provider.login( + request, + { + type: SAMLLogin.LoginWithSAMLResponse, + samlResponse: 'saml-response-xml', + relayState: '/mock-server-basepath/app/some-app#some-deep-link', + }, + state + ) + ).resolves.toEqual( + AuthenticationResult.redirectTo('/mock-server-basepath/app/some-app#some-deep-link', { state: { username: 'user', accessToken: 'new-valid-token', @@ -723,7 +999,7 @@ describe('SAMLAuthenticationProvider', () => { await expect(provider.authenticate(request)).resolves.toEqual( AuthenticationResult.redirectTo( '/mock-server-basepath/internal/security/saml/capture-url-fragment', - { state: { redirectURL: '/base-path/s/foo/some-path', realm: 'test-realm' } } + { state: { redirectURL: '/mock-server-basepath/s/foo/some-path', realm: 'test-realm' } } ) ); @@ -753,7 +1029,7 @@ describe('SAMLAuthenticationProvider', () => { expect(mockOptions.logger.warn).toHaveBeenCalledTimes(1); expect(mockOptions.logger.warn).toHaveBeenCalledWith( - 'Max URL path size should not exceed 100b but it was 107b. URL is not captured.' + 'Max URL path size should not exceed 100b but it was 118b. URL is not captured.' ); }); @@ -998,7 +1274,7 @@ describe('SAMLAuthenticationProvider', () => { await expect(provider.authenticate(request, state)).resolves.toEqual( AuthenticationResult.redirectTo( '/mock-server-basepath/internal/security/saml/capture-url-fragment', - { state: { redirectURL: '/base-path/s/foo/some-path', realm: 'test-realm' } } + { state: { redirectURL: '/mock-server-basepath/s/foo/some-path', realm: 'test-realm' } } ) ); @@ -1054,7 +1330,7 @@ describe('SAMLAuthenticationProvider', () => { expect(mockOptions.logger.warn).toHaveBeenCalledTimes(1); expect(mockOptions.logger.warn).toHaveBeenCalledWith( - 'Max URL path size should not exceed 100b but it was 107b. URL is not captured.' + 'Max URL path size should not exceed 100b but it was 118b. URL is not captured.' ); }); @@ -1124,7 +1400,7 @@ describe('SAMLAuthenticationProvider', () => { }); }); - it('redirects to /security/logged_out if `redirect` field in SAML logout response is null.', async () => { + it('redirects to `loggedOut` URL if `redirect` field in SAML logout response is null.', async () => { const request = httpServerMock.createKibanaRequest(); const accessToken = 'x-saml-token'; const refreshToken = 'x-saml-refresh-token'; @@ -1138,9 +1414,7 @@ describe('SAMLAuthenticationProvider', () => { refreshToken, realm: 'test-realm', }) - ).resolves.toEqual( - DeauthenticationResult.redirectTo('/mock-server-basepath/security/logged_out') - ); + ).resolves.toEqual(DeauthenticationResult.redirectTo(mockOptions.urls.loggedOut)); expect(mockOptions.client.callAsInternalUser).toHaveBeenCalledTimes(1); expect(mockOptions.client.callAsInternalUser).toHaveBeenCalledWith('shield.samlLogout', { @@ -1148,7 +1422,7 @@ describe('SAMLAuthenticationProvider', () => { }); }); - it('redirects to /security/logged_out if `redirect` field in SAML logout response is not defined.', async () => { + it('redirects to `loggedOut` URL if `redirect` field in SAML logout response is not defined.', async () => { const request = httpServerMock.createKibanaRequest(); const accessToken = 'x-saml-token'; const refreshToken = 'x-saml-refresh-token'; @@ -1162,9 +1436,7 @@ describe('SAMLAuthenticationProvider', () => { refreshToken, realm: 'test-realm', }) - ).resolves.toEqual( - DeauthenticationResult.redirectTo('/mock-server-basepath/security/logged_out') - ); + ).resolves.toEqual(DeauthenticationResult.redirectTo(mockOptions.urls.loggedOut)); expect(mockOptions.client.callAsInternalUser).toHaveBeenCalledTimes(1); expect(mockOptions.client.callAsInternalUser).toHaveBeenCalledWith('shield.samlLogout', { @@ -1174,7 +1446,7 @@ describe('SAMLAuthenticationProvider', () => { it('relies on SAML logout if query string is not empty, but does not include SAMLRequest.', async () => { const request = httpServerMock.createKibanaRequest({ - query: { Whatever: 'something unrelated' }, + query: { Whatever: 'something unrelated', SAMLResponse: 'xxx yyy' }, }); const accessToken = 'x-saml-token'; const refreshToken = 'x-saml-refresh-token'; @@ -1188,9 +1460,7 @@ describe('SAMLAuthenticationProvider', () => { refreshToken, realm: 'test-realm', }) - ).resolves.toEqual( - DeauthenticationResult.redirectTo('/mock-server-basepath/security/logged_out') - ); + ).resolves.toEqual(DeauthenticationResult.redirectTo(mockOptions.urls.loggedOut)); expect(mockOptions.client.callAsInternalUser).toHaveBeenCalledTimes(1); expect(mockOptions.client.callAsInternalUser).toHaveBeenCalledWith('shield.samlLogout', { @@ -1210,9 +1480,7 @@ describe('SAMLAuthenticationProvider', () => { refreshToken: 'x-saml-refresh-token', realm: 'test-realm', }) - ).resolves.toEqual( - DeauthenticationResult.redirectTo('/mock-server-basepath/security/logged_out') - ); + ).resolves.toEqual(DeauthenticationResult.redirectTo(mockOptions.urls.loggedOut)); expect(mockOptions.client.callAsInternalUser).toHaveBeenCalledTimes(1); expect(mockOptions.client.callAsInternalUser).toHaveBeenCalledWith('shield.samlInvalidate', { @@ -1220,13 +1488,13 @@ describe('SAMLAuthenticationProvider', () => { }); }); - it('redirects to /security/logged_out if `redirect` field in SAML invalidate response is null.', async () => { + it('redirects to `loggedOut` URL if `redirect` field in SAML invalidate response is null.', async () => { const request = httpServerMock.createKibanaRequest({ query: { SAMLRequest: 'xxx yyy' } }); mockOptions.client.callAsInternalUser.mockResolvedValue({ redirect: null }); await expect(provider.logout(request)).resolves.toEqual( - DeauthenticationResult.redirectTo('/mock-server-basepath/security/logged_out') + DeauthenticationResult.redirectTo(mockOptions.urls.loggedOut) ); expect(mockOptions.client.callAsInternalUser).toHaveBeenCalledTimes(1); @@ -1235,13 +1503,13 @@ describe('SAMLAuthenticationProvider', () => { }); }); - it('redirects to /security/logged_out if `redirect` field in SAML invalidate response is not defined.', async () => { + it('redirects to `loggedOut` URL if `redirect` field in SAML invalidate response is not defined.', async () => { const request = httpServerMock.createKibanaRequest({ query: { SAMLRequest: 'xxx yyy' } }); mockOptions.client.callAsInternalUser.mockResolvedValue({ redirect: undefined }); await expect(provider.logout(request)).resolves.toEqual( - DeauthenticationResult.redirectTo('/mock-server-basepath/security/logged_out') + DeauthenticationResult.redirectTo(mockOptions.urls.loggedOut) ); expect(mockOptions.client.callAsInternalUser).toHaveBeenCalledTimes(1); @@ -1250,6 +1518,16 @@ describe('SAMLAuthenticationProvider', () => { }); }); + it('redirects to `loggedOut` URL if SAML logout response is received.', async () => { + const request = httpServerMock.createKibanaRequest({ query: { SAMLResponse: 'xxx yyy' } }); + + await expect(provider.logout(request)).resolves.toEqual( + DeauthenticationResult.redirectTo(mockOptions.urls.loggedOut) + ); + + expect(mockOptions.client.callAsInternalUser).not.toHaveBeenCalled(); + }); + it('redirects user to the IdP if SLO is supported by IdP in case of SP initiated logout.', async () => { const request = httpServerMock.createKibanaRequest(); const accessToken = 'x-saml-token'; diff --git a/x-pack/plugins/security/server/authentication/providers/saml.ts b/x-pack/plugins/security/server/authentication/providers/saml.ts index 3161144023c1f3..d121cd4979aa73 100644 --- a/x-pack/plugins/security/server/authentication/providers/saml.ts +++ b/x-pack/plugins/security/server/authentication/providers/saml.ts @@ -7,6 +7,7 @@ import Boom from 'boom'; import { ByteSizeValue } from '@kbn/config-schema'; import { KibanaRequest } from '../../../../../../src/core/server'; +import { isInternalURL } from '../../../common/is_internal_url'; import { AuthenticationResult } from '../authentication_result'; import { DeauthenticationResult } from '../deauthentication_result'; import { canRedirectRequest } from '../can_redirect_request'; @@ -59,7 +60,7 @@ export enum SAMLLogin { */ type ProviderLoginAttempt = | { type: SAMLLogin.LoginInitiatedByUser; redirectURLPath?: string; redirectURLFragment?: string } - | { type: SAMLLogin.LoginWithSAMLResponse; samlResponse: string }; + | { type: SAMLLogin.LoginWithSAMLResponse; samlResponse: string; relayState?: string }; /** * Checks whether request query includes SAML request from IdP. @@ -69,6 +70,14 @@ function isSAMLRequestQuery(query: any): query is { SAMLRequest: string } { return query && query.SAMLRequest; } +/** + * Checks whether request query includes SAML response from IdP. + * @param query Parsed HTTP request query. + */ +function isSAMLResponseQuery(query: any): query is { SAMLResponse: string } { + return query && query.SAMLResponse; +} + /** * Checks whether current request can initiate new session. * @param request Request instance. @@ -98,9 +107,19 @@ export class SAMLAuthenticationProvider extends BaseAuthenticationProvider { */ private readonly maxRedirectURLSize: ByteSizeValue; + /** + * Indicates if we should treat non-empty `RelayState` as a deep link in Kibana we should redirect + * user to after successful IdP initiated login. `RelayState` is ignored for SP initiated login. + */ + private readonly useRelayStateDeepLink: boolean; + constructor( protected readonly options: Readonly, - samlOptions?: Readonly<{ realm?: string; maxRedirectURLSize?: ByteSizeValue }> + samlOptions?: Readonly<{ + realm?: string; + maxRedirectURLSize?: ByteSizeValue; + useRelayStateDeepLink?: boolean; + }> ) { super(options); @@ -114,6 +133,7 @@ export class SAMLAuthenticationProvider extends BaseAuthenticationProvider { this.realm = samlOptions.realm; this.maxRedirectURLSize = samlOptions.maxRedirectURLSize; + this.useRelayStateDeepLink = samlOptions.useRelayStateDeepLink ?? false; } /** @@ -148,14 +168,14 @@ export class SAMLAuthenticationProvider extends BaseAuthenticationProvider { return this.captureRedirectURL(request, redirectURLPath, attempt.redirectURLFragment); } - const { samlResponse } = attempt; + const { samlResponse, relayState } = attempt; const authenticationResult = state ? await this.authenticateViaState(request, state) : AuthenticationResult.notHandled(); // Let's check if user is redirected to Kibana from IdP with valid SAMLResponse. if (authenticationResult.notHandled()) { - return await this.loginWithSAMLResponse(request, samlResponse, state); + return await this.loginWithSAMLResponse(request, samlResponse, relayState, state); } // If user has been authenticated via session or failed to do so because of expired access token, @@ -169,6 +189,7 @@ export class SAMLAuthenticationProvider extends BaseAuthenticationProvider { return await this.loginWithNewSAMLResponse( request, samlResponse, + relayState, (authenticationResult.state || state) as ProviderState ); } @@ -234,22 +255,36 @@ export class SAMLAuthenticationProvider extends BaseAuthenticationProvider { this.logger.debug(`Trying to log user out via ${request.url.path}.`); // Normally when there is no active session in Kibana, `logout` method shouldn't do anything - // and user will eventually be redirected to the home page to log in. But when SAML is enabled - // there is a special case when logout is initiated by the IdP or another SP, then IdP will - // request _every_ SP associated with the current user session to do the logout. So if Kibana, - // without an active session, receives such request it shouldn't redirect user to the home page, - // but rather redirect back to IdP with correct logout response and only Elasticsearch knows how - // to do that. - const isIdPInitiatedSLO = isSAMLRequestQuery(request.query); - if (!state?.accessToken && !isIdPInitiatedSLO) { + // and user will eventually be redirected to the home page to log in. But when SAML SLO is + // supported there are two special cases that we need to handle even if there is no active + // Kibana session: + // + // 1. When IdP or another SP initiates logout, then IdP will request _every_ SP associated with + // the current user session to do the logout. So if Kibana receives such request it shouldn't + // redirect user to the home page, but rather redirect back to IdP with correct logout response + // and only Elasticsearch knows how to do that. + // + // 2. When Kibana initiates logout, then IdP may eventually respond with the logout response. So + // if Kibana receives such response it shouldn't redirect user to the home page, but rather + // redirect to the `loggedOut` URL instead. + const isIdPInitiatedSLORequest = isSAMLRequestQuery(request.query); + const isSPInitiatedSLOResponse = isSAMLResponseQuery(request.query); + if (!state?.accessToken && !isIdPInitiatedSLORequest && !isSPInitiatedSLOResponse) { this.logger.debug('There is no SAML session to invalidate.'); return DeauthenticationResult.notHandled(); } try { - const redirect = isIdPInitiatedSLO + // It may _theoretically_ (highly unlikely in practice though) happen that when user receives + // logout response they may already have a new SAML session (isSPInitiatedSLOResponse == true + // and state !== undefined). In this case case it'd be safer to trigger SP initiated logout + // for the new session as well. + const redirect = isIdPInitiatedSLORequest ? await this.performIdPInitiatedSingleLogout(request) - : await this.performUserInitiatedSingleLogout(state?.accessToken!, state?.refreshToken!); + : state + ? await this.performUserInitiatedSingleLogout(state.accessToken!, state.refreshToken!) + : // Once Elasticsearch can consume logout response we'll be sending it here. See https://github.com/elastic/elasticsearch/issues/40901 + null; // Having non-null `redirect` field within logout response means that IdP // supports SAML Single Logout and we should redirect user to the specified @@ -259,9 +294,7 @@ export class SAMLAuthenticationProvider extends BaseAuthenticationProvider { return DeauthenticationResult.redirectTo(redirect); } - return DeauthenticationResult.redirectTo( - `${this.options.basePath.serverBasePath}/security/logged_out` - ); + return DeauthenticationResult.redirectTo(this.options.urls.loggedOut); } catch (err) { this.logger.debug(`Failed to deauthenticate user: ${err.message}`); return DeauthenticationResult.failed(err); @@ -290,11 +323,13 @@ export class SAMLAuthenticationProvider extends BaseAuthenticationProvider { * initiated login. * @param request Request instance. * @param samlResponse SAMLResponse payload string. + * @param relayState RelayState payload string. * @param [state] Optional state object associated with the provider. */ private async loginWithSAMLResponse( request: KibanaRequest, samlResponse: string, + relayState?: string, state?: ProviderState | null ) { this.logger.debug('Trying to log in with SAML response payload.'); @@ -334,9 +369,29 @@ export class SAMLAuthenticationProvider extends BaseAuthenticationProvider { }, }); + // IdP can pass `RelayState` with the deep link in Kibana during IdP initiated login and + // depending on the configuration we may need to redirect user to this URL. + let redirectURLFromRelayState; + if (isIdPInitiatedLogin && relayState) { + if (!this.useRelayStateDeepLink) { + this.options.logger.debug( + `"RelayState" is provided, but deep links support is not enabled for "${this.type}/${this.options.name}" provider.` + ); + } else if (!isInternalURL(relayState, this.options.basePath.serverBasePath)) { + this.options.logger.debug( + `"RelayState" is provided, but it is not a valid Kibana internal URL.` + ); + } else { + this.options.logger.debug( + `User will be redirected to the Kibana internal URL specified in "RelayState".` + ); + redirectURLFromRelayState = relayState; + } + } + this.logger.debug('Login has been performed with SAML response.'); return AuthenticationResult.redirectTo( - stateRedirectURL || `${this.options.basePath.get(request)}/`, + redirectURLFromRelayState || stateRedirectURL || `${this.options.basePath.get(request)}/`, { state: { username, accessToken, refreshToken, realm: this.realm } } ); } catch (err) { @@ -361,17 +416,23 @@ export class SAMLAuthenticationProvider extends BaseAuthenticationProvider { * we'll forward user to a page with the respective warning. * @param request Request instance. * @param samlResponse SAMLResponse payload string. + * @param relayState RelayState payload string. * @param existingState State existing user session is based on. */ private async loginWithNewSAMLResponse( request: KibanaRequest, samlResponse: string, + relayState: string | undefined, existingState: ProviderState ) { this.logger.debug('Trying to log in with SAML response payload and existing valid session.'); // First let's try to authenticate via SAML Response payload. - const payloadAuthenticationResult = await this.loginWithSAMLResponse(request, samlResponse); + const payloadAuthenticationResult = await this.loginWithSAMLResponse( + request, + samlResponse, + relayState + ); if (payloadAuthenticationResult.failed() || payloadAuthenticationResult.notHandled()) { return payloadAuthenticationResult; } diff --git a/x-pack/plugins/security/server/authentication/providers/token.test.ts b/x-pack/plugins/security/server/authentication/providers/token.test.ts index 92cea424e575da..84ff7d1f5a1ef7 100644 --- a/x-pack/plugins/security/server/authentication/providers/token.test.ts +++ b/x-pack/plugins/security/server/authentication/providers/token.test.ts @@ -179,7 +179,7 @@ describe('TokenAuthenticationProvider', () => { ) ).resolves.toEqual( AuthenticationResult.redirectTo( - '/base-path/login?next=%2Fbase-path%2Fs%2Ffoo%2Fsome-path%20%23%20that%20needs%20to%20be%20encoded' + '/mock-server-basepath/login?next=%2Fmock-server-basepath%2Fs%2Ffoo%2Fsome-path%20%23%20that%20needs%20to%20be%20encoded' ) ); }); @@ -309,9 +309,10 @@ describe('TokenAuthenticationProvider', () => { mockOptions.tokens.refresh.mockResolvedValue(null); await expect(provider.authenticate(request, tokenPair)).resolves.toEqual( - AuthenticationResult.redirectTo('/base-path/login?next=%2Fbase-path%2Fsome-path', { - state: null, - }) + AuthenticationResult.redirectTo( + '/mock-server-basepath/login?next=%2Fmock-server-basepath%2Fsome-path', + { state: null } + ) ); expect(mockOptions.tokens.refresh).toHaveBeenCalledTimes(1); @@ -455,7 +456,7 @@ describe('TokenAuthenticationProvider', () => { mockOptions.tokens.invalidate.mockResolvedValue(undefined); await expect(provider.logout(request, tokenPair)).resolves.toEqual( - DeauthenticationResult.redirectTo('/base-path/login?msg=LOGGED_OUT') + DeauthenticationResult.redirectTo('/mock-server-basepath/login?msg=LOGGED_OUT') ); expect(mockOptions.tokens.invalidate).toHaveBeenCalledTimes(1); @@ -469,7 +470,7 @@ describe('TokenAuthenticationProvider', () => { mockOptions.tokens.invalidate.mockResolvedValue(undefined); await expect(provider.logout(request, tokenPair)).resolves.toEqual( - DeauthenticationResult.redirectTo('/base-path/login?yep=nope') + DeauthenticationResult.redirectTo('/mock-server-basepath/login?yep=nope') ); expect(mockOptions.tokens.invalidate).toHaveBeenCalledTimes(1); diff --git a/x-pack/plugins/security/server/authentication/tokens.test.ts b/x-pack/plugins/security/server/authentication/tokens.test.ts index 57366183050d7e..b42018b93e73fd 100644 --- a/x-pack/plugins/security/server/authentication/tokens.test.ts +++ b/x-pack/plugins/security/server/authentication/tokens.test.ts @@ -6,7 +6,7 @@ import { errors } from 'elasticsearch'; -import { elasticsearchServiceMock, loggingServiceMock } from '../../../../../src/core/server/mocks'; +import { elasticsearchServiceMock, loggingSystemMock } from '../../../../../src/core/server/mocks'; import { IClusterClient, ElasticsearchErrorHelpers } from '../../../../../src/core/server'; import { Tokens } from './tokens'; @@ -19,7 +19,7 @@ describe('Tokens', () => { const tokensOptions = { client: mockClusterClient, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), }; tokens = new Tokens(tokensOptions); diff --git a/x-pack/plugins/security/server/authorization/api_authorization.test.ts b/x-pack/plugins/security/server/authorization/api_authorization.test.ts index 183a36274142c8..75aa27c3c88c6a 100644 --- a/x-pack/plugins/security/server/authorization/api_authorization.test.ts +++ b/x-pack/plugins/security/server/authorization/api_authorization.test.ts @@ -10,7 +10,7 @@ import { coreMock, httpServerMock, httpServiceMock, - loggingServiceMock, + loggingSystemMock, } from '../../../../../src/core/server/mocks'; import { authorizationMock } from './index.mock'; @@ -18,7 +18,7 @@ describe('initAPIAuthorization', () => { test(`protected route when "mode.useRbacForRequest()" returns false continues`, async () => { const mockHTTPSetup = coreMock.createSetup().http; const mockAuthz = authorizationMock.create(); - initAPIAuthorization(mockHTTPSetup, mockAuthz, loggingServiceMock.create().get()); + initAPIAuthorization(mockHTTPSetup, mockAuthz, loggingSystemMock.create().get()); const [[postAuthHandler]] = mockHTTPSetup.registerOnPostAuth.mock.calls; @@ -42,7 +42,7 @@ describe('initAPIAuthorization', () => { test(`unprotected route when "mode.useRbacForRequest()" returns true continues`, async () => { const mockHTTPSetup = coreMock.createSetup().http; const mockAuthz = authorizationMock.create(); - initAPIAuthorization(mockHTTPSetup, mockAuthz, loggingServiceMock.create().get()); + initAPIAuthorization(mockHTTPSetup, mockAuthz, loggingSystemMock.create().get()); const [[postAuthHandler]] = mockHTTPSetup.registerOnPostAuth.mock.calls; @@ -66,7 +66,7 @@ describe('initAPIAuthorization', () => { test(`protected route when "mode.useRbacForRequest()" returns true and user is authorized continues`, async () => { const mockHTTPSetup = coreMock.createSetup().http; const mockAuthz = authorizationMock.create({ version: '1.0.0-zeta1' }); - initAPIAuthorization(mockHTTPSetup, mockAuthz, loggingServiceMock.create().get()); + initAPIAuthorization(mockHTTPSetup, mockAuthz, loggingSystemMock.create().get()); const [[postAuthHandler]] = mockHTTPSetup.registerOnPostAuth.mock.calls; @@ -101,7 +101,7 @@ describe('initAPIAuthorization', () => { test(`protected route when "mode.useRbacForRequest()" returns true and user isn't authorized responds with a 404`, async () => { const mockHTTPSetup = coreMock.createSetup().http; const mockAuthz = authorizationMock.create({ version: '1.0.0-zeta1' }); - initAPIAuthorization(mockHTTPSetup, mockAuthz, loggingServiceMock.create().get()); + initAPIAuthorization(mockHTTPSetup, mockAuthz, loggingSystemMock.create().get()); const [[postAuthHandler]] = mockHTTPSetup.registerOnPostAuth.mock.calls; diff --git a/x-pack/plugins/security/server/authorization/app_authorization.test.ts b/x-pack/plugins/security/server/authorization/app_authorization.test.ts index 1dc56161d63631..2d3a981fb32472 100644 --- a/x-pack/plugins/security/server/authorization/app_authorization.test.ts +++ b/x-pack/plugins/security/server/authorization/app_authorization.test.ts @@ -8,7 +8,7 @@ import { PluginSetupContract as FeaturesSetupContract } from '../../../features/ import { initAppAuthorization } from './app_authorization'; import { - loggingServiceMock, + loggingSystemMock, coreMock, httpServerMock, httpServiceMock, @@ -27,7 +27,7 @@ describe('initAppAuthorization', () => { initAppAuthorization( mockHTTPSetup, authorizationMock.create(), - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), createFeaturesSetupContractMock() ); @@ -49,7 +49,7 @@ describe('initAppAuthorization', () => { initAppAuthorization( mockHTTPSetup, mockAuthz, - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), createFeaturesSetupContractMock() ); @@ -74,7 +74,7 @@ describe('initAppAuthorization', () => { initAppAuthorization( mockHTTPSetup, mockAuthz, - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), createFeaturesSetupContractMock() ); @@ -100,7 +100,7 @@ describe('initAppAuthorization', () => { initAppAuthorization( mockHTTPSetup, mockAuthz, - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), createFeaturesSetupContractMock() ); @@ -140,7 +140,7 @@ describe('initAppAuthorization', () => { initAppAuthorization( mockHTTPSetup, mockAuthz, - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), createFeaturesSetupContractMock() ); diff --git a/x-pack/plugins/security/server/authorization/authorization_service.test.ts b/x-pack/plugins/security/server/authorization/authorization_service.test.ts index 978c985cfe820d..4d0ab1c964741d 100644 --- a/x-pack/plugins/security/server/authorization/authorization_service.test.ts +++ b/x-pack/plugins/security/server/authorization/authorization_service.test.ts @@ -25,7 +25,7 @@ import { AuthorizationService } from '.'; import { coreMock, elasticsearchServiceMock, - loggingServiceMock, + loggingSystemMock, } from '../../../../../src/core/server/mocks'; import { featuresPluginMock } from '../../../features/server/mocks'; import { licenseMock } from '../../common/licensing/index.mock'; @@ -71,7 +71,7 @@ it(`#setup returns exposed services`, () => { status: mockCoreSetup.status, clusterClient: mockClusterClient, license: mockLicense, - loggers: loggingServiceMock.create(), + loggers: loggingSystemMock.create(), kibanaIndexName, packageVersion: 'some-version', features: mockFeaturesSetup, @@ -140,7 +140,7 @@ describe('#start', () => { status: mockCoreSetup.status, clusterClient: mockClusterClient, license: mockLicense, - loggers: loggingServiceMock.create(), + loggers: loggingSystemMock.create(), kibanaIndexName, packageVersion: 'some-version', features: featuresPluginMock.createSetup(), @@ -241,7 +241,7 @@ it('#stop unsubscribes from license and ES updates.', () => { status: mockCoreSetup.status, clusterClient: mockClusterClient, license: mockLicense, - loggers: loggingServiceMock.create(), + loggers: loggingSystemMock.create(), kibanaIndexName, packageVersion: 'some-version', features: featuresPluginMock.createSetup(), diff --git a/x-pack/plugins/security/server/authorization/disable_ui_capabilities.test.ts b/x-pack/plugins/security/server/authorization/disable_ui_capabilities.test.ts index 082484d5fa6b49..a1bedea9f7debe 100644 --- a/x-pack/plugins/security/server/authorization/disable_ui_capabilities.test.ts +++ b/x-pack/plugins/security/server/authorization/disable_ui_capabilities.test.ts @@ -7,7 +7,7 @@ import { Actions } from '.'; import { disableUICapabilitiesFactory } from './disable_ui_capabilities'; -import { httpServerMock, loggingServiceMock } from '../../../../../src/core/server/mocks'; +import { httpServerMock, loggingSystemMock } from '../../../../../src/core/server/mocks'; import { authorizationMock } from './index.mock'; import { Feature } from '../../../features/server'; @@ -42,7 +42,7 @@ describe('usingPrivileges', () => { const mockAuthz = createMockAuthz({ rejectCheckPrivileges: { statusCode: 401, message: 'super informative message' }, }); - const mockLoggers = loggingServiceMock.create(); + const mockLoggers = loggingSystemMock.create(); const { usingPrivileges } = disableUICapabilitiesFactory( mockRequest, @@ -103,7 +103,7 @@ describe('usingPrivileges', () => { }, }); - expect(loggingServiceMock.collect(mockLoggers).debug).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(mockLoggers).debug).toMatchInlineSnapshot(` Array [ Array [ "Disabling all uiCapabilities because we received a 401: super informative message", @@ -116,7 +116,7 @@ describe('usingPrivileges', () => { const mockAuthz = createMockAuthz({ rejectCheckPrivileges: { statusCode: 403, message: 'even more super informative message' }, }); - const mockLoggers = loggingServiceMock.create(); + const mockLoggers = loggingSystemMock.create(); const { usingPrivileges } = disableUICapabilitiesFactory( mockRequest, @@ -176,7 +176,7 @@ describe('usingPrivileges', () => { bar: false, }, }); - expect(loggingServiceMock.collect(mockLoggers).debug).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(mockLoggers).debug).toMatchInlineSnapshot(` Array [ Array [ "Disabling all uiCapabilities because we received a 403: even more super informative message", @@ -189,7 +189,7 @@ describe('usingPrivileges', () => { const mockAuthz = createMockAuthz({ rejectCheckPrivileges: new Error('something else entirely'), }); - const mockLoggers = loggingServiceMock.create(); + const mockLoggers = loggingSystemMock.create(); const { usingPrivileges } = disableUICapabilitiesFactory( mockRequest, @@ -212,7 +212,7 @@ describe('usingPrivileges', () => { catalogue: {}, }) ).rejects.toThrowErrorMatchingSnapshot(); - expect(loggingServiceMock.collect(mockLoggers)).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(mockLoggers)).toMatchInlineSnapshot(` Object { "debug": Array [], "error": Array [], @@ -261,7 +261,7 @@ describe('usingPrivileges', () => { privileges: null, }), ], - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), mockAuthz ); @@ -347,7 +347,7 @@ describe('usingPrivileges', () => { privileges: null, }), ], - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), mockAuthz ); @@ -412,7 +412,7 @@ describe('all', () => { privileges: null, }), ], - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), mockAuthz ); diff --git a/x-pack/plugins/security/server/authorization/register_privileges_with_cluster.test.ts b/x-pack/plugins/security/server/authorization/register_privileges_with_cluster.test.ts index e21203e60b887e..8604e02b632766 100644 --- a/x-pack/plugins/security/server/authorization/register_privileges_with_cluster.test.ts +++ b/x-pack/plugins/security/server/authorization/register_privileges_with_cluster.test.ts @@ -8,7 +8,7 @@ import { IClusterClient, Logger } from 'kibana/server'; import { RawKibanaPrivileges } from '../../common/model'; import { registerPrivilegesWithCluster } from './register_privileges_with_cluster'; -import { elasticsearchServiceMock, loggingServiceMock } from '../../../../../src/core/server/mocks'; +import { elasticsearchServiceMock, loggingSystemMock } from '../../../../../src/core/server/mocks'; const application = 'default-application'; const registerPrivilegesWithClusterTest = ( @@ -130,7 +130,7 @@ const registerPrivilegesWithClusterTest = ( } } }); - const mockLogger = loggingServiceMock.create().get() as jest.Mocked; + const mockLogger = loggingSystemMock.create().get() as jest.Mocked; let error; try { diff --git a/x-pack/plugins/security/server/config.test.ts b/x-pack/plugins/security/server/config.test.ts index 0e1e2e2afeb13d..6ba33b2cccb7cb 100644 --- a/x-pack/plugins/security/server/config.test.ts +++ b/x-pack/plugins/security/server/config.test.ts @@ -6,7 +6,7 @@ jest.mock('crypto', () => ({ randomBytes: jest.fn() })); -import { loggingServiceMock } from '../../../../src/core/server/mocks'; +import { loggingSystemMock } from '../../../../src/core/server/mocks'; import { createConfig, ConfigSchema } from './config'; describe('config schema', () => { @@ -655,6 +655,7 @@ describe('config schema', () => { saml: { saml1: { order: 0, realm: 'saml1' }, saml2: { order: 1, realm: 'saml2', maxRedirectURLSize: '1kb' }, + saml3: { order: 2, realm: 'saml3', useRelayStateDeepLink: true }, }, }, }, @@ -670,6 +671,7 @@ describe('config schema', () => { "order": 0, "realm": "saml1", "showInSelector": true, + "useRelayStateDeepLink": false, }, "saml2": Object { "enabled": true, @@ -679,6 +681,17 @@ describe('config schema', () => { "order": 1, "realm": "saml2", "showInSelector": true, + "useRelayStateDeepLink": false, + }, + "saml3": Object { + "enabled": true, + "maxRedirectURLSize": ByteSizeValue { + "valueInBytes": 2048, + }, + "order": 2, + "realm": "saml3", + "showInSelector": true, + "useRelayStateDeepLink": true, }, }, } @@ -767,6 +780,7 @@ describe('config schema', () => { "order": 3, "realm": "saml3", "showInSelector": true, + "useRelayStateDeepLink": false, }, "saml1": Object { "enabled": true, @@ -776,6 +790,7 @@ describe('config schema', () => { "order": 1, "realm": "saml1", "showInSelector": true, + "useRelayStateDeepLink": false, }, "saml2": Object { "enabled": true, @@ -785,6 +800,7 @@ describe('config schema', () => { "order": 2, "realm": "saml2", "showInSelector": true, + "useRelayStateDeepLink": false, }, }, } @@ -798,13 +814,13 @@ describe('createConfig()', () => { const mockRandomBytes = jest.requireMock('crypto').randomBytes; mockRandomBytes.mockReturnValue('ab'.repeat(16)); - const logger = loggingServiceMock.create().get(); + const logger = loggingSystemMock.create().get(); const config = createConfig(ConfigSchema.validate({}, { dist: true }), logger, { isTLSEnabled: true, }); expect(config.encryptionKey).toEqual('ab'.repeat(16)); - expect(loggingServiceMock.collect(logger).warn).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).warn).toMatchInlineSnapshot(` Array [ Array [ "Generating a random key for xpack.security.encryptionKey. To prevent sessions from being invalidated on restart, please set xpack.security.encryptionKey in kibana.yml", @@ -814,11 +830,11 @@ describe('createConfig()', () => { }); it('should log a warning if SSL is not configured', async () => { - const logger = loggingServiceMock.create().get(); + const logger = loggingSystemMock.create().get(); const config = createConfig(ConfigSchema.validate({}), logger, { isTLSEnabled: false }); expect(config.secureCookies).toEqual(false); - expect(loggingServiceMock.collect(logger).warn).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).warn).toMatchInlineSnapshot(` Array [ Array [ "Session cookies will be transmitted over insecure connections. This is not recommended.", @@ -828,13 +844,13 @@ describe('createConfig()', () => { }); it('should log a warning if SSL is not configured yet secure cookies are being used', async () => { - const logger = loggingServiceMock.create().get(); + const logger = loggingSystemMock.create().get(); const config = createConfig(ConfigSchema.validate({ secureCookies: true }), logger, { isTLSEnabled: false, }); expect(config.secureCookies).toEqual(true); - expect(loggingServiceMock.collect(logger).warn).toMatchInlineSnapshot(` + expect(loggingSystemMock.collect(logger).warn).toMatchInlineSnapshot(` Array [ Array [ "Using secure cookies, but SSL is not enabled inside Kibana. SSL must be configured outside of Kibana to function properly.", @@ -844,15 +860,15 @@ describe('createConfig()', () => { }); it('should set xpack.security.secureCookies if SSL is configured', async () => { - const logger = loggingServiceMock.create().get(); + const logger = loggingSystemMock.create().get(); const config = createConfig(ConfigSchema.validate({}), logger, { isTLSEnabled: true }); expect(config.secureCookies).toEqual(true); - expect(loggingServiceMock.collect(logger).warn).toEqual([]); + expect(loggingSystemMock.collect(logger).warn).toEqual([]); }); it('transforms legacy `authc.providers` into new format', () => { - const logger = loggingServiceMock.create().get(); + const logger = loggingSystemMock.create().get(); expect( createConfig( @@ -919,7 +935,7 @@ describe('createConfig()', () => { ConfigSchema.validate({ authc: { providers: ['saml', 'basic'], saml: { realm: 'saml-realm' } }, }), - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), { isTLSEnabled: true } ).authc.selector.enabled ).toBe(false); @@ -934,7 +950,7 @@ describe('createConfig()', () => { saml: { realm: 'saml-realm' }, }, }), - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), { isTLSEnabled: true } ).authc.selector.enabled ).toBe(true); @@ -954,7 +970,7 @@ describe('createConfig()', () => { }, }, }), - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), { isTLSEnabled: true } ).authc.selector.enabled ).toBe(false); @@ -971,7 +987,7 @@ describe('createConfig()', () => { }, }, }), - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), { isTLSEnabled: true } ).authc.selector.enabled ).toBe(true); @@ -989,7 +1005,7 @@ describe('createConfig()', () => { }, }, }), - loggingServiceMock.create().get(), + loggingSystemMock.create().get(), { isTLSEnabled: true } ).authc.sortedProviders ).toMatchInlineSnapshot(` diff --git a/x-pack/plugins/security/server/config.ts b/x-pack/plugins/security/server/config.ts index 8a7865fa17efcd..051a3d2ab13422 100644 --- a/x-pack/plugins/security/server/config.ts +++ b/x-pack/plugins/security/server/config.ts @@ -97,6 +97,7 @@ const providersConfigSchema = schema.object( ...getCommonProviderSchemaProperties(), realm: schema.string(), maxRedirectURLSize: schema.byteSize({ defaultValue: '2kb' }), + useRelayStateDeepLink: schema.boolean({ defaultValue: false }), }) ) ), diff --git a/x-pack/plugins/security/server/routes/authentication/common.ts b/x-pack/plugins/security/server/routes/authentication/common.ts index 91783140539a5b..ad38a158af2b93 100644 --- a/x-pack/plugins/security/server/routes/authentication/common.ts +++ b/x-pack/plugins/security/server/routes/authentication/common.ts @@ -31,7 +31,7 @@ export function defineCommonRoutes({ { path, // Allow unknown query parameters as this endpoint can be hit by the 3rd-party with any - // set of query string parameters (e.g. SAML/OIDC logout request parameters). + // set of query string parameters (e.g. SAML/OIDC logout request/response parameters). validate: { query: schema.object({}, { unknowns: 'allow' }) }, options: { authRequired: false }, }, diff --git a/x-pack/plugins/security/server/routes/authentication/saml.test.ts b/x-pack/plugins/security/server/routes/authentication/saml.test.ts index af63dfa2f44718..5f5161126f215d 100644 --- a/x-pack/plugins/security/server/routes/authentication/saml.test.ts +++ b/x-pack/plugins/security/server/routes/authentication/saml.test.ts @@ -62,9 +62,9 @@ describe('SAML authentication routes', () => { `"[SAMLResponse]: expected value of type [string] but got [undefined]"` ); - expect(() => - bodyValidator.validate({ SAMLResponse: 'saml-response', UnknownArg: 'arg' }) - ).toThrowErrorMatchingInlineSnapshot(`"[UnknownArg]: definition for this key is missing"`); + expect(bodyValidator.validate({ SAMLResponse: 'saml-response', UnknownArg: 'arg' })).toEqual({ + SAMLResponse: 'saml-response', + }); }); it('returns 500 if authentication throws unhandled exception.', async () => { @@ -174,5 +174,34 @@ describe('SAML authentication routes', () => { headers: { location: 'http://redirect-to/path' }, }); }); + + it('passes `RelayState` within login attempt.', async () => { + authc.login.mockResolvedValue(AuthenticationResult.redirectTo('http://redirect-to/path')); + + const redirectResponse = Symbol('error'); + const responseFactory = httpServerMock.createResponseFactory(); + responseFactory.redirected.mockReturnValue(redirectResponse as any); + + const request = httpServerMock.createKibanaRequest({ + body: { SAMLResponse: 'saml-response', RelayState: '/app/kibana' }, + }); + + await expect(routeHandler({} as any, request, responseFactory)).resolves.toBe( + redirectResponse + ); + + expect(authc.login).toHaveBeenCalledWith(request, { + provider: { type: 'saml' }, + value: { + type: SAMLLogin.LoginWithSAMLResponse, + samlResponse: 'saml-response', + relayState: '/app/kibana', + }, + }); + + expect(responseFactory.redirected).toHaveBeenCalledWith({ + headers: { location: 'http://redirect-to/path' }, + }); + }); }); }); diff --git a/x-pack/plugins/security/server/routes/authentication/saml.ts b/x-pack/plugins/security/server/routes/authentication/saml.ts index 30e1f6f336bdd3..ce7516c2c9d880 100644 --- a/x-pack/plugins/security/server/routes/authentication/saml.ts +++ b/x-pack/plugins/security/server/routes/authentication/saml.ts @@ -89,10 +89,10 @@ export function defineSAMLRoutes({ { path: '/api/security/saml/callback', validate: { - body: schema.object({ - SAMLResponse: schema.string(), - RelayState: schema.maybe(schema.string()), - }), + body: schema.object( + { SAMLResponse: schema.string(), RelayState: schema.maybe(schema.string()) }, + { unknowns: 'ignore' } + ), }, options: { authRequired: false, xsrfRequired: false }, }, @@ -101,7 +101,11 @@ export function defineSAMLRoutes({ // When authenticating using SAML we _expect_ to redirect to the Kibana target location. const authenticationResult = await authc.login(request, { provider: { type: SAMLAuthenticationProvider.type }, - value: { type: SAMLLogin.LoginWithSAMLResponse, samlResponse: request.body.SAMLResponse }, + value: { + type: SAMLLogin.LoginWithSAMLResponse, + samlResponse: request.body.SAMLResponse, + relayState: request.body.RelayState, + }, }); if (authenticationResult.redirected()) { diff --git a/x-pack/plugins/security/server/routes/index.mock.ts b/x-pack/plugins/security/server/routes/index.mock.ts index 1a93d6701e257d..c7ff2a1e68b027 100644 --- a/x-pack/plugins/security/server/routes/index.mock.ts +++ b/x-pack/plugins/security/server/routes/index.mock.ts @@ -7,7 +7,7 @@ import { elasticsearchServiceMock, httpServiceMock, - loggingServiceMock, + loggingSystemMock, httpResourcesMock, } from '../../../../../src/core/server/mocks'; import { authenticationMock } from '../authentication/index.mock'; @@ -20,9 +20,9 @@ export const routeDefinitionParamsMock = { router: httpServiceMock.createRouter(), basePath: httpServiceMock.createBasePath(), csp: httpServiceMock.createSetupContract().csp, - logger: loggingServiceMock.create().get(), + logger: loggingSystemMock.create().get(), clusterClient: elasticsearchServiceMock.createClusterClient(), - config: createConfig(ConfigSchema.validate(config), loggingServiceMock.create().get(), { + config: createConfig(ConfigSchema.validate(config), loggingSystemMock.create().get(), { isTLSEnabled: false, }), authc: authenticationMock.create(), diff --git a/x-pack/plugins/security_solution/common/constants.ts b/x-pack/plugins/security_solution/common/constants.ts index 0d162c068376fa..58431e405ea8b6 100644 --- a/x-pack/plugins/security_solution/common/constants.ts +++ b/x-pack/plugins/security_solution/common/constants.ts @@ -42,6 +42,9 @@ export const APP_TIMELINES_PATH = `${APP_PATH}/timelines`; export const APP_CASES_PATH = `${APP_PATH}/cases`; export const APP_MANAGEMENT_PATH = `${APP_PATH}/management`; +export const SHOW_ENDPOINT_ALERTS_NAV = true; +export const APP_ENDPOINT_ALERTS_PATH = `${APP_PATH}/endpoint-alerts`; + /** The comma-delimited list of Elasticsearch indices from which the SIEM app collects events */ export const DEFAULT_INDEX_PATTERN = [ 'apm-*-transaction*', diff --git a/x-pack/plugins/security_solution/cypress/integration/cases.spec.ts b/x-pack/plugins/security_solution/cypress/integration/cases.spec.ts index bb2dffe8ddd7de..efd9ece8aec566 100644 --- a/x-pack/plugins/security_solution/cypress/integration/cases.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/cases.spec.ts @@ -44,7 +44,7 @@ import { backToCases, createNewCase } from '../tasks/create_new_case'; import { loginAndWaitForPageWithoutDateRange } from '../tasks/login'; import { esArchiverLoad, esArchiverUnload } from '../tasks/es_archiver'; -import { CASES } from '../urls/navigation'; +import { CASES_URL } from '../urls/navigation'; describe('Cases', () => { before(() => { @@ -56,7 +56,7 @@ describe('Cases', () => { }); it('Creates a new case with timeline and opens the timeline', () => { - loginAndWaitForPageWithoutDateRange(CASES); + loginAndWaitForPageWithoutDateRange(CASES_URL); goToCreateNewCase(); createNewCase(case1); backToCases(); diff --git a/x-pack/plugins/security_solution/cypress/integration/cases_connectors.spec.ts b/x-pack/plugins/security_solution/cypress/integration/cases_connectors.spec.ts index 266d183ea1b858..ed885ad653e5de 100644 --- a/x-pack/plugins/security_solution/cypress/integration/cases_connectors.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/cases_connectors.spec.ts @@ -15,7 +15,7 @@ import { } from '../tasks/configure_cases'; import { loginAndWaitForPageWithoutDateRange } from '../tasks/login'; -import { CASES } from '../urls/navigation'; +import { CASES_URL } from '../urls/navigation'; describe('Cases connectors', () => { before(() => { @@ -25,7 +25,7 @@ describe('Cases connectors', () => { }); it('Configures a new connector', () => { - loginAndWaitForPageWithoutDateRange(CASES); + loginAndWaitForPageWithoutDateRange(CASES_URL); goToEditExternalConnection(); openAddNewConnectorOption(); addServiceNowConnector(serviceNowConnector); diff --git a/x-pack/plugins/security_solution/cypress/integration/events_viewer.spec.ts b/x-pack/plugins/security_solution/cypress/integration/events_viewer.spec.ts index b2d35f3f0c3361..cd4573817cc27c 100644 --- a/x-pack/plugins/security_solution/cypress/integration/events_viewer.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/events_viewer.spec.ts @@ -33,7 +33,7 @@ import { } from '../tasks/hosts/events'; import { clearSearchBar, kqlSearch } from '../tasks/security_header'; -import { HOSTS_PAGE } from '../urls/navigation'; +import { HOSTS_URL } from '../urls/navigation'; import { resetFields } from '../tasks/timeline'; const defaultHeadersInDefaultEcsCategory = [ @@ -49,7 +49,7 @@ const defaultHeadersInDefaultEcsCategory = [ describe('Events Viewer', () => { context('Fields rendering', () => { before(() => { - loginAndWaitForPage(HOSTS_PAGE); + loginAndWaitForPage(HOSTS_URL); openEvents(); }); @@ -75,7 +75,7 @@ describe('Events Viewer', () => { context('Events viewer query modal', () => { before(() => { - loginAndWaitForPage(HOSTS_PAGE); + loginAndWaitForPage(HOSTS_URL); openEvents(); }); @@ -93,7 +93,7 @@ describe('Events Viewer', () => { context('Events viewer fields behaviour', () => { before(() => { - loginAndWaitForPage(HOSTS_PAGE); + loginAndWaitForPage(HOSTS_URL); openEvents(); }); @@ -124,7 +124,7 @@ describe('Events Viewer', () => { context('Events behaviour', () => { before(() => { - loginAndWaitForPage(HOSTS_PAGE); + loginAndWaitForPage(HOSTS_URL); openEvents(); waitsForEventsToBeLoaded(); }); @@ -155,7 +155,7 @@ describe('Events Viewer', () => { context.skip('Events columns', () => { before(() => { - loginAndWaitForPage(HOSTS_PAGE); + loginAndWaitForPage(HOSTS_URL); openEvents(); waitsForEventsToBeLoaded(); }); diff --git a/x-pack/plugins/security_solution/cypress/integration/fields_browser.spec.ts b/x-pack/plugins/security_solution/cypress/integration/fields_browser.spec.ts index b0dbf94c0efb92..6438a738580b78 100644 --- a/x-pack/plugins/security_solution/cypress/integration/fields_browser.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/fields_browser.spec.ts @@ -31,7 +31,7 @@ import { loginAndWaitForPage } from '../tasks/login'; import { openTimeline } from '../tasks/security_main'; import { openTimelineFieldsBrowser, populateTimeline } from '../tasks/timeline'; -import { HOSTS_PAGE } from '../urls/navigation'; +import { HOSTS_URL } from '../urls/navigation'; const defaultHeaders = [ { id: '@timestamp' }, @@ -47,7 +47,7 @@ const defaultHeaders = [ describe('Fields Browser', () => { context('Fields Browser rendering', () => { before(() => { - loginAndWaitForPage(HOSTS_PAGE); + loginAndWaitForPage(HOSTS_URL); openTimeline(); populateTimeline(); openTimelineFieldsBrowser(); @@ -110,7 +110,7 @@ describe('Fields Browser', () => { context('Editing the timeline', () => { before(() => { - loginAndWaitForPage(HOSTS_PAGE); + loginAndWaitForPage(HOSTS_URL); openTimeline(); populateTimeline(); openTimelineFieldsBrowser(); diff --git a/x-pack/plugins/security_solution/cypress/integration/inspect.spec.ts b/x-pack/plugins/security_solution/cypress/integration/inspect.spec.ts index 0529c797ee07a5..53ddff501db823 100644 --- a/x-pack/plugins/security_solution/cypress/integration/inspect.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/inspect.spec.ts @@ -19,12 +19,12 @@ import { openTimelineSettings, } from '../tasks/timeline'; -import { HOSTS_PAGE, NETWORK_PAGE } from '../urls/navigation'; +import { HOSTS_URL, NETWORK_URL } from '../urls/navigation'; describe('Inspect', () => { context('Hosts stats and tables', () => { before(() => { - loginAndWaitForPage(HOSTS_PAGE); + loginAndWaitForPage(HOSTS_URL); }); afterEach(() => { closesModal(); @@ -40,7 +40,7 @@ describe('Inspect', () => { context('Network stats and tables', () => { before(() => { - loginAndWaitForPage(NETWORK_PAGE); + loginAndWaitForPage(NETWORK_URL); }); afterEach(() => { closesModal(); @@ -57,7 +57,7 @@ describe('Inspect', () => { context('Timeline', () => { it('inspects the timeline', () => { const hostExistsQuery = 'host.name: *'; - loginAndWaitForPage(HOSTS_PAGE); + loginAndWaitForPage(HOSTS_URL); openTimeline(); executeTimelineKQL(hostExistsQuery); openTimelineSettings(); diff --git a/x-pack/plugins/security_solution/cypress/integration/navigation.spec.ts b/x-pack/plugins/security_solution/cypress/integration/navigation.spec.ts index 28ae42f8c09746..ea3a78c77152a8 100644 --- a/x-pack/plugins/security_solution/cypress/integration/navigation.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/navigation.spec.ts @@ -16,45 +16,107 @@ import { import { loginAndWaitForPage } from '../tasks/login'; import { navigateFromHeaderTo } from '../tasks/security_header'; -import { TIMELINES_PAGE } from '../urls/navigation'; +import { + ALERTS_URL, + CASES_URL, + HOSTS_URL, + KIBANA_HOME, + MANAGEMENT_URL, + NETWORK_URL, + OVERVIEW_URL, + TIMELINES_URL, +} from '../urls/navigation'; +import { openKibanaNavigation, navigateFromKibanaCollapsibleTo } from '../tasks/kibana_navigation'; +import { + ALERTS_PAGE, + CASES_PAGE, + HOSTS_PAGE, + MANAGEMENT_PAGE, + NETWORK_PAGE, + OVERVIEW_PAGE, + TIMELINES_PAGE, +} from '../screens/kibana_navigation'; describe('top-level navigation common to all pages in the Security app', () => { before(() => { - loginAndWaitForPage(TIMELINES_PAGE); + loginAndWaitForPage(TIMELINES_URL); }); it('navigates to the Overview page', () => { navigateFromHeaderTo(OVERVIEW); - cy.url().should('include', '/security/overview'); + cy.url().should('include', OVERVIEW_URL); }); it('navigates to the Alerts page', () => { navigateFromHeaderTo(ALERTS); - cy.url().should('include', '/security/alerts'); + cy.url().should('include', ALERTS_URL); }); it('navigates to the Hosts page', () => { navigateFromHeaderTo(HOSTS); - cy.url().should('include', '/security/hosts'); + cy.url().should('include', HOSTS_URL); }); it('navigates to the Network page', () => { navigateFromHeaderTo(NETWORK); - cy.url().should('include', '/security/network'); + cy.url().should('include', NETWORK_URL); }); it('navigates to the Timelines page', () => { navigateFromHeaderTo(TIMELINES); - cy.url().should('include', '/security/timelines'); + cy.url().should('include', TIMELINES_URL); }); it('navigates to the Cases page', () => { navigateFromHeaderTo(CASES); - cy.url().should('include', '/security/cases'); + cy.url().should('include', CASES_URL); }); it('navigates to the Management page', () => { navigateFromHeaderTo(MANAGEMENT); - cy.url().should('include', '/security/management'); + cy.url().should('include', MANAGEMENT_URL); + }); +}); + +describe('Kibana navigation to all pages in the Security app ', () => { + before(() => { + loginAndWaitForPage(KIBANA_HOME); + }); + beforeEach(() => { + openKibanaNavigation(); + }); + it('navigates to the Overview page', () => { + navigateFromKibanaCollapsibleTo(OVERVIEW_PAGE); + cy.url().should('include', OVERVIEW_URL); + }); + + it('navigates to the Alerts page', () => { + navigateFromKibanaCollapsibleTo(ALERTS_PAGE); + cy.url().should('include', ALERTS_URL); + }); + + it('navigates to the Hosts page', () => { + navigateFromKibanaCollapsibleTo(HOSTS_PAGE); + cy.url().should('include', HOSTS_URL); + }); + + it('navigates to the Network page', () => { + navigateFromKibanaCollapsibleTo(NETWORK_PAGE); + cy.url().should('include', NETWORK_URL); + }); + + it('navigates to the Timelines page', () => { + navigateFromKibanaCollapsibleTo(TIMELINES_PAGE); + cy.url().should('include', TIMELINES_URL); + }); + + it('navigates to the Cases page', () => { + navigateFromKibanaCollapsibleTo(CASES_PAGE); + cy.url().should('include', CASES_URL); + }); + + it('navigates to the Management page', () => { + navigateFromKibanaCollapsibleTo(MANAGEMENT_PAGE); + cy.url().should('include', MANAGEMENT_URL); }); }); diff --git a/x-pack/plugins/security_solution/cypress/integration/overview.spec.ts b/x-pack/plugins/security_solution/cypress/integration/overview.spec.ts index 284deb67e1386b..b799d487acd086 100644 --- a/x-pack/plugins/security_solution/cypress/integration/overview.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/overview.spec.ts @@ -9,12 +9,12 @@ import { HOST_STATS, NETWORK_STATS } from '../screens/overview'; import { expandHostStats, expandNetworkStats } from '../tasks/overview'; import { loginAndWaitForPage } from '../tasks/login'; -import { OVERVIEW_PAGE } from '../urls/navigation'; +import { OVERVIEW_URL } from '../urls/navigation'; describe('Overview Page', () => { before(() => { cy.stubSecurityApi('overview'); - loginAndWaitForPage(OVERVIEW_PAGE); + loginAndWaitForPage(OVERVIEW_URL); }); it('Host stats render with correct values', () => { diff --git a/x-pack/plugins/security_solution/cypress/integration/search_bar.spec.ts b/x-pack/plugins/security_solution/cypress/integration/search_bar.spec.ts index 6428a855c84852..10759cc7de6e91 100644 --- a/x-pack/plugins/security_solution/cypress/integration/search_bar.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/search_bar.spec.ts @@ -9,13 +9,13 @@ import { openAddFilterPopover, fillAddFilterForm } from '../tasks/search_bar'; import { GLOBAL_SEARCH_BAR_FILTER_ITEM } from '../screens/search_bar'; import { hostIpFilter } from '../objects/filter'; -import { HOSTS_PAGE } from '../urls/navigation'; +import { HOSTS_URL } from '../urls/navigation'; import { waitForAllHostsToBeLoaded } from '../tasks/hosts/all_hosts'; // FAILING: https://github.com/elastic/kibana/issues/69595 describe.skip('SearchBar', () => { before(() => { - loginAndWaitForPage(HOSTS_PAGE); + loginAndWaitForPage(HOSTS_URL); waitForAllHostsToBeLoaded(); }); diff --git a/x-pack/plugins/security_solution/cypress/integration/timeline_data_providers.spec.ts b/x-pack/plugins/security_solution/cypress/integration/timeline_data_providers.spec.ts index 33394760c4da90..df0a26f3649c04 100644 --- a/x-pack/plugins/security_solution/cypress/integration/timeline_data_providers.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/timeline_data_providers.spec.ts @@ -22,11 +22,11 @@ import { loginAndWaitForPage } from '../tasks/login'; import { openTimeline } from '../tasks/security_main'; import { createNewTimeline } from '../tasks/timeline'; -import { HOSTS_PAGE } from '../urls/navigation'; +import { HOSTS_URL } from '../urls/navigation'; describe('timeline data providers', () => { before(() => { - loginAndWaitForPage(HOSTS_PAGE); + loginAndWaitForPage(HOSTS_URL); waitForAllHostsToBeLoaded(); }); diff --git a/x-pack/plugins/security_solution/cypress/integration/timeline_flyout_button.spec.ts b/x-pack/plugins/security_solution/cypress/integration/timeline_flyout_button.spec.ts index e462d6ade5dc44..87639f41d41097 100644 --- a/x-pack/plugins/security_solution/cypress/integration/timeline_flyout_button.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/timeline_flyout_button.spec.ts @@ -11,11 +11,11 @@ import { loginAndWaitForPage } from '../tasks/login'; import { openTimeline, openTimelineIfClosed } from '../tasks/security_main'; import { createNewTimeline } from '../tasks/timeline'; -import { HOSTS_PAGE } from '../urls/navigation'; +import { HOSTS_URL } from '../urls/navigation'; describe('timeline flyout button', () => { before(() => { - loginAndWaitForPage(HOSTS_PAGE); + loginAndWaitForPage(HOSTS_URL); waitForAllHostsToBeLoaded(); }); diff --git a/x-pack/plugins/security_solution/cypress/integration/timeline_local_storage.spec.ts b/x-pack/plugins/security_solution/cypress/integration/timeline_local_storage.spec.ts index a4352f58e6fc74..383ebe22205859 100644 --- a/x-pack/plugins/security_solution/cypress/integration/timeline_local_storage.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/timeline_local_storage.spec.ts @@ -6,7 +6,7 @@ import { reload } from '../tasks/common'; import { loginAndWaitForPage } from '../tasks/login'; -import { HOSTS_PAGE } from '../urls/navigation'; +import { HOSTS_URL } from '../urls/navigation'; import { openEvents } from '../tasks/hosts/main'; import { DRAGGABLE_HEADER } from '../screens/timeline'; import { TABLE_COLUMN_EVENTS_MESSAGE } from '../screens/hosts/external_events'; @@ -15,7 +15,7 @@ import { removeColumn, resetFields } from '../tasks/timeline'; describe('persistent timeline', () => { before(() => { - loginAndWaitForPage(HOSTS_PAGE); + loginAndWaitForPage(HOSTS_URL); openEvents(); waitsForEventsToBeLoaded(); }); diff --git a/x-pack/plugins/security_solution/cypress/integration/timeline_search_or_filter.spec.ts b/x-pack/plugins/security_solution/cypress/integration/timeline_search_or_filter.spec.ts index 00994f7a87a7bf..a2e2a72a17946b 100644 --- a/x-pack/plugins/security_solution/cypress/integration/timeline_search_or_filter.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/timeline_search_or_filter.spec.ts @@ -10,11 +10,11 @@ import { loginAndWaitForPage } from '../tasks/login'; import { openTimeline } from '../tasks/security_main'; import { executeTimelineKQL } from '../tasks/timeline'; -import { HOSTS_PAGE } from '../urls/navigation'; +import { HOSTS_URL } from '../urls/navigation'; describe('timeline search or filter KQL bar', () => { beforeEach(() => { - loginAndWaitForPage(HOSTS_PAGE); + loginAndWaitForPage(HOSTS_URL); }); it('executes a KQL query', () => { diff --git a/x-pack/plugins/security_solution/cypress/integration/timeline_toggle_column.spec.ts b/x-pack/plugins/security_solution/cypress/integration/timeline_toggle_column.spec.ts index 841d41782b3509..12e6f3db9b61e3 100644 --- a/x-pack/plugins/security_solution/cypress/integration/timeline_toggle_column.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/timeline_toggle_column.spec.ts @@ -22,11 +22,11 @@ import { uncheckTimestampToggleField, } from '../tasks/timeline'; -import { HOSTS_PAGE } from '../urls/navigation'; +import { HOSTS_URL } from '../urls/navigation'; describe('toggle column in timeline', () => { before(() => { - loginAndWaitForPage(HOSTS_PAGE); + loginAndWaitForPage(HOSTS_URL); }); beforeEach(() => { diff --git a/x-pack/plugins/security_solution/cypress/integration/url_state.spec.ts b/x-pack/plugins/security_solution/cypress/integration/url_state.spec.ts index f7717ce3a617cc..6b69c3e1d14c3a 100644 --- a/x-pack/plugins/security_solution/cypress/integration/url_state.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/url_state.spec.ts @@ -38,7 +38,7 @@ import { executeTimelineKQL, } from '../tasks/timeline'; -import { HOSTS_PAGE } from '../urls/navigation'; +import { HOSTS_URL } from '../urls/navigation'; import { ABSOLUTE_DATE_RANGE } from '../urls/state'; const ABSOLUTE_DATE = { diff --git a/x-pack/plugins/security_solution/cypress/screens/kibana_navigation.ts b/x-pack/plugins/security_solution/cypress/screens/kibana_navigation.ts new file mode 100644 index 00000000000000..2f7956ce370bc4 --- /dev/null +++ b/x-pack/plugins/security_solution/cypress/screens/kibana_navigation.ts @@ -0,0 +1,22 @@ +/* + * 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 ALERTS_PAGE = '[data-test-subj="collapsibleNavGroup-security"] [title="Alerts"]'; + +export const CASES_PAGE = '[data-test-subj="collapsibleNavGroup-security"] [title="Cases"]'; + +export const HOSTS_PAGE = '[data-test-subj="collapsibleNavGroup-security"] [title="Hosts"]'; + +export const KIBANA_NAVIGATION_TOGGLE = '[data-test-subj="toggleNavButton"]'; + +export const MANAGEMENT_PAGE = + '[data-test-subj="collapsibleNavGroup-security"] [title="Management"]'; + +export const NETWORK_PAGE = '[data-test-subj="collapsibleNavGroup-security"] [title="Network"]'; + +export const OVERVIEW_PAGE = '[data-test-subj="collapsibleNavGroup-security"] [title="Overview"]'; + +export const TIMELINES_PAGE = '[data-test-subj="collapsibleNavGroup-security"] [title="Timelines"]'; diff --git a/x-pack/plugins/security_solution/cypress/tasks/kibana_navigation.ts b/x-pack/plugins/security_solution/cypress/tasks/kibana_navigation.ts new file mode 100644 index 00000000000000..2d5b5d0de39d28 --- /dev/null +++ b/x-pack/plugins/security_solution/cypress/tasks/kibana_navigation.ts @@ -0,0 +1,15 @@ +/* + * 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 { KIBANA_NAVIGATION_TOGGLE } from '../screens/kibana_navigation'; + +export const navigateFromKibanaCollapsibleTo = (page: string) => { + cy.get(page).click(); +}; + +export const openKibanaNavigation = () => { + cy.get(KIBANA_NAVIGATION_TOGGLE).click(); +}; diff --git a/x-pack/plugins/security_solution/cypress/urls/navigation.ts b/x-pack/plugins/security_solution/cypress/urls/navigation.ts index 7978aebfb413bd..9da9abf388e4d8 100644 --- a/x-pack/plugins/security_solution/cypress/urls/navigation.ts +++ b/x-pack/plugins/security_solution/cypress/urls/navigation.ts @@ -5,9 +5,9 @@ */ export const ALERTS_URL = 'app/security/alerts'; -export const CASES = '/app/security/cases'; +export const CASES_URL = '/app/security/cases'; export const DETECTIONS = '/app/siem#/detections'; -export const HOSTS_PAGE = '/app/security/hosts/allHosts'; +export const HOSTS_URL = '/app/security/hosts/allHosts'; export const HOSTS_PAGE_TAB_URLS = { allHosts: '/app/security/hosts/allHosts', anomalies: '/app/security/hosts/anomalies', @@ -15,6 +15,8 @@ export const HOSTS_PAGE_TAB_URLS = { events: '/app/security/hosts/events', uncommonProcesses: '/app/security/hosts/uncommonProcesses', }; -export const NETWORK_PAGE = '/app/security/network'; -export const OVERVIEW_PAGE = '/app/security/overview'; -export const TIMELINES_PAGE = '/app/security/timelines'; +export const KIBANA_HOME = '/app/home#/'; +export const MANAGEMENT_URL = '/app/security/management'; +export const NETWORK_URL = '/app/security/network'; +export const OVERVIEW_URL = '/app/security/overview'; +export const TIMELINES_URL = '/app/security/timelines'; diff --git a/x-pack/plugins/security_solution/public/app/home/home_navigations.tsx b/x-pack/plugins/security_solution/public/app/home/home_navigations.tsx index 88e9d4179a9714..8839919af20604 100644 --- a/x-pack/plugins/security_solution/public/app/home/home_navigations.tsx +++ b/x-pack/plugins/security_solution/public/app/home/home_navigations.tsx @@ -15,6 +15,7 @@ import { APP_TIMELINES_PATH, APP_CASES_PATH, APP_MANAGEMENT_PATH, + APP_ENDPOINT_ALERTS_PATH, } from '../../../common/constants'; export const navTabs: SiemNavTab = { @@ -68,4 +69,11 @@ export const navTabs: SiemNavTab = { disabled: false, urlKey: SecurityPageName.management, }, + [SecurityPageName.endpointAlerts]: { + id: SecurityPageName.endpointAlerts, + name: 'Endpoint Alerts', // No Need of i18n since, it is just temporary + href: APP_ENDPOINT_ALERTS_PATH, + disabled: false, + urlKey: SecurityPageName.management, // Just to make type happy, this should go away soon + }, }; diff --git a/x-pack/plugins/security_solution/public/app/home/setup.tsx b/x-pack/plugins/security_solution/public/app/home/setup.tsx index 5b977a83302a97..bf7ce2ddf8b509 100644 --- a/x-pack/plugins/security_solution/public/app/home/setup.tsx +++ b/x-pack/plugins/security_solution/public/app/home/setup.tsx @@ -32,20 +32,7 @@ export const Setup: React.FunctionComponent<{ }); }; - const displayToast = () => { - notifications.toasts.addDanger({ - title, - text: defaultText, - }); - }; - - if (!ingestManager.success) { - if (ingestManager.error) { - displayToastWithModal(ingestManager.error.message); - } else { - displayToast(); - } - } + ingestManager.success.catch((error: Error) => displayToastWithModal(error.message)); }, [ingestManager, notifications.toasts]); return null; diff --git a/x-pack/plugins/security_solution/public/app/types.ts b/x-pack/plugins/security_solution/public/app/types.ts index 4bd888e87bbdc7..866a19b15771eb 100644 --- a/x-pack/plugins/security_solution/public/app/types.ts +++ b/x-pack/plugins/security_solution/public/app/types.ts @@ -27,6 +27,7 @@ export enum SecurityPageName { timelines = 'timelines', case = 'case', management = 'management', + endpointAlerts = 'endpointAlerts', } export interface SecuritySubPluginStore { initialState: Record; diff --git a/x-pack/plugins/security_solution/public/common/components/drag_and_drop/drag_drop_context_wrapper.tsx b/x-pack/plugins/security_solution/public/common/components/drag_and_drop/drag_drop_context_wrapper.tsx index 32f05e7c837a7c..3edc1d0d84b692 100644 --- a/x-pack/plugins/security_solution/public/common/components/drag_and_drop/drag_drop_context_wrapper.tsx +++ b/x-pack/plugins/security_solution/public/common/components/drag_and_drop/drag_drop_context_wrapper.tsx @@ -168,18 +168,6 @@ export const DragDropContextWrapper = connector(DragDropContextWrapperComponent) DragDropContextWrapper.displayName = 'DragDropContextWrapper'; const onBeforeCapture = (before: BeforeCapture) => { - const x = - window.pageXOffset !== undefined - ? window.pageXOffset - : (document.documentElement || document.body.parentNode || document.body).scrollLeft; - - const y = - window.pageYOffset !== undefined - ? window.pageYOffset - : (document.documentElement || document.body.parentNode || document.body).scrollTop; - - window.onscroll = () => window.scrollTo(x, y); - if (!draggableIsField(before)) { document.body.classList.add(IS_DRAGGING_CLASS_NAME); } diff --git a/x-pack/plugins/security_solution/public/common/components/navigation/index.test.tsx b/x-pack/plugins/security_solution/public/common/components/navigation/index.test.tsx index a99497090f8435..cab4ef8ead63f9 100644 --- a/x-pack/plugins/security_solution/public/common/components/navigation/index.test.tsx +++ b/x-pack/plugins/security_solution/public/common/components/navigation/index.test.tsx @@ -140,6 +140,13 @@ describe('SIEM Navigation', () => { name: 'Timelines', urlKey: 'timeline', }, + endpointAlerts: { + disabled: false, + href: '/app/security/endpoint-alerts', + id: 'endpointAlerts', + name: 'Endpoint Alerts', + urlKey: 'management', + }, }, pageName: 'hosts', pathName: '/', @@ -185,7 +192,7 @@ describe('SIEM Navigation', () => { wrapper.setProps({ pageName: 'network', pathName: '/', - tabName: undefined, + tabName: 'authentications', }); wrapper.update(); expect(setBreadcrumbs).toHaveBeenNthCalledWith( @@ -209,7 +216,13 @@ describe('SIEM Navigation', () => { name: 'Cases', urlKey: 'case', }, - + endpointAlerts: { + disabled: false, + href: '/app/security/endpoint-alerts', + id: 'endpointAlerts', + name: 'Endpoint Alerts', + urlKey: 'management', + }, hosts: { disabled: false, href: '/app/security/hosts', @@ -252,7 +265,7 @@ describe('SIEM Navigation', () => { savedQuery: undefined, search: '', state: undefined, - tabName: undefined, + tabName: 'authentications', timeline: { id: '', isOpen: false }, timerange: { global: { diff --git a/x-pack/plugins/security_solution/public/common/components/navigation/types.ts b/x-pack/plugins/security_solution/public/common/components/navigation/types.ts index 80302be18355c4..a870c790527b75 100644 --- a/x-pack/plugins/security_solution/public/common/components/navigation/types.ts +++ b/x-pack/plugins/security_solution/public/common/components/navigation/types.ts @@ -48,7 +48,8 @@ export type SiemNavTabKey = | SecurityPageName.alerts | SecurityPageName.timelines | SecurityPageName.case - | SecurityPageName.management; + | SecurityPageName.management + | SecurityPageName.endpointAlerts; export type SiemNavTab = Record; diff --git a/x-pack/plugins/security_solution/public/common/mock/endpoint/dependencies_start_mock.ts b/x-pack/plugins/security_solution/public/common/mock/endpoint/dependencies_start_mock.ts index 759ec45c7e54be..f2e8d045eccf9f 100644 --- a/x-pack/plugins/security_solution/public/common/mock/endpoint/dependencies_start_mock.ts +++ b/x-pack/plugins/security_solution/public/common/mock/endpoint/dependencies_start_mock.ts @@ -56,6 +56,6 @@ export const depsStartMock: () => DepsStartMock = () => { return { data: dataMock, - ingestManager: { success: true, registerDatasource }, + ingestManager: { success: Promise.resolve(true), registerDatasource }, }; }; diff --git a/x-pack/plugins/security_solution/public/endpoint_alerts/routes.tsx b/x-pack/plugins/security_solution/public/endpoint_alerts/routes.tsx index acc6a82e29a2cf..1c92919aa982fe 100644 --- a/x-pack/plugins/security_solution/public/endpoint_alerts/routes.tsx +++ b/x-pack/plugins/security_solution/public/endpoint_alerts/routes.tsx @@ -11,7 +11,7 @@ import { AlertIndex } from './view'; export const EndpointAlertsRoutes: React.FC = () => ( - + diff --git a/x-pack/plugins/security_solution/public/endpoint_alerts/store/selectors.ts b/x-pack/plugins/security_solution/public/endpoint_alerts/store/selectors.ts index ab0e4165a25771..878c5f4fd2bb85 100644 --- a/x-pack/plugins/security_solution/public/endpoint_alerts/store/selectors.ts +++ b/x-pack/plugins/security_solution/public/endpoint_alerts/store/selectors.ts @@ -44,7 +44,10 @@ export const alertListPagination = createStructuredSelector({ * Returns a boolean based on whether or not the user is on the alerts page */ export const isOnAlertPage = (state: Immutable): boolean => { - return state.location ? state.location.pathname === '/endpoint-alerts' : false; + return state.location + ? state.location.pathname === '/endpoint-alerts' || + window.location.pathname.includes('/endpoint-alerts') + : false; }; /** diff --git a/x-pack/plugins/security_solution/public/management/pages/policy/store/policy_details/middleware.ts b/x-pack/plugins/security_solution/public/management/pages/policy/store/policy_details/middleware.ts index ec0c526482b453..899f85ecdea306 100644 --- a/x-pack/plugins/security_solution/public/management/pages/policy/store/policy_details/middleware.ts +++ b/x-pack/plugins/security_solution/public/management/pages/policy/store/policy_details/middleware.ts @@ -17,7 +17,6 @@ import { sendPutDatasource, } from '../policy_list/services/ingest'; import { NewPolicyData, PolicyData } from '../../../../../../common/endpoint/types'; -import { factory as policyConfigFactory } from '../../../../../../common/endpoint/models/policy_config'; import { ImmutableMiddlewareFactory } from '../../../../../common/store'; export const policyDetailsMiddlewareFactory: ImmutableMiddlewareFactory = ( @@ -43,23 +42,6 @@ export const policyDetailsMiddlewareFactory: ImmutableMiddlewareFactory { ]); }); - test('finds glob-only index patterns ', () => { + test('excludes glob-only index patterns', () => { const matchingIndexPatterns = findMatchingIndexPatterns({ kibanaIndexPatterns: [mockGlobIndexPattern, mockFilebeatIndexPattern], siemDefaultIndices, }); - expect(matchingIndexPatterns).toEqual([mockGlobIndexPattern, mockFilebeatIndexPattern]); + expect(matchingIndexPatterns).toEqual([mockFilebeatIndexPattern]); + }); + + test('excludes glob-only CCS index patterns', () => { + const matchingIndexPatterns = findMatchingIndexPatterns({ + kibanaIndexPatterns: [mockCCSGlobIndexPattern, mockFilebeatIndexPattern], + siemDefaultIndices, + }); + expect(matchingIndexPatterns).toEqual([mockFilebeatIndexPattern]); }); }); }); diff --git a/x-pack/plugins/security_solution/public/network/components/embeddables/embedded_map_helpers.tsx b/x-pack/plugins/security_solution/public/network/components/embeddables/embedded_map_helpers.tsx index e50dcd7a8c8d83..b0f8e2cc024033 100644 --- a/x-pack/plugins/security_solution/public/network/components/embeddables/embedded_map_helpers.tsx +++ b/x-pack/plugins/security_solution/public/network/components/embeddables/embedded_map_helpers.tsx @@ -128,6 +128,9 @@ export const createEmbeddable = async ( return embeddableObject; }; +// These patterns are overly greedy and must be excluded when matching against Security indexes. +const ignoredIndexPatterns = ['*', '*:*']; + /** * Returns kibanaIndexPatterns that wildcard match at least one of siemDefaultIndices * @@ -142,9 +145,13 @@ export const findMatchingIndexPatterns = ({ siemDefaultIndices: string[]; }): IndexPatternSavedObject[] => { try { - return kibanaIndexPatterns.filter((kip) => - siemDefaultIndices.some((sdi) => minimatch(sdi, kip.attributes.title)) - ); + return kibanaIndexPatterns.filter((kip) => { + const pattern = kip.attributes.title; + return ( + !ignoredIndexPatterns.includes(pattern) && + siemDefaultIndices.some((sdi) => minimatch(sdi, pattern)) + ); + }); } catch { return []; } diff --git a/x-pack/plugins/security_solution/public/plugin.tsx b/x-pack/plugins/security_solution/public/plugin.tsx index 58f0a0ddb749e9..360c81abadc810 100644 --- a/x-pack/plugins/security_solution/public/plugin.tsx +++ b/x-pack/plugins/security_solution/public/plugin.tsx @@ -33,6 +33,8 @@ import { APP_TIMELINES_PATH, APP_MANAGEMENT_PATH, APP_CASES_PATH, + SHOW_ENDPOINT_ALERTS_NAV, + APP_ENDPOINT_ALERTS_PATH, } from '../common/constants'; import { ConfigureEndpointDatasource } from './management/pages/policy/view/ingest_manager_integration/configure_datasource'; @@ -290,6 +292,35 @@ export class Plugin implements IPlugin { + const [ + { coreStart, startPlugins, store, services }, + { renderApp, composeLibs }, + { endpointAlertsSubPlugin }, + ] = await Promise.all([ + mountSecurityFactory(), + this.downloadAssets(), + this.downloadSubPlugins(), + ]); + return renderApp({ + ...composeLibs(coreStart), + ...params, + services, + store, + SubPluginRoutes: endpointAlertsSubPlugin.start(coreStart, startPlugins).SubPluginRoutes, + }); + }, + }); + } + core.application.register({ id: 'siem', appRoute: 'app/siem', diff --git a/x-pack/plugins/security_solution/scripts/optimize_tsconfig/tsconfig.json b/x-pack/plugins/security_solution/scripts/optimize_tsconfig/tsconfig.json index dcce9746086e00..ea7a11b89dab2b 100644 --- a/x-pack/plugins/security_solution/scripts/optimize_tsconfig/tsconfig.json +++ b/x-pack/plugins/security_solution/scripts/optimize_tsconfig/tsconfig.json @@ -1,6 +1,7 @@ { "include": [ "typings/**/*", + "plugins/lists/**/*", "plugins/security_solution/**/*", "plugins/apm/typings/numeral.d.ts", "plugins/canvas/types/webpack.d.ts", diff --git a/x-pack/plugins/security_solution/server/endpoint/alerts/handlers/alerts.test.ts b/x-pack/plugins/security_solution/server/endpoint/alerts/handlers/alerts.test.ts index fd785bca4aa246..6c0ff9fcdc66f7 100644 --- a/x-pack/plugins/security_solution/server/endpoint/alerts/handlers/alerts.test.ts +++ b/x-pack/plugins/security_solution/server/endpoint/alerts/handlers/alerts.test.ts @@ -7,11 +7,11 @@ import { IClusterClient, IRouter, IScopedClusterClient } from 'kibana/server'; import { elasticsearchServiceMock, httpServiceMock, - loggingServiceMock, + loggingSystemMock, } from '../../../../../../../src/core/server/mocks'; import { registerAlertRoutes } from '../routes'; import { alertingIndexGetQuerySchema } from '../../../../common/endpoint_alerts/schema/alert_index'; -import { createMockAgentService } from '../../mocks'; +import { createMockEndpointAppContextServiceStartContract } from '../../mocks'; import { EndpointAppContextService } from '../../endpoint_app_context_services'; import { createMockConfig } from '../../../lib/detection_engine/routes/__mocks__'; @@ -28,12 +28,10 @@ describe('test alerts route', () => { routerMock = httpServiceMock.createRouter(); endpointAppContextService = new EndpointAppContextService(); - endpointAppContextService.start({ - agentService: createMockAgentService(), - }); + endpointAppContextService.start(createMockEndpointAppContextServiceStartContract()); registerAlertRoutes(routerMock, { - logFactory: loggingServiceMock.create(), + logFactory: loggingSystemMock.create(), service: endpointAppContextService, config: () => Promise.resolve(createMockConfig()), }); diff --git a/x-pack/plugins/security_solution/server/endpoint/endpoint_app_context_services.ts b/x-pack/plugins/security_solution/server/endpoint/endpoint_app_context_services.ts index cb8c913a73b8e8..7b8a368b6c9757 100644 --- a/x-pack/plugins/security_solution/server/endpoint/endpoint_app_context_services.ts +++ b/x-pack/plugins/security_solution/server/endpoint/endpoint_app_context_services.ts @@ -3,7 +3,15 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { AgentService } from '../../../ingest_manager/server'; +import { AgentService, IngestManagerStartContract } from '../../../ingest_manager/server'; +import { handleDatasourceCreate } from './ingest_integration'; + +export type EndpointAppContextServiceStartContract = Pick< + IngestManagerStartContract, + 'agentService' +> & { + registerIngestCallback: IngestManagerStartContract['registerExternalCallback']; +}; /** * A singleton that holds shared services that are initialized during the start up phase @@ -12,8 +20,9 @@ import { AgentService } from '../../../ingest_manager/server'; export class EndpointAppContextService { private agentService: AgentService | undefined; - public start(dependencies: { agentService: AgentService }) { + public start(dependencies: EndpointAppContextServiceStartContract) { this.agentService = dependencies.agentService; + dependencies.registerIngestCallback('datasourceCreate', handleDatasourceCreate); } public stop() {} diff --git a/x-pack/plugins/security_solution/server/endpoint/ingest_integration.ts b/x-pack/plugins/security_solution/server/endpoint/ingest_integration.ts new file mode 100644 index 00000000000000..6ff0949311587f --- /dev/null +++ b/x-pack/plugins/security_solution/server/endpoint/ingest_integration.ts @@ -0,0 +1,49 @@ +/* + * 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 { factory as policyConfigFactory } from '../../common/endpoint/models/policy_config'; +import { NewPolicyData } from '../../common/endpoint/types'; +import { NewDatasource } from '../../../ingest_manager/common/types/models'; + +/** + * Callback to handle creation of Datasources in Ingest Manager + * @param newDatasource + */ +export const handleDatasourceCreate = async ( + newDatasource: NewDatasource +): Promise => { + // We only care about Endpoint datasources + if (newDatasource.package?.name !== 'endpoint') { + return newDatasource; + } + + // We cast the type here so that any changes to the Endpoint specific data + // follow the types/schema expected + let updatedDatasource = newDatasource as NewPolicyData; + + // Until we get the Default Policy Configuration in the Endpoint package, + // we will add it here manually at creation time. + // @ts-ignore + if (newDatasource.inputs.length === 0) { + updatedDatasource = { + ...newDatasource, + inputs: [ + { + type: 'endpoint', + enabled: true, + streams: [], + config: { + policy: { + value: policyConfigFactory(), + }, + }, + }, + ], + }; + } + + return updatedDatasource; +}; diff --git a/x-pack/plugins/security_solution/server/endpoint/mocks.ts b/x-pack/plugins/security_solution/server/endpoint/mocks.ts index b10e9e4dc90e77..5435eff4ef1507 100644 --- a/x-pack/plugins/security_solution/server/endpoint/mocks.ts +++ b/x-pack/plugins/security_solution/server/endpoint/mocks.ts @@ -6,7 +6,28 @@ import { IScopedClusterClient, SavedObjectsClientContract } from 'kibana/server'; import { xpackMocks } from '../../../../mocks'; -import { AgentService, IngestManagerStartContract } from '../../../ingest_manager/server'; +import { + AgentService, + IngestManagerStartContract, + ExternalCallback, +} from '../../../ingest_manager/server'; +import { EndpointAppContextServiceStartContract } from './endpoint_app_context_services'; +import { createDatasourceServiceMock } from '../../../ingest_manager/server/mocks'; + +/** + * Crates a mocked input contract for the `EndpointAppContextService#start()` method + */ +export const createMockEndpointAppContextServiceStartContract = (): jest.Mocked< + EndpointAppContextServiceStartContract +> => { + return { + agentService: createMockAgentService(), + registerIngestCallback: jest.fn< + ReturnType, + Parameters + >(), + }; +}; /** * Creates a mock AgentService @@ -32,6 +53,8 @@ export const createMockIngestManagerStartContract = ( getESIndexPattern: jest.fn().mockResolvedValue(indexPattern), }, agentService: createMockAgentService(), + registerExternalCallback: jest.fn((...args: ExternalCallback) => {}), + datasourceService: createDatasourceServiceMock(), }; }; diff --git a/x-pack/plugins/security_solution/server/endpoint/routes/metadata/metadata.test.ts b/x-pack/plugins/security_solution/server/endpoint/routes/metadata/metadata.test.ts index 92835dc5329ce3..c04975fa8b28e0 100644 --- a/x-pack/plugins/security_solution/server/endpoint/routes/metadata/metadata.test.ts +++ b/x-pack/plugins/security_solution/server/endpoint/routes/metadata/metadata.test.ts @@ -16,7 +16,7 @@ import { elasticsearchServiceMock, httpServerMock, httpServiceMock, - loggingServiceMock, + loggingSystemMock, savedObjectsClientMock, } from '../../../../../../../src/core/server/mocks'; import { @@ -27,8 +27,10 @@ import { } from '../../../../common/endpoint/types'; import { SearchResponse } from 'elasticsearch'; import { registerEndpointRoutes } from './index'; -import { createMockAgentService, createRouteHandlerContext } from '../../mocks'; -import { AgentService } from '../../../../../ingest_manager/server'; +import { + createMockEndpointAppContextServiceStartContract, + createRouteHandlerContext, +} from '../../mocks'; import Boom from 'boom'; import { EndpointAppContextService } from '../../endpoint_app_context_services'; import { createMockConfig } from '../../../lib/detection_engine/routes/__mocks__'; @@ -44,7 +46,9 @@ describe('test endpoint route', () => { let routeHandler: RequestHandler; // eslint-disable-next-line @typescript-eslint/no-explicit-any let routeConfig: RouteConfig; - let mockAgentService: jest.Mocked; + let mockAgentService: ReturnType< + typeof createMockEndpointAppContextServiceStartContract + >['agentService']; let endpointAppContextService: EndpointAppContextService; beforeEach(() => { @@ -56,14 +60,13 @@ describe('test endpoint route', () => { mockClusterClient.asScoped.mockReturnValue(mockScopedClient); routerMock = httpServiceMock.createRouter(); mockResponse = httpServerMock.createResponseFactory(); - mockAgentService = createMockAgentService(); endpointAppContextService = new EndpointAppContextService(); - endpointAppContextService.start({ - agentService: mockAgentService, - }); + const startContract = createMockEndpointAppContextServiceStartContract(); + endpointAppContextService.start(startContract); + mockAgentService = startContract.agentService; registerEndpointRoutes(routerMock, { - logFactory: loggingServiceMock.create(), + logFactory: loggingSystemMock.create(), service: endpointAppContextService, config: () => Promise.resolve(createMockConfig()), }); diff --git a/x-pack/plugins/security_solution/server/endpoint/routes/metadata/query_builders.test.ts b/x-pack/plugins/security_solution/server/endpoint/routes/metadata/query_builders.test.ts index 9e9eaafd0f1dea..f83fb5b4a5a117 100644 --- a/x-pack/plugins/security_solution/server/endpoint/routes/metadata/query_builders.test.ts +++ b/x-pack/plugins/security_solution/server/endpoint/routes/metadata/query_builders.test.ts @@ -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 { httpServerMock, loggingServiceMock } from '../../../../../../../src/core/server/mocks'; +import { httpServerMock, loggingSystemMock } from '../../../../../../../src/core/server/mocks'; import { kibanaRequestToMetadataListESQuery, getESQueryHostMetadataByID } from './query_builders'; import { EndpointAppContextService } from '../../endpoint_app_context_services'; import { createMockConfig } from '../../../lib/detection_engine/routes/__mocks__'; @@ -18,7 +18,7 @@ describe('query builder', () => { const query = await kibanaRequestToMetadataListESQuery( mockRequest, { - logFactory: loggingServiceMock.create(), + logFactory: loggingSystemMock.create(), service: new EndpointAppContextService(), config: () => Promise.resolve(createMockConfig()), }, @@ -70,7 +70,7 @@ describe('query builder', () => { const query = await kibanaRequestToMetadataListESQuery( mockRequest, { - logFactory: loggingServiceMock.create(), + logFactory: loggingSystemMock.create(), service: new EndpointAppContextService(), config: () => Promise.resolve(createMockConfig()), }, diff --git a/x-pack/plugins/security_solution/server/endpoint/routes/policy/handlers.test.ts b/x-pack/plugins/security_solution/server/endpoint/routes/policy/handlers.test.ts index 2b94fe3576e2dc..16af3a95bc72da 100644 --- a/x-pack/plugins/security_solution/server/endpoint/routes/policy/handlers.test.ts +++ b/x-pack/plugins/security_solution/server/endpoint/routes/policy/handlers.test.ts @@ -4,7 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ import { EndpointAppContextService } from '../../endpoint_app_context_services'; -import { createMockAgentService, createRouteHandlerContext } from '../../mocks'; +import { + createMockEndpointAppContextServiceStartContract, + createRouteHandlerContext, +} from '../../mocks'; import { getHostPolicyResponseHandler } from './handlers'; import { IScopedClusterClient, @@ -14,10 +17,9 @@ import { import { elasticsearchServiceMock, httpServerMock, - loggingServiceMock, + loggingSystemMock, savedObjectsClientMock, } from '../../../../../../../src/core/server/mocks'; -import { AgentService } from '../../../../../ingest_manager/server/services'; import { SearchResponse } from 'elasticsearch'; import { GetHostPolicyResponse, HostPolicyResponse } from '../../../../common/endpoint/types'; import { EndpointDocGenerator } from '../../../../common/endpoint/generate_data'; @@ -28,17 +30,13 @@ describe('test policy response handler', () => { let mockScopedClient: jest.Mocked; let mockSavedObjectClient: jest.Mocked; let mockResponse: jest.Mocked; - let mockAgentService: jest.Mocked; beforeEach(() => { mockScopedClient = elasticsearchServiceMock.createScopedClusterClient(); mockSavedObjectClient = savedObjectsClientMock.create(); mockResponse = httpServerMock.createResponseFactory(); endpointAppContextService = new EndpointAppContextService(); - mockAgentService = createMockAgentService(); - endpointAppContextService.start({ - agentService: mockAgentService, - }); + endpointAppContextService.start(createMockEndpointAppContextServiceStartContract()); }); afterEach(() => endpointAppContextService.stop()); @@ -46,7 +44,7 @@ describe('test policy response handler', () => { it('should return the latest policy response for a host', async () => { const response = createSearchResponse(new EndpointDocGenerator().generatePolicyResponse()); const hostPolicyResponseHandler = getHostPolicyResponseHandler({ - logFactory: loggingServiceMock.create(), + logFactory: loggingSystemMock.create(), service: endpointAppContextService, config: () => Promise.resolve(createMockConfig()), }); @@ -69,7 +67,7 @@ describe('test policy response handler', () => { it('should return not found when there is no response policy for host', async () => { const hostPolicyResponseHandler = getHostPolicyResponseHandler({ - logFactory: loggingServiceMock.create(), + logFactory: loggingSystemMock.create(), service: endpointAppContextService, config: () => Promise.resolve(createMockConfig()), }); diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/notifications/rules_notification_alert_type.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/notifications/rules_notification_alert_type.test.ts index 47356679c8075e..3eefd3e665cd62 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/notifications/rules_notification_alert_type.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/notifications/rules_notification_alert_type.test.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { loggingServiceMock } from 'src/core/server/mocks'; +import { loggingSystemMock } from 'src/core/server/mocks'; import { getResult } from '../routes/__mocks__/request_responses'; import { rulesNotificationAlertType } from './rules_notification_alert_type'; import { buildSignalsSearchQuery } from './build_signals_query'; @@ -15,12 +15,12 @@ jest.mock('./build_signals_query'); describe('rules_notification_alert_type', () => { let payload: NotificationExecutorOptions; let alert: ReturnType; - let logger: ReturnType; + let logger: ReturnType; let alertServices: AlertServicesMock; beforeEach(() => { alertServices = alertsMock.createAlertServices(); - logger = loggingServiceMock.createLogger(); + logger = loggingSystemMock.createLogger(); payload = { alertId: '1111', diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/notifications/types.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/notifications/types.test.ts index 0c9ccf069b3b62..03d08625000eca 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/notifications/types.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/notifications/types.test.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { loggingServiceMock } from 'src/core/server/mocks'; +import { loggingSystemMock } from 'src/core/server/mocks'; import { getNotificationResult, getResult } from '../routes/__mocks__/request_responses'; import { isAlertTypes, isNotificationAlertExecutor } from './types'; import { rulesNotificationAlertType } from './rules_notification_alert_type'; @@ -21,7 +21,7 @@ describe('types', () => { it('isNotificationAlertExecutor should return true it passed object is NotificationAlertTypeDefinition type', () => { expect( isNotificationAlertExecutor( - rulesNotificationAlertType({ logger: loggingServiceMock.createLogger() }) + rulesNotificationAlertType({ logger: loggingSystemMock.createLogger() }) ) ).toEqual(true); }); diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/__mocks__/es_results.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/__mocks__/es_results.ts index 01ee41e3b877c9..101c998efa2429 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/__mocks__/es_results.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/__mocks__/es_results.ts @@ -10,7 +10,7 @@ import { SavedObject, SavedObjectsFindResponse, } from '../../../../../../../../src/core/server'; -import { loggingServiceMock } from '../../../../../../../../src/core/server/mocks'; +import { loggingSystemMock } from '../../../../../../../../src/core/server/mocks'; import { RuleTypeParams } from '../../types'; import { IRuleStatusAttributes } from '../../rules/types'; import { ruleStatusSavedObjectType } from '../../rules/saved_object_mappings'; @@ -394,7 +394,7 @@ export const exampleFindRuleStatusResponse: ( saved_objects: mockStatuses.map((obj) => ({ ...obj, score: 1 })), }); -export const mockLogger: Logger = loggingServiceMock.createLogger(); +export const mockLogger: Logger = loggingSystemMock.createLogger(); export const sampleBulkErrorItem = ( { diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/signal_rule_alert_type.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/signal_rule_alert_type.test.ts index a2dc33ba1c2bf3..23c2d6068c09c6 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/signal_rule_alert_type.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/signal_rule_alert_type.test.ts @@ -5,7 +5,7 @@ */ import moment from 'moment'; -import { loggingServiceMock } from 'src/core/server/mocks'; +import { loggingSystemMock } from 'src/core/server/mocks'; import { getResult, getMlResult } from '../routes/__mocks__/request_responses'; import { signalRulesAlertType } from './signal_rule_alert_type'; import { alertsMock, AlertServicesMock } from '../../../../../alerts/server/mocks'; @@ -69,13 +69,13 @@ describe('rules_notification_alert_type', () => { }; let payload: jest.Mocked; let alert: ReturnType; - let logger: ReturnType; + let logger: ReturnType; let alertServices: AlertServicesMock; let ruleStatusService: Record; beforeEach(() => { alertServices = alertsMock.createAlertServices(); - logger = loggingServiceMock.createLogger(); + logger = loggingSystemMock.createLogger(); ruleStatusService = { success: jest.fn(), find: jest.fn(), diff --git a/x-pack/plugins/security_solution/server/plugin.ts b/x-pack/plugins/security_solution/server/plugin.ts index 9fe7307e8cb6da..879c132ddec54d 100644 --- a/x-pack/plugins/security_solution/server/plugin.ts +++ b/x-pack/plugins/security_solution/server/plugin.ts @@ -219,7 +219,9 @@ export class Plugin implements IPlugin void; diff --git a/x-pack/plugins/spaces/public/copy_saved_objects_to_space/components/copy_to_space_flyout_footer.tsx b/x-pack/plugins/spaces/public/copy_saved_objects_to_space/components/copy_to_space_flyout_footer.tsx index 38bd3e04bc2404..d7ded819771fc4 100644 --- a/x-pack/plugins/spaces/public/copy_saved_objects_to_space/components/copy_to_space_flyout_footer.tsx +++ b/x-pack/plugins/spaces/public/copy_saved_objects_to_space/components/copy_to_space_flyout_footer.tsx @@ -8,8 +8,8 @@ import React, { Fragment } from 'react'; import { EuiButton, EuiFlexGroup, EuiFlexItem, EuiStat, EuiHorizontalRule } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; import { i18n } from '@kbn/i18n'; -import { ProcessedImportResponse } from '../../../../../../src/legacy/core_plugins/kibana/public'; import { ImportRetry } from '../types'; +import { ProcessedImportResponse } from '../../../../../../src/plugins/saved_objects_management/public'; interface Props { copyInProgress: boolean; diff --git a/x-pack/plugins/spaces/public/copy_saved_objects_to_space/components/processing_copy_to_space.tsx b/x-pack/plugins/spaces/public/copy_saved_objects_to_space/components/processing_copy_to_space.tsx index fc1810ba8f8193..255268d388eb8d 100644 --- a/x-pack/plugins/spaces/public/copy_saved_objects_to_space/components/processing_copy_to_space.tsx +++ b/x-pack/plugins/spaces/public/copy_saved_objects_to_space/components/processing_copy_to_space.tsx @@ -13,8 +13,10 @@ import { EuiHorizontalRule, } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; -import { ProcessedImportResponse } from '../../../../../../src/legacy/core_plugins/kibana/public'; -import { SavedObjectsManagementRecord } from '../../../../../../src/plugins/saved_objects_management/public'; +import { + ProcessedImportResponse, + SavedObjectsManagementRecord, +} from 'src/plugins/saved_objects_management/public'; import { Space } from '../../../common/model/space'; import { CopyOptions, ImportRetry } from '../types'; import { SpaceResult } from './space_result'; diff --git a/x-pack/plugins/spaces/public/copy_saved_objects_to_space/summarize_copy_result.test.ts b/x-pack/plugins/spaces/public/copy_saved_objects_to_space/summarize_copy_result.test.ts index 65a0cabfeb7166..a8ecd7c7b9d9f5 100644 --- a/x-pack/plugins/spaces/public/copy_saved_objects_to_space/summarize_copy_result.test.ts +++ b/x-pack/plugins/spaces/public/copy_saved_objects_to_space/summarize_copy_result.test.ts @@ -5,7 +5,7 @@ */ import { summarizeCopyResult } from './summarize_copy_result'; -import { ProcessedImportResponse } from 'src/legacy/core_plugins/kibana/public'; +import { ProcessedImportResponse } from 'src/plugins/saved_objects_management/public'; const createSavedObjectsManagementRecord = () => ({ type: 'dashboard', diff --git a/x-pack/plugins/spaces/public/copy_saved_objects_to_space/summarize_copy_result.ts b/x-pack/plugins/spaces/public/copy_saved_objects_to_space/summarize_copy_result.ts index 5c1fe6afcf0833..518e89df579a64 100644 --- a/x-pack/plugins/spaces/public/copy_saved_objects_to_space/summarize_copy_result.ts +++ b/x-pack/plugins/spaces/public/copy_saved_objects_to_space/summarize_copy_result.ts @@ -4,8 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ -import { ProcessedImportResponse } from 'src/legacy/core_plugins/kibana/public'; -import { SavedObjectsManagementRecord } from 'src/plugins/saved_objects_management/public'; +import { + SavedObjectsManagementRecord, + ProcessedImportResponse, +} from 'src/plugins/saved_objects_management/public'; export interface SummarizedSavedObjectResult { type: string; diff --git a/x-pack/plugins/spaces/server/capabilities/capabilities_switcher.test.ts b/x-pack/plugins/spaces/server/capabilities/capabilities_switcher.test.ts index b4489e57001591..1e01e04332f43d 100644 --- a/x-pack/plugins/spaces/server/capabilities/capabilities_switcher.test.ts +++ b/x-pack/plugins/spaces/server/capabilities/capabilities_switcher.test.ts @@ -8,7 +8,7 @@ import { Feature } from '../../../../plugins/features/server'; import { Space } from '../../common/model/space'; import { setupCapabilitiesSwitcher } from './capabilities_switcher'; import { Capabilities, CoreSetup } from 'src/core/server'; -import { coreMock, httpServerMock, loggingServiceMock } from 'src/core/server/mocks'; +import { coreMock, httpServerMock, loggingSystemMock } from 'src/core/server/mocks'; import { featuresPluginMock } from '../../../features/server/mocks'; import { spacesServiceMock } from '../spaces_service/spaces_service.mock'; import { PluginsStart } from '../plugin'; @@ -109,7 +109,7 @@ const setup = (space: Space) => { const spacesService = spacesServiceMock.createSetupContract(); spacesService.getActiveSpace.mockResolvedValue(space); - const logger = loggingServiceMock.createLogger(); + const logger = loggingSystemMock.createLogger(); const switcher = setupCapabilitiesSwitcher( (coreSetup as unknown) as CoreSetup, diff --git a/x-pack/plugins/spaces/server/default_space/create_default_space.test.ts b/x-pack/plugins/spaces/server/default_space/create_default_space.test.ts index 80cc7428e28e74..f281cd8efaa9f6 100644 --- a/x-pack/plugins/spaces/server/default_space/create_default_space.test.ts +++ b/x-pack/plugins/spaces/server/default_space/create_default_space.test.ts @@ -6,7 +6,7 @@ import { createDefaultSpace } from './create_default_space'; import { SavedObjectsErrorHelpers } from 'src/core/server'; -import { loggingServiceMock } from '../../../../../src/core/server/mocks'; +import { loggingSystemMock } from '../../../../../src/core/server/mocks'; interface MockServerSettings { defaultExists?: boolean; @@ -57,7 +57,7 @@ const createMockDeps = (settings: MockServerSettings = {}) => { }; }), }), - logger: loggingServiceMock.createLogger(), + logger: loggingSystemMock.createLogger(), }; }; diff --git a/x-pack/plugins/spaces/server/default_space/default_space_service.test.ts b/x-pack/plugins/spaces/server/default_space/default_space_service.test.ts index 2d677565164a20..311bedd0bf9e74 100644 --- a/x-pack/plugins/spaces/server/default_space/default_space_service.test.ts +++ b/x-pack/plugins/spaces/server/default_space/default_space_service.test.ts @@ -16,7 +16,7 @@ import { SavedObjectsRepository, SavedObjectsErrorHelpers, } from '../../../../../src/core/server'; -import { coreMock, loggingServiceMock } from 'src/core/server/mocks'; +import { coreMock, loggingSystemMock } from 'src/core/server/mocks'; import { licensingMock } from '../../../licensing/server/mocks'; import { SpacesLicenseService } from '../../common/licensing'; import { ILicense } from '../../../licensing/server'; @@ -59,7 +59,7 @@ const setup = ({ elasticsearchStatus, savedObjectsStatus, license }: SetupOpts) const license$ = new Rx.BehaviorSubject(license); - const logger = loggingServiceMock.createLogger(); + const logger = loggingSystemMock.createLogger(); const { license: spacesLicense } = new SpacesLicenseService().setup({ license$ }); diff --git a/x-pack/plugins/spaces/server/lib/request_interceptors/on_post_auth_interceptor.test.ts b/x-pack/plugins/spaces/server/lib/request_interceptors/on_post_auth_interceptor.test.ts index e596eade802fdc..17a1fbcca73bd2 100644 --- a/x-pack/plugins/spaces/server/lib/request_interceptors/on_post_auth_interceptor.test.ts +++ b/x-pack/plugins/spaces/server/lib/request_interceptors/on_post_auth_interceptor.test.ts @@ -17,7 +17,7 @@ import { } from '../../../../../../src/core/server'; import { elasticsearchServiceMock, - loggingServiceMock, + loggingSystemMock, coreMock, } from '../../../../../../src/core/server/mocks'; import * as kbnTestServer from '../../../../../../src/test_utils/kbn_server'; @@ -121,7 +121,7 @@ describe.skip('onPostAuthInterceptor', () => { // Mock esNodesCompatibility$ to prevent `root.start()` from blocking on ES version check elasticsearch.esNodesCompatibility$ = elasticsearchServiceMock.createInternalSetup().esNodesCompatibility$; - const loggingMock = loggingServiceMock.create().asLoggerFactory().get('xpack', 'spaces'); + const loggingMock = loggingSystemMock.create().asLoggerFactory().get('xpack', 'spaces'); const featuresPlugin = { getFeatures: () => diff --git a/x-pack/plugins/spaces/server/lib/spaces_tutorial_context_factory.test.ts b/x-pack/plugins/spaces/server/lib/spaces_tutorial_context_factory.test.ts index 0abf545fa7493c..8ec2e6f978d81e 100644 --- a/x-pack/plugins/spaces/server/lib/spaces_tutorial_context_factory.test.ts +++ b/x-pack/plugins/spaces/server/lib/spaces_tutorial_context_factory.test.ts @@ -9,12 +9,12 @@ import { DEFAULT_SPACE_ID } from '../../common/constants'; import { createSpacesTutorialContextFactory } from './spaces_tutorial_context_factory'; import { SpacesService } from '../spaces_service'; import { SpacesAuditLogger } from './audit_logger'; -import { coreMock, loggingServiceMock } from '../../../../../src/core/server/mocks'; +import { coreMock, loggingSystemMock } from '../../../../../src/core/server/mocks'; import { spacesServiceMock } from '../spaces_service/spaces_service.mock'; import { spacesConfig } from './__fixtures__'; import { securityMock } from '../../../security/server/mocks'; -const log = loggingServiceMock.createLogger(); +const log = loggingSystemMock.createLogger(); const service = new SpacesService(log); diff --git a/x-pack/plugins/spaces/server/routes/api/external/copy_to_space.test.ts b/x-pack/plugins/spaces/server/routes/api/external/copy_to_space.test.ts index 53f5a219dda5b9..b604554cbc59ac 100644 --- a/x-pack/plugins/spaces/server/routes/api/external/copy_to_space.test.ts +++ b/x-pack/plugins/spaces/server/routes/api/external/copy_to_space.test.ts @@ -16,7 +16,7 @@ import { } from '../__fixtures__'; import { CoreSetup, kibanaResponseFactory, RouteValidatorConfig } from 'src/core/server'; import { - loggingServiceMock, + loggingSystemMock, httpServiceMock, httpServerMock, coreMock, @@ -68,7 +68,7 @@ describe('copy to space', () => { createResolveSavedObjectsImportErrorsMock() ); - const log = loggingServiceMock.create().get('spaces'); + const log = loggingSystemMock.create().get('spaces'); const coreStart = coreMock.createStart(); coreStart.savedObjects = createMockSavedObjectsService(spaces); diff --git a/x-pack/plugins/spaces/server/routes/api/external/delete.test.ts b/x-pack/plugins/spaces/server/routes/api/external/delete.test.ts index f31ef657642e74..5461aaf1e36ea8 100644 --- a/x-pack/plugins/spaces/server/routes/api/external/delete.test.ts +++ b/x-pack/plugins/spaces/server/routes/api/external/delete.test.ts @@ -18,7 +18,7 @@ import { SavedObjectsErrorHelpers, } from 'src/core/server'; import { - loggingServiceMock, + loggingSystemMock, httpServiceMock, httpServerMock, coreMock, @@ -40,7 +40,7 @@ describe('Spaces Public API', () => { const savedObjectsRepositoryMock = createMockSavedObjectsRepository(spacesSavedObjects); - const log = loggingServiceMock.create().get('spaces'); + const log = loggingSystemMock.create().get('spaces'); const coreStart = coreMock.createStart(); diff --git a/x-pack/plugins/spaces/server/routes/api/external/get.test.ts b/x-pack/plugins/spaces/server/routes/api/external/get.test.ts index 55e153cf47f5bc..ac9a46ee9c3fac 100644 --- a/x-pack/plugins/spaces/server/routes/api/external/get.test.ts +++ b/x-pack/plugins/spaces/server/routes/api/external/get.test.ts @@ -13,7 +13,7 @@ import { import { initGetSpaceApi } from './get'; import { CoreSetup, kibanaResponseFactory } from 'src/core/server'; import { - loggingServiceMock, + loggingSystemMock, httpServiceMock, httpServerMock, coreMock, @@ -36,7 +36,7 @@ describe('GET space', () => { const savedObjectsRepositoryMock = createMockSavedObjectsRepository(spacesSavedObjects); - const log = loggingServiceMock.create().get('spaces'); + const log = loggingSystemMock.create().get('spaces'); const service = new SpacesService(log); const spacesService = await service.setup({ diff --git a/x-pack/plugins/spaces/server/routes/api/external/get_all.test.ts b/x-pack/plugins/spaces/server/routes/api/external/get_all.test.ts index aabd4900c5469b..ec841808f771d2 100644 --- a/x-pack/plugins/spaces/server/routes/api/external/get_all.test.ts +++ b/x-pack/plugins/spaces/server/routes/api/external/get_all.test.ts @@ -12,7 +12,7 @@ import { } from '../__fixtures__'; import { CoreSetup, kibanaResponseFactory } from 'src/core/server'; import { - loggingServiceMock, + loggingSystemMock, httpServiceMock, httpServerMock, coreMock, @@ -36,7 +36,7 @@ describe('GET /spaces/space', () => { const savedObjectsRepositoryMock = createMockSavedObjectsRepository(spacesSavedObjects); - const log = loggingServiceMock.create().get('spaces'); + const log = loggingSystemMock.create().get('spaces'); const service = new SpacesService(log); const spacesService = await service.setup({ diff --git a/x-pack/plugins/spaces/server/routes/api/external/post.test.ts b/x-pack/plugins/spaces/server/routes/api/external/post.test.ts index 5e09308f07d312..6aa89b36b020ab 100644 --- a/x-pack/plugins/spaces/server/routes/api/external/post.test.ts +++ b/x-pack/plugins/spaces/server/routes/api/external/post.test.ts @@ -12,7 +12,7 @@ import { } from '../__fixtures__'; import { CoreSetup, kibanaResponseFactory, RouteValidatorConfig } from 'src/core/server'; import { - loggingServiceMock, + loggingSystemMock, httpServerMock, httpServiceMock, coreMock, @@ -36,7 +36,7 @@ describe('Spaces Public API', () => { const savedObjectsRepositoryMock = createMockSavedObjectsRepository(spacesSavedObjects); - const log = loggingServiceMock.create().get('spaces'); + const log = loggingSystemMock.create().get('spaces'); const service = new SpacesService(log); const spacesService = await service.setup({ diff --git a/x-pack/plugins/spaces/server/routes/api/external/put.test.ts b/x-pack/plugins/spaces/server/routes/api/external/put.test.ts index 7b068d37840438..ebdffa20a6c8e9 100644 --- a/x-pack/plugins/spaces/server/routes/api/external/put.test.ts +++ b/x-pack/plugins/spaces/server/routes/api/external/put.test.ts @@ -13,7 +13,7 @@ import { } from '../__fixtures__'; import { CoreSetup, kibanaResponseFactory, RouteValidatorConfig } from 'src/core/server'; import { - loggingServiceMock, + loggingSystemMock, httpServiceMock, httpServerMock, coreMock, @@ -37,7 +37,7 @@ describe('PUT /api/spaces/space', () => { const savedObjectsRepositoryMock = createMockSavedObjectsRepository(spacesSavedObjects); - const log = loggingServiceMock.create().get('spaces'); + const log = loggingSystemMock.create().get('spaces'); const service = new SpacesService(log); const spacesService = await service.setup({ diff --git a/x-pack/plugins/spaces/server/spaces_service/spaces_service.test.ts b/x-pack/plugins/spaces/server/spaces_service/spaces_service.test.ts index 3e1a849a9bdfab..b341d76c86649a 100644 --- a/x-pack/plugins/spaces/server/spaces_service/spaces_service.test.ts +++ b/x-pack/plugins/spaces/server/spaces_service/spaces_service.test.ts @@ -5,7 +5,7 @@ */ import * as Rx from 'rxjs'; import { SpacesService } from './spaces_service'; -import { coreMock, httpServerMock, loggingServiceMock } from 'src/core/server/mocks'; +import { coreMock, httpServerMock, loggingSystemMock } from 'src/core/server/mocks'; import { SpacesAuditLogger } from '../lib/audit_logger'; import { KibanaRequest, @@ -18,7 +18,7 @@ import { getSpaceIdFromPath } from '../../common/lib/spaces_url_parser'; import { spacesConfig } from '../lib/__fixtures__'; import { securityMock } from '../../../security/server/mocks'; -const mockLogger = loggingServiceMock.createLogger(); +const mockLogger = loggingSystemMock.createLogger(); const createService = async (serverBasePath: string = '') => { const spacesService = new SpacesService(mockLogger); diff --git a/x-pack/plugins/translations/translations/ja-JP.json b/x-pack/plugins/translations/translations/ja-JP.json index 76650bf421f225..0d85960807f93b 100644 --- a/x-pack/plugins/translations/translations/ja-JP.json +++ b/x-pack/plugins/translations/translations/ja-JP.json @@ -4296,21 +4296,6 @@ "xpack.apm.serviceDetails.alertsMenu.errorRate": "エラー率", "xpack.apm.serviceDetails.alertsMenu.transactionDuration": "トランザクション期間", "xpack.apm.serviceDetails.alertsMenu.viewActiveAlerts": "アクティブアラートを表示", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.callout.jobExistsDescription": "現在 {serviceName} ({transactionType}) の実行中のジョブがあります。", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.callout.jobExistsDescription.viewJobLinkText": "既存のジョブを表示", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.callout.jobExistsTitle": "ジョブが既に存在します", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.createMLJobDescription.transactionDurationGraphText": "トランザクション時間のグラフ", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.createNewJobButtonLabel": "ジョブを作成", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.enableAnomalyDetectionTitle": "異常検知を有効にする", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.jobCreatedNotificationText": "現在 {serviceName} ({transactionType}) の分析を実行中です。応答時間グラフに結果が追加されるまで少し時間がかかる場合があります。", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.jobCreatedNotificationText.viewJobLinkText": "ジョブを表示", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.jobCreatedNotificationTitle": "ジョブが作成されました", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.jobCreationFailedNotificationText": "現在のライセンスでは機械学習ジョブの作成が許可されていないか、ジョブが既に存在する可能性があります。", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.jobCreationFailedNotificationTitle": "ジョブの作成に失敗", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.manageMLJobDescription": "ジョブはそれぞれのサービス + トランザクションタイプの組み合わせに対して作成できます。ジョブの作成後、{mlJobsPageLink} で管理と詳細の確認ができます。", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.manageMLJobDescription.mlJobsPageLinkText": "機械学習ジョブの管理ページ", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.manageMLJobDescription.noteText": "注:ジョブが結果の計算を開始するまでに少し時間がかかる場合があります。", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.selectTransactionTypeLabel": "このジョブのトランザクションタイプを選択してください", "xpack.apm.serviceDetails.enableErrorReportsPanel.actionsDescription": "レポートはメールで送信するか Slack チャンネルに投稿できます。各レポートにはオカランス別のトップ 10 のエラーが含まれます。", "xpack.apm.serviceDetails.enableErrorReportsPanel.actionsTitle": "アクション", "xpack.apm.serviceDetails.enableErrorReportsPanel.conditionTitle": "コンディション", @@ -4346,8 +4331,6 @@ "xpack.apm.serviceDetails.enableErrorReportsPanel.watchCreationFailedNotificationText": "ユーザーにウォッチ作成のパーミッションがあることを確認してください。", "xpack.apm.serviceDetails.enableErrorReportsPanel.watchCreationFailedNotificationTitle": "ウォッチの作成に失敗", "xpack.apm.serviceDetails.errorsTabLabel": "エラー", - "xpack.apm.serviceDetails.integrationsMenu.enableMLAnomalyDetectionButtonLabel": "ML 異常検知を有効にする", - "xpack.apm.serviceDetails.integrationsMenu.enableMLAnomalyDetectionButtonTooltip": "このサービスの機械学習ジョブをセットアップします", "xpack.apm.serviceDetails.integrationsMenu.enableWatcherErrorReportsButtonLabel": "ウォッチエラーレポートを有効にする", "xpack.apm.serviceDetails.integrationsMenu.integrationsButtonLabel": "統合", "xpack.apm.serviceDetails.integrationsMenu.viewWatchesButtonLabel": "既存のウォッチを表示", @@ -4357,9 +4340,6 @@ "xpack.apm.serviceDetails.metricsTabLabel": "メトリック", "xpack.apm.serviceDetails.nodesTabLabel": "JVM", "xpack.apm.serviceDetails.transactionsTabLabel": "トランザクション", - "xpack.apm.serviceMap.anomalyDetectionPopoverLink": "異常を表示", - "xpack.apm.serviceMap.anomalyDetectionPopoverScoreMetric": "スコア(最大)", - "xpack.apm.serviceMap.anomalyDetectionPopoverTitle": "異常検知", "xpack.apm.serviceMap.avgCpuUsagePopoverMetric": "CPU使用状況 (平均)", "xpack.apm.serviceMap.avgErrorsPerMinutePopoverMetric": "1分あたりのエラー(平均)", "xpack.apm.serviceMap.avgMemoryUsagePopoverMetric": "メモリー使用状況(平均)", @@ -10691,7 +10671,6 @@ "xpack.ml.newJob.wizard.timeRangeStep.timeRangePicker.startDateLabel": "開始日", "xpack.ml.newJob.wizard.validateJob.bucketSpanMustBeSetErrorMessage": "バケットスパンを設定する必要があります", "xpack.ml.newJob.wizard.validateJob.duplicatedDetectorsErrorMessage": "重複する検知器が検出されました。", - "xpack.ml.newJob.wizard.validateJob.frequencyInvalidTimeIntervalFormatErrorMessage": "{value} は有効な時間間隔のフォーマット (例: {tenMinutes}、{oneHour}) ではありません。また、0 よりも大きい数字である必要があります。", "xpack.ml.newJob.wizard.validateJob.groupNameAlreadyExists": "グループ ID が既に存在します。グループ ID は既存のジョブやグループと同じにできません。", "xpack.ml.newJob.wizard.validateJob.jobGroupAllowedCharactersDescription": "ジョブグループ名にはアルファベットの小文字 (a-z と 0-9)、ハイフンまたはアンダーラインが使用でき、最初と最後を英数字にする必要があります", "xpack.ml.newJob.wizard.validateJob.jobGroupMaxLengthDescription": "ジョブグループ名は {maxLength, plural, one {# 文字} other {# 文字}} 以内でなければなりません。", diff --git a/x-pack/plugins/translations/translations/zh-CN.json b/x-pack/plugins/translations/translations/zh-CN.json index dc20275561cb01..85167e11b28baf 100644 --- a/x-pack/plugins/translations/translations/zh-CN.json +++ b/x-pack/plugins/translations/translations/zh-CN.json @@ -4299,21 +4299,6 @@ "xpack.apm.serviceDetails.alertsMenu.errorRate": "错误率", "xpack.apm.serviceDetails.alertsMenu.transactionDuration": "事务持续时间", "xpack.apm.serviceDetails.alertsMenu.viewActiveAlerts": "查看活动的告警", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.callout.jobExistsDescription": "当前有 {serviceName}({transactionType})的作业正在运行。", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.callout.jobExistsDescription.viewJobLinkText": "查看现有作业", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.callout.jobExistsTitle": "作业已存在", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.createMLJobDescription.transactionDurationGraphText": "事务持续时间图表", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.createNewJobButtonLabel": "创建作业", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.enableAnomalyDetectionTitle": "启用异常检测", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.jobCreatedNotificationText": "现在正在运行对 {serviceName}({transactionType})的分析。可能要花费点时间,才会将结果添加响应时间图表。", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.jobCreatedNotificationText.viewJobLinkText": "查看作业", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.jobCreatedNotificationTitle": "作业已成功创建", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.jobCreationFailedNotificationText": "您当前的许可可能不允许创建 Machine Learning 作业,或者此作业可能已存在。", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.jobCreationFailedNotificationTitle": "作业创建失败", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.manageMLJobDescription": "可以创建每个服务 + 事务类型组合的作业。创建作业后,可以在 {mlJobsPageLink}中管理作业以及查看更多详细信息。", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.manageMLJobDescription.mlJobsPageLinkText": "Machine Learning 作业管理页面", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.manageMLJobDescription.noteText": "注意:可能要过几分钟后,作业才会开始计算结果。", - "xpack.apm.serviceDetails.enableAnomalyDetectionPanel.selectTransactionTypeLabel": "为此作业选择事务类型", "xpack.apm.serviceDetails.enableErrorReportsPanel.actionsDescription": "可以通过电子邮件发送报告或将报告发布到 Slack 频道。每个报告将包括按发生次数排序的前 10 个错误。", "xpack.apm.serviceDetails.enableErrorReportsPanel.actionsTitle": "操作", "xpack.apm.serviceDetails.enableErrorReportsPanel.conditionTitle": "条件", @@ -4349,8 +4334,6 @@ "xpack.apm.serviceDetails.enableErrorReportsPanel.watchCreationFailedNotificationText": "确保您的用户有权创建监视。", "xpack.apm.serviceDetails.enableErrorReportsPanel.watchCreationFailedNotificationTitle": "监视创建失败", "xpack.apm.serviceDetails.errorsTabLabel": "错误", - "xpack.apm.serviceDetails.integrationsMenu.enableMLAnomalyDetectionButtonLabel": "启用 ML 异常检测", - "xpack.apm.serviceDetails.integrationsMenu.enableMLAnomalyDetectionButtonTooltip": "为此服务设置 Machine Learning 作业", "xpack.apm.serviceDetails.integrationsMenu.enableWatcherErrorReportsButtonLabel": "启用 Watcher 错误报告", "xpack.apm.serviceDetails.integrationsMenu.integrationsButtonLabel": "集成", "xpack.apm.serviceDetails.integrationsMenu.viewWatchesButtonLabel": "查看现有监视", @@ -4360,9 +4343,6 @@ "xpack.apm.serviceDetails.metricsTabLabel": "指标", "xpack.apm.serviceDetails.nodesTabLabel": "JVM", "xpack.apm.serviceDetails.transactionsTabLabel": "事务", - "xpack.apm.serviceMap.anomalyDetectionPopoverLink": "查看异常", - "xpack.apm.serviceMap.anomalyDetectionPopoverScoreMetric": "分数(最大)", - "xpack.apm.serviceMap.anomalyDetectionPopoverTitle": "异常检测", "xpack.apm.serviceMap.avgCpuUsagePopoverMetric": "CPU 使用(平均)", "xpack.apm.serviceMap.avgErrorsPerMinutePopoverMetric": "每分钟错误数(平均)", "xpack.apm.serviceMap.avgMemoryUsagePopoverMetric": "内存使用(平均)", @@ -10695,7 +10675,6 @@ "xpack.ml.newJob.wizard.timeRangeStep.timeRangePicker.startDateLabel": "开始日期", "xpack.ml.newJob.wizard.validateJob.bucketSpanMustBeSetErrorMessage": "必须设置存储桶跨度", "xpack.ml.newJob.wizard.validateJob.duplicatedDetectorsErrorMessage": "找到重复的检测工具。", - "xpack.ml.newJob.wizard.validateJob.frequencyInvalidTimeIntervalFormatErrorMessage": "{value} 不是有效的时间间隔格式,例如,{tenMinutes}、{oneHour}。还需要大于零。", "xpack.ml.newJob.wizard.validateJob.groupNameAlreadyExists": "组 ID 已存在。组 ID 不能与现有作业或组相同。", "xpack.ml.newJob.wizard.validateJob.jobGroupAllowedCharactersDescription": "作业组名称可以包含小写字母数字(a-z 和 0-9)、连字符或下划线;必须以字母数字字符开头和结尾", "xpack.ml.newJob.wizard.validateJob.jobGroupMaxLengthDescription": "作业组名称的长度不得超过 {maxLength, plural, one {# 个字符} other {# 个字符}}。", diff --git a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.test.ts b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.test.ts index 9c2593ee79f937..dea9974791a881 100644 --- a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.test.ts +++ b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.test.ts @@ -6,7 +6,7 @@ jest.mock('../es_indices_state_check', () => ({ esIndicesStateCheck: jest.fn() })); import { BehaviorSubject } from 'rxjs'; import { Logger } from 'src/core/server'; -import { loggingServiceMock } from 'src/core/server/mocks'; +import { loggingSystemMock } from 'src/core/server/mocks'; import { IndexGroup, @@ -60,7 +60,7 @@ describe('reindexService', () => { runWhileIndexGroupLocked: jest.fn(async (group: string, f: any) => f({ attributes: {} })), }; callCluster = jest.fn(); - log = loggingServiceMock.create().get(); + log = loggingSystemMock.create().get(); licensingPluginSetup = licensingMock.createSetup(); licensingPluginSetup.license$ = new BehaviorSubject( licensingMock.createLicense({ diff --git a/x-pack/plugins/uptime/public/components/common/charts/duration_chart.tsx b/x-pack/plugins/uptime/public/components/common/charts/duration_chart.tsx index a6bb097de45ad7..a1e23ab8b38a72 100644 --- a/x-pack/plugins/uptime/public/components/common/charts/duration_chart.tsx +++ b/x-pack/plugins/uptime/public/components/common/charts/duration_chart.tsx @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import React, { useState } from 'react'; +import React, { useContext, useState } from 'react'; import { i18n } from '@kbn/i18n'; import moment from 'moment'; import { FormattedMessage } from '@kbn/i18n/react'; @@ -26,6 +26,7 @@ import { getTickFormat } from './get_tick_format'; import { ChartEmptyState } from './chart_empty_state'; import { DurationAnomaliesBar } from './duration_line_bar_list'; import { AnomalyRecords } from '../../../state/actions'; +import { UptimeThemeContext } from '../../../contexts'; interface DurationChartProps { /** @@ -59,6 +60,8 @@ export const DurationChartComponent = ({ const [hiddenLegends, setHiddenLegends] = useState([]); + const { chartTheme } = useContext(UptimeThemeContext); + const onBrushEnd: BrushEndListener = ({ x }) => { if (!x) { return; @@ -93,6 +96,7 @@ export const DurationChartComponent = ({ legendPosition={Position.Bottom} onBrushEnd={onBrushEnd} onLegendItemClick={legendToggleVisibility} + {...chartTheme} /> { const { colors: { danger }, + chartTheme, } = useContext(UptimeThemeContext); const [getUrlParams, updateUrlParams] = useUrlParams(); const { absoluteDateRangeStart, absoluteDateRangeEnd } = getUrlParams(); @@ -62,6 +63,7 @@ export const MonitorBarSeries = ({ histogramSeries }: MonitorBarSeriesProps) => = ({ }) => { const { colors: { danger, gray }, + chartTheme, } = useContext(UptimeThemeContext); const [, updateUrlParams] = useUrlParams(); @@ -128,6 +129,7 @@ export const PingHistogramComponent: React.FC = ({ }} showLegend={false} onBrushEnd={onBrushEnd} + {...chartTheme} /> = ({ darkMo const value = useMemo(() => { return { colors, + chartTheme: { + baseTheme: darkMode ? DARK_THEME : LIGHT_THEME, + theme: darkMode ? EUI_CHARTS_THEME_DARK.theme : EUI_CHARTS_THEME_LIGHT.theme, + }, }; - }, [colors]); + }, [colors, darkMode]); return ; }; diff --git a/x-pack/test/api_integration/apis/management/index_management/data_streams.ts b/x-pack/test/api_integration/apis/management/index_management/data_streams.ts index 219f2471f8e68b..e1756df42ca25b 100644 --- a/x-pack/test/api_integration/apis/management/index_management/data_streams.ts +++ b/x-pack/test/api_integration/apis/management/index_management/data_streams.ts @@ -50,17 +50,18 @@ export default function ({ getService }: FtrProviderContext) { const deleteDataStream = (name: string) => { return es.dataManagement - .deleteComposableIndexTemplate({ + .deleteDataStream({ name, }) .then(() => - es.dataManagement.deleteDataStream({ + es.dataManagement.deleteComposableIndexTemplate({ name, }) ); }; - describe('Data streams', function () { + // Unskip once ES snapshot has been promoted that updates the data stream response + describe.skip('Data streams', function () { const testDataStreamName = 'test-data-stream'; describe('Get', () => { @@ -79,7 +80,7 @@ export default function ({ getService }: FtrProviderContext) { expect(dataStreams).to.eql([ { name: testDataStreamName, - timeStampField: '@timestamp', + timeStampField: { name: '@timestamp', mapping: { type: 'date' } }, indices: [ { name: indexName, diff --git a/x-pack/test/api_integration/apis/telemetry/telemetry_local.js b/x-pack/test/api_integration/apis/telemetry/telemetry_local.js index f06af3baa2301c..9dbc3e1c8a5bb9 100644 --- a/x-pack/test/api_integration/apis/telemetry/telemetry_local.js +++ b/x-pack/test/api_integration/apis/telemetry/telemetry_local.js @@ -76,9 +76,10 @@ export default function ({ getService }) { expect(stats.stack_stats.kibana.plugins.apm.services_per_agent).to.be.an('object'); expect(stats.stack_stats.kibana.plugins.infraops.last_24_hours).to.be.an('object'); expect(stats.stack_stats.kibana.plugins.kql.defaultQueryLanguage).to.be.a('string'); - expect(stats.stack_stats.kibana.plugins['maps-telemetry'].attributes.timeCaptured).to.be.a( - 'string' - ); + expect(stats.stack_stats.kibana.plugins.maps.timeCaptured).to.be.a('string'); + expect(stats.stack_stats.kibana.plugins.maps.attributes).to.be(undefined); + expect(stats.stack_stats.kibana.plugins.maps.id).to.be(undefined); + expect(stats.stack_stats.kibana.plugins.maps.type).to.be(undefined); expect(stats.stack_stats.kibana.plugins.reporting.enabled).to.be(true); expect(stats.stack_stats.kibana.plugins.rollups.index_patterns).to.be.an('object'); diff --git a/x-pack/test/functional/apps/lens/smokescreen.ts b/x-pack/test/functional/apps/lens/smokescreen.ts index 181d41d77b4cb7..b399c9e915e27a 100644 --- a/x-pack/test/functional/apps/lens/smokescreen.ts +++ b/x-pack/test/functional/apps/lens/smokescreen.ts @@ -23,6 +23,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { const dashboardAddPanel = getService('dashboardAddPanel'); const elasticChart = getService('elasticChart'); const browser = getService('browser'); + const retry = getService('retry'); const testSubjects = getService('testSubjects'); const filterBar = getService('filterBar'); const listingTable = getService('listingTable'); @@ -93,7 +94,11 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { await dashboardAddPanel.closeAddPanel(); await PageObjects.lens.goToTimeRange(); await clickOnBarHistogram(); - await testSubjects.click('applyFiltersPopoverButton'); + + await retry.try(async () => { + await testSubjects.click('applyFiltersPopoverButton'); + await testSubjects.missingOrFail('applyFiltersPopoverButton'); + }); await assertExpectedChart(); await assertExpectedTimerange(); diff --git a/x-pack/test/login_selector_api_integration/apis/login_selector.ts b/x-pack/test/login_selector_api_integration/apis/login_selector.ts index 57c91bd49808e8..54b37fe52cc56c 100644 --- a/x-pack/test/login_selector_api_integration/apis/login_selector.ts +++ b/x-pack/test/login_selector_api_integration/apis/login_selector.ts @@ -102,7 +102,7 @@ export default function ({ getService }: FtrProviderContext) { }); } - it('should be able to log in via IdP initiated login for any configured realm', async () => { + it('should be able to log in via IdP initiated login for any configured provider', async () => { for (const providerName of ['saml1', 'saml2']) { const authenticationResponse = await supertest .post('/api/security/saml/callback') @@ -124,6 +124,57 @@ export default function ({ getService }: FtrProviderContext) { } }); + it('should redirect to URL from relay state in case of IdP initiated login only for providers that explicitly enabled that behaviour', async () => { + for (const { providerName, redirectURL } of [ + { providerName: 'saml1', redirectURL: '/' }, + { providerName: 'saml2', redirectURL: '/app/kibana#/dashboards' }, + ]) { + const authenticationResponse = await supertest + .post('/api/security/saml/callback') + .ca(CA_CERT) + .type('form') + .send({ + SAMLResponse: await createSAMLResponse({ + issuer: `http://www.elastic.co/${providerName}`, + }), + }) + .send({ RelayState: '/app/kibana#/dashboards' }) + .expect(302); + + // User should be redirected to the base URL. + expect(authenticationResponse.headers.location).to.be(redirectURL); + + const cookies = authenticationResponse.headers['set-cookie']; + expect(cookies).to.have.length(1); + + await checkSessionCookie(request.cookie(cookies[0])!, 'a@b.c', providerName); + } + }); + + it('should not redirect to URL from relay state in case of IdP initiated login if URL is not internal', async () => { + for (const providerName of ['saml1', 'saml2']) { + const authenticationResponse = await supertest + .post('/api/security/saml/callback') + .ca(CA_CERT) + .type('form') + .send({ + SAMLResponse: await createSAMLResponse({ + issuer: `http://www.elastic.co/${providerName}`, + }), + }) + .send({ RelayState: 'http://www.elastic.co/app/kibana#/dashboards' }) + .expect(302); + + // User should be redirected to the base URL. + expect(authenticationResponse.headers.location).to.be('/'); + + const cookies = authenticationResponse.headers['set-cookie']; + expect(cookies).to.have.length(1); + + await checkSessionCookie(request.cookie(cookies[0])!, 'a@b.c', providerName); + } + }); + it('should be able to log in via IdP initiated login even if session with other provider type exists', async () => { const basicAuthenticationResponse = await supertest .post('/internal/security/login') @@ -193,6 +244,43 @@ export default function ({ getService }: FtrProviderContext) { await checkSessionCookie(saml2SessionCookie, 'a@b.c', 'saml2'); }); + it('should redirect to URL from relay state in case of IdP initiated login even if session with other SAML provider exists', async () => { + // First login with `saml1`. + const saml1AuthenticationResponse = await supertest + .post('/api/security/saml/callback') + .ca(CA_CERT) + .send({ + SAMLResponse: await createSAMLResponse({ issuer: `http://www.elastic.co/saml1` }), + }) + .expect(302); + + const saml1SessionCookie = request.cookie( + saml1AuthenticationResponse.headers['set-cookie'][0] + )!; + await checkSessionCookie(saml1SessionCookie, 'a@b.c', 'saml1'); + + // And now try to login with `saml2`. + const saml2AuthenticationResponse = await supertest + .post('/api/security/saml/callback') + .ca(CA_CERT) + .set('Cookie', saml1SessionCookie.cookieString()) + .type('form') + .send({ + SAMLResponse: await createSAMLResponse({ issuer: `http://www.elastic.co/saml2` }), + }) + .send({ RelayState: '/app/kibana#/dashboards' }) + .expect(302); + + // It should be `/overwritten_session` with `?next='/app/kibana#/dashboards'` instead of just + // `'/app/kibana#/dashboards'` once it's generalized. + expect(saml2AuthenticationResponse.headers.location).to.be('/app/kibana#/dashboards'); + + const saml2SessionCookie = request.cookie( + saml2AuthenticationResponse.headers['set-cookie'][0] + )!; + await checkSessionCookie(saml2SessionCookie, 'a@b.c', 'saml2'); + }); + // Ideally we should be able to abandon intermediate session and let user log in, but for the // time being we cannot distinguish errors coming from Elasticsearch for the case when SAML // response just doesn't correspond to request ID we have in intermediate cookie and the case diff --git a/x-pack/test/login_selector_api_integration/config.ts b/x-pack/test/login_selector_api_integration/config.ts index ba7aadb121e825..67bc2e6f17b565 100644 --- a/x-pack/test/login_selector_api_integration/config.ts +++ b/x-pack/test/login_selector_api_integration/config.ts @@ -127,7 +127,12 @@ export default async function ({ readConfigFile }: FtrConfigProviderContext) { oidc: { oidc1: { order: 3, realm: 'oidc1' } }, saml: { saml1: { order: 1, realm: 'saml1' }, - saml2: { order: 5, realm: 'saml2', maxRedirectURLSize: '100b' }, + saml2: { + order: 5, + realm: 'saml2', + maxRedirectURLSize: '100b', + useRelayStateDeepLink: true, + }, }, })}`, ], diff --git a/x-pack/test/security_solution_endpoint/apps/endpoint/index.ts b/x-pack/test/security_solution_endpoint/apps/endpoint/index.ts index 199d138d1c450a..d94ee260b27820 100644 --- a/x-pack/test/security_solution_endpoint/apps/endpoint/index.ts +++ b/x-pack/test/security_solution_endpoint/apps/endpoint/index.ts @@ -5,10 +5,13 @@ */ import { FtrProviderContext } from '../../ftr_provider_context'; -export default function ({ loadTestFile }: FtrProviderContext) { +export default function ({ loadTestFile, getService }: FtrProviderContext) { describe('endpoint', function () { this.tags('ciGroup7'); - + const ingestManager = getService('ingestManager'); + before(async () => { + await ingestManager.setup(); + }); loadTestFile(require.resolve('./endpoint_list')); loadTestFile(require.resolve('./policy_list')); loadTestFile(require.resolve('./policy_details')); diff --git a/x-pack/test/security_solution_endpoint/services/index.ts b/x-pack/test/security_solution_endpoint/services/index.ts index 0247d9b00968a1..90b4bc0b4d0457 100644 --- a/x-pack/test/security_solution_endpoint/services/index.ts +++ b/x-pack/test/security_solution_endpoint/services/index.ts @@ -4,10 +4,12 @@ * you may not use this file except in compliance with the Elastic License. */ +import { services as apiIntegrationServices } from '../../api_integration/services'; import { services as xPackFunctionalServices } from '../../functional/services'; import { EndpointPolicyTestResourcesProvider } from './endpoint_policy'; export const services = { ...xPackFunctionalServices, + ingestManager: apiIntegrationServices.ingestManager, policyTestResources: EndpointPolicyTestResourcesProvider, };