diff --git a/.ci/Jenkinsfile_visual_baseline b/.ci/Jenkinsfile_baseline_capture similarity index 100% rename from .ci/Jenkinsfile_visual_baseline rename to .ci/Jenkinsfile_baseline_capture diff --git a/.ci/Jenkinsfile_baseline_trigger b/.ci/Jenkinsfile_baseline_trigger new file mode 100644 index 0000000000000..cc9fb47ca4993 --- /dev/null +++ b/.ci/Jenkinsfile_baseline_trigger @@ -0,0 +1,64 @@ +#!/bin/groovy + +def MAXIMUM_COMMITS_TO_CHECK = 10 +def MAXIMUM_COMMITS_TO_BUILD = 5 + +if (!params.branches_yaml) { + error "'branches_yaml' parameter must be specified" +} + +def additionalBranches = [] + +def branches = readYaml(text: params.branches_yaml) + additionalBranches + +library 'kibana-pipeline-library' +kibanaLibrary.load() + +withGithubCredentials { + branches.each { branch -> + stage(branch) { + def commits = getCommits(branch, MAXIMUM_COMMITS_TO_CHECK, MAXIMUM_COMMITS_TO_BUILD) + + commits.take(MAXIMUM_COMMITS_TO_BUILD).each { commit -> + catchErrors { + githubCommitStatus.create(commit, 'pending', 'Baseline started.', 'kibana-ci-baseline') + + build( + propagate: false, + wait: false, + job: 'elastic+kibana+baseline-capture', + parameters: [ + string(name: 'branch_specifier', value: branch), + string(name: 'commit', value: commit), + ] + ) + } + } + } + } +} + +def getCommits(String branch, maximumCommitsToCheck, maximumCommitsToBuild) { + print "Getting latest commits for ${branch}..." + def commits = githubApi.get("repos/elastic/kibana/commits?sha=${branch}").take(maximumCommitsToCheck).collect { it.sha } + def commitsToBuild = [] + + for (commit in commits) { + print "Getting statuses for ${commit}" + def status = githubApi.get("repos/elastic/kibana/statuses/${commit}").find { it.context == 'kibana-ci-baseline' } + print "Commit '${commit}' already built? ${status ? 'Yes' : 'No'}" + + if (!status) { + commitsToBuild << commit + } else { + // Stop at the first commit we find that's already been triggered + break + } + + if (commitsToBuild.size() >= maximumCommitsToBuild) { + break + } + } + + return commitsToBuild.reverse() // We want the builds to trigger oldest-to-newest +} diff --git a/.eslintignore b/.eslintignore index 4b5e781c26971..d983c4bedfaab 100644 --- a/.eslintignore +++ b/.eslintignore @@ -26,6 +26,7 @@ target /src/plugins/data/common/es_query/kuery/ast/_generated_/** /src/plugins/vis_type_timelion/public/_generated_/** /src/plugins/vis_type_timelion/public/webpackShims/jquery.flot.* +/src/plugins/timelion/public/webpackShims/jquery.flot.* /x-pack/legacy/plugins/**/__tests__/fixtures/** /x-pack/plugins/apm/e2e/**/snapshots.js /x-pack/plugins/apm/e2e/tmp/* diff --git a/.i18nrc.json b/.i18nrc.json index 9af7f17067b8e..e8431fdb3f0e1 100644 --- a/.i18nrc.json +++ b/.i18nrc.json @@ -44,7 +44,7 @@ "src/plugins/telemetry_management_section" ], "tileMap": "src/plugins/tile_map", - "timelion": ["src/legacy/core_plugins/timelion", "src/plugins/vis_type_timelion"], + "timelion": ["src/plugins/timelion", "src/plugins/vis_type_timelion"], "uiActions": "src/plugins/ui_actions", "visDefaultEditor": "src/plugins/vis_default_editor", "visTypeMarkdown": "src/plugins/vis_type_markdown", diff --git a/.sass-lint.yml b/.sass-lint.yml index 56b85adca8a71..50cbe81cc7da2 100644 --- a/.sass-lint.yml +++ b/.sass-lint.yml @@ -1,7 +1,7 @@ files: include: - 'src/legacy/core_plugins/metrics/**/*.s+(a|c)ss' - - 'src/legacy/core_plugins/timelion/**/*.s+(a|c)ss' + - 'src/plugins/timelion/**/*.s+(a|c)ss' - 'src/plugins/vis_type_vislib/**/*.s+(a|c)ss' - 'src/plugins/vis_type_xy/**/*.s+(a|c)ss' - 'x-pack/plugins/canvas/**/*.s+(a|c)ss' diff --git a/Jenkinsfile b/Jenkinsfile index f6f77ccae8427..69c61b5bfa988 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -42,7 +42,6 @@ kibanaPipeline(timeoutMinutes: 155, checkPrChanges: true, setCommitStatus: true) 'xpack-ciGroup10': kibanaPipeline.xpackCiGroupProcess(10), 'xpack-accessibility': kibanaPipeline.functionalTestProcess('xpack-accessibility', './test/scripts/jenkins_xpack_accessibility.sh'), 'xpack-savedObjectsFieldMetrics': kibanaPipeline.functionalTestProcess('xpack-savedObjectsFieldMetrics', './test/scripts/jenkins_xpack_saved_objects_field_metrics.sh'), - // 'xpack-pageLoadMetrics': kibanaPipeline.functionalTestProcess('xpack-pageLoadMetrics', './test/scripts/jenkins_xpack_page_load_metrics.sh'), 'xpack-securitySolutionCypress': { processNumber -> whenChanged(['x-pack/plugins/security_solution/', 'x-pack/test/security_solution_cypress/']) { kibanaPipeline.functionalTestProcess('xpack-securitySolutionCypress', './test/scripts/jenkins_security_solution_cypress.sh')(processNumber) diff --git a/docs/api/dashboard/export-dashboard.asciidoc b/docs/api/dashboard/export-dashboard.asciidoc index 36c551dee84fc..2099fb599ba67 100644 --- a/docs/api/dashboard/export-dashboard.asciidoc +++ b/docs/api/dashboard/export-dashboard.asciidoc @@ -35,7 +35,7 @@ experimental[] Export dashboards and corresponding saved objects. [source,sh] -------------------------------------------------- -$ curl -X GET "localhost:5601/api/kibana/dashboards/export?dashboard=942dcef0-b2cd-11e8-ad8e-85441f0c2e5c" <1> +$ curl -X GET api/kibana/dashboards/export?dashboard=942dcef0-b2cd-11e8-ad8e-85441f0c2e5c <1> -------------------------------------------------- // KIBANA diff --git a/docs/api/dashboard/import-dashboard.asciidoc b/docs/api/dashboard/import-dashboard.asciidoc index 320859f78c617..020ec8018b85b 100644 --- a/docs/api/dashboard/import-dashboard.asciidoc +++ b/docs/api/dashboard/import-dashboard.asciidoc @@ -42,7 +42,7 @@ Use the complete response body from the < "index1", @@ -40,7 +40,9 @@ POST /api/upgrade_assistant/reindex/batch ] } -------------------------------------------------- -<1> The order in which the indices are provided here determines the order in which the reindex tasks will be executed. +// KIBANA + +<1> The order of the indices determines the order that the reindex tasks are executed. Similar to the <>, the API returns the following: diff --git a/docs/api/upgrade-assistant/check_reindex_status.asciidoc b/docs/api/upgrade-assistant/check_reindex_status.asciidoc index 00801f201d1e1..98cf263673f73 100644 --- a/docs/api/upgrade-assistant/check_reindex_status.asciidoc +++ b/docs/api/upgrade-assistant/check_reindex_status.asciidoc @@ -64,6 +64,7 @@ The API returns the following: `3`:: Paused ++ NOTE: If the {kib} node that started the reindex is shutdown or restarted, the reindex goes into a paused state after some time. To resume the reindex, you must submit a new POST request to the `/api/upgrade_assistant/reindex/` endpoint. diff --git a/docs/api/url-shortening.asciidoc b/docs/api/url-shortening.asciidoc index a62529e11a9ba..ffe1d925e5dcb 100644 --- a/docs/api/url-shortening.asciidoc +++ b/docs/api/url-shortening.asciidoc @@ -1,5 +1,5 @@ [[url-shortening-api]] -=== Shorten URL API +== Shorten URL API ++++ Shorten URL ++++ @@ -9,34 +9,39 @@ Internet Explorer has URL length restrictions, and some wiki and markup parsers Short URLs are designed to make sharing {kib} URLs easier. +[float] [[url-shortening-api-request]] -==== Request +=== Request `POST :/api/shorten_url` +[float] [[url-shortening-api-request-body]] -==== Request body +=== Request body `url`:: (Required, string) The {kib} URL that you want to shorten, relative to `/app/kibana`. +[float] [[url-shortening-api-response-body]] -==== Response body +=== Response body urlId:: A top-level property that contains the shortened URL token for the provided request body. +[float] [[url-shortening-api-codes]] -==== Response code +=== Response code `200`:: Indicates a successful call. +[float] [[url-shortening-api-example]] -==== Example +=== Example [source,sh] -------------------------------------------------- -$ curl -X POST "localhost:5601/api/shorten_url" +$ curl -X POST api/shorten_url { "url": "/app/kibana#/dashboard?_g=()&_a=(description:'',filters:!(),fullScreenMode:!f,options:(hidePanelTitles:!f,useMargins:!t),panels:!((embeddableConfig:(),gridData:(h:15,i:'1',w:24,x:0,y:0),id:'8f4d0c00-4c86-11e8-b3d7-01146121b73d',panelIndex:'1',type:visualization,version:'7.0.0-alpha1')),query:(language:lucene,query:''),timeRestore:!f,title:'New%20Dashboard',viewMode:edit)" } diff --git a/docs/api/using-api.asciidoc b/docs/api/using-api.asciidoc index e58d9c39ee8c4..c61edfb62b079 100644 --- a/docs/api/using-api.asciidoc +++ b/docs/api/using-api.asciidoc @@ -10,7 +10,23 @@ NOTE: The {kib} Console supports only Elasticsearch APIs. You are unable to inte [float] [[api-authentication]] === Authentication -{kib} supports token-based authentication with the same username and password that you use to log into the {kib} Console. In a given HTTP tool, and when available, you can select to use its 'Basic Authentication' option, which is where the username and password are stored in order to be passed as part of the call. +The {kib} APIs support key- and token-based authentication. + +[float] +[[token-api-authentication]] +==== Token-based authentication + +To use token-based authentication, you use the same username and password that you use to log into Elastic. +In a given HTTP tool, and when available, you can select to use its 'Basic Authentication' option, +which is where the username and password are stored in order to be passed as part of the call. + +[float] +[[key-authentication]] +==== Key-based authentication + +To use key-based authentication, you create an API key using the Elastic Console, then specify the key in the header of your API calls. + +For information about API keys, refer to <>. [float] [[api-calls]] @@ -31,7 +47,7 @@ For example, the following `curl` command exports a dashboard: [source,sh] -- -curl -X POST -u $USER:$PASSWORD "localhost:5601/api/kibana/dashboards/export?dashboard=942dcef0-b2cd-11e8-ad8e-85441f0c2e5c" +curl -X POST api/kibana/dashboards/export?dashboard=942dcef0-b2cd-11e8-ad8e-85441f0c2e5c -- // KIBANA @@ -51,7 +67,8 @@ For all APIs, you must use a request header. The {kib} APIs support the `kbn-xsr * XSRF protections are disabled using the `server.xsrf.disableProtection` setting `Content-Type: application/json`:: - Applicable only when you send a payload in the API request. {kib} API requests and responses use JSON. Typically, if you include the `kbn-xsrf` header, you must also include the `Content-Type` header. + Applicable only when you send a payload in the API request. {kib} API requests and responses use JSON. + Typically, if you include the `kbn-xsrf` header, you must also include the `Content-Type` header. Request header example: diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.esaggsexpressionfunctiondefinition.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.esaggsexpressionfunctiondefinition.md new file mode 100644 index 0000000000000..6cf05dde27627 --- /dev/null +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.esaggsexpressionfunctiondefinition.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) > [EsaggsExpressionFunctionDefinition](./kibana-plugin-plugins-data-public.esaggsexpressionfunctiondefinition.md) + +## EsaggsExpressionFunctionDefinition type + +Signature: + +```typescript +export declare type EsaggsExpressionFunctionDefinition = ExpressionFunctionDefinition<'esaggs', Input, Arguments, Output>; +``` diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md index 7cb6ef64431bf..4852ad15781c7 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md @@ -125,6 +125,7 @@ | [AggGroupName](./kibana-plugin-plugins-data-public.agggroupname.md) | | | [AggParam](./kibana-plugin-plugins-data-public.aggparam.md) | | | [CustomFilter](./kibana-plugin-plugins-data-public.customfilter.md) | | +| [EsaggsExpressionFunctionDefinition](./kibana-plugin-plugins-data-public.esaggsexpressionfunctiondefinition.md) | | | [EsQuerySortValue](./kibana-plugin-plugins-data-public.esquerysortvalue.md) | | | [ExistsFilter](./kibana-plugin-plugins-data-public.existsfilter.md) | | | [FieldFormatId](./kibana-plugin-plugins-data-public.fieldformatid.md) | id type is needed for creating custom converters. | diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.esaggsexpressionfunctiondefinition.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.esaggsexpressionfunctiondefinition.md new file mode 100644 index 0000000000000..572c4e0c1eb2f --- /dev/null +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.esaggsexpressionfunctiondefinition.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) > [EsaggsExpressionFunctionDefinition](./kibana-plugin-plugins-data-server.esaggsexpressionfunctiondefinition.md) + +## EsaggsExpressionFunctionDefinition type + +Signature: + +```typescript +export declare type EsaggsExpressionFunctionDefinition = ExpressionFunctionDefinition<'esaggs', Input, Arguments, Output>; +``` diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.irequesttypesmap.es.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.irequesttypesmap.es.md deleted file mode 100644 index 9cebff05dc9db..0000000000000 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.irequesttypesmap.es.md +++ /dev/null @@ -1,11 +0,0 @@ - - -[Home](./index.md) > [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) > [IRequestTypesMap](./kibana-plugin-plugins-data-server.irequesttypesmap.md) > [es](./kibana-plugin-plugins-data-server.irequesttypesmap.es.md) - -## IRequestTypesMap.es property - -Signature: - -```typescript -[ES_SEARCH_STRATEGY]: IEsSearchRequest; -``` diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.irequesttypesmap.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.irequesttypesmap.md deleted file mode 100644 index 3f5e4ba0f7799..0000000000000 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.irequesttypesmap.md +++ /dev/null @@ -1,20 +0,0 @@ - - -[Home](./index.md) > [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) > [IRequestTypesMap](./kibana-plugin-plugins-data-server.irequesttypesmap.md) - -## IRequestTypesMap interface - -The map of search strategy IDs to the corresponding request type definitions. - -Signature: - -```typescript -export interface IRequestTypesMap -``` - -## Properties - -| Property | Type | Description | -| --- | --- | --- | -| [es](./kibana-plugin-plugins-data-server.irequesttypesmap.es.md) | IEsSearchRequest | | - diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.iresponsetypesmap.es.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.iresponsetypesmap.es.md deleted file mode 100644 index 1154fc141d6c7..0000000000000 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.iresponsetypesmap.es.md +++ /dev/null @@ -1,11 +0,0 @@ - - -[Home](./index.md) > [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) > [IResponseTypesMap](./kibana-plugin-plugins-data-server.iresponsetypesmap.md) > [es](./kibana-plugin-plugins-data-server.iresponsetypesmap.es.md) - -## IResponseTypesMap.es property - -Signature: - -```typescript -[ES_SEARCH_STRATEGY]: IEsSearchResponse; -``` diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.iresponsetypesmap.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.iresponsetypesmap.md deleted file mode 100644 index 629ab4347eda8..0000000000000 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.iresponsetypesmap.md +++ /dev/null @@ -1,20 +0,0 @@ - - -[Home](./index.md) > [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) > [IResponseTypesMap](./kibana-plugin-plugins-data-server.iresponsetypesmap.md) - -## IResponseTypesMap interface - -The map of search strategy IDs to the corresponding response type definitions. - -Signature: - -```typescript -export interface IResponseTypesMap -``` - -## Properties - -| Property | Type | Description | -| --- | --- | --- | -| [es](./kibana-plugin-plugins-data-server.iresponsetypesmap.es.md) | IEsSearchResponse | | - diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearch.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearch.md deleted file mode 100644 index 96991579c1716..0000000000000 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearch.md +++ /dev/null @@ -1,11 +0,0 @@ - - -[Home](./index.md) > [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) > [ISearch](./kibana-plugin-plugins-data-server.isearch.md) - -## ISearch type - -Signature: - -```typescript -export declare type ISearch = (context: RequestHandlerContext, request: IRequestTypesMap[T], options?: ISearchOptions) => Promise; -``` diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchcancel.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchcancel.md deleted file mode 100644 index b5a687d1b19d8..0000000000000 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchcancel.md +++ /dev/null @@ -1,11 +0,0 @@ - - -[Home](./index.md) > [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) > [ISearchCancel](./kibana-plugin-plugins-data-server.isearchcancel.md) - -## ISearchCancel type - -Signature: - -```typescript -export declare type ISearchCancel = (context: RequestHandlerContext, id: string) => Promise; -``` diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchoptions.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchoptions.md index 49412fc42d3b5..002ce864a1aa4 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchoptions.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchoptions.md @@ -15,4 +15,5 @@ export interface ISearchOptions | Property | Type | Description | | --- | --- | --- | | [signal](./kibana-plugin-plugins-data-server.isearchoptions.signal.md) | AbortSignal | An AbortSignal that allows the caller of search to abort a search request. | +| [strategy](./kibana-plugin-plugins-data-server.isearchoptions.strategy.md) | string | | diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchoptions.strategy.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchoptions.strategy.md new file mode 100644 index 0000000000000..6df72d023e2c0 --- /dev/null +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchoptions.strategy.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) > [ISearchOptions](./kibana-plugin-plugins-data-server.isearchoptions.md) > [strategy](./kibana-plugin-plugins-data-server.isearchoptions.strategy.md) + +## ISearchOptions.strategy property + +Signature: + +```typescript +strategy?: string; +``` diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchsetup.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchsetup.md index 93e253b2e98a3..ca8ad8fdc06ea 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchsetup.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchsetup.md @@ -14,5 +14,5 @@ export interface ISearchSetup | Property | Type | Description | | --- | --- | --- | -| [registerSearchStrategy](./kibana-plugin-plugins-data-server.isearchsetup.registersearchstrategy.md) | TRegisterSearchStrategy | Extension point exposed for other plugins to register their own search strategies. | +| [registerSearchStrategy](./kibana-plugin-plugins-data-server.isearchsetup.registersearchstrategy.md) | (name: string, strategy: ISearchStrategy) => void | Extension point exposed for other plugins to register their own search strategies. | diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchsetup.registersearchstrategy.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchsetup.registersearchstrategy.md index c06b8b00806bf..73c575e7095ed 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchsetup.registersearchstrategy.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchsetup.registersearchstrategy.md @@ -9,5 +9,5 @@ Extension point exposed for other plugins to register their own search strategie Signature: ```typescript -registerSearchStrategy: TRegisterSearchStrategy; +registerSearchStrategy: (name: string, strategy: ISearchStrategy) => void; ``` diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstart.getsearchstrategy.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstart.getsearchstrategy.md index 0ba4bf578d6cc..970b2811a574b 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstart.getsearchstrategy.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstart.getsearchstrategy.md @@ -9,5 +9,5 @@ Get other registered search strategies. For example, if a new strategy needs to Signature: ```typescript -getSearchStrategy: TGetSearchStrategy; +getSearchStrategy: (name: string) => ISearchStrategy; ``` diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstart.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstart.md index abe72396f61e1..308ce3cb568bc 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstart.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstart.md @@ -14,5 +14,6 @@ export interface ISearchStart | Property | Type | Description | | --- | --- | --- | -| [getSearchStrategy](./kibana-plugin-plugins-data-server.isearchstart.getsearchstrategy.md) | TGetSearchStrategy | Get other registered search strategies. For example, if a new strategy needs to use the already-registered ES search strategy, it can use this function to accomplish that. | +| [getSearchStrategy](./kibana-plugin-plugins-data-server.isearchstart.getsearchstrategy.md) | (name: string) => ISearchStrategy | Get other registered search strategies. For example, if a new strategy needs to use the already-registered ES search strategy, it can use this function to accomplish that. | +| [search](./kibana-plugin-plugins-data-server.isearchstart.search.md) | (context: RequestHandlerContext, request: IKibanaSearchRequest, options: ISearchOptions) => Promise<IKibanaSearchResponse> | | diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstart.search.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstart.search.md new file mode 100644 index 0000000000000..1c2ae91699559 --- /dev/null +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstart.search.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) > [ISearchStart](./kibana-plugin-plugins-data-server.isearchstart.md) > [search](./kibana-plugin-plugins-data-server.isearchstart.search.md) + +## ISearchStart.search property + +Signature: + +```typescript +search: (context: RequestHandlerContext, request: IKibanaSearchRequest, options: ISearchOptions) => Promise; +``` diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstrategy.cancel.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstrategy.cancel.md index c1e0c3d9f2330..34903697090ea 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstrategy.cancel.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstrategy.cancel.md @@ -7,5 +7,5 @@ Signature: ```typescript -cancel?: ISearchCancel; +cancel?: (context: RequestHandlerContext, id: string) => Promise; ``` diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstrategy.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstrategy.md index 167c6ab6e5a16..d54e027c4b847 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstrategy.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstrategy.md @@ -9,13 +9,13 @@ Search strategy interface contains a search method that takes in a request and r Signature: ```typescript -export interface ISearchStrategy +export interface ISearchStrategy ``` ## Properties | Property | Type | Description | | --- | --- | --- | -| [cancel](./kibana-plugin-plugins-data-server.isearchstrategy.cancel.md) | ISearchCancel<T> | | -| [search](./kibana-plugin-plugins-data-server.isearchstrategy.search.md) | ISearch<T> | | +| [cancel](./kibana-plugin-plugins-data-server.isearchstrategy.cancel.md) | (context: RequestHandlerContext, id: string) => Promise<void> | | +| [search](./kibana-plugin-plugins-data-server.isearchstrategy.search.md) | (context: RequestHandlerContext, request: IEsSearchRequest, options?: ISearchOptions) => Promise<IEsSearchResponse> | | diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstrategy.search.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstrategy.search.md index 34a17ca87807a..1a225d0c9aeab 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstrategy.search.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.isearchstrategy.search.md @@ -7,5 +7,5 @@ Signature: ```typescript -search: ISearch; +search: (context: RequestHandlerContext, request: IEsSearchRequest, options?: ISearchOptions) => Promise; ``` diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.md index c80112fb17dde..6bf481841f334 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.md @@ -40,8 +40,6 @@ | [IIndexPattern](./kibana-plugin-plugins-data-server.iindexpattern.md) | | | [IndexPatternAttributes](./kibana-plugin-plugins-data-server.indexpatternattributes.md) | Use data plugin interface instead | | [IndexPatternFieldDescriptor](./kibana-plugin-plugins-data-server.indexpatternfielddescriptor.md) | | -| [IRequestTypesMap](./kibana-plugin-plugins-data-server.irequesttypesmap.md) | The map of search strategy IDs to the corresponding request type definitions. | -| [IResponseTypesMap](./kibana-plugin-plugins-data-server.iresponsetypesmap.md) | The map of search strategy IDs to the corresponding response type definitions. | | [ISearchOptions](./kibana-plugin-plugins-data-server.isearchoptions.md) | | | [ISearchSetup](./kibana-plugin-plugins-data-server.isearchsetup.md) | | | [ISearchStart](./kibana-plugin-plugins-data-server.isearchstart.md) | | @@ -71,10 +69,8 @@ | Type Alias | Description | | --- | --- | +| [EsaggsExpressionFunctionDefinition](./kibana-plugin-plugins-data-server.esaggsexpressionfunctiondefinition.md) | | | [FieldFormatsGetConfigFn](./kibana-plugin-plugins-data-server.fieldformatsgetconfigfn.md) | | | [IFieldFormatsRegistry](./kibana-plugin-plugins-data-server.ifieldformatsregistry.md) | | -| [ISearch](./kibana-plugin-plugins-data-server.isearch.md) | | -| [ISearchCancel](./kibana-plugin-plugins-data-server.isearchcancel.md) | | | [ParsedInterval](./kibana-plugin-plugins-data-server.parsedinterval.md) | | -| [TStrategyTypes](./kibana-plugin-plugins-data-server.tstrategytypes.md) | Contains all known strategy type identifiers that will be used to map to request and response shapes. Plugins that wish to add their own custom search strategies should extend this type via:const MY\_STRATEGY = 'MY\_STRATEGY';declare module 'src/plugins/search/server' { export interface IRequestTypesMap { \[MY\_STRATEGY\]: IMySearchRequest; }export interface IResponseTypesMap { \[MY\_STRATEGY\]: IMySearchResponse } } | diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.tstrategytypes.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.tstrategytypes.md deleted file mode 100644 index 443d8d1b424d0..0000000000000 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.tstrategytypes.md +++ /dev/null @@ -1,19 +0,0 @@ - - -[Home](./index.md) > [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) > [TStrategyTypes](./kibana-plugin-plugins-data-server.tstrategytypes.md) - -## TStrategyTypes type - -Contains all known strategy type identifiers that will be used to map to request and response shapes. Plugins that wish to add their own custom search strategies should extend this type via: - -const MY\_STRATEGY = 'MY\_STRATEGY'; - -declare module 'src/plugins/search/server' { export interface IRequestTypesMap { \[MY\_STRATEGY\]: IMySearchRequest; } - -export interface IResponseTypesMap { \[MY\_STRATEGY\]: IMySearchResponse } } - -Signature: - -```typescript -export declare type TStrategyTypes = typeof ES_SEARCH_STRATEGY | string; -``` diff --git a/docs/management/advanced-options.asciidoc b/docs/management/advanced-options.asciidoc index 561919738786e..9a94c25bcdf6e 100644 --- a/docs/management/advanced-options.asciidoc +++ b/docs/management/advanced-options.asciidoc @@ -247,7 +247,7 @@ retrieved. `timelion:es.timefield`:: The default field containing a timestamp when using the `.es()` query. `timelion:graphite.url`:: [experimental] Used with graphite queries, this is the URL of your graphite host in the form https://www.hostedgraphite.com/UID/ACCESS_KEY/graphite. This URL can be -selected from a whitelist configured in the `kibana.yml` under `timelion.graphiteUrls`. +selected from an allow-list configured in the `kibana.yml` under `timelion.graphiteUrls`. `timelion:max_buckets`:: The maximum number of buckets a single data source can return. This value is used for calculating automatic intervals in visualizations. `timelion:min_interval`:: The smallest interval to calculate when using "auto". diff --git a/docs/settings/ingest-manager-settings.asciidoc b/docs/settings/ingest-manager-settings.asciidoc index 604471edc4d59..30e11f726c26b 100644 --- a/docs/settings/ingest-manager-settings.asciidoc +++ b/docs/settings/ingest-manager-settings.asciidoc @@ -8,8 +8,7 @@ experimental[] You can configure `xpack.ingestManager` settings in your `kibana.yml`. -By default, {ingest-manager} is not enabled. You need to -enable it. To use {fleet}, you also need to configure {kib} and {es} hosts. +By default, {ingest-manager} is enabled. To use {fleet}, you also need to configure {kib} and {es} hosts. See the {ingest-guide}/index.html[Ingest Management] docs for more information. @@ -19,7 +18,7 @@ See the {ingest-guide}/index.html[Ingest Management] docs for more information. [cols="2*<"] |=== | `xpack.ingestManager.enabled` {ess-icon} - | Set to `true` to enable {ingest-manager}. + | Set to `true` (default) to enable {ingest-manager}. | `xpack.ingestManager.fleet.enabled` {ess-icon} | Set to `true` (default) to enable {fleet}. |=== diff --git a/packages/kbn-config-schema/src/types/string_type.ts b/packages/kbn-config-schema/src/types/string_type.ts index cb780bcbbc6bd..c7d386df7c3ba 100644 --- a/packages/kbn-config-schema/src/types/string_type.ts +++ b/packages/kbn-config-schema/src/types/string_type.ts @@ -29,8 +29,8 @@ export type StringOptions = TypeOptions & { export class StringType extends Type { constructor(options: StringOptions = {}) { - // We want to allow empty strings, however calling `allow('')` casues - // Joi to whitelist the value and skip any additional validation. + // We want to allow empty strings, however calling `allow('')` causes + // Joi to allow the value and skip any additional validation. // Instead, we reimplement the string validator manually except in the // hostname case where empty strings aren't allowed anyways. let schema = diff --git a/packages/kbn-optimizer/src/integration_tests/__snapshots__/basic_optimization.test.ts.snap b/packages/kbn-optimizer/src/integration_tests/__snapshots__/basic_optimization.test.ts.snap index c52873ab7ec20..109188e163d06 100644 --- a/packages/kbn-optimizer/src/integration_tests/__snapshots__/basic_optimization.test.ts.snap +++ b/packages/kbn-optimizer/src/integration_tests/__snapshots__/basic_optimization.test.ts.snap @@ -75,4 +75,4 @@ exports[`prepares assets for distribution: bar bundle 1`] = `"(function(modules) exports[`prepares assets for distribution: foo async bundle 1`] = `"(window[\\"foo_bundle_jsonpfunction\\"]=window[\\"foo_bundle_jsonpfunction\\"]||[]).push([[1],{3:function(module,__webpack_exports__,__webpack_require__){\\"use strict\\";__webpack_require__.r(__webpack_exports__);__webpack_require__.d(__webpack_exports__,\\"foo\\",(function(){return foo}));function foo(){}}}]);"`; -exports[`prepares assets for distribution: foo bundle 1`] = `"(function(modules){function webpackJsonpCallback(data){var chunkIds=data[0];var moreModules=data[1];var moduleId,chunkId,i=0,resolves=[];for(;i { expectFileMatchesSnapshotWithCompression('plugins/foo/target/public/foo.plugin.js', 'foo bundle'); expectFileMatchesSnapshotWithCompression( - 'plugins/foo/target/public/1.plugin.js', + 'plugins/foo/target/public/foo.chunk.1.js', 'foo async bundle' ); expectFileMatchesSnapshotWithCompression('plugins/bar/target/public/bar.plugin.js', 'bar bundle'); diff --git a/packages/kbn-optimizer/src/report_optimizer_stats.ts b/packages/kbn-optimizer/src/report_optimizer_stats.ts index 5f3153bff5175..2f92f3d648ab7 100644 --- a/packages/kbn-optimizer/src/report_optimizer_stats.ts +++ b/packages/kbn-optimizer/src/report_optimizer_stats.ts @@ -17,6 +17,9 @@ * under the License. */ +import Fs from 'fs'; +import Path from 'path'; + import { materialize, mergeMap, dematerialize } from 'rxjs/operators'; import { CiStatsReporter } from '@kbn/dev-utils'; @@ -24,6 +27,32 @@ import { OptimizerUpdate$ } from './run_optimizer'; import { OptimizerState, OptimizerConfig } from './optimizer'; import { pipeClosure } from './common'; +const flatten = (arr: Array): T[] => + arr.reduce((acc: T[], item) => acc.concat(item), []); + +interface Entry { + relPath: string; + stats: Fs.Stats; +} + +const getFiles = (dir: string, parent?: string) => + flatten( + Fs.readdirSync(dir).map((name): Entry | Entry[] => { + const absPath = Path.join(dir, name); + const relPath = parent ? Path.join(parent, name) : name; + const stats = Fs.statSync(absPath); + + if (stats.isDirectory()) { + return getFiles(absPath, relPath); + } + + return { + relPath, + stats, + }; + }) + ); + export function reportOptimizerStats(reporter: CiStatsReporter, config: OptimizerConfig) { return pipeClosure((update$: OptimizerUpdate$) => { let lastState: OptimizerState | undefined; @@ -36,16 +65,55 @@ export function reportOptimizerStats(reporter: CiStatsReporter, config: Optimize if (n.kind === 'C' && lastState) { await reporter.metrics( - config.bundles.map((bundle) => { - // make the cache read from the cache file since it was likely updated by the worker - bundle.cache.refresh(); - - return { - group: `@kbn/optimizer bundle module count`, - id: bundle.id, - value: bundle.cache.getModuleCount() || 0, - }; - }) + flatten( + config.bundles.map((bundle) => { + // make the cache read from the cache file since it was likely updated by the worker + bundle.cache.refresh(); + + const outputFiles = getFiles(bundle.outputDir).filter( + (file) => !(file.relPath.startsWith('.') || file.relPath.endsWith('.map')) + ); + + const entryName = `${bundle.id}.${bundle.type}.js`; + const entry = outputFiles.find((f) => f.relPath === entryName); + if (!entry) { + throw new Error( + `Unable to find bundle entry named [${entryName}] in [${bundle.outputDir}]` + ); + } + + const chunkPrefix = `${bundle.id}.chunk.`; + const asyncChunks = outputFiles.filter((f) => f.relPath.startsWith(chunkPrefix)); + const miscFiles = outputFiles.filter( + (f) => f !== entry && !asyncChunks.includes(f) + ); + const sumSize = (files: Entry[]) => + files.reduce((acc: number, f) => acc + f.stats!.size, 0); + + return [ + { + group: `@kbn/optimizer bundle module count`, + id: bundle.id, + value: bundle.cache.getModuleCount() || 0, + }, + { + group: `page load bundle size`, + id: bundle.id, + value: entry.stats!.size, + }, + { + group: `async chunks size`, + id: bundle.id, + value: sumSize(asyncChunks), + }, + { + group: `miscellaneous assets size`, + id: bundle.id, + value: sumSize(miscFiles), + }, + ]; + }) + ) ); } diff --git a/packages/kbn-optimizer/src/worker/webpack.config.ts b/packages/kbn-optimizer/src/worker/webpack.config.ts index aaea70d12c60d..271ad49aee351 100644 --- a/packages/kbn-optimizer/src/worker/webpack.config.ts +++ b/packages/kbn-optimizer/src/worker/webpack.config.ts @@ -52,7 +52,8 @@ export function getWebpackConfig(bundle: Bundle, bundleRefs: BundleRefs, worker: output: { path: bundle.outputDir, - filename: `[name].${bundle.type}.js`, + filename: `${bundle.id}.${bundle.type}.js`, + chunkFilename: `${bundle.id}.chunk.[id].js`, devtoolModuleFilenameTemplate: (info) => `/${bundle.type}:${bundle.id}/${Path.relative( bundle.sourceRoot, diff --git a/packages/kbn-test/package.json b/packages/kbn-test/package.json index 0c49ccf276b2b..38e4668fc1e42 100644 --- a/packages/kbn-test/package.json +++ b/packages/kbn-test/package.json @@ -16,7 +16,6 @@ "@types/joi": "^13.4.2", "@types/lodash": "^4.14.155", "@types/parse-link-header": "^1.0.0", - "@types/puppeteer": "^3.0.0", "@types/strip-ansi": "^5.2.1", "@types/xml2js": "^0.4.5", "diff": "^4.0.1" @@ -31,7 +30,6 @@ "joi": "^13.5.2", "lodash": "^4.17.15", "parse-link-header": "^1.0.1", - "puppeteer": "^3.3.0", "rxjs": "^6.5.5", "strip-ansi": "^5.2.0", "tar-fs": "^1.16.3", diff --git a/packages/kbn-test/src/index.ts b/packages/kbn-test/src/index.ts index 46f753b909553..f7321ca713087 100644 --- a/packages/kbn-test/src/index.ts +++ b/packages/kbn-test/src/index.ts @@ -60,4 +60,3 @@ export { makeJunitReportPath } from './junit_report_path'; export { CI_PARALLEL_PROCESS_PREFIX } from './ci_parallel_process_prefix'; export * from './functional_test_runner'; -export * from './page_load_metrics'; diff --git a/packages/kbn-test/src/page_load_metrics/capture_page_load_metrics.ts b/packages/kbn-test/src/page_load_metrics/capture_page_load_metrics.ts deleted file mode 100644 index 013d49a29a51c..0000000000000 --- a/packages/kbn-test/src/page_load_metrics/capture_page_load_metrics.ts +++ /dev/null @@ -1,81 +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 { ToolingLog } from '@kbn/dev-utils'; -import { NavigationOptions, createUrl, navigateToApps } from './navigation'; - -export async function capturePageLoadMetrics(log: ToolingLog, options: NavigationOptions) { - const responsesByPageView = await navigateToApps(log, options); - - const assetSizeMeasurements = new Map(); - - const numberOfPagesVisited = responsesByPageView.size; - - for (const [, frameResponses] of responsesByPageView) { - for (const [, { url, dataLength }] of frameResponses) { - if (url.length === 0) { - throw new Error('navigateToApps(); failed to identify the url of the request'); - } - if (assetSizeMeasurements.has(url)) { - assetSizeMeasurements.set(url, [dataLength].concat(assetSizeMeasurements.get(url) || [])); - } else { - assetSizeMeasurements.set(url, [dataLength]); - } - } - } - - return Array.from(assetSizeMeasurements.entries()) - .map(([url, measurements]) => { - const baseUrl = createUrl('/', options.appConfig.url); - const relativeUrl = url - // remove the baseUrl (expect the trailing slash) to make url relative - .replace(baseUrl.slice(0, -1), '') - // strip the build number from asset urls - .replace(/^\/\d+\//, '/'); - return [relativeUrl, measurements] as const; - }) - .filter(([url, measurements]) => { - if (measurements.length !== numberOfPagesVisited) { - // ignore urls seen only on some pages - return false; - } - - if (url.startsWith('data:')) { - // ignore data urls since they are already counted by other assets - return false; - } - - if (url.startsWith('/api/') || url.startsWith('/internal/')) { - // ignore api requests since they don't have deterministic sizes - return false; - } - - const allMetricsAreEqual = measurements.every((x, i) => - i === 0 ? true : x === measurements[i - 1] - ); - if (!allMetricsAreEqual) { - throw new Error(`measurements for url [${url}] are not equal [${measurements.join(',')}]`); - } - - return true; - }) - .map(([url, measurements]) => { - return { group: 'page load asset size', id: url, value: measurements[0] }; - }); -} diff --git a/packages/kbn-test/src/page_load_metrics/cli.ts b/packages/kbn-test/src/page_load_metrics/cli.ts deleted file mode 100644 index 95421384c79cb..0000000000000 --- a/packages/kbn-test/src/page_load_metrics/cli.ts +++ /dev/null @@ -1,90 +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 Url from 'url'; - -import { run, createFlagError } from '@kbn/dev-utils'; -import { resolve, basename } from 'path'; -import { capturePageLoadMetrics } from './capture_page_load_metrics'; - -const defaultScreenshotsDir = resolve(__dirname, 'screenshots'); - -export function runPageLoadMetricsCli() { - run( - async ({ flags, log }) => { - const kibanaUrl = flags['kibana-url']; - if (!kibanaUrl || typeof kibanaUrl !== 'string') { - throw createFlagError('Expect --kibana-url to be a string'); - } - - const parsedUrl = Url.parse(kibanaUrl); - - const [username, password] = parsedUrl.auth - ? parsedUrl.auth.split(':') - : [flags.username, flags.password]; - - if (typeof username !== 'string' || typeof password !== 'string') { - throw createFlagError( - 'Mising username and/or password, either specify in --kibana-url or pass --username and --password' - ); - } - - const headless = !flags.head; - - const screenshotsDir = flags.screenshotsDir || defaultScreenshotsDir; - - if (typeof screenshotsDir !== 'string' || screenshotsDir === basename(screenshotsDir)) { - throw createFlagError('Expect screenshotsDir to be valid path string'); - } - - const metrics = await capturePageLoadMetrics(log, { - headless, - appConfig: { - url: kibanaUrl, - username, - password, - }, - screenshotsDir, - }); - for (const metric of metrics) { - log.info(`${metric.id}: ${metric.value}`); - } - }, - { - description: `Loads several pages with Puppeteer to capture the size of assets`, - flags: { - string: ['kibana-url', 'username', 'password', 'screenshotsDir'], - boolean: ['head'], - default: { - username: 'elastic', - password: 'changeme', - debug: true, - screenshotsDir: defaultScreenshotsDir, - }, - help: ` - --kibana-url Url for Kibana we should connect to, can include login info - --head Run puppeteer with graphical user interface - --username Set username, defaults to 'elastic' - --password Set password, defaults to 'changeme' - --screenshotsDir Set screenshots directory, defaults to '${defaultScreenshotsDir}' - `, - }, - } - ); -} diff --git a/packages/kbn-test/src/page_load_metrics/navigation.ts b/packages/kbn-test/src/page_load_metrics/navigation.ts deleted file mode 100644 index db53df789ac69..0000000000000 --- a/packages/kbn-test/src/page_load_metrics/navigation.ts +++ /dev/null @@ -1,164 +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 Fs from 'fs'; -import Url from 'url'; -import puppeteer from 'puppeteer'; -import { resolve } from 'path'; -import { ToolingLog } from '@kbn/dev-utils'; -import { ResponseReceivedEvent, DataReceivedEvent } from './event'; - -export interface NavigationOptions { - headless: boolean; - appConfig: { url: string; username: string; password: string }; - screenshotsDir: string; -} - -export type NavigationResults = Map>; - -interface FrameResponse { - url: string; - dataLength: number; -} - -function joinPath(pathA: string, pathB: string) { - return `${pathA.endsWith('/') ? pathA.slice(0, -1) : pathA}/${ - pathB.startsWith('/') ? pathB.slice(1) : pathB - }`; -} - -export function createUrl(path: string, url: string) { - const baseUrl = Url.parse(url); - return Url.format({ - protocol: baseUrl.protocol, - hostname: baseUrl.hostname, - port: baseUrl.port, - pathname: joinPath(baseUrl.pathname || '', path), - }); -} - -async function loginToKibana( - log: ToolingLog, - browser: puppeteer.Browser, - options: NavigationOptions -) { - log.debug(`log in to the app..`); - const page = await browser.newPage(); - const loginUrl = createUrl('/login', options.appConfig.url); - await page.goto(loginUrl, { - waitUntil: 'networkidle0', - }); - await page.type('[data-test-subj="loginUsername"]', options.appConfig.username); - await page.type('[data-test-subj="loginPassword"]', options.appConfig.password); - await page.click('[data-test-subj="loginSubmit"]'); - await page.waitForNavigation({ waitUntil: 'networkidle0' }); - await page.close(); -} - -export async function navigateToApps(log: ToolingLog, options: NavigationOptions) { - const browser = await puppeteer.launch({ headless: options.headless, args: ['--no-sandbox'] }); - const devToolsResponses: NavigationResults = new Map(); - const apps = [ - { path: '/app/discover', locator: '[data-test-subj="discover-sidebar"]' }, - { path: '/app/home', locator: '[data-test-subj="homeApp"]' }, - { path: '/app/canvas', locator: '[data-test-subj="create-workpad-button"]' }, - { path: '/app/maps', locator: '[title="Maps"]' }, - { path: '/app/apm', locator: '[data-test-subj="apmMainContainer"]' }, - ]; - - await loginToKibana(log, browser, options); - - await Promise.all( - apps.map(async (app) => { - const page = await browser.newPage(); - page.setCacheEnabled(false); - page.setDefaultNavigationTimeout(0); - const frameResponses = new Map(); - devToolsResponses.set(app.path, frameResponses); - - const client = await page.target().createCDPSession(); - await client.send('Network.enable'); - - function getRequestData(requestId: string) { - if (!frameResponses.has(requestId)) { - frameResponses.set(requestId, { url: '', dataLength: 0 }); - } - - return frameResponses.get(requestId)!; - } - - client.on('Network.responseReceived', (event: ResponseReceivedEvent) => { - getRequestData(event.requestId).url = event.response.url; - }); - - client.on('Network.dataReceived', (event: DataReceivedEvent) => { - getRequestData(event.requestId).dataLength += event.dataLength; - }); - - const url = createUrl(app.path, options.appConfig.url); - log.debug(`goto ${url}`); - await page.goto(url, { - waitUntil: 'networkidle0', - }); - - let readyAttempt = 0; - let selectorFound = false; - while (!selectorFound) { - readyAttempt += 1; - try { - await page.waitForSelector(app.locator, { timeout: 5000 }); - selectorFound = true; - } catch (error) { - log.error( - `Page '${app.path}' was not loaded properly, unable to find '${ - app.locator - }', url: ${page.url()}` - ); - - if (readyAttempt < 6) { - continue; - } - - const failureDir = resolve(options.screenshotsDir, 'failure'); - const screenshotPath = resolve( - failureDir, - `${app.path.slice(1).split('/').join('_')}_navigation.png` - ); - Fs.mkdirSync(failureDir, { recursive: true }); - - await page.bringToFront(); - await page.screenshot({ - path: screenshotPath, - type: 'png', - fullPage: true, - }); - log.debug(`Saving screenshot to ${screenshotPath}`); - - throw new Error(`Page load timeout: ${app.path} not loaded after 30 seconds`); - } - } - - await page.close(); - }) - ); - - await browser.close(); - - return devToolsResponses; -} diff --git a/src/core/MIGRATION.md b/src/core/MIGRATION.md index 90b1bb4fd5320..f7acff14915a7 100644 --- a/src/core/MIGRATION.md +++ b/src/core/MIGRATION.md @@ -942,7 +942,7 @@ export class MyPlugin implements Plugin { return mountApp(await core.getStartServices(), params); }, }); - plugins.management.sections.getSection('another').registerApp({ + plugins.management.sections.section.kibana.registerApp({ id: 'app', title: 'My app', order: 1, @@ -1309,7 +1309,7 @@ This table shows where these uiExports have moved to in the New Platform. In mos | `hacks` | n/a | Just run the code in your plugin's `start` method. | | `home` | [`plugins.home.featureCatalogue.register`](./src/plugins/home/public/feature_catalogue) | Must add `home` as a dependency in your kibana.json. | | `indexManagement` | | Should be an API on the indexManagement plugin. | -| `injectDefaultVars` | n/a | Plugins will only be able to "whitelist" config values for the frontend. See [#41990](https://github.com/elastic/kibana/issues/41990) | +| `injectDefaultVars` | n/a | Plugins will only be able to allow config values for the frontend. See [#41990](https://github.com/elastic/kibana/issues/41990) | | `inspectorViews` | | Should be an API on the data (?) plugin. | | `interpreter` | | Should be an API on the interpreter plugin. | | `links` | n/a | Not necessary, just register your app via `core.application.register` | @@ -1389,7 +1389,7 @@ class MyPlugin { } ``` -If your plugin also have a client-side part, you can also expose configuration properties to it using a whitelisting mechanism with the configuration `exposeToBrowser` property. +If your plugin also have a client-side part, you can also expose configuration properties to it using the configuration `exposeToBrowser` allow-list property. ```typescript // my_plugin/server/index.ts diff --git a/src/core/server/elasticsearch/client/mocks.ts b/src/core/server/elasticsearch/client/mocks.ts index 75644435a7f2a..34e83922d4d86 100644 --- a/src/core/server/elasticsearch/client/mocks.ts +++ b/src/core/server/elasticsearch/client/mocks.ts @@ -28,7 +28,7 @@ const createInternalClientMock = (): DeeplyMockedKeys => { node: 'http://localhost', }) as any; - const blackListedProps = [ + const omittedProps = [ '_events', '_eventsCount', '_maxListeners', @@ -39,9 +39,9 @@ const createInternalClientMock = (): DeeplyMockedKeys => { 'helpers', ]; - const mockify = (obj: Record, blacklist: string[] = []) => { + const mockify = (obj: Record, omitted: string[] = []) => { Object.keys(obj) - .filter((key) => !blacklist.includes(key)) + .filter((key) => !omitted.includes(key)) .forEach((key) => { const propType = typeof obj[key]; if (propType === 'function') { @@ -52,7 +52,7 @@ const createInternalClientMock = (): DeeplyMockedKeys => { }); }; - mockify(client, blackListedProps); + mockify(client, omittedProps); client.transport = { request: jest.fn(), diff --git a/src/core/server/saved_objects/mappings/lib/get_root_properties_objects.ts b/src/core/server/saved_objects/mappings/lib/get_root_properties_objects.ts index 81ba1d8235561..a998dbee0259e 100644 --- a/src/core/server/saved_objects/mappings/lib/get_root_properties_objects.ts +++ b/src/core/server/saved_objects/mappings/lib/get_root_properties_objects.ts @@ -39,14 +39,14 @@ import { getRootProperties } from './get_root_properties'; * @return {EsPropertyMappings} */ -const blacklist = ['migrationVersion', 'references']; +const omittedRootProps = ['migrationVersion', 'references']; export function getRootPropertiesObjects(mappings: IndexMapping) { const rootProperties = getRootProperties(mappings); return Object.entries(rootProperties).reduce((acc, [key, value]) => { // we consider the existence of the properties or type of object to designate that this is an object datatype if ( - !blacklist.includes(key) && + !omittedRootProps.includes(key) && ((value as SavedObjectsComplexFieldMapping).properties || value.type === 'object') ) { acc[key] = value; diff --git a/src/dev/precommit_hook/casing_check_config.js b/src/dev/precommit_hook/casing_check_config.js index cec80dd547a53..b8eacdd6a3897 100644 --- a/src/dev/precommit_hook/casing_check_config.js +++ b/src/dev/precommit_hook/casing_check_config.js @@ -173,12 +173,12 @@ export const TEMPORARILY_IGNORED_PATHS = [ 'x-pack/plugins/monitoring/public/icons/health-green.svg', 'x-pack/plugins/monitoring/public/icons/health-red.svg', 'x-pack/plugins/monitoring/public/icons/health-yellow.svg', - 'x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/noto/NotoSansCJKtc-Medium.ttf', - 'x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/noto/NotoSansCJKtc-Regular.ttf', - 'x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/roboto/Roboto-Italic.ttf', - 'x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/roboto/Roboto-Medium.ttf', - 'x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/roboto/Roboto-Regular.ttf', - 'x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/img/logo-grey.png', + 'x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/noto/NotoSansCJKtc-Medium.ttf', + 'x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/noto/NotoSansCJKtc-Regular.ttf', + 'x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/roboto/Roboto-Italic.ttf', + 'x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/roboto/Roboto-Medium.ttf', + 'x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/roboto/Roboto-Regular.ttf', + 'x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/img/logo-grey.png', 'x-pack/test/functional/es_archives/monitoring/beats-with-restarted-instance/data.json.gz', 'x-pack/test/functional/es_archives/monitoring/beats-with-restarted-instance/mappings.json', 'x-pack/test/functional/es_archives/monitoring/logstash-pipelines/data.json.gz', diff --git a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_graph.hjson b/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_graph.hjson deleted file mode 100644 index db19c937ca990..0000000000000 --- a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_graph.hjson +++ /dev/null @@ -1,76 +0,0 @@ -{ - // Adapted from Vega's https://vega.github.io/vega/examples/stacked-area-chart/ - - $schema: https://vega.github.io/schema/vega/v5.json - data: [ - { - name: table - values: [ - {x: 0, y: 28, c: 0}, {x: 0, y: 55, c: 1}, {x: 1, y: 43, c: 0}, {x: 1, y: 91, c: 1}, - {x: 2, y: 81, c: 0}, {x: 2, y: 53, c: 1}, {x: 3, y: 19, c: 0}, {x: 3, y: 87, c: 1}, - {x: 4, y: 52, c: 0}, {x: 4, y: 48, c: 1}, {x: 5, y: 24, c: 0}, {x: 5, y: 49, c: 1}, - {x: 6, y: 87, c: 0}, {x: 6, y: 66, c: 1}, {x: 7, y: 17, c: 0}, {x: 7, y: 27, c: 1}, - {x: 8, y: 68, c: 0}, {x: 8, y: 16, c: 1}, {x: 9, y: 49, c: 0}, {x: 9, y: 15, c: 1} - ] - transform: [ - { - type: stack - groupby: ["x"] - sort: {field: "c"} - field: y - } - ] - } - ] - scales: [ - { - name: x - type: point - range: width - domain: {data: "table", field: "x"} - } - { - name: y - type: linear - range: height - nice: true - zero: true - domain: {data: "table", field: "y1"} - } - { - name: color - type: ordinal - range: category - domain: {data: "table", field: "c"} - } - ] - marks: [ - { - type: group - from: { - facet: {name: "series", data: "table", groupby: "c"} - } - marks: [ - { - type: area - from: {data: "series"} - encode: { - enter: { - interpolate: {value: "monotone"} - x: {scale: "x", field: "x"} - y: {scale: "y", field: "y0"} - y2: {scale: "y", field: "y1"} - fill: {scale: "color", field: "c"} - } - update: { - fillOpacity: {value: 1} - } - hover: { - fillOpacity: {value: 0.5} - } - } - } - ] - } - ] -} diff --git a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_image_512.png b/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_image_512.png deleted file mode 100644 index cc28886794f03..0000000000000 Binary files a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_image_512.png and /dev/null differ diff --git a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_map_image_256.png b/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_map_image_256.png deleted file mode 100644 index ac455ada3900b..0000000000000 Binary files a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_map_image_256.png and /dev/null differ diff --git a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_map_test.hjson b/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_map_test.hjson deleted file mode 100644 index 633b8658ad849..0000000000000 --- a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_map_test.hjson +++ /dev/null @@ -1,20 +0,0 @@ -# This graph creates a single rectangle for the whole graph on top of a map -# Note that the actual map tiles are not loaded -{ - $schema: https://vega.github.io/schema/vega/v5.json - config: { - kibana: {type: "map", mapStyle: false} - } - marks: [ - { - type: rect - encode: { - enter: { - fill: {value: "#0f0"} - width: {signal: "width"} - height: {signal: "height"} - } - } - } - ] -} diff --git a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_tooltip_test.hjson b/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_tooltip_test.hjson deleted file mode 100644 index 77465c8b3f007..0000000000000 --- a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_tooltip_test.hjson +++ /dev/null @@ -1,44 +0,0 @@ -# This graph creates a single rectangle for the whole graph, -# backed by a datum with two fields - fld1 & fld2 -# On mouse over, with 0 delay, it should show tooltip -{ - config: { - kibana: { - tooltips: { - // always center on the mark, not mouse x,y - centerOnMark: false - position: top - padding: 20 - } - } - } - data: [ - { - name: table - values: [ - { - title: This is a long title - fieldA: value of fld1 - fld2: 42 - } - ] - } - ] - $schema: https://vega.github.io/schema/vega/v5.json - marks: [ - { - from: {data: "table"} - type: rect - encode: { - enter: { - fill: {value: "#060"} - x: {signal: "0"} - y: {signal: "0"} - width: {signal: "width"} - height: {signal: "height"} - tooltip: {signal: "datum || null"} - } - } - } - ] -} diff --git a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_visualization.js b/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_visualization.js deleted file mode 100644 index 30e7587707d2e..0000000000000 --- a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_visualization.js +++ /dev/null @@ -1,362 +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 Bluebird from 'bluebird'; -import expect from '@kbn/expect'; -import ngMock from 'ng_mock'; -import $ from 'jquery'; - -import 'leaflet/dist/leaflet.js'; -import 'leaflet-vega'; -// Will be replaced with new path when tests are moved -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { createVegaVisualization } from '../../../../../../plugins/vis_type_vega/public/vega_visualization'; -import { ImageComparator } from 'test_utils/image_comparator'; - -import vegaliteGraph from '!!raw-loader!./vegalite_graph.hjson'; -import vegaliteImage256 from './vegalite_image_256.png'; -import vegaliteImage512 from './vegalite_image_512.png'; - -import vegaGraph from '!!raw-loader!./vega_graph.hjson'; -import vegaImage512 from './vega_image_512.png'; - -import vegaTooltipGraph from '!!raw-loader!./vega_tooltip_test.hjson'; - -import vegaMapGraph from '!!raw-loader!./vega_map_test.hjson'; -import vegaMapImage256 from './vega_map_image_256.png'; -// Will be replaced with new path when tests are moved -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { VegaParser } from '../../../../../../plugins/vis_type_vega/public/data_model/vega_parser'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { SearchAPI } from '../../../../../../plugins/vis_type_vega/public/data_model/search_api'; - -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { createVegaTypeDefinition } from '../../../../../../plugins/vis_type_vega/public/vega_type'; -// TODO This is an integration test and thus requires a running platform. When moving to the new platform, -// this test has to be migrated to the newly created integration test environment. -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { npStart } from 'ui/new_platform'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { BaseVisType } from '../../../../../../plugins/visualizations/public/vis_types/base_vis_type'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { ExprVis } from '../../../../../../plugins/visualizations/public/expressions/vis'; - -import { - setInjectedVars, - setData, - setSavedObjects, - setNotifications, - setKibanaMapFactory, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../../../../../plugins/vis_type_vega/public/services'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { ServiceSettings } from '../../../../../../plugins/maps_legacy/public/map/service_settings'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { KibanaMap } from '../../../../../../plugins/maps_legacy/public/map/kibana_map'; - -const THRESHOLD = 0.1; -const PIXEL_DIFF = 30; - -describe('VegaVisualizations', () => { - let domNode; - let VegaVisualization; - let vis; - let imageComparator; - let vegaVisualizationDependencies; - let vegaVisType; - - setKibanaMapFactory((...args) => new KibanaMap(...args)); - setInjectedVars({ - emsTileLayerId: {}, - enableExternalUrls: true, - esShardTimeout: 10000, - }); - setData(npStart.plugins.data); - setSavedObjects(npStart.core.savedObjects); - setNotifications(npStart.core.notifications); - - const mockMapConfig = { - includeElasticMapsService: true, - proxyElasticMapsServiceInMaps: false, - tilemap: { - deprecated: { - config: { - options: { - attribution: '', - }, - }, - }, - options: { - attribution: '', - minZoom: 0, - maxZoom: 10, - }, - }, - regionmap: { - includeElasticMapsService: true, - layers: [], - }, - manifestServiceUrl: '', - emsFileApiUrl: 'https://vector.maps.elastic.co', - emsTileApiUrl: 'https://tiles.maps.elastic.co', - emsLandingPageUrl: 'https://maps.elastic.co/v7.7', - emsFontLibraryUrl: 'https://tiles.maps.elastic.co/fonts/{fontstack}/{range}.pbf', - emsTileLayerId: { - bright: 'road_map', - desaturated: 'road_map_desaturated', - dark: 'dark_map', - }, - }; - - beforeEach(ngMock.module('kibana')); - beforeEach( - ngMock.inject(() => { - const serviceSettings = new ServiceSettings(mockMapConfig, mockMapConfig.tilemap); - vegaVisualizationDependencies = { - serviceSettings, - core: { - uiSettings: npStart.core.uiSettings, - }, - plugins: { - data: { - query: { - timefilter: { - timefilter: {}, - }, - }, - }, - }, - }; - - vegaVisType = new BaseVisType(createVegaTypeDefinition(vegaVisualizationDependencies)); - VegaVisualization = createVegaVisualization(vegaVisualizationDependencies); - }) - ); - - describe('VegaVisualization - basics', () => { - beforeEach(async function () { - setupDOM('512px', '512px'); - imageComparator = new ImageComparator(); - - vis = new ExprVis({ - type: vegaVisType, - }); - }); - - afterEach(function () { - teardownDOM(); - imageComparator.destroy(); - }); - - it('should show vegalite graph and update on resize (may fail in dev env)', async function () { - let vegaVis; - try { - vegaVis = new VegaVisualization(domNode, vis); - - const vegaParser = new VegaParser( - vegaliteGraph, - new SearchAPI({ - search: npStart.plugins.data.search, - uiSettings: npStart.core.uiSettings, - injectedMetadata: npStart.core.injectedMetadata, - }) - ); - await vegaParser.parseAsync(); - - await vegaVis.render(vegaParser, vis.params, { data: true }); - const mismatchedPixels1 = await compareImage(vegaliteImage512); - expect(mismatchedPixels1).to.be.lessThan(PIXEL_DIFF); - - domNode.style.width = '256px'; - domNode.style.height = '256px'; - - await vegaVis.render(vegaParser, vis.params, { resize: true }); - const mismatchedPixels2 = await compareImage(vegaliteImage256); - expect(mismatchedPixels2).to.be.lessThan(PIXEL_DIFF); - } finally { - vegaVis.destroy(); - } - }); - - it('should show vega graph (may fail in dev env)', async function () { - let vegaVis; - try { - vegaVis = new VegaVisualization(domNode, vis); - const vegaParser = new VegaParser( - vegaGraph, - new SearchAPI({ - search: npStart.plugins.data.search, - uiSettings: npStart.core.uiSettings, - injectedMetadata: npStart.core.injectedMetadata, - }) - ); - await vegaParser.parseAsync(); - - await vegaVis.render(vegaParser, vis.params, { data: true }); - const mismatchedPixels = await compareImage(vegaImage512); - - expect(mismatchedPixels).to.be.lessThan(PIXEL_DIFF); - } finally { - vegaVis.destroy(); - } - }); - - it('should show vegatooltip on mouseover over a vega graph (may fail in dev env)', async () => { - let vegaVis; - try { - vegaVis = new VegaVisualization(domNode, vis); - const vegaParser = new VegaParser( - vegaTooltipGraph, - new SearchAPI({ - search: npStart.plugins.data.search, - uiSettings: npStart.core.uiSettings, - injectedMetadata: npStart.core.injectedMetadata, - }) - ); - await vegaParser.parseAsync(); - await vegaVis.render(vegaParser, vis.params, { data: true }); - - const $el = $(domNode); - const offset = $el.offset(); - - const event = new MouseEvent('mousemove', { - view: window, - bubbles: true, - cancelable: true, - clientX: offset.left + 10, - clientY: offset.top + 10, - }); - - $el.find('canvas')[0].dispatchEvent(event); - - await Bluebird.delay(10); - - let tooltip = document.getElementById('vega-kibana-tooltip'); - expect(tooltip).to.be.ok(); - expect(tooltip.innerHTML).to.be( - '

This is a long title

' + - '' + - '' + - '' + - '
fieldA:value of fld1
fld2:42
' - ); - - vegaVis.destroy(); - - tooltip = document.getElementById('vega-kibana-tooltip'); - expect(tooltip).to.not.be.ok(); - } finally { - vegaVis.destroy(); - } - }); - - it('should show vega blank rectangle on top of a map (vegamap)', async () => { - let vegaVis; - try { - vegaVis = new VegaVisualization(domNode, vis); - const vegaParser = new VegaParser( - vegaMapGraph, - new SearchAPI({ - search: npStart.plugins.data.search, - uiSettings: npStart.core.uiSettings, - injectedMetadata: npStart.core.injectedMetadata, - }) - ); - await vegaParser.parseAsync(); - - domNode.style.width = '256px'; - domNode.style.height = '256px'; - - await vegaVis.render(vegaParser, vis.params, { data: true }); - const mismatchedPixels = await compareImage(vegaMapImage256); - expect(mismatchedPixels).to.be.lessThan(PIXEL_DIFF); - } finally { - vegaVis.destroy(); - } - }); - - it('should add a small subpixel value to the height of the canvas to avoid getting it set to 0', async () => { - let vegaVis; - try { - vegaVis = new VegaVisualization(domNode, vis); - const vegaParser = new VegaParser( - `{ - "$schema": "https://vega.github.io/schema/vega/v5.json", - "marks": [ - { - "type": "text", - "encode": { - "update": { - "text": { - "value": "Test" - }, - "align": {"value": "center"}, - "baseline": {"value": "middle"}, - "xc": {"signal": "width/2"}, - "yc": {"signal": "height/2"} - fontSize: {value: "14"} - } - } - } - ] - }`, - new SearchAPI({ - search: npStart.plugins.data.search, - uiSettings: npStart.core.uiSettings, - injectedMetadata: npStart.core.injectedMetadata, - }) - ); - await vegaParser.parseAsync(); - - domNode.style.width = '256px'; - domNode.style.height = '256px'; - - await vegaVis.render(vegaParser, vis.params, { data: true }); - const vegaView = vegaVis._vegaView._view; - expect(vegaView.height()).to.be(250.00000001); - } finally { - vegaVis.destroy(); - } - }); - }); - - async function compareImage(expectedImageSource) { - const elementList = domNode.querySelectorAll('canvas'); - expect(elementList.length).to.equal(1); - const firstCanvasOnMap = elementList[0]; - return imageComparator.compareImage(firstCanvasOnMap, expectedImageSource, THRESHOLD); - } - - function setupDOM(width, height) { - domNode = document.createElement('div'); - domNode.style.top = '0'; - domNode.style.left = '0'; - domNode.style.width = width; - domNode.style.height = height; - domNode.style.position = 'fixed'; - domNode.style.border = '1px solid blue'; - domNode.style['pointer-events'] = 'none'; - document.body.appendChild(domNode); - } - - function teardownDOM() { - domNode.innerHTML = ''; - document.body.removeChild(domNode); - } -}); diff --git a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vegalite_graph.hjson b/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vegalite_graph.hjson deleted file mode 100644 index 2132b0f77e6bc..0000000000000 --- a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vegalite_graph.hjson +++ /dev/null @@ -1,45 +0,0 @@ -{ - $schema: https://vega.github.io/schema/vega-lite/v4.json - data: { - format: {property: "aggregations.time_buckets.buckets"} - values: { - aggregations: { - time_buckets: { - buckets: [ - {key: 1512950400000, doc_count: 0} - {key: 1513036800000, doc_count: 0} - {key: 1513123200000, doc_count: 0} - {key: 1513209600000, doc_count: 4545} - {key: 1513296000000, doc_count: 4667} - {key: 1513382400000, doc_count: 4660} - {key: 1513468800000, doc_count: 133} - {key: 1513555200000, doc_count: 0} - {key: 1513641600000, doc_count: 0} - {key: 1513728000000, doc_count: 0} - ] - } - } - status: 200 - } - } - mark: line - encoding: { - x: { - field: key - type: temporal - axis: null - } - y: { - field: doc_count - type: quantitative - axis: null - } - } - config: { - range: { - category: {scheme: "elastic"} - } - mark: {color: "#54B399"} - } - autosize: {type: "fit", contains: "padding"} -} diff --git a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vegalite_image_256.png b/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vegalite_image_256.png deleted file mode 100644 index 8f2d146287b08..0000000000000 Binary files a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vegalite_image_256.png and /dev/null differ diff --git a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vegalite_image_512.png b/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vegalite_image_512.png deleted file mode 100644 index 82077a1096b99..0000000000000 Binary files a/src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vegalite_image_512.png and /dev/null differ diff --git a/src/legacy/core_plugins/timelion/index.ts b/src/legacy/core_plugins/timelion/index.ts deleted file mode 100644 index 9c8ab156d1a79..0000000000000 --- a/src/legacy/core_plugins/timelion/index.ts +++ /dev/null @@ -1,189 +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 { resolve } from 'path'; -import { i18n } from '@kbn/i18n'; -import { Legacy } from 'kibana'; -import { LegacyPluginApi, LegacyPluginInitializer } from 'src/legacy/plugin_discovery/types'; -import { DEFAULT_APP_CATEGORIES } from '../../../core/server'; - -const experimentalLabel = i18n.translate('timelion.uiSettings.experimentalLabel', { - defaultMessage: 'experimental', -}); - -const timelionPluginInitializer: LegacyPluginInitializer = ({ Plugin }: LegacyPluginApi) => - new Plugin({ - require: ['kibana', 'elasticsearch'], - config(Joi: any) { - return Joi.object({ - enabled: Joi.boolean().default(true), - ui: Joi.object({ - enabled: Joi.boolean().default(false), - }).default(), - graphiteUrls: Joi.array() - .items(Joi.string().uri({ scheme: ['http', 'https'] })) - .default([]), - }).default(); - }, - // @ts-ignore - // https://github.com/elastic/kibana/pull/44039#discussion_r326582255 - uiCapabilities() { - return { - timelion: { - save: true, - }, - }; - }, - publicDir: resolve(__dirname, 'public'), - uiExports: { - app: { - title: 'Timelion', - order: 8000, - icon: 'plugins/timelion/icon.svg', - euiIconType: 'timelionApp', - main: 'plugins/timelion/app', - category: DEFAULT_APP_CATEGORIES.kibana, - }, - styleSheetPaths: resolve(__dirname, 'public/index.scss'), - hacks: [resolve(__dirname, 'public/legacy')], - uiSettingDefaults: { - 'timelion:showTutorial': { - name: i18n.translate('timelion.uiSettings.showTutorialLabel', { - defaultMessage: 'Show tutorial', - }), - value: false, - description: i18n.translate('timelion.uiSettings.showTutorialDescription', { - defaultMessage: 'Should I show the tutorial by default when entering the timelion app?', - }), - category: ['timelion'], - }, - 'timelion:es.timefield': { - name: i18n.translate('timelion.uiSettings.timeFieldLabel', { - defaultMessage: 'Time field', - }), - value: '@timestamp', - description: i18n.translate('timelion.uiSettings.timeFieldDescription', { - defaultMessage: 'Default field containing a timestamp when using {esParam}', - values: { esParam: '.es()' }, - }), - category: ['timelion'], - }, - 'timelion:es.default_index': { - name: i18n.translate('timelion.uiSettings.defaultIndexLabel', { - defaultMessage: 'Default index', - }), - value: '_all', - description: i18n.translate('timelion.uiSettings.defaultIndexDescription', { - defaultMessage: 'Default elasticsearch index to search with {esParam}', - values: { esParam: '.es()' }, - }), - category: ['timelion'], - }, - 'timelion:target_buckets': { - name: i18n.translate('timelion.uiSettings.targetBucketsLabel', { - defaultMessage: 'Target buckets', - }), - value: 200, - description: i18n.translate('timelion.uiSettings.targetBucketsDescription', { - defaultMessage: 'The number of buckets to shoot for when using auto intervals', - }), - category: ['timelion'], - }, - 'timelion:max_buckets': { - name: i18n.translate('timelion.uiSettings.maximumBucketsLabel', { - defaultMessage: 'Maximum buckets', - }), - value: 2000, - description: i18n.translate('timelion.uiSettings.maximumBucketsDescription', { - defaultMessage: 'The maximum number of buckets a single datasource can return', - }), - category: ['timelion'], - }, - 'timelion:default_columns': { - name: i18n.translate('timelion.uiSettings.defaultColumnsLabel', { - defaultMessage: 'Default columns', - }), - value: 2, - description: i18n.translate('timelion.uiSettings.defaultColumnsDescription', { - defaultMessage: 'Number of columns on a timelion sheet by default', - }), - category: ['timelion'], - }, - 'timelion:default_rows': { - name: i18n.translate('timelion.uiSettings.defaultRowsLabel', { - defaultMessage: 'Default rows', - }), - value: 2, - description: i18n.translate('timelion.uiSettings.defaultRowsDescription', { - defaultMessage: 'Number of rows on a timelion sheet by default', - }), - category: ['timelion'], - }, - 'timelion:min_interval': { - name: i18n.translate('timelion.uiSettings.minimumIntervalLabel', { - defaultMessage: 'Minimum interval', - }), - value: '1ms', - description: i18n.translate('timelion.uiSettings.minimumIntervalDescription', { - defaultMessage: 'The smallest interval that will be calculated when using "auto"', - description: - '"auto" is a technical value in that context, that should not be translated.', - }), - category: ['timelion'], - }, - 'timelion:graphite.url': { - name: i18n.translate('timelion.uiSettings.graphiteURLLabel', { - defaultMessage: 'Graphite URL', - description: - 'The URL should be in the form of https://www.hostedgraphite.com/UID/ACCESS_KEY/graphite', - }), - value: (server: Legacy.Server) => { - const urls = server.config().get('timelion.graphiteUrls') as string[]; - if (urls.length === 0) { - return null; - } else { - return urls[0]; - } - }, - description: i18n.translate('timelion.uiSettings.graphiteURLDescription', { - defaultMessage: - '{experimentalLabel} The URL of your graphite host', - values: { experimentalLabel: `[${experimentalLabel}]` }, - }), - type: 'select', - options: (server: Legacy.Server) => server.config().get('timelion.graphiteUrls'), - category: ['timelion'], - }, - 'timelion:quandl.key': { - name: i18n.translate('timelion.uiSettings.quandlKeyLabel', { - defaultMessage: 'Quandl key', - }), - value: 'someKeyHere', - description: i18n.translate('timelion.uiSettings.quandlKeyDescription', { - defaultMessage: '{experimentalLabel} Your API key from www.quandl.com', - values: { experimentalLabel: `[${experimentalLabel}]` }, - }), - category: ['timelion'], - }, - }, - }, - }); - -// eslint-disable-next-line import/no-default-export -export default timelionPluginInitializer; diff --git a/src/legacy/core_plugins/timelion/package.json b/src/legacy/core_plugins/timelion/package.json deleted file mode 100644 index 8b138e3b76d1a..0000000000000 --- a/src/legacy/core_plugins/timelion/package.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "author": "Rashid Khan ", - "name": "timelion", - "version": "kibana" -} diff --git a/src/legacy/core_plugins/timelion/public/app.js b/src/legacy/core_plugins/timelion/public/app.js deleted file mode 100644 index 602b221b7d14d..0000000000000 --- a/src/legacy/core_plugins/timelion/public/app.js +++ /dev/null @@ -1,517 +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 _ from 'lodash'; -// required for `ngSanitize` angular module -import 'angular-sanitize'; - -import { i18n } from '@kbn/i18n'; - -import routes from 'ui/routes'; -import { capabilities } from 'ui/capabilities'; -import { docTitle } from 'ui/doc_title'; -import { fatalError, toastNotifications } from 'ui/notify'; -import { timefilter } from 'ui/timefilter'; -import { npStart } from 'ui/new_platform'; -import { getSavedSheetBreadcrumbs, getCreateBreadcrumbs } from './breadcrumbs'; -import { getTimezone } from '../../../../plugins/vis_type_timelion/public'; - -import 'uiExports/savedObjectTypes'; - -require('ui/i18n'); -require('ui/autoload/all'); - -// TODO: remove ui imports completely (move to plugins) -import 'ui/directives/input_focus'; -import './directives/saved_object_finder'; -import 'ui/directives/listen'; -import './directives/saved_object_save_as_checkbox'; -import './services/saved_sheet_register'; - -import rootTemplate from 'plugins/timelion/index.html'; - -import { loadKbnTopNavDirectives } from '../../../../plugins/kibana_legacy/public'; -loadKbnTopNavDirectives(npStart.plugins.navigation.ui); - -require('plugins/timelion/directives/cells/cells'); -require('plugins/timelion/directives/fixed_element'); -require('plugins/timelion/directives/fullscreen/fullscreen'); -require('plugins/timelion/directives/timelion_expression_input'); -require('plugins/timelion/directives/timelion_help/timelion_help'); -require('plugins/timelion/directives/timelion_interval/timelion_interval'); -require('plugins/timelion/directives/timelion_save_sheet'); -require('plugins/timelion/directives/timelion_load_sheet'); -require('plugins/timelion/directives/timelion_options_sheet'); - -document.title = 'Timelion - Kibana'; - -const app = require('ui/modules').get('apps/timelion', ['i18n', 'ngSanitize']); - -routes.enable(); - -routes.when('/:id?', { - template: rootTemplate, - reloadOnSearch: false, - k7Breadcrumbs: ($injector, $route) => - $injector.invoke($route.current.params.id ? getSavedSheetBreadcrumbs : getCreateBreadcrumbs), - badge: (uiCapabilities) => { - if (uiCapabilities.timelion.save) { - return undefined; - } - - return { - text: i18n.translate('timelion.badge.readOnly.text', { - defaultMessage: 'Read only', - }), - tooltip: i18n.translate('timelion.badge.readOnly.tooltip', { - defaultMessage: 'Unable to save Timelion sheets', - }), - iconType: 'glasses', - }; - }, - resolve: { - savedSheet: function (redirectWhenMissing, savedSheets, $route) { - return savedSheets - .get($route.current.params.id) - .then((savedSheet) => { - if ($route.current.params.id) { - npStart.core.chrome.recentlyAccessed.add( - savedSheet.getFullPath(), - savedSheet.title, - savedSheet.id - ); - } - return savedSheet; - }) - .catch( - redirectWhenMissing({ - search: '/', - }) - ); - }, - }, -}); - -const location = 'Timelion'; - -app.controller('timelion', function ( - $http, - $route, - $routeParams, - $scope, - $timeout, - AppState, - config, - kbnUrl -) { - // Keeping this at app scope allows us to keep the current page when the user - // switches to say, the timepicker. - $scope.page = config.get('timelion:showTutorial', true) ? 1 : 0; - $scope.setPage = (page) => ($scope.page = page); - - timefilter.enableAutoRefreshSelector(); - timefilter.enableTimeRangeSelector(); - - const savedVisualizations = npStart.plugins.visualizations.savedVisualizationsLoader; - const timezone = getTimezone(config); - - const defaultExpression = '.es(*)'; - const savedSheet = $route.current.locals.savedSheet; - - $scope.topNavMenu = getTopNavMenu(); - - $timeout(function () { - if (config.get('timelion:showTutorial', true)) { - $scope.toggleMenu('showHelp'); - } - }, 0); - - $scope.transient = {}; - $scope.state = new AppState(getStateDefaults()); - function getStateDefaults() { - return { - sheet: savedSheet.timelion_sheet, - selected: 0, - columns: savedSheet.timelion_columns, - rows: savedSheet.timelion_rows, - interval: savedSheet.timelion_interval, - }; - } - - function getTopNavMenu() { - const newSheetAction = { - id: 'new', - label: i18n.translate('timelion.topNavMenu.newSheetButtonLabel', { - defaultMessage: 'New', - }), - description: i18n.translate('timelion.topNavMenu.newSheetButtonAriaLabel', { - defaultMessage: 'New Sheet', - }), - run: function () { - kbnUrl.change('/'); - }, - testId: 'timelionNewButton', - }; - - const addSheetAction = { - id: 'add', - label: i18n.translate('timelion.topNavMenu.addChartButtonLabel', { - defaultMessage: 'Add', - }), - description: i18n.translate('timelion.topNavMenu.addChartButtonAriaLabel', { - defaultMessage: 'Add a chart', - }), - run: function () { - $scope.$evalAsync(() => $scope.newCell()); - }, - testId: 'timelionAddChartButton', - }; - - const saveSheetAction = { - id: 'save', - label: i18n.translate('timelion.topNavMenu.saveSheetButtonLabel', { - defaultMessage: 'Save', - }), - description: i18n.translate('timelion.topNavMenu.saveSheetButtonAriaLabel', { - defaultMessage: 'Save Sheet', - }), - run: () => { - $scope.$evalAsync(() => $scope.toggleMenu('showSave')); - }, - testId: 'timelionSaveButton', - }; - - const deleteSheetAction = { - id: 'delete', - label: i18n.translate('timelion.topNavMenu.deleteSheetButtonLabel', { - defaultMessage: 'Delete', - }), - description: i18n.translate('timelion.topNavMenu.deleteSheetButtonAriaLabel', { - defaultMessage: 'Delete current sheet', - }), - disableButton: function () { - return !savedSheet.id; - }, - run: function () { - const title = savedSheet.title; - function doDelete() { - savedSheet - .delete() - .then(() => { - toastNotifications.addSuccess( - i18n.translate('timelion.topNavMenu.delete.modal.successNotificationText', { - defaultMessage: `Deleted '{title}'`, - values: { title }, - }) - ); - kbnUrl.change('/'); - }) - .catch((error) => fatalError(error, location)); - } - - const confirmModalOptions = { - confirmButtonText: i18n.translate('timelion.topNavMenu.delete.modal.confirmButtonLabel', { - defaultMessage: 'Delete', - }), - title: i18n.translate('timelion.topNavMenu.delete.modalTitle', { - defaultMessage: `Delete Timelion sheet '{title}'?`, - values: { title }, - }), - }; - - $scope.$evalAsync(() => { - npStart.core.overlays - .openConfirm( - i18n.translate('timelion.topNavMenu.delete.modal.warningText', { - defaultMessage: `You can't recover deleted sheets.`, - }), - confirmModalOptions - ) - .then((isConfirmed) => { - if (isConfirmed) { - doDelete(); - } - }); - }); - }, - testId: 'timelionDeleteButton', - }; - - const openSheetAction = { - id: 'open', - label: i18n.translate('timelion.topNavMenu.openSheetButtonLabel', { - defaultMessage: 'Open', - }), - description: i18n.translate('timelion.topNavMenu.openSheetButtonAriaLabel', { - defaultMessage: 'Open Sheet', - }), - run: () => { - $scope.$evalAsync(() => $scope.toggleMenu('showLoad')); - }, - testId: 'timelionOpenButton', - }; - - const optionsAction = { - id: 'options', - label: i18n.translate('timelion.topNavMenu.optionsButtonLabel', { - defaultMessage: 'Options', - }), - description: i18n.translate('timelion.topNavMenu.optionsButtonAriaLabel', { - defaultMessage: 'Options', - }), - run: () => { - $scope.$evalAsync(() => $scope.toggleMenu('showOptions')); - }, - testId: 'timelionOptionsButton', - }; - - const helpAction = { - id: 'help', - label: i18n.translate('timelion.topNavMenu.helpButtonLabel', { - defaultMessage: 'Help', - }), - description: i18n.translate('timelion.topNavMenu.helpButtonAriaLabel', { - defaultMessage: 'Help', - }), - run: () => { - $scope.$evalAsync(() => $scope.toggleMenu('showHelp')); - }, - testId: 'timelionDocsButton', - }; - - if (capabilities.get().timelion.save) { - return [ - newSheetAction, - addSheetAction, - saveSheetAction, - deleteSheetAction, - openSheetAction, - optionsAction, - helpAction, - ]; - } - return [newSheetAction, addSheetAction, openSheetAction, optionsAction, helpAction]; - } - - let refresher; - const setRefreshData = function () { - if (refresher) $timeout.cancel(refresher); - const interval = timefilter.getRefreshInterval(); - if (interval.value > 0 && !interval.pause) { - function startRefresh() { - refresher = $timeout(function () { - if (!$scope.running) $scope.search(); - startRefresh(); - }, interval.value); - } - startRefresh(); - } - }; - - const init = function () { - $scope.running = false; - $scope.search(); - setRefreshData(); - - $scope.model = { - timeRange: timefilter.getTime(), - refreshInterval: timefilter.getRefreshInterval(), - }; - - $scope.$listen($scope.state, 'fetch_with_changes', $scope.search); - timefilter.getFetch$().subscribe($scope.search); - - $scope.opts = { - saveExpression: saveExpression, - saveSheet: saveSheet, - savedSheet: savedSheet, - state: $scope.state, - search: $scope.search, - dontShowHelp: function () { - config.set('timelion:showTutorial', false); - $scope.setPage(0); - $scope.closeMenus(); - }, - }; - - $scope.menus = { - showHelp: false, - showSave: false, - showLoad: false, - showOptions: false, - }; - - $scope.toggleMenu = (menuName) => { - const curState = $scope.menus[menuName]; - $scope.closeMenus(); - $scope.menus[menuName] = !curState; - }; - - $scope.closeMenus = () => { - _.forOwn($scope.menus, function (value, key) { - $scope.menus[key] = false; - }); - }; - }; - - $scope.onTimeUpdate = function ({ dateRange }) { - $scope.model.timeRange = { - ...dateRange, - }; - timefilter.setTime(dateRange); - }; - - $scope.onRefreshChange = function ({ isPaused, refreshInterval }) { - $scope.model.refreshInterval = { - pause: isPaused, - value: refreshInterval, - }; - timefilter.setRefreshInterval({ - pause: isPaused, - value: refreshInterval ? refreshInterval : $scope.refreshInterval.value, - }); - - setRefreshData(); - }; - - $scope.$watch( - function () { - return savedSheet.lastSavedTitle; - }, - function (newTitle) { - docTitle.change(savedSheet.id ? newTitle : undefined); - } - ); - - $scope.toggle = function (property) { - $scope[property] = !$scope[property]; - }; - - $scope.newSheet = function () { - kbnUrl.change('/', {}); - }; - - $scope.newCell = function () { - $scope.state.sheet.push(defaultExpression); - $scope.state.selected = $scope.state.sheet.length - 1; - $scope.safeSearch(); - }; - - $scope.setActiveCell = function (cell) { - $scope.state.selected = cell; - }; - - $scope.search = function () { - $scope.state.save(); - $scope.running = true; - - // parse the time range client side to make sure it behaves like other charts - const timeRangeBounds = timefilter.getBounds(); - - const httpResult = $http - .post('../api/timelion/run', { - sheet: $scope.state.sheet, - time: _.assignIn( - { - from: timeRangeBounds.min, - to: timeRangeBounds.max, - }, - { - interval: $scope.state.interval, - timezone: timezone, - } - ), - }) - .then((resp) => resp.data) - .catch((resp) => { - throw resp.data; - }); - - httpResult - .then(function (resp) { - $scope.stats = resp.stats; - $scope.sheet = resp.sheet; - _.each(resp.sheet, function (cell) { - if (cell.exception) { - $scope.state.selected = cell.plot; - } - }); - $scope.running = false; - }) - .catch(function (resp) { - $scope.sheet = []; - $scope.running = false; - - const err = new Error(resp.message); - err.stack = resp.stack; - toastNotifications.addError(err, { - title: i18n.translate('timelion.searchErrorTitle', { - defaultMessage: 'Timelion request error', - }), - }); - }); - }; - - $scope.safeSearch = _.debounce($scope.search, 500); - - function saveSheet() { - savedSheet.timelion_sheet = $scope.state.sheet; - savedSheet.timelion_interval = $scope.state.interval; - savedSheet.timelion_columns = $scope.state.columns; - savedSheet.timelion_rows = $scope.state.rows; - savedSheet.save().then(function (id) { - if (id) { - toastNotifications.addSuccess({ - title: i18n.translate('timelion.saveSheet.successNotificationText', { - defaultMessage: `Saved sheet '{title}'`, - values: { title: savedSheet.title }, - }), - 'data-test-subj': 'timelionSaveSuccessToast', - }); - - if (savedSheet.id !== $routeParams.id) { - kbnUrl.change('/{{id}}', { id: savedSheet.id }); - } - } - }); - } - - function saveExpression(title) { - savedVisualizations.get({ type: 'timelion' }).then(function (savedExpression) { - savedExpression.visState.params = { - expression: $scope.state.sheet[$scope.state.selected], - interval: $scope.state.interval, - }; - savedExpression.title = title; - savedExpression.visState.title = title; - savedExpression.save().then(function (id) { - if (id) { - toastNotifications.addSuccess( - i18n.translate('timelion.saveExpression.successNotificationText', { - defaultMessage: `Saved expression '{title}'`, - values: { title: savedExpression.title }, - }) - ); - } - }); - }); - } - - init(); -}); diff --git a/src/legacy/core_plugins/timelion/public/directives/cells/cells.js b/src/legacy/core_plugins/timelion/public/directives/cells/cells.js deleted file mode 100644 index 104af3b1043d6..0000000000000 --- a/src/legacy/core_plugins/timelion/public/directives/cells/cells.js +++ /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 _ from 'lodash'; -import { move } from 'ui/utils/collection'; - -require('angular-sortable-view'); -require('plugins/timelion/directives/chart/chart'); -require('plugins/timelion/directives/timelion_grid'); - -const app = require('ui/modules').get('apps/timelion', ['angular-sortable-view']); -import html from './cells.html'; - -app.directive('timelionCells', function () { - return { - restrict: 'E', - scope: { - sheet: '=', - state: '=', - transient: '=', - onSearch: '=', - onSelect: '=', - }, - template: html, - link: function ($scope) { - $scope.removeCell = function (index) { - _.pullAt($scope.state.sheet, index); - $scope.onSearch(); - }; - - $scope.dropCell = function (item, partFrom, partTo, indexFrom, indexTo) { - $scope.onSelect(indexTo); - move($scope.sheet, indexFrom, indexTo); - }; - }, - }; -}); diff --git a/src/legacy/core_plugins/timelion/public/directives/fixed_element.js b/src/legacy/core_plugins/timelion/public/directives/fixed_element.js deleted file mode 100644 index e3a8b2184bb20..0000000000000 --- a/src/legacy/core_plugins/timelion/public/directives/fixed_element.js +++ /dev/null @@ -1,49 +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 $ from 'jquery'; - -const app = require('ui/modules').get('apps/timelion', []); -app.directive('fixedElementRoot', function () { - return { - restrict: 'A', - link: function ($elem) { - let fixedAt; - $(window).bind('scroll', function () { - const fixed = $('[fixed-element]', $elem); - const body = $('[fixed-element-body]', $elem); - const top = fixed.offset().top; - - if ($(window).scrollTop() > top) { - // This is a gross hack, but its better than it was. I guess - fixedAt = $(window).scrollTop(); - fixed.addClass(fixed.attr('fixed-element')); - body.addClass(fixed.attr('fixed-element-body')); - body.css({ top: fixed.height() }); - } - - if ($(window).scrollTop() < fixedAt) { - fixed.removeClass(fixed.attr('fixed-element')); - body.removeClass(fixed.attr('fixed-element-body')); - body.removeAttr('style'); - } - }); - }, - }; -}); diff --git a/src/legacy/core_plugins/timelion/public/directives/saved_object_finder.js b/src/legacy/core_plugins/timelion/public/directives/saved_object_finder.js deleted file mode 100644 index ae042310fd464..0000000000000 --- a/src/legacy/core_plugins/timelion/public/directives/saved_object_finder.js +++ /dev/null @@ -1,315 +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 _ from 'lodash'; -import rison from 'rison-node'; -import { uiModules } from 'ui/modules'; -import 'ui/directives/input_focus'; -import savedObjectFinderTemplate from './saved_object_finder.html'; -import { savedSheetLoader } from '../services/saved_sheets'; -import { keyMap } from 'ui/directives/key_map'; -import { - PaginateControlsDirectiveProvider, - PaginateDirectiveProvider, -} from '../../../../../plugins/kibana_legacy/public'; -import { PER_PAGE_SETTING } from '../../../../../plugins/saved_objects/common'; -import { VISUALIZE_ENABLE_LABS_SETTING } from '../../../../../plugins/visualizations/public'; - -const module = uiModules.get('kibana'); - -module - .directive('paginate', PaginateDirectiveProvider) - .directive('paginateControls', PaginateControlsDirectiveProvider) - .directive('savedObjectFinder', function ($location, kbnUrl, Private, config) { - return { - restrict: 'E', - scope: { - type: '@', - // optional make-url attr, sets the userMakeUrl in our scope - userMakeUrl: '=?makeUrl', - // optional on-choose attr, sets the userOnChoose in our scope - userOnChoose: '=?onChoose', - // optional useLocalManagement attr, removes link to management section - useLocalManagement: '=?useLocalManagement', - /** - * @type {function} - an optional function. If supplied an `Add new X` button is shown - * and this function is called when clicked. - */ - onAddNew: '=', - /** - * @{type} boolean - set this to true, if you don't want the search box above the - * table to automatically gain focus once loaded - */ - disableAutoFocus: '=', - }, - template: savedObjectFinderTemplate, - controllerAs: 'finder', - controller: function ($scope, $element) { - const self = this; - - // the text input element - const $input = $element.find('input[ng-model=filter]'); - - // The number of items to show in the list - $scope.perPage = config.get(PER_PAGE_SETTING); - - // the list that will hold the suggestions - const $list = $element.find('ul'); - - // the current filter string, used to check that returned results are still useful - let currentFilter = $scope.filter; - - // the most recently entered search/filter - let prevSearch; - - // the list of hits, used to render display - self.hits = []; - - self.service = savedSheetLoader; - self.properties = self.service.loaderProperties; - - filterResults(); - - /** - * Boolean that keeps track of whether hits are sorted ascending (true) - * or descending (false) by title - * @type {Boolean} - */ - self.isAscending = true; - - /** - * Sorts saved object finder hits either ascending or descending - * @param {Array} hits Array of saved finder object hits - * @return {Array} Array sorted either ascending or descending - */ - self.sortHits = function (hits) { - self.isAscending = !self.isAscending; - self.hits = self.isAscending - ? _.sortBy(hits, 'title') - : _.sortBy(hits, 'title').reverse(); - }; - - /** - * Passed the hit objects and will determine if the - * hit should have a url in the UI, returns it if so - * @return {string|null} - the url or nothing - */ - self.makeUrl = function (hit) { - if ($scope.userMakeUrl) { - return $scope.userMakeUrl(hit); - } - - if (!$scope.userOnChoose) { - return hit.url; - } - - return '#'; - }; - - self.preventClick = function ($event) { - $event.preventDefault(); - }; - - /** - * Called when a hit object is clicked, can override the - * url behavior if necessary. - */ - self.onChoose = function (hit, $event) { - if ($scope.userOnChoose) { - $scope.userOnChoose(hit, $event); - } - - const url = self.makeUrl(hit); - if (!url || url === '#' || url.charAt(0) !== '#') return; - - $event.preventDefault(); - - // we want the '/path', not '#/path' - kbnUrl.change(url.substr(1)); - }; - - $scope.$watch('filter', function (newFilter) { - // ensure that the currentFilter changes from undefined to '' - // which triggers - currentFilter = newFilter || ''; - filterResults(); - }); - - $scope.pageFirstItem = 0; - $scope.pageLastItem = 0; - $scope.onPageChanged = (page) => { - $scope.pageFirstItem = page.firstItem; - $scope.pageLastItem = page.lastItem; - }; - - //manages the state of the keyboard selector - self.selector = { - enabled: false, - index: -1, - }; - - self.getLabel = function () { - return _.words(self.properties.nouns).map(_.upperFirst).join(' '); - }; - - //key handler for the filter text box - self.filterKeyDown = function ($event) { - switch (keyMap[$event.keyCode]) { - case 'enter': - if (self.hitCount !== 1) return; - - const hit = self.hits[0]; - if (!hit) return; - - self.onChoose(hit, $event); - $event.preventDefault(); - break; - } - }; - - //key handler for the list items - self.hitKeyDown = function ($event, page, paginate) { - switch (keyMap[$event.keyCode]) { - case 'tab': - if (!self.selector.enabled) break; - - self.selector.index = -1; - self.selector.enabled = false; - - //if the user types shift-tab return to the textbox - //if the user types tab, set the focus to the currently selected hit. - if ($event.shiftKey) { - $input.focus(); - } else { - $list.find('li.active a').focus(); - } - - $event.preventDefault(); - break; - case 'down': - if (!self.selector.enabled) break; - - if (self.selector.index + 1 < page.length) { - self.selector.index += 1; - } - $event.preventDefault(); - break; - case 'up': - if (!self.selector.enabled) break; - - if (self.selector.index > 0) { - self.selector.index -= 1; - } - $event.preventDefault(); - break; - case 'right': - if (!self.selector.enabled) break; - - if (page.number < page.count) { - paginate.goToPage(page.number + 1); - self.selector.index = 0; - selectTopHit(); - } - $event.preventDefault(); - break; - case 'left': - if (!self.selector.enabled) break; - - if (page.number > 1) { - paginate.goToPage(page.number - 1); - self.selector.index = 0; - selectTopHit(); - } - $event.preventDefault(); - break; - case 'escape': - if (!self.selector.enabled) break; - - $input.focus(); - $event.preventDefault(); - break; - case 'enter': - if (!self.selector.enabled) break; - - const hitIndex = (page.number - 1) * paginate.perPage + self.selector.index; - const hit = self.hits[hitIndex]; - if (!hit) break; - - self.onChoose(hit, $event); - $event.preventDefault(); - break; - case 'shift': - break; - default: - $input.focus(); - break; - } - }; - - self.hitBlur = function () { - self.selector.index = -1; - self.selector.enabled = false; - }; - - self.manageObjects = function (type) { - $location.url('/management/kibana/objects?_a=' + rison.encode({ tab: type })); - }; - - self.hitCountNoun = function () { - return (self.hitCount === 1 ? self.properties.noun : self.properties.nouns).toLowerCase(); - }; - - function selectTopHit() { - setTimeout(function () { - //triggering a focus event kicks off a new angular digest cycle. - $list.find('a:first').focus(); - }, 0); - } - - function filterResults() { - if (!self.service) return; - if (!self.properties) return; - - // track the filter that we use for this search, - // but ensure that we don't search for the same - // thing twice. This is called from multiple places - // and needs to be smart about when it actually searches - const filter = currentFilter; - if (prevSearch === filter) return; - - prevSearch = filter; - - const isLabsEnabled = config.get(VISUALIZE_ENABLE_LABS_SETTING); - self.service.find(filter).then(function (hits) { - hits.hits = hits.hits.filter( - (hit) => isLabsEnabled || _.get(hit, 'type.stage') !== 'experimental' - ); - hits.total = hits.hits.length; - - // ensure that we don't display old results - // as we can't really cancel requests - if (currentFilter === filter) { - self.hitCount = hits.total; - self.hits = _.sortBy(hits.hits, 'title'); - } - }); - } - }, - }; - }); diff --git a/src/legacy/core_plugins/timelion/public/directives/timelion_expression_input.js b/src/legacy/core_plugins/timelion/public/directives/timelion_expression_input.js deleted file mode 100644 index 8b4c28a50b732..0000000000000 --- a/src/legacy/core_plugins/timelion/public/directives/timelion_expression_input.js +++ /dev/null @@ -1,281 +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. - */ - -/** - * Timelion Expression Autocompleter - * - * This directive allows users to enter multiline timelion expressions. If the user has entered - * a valid expression and then types a ".", this directive will display a list of suggestions. - * - * Users can navigate suggestions using the arrow keys. When a user selects a suggestion, it's - * inserted into the expression and the caret position is updated to be inside of the newly- - * added function's parentheses. - * - * Beneath the hood, we use a PEG grammar to validate the Timelion expression and detect if - * the caret is in a position within the expression that allows functions to be suggested. - * - * NOTE: This directive doesn't work well with contenteditable divs. Challenges include: - * - You have to replace markup with newline characters and spaces when passing the expression - * to the grammar. - * - You have to do the opposite when loading a saved expression, so that it appears correctly - * within the contenteditable (i.e. replace newlines with
markup). - * - The Range and Selection APIs ignore newlines when providing caret position, so there is - * literally no way to insert suggestions into the correct place in a multiline expression - * that has more than a single consecutive newline. - */ - -import _ from 'lodash'; -import $ from 'jquery'; -import PEG from 'pegjs'; -import grammar from 'raw-loader!../../../../../plugins/vis_type_timelion/common/chain.peg'; -import timelionExpressionInputTemplate from './timelion_expression_input.html'; -import { - SUGGESTION_TYPE, - Suggestions, - suggest, - insertAtLocation, -} from './timelion_expression_input_helpers'; -import { comboBoxKeys } from '@elastic/eui'; -import { npStart } from 'ui/new_platform'; - -const Parser = PEG.generate(grammar); - -export function TimelionExpInput($http, $timeout) { - return { - restrict: 'E', - scope: { - rows: '=', - sheet: '=', - updateChart: '&', - shouldPopoverSuggestions: '@', - }, - replace: true, - template: timelionExpressionInputTemplate, - link: function (scope, elem) { - const argValueSuggestions = npStart.plugins.visTypeTimelion.getArgValueSuggestions(); - const expressionInput = elem.find('[data-expression-input]'); - const functionReference = {}; - let suggestibleFunctionLocation = {}; - - scope.suggestions = new Suggestions(); - - function init() { - $http.get('../api/timelion/functions').then(function (resp) { - Object.assign(functionReference, { - byName: _.keyBy(resp.data, 'name'), - list: resp.data, - }); - }); - } - - function setCaretOffset(caretOffset) { - // Wait for Angular to update the input with the new expression and *then* we can set - // the caret position. - $timeout(() => { - expressionInput.focus(); - expressionInput[0].selectionStart = expressionInput[0].selectionEnd = caretOffset; - scope.$apply(); - }, 0); - } - - function insertSuggestionIntoExpression(suggestionIndex) { - if (scope.suggestions.isEmpty()) { - return; - } - - const { min, max } = suggestibleFunctionLocation; - let insertedValue; - let insertPositionMinOffset = 0; - - switch (scope.suggestions.type) { - case SUGGESTION_TYPE.FUNCTIONS: { - // Position the caret inside of the function parentheses. - insertedValue = `${scope.suggestions.list[suggestionIndex].name}()`; - - // min advanced one to not replace function '.' - insertPositionMinOffset = 1; - break; - } - case SUGGESTION_TYPE.ARGUMENTS: { - // Position the caret after the '=' - insertedValue = `${scope.suggestions.list[suggestionIndex].name}=`; - break; - } - case SUGGESTION_TYPE.ARGUMENT_VALUE: { - // Position the caret after the argument value - insertedValue = `${scope.suggestions.list[suggestionIndex].name}`; - break; - } - } - - const updatedExpression = insertAtLocation( - insertedValue, - scope.sheet, - min + insertPositionMinOffset, - max - ); - scope.sheet = updatedExpression; - - const newCaretOffset = min + insertedValue.length; - setCaretOffset(newCaretOffset); - } - - function scrollToSuggestionAt(index) { - // We don't cache these because the list changes based on user input. - const suggestionsList = $('[data-suggestions-list]'); - const suggestionListItem = $('[data-suggestion-list-item]')[index]; - // Scroll to the position of the item relative to the list, not to the window. - suggestionsList.scrollTop(suggestionListItem.offsetTop - suggestionsList[0].offsetTop); - } - - function getCursorPosition() { - if (expressionInput.length) { - return expressionInput[0].selectionStart; - } - return null; - } - - async function getSuggestions() { - const suggestions = await suggest( - scope.sheet, - functionReference.list, - Parser, - getCursorPosition(), - argValueSuggestions - ); - - // We're using ES6 Promises, not $q, so we have to wrap this in $apply. - scope.$apply(() => { - if (suggestions) { - scope.suggestions.setList(suggestions.list, suggestions.type); - scope.suggestions.show(); - suggestibleFunctionLocation = suggestions.location; - $timeout(() => { - const suggestionsList = $('[data-suggestions-list]'); - suggestionsList.scrollTop(0); - }, 0); - return; - } - - suggestibleFunctionLocation = undefined; - scope.suggestions.reset(); - }); - } - - function isNavigationalKey(key) { - const keyCodes = _.values(comboBoxKeys); - return keyCodes.includes(key); - } - - scope.onFocusInput = () => { - // Wait for the caret position of the input to update and then we can get suggestions - // (which depends on the caret position). - $timeout(getSuggestions, 0); - }; - - scope.onBlurInput = () => { - scope.suggestions.hide(); - }; - - scope.onKeyDownInput = (e) => { - // If we've pressed any non-navigational keys, then the user has typed something and we - // can exit early without doing any navigation. The keyup handler will pull up suggestions. - if (!isNavigationalKey(e.key)) { - return; - } - - switch (e.keyCode) { - case comboBoxKeys.ARROW_UP: - if (scope.suggestions.isVisible) { - // Up and down keys navigate through suggestions. - e.preventDefault(); - scope.suggestions.stepForward(); - scrollToSuggestionAt(scope.suggestions.index); - } - break; - - case comboBoxKeys.ARROW_DOWN: - if (scope.suggestions.isVisible) { - // Up and down keys navigate through suggestions. - e.preventDefault(); - scope.suggestions.stepBackward(); - scrollToSuggestionAt(scope.suggestions.index); - } - break; - - case comboBoxKeys.TAB: - // If there are no suggestions or none is selected, the user tabs to the next input. - if (scope.suggestions.isEmpty() || scope.suggestions.index < 0) { - // Before letting the tab be handled to focus the next element - // we need to hide the suggestions, otherwise it will focus these - // instead of the time interval select. - scope.suggestions.hide(); - return; - } - - // If we have suggestions, complete the selected one. - e.preventDefault(); - insertSuggestionIntoExpression(scope.suggestions.index); - break; - - case comboBoxKeys.ENTER: - if (e.metaKey || e.ctrlKey) { - // Re-render the chart when the user hits CMD+ENTER. - e.preventDefault(); - scope.updateChart(); - } else if (!scope.suggestions.isEmpty()) { - // If the suggestions are open, complete the expression with the suggestion. - e.preventDefault(); - insertSuggestionIntoExpression(scope.suggestions.index); - } - break; - - case comboBoxKeys.ESCAPE: - e.preventDefault(); - scope.suggestions.hide(); - break; - } - }; - - scope.onKeyUpInput = (e) => { - // If the user isn't navigating, then we should update the suggestions based on their input. - if (!isNavigationalKey(e.key)) { - getSuggestions(); - } - }; - - scope.onClickExpression = () => { - getSuggestions(); - }; - - scope.onClickSuggestion = (index) => { - insertSuggestionIntoExpression(index); - }; - - scope.getActiveSuggestionId = () => { - if (scope.suggestions.isVisible && scope.suggestions.index > -1) { - return `timelionSuggestion${scope.suggestions.index}`; - } - return ''; - }; - - init(); - }, - }; -} diff --git a/src/legacy/core_plugins/timelion/public/directives/timelion_grid.js b/src/legacy/core_plugins/timelion/public/directives/timelion_grid.js deleted file mode 100644 index 256c35331d016..0000000000000 --- a/src/legacy/core_plugins/timelion/public/directives/timelion_grid.js +++ /dev/null @@ -1,67 +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 $ from 'jquery'; - -const app = require('ui/modules').get('apps/timelion', []); -app.directive('timelionGrid', function () { - return { - restrict: 'A', - scope: { - timelionGridRows: '=', - timelionGridColumns: '=', - }, - link: function ($scope, $elem) { - function init() { - setDimensions(); - } - - $scope.$on('$destroy', function () { - $(window).off('resize'); //remove the handler added earlier - }); - - $(window).resize(function () { - setDimensions(); - }); - - $scope.$watchMulti(['timelionGridColumns', 'timelionGridRows'], function () { - setDimensions(); - }); - - function setDimensions() { - const borderSize = 2; - const headerSize = 45 + 35 + 28 + 20 * 2; // chrome + subnav + buttons + (container padding) - const verticalPadding = 10; - - if ($scope.timelionGridColumns != null) { - $elem.width($elem.parent().width() / $scope.timelionGridColumns - borderSize * 2); - } - - if ($scope.timelionGridRows != null) { - $elem.height( - ($(window).height() - headerSize) / $scope.timelionGridRows - - (verticalPadding + borderSize * 2) - ); - } - } - - init(); - }, - }; -}); diff --git a/src/legacy/core_plugins/timelion/public/directives/timelion_help/timelion_help.js b/src/legacy/core_plugins/timelion/public/directives/timelion_help/timelion_help.js deleted file mode 100644 index 25f3df13153ba..0000000000000 --- a/src/legacy/core_plugins/timelion/public/directives/timelion_help/timelion_help.js +++ /dev/null @@ -1,168 +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 template from './timelion_help.html'; -import { i18n } from '@kbn/i18n'; -import { uiModules } from 'ui/modules'; -import _ from 'lodash'; -import moment from 'moment'; -import '../../components/timelionhelp_tabs_directive'; - -const app = uiModules.get('apps/timelion', []); - -app.directive('timelionHelp', function ($http) { - return { - restrict: 'E', - template, - controller: function ($scope) { - $scope.functions = { - list: [], - details: null, - }; - - $scope.activeTab = 'funcref'; - $scope.activateTab = function (tabName) { - $scope.activeTab = tabName; - }; - - function init() { - $scope.es = { - invalidCount: 0, - }; - - $scope.translations = { - nextButtonLabel: i18n.translate('timelion.help.nextPageButtonLabel', { - defaultMessage: 'Next', - }), - previousButtonLabel: i18n.translate('timelion.help.previousPageButtonLabel', { - defaultMessage: 'Previous', - }), - dontShowHelpButtonLabel: i18n.translate('timelion.help.dontShowHelpButtonLabel', { - defaultMessage: `Don't show this again`, - }), - strongNextText: i18n.translate('timelion.help.welcome.content.strongNextText', { - defaultMessage: 'Next', - }), - emphasizedEverythingText: i18n.translate( - 'timelion.help.welcome.content.emphasizedEverythingText', - { - defaultMessage: 'everything', - } - ), - notValidAdvancedSettingsPath: i18n.translate( - 'timelion.help.configuration.notValid.advancedSettingsPathText', - { - defaultMessage: 'Management / Kibana / Advanced Settings', - } - ), - validAdvancedSettingsPath: i18n.translate( - 'timelion.help.configuration.valid.advancedSettingsPathText', - { - defaultMessage: 'Management/Kibana/Advanced Settings', - } - ), - esAsteriskQueryDescription: i18n.translate( - 'timelion.help.querying.esAsteriskQueryDescriptionText', - { - defaultMessage: 'hey Elasticsearch, find everything in my default index', - } - ), - esIndexQueryDescription: i18n.translate( - 'timelion.help.querying.esIndexQueryDescriptionText', - { - defaultMessage: 'use * as the q (query) for the logstash-* index', - } - ), - strongAddText: i18n.translate('timelion.help.expressions.strongAddText', { - defaultMessage: 'Add', - }), - twoExpressionsDescriptionTitle: i18n.translate( - 'timelion.help.expressions.examples.twoExpressionsDescriptionTitle', - { - defaultMessage: 'Double the fun.', - } - ), - customStylingDescriptionTitle: i18n.translate( - 'timelion.help.expressions.examples.customStylingDescriptionTitle', - { - defaultMessage: 'Custom styling.', - } - ), - namedArgumentsDescriptionTitle: i18n.translate( - 'timelion.help.expressions.examples.namedArgumentsDescriptionTitle', - { - defaultMessage: 'Named arguments.', - } - ), - groupedExpressionsDescriptionTitle: i18n.translate( - 'timelion.help.expressions.examples.groupedExpressionsDescriptionTitle', - { - defaultMessage: 'Grouped expressions.', - } - ), - }; - - getFunctions(); - checkElasticsearch(); - } - - function getFunctions() { - return $http.get('../api/timelion/functions').then(function (resp) { - $scope.functions.list = resp.data; - }); - } - $scope.recheckElasticsearch = function () { - $scope.es.valid = null; - checkElasticsearch().then(function (valid) { - if (!valid) $scope.es.invalidCount++; - }); - }; - - function checkElasticsearch() { - return $http.get('../api/timelion/validate/es').then(function (resp) { - if (resp.data.ok) { - $scope.es.valid = true; - $scope.es.stats = { - min: moment(resp.data.min).format('LLL'), - max: moment(resp.data.max).format('LLL'), - field: resp.data.field, - }; - } else { - $scope.es.valid = false; - $scope.es.invalidReason = (function () { - try { - const esResp = JSON.parse(resp.data.resp.response); - return _.get(esResp, 'error.root_cause[0].reason'); - } catch (e) { - if (_.get(resp, 'data.resp.message')) return _.get(resp, 'data.resp.message'); - if (_.get(resp, 'data.resp.output.payload.message')) - return _.get(resp, 'data.resp.output.payload.message'); - return i18n.translate('timelion.help.unknownErrorMessage', { - defaultMessage: 'Unknown error', - }); - } - })(); - } - return $scope.es.valid; - }); - } - init(); - }, - }; -}); diff --git a/src/legacy/core_plugins/timelion/public/header.svg b/src/legacy/core_plugins/timelion/public/header.svg deleted file mode 100644 index 56f2f0dc51a6e..0000000000000 --- a/src/legacy/core_plugins/timelion/public/header.svg +++ /dev/null @@ -1,227 +0,0 @@ - - - - - - image/svg+xml - - Kibana-Full-Logo - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Kibana-Full-Logo - - - - - - - - - - - - - - - - - - - - - diff --git a/src/legacy/core_plugins/timelion/public/icon.svg b/src/legacy/core_plugins/timelion/public/icon.svg deleted file mode 100644 index ba9a704b3ade2..0000000000000 --- a/src/legacy/core_plugins/timelion/public/icon.svg +++ /dev/null @@ -1,97 +0,0 @@ - - - - - - - - - - - - - - - - - - image/svg+xml - - - - - - - - - diff --git a/src/legacy/core_plugins/timelion/public/legacy.ts b/src/legacy/core_plugins/timelion/public/legacy.ts deleted file mode 100644 index 7980291e2d462..0000000000000 --- a/src/legacy/core_plugins/timelion/public/legacy.ts +++ /dev/null @@ -1,35 +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 { PluginInitializerContext } from 'kibana/public'; -import { npSetup, npStart } from 'ui/new_platform'; -import { plugin } from '.'; -import { TimelionPluginSetupDependencies } from './plugin'; -import { LegacyDependenciesPlugin } from './shim'; - -const setupPlugins: Readonly = { - // Temporary solution - // It will be removed when all dependent services are migrated to the new platform. - __LEGACY: new LegacyDependenciesPlugin(), -}; - -const pluginInstance = plugin({} as PluginInitializerContext); - -export const setup = pluginInstance.setup(npSetup.core, setupPlugins); -export const start = pluginInstance.start(npStart.core, npStart.plugins); diff --git a/src/legacy/core_plugins/timelion/public/logo.png b/src/legacy/core_plugins/timelion/public/logo.png deleted file mode 100644 index 7a62253697a06..0000000000000 Binary files a/src/legacy/core_plugins/timelion/public/logo.png and /dev/null differ diff --git a/src/legacy/core_plugins/timelion/public/plugin.ts b/src/legacy/core_plugins/timelion/public/plugin.ts deleted file mode 100644 index 1f837303a2b3d..0000000000000 --- a/src/legacy/core_plugins/timelion/public/plugin.ts +++ /dev/null @@ -1,74 +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 { - 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 { - uiSettings: IUiSettingsClient; - timelionPanels: Map; -} - -/** @internal */ -export interface TimelionPluginSetupDependencies { - // Temporary solution - __LEGACY: LegacyDependenciesPlugin; -} - -/** @internal */ -export class TimelionPlugin implements Plugin, void> { - initializerContext: PluginInitializerContext; - - constructor(initializerContext: PluginInitializerContext) { - this.initializerContext = initializerContext; - } - - public async setup(core: CoreSetup, { __LEGACY }: TimelionPluginSetupDependencies) { - const timelionPanels: Map = new Map(); - - const dependencies: TimelionVisualizationDependencies = { - uiSettings: core.uiSettings, - timelionPanels, - ...(await __LEGACY.setup(core, timelionPanels)), - }; - - this.registerPanels(dependencies); - } - - private registerPanels(dependencies: TimelionVisualizationDependencies) { - const timeChartPanel: Panel = getTimeChart(dependencies); - - dependencies.timelionPanels.set(timeChartPanel.name, timeChartPanel); - } - - public start(core: CoreStart, { kibanaLegacy }: { kibanaLegacy: KibanaLegacyStart }) { - kibanaLegacy.loadFontAwesome(); - } - - public stop(): void {} -} diff --git a/src/legacy/core_plugins/timelion/public/services/saved_sheets.ts b/src/legacy/core_plugins/timelion/public/services/saved_sheets.ts deleted file mode 100644 index 1fb29de83d3d7..0000000000000 --- a/src/legacy/core_plugins/timelion/public/services/saved_sheets.ts +++ /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 { npStart } from 'ui/new_platform'; -// @ts-ignore -import { uiModules } from 'ui/modules'; -import { SavedObjectLoader } from '../../../../../plugins/saved_objects/public'; -import { createSavedSheetClass } from './_saved_sheet'; - -const module = uiModules.get('app/sheet'); - -const savedObjectsClient = npStart.core.savedObjects.client; -const services = { - savedObjectsClient, - indexPatterns: npStart.plugins.data.indexPatterns, - search: npStart.plugins.data.search, - chrome: npStart.core.chrome, - overlays: npStart.core.overlays, -}; - -const SavedSheet = createSavedSheetClass(services, npStart.core.uiSettings); - -export const savedSheetLoader = new SavedObjectLoader( - SavedSheet, - savedObjectsClient, - npStart.core.chrome -); -savedSheetLoader.urlFor = (id) => `#/${encodeURIComponent(id)}`; -// Customize loader properties since adding an 's' on type doesn't work for type 'timelion-sheet'. -savedSheetLoader.loaderProperties = { - name: 'timelion-sheet', - noun: 'Saved Sheets', - nouns: 'saved sheets', -}; - -// This is the only thing that gets injected into controllers -module.service('savedSheets', () => savedSheetLoader); diff --git a/src/legacy/core_plugins/timelion/public/shim/timelion_legacy_module.ts b/src/legacy/core_plugins/timelion/public/shim/timelion_legacy_module.ts deleted file mode 100644 index 8122259f1c991..0000000000000 --- a/src/legacy/core_plugins/timelion/public/shim/timelion_legacy_module.ts +++ /dev/null @@ -1,55 +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 'ngreact'; -import 'brace/mode/hjson'; -import 'brace/ext/searchbox'; -import 'ui/accessibility/kbn_ui_ace_keyboard_mode'; - -import { once } from 'lodash'; -// @ts-ignore -import { uiModules } from 'ui/modules'; -import { Panel } from '../panels/panel'; -// @ts-ignore -import { Chart } from '../directives/chart/chart'; -// @ts-ignore -import { TimelionInterval } from '../directives/timelion_interval/timelion_interval'; -// @ts-ignore -import { TimelionExpInput } from '../directives/timelion_expression_input'; -// @ts-ignore -import { TimelionExpressionSuggestions } from '../directives/timelion_expression_suggestions/timelion_expression_suggestions'; - -/** @internal */ -export const initTimelionLegacyModule = once((timelionPanels: Map): void => { - require('ui/state_management/app_state'); - - uiModules - .get('apps/timelion', []) - .controller('TimelionVisController', function ($scope: any) { - $scope.$on('timelionChartRendered', (event: any) => { - event.stopPropagation(); - $scope.renderComplete(); - }); - }) - .constant('timelionPanels', timelionPanels) - .directive('chart', Chart) - .directive('timelionInterval', TimelionInterval) - .directive('timelionExpressionSuggestions', TimelionExpressionSuggestions) - .directive('timelionExpressionInput', TimelionExpInput); -}); diff --git a/src/legacy/server/config/schema.js b/src/legacy/server/config/schema.js index 53f5185442688..952c35df244c1 100644 --- a/src/legacy/server/config/schema.js +++ b/src/legacy/server/config/schema.js @@ -237,7 +237,7 @@ export default () => manifestServiceUrl: Joi.string().default('').allow(''), emsFileApiUrl: Joi.string().default('https://vector.maps.elastic.co'), emsTileApiUrl: Joi.string().default('https://tiles.maps.elastic.co'), - emsLandingPageUrl: Joi.string().default('https://maps.elastic.co/v7.8'), + emsLandingPageUrl: Joi.string().default('https://maps.elastic.co/v7.9'), emsFontLibraryUrl: Joi.string().default( 'https://tiles.maps.elastic.co/fonts/{fontstack}/{range}.pbf' ), diff --git a/src/legacy/ui/public/state_management/__tests__/state.js b/src/legacy/ui/public/state_management/__tests__/state.js index cde123e6c1d85..b6c705e814509 100644 --- a/src/legacy/ui/public/state_management/__tests__/state.js +++ b/src/legacy/ui/public/state_management/__tests__/state.js @@ -21,6 +21,7 @@ import sinon from 'sinon'; import expect from '@kbn/expect'; import ngMock from 'ng_mock'; import { encode as encodeRison } from 'rison-node'; +import uiRoutes from 'ui/routes'; import '../../private'; import { toastNotifications } from '../../notify'; import * as FatalErrorNS from '../../notify/fatal_error'; @@ -38,6 +39,8 @@ describe('State Management', () => { const sandbox = sinon.createSandbox(); afterEach(() => sandbox.restore()); + uiRoutes.enable(); + describe('Enabled', () => { let $rootScope; let $location; diff --git a/src/plugins/advanced_settings/public/plugin.ts b/src/plugins/advanced_settings/public/plugin.ts index 8b3347f8d88f0..35f6dd65925ba 100644 --- a/src/plugins/advanced_settings/public/plugin.ts +++ b/src/plugins/advanced_settings/public/plugin.ts @@ -18,7 +18,6 @@ */ import { i18n } from '@kbn/i18n'; import { CoreSetup, Plugin } from 'kibana/public'; -import { ManagementSectionId } from '../../management/public'; import { ComponentRegistry } from './component_registry'; import { AdvancedSettingsSetup, AdvancedSettingsStart, AdvancedSettingsPluginSetup } from './types'; @@ -31,7 +30,7 @@ const title = i18n.translate('advancedSettings.advancedSettingsLabel', { export class AdvancedSettingsPlugin implements Plugin { public setup(core: CoreSetup, { management }: AdvancedSettingsPluginSetup) { - const kibanaSection = management.sections.getSection(ManagementSectionId.Kibana); + const kibanaSection = management.sections.section.kibana; kibanaSection.registerApp({ id: 'settings', diff --git a/src/plugins/data/common/index.ts b/src/plugins/data/common/index.ts index 0fb45fcc739d4..ca6bc965d48c5 100644 --- a/src/plugins/data/common/index.ts +++ b/src/plugins/data/common/index.ts @@ -26,5 +26,6 @@ export * from './kbn_field_types'; export * from './query'; export * from './search'; export * from './search/aggs'; +export * from './search/expressions'; export * from './types'; export * from './utils'; diff --git a/src/legacy/core_plugins/timelion/public/directives/fullscreen/fullscreen.js b/src/plugins/data/common/search/expressions/esaggs.ts similarity index 61% rename from src/legacy/core_plugins/timelion/public/directives/fullscreen/fullscreen.js rename to src/plugins/data/common/search/expressions/esaggs.ts index 5c4bd72ceb708..2957512886b4d 100644 --- a/src/legacy/core_plugins/timelion/public/directives/fullscreen/fullscreen.js +++ b/src/plugins/data/common/search/expressions/esaggs.ts @@ -17,23 +17,27 @@ * under the License. */ -require('angular-sortable-view'); -require('plugins/timelion/directives/chart/chart'); -require('plugins/timelion/directives/timelion_grid'); +import { + KibanaContext, + KibanaDatatable, + ExpressionFunctionDefinition, +} from '../../../../../plugins/expressions/common'; -const app = require('ui/modules').get('apps/timelion', ['angular-sortable-view']); -import html from './fullscreen.html'; +type Input = KibanaContext | null; +type Output = Promise; -app.directive('timelionFullscreen', function () { - return { - restrict: 'E', - scope: { - expression: '=', - series: '=', - state: '=', - transient: '=', - onSearch: '=', - }, - template: html, - }; -}); +interface Arguments { + index: string; + metricsAtAllLevels: boolean; + partialRows: boolean; + includeFormatHints: boolean; + aggConfigs: string; + timeFields?: string[]; +} + +export type EsaggsExpressionFunctionDefinition = ExpressionFunctionDefinition< + 'esaggs', + Input, + Arguments, + Output +>; diff --git a/src/legacy/core_plugins/timelion/public/services/saved_sheet_register.ts b/src/plugins/data/common/search/expressions/index.ts similarity index 96% rename from src/legacy/core_plugins/timelion/public/services/saved_sheet_register.ts rename to src/plugins/data/common/search/expressions/index.ts index 7c8a2909238da..f1a39a8383629 100644 --- a/src/legacy/core_plugins/timelion/public/services/saved_sheet_register.ts +++ b/src/plugins/data/common/search/expressions/index.ts @@ -16,4 +16,5 @@ * specific language governing permissions and limitations * under the License. */ -import './saved_sheets'; + +export * from './esaggs'; diff --git a/src/plugins/data/public/index.ts b/src/plugins/data/public/index.ts index 2efd1c82aae79..6328e694193c9 100644 --- a/src/plugins/data/public/index.ts +++ b/src/plugins/data/public/index.ts @@ -313,7 +313,7 @@ import { toAbsoluteDates, } from '../common'; -export { ParsedInterval } from '../common'; +export { EsaggsExpressionFunctionDefinition, ParsedInterval } from '../common'; export { // aggs diff --git a/src/plugins/data/public/public.api.md b/src/plugins/data/public/public.api.md index 0c23ba340304f..cd3fff010c053 100644 --- a/src/plugins/data/public/public.api.md +++ b/src/plugins/data/public/public.api.md @@ -52,6 +52,7 @@ import { EuiButtonEmptyProps } from '@elastic/eui'; import { EuiComboBoxProps } from '@elastic/eui'; import { EuiConfirmModalProps } from '@elastic/eui'; import { EuiGlobalToastListToast } from '@elastic/eui'; +import { EventEmitter } from 'events'; import { ExclusiveUnion } from '@elastic/eui'; import { ExistsParams } from 'elasticsearch'; import { ExplainParams } from 'elasticsearch'; @@ -145,7 +146,7 @@ import { ReindexParams } from 'elasticsearch'; import { ReindexRethrottleParams } from 'elasticsearch'; import { RenderSearchTemplateParams } from 'elasticsearch'; import { RequestAdapter } from 'src/plugins/inspector/common'; -import { RequestStatistics } from 'src/plugins/inspector/common'; +import { RequestStatistics as RequestStatistics_2 } from 'src/plugins/inspector/common'; import { Required } from '@kbn/utility-types'; import * as Rx from 'rxjs'; import { SavedObject } from 'src/core/server'; @@ -180,6 +181,7 @@ import { UiActionsSetup } from 'src/plugins/ui_actions/public'; import { UiActionsStart } from 'src/plugins/ui_actions/public'; import { Unit } from '@elastic/datemath'; import { UnregisterCallback } from 'history'; +import { UnwrapPromiseOrReturn } from '@kbn/utility-types'; import { UpdateDocumentByQueryParams } from 'elasticsearch'; import { UpdateDocumentParams } from 'elasticsearch'; import { UserProvidedValues } from 'src/core/server/types'; @@ -425,6 +427,15 @@ export enum ES_FIELD_TYPES { // @public (undocumented) export const ES_SEARCH_STRATEGY = "es"; +// Warning: (ae-forgotten-export) The symbol "ExpressionFunctionDefinition" needs to be exported by the entry point index.d.ts +// Warning: (ae-forgotten-export) The symbol "Input" needs to be exported by the entry point index.d.ts +// Warning: (ae-forgotten-export) The symbol "Arguments" needs to be exported by the entry point index.d.ts +// Warning: (ae-forgotten-export) The symbol "Output" needs to be exported by the entry point index.d.ts +// Warning: (ae-missing-release-tag) "EsaggsExpressionFunctionDefinition" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export type EsaggsExpressionFunctionDefinition = ExpressionFunctionDefinition<'esaggs', Input, Arguments, Output>; + // Warning: (ae-missing-release-tag) "esFilters" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) diff --git a/src/plugins/data/public/search/aggs/metrics/top_hit.test.ts b/src/plugins/data/public/search/aggs/metrics/top_hit.test.ts index cd5b4a2f724bd..c2434df3ae53c 100644 --- a/src/plugins/data/public/search/aggs/metrics/top_hit.test.ts +++ b/src/plugins/data/public/search/aggs/metrics/top_hit.test.ts @@ -111,9 +111,7 @@ describe('Top hit metric', () => { it('requests both source and docvalues_fields for non-text aggregatable fields', () => { init({ fieldName: 'bytes', readFromDocValues: true }); expect(aggDsl.top_hits._source).toBe('bytes'); - expect(aggDsl.top_hits.docvalue_fields).toEqual([ - { field: 'bytes', format: 'use_field_mapping' }, - ]); + expect(aggDsl.top_hits.docvalue_fields).toEqual([{ field: 'bytes' }]); }); it('requests both source and docvalues_fields for date aggregatable fields', () => { diff --git a/src/plugins/data/public/search/aggs/metrics/top_hit.ts b/src/plugins/data/public/search/aggs/metrics/top_hit.ts index 5ca883e60afd3..bee731dcc2e0d 100644 --- a/src/plugins/data/public/search/aggs/metrics/top_hit.ts +++ b/src/plugins/data/public/search/aggs/metrics/top_hit.ts @@ -88,12 +88,15 @@ export const getTopHitMetricAgg = () => { }; } else { if (field.readFromDocValues) { - // always format date fields as date_time to avoid - // displaying unformatted dates like epoch_millis - // or other not-accepted momentjs formats - const format = - field.type === KBN_FIELD_TYPES.DATE ? 'date_time' : 'use_field_mapping'; - output.params.docvalue_fields = [{ field: field.name, format }]; + output.params.docvalue_fields = [ + { + field: field.name, + // always format date fields as date_time to avoid + // displaying unformatted dates like epoch_millis + // or other not-accepted momentjs formats + ...(field.type === KBN_FIELD_TYPES.DATE && { format: 'date_time' }), + }, + ]; } output.params._source = field.name === '_source' ? true : field.name; } diff --git a/src/plugins/data/public/search/expressions/esaggs.ts b/src/plugins/data/public/search/expressions/esaggs.ts index 4ac6c823d2e3b..b01f17762b2be 100644 --- a/src/plugins/data/public/search/expressions/esaggs.ts +++ b/src/plugins/data/public/search/expressions/esaggs.ts @@ -19,12 +19,8 @@ import { get, hasIn } from 'lodash'; import { i18n } from '@kbn/i18n'; -import { - KibanaContext, - KibanaDatatable, - ExpressionFunctionDefinition, - KibanaDatatableColumn, -} from 'src/plugins/expressions/public'; + +import { KibanaDatatable, KibanaDatatableColumn } from 'src/plugins/expressions/public'; import { calculateObjectHash } from '../../../../../plugins/kibana_utils/public'; import { PersistedState } from '../../../../../plugins/visualizations/public'; import { Adapters } from '../../../../../plugins/inspector/public'; @@ -34,6 +30,7 @@ import { ISearchSource } from '../search_source'; import { tabifyAggResponse } from '../tabify'; import { calculateBounds, + EsaggsExpressionFunctionDefinition, Filter, getTime, IIndexPattern, @@ -71,18 +68,6 @@ export interface RequestHandlerParams { const name = 'esaggs'; -type Input = KibanaContext | null; -type Output = Promise; - -interface Arguments { - index: string; - metricsAtAllLevels: boolean; - partialRows: boolean; - includeFormatHints: boolean; - aggConfigs: string; - timeFields?: string[]; -} - const handleCourierRequest = async ({ searchSource, aggs, @@ -244,7 +229,7 @@ const handleCourierRequest = async ({ return (searchSource as any).tabifiedResponse; }; -export const esaggs = (): ExpressionFunctionDefinition => ({ +export const esaggs = (): EsaggsExpressionFunctionDefinition => ({ name, type: 'kibana_datatable', inputTypes: ['kibana_context', 'null'], diff --git a/src/plugins/data/server/index.ts b/src/plugins/data/server/index.ts index b94238dcf96a4..461b21e1cc980 100644 --- a/src/plugins/data/server/index.ts +++ b/src/plugins/data/server/index.ts @@ -161,18 +161,13 @@ import { toAbsoluteDates, } from '../common'; -export { ParsedInterval } from '../common'; +export { EsaggsExpressionFunctionDefinition, ParsedInterval } from '../common'; export { - ISearch, - ISearchCancel, + ISearchStrategy, ISearchOptions, - IRequestTypesMap, - IResponseTypesMap, ISearchSetup, ISearchStart, - TStrategyTypes, - ISearchStrategy, getDefaultSearchParams, getTotalLoaded, } from './search'; diff --git a/src/plugins/data/server/search/es_search/es_search_strategy.ts b/src/plugins/data/server/search/es_search/es_search_strategy.ts index db08ddf920818..82f8ef21ebb38 100644 --- a/src/plugins/data/server/search/es_search/es_search_strategy.ts +++ b/src/plugins/data/server/search/es_search/es_search_strategy.ts @@ -17,17 +17,16 @@ * under the License. */ import { first } from 'rxjs/operators'; -import { RequestHandlerContext, SharedGlobalConfig } from 'kibana/server'; +import { SharedGlobalConfig } from 'kibana/server'; import { SearchResponse } from 'elasticsearch'; import { Observable } from 'rxjs'; -import { ES_SEARCH_STRATEGY } from '../../../common/search'; import { ISearchStrategy, getDefaultSearchParams, getTotalLoaded } from '..'; export const esSearchStrategyProvider = ( config$: Observable -): ISearchStrategy => { +): ISearchStrategy => { return { - search: async (context: RequestHandlerContext, request, options) => { + search: async (context, request, options) => { const config = await config$.pipe(first()).toPromise(); const defaultParams = getDefaultSearchParams(config); diff --git a/src/plugins/data/server/search/index.ts b/src/plugins/data/server/search/index.ts index 882f56e83d4ca..67789fcbf56b4 100644 --- a/src/plugins/data/server/search/index.ts +++ b/src/plugins/data/server/search/index.ts @@ -17,16 +17,6 @@ * under the License. */ -export { - ISearch, - ISearchCancel, - ISearchOptions, - IRequestTypesMap, - IResponseTypesMap, - ISearchSetup, - ISearchStart, - TStrategyTypes, - ISearchStrategy, -} from './types'; +export { ISearchStrategy, ISearchOptions, ISearchSetup, ISearchStart } from './types'; export { getDefaultSearchParams, getTotalLoaded } from './es_search'; diff --git a/src/plugins/data/server/search/mocks.ts b/src/plugins/data/server/search/mocks.ts index 0aab466a9a0d9..b210df3c55db9 100644 --- a/src/plugins/data/server/search/mocks.ts +++ b/src/plugins/data/server/search/mocks.ts @@ -26,5 +26,6 @@ export function createSearchSetupMock() { export function createSearchStartMock() { return { getSearchStrategy: jest.fn(), + search: jest.fn(), }; } diff --git a/src/plugins/data/server/search/routes.test.ts b/src/plugins/data/server/search/routes.test.ts index 4ef67de93e454..167bd5af5d51d 100644 --- a/src/plugins/data/server/search/routes.test.ts +++ b/src/plugins/data/server/search/routes.test.ts @@ -33,9 +33,8 @@ describe('Search service', () => { }); it('handler calls context.search.search with the given request and strategy', async () => { - const mockSearch = jest.fn().mockResolvedValue('yay'); - mockDataStart.search.getSearchStrategy.mockReturnValueOnce({ search: mockSearch }); - + const response = { id: 'yay' }; + mockDataStart.search.search.mockResolvedValue(response); const mockContext = {}; const mockBody = { params: {} }; const mockParams = { strategy: 'foo' }; @@ -51,21 +50,21 @@ describe('Search service', () => { const handler = mockRouter.post.mock.calls[0][1]; await handler((mockContext as unknown) as RequestHandlerContext, mockRequest, mockResponse); - expect(mockDataStart.search.getSearchStrategy.mock.calls[0][0]).toBe(mockParams.strategy); - expect(mockSearch).toBeCalled(); - expect(mockSearch.mock.calls[0][1]).toStrictEqual(mockBody); + expect(mockDataStart.search.search).toBeCalled(); + expect(mockDataStart.search.search.mock.calls[0][1]).toStrictEqual(mockBody); expect(mockResponse.ok).toBeCalled(); - expect(mockResponse.ok.mock.calls[0][0]).toEqual({ body: 'yay' }); + expect(mockResponse.ok.mock.calls[0][0]).toEqual({ + body: response, + }); }); it('handler throws an error if the search throws an error', async () => { - const mockSearch = jest.fn().mockRejectedValue({ + mockDataStart.search.search.mockRejectedValue({ message: 'oh no', body: { error: 'oops', }, }); - mockDataStart.search.getSearchStrategy.mockReturnValueOnce({ search: mockSearch }); const mockContext = {}; const mockBody = { params: {} }; @@ -82,9 +81,8 @@ describe('Search service', () => { const handler = mockRouter.post.mock.calls[0][1]; await handler((mockContext as unknown) as RequestHandlerContext, mockRequest, mockResponse); - expect(mockDataStart.search.getSearchStrategy.mock.calls[0][0]).toBe(mockParams.strategy); - expect(mockSearch).toBeCalled(); - expect(mockSearch.mock.calls[0][1]).toStrictEqual(mockBody); + expect(mockDataStart.search.search).toBeCalled(); + expect(mockDataStart.search.search.mock.calls[0][1]).toStrictEqual(mockBody); expect(mockResponse.customError).toBeCalled(); const error: any = mockResponse.customError.mock.calls[0][0]; expect(error.body.message).toBe('oh no'); diff --git a/src/plugins/data/server/search/routes.ts b/src/plugins/data/server/search/routes.ts index 7b6c045b0908c..bf1982a1f7fb2 100644 --- a/src/plugins/data/server/search/routes.ts +++ b/src/plugins/data/server/search/routes.ts @@ -42,10 +42,12 @@ export function registerSearchRoute(core: CoreSetup): v const signal = getRequestAbortedSignal(request.events.aborted$); const [, , selfStart] = await core.getStartServices(); - const searchStrategy = selfStart.search.getSearchStrategy(strategy); try { - const response = await searchStrategy.search(context, searchRequest, { signal }); + const response = await selfStart.search.search(context, searchRequest, { + signal, + strategy, + }); return res.ok({ body: response }); } catch (err) { return res.customError({ diff --git a/src/plugins/data/server/search/search_service.ts b/src/plugins/data/server/search/search_service.ts index 34ed8c6c6f401..20f9a7488893f 100644 --- a/src/plugins/data/server/search/search_service.ts +++ b/src/plugins/data/server/search/search_service.ts @@ -17,20 +17,24 @@ * under the License. */ -import { Plugin, PluginInitializerContext, CoreSetup } from '../../../../core/server'; import { - ISearchSetup, - ISearchStart, - TSearchStrategiesMap, - TRegisterSearchStrategy, - TGetSearchStrategy, -} from './types'; + Plugin, + PluginInitializerContext, + CoreSetup, + RequestHandlerContext, +} from '../../../../core/server'; +import { ISearchSetup, ISearchStart, ISearchStrategy } from './types'; import { registerSearchRoute } from './routes'; import { ES_SEARCH_STRATEGY, esSearchStrategyProvider } from './es_search'; import { DataPluginStart } from '../plugin'; +import { IEsSearchRequest } from '../../common'; + +interface StrategyMap { + [name: string]: ISearchStrategy; +} export class SearchService implements Plugin { - private searchStrategies: TSearchStrategiesMap = {}; + private searchStrategies: StrategyMap = {}; constructor(private initializerContext: PluginInitializerContext) {} @@ -45,17 +49,28 @@ export class SearchService implements Plugin { return { registerSearchStrategy: this.registerSearchStrategy }; } + private search(context: RequestHandlerContext, searchRequest: IEsSearchRequest, options: any) { + return this.getSearchStrategy(options.strategy || ES_SEARCH_STRATEGY).search( + context, + searchRequest, + { signal: options.signal } + ); + } + public start(): ISearchStart { - return { getSearchStrategy: this.getSearchStrategy }; + return { + getSearchStrategy: this.getSearchStrategy, + search: this.search, + }; } public stop() {} - private registerSearchStrategy: TRegisterSearchStrategy = (name, strategy) => { + private registerSearchStrategy = (name: string, strategy: ISearchStrategy) => { this.searchStrategies[name] = strategy; }; - private getSearchStrategy: TGetSearchStrategy = (name) => { + private getSearchStrategy = (name: string): ISearchStrategy => { const strategy = this.searchStrategies[name]; if (!strategy) { throw new Error(`Search strategy ${name} not found`); diff --git a/src/plugins/data/server/search/types.ts b/src/plugins/data/server/search/types.ts index dea325cc063bb..12f1a1a508bd2 100644 --- a/src/plugins/data/server/search/types.ts +++ b/src/plugins/data/server/search/types.ts @@ -19,14 +19,22 @@ import { RequestHandlerContext } from '../../../../core/server'; import { IKibanaSearchResponse, IKibanaSearchRequest } from '../../common/search'; -import { ES_SEARCH_STRATEGY, IEsSearchRequest, IEsSearchResponse } from './es_search'; +import { IEsSearchRequest, IEsSearchResponse } from './es_search'; + +export interface ISearchOptions { + /** + * An `AbortSignal` that allows the caller of `search` to abort a search request. + */ + signal?: AbortSignal; + strategy?: string; +} export interface ISearchSetup { /** * Extension point exposed for other plugins to register their own search * strategies. */ - registerSearchStrategy: TRegisterSearchStrategy; + registerSearchStrategy: (name: string, strategy: ISearchStrategy) => void; } export interface ISearchStart { @@ -34,78 +42,23 @@ export interface ISearchStart { * Get other registered search strategies. For example, if a new strategy needs to use the * already-registered ES search strategy, it can use this function to accomplish that. */ - getSearchStrategy: TGetSearchStrategy; -} - -export interface ISearchOptions { - /** - * An `AbortSignal` that allows the caller of `search` to abort a search request. - */ - signal?: AbortSignal; + getSearchStrategy: (name: string) => ISearchStrategy; + search: ( + context: RequestHandlerContext, + request: IKibanaSearchRequest, + options: ISearchOptions + ) => Promise; } -/** - * Contains all known strategy type identifiers that will be used to map to - * request and response shapes. Plugins that wish to add their own custom search - * strategies should extend this type via: - * - * const MY_STRATEGY = 'MY_STRATEGY'; - * - * declare module 'src/plugins/search/server' { - * export interface IRequestTypesMap { - * [MY_STRATEGY]: IMySearchRequest; - * } - * - * export interface IResponseTypesMap { - * [MY_STRATEGY]: IMySearchResponse - * } - * } - */ -export type TStrategyTypes = typeof ES_SEARCH_STRATEGY | string; - -/** - * The map of search strategy IDs to the corresponding request type definitions. - */ -export interface IRequestTypesMap { - [ES_SEARCH_STRATEGY]: IEsSearchRequest; - [key: string]: IKibanaSearchRequest; -} - -/** - * The map of search strategy IDs to the corresponding response type definitions. - */ -export interface IResponseTypesMap { - [ES_SEARCH_STRATEGY]: IEsSearchResponse; - [key: string]: IKibanaSearchResponse; -} - -export type ISearch = ( - context: RequestHandlerContext, - request: IRequestTypesMap[T], - options?: ISearchOptions -) => Promise; - -export type ISearchCancel = ( - context: RequestHandlerContext, - id: string -) => Promise; - /** * Search strategy interface contains a search method that takes in a request and returns a promise * that resolves to a response. */ -export interface ISearchStrategy { - search: ISearch; - cancel?: ISearchCancel; +export interface ISearchStrategy { + search: ( + context: RequestHandlerContext, + request: IEsSearchRequest, + options?: ISearchOptions + ) => Promise; + cancel?: (context: RequestHandlerContext, id: string) => Promise; } - -export type TRegisterSearchStrategy = ( - name: T, - searchStrategy: ISearchStrategy -) => void; - -export type TGetSearchStrategy = (name: T) => ISearchStrategy; - -export type TSearchStrategiesMap = { - [K in TStrategyTypes]?: ISearchStrategy; -}; diff --git a/src/plugins/data/server/server.api.md b/src/plugins/data/server/server.api.md index 1fe03119c789d..4dc60056ed918 100644 --- a/src/plugins/data/server/server.api.md +++ b/src/plugins/data/server/server.api.md @@ -39,6 +39,7 @@ import { DeleteTemplateParams } from 'elasticsearch'; import { DetailedPeerCertificate } from 'tls'; import { Duration } from 'moment'; import { ErrorToastOptions } from 'src/core/public/notifications'; +import { EventEmitter } from 'events'; import { ExistsParams } from 'elasticsearch'; import { ExplainParams } from 'elasticsearch'; import { FieldStatsParams } from 'elasticsearch'; @@ -146,6 +147,7 @@ import { ToastInputFields } from 'src/core/public/notifications'; import { Type } from '@kbn/config-schema'; import { TypeOf } from '@kbn/config-schema'; import { Unit } from '@elastic/datemath'; +import { UnwrapPromiseOrReturn } from '@kbn/utility-types'; import { UpdateDocumentByQueryParams } from 'elasticsearch'; import { UpdateDocumentParams } from 'elasticsearch'; import { Url } from 'url'; @@ -220,6 +222,15 @@ export enum ES_FIELD_TYPES { _TYPE = "_type" } +// Warning: (ae-forgotten-export) The symbol "ExpressionFunctionDefinition" needs to be exported by the entry point index.d.ts +// Warning: (ae-forgotten-export) The symbol "Input" needs to be exported by the entry point index.d.ts +// Warning: (ae-forgotten-export) The symbol "Arguments" needs to be exported by the entry point index.d.ts +// Warning: (ae-forgotten-export) The symbol "Output" needs to be exported by the entry point index.d.ts +// Warning: (ae-missing-release-tag) "EsaggsExpressionFunctionDefinition" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// +// @public (undocumented) +export type EsaggsExpressionFunctionDefinition = ExpressionFunctionDefinition<'esaggs', Input, Arguments, Output>; + // Warning: (ae-missing-release-tag) "esFilters" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) @@ -507,77 +518,46 @@ export class IndexPatternsFetcher { }): Promise; } -// Warning: (ae-missing-release-tag) "IRequestTypesMap" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// -// @public -export interface IRequestTypesMap { - // Warning: (ae-forgotten-export) The symbol "IKibanaSearchRequest" needs to be exported by the entry point index.d.ts - // - // (undocumented) - [key: string]: IKibanaSearchRequest; - // Warning: (ae-forgotten-export) The symbol "ES_SEARCH_STRATEGY" needs to be exported by the entry point index.d.ts - // Warning: (ae-forgotten-export) The symbol "IEsSearchRequest" needs to be exported by the entry point index.d.ts - // - // (undocumented) - [ES_SEARCH_STRATEGY]: IEsSearchRequest; -} - -// Warning: (ae-missing-release-tag) "IResponseTypesMap" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// -// @public -export interface IResponseTypesMap { - // Warning: (ae-forgotten-export) The symbol "IKibanaSearchResponse" needs to be exported by the entry point index.d.ts - // - // (undocumented) - [key: string]: IKibanaSearchResponse; - // Warning: (ae-forgotten-export) The symbol "IEsSearchResponse" needs to be exported by the entry point index.d.ts - // - // (undocumented) - [ES_SEARCH_STRATEGY]: IEsSearchResponse; -} - -// Warning: (ae-forgotten-export) The symbol "RequestHandlerContext" needs to be exported by the entry point index.d.ts -// Warning: (ae-missing-release-tag) "ISearch" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// -// @public (undocumented) -export type ISearch = (context: RequestHandlerContext, request: IRequestTypesMap[T], options?: ISearchOptions) => Promise; - -// Warning: (ae-missing-release-tag) "ISearchCancel" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// -// @public (undocumented) -export type ISearchCancel = (context: RequestHandlerContext, id: string) => Promise; - // Warning: (ae-missing-release-tag) "ISearchOptions" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) export interface ISearchOptions { signal?: AbortSignal; + // (undocumented) + strategy?: string; } // Warning: (ae-missing-release-tag) "ISearchSetup" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) export interface ISearchSetup { - // Warning: (ae-forgotten-export) The symbol "TRegisterSearchStrategy" needs to be exported by the entry point index.d.ts - registerSearchStrategy: TRegisterSearchStrategy; + registerSearchStrategy: (name: string, strategy: ISearchStrategy) => void; } // Warning: (ae-missing-release-tag) "ISearchStart" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) export interface ISearchStart { - // Warning: (ae-forgotten-export) The symbol "TGetSearchStrategy" needs to be exported by the entry point index.d.ts - getSearchStrategy: TGetSearchStrategy; + getSearchStrategy: (name: string) => ISearchStrategy; + // Warning: (ae-forgotten-export) The symbol "RequestHandlerContext" needs to be exported by the entry point index.d.ts + // Warning: (ae-forgotten-export) The symbol "IKibanaSearchRequest" needs to be exported by the entry point index.d.ts + // Warning: (ae-forgotten-export) The symbol "IKibanaSearchResponse" needs to be exported by the entry point index.d.ts + // + // (undocumented) + search: (context: RequestHandlerContext, request: IKibanaSearchRequest, options: ISearchOptions) => Promise; } // Warning: (ae-missing-release-tag) "ISearchStrategy" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public -export interface ISearchStrategy { +export interface ISearchStrategy { // (undocumented) - cancel?: ISearchCancel; + cancel?: (context: RequestHandlerContext, id: string) => Promise; + // Warning: (ae-forgotten-export) The symbol "IEsSearchRequest" needs to be exported by the entry point index.d.ts + // Warning: (ae-forgotten-export) The symbol "IEsSearchResponse" needs to be exported by the entry point index.d.ts + // // (undocumented) - search: ISearch; + search: (context: RequestHandlerContext, request: IEsSearchRequest, options?: ISearchOptions) => Promise; } // @public (undocumented) @@ -757,11 +737,6 @@ export interface TimeRange { to: string; } -// Warning: (ae-missing-release-tag) "TStrategyTypes" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// -// @public -export type TStrategyTypes = typeof ES_SEARCH_STRATEGY | string; - // Warning: (ae-missing-release-tag) "UI_SETTINGS" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) @@ -820,13 +795,13 @@ export const UI_SETTINGS: { // src/plugins/data/server/index.ts:101:26 - (ae-forgotten-export) The symbol "TruncateFormat" needs to be exported by the entry point index.d.ts // src/plugins/data/server/index.ts:127:27 - (ae-forgotten-export) The symbol "isFilterable" needs to be exported by the entry point index.d.ts // src/plugins/data/server/index.ts:127:27 - (ae-forgotten-export) The symbol "isNestedField" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:183:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:184:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:185:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts -// src/plugins/data/server/index.ts:186:1 - (ae-forgotten-export) The symbol "Ipv4Address" 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 +// src/plugins/data/server/index.ts:178:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:179:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:180:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:181:1 - (ae-forgotten-export) The symbol "Ipv4Address" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:182:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:183:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/server/index.ts:186: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/expressions/common/ast/build_expression.test.ts b/src/plugins/expressions/common/ast/build_expression.test.ts new file mode 100644 index 0000000000000..657b9d3bdda28 --- /dev/null +++ b/src/plugins/expressions/common/ast/build_expression.test.ts @@ -0,0 +1,386 @@ +/* + * 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 { ExpressionAstExpression } from './types'; +import { buildExpression, isExpressionAstBuilder, isExpressionAst } from './build_expression'; +import { buildExpressionFunction, ExpressionAstFunctionBuilder } from './build_function'; +import { format } from './format'; + +describe('isExpressionAst()', () => { + test('returns true when a valid AST is provided', () => { + const ast = { + type: 'expression', + chain: [ + { + type: 'function', + function: 'foo', + arguments: {}, + }, + ], + }; + expect(isExpressionAst(ast)).toBe(true); + }); + + test('returns false when a invalid value is provided', () => { + const invalidValues = [ + buildExpression('hello | world'), + false, + null, + undefined, + 'hi', + { type: 'unknown' }, + {}, + ]; + + invalidValues.forEach((value) => { + expect(isExpressionAst(value)).toBe(false); + }); + }); +}); + +describe('isExpressionAstBuilder()', () => { + test('returns true when a valid builder is provided', () => { + const builder = buildExpression('hello | world'); + expect(isExpressionAstBuilder(builder)).toBe(true); + }); + + test('returns false when a invalid value is provided', () => { + const invalidValues = [ + buildExpressionFunction('myFn', {}), + false, + null, + undefined, + 'hi', + { type: 'unknown' }, + {}, + ]; + + invalidValues.forEach((value) => { + expect(isExpressionAstBuilder(value)).toBe(false); + }); + }); +}); + +describe('buildExpression()', () => { + let ast: ExpressionAstExpression; + let str: string; + + beforeEach(() => { + ast = { + type: 'expression', + chain: [ + { + type: 'function', + function: 'foo', + arguments: { + bar: ['baz'], + subexp: [ + { + type: 'expression', + chain: [ + { + type: 'function', + function: 'hello', + arguments: { + world: [false, true], + }, + }, + ], + }, + ], + }, + }, + ], + }; + str = format(ast, 'expression'); + }); + + test('accepts an expression AST as input', () => { + ast = { + type: 'expression', + chain: [ + { + type: 'function', + function: 'foo', + arguments: { + bar: ['baz'], + }, + }, + ], + }; + const exp = buildExpression(ast); + expect(exp.toAst()).toEqual(ast); + }); + + test('converts subexpressions in provided AST to expression builder instances', () => { + const exp = buildExpression(ast); + expect(isExpressionAstBuilder(exp.functions[0].getArgument('subexp')![0])).toBe(true); + }); + + test('accepts an expresssion string as input', () => { + const exp = buildExpression(str); + expect(exp.toAst()).toEqual(ast); + }); + + test('accepts an array of function builders as input', () => { + const firstFn = ast.chain[0]; + const exp = buildExpression([ + buildExpressionFunction(firstFn.function, firstFn.arguments), + buildExpressionFunction('hiya', {}), + ]); + expect(exp.toAst()).toMatchInlineSnapshot(` + Object { + "chain": Array [ + Object { + "arguments": Object { + "bar": Array [ + "baz", + ], + "subexp": Array [ + Object { + "chain": Array [ + Object { + "arguments": Object { + "world": Array [ + false, + true, + ], + }, + "function": "hello", + "type": "function", + }, + ], + "type": "expression", + }, + ], + }, + "function": "foo", + "type": "function", + }, + Object { + "arguments": Object {}, + "function": "hiya", + "type": "function", + }, + ], + "type": "expression", + } + `); + }); + + describe('functions', () => { + test('returns an array of buildExpressionFunctions', () => { + const exp = buildExpression(ast); + expect(exp.functions).toHaveLength(1); + expect(exp.functions.map((f) => f.name)).toEqual(['foo']); + }); + + test('functions.push() adds new function to the AST', () => { + const exp = buildExpression(ast); + const fn = buildExpressionFunction('test', { abc: [123] }); + exp.functions.push(fn); + expect(exp.toAst()).toMatchInlineSnapshot(` + Object { + "chain": Array [ + Object { + "arguments": Object { + "bar": Array [ + "baz", + ], + "subexp": Array [ + Object { + "chain": Array [ + Object { + "arguments": Object { + "world": Array [ + false, + true, + ], + }, + "function": "hello", + "type": "function", + }, + ], + "type": "expression", + }, + ], + }, + "function": "foo", + "type": "function", + }, + Object { + "arguments": Object { + "abc": Array [ + 123, + ], + }, + "function": "test", + "type": "function", + }, + ], + "type": "expression", + } + `); + }); + + test('functions can be reordered', () => { + const exp = buildExpression(ast); + const fn = buildExpressionFunction('test', { abc: [123] }); + exp.functions.push(fn); + expect(exp.functions.map((f) => f.name)).toEqual(['foo', 'test']); + const testFn = exp.functions[1]; + exp.functions[1] = exp.functions[0]; + exp.functions[0] = testFn; + expect(exp.functions.map((f) => f.name)).toEqual(['test', 'foo']); + const barFn = buildExpressionFunction('bar', {}); + const fooFn = exp.functions[1]; + exp.functions[1] = barFn; + exp.functions[2] = fooFn; + expect(exp.functions.map((f) => f.name)).toEqual(['test', 'bar', 'foo']); + }); + + test('functions can be removed', () => { + const exp = buildExpression(ast); + const fn = buildExpressionFunction('test', { abc: [123] }); + exp.functions.push(fn); + expect(exp.functions.map((f) => f.name)).toEqual(['foo', 'test']); + exp.functions.shift(); + expect(exp.functions.map((f) => f.name)).toEqual(['test']); + }); + }); + + describe('#toAst', () => { + test('generates the AST for an expression', () => { + const exp = buildExpression('foo | bar hello=true hello=false'); + expect(exp.toAst()).toMatchInlineSnapshot(` + Object { + "chain": Array [ + Object { + "arguments": Object {}, + "function": "foo", + "type": "function", + }, + Object { + "arguments": Object { + "hello": Array [ + true, + false, + ], + }, + "function": "bar", + "type": "function", + }, + ], + "type": "expression", + } + `); + }); + + test('throws when called on an expression with no functions', () => { + ast.chain = []; + const exp = buildExpression(ast); + expect(() => { + exp.toAst(); + }).toThrowError(); + }); + }); + + describe('#toString', () => { + test('generates an expression string from the AST', () => { + const exp = buildExpression(ast); + expect(exp.toString()).toMatchInlineSnapshot( + `"foo bar=\\"baz\\" subexp={hello world=false world=true}"` + ); + }); + + test('throws when called on an expression with no functions', () => { + ast.chain = []; + const exp = buildExpression(ast); + expect(() => { + exp.toString(); + }).toThrowError(); + }); + }); + + describe('#findFunction', () => { + test('finds a function by name', () => { + const exp = buildExpression(`where | is | waldo`); + const fns: ExpressionAstFunctionBuilder[] = exp.findFunction('waldo'); + expect(fns.map((fn) => fn.toAst())).toMatchInlineSnapshot(` + Array [ + Object { + "arguments": Object {}, + "function": "waldo", + "type": "function", + }, + ] + `); + }); + + test('recursively finds nested subexpressions', () => { + const exp = buildExpression( + `miss | miss sub={miss} | miss sub={hit sub={miss sub={hit sub={hit}}}} sub={miss}` + ); + const fns: ExpressionAstFunctionBuilder[] = exp.findFunction('hit'); + expect(fns.map((fn) => fn.name)).toMatchInlineSnapshot(` + Array [ + "hit", + "hit", + "hit", + ] + `); + }); + + test('retains references back to the original expression so you can perform migrations', () => { + const before = ` + foo sub={baz | bar a=1 sub={foo}} + | bar a=1 + | baz sub={bar a=1 c=4 sub={bar a=1 c=5}} + `; + + // Migrates all `bar` functions in the expression + const exp = buildExpression(before); + exp.findFunction('bar').forEach((fn) => { + const arg = fn.getArgument('a'); + if (arg) { + fn.replaceArgument('a', [1, 2]); + fn.addArgument('b', 3); + fn.removeArgument('c'); + } + }); + + expect(exp.toString()).toMatchInlineSnapshot(` + "foo sub={baz | bar a=1 a=2 sub={foo} b=3} + | bar a=1 a=2 b=3 + | baz sub={bar a=1 a=2 sub={bar a=1 a=2 b=3} b=3}" + `); + }); + + test('returns any subexpressions as expression builder instances', () => { + const exp = buildExpression( + `miss | miss sub={miss} | miss sub={hit sub={miss sub={hit sub={hit}}}} sub={miss}` + ); + const fns: ExpressionAstFunctionBuilder[] = exp.findFunction('hit'); + const subexpressionArgs = fns.map((fn) => + fn.getArgument('sub')?.map((arg) => isExpressionAstBuilder(arg)) + ); + expect(subexpressionArgs).toEqual([undefined, [true], [true]]); + }); + }); +}); diff --git a/src/plugins/expressions/common/ast/build_expression.ts b/src/plugins/expressions/common/ast/build_expression.ts new file mode 100644 index 0000000000000..b0a560600883a --- /dev/null +++ b/src/plugins/expressions/common/ast/build_expression.ts @@ -0,0 +1,169 @@ +/* + * 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 { AnyExpressionFunctionDefinition } from '../expression_functions/types'; +import { ExpressionAstExpression, ExpressionAstFunction } from './types'; +import { + buildExpressionFunction, + ExpressionAstFunctionBuilder, + InferFunctionDefinition, +} from './build_function'; +import { format } from './format'; +import { parse } from './parse'; + +/** + * Type guard that checks whether a given value is an + * `ExpressionAstExpressionBuilder`. This is useful when working + * with subexpressions, where you might be retrieving a function + * argument, and need to know whether it is an expression builder + * instance which you can perform operations on. + * + * @example + * const arg = myFunction.getArgument('foo'); + * if (isExpressionAstBuilder(foo)) { + * foo.toAst(); + * } + * + * @param val Value you want to check. + * @return boolean + */ +export function isExpressionAstBuilder(val: any): val is ExpressionAstExpressionBuilder { + return val?.type === 'expression_builder'; +} + +/** @internal */ +export function isExpressionAst(val: any): val is ExpressionAstExpression { + return val?.type === 'expression'; +} + +export interface ExpressionAstExpressionBuilder { + /** + * Used to identify expression builder objects. + */ + type: 'expression_builder'; + /** + * Array of each of the `buildExpressionFunction()` instances + * in this expression. Use this to remove or reorder functions + * in the expression. + */ + functions: ExpressionAstFunctionBuilder[]; + /** + * Recursively searches expression for all ocurrences of the + * function, including in subexpressions. + * + * Useful when performing migrations on a specific function, + * as you can iterate over the array of references and update + * all functions at once. + * + * @param fnName Name of the function to search for. + * @return `ExpressionAstFunctionBuilder[]` + */ + findFunction: ( + fnName: InferFunctionDefinition['name'] + ) => Array> | []; + /** + * Converts expression to an AST. + * + * @return `ExpressionAstExpression` + */ + toAst: () => ExpressionAstExpression; + /** + * Converts expression to an expression string. + * + * @return `string` + */ + toString: () => string; +} + +const generateExpressionAst = (fns: ExpressionAstFunctionBuilder[]): ExpressionAstExpression => ({ + type: 'expression', + chain: fns.map((fn) => fn.toAst()), +}); + +/** + * Makes it easy to progressively build, update, and traverse an + * expression AST. You can either start with an empty AST, or + * provide an expression string, AST, or array of expression + * function builders to use as initial state. + * + * @param initialState Optional. An expression string, AST, or array of `ExpressionAstFunctionBuilder[]`. + * @return `this` + */ +export function buildExpression( + initialState?: ExpressionAstFunctionBuilder[] | ExpressionAstExpression | string +): ExpressionAstExpressionBuilder { + const chainToFunctionBuilder = (chain: ExpressionAstFunction[]): ExpressionAstFunctionBuilder[] => + chain.map((fn) => buildExpressionFunction(fn.function, fn.arguments)); + + // Takes `initialState` and converts it to an array of `ExpressionAstFunctionBuilder` + const extractFunctionsFromState = ( + state: ExpressionAstFunctionBuilder[] | ExpressionAstExpression | string + ): ExpressionAstFunctionBuilder[] => { + if (typeof state === 'string') { + return chainToFunctionBuilder(parse(state, 'expression').chain); + } else if (!Array.isArray(state)) { + // If it isn't an array, it is an `ExpressionAstExpression` + return chainToFunctionBuilder(state.chain); + } + return state; + }; + + const fns: ExpressionAstFunctionBuilder[] = initialState + ? extractFunctionsFromState(initialState) + : []; + + return { + type: 'expression_builder', + functions: fns, + + findFunction( + fnName: InferFunctionDefinition['name'] + ) { + const foundFns: Array> = []; + return fns.reduce((found, currFn) => { + Object.values(currFn.arguments).forEach((values) => { + values.forEach((value) => { + if (isExpressionAstBuilder(value)) { + // `value` is a subexpression, recurse and continue searching + found = found.concat(value.findFunction(fnName)); + } + }); + }); + if (currFn.name === fnName) { + found.push(currFn as ExpressionAstFunctionBuilder); + } + return found; + }, foundFns); + }, + + toAst() { + if (fns.length < 1) { + throw new Error('Functions have not been added to the expression builder'); + } + return generateExpressionAst(fns); + }, + + toString() { + if (fns.length < 1) { + throw new Error('Functions have not been added to the expression builder'); + } + return format(generateExpressionAst(fns), 'expression'); + }, + }; +} diff --git a/src/plugins/expressions/common/ast/build_function.test.ts b/src/plugins/expressions/common/ast/build_function.test.ts new file mode 100644 index 0000000000000..a2b54f31f6f8f --- /dev/null +++ b/src/plugins/expressions/common/ast/build_function.test.ts @@ -0,0 +1,399 @@ +/* + * 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 { ExpressionAstExpression } from './types'; +import { buildExpression } from './build_expression'; +import { buildExpressionFunction } from './build_function'; + +describe('buildExpressionFunction()', () => { + let subexp: ExpressionAstExpression; + let ast: ExpressionAstExpression; + + beforeEach(() => { + subexp = { + type: 'expression', + chain: [ + { + type: 'function', + function: 'hello', + arguments: { + world: [false, true], + }, + }, + ], + }; + ast = { + type: 'expression', + chain: [ + { + type: 'function', + function: 'foo', + arguments: { + bar: ['baz'], + subexp: [subexp], + }, + }, + ], + }; + }); + + test('accepts an args object as initial state', () => { + const fn = buildExpressionFunction('hello', { world: [true] }); + expect(fn.toAst()).toMatchInlineSnapshot(` + Object { + "arguments": Object { + "world": Array [ + true, + ], + }, + "function": "hello", + "type": "function", + } + `); + }); + + test('wraps any args in initial state in an array', () => { + const fn = buildExpressionFunction('hello', { world: true }); + expect(fn.arguments).toMatchInlineSnapshot(` + Object { + "world": Array [ + true, + ], + } + `); + }); + + test('returns all expected properties', () => { + const fn = buildExpressionFunction('hello', { world: [true] }); + expect(Object.keys(fn)).toMatchInlineSnapshot(` + Array [ + "type", + "name", + "arguments", + "addArgument", + "getArgument", + "replaceArgument", + "removeArgument", + "toAst", + "toString", + ] + `); + }); + + test('handles subexpressions in initial state', () => { + const fn = buildExpressionFunction(ast.chain[0].function, ast.chain[0].arguments); + expect(fn.toAst()).toMatchInlineSnapshot(` + Object { + "arguments": Object { + "bar": Array [ + "baz", + ], + "subexp": Array [ + Object { + "chain": Array [ + Object { + "arguments": Object { + "world": Array [ + false, + true, + ], + }, + "function": "hello", + "type": "function", + }, + ], + "type": "expression", + }, + ], + }, + "function": "foo", + "type": "function", + } + `); + }); + + test('handles subexpressions in multi-args in initial state', () => { + const subexpression = buildExpression([buildExpressionFunction('mySubexpression', {})]); + const fn = buildExpressionFunction('hello', { world: [true, subexpression] }); + expect(fn.toAst().arguments.world).toMatchInlineSnapshot(` + Array [ + true, + Object { + "chain": Array [ + Object { + "arguments": Object {}, + "function": "mySubexpression", + "type": "function", + }, + ], + "type": "expression", + }, + ] + `); + }); + + describe('handles subexpressions as args', () => { + test('when provided an AST for the subexpression', () => { + const fn = buildExpressionFunction('hello', { world: [true] }); + fn.addArgument('subexp', buildExpression(subexp).toAst()); + expect(fn.toAst().arguments.subexp).toMatchInlineSnapshot(` + Array [ + Object { + "chain": Array [ + Object { + "arguments": Object { + "world": Array [ + false, + true, + ], + }, + "function": "hello", + "type": "function", + }, + ], + "type": "expression", + }, + ] + `); + }); + + test('when provided a function builder for the subexpression', () => { + // test using `markdownVis`, which expects a subexpression + // using the `font` function + const anotherSubexpression = buildExpression([buildExpressionFunction('font', { size: 12 })]); + const fn = buildExpressionFunction('markdownVis', { + markdown: 'hello', + openLinksInNewTab: true, + font: anotherSubexpression, + }); + expect(fn.toAst().arguments.font).toMatchInlineSnapshot(` + Array [ + Object { + "chain": Array [ + Object { + "arguments": Object { + "size": Array [ + 12, + ], + }, + "function": "font", + "type": "function", + }, + ], + "type": "expression", + }, + ] + `); + }); + + test('when subexpressions are changed by reference', () => { + const fontFn = buildExpressionFunction('font', { size: 12 }); + const fn = buildExpressionFunction('markdownVis', { + markdown: 'hello', + openLinksInNewTab: true, + font: buildExpression([fontFn]), + }); + fontFn.addArgument('color', 'blue'); + fontFn.replaceArgument('size', [72]); + expect(fn.toAst().arguments.font).toMatchInlineSnapshot(` + Array [ + Object { + "chain": Array [ + Object { + "arguments": Object { + "color": Array [ + "blue", + ], + "size": Array [ + 72, + ], + }, + "function": "font", + "type": "function", + }, + ], + "type": "expression", + }, + ] + `); + }); + }); + + describe('#addArgument', () => { + test('allows you to add a new argument', () => { + const fn = buildExpressionFunction('hello', { world: [true] }); + fn.addArgument('world', false); + expect(fn.toAst().arguments).toMatchInlineSnapshot(` + Object { + "world": Array [ + true, + false, + ], + } + `); + }); + + test('creates new args if they do not yet exist', () => { + const fn = buildExpressionFunction('hello', { world: [true] }); + fn.addArgument('foo', 'bar'); + expect(fn.toAst().arguments).toMatchInlineSnapshot(` + Object { + "foo": Array [ + "bar", + ], + "world": Array [ + true, + ], + } + `); + }); + + test('mutates a function already associated with an expression', () => { + const fn = buildExpressionFunction('hello', { world: [true] }); + const exp = buildExpression([fn]); + fn.addArgument('foo', 'bar'); + expect(exp.toAst().chain).toMatchInlineSnapshot(` + Array [ + Object { + "arguments": Object { + "foo": Array [ + "bar", + ], + "world": Array [ + true, + ], + }, + "function": "hello", + "type": "function", + }, + ] + `); + fn.removeArgument('foo'); + expect(exp.toAst().chain).toMatchInlineSnapshot(` + Array [ + Object { + "arguments": Object { + "world": Array [ + true, + ], + }, + "function": "hello", + "type": "function", + }, + ] + `); + }); + }); + + describe('#getArgument', () => { + test('retrieves an arg by name', () => { + const fn = buildExpressionFunction('hello', { world: [true] }); + expect(fn.getArgument('world')).toEqual([true]); + }); + + test(`returns undefined when an arg doesn't exist`, () => { + const fn = buildExpressionFunction('hello', { world: [true] }); + expect(fn.getArgument('test')).toBe(undefined); + }); + + test('returned array can be updated to add/remove multiargs', () => { + const fn = buildExpressionFunction('hello', { world: [0, 1] }); + const arg = fn.getArgument('world'); + arg!.push(2); + expect(fn.getArgument('world')).toEqual([0, 1, 2]); + fn.replaceArgument( + 'world', + arg!.filter((a) => a !== 1) + ); + expect(fn.getArgument('world')).toEqual([0, 2]); + }); + }); + + describe('#toAst', () => { + test('returns a function AST', () => { + const fn = buildExpressionFunction('hello', { foo: [true] }); + expect(fn.toAst()).toMatchInlineSnapshot(` + Object { + "arguments": Object { + "foo": Array [ + true, + ], + }, + "function": "hello", + "type": "function", + } + `); + }); + }); + + describe('#toString', () => { + test('returns a function String', () => { + const fn = buildExpressionFunction('hello', { foo: [true], bar: ['hi'] }); + expect(fn.toString()).toMatchInlineSnapshot(`"hello foo=true bar=\\"hi\\""`); + }); + }); + + describe('#replaceArgument', () => { + test('allows you to replace an existing argument', () => { + const fn = buildExpressionFunction('hello', { world: [true] }); + fn.replaceArgument('world', [false]); + expect(fn.toAst().arguments).toMatchInlineSnapshot(` + Object { + "world": Array [ + false, + ], + } + `); + }); + + test('allows you to replace an existing argument with multi args', () => { + const fn = buildExpressionFunction('hello', { world: [true] }); + fn.replaceArgument('world', [true, false]); + expect(fn.toAst().arguments).toMatchInlineSnapshot(` + Object { + "world": Array [ + true, + false, + ], + } + `); + }); + + test('throws an error when replacing a non-existant arg', () => { + const fn = buildExpressionFunction('hello', { world: [true] }); + expect(() => { + fn.replaceArgument('whoops', [false]); + }).toThrowError(); + }); + }); + + describe('#removeArgument', () => { + test('removes an argument by name', () => { + const fn = buildExpressionFunction('hello', { foo: [true], bar: [false] }); + fn.removeArgument('bar'); + expect(fn.toAst().arguments).toMatchInlineSnapshot(` + Object { + "foo": Array [ + true, + ], + } + `); + }); + }); +}); diff --git a/src/plugins/expressions/common/ast/build_function.ts b/src/plugins/expressions/common/ast/build_function.ts new file mode 100644 index 0000000000000..5a1bd615d6450 --- /dev/null +++ b/src/plugins/expressions/common/ast/build_function.ts @@ -0,0 +1,243 @@ +/* + * 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 { ExpressionAstFunction } from './types'; +import { + AnyExpressionFunctionDefinition, + ExpressionFunctionDefinition, +} from '../expression_functions/types'; +import { + buildExpression, + ExpressionAstExpressionBuilder, + isExpressionAstBuilder, + isExpressionAst, +} from './build_expression'; +import { format } from './format'; + +// Infers the types from an ExpressionFunctionDefinition. +// @internal +export type InferFunctionDefinition< + FnDef extends AnyExpressionFunctionDefinition +> = FnDef extends ExpressionFunctionDefinition< + infer Name, + infer Input, + infer Arguments, + infer Output, + infer Context +> + ? { name: Name; input: Input; arguments: Arguments; output: Output; context: Context } + : never; + +// Shortcut for inferring args from a function definition. +type FunctionArgs = InferFunctionDefinition< + FnDef +>['arguments']; + +// Gets a list of possible arg names for a given function. +type FunctionArgName = { + [A in keyof FunctionArgs]: A extends string ? A : never; +}[keyof FunctionArgs]; + +// Gets all optional string keys from an interface. +type OptionalKeys = { + [K in keyof T]-?: {} extends Pick ? (K extends string ? K : never) : never; +}[keyof T]; + +// Represents the shape of arguments as they are stored +// in the function builder. +interface FunctionBuilderArguments { + [key: string]: Array[string] | ExpressionAstExpressionBuilder>; +} + +export interface ExpressionAstFunctionBuilder< + FnDef extends AnyExpressionFunctionDefinition = AnyExpressionFunctionDefinition +> { + /** + * Used to identify expression function builder objects. + */ + type: 'expression_function_builder'; + /** + * Name of this expression function. + */ + name: InferFunctionDefinition['name']; + /** + * Object of all args currently added to the function. This is + * structured similarly to `ExpressionAstFunction['arguments']`, + * however any subexpressions are returned as expression builder + * instances instead of expression ASTs. + */ + arguments: FunctionBuilderArguments; + /** + * Adds an additional argument to the function. For multi-args, + * this should be called once for each new arg. Note that TS + * will not enforce whether multi-args are available, so only + * use this to update an existing arg if you are certain it + * is a multi-arg. + * + * @param name The name of the argument to add. + * @param value The value of the argument to add. + * @return `this` + */ + addArgument: >( + name: A, + value: FunctionArgs[A] | ExpressionAstExpressionBuilder + ) => this; + /** + * Retrieves an existing argument by name. + * Useful when you want to retrieve the current array of args and add + * something to it before calling `replaceArgument`. Any subexpression + * arguments will be returned as expression builder instances. + * + * @param name The name of the argument to retrieve. + * @return `ExpressionAstFunctionBuilderArgument[] | undefined` + */ + getArgument: >( + name: A + ) => Array[A] | ExpressionAstExpressionBuilder> | undefined; + /** + * Overwrites an existing argument with a new value. + * In order to support multi-args, the value given must always be + * an array. + * + * @param name The name of the argument to replace. + * @param value The value of the argument. Must always be an array. + * @return `this` + */ + replaceArgument: >( + name: A, + value: Array[A] | ExpressionAstExpressionBuilder> + ) => this; + /** + * Removes an (optional) argument from the function. + * + * TypeScript will enforce that you only remove optional + * arguments. For manipulating required args, use `replaceArgument`. + * + * @param name The name of the argument to remove. + * @return `this` + */ + removeArgument: >>(name: A) => this; + /** + * Converts function to an AST. + * + * @return `ExpressionAstFunction` + */ + toAst: () => ExpressionAstFunction; + /** + * Converts function to an expression string. + * + * @return `string` + */ + toString: () => string; +} + +/** + * Manages an AST for a single expression function. The return value + * can be provided to `buildExpression` to add this function to an + * expression. + * + * Note that to preserve type safety and ensure no args are missing, + * all required arguments for the specified function must be provided + * up front. If desired, they can be changed or removed later. + * + * @param fnName String representing the name of this expression function. + * @param initialArgs Object containing the arguments to this function. + * @return `this` + */ +export function buildExpressionFunction< + FnDef extends AnyExpressionFunctionDefinition = AnyExpressionFunctionDefinition +>( + fnName: InferFunctionDefinition['name'], + /** + * To support subexpressions, we override all args to also accept an + * ExpressionBuilder. This isn't perfectly typesafe since we don't + * know with certainty that the builder's output matches the required + * argument input, so we trust that folks using subexpressions in the + * builder know what they're doing. + */ + initialArgs: { + [K in keyof FunctionArgs]: + | FunctionArgs[K] + | ExpressionAstExpressionBuilder + | ExpressionAstExpressionBuilder[]; + } +): ExpressionAstFunctionBuilder { + const args = Object.entries(initialArgs).reduce((acc, [key, value]) => { + if (Array.isArray(value)) { + acc[key] = value.map((v) => { + return isExpressionAst(v) ? buildExpression(v) : v; + }); + } else { + acc[key] = isExpressionAst(value) ? [buildExpression(value)] : [value]; + } + return acc; + }, initialArgs as FunctionBuilderArguments); + + return { + type: 'expression_function_builder', + name: fnName, + arguments: args, + + addArgument(key, value) { + if (!args.hasOwnProperty(key)) { + args[key] = []; + } + args[key].push(value); + return this; + }, + + getArgument(key) { + if (!args.hasOwnProperty(key)) { + return; + } + return args[key]; + }, + + replaceArgument(key, values) { + if (!args.hasOwnProperty(key)) { + throw new Error('Argument to replace does not exist on this function'); + } + args[key] = values; + return this; + }, + + removeArgument(key) { + delete args[key]; + return this; + }, + + toAst() { + const ast: ExpressionAstFunction['arguments'] = {}; + return { + type: 'function', + function: fnName, + arguments: Object.entries(args).reduce((acc, [key, values]) => { + acc[key] = values.map((val) => { + return isExpressionAstBuilder(val) ? val.toAst() : val; + }); + return acc; + }, ast), + }; + }, + + toString() { + return format({ type: 'expression', chain: [this.toAst()] }, 'expression'); + }, + }; +} diff --git a/src/plugins/expressions/common/ast/format.test.ts b/src/plugins/expressions/common/ast/format.test.ts index d680ab2e30ce4..3d443c87b1ae2 100644 --- a/src/plugins/expressions/common/ast/format.test.ts +++ b/src/plugins/expressions/common/ast/format.test.ts @@ -17,11 +17,12 @@ * under the License. */ -import { formatExpression } from './format'; +import { ExpressionAstExpression, ExpressionAstArgument } from './types'; +import { format } from './format'; -describe('formatExpression()', () => { - test('converts expression AST to string', () => { - const str = formatExpression({ +describe('format()', () => { + test('formats an expression AST', () => { + const ast: ExpressionAstExpression = { type: 'expression', chain: [ { @@ -32,8 +33,13 @@ describe('formatExpression()', () => { function: 'foo', }, ], - }); + }; - expect(str).toMatchInlineSnapshot(`"foo bar=\\"baz\\""`); + expect(format(ast, 'expression')).toMatchInlineSnapshot(`"foo bar=\\"baz\\""`); + }); + + test('formats an argument', () => { + const ast: ExpressionAstArgument = 'foo'; + expect(format(ast, 'argument')).toMatchInlineSnapshot(`"\\"foo\\""`); }); }); diff --git a/src/plugins/expressions/common/ast/format.ts b/src/plugins/expressions/common/ast/format.ts index 985f07008b33d..7af0ab3350ab6 100644 --- a/src/plugins/expressions/common/ast/format.ts +++ b/src/plugins/expressions/common/ast/format.ts @@ -22,13 +22,9 @@ import { ExpressionAstExpression, ExpressionAstArgument } from './types'; // eslint-disable-next-line @typescript-eslint/no-var-requires const { toExpression } = require('@kbn/interpreter/common'); -export function format( - ast: ExpressionAstExpression | ExpressionAstArgument, - type: 'expression' | 'argument' +export function format( + ast: T, + type: T extends ExpressionAstExpression ? 'expression' : 'argument' ): string { return toExpression(ast, type); } - -export function formatExpression(ast: ExpressionAstExpression): string { - return format(ast, 'expression'); -} diff --git a/src/legacy/core_plugins/timelion/public/directives/timelion_save_sheet.js b/src/plugins/expressions/common/ast/format_expression.test.ts similarity index 64% rename from src/legacy/core_plugins/timelion/public/directives/timelion_save_sheet.js rename to src/plugins/expressions/common/ast/format_expression.test.ts index 6dd44a10dc48c..933fe78fc4dca 100644 --- a/src/legacy/core_plugins/timelion/public/directives/timelion_save_sheet.js +++ b/src/plugins/expressions/common/ast/format_expression.test.ts @@ -17,14 +17,23 @@ * under the License. */ -import { uiModules } from 'ui/modules'; -import saveTemplate from 'plugins/timelion/partials/save_sheet.html'; -const app = uiModules.get('apps/timelion', []); +import { formatExpression } from './format_expression'; -app.directive('timelionSave', function () { - return { - replace: true, - restrict: 'E', - template: saveTemplate, - }; +describe('formatExpression()', () => { + test('converts expression AST to string', () => { + const str = formatExpression({ + type: 'expression', + chain: [ + { + type: 'function', + arguments: { + bar: ['baz'], + }, + function: 'foo', + }, + ], + }); + + expect(str).toMatchInlineSnapshot(`"foo bar=\\"baz\\""`); + }); }); diff --git a/packages/kbn-test/src/page_load_metrics/event.ts b/src/plugins/expressions/common/ast/format_expression.ts similarity index 72% rename from packages/kbn-test/src/page_load_metrics/event.ts rename to src/plugins/expressions/common/ast/format_expression.ts index 481954bbf672e..cc9fe05fb85d2 100644 --- a/packages/kbn-test/src/page_load_metrics/event.ts +++ b/src/plugins/expressions/common/ast/format_expression.ts @@ -17,18 +17,14 @@ * under the License. */ -export interface ResponseReceivedEvent { - frameId: string; - loaderId: string; - requestId: string; - response: Record; - timestamp: number; - type: string; -} +import { ExpressionAstExpression } from './types'; +import { format } from './format'; -export interface DataReceivedEvent { - encodedDataLength: number; - dataLength: number; - requestId: string; - timestamp: number; +/** + * Given expression pipeline AST, returns formatted string. + * + * @param ast Expression pipeline AST. + */ +export function formatExpression(ast: ExpressionAstExpression): string { + return format(ast, 'expression'); } diff --git a/src/plugins/expressions/common/ast/index.ts b/src/plugins/expressions/common/ast/index.ts index 398718e8092b3..45ef8d45422eb 100644 --- a/src/plugins/expressions/common/ast/index.ts +++ b/src/plugins/expressions/common/ast/index.ts @@ -17,7 +17,10 @@ * under the License. */ -export * from './types'; -export * from './parse'; -export * from './parse_expression'; +export * from './build_expression'; +export * from './build_function'; +export * from './format_expression'; export * from './format'; +export * from './parse_expression'; +export * from './parse'; +export * from './types'; diff --git a/src/plugins/expressions/common/ast/parse.test.ts b/src/plugins/expressions/common/ast/parse.test.ts index 967091a52082f..77487f0a1ee90 100644 --- a/src/plugins/expressions/common/ast/parse.test.ts +++ b/src/plugins/expressions/common/ast/parse.test.ts @@ -37,6 +37,12 @@ describe('parse()', () => { }); }); + test('throws on malformed expression', () => { + expect(() => { + parse('{ intentionally malformed }', 'expression'); + }).toThrowError(); + }); + test('parses an argument', () => { const arg = parse('foo', 'argument'); expect(arg).toBe('foo'); diff --git a/src/plugins/expressions/common/ast/parse.ts b/src/plugins/expressions/common/ast/parse.ts index 0204694d1926d..f02c51d7b6799 100644 --- a/src/plugins/expressions/common/ast/parse.ts +++ b/src/plugins/expressions/common/ast/parse.ts @@ -22,10 +22,10 @@ import { ExpressionAstExpression, ExpressionAstArgument } from './types'; // eslint-disable-next-line @typescript-eslint/no-var-requires const { parse: parseRaw } = require('@kbn/interpreter/common'); -export function parse( - expression: string, - startRule: 'expression' | 'argument' -): ExpressionAstExpression | ExpressionAstArgument { +export function parse( + expression: E, + startRule: S +): S extends 'expression' ? ExpressionAstExpression : ExpressionAstArgument { try { return parseRaw(String(expression), { startRule }); } catch (e) { diff --git a/src/plugins/expressions/common/ast/parse_expression.ts b/src/plugins/expressions/common/ast/parse_expression.ts index ae4d80bd1fb5b..1ae542aa3d0c7 100644 --- a/src/plugins/expressions/common/ast/parse_expression.ts +++ b/src/plugins/expressions/common/ast/parse_expression.ts @@ -26,5 +26,5 @@ import { parse } from './parse'; * @param expression Expression pipeline string. */ export function parseExpression(expression: string): ExpressionAstExpression { - return parse(expression, 'expression') as ExpressionAstExpression; + return parse(expression, 'expression'); } diff --git a/src/plugins/expressions/common/expression_functions/specs/clog.ts b/src/plugins/expressions/common/expression_functions/specs/clog.ts index 7839f1fc7998d..28294af04c881 100644 --- a/src/plugins/expressions/common/expression_functions/specs/clog.ts +++ b/src/plugins/expressions/common/expression_functions/specs/clog.ts @@ -19,7 +19,9 @@ import { ExpressionFunctionDefinition } from '../types'; -export const clog: ExpressionFunctionDefinition<'clog', unknown, {}, unknown> = { +export type ExpressionFunctionClog = ExpressionFunctionDefinition<'clog', unknown, {}, unknown>; + +export const clog: ExpressionFunctionClog = { name: 'clog', args: {}, help: 'Outputs the context to the console', diff --git a/src/plugins/expressions/common/expression_functions/specs/font.ts b/src/plugins/expressions/common/expression_functions/specs/font.ts index c8016bfacc710..c46ce0adadef0 100644 --- a/src/plugins/expressions/common/expression_functions/specs/font.ts +++ b/src/plugins/expressions/common/expression_functions/specs/font.ts @@ -52,7 +52,9 @@ interface Arguments { weight?: FontWeight; } -export const font: ExpressionFunctionDefinition<'font', null, Arguments, Style> = { +export type ExpressionFunctionFont = ExpressionFunctionDefinition<'font', null, Arguments, Style>; + +export const font: ExpressionFunctionFont = { name: 'font', aliases: [], type: 'style', diff --git a/src/plugins/expressions/common/expression_functions/specs/var.ts b/src/plugins/expressions/common/expression_functions/specs/var.ts index e90a21101c557..4bc185a4cadfd 100644 --- a/src/plugins/expressions/common/expression_functions/specs/var.ts +++ b/src/plugins/expressions/common/expression_functions/specs/var.ts @@ -24,7 +24,12 @@ interface Arguments { name: string; } -type ExpressionFunctionVar = ExpressionFunctionDefinition<'var', unknown, Arguments, unknown>; +export type ExpressionFunctionVar = ExpressionFunctionDefinition< + 'var', + unknown, + Arguments, + unknown +>; export const variable: ExpressionFunctionVar = { name: 'var', diff --git a/src/plugins/expressions/common/expression_functions/specs/var_set.ts b/src/plugins/expressions/common/expression_functions/specs/var_set.ts index 0bf89f5470b3d..8f15bc8b90042 100644 --- a/src/plugins/expressions/common/expression_functions/specs/var_set.ts +++ b/src/plugins/expressions/common/expression_functions/specs/var_set.ts @@ -25,7 +25,14 @@ interface Arguments { value?: any; } -export const variableSet: ExpressionFunctionDefinition<'var_set', unknown, Arguments, unknown> = { +export type ExpressionFunctionVarSet = ExpressionFunctionDefinition< + 'var_set', + unknown, + Arguments, + unknown +>; + +export const variableSet: ExpressionFunctionVarSet = { name: 'var_set', help: i18n.translate('expressions.functions.varset.help', { defaultMessage: 'Updates kibana global context', diff --git a/src/plugins/expressions/common/expression_functions/types.ts b/src/plugins/expressions/common/expression_functions/types.ts index b91deea36aee8..5979bcffb3175 100644 --- a/src/plugins/expressions/common/expression_functions/types.ts +++ b/src/plugins/expressions/common/expression_functions/types.ts @@ -21,6 +21,14 @@ import { UnwrapPromiseOrReturn } from '@kbn/utility-types'; import { ArgumentType } from './arguments'; import { TypeToString } from '../types/common'; import { ExecutionContext } from '../execution/types'; +import { + ExpressionFunctionClog, + ExpressionFunctionFont, + ExpressionFunctionKibanaContext, + ExpressionFunctionKibana, + ExpressionFunctionVarSet, + ExpressionFunctionVar, +} from './specs'; /** * `ExpressionFunctionDefinition` is the interface plugins have to implement to @@ -29,7 +37,7 @@ import { ExecutionContext } from '../execution/types'; export interface ExpressionFunctionDefinition< Name extends string, Input, - Arguments, + Arguments extends Record, Output, Context extends ExecutionContext = ExecutionContext > { @@ -93,4 +101,25 @@ export interface ExpressionFunctionDefinition< /** * Type to capture every possible expression function definition. */ -export type AnyExpressionFunctionDefinition = ExpressionFunctionDefinition; +export type AnyExpressionFunctionDefinition = ExpressionFunctionDefinition< + string, + any, + Record, + any +>; + +/** + * A mapping of `ExpressionFunctionDefinition`s for functions which the + * Expressions services provides out-of-the-box. Any new functions registered + * by the Expressions plugin should have their types added here. + * + * @public + */ +export interface ExpressionFunctionDefinitions { + clog: ExpressionFunctionClog; + font: ExpressionFunctionFont; + kibana_context: ExpressionFunctionKibanaContext; + kibana: ExpressionFunctionKibana; + var_set: ExpressionFunctionVarSet; + var: ExpressionFunctionVar; +} diff --git a/src/plugins/expressions/public/index.ts b/src/plugins/expressions/public/index.ts index 336a80d98a110..87406db89a2a8 100644 --- a/src/plugins/expressions/public/index.ts +++ b/src/plugins/expressions/public/index.ts @@ -42,6 +42,8 @@ export { AnyExpressionFunctionDefinition, AnyExpressionTypeDefinition, ArgumentType, + buildExpression, + buildExpressionFunction, Datatable, DatatableColumn, DatatableColumnType, @@ -57,10 +59,13 @@ export { ExecutorState, ExpressionAstArgument, ExpressionAstExpression, + ExpressionAstExpressionBuilder, ExpressionAstFunction, + ExpressionAstFunctionBuilder, ExpressionAstNode, ExpressionFunction, ExpressionFunctionDefinition, + ExpressionFunctionDefinitions, ExpressionFunctionKibana, ExpressionFunctionParameter, ExpressionImage, @@ -90,6 +95,7 @@ export { IInterpreterRenderHandlers, InterpreterErrorType, IRegistry, + isExpressionAstBuilder, KIBANA_CONTEXT_NAME, KibanaContext, KibanaDatatable, diff --git a/src/plugins/expressions/server/index.ts b/src/plugins/expressions/server/index.ts index 61d3838466bef..9b2f0b794258b 100644 --- a/src/plugins/expressions/server/index.ts +++ b/src/plugins/expressions/server/index.ts @@ -34,6 +34,8 @@ export { AnyExpressionFunctionDefinition, AnyExpressionTypeDefinition, ArgumentType, + buildExpression, + buildExpressionFunction, Datatable, DatatableColumn, DatatableColumnType, @@ -48,10 +50,13 @@ export { ExecutorState, ExpressionAstArgument, ExpressionAstExpression, + ExpressionAstExpressionBuilder, ExpressionAstFunction, + ExpressionAstFunctionBuilder, ExpressionAstNode, ExpressionFunction, ExpressionFunctionDefinition, + ExpressionFunctionDefinitions, ExpressionFunctionKibana, ExpressionFunctionParameter, ExpressionImage, @@ -81,6 +86,7 @@ export { IInterpreterRenderHandlers, InterpreterErrorType, IRegistry, + isExpressionAstBuilder, KIBANA_CONTEXT_NAME, KibanaContext, KibanaDatatable, diff --git a/src/plugins/index_pattern_management/public/plugin.ts b/src/plugins/index_pattern_management/public/plugin.ts index 6e93d23f8469c..fe680eff8657e 100644 --- a/src/plugins/index_pattern_management/public/plugin.ts +++ b/src/plugins/index_pattern_management/public/plugin.ts @@ -27,7 +27,7 @@ import { IndexPatternManagementServiceStart, } from './service'; -import { ManagementSetup, ManagementSectionId } from '../../management/public'; +import { ManagementSetup } from '../../management/public'; export interface IndexPatternManagementSetupDependencies { management: ManagementSetup; @@ -64,7 +64,7 @@ export class IndexPatternManagementPlugin core: CoreSetup, { management, kibanaLegacy }: IndexPatternManagementSetupDependencies ) { - const kibanaSection = management.sections.getSection(ManagementSectionId.Kibana); + const kibanaSection = management.sections.section.kibana; if (!kibanaSection) { throw new Error('`kibana` management section not found.'); diff --git a/src/plugins/management/kibana.json b/src/plugins/management/kibana.json index f48158e98ff3f..308e006b5aba0 100644 --- a/src/plugins/management/kibana.json +++ b/src/plugins/management/kibana.json @@ -4,5 +4,5 @@ "server": true, "ui": true, "requiredPlugins": ["kibanaLegacy", "home"], - "requiredBundles": ["kibanaReact"] + "requiredBundles": ["kibanaReact", "kibanaUtils"] } diff --git a/src/plugins/management/public/application.tsx b/src/plugins/management/public/application.tsx index 5d014504b8938..035f5d56e4cc7 100644 --- a/src/plugins/management/public/application.tsx +++ b/src/plugins/management/public/application.tsx @@ -20,21 +20,15 @@ import React from 'react'; import ReactDOM from 'react-dom'; -import { AppMountContext, AppMountParameters } from 'kibana/public'; +import { AppMountParameters } from 'kibana/public'; import { ManagementApp, ManagementAppDependencies } from './components/management_app'; export const renderApp = async ( - context: AppMountContext, { history, appBasePath, element }: AppMountParameters, dependencies: ManagementAppDependencies ) => { ReactDOM.render( - , + , element ); diff --git a/src/plugins/management/public/components/index.ts b/src/plugins/management/public/components/index.ts index 8979809c5245e..3a2a3eafb89e2 100644 --- a/src/plugins/management/public/components/index.ts +++ b/src/plugins/management/public/components/index.ts @@ -18,4 +18,3 @@ */ export { ManagementApp } from './management_app'; -export { managementSections } from './management_sections'; diff --git a/src/plugins/management/public/components/management_app/management_app.tsx b/src/plugins/management/public/components/management_app/management_app.tsx index fc5a8924c95d6..313884a90908f 100644 --- a/src/plugins/management/public/components/management_app/management_app.tsx +++ b/src/plugins/management/public/components/management_app/management_app.tsx @@ -17,36 +17,32 @@ * under the License. */ import React, { useState, useEffect, useCallback } from 'react'; -import { - AppMountContext, - AppMountParameters, - ChromeBreadcrumb, - ScopedHistory, -} from 'kibana/public'; +import { AppMountParameters, ChromeBreadcrumb, ScopedHistory } from 'kibana/public'; import { I18nProvider } from '@kbn/i18n/react'; import { EuiPage } from '@elastic/eui'; -import { ManagementStart } from '../../types'; import { ManagementSection, MANAGEMENT_BREADCRUMB } from '../../utils'; import { ManagementRouter } from './management_router'; import { ManagementSidebarNav } from '../management_sidebar_nav'; import { reactRouterNavigate } from '../../../../kibana_react/public'; +import { SectionsServiceStart } from '../../types'; import './management_app.scss'; interface ManagementAppProps { appBasePath: string; - context: AppMountContext; history: AppMountParameters['history']; dependencies: ManagementAppDependencies; } export interface ManagementAppDependencies { - management: ManagementStart; + sections: SectionsServiceStart; kibanaVersion: string; + setBreadcrumbs: (newBreadcrumbs: ChromeBreadcrumb[]) => void; } -export const ManagementApp = ({ context, dependencies, history }: ManagementAppProps) => { +export const ManagementApp = ({ dependencies, history }: ManagementAppProps) => { + const { setBreadcrumbs } = dependencies; const [selectedId, setSelectedId] = useState(''); const [sections, setSections] = useState(); @@ -55,24 +51,24 @@ export const ManagementApp = ({ context, dependencies, history }: ManagementAppP window.scrollTo(0, 0); }, []); - const setBreadcrumbs = useCallback( + const setBreadcrumbsScoped = useCallback( (crumbs: ChromeBreadcrumb[] = [], appHistory?: ScopedHistory) => { const wrapBreadcrumb = (item: ChromeBreadcrumb, scopedHistory: ScopedHistory) => ({ ...item, ...(item.href ? reactRouterNavigate(scopedHistory, item.href) : {}), }); - context.core.chrome.setBreadcrumbs([ + setBreadcrumbs([ wrapBreadcrumb(MANAGEMENT_BREADCRUMB, history), ...crumbs.map((item) => wrapBreadcrumb(item, appHistory || history)), ]); }, - [context.core.chrome, history] + [setBreadcrumbs, history] ); useEffect(() => { - setSections(dependencies.management.sections.getSectionsEnabled()); - }, [dependencies.management.sections]); + setSections(dependencies.sections.getSectionsEnabled()); + }, [dependencies.sections]); if (!sections) { return null; @@ -84,7 +80,7 @@ export const ManagementApp = ({ context, dependencies, history }: ManagementAppP ( - - - {text} +export const KibanaSection = { + id: ManagementSectionId.Kibana, + title: kibanaTitle, + tip: kibanaTip, + order: 4, +}; - - - - - -); +export const StackSection = { + id: ManagementSectionId.Stack, + title: stackTitle, + tip: stackTip, + order: 4, +}; export const managementSections = [ - { - id: ManagementSectionId.Ingest, - title: ( - - ), - }, - { - id: ManagementSectionId.Data, - title: , - }, - { - id: ManagementSectionId.InsightsAndAlerting, - title: ( - - ), - }, - { - id: ManagementSectionId.Security, - title: , - }, - { - id: ManagementSectionId.Kibana, - title: , - }, - { - id: ManagementSectionId.Stack, - title: , - }, + IngestSection, + DataSection, + InsightsAndAlertingSection, + SecuritySection, + KibanaSection, + StackSection, ]; diff --git a/src/plugins/management/public/components/management_sidebar_nav/management_sidebar_nav.tsx b/src/plugins/management/public/components/management_sidebar_nav/management_sidebar_nav.tsx index 055dda5ed84a1..37d1167661d82 100644 --- a/src/plugins/management/public/components/management_sidebar_nav/management_sidebar_nav.tsx +++ b/src/plugins/management/public/components/management_sidebar_nav/management_sidebar_nav.tsx @@ -21,7 +21,15 @@ import React, { useState } from 'react'; import { i18n } from '@kbn/i18n'; import { sortBy } from 'lodash'; -import { EuiIcon, EuiSideNav, EuiScreenReaderOnly, EuiSideNavItemType } from '@elastic/eui'; +import { + EuiIcon, + EuiSideNav, + EuiScreenReaderOnly, + EuiSideNavItemType, + EuiFlexGroup, + EuiFlexItem, + EuiToolTip, +} from '@elastic/eui'; import { AppMountParameters } from 'kibana/public'; import { ManagementApp, ManagementSection } from '../../utils'; @@ -79,6 +87,23 @@ export const ManagementSidebarNav = ({ }), })); + interface TooltipWrapperProps { + text: string; + tip?: string; + } + + const TooltipWrapper = ({ text, tip }: TooltipWrapperProps) => ( + + + {text} + + + + + + + ); + const createNavItem = ( item: T, customParams: Partial> = {} @@ -87,7 +112,7 @@ export const ManagementSidebarNav = ({ return { id: item.id, - name: item.title, + name: item.tip ? : item.title, isSelected: item.id === selectedId, icon: iconType ? : undefined, 'data-test-subj': item.id, diff --git a/src/plugins/management/public/index.ts b/src/plugins/management/public/index.ts index 3ba469c7831f6..f6c23ccf0143f 100644 --- a/src/plugins/management/public/index.ts +++ b/src/plugins/management/public/index.ts @@ -27,8 +27,8 @@ export function plugin(initializerContext: PluginInitializerContext) { export { RegisterManagementAppArgs, ManagementSection, ManagementApp } from './utils'; export { - ManagementSectionId, ManagementAppMountParams, ManagementSetup, ManagementStart, + DefinedSections, } from './types'; diff --git a/src/plugins/management/public/management_sections_service.test.ts b/src/plugins/management/public/management_sections_service.test.ts index fd56dd8a6ee27..3e0001e4ca550 100644 --- a/src/plugins/management/public/management_sections_service.test.ts +++ b/src/plugins/management/public/management_sections_service.test.ts @@ -17,8 +17,10 @@ * under the License. */ -import { ManagementSectionId } from './index'; -import { ManagementSectionsService } from './management_sections_service'; +import { + ManagementSectionsService, + getSectionsServiceStartPrivate, +} from './management_sections_service'; describe('ManagementService', () => { let managementService: ManagementSectionsService; @@ -35,15 +37,10 @@ describe('ManagementService', () => { test('Provides default sections', () => { managementService.setup(); - const start = managementService.start({ capabilities }); - - expect(start.getAllSections().length).toEqual(6); - expect(start.getSection(ManagementSectionId.Ingest)).toBeDefined(); - expect(start.getSection(ManagementSectionId.Data)).toBeDefined(); - expect(start.getSection(ManagementSectionId.InsightsAndAlerting)).toBeDefined(); - expect(start.getSection(ManagementSectionId.Security)).toBeDefined(); - expect(start.getSection(ManagementSectionId.Kibana)).toBeDefined(); - expect(start.getSection(ManagementSectionId.Stack)).toBeDefined(); + managementService.start({ capabilities }); + const start = getSectionsServiceStartPrivate(); + + expect(start.getSectionsEnabled().length).toEqual(6); }); test('Register section, enable and disable', () => { @@ -51,10 +48,11 @@ describe('ManagementService', () => { const setup = managementService.setup(); const testSection = setup.register({ id: 'test-section', title: 'Test Section' }); - expect(setup.getSection('test-section')).not.toBeUndefined(); + expect(testSection).not.toBeUndefined(); // Start phase: - const start = managementService.start({ capabilities }); + managementService.start({ capabilities }); + const start = getSectionsServiceStartPrivate(); expect(start.getSectionsEnabled().length).toEqual(7); @@ -71,7 +69,7 @@ describe('ManagementService', () => { testSection.registerApp({ id: 'test-app-2', title: 'Test App 2', mount: jest.fn() }); testSection.registerApp({ id: 'test-app-3', title: 'Test App 3', mount: jest.fn() }); - expect(setup.getSection('test-section')).not.toBeUndefined(); + expect(testSection).not.toBeUndefined(); // Start phase: managementService.start({ diff --git a/src/plugins/management/public/management_sections_service.ts b/src/plugins/management/public/management_sections_service.ts index d8d148a9247ff..b9dc2dd416d9a 100644 --- a/src/plugins/management/public/management_sections_service.ts +++ b/src/plugins/management/public/management_sections_service.ts @@ -17,22 +17,47 @@ * under the License. */ -import { ReactElement } from 'react'; import { ManagementSection, RegisterManagementSectionArgs } from './utils'; -import { managementSections } from './components/management_sections'; +import { + IngestSection, + DataSection, + InsightsAndAlertingSection, + SecuritySection, + KibanaSection, + StackSection, +} from './components/management_sections'; import { ManagementSectionId, SectionsServiceSetup, - SectionsServiceStart, SectionsServiceStartDeps, + DefinedSections, + ManagementSectionsStartPrivate, } from './types'; +import { createGetterSetter } from '../../kibana_utils/public'; + +const [getSectionsServiceStartPrivate, setSectionsServiceStartPrivate] = createGetterSetter< + ManagementSectionsStartPrivate +>('SectionsServiceStartPrivate'); + +export { getSectionsServiceStartPrivate }; export class ManagementSectionsService { - private sections: Map = new Map(); + definedSections: DefinedSections; - private getSection = (sectionId: ManagementSectionId | string) => - this.sections.get(sectionId) as ManagementSection; + constructor() { + // Note on adding sections - sections can be defined in a plugin and exported as a contract + // It is not necessary to define all sections here, although we've chose to do it for discovery reasons. + this.definedSections = { + ingest: this.registerSection(IngestSection), + data: this.registerSection(DataSection), + insightsAndAlerting: this.registerSection(InsightsAndAlertingSection), + security: this.registerSection(SecuritySection), + kibana: this.registerSection(KibanaSection), + stack: this.registerSection(StackSection), + }; + } + private sections: Map = new Map(); private getAllSections = () => [...this.sections.values()]; @@ -48,19 +73,15 @@ export class ManagementSectionsService { }; setup(): SectionsServiceSetup { - managementSections.forEach( - ({ id, title }: { id: ManagementSectionId; title: ReactElement }, idx: number) => { - this.registerSection({ id, title, order: idx }); - } - ); - return { register: this.registerSection, - getSection: this.getSection, + section: { + ...this.definedSections, + }, }; } - start({ capabilities }: SectionsServiceStartDeps): SectionsServiceStart { + start({ capabilities }: SectionsServiceStartDeps) { this.getAllSections().forEach((section) => { if (capabilities.management.hasOwnProperty(section.id)) { const sectionCapabilities = capabilities.management[section.id]; @@ -72,10 +93,10 @@ export class ManagementSectionsService { } }); - return { - getSection: this.getSection, - getAllSections: this.getAllSections, + setSectionsServiceStartPrivate({ getSectionsEnabled: () => this.getAllSections().filter((section) => section.enabled), - }; + }); + + return {}; } } diff --git a/src/plugins/management/public/mocks/index.ts b/src/plugins/management/public/mocks/index.ts index 123e3f28877aa..fbb37647dad90 100644 --- a/src/plugins/management/public/mocks/index.ts +++ b/src/plugins/management/public/mocks/index.ts @@ -17,10 +17,10 @@ * under the License. */ -import { ManagementSetup, ManagementStart } from '../types'; +import { ManagementSetup, ManagementStart, DefinedSections } from '../types'; import { ManagementSection } from '../index'; -const createManagementSectionMock = () => +export const createManagementSectionMock = () => (({ disable: jest.fn(), enable: jest.fn(), @@ -29,19 +29,22 @@ const createManagementSectionMock = () => getEnabledItems: jest.fn().mockReturnValue([]), } as unknown) as ManagementSection); -const createSetupContract = (): DeeplyMockedKeys => ({ +const createSetupContract = (): ManagementSetup => ({ sections: { - register: jest.fn(), - getSection: jest.fn().mockReturnValue(createManagementSectionMock()), + register: jest.fn(() => createManagementSectionMock()), + section: ({ + ingest: createManagementSectionMock(), + data: createManagementSectionMock(), + insightsAndAlerting: createManagementSectionMock(), + security: createManagementSectionMock(), + kibana: createManagementSectionMock(), + stack: createManagementSectionMock(), + } as unknown) as DefinedSections, }, }); -const createStartContract = (): DeeplyMockedKeys => ({ - sections: { - getSection: jest.fn(), - getAllSections: jest.fn(), - getSectionsEnabled: jest.fn(), - }, +const createStartContract = (): ManagementStart => ({ + sections: {}, }); export const managementPluginMock = { diff --git a/src/plugins/management/public/plugin.ts b/src/plugins/management/public/plugin.ts index dada4636e6add..17d8cb4adc701 100644 --- a/src/plugins/management/public/plugin.ts +++ b/src/plugins/management/public/plugin.ts @@ -26,9 +26,13 @@ import { Plugin, DEFAULT_APP_CATEGORIES, PluginInitializerContext, + AppMountParameters, } from '../../../core/public'; -import { ManagementSectionsService } from './management_sections_service'; +import { + ManagementSectionsService, + getSectionsServiceStartPrivate, +} from './management_sections_service'; interface ManagementSetupDependencies { home: HomePublicPluginSetup; @@ -64,13 +68,14 @@ export class ManagementPlugin implements Plugin ManagementSection[]; } export interface SectionsServiceStartDeps { @@ -36,12 +47,10 @@ export interface SectionsServiceStartDeps { export interface SectionsServiceSetup { register: (args: Omit) => ManagementSection; - getSection: (sectionId: ManagementSectionId | string) => ManagementSection; + section: DefinedSections; } export interface SectionsServiceStart { - getSection: (sectionId: ManagementSectionId | string) => ManagementSection; - getAllSections: () => ManagementSection[]; getSectionsEnabled: () => ManagementSection[]; } @@ -66,7 +75,8 @@ export interface ManagementAppMountParams { export interface CreateManagementItemArgs { id: string; - title: string | ReactElement; + title: string; + tip?: string; order?: number; euiIconType?: string; // takes precedence over `icon` property. icon?: string; // URL to image file; fallback if no `euiIconType` diff --git a/src/plugins/management/public/utils/management_item.ts b/src/plugins/management/public/utils/management_item.ts index ef0c8e4693895..e6e473c77bf61 100644 --- a/src/plugins/management/public/utils/management_item.ts +++ b/src/plugins/management/public/utils/management_item.ts @@ -16,21 +16,22 @@ * specific language governing permissions and limitations * under the License. */ -import { ReactElement } from 'react'; import { CreateManagementItemArgs } from '../types'; export class ManagementItem { public readonly id: string = ''; - public readonly title: string | ReactElement = ''; + public readonly title: string; + public readonly tip?: string; public readonly order: number; public readonly euiIconType?: string; public readonly icon?: string; public enabled: boolean = true; - constructor({ id, title, order = 100, euiIconType, icon }: CreateManagementItemArgs) { + constructor({ id, title, tip, order = 100, euiIconType, icon }: CreateManagementItemArgs) { this.id = id; this.title = title; + this.tip = tip; this.order = order; this.euiIconType = euiIconType; this.icon = icon; diff --git a/src/plugins/saved_objects/public/index.ts b/src/plugins/saved_objects/public/index.ts index 4f7a4ff7f196f..9140de316605c 100644 --- a/src/plugins/saved_objects/public/index.ts +++ b/src/plugins/saved_objects/public/index.ts @@ -36,6 +36,7 @@ export { isErrorNonFatal, } from './saved_object'; export { SavedObjectSaveOpts, SavedObjectKibanaServices, SavedObject } from './types'; +export { PER_PAGE_SETTING, LISTING_LIMIT_SETTING } from '../common'; export { SavedObjectsStart } from './plugin'; export const plugin = () => new SavedObjectsPublicPlugin(); diff --git a/src/plugins/saved_objects_management/public/plugin.ts b/src/plugins/saved_objects_management/public/plugin.ts index f3d6318db89f2..47d445e63b942 100644 --- a/src/plugins/saved_objects_management/public/plugin.ts +++ b/src/plugins/saved_objects_management/public/plugin.ts @@ -19,7 +19,7 @@ import { i18n } from '@kbn/i18n'; import { CoreSetup, CoreStart, Plugin } from 'src/core/public'; -import { ManagementSetup, ManagementSectionId } from '../../management/public'; +import { ManagementSetup } from '../../management/public'; import { DataPublicPluginStart } from '../../data/public'; import { DashboardStart } from '../../dashboard/public'; import { DiscoverStart } from '../../discover/public'; @@ -87,7 +87,7 @@ export class SavedObjectsManagementPlugin category: FeatureCatalogueCategory.ADMIN, }); - const kibanaSection = management.sections.getSection(ManagementSectionId.Kibana); + const kibanaSection = management.sections.section.kibana; kibanaSection.registerApp({ id: 'objects', title: i18n.translate('savedObjectsManagement.managementSectionLabel', { diff --git a/src/plugins/telemetry_collection_manager/server/plugin.ts b/src/plugins/telemetry_collection_manager/server/plugin.ts index c3db0ca39e6ac..051bb3a11cb16 100644 --- a/src/plugins/telemetry_collection_manager/server/plugin.ts +++ b/src/plugins/telemetry_collection_manager/server/plugin.ts @@ -37,7 +37,7 @@ import { UsageStatsPayload, StatsCollectionContext, } from './types'; - +import { isClusterOptedIn } from './util'; import { encryptTelemetry } from './encryption'; interface TelemetryCollectionPluginsDepsSetup { @@ -205,7 +205,9 @@ export class TelemetryCollectionManagerPlugin return usageData; } - return encryptTelemetry(usageData, { useProdKey: this.isDistributable }); + return encryptTelemetry(usageData.filter(isClusterOptedIn), { + useProdKey: this.isDistributable, + }); } } catch (err) { this.logger.debug( diff --git a/src/plugins/telemetry_collection_manager/server/util.test.ts b/src/plugins/telemetry_collection_manager/server/util.test.ts new file mode 100644 index 0000000000000..ba5d999c3bf9a --- /dev/null +++ b/src/plugins/telemetry_collection_manager/server/util.test.ts @@ -0,0 +1,51 @@ +/* + * 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 { isClusterOptedIn } from './util'; + +const createMockClusterUsage = (plugins: any) => { + return { + stack_stats: { + kibana: { plugins }, + }, + }; +}; + +describe('isClusterOptedIn', () => { + it('returns true if cluster has opt_in_status: true', () => { + const mockClusterUsage = createMockClusterUsage({ telemetry: { opt_in_status: true } }); + const result = isClusterOptedIn(mockClusterUsage); + expect(result).toBe(true); + }); + it('returns false if cluster has opt_in_status: false', () => { + const mockClusterUsage = createMockClusterUsage({ telemetry: { opt_in_status: false } }); + const result = isClusterOptedIn(mockClusterUsage); + expect(result).toBe(false); + }); + it('returns false if cluster has opt_in_status: undefined', () => { + const mockClusterUsage = createMockClusterUsage({ telemetry: {} }); + const result = isClusterOptedIn(mockClusterUsage); + expect(result).toBe(false); + }); + it('returns false if cluster stats is malformed', () => { + expect(isClusterOptedIn(createMockClusterUsage({}))).toBe(false); + expect(isClusterOptedIn({})).toBe(false); + expect(isClusterOptedIn(undefined)).toBe(false); + }); +}); diff --git a/scripts/page_load_metrics.js b/src/plugins/telemetry_collection_manager/server/util.ts similarity index 83% rename from scripts/page_load_metrics.js rename to src/plugins/telemetry_collection_manager/server/util.ts index 37500c26e0b20..d6e1b51663688 100644 --- a/scripts/page_load_metrics.js +++ b/src/plugins/telemetry_collection_manager/server/util.ts @@ -17,5 +17,6 @@ * under the License. */ -require('../src/setup_node_env'); -require('@kbn/test').runPageLoadMetricsCli(); +export const isClusterOptedIn = (clusterUsage: any): boolean => { + return clusterUsage?.stack_stats?.kibana?.plugins?.telemetry?.opt_in_status === true; +}; diff --git a/src/plugins/tile_map/public/plugin.ts b/src/plugins/tile_map/public/plugin.ts index 1f79104b183ee..4582cd2283dc1 100644 --- a/src/plugins/tile_map/public/plugin.ts +++ b/src/plugins/tile_map/public/plugin.ts @@ -32,7 +32,7 @@ import 'angular-sanitize'; import { createTileMapFn } from './tile_map_fn'; // @ts-ignore import { createTileMapTypeDefinition } from './tile_map_type'; -import { MapsLegacyPluginSetup } from '../../maps_legacy/public'; +import { IServiceSettings, MapsLegacyPluginSetup } from '../../maps_legacy/public'; import { DataPublicPluginStart } from '../../data/public'; import { setFormatService, setQueryService } from './services'; import { setKibanaLegacy } from './services'; @@ -48,6 +48,7 @@ interface TileMapVisualizationDependencies { getZoomPrecision: any; getPrecision: any; BaseMapsVisualization: any; + serviceSettings: IServiceSettings; } /** @internal */ @@ -81,12 +82,13 @@ export class TileMapPlugin implements Plugin = { getZoomPrecision, getPrecision, BaseMapsVisualization: mapsLegacy.getBaseMapsVis(), uiSettings: core.uiSettings, + serviceSettings, }; expressions.registerFunction(() => createTileMapFn(visualizationDependencies)); diff --git a/src/plugins/timelion/kibana.json b/src/plugins/timelion/kibana.json index 55e492e8f23cd..d8c709d867a3f 100644 --- a/src/plugins/timelion/kibana.json +++ b/src/plugins/timelion/kibana.json @@ -1,8 +1,19 @@ { "id": "timelion", - "version": "0.0.1", - "kibanaVersion": "kibana", - "configPath": "timelion", - "ui": false, - "server": true + "version": "kibana", + "ui": true, + "server": true, + "requiredBundles": [ + "kibanaLegacy", + "kibanaUtils", + "savedObjects", + "visTypeTimelion" + ], + "requiredPlugins": [ + "visualizations", + "data", + "navigation", + "visTypeTimelion", + "kibanaLegacy" + ] } diff --git a/src/legacy/core_plugins/timelion/public/_app.scss b/src/plugins/timelion/public/_app.scss similarity index 100% rename from src/legacy/core_plugins/timelion/public/_app.scss rename to src/plugins/timelion/public/_app.scss diff --git a/src/plugins/timelion/public/app.js b/src/plugins/timelion/public/app.js new file mode 100644 index 0000000000000..0294e71084f98 --- /dev/null +++ b/src/plugins/timelion/public/app.js @@ -0,0 +1,661 @@ +/* + * 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 _ from 'lodash'; + +import { i18n } from '@kbn/i18n'; + +import { createHashHistory } from 'history'; + +import { createKbnUrlStateStorage } from '../../kibana_utils/public'; +import { syncQueryStateWithUrl } from '../../data/public'; + +import { getSavedSheetBreadcrumbs, getCreateBreadcrumbs } from './breadcrumbs'; +import { + addFatalError, + registerListenEventListener, + watchMultiDecorator, +} from '../../kibana_legacy/public'; +import { getTimezone } from '../../vis_type_timelion/public'; +import { initCellsDirective } from './directives/cells/cells'; +import { initFullscreenDirective } from './directives/fullscreen/fullscreen'; +import { initFixedElementDirective } from './directives/fixed_element'; +import { initTimelionLoadSheetDirective } from './directives/timelion_load_sheet'; +import { initTimelionHelpDirective } from './directives/timelion_help/timelion_help'; +import { initTimelionSaveSheetDirective } from './directives/timelion_save_sheet'; +import { initTimelionOptionsSheetDirective } from './directives/timelion_options_sheet'; +import { initSavedObjectSaveAsCheckBoxDirective } from './directives/saved_object_save_as_checkbox'; +import { initSavedObjectFinderDirective } from './directives/saved_object_finder'; +import { initTimelionTabsDirective } from './components/timelionhelp_tabs_directive'; +import { initInputFocusDirective } from './directives/input_focus'; +import { Chart } from './directives/chart/chart'; +import { TimelionInterval } from './directives/timelion_interval/timelion_interval'; +import { timelionExpInput } from './directives/timelion_expression_input'; +import { TimelionExpressionSuggestions } from './directives/timelion_expression_suggestions/timelion_expression_suggestions'; +import { initSavedSheetService } from './services/saved_sheets'; +import { initTimelionAppState } from './timelion_app_state'; + +import rootTemplate from './index.html'; + +export function initTimelionApp(app, deps) { + app.run(registerListenEventListener); + + const savedSheetLoader = initSavedSheetService(app, deps); + + app.factory('history', () => createHashHistory()); + app.factory('kbnUrlStateStorage', (history) => + createKbnUrlStateStorage({ + history, + useHash: deps.core.uiSettings.get('state:storeInSessionStorage'), + }) + ); + app.config(watchMultiDecorator); + + app + .controller('TimelionVisController', function ($scope) { + $scope.$on('timelionChartRendered', (event) => { + event.stopPropagation(); + $scope.renderComplete(); + }); + }) + .constant('timelionPanels', deps.timelionPanels) + .directive('chart', Chart) + .directive('timelionInterval', TimelionInterval) + .directive('timelionExpressionSuggestions', TimelionExpressionSuggestions) + .directive('timelionExpressionInput', timelionExpInput(deps)); + + initTimelionHelpDirective(app); + initInputFocusDirective(app); + initTimelionTabsDirective(app, deps); + initSavedObjectFinderDirective(app, savedSheetLoader, deps.core.uiSettings); + initSavedObjectSaveAsCheckBoxDirective(app); + initCellsDirective(app); + initFixedElementDirective(app); + initFullscreenDirective(app); + initTimelionSaveSheetDirective(app); + initTimelionLoadSheetDirective(app); + initTimelionOptionsSheetDirective(app); + + const location = 'Timelion'; + + app.directive('timelionApp', function () { + return { + restrict: 'E', + controllerAs: 'timelionApp', + controller: timelionController, + }; + }); + + function timelionController( + $http, + $route, + $routeParams, + $scope, + $timeout, + history, + kbnUrlStateStorage + ) { + // Keeping this at app scope allows us to keep the current page when the user + // switches to say, the timepicker. + $scope.page = deps.core.uiSettings.get('timelion:showTutorial', true) ? 1 : 0; + $scope.setPage = (page) => ($scope.page = page); + const timefilter = deps.plugins.data.query.timefilter.timefilter; + + timefilter.enableAutoRefreshSelector(); + timefilter.enableTimeRangeSelector(); + + deps.core.chrome.docTitle.change('Timelion - Kibana'); + + // starts syncing `_g` portion of url with query services + const { stop: stopSyncingQueryServiceStateWithUrl } = syncQueryStateWithUrl( + deps.plugins.data.query, + kbnUrlStateStorage + ); + + const savedSheet = $route.current.locals.savedSheet; + + function getStateDefaults() { + return { + sheet: savedSheet.timelion_sheet, + selected: 0, + columns: savedSheet.timelion_columns, + rows: savedSheet.timelion_rows, + interval: savedSheet.timelion_interval, + }; + } + + const { stateContainer, stopStateSync } = initTimelionAppState({ + stateDefaults: getStateDefaults(), + kbnUrlStateStorage, + }); + + $scope.state = _.cloneDeep(stateContainer.getState()); + $scope.expression = _.clone($scope.state.sheet[$scope.state.selected]); + $scope.updatedSheets = []; + + const savedVisualizations = deps.plugins.visualizations.savedVisualizationsLoader; + const timezone = getTimezone(deps.core.uiSettings); + + const defaultExpression = '.es(*)'; + + $scope.topNavMenu = getTopNavMenu(); + + $timeout(function () { + if (deps.core.uiSettings.get('timelion:showTutorial', true)) { + $scope.toggleMenu('showHelp'); + } + }, 0); + + $scope.transient = {}; + + function getTopNavMenu() { + const newSheetAction = { + id: 'new', + label: i18n.translate('timelion.topNavMenu.newSheetButtonLabel', { + defaultMessage: 'New', + }), + description: i18n.translate('timelion.topNavMenu.newSheetButtonAriaLabel', { + defaultMessage: 'New Sheet', + }), + run: function () { + history.push('/'); + $route.reload(); + }, + testId: 'timelionNewButton', + }; + + const addSheetAction = { + id: 'add', + label: i18n.translate('timelion.topNavMenu.addChartButtonLabel', { + defaultMessage: 'Add', + }), + description: i18n.translate('timelion.topNavMenu.addChartButtonAriaLabel', { + defaultMessage: 'Add a chart', + }), + run: function () { + $scope.$evalAsync(() => $scope.newCell()); + }, + testId: 'timelionAddChartButton', + }; + + const saveSheetAction = { + id: 'save', + label: i18n.translate('timelion.topNavMenu.saveSheetButtonLabel', { + defaultMessage: 'Save', + }), + description: i18n.translate('timelion.topNavMenu.saveSheetButtonAriaLabel', { + defaultMessage: 'Save Sheet', + }), + run: () => { + $scope.$evalAsync(() => $scope.toggleMenu('showSave')); + }, + testId: 'timelionSaveButton', + }; + + const deleteSheetAction = { + id: 'delete', + label: i18n.translate('timelion.topNavMenu.deleteSheetButtonLabel', { + defaultMessage: 'Delete', + }), + description: i18n.translate('timelion.topNavMenu.deleteSheetButtonAriaLabel', { + defaultMessage: 'Delete current sheet', + }), + disableButton: function () { + return !savedSheet.id; + }, + run: function () { + const title = savedSheet.title; + function doDelete() { + savedSheet + .delete() + .then(() => { + deps.core.notifications.toasts.addSuccess( + i18n.translate('timelion.topNavMenu.delete.modal.successNotificationText', { + defaultMessage: `Deleted '{title}'`, + values: { title }, + }) + ); + history.push('/'); + }) + .catch((error) => addFatalError(deps.core.fatalErrors, error, location)); + } + + const confirmModalOptions = { + confirmButtonText: i18n.translate( + 'timelion.topNavMenu.delete.modal.confirmButtonLabel', + { + defaultMessage: 'Delete', + } + ), + title: i18n.translate('timelion.topNavMenu.delete.modalTitle', { + defaultMessage: `Delete Timelion sheet '{title}'?`, + values: { title }, + }), + }; + + $scope.$evalAsync(() => { + deps.core.overlays + .openConfirm( + i18n.translate('timelion.topNavMenu.delete.modal.warningText', { + defaultMessage: `You can't recover deleted sheets.`, + }), + confirmModalOptions + ) + .then((isConfirmed) => { + if (isConfirmed) { + doDelete(); + } + }); + }); + }, + testId: 'timelionDeleteButton', + }; + + const openSheetAction = { + id: 'open', + label: i18n.translate('timelion.topNavMenu.openSheetButtonLabel', { + defaultMessage: 'Open', + }), + description: i18n.translate('timelion.topNavMenu.openSheetButtonAriaLabel', { + defaultMessage: 'Open Sheet', + }), + run: () => { + $scope.$evalAsync(() => $scope.toggleMenu('showLoad')); + }, + testId: 'timelionOpenButton', + }; + + const optionsAction = { + id: 'options', + label: i18n.translate('timelion.topNavMenu.optionsButtonLabel', { + defaultMessage: 'Options', + }), + description: i18n.translate('timelion.topNavMenu.optionsButtonAriaLabel', { + defaultMessage: 'Options', + }), + run: () => { + $scope.$evalAsync(() => $scope.toggleMenu('showOptions')); + }, + testId: 'timelionOptionsButton', + }; + + const helpAction = { + id: 'help', + label: i18n.translate('timelion.topNavMenu.helpButtonLabel', { + defaultMessage: 'Help', + }), + description: i18n.translate('timelion.topNavMenu.helpButtonAriaLabel', { + defaultMessage: 'Help', + }), + run: () => { + $scope.$evalAsync(() => $scope.toggleMenu('showHelp')); + }, + testId: 'timelionDocsButton', + }; + + if (deps.core.application.capabilities.timelion.save) { + return [ + newSheetAction, + addSheetAction, + saveSheetAction, + deleteSheetAction, + openSheetAction, + optionsAction, + helpAction, + ]; + } + return [newSheetAction, addSheetAction, openSheetAction, optionsAction, helpAction]; + } + + let refresher; + const setRefreshData = function () { + if (refresher) $timeout.cancel(refresher); + const interval = timefilter.getRefreshInterval(); + if (interval.value > 0 && !interval.pause) { + function startRefresh() { + refresher = $timeout(function () { + if (!$scope.running) $scope.search(); + startRefresh(); + }, interval.value); + } + startRefresh(); + } + }; + + const init = function () { + $scope.running = false; + $scope.search(); + setRefreshData(); + + $scope.model = { + timeRange: timefilter.getTime(), + refreshInterval: timefilter.getRefreshInterval(), + }; + + const unsubscribeStateUpdates = stateContainer.subscribe((state) => { + const clonedState = _.cloneDeep(state); + $scope.updatedSheets.forEach((updatedSheet) => { + clonedState.sheet[updatedSheet.id] = updatedSheet.expression; + }); + $scope.state = clonedState; + $scope.opts.state = clonedState; + $scope.expression = _.clone($scope.state.sheet[$scope.state.selected]); + $scope.search(); + }); + + timefilter.getFetch$().subscribe($scope.search); + + $scope.opts = { + saveExpression: saveExpression, + saveSheet: saveSheet, + savedSheet: savedSheet, + state: _.cloneDeep(stateContainer.getState()), + search: $scope.search, + dontShowHelp: function () { + deps.core.uiSettings.set('timelion:showTutorial', false); + $scope.setPage(0); + $scope.closeMenus(); + }, + }; + + $scope.$watch('opts.state.rows', function (newRow) { + const state = stateContainer.getState(); + if (state.rows !== newRow) { + stateContainer.transitions.set('rows', newRow); + } + }); + + $scope.$watch('opts.state.columns', function (newColumn) { + const state = stateContainer.getState(); + if (state.columns !== newColumn) { + stateContainer.transitions.set('columns', newColumn); + } + }); + + $scope.menus = { + showHelp: false, + showSave: false, + showLoad: false, + showOptions: false, + }; + + $scope.toggleMenu = (menuName) => { + const curState = $scope.menus[menuName]; + $scope.closeMenus(); + $scope.menus[menuName] = !curState; + }; + + $scope.closeMenus = () => { + _.forOwn($scope.menus, function (value, key) { + $scope.menus[key] = false; + }); + }; + + $scope.$on('$destroy', () => { + stopSyncingQueryServiceStateWithUrl(); + unsubscribeStateUpdates(); + stopStateSync(); + }); + }; + + $scope.onTimeUpdate = function ({ dateRange }) { + $scope.model.timeRange = { + ...dateRange, + }; + timefilter.setTime(dateRange); + if (!$scope.running) $scope.search(); + }; + + $scope.onRefreshChange = function ({ isPaused, refreshInterval }) { + $scope.model.refreshInterval = { + pause: isPaused, + value: refreshInterval, + }; + timefilter.setRefreshInterval({ + pause: isPaused, + value: refreshInterval ? refreshInterval : $scope.refreshInterval.value, + }); + + setRefreshData(); + }; + + $scope.$watch( + function () { + return savedSheet.lastSavedTitle; + }, + function (newTitle) { + if (savedSheet.id && newTitle) { + deps.core.chrome.docTitle.change(newTitle); + } + } + ); + + $scope.$watch('expression', function (newExpression) { + const state = stateContainer.getState(); + if (state.sheet[state.selected] !== newExpression) { + const updatedSheet = $scope.updatedSheets.find( + (updatedSheet) => updatedSheet.id === state.selected + ); + if (updatedSheet) { + updatedSheet.expression = newExpression; + } else { + $scope.updatedSheets.push({ + id: state.selected, + expression: newExpression, + }); + } + } + }); + + $scope.toggle = function (property) { + $scope[property] = !$scope[property]; + }; + + $scope.changeInterval = function (interval) { + $scope.currentInterval = interval; + }; + + $scope.updateChart = function () { + const state = stateContainer.getState(); + const newSheet = _.clone(state.sheet); + if ($scope.updatedSheets.length) { + $scope.updatedSheets.forEach((updatedSheet) => { + newSheet[updatedSheet.id] = updatedSheet.expression; + }); + $scope.updatedSheets = []; + } + stateContainer.transitions.updateState({ + interval: $scope.currentInterval ? $scope.currentInterval : state.interval, + sheet: newSheet, + }); + }; + + $scope.newSheet = function () { + history.push('/'); + }; + + $scope.removeSheet = function (removedIndex) { + const state = stateContainer.getState(); + const newSheet = state.sheet.filter((el, index) => index !== removedIndex); + $scope.updatedSheets = $scope.updatedSheets.filter((el) => el.id !== removedIndex); + stateContainer.transitions.updateState({ + sheet: newSheet, + selected: removedIndex ? removedIndex - 1 : removedIndex, + }); + }; + + $scope.newCell = function () { + const state = stateContainer.getState(); + const newSheet = [...state.sheet, defaultExpression]; + stateContainer.transitions.updateState({ sheet: newSheet, selected: newSheet.length - 1 }); + }; + + $scope.setActiveCell = function (cell) { + const state = stateContainer.getState(); + if (state.selected !== cell) { + stateContainer.transitions.updateState({ sheet: $scope.state.sheet, selected: cell }); + } + }; + + $scope.search = function () { + $scope.running = true; + const state = stateContainer.getState(); + + // parse the time range client side to make sure it behaves like other charts + const timeRangeBounds = timefilter.getBounds(); + + const httpResult = $http + .post('../api/timelion/run', { + sheet: state.sheet, + time: _.assignIn( + { + from: timeRangeBounds.min, + to: timeRangeBounds.max, + }, + { + interval: state.interval, + timezone: timezone, + } + ), + }) + .then((resp) => resp.data) + .catch((resp) => { + throw resp.data; + }); + + httpResult + .then(function (resp) { + $scope.stats = resp.stats; + $scope.sheet = resp.sheet; + _.forEach(resp.sheet, function (cell) { + if (cell.exception && cell.plot !== state.selected) { + stateContainer.transitions.set('selected', cell.plot); + } + }); + $scope.running = false; + }) + .catch(function (resp) { + $scope.sheet = []; + $scope.running = false; + + const err = new Error(resp.message); + err.stack = resp.stack; + deps.core.notifications.toasts.addError(err, { + title: i18n.translate('timelion.searchErrorTitle', { + defaultMessage: 'Timelion request error', + }), + }); + }); + }; + + $scope.safeSearch = _.debounce($scope.search, 500); + + function saveSheet() { + const state = stateContainer.getState(); + savedSheet.timelion_sheet = state.sheet; + savedSheet.timelion_interval = state.interval; + savedSheet.timelion_columns = state.columns; + savedSheet.timelion_rows = state.rows; + savedSheet.save().then(function (id) { + if (id) { + deps.core.notifications.toasts.addSuccess({ + title: i18n.translate('timelion.saveSheet.successNotificationText', { + defaultMessage: `Saved sheet '{title}'`, + values: { title: savedSheet.title }, + }), + 'data-test-subj': 'timelionSaveSuccessToast', + }); + + if (savedSheet.id !== $routeParams.id) { + history.push(`/${savedSheet.id}`); + } + } + }); + } + + async function saveExpression(title) { + const vis = await deps.plugins.visualizations.createVis('timelion', { + title, + params: { + expression: $scope.state.sheet[$scope.state.selected], + interval: $scope.state.interval, + }, + }); + const state = deps.plugins.visualizations.convertFromSerializedVis(vis.serialize()); + const visSavedObject = await savedVisualizations.get(); + Object.assign(visSavedObject, state); + const id = await visSavedObject.save(); + if (id) { + deps.core.notifications.toasts.addSuccess( + i18n.translate('timelion.saveExpression.successNotificationText', { + defaultMessage: `Saved expression '{title}'`, + values: { title: state.title }, + }) + ); + } + } + + init(); + } + + app.config(function ($routeProvider) { + $routeProvider + .when('/:id?', { + template: rootTemplate, + reloadOnSearch: false, + k7Breadcrumbs: ($injector, $route) => + $injector.invoke( + $route.current.params.id ? getSavedSheetBreadcrumbs : getCreateBreadcrumbs + ), + badge: () => { + if (deps.core.application.capabilities.timelion.save) { + return undefined; + } + + return { + text: i18n.translate('timelion.badge.readOnly.text', { + defaultMessage: 'Read only', + }), + tooltip: i18n.translate('timelion.badge.readOnly.tooltip', { + defaultMessage: 'Unable to save Timelion sheets', + }), + iconType: 'glasses', + }; + }, + resolve: { + savedSheet: function (savedSheets, $route) { + return savedSheets + .get($route.current.params.id) + .then((savedSheet) => { + if ($route.current.params.id) { + deps.core.chrome.recentlyAccessed.add( + savedSheet.getFullPath(), + savedSheet.title, + savedSheet.id + ); + } + return savedSheet; + }) + .catch(); + }, + }, + }) + .otherwise('/'); + }); +} diff --git a/src/plugins/timelion/public/application.ts b/src/plugins/timelion/public/application.ts new file mode 100644 index 0000000000000..a398106d56f58 --- /dev/null +++ b/src/plugins/timelion/public/application.ts @@ -0,0 +1,153 @@ +/* + * 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 './index.scss'; + +import { EuiIcon } from '@elastic/eui'; +import angular, { IModule } from 'angular'; +// required for `ngSanitize` angular module +import 'angular-sanitize'; +// required for ngRoute +import 'angular-route'; +import 'angular-sortable-view'; +import { i18nDirective, i18nFilter, I18nProvider } from '@kbn/i18n/angular'; +import { + IUiSettingsClient, + CoreStart, + PluginInitializerContext, + AppMountParameters, +} from 'kibana/public'; +import { getTimeChart } from './panels/timechart/timechart'; +import { Panel } from './panels/panel'; + +import { + configureAppAngularModule, + createTopNavDirective, + createTopNavHelper, +} from '../../kibana_legacy/public'; +import { TimelionPluginDependencies } from './plugin'; +import { DataPublicPluginStart } from '../../data/public'; +// @ts-ignore +import { initTimelionApp } from './app'; + +export interface RenderDeps { + pluginInitializerContext: PluginInitializerContext; + mountParams: AppMountParameters; + core: CoreStart; + plugins: TimelionPluginDependencies; + timelionPanels: Map; +} + +export interface TimelionVisualizationDependencies { + uiSettings: IUiSettingsClient; + timelionPanels: Map; + data: DataPublicPluginStart; + $rootScope: any; + $compile: any; +} + +let angularModuleInstance: IModule | null = null; + +export const renderApp = (deps: RenderDeps) => { + if (!angularModuleInstance) { + angularModuleInstance = createLocalAngularModule(deps); + // global routing stuff + configureAppAngularModule( + angularModuleInstance, + { core: deps.core, env: deps.pluginInitializerContext.env }, + true + ); + initTimelionApp(angularModuleInstance, deps); + } + + const $injector = mountTimelionApp(deps.mountParams.appBasePath, deps.mountParams.element, deps); + + return () => { + $injector.get('$rootScope').$destroy(); + }; +}; + +function registerPanels(dependencies: TimelionVisualizationDependencies) { + const timeChartPanel: Panel = getTimeChart(dependencies); + + dependencies.timelionPanels.set(timeChartPanel.name, timeChartPanel); +} + +const mainTemplate = (basePath: string) => `
+ +
`; + +const moduleName = 'app/timelion'; + +const thirdPartyAngularDependencies = ['ngSanitize', 'ngRoute', 'react', 'angular-sortable-view']; + +function mountTimelionApp(appBasePath: string, element: HTMLElement, deps: RenderDeps) { + const mountpoint = document.createElement('div'); + mountpoint.setAttribute('class', 'timelionAppContainer'); + // eslint-disable-next-line + mountpoint.innerHTML = mainTemplate(appBasePath); + // bootstrap angular into detached element and attach it later to + // make angular-within-angular possible + const $injector = angular.bootstrap(mountpoint, [moduleName]); + + registerPanels({ + uiSettings: deps.core.uiSettings, + timelionPanels: deps.timelionPanels, + data: deps.plugins.data, + $rootScope: $injector.get('$rootScope'), + $compile: $injector.get('$compile'), + }); + element.appendChild(mountpoint); + return $injector; +} + +function createLocalAngularModule(deps: RenderDeps) { + createLocalI18nModule(); + createLocalIconModule(); + createLocalTopNavModule(deps.plugins.navigation); + + const dashboardAngularModule = angular.module(moduleName, [ + ...thirdPartyAngularDependencies, + 'app/timelion/TopNav', + 'app/timelion/I18n', + 'app/timelion/icon', + ]); + return dashboardAngularModule; +} + +function createLocalIconModule() { + angular + .module('app/timelion/icon', ['react']) + .directive('icon', (reactDirective) => reactDirective(EuiIcon)); +} + +function createLocalTopNavModule(navigation: TimelionPluginDependencies['navigation']) { + angular + .module('app/timelion/TopNav', ['react']) + .directive('kbnTopNav', createTopNavDirective) + .directive('kbnTopNavHelper', createTopNavHelper(navigation.ui)); +} + +function createLocalI18nModule() { + angular + .module('app/timelion/I18n', []) + .provider('i18n', I18nProvider) + .filter('i18n', i18nFilter) + .directive('i18nId', i18nDirective); +} diff --git a/src/legacy/core_plugins/timelion/public/breadcrumbs.js b/src/plugins/timelion/public/breadcrumbs.js similarity index 100% rename from src/legacy/core_plugins/timelion/public/breadcrumbs.js rename to src/plugins/timelion/public/breadcrumbs.js diff --git a/src/legacy/core_plugins/timelion/public/components/timelionhelp_tabs.js b/src/plugins/timelion/public/components/timelionhelp_tabs.js similarity index 95% rename from src/legacy/core_plugins/timelion/public/components/timelionhelp_tabs.js rename to src/plugins/timelion/public/components/timelionhelp_tabs.js index 639bd7d65a19e..7939afce412e1 100644 --- a/src/legacy/core_plugins/timelion/public/components/timelionhelp_tabs.js +++ b/src/plugins/timelion/public/components/timelionhelp_tabs.js @@ -54,6 +54,6 @@ export function TimelionHelpTabs(props) { } TimelionHelpTabs.propTypes = { - activeTab: PropTypes.string.isRequired, - activateTab: PropTypes.func.isRequired, + activeTab: PropTypes.string, + activateTab: PropTypes.func, }; diff --git a/src/plugins/timelion/public/components/timelionhelp_tabs_directive.js b/src/plugins/timelion/public/components/timelionhelp_tabs_directive.js new file mode 100644 index 0000000000000..67e0d595314f6 --- /dev/null +++ b/src/plugins/timelion/public/components/timelionhelp_tabs_directive.js @@ -0,0 +1,43 @@ +/* + * 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 React from 'react'; +import { TimelionHelpTabs } from './timelionhelp_tabs'; + +export function initTimelionTabsDirective(app, deps) { + app.directive('timelionHelpTabs', function (reactDirective) { + return reactDirective( + (props) => { + return ( + + + + ); + }, + [['activeTab'], ['activateTab', { watchDepth: 'reference' }]], + { + restrict: 'E', + scope: { + activeTab: '=', + activateTab: '=', + }, + } + ); + }); +} diff --git a/src/legacy/core_plugins/timelion/public/directives/_index.scss b/src/plugins/timelion/public/directives/_index.scss similarity index 100% rename from src/legacy/core_plugins/timelion/public/directives/_index.scss rename to src/plugins/timelion/public/directives/_index.scss diff --git a/src/legacy/core_plugins/timelion/public/directives/_timelion_expression_input.scss b/src/plugins/timelion/public/directives/_timelion_expression_input.scss similarity index 100% rename from src/legacy/core_plugins/timelion/public/directives/_timelion_expression_input.scss rename to src/plugins/timelion/public/directives/_timelion_expression_input.scss diff --git a/src/legacy/core_plugins/timelion/public/directives/cells/_cells.scss b/src/plugins/timelion/public/directives/cells/_cells.scss similarity index 100% rename from src/legacy/core_plugins/timelion/public/directives/cells/_cells.scss rename to src/plugins/timelion/public/directives/cells/_cells.scss diff --git a/src/legacy/core_plugins/timelion/public/directives/cells/_index.scss b/src/plugins/timelion/public/directives/cells/_index.scss similarity index 100% rename from src/legacy/core_plugins/timelion/public/directives/cells/_index.scss rename to src/plugins/timelion/public/directives/cells/_index.scss diff --git a/src/legacy/core_plugins/timelion/public/directives/cells/cells.html b/src/plugins/timelion/public/directives/cells/cells.html similarity index 100% rename from src/legacy/core_plugins/timelion/public/directives/cells/cells.html rename to src/plugins/timelion/public/directives/cells/cells.html diff --git a/src/legacy/core_plugins/timelion/public/shim/legacy_dependencies_plugin.ts b/src/plugins/timelion/public/directives/cells/cells.js similarity index 50% rename from src/legacy/core_plugins/timelion/public/shim/legacy_dependencies_plugin.ts rename to src/plugins/timelion/public/directives/cells/cells.js index f6c329d417f2b..36a1e80dd470e 100644 --- a/src/legacy/core_plugins/timelion/public/shim/legacy_dependencies_plugin.ts +++ b/src/plugins/timelion/public/directives/cells/cells.js @@ -17,31 +17,36 @@ * under the License. */ -import chrome from 'ui/chrome'; -import { CoreSetup, Plugin } from 'kibana/public'; -import { initTimelionLegacyModule } from './timelion_legacy_module'; -import { Panel } from '../panels/panel'; +import { move } from './collection'; +import { initTimelionGridDirective } from '../timelion_grid'; -/** @internal */ -export interface LegacyDependenciesPluginSetup { - $rootScope: any; - $compile: any; -} - -export class LegacyDependenciesPlugin - implements Plugin, void> { - public async setup(core: CoreSetup, timelionPanels: Map) { - initTimelionLegacyModule(timelionPanels); +import html from './cells.html'; - const $injector = await chrome.dangerouslyGetActiveInjector(); +export function initCellsDirective(app) { + initTimelionGridDirective(app); + app.directive('timelionCells', function () { return { - $rootScope: $injector.get('$rootScope'), - $compile: $injector.get('$compile'), - } as LegacyDependenciesPluginSetup; - } + restrict: 'E', + scope: { + sheet: '=', + state: '=', + transient: '=', + onSearch: '=', + onSelect: '=', + onRemoveSheet: '=', + }, + template: html, + link: function ($scope) { + $scope.removeCell = function (index) { + $scope.onRemoveSheet(index); + }; - public start() { - // nothing to do here yet - } + $scope.dropCell = function (item, partFrom, partTo, indexFrom, indexTo) { + move($scope.sheet, indexFrom, indexTo); + $scope.onSelect(indexTo); + }; + }, + }; + }); } diff --git a/src/plugins/timelion/public/directives/cells/collection.ts b/src/plugins/timelion/public/directives/cells/collection.ts new file mode 100644 index 0000000000000..b882a2bbe6e5b --- /dev/null +++ b/src/plugins/timelion/public/directives/cells/collection.ts @@ -0,0 +1,76 @@ +/* + * 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 _ from 'lodash'; + +/** + * move an obj either up or down in the collection by + * injecting it either before/after the prev/next obj that + * satisfied the qualifier + * + * or, just from one index to another... + * + * @param {array} objs - the list to move the object within + * @param {number|any} obj - the object that should be moved, or the index that the object is currently at + * @param {number|boolean} below - the index to move the object to, or whether it should be moved up or down + * @param {function} qualifier - a lodash-y callback, object = _.where, string = _.pluck + * @return {array} - the objs argument + */ +export function move( + objs: any[], + obj: object | number, + below: number | boolean, + qualifier?: ((object: object, index: number) => any) | Record | string +): object[] { + const origI = _.isNumber(obj) ? obj : objs.indexOf(obj); + if (origI === -1) { + return objs; + } + + if (_.isNumber(below)) { + // move to a specific index + objs.splice(below, 0, objs.splice(origI, 1)[0]); + return objs; + } + + below = !!below; + qualifier = qualifier && _.iteratee(qualifier); + + const above = !below; + const finder = below ? _.findIndex : _.findLastIndex; + + // find the index of the next/previous obj that meets the qualifications + const targetI = finder(objs, (otherAgg, otherI) => { + if (below && otherI <= origI) { + return; + } + if (above && otherI >= origI) { + return; + } + return Boolean(_.isFunction(qualifier) && qualifier(otherAgg, otherI)); + }); + + if (targetI === -1) { + return objs; + } + + // place the obj at it's new index + objs.splice(targetI, 0, objs.splice(origI, 1)[0]); + return objs; +} diff --git a/src/legacy/core_plugins/timelion/public/directives/chart/chart.js b/src/plugins/timelion/public/directives/chart/chart.js similarity index 100% rename from src/legacy/core_plugins/timelion/public/directives/chart/chart.js rename to src/plugins/timelion/public/directives/chart/chart.js diff --git a/src/plugins/timelion/public/directives/fixed_element.js b/src/plugins/timelion/public/directives/fixed_element.js new file mode 100644 index 0000000000000..f57c391e7fcda --- /dev/null +++ b/src/plugins/timelion/public/directives/fixed_element.js @@ -0,0 +1,50 @@ +/* + * 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 $ from 'jquery'; + +export function initFixedElementDirective(app) { + app.directive('fixedElementRoot', function () { + return { + restrict: 'A', + link: function ($elem) { + let fixedAt; + $(window).bind('scroll', function () { + const fixed = $('[fixed-element]', $elem); + const body = $('[fixed-element-body]', $elem); + const top = fixed.offset().top; + + if ($(window).scrollTop() > top) { + // This is a gross hack, but its better than it was. I guess + fixedAt = $(window).scrollTop(); + fixed.addClass(fixed.attr('fixed-element')); + body.addClass(fixed.attr('fixed-element-body')); + body.css({ top: fixed.height() }); + } + + if ($(window).scrollTop() < fixedAt) { + fixed.removeClass(fixed.attr('fixed-element')); + body.removeClass(fixed.attr('fixed-element-body')); + body.removeAttr('style'); + } + }); + }, + }; + }); +} diff --git a/src/legacy/core_plugins/timelion/public/directives/fullscreen/fullscreen.html b/src/plugins/timelion/public/directives/fullscreen/fullscreen.html similarity index 85% rename from src/legacy/core_plugins/timelion/public/directives/fullscreen/fullscreen.html rename to src/plugins/timelion/public/directives/fullscreen/fullscreen.html index 325c7eabb2b03..194596ba79d0e 100644 --- a/src/legacy/core_plugins/timelion/public/directives/fullscreen/fullscreen.html +++ b/src/plugins/timelion/public/directives/fullscreen/fullscreen.html @@ -1,5 +1,5 @@
-
+
diff --git a/src/legacy/core_plugins/timelion/public/index.scss b/src/plugins/timelion/public/index.scss similarity index 100% rename from src/legacy/core_plugins/timelion/public/index.scss rename to src/plugins/timelion/public/index.scss diff --git a/src/legacy/core_plugins/timelion/public/index.ts b/src/plugins/timelion/public/index.ts similarity index 100% rename from src/legacy/core_plugins/timelion/public/index.ts rename to src/plugins/timelion/public/index.ts diff --git a/src/legacy/core_plugins/timelion/public/lib/observe_resize.js b/src/plugins/timelion/public/lib/observe_resize.js similarity index 100% rename from src/legacy/core_plugins/timelion/public/lib/observe_resize.js rename to src/plugins/timelion/public/lib/observe_resize.js diff --git a/src/legacy/core_plugins/timelion/public/panels/panel.ts b/src/plugins/timelion/public/panels/panel.ts similarity index 100% rename from src/legacy/core_plugins/timelion/public/panels/panel.ts rename to src/plugins/timelion/public/panels/panel.ts diff --git a/src/legacy/core_plugins/timelion/public/panels/timechart/schema.ts b/src/plugins/timelion/public/panels/timechart/schema.ts similarity index 93% rename from src/legacy/core_plugins/timelion/public/panels/timechart/schema.ts rename to src/plugins/timelion/public/panels/timechart/schema.ts index 087e166925327..b56d8a66110c2 100644 --- a/src/legacy/core_plugins/timelion/public/panels/timechart/schema.ts +++ b/src/plugins/timelion/public/panels/timechart/schema.ts @@ -17,31 +17,32 @@ * under the License. */ -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import '../../../../../../plugins/vis_type_timelion/public/flot'; +import '../../flot'; import _ from 'lodash'; import $ from 'jquery'; import moment from 'moment-timezone'; -import { timefilter } from 'ui/timefilter'; // @ts-ignore import observeResize from '../../lib/observe_resize'; import { calculateInterval, DEFAULT_TIME_FORMAT, - // @ts-ignore -} from '../../../../../../plugins/vis_type_timelion/common/lib'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { tickFormatters } from '../../../../../../plugins/vis_type_timelion/public/helpers/tick_formatters'; -import { TimelionVisualizationDependencies } from '../../plugin'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { xaxisFormatterProvider } from '../../../../../../plugins/vis_type_timelion/public/helpers/xaxis_formatter'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { generateTicksProvider } from '../../../../../../plugins/vis_type_timelion/public/helpers/tick_generator'; + tickFormatters, + xaxisFormatterProvider, + generateTicksProvider, +} from '../../../../vis_type_timelion/public'; +import { TimelionVisualizationDependencies } from '../../application'; const DEBOUNCE_DELAY = 50; export function timechartFn(dependencies: TimelionVisualizationDependencies) { - const { $rootScope, $compile, uiSettings } = dependencies; + const { + $rootScope, + $compile, + uiSettings, + data: { + query: { timefilter }, + }, + } = dependencies; return function () { return { @@ -199,7 +200,7 @@ export function timechartFn(dependencies: TimelionVisualizationDependencies) { }); $elem.on('plotselected', function (event: any, ranges: any) { - timefilter.setTime({ + timefilter.timefilter.setTime({ from: moment(ranges.xaxis.from), to: moment(ranges.xaxis.to), }); @@ -299,7 +300,7 @@ export function timechartFn(dependencies: TimelionVisualizationDependencies) { const options = _.cloneDeep(defaultOptions) as any; // Get the X-axis tick format - const time = timefilter.getBounds() as any; + const time = timefilter.timefilter.getBounds() as any; const interval = calculateInterval( time.min.valueOf(), time.max.valueOf(), diff --git a/src/legacy/core_plugins/timelion/public/panels/timechart/timechart.ts b/src/plugins/timelion/public/panels/timechart/timechart.ts similarity index 94% rename from src/legacy/core_plugins/timelion/public/panels/timechart/timechart.ts rename to src/plugins/timelion/public/panels/timechart/timechart.ts index 4173bfeb331e2..525a994e3121d 100644 --- a/src/legacy/core_plugins/timelion/public/panels/timechart/timechart.ts +++ b/src/plugins/timelion/public/panels/timechart/timechart.ts @@ -19,7 +19,7 @@ import { timechartFn } from './schema'; import { Panel } from '../panel'; -import { TimelionVisualizationDependencies } from '../../plugin'; +import { TimelionVisualizationDependencies } from '../../application'; export function getTimeChart(dependencies: TimelionVisualizationDependencies) { // Schema is broken out so that it may be extended for use in other plugins diff --git a/src/legacy/core_plugins/timelion/public/partials/load_sheet.html b/src/plugins/timelion/public/partials/load_sheet.html similarity index 100% rename from src/legacy/core_plugins/timelion/public/partials/load_sheet.html rename to src/plugins/timelion/public/partials/load_sheet.html diff --git a/src/legacy/core_plugins/timelion/public/partials/save_sheet.html b/src/plugins/timelion/public/partials/save_sheet.html similarity index 100% rename from src/legacy/core_plugins/timelion/public/partials/save_sheet.html rename to src/plugins/timelion/public/partials/save_sheet.html diff --git a/src/legacy/core_plugins/timelion/public/partials/sheet_options.html b/src/plugins/timelion/public/partials/sheet_options.html similarity index 100% rename from src/legacy/core_plugins/timelion/public/partials/sheet_options.html rename to src/plugins/timelion/public/partials/sheet_options.html diff --git a/src/plugins/timelion/public/plugin.ts b/src/plugins/timelion/public/plugin.ts new file mode 100644 index 0000000000000..a92ced20cb6d1 --- /dev/null +++ b/src/plugins/timelion/public/plugin.ts @@ -0,0 +1,134 @@ +/* + * 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 { BehaviorSubject } from 'rxjs'; +import { filter, map } from 'rxjs/operators'; +import { + CoreSetup, + CoreStart, + Plugin, + PluginInitializerContext, + DEFAULT_APP_CATEGORIES, + AppMountParameters, + AppUpdater, + ScopedHistory, +} from '../../../core/public'; +import { Panel } from './panels/panel'; +import { initAngularBootstrap, KibanaLegacyStart } from '../../kibana_legacy/public'; +import { createKbnUrlTracker } from '../../kibana_utils/public'; +import { DataPublicPluginStart, esFilters, DataPublicPluginSetup } from '../../data/public'; +import { NavigationPublicPluginStart } from '../../navigation/public'; +import { VisualizationsStart } from '../../visualizations/public'; +import { VisTypeTimelionPluginStart } from '../../vis_type_timelion/public'; + +export interface TimelionPluginDependencies { + data: DataPublicPluginStart; + navigation: NavigationPublicPluginStart; + visualizations: VisualizationsStart; + visTypeTimelion: VisTypeTimelionPluginStart; +} + +/** @internal */ +export class TimelionPlugin implements Plugin { + initializerContext: PluginInitializerContext; + private appStateUpdater = new BehaviorSubject(() => ({})); + private stopUrlTracking: (() => void) | undefined = undefined; + private currentHistory: ScopedHistory | undefined = undefined; + + constructor(initializerContext: PluginInitializerContext) { + this.initializerContext = initializerContext; + } + + public setup(core: CoreSetup, { data }: { data: DataPublicPluginSetup }) { + const timelionPanels: Map = new Map(); + + const { appMounted, appUnMounted, stop: stopUrlTracker } = createKbnUrlTracker({ + baseUrl: core.http.basePath.prepend('/app/timelion'), + defaultSubUrl: '#/', + storageKey: `lastUrl:${core.http.basePath.get()}:timelion`, + navLinkUpdater$: this.appStateUpdater, + toastNotifications: core.notifications.toasts, + stateParams: [ + { + kbnUrlKey: '_g', + stateUpdate$: data.query.state$.pipe( + filter( + ({ changes }) => !!(changes.globalFilters || changes.time || changes.refreshInterval) + ), + map(({ state }) => ({ + ...state, + filters: state.filters?.filter(esFilters.isFilterPinned), + })) + ), + }, + ], + getHistory: () => this.currentHistory!, + }); + + this.stopUrlTracking = () => { + stopUrlTracker(); + }; + + initAngularBootstrap(); + core.application.register({ + id: 'timelion', + title: 'Timelion', + order: 8000, + defaultPath: '#/', + euiIconType: 'timelionApp', + category: DEFAULT_APP_CATEGORIES.kibana, + updater$: this.appStateUpdater.asObservable(), + mount: async (params: AppMountParameters) => { + const [coreStart, pluginsStart] = await core.getStartServices(); + this.currentHistory = params.history; + + appMounted(); + + const unlistenParentHistory = params.history.listen(() => { + window.dispatchEvent(new HashChangeEvent('hashchange')); + }); + + const { renderApp } = await import('./application'); + params.element.classList.add('timelionAppContainer'); + const unmount = renderApp({ + mountParams: params, + pluginInitializerContext: this.initializerContext, + timelionPanels, + core: coreStart, + plugins: pluginsStart as TimelionPluginDependencies, + }); + return () => { + unlistenParentHistory(); + unmount(); + appUnMounted(); + }; + }, + }); + } + + public start(core: CoreStart, { kibanaLegacy }: { kibanaLegacy: KibanaLegacyStart }) { + kibanaLegacy.loadFontAwesome(); + } + + public stop(): void { + if (this.stopUrlTracking) { + this.stopUrlTracking(); + } + } +} diff --git a/src/legacy/core_plugins/timelion/public/services/_saved_sheet.ts b/src/plugins/timelion/public/services/_saved_sheet.ts similarity index 95% rename from src/legacy/core_plugins/timelion/public/services/_saved_sheet.ts rename to src/plugins/timelion/public/services/_saved_sheet.ts index 4e5aa8d445e7d..0958cce860126 100644 --- a/src/legacy/core_plugins/timelion/public/services/_saved_sheet.ts +++ b/src/plugins/timelion/public/services/_saved_sheet.ts @@ -18,10 +18,7 @@ */ import { IUiSettingsClient } from 'kibana/public'; -import { - createSavedObjectClass, - SavedObjectKibanaServices, -} from '../../../../../plugins/saved_objects/public'; +import { createSavedObjectClass, SavedObjectKibanaServices } from '../../../saved_objects/public'; // Used only by the savedSheets service, usually no reason to change this export function createSavedSheetClass( diff --git a/src/plugins/timelion/public/services/saved_sheets.ts b/src/plugins/timelion/public/services/saved_sheets.ts new file mode 100644 index 0000000000000..a3e7f66d9ee47 --- /dev/null +++ b/src/plugins/timelion/public/services/saved_sheets.ts @@ -0,0 +1,50 @@ +/* + * 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 { SavedObjectLoader } from '../../../saved_objects/public'; +import { createSavedSheetClass } from './_saved_sheet'; +import { RenderDeps } from '../application'; + +export function initSavedSheetService(app: angular.IModule, deps: RenderDeps) { + const savedObjectsClient = deps.core.savedObjects.client; + const services = { + savedObjectsClient, + indexPatterns: deps.plugins.data.indexPatterns, + search: deps.plugins.data.search, + chrome: deps.core.chrome, + overlays: deps.core.overlays, + }; + + const SavedSheet = createSavedSheetClass(services, deps.core.uiSettings); + + const savedSheetLoader = new SavedObjectLoader(SavedSheet, savedObjectsClient, deps.core.chrome); + savedSheetLoader.urlFor = (id) => `#/${encodeURIComponent(id)}`; + // Customize loader properties since adding an 's' on type doesn't work for type 'timelion-sheet'. + savedSheetLoader.loaderProperties = { + name: 'timelion-sheet', + noun: 'Saved Sheets', + nouns: 'saved sheets', + }; + // This is the only thing that gets injected into controllers + app.service('savedSheets', function () { + return savedSheetLoader; + }); + + return savedSheetLoader; +} diff --git a/src/plugins/timelion/public/timelion_app_state.ts b/src/plugins/timelion/public/timelion_app_state.ts new file mode 100644 index 0000000000000..43382adbf8f80 --- /dev/null +++ b/src/plugins/timelion/public/timelion_app_state.ts @@ -0,0 +1,73 @@ +/* + * 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 { createStateContainer, syncState, IKbnUrlStateStorage } from '../../kibana_utils/public'; + +import { TimelionAppState, TimelionAppStateTransitions } from './types'; + +const STATE_STORAGE_KEY = '_a'; + +interface Arguments { + kbnUrlStateStorage: IKbnUrlStateStorage; + stateDefaults: TimelionAppState; +} + +export function initTimelionAppState({ stateDefaults, kbnUrlStateStorage }: Arguments) { + const urlState = kbnUrlStateStorage.get(STATE_STORAGE_KEY); + const initialState = { + ...stateDefaults, + ...urlState, + }; + + /* + make sure url ('_a') matches initial state + Initializing appState does two things - first it translates the defaults into AppState, + second it updates appState based on the url (the url trumps the defaults). This means if + we update the state format at all and want to handle BWC, we must not only migrate the + data stored with saved vis, but also any old state in the url. + */ + kbnUrlStateStorage.set(STATE_STORAGE_KEY, initialState, { replace: true }); + + const stateContainer = createStateContainer( + initialState, + { + set: (state) => (prop, value) => ({ ...state, [prop]: value }), + updateState: (state) => (newValues) => ({ ...state, ...newValues }), + } + ); + + const { start: startStateSync, stop: stopStateSync } = syncState({ + storageKey: STATE_STORAGE_KEY, + stateContainer: { + ...stateContainer, + set: (state) => { + if (state) { + // syncState utils requires to handle incoming "null" value + stateContainer.set(state); + } + }, + }, + stateStorage: kbnUrlStateStorage, + }); + + // start syncing the appState with the ('_a') url + startStateSync(); + + return { stateContainer, stopStateSync }; +} diff --git a/src/plugins/timelion/public/types.ts b/src/plugins/timelion/public/types.ts new file mode 100644 index 0000000000000..700485064e41b --- /dev/null +++ b/src/plugins/timelion/public/types.ts @@ -0,0 +1,35 @@ +/* + * 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. + */ + +export interface TimelionAppState { + sheet: string[]; + selected: number; + columns: number; + rows: number; + interval: string; +} + +export interface TimelionAppStateTransitions { + set: ( + state: TimelionAppState + ) => (prop: T, value: TimelionAppState[T]) => TimelionAppState; + updateState: ( + state: TimelionAppState + ) => (newValues: Partial) => TimelionAppState; +} diff --git a/src/plugins/timelion/public/webpackShims/jquery.flot.axislabels.js b/src/plugins/timelion/public/webpackShims/jquery.flot.axislabels.js new file mode 100644 index 0000000000000..cda8038953c76 --- /dev/null +++ b/src/plugins/timelion/public/webpackShims/jquery.flot.axislabels.js @@ -0,0 +1,462 @@ +/* +Axis Labels Plugin for flot. +http://github.com/markrcote/flot-axislabels +Original code is Copyright (c) 2010 Xuan Luo. +Original code was released under the GPLv3 license by Xuan Luo, September 2010. +Original code was rereleased under the MIT license by Xuan Luo, April 2012. +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +(function ($) { + var options = { + axisLabels: { + show: true + } + }; + + function canvasSupported() { + return !!document.createElement('canvas').getContext; + } + + function canvasTextSupported() { + if (!canvasSupported()) { + return false; + } + var dummy_canvas = document.createElement('canvas'); + var context = dummy_canvas.getContext('2d'); + return typeof context.fillText == 'function'; + } + + function css3TransitionSupported() { + var div = document.createElement('div'); + return typeof div.style.MozTransition != 'undefined' // Gecko + || typeof div.style.OTransition != 'undefined' // Opera + || typeof div.style.webkitTransition != 'undefined' // WebKit + || typeof div.style.transition != 'undefined'; + } + + + function AxisLabel(axisName, position, padding, plot, opts) { + this.axisName = axisName; + this.position = position; + this.padding = padding; + this.plot = plot; + this.opts = opts; + this.width = 0; + this.height = 0; + } + + AxisLabel.prototype.cleanup = function() { + }; + + + CanvasAxisLabel.prototype = new AxisLabel(); + CanvasAxisLabel.prototype.constructor = CanvasAxisLabel; + function CanvasAxisLabel(axisName, position, padding, plot, opts) { + AxisLabel.prototype.constructor.call(this, axisName, position, padding, + plot, opts); + } + + CanvasAxisLabel.prototype.calculateSize = function() { + if (!this.opts.axisLabelFontSizePixels) + this.opts.axisLabelFontSizePixels = 14; + if (!this.opts.axisLabelFontFamily) + this.opts.axisLabelFontFamily = 'sans-serif'; + + var textWidth = this.opts.axisLabelFontSizePixels + this.padding; + var textHeight = this.opts.axisLabelFontSizePixels + this.padding; + if (this.position == 'left' || this.position == 'right') { + this.width = this.opts.axisLabelFontSizePixels + this.padding; + this.height = 0; + } else { + this.width = 0; + this.height = this.opts.axisLabelFontSizePixels + this.padding; + } + }; + + CanvasAxisLabel.prototype.draw = function(box) { + if (!this.opts.axisLabelColour) + this.opts.axisLabelColour = 'black'; + var ctx = this.plot.getCanvas().getContext('2d'); + ctx.save(); + ctx.font = this.opts.axisLabelFontSizePixels + 'px ' + + this.opts.axisLabelFontFamily; + ctx.fillStyle = this.opts.axisLabelColour; + var width = ctx.measureText(this.opts.axisLabel).width; + var height = this.opts.axisLabelFontSizePixels; + var x, y, angle = 0; + if (this.position == 'top') { + x = box.left + box.width/2 - width/2; + y = box.top + height*0.72; + } else if (this.position == 'bottom') { + x = box.left + box.width/2 - width/2; + y = box.top + box.height - height*0.72; + } else if (this.position == 'left') { + x = box.left + height*0.72; + y = box.height/2 + box.top + width/2; + angle = -Math.PI/2; + } else if (this.position == 'right') { + x = box.left + box.width - height*0.72; + y = box.height/2 + box.top - width/2; + angle = Math.PI/2; + } + ctx.translate(x, y); + ctx.rotate(angle); + ctx.fillText(this.opts.axisLabel, 0, 0); + ctx.restore(); + }; + + + HtmlAxisLabel.prototype = new AxisLabel(); + HtmlAxisLabel.prototype.constructor = HtmlAxisLabel; + function HtmlAxisLabel(axisName, position, padding, plot, opts) { + AxisLabel.prototype.constructor.call(this, axisName, position, + padding, plot, opts); + this.elem = null; + } + + HtmlAxisLabel.prototype.calculateSize = function() { + var elem = $('
' + + this.opts.axisLabel + '
'); + this.plot.getPlaceholder().append(elem); + // store height and width of label itself, for use in draw() + this.labelWidth = elem.outerWidth(true); + this.labelHeight = elem.outerHeight(true); + elem.remove(); + + this.width = this.height = 0; + if (this.position == 'left' || this.position == 'right') { + this.width = this.labelWidth + this.padding; + } else { + this.height = this.labelHeight + this.padding; + } + }; + + HtmlAxisLabel.prototype.cleanup = function() { + if (this.elem) { + this.elem.remove(); + } + }; + + HtmlAxisLabel.prototype.draw = function(box) { + this.plot.getPlaceholder().find('#' + this.axisName + 'Label').remove(); + this.elem = $('
' + + this.opts.axisLabel + '
'); + this.plot.getPlaceholder().append(this.elem); + if (this.position == 'top') { + this.elem.css('left', box.left + box.width/2 - this.labelWidth/2 + + 'px'); + this.elem.css('top', box.top + 'px'); + } else if (this.position == 'bottom') { + this.elem.css('left', box.left + box.width/2 - this.labelWidth/2 + + 'px'); + this.elem.css('top', box.top + box.height - this.labelHeight + + 'px'); + } else if (this.position == 'left') { + this.elem.css('top', box.top + box.height/2 - this.labelHeight/2 + + 'px'); + this.elem.css('left', box.left + 'px'); + } else if (this.position == 'right') { + this.elem.css('top', box.top + box.height/2 - this.labelHeight/2 + + 'px'); + this.elem.css('left', box.left + box.width - this.labelWidth + + 'px'); + } + }; + + + CssTransformAxisLabel.prototype = new HtmlAxisLabel(); + CssTransformAxisLabel.prototype.constructor = CssTransformAxisLabel; + function CssTransformAxisLabel(axisName, position, padding, plot, opts) { + HtmlAxisLabel.prototype.constructor.call(this, axisName, position, + padding, plot, opts); + } + + CssTransformAxisLabel.prototype.calculateSize = function() { + HtmlAxisLabel.prototype.calculateSize.call(this); + this.width = this.height = 0; + if (this.position == 'left' || this.position == 'right') { + this.width = this.labelHeight + this.padding; + } else { + this.height = this.labelHeight + this.padding; + } + }; + + CssTransformAxisLabel.prototype.transforms = function(degrees, x, y) { + var stransforms = { + '-moz-transform': '', + '-webkit-transform': '', + '-o-transform': '', + '-ms-transform': '' + }; + if (x != 0 || y != 0) { + var stdTranslate = ' translate(' + x + 'px, ' + y + 'px)'; + stransforms['-moz-transform'] += stdTranslate; + stransforms['-webkit-transform'] += stdTranslate; + stransforms['-o-transform'] += stdTranslate; + stransforms['-ms-transform'] += stdTranslate; + } + if (degrees != 0) { + var rotation = degrees / 90; + var stdRotate = ' rotate(' + degrees + 'deg)'; + stransforms['-moz-transform'] += stdRotate; + stransforms['-webkit-transform'] += stdRotate; + stransforms['-o-transform'] += stdRotate; + stransforms['-ms-transform'] += stdRotate; + } + var s = 'top: 0; left: 0; '; + for (var prop in stransforms) { + if (stransforms[prop]) { + s += prop + ':' + stransforms[prop] + ';'; + } + } + s += ';'; + return s; + }; + + CssTransformAxisLabel.prototype.calculateOffsets = function(box) { + var offsets = { x: 0, y: 0, degrees: 0 }; + if (this.position == 'bottom') { + offsets.x = box.left + box.width/2 - this.labelWidth/2; + offsets.y = box.top + box.height - this.labelHeight; + } else if (this.position == 'top') { + offsets.x = box.left + box.width/2 - this.labelWidth/2; + offsets.y = box.top; + } else if (this.position == 'left') { + offsets.degrees = -90; + offsets.x = box.left - this.labelWidth/2 + this.labelHeight/2; + offsets.y = box.height/2 + box.top; + } else if (this.position == 'right') { + offsets.degrees = 90; + offsets.x = box.left + box.width - this.labelWidth/2 + - this.labelHeight/2; + offsets.y = box.height/2 + box.top; + } + offsets.x = Math.round(offsets.x); + offsets.y = Math.round(offsets.y); + + return offsets; + }; + + CssTransformAxisLabel.prototype.draw = function(box) { + this.plot.getPlaceholder().find("." + this.axisName + "Label").remove(); + var offsets = this.calculateOffsets(box); + this.elem = $('
' + this.opts.axisLabel + '
'); + this.plot.getPlaceholder().append(this.elem); + }; + + + IeTransformAxisLabel.prototype = new CssTransformAxisLabel(); + IeTransformAxisLabel.prototype.constructor = IeTransformAxisLabel; + function IeTransformAxisLabel(axisName, position, padding, plot, opts) { + CssTransformAxisLabel.prototype.constructor.call(this, axisName, + position, padding, + plot, opts); + this.requiresResize = false; + } + + IeTransformAxisLabel.prototype.transforms = function(degrees, x, y) { + // I didn't feel like learning the crazy Matrix stuff, so this uses + // a combination of the rotation transform and CSS positioning. + var s = ''; + if (degrees != 0) { + var rotation = degrees/90; + while (rotation < 0) { + rotation += 4; + } + s += ' filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=' + rotation + '); '; + // see below + this.requiresResize = (this.position == 'right'); + } + if (x != 0) { + s += 'left: ' + x + 'px; '; + } + if (y != 0) { + s += 'top: ' + y + 'px; '; + } + return s; + }; + + IeTransformAxisLabel.prototype.calculateOffsets = function(box) { + var offsets = CssTransformAxisLabel.prototype.calculateOffsets.call( + this, box); + // adjust some values to take into account differences between + // CSS and IE rotations. + if (this.position == 'top') { + // FIXME: not sure why, but placing this exactly at the top causes + // the top axis label to flip to the bottom... + offsets.y = box.top + 1; + } else if (this.position == 'left') { + offsets.x = box.left; + offsets.y = box.height/2 + box.top - this.labelWidth/2; + } else if (this.position == 'right') { + offsets.x = box.left + box.width - this.labelHeight; + offsets.y = box.height/2 + box.top - this.labelWidth/2; + } + return offsets; + }; + + IeTransformAxisLabel.prototype.draw = function(box) { + CssTransformAxisLabel.prototype.draw.call(this, box); + if (this.requiresResize) { + this.elem = this.plot.getPlaceholder().find("." + this.axisName + + "Label"); + // Since we used CSS positioning instead of transforms for + // translating the element, and since the positioning is done + // before any rotations, we have to reset the width and height + // in case the browser wrapped the text (specifically for the + // y2axis). + this.elem.css('width', this.labelWidth); + this.elem.css('height', this.labelHeight); + } + }; + + + function init(plot) { + plot.hooks.processOptions.push(function (plot, options) { + + if (!options.axisLabels.show) + return; + + // This is kind of a hack. There are no hooks in Flot between + // the creation and measuring of the ticks (setTicks, measureTickLabels + // in setupGrid() ) and the drawing of the ticks and plot box + // (insertAxisLabels in setupGrid() ). + // + // Therefore, we use a trick where we run the draw routine twice: + // the first time to get the tick measurements, so that we can change + // them, and then have it draw it again. + var secondPass = false; + + var axisLabels = {}; + var axisOffsetCounts = { left: 0, right: 0, top: 0, bottom: 0 }; + + var defaultPadding = 2; // padding between axis and tick labels + plot.hooks.draw.push(function (plot, ctx) { + var hasAxisLabels = false; + if (!secondPass) { + // MEASURE AND SET OPTIONS + $.each(plot.getAxes(), function(axisName, axis) { + var opts = axis.options // Flot 0.7 + || plot.getOptions()[axisName]; // Flot 0.6 + + // Handle redraws initiated outside of this plug-in. + if (axisName in axisLabels) { + axis.labelHeight = axis.labelHeight - + axisLabels[axisName].height; + axis.labelWidth = axis.labelWidth - + axisLabels[axisName].width; + opts.labelHeight = axis.labelHeight; + opts.labelWidth = axis.labelWidth; + axisLabels[axisName].cleanup(); + delete axisLabels[axisName]; + } + + if (!opts || !opts.axisLabel || !axis.show) + return; + + hasAxisLabels = true; + var renderer = null; + + if (!opts.axisLabelUseHtml && + navigator.appName == 'Microsoft Internet Explorer') { + var ua = navigator.userAgent; + var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})"); + if (re.exec(ua) != null) { + rv = parseFloat(RegExp.$1); + } + if (rv >= 9 && !opts.axisLabelUseCanvas && !opts.axisLabelUseHtml) { + renderer = CssTransformAxisLabel; + } else if (!opts.axisLabelUseCanvas && !opts.axisLabelUseHtml) { + renderer = IeTransformAxisLabel; + } else if (opts.axisLabelUseCanvas) { + renderer = CanvasAxisLabel; + } else { + renderer = HtmlAxisLabel; + } + } else { + if (opts.axisLabelUseHtml || (!css3TransitionSupported() && !canvasTextSupported()) && !opts.axisLabelUseCanvas) { + renderer = HtmlAxisLabel; + } else if (opts.axisLabelUseCanvas || !css3TransitionSupported()) { + renderer = CanvasAxisLabel; + } else { + renderer = CssTransformAxisLabel; + } + } + + var padding = opts.axisLabelPadding === undefined ? + defaultPadding : opts.axisLabelPadding; + + axisLabels[axisName] = new renderer(axisName, + axis.position, padding, + plot, opts); + + // flot interprets axis.labelHeight and .labelWidth as + // the height and width of the tick labels. We increase + // these values to make room for the axis label and + // padding. + + axisLabels[axisName].calculateSize(); + + // AxisLabel.height and .width are the size of the + // axis label and padding. + // Just set opts here because axis will be sorted out on + // the redraw. + + opts.labelHeight = axis.labelHeight + + axisLabels[axisName].height; + opts.labelWidth = axis.labelWidth + + axisLabels[axisName].width; + }); + + // If there are axis labels, re-draw with new label widths and + // heights. + + if (hasAxisLabels) { + secondPass = true; + plot.setupGrid(); + plot.draw(); + } + } else { + secondPass = false; + // DRAW + $.each(plot.getAxes(), function(axisName, axis) { + var opts = axis.options // Flot 0.7 + || plot.getOptions()[axisName]; // Flot 0.6 + if (!opts || !opts.axisLabel || !axis.show) + return; + + axisLabels[axisName].draw(axis.box); + }); + } + }); + }); + } + + + $.plot.plugins.push({ + init: init, + options: options, + name: 'axisLabels', + version: '2.0' + }); +})(jQuery); diff --git a/src/plugins/timelion/public/webpackShims/jquery.flot.crosshair.js b/src/plugins/timelion/public/webpackShims/jquery.flot.crosshair.js new file mode 100644 index 0000000000000..5111695e3d12c --- /dev/null +++ b/src/plugins/timelion/public/webpackShims/jquery.flot.crosshair.js @@ -0,0 +1,176 @@ +/* Flot plugin for showing crosshairs when the mouse hovers over the plot. + +Copyright (c) 2007-2014 IOLA and Ole Laursen. +Licensed under the MIT license. + +The plugin supports these options: + + crosshair: { + mode: null or "x" or "y" or "xy" + color: color + lineWidth: number + } + +Set the mode to one of "x", "y" or "xy". The "x" mode enables a vertical +crosshair that lets you trace the values on the x axis, "y" enables a +horizontal crosshair and "xy" enables them both. "color" is the color of the +crosshair (default is "rgba(170, 0, 0, 0.80)"), "lineWidth" is the width of +the drawn lines (default is 1). + +The plugin also adds four public methods: + + - setCrosshair( pos ) + + Set the position of the crosshair. Note that this is cleared if the user + moves the mouse. "pos" is in coordinates of the plot and should be on the + form { x: xpos, y: ypos } (you can use x2/x3/... if you're using multiple + axes), which is coincidentally the same format as what you get from a + "plothover" event. If "pos" is null, the crosshair is cleared. + + - clearCrosshair() + + Clear the crosshair. + + - lockCrosshair(pos) + + Cause the crosshair to lock to the current location, no longer updating if + the user moves the mouse. Optionally supply a position (passed on to + setCrosshair()) to move it to. + + Example usage: + + var myFlot = $.plot( $("#graph"), ..., { crosshair: { mode: "x" } } }; + $("#graph").bind( "plothover", function ( evt, position, item ) { + if ( item ) { + // Lock the crosshair to the data point being hovered + myFlot.lockCrosshair({ + x: item.datapoint[ 0 ], + y: item.datapoint[ 1 ] + }); + } else { + // Return normal crosshair operation + myFlot.unlockCrosshair(); + } + }); + + - unlockCrosshair() + + Free the crosshair to move again after locking it. +*/ + +(function ($) { + var options = { + crosshair: { + mode: null, // one of null, "x", "y" or "xy", + color: "rgba(170, 0, 0, 0.80)", + lineWidth: 1 + } + }; + + function init(plot) { + // position of crosshair in pixels + var crosshair = { x: -1, y: -1, locked: false }; + + plot.setCrosshair = function setCrosshair(pos) { + if (!pos) + crosshair.x = -1; + else { + var o = plot.p2c(pos); + crosshair.x = Math.max(0, Math.min(o.left, plot.width())); + crosshair.y = Math.max(0, Math.min(o.top, plot.height())); + } + + plot.triggerRedrawOverlay(); + }; + + plot.clearCrosshair = plot.setCrosshair; // passes null for pos + + plot.lockCrosshair = function lockCrosshair(pos) { + if (pos) + plot.setCrosshair(pos); + crosshair.locked = true; + }; + + plot.unlockCrosshair = function unlockCrosshair() { + crosshair.locked = false; + }; + + function onMouseOut(e) { + if (crosshair.locked) + return; + + if (crosshair.x != -1) { + crosshair.x = -1; + plot.triggerRedrawOverlay(); + } + } + + function onMouseMove(e) { + if (crosshair.locked) + return; + + if (plot.getSelection && plot.getSelection()) { + crosshair.x = -1; // hide the crosshair while selecting + return; + } + + var offset = plot.offset(); + crosshair.x = Math.max(0, Math.min(e.pageX - offset.left, plot.width())); + crosshair.y = Math.max(0, Math.min(e.pageY - offset.top, plot.height())); + plot.triggerRedrawOverlay(); + } + + plot.hooks.bindEvents.push(function (plot, eventHolder) { + if (!plot.getOptions().crosshair.mode) + return; + + eventHolder.mouseout(onMouseOut); + eventHolder.mousemove(onMouseMove); + }); + + plot.hooks.drawOverlay.push(function (plot, ctx) { + var c = plot.getOptions().crosshair; + if (!c.mode) + return; + + var plotOffset = plot.getPlotOffset(); + + ctx.save(); + ctx.translate(plotOffset.left, plotOffset.top); + + if (crosshair.x != -1) { + var adj = plot.getOptions().crosshair.lineWidth % 2 ? 0.5 : 0; + + ctx.strokeStyle = c.color; + ctx.lineWidth = c.lineWidth; + ctx.lineJoin = "round"; + + ctx.beginPath(); + if (c.mode.indexOf("x") != -1) { + var drawX = Math.floor(crosshair.x) + adj; + ctx.moveTo(drawX, 0); + ctx.lineTo(drawX, plot.height()); + } + if (c.mode.indexOf("y") != -1) { + var drawY = Math.floor(crosshair.y) + adj; + ctx.moveTo(0, drawY); + ctx.lineTo(plot.width(), drawY); + } + ctx.stroke(); + } + ctx.restore(); + }); + + plot.hooks.shutdown.push(function (plot, eventHolder) { + eventHolder.unbind("mouseout", onMouseOut); + eventHolder.unbind("mousemove", onMouseMove); + }); + } + + $.plot.plugins.push({ + init: init, + options: options, + name: 'crosshair', + version: '1.0' + }); +})(jQuery); diff --git a/src/plugins/timelion/public/webpackShims/jquery.flot.js b/src/plugins/timelion/public/webpackShims/jquery.flot.js new file mode 100644 index 0000000000000..5d613037cf234 --- /dev/null +++ b/src/plugins/timelion/public/webpackShims/jquery.flot.js @@ -0,0 +1,3168 @@ +/* JavaScript plotting library for jQuery, version 0.8.3. + +Copyright (c) 2007-2014 IOLA and Ole Laursen. +Licensed under the MIT license. + +*/ + +// first an inline dependency, jquery.colorhelpers.js, we inline it here +// for convenience + +/* Plugin for jQuery for working with colors. + * + * Version 1.1. + * + * Inspiration from jQuery color animation plugin by John Resig. + * + * Released under the MIT license by Ole Laursen, October 2009. + * + * Examples: + * + * $.color.parse("#fff").scale('rgb', 0.25).add('a', -0.5).toString() + * var c = $.color.extract($("#mydiv"), 'background-color'); + * console.log(c.r, c.g, c.b, c.a); + * $.color.make(100, 50, 25, 0.4).toString() // returns "rgba(100,50,25,0.4)" + * + * Note that .scale() and .add() return the same modified object + * instead of making a new one. + * + * V. 1.1: Fix error handling so e.g. parsing an empty string does + * produce a color rather than just crashing. + */ +(function($){$.color={};$.color.make=function(r,g,b,a){var o={};o.r=r||0;o.g=g||0;o.b=b||0;o.a=a!=null?a:1;o.add=function(c,d){for(var i=0;i=1){return"rgb("+[o.r,o.g,o.b].join(",")+")"}else{return"rgba("+[o.r,o.g,o.b,o.a].join(",")+")"}};o.normalize=function(){function clamp(min,value,max){return valuemax?max:value}o.r=clamp(0,parseInt(o.r),255);o.g=clamp(0,parseInt(o.g),255);o.b=clamp(0,parseInt(o.b),255);o.a=clamp(0,o.a,1);return o};o.clone=function(){return $.color.make(o.r,o.b,o.g,o.a)};return o.normalize()};$.color.extract=function(elem,css){var c;do{c=elem.css(css).toLowerCase();if(c!=""&&c!="transparent")break;elem=elem.parent()}while(elem.length&&!$.nodeName(elem.get(0),"body"));if(c=="rgba(0, 0, 0, 0)")c="transparent";return $.color.parse(c)};$.color.parse=function(str){var res,m=$.color.make;if(res=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(str))return m(parseInt(res[1],10),parseInt(res[2],10),parseInt(res[3],10));if(res=/rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(str))return m(parseInt(res[1],10),parseInt(res[2],10),parseInt(res[3],10),parseFloat(res[4]));if(res=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(str))return m(parseFloat(res[1])*2.55,parseFloat(res[2])*2.55,parseFloat(res[3])*2.55);if(res=/rgba\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(str))return m(parseFloat(res[1])*2.55,parseFloat(res[2])*2.55,parseFloat(res[3])*2.55,parseFloat(res[4]));if(res=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(str))return m(parseInt(res[1],16),parseInt(res[2],16),parseInt(res[3],16));if(res=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(str))return m(parseInt(res[1]+res[1],16),parseInt(res[2]+res[2],16),parseInt(res[3]+res[3],16));var name=$.trim(str).toLowerCase();if(name=="transparent")return m(255,255,255,0);else{res=lookupColors[name]||[0,0,0];return m(res[0],res[1],res[2])}};var lookupColors={aqua:[0,255,255],azure:[240,255,255],beige:[245,245,220],black:[0,0,0],blue:[0,0,255],brown:[165,42,42],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgrey:[169,169,169],darkgreen:[0,100,0],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkviolet:[148,0,211],fuchsia:[255,0,255],gold:[255,215,0],green:[0,128,0],indigo:[75,0,130],khaki:[240,230,140],lightblue:[173,216,230],lightcyan:[224,255,255],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightyellow:[255,255,224],lime:[0,255,0],magenta:[255,0,255],maroon:[128,0,0],navy:[0,0,128],olive:[128,128,0],orange:[255,165,0],pink:[255,192,203],purple:[128,0,128],violet:[128,0,128],red:[255,0,0],silver:[192,192,192],white:[255,255,255],yellow:[255,255,0]}})(jQuery); + +// the actual Flot code +(function($) { + + // Cache the prototype hasOwnProperty for faster access + + var hasOwnProperty = Object.prototype.hasOwnProperty; + + // A shim to provide 'detach' to jQuery versions prior to 1.4. Using a DOM + // operation produces the same effect as detach, i.e. removing the element + // without touching its jQuery data. + + // Do not merge this into Flot 0.9, since it requires jQuery 1.4.4+. + + if (!$.fn.detach) { + $.fn.detach = function() { + return this.each(function() { + if (this.parentNode) { + this.parentNode.removeChild( this ); + } + }); + }; + } + + /////////////////////////////////////////////////////////////////////////// + // The Canvas object is a wrapper around an HTML5 tag. + // + // @constructor + // @param {string} cls List of classes to apply to the canvas. + // @param {element} container Element onto which to append the canvas. + // + // Requiring a container is a little iffy, but unfortunately canvas + // operations don't work unless the canvas is attached to the DOM. + + function Canvas(cls, container) { + + var element = container.children("." + cls)[0]; + + if (element == null) { + + element = document.createElement("canvas"); + element.className = cls; + + $(element).css({ direction: "ltr", position: "absolute", left: 0, top: 0 }) + .appendTo(container); + + // If HTML5 Canvas isn't available, fall back to [Ex|Flash]canvas + + if (!element.getContext) { + if (window.G_vmlCanvasManager) { + element = window.G_vmlCanvasManager.initElement(element); + } else { + throw new Error("Canvas is not available. If you're using IE with a fall-back such as Excanvas, then there's either a mistake in your conditional include, or the page has no DOCTYPE and is rendering in Quirks Mode."); + } + } + } + + this.element = element; + + var context = this.context = element.getContext("2d"); + + // Determine the screen's ratio of physical to device-independent + // pixels. This is the ratio between the canvas width that the browser + // advertises and the number of pixels actually present in that space. + + // The iPhone 4, for example, has a device-independent width of 320px, + // but its screen is actually 640px wide. It therefore has a pixel + // ratio of 2, while most normal devices have a ratio of 1. + + var devicePixelRatio = window.devicePixelRatio || 1, + backingStoreRatio = + context.webkitBackingStorePixelRatio || + context.mozBackingStorePixelRatio || + context.msBackingStorePixelRatio || + context.oBackingStorePixelRatio || + context.backingStorePixelRatio || 1; + + this.pixelRatio = devicePixelRatio / backingStoreRatio; + + // Size the canvas to match the internal dimensions of its container + + this.resize(container.width(), container.height()); + + // Collection of HTML div layers for text overlaid onto the canvas + + this.textContainer = null; + this.text = {}; + + // Cache of text fragments and metrics, so we can avoid expensively + // re-calculating them when the plot is re-rendered in a loop. + + this._textCache = {}; + } + + // Resizes the canvas to the given dimensions. + // + // @param {number} width New width of the canvas, in pixels. + // @param {number} width New height of the canvas, in pixels. + + Canvas.prototype.resize = function(width, height) { + + if (width <= 0 || height <= 0) { + throw new Error("Invalid dimensions for plot, width = " + width + ", height = " + height); + } + + var element = this.element, + context = this.context, + pixelRatio = this.pixelRatio; + + // Resize the canvas, increasing its density based on the display's + // pixel ratio; basically giving it more pixels without increasing the + // size of its element, to take advantage of the fact that retina + // displays have that many more pixels in the same advertised space. + + // Resizing should reset the state (excanvas seems to be buggy though) + + if (this.width != width) { + element.width = width * pixelRatio; + element.style.width = width + "px"; + this.width = width; + } + + if (this.height != height) { + element.height = height * pixelRatio; + element.style.height = height + "px"; + this.height = height; + } + + // Save the context, so we can reset in case we get replotted. The + // restore ensure that we're really back at the initial state, and + // should be safe even if we haven't saved the initial state yet. + + context.restore(); + context.save(); + + // Scale the coordinate space to match the display density; so even though we + // may have twice as many pixels, we still want lines and other drawing to + // appear at the same size; the extra pixels will just make them crisper. + + context.scale(pixelRatio, pixelRatio); + }; + + // Clears the entire canvas area, not including any overlaid HTML text + + Canvas.prototype.clear = function() { + this.context.clearRect(0, 0, this.width, this.height); + }; + + // Finishes rendering the canvas, including managing the text overlay. + + Canvas.prototype.render = function() { + + var cache = this._textCache; + + // For each text layer, add elements marked as active that haven't + // already been rendered, and remove those that are no longer active. + + for (var layerKey in cache) { + if (hasOwnProperty.call(cache, layerKey)) { + + var layer = this.getTextLayer(layerKey), + layerCache = cache[layerKey]; + + layer.hide(); + + for (var styleKey in layerCache) { + if (hasOwnProperty.call(layerCache, styleKey)) { + var styleCache = layerCache[styleKey]; + for (var key in styleCache) { + if (hasOwnProperty.call(styleCache, key)) { + + var positions = styleCache[key].positions; + + for (var i = 0, position; position = positions[i]; i++) { + if (position.active) { + if (!position.rendered) { + layer.append(position.element); + position.rendered = true; + } + } else { + positions.splice(i--, 1); + if (position.rendered) { + position.element.detach(); + } + } + } + + if (positions.length == 0) { + delete styleCache[key]; + } + } + } + } + } + + layer.show(); + } + } + }; + + // Creates (if necessary) and returns the text overlay container. + // + // @param {string} classes String of space-separated CSS classes used to + // uniquely identify the text layer. + // @return {object} The jQuery-wrapped text-layer div. + + Canvas.prototype.getTextLayer = function(classes) { + + var layer = this.text[classes]; + + // Create the text layer if it doesn't exist + + if (layer == null) { + + // Create the text layer container, if it doesn't exist + + if (this.textContainer == null) { + this.textContainer = $("
") + .css({ + position: "absolute", + top: 0, + left: 0, + bottom: 0, + right: 0, + 'font-size': "smaller", + color: "#545454" + }) + .insertAfter(this.element); + } + + layer = this.text[classes] = $("
") + .addClass(classes) + .css({ + position: "absolute", + top: 0, + left: 0, + bottom: 0, + right: 0 + }) + .appendTo(this.textContainer); + } + + return layer; + }; + + // Creates (if necessary) and returns a text info object. + // + // The object looks like this: + // + // { + // width: Width of the text's wrapper div. + // height: Height of the text's wrapper div. + // element: The jQuery-wrapped HTML div containing the text. + // positions: Array of positions at which this text is drawn. + // } + // + // The positions array contains objects that look like this: + // + // { + // active: Flag indicating whether the text should be visible. + // rendered: Flag indicating whether the text is currently visible. + // element: The jQuery-wrapped HTML div containing the text. + // x: X coordinate at which to draw the text. + // y: Y coordinate at which to draw the text. + // } + // + // Each position after the first receives a clone of the original element. + // + // The idea is that that the width, height, and general 'identity' of the + // text is constant no matter where it is placed; the placements are a + // secondary property. + // + // Canvas maintains a cache of recently-used text info objects; getTextInfo + // either returns the cached element or creates a new entry. + // + // @param {string} layer A string of space-separated CSS classes uniquely + // identifying the layer containing this text. + // @param {string} text Text string to retrieve info for. + // @param {(string|object)=} font Either a string of space-separated CSS + // classes or a font-spec object, defining the text's font and style. + // @param {number=} angle Angle at which to rotate the text, in degrees. + // Angle is currently unused, it will be implemented in the future. + // @param {number=} width Maximum width of the text before it wraps. + // @return {object} a text info object. + + Canvas.prototype.getTextInfo = function(layer, text, font, angle, width) { + + var textStyle, layerCache, styleCache, info; + + // Cast the value to a string, in case we were given a number or such + + text = "" + text; + + // If the font is a font-spec object, generate a CSS font definition + + if (typeof font === "object") { + textStyle = font.style + " " + font.variant + " " + font.weight + " " + font.size + "px/" + font.lineHeight + "px " + font.family; + } else { + textStyle = font; + } + + // Retrieve (or create) the cache for the text's layer and styles + + layerCache = this._textCache[layer]; + + if (layerCache == null) { + layerCache = this._textCache[layer] = {}; + } + + styleCache = layerCache[textStyle]; + + if (styleCache == null) { + styleCache = layerCache[textStyle] = {}; + } + + info = styleCache[text]; + + // If we can't find a matching element in our cache, create a new one + + if (info == null) { + + var element = $("
").html(text) + .css({ + position: "absolute", + 'max-width': width, + top: -9999 + }) + .appendTo(this.getTextLayer(layer)); + + if (typeof font === "object") { + element.css({ + font: textStyle, + color: font.color + }); + } else if (typeof font === "string") { + element.addClass(font); + } + + info = styleCache[text] = { + width: element.outerWidth(true), + height: element.outerHeight(true), + element: element, + positions: [] + }; + + element.detach(); + } + + return info; + }; + + // Adds a text string to the canvas text overlay. + // + // The text isn't drawn immediately; it is marked as rendering, which will + // result in its addition to the canvas on the next render pass. + // + // @param {string} layer A string of space-separated CSS classes uniquely + // identifying the layer containing this text. + // @param {number} x X coordinate at which to draw the text. + // @param {number} y Y coordinate at which to draw the text. + // @param {string} text Text string to draw. + // @param {(string|object)=} font Either a string of space-separated CSS + // classes or a font-spec object, defining the text's font and style. + // @param {number=} angle Angle at which to rotate the text, in degrees. + // Angle is currently unused, it will be implemented in the future. + // @param {number=} width Maximum width of the text before it wraps. + // @param {string=} halign Horizontal alignment of the text; either "left", + // "center" or "right". + // @param {string=} valign Vertical alignment of the text; either "top", + // "middle" or "bottom". + + Canvas.prototype.addText = function(layer, x, y, text, font, angle, width, halign, valign) { + + var info = this.getTextInfo(layer, text, font, angle, width), + positions = info.positions; + + // Tweak the div's position to match the text's alignment + + if (halign == "center") { + x -= info.width / 2; + } else if (halign == "right") { + x -= info.width; + } + + if (valign == "middle") { + y -= info.height / 2; + } else if (valign == "bottom") { + y -= info.height; + } + + // Determine whether this text already exists at this position. + // If so, mark it for inclusion in the next render pass. + + for (var i = 0, position; position = positions[i]; i++) { + if (position.x == x && position.y == y) { + position.active = true; + return; + } + } + + // If the text doesn't exist at this position, create a new entry + + // For the very first position we'll re-use the original element, + // while for subsequent ones we'll clone it. + + position = { + active: true, + rendered: false, + element: positions.length ? info.element.clone() : info.element, + x: x, + y: y + }; + + positions.push(position); + + // Move the element to its final position within the container + + position.element.css({ + top: Math.round(y), + left: Math.round(x), + 'text-align': halign // In case the text wraps + }); + }; + + // Removes one or more text strings from the canvas text overlay. + // + // If no parameters are given, all text within the layer is removed. + // + // Note that the text is not immediately removed; it is simply marked as + // inactive, which will result in its removal on the next render pass. + // This avoids the performance penalty for 'clear and redraw' behavior, + // where we potentially get rid of all text on a layer, but will likely + // add back most or all of it later, as when redrawing axes, for example. + // + // @param {string} layer A string of space-separated CSS classes uniquely + // identifying the layer containing this text. + // @param {number=} x X coordinate of the text. + // @param {number=} y Y coordinate of the text. + // @param {string=} text Text string to remove. + // @param {(string|object)=} font Either a string of space-separated CSS + // classes or a font-spec object, defining the text's font and style. + // @param {number=} angle Angle at which the text is rotated, in degrees. + // Angle is currently unused, it will be implemented in the future. + + Canvas.prototype.removeText = function(layer, x, y, text, font, angle) { + if (text == null) { + var layerCache = this._textCache[layer]; + if (layerCache != null) { + for (var styleKey in layerCache) { + if (hasOwnProperty.call(layerCache, styleKey)) { + var styleCache = layerCache[styleKey]; + for (var key in styleCache) { + if (hasOwnProperty.call(styleCache, key)) { + var positions = styleCache[key].positions; + for (var i = 0, position; position = positions[i]; i++) { + position.active = false; + } + } + } + } + } + } + } else { + var positions = this.getTextInfo(layer, text, font, angle).positions; + for (var i = 0, position; position = positions[i]; i++) { + if (position.x == x && position.y == y) { + position.active = false; + } + } + } + }; + + /////////////////////////////////////////////////////////////////////////// + // The top-level container for the entire plot. + + function Plot(placeholder, data_, options_, plugins) { + // data is on the form: + // [ series1, series2 ... ] + // where series is either just the data as [ [x1, y1], [x2, y2], ... ] + // or { data: [ [x1, y1], [x2, y2], ... ], label: "some label", ... } + + var series = [], + options = { + // the color theme used for graphs + colors: ["#edc240", "#afd8f8", "#cb4b4b", "#4da74d", "#9440ed"], + legend: { + show: true, + noColumns: 1, // number of columns in legend table + labelFormatter: null, // fn: string -> string + labelBoxBorderColor: "#ccc", // border color for the little label boxes + container: null, // container (as jQuery object) to put legend in, null means default on top of graph + position: "ne", // position of default legend container within plot + margin: 5, // distance from grid edge to default legend container within plot + backgroundColor: null, // null means auto-detect + backgroundOpacity: 0.85, // set to 0 to avoid background + sorted: null // default to no legend sorting + }, + xaxis: { + show: null, // null = auto-detect, true = always, false = never + position: "bottom", // or "top" + mode: null, // null or "time" + font: null, // null (derived from CSS in placeholder) or object like { size: 11, lineHeight: 13, style: "italic", weight: "bold", family: "sans-serif", variant: "small-caps" } + color: null, // base color, labels, ticks + tickColor: null, // possibly different color of ticks, e.g. "rgba(0,0,0,0.15)" + transform: null, // null or f: number -> number to transform axis + inverseTransform: null, // if transform is set, this should be the inverse function + min: null, // min. value to show, null means set automatically + max: null, // max. value to show, null means set automatically + autoscaleMargin: null, // margin in % to add if auto-setting min/max + ticks: null, // either [1, 3] or [[1, "a"], 3] or (fn: axis info -> ticks) or app. number of ticks for auto-ticks + tickFormatter: null, // fn: number -> string + labelWidth: null, // size of tick labels in pixels + labelHeight: null, + reserveSpace: null, // whether to reserve space even if axis isn't shown + tickLength: null, // size in pixels of ticks, or "full" for whole line + alignTicksWithAxis: null, // axis number or null for no sync + tickDecimals: null, // no. of decimals, null means auto + tickSize: null, // number or [number, "unit"] + minTickSize: null // number or [number, "unit"] + }, + yaxis: { + autoscaleMargin: 0.02, + position: "left" // or "right" + }, + xaxes: [], + yaxes: [], + series: { + points: { + show: false, + radius: 3, + lineWidth: 2, // in pixels + fill: true, + fillColor: "#ffffff", + symbol: "circle" // or callback + }, + lines: { + // we don't put in show: false so we can see + // whether lines were actively disabled + lineWidth: 2, // in pixels + fill: false, + fillColor: null, + steps: false + // Omit 'zero', so we can later default its value to + // match that of the 'fill' option. + }, + bars: { + show: false, + lineWidth: 2, // in pixels + barWidth: 1, // in units of the x axis + fill: true, + fillColor: null, + align: "left", // "left", "right", or "center" + horizontal: false, + zero: true + }, + shadowSize: 3, + highlightColor: null + }, + grid: { + show: true, + aboveData: false, + color: "#545454", // primary color used for outline and labels + backgroundColor: null, // null for transparent, else color + borderColor: null, // set if different from the grid color + tickColor: null, // color for the ticks, e.g. "rgba(0,0,0,0.15)" + margin: 0, // distance from the canvas edge to the grid + labelMargin: 5, // in pixels + axisMargin: 8, // in pixels + borderWidth: 2, // in pixels + minBorderMargin: null, // in pixels, null means taken from points radius + markings: null, // array of ranges or fn: axes -> array of ranges + markingsColor: "#f4f4f4", + markingsLineWidth: 2, + // interactive stuff + clickable: false, + hoverable: false, + autoHighlight: true, // highlight in case mouse is near + mouseActiveRadius: 10 // how far the mouse can be away to activate an item + }, + interaction: { + redrawOverlayInterval: 1000/60 // time between updates, -1 means in same flow + }, + hooks: {} + }, + surface = null, // the canvas for the plot itself + overlay = null, // canvas for interactive stuff on top of plot + eventHolder = null, // jQuery object that events should be bound to + ctx = null, octx = null, + xaxes = [], yaxes = [], + plotOffset = { left: 0, right: 0, top: 0, bottom: 0}, + plotWidth = 0, plotHeight = 0, + hooks = { + processOptions: [], + processRawData: [], + processDatapoints: [], + processOffset: [], + drawBackground: [], + drawSeries: [], + draw: [], + bindEvents: [], + drawOverlay: [], + shutdown: [] + }, + plot = this; + + // public functions + plot.setData = setData; + plot.setupGrid = setupGrid; + plot.draw = draw; + plot.getPlaceholder = function() { return placeholder; }; + plot.getCanvas = function() { return surface.element; }; + plot.getPlotOffset = function() { return plotOffset; }; + plot.width = function () { return plotWidth; }; + plot.height = function () { return plotHeight; }; + plot.offset = function () { + var o = eventHolder.offset(); + o.left += plotOffset.left; + o.top += plotOffset.top; + return o; + }; + plot.getData = function () { return series; }; + plot.getAxes = function () { + var res = {}, i; + $.each(xaxes.concat(yaxes), function (_, axis) { + if (axis) + res[axis.direction + (axis.n != 1 ? axis.n : "") + "axis"] = axis; + }); + return res; + }; + plot.getXAxes = function () { return xaxes; }; + plot.getYAxes = function () { return yaxes; }; + plot.c2p = canvasToAxisCoords; + plot.p2c = axisToCanvasCoords; + plot.getOptions = function () { return options; }; + plot.highlight = highlight; + plot.unhighlight = unhighlight; + plot.triggerRedrawOverlay = triggerRedrawOverlay; + plot.pointOffset = function(point) { + return { + left: parseInt(xaxes[axisNumber(point, "x") - 1].p2c(+point.x) + plotOffset.left, 10), + top: parseInt(yaxes[axisNumber(point, "y") - 1].p2c(+point.y) + plotOffset.top, 10) + }; + }; + plot.shutdown = shutdown; + plot.destroy = function () { + shutdown(); + placeholder.removeData("plot").empty(); + + series = []; + options = null; + surface = null; + overlay = null; + eventHolder = null; + ctx = null; + octx = null; + xaxes = []; + yaxes = []; + hooks = null; + highlights = []; + plot = null; + }; + plot.resize = function () { + var width = placeholder.width(), + height = placeholder.height(); + surface.resize(width, height); + overlay.resize(width, height); + }; + + // public attributes + plot.hooks = hooks; + + // initialize + initPlugins(plot); + parseOptions(options_); + setupCanvases(); + setData(data_); + setupGrid(); + draw(); + bindEvents(); + + + function executeHooks(hook, args) { + args = [plot].concat(args); + for (var i = 0; i < hook.length; ++i) + hook[i].apply(this, args); + } + + function initPlugins() { + + // References to key classes, allowing plugins to modify them + + var classes = { + Canvas: Canvas + }; + + for (var i = 0; i < plugins.length; ++i) { + var p = plugins[i]; + p.init(plot, classes); + if (p.options) + $.extend(true, options, p.options); + } + } + + function parseOptions(opts) { + + $.extend(true, options, opts); + + // $.extend merges arrays, rather than replacing them. When less + // colors are provided than the size of the default palette, we + // end up with those colors plus the remaining defaults, which is + // not expected behavior; avoid it by replacing them here. + + if (opts && opts.colors) { + options.colors = opts.colors; + } + + if (options.xaxis.color == null) + options.xaxis.color = $.color.parse(options.grid.color).scale('a', 0.22).toString(); + if (options.yaxis.color == null) + options.yaxis.color = $.color.parse(options.grid.color).scale('a', 0.22).toString(); + + if (options.xaxis.tickColor == null) // grid.tickColor for back-compatibility + options.xaxis.tickColor = options.grid.tickColor || options.xaxis.color; + if (options.yaxis.tickColor == null) // grid.tickColor for back-compatibility + options.yaxis.tickColor = options.grid.tickColor || options.yaxis.color; + + if (options.grid.borderColor == null) + options.grid.borderColor = options.grid.color; + if (options.grid.tickColor == null) + options.grid.tickColor = $.color.parse(options.grid.color).scale('a', 0.22).toString(); + + // Fill in defaults for axis options, including any unspecified + // font-spec fields, if a font-spec was provided. + + // If no x/y axis options were provided, create one of each anyway, + // since the rest of the code assumes that they exist. + + var i, axisOptions, axisCount, + fontSize = placeholder.css("font-size"), + fontSizeDefault = fontSize ? +fontSize.replace("px", "") : 13, + fontDefaults = { + style: placeholder.css("font-style"), + size: Math.round(0.8 * fontSizeDefault), + variant: placeholder.css("font-variant"), + weight: placeholder.css("font-weight"), + family: placeholder.css("font-family") + }; + + axisCount = options.xaxes.length || 1; + for (i = 0; i < axisCount; ++i) { + + axisOptions = options.xaxes[i]; + if (axisOptions && !axisOptions.tickColor) { + axisOptions.tickColor = axisOptions.color; + } + + axisOptions = $.extend(true, {}, options.xaxis, axisOptions); + options.xaxes[i] = axisOptions; + + if (axisOptions.font) { + axisOptions.font = $.extend({}, fontDefaults, axisOptions.font); + if (!axisOptions.font.color) { + axisOptions.font.color = axisOptions.color; + } + if (!axisOptions.font.lineHeight) { + axisOptions.font.lineHeight = Math.round(axisOptions.font.size * 1.15); + } + } + } + + axisCount = options.yaxes.length || 1; + for (i = 0; i < axisCount; ++i) { + + axisOptions = options.yaxes[i]; + if (axisOptions && !axisOptions.tickColor) { + axisOptions.tickColor = axisOptions.color; + } + + axisOptions = $.extend(true, {}, options.yaxis, axisOptions); + options.yaxes[i] = axisOptions; + + if (axisOptions.font) { + axisOptions.font = $.extend({}, fontDefaults, axisOptions.font); + if (!axisOptions.font.color) { + axisOptions.font.color = axisOptions.color; + } + if (!axisOptions.font.lineHeight) { + axisOptions.font.lineHeight = Math.round(axisOptions.font.size * 1.15); + } + } + } + + // backwards compatibility, to be removed in future + if (options.xaxis.noTicks && options.xaxis.ticks == null) + options.xaxis.ticks = options.xaxis.noTicks; + if (options.yaxis.noTicks && options.yaxis.ticks == null) + options.yaxis.ticks = options.yaxis.noTicks; + if (options.x2axis) { + options.xaxes[1] = $.extend(true, {}, options.xaxis, options.x2axis); + options.xaxes[1].position = "top"; + // Override the inherit to allow the axis to auto-scale + if (options.x2axis.min == null) { + options.xaxes[1].min = null; + } + if (options.x2axis.max == null) { + options.xaxes[1].max = null; + } + } + if (options.y2axis) { + options.yaxes[1] = $.extend(true, {}, options.yaxis, options.y2axis); + options.yaxes[1].position = "right"; + // Override the inherit to allow the axis to auto-scale + if (options.y2axis.min == null) { + options.yaxes[1].min = null; + } + if (options.y2axis.max == null) { + options.yaxes[1].max = null; + } + } + if (options.grid.coloredAreas) + options.grid.markings = options.grid.coloredAreas; + if (options.grid.coloredAreasColor) + options.grid.markingsColor = options.grid.coloredAreasColor; + if (options.lines) + $.extend(true, options.series.lines, options.lines); + if (options.points) + $.extend(true, options.series.points, options.points); + if (options.bars) + $.extend(true, options.series.bars, options.bars); + if (options.shadowSize != null) + options.series.shadowSize = options.shadowSize; + if (options.highlightColor != null) + options.series.highlightColor = options.highlightColor; + + // save options on axes for future reference + for (i = 0; i < options.xaxes.length; ++i) + getOrCreateAxis(xaxes, i + 1).options = options.xaxes[i]; + for (i = 0; i < options.yaxes.length; ++i) + getOrCreateAxis(yaxes, i + 1).options = options.yaxes[i]; + + // add hooks from options + for (var n in hooks) + if (options.hooks[n] && options.hooks[n].length) + hooks[n] = hooks[n].concat(options.hooks[n]); + + executeHooks(hooks.processOptions, [options]); + } + + function setData(d) { + series = parseData(d); + fillInSeriesOptions(); + processData(); + } + + function parseData(d) { + var res = []; + for (var i = 0; i < d.length; ++i) { + var s = $.extend(true, {}, options.series); + + if (d[i].data != null) { + s.data = d[i].data; // move the data instead of deep-copy + delete d[i].data; + + $.extend(true, s, d[i]); + + d[i].data = s.data; + } + else + s.data = d[i]; + res.push(s); + } + + return res; + } + + function axisNumber(obj, coord) { + var a = obj[coord + "axis"]; + if (typeof a == "object") // if we got a real axis, extract number + a = a.n; + if (typeof a != "number") + a = 1; // default to first axis + return a; + } + + function allAxes() { + // return flat array without annoying null entries + return $.grep(xaxes.concat(yaxes), function (a) { return a; }); + } + + function canvasToAxisCoords(pos) { + // return an object with x/y corresponding to all used axes + var res = {}, i, axis; + for (i = 0; i < xaxes.length; ++i) { + axis = xaxes[i]; + if (axis && axis.used) + res["x" + axis.n] = axis.c2p(pos.left); + } + + for (i = 0; i < yaxes.length; ++i) { + axis = yaxes[i]; + if (axis && axis.used) + res["y" + axis.n] = axis.c2p(pos.top); + } + + if (res.x1 !== undefined) + res.x = res.x1; + if (res.y1 !== undefined) + res.y = res.y1; + + return res; + } + + function axisToCanvasCoords(pos) { + // get canvas coords from the first pair of x/y found in pos + var res = {}, i, axis, key; + + for (i = 0; i < xaxes.length; ++i) { + axis = xaxes[i]; + if (axis && axis.used) { + key = "x" + axis.n; + if (pos[key] == null && axis.n == 1) + key = "x"; + + if (pos[key] != null) { + res.left = axis.p2c(pos[key]); + break; + } + } + } + + for (i = 0; i < yaxes.length; ++i) { + axis = yaxes[i]; + if (axis && axis.used) { + key = "y" + axis.n; + if (pos[key] == null && axis.n == 1) + key = "y"; + + if (pos[key] != null) { + res.top = axis.p2c(pos[key]); + break; + } + } + } + + return res; + } + + function getOrCreateAxis(axes, number) { + if (!axes[number - 1]) + axes[number - 1] = { + n: number, // save the number for future reference + direction: axes == xaxes ? "x" : "y", + options: $.extend(true, {}, axes == xaxes ? options.xaxis : options.yaxis) + }; + + return axes[number - 1]; + } + + function fillInSeriesOptions() { + + var neededColors = series.length, maxIndex = -1, i; + + // Subtract the number of series that already have fixed colors or + // color indexes from the number that we still need to generate. + + for (i = 0; i < series.length; ++i) { + var sc = series[i].color; + if (sc != null) { + neededColors--; + if (typeof sc == "number" && sc > maxIndex) { + maxIndex = sc; + } + } + } + + // If any of the series have fixed color indexes, then we need to + // generate at least as many colors as the highest index. + + if (neededColors <= maxIndex) { + neededColors = maxIndex + 1; + } + + // Generate all the colors, using first the option colors and then + // variations on those colors once they're exhausted. + + var c, colors = [], colorPool = options.colors, + colorPoolSize = colorPool.length, variation = 0; + + for (i = 0; i < neededColors; i++) { + + c = $.color.parse(colorPool[i % colorPoolSize] || "#666"); + + // Each time we exhaust the colors in the pool we adjust + // a scaling factor used to produce more variations on + // those colors. The factor alternates negative/positive + // to produce lighter/darker colors. + + // Reset the variation after every few cycles, or else + // it will end up producing only white or black colors. + + if (i % colorPoolSize == 0 && i) { + if (variation >= 0) { + if (variation < 0.5) { + variation = -variation - 0.2; + } else variation = 0; + } else variation = -variation; + } + + colors[i] = c.scale('rgb', 1 + variation); + } + + // Finalize the series options, filling in their colors + + var colori = 0, s; + for (i = 0; i < series.length; ++i) { + s = series[i]; + + // assign colors + if (s.color == null) { + s.color = colors[colori].toString(); + ++colori; + } + else if (typeof s.color == "number") + s.color = colors[s.color].toString(); + + // turn on lines automatically in case nothing is set + if (s.lines.show == null) { + var v, show = true; + for (v in s) + if (s[v] && s[v].show) { + show = false; + break; + } + if (show) + s.lines.show = true; + } + + // If nothing was provided for lines.zero, default it to match + // lines.fill, since areas by default should extend to zero. + + if (s.lines.zero == null) { + s.lines.zero = !!s.lines.fill; + } + + // setup axes + s.xaxis = getOrCreateAxis(xaxes, axisNumber(s, "x")); + s.yaxis = getOrCreateAxis(yaxes, axisNumber(s, "y")); + } + } + + function processData() { + var topSentry = Number.POSITIVE_INFINITY, + bottomSentry = Number.NEGATIVE_INFINITY, + fakeInfinity = Number.MAX_VALUE, + i, j, k, m, length, + s, points, ps, x, y, axis, val, f, p, + data, format; + + function updateAxis(axis, min, max) { + if (min < axis.datamin && min != -fakeInfinity) + axis.datamin = min; + if (max > axis.datamax && max != fakeInfinity) + axis.datamax = max; + } + + $.each(allAxes(), function (_, axis) { + // init axis + axis.datamin = topSentry; + axis.datamax = bottomSentry; + axis.used = false; + }); + + for (i = 0; i < series.length; ++i) { + s = series[i]; + s.datapoints = { points: [] }; + + executeHooks(hooks.processRawData, [ s, s.data, s.datapoints ]); + } + + // first pass: clean and copy data + for (i = 0; i < series.length; ++i) { + s = series[i]; + + data = s.data; + format = s.datapoints.format; + + if (!format) { + format = []; + // find out how to copy + format.push({ x: true, number: true, required: true }); + format.push({ y: true, number: true, required: true }); + + if (s.bars.show || (s.lines.show && s.lines.fill)) { + var autoscale = !!((s.bars.show && s.bars.zero) || (s.lines.show && s.lines.zero)); + format.push({ y: true, number: true, required: false, defaultValue: 0, autoscale: autoscale }); + if (s.bars.horizontal) { + delete format[format.length - 1].y; + format[format.length - 1].x = true; + } + } + + s.datapoints.format = format; + } + + if (s.datapoints.pointsize != null) + continue; // already filled in + + s.datapoints.pointsize = format.length; + + ps = s.datapoints.pointsize; + points = s.datapoints.points; + + var insertSteps = s.lines.show && s.lines.steps; + s.xaxis.used = s.yaxis.used = true; + + for (j = k = 0; j < data.length; ++j, k += ps) { + p = data[j]; + + var nullify = p == null; + if (!nullify) { + for (m = 0; m < ps; ++m) { + val = p[m]; + f = format[m]; + + if (f) { + if (f.number && val != null) { + val = +val; // convert to number + if (isNaN(val)) + val = null; + else if (val == Infinity) + val = fakeInfinity; + else if (val == -Infinity) + val = -fakeInfinity; + } + + if (val == null) { + if (f.required) + nullify = true; + + if (f.defaultValue != null) + val = f.defaultValue; + } + } + + points[k + m] = val; + } + } + + if (nullify) { + for (m = 0; m < ps; ++m) { + val = points[k + m]; + if (val != null) { + f = format[m]; + // extract min/max info + if (f.autoscale !== false) { + if (f.x) { + updateAxis(s.xaxis, val, val); + } + if (f.y) { + updateAxis(s.yaxis, val, val); + } + } + } + points[k + m] = null; + } + } + else { + // a little bit of line specific stuff that + // perhaps shouldn't be here, but lacking + // better means... + if (insertSteps && k > 0 + && points[k - ps] != null + && points[k - ps] != points[k] + && points[k - ps + 1] != points[k + 1]) { + // copy the point to make room for a middle point + for (m = 0; m < ps; ++m) + points[k + ps + m] = points[k + m]; + + // middle point has same y + points[k + 1] = points[k - ps + 1]; + + // we've added a point, better reflect that + k += ps; + } + } + } + } + + // give the hooks a chance to run + for (i = 0; i < series.length; ++i) { + s = series[i]; + + executeHooks(hooks.processDatapoints, [ s, s.datapoints]); + } + + // second pass: find datamax/datamin for auto-scaling + for (i = 0; i < series.length; ++i) { + s = series[i]; + points = s.datapoints.points; + ps = s.datapoints.pointsize; + format = s.datapoints.format; + + var xmin = topSentry, ymin = topSentry, + xmax = bottomSentry, ymax = bottomSentry; + + for (j = 0; j < points.length; j += ps) { + if (points[j] == null) + continue; + + for (m = 0; m < ps; ++m) { + val = points[j + m]; + f = format[m]; + if (!f || f.autoscale === false || val == fakeInfinity || val == -fakeInfinity) + continue; + + if (f.x) { + if (val < xmin) + xmin = val; + if (val > xmax) + xmax = val; + } + if (f.y) { + if (val < ymin) + ymin = val; + if (val > ymax) + ymax = val; + } + } + } + + if (s.bars.show) { + // make sure we got room for the bar on the dancing floor + var delta; + + switch (s.bars.align) { + case "left": + delta = 0; + break; + case "right": + delta = -s.bars.barWidth; + break; + default: + delta = -s.bars.barWidth / 2; + } + + if (s.bars.horizontal) { + ymin += delta; + ymax += delta + s.bars.barWidth; + } + else { + xmin += delta; + xmax += delta + s.bars.barWidth; + } + } + + updateAxis(s.xaxis, xmin, xmax); + updateAxis(s.yaxis, ymin, ymax); + } + + $.each(allAxes(), function (_, axis) { + if (axis.datamin == topSentry) + axis.datamin = null; + if (axis.datamax == bottomSentry) + axis.datamax = null; + }); + } + + function setupCanvases() { + + // Make sure the placeholder is clear of everything except canvases + // from a previous plot in this container that we'll try to re-use. + + placeholder.css("padding", 0) // padding messes up the positioning + .children().filter(function(){ + return !$(this).hasClass("flot-overlay") && !$(this).hasClass('flot-base'); + }).remove(); + + if (placeholder.css("position") == 'static') + placeholder.css("position", "relative"); // for positioning labels and overlay + + surface = new Canvas("flot-base", placeholder); + overlay = new Canvas("flot-overlay", placeholder); // overlay canvas for interactive features + + ctx = surface.context; + octx = overlay.context; + + // define which element we're listening for events on + eventHolder = $(overlay.element).unbind(); + + // If we're re-using a plot object, shut down the old one + + var existing = placeholder.data("plot"); + + if (existing) { + existing.shutdown(); + overlay.clear(); + } + + // save in case we get replotted + placeholder.data("plot", plot); + } + + function bindEvents() { + // bind events + if (options.grid.hoverable) { + eventHolder.mousemove(onMouseMove); + + // Use bind, rather than .mouseleave, because we officially + // still support jQuery 1.2.6, which doesn't define a shortcut + // for mouseenter or mouseleave. This was a bug/oversight that + // was fixed somewhere around 1.3.x. We can return to using + // .mouseleave when we drop support for 1.2.6. + + eventHolder.bind("mouseleave", onMouseLeave); + } + + if (options.grid.clickable) + eventHolder.click(onClick); + + executeHooks(hooks.bindEvents, [eventHolder]); + } + + function shutdown() { + if (redrawTimeout) + clearTimeout(redrawTimeout); + + eventHolder.unbind("mousemove", onMouseMove); + eventHolder.unbind("mouseleave", onMouseLeave); + eventHolder.unbind("click", onClick); + + executeHooks(hooks.shutdown, [eventHolder]); + } + + function setTransformationHelpers(axis) { + // set helper functions on the axis, assumes plot area + // has been computed already + + function identity(x) { return x; } + + var s, m, t = axis.options.transform || identity, + it = axis.options.inverseTransform; + + // precompute how much the axis is scaling a point + // in canvas space + if (axis.direction == "x") { + s = axis.scale = plotWidth / Math.abs(t(axis.max) - t(axis.min)); + m = Math.min(t(axis.max), t(axis.min)); + } + else { + s = axis.scale = plotHeight / Math.abs(t(axis.max) - t(axis.min)); + s = -s; + m = Math.max(t(axis.max), t(axis.min)); + } + + // data point to canvas coordinate + if (t == identity) // slight optimization + axis.p2c = function (p) { return (p - m) * s; }; + else + axis.p2c = function (p) { return (t(p) - m) * s; }; + // canvas coordinate to data point + if (!it) + axis.c2p = function (c) { return m + c / s; }; + else + axis.c2p = function (c) { return it(m + c / s); }; + } + + function measureTickLabels(axis) { + + var opts = axis.options, + ticks = axis.ticks || [], + labelWidth = opts.labelWidth || 0, + labelHeight = opts.labelHeight || 0, + maxWidth = labelWidth || (axis.direction == "x" ? Math.floor(surface.width / (ticks.length || 1)) : null), + legacyStyles = axis.direction + "Axis " + axis.direction + axis.n + "Axis", + layer = "flot-" + axis.direction + "-axis flot-" + axis.direction + axis.n + "-axis " + legacyStyles, + font = opts.font || "flot-tick-label tickLabel"; + + for (var i = 0; i < ticks.length; ++i) { + + var t = ticks[i]; + + if (!t.label) + continue; + + var info = surface.getTextInfo(layer, t.label, font, null, maxWidth); + + labelWidth = Math.max(labelWidth, info.width); + labelHeight = Math.max(labelHeight, info.height); + } + + axis.labelWidth = opts.labelWidth || labelWidth; + axis.labelHeight = opts.labelHeight || labelHeight; + } + + function allocateAxisBoxFirstPhase(axis) { + // find the bounding box of the axis by looking at label + // widths/heights and ticks, make room by diminishing the + // plotOffset; this first phase only looks at one + // dimension per axis, the other dimension depends on the + // other axes so will have to wait + + var lw = axis.labelWidth, + lh = axis.labelHeight, + pos = axis.options.position, + isXAxis = axis.direction === "x", + tickLength = axis.options.tickLength, + axisMargin = options.grid.axisMargin, + padding = options.grid.labelMargin, + innermost = true, + outermost = true, + first = true, + found = false; + + // Determine the axis's position in its direction and on its side + + $.each(isXAxis ? xaxes : yaxes, function(i, a) { + if (a && (a.show || a.reserveSpace)) { + if (a === axis) { + found = true; + } else if (a.options.position === pos) { + if (found) { + outermost = false; + } else { + innermost = false; + } + } + if (!found) { + first = false; + } + } + }); + + // The outermost axis on each side has no margin + + if (outermost) { + axisMargin = 0; + } + + // The ticks for the first axis in each direction stretch across + + if (tickLength == null) { + tickLength = first ? "full" : 5; + } + + if (!isNaN(+tickLength)) + padding += +tickLength; + + if (isXAxis) { + lh += padding; + + if (pos == "bottom") { + plotOffset.bottom += lh + axisMargin; + axis.box = { top: surface.height - plotOffset.bottom, height: lh }; + } + else { + axis.box = { top: plotOffset.top + axisMargin, height: lh }; + plotOffset.top += lh + axisMargin; + } + } + else { + lw += padding; + + if (pos == "left") { + axis.box = { left: plotOffset.left + axisMargin, width: lw }; + plotOffset.left += lw + axisMargin; + } + else { + plotOffset.right += lw + axisMargin; + axis.box = { left: surface.width - plotOffset.right, width: lw }; + } + } + + // save for future reference + axis.position = pos; + axis.tickLength = tickLength; + axis.box.padding = padding; + axis.innermost = innermost; + } + + function allocateAxisBoxSecondPhase(axis) { + // now that all axis boxes have been placed in one + // dimension, we can set the remaining dimension coordinates + if (axis.direction == "x") { + axis.box.left = plotOffset.left - axis.labelWidth / 2; + axis.box.width = surface.width - plotOffset.left - plotOffset.right + axis.labelWidth; + } + else { + axis.box.top = plotOffset.top - axis.labelHeight / 2; + axis.box.height = surface.height - plotOffset.bottom - plotOffset.top + axis.labelHeight; + } + } + + function adjustLayoutForThingsStickingOut() { + // possibly adjust plot offset to ensure everything stays + // inside the canvas and isn't clipped off + + var minMargin = options.grid.minBorderMargin, + axis, i; + + // check stuff from the plot (FIXME: this should just read + // a value from the series, otherwise it's impossible to + // customize) + if (minMargin == null) { + minMargin = 0; + for (i = 0; i < series.length; ++i) + minMargin = Math.max(minMargin, 2 * (series[i].points.radius + series[i].points.lineWidth/2)); + } + + var margins = { + left: minMargin, + right: minMargin, + top: minMargin, + bottom: minMargin + }; + + // check axis labels, note we don't check the actual + // labels but instead use the overall width/height to not + // jump as much around with replots + $.each(allAxes(), function (_, axis) { + if (axis.reserveSpace && axis.ticks && axis.ticks.length) { + if (axis.direction === "x") { + margins.left = Math.max(margins.left, axis.labelWidth / 2); + margins.right = Math.max(margins.right, axis.labelWidth / 2); + } else { + margins.bottom = Math.max(margins.bottom, axis.labelHeight / 2); + margins.top = Math.max(margins.top, axis.labelHeight / 2); + } + } + }); + + plotOffset.left = Math.ceil(Math.max(margins.left, plotOffset.left)); + plotOffset.right = Math.ceil(Math.max(margins.right, plotOffset.right)); + plotOffset.top = Math.ceil(Math.max(margins.top, plotOffset.top)); + plotOffset.bottom = Math.ceil(Math.max(margins.bottom, plotOffset.bottom)); + } + + function setupGrid() { + var i, axes = allAxes(), showGrid = options.grid.show; + + // Initialize the plot's offset from the edge of the canvas + + for (var a in plotOffset) { + var margin = options.grid.margin || 0; + plotOffset[a] = typeof margin == "number" ? margin : margin[a] || 0; + } + + executeHooks(hooks.processOffset, [plotOffset]); + + // If the grid is visible, add its border width to the offset + + for (var a in plotOffset) { + if(typeof(options.grid.borderWidth) == "object") { + plotOffset[a] += showGrid ? options.grid.borderWidth[a] : 0; + } + else { + plotOffset[a] += showGrid ? options.grid.borderWidth : 0; + } + } + + $.each(axes, function (_, axis) { + var axisOpts = axis.options; + axis.show = axisOpts.show == null ? axis.used : axisOpts.show; + axis.reserveSpace = axisOpts.reserveSpace == null ? axis.show : axisOpts.reserveSpace; + setRange(axis); + }); + + if (showGrid) { + + var allocatedAxes = $.grep(axes, function (axis) { + return axis.show || axis.reserveSpace; + }); + + $.each(allocatedAxes, function (_, axis) { + // make the ticks + setupTickGeneration(axis); + setTicks(axis); + snapRangeToTicks(axis, axis.ticks); + // find labelWidth/Height for axis + measureTickLabels(axis); + }); + + // with all dimensions calculated, we can compute the + // axis bounding boxes, start from the outside + // (reverse order) + for (i = allocatedAxes.length - 1; i >= 0; --i) + allocateAxisBoxFirstPhase(allocatedAxes[i]); + + // make sure we've got enough space for things that + // might stick out + adjustLayoutForThingsStickingOut(); + + $.each(allocatedAxes, function (_, axis) { + allocateAxisBoxSecondPhase(axis); + }); + } + + plotWidth = surface.width - plotOffset.left - plotOffset.right; + plotHeight = surface.height - plotOffset.bottom - plotOffset.top; + + // now we got the proper plot dimensions, we can compute the scaling + $.each(axes, function (_, axis) { + setTransformationHelpers(axis); + }); + + if (showGrid) { + drawAxisLabels(); + } + + insertLegend(); + } + + function setRange(axis) { + var opts = axis.options, + min = +(opts.min != null ? opts.min : axis.datamin), + max = +(opts.max != null ? opts.max : axis.datamax), + delta = max - min; + + if (delta == 0.0) { + // degenerate case + var widen = max == 0 ? 1 : 0.01; + + if (opts.min == null) + min -= widen; + // always widen max if we couldn't widen min to ensure we + // don't fall into min == max which doesn't work + if (opts.max == null || opts.min != null) + max += widen; + } + else { + // consider autoscaling + var margin = opts.autoscaleMargin; + if (margin != null) { + if (opts.min == null) { + min -= delta * margin; + // make sure we don't go below zero if all values + // are positive + if (min < 0 && axis.datamin != null && axis.datamin >= 0) + min = 0; + } + if (opts.max == null) { + max += delta * margin; + if (max > 0 && axis.datamax != null && axis.datamax <= 0) + max = 0; + } + } + } + axis.min = min; + axis.max = max; + } + + function setupTickGeneration(axis) { + var opts = axis.options; + + // estimate number of ticks + var noTicks; + if (typeof opts.ticks == "number" && opts.ticks > 0) + noTicks = opts.ticks; + else + // heuristic based on the model a*sqrt(x) fitted to + // some data points that seemed reasonable + noTicks = 0.3 * Math.sqrt(axis.direction == "x" ? surface.width : surface.height); + + var delta = (axis.max - axis.min) / noTicks, + dec = -Math.floor(Math.log(delta) / Math.LN10), + maxDec = opts.tickDecimals; + + if (maxDec != null && dec > maxDec) { + dec = maxDec; + } + + var magn = Math.pow(10, -dec), + norm = delta / magn, // norm is between 1.0 and 10.0 + size; + + if (norm < 1.5) { + size = 1; + } else if (norm < 3) { + size = 2; + // special case for 2.5, requires an extra decimal + if (norm > 2.25 && (maxDec == null || dec + 1 <= maxDec)) { + size = 2.5; + ++dec; + } + } else if (norm < 7.5) { + size = 5; + } else { + size = 10; + } + + size *= magn; + + if (opts.minTickSize != null && size < opts.minTickSize) { + size = opts.minTickSize; + } + + axis.delta = delta; + axis.tickDecimals = Math.max(0, maxDec != null ? maxDec : dec); + axis.tickSize = opts.tickSize || size; + + // Time mode was moved to a plug-in in 0.8, and since so many people use it + // we'll add an especially friendly reminder to make sure they included it. + + if (opts.mode == "time" && !axis.tickGenerator) { + throw new Error("Time mode requires the flot.time plugin."); + } + + // Flot supports base-10 axes; any other mode else is handled by a plug-in, + // like flot.time.js. + + if (!axis.tickGenerator) { + + axis.tickGenerator = function (axis) { + + var ticks = [], + start = floorInBase(axis.min, axis.tickSize), + i = 0, + v = Number.NaN, + prev; + + do { + prev = v; + v = start + i * axis.tickSize; + ticks.push(v); + ++i; + } while (v < axis.max && v != prev); + return ticks; + }; + + axis.tickFormatter = function (value, axis) { + + var factor = axis.tickDecimals ? Math.pow(10, axis.tickDecimals) : 1; + var formatted = "" + Math.round(value * factor) / factor; + + // If tickDecimals was specified, ensure that we have exactly that + // much precision; otherwise default to the value's own precision. + + if (axis.tickDecimals != null) { + var decimal = formatted.indexOf("."); + var precision = decimal == -1 ? 0 : formatted.length - decimal - 1; + if (precision < axis.tickDecimals) { + return (precision ? formatted : formatted + ".") + ("" + factor).substr(1, axis.tickDecimals - precision); + } + } + + return formatted; + }; + } + + if ($.isFunction(opts.tickFormatter)) + axis.tickFormatter = function (v, axis) { return "" + opts.tickFormatter(v, axis); }; + + if (opts.alignTicksWithAxis != null) { + var otherAxis = (axis.direction == "x" ? xaxes : yaxes)[opts.alignTicksWithAxis - 1]; + if (otherAxis && otherAxis.used && otherAxis != axis) { + // consider snapping min/max to outermost nice ticks + var niceTicks = axis.tickGenerator(axis); + if (niceTicks.length > 0) { + if (opts.min == null) + axis.min = Math.min(axis.min, niceTicks[0]); + if (opts.max == null && niceTicks.length > 1) + axis.max = Math.max(axis.max, niceTicks[niceTicks.length - 1]); + } + + axis.tickGenerator = function (axis) { + // copy ticks, scaled to this axis + var ticks = [], v, i; + for (i = 0; i < otherAxis.ticks.length; ++i) { + v = (otherAxis.ticks[i].v - otherAxis.min) / (otherAxis.max - otherAxis.min); + v = axis.min + v * (axis.max - axis.min); + ticks.push(v); + } + return ticks; + }; + + // we might need an extra decimal since forced + // ticks don't necessarily fit naturally + if (!axis.mode && opts.tickDecimals == null) { + var extraDec = Math.max(0, -Math.floor(Math.log(axis.delta) / Math.LN10) + 1), + ts = axis.tickGenerator(axis); + + // only proceed if the tick interval rounded + // with an extra decimal doesn't give us a + // zero at end + if (!(ts.length > 1 && /\..*0$/.test((ts[1] - ts[0]).toFixed(extraDec)))) + axis.tickDecimals = extraDec; + } + } + } + } + + function setTicks(axis) { + var oticks = axis.options.ticks, ticks = []; + if (oticks == null || (typeof oticks == "number" && oticks > 0)) + ticks = axis.tickGenerator(axis); + else if (oticks) { + if ($.isFunction(oticks)) + // generate the ticks + ticks = oticks(axis); + else + ticks = oticks; + } + + // clean up/labelify the supplied ticks, copy them over + var i, v; + axis.ticks = []; + for (i = 0; i < ticks.length; ++i) { + var label = null; + var t = ticks[i]; + if (typeof t == "object") { + v = +t[0]; + if (t.length > 1) + label = t[1]; + } + else + v = +t; + if (label == null) + label = axis.tickFormatter(v, axis); + if (!isNaN(v)) + axis.ticks.push({ v: v, label: label }); + } + } + + function snapRangeToTicks(axis, ticks) { + if (axis.options.autoscaleMargin && ticks.length > 0) { + // snap to ticks + if (axis.options.min == null) + axis.min = Math.min(axis.min, ticks[0].v); + if (axis.options.max == null && ticks.length > 1) + axis.max = Math.max(axis.max, ticks[ticks.length - 1].v); + } + } + + function draw() { + + surface.clear(); + + executeHooks(hooks.drawBackground, [ctx]); + + var grid = options.grid; + + // draw background, if any + if (grid.show && grid.backgroundColor) + drawBackground(); + + if (grid.show && !grid.aboveData) { + drawGrid(); + } + + for (var i = 0; i < series.length; ++i) { + executeHooks(hooks.drawSeries, [ctx, series[i]]); + drawSeries(series[i]); + } + + executeHooks(hooks.draw, [ctx]); + + if (grid.show && grid.aboveData) { + drawGrid(); + } + + surface.render(); + + // A draw implies that either the axes or data have changed, so we + // should probably update the overlay highlights as well. + + triggerRedrawOverlay(); + } + + function extractRange(ranges, coord) { + var axis, from, to, key, axes = allAxes(); + + for (var i = 0; i < axes.length; ++i) { + axis = axes[i]; + if (axis.direction == coord) { + key = coord + axis.n + "axis"; + if (!ranges[key] && axis.n == 1) + key = coord + "axis"; // support x1axis as xaxis + if (ranges[key]) { + from = ranges[key].from; + to = ranges[key].to; + break; + } + } + } + + // backwards-compat stuff - to be removed in future + if (!ranges[key]) { + axis = coord == "x" ? xaxes[0] : yaxes[0]; + from = ranges[coord + "1"]; + to = ranges[coord + "2"]; + } + + // auto-reverse as an added bonus + if (from != null && to != null && from > to) { + var tmp = from; + from = to; + to = tmp; + } + + return { from: from, to: to, axis: axis }; + } + + function drawBackground() { + ctx.save(); + ctx.translate(plotOffset.left, plotOffset.top); + + ctx.fillStyle = getColorOrGradient(options.grid.backgroundColor, plotHeight, 0, "rgba(255, 255, 255, 0)"); + ctx.fillRect(0, 0, plotWidth, plotHeight); + ctx.restore(); + } + + function drawGrid() { + var i, axes, bw, bc; + + ctx.save(); + ctx.translate(plotOffset.left, plotOffset.top); + + // draw markings + var markings = options.grid.markings; + if (markings) { + if ($.isFunction(markings)) { + axes = plot.getAxes(); + // xmin etc. is backwards compatibility, to be + // removed in the future + axes.xmin = axes.xaxis.min; + axes.xmax = axes.xaxis.max; + axes.ymin = axes.yaxis.min; + axes.ymax = axes.yaxis.max; + + markings = markings(axes); + } + + for (i = 0; i < markings.length; ++i) { + var m = markings[i], + xrange = extractRange(m, "x"), + yrange = extractRange(m, "y"); + + // fill in missing + if (xrange.from == null) + xrange.from = xrange.axis.min; + if (xrange.to == null) + xrange.to = xrange.axis.max; + if (yrange.from == null) + yrange.from = yrange.axis.min; + if (yrange.to == null) + yrange.to = yrange.axis.max; + + // clip + if (xrange.to < xrange.axis.min || xrange.from > xrange.axis.max || + yrange.to < yrange.axis.min || yrange.from > yrange.axis.max) + continue; + + xrange.from = Math.max(xrange.from, xrange.axis.min); + xrange.to = Math.min(xrange.to, xrange.axis.max); + yrange.from = Math.max(yrange.from, yrange.axis.min); + yrange.to = Math.min(yrange.to, yrange.axis.max); + + var xequal = xrange.from === xrange.to, + yequal = yrange.from === yrange.to; + + if (xequal && yequal) { + continue; + } + + // then draw + xrange.from = Math.floor(xrange.axis.p2c(xrange.from)); + xrange.to = Math.floor(xrange.axis.p2c(xrange.to)); + yrange.from = Math.floor(yrange.axis.p2c(yrange.from)); + yrange.to = Math.floor(yrange.axis.p2c(yrange.to)); + + if (xequal || yequal) { + var lineWidth = m.lineWidth || options.grid.markingsLineWidth, + subPixel = lineWidth % 2 ? 0.5 : 0; + ctx.beginPath(); + ctx.strokeStyle = m.color || options.grid.markingsColor; + ctx.lineWidth = lineWidth; + if (xequal) { + ctx.moveTo(xrange.to + subPixel, yrange.from); + ctx.lineTo(xrange.to + subPixel, yrange.to); + } else { + ctx.moveTo(xrange.from, yrange.to + subPixel); + ctx.lineTo(xrange.to, yrange.to + subPixel); + } + ctx.stroke(); + } else { + ctx.fillStyle = m.color || options.grid.markingsColor; + ctx.fillRect(xrange.from, yrange.to, + xrange.to - xrange.from, + yrange.from - yrange.to); + } + } + } + + // draw the ticks + axes = allAxes(); + bw = options.grid.borderWidth; + + for (var j = 0; j < axes.length; ++j) { + var axis = axes[j], box = axis.box, + t = axis.tickLength, x, y, xoff, yoff; + if (!axis.show || axis.ticks.length == 0) + continue; + + ctx.lineWidth = 1; + + // find the edges + if (axis.direction == "x") { + x = 0; + if (t == "full") + y = (axis.position == "top" ? 0 : plotHeight); + else + y = box.top - plotOffset.top + (axis.position == "top" ? box.height : 0); + } + else { + y = 0; + if (t == "full") + x = (axis.position == "left" ? 0 : plotWidth); + else + x = box.left - plotOffset.left + (axis.position == "left" ? box.width : 0); + } + + // draw tick bar + if (!axis.innermost) { + ctx.strokeStyle = axis.options.color; + ctx.beginPath(); + xoff = yoff = 0; + if (axis.direction == "x") + xoff = plotWidth + 1; + else + yoff = plotHeight + 1; + + if (ctx.lineWidth == 1) { + if (axis.direction == "x") { + y = Math.floor(y) + 0.5; + } else { + x = Math.floor(x) + 0.5; + } + } + + ctx.moveTo(x, y); + ctx.lineTo(x + xoff, y + yoff); + ctx.stroke(); + } + + // draw ticks + + ctx.strokeStyle = axis.options.tickColor; + + ctx.beginPath(); + for (i = 0; i < axis.ticks.length; ++i) { + var v = axis.ticks[i].v; + + xoff = yoff = 0; + + if (isNaN(v) || v < axis.min || v > axis.max + // skip those lying on the axes if we got a border + || (t == "full" + && ((typeof bw == "object" && bw[axis.position] > 0) || bw > 0) + && (v == axis.min || v == axis.max))) + continue; + + if (axis.direction == "x") { + x = axis.p2c(v); + yoff = t == "full" ? -plotHeight : t; + + if (axis.position == "top") + yoff = -yoff; + } + else { + y = axis.p2c(v); + xoff = t == "full" ? -plotWidth : t; + + if (axis.position == "left") + xoff = -xoff; + } + + if (ctx.lineWidth == 1) { + if (axis.direction == "x") + x = Math.floor(x) + 0.5; + else + y = Math.floor(y) + 0.5; + } + + ctx.moveTo(x, y); + ctx.lineTo(x + xoff, y + yoff); + } + + ctx.stroke(); + } + + + // draw border + if (bw) { + // If either borderWidth or borderColor is an object, then draw the border + // line by line instead of as one rectangle + bc = options.grid.borderColor; + if(typeof bw == "object" || typeof bc == "object") { + if (typeof bw !== "object") { + bw = {top: bw, right: bw, bottom: bw, left: bw}; + } + if (typeof bc !== "object") { + bc = {top: bc, right: bc, bottom: bc, left: bc}; + } + + if (bw.top > 0) { + ctx.strokeStyle = bc.top; + ctx.lineWidth = bw.top; + ctx.beginPath(); + ctx.moveTo(0 - bw.left, 0 - bw.top/2); + ctx.lineTo(plotWidth, 0 - bw.top/2); + ctx.stroke(); + } + + if (bw.right > 0) { + ctx.strokeStyle = bc.right; + ctx.lineWidth = bw.right; + ctx.beginPath(); + ctx.moveTo(plotWidth + bw.right / 2, 0 - bw.top); + ctx.lineTo(plotWidth + bw.right / 2, plotHeight); + ctx.stroke(); + } + + if (bw.bottom > 0) { + ctx.strokeStyle = bc.bottom; + ctx.lineWidth = bw.bottom; + ctx.beginPath(); + ctx.moveTo(plotWidth + bw.right, plotHeight + bw.bottom / 2); + ctx.lineTo(0, plotHeight + bw.bottom / 2); + ctx.stroke(); + } + + if (bw.left > 0) { + ctx.strokeStyle = bc.left; + ctx.lineWidth = bw.left; + ctx.beginPath(); + ctx.moveTo(0 - bw.left/2, plotHeight + bw.bottom); + ctx.lineTo(0- bw.left/2, 0); + ctx.stroke(); + } + } + else { + ctx.lineWidth = bw; + ctx.strokeStyle = options.grid.borderColor; + ctx.strokeRect(-bw/2, -bw/2, plotWidth + bw, plotHeight + bw); + } + } + + ctx.restore(); + } + + function drawAxisLabels() { + + $.each(allAxes(), function (_, axis) { + var box = axis.box, + legacyStyles = axis.direction + "Axis " + axis.direction + axis.n + "Axis", + layer = "flot-" + axis.direction + "-axis flot-" + axis.direction + axis.n + "-axis " + legacyStyles, + font = axis.options.font || "flot-tick-label tickLabel", + tick, x, y, halign, valign; + + // Remove text before checking for axis.show and ticks.length; + // otherwise plugins, like flot-tickrotor, that draw their own + // tick labels will end up with both theirs and the defaults. + + surface.removeText(layer); + + if (!axis.show || axis.ticks.length == 0) + return; + + for (var i = 0; i < axis.ticks.length; ++i) { + + tick = axis.ticks[i]; + if (!tick.label || tick.v < axis.min || tick.v > axis.max) + continue; + + if (axis.direction == "x") { + halign = "center"; + x = plotOffset.left + axis.p2c(tick.v); + if (axis.position == "bottom") { + y = box.top + box.padding; + } else { + y = box.top + box.height - box.padding; + valign = "bottom"; + } + } else { + valign = "middle"; + y = plotOffset.top + axis.p2c(tick.v); + if (axis.position == "left") { + x = box.left + box.width - box.padding; + halign = "right"; + } else { + x = box.left + box.padding; + } + } + + surface.addText(layer, x, y, tick.label, font, null, null, halign, valign); + } + }); + } + + function drawSeries(series) { + if (series.lines.show) + drawSeriesLines(series); + if (series.bars.show) + drawSeriesBars(series); + if (series.points.show) + drawSeriesPoints(series); + } + + function drawSeriesLines(series) { + function plotLine(datapoints, xoffset, yoffset, axisx, axisy) { + var points = datapoints.points, + ps = datapoints.pointsize, + prevx = null, prevy = null; + + ctx.beginPath(); + for (var i = ps; i < points.length; i += ps) { + var x1 = points[i - ps], y1 = points[i - ps + 1], + x2 = points[i], y2 = points[i + 1]; + + if (x1 == null || x2 == null) + continue; + + // clip with ymin + if (y1 <= y2 && y1 < axisy.min) { + if (y2 < axisy.min) + continue; // line segment is outside + // compute new intersection point + x1 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1; + y1 = axisy.min; + } + else if (y2 <= y1 && y2 < axisy.min) { + if (y1 < axisy.min) + continue; + x2 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1; + y2 = axisy.min; + } + + // clip with ymax + if (y1 >= y2 && y1 > axisy.max) { + if (y2 > axisy.max) + continue; + x1 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1; + y1 = axisy.max; + } + else if (y2 >= y1 && y2 > axisy.max) { + if (y1 > axisy.max) + continue; + x2 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1; + y2 = axisy.max; + } + + // clip with xmin + if (x1 <= x2 && x1 < axisx.min) { + if (x2 < axisx.min) + continue; + y1 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1; + x1 = axisx.min; + } + else if (x2 <= x1 && x2 < axisx.min) { + if (x1 < axisx.min) + continue; + y2 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1; + x2 = axisx.min; + } + + // clip with xmax + if (x1 >= x2 && x1 > axisx.max) { + if (x2 > axisx.max) + continue; + y1 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1; + x1 = axisx.max; + } + else if (x2 >= x1 && x2 > axisx.max) { + if (x1 > axisx.max) + continue; + y2 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1; + x2 = axisx.max; + } + + if (x1 != prevx || y1 != prevy) + ctx.moveTo(axisx.p2c(x1) + xoffset, axisy.p2c(y1) + yoffset); + + prevx = x2; + prevy = y2; + ctx.lineTo(axisx.p2c(x2) + xoffset, axisy.p2c(y2) + yoffset); + } + ctx.stroke(); + } + + function plotLineArea(datapoints, axisx, axisy) { + var points = datapoints.points, + ps = datapoints.pointsize, + bottom = Math.min(Math.max(0, axisy.min), axisy.max), + i = 0, top, areaOpen = false, + ypos = 1, segmentStart = 0, segmentEnd = 0; + + // we process each segment in two turns, first forward + // direction to sketch out top, then once we hit the + // end we go backwards to sketch the bottom + while (true) { + if (ps > 0 && i > points.length + ps) + break; + + i += ps; // ps is negative if going backwards + + var x1 = points[i - ps], + y1 = points[i - ps + ypos], + x2 = points[i], y2 = points[i + ypos]; + + if (areaOpen) { + if (ps > 0 && x1 != null && x2 == null) { + // at turning point + segmentEnd = i; + ps = -ps; + ypos = 2; + continue; + } + + if (ps < 0 && i == segmentStart + ps) { + // done with the reverse sweep + ctx.fill(); + areaOpen = false; + ps = -ps; + ypos = 1; + i = segmentStart = segmentEnd + ps; + continue; + } + } + + if (x1 == null || x2 == null) + continue; + + // clip x values + + // clip with xmin + if (x1 <= x2 && x1 < axisx.min) { + if (x2 < axisx.min) + continue; + y1 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1; + x1 = axisx.min; + } + else if (x2 <= x1 && x2 < axisx.min) { + if (x1 < axisx.min) + continue; + y2 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1; + x2 = axisx.min; + } + + // clip with xmax + if (x1 >= x2 && x1 > axisx.max) { + if (x2 > axisx.max) + continue; + y1 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1; + x1 = axisx.max; + } + else if (x2 >= x1 && x2 > axisx.max) { + if (x1 > axisx.max) + continue; + y2 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1; + x2 = axisx.max; + } + + if (!areaOpen) { + // open area + ctx.beginPath(); + ctx.moveTo(axisx.p2c(x1), axisy.p2c(bottom)); + areaOpen = true; + } + + // now first check the case where both is outside + if (y1 >= axisy.max && y2 >= axisy.max) { + ctx.lineTo(axisx.p2c(x1), axisy.p2c(axisy.max)); + ctx.lineTo(axisx.p2c(x2), axisy.p2c(axisy.max)); + continue; + } + else if (y1 <= axisy.min && y2 <= axisy.min) { + ctx.lineTo(axisx.p2c(x1), axisy.p2c(axisy.min)); + ctx.lineTo(axisx.p2c(x2), axisy.p2c(axisy.min)); + continue; + } + + // else it's a bit more complicated, there might + // be a flat maxed out rectangle first, then a + // triangular cutout or reverse; to find these + // keep track of the current x values + var x1old = x1, x2old = x2; + + // clip the y values, without shortcutting, we + // go through all cases in turn + + // clip with ymin + if (y1 <= y2 && y1 < axisy.min && y2 >= axisy.min) { + x1 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1; + y1 = axisy.min; + } + else if (y2 <= y1 && y2 < axisy.min && y1 >= axisy.min) { + x2 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1; + y2 = axisy.min; + } + + // clip with ymax + if (y1 >= y2 && y1 > axisy.max && y2 <= axisy.max) { + x1 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1; + y1 = axisy.max; + } + else if (y2 >= y1 && y2 > axisy.max && y1 <= axisy.max) { + x2 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1; + y2 = axisy.max; + } + + // if the x value was changed we got a rectangle + // to fill + if (x1 != x1old) { + ctx.lineTo(axisx.p2c(x1old), axisy.p2c(y1)); + // it goes to (x1, y1), but we fill that below + } + + // fill triangular section, this sometimes result + // in redundant points if (x1, y1) hasn't changed + // from previous line to, but we just ignore that + ctx.lineTo(axisx.p2c(x1), axisy.p2c(y1)); + ctx.lineTo(axisx.p2c(x2), axisy.p2c(y2)); + + // fill the other rectangle if it's there + if (x2 != x2old) { + ctx.lineTo(axisx.p2c(x2), axisy.p2c(y2)); + ctx.lineTo(axisx.p2c(x2old), axisy.p2c(y2)); + } + } + } + + ctx.save(); + ctx.translate(plotOffset.left, plotOffset.top); + ctx.lineJoin = "round"; + + var lw = series.lines.lineWidth, + sw = series.shadowSize; + // FIXME: consider another form of shadow when filling is turned on + if (lw > 0 && sw > 0) { + // draw shadow as a thick and thin line with transparency + ctx.lineWidth = sw; + ctx.strokeStyle = "rgba(0,0,0,0.1)"; + // position shadow at angle from the mid of line + var angle = Math.PI/18; + plotLine(series.datapoints, Math.sin(angle) * (lw/2 + sw/2), Math.cos(angle) * (lw/2 + sw/2), series.xaxis, series.yaxis); + ctx.lineWidth = sw/2; + plotLine(series.datapoints, Math.sin(angle) * (lw/2 + sw/4), Math.cos(angle) * (lw/2 + sw/4), series.xaxis, series.yaxis); + } + + ctx.lineWidth = lw; + ctx.strokeStyle = series.color; + var fillStyle = getFillStyle(series.lines, series.color, 0, plotHeight); + if (fillStyle) { + ctx.fillStyle = fillStyle; + plotLineArea(series.datapoints, series.xaxis, series.yaxis); + } + + if (lw > 0) + plotLine(series.datapoints, 0, 0, series.xaxis, series.yaxis); + ctx.restore(); + } + + function drawSeriesPoints(series) { + function plotPoints(datapoints, radius, fillStyle, offset, shadow, axisx, axisy, symbol) { + var points = datapoints.points, ps = datapoints.pointsize; + + for (var i = 0; i < points.length; i += ps) { + var x = points[i], y = points[i + 1]; + if (x == null || x < axisx.min || x > axisx.max || y < axisy.min || y > axisy.max) + continue; + + ctx.beginPath(); + x = axisx.p2c(x); + y = axisy.p2c(y) + offset; + if (symbol == "circle") + ctx.arc(x, y, radius, 0, shadow ? Math.PI : Math.PI * 2, false); + else + symbol(ctx, x, y, radius, shadow); + ctx.closePath(); + + if (fillStyle) { + ctx.fillStyle = fillStyle; + ctx.fill(); + } + ctx.stroke(); + } + } + + ctx.save(); + ctx.translate(plotOffset.left, plotOffset.top); + + var lw = series.points.lineWidth, + sw = series.shadowSize, + radius = series.points.radius, + symbol = series.points.symbol; + + // If the user sets the line width to 0, we change it to a very + // small value. A line width of 0 seems to force the default of 1. + // Doing the conditional here allows the shadow setting to still be + // optional even with a lineWidth of 0. + + if( lw == 0 ) + lw = 0.0001; + + if (lw > 0 && sw > 0) { + // draw shadow in two steps + var w = sw / 2; + ctx.lineWidth = w; + ctx.strokeStyle = "rgba(0,0,0,0.1)"; + plotPoints(series.datapoints, radius, null, w + w/2, true, + series.xaxis, series.yaxis, symbol); + + ctx.strokeStyle = "rgba(0,0,0,0.2)"; + plotPoints(series.datapoints, radius, null, w/2, true, + series.xaxis, series.yaxis, symbol); + } + + ctx.lineWidth = lw; + ctx.strokeStyle = series.color; + plotPoints(series.datapoints, radius, + getFillStyle(series.points, series.color), 0, false, + series.xaxis, series.yaxis, symbol); + ctx.restore(); + } + + function drawBar(x, y, b, barLeft, barRight, fillStyleCallback, axisx, axisy, c, horizontal, lineWidth) { + var left, right, bottom, top, + drawLeft, drawRight, drawTop, drawBottom, + tmp; + + // in horizontal mode, we start the bar from the left + // instead of from the bottom so it appears to be + // horizontal rather than vertical + if (horizontal) { + drawBottom = drawRight = drawTop = true; + drawLeft = false; + left = b; + right = x; + top = y + barLeft; + bottom = y + barRight; + + // account for negative bars + if (right < left) { + tmp = right; + right = left; + left = tmp; + drawLeft = true; + drawRight = false; + } + } + else { + drawLeft = drawRight = drawTop = true; + drawBottom = false; + left = x + barLeft; + right = x + barRight; + bottom = b; + top = y; + + // account for negative bars + if (top < bottom) { + tmp = top; + top = bottom; + bottom = tmp; + drawBottom = true; + drawTop = false; + } + } + + // clip + if (right < axisx.min || left > axisx.max || + top < axisy.min || bottom > axisy.max) + return; + + if (left < axisx.min) { + left = axisx.min; + drawLeft = false; + } + + if (right > axisx.max) { + right = axisx.max; + drawRight = false; + } + + if (bottom < axisy.min) { + bottom = axisy.min; + drawBottom = false; + } + + if (top > axisy.max) { + top = axisy.max; + drawTop = false; + } + + left = axisx.p2c(left); + bottom = axisy.p2c(bottom); + right = axisx.p2c(right); + top = axisy.p2c(top); + + // fill the bar + if (fillStyleCallback) { + c.fillStyle = fillStyleCallback(bottom, top); + c.fillRect(left, top, right - left, bottom - top) + } + + // draw outline + if (lineWidth > 0 && (drawLeft || drawRight || drawTop || drawBottom)) { + c.beginPath(); + + // FIXME: inline moveTo is buggy with excanvas + c.moveTo(left, bottom); + if (drawLeft) + c.lineTo(left, top); + else + c.moveTo(left, top); + if (drawTop) + c.lineTo(right, top); + else + c.moveTo(right, top); + if (drawRight) + c.lineTo(right, bottom); + else + c.moveTo(right, bottom); + if (drawBottom) + c.lineTo(left, bottom); + else + c.moveTo(left, bottom); + c.stroke(); + } + } + + function drawSeriesBars(series) { + function plotBars(datapoints, barLeft, barRight, fillStyleCallback, axisx, axisy) { + var points = datapoints.points, ps = datapoints.pointsize; + + for (var i = 0; i < points.length; i += ps) { + if (points[i] == null) + continue; + drawBar(points[i], points[i + 1], points[i + 2], barLeft, barRight, fillStyleCallback, axisx, axisy, ctx, series.bars.horizontal, series.bars.lineWidth); + } + } + + ctx.save(); + ctx.translate(plotOffset.left, plotOffset.top); + + // FIXME: figure out a way to add shadows (for instance along the right edge) + ctx.lineWidth = series.bars.lineWidth; + ctx.strokeStyle = series.color; + + var barLeft; + + switch (series.bars.align) { + case "left": + barLeft = 0; + break; + case "right": + barLeft = -series.bars.barWidth; + break; + default: + barLeft = -series.bars.barWidth / 2; + } + + var fillStyleCallback = series.bars.fill ? function (bottom, top) { return getFillStyle(series.bars, series.color, bottom, top); } : null; + plotBars(series.datapoints, barLeft, barLeft + series.bars.barWidth, fillStyleCallback, series.xaxis, series.yaxis); + ctx.restore(); + } + + function getFillStyle(filloptions, seriesColor, bottom, top) { + var fill = filloptions.fill; + if (!fill) + return null; + + if (filloptions.fillColor) + return getColorOrGradient(filloptions.fillColor, bottom, top, seriesColor); + + var c = $.color.parse(seriesColor); + c.a = typeof fill == "number" ? fill : 0.4; + c.normalize(); + return c.toString(); + } + + function insertLegend() { + + if (options.legend.container != null) { + $(options.legend.container).html(""); + } else { + placeholder.find(".legend").remove(); + } + + if (!options.legend.show) { + return; + } + + var fragments = [], entries = [], rowStarted = false, + lf = options.legend.labelFormatter, s, label; + + // Build a list of legend entries, with each having a label and a color + + for (var i = 0; i < series.length; ++i) { + s = series[i]; + if (s.label) { + label = lf ? lf(s.label, s) : s.label; + if (label) { + entries.push({ + label: label, + color: s.color + }); + } + } + } + + // Sort the legend using either the default or a custom comparator + + if (options.legend.sorted) { + if ($.isFunction(options.legend.sorted)) { + entries.sort(options.legend.sorted); + } else if (options.legend.sorted == "reverse") { + entries.reverse(); + } else { + var ascending = options.legend.sorted != "descending"; + entries.sort(function(a, b) { + return a.label == b.label ? 0 : ( + (a.label < b.label) != ascending ? 1 : -1 // Logical XOR + ); + }); + } + } + + // Generate markup for the list of entries, in their final order + + for (var i = 0; i < entries.length; ++i) { + + var entry = entries[i]; + + if (i % options.legend.noColumns == 0) { + if (rowStarted) + fragments.push(''); + fragments.push(''); + rowStarted = true; + } + + fragments.push( + '
' + + '' + entry.label + '' + ); + } + + if (rowStarted) + fragments.push(''); + + if (fragments.length == 0) + return; + + var table = '' + fragments.join("") + '
'; + if (options.legend.container != null) + $(options.legend.container).html(table); + else { + var pos = "", + p = options.legend.position, + m = options.legend.margin; + if (m[0] == null) + m = [m, m]; + if (p.charAt(0) == "n") + pos += 'top:' + (m[1] + plotOffset.top) + 'px;'; + else if (p.charAt(0) == "s") + pos += 'bottom:' + (m[1] + plotOffset.bottom) + 'px;'; + if (p.charAt(1) == "e") + pos += 'right:' + (m[0] + plotOffset.right) + 'px;'; + else if (p.charAt(1) == "w") + pos += 'left:' + (m[0] + plotOffset.left) + 'px;'; + var legend = $('
' + table.replace('style="', 'style="position:absolute;' + pos +';') + '
').appendTo(placeholder); + if (options.legend.backgroundOpacity != 0.0) { + // put in the transparent background + // separately to avoid blended labels and + // label boxes + var c = options.legend.backgroundColor; + if (c == null) { + c = options.grid.backgroundColor; + if (c && typeof c == "string") + c = $.color.parse(c); + else + c = $.color.extract(legend, 'background-color'); + c.a = 1; + c = c.toString(); + } + var div = legend.children(); + $('
').prependTo(legend).css('opacity', options.legend.backgroundOpacity); + } + } + } + + + // interactive features + + var highlights = [], + redrawTimeout = null; + + // returns the data item the mouse is over, or null if none is found + function findNearbyItem(mouseX, mouseY, seriesFilter) { + var maxDistance = options.grid.mouseActiveRadius, + smallestDistance = maxDistance * maxDistance + 1, + item = null, foundPoint = false, i, j, ps; + + for (i = series.length - 1; i >= 0; --i) { + if (!seriesFilter(series[i])) + continue; + + var s = series[i], + axisx = s.xaxis, + axisy = s.yaxis, + points = s.datapoints.points, + mx = axisx.c2p(mouseX), // precompute some stuff to make the loop faster + my = axisy.c2p(mouseY), + maxx = maxDistance / axisx.scale, + maxy = maxDistance / axisy.scale; + + ps = s.datapoints.pointsize; + // with inverse transforms, we can't use the maxx/maxy + // optimization, sadly + if (axisx.options.inverseTransform) + maxx = Number.MAX_VALUE; + if (axisy.options.inverseTransform) + maxy = Number.MAX_VALUE; + + if (s.lines.show || s.points.show) { + for (j = 0; j < points.length; j += ps) { + var x = points[j], y = points[j + 1]; + if (x == null) + continue; + + // For points and lines, the cursor must be within a + // certain distance to the data point + if (x - mx > maxx || x - mx < -maxx || + y - my > maxy || y - my < -maxy) + continue; + + // We have to calculate distances in pixels, not in + // data units, because the scales of the axes may be different + var dx = Math.abs(axisx.p2c(x) - mouseX), + dy = Math.abs(axisy.p2c(y) - mouseY), + dist = dx * dx + dy * dy; // we save the sqrt + + // use <= to ensure last point takes precedence + // (last generally means on top of) + if (dist < smallestDistance) { + smallestDistance = dist; + item = [i, j / ps]; + } + } + } + + if (s.bars.show && !item) { // no other point can be nearby + + var barLeft, barRight; + + switch (s.bars.align) { + case "left": + barLeft = 0; + break; + case "right": + barLeft = -s.bars.barWidth; + break; + default: + barLeft = -s.bars.barWidth / 2; + } + + barRight = barLeft + s.bars.barWidth; + + for (j = 0; j < points.length; j += ps) { + var x = points[j], y = points[j + 1], b = points[j + 2]; + if (x == null) + continue; + + // for a bar graph, the cursor must be inside the bar + if (series[i].bars.horizontal ? + (mx <= Math.max(b, x) && mx >= Math.min(b, x) && + my >= y + barLeft && my <= y + barRight) : + (mx >= x + barLeft && mx <= x + barRight && + my >= Math.min(b, y) && my <= Math.max(b, y))) + item = [i, j / ps]; + } + } + } + + if (item) { + i = item[0]; + j = item[1]; + ps = series[i].datapoints.pointsize; + + return { datapoint: series[i].datapoints.points.slice(j * ps, (j + 1) * ps), + dataIndex: j, + series: series[i], + seriesIndex: i }; + } + + return null; + } + + function onMouseMove(e) { + if (options.grid.hoverable) + triggerClickHoverEvent("plothover", e, + function (s) { return s["hoverable"] != false; }); + } + + function onMouseLeave(e) { + if (options.grid.hoverable) + triggerClickHoverEvent("plothover", e, + function (s) { return false; }); + } + + function onClick(e) { + triggerClickHoverEvent("plotclick", e, + function (s) { return s["clickable"] != false; }); + } + + // trigger click or hover event (they send the same parameters + // so we share their code) + function triggerClickHoverEvent(eventname, event, seriesFilter) { + var offset = eventHolder.offset(), + canvasX = event.pageX - offset.left - plotOffset.left, + canvasY = event.pageY - offset.top - plotOffset.top, + pos = canvasToAxisCoords({ left: canvasX, top: canvasY }); + + pos.pageX = event.pageX; + pos.pageY = event.pageY; + + var item = findNearbyItem(canvasX, canvasY, seriesFilter); + + if (item) { + // fill in mouse pos for any listeners out there + item.pageX = parseInt(item.series.xaxis.p2c(item.datapoint[0]) + offset.left + plotOffset.left, 10); + item.pageY = parseInt(item.series.yaxis.p2c(item.datapoint[1]) + offset.top + plotOffset.top, 10); + } + + if (options.grid.autoHighlight) { + // clear auto-highlights + for (var i = 0; i < highlights.length; ++i) { + var h = highlights[i]; + if (h.auto == eventname && + !(item && h.series == item.series && + h.point[0] == item.datapoint[0] && + h.point[1] == item.datapoint[1])) + unhighlight(h.series, h.point); + } + + if (item) + highlight(item.series, item.datapoint, eventname); + } + + placeholder.trigger(eventname, [ pos, item ]); + } + + function triggerRedrawOverlay() { + var t = options.interaction.redrawOverlayInterval; + if (t == -1) { // skip event queue + drawOverlay(); + return; + } + + if (!redrawTimeout) + redrawTimeout = setTimeout(drawOverlay, t); + } + + function drawOverlay() { + redrawTimeout = null; + + // draw highlights + octx.save(); + overlay.clear(); + octx.translate(plotOffset.left, plotOffset.top); + + var i, hi; + for (i = 0; i < highlights.length; ++i) { + hi = highlights[i]; + + if (hi.series.bars.show) + drawBarHighlight(hi.series, hi.point); + else + drawPointHighlight(hi.series, hi.point); + } + octx.restore(); + + executeHooks(hooks.drawOverlay, [octx]); + } + + function highlight(s, point, auto) { + if (typeof s == "number") + s = series[s]; + + if (typeof point == "number") { + var ps = s.datapoints.pointsize; + point = s.datapoints.points.slice(ps * point, ps * (point + 1)); + } + + var i = indexOfHighlight(s, point); + if (i == -1) { + highlights.push({ series: s, point: point, auto: auto }); + + triggerRedrawOverlay(); + } + else if (!auto) + highlights[i].auto = false; + } + + function unhighlight(s, point) { + if (s == null && point == null) { + highlights = []; + triggerRedrawOverlay(); + return; + } + + if (typeof s == "number") + s = series[s]; + + if (typeof point == "number") { + var ps = s.datapoints.pointsize; + point = s.datapoints.points.slice(ps * point, ps * (point + 1)); + } + + var i = indexOfHighlight(s, point); + if (i != -1) { + highlights.splice(i, 1); + + triggerRedrawOverlay(); + } + } + + function indexOfHighlight(s, p) { + for (var i = 0; i < highlights.length; ++i) { + var h = highlights[i]; + if (h.series == s && h.point[0] == p[0] + && h.point[1] == p[1]) + return i; + } + return -1; + } + + function drawPointHighlight(series, point) { + var x = point[0], y = point[1], + axisx = series.xaxis, axisy = series.yaxis, + highlightColor = (typeof series.highlightColor === "string") ? series.highlightColor : $.color.parse(series.color).scale('a', 0.5).toString(); + + if (x < axisx.min || x > axisx.max || y < axisy.min || y > axisy.max) + return; + + var pointRadius = series.points.radius + series.points.lineWidth / 2; + octx.lineWidth = pointRadius; + octx.strokeStyle = highlightColor; + var radius = 1.5 * pointRadius; + x = axisx.p2c(x); + y = axisy.p2c(y); + + octx.beginPath(); + if (series.points.symbol == "circle") + octx.arc(x, y, radius, 0, 2 * Math.PI, false); + else + series.points.symbol(octx, x, y, radius, false); + octx.closePath(); + octx.stroke(); + } + + function drawBarHighlight(series, point) { + var highlightColor = (typeof series.highlightColor === "string") ? series.highlightColor : $.color.parse(series.color).scale('a', 0.5).toString(), + fillStyle = highlightColor, + barLeft; + + switch (series.bars.align) { + case "left": + barLeft = 0; + break; + case "right": + barLeft = -series.bars.barWidth; + break; + default: + barLeft = -series.bars.barWidth / 2; + } + + octx.lineWidth = series.bars.lineWidth; + octx.strokeStyle = highlightColor; + + drawBar(point[0], point[1], point[2] || 0, barLeft, barLeft + series.bars.barWidth, + function () { return fillStyle; }, series.xaxis, series.yaxis, octx, series.bars.horizontal, series.bars.lineWidth); + } + + function getColorOrGradient(spec, bottom, top, defaultColor) { + if (typeof spec == "string") + return spec; + else { + // assume this is a gradient spec; IE currently only + // supports a simple vertical gradient properly, so that's + // what we support too + var gradient = ctx.createLinearGradient(0, top, 0, bottom); + + for (var i = 0, l = spec.colors.length; i < l; ++i) { + var c = spec.colors[i]; + if (typeof c != "string") { + var co = $.color.parse(defaultColor); + if (c.brightness != null) + co = co.scale('rgb', c.brightness); + if (c.opacity != null) + co.a *= c.opacity; + c = co.toString(); + } + gradient.addColorStop(i / (l - 1), c); + } + + return gradient; + } + } + } + + // Add the plot function to the top level of the jQuery object + + $.plot = function(placeholder, data, options) { + //var t0 = new Date(); + var plot = new Plot($(placeholder), data, options, $.plot.plugins); + //(window.console ? console.log : alert)("time used (msecs): " + ((new Date()).getTime() - t0.getTime())); + return plot; + }; + + $.plot.version = "0.8.3"; + + $.plot.plugins = []; + + // Also add the plot function as a chainable property + + $.fn.plot = function(data, options) { + return this.each(function() { + $.plot(this, data, options); + }); + }; + + // round to nearby lower multiple of base + function floorInBase(n, base) { + return base * Math.floor(n / base); + } + +})(jQuery); diff --git a/src/plugins/timelion/public/webpackShims/jquery.flot.selection.js b/src/plugins/timelion/public/webpackShims/jquery.flot.selection.js new file mode 100644 index 0000000000000..c8707b30f4e6f --- /dev/null +++ b/src/plugins/timelion/public/webpackShims/jquery.flot.selection.js @@ -0,0 +1,360 @@ +/* Flot plugin for selecting regions of a plot. + +Copyright (c) 2007-2014 IOLA and Ole Laursen. +Licensed under the MIT license. + +The plugin supports these options: + +selection: { + mode: null or "x" or "y" or "xy", + color: color, + shape: "round" or "miter" or "bevel", + minSize: number of pixels +} + +Selection support is enabled by setting the mode to one of "x", "y" or "xy". +In "x" mode, the user will only be able to specify the x range, similarly for +"y" mode. For "xy", the selection becomes a rectangle where both ranges can be +specified. "color" is color of the selection (if you need to change the color +later on, you can get to it with plot.getOptions().selection.color). "shape" +is the shape of the corners of the selection. + +"minSize" is the minimum size a selection can be in pixels. This value can +be customized to determine the smallest size a selection can be and still +have the selection rectangle be displayed. When customizing this value, the +fact that it refers to pixels, not axis units must be taken into account. +Thus, for example, if there is a bar graph in time mode with BarWidth set to 1 +minute, setting "minSize" to 1 will not make the minimum selection size 1 +minute, but rather 1 pixel. Note also that setting "minSize" to 0 will prevent +"plotunselected" events from being fired when the user clicks the mouse without +dragging. + +When selection support is enabled, a "plotselected" event will be emitted on +the DOM element you passed into the plot function. The event handler gets a +parameter with the ranges selected on the axes, like this: + + placeholder.bind( "plotselected", function( event, ranges ) { + alert("You selected " + ranges.xaxis.from + " to " + ranges.xaxis.to) + // similar for yaxis - with multiple axes, the extra ones are in + // x2axis, x3axis, ... + }); + +The "plotselected" event is only fired when the user has finished making the +selection. A "plotselecting" event is fired during the process with the same +parameters as the "plotselected" event, in case you want to know what's +happening while it's happening, + +A "plotunselected" event with no arguments is emitted when the user clicks the +mouse to remove the selection. As stated above, setting "minSize" to 0 will +destroy this behavior. + +The plugin also adds the following methods to the plot object: + +- setSelection( ranges, preventEvent ) + + Set the selection rectangle. The passed in ranges is on the same form as + returned in the "plotselected" event. If the selection mode is "x", you + should put in either an xaxis range, if the mode is "y" you need to put in + an yaxis range and both xaxis and yaxis if the selection mode is "xy", like + this: + + setSelection({ xaxis: { from: 0, to: 10 }, yaxis: { from: 40, to: 60 } }); + + setSelection will trigger the "plotselected" event when called. If you don't + want that to happen, e.g. if you're inside a "plotselected" handler, pass + true as the second parameter. If you are using multiple axes, you can + specify the ranges on any of those, e.g. as x2axis/x3axis/... instead of + xaxis, the plugin picks the first one it sees. + +- clearSelection( preventEvent ) + + Clear the selection rectangle. Pass in true to avoid getting a + "plotunselected" event. + +- getSelection() + + Returns the current selection in the same format as the "plotselected" + event. If there's currently no selection, the function returns null. + +*/ + +(function ($) { + function init(plot) { + var selection = { + first: { x: -1, y: -1}, second: { x: -1, y: -1}, + show: false, + active: false + }; + + // FIXME: The drag handling implemented here should be + // abstracted out, there's some similar code from a library in + // the navigation plugin, this should be massaged a bit to fit + // the Flot cases here better and reused. Doing this would + // make this plugin much slimmer. + var savedhandlers = {}; + + var mouseUpHandler = null; + + function onMouseMove(e) { + if (selection.active) { + updateSelection(e); + + plot.getPlaceholder().trigger("plotselecting", [ getSelection() ]); + } + } + + function onMouseDown(e) { + if (e.which != 1) // only accept left-click + return; + + // cancel out any text selections + document.body.focus(); + + // prevent text selection and drag in old-school browsers + if (document.onselectstart !== undefined && savedhandlers.onselectstart == null) { + savedhandlers.onselectstart = document.onselectstart; + document.onselectstart = function () { return false; }; + } + if (document.ondrag !== undefined && savedhandlers.ondrag == null) { + savedhandlers.ondrag = document.ondrag; + document.ondrag = function () { return false; }; + } + + setSelectionPos(selection.first, e); + + selection.active = true; + + // this is a bit silly, but we have to use a closure to be + // able to whack the same handler again + mouseUpHandler = function (e) { onMouseUp(e); }; + + $(document).one("mouseup", mouseUpHandler); + } + + function onMouseUp(e) { + mouseUpHandler = null; + + // revert drag stuff for old-school browsers + if (document.onselectstart !== undefined) + document.onselectstart = savedhandlers.onselectstart; + if (document.ondrag !== undefined) + document.ondrag = savedhandlers.ondrag; + + // no more dragging + selection.active = false; + updateSelection(e); + + if (selectionIsSane()) + triggerSelectedEvent(); + else { + // this counts as a clear + plot.getPlaceholder().trigger("plotunselected", [ ]); + plot.getPlaceholder().trigger("plotselecting", [ null ]); + } + + return false; + } + + function getSelection() { + if (!selectionIsSane()) + return null; + + if (!selection.show) return null; + + var r = {}, c1 = selection.first, c2 = selection.second; + $.each(plot.getAxes(), function (name, axis) { + if (axis.used) { + var p1 = axis.c2p(c1[axis.direction]), p2 = axis.c2p(c2[axis.direction]); + r[name] = { from: Math.min(p1, p2), to: Math.max(p1, p2) }; + } + }); + return r; + } + + function triggerSelectedEvent() { + var r = getSelection(); + + plot.getPlaceholder().trigger("plotselected", [ r ]); + + // backwards-compat stuff, to be removed in future + if (r.xaxis && r.yaxis) + plot.getPlaceholder().trigger("selected", [ { x1: r.xaxis.from, y1: r.yaxis.from, x2: r.xaxis.to, y2: r.yaxis.to } ]); + } + + function clamp(min, value, max) { + return value < min ? min: (value > max ? max: value); + } + + function setSelectionPos(pos, e) { + var o = plot.getOptions(); + var offset = plot.getPlaceholder().offset(); + var plotOffset = plot.getPlotOffset(); + pos.x = clamp(0, e.pageX - offset.left - plotOffset.left, plot.width()); + pos.y = clamp(0, e.pageY - offset.top - plotOffset.top, plot.height()); + + if (o.selection.mode == "y") + pos.x = pos == selection.first ? 0 : plot.width(); + + if (o.selection.mode == "x") + pos.y = pos == selection.first ? 0 : plot.height(); + } + + function updateSelection(pos) { + if (pos.pageX == null) + return; + + setSelectionPos(selection.second, pos); + if (selectionIsSane()) { + selection.show = true; + plot.triggerRedrawOverlay(); + } + else + clearSelection(true); + } + + function clearSelection(preventEvent) { + if (selection.show) { + selection.show = false; + plot.triggerRedrawOverlay(); + if (!preventEvent) + plot.getPlaceholder().trigger("plotunselected", [ ]); + } + } + + // function taken from markings support in Flot + function extractRange(ranges, coord) { + var axis, from, to, key, axes = plot.getAxes(); + + for (var k in axes) { + axis = axes[k]; + if (axis.direction == coord) { + key = coord + axis.n + "axis"; + if (!ranges[key] && axis.n == 1) + key = coord + "axis"; // support x1axis as xaxis + if (ranges[key]) { + from = ranges[key].from; + to = ranges[key].to; + break; + } + } + } + + // backwards-compat stuff - to be removed in future + if (!ranges[key]) { + axis = coord == "x" ? plot.getXAxes()[0] : plot.getYAxes()[0]; + from = ranges[coord + "1"]; + to = ranges[coord + "2"]; + } + + // auto-reverse as an added bonus + if (from != null && to != null && from > to) { + var tmp = from; + from = to; + to = tmp; + } + + return { from: from, to: to, axis: axis }; + } + + function setSelection(ranges, preventEvent) { + var axis, range, o = plot.getOptions(); + + if (o.selection.mode == "y") { + selection.first.x = 0; + selection.second.x = plot.width(); + } + else { + range = extractRange(ranges, "x"); + + selection.first.x = range.axis.p2c(range.from); + selection.second.x = range.axis.p2c(range.to); + } + + if (o.selection.mode == "x") { + selection.first.y = 0; + selection.second.y = plot.height(); + } + else { + range = extractRange(ranges, "y"); + + selection.first.y = range.axis.p2c(range.from); + selection.second.y = range.axis.p2c(range.to); + } + + selection.show = true; + plot.triggerRedrawOverlay(); + if (!preventEvent && selectionIsSane()) + triggerSelectedEvent(); + } + + function selectionIsSane() { + var minSize = plot.getOptions().selection.minSize; + return Math.abs(selection.second.x - selection.first.x) >= minSize && + Math.abs(selection.second.y - selection.first.y) >= minSize; + } + + plot.clearSelection = clearSelection; + plot.setSelection = setSelection; + plot.getSelection = getSelection; + + plot.hooks.bindEvents.push(function(plot, eventHolder) { + var o = plot.getOptions(); + if (o.selection.mode != null) { + eventHolder.mousemove(onMouseMove); + eventHolder.mousedown(onMouseDown); + } + }); + + + plot.hooks.drawOverlay.push(function (plot, ctx) { + // draw selection + if (selection.show && selectionIsSane()) { + var plotOffset = plot.getPlotOffset(); + var o = plot.getOptions(); + + ctx.save(); + ctx.translate(plotOffset.left, plotOffset.top); + + var c = $.color.parse(o.selection.color); + + ctx.strokeStyle = c.scale('a', 0.8).toString(); + ctx.lineWidth = 1; + ctx.lineJoin = o.selection.shape; + ctx.fillStyle = c.scale('a', 0.4).toString(); + + var x = Math.min(selection.first.x, selection.second.x) + 0.5, + y = Math.min(selection.first.y, selection.second.y) + 0.5, + w = Math.abs(selection.second.x - selection.first.x) - 1, + h = Math.abs(selection.second.y - selection.first.y) - 1; + + ctx.fillRect(x, y, w, h); + ctx.strokeRect(x, y, w, h); + + ctx.restore(); + } + }); + + plot.hooks.shutdown.push(function (plot, eventHolder) { + eventHolder.unbind("mousemove", onMouseMove); + eventHolder.unbind("mousedown", onMouseDown); + + if (mouseUpHandler) + $(document).unbind("mouseup", mouseUpHandler); + }); + + } + + $.plot.plugins.push({ + init: init, + options: { + selection: { + mode: null, // one of null, "x", "y" or "xy" + color: "#e8cfac", + shape: "round", // one of "round", "miter", or "bevel" + minSize: 5 // minimum number of pixels + } + }, + name: 'selection', + version: '1.1' + }); +})(jQuery); diff --git a/src/plugins/timelion/public/webpackShims/jquery.flot.stack.js b/src/plugins/timelion/public/webpackShims/jquery.flot.stack.js new file mode 100644 index 0000000000000..0d91c0f3c0160 --- /dev/null +++ b/src/plugins/timelion/public/webpackShims/jquery.flot.stack.js @@ -0,0 +1,188 @@ +/* Flot plugin for stacking data sets rather than overlaying them. + +Copyright (c) 2007-2014 IOLA and Ole Laursen. +Licensed under the MIT license. + +The plugin assumes the data is sorted on x (or y if stacking horizontally). +For line charts, it is assumed that if a line has an undefined gap (from a +null point), then the line above it should have the same gap - insert zeros +instead of "null" if you want another behaviour. This also holds for the start +and end of the chart. Note that stacking a mix of positive and negative values +in most instances doesn't make sense (so it looks weird). + +Two or more series are stacked when their "stack" attribute is set to the same +key (which can be any number or string or just "true"). To specify the default +stack, you can set the stack option like this: + + series: { + stack: null/false, true, or a key (number/string) + } + +You can also specify it for a single series, like this: + + $.plot( $("#placeholder"), [{ + data: [ ... ], + stack: true + }]) + +The stacking order is determined by the order of the data series in the array +(later series end up on top of the previous). + +Internally, the plugin modifies the datapoints in each series, adding an +offset to the y value. For line series, extra data points are inserted through +interpolation. If there's a second y value, it's also adjusted (e.g for bar +charts or filled areas). + +*/ + +(function ($) { + var options = { + series: { stack: null } // or number/string + }; + + function init(plot) { + function findMatchingSeries(s, allseries) { + var res = null; + for (var i = 0; i < allseries.length; ++i) { + if (s == allseries[i]) + break; + + if (allseries[i].stack == s.stack) + res = allseries[i]; + } + + return res; + } + + function stackData(plot, s, datapoints) { + if (s.stack == null || s.stack === false) + return; + + var other = findMatchingSeries(s, plot.getData()); + if (!other) + return; + + var ps = datapoints.pointsize, + points = datapoints.points, + otherps = other.datapoints.pointsize, + otherpoints = other.datapoints.points, + newpoints = [], + px, py, intery, qx, qy, bottom, + withlines = s.lines.show, + horizontal = s.bars.horizontal, + withbottom = ps > 2 && (horizontal ? datapoints.format[2].x : datapoints.format[2].y), + withsteps = withlines && s.lines.steps, + fromgap = true, + keyOffset = horizontal ? 1 : 0, + accumulateOffset = horizontal ? 0 : 1, + i = 0, j = 0, l, m; + + while (true) { + if (i >= points.length) + break; + + l = newpoints.length; + + if (points[i] == null) { + // copy gaps + for (m = 0; m < ps; ++m) + newpoints.push(points[i + m]); + i += ps; + } + else if (j >= otherpoints.length) { + // for lines, we can't use the rest of the points + if (!withlines) { + for (m = 0; m < ps; ++m) + newpoints.push(points[i + m]); + } + i += ps; + } + else if (otherpoints[j] == null) { + // oops, got a gap + for (m = 0; m < ps; ++m) + newpoints.push(null); + fromgap = true; + j += otherps; + } + else { + // cases where we actually got two points + px = points[i + keyOffset]; + py = points[i + accumulateOffset]; + qx = otherpoints[j + keyOffset]; + qy = otherpoints[j + accumulateOffset]; + bottom = 0; + + if (px == qx) { + for (m = 0; m < ps; ++m) + newpoints.push(points[i + m]); + + newpoints[l + accumulateOffset] += qy; + bottom = qy; + + i += ps; + j += otherps; + } + else if (px > qx) { + // we got past point below, might need to + // insert interpolated extra point + if (withlines && i > 0 && points[i - ps] != null) { + intery = py + (points[i - ps + accumulateOffset] - py) * (qx - px) / (points[i - ps + keyOffset] - px); + newpoints.push(qx); + newpoints.push(intery + qy); + for (m = 2; m < ps; ++m) + newpoints.push(points[i + m]); + bottom = qy; + } + + j += otherps; + } + else { // px < qx + if (fromgap && withlines) { + // if we come from a gap, we just skip this point + i += ps; + continue; + } + + for (m = 0; m < ps; ++m) + newpoints.push(points[i + m]); + + // we might be able to interpolate a point below, + // this can give us a better y + if (withlines && j > 0 && otherpoints[j - otherps] != null) + bottom = qy + (otherpoints[j - otherps + accumulateOffset] - qy) * (px - qx) / (otherpoints[j - otherps + keyOffset] - qx); + + newpoints[l + accumulateOffset] += bottom; + + i += ps; + } + + fromgap = false; + + if (l != newpoints.length && withbottom) + newpoints[l + 2] += bottom; + } + + // maintain the line steps invariant + if (withsteps && l != newpoints.length && l > 0 + && newpoints[l] != null + && newpoints[l] != newpoints[l - ps] + && newpoints[l + 1] != newpoints[l - ps + 1]) { + for (m = 0; m < ps; ++m) + newpoints[l + ps + m] = newpoints[l + m]; + newpoints[l + 1] = newpoints[l - ps + 1]; + } + } + + datapoints.points = newpoints; + } + + plot.hooks.processDatapoints.push(stackData); + } + + $.plot.plugins.push({ + init: init, + options: options, + name: 'stack', + version: '1.2' + }); +})(jQuery); diff --git a/src/plugins/timelion/public/webpackShims/jquery.flot.symbol.js b/src/plugins/timelion/public/webpackShims/jquery.flot.symbol.js new file mode 100644 index 0000000000000..79f634971b6fa --- /dev/null +++ b/src/plugins/timelion/public/webpackShims/jquery.flot.symbol.js @@ -0,0 +1,71 @@ +/* Flot plugin that adds some extra symbols for plotting points. + +Copyright (c) 2007-2014 IOLA and Ole Laursen. +Licensed under the MIT license. + +The symbols are accessed as strings through the standard symbol options: + + series: { + points: { + symbol: "square" // or "diamond", "triangle", "cross" + } + } + +*/ + +(function ($) { + function processRawData(plot, series, datapoints) { + // we normalize the area of each symbol so it is approximately the + // same as a circle of the given radius + + var handlers = { + square: function (ctx, x, y, radius, shadow) { + // pi * r^2 = (2s)^2 => s = r * sqrt(pi)/2 + var size = radius * Math.sqrt(Math.PI) / 2; + ctx.rect(x - size, y - size, size + size, size + size); + }, + diamond: function (ctx, x, y, radius, shadow) { + // pi * r^2 = 2s^2 => s = r * sqrt(pi/2) + var size = radius * Math.sqrt(Math.PI / 2); + ctx.moveTo(x - size, y); + ctx.lineTo(x, y - size); + ctx.lineTo(x + size, y); + ctx.lineTo(x, y + size); + ctx.lineTo(x - size, y); + }, + triangle: function (ctx, x, y, radius, shadow) { + // pi * r^2 = 1/2 * s^2 * sin (pi / 3) => s = r * sqrt(2 * pi / sin(pi / 3)) + var size = radius * Math.sqrt(2 * Math.PI / Math.sin(Math.PI / 3)); + var height = size * Math.sin(Math.PI / 3); + ctx.moveTo(x - size/2, y + height/2); + ctx.lineTo(x + size/2, y + height/2); + if (!shadow) { + ctx.lineTo(x, y - height/2); + ctx.lineTo(x - size/2, y + height/2); + } + }, + cross: function (ctx, x, y, radius, shadow) { + // pi * r^2 = (2s)^2 => s = r * sqrt(pi)/2 + var size = radius * Math.sqrt(Math.PI) / 2; + ctx.moveTo(x - size, y - size); + ctx.lineTo(x + size, y + size); + ctx.moveTo(x - size, y + size); + ctx.lineTo(x + size, y - size); + } + }; + + var s = series.points.symbol; + if (handlers[s]) + series.points.symbol = handlers[s]; + } + + function init(plot) { + plot.hooks.processDatapoints.push(processRawData); + } + + $.plot.plugins.push({ + init: init, + name: 'symbols', + version: '1.0' + }); +})(jQuery); diff --git a/src/plugins/timelion/public/webpackShims/jquery.flot.time.js b/src/plugins/timelion/public/webpackShims/jquery.flot.time.js new file mode 100644 index 0000000000000..34c1d121259a2 --- /dev/null +++ b/src/plugins/timelion/public/webpackShims/jquery.flot.time.js @@ -0,0 +1,432 @@ +/* Pretty handling of time axes. + +Copyright (c) 2007-2014 IOLA and Ole Laursen. +Licensed under the MIT license. + +Set axis.mode to "time" to enable. See the section "Time series data" in +API.txt for details. + +*/ + +(function($) { + + var options = { + xaxis: { + timezone: null, // "browser" for local to the client or timezone for timezone-js + timeformat: null, // format string to use + twelveHourClock: false, // 12 or 24 time in time mode + monthNames: null // list of names of months + } + }; + + // round to nearby lower multiple of base + + function floorInBase(n, base) { + return base * Math.floor(n / base); + } + + // Returns a string with the date d formatted according to fmt. + // A subset of the Open Group's strftime format is supported. + + function formatDate(d, fmt, monthNames, dayNames) { + + if (typeof d.strftime == "function") { + return d.strftime(fmt); + } + + var leftPad = function(n, pad) { + n = "" + n; + pad = "" + (pad == null ? "0" : pad); + return n.length == 1 ? pad + n : n; + }; + + var r = []; + var escape = false; + var hours = d.getHours(); + var isAM = hours < 12; + + if (monthNames == null) { + monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; + } + + if (dayNames == null) { + dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; + } + + var hours12; + + if (hours > 12) { + hours12 = hours - 12; + } else if (hours == 0) { + hours12 = 12; + } else { + hours12 = hours; + } + + for (var i = 0; i < fmt.length; ++i) { + + var c = fmt.charAt(i); + + if (escape) { + switch (c) { + case 'a': c = "" + dayNames[d.getDay()]; break; + case 'b': c = "" + monthNames[d.getMonth()]; break; + case 'd': c = leftPad(d.getDate()); break; + case 'e': c = leftPad(d.getDate(), " "); break; + case 'h': // For back-compat with 0.7; remove in 1.0 + case 'H': c = leftPad(hours); break; + case 'I': c = leftPad(hours12); break; + case 'l': c = leftPad(hours12, " "); break; + case 'm': c = leftPad(d.getMonth() + 1); break; + case 'M': c = leftPad(d.getMinutes()); break; + // quarters not in Open Group's strftime specification + case 'q': + c = "" + (Math.floor(d.getMonth() / 3) + 1); break; + case 'S': c = leftPad(d.getSeconds()); break; + case 'y': c = leftPad(d.getFullYear() % 100); break; + case 'Y': c = "" + d.getFullYear(); break; + case 'p': c = (isAM) ? ("" + "am") : ("" + "pm"); break; + case 'P': c = (isAM) ? ("" + "AM") : ("" + "PM"); break; + case 'w': c = "" + d.getDay(); break; + } + r.push(c); + escape = false; + } else { + if (c == "%") { + escape = true; + } else { + r.push(c); + } + } + } + + return r.join(""); + } + + // To have a consistent view of time-based data independent of which time + // zone the client happens to be in we need a date-like object independent + // of time zones. This is done through a wrapper that only calls the UTC + // versions of the accessor methods. + + function makeUtcWrapper(d) { + + function addProxyMethod(sourceObj, sourceMethod, targetObj, targetMethod) { + sourceObj[sourceMethod] = function() { + return targetObj[targetMethod].apply(targetObj, arguments); + }; + }; + + var utc = { + date: d + }; + + // support strftime, if found + + if (d.strftime != undefined) { + addProxyMethod(utc, "strftime", d, "strftime"); + } + + addProxyMethod(utc, "getTime", d, "getTime"); + addProxyMethod(utc, "setTime", d, "setTime"); + + var props = ["Date", "Day", "FullYear", "Hours", "Milliseconds", "Minutes", "Month", "Seconds"]; + + for (var p = 0; p < props.length; p++) { + addProxyMethod(utc, "get" + props[p], d, "getUTC" + props[p]); + addProxyMethod(utc, "set" + props[p], d, "setUTC" + props[p]); + } + + return utc; + }; + + // select time zone strategy. This returns a date-like object tied to the + // desired timezone + + function dateGenerator(ts, opts) { + if (opts.timezone == "browser") { + return new Date(ts); + } else if (!opts.timezone || opts.timezone == "utc") { + return makeUtcWrapper(new Date(ts)); + } else if (typeof timezoneJS != "undefined" && typeof timezoneJS.Date != "undefined") { + var d = new timezoneJS.Date(); + // timezone-js is fickle, so be sure to set the time zone before + // setting the time. + d.setTimezone(opts.timezone); + d.setTime(ts); + return d; + } else { + return makeUtcWrapper(new Date(ts)); + } + } + + // map of app. size of time units in milliseconds + + var timeUnitSize = { + "second": 1000, + "minute": 60 * 1000, + "hour": 60 * 60 * 1000, + "day": 24 * 60 * 60 * 1000, + "month": 30 * 24 * 60 * 60 * 1000, + "quarter": 3 * 30 * 24 * 60 * 60 * 1000, + "year": 365.2425 * 24 * 60 * 60 * 1000 + }; + + // the allowed tick sizes, after 1 year we use + // an integer algorithm + + var baseSpec = [ + [1, "second"], [2, "second"], [5, "second"], [10, "second"], + [30, "second"], + [1, "minute"], [2, "minute"], [5, "minute"], [10, "minute"], + [30, "minute"], + [1, "hour"], [2, "hour"], [4, "hour"], + [8, "hour"], [12, "hour"], + [1, "day"], [2, "day"], [3, "day"], + [0.25, "month"], [0.5, "month"], [1, "month"], + [2, "month"] + ]; + + // we don't know which variant(s) we'll need yet, but generating both is + // cheap + + var specMonths = baseSpec.concat([[3, "month"], [6, "month"], + [1, "year"]]); + var specQuarters = baseSpec.concat([[1, "quarter"], [2, "quarter"], + [1, "year"]]); + + function init(plot) { + plot.hooks.processOptions.push(function (plot, options) { + $.each(plot.getAxes(), function(axisName, axis) { + + var opts = axis.options; + + if (opts.mode == "time") { + axis.tickGenerator = function(axis) { + + var ticks = []; + var d = dateGenerator(axis.min, opts); + var minSize = 0; + + // make quarter use a possibility if quarters are + // mentioned in either of these options + + var spec = (opts.tickSize && opts.tickSize[1] === + "quarter") || + (opts.minTickSize && opts.minTickSize[1] === + "quarter") ? specQuarters : specMonths; + + if (opts.minTickSize != null) { + if (typeof opts.tickSize == "number") { + minSize = opts.tickSize; + } else { + minSize = opts.minTickSize[0] * timeUnitSize[opts.minTickSize[1]]; + } + } + + for (var i = 0; i < spec.length - 1; ++i) { + if (axis.delta < (spec[i][0] * timeUnitSize[spec[i][1]] + + spec[i + 1][0] * timeUnitSize[spec[i + 1][1]]) / 2 + && spec[i][0] * timeUnitSize[spec[i][1]] >= minSize) { + break; + } + } + + var size = spec[i][0]; + var unit = spec[i][1]; + + // special-case the possibility of several years + + if (unit == "year") { + + // if given a minTickSize in years, just use it, + // ensuring that it's an integer + + if (opts.minTickSize != null && opts.minTickSize[1] == "year") { + size = Math.floor(opts.minTickSize[0]); + } else { + + var magn = Math.pow(10, Math.floor(Math.log(axis.delta / timeUnitSize.year) / Math.LN10)); + var norm = (axis.delta / timeUnitSize.year) / magn; + + if (norm < 1.5) { + size = 1; + } else if (norm < 3) { + size = 2; + } else if (norm < 7.5) { + size = 5; + } else { + size = 10; + } + + size *= magn; + } + + // minimum size for years is 1 + + if (size < 1) { + size = 1; + } + } + + axis.tickSize = opts.tickSize || [size, unit]; + var tickSize = axis.tickSize[0]; + unit = axis.tickSize[1]; + + var step = tickSize * timeUnitSize[unit]; + + if (unit == "second") { + d.setSeconds(floorInBase(d.getSeconds(), tickSize)); + } else if (unit == "minute") { + d.setMinutes(floorInBase(d.getMinutes(), tickSize)); + } else if (unit == "hour") { + d.setHours(floorInBase(d.getHours(), tickSize)); + } else if (unit == "month") { + d.setMonth(floorInBase(d.getMonth(), tickSize)); + } else if (unit == "quarter") { + d.setMonth(3 * floorInBase(d.getMonth() / 3, + tickSize)); + } else if (unit == "year") { + d.setFullYear(floorInBase(d.getFullYear(), tickSize)); + } + + // reset smaller components + + d.setMilliseconds(0); + + if (step >= timeUnitSize.minute) { + d.setSeconds(0); + } + if (step >= timeUnitSize.hour) { + d.setMinutes(0); + } + if (step >= timeUnitSize.day) { + d.setHours(0); + } + if (step >= timeUnitSize.day * 4) { + d.setDate(1); + } + if (step >= timeUnitSize.month * 2) { + d.setMonth(floorInBase(d.getMonth(), 3)); + } + if (step >= timeUnitSize.quarter * 2) { + d.setMonth(floorInBase(d.getMonth(), 6)); + } + if (step >= timeUnitSize.year) { + d.setMonth(0); + } + + var carry = 0; + var v = Number.NaN; + var prev; + + do { + + prev = v; + v = d.getTime(); + ticks.push(v); + + if (unit == "month" || unit == "quarter") { + if (tickSize < 1) { + + // a bit complicated - we'll divide the + // month/quarter up but we need to take + // care of fractions so we don't end up in + // the middle of a day + + d.setDate(1); + var start = d.getTime(); + d.setMonth(d.getMonth() + + (unit == "quarter" ? 3 : 1)); + var end = d.getTime(); + d.setTime(v + carry * timeUnitSize.hour + (end - start) * tickSize); + carry = d.getHours(); + d.setHours(0); + } else { + d.setMonth(d.getMonth() + + tickSize * (unit == "quarter" ? 3 : 1)); + } + } else if (unit == "year") { + d.setFullYear(d.getFullYear() + tickSize); + } else { + d.setTime(v + step); + } + } while (v < axis.max && v != prev); + + return ticks; + }; + + axis.tickFormatter = function (v, axis) { + + var d = dateGenerator(v, axis.options); + + // first check global format + + if (opts.timeformat != null) { + return formatDate(d, opts.timeformat, opts.monthNames, opts.dayNames); + } + + // possibly use quarters if quarters are mentioned in + // any of these places + + var useQuarters = (axis.options.tickSize && + axis.options.tickSize[1] == "quarter") || + (axis.options.minTickSize && + axis.options.minTickSize[1] == "quarter"); + + var t = axis.tickSize[0] * timeUnitSize[axis.tickSize[1]]; + var span = axis.max - axis.min; + var suffix = (opts.twelveHourClock) ? " %p" : ""; + var hourCode = (opts.twelveHourClock) ? "%I" : "%H"; + var fmt; + + if (t < timeUnitSize.minute) { + fmt = hourCode + ":%M:%S" + suffix; + } else if (t < timeUnitSize.day) { + if (span < 2 * timeUnitSize.day) { + fmt = hourCode + ":%M" + suffix; + } else { + fmt = "%b %d " + hourCode + ":%M" + suffix; + } + } else if (t < timeUnitSize.month) { + fmt = "%b %d"; + } else if ((useQuarters && t < timeUnitSize.quarter) || + (!useQuarters && t < timeUnitSize.year)) { + if (span < timeUnitSize.year) { + fmt = "%b"; + } else { + fmt = "%b %Y"; + } + } else if (useQuarters && t < timeUnitSize.year) { + if (span < timeUnitSize.year) { + fmt = "Q%q"; + } else { + fmt = "Q%q %Y"; + } + } else { + fmt = "%Y"; + } + + var rt = formatDate(d, fmt, opts.monthNames, opts.dayNames); + + return rt; + }; + } + }); + }); + } + + $.plot.plugins.push({ + init: init, + options: options, + name: 'time', + version: '1.0' + }); + + // Time-axis support used to be in Flot core, which exposed the + // formatDate function on the plot object. Various plugins depend + // on the function, so we need to re-expose it here. + + $.plot.formatDate = formatDate; + $.plot.dateGenerator = dateGenerator; + +})(jQuery); diff --git a/src/legacy/core_plugins/timelion/public/components/timelionhelp_tabs_directive.js b/src/plugins/timelion/server/config.ts similarity index 67% rename from src/legacy/core_plugins/timelion/public/components/timelionhelp_tabs_directive.js rename to src/plugins/timelion/server/config.ts index 7e77027f750c6..16e559761e9ad 100644 --- a/src/legacy/core_plugins/timelion/public/components/timelionhelp_tabs_directive.js +++ b/src/plugins/timelion/server/config.ts @@ -17,14 +17,16 @@ * under the License. */ -import 'ngreact'; +import { schema, TypeOf } from '@kbn/config-schema'; -import { wrapInI18nContext } from 'ui/i18n'; -import { uiModules } from 'ui/modules'; -const module = uiModules.get('apps/timelion', ['react']); +export const configSchema = { + schema: schema.object({ + graphiteUrls: schema.maybe(schema.arrayOf(schema.string())), + enabled: schema.boolean({ defaultValue: true }), + ui: schema.object({ + enabled: schema.boolean({ defaultValue: true }), + }), + }), +}; -import { TimelionHelpTabs } from './timelionhelp_tabs'; - -module.directive('timelionHelpTabs', function (reactDirective) { - return reactDirective(wrapInI18nContext(TimelionHelpTabs), undefined, { restrict: 'E' }); -}); +export type TimelionConfigType = TypeOf; diff --git a/src/plugins/timelion/server/index.ts b/src/plugins/timelion/server/index.ts index 5bb0c9e2567e0..28c5709d89132 100644 --- a/src/plugins/timelion/server/index.ts +++ b/src/plugins/timelion/server/index.ts @@ -16,7 +16,13 @@ * specific language governing permissions and limitations * under the License. */ -import { PluginInitializerContext } from 'src/core/server'; +import { PluginInitializerContext, PluginConfigDescriptor } from 'src/core/server'; import { TimelionPlugin } from './plugin'; +import { configSchema, TimelionConfigType } from './config'; -export const plugin = (context: PluginInitializerContext) => new TimelionPlugin(context); +export const config: PluginConfigDescriptor = { + schema: configSchema.schema, +}; + +export const plugin = (context: PluginInitializerContext) => + new TimelionPlugin(context); diff --git a/src/plugins/timelion/server/plugin.ts b/src/plugins/timelion/server/plugin.ts index 015f0c573e531..3e4cd5467dd44 100644 --- a/src/plugins/timelion/server/plugin.ts +++ b/src/plugins/timelion/server/plugin.ts @@ -16,12 +16,21 @@ * specific language governing permissions and limitations * under the License. */ + import { CoreSetup, Plugin, PluginInitializerContext } from 'src/core/server'; +import { i18n } from '@kbn/i18n'; +import { schema } from '@kbn/config-schema'; +import { TimelionConfigType } from './config'; export class TimelionPlugin implements Plugin { - constructor(context: PluginInitializerContext) {} + constructor(context: PluginInitializerContext) {} - setup(core: CoreSetup) { + public setup(core: CoreSetup) { + core.capabilities.registerProvider(() => ({ + timelion: { + save: true, + }, + })); core.savedObjects.registerType({ name: 'timelion-sheet', hidden: false, @@ -46,6 +55,42 @@ export class TimelionPlugin implements Plugin { }, }, }); + + core.uiSettings.register({ + 'timelion:showTutorial': { + name: i18n.translate('timelion.uiSettings.showTutorialLabel', { + defaultMessage: 'Show tutorial', + }), + value: false, + description: i18n.translate('timelion.uiSettings.showTutorialDescription', { + defaultMessage: 'Should I show the tutorial by default when entering the timelion app?', + }), + category: ['timelion'], + schema: schema.boolean(), + }, + 'timelion:default_columns': { + name: i18n.translate('timelion.uiSettings.defaultColumnsLabel', { + defaultMessage: 'Default columns', + }), + value: 2, + description: i18n.translate('timelion.uiSettings.defaultColumnsDescription', { + defaultMessage: 'Number of columns on a timelion sheet by default', + }), + category: ['timelion'], + schema: schema.number(), + }, + 'timelion:default_rows': { + name: i18n.translate('timelion.uiSettings.defaultRowsLabel', { + defaultMessage: 'Default rows', + }), + value: 2, + description: i18n.translate('timelion.uiSettings.defaultRowsDescription', { + defaultMessage: 'Number of rows on a timelion sheet by default', + }), + category: ['timelion'], + schema: schema.number(), + }, + }); } start() {} stop() {} diff --git a/src/plugins/vis_type_table/public/__snapshots__/table_vis_fn.test.ts.snap b/src/plugins/vis_type_table/public/__snapshots__/table_vis_fn.test.ts.snap index a8fe25582717c..dc6571de969f0 100644 --- a/src/plugins/vis_type_table/public/__snapshots__/table_vis_fn.test.ts.snap +++ b/src/plugins/vis_type_table/public/__snapshots__/table_vis_fn.test.ts.snap @@ -30,6 +30,7 @@ Object { "columnIndex": null, "direction": null, }, + "title": "My Chart title", "totalFunc": "sum", }, "visData": Object { diff --git a/src/plugins/vis_type_table/public/agg_table/agg_table.js b/src/plugins/vis_type_table/public/agg_table/agg_table.js index bd7626a493338..1e98a06c2a6a9 100644 --- a/src/plugins/vis_type_table/public/agg_table/agg_table.js +++ b/src/plugins/vis_type_table/public/agg_table/agg_table.js @@ -116,7 +116,7 @@ export function KbnAggTable(config, RecursionHelper) { return; } - self.csv.filename = (exportTitle || table.title || 'table') + '.csv'; + self.csv.filename = (exportTitle || table.title || 'unsaved') + '.csv'; $scope.rows = table.rows; $scope.formattedColumns = []; diff --git a/src/plugins/vis_type_table/public/table_vis_fn.test.ts b/src/plugins/vis_type_table/public/table_vis_fn.test.ts index 9accf8950d910..6cb3f3e0f3779 100644 --- a/src/plugins/vis_type_table/public/table_vis_fn.test.ts +++ b/src/plugins/vis_type_table/public/table_vis_fn.test.ts @@ -37,6 +37,7 @@ describe('interpreter/functions#table', () => { columns: [{ id: 'col-0-1', name: 'Count' }], }; const visConfig = { + title: 'My Chart title', perPage: 10, showPartialRows: false, showMetricsAtAllLevels: false, diff --git a/src/plugins/vis_type_table/public/vis_controller.ts b/src/plugins/vis_type_table/public/vis_controller.ts index a5086e0c9a2d8..d87812b9f5d69 100644 --- a/src/plugins/vis_type_table/public/vis_controller.ts +++ b/src/plugins/vis_type_table/public/vis_controller.ts @@ -78,8 +78,18 @@ export function getTableVisualizationControllerClass( if (!this.$scope) { return; } + + // How things get into this $scope? + // To inject variables into this $scope there's the following pipeline of stuff to check: + // - visualize_embeddable => that's what the editor creates to wrap this Angular component + // - build_pipeline => it serialize all the params into an Angular template compiled on the fly + // - table_vis_fn => unserialize the params and prepare them for the final React/Angular bridge + // - visualization_renderer => creates the wrapper component for this controller and passes the params + // + // In case some prop is missing check into the top of the chain if they are available and check + // the list above that it is passing through this.$scope.vis = this.vis; - this.$scope.visState = { params: visParams }; + this.$scope.visState = { params: visParams, title: visParams.title }; this.$scope.esResponse = esResponse; this.$scope.visParams = visParams; diff --git a/src/plugins/vis_type_tagcloud/public/components/tag_cloud_visualization.test.js b/src/plugins/vis_type_tagcloud/public/components/tag_cloud_visualization.test.js index 7f96066c16076..afdc273782709 100644 --- a/src/plugins/vis_type_tagcloud/public/components/tag_cloud_visualization.test.js +++ b/src/plugins/vis_type_tagcloud/public/components/tag_cloud_visualization.test.js @@ -122,8 +122,6 @@ describe('TagCloudVisualizationTest', () => { uiState: false, }); - domNode.style.width = '256px'; - domNode.style.height = '368px'; await tagcloudVisualization.render(dummyTableGroup, vis.params, { resize: true, params: false, diff --git a/src/plugins/vis_type_timelion/public/index.ts b/src/plugins/vis_type_timelion/public/index.ts index 0aa5f3a810033..abfe345d8c672 100644 --- a/src/plugins/vis_type_timelion/public/index.ts +++ b/src/plugins/vis_type_timelion/public/index.ts @@ -25,5 +25,10 @@ export function plugin(initializerContext: PluginInitializerContext) { } export { getTimezone } from './helpers/get_timezone'; +export { tickFormatters } from './helpers/tick_formatters'; +export { xaxisFormatterProvider } from './helpers/xaxis_formatter'; +export { generateTicksProvider } from './helpers/tick_generator'; + +export { DEFAULT_TIME_FORMAT, calculateInterval } from '../common/lib'; export { VisTypeTimelionPluginStart } from './plugin'; diff --git a/src/plugins/vis_type_timelion/server/plugin.ts b/src/plugins/vis_type_timelion/server/plugin.ts index 605c6be0a85df..5e6557e305692 100644 --- a/src/plugins/vis_type_timelion/server/plugin.ts +++ b/src/plugins/vis_type_timelion/server/plugin.ts @@ -19,7 +19,7 @@ import { i18n } from '@kbn/i18n'; import { first } from 'rxjs/operators'; -import { TypeOf } from '@kbn/config-schema'; +import { TypeOf, schema } from '@kbn/config-schema'; import { RecursiveReadonly } from '@kbn/utility-types'; import { CoreSetup, PluginInitializerContext } from '../../../../src/core/server'; @@ -31,6 +31,10 @@ import { validateEsRoute } from './routes/validate_es'; import { runRoute } from './routes/run'; import { ConfigManager } from './lib/config_manager'; +const experimentalLabel = i18n.translate('timelion.uiSettings.experimentalLabel', { + defaultMessage: 'experimental', +}); + /** * Describes public Timelion plugin contract returned at the `setup` stage. */ @@ -82,6 +86,97 @@ export class Plugin { runRoute(router, deps); validateEsRoute(router); + core.uiSettings.register({ + 'timelion:es.timefield': { + name: i18n.translate('timelion.uiSettings.timeFieldLabel', { + defaultMessage: 'Time field', + }), + value: '@timestamp', + description: i18n.translate('timelion.uiSettings.timeFieldDescription', { + defaultMessage: 'Default field containing a timestamp when using {esParam}', + values: { esParam: '.es()' }, + }), + category: ['timelion'], + schema: schema.string(), + }, + 'timelion:es.default_index': { + name: i18n.translate('timelion.uiSettings.defaultIndexLabel', { + defaultMessage: 'Default index', + }), + value: '_all', + description: i18n.translate('timelion.uiSettings.defaultIndexDescription', { + defaultMessage: 'Default elasticsearch index to search with {esParam}', + values: { esParam: '.es()' }, + }), + category: ['timelion'], + schema: schema.string(), + }, + 'timelion:target_buckets': { + name: i18n.translate('timelion.uiSettings.targetBucketsLabel', { + defaultMessage: 'Target buckets', + }), + value: 200, + description: i18n.translate('timelion.uiSettings.targetBucketsDescription', { + defaultMessage: 'The number of buckets to shoot for when using auto intervals', + }), + category: ['timelion'], + schema: schema.number(), + }, + 'timelion:max_buckets': { + name: i18n.translate('timelion.uiSettings.maximumBucketsLabel', { + defaultMessage: 'Maximum buckets', + }), + value: 2000, + description: i18n.translate('timelion.uiSettings.maximumBucketsDescription', { + defaultMessage: 'The maximum number of buckets a single datasource can return', + }), + category: ['timelion'], + schema: schema.number(), + }, + 'timelion:min_interval': { + name: i18n.translate('timelion.uiSettings.minimumIntervalLabel', { + defaultMessage: 'Minimum interval', + }), + value: '1ms', + description: i18n.translate('timelion.uiSettings.minimumIntervalDescription', { + defaultMessage: 'The smallest interval that will be calculated when using "auto"', + description: + '"auto" is a technical value in that context, that should not be translated.', + }), + category: ['timelion'], + schema: schema.string(), + }, + 'timelion:graphite.url': { + name: i18n.translate('timelion.uiSettings.graphiteURLLabel', { + defaultMessage: 'Graphite URL', + description: + 'The URL should be in the form of https://www.hostedgraphite.com/UID/ACCESS_KEY/graphite', + }), + value: config.graphiteUrls && config.graphiteUrls.length ? config.graphiteUrls[0] : null, + description: i18n.translate('timelion.uiSettings.graphiteURLDescription', { + defaultMessage: + '{experimentalLabel} The
URL of your graphite host', + values: { experimentalLabel: `[${experimentalLabel}]` }, + }), + type: 'select', + options: config.graphiteUrls || [], + category: ['timelion'], + schema: schema.nullable(schema.string()), + }, + 'timelion:quandl.key': { + name: i18n.translate('timelion.uiSettings.quandlKeyLabel', { + defaultMessage: 'Quandl key', + }), + value: 'someKeyHere', + description: i18n.translate('timelion.uiSettings.quandlKeyDescription', { + defaultMessage: '{experimentalLabel} Your API key from www.quandl.com', + values: { experimentalLabel: `[${experimentalLabel}]` }, + }), + category: ['timelion'], + schema: schema.string(), + }, + }); + return deepFreeze({ uiEnabled: config.ui.enabled }); } diff --git a/src/plugins/vis_type_vega/public/__mocks__/services.ts b/src/plugins/vis_type_vega/public/__mocks__/services.ts deleted file mode 100644 index 4775241a66d50..0000000000000 --- a/src/plugins/vis_type_vega/public/__mocks__/services.ts +++ /dev/null @@ -1,57 +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 { CoreStart, IUiSettingsClient, NotificationsStart, SavedObjectsStart } from 'kibana/public'; - -import { createGetterSetter } from '../../../kibana_utils/public'; -import { DataPublicPluginStart } from '../../../data/public'; -import { dataPluginMock } from '../../../data/public/mocks'; -import { coreMock } from '../../../../core/public/mocks'; - -export const [getData, setData] = createGetterSetter('Data'); -setData(dataPluginMock.createStartContract()); - -export const [getNotifications, setNotifications] = createGetterSetter( - 'Notifications' -); -setNotifications(coreMock.createStart().notifications); - -export const [getUISettings, setUISettings] = createGetterSetter('UISettings'); -setUISettings(coreMock.createStart().uiSettings); - -export const [getInjectedMetadata, setInjectedMetadata] = createGetterSetter< - CoreStart['injectedMetadata'] ->('InjectedMetadata'); -setInjectedMetadata(coreMock.createStart().injectedMetadata); - -export const [getSavedObjects, setSavedObjects] = createGetterSetter( - 'SavedObjects' -); -setSavedObjects(coreMock.createStart().savedObjects); - -export const [getInjectedVars, setInjectedVars] = createGetterSetter<{ - enableExternalUrls: boolean; - emsTileLayerId: unknown; -}>('InjectedVars'); -setInjectedVars({ - emsTileLayerId: {}, - enableExternalUrls: true, -}); - -export const getEnableExternalUrls = () => getInjectedVars().enableExternalUrls; -export const getEmsTileLayerId = () => getInjectedVars().emsTileLayerId; diff --git a/src/plugins/vis_type_vega/public/__snapshots__/vega_visualization.test.js.snap b/src/plugins/vis_type_vega/public/__snapshots__/vega_visualization.test.js.snap new file mode 100644 index 0000000000000..650d9c1b430f0 --- /dev/null +++ b/src/plugins/vis_type_vega/public/__snapshots__/vega_visualization.test.js.snap @@ -0,0 +1,9 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`VegaVisualizations VegaVisualization - basics should show vega blank rectangle on top of a map (vegamap) 1`] = `"
"`; + +exports[`VegaVisualizations VegaVisualization - basics should show vega graph (may fail in dev env) 1`] = `"
"`; + +exports[`VegaVisualizations VegaVisualization - basics should show vegalite graph and update on resize (may fail in dev env) 1`] = `"
"`; + +exports[`VegaVisualizations VegaVisualization - basics should show vegalite graph and update on resize (may fail in dev env) 2`] = `"
"`; diff --git a/packages/kbn-test/src/page_load_metrics/index.ts b/src/plugins/vis_type_vega/public/default_spec.ts similarity index 86% rename from packages/kbn-test/src/page_load_metrics/index.ts rename to src/plugins/vis_type_vega/public/default_spec.ts index 4309d558518a6..71f44b694a10e 100644 --- a/packages/kbn-test/src/page_load_metrics/index.ts +++ b/src/plugins/vis_type_vega/public/default_spec.ts @@ -17,5 +17,7 @@ * under the License. */ -export * from './cli'; -export { capturePageLoadMetrics } from './capture_page_load_metrics'; +// @ts-ignore +import defaultSpec from '!!raw-loader!./default.spec.hjson'; + +export const getDefaultSpec = () => defaultSpec; diff --git a/src/plugins/vis_type_vega/public/test_utils/default.spec.json b/src/plugins/vis_type_vega/public/test_utils/default.spec.json new file mode 100644 index 0000000000000..8cf763647115f --- /dev/null +++ b/src/plugins/vis_type_vega/public/test_utils/default.spec.json @@ -0,0 +1,41 @@ +{ + "$schema": "https://vega.github.io/schema/vega-lite/v4.json", + "title": "Event counts from all indexes", + "data": { + "url": { + "%context%": true, + "%timefield%": "@timestamp", + "index": "_all", + "body": { + "aggs": { + "time_buckets": { + "date_histogram": { + "field": "@timestamp", + "interval": { "%autointerval%": true }, + "extended_bounds": { + "min": { "%timefilter%": "min" }, + "max": { "%timefilter%": "max" } + }, + "min_doc_count": 0 + } + } + }, + "size": 0 + } + }, + "format": { "property": "aggregations.time_buckets.buckets" } + }, + "mark": "line", + "encoding": { + "x": { + "field": "key", + "type": "temporal", + "axis": { "title": false } + }, + "y": { + "field": "doc_count", + "type": "quantitative", + "axis": { "title": "Document count" } + } + } +} diff --git a/src/plugins/vis_type_vega/public/test_utils/vega_graph.json b/src/plugins/vis_type_vega/public/test_utils/vega_graph.json new file mode 100644 index 0000000000000..babde96fd3dae --- /dev/null +++ b/src/plugins/vis_type_vega/public/test_utils/vega_graph.json @@ -0,0 +1,78 @@ +{ + "$schema": "https://vega.github.io/schema/vega/v5.json", + "data": [ + { + "name": "table", + "values": [ + {"x": 0, "y": 28, "c": 0}, {"x": 0, "y": 55, "c": 1}, {"x": 1, "y": 43, "c": 0}, {"x": 1, "y": 91, "c": 1}, + {"x": 2, "y": 81, "c": 0}, {"x": 2, "y": 53, "c": 1}, {"x": 3, "y": 19, "c": 0}, {"x": 3, "y": 87, "c": 1}, + {"x": 4, "y": 52, "c": 0}, {"x": 4, "y": 48, "c": 1}, {"x": 5, "y": 24, "c": 0}, {"x": 5, "y": 49, "c": 1}, + {"x": 6, "y": 87, "c": 0}, {"x": 6, "y": 66, "c": 1}, {"x": 7, "y": 17, "c": 0}, {"x": 7, "y": 27, "c": 1}, + {"x": 8, "y": 68, "c": 0}, {"x": 8, "y": 16, "c": 1}, {"x": 9, "y": 49, "c": 0}, {"x": 9, "y": 15, "c": 1} + ], + "transform": [ + { + "type": "stack", + "groupby": ["x"], + "sort": {"field": "c"}, + "field": "y" + } + ] + } + ], + "scales": [ + { + "name": "x", + "type": "point", + "range": "width", + "domain": {"data": "table", "field": "x"} + }, + { + "name": "y", + "type": "linear", + "range": "height", + "nice": true, + "zero": true, + "domain": {"data": "table", "field": "y1"} + }, + { + "name": "color", + "type": "ordinal", + "range": "category", + "domain": {"data": "table", "field": "c"} + } + ], + "marks": [ + { + "type": "group", + "from": { + "facet": {"name": "series", "data": "table", "groupby": "c"} + }, + "marks": [ + { + "type": "area", + "from": {"data": "series"}, + "encode": { + "enter": { + "interpolate": {"value": "monotone"}, + "x": {"scale": "x", "field": "x"}, + "y": {"scale": "y", "field": "y0"}, + "y2": {"scale": "y", "field": "y1"}, + "fill": {"scale": "color", "field": "c"} + }, + "update": { + "fillOpacity": {"value": 1} + }, + "hover": { + "fillOpacity": {"value": 0.5} + } + } + } + ] + } + ], + "autosize": { "type": "none" }, + "width": 512, + "height": 512, + "config": { "kibana": { "renderer": "svg" }} +} diff --git a/src/plugins/vis_type_vega/public/test_utils/vega_map_test.json b/src/plugins/vis_type_vega/public/test_utils/vega_map_test.json new file mode 100644 index 0000000000000..9100de38ae387 --- /dev/null +++ b/src/plugins/vis_type_vega/public/test_utils/vega_map_test.json @@ -0,0 +1,20 @@ +{ + "$schema": "https://vega.github.io/schema/vega/v5.json", + "config": { + "kibana": { "renderer": "svg", "type": "map", "mapStyle": false} + }, + "width": 512, + "height": 512, + "marks": [ + { + "type": "rect", + "encode": { + "enter": { + "fill": {"value": "#0f0"}, + "width": {"signal": "width"}, + "height": {"signal": "height"} + } + } + } + ] +} diff --git a/src/plugins/vis_type_vega/public/test_utils/vegalite_graph.json b/src/plugins/vis_type_vega/public/test_utils/vegalite_graph.json new file mode 100644 index 0000000000000..5394f009b074f --- /dev/null +++ b/src/plugins/vis_type_vega/public/test_utils/vegalite_graph.json @@ -0,0 +1,42 @@ +{ + "$schema": "https://vega.github.io/schema/vega-lite/v4.json", + "data": { + "format": {"property": "aggregations.time_buckets.buckets"}, + "values": { + "aggregations": { + "time_buckets": { + "buckets": [ + {"key": 1512950400000, "doc_count": 0}, + {"key": 1513036800000, "doc_count": 0}, + {"key": 1513123200000, "doc_count": 0}, + {"key": 1513209600000, "doc_count": 4545}, + {"key": 1513296000000, "doc_count": 4667}, + {"key": 1513382400000, "doc_count": 4660}, + {"key": 1513468800000, "doc_count": 133}, + {"key": 1513555200000, "doc_count": 0}, + {"key": 1513641600000, "doc_count": 0}, + {"key": 1513728000000, "doc_count": 0} + ] + } + }, + "status": 200 + } + }, + "mark": "line", + "encoding": { + "x": { + "field": "key", + "type": "temporal", + "axis": null + }, + "y": { + "field": "doc_count", + "type": "quantitative", + "axis": null + } + }, + "autosize": { "type": "fit" }, + "width": 512, + "height": 512, + "config": { "kibana": { "renderer": "svg" }} +} diff --git a/src/plugins/vis_type_vega/public/vega_type.ts b/src/plugins/vis_type_vega/public/vega_type.ts index 55ad134c05301..5825661f9001c 100644 --- a/src/plugins/vis_type_vega/public/vega_type.ts +++ b/src/plugins/vis_type_vega/public/vega_type.ts @@ -25,8 +25,7 @@ import { VegaVisEditor } from './components'; import { createVegaRequestHandler } from './vega_request_handler'; // @ts-ignore import { createVegaVisualization } from './vega_visualization'; -// @ts-ignore -import defaultSpec from '!!raw-loader!./default.spec.hjson'; +import { getDefaultSpec } from './default_spec'; export const createVegaTypeDefinition = (dependencies: VegaVisualizationDependencies) => { const requestHandler = createVegaRequestHandler(dependencies); @@ -40,7 +39,7 @@ export const createVegaTypeDefinition = (dependencies: VegaVisualizationDependen description: 'Vega and Vega-Lite are product names and should not be translated', }), icon: 'visVega', - visConfig: { defaults: { spec: defaultSpec } }, + visConfig: { defaults: { spec: getDefaultSpec() } }, editorConfig: { optionsTemplate: VegaVisEditor, enableAutoApply: true, diff --git a/src/plugins/vis_type_vega/public/vega_visualization.test.js b/src/plugins/vis_type_vega/public/vega_visualization.test.js new file mode 100644 index 0000000000000..108b34b36c66f --- /dev/null +++ b/src/plugins/vis_type_vega/public/vega_visualization.test.js @@ -0,0 +1,233 @@ +/* + * 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 $ from 'jquery'; + +import 'leaflet/dist/leaflet.js'; +import 'leaflet-vega'; +import { createVegaVisualization } from './vega_visualization'; + +import vegaliteGraph from './test_utils/vegalite_graph.json'; +import vegaGraph from './test_utils/vega_graph.json'; +import vegaMapGraph from './test_utils/vega_map_test.json'; + +import { VegaParser } from './data_model/vega_parser'; +import { SearchAPI } from './data_model/search_api'; + +import { createVegaTypeDefinition } from './vega_type'; + +import { + setInjectedVars, + setData, + setSavedObjects, + setNotifications, + setKibanaMapFactory, +} from './services'; +import { coreMock } from '../../../core/public/mocks'; +import { dataPluginMock } from '../../data/public/mocks'; +import { KibanaMap } from '../../maps_legacy/public/map/kibana_map'; + +jest.mock('./default_spec', () => ({ + getDefaultSpec: () => jest.requireActual('./test_utils/default.spec.json'), +})); + +jest.mock('./lib/vega', () => ({ + vega: jest.requireActual('vega'), + vegaLite: jest.requireActual('vega-lite'), +})); + +// FLAKY: https://github.com/elastic/kibana/issues/71713 +describe.skip('VegaVisualizations', () => { + let domNode; + let VegaVisualization; + let vis; + let vegaVisualizationDependencies; + let vegaVisType; + + let mockWidth; + let mockedWidthValue; + let mockHeight; + let mockedHeightValue; + + const coreStart = coreMock.createStart(); + const dataPluginStart = dataPluginMock.createStartContract(); + + const setupDOM = (width = 512, height = 512) => { + mockedWidthValue = width; + mockedHeightValue = height; + domNode = document.createElement('div'); + + mockWidth = jest.spyOn($.prototype, 'width').mockImplementation(() => mockedWidthValue); + mockHeight = jest.spyOn($.prototype, 'height').mockImplementation(() => mockedHeightValue); + }; + + setKibanaMapFactory((...args) => new KibanaMap(...args)); + setInjectedVars({ + emsTileLayerId: {}, + enableExternalUrls: true, + esShardTimeout: 10000, + }); + setData(dataPluginStart); + setSavedObjects(coreStart.savedObjects); + setNotifications(coreStart.notifications); + + beforeEach(() => { + vegaVisualizationDependencies = { + core: coreMock.createSetup(), + plugins: { + data: dataPluginMock.createSetupContract(), + }, + }; + + vegaVisType = createVegaTypeDefinition(vegaVisualizationDependencies); + VegaVisualization = createVegaVisualization(vegaVisualizationDependencies); + }); + + describe('VegaVisualization - basics', () => { + beforeEach(async () => { + setupDOM(); + + vis = { + type: vegaVisType, + }; + }); + + afterEach(() => { + mockWidth.mockRestore(); + mockHeight.mockRestore(); + }); + + test('should show vegalite graph and update on resize (may fail in dev env)', async () => { + let vegaVis; + try { + vegaVis = new VegaVisualization(domNode, vis); + + const vegaParser = new VegaParser( + JSON.stringify(vegaliteGraph), + new SearchAPI({ + search: dataPluginStart.search, + uiSettings: coreStart.uiSettings, + injectedMetadata: coreStart.injectedMetadata, + }) + ); + await vegaParser.parseAsync(); + await vegaVis.render(vegaParser); + expect(domNode.innerHTML).toMatchSnapshot(); + + mockedWidthValue = 256; + mockedHeightValue = 256; + + await vegaVis._vegaView.resize(); + + expect(domNode.innerHTML).toMatchSnapshot(); + } finally { + vegaVis.destroy(); + } + }); + + test('should show vega graph (may fail in dev env)', async () => { + let vegaVis; + try { + vegaVis = new VegaVisualization(domNode, vis); + const vegaParser = new VegaParser( + JSON.stringify(vegaGraph), + new SearchAPI({ + search: dataPluginStart.search, + uiSettings: coreStart.uiSettings, + injectedMetadata: coreStart.injectedMetadata, + }) + ); + await vegaParser.parseAsync(); + + await vegaVis.render(vegaParser); + expect(domNode.innerHTML).toMatchSnapshot(); + } finally { + vegaVis.destroy(); + } + }); + + test('should show vega blank rectangle on top of a map (vegamap)', async () => { + let vegaVis; + try { + vegaVis = new VegaVisualization(domNode, vis); + const vegaParser = new VegaParser( + JSON.stringify(vegaMapGraph), + new SearchAPI({ + search: dataPluginStart.search, + uiSettings: coreStart.uiSettings, + injectedMetadata: coreStart.injectedMetadata, + }) + ); + await vegaParser.parseAsync(); + + mockedWidthValue = 256; + mockedHeightValue = 256; + + await vegaVis.render(vegaParser); + expect(domNode.innerHTML).toMatchSnapshot(); + } finally { + vegaVis.destroy(); + } + }); + + test('should add a small subpixel value to the height of the canvas to avoid getting it set to 0', async () => { + let vegaVis; + try { + vegaVis = new VegaVisualization(domNode, vis); + const vegaParser = new VegaParser( + `{ + "$schema": "https://vega.github.io/schema/vega/v5.json", + "marks": [ + { + "type": "text", + "encode": { + "update": { + "text": { + "value": "Test" + }, + "align": {"value": "center"}, + "baseline": {"value": "middle"}, + "xc": {"signal": "width/2"}, + "yc": {"signal": "height/2"} + fontSize: {value: "14"} + } + } + } + ] + }`, + new SearchAPI({ + search: dataPluginStart.search, + uiSettings: coreStart.uiSettings, + injectedMetadata: coreStart.injectedMetadata, + }) + ); + await vegaParser.parseAsync(); + + mockedWidthValue = 256; + mockedHeightValue = 256; + + await vegaVis.render(vegaParser); + const vegaView = vegaVis._vegaView._view; + expect(vegaView.height()).toBe(250.00000001); + } finally { + vegaVis.destroy(); + } + }); + }); +}); diff --git a/src/plugins/visualizations/public/expressions/visualization_renderer.tsx b/src/plugins/visualizations/public/expressions/visualization_renderer.tsx index 0fd81c753da24..1bca5b4f0d539 100644 --- a/src/plugins/visualizations/public/expressions/visualization_renderer.tsx +++ b/src/plugins/visualizations/public/expressions/visualization_renderer.tsx @@ -33,6 +33,7 @@ export const visualization = () => ({ const visType = config.visType || visConfig.type; const vis = new ExprVis({ + title: config.title, type: visType as string, params: visConfig as VisParams, }); diff --git a/src/plugins/visualizations/public/legacy/build_pipeline.ts b/src/plugins/visualizations/public/legacy/build_pipeline.ts index 62ff1f83426b9..2ef07bf18c91c 100644 --- a/src/plugins/visualizations/public/legacy/build_pipeline.ts +++ b/src/plugins/visualizations/public/legacy/build_pipeline.ts @@ -490,7 +490,7 @@ export const buildPipeline = async ( const { indexPattern, searchSource } = vis.data; const query = searchSource!.getField('query'); const filters = searchSource!.getField('filter'); - const { uiState } = vis; + const { uiState, title } = vis; // context let pipeline = `kibana | kibana_context `; @@ -519,7 +519,7 @@ export const buildPipeline = async ( timefilter: params.timefilter, }); if (buildPipelineVisFunction[vis.type.name]) { - pipeline += buildPipelineVisFunction[vis.type.name](vis.params, schemas, uiState); + pipeline += buildPipelineVisFunction[vis.type.name]({ title, ...vis.params }, schemas, uiState); } else if (vislibCharts.includes(vis.type.name)) { const visConfig = { ...vis.params }; visConfig.dimensions = await buildVislibDimensions(vis, params); diff --git a/src/test_utils/public/key_map.ts b/src/test_utils/public/key_map.ts new file mode 100644 index 0000000000000..aac3c6b2db3e0 --- /dev/null +++ b/src/test_utils/public/key_map.ts @@ -0,0 +1,121 @@ +/* + * 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. + */ + +export const keyMap: { [key: number]: string } = { + 8: 'backspace', + 9: 'tab', + 13: 'enter', + 16: 'shift', + 17: 'ctrl', + 18: 'alt', + 19: 'pause', + 20: 'capsLock', + 27: 'escape', + 32: 'space', + 33: 'pageUp', + 34: 'pageDown', + 35: 'end', + 36: 'home', + 37: 'left', + 38: 'up', + 39: 'right', + 40: 'down', + 45: 'insert', + 46: 'delete', + 48: '0', + 49: '1', + 50: '2', + 51: '3', + 52: '4', + 53: '5', + 54: '6', + 55: '7', + 56: '8', + 57: '9', + 65: 'a', + 66: 'b', + 67: 'c', + 68: 'd', + 69: 'e', + 70: 'f', + 71: 'g', + 72: 'h', + 73: 'i', + 74: 'j', + 75: 'k', + 76: 'l', + 77: 'm', + 78: 'n', + 79: 'o', + 80: 'p', + 81: 'q', + 82: 'r', + 83: 's', + 84: 't', + 85: 'u', + 86: 'v', + 87: 'w', + 88: 'x', + 89: 'y', + 90: 'z', + 91: 'leftWindowKey', + 92: 'rightWindowKey', + 93: 'selectKey', + 96: '0', + 97: '1', + 98: '2', + 99: '3', + 100: '4', + 101: '5', + 102: '6', + 103: '7', + 104: '8', + 105: '9', + 106: 'multiply', + 107: 'add', + 109: 'subtract', + 110: 'period', + 111: 'divide', + 112: 'f1', + 113: 'f2', + 114: 'f3', + 115: 'f4', + 116: 'f5', + 117: 'f6', + 118: 'f7', + 119: 'f8', + 120: 'f9', + 121: 'f10', + 122: 'f11', + 123: 'f12', + 144: 'numLock', + 145: 'scrollLock', + 186: 'semiColon', + 187: 'equalSign', + 188: 'comma', + 189: 'dash', + 190: 'period', + 191: 'forwardSlash', + 192: 'graveAccent', + 219: 'openBracket', + 220: 'backSlash', + 221: 'closeBracket', + 222: 'singleQuote', + 224: 'meta', +}; diff --git a/src/test_utils/public/simulate_keys.js b/src/test_utils/public/simulate_keys.js index 56596508a2181..460a75486169a 100644 --- a/src/test_utils/public/simulate_keys.js +++ b/src/test_utils/public/simulate_keys.js @@ -20,7 +20,7 @@ import $ from 'jquery'; import _ from 'lodash'; import Bluebird from 'bluebird'; -import { keyMap } from 'ui/directives/key_map'; +import { keyMap } from './key_map'; const reverseKeyMap = _.mapValues(_.invert(keyMap), _.ary(_.parseInt, 1)); /** diff --git a/test/functional/apps/visualize/_area_chart.js b/test/functional/apps/visualize/_area_chart.js index ea4db35d75ccf..41e56986f677b 100644 --- a/test/functional/apps/visualize/_area_chart.js +++ b/test/functional/apps/visualize/_area_chart.js @@ -246,9 +246,7 @@ export default function ({ getService, getPageObjects }) { await inspector.close(); }); - // Preventing ES Promotion for master (8.0) - // https://github.com/elastic/kibana/issues/64734 - it.skip('does not scale top hit agg', async () => { + it('does not scale top hit agg', async () => { const expectedTableData = [ ['2015-09-20 00:00', '6', '9.035KB'], ['2015-09-20 01:00', '9', '5.854KB'], diff --git a/test/functional/services/saved_query_management_component.ts b/test/functional/services/saved_query_management_component.ts index 66bf15f3da53c..f600dba368485 100644 --- a/test/functional/services/saved_query_management_component.ts +++ b/test/functional/services/saved_query_management_component.ts @@ -20,11 +20,15 @@ import expect from '@kbn/expect'; import { FtrProviderContext } from '../ftr_provider_context'; -export function SavedQueryManagementComponentProvider({ getService }: FtrProviderContext) { +export function SavedQueryManagementComponentProvider({ + getService, + getPageObjects, +}: FtrProviderContext) { const testSubjects = getService('testSubjects'); const queryBar = getService('queryBar'); const retry = getService('retry'); const config = getService('config'); + const PageObjects = getPageObjects(['common']); class SavedQueryManagementComponent { public async getCurrentlyLoadedQueryID() { @@ -105,7 +109,7 @@ export function SavedQueryManagementComponentProvider({ getService }: FtrProvide public async deleteSavedQuery(title: string) { await this.openSavedQueryManagementComponent(); await testSubjects.click(`~delete-saved-query-${title}-button`); - await testSubjects.click('confirmModalConfirmButton'); + await PageObjects.common.clickConfirmOnModal(); } async clearCurrentlyLoadedQuery() { @@ -169,8 +173,8 @@ export function SavedQueryManagementComponentProvider({ getService }: FtrProvide const isOpenAlready = await testSubjects.exists('saved-query-management-popover'); if (isOpenAlready) return; - await testSubjects.click('saved-query-management-popover-button'); await retry.waitFor('saved query management popover to have any text', async () => { + await testSubjects.click('saved-query-management-popover-button'); const queryText = await testSubjects.getVisibleText('saved-query-management-popover'); return queryText.length > 0; }); @@ -180,7 +184,10 @@ export function SavedQueryManagementComponentProvider({ getService }: FtrProvide const isOpenAlready = await testSubjects.exists('saved-query-management-popover'); if (!isOpenAlready) return; - await testSubjects.click('saved-query-management-popover-button'); + await retry.try(async () => { + await testSubjects.click('saved-query-management-popover-button'); + await testSubjects.missingOrFail('saved-query-management-popover'); + }); } async openSaveCurrentQueryModal() { diff --git a/test/plugin_functional/plugins/management_test_plugin/public/plugin.tsx b/test/plugin_functional/plugins/management_test_plugin/public/plugin.tsx index 494570b26f561..9cbff335590a3 100644 --- a/test/plugin_functional/plugins/management_test_plugin/public/plugin.tsx +++ b/test/plugin_functional/plugins/management_test_plugin/public/plugin.tsx @@ -21,12 +21,12 @@ import * as React from 'react'; import ReactDOM from 'react-dom'; import { Router, Switch, Route, Link } from 'react-router-dom'; import { CoreSetup, Plugin } from 'kibana/public'; -import { ManagementSetup, ManagementSectionId } from '../../../../../src/plugins/management/public'; +import { ManagementSetup } from '../../../../../src/plugins/management/public'; export class ManagementTestPlugin implements Plugin { public setup(core: CoreSetup, { management }: { management: ManagementSetup }) { - const testSection = management.sections.getSection(ManagementSectionId.Data); + const testSection = management.sections.section.data; testSection.registerApp({ id: 'test-management', diff --git a/test/scripts/jenkins_xpack_page_load_metrics.sh b/test/scripts/jenkins_xpack_page_load_metrics.sh deleted file mode 100644 index 679f0b8d2ddc5..0000000000000 --- a/test/scripts/jenkins_xpack_page_load_metrics.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash - -source test/scripts/jenkins_test_setup_xpack.sh - -checks-reporter-with-killswitch "Capture Kibana page load metrics" \ - node scripts/functional_tests \ - --debug --bail \ - --kibana-install-dir "$installDir" \ - --config test/page_load_metrics/config.ts; diff --git a/test/scripts/jenkins_xpack_visual_regression.sh b/test/scripts/jenkins_xpack_visual_regression.sh index ac567a188a6d4..7fb7d7b71b2e4 100755 --- a/test/scripts/jenkins_xpack_visual_regression.sh +++ b/test/scripts/jenkins_xpack_visual_regression.sh @@ -11,6 +11,15 @@ installDir="$PARENT_DIR/install/kibana" mkdir -p "$installDir" tar -xzf "$linuxBuild" -C "$installDir" --strip=1 +# cd "$KIBANA_DIR" +# source "test/scripts/jenkins_xpack_page_load_metrics.sh" + +cd "$KIBANA_DIR" +source "test/scripts/jenkins_xpack_saved_objects_field_metrics.sh" + +cd "$KIBANA_DIR" +source "test/scripts/jenkins_xpack_saved_objects_field_metrics.sh" + echo " -> running visual regression tests from x-pack directory" cd "$XPACK_DIR" yarn percy exec -t 10000 -- -- \ @@ -18,9 +27,3 @@ yarn percy exec -t 10000 -- -- \ --debug --bail \ --kibana-install-dir "$installDir" \ --config test/visual_regression/config.ts; - -# cd "$KIBANA_DIR" -# source "test/scripts/jenkins_xpack_page_load_metrics.sh" - -cd "$KIBANA_DIR" -source "test/scripts/jenkins_xpack_saved_objects_field_metrics.sh" diff --git a/vars/githubCommitStatus.groovy b/vars/githubCommitStatus.groovy index 4cd4228d55f03..17d3c234f6928 100644 --- a/vars/githubCommitStatus.groovy +++ b/vars/githubCommitStatus.groovy @@ -35,7 +35,12 @@ def onFinish() { // state: error|failure|pending|success def create(sha, state, description, context = 'kibana-ci') { withGithubCredentials { - return githubApi.post("repos/elastic/kibana/statuses/${sha}", [ state: state, description: description, context: context, target_url: env.BUILD_URL ]) + return githubApi.post("repos/elastic/kibana/statuses/${sha}", [ + state: state, + description: description, + context: context, + target_url: env.BUILD_URL + ]) } } diff --git a/x-pack/.gitignore b/x-pack/.gitignore index 0c916ef0e9b91..d73b6f64f036a 100644 --- a/x-pack/.gitignore +++ b/x-pack/.gitignore @@ -3,7 +3,6 @@ /target /test/functional/failure_debug /test/functional/screenshots -/test/page_load_metrics/screenshots /test/functional/apps/reporting/reports/session /test/reporting/configs/failure_debug/ /plugins/reporting/.chromium/ diff --git a/x-pack/legacy/plugins/monitoring/index.ts b/x-pack/legacy/plugins/monitoring/index.ts index ee31a3037a0cb..f03e1ebc009f5 100644 --- a/x-pack/legacy/plugins/monitoring/index.ts +++ b/x-pack/legacy/plugins/monitoring/index.ts @@ -6,7 +6,6 @@ import Hapi from 'hapi'; import { config } from './config'; -import { KIBANA_ALERTING_ENABLED } from '../../../plugins/monitoring/common/constants'; /** * Invokes plugin modules to instantiate the Monitoring plugin for Kibana @@ -14,9 +13,6 @@ import { KIBANA_ALERTING_ENABLED } from '../../../plugins/monitoring/common/cons * @return {Object} Monitoring UI Kibana plugin object */ const deps = ['kibana', 'elasticsearch', 'xpack_main']; -if (KIBANA_ALERTING_ENABLED) { - deps.push(...['alerts', 'actions']); -} export const monitoring = (kibana: any) => { return new kibana.Plugin({ require: deps, diff --git a/x-pack/plugins/actions/README.md b/x-pack/plugins/actions/README.md index e6b22da7a1fe3..3470ede0f15c7 100644 --- a/x-pack/plugins/actions/README.md +++ b/x-pack/plugins/actions/README.md @@ -160,7 +160,7 @@ This is the primary function for an action type. Whenever the action needs to ex | config | The decrypted configuration given to an action. This comes from the action saved object that is partially or fully encrypted within the data store. If you would like to validate the config before being passed to the executor, define `validate.config` within the action type. | | params | Parameters for the execution. These will be given at execution time by either an alert or manually provided when calling the plugin provided execute function. | | services.callCluster(path, opts) | Use this to do Elasticsearch queries on the cluster Kibana connects to. This function is the same as any other `callCluster` in Kibana but runs in the context of the user who is calling the action when security is enabled. | -| services.getScopedCallCluster | This function scopes an instance of CallCluster by returning a `callCluster(path, opts)` function that runs in the context of the user who is calling the action when security is enabled. This must only be called with instances of CallCluster provided by core. | +| services.getLegacyScopedClusterClient | This function returns an instance of the LegacyScopedClusterClient scoped to the user who is calling the action when security is enabled. | | services.savedObjectsClient | This is an instance of the saved objects client. This provides the ability to do CRUD on any saved objects within the same space the alert lives in.

The scope of the saved objects client is tied to the user in context calling the execute API or the API key provided to the execute plugin function (only when security isenabled). | | services.log(tags, [data], [timestamp]) | Use this to create server logs. (This is the same function as server.log) | diff --git a/x-pack/plugins/actions/server/mocks.ts b/x-pack/plugins/actions/server/mocks.ts index 87aa571ce6b8a..b1e40dce811a0 100644 --- a/x-pack/plugins/actions/server/mocks.ts +++ b/x-pack/plugins/actions/server/mocks.ts @@ -38,7 +38,7 @@ const createServicesMock = () => { } > = { callCluster: elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser, - getScopedCallCluster: jest.fn(), + getLegacyScopedClusterClient: jest.fn(), savedObjectsClient: savedObjectsClientMock.create(), }; return mock; diff --git a/x-pack/plugins/actions/server/plugin.ts b/x-pack/plugins/actions/server/plugin.ts index eae2595136627..114c85ae9f9da 100644 --- a/x-pack/plugins/actions/server/plugin.ts +++ b/x-pack/plugins/actions/server/plugin.ts @@ -307,8 +307,8 @@ export class ActionsPlugin implements Plugin, Plugi return (request) => ({ callCluster: elasticsearch.legacy.client.asScoped(request).callAsCurrentUser, savedObjectsClient: getScopedClient(request), - getScopedCallCluster(clusterClient: ILegacyClusterClient) { - return clusterClient.asScoped(request).callAsCurrentUser; + getLegacyScopedClusterClient(clusterClient: ILegacyClusterClient) { + return clusterClient.asScoped(request); }, }); } diff --git a/x-pack/plugins/actions/server/types.ts b/x-pack/plugins/actions/server/types.ts index ca5da2779139e..a8e19e3ff2e79 100644 --- a/x-pack/plugins/actions/server/types.ts +++ b/x-pack/plugins/actions/server/types.ts @@ -25,9 +25,7 @@ export type SpaceIdToNamespaceFunction = (spaceId?: string) => string | undefine export interface Services { callCluster: ILegacyScopedClusterClient['callAsCurrentUser']; savedObjectsClient: SavedObjectsClientContract; - getScopedCallCluster( - clusterClient: ILegacyClusterClient - ): ILegacyScopedClusterClient['callAsCurrentUser']; + getLegacyScopedClusterClient(clusterClient: ILegacyClusterClient): ILegacyScopedClusterClient; } declare module 'src/core/server' { diff --git a/x-pack/plugins/alerts/README.md b/x-pack/plugins/alerts/README.md index 2f2ffb52e7e90..0464ec78a4e9d 100644 --- a/x-pack/plugins/alerts/README.md +++ b/x-pack/plugins/alerts/README.md @@ -103,7 +103,7 @@ This is the primary function for an alert type. Whenever the alert needs to exec |---|---| |services.callCluster(path, opts)|Use this to do Elasticsearch queries on the cluster Kibana connects to. This function is the same as any other `callCluster` in Kibana but in the context of the user who created the alert when security is enabled.| |services.savedObjectsClient|This is an instance of the saved objects client. This provides the ability to do CRUD on any saved objects within the same space the alert lives in.

The scope of the saved objects client is tied to the user who created the alert (only when security isenabled).| -|services.getScopedCallCluster|This function scopes an instance of CallCluster by returning a `callCluster(path, opts)` function that runs in the context of the user who created the alert when security is enabled. This must only be called with instances of CallCluster provided by core.| +|services.getLegacyScopedClusterClient|This function returns an instance of the LegacyScopedClusterClient scoped to the user who created the alert when security is enabled.| |services.alertInstanceFactory(id)|This [alert instance factory](#alert-instance-factory) creates instances of alerts and must be used in order to execute actions. The id you give to the alert instance factory is a unique identifier to the alert instance.| |services.log(tags, [data], [timestamp])|Use this to create server logs. (This is the same function as server.log)| |startedAt|The date and time the alert type started execution.| diff --git a/x-pack/plugins/alerts/server/alerts_client.ts b/x-pack/plugins/alerts/server/alerts_client.ts index e8e6f82f13882..e49745b186bb3 100644 --- a/x-pack/plugins/alerts/server/alerts_client.ts +++ b/x-pack/plugins/alerts/server/alerts_client.ts @@ -5,7 +5,7 @@ */ import Boom from 'boom'; -import { omit, isEqual, map } from 'lodash'; +import { omit, isEqual, map, truncate } from 'lodash'; import { i18n } from '@kbn/i18n'; import { Logger, @@ -53,7 +53,7 @@ interface ConstructorOptions { spaceId?: string; namespace?: string; getUserName: () => Promise; - createAPIKey: () => Promise; + createAPIKey: (name: string) => Promise; invalidateAPIKey: (params: InvalidateAPIKeyParams) => Promise; getActionsClient: () => Promise; } @@ -129,7 +129,7 @@ export class AlertsClient { private readonly taskManager: TaskManagerStartContract; private readonly savedObjectsClient: SavedObjectsClientContract; private readonly alertTypeRegistry: AlertTypeRegistry; - private readonly createAPIKey: () => Promise; + private readonly createAPIKey: (name: string) => Promise; private readonly invalidateAPIKey: ( params: InvalidateAPIKeyParams ) => Promise; @@ -167,7 +167,10 @@ export class AlertsClient { const alertType = this.alertTypeRegistry.get(data.alertTypeId); const validatedAlertTypeParams = validateAlertTypeParams(alertType, data.params); const username = await this.getUserName(); - const createdAPIKey = data.enabled ? await this.createAPIKey() : null; + + const createdAPIKey = data.enabled + ? await this.createAPIKey(this.generateAPIKeyName(alertType.id, data.name)) + : null; this.validateActions(alertType, data.actions); @@ -334,7 +337,9 @@ export class AlertsClient { const { actions, references } = await this.denormalizeActions(data.actions); const username = await this.getUserName(); - const createdAPIKey = attributes.enabled ? await this.createAPIKey() : null; + const createdAPIKey = attributes.enabled + ? await this.createAPIKey(this.generateAPIKeyName(alertType.id, data.name)) + : null; const apiKeyAttributes = this.apiKeyAsAlertAttributes(createdAPIKey, username); const updatedObject = await this.savedObjectsClient.update( @@ -406,7 +411,10 @@ export class AlertsClient { id, { ...attributes, - ...this.apiKeyAsAlertAttributes(await this.createAPIKey(), username), + ...this.apiKeyAsAlertAttributes( + await this.createAPIKey(this.generateAPIKeyName(attributes.alertTypeId, attributes.name)), + username + ), updatedBy: username, }, { version } @@ -464,7 +472,12 @@ export class AlertsClient { { ...attributes, enabled: true, - ...this.apiKeyAsAlertAttributes(await this.createAPIKey(), username), + ...this.apiKeyAsAlertAttributes( + await this.createAPIKey( + this.generateAPIKeyName(attributes.alertTypeId, attributes.name) + ), + username + ), updatedBy: username, }, { version } @@ -697,4 +710,8 @@ export class AlertsClient { references, }; } + + private generateAPIKeyName(alertTypeId: string, alertName: string) { + return truncate(`Alerting: ${alertTypeId}/${alertName}`, { length: 256 }); + } } diff --git a/x-pack/plugins/alerts/server/alerts_client_factory.ts b/x-pack/plugins/alerts/server/alerts_client_factory.ts index af546f965d7df..30fcd1b949f2b 100644 --- a/x-pack/plugins/alerts/server/alerts_client_factory.ts +++ b/x-pack/plugins/alerts/server/alerts_client_factory.ts @@ -70,7 +70,7 @@ export class AlertsClientFactory { const user = await securityPluginSetup.authc.getCurrentUser(request); return user ? user.username : null; }, - async createAPIKey() { + async createAPIKey(name: string) { if (!securityPluginSetup) { return { apiKeysEnabled: false }; } @@ -78,7 +78,11 @@ export class AlertsClientFactory { // API key for the user, instead of having the user create it themselves, which requires api_key // privileges const createAPIKeyResult = await securityPluginSetup.authc.grantAPIKeyAsInternalUser( - request + request, + { + name, + role_descriptors: {}, + } ); if (!createAPIKeyResult) { return { apiKeysEnabled: false }; diff --git a/x-pack/plugins/alerts/server/mocks.ts b/x-pack/plugins/alerts/server/mocks.ts index 84f79d53f218c..c39aa13b580fc 100644 --- a/x-pack/plugins/alerts/server/mocks.ts +++ b/x-pack/plugins/alerts/server/mocks.ts @@ -59,7 +59,7 @@ const createAlertServicesMock = () => { .fn, [string]>() .mockReturnValue(alertInstanceFactoryMock), callCluster: elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser, - getScopedCallCluster: jest.fn(), + getLegacyScopedClusterClient: jest.fn(), savedObjectsClient: savedObjectsClientMock.create(), }; }; diff --git a/x-pack/plugins/alerts/server/plugin.ts b/x-pack/plugins/alerts/server/plugin.ts index 23a5dc51d0475..07ed021d8ca84 100644 --- a/x-pack/plugins/alerts/server/plugin.ts +++ b/x-pack/plugins/alerts/server/plugin.ts @@ -273,8 +273,8 @@ export class AlertingPlugin { return (request) => ({ callCluster: elasticsearch.legacy.client.asScoped(request).callAsCurrentUser, savedObjectsClient: this.getScopedClientWithAlertSavedObjectType(savedObjects, request), - getScopedCallCluster(clusterClient: ILegacyClusterClient) { - return clusterClient.asScoped(request).callAsCurrentUser; + getLegacyScopedClusterClient(clusterClient: ILegacyClusterClient) { + return clusterClient.asScoped(request); }, }); } diff --git a/x-pack/plugins/alerts/server/types.ts b/x-pack/plugins/alerts/server/types.ts index 24dfb391f0791..66eec370f2c20 100644 --- a/x-pack/plugins/alerts/server/types.ts +++ b/x-pack/plugins/alerts/server/types.ts @@ -40,9 +40,7 @@ declare module 'src/core/server' { export interface Services { callCluster: ILegacyScopedClusterClient['callAsCurrentUser']; savedObjectsClient: SavedObjectsClientContract; - getScopedCallCluster( - clusterClient: ILegacyClusterClient - ): ILegacyScopedClusterClient['callAsCurrentUser']; + getLegacyScopedClusterClient(clusterClient: ILegacyClusterClient): ILegacyScopedClusterClient; } export interface AlertServices extends Services { diff --git a/x-pack/plugins/apm/common/__snapshots__/apm_telemetry.test.ts.snap b/x-pack/plugins/apm/common/__snapshots__/apm_telemetry.test.ts.snap index 4ee7692222d68..1d8cfa28aea75 100644 --- a/x-pack/plugins/apm/common/__snapshots__/apm_telemetry.test.ts.snap +++ b/x-pack/plugins/apm/common/__snapshots__/apm_telemetry.test.ts.snap @@ -1,929 +1,973 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`APM telemetry helpers getApmTelemetry generates a JSON object with the telemetry mapping 1`] = ` -Object { - "properties": Object { - "agents": Object { - "properties": Object { - "dotnet": Object { - "properties": Object { - "agent": Object { - "properties": Object { - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "service": Object { - "properties": Object { - "framework": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "language": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "runtime": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - }, - }, - }, - }, - "go": Object { - "properties": Object { - "agent": Object { - "properties": Object { - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "service": Object { - "properties": Object { - "framework": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "language": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "runtime": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - }, - }, - }, - }, - "java": Object { - "properties": Object { - "agent": Object { - "properties": Object { - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "service": Object { - "properties": Object { - "framework": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "language": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "runtime": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - }, - }, - }, - }, - "js-base": Object { - "properties": Object { - "agent": Object { - "properties": Object { - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "service": Object { - "properties": Object { - "framework": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "language": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "runtime": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - }, - }, - }, - }, - "nodejs": Object { - "properties": Object { - "agent": Object { - "properties": Object { - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "service": Object { - "properties": Object { - "framework": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "language": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "runtime": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - }, - }, - }, - }, - "python": Object { - "properties": Object { - "agent": Object { - "properties": Object { - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "service": Object { - "properties": Object { - "framework": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "language": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "runtime": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - }, - }, - }, - }, - "ruby": Object { - "properties": Object { - "agent": Object { - "properties": Object { - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "service": Object { - "properties": Object { - "framework": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "language": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "runtime": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - }, - }, - }, - }, - "rum-js": Object { - "properties": Object { - "agent": Object { - "properties": Object { - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "service": Object { - "properties": Object { - "framework": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "language": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "runtime": Object { - "properties": Object { - "composite": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "name": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "version": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - }, - }, - }, - }, - }, - }, - "cardinality": Object { - "properties": Object { - "transaction": Object { - "properties": Object { - "name": Object { - "properties": Object { - "all_agents": Object { - "properties": Object { - "1d": Object { - "type": "long", - }, - }, - }, - "rum": Object { - "properties": Object { - "1d": Object { - "type": "long", - }, - }, - }, - }, - }, - }, - }, - "user_agent": Object { - "properties": Object { - "original": Object { - "properties": Object { - "all_agents": Object { - "properties": Object { - "1d": Object { - "type": "long", - }, - }, - }, - "rum": Object { - "properties": Object { - "1d": Object { - "type": "long", - }, - }, - }, - }, - }, - }, - }, - }, - }, - "cloud": Object { - "properties": Object { - "availability_zone": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "provider": Object { - "ignore_above": 1024, - "type": "keyword", - }, - "region": Object { - "ignore_above": 1024, - "type": "keyword", - }, - }, - }, - "counts": Object { - "properties": Object { - "agent_configuration": Object { - "properties": Object { - "all": Object { - "type": "long", - }, - }, - }, - "error": Object { - "properties": Object { - "1d": Object { - "type": "long", - }, - "all": Object { - "type": "long", - }, - }, - }, - "max_error_groups_per_service": Object { - "properties": Object { - "1d": Object { - "type": "long", - }, - }, - }, - "max_transaction_groups_per_service": Object { - "properties": Object { - "1d": Object { - "type": "long", - }, - }, - }, - "metric": Object { - "properties": Object { - "1d": Object { - "type": "long", - }, - "all": Object { - "type": "long", - }, - }, - }, - "onboarding": Object { - "properties": Object { - "1d": Object { - "type": "long", - }, - "all": Object { - "type": "long", - }, - }, - }, - "services": Object { - "properties": Object { - "1d": Object { - "type": "long", - }, - }, - }, - "sourcemap": Object { - "properties": Object { - "1d": Object { - "type": "long", - }, - "all": Object { - "type": "long", - }, - }, - }, - "span": Object { - "properties": Object { - "1d": Object { - "type": "long", - }, - "all": Object { - "type": "long", - }, - }, - }, - "traces": Object { - "properties": Object { - "1d": Object { - "type": "long", - }, - }, - }, - "transaction": Object { - "properties": Object { - "1d": Object { - "type": "long", - }, - "all": Object { - "type": "long", - }, - }, - }, - }, - }, - "has_any_services": Object { - "type": "boolean", - }, - "indices": Object { - "properties": Object { - "all": Object { - "properties": Object { - "total": Object { - "properties": Object { - "docs": Object { - "properties": Object { - "count": Object { - "type": "long", - }, - }, - }, - "store": Object { - "properties": Object { - "size_in_bytes": Object { - "type": "long", - }, - }, - }, - }, - }, - }, - }, - "shards": Object { - "properties": Object { - "total": Object { - "type": "long", - }, - }, - }, - }, - }, - "integrations": Object { - "properties": Object { - "ml": Object { - "properties": Object { - "all_jobs_count": Object { - "type": "long", - }, - }, - }, - }, - }, - "retainment": Object { - "properties": Object { - "error": Object { - "properties": Object { - "ms": Object { - "type": "long", - }, - }, - }, - "metric": Object { - "properties": Object { - "ms": Object { - "type": "long", - }, - }, - }, - "onboarding": Object { - "properties": Object { - "ms": Object { - "type": "long", - }, - }, - }, - "span": Object { - "properties": Object { - "ms": Object { - "type": "long", - }, - }, - }, - "transaction": Object { - "properties": Object { - "ms": Object { - "type": "long", - }, - }, - }, - }, - }, - "services_per_agent": Object { - "properties": Object { - "dotnet": Object { - "null_value": 0, - "type": "long", - }, - "go": Object { - "null_value": 0, - "type": "long", - }, - "java": Object { - "null_value": 0, - "type": "long", - }, - "js-base": Object { - "null_value": 0, - "type": "long", - }, - "nodejs": Object { - "null_value": 0, - "type": "long", - }, - "python": Object { - "null_value": 0, - "type": "long", - }, - "ruby": Object { - "null_value": 0, - "type": "long", - }, - "rum-js": Object { - "null_value": 0, - "type": "long", - }, - }, - }, - "tasks": Object { - "properties": Object { - "agent_configuration": Object { - "properties": Object { - "took": Object { - "properties": Object { - "ms": Object { - "type": "long", - }, - }, - }, - }, - }, - "agents": Object { - "properties": Object { - "took": Object { - "properties": Object { - "ms": Object { - "type": "long", - }, - }, - }, - }, - }, - "cardinality": Object { - "properties": Object { - "took": Object { - "properties": Object { - "ms": Object { - "type": "long", - }, - }, - }, - }, - }, - "groupings": Object { - "properties": Object { - "took": Object { - "properties": Object { - "ms": Object { - "type": "long", - }, - }, - }, - }, - }, - "indices_stats": Object { - "properties": Object { - "took": Object { - "properties": Object { - "ms": Object { - "type": "long", - }, - }, - }, - }, - }, - "integrations": Object { - "properties": Object { - "took": Object { - "properties": Object { - "ms": Object { - "type": "long", - }, - }, - }, - }, - }, - "processor_events": Object { - "properties": Object { - "took": Object { - "properties": Object { - "ms": Object { - "type": "long", - }, - }, - }, - }, - }, - "services": Object { - "properties": Object { - "took": Object { - "properties": Object { - "ms": Object { - "type": "long", - }, - }, - }, - }, - }, - "versions": Object { - "properties": Object { - "took": Object { - "properties": Object { - "ms": Object { - "type": "long", - }, - }, - }, - }, - }, - }, - }, - "version": Object { - "properties": Object { - "apm_server": Object { - "properties": Object { - "major": Object { - "type": "long", - }, - "minor": Object { - "type": "long", - }, - "patch": Object { - "type": "long", - }, - }, - }, - }, - }, - }, +{ + "properties": { + "stack_stats": { + "properties": { + "kibana": { + "properties": { + "plugins": { + "properties": { + "apm": { + "properties": { + "agents": { + "properties": { + "dotnet": { + "properties": { + "agent": { + "properties": { + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "service": { + "properties": { + "framework": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "language": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "runtime": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + } + } + } + } + }, + "go": { + "properties": { + "agent": { + "properties": { + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "service": { + "properties": { + "framework": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "language": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "runtime": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + } + } + } + } + }, + "java": { + "properties": { + "agent": { + "properties": { + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "service": { + "properties": { + "framework": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "language": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "runtime": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + } + } + } + } + }, + "js-base": { + "properties": { + "agent": { + "properties": { + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "service": { + "properties": { + "framework": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "language": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "runtime": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + } + } + } + } + }, + "nodejs": { + "properties": { + "agent": { + "properties": { + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "service": { + "properties": { + "framework": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "language": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "runtime": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + } + } + } + } + }, + "python": { + "properties": { + "agent": { + "properties": { + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "service": { + "properties": { + "framework": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "language": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "runtime": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + } + } + } + } + }, + "ruby": { + "properties": { + "agent": { + "properties": { + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "service": { + "properties": { + "framework": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "language": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "runtime": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + } + } + } + } + }, + "rum-js": { + "properties": { + "agent": { + "properties": { + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "service": { + "properties": { + "framework": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "language": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "runtime": { + "properties": { + "composite": { + "type": "keyword", + "ignore_above": 1024 + }, + "name": { + "type": "keyword", + "ignore_above": 1024 + }, + "version": { + "type": "keyword", + "ignore_above": 1024 + } + } + } + } + } + } + } + } + }, + "cloud": { + "properties": { + "availability_zone": { + "type": "keyword", + "ignore_above": 1024 + }, + "provider": { + "type": "keyword", + "ignore_above": 1024 + }, + "region": { + "type": "keyword", + "ignore_above": 1024 + } + } + }, + "counts": { + "properties": { + "agent_configuration": { + "properties": { + "all": { + "type": "long" + } + } + }, + "error": { + "properties": { + "1d": { + "type": "long" + }, + "all": { + "type": "long" + } + } + }, + "max_error_groups_per_service": { + "properties": { + "1d": { + "type": "long" + } + } + }, + "max_transaction_groups_per_service": { + "properties": { + "1d": { + "type": "long" + } + } + }, + "metric": { + "properties": { + "1d": { + "type": "long" + }, + "all": { + "type": "long" + } + } + }, + "onboarding": { + "properties": { + "1d": { + "type": "long" + }, + "all": { + "type": "long" + } + } + }, + "services": { + "properties": { + "1d": { + "type": "long" + } + } + }, + "sourcemap": { + "properties": { + "1d": { + "type": "long" + }, + "all": { + "type": "long" + } + } + }, + "span": { + "properties": { + "1d": { + "type": "long" + }, + "all": { + "type": "long" + } + } + }, + "traces": { + "properties": { + "1d": { + "type": "long" + } + } + }, + "transaction": { + "properties": { + "1d": { + "type": "long" + }, + "all": { + "type": "long" + } + } + } + } + }, + "cardinality": { + "properties": { + "client": { + "properties": { + "geo": { + "properites": { + "country_iso_code": { + "rum": { + "properties": { + "1d": { + "type": "long" + } + } + } + } + } + } + } + }, + "user_agent": { + "properties": { + "original": { + "properties": { + "all_agents": { + "properties": { + "1d": { + "type": "long" + } + } + }, + "rum": { + "properties": { + "1d": { + "type": "long" + } + } + } + } + } + } + }, + "transaction": { + "properties": { + "name": { + "properties": { + "all_agents": { + "properties": { + "1d": { + "type": "long" + } + } + }, + "rum": { + "properties": { + "1d": { + "type": "long" + } + } + } + } + } + } + } + } + }, + "has_any_services": { + "type": "boolean" + }, + "indices": { + "properties": { + "all": { + "properties": { + "total": { + "properties": { + "docs": { + "properties": { + "count": { + "type": "long" + } + } + }, + "store": { + "properties": { + "size_in_bytes": { + "type": "long" + } + } + } + } + } + } + }, + "shards": { + "properties": { + "total": { + "type": "long" + } + } + } + } + }, + "integrations": { + "properties": { + "ml": { + "properties": { + "all_jobs_count": { + "type": "long" + } + } + } + } + }, + "retainment": { + "properties": { + "error": { + "properties": { + "ms": { + "type": "long" + } + } + }, + "metric": { + "properties": { + "ms": { + "type": "long" + } + } + }, + "onboarding": { + "properties": { + "ms": { + "type": "long" + } + } + }, + "span": { + "properties": { + "ms": { + "type": "long" + } + } + }, + "transaction": { + "properties": { + "ms": { + "type": "long" + } + } + } + } + }, + "services_per_agent": { + "properties": { + "dotnet": { + "type": "long", + "null_value": 0 + }, + "go": { + "type": "long", + "null_value": 0 + }, + "java": { + "type": "long", + "null_value": 0 + }, + "js-base": { + "type": "long", + "null_value": 0 + }, + "nodejs": { + "type": "long", + "null_value": 0 + }, + "python": { + "type": "long", + "null_value": 0 + }, + "ruby": { + "type": "long", + "null_value": 0 + }, + "rum-js": { + "type": "long", + "null_value": 0 + } + } + }, + "tasks": { + "properties": { + "agent_configuration": { + "properties": { + "took": { + "properties": { + "ms": { + "type": "long" + } + } + } + } + }, + "agents": { + "properties": { + "took": { + "properties": { + "ms": { + "type": "long" + } + } + } + } + }, + "cardinality": { + "properties": { + "took": { + "properties": { + "ms": { + "type": "long" + } + } + } + } + }, + "cloud": { + "properties": { + "took": { + "properties": { + "ms": { + "type": "long" + } + } + } + } + }, + "groupings": { + "properties": { + "took": { + "properties": { + "ms": { + "type": "long" + } + } + } + } + }, + "indices_stats": { + "properties": { + "took": { + "properties": { + "ms": { + "type": "long" + } + } + } + } + }, + "integrations": { + "properties": { + "took": { + "properties": { + "ms": { + "type": "long" + } + } + } + } + }, + "processor_events": { + "properties": { + "took": { + "properties": { + "ms": { + "type": "long" + } + } + } + } + }, + "services": { + "properties": { + "took": { + "properties": { + "ms": { + "type": "long" + } + } + } + } + }, + "versions": { + "properties": { + "took": { + "properties": { + "ms": { + "type": "long" + } + } + } + } + } + } + }, + "version": { + "properties": { + "apm_server": { + "properties": { + "major": { + "type": "long" + }, + "minor": { + "type": "long" + }, + "patch": { + "type": "long" + } + } + } + } + } + } + } + } + } + } + } + } + } + } } `; diff --git a/x-pack/plugins/apm/common/apm_telemetry.test.ts b/x-pack/plugins/apm/common/apm_telemetry.test.ts index 1612716142ce7..035c546a5b49a 100644 --- a/x-pack/plugins/apm/common/apm_telemetry.test.ts +++ b/x-pack/plugins/apm/common/apm_telemetry.test.ts @@ -4,48 +4,43 @@ * you may not use this file except in compliance with the Elastic License. */ -import { - getApmTelemetryMapping, - mergeApmTelemetryMapping, -} from './apm_telemetry'; +import { getApmTelemetryMapping } from './apm_telemetry'; + +// Add this snapshot serializer for this test. The default snapshot serializer +// prints "Object" next to objects in the JSON output, but we want to be able to +// Use the output from this JSON snapshot to share with the telemetry team. When +// new fields are added to the mapping, we'll have a diff in the snapshot. +expect.addSnapshotSerializer({ + print: (contents) => { + return JSON.stringify(contents, null, 2); + }, + test: () => true, +}); describe('APM telemetry helpers', () => { describe('getApmTelemetry', () => { + // This test creates a snapshot with the JSON of our full telemetry mapping + // that can be PUT in a query to the index on the telemetry cluster. Sharing + // the contents of the snapshot with the telemetry team can provide them with + // useful information about changes to our telmetry. it('generates a JSON object with the telemetry mapping', () => { - expect(getApmTelemetryMapping()).toMatchSnapshot(); - }); - }); - - describe('mergeApmTelemetryMapping', () => { - describe('with an invalid mapping', () => { - it('throws an error', () => { - expect(() => mergeApmTelemetryMapping({})).toThrowError(); - }); - }); - - describe('with a valid mapping', () => { - it('merges the mapping', () => { - // This is "valid" in the sense that it has all of the deep fields - // needed to merge. It's not a valid mapping opbject. - const validTelemetryMapping = { - mappings: { + expect({ + properties: { + stack_stats: { properties: { - stack_stats: { + kibana: { properties: { - kibana: { - properties: { plugins: { properties: { apm: {} } } }, + plugins: { + properties: { + apm: getApmTelemetryMapping(), + }, }, }, }, }, }, - }; - - expect( - mergeApmTelemetryMapping(validTelemetryMapping)?.mappings.properties - .stack_stats.properties.kibana.properties.plugins.properties.apm - ).toEqual(getApmTelemetryMapping()); - }); + }, + }).toMatchSnapshot(); }); }); }); diff --git a/x-pack/plugins/apm/common/apm_telemetry.ts b/x-pack/plugins/apm/common/apm_telemetry.ts index 5837648f3e505..5fb6414674d1c 100644 --- a/x-pack/plugins/apm/common/apm_telemetry.ts +++ b/x-pack/plugins/apm/common/apm_telemetry.ts @@ -3,7 +3,6 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { produce } from 'immer'; import { AGENT_NAMES } from './agent_name'; /** @@ -115,6 +114,15 @@ export function getApmTelemetryMapping() { }, cardinality: { properties: { + client: { + properties: { + geo: { + properites: { + country_iso_code: { rum: oneDayProperties }, + }, + }, + }, + }, user_agent: { properties: { original: { @@ -199,6 +207,7 @@ export function getApmTelemetryMapping() { agent_configuration: tookProperties, agents: tookProperties, cardinality: tookProperties, + cloud: tookProperties, groupings: tookProperties, indices_stats: tookProperties, integrations: tookProperties, @@ -221,16 +230,3 @@ export function getApmTelemetryMapping() { }, }; } - -/** - * Merge a telemetry mapping object (from https://github.com/elastic/telemetry/blob/master/config/templates/xpack-phone-home.json) - * with the output from `getApmTelemetryMapping`. - */ -export function mergeApmTelemetryMapping( - xpackPhoneHomeMapping: Record -) { - return produce(xpackPhoneHomeMapping, (draft: Record) => { - draft.mappings.properties.stack_stats.properties.kibana.properties.plugins.properties.apm = getApmTelemetryMapping(); - return draft; - }); -} diff --git a/x-pack/plugins/apm/dev_docs/telemetry.md b/x-pack/plugins/apm/dev_docs/telemetry.md index fa8e057a59595..d61afbe07522f 100644 --- a/x-pack/plugins/apm/dev_docs/telemetry.md +++ b/x-pack/plugins/apm/dev_docs/telemetry.md @@ -55,20 +55,16 @@ The mapping for the telemetry data is here under `stack_stats.kibana.plugins.apm The mapping used there can be generated with the output of the [`getTelemetryMapping`](../common/apm_telemetry.ts) function. -To make a change to the mapping, edit this function, run the tests to update the snapshots, then use the `merge_telemetry_mapping` script to merge the data into the telemetry repository. +The `schema` property of the `makeUsageCollector` call in the [`createApmTelemetry` function](../server/lib/apm_telemetry/index.ts) contains the output of `getTelemetryMapping`. -If the [telemetry repository](https://github.com/elastic/telemetry) is cloned as a sibling to the kibana directory, you can run the following from x-pack/plugins/apm: - -```bash -node ./scripts/merge-telemetry-mapping.js ../../../../telemetry/config/templates/xpack-phone-home.json -``` - -this will replace the contents of the mapping in the repository checkout with the updated mapping. You can then [follow the telemetry team's instructions](https://github.com/elastic/telemetry#mappings) for opening a pull request with the mapping changes. +When adding a task, the key of the task and the `took` properties need to be added under the `tasks` properties in the mapping, as when tasks run they report the time they took. The queries for the stats are in the [collect data telemetry tasks](../server/lib/apm_telemetry/collect_data_telemetry/tasks.ts). The collection tasks also use the [`APMDataTelemetry` type](../server/lib/apm_telemetry/types.ts) which also needs to be updated with any changes to the fields. +Running `node scripts/telemetry_check --fix` from the root Kibana directory will update the schemas which schema should automatically notify the Telemetry team when a pull request is opened so they can update the mapping in the telemetry clusters. (At the time of this writing the APM schema is excluded. #70180 is open to remove these exclusions so at this time any pull requests with mapping changes will have to manually request the Telemetry team as a reviewer.) + ## Behavioral Telemetry Behavioral telemetry is recorded with the ui_metrics and application_usage methods from the Usage Collection plugin. diff --git a/x-pack/plugins/apm/e2e/cypress/integration/rum_dashboard.feature b/x-pack/plugins/apm/e2e/cypress/integration/rum_dashboard.feature index c98e3f81b2bc6..be1597c8340eb 100644 --- a/x-pack/plugins/apm/e2e/cypress/integration/rum_dashboard.feature +++ b/x-pack/plugins/apm/e2e/cypress/integration/rum_dashboard.feature @@ -1,10 +1,8 @@ Feature: RUM Dashboard Scenario: Client metrics - Given a user browses the APM UI application for RUM Data - When the user inspects the real user monitoring tab - Then should redirect to rum dashboard - And should have correct client metrics + When a user browses the APM UI application for RUM Data + Then should have correct client metrics Scenario Outline: Rum page filters When the user filters by "" @@ -15,22 +13,16 @@ Feature: RUM Dashboard | location | Scenario: Page load distribution percentiles - Given a user browses the APM UI application for RUM Data - When the user inspects the real user monitoring tab - Then should redirect to rum dashboard - And should display percentile for page load chart + When a user browses the APM UI application for RUM Data + Then should display percentile for page load chart Scenario: Page load distribution chart tooltip - Given a user browses the APM UI application for RUM Data - When the user inspects the real user monitoring tab - Then should redirect to rum dashboard - And should display tooltip on hover + When a user browses the APM UI application for RUM Data + Then should display tooltip on hover Scenario: Page load distribution chart legends - Given a user browses the APM UI application for RUM Data - When the user inspects the real user monitoring tab - Then should redirect to rum dashboard - And should display chart legend + When a user browses the APM UI application for RUM Data + Then should display chart legend Scenario: Breakdown filter Given a user click page load breakdown filter diff --git a/x-pack/plugins/apm/e2e/cypress/integration/snapshots.js b/x-pack/plugins/apm/e2e/cypress/integration/snapshots.js index 7fbce2583903c..6ee204781c8a7 100644 --- a/x-pack/plugins/apm/e2e/cypress/integration/snapshots.js +++ b/x-pack/plugins/apm/e2e/cypress/integration/snapshots.js @@ -1,11 +1,6 @@ module.exports = { "__version": "4.9.0", "RUM Dashboard": { - "Client metrics": { - "1": "55 ", - "2": "0.08 sec", - "3": "0.01 sec" - }, "Rum page filters (example #1)": { "1": "8 ", "2": "0.08 sec", @@ -16,19 +11,8 @@ module.exports = { "2": "0.07 sec", "3": "0.01 sec" }, - "Page load distribution percentiles": { - "1": "50th", - "2": "75th", - "3": "90th", - "4": "95th" - }, "Page load distribution chart legends": { "1": "Overall" - }, - "Service name filter": { - "1": "7 ", - "2": "0.07 sec", - "3": "0.01 sec" } } } diff --git a/x-pack/plugins/apm/e2e/cypress/support/step_definitions/rum/page_load_dist.ts b/x-pack/plugins/apm/e2e/cypress/support/step_definitions/rum/page_load_dist.ts index 89dc3437c3e69..f319f7ef98667 100644 --- a/x-pack/plugins/apm/e2e/cypress/support/step_definitions/rum/page_load_dist.ts +++ b/x-pack/plugins/apm/e2e/cypress/support/step_definitions/rum/page_load_dist.ts @@ -27,7 +27,9 @@ When(`the user selected the breakdown`, () => { Then(`breakdown series should appear in chart`, () => { cy.get('.euiLoadingChart').should('not.be.visible'); - cy.get('div.echLegendItem__label[title=Chrome] ') + cy.get('div.echLegendItem__label[title=Chrome] ', { + timeout: DEFAULT_TIMEOUT, + }) .invoke('text') .should('eq', 'Chrome'); }); diff --git a/x-pack/plugins/apm/e2e/cypress/support/step_definitions/rum/rum_dashboard.ts b/x-pack/plugins/apm/e2e/cypress/support/step_definitions/rum/rum_dashboard.ts index 24961ceb3b3c2..ac7aaf33b7849 100644 --- a/x-pack/plugins/apm/e2e/cypress/support/step_definitions/rum/rum_dashboard.ts +++ b/x-pack/plugins/apm/e2e/cypress/support/step_definitions/rum/rum_dashboard.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Given, When, Then } from 'cypress-cucumber-preprocessor/steps'; +import { Given, Then } from 'cypress-cucumber-preprocessor/steps'; import { loginAndWaitForPage } from '../../../integration/helpers'; /** The default time in ms to wait for a Cypress command to complete */ @@ -14,18 +14,10 @@ Given(`a user browses the APM UI application for RUM Data`, () => { // open service overview page const RANGE_FROM = 'now-24h'; const RANGE_TO = 'now'; - loginAndWaitForPage(`/app/apm#/services`, { from: RANGE_FROM, to: RANGE_TO }); -}); - -When(`the user inspects the real user monitoring tab`, () => { - // click rum tab - cy.get(':contains(Real User Monitoring)', { timeout: DEFAULT_TIMEOUT }) - .last() - .click({ force: true }); -}); - -Then(`should redirect to rum dashboard`, () => { - cy.url().should('contain', `/app/apm#/rum-overview`); + loginAndWaitForPage(`/app/apm#/rum-preview`, { + from: RANGE_FROM, + to: RANGE_TO, + }); }); Then(`should have correct client metrics`, () => { @@ -33,31 +25,33 @@ Then(`should have correct client metrics`, () => { // wait for all loading to finish cy.get('kbnLoadingIndicator').should('not.be.visible'); + cy.get('.euiStat__title', { timeout: DEFAULT_TIMEOUT }).should('be.visible'); + cy.get('.euiSelect-isLoading').should('not.be.visible'); cy.get('.euiStat__title-isLoading').should('not.be.visible'); - cy.get(clientMetrics).eq(2).invoke('text').snapshot(); + cy.get(clientMetrics).eq(2).should('have.text', '55 '); - cy.get(clientMetrics).eq(1).invoke('text').snapshot(); + cy.get(clientMetrics).eq(1).should('have.text', '0.08 sec'); - cy.get(clientMetrics).eq(0).invoke('text').snapshot(); + cy.get(clientMetrics).eq(0).should('have.text', '0.01 sec'); }); Then(`should display percentile for page load chart`, () => { const pMarkers = '[data-cy=percentile-markers] span'; - cy.get('.euiLoadingChart').should('be.visible'); + cy.get('.euiLoadingChart', { timeout: DEFAULT_TIMEOUT }).should('be.visible'); // wait for all loading to finish cy.get('kbnLoadingIndicator').should('not.be.visible'); cy.get('.euiStat__title-isLoading').should('not.be.visible'); - cy.get(pMarkers).eq(0).invoke('text').snapshot(); + cy.get(pMarkers).eq(0).should('have.text', '50th'); - cy.get(pMarkers).eq(1).invoke('text').snapshot(); + cy.get(pMarkers).eq(1).should('have.text', '75th'); - cy.get(pMarkers).eq(2).invoke('text').snapshot(); + cy.get(pMarkers).eq(2).should('have.text', '90th'); - cy.get(pMarkers).eq(3).invoke('text').snapshot(); + cy.get(pMarkers).eq(3).should('have.text', '95th'); }); Then(`should display chart legend`, () => { diff --git a/x-pack/plugins/apm/e2e/cypress/support/step_definitions/rum/service_name_filter.ts b/x-pack/plugins/apm/e2e/cypress/support/step_definitions/rum/service_name_filter.ts index 9a3d7b52674b7..b0694c902085a 100644 --- a/x-pack/plugins/apm/e2e/cypress/support/step_definitions/rum/service_name_filter.ts +++ b/x-pack/plugins/apm/e2e/cypress/support/step_definitions/rum/service_name_filter.ts @@ -22,9 +22,9 @@ Then(`it displays relevant client metrics`, () => { cy.get('kbnLoadingIndicator').should('not.be.visible'); cy.get('.euiStat__title-isLoading').should('not.be.visible'); - cy.get(clientMetrics).eq(2).invoke('text').snapshot(); + cy.get(clientMetrics).eq(2).should('have.text', '7 '); - cy.get(clientMetrics).eq(1).invoke('text').snapshot(); + cy.get(clientMetrics).eq(1).should('have.text', '0.07 sec'); - cy.get(clientMetrics).eq(0).invoke('text').snapshot(); + cy.get(clientMetrics).eq(0).should('have.text', '0.01 sec'); }); diff --git a/x-pack/plugins/apm/public/components/app/ErrorGroupOverview/List/__test__/__snapshots__/List.test.tsx.snap b/x-pack/plugins/apm/public/components/app/ErrorGroupOverview/List/__test__/__snapshots__/List.test.tsx.snap index a86f7fdf41f4f..0589fce727115 100644 --- a/x-pack/plugins/apm/public/components/app/ErrorGroupOverview/List/__test__/__snapshots__/List.test.tsx.snap +++ b/x-pack/plugins/apm/public/components/app/ErrorGroupOverview/List/__test__/__snapshots__/List.test.tsx.snap @@ -786,11 +786,11 @@ exports[`ErrorGroupOverview -> List should render with data 1`] = ` > a0ce2 @@ -831,11 +831,11 @@ exports[`ErrorGroupOverview -> List should render with data 1`] = ` > @@ -878,13 +878,13 @@ exports[`ErrorGroupOverview -> List should render with data 1`] = ` > List should render with data 1`] = ` > f3ac9 @@ -1065,11 +1065,11 @@ exports[`ErrorGroupOverview -> List should render with data 1`] = ` > @@ -1112,13 +1112,13 @@ exports[`ErrorGroupOverview -> List should render with data 1`] = ` > List should render with data 1`] = ` > e9086 @@ -1299,11 +1299,11 @@ exports[`ErrorGroupOverview -> List should render with data 1`] = ` > @@ -1346,13 +1346,13 @@ exports[`ErrorGroupOverview -> List should render with data 1`] = ` > List should render with data 1`] = ` > 8673d @@ -1533,11 +1533,11 @@ exports[`ErrorGroupOverview -> List should render with data 1`] = ` > @@ -1580,13 +1580,13 @@ exports[`ErrorGroupOverview -> List should render with data 1`] = ` > - {i18n.translate('xpack.apm.home.rumTabLabel', { - defaultMessage: 'Real User Monitoring', - })} - - ), - render: () => , - name: 'rum-overview', - }); - return homeTabs; } @@ -93,7 +79,7 @@ const SETTINGS_LINK_LABEL = i18n.translate('xpack.apm.settingsLinkLabel', { }); interface Props { - tab: 'traces' | 'services' | 'service-map' | 'rum-overview'; + tab: 'traces' | 'services' | 'service-map'; } export function Home({ tab }: Props) { diff --git a/x-pack/plugins/apm/public/components/app/Main/UpdateBreadcrumbs.test.tsx b/x-pack/plugins/apm/public/components/app/Main/UpdateBreadcrumbs.test.tsx index 15be023c32e90..6aec6e9bf181a 100644 --- a/x-pack/plugins/apm/public/components/app/Main/UpdateBreadcrumbs.test.tsx +++ b/x-pack/plugins/apm/public/components/app/Main/UpdateBreadcrumbs.test.tsx @@ -17,7 +17,7 @@ import { const setBreadcrumbs = jest.fn(); -function expectBreadcrumbToMatchSnapshot(route: string, params = '') { +function mountBreadcrumb(route: string, params = '') { mount( ); expect(setBreadcrumbs).toHaveBeenCalledTimes(1); - expect(setBreadcrumbs.mock.calls[0][0]).toMatchSnapshot(); } describe('UpdateBreadcrumbs', () => { @@ -58,36 +57,88 @@ describe('UpdateBreadcrumbs', () => { }); it('Homepage', () => { - expectBreadcrumbToMatchSnapshot('/'); + mountBreadcrumb('/'); expect(window.document.title).toMatchInlineSnapshot(`"APM"`); }); it('/services/:serviceName/errors/:groupId', () => { - expectBreadcrumbToMatchSnapshot('/services/opbeans-node/errors/myGroupId'); + mountBreadcrumb( + '/services/opbeans-node/errors/myGroupId', + 'rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0' + ); + const breadcrumbs = setBreadcrumbs.mock.calls[0][0]; + expect(breadcrumbs).toEqual([ + { + text: 'APM', + href: + '#/?kuery=myKuery&rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0', + }, + { + text: 'Services', + href: + '#/services?kuery=myKuery&rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0', + }, + { + text: 'opbeans-node', + href: + '#/services/opbeans-node?kuery=myKuery&rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0', + }, + { + text: 'Errors', + href: + '#/services/opbeans-node/errors?kuery=myKuery&rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0', + }, + { text: 'myGroupId', href: undefined }, + ]); expect(window.document.title).toMatchInlineSnapshot( `"myGroupId | Errors | opbeans-node | Services | APM"` ); }); it('/services/:serviceName/errors', () => { - expectBreadcrumbToMatchSnapshot('/services/opbeans-node/errors'); + mountBreadcrumb('/services/opbeans-node/errors'); + const breadcrumbs = setBreadcrumbs.mock.calls[0][0]; + expect(breadcrumbs).toEqual([ + { text: 'APM', href: '#/?kuery=myKuery' }, + { text: 'Services', href: '#/services?kuery=myKuery' }, + { text: 'opbeans-node', href: '#/services/opbeans-node?kuery=myKuery' }, + { text: 'Errors', href: undefined }, + ]); expect(window.document.title).toMatchInlineSnapshot( `"Errors | opbeans-node | Services | APM"` ); }); it('/services/:serviceName/transactions', () => { - expectBreadcrumbToMatchSnapshot('/services/opbeans-node/transactions'); + mountBreadcrumb('/services/opbeans-node/transactions'); + const breadcrumbs = setBreadcrumbs.mock.calls[0][0]; + expect(breadcrumbs).toEqual([ + { text: 'APM', href: '#/?kuery=myKuery' }, + { text: 'Services', href: '#/services?kuery=myKuery' }, + { text: 'opbeans-node', href: '#/services/opbeans-node?kuery=myKuery' }, + { text: 'Transactions', href: undefined }, + ]); expect(window.document.title).toMatchInlineSnapshot( `"Transactions | opbeans-node | Services | APM"` ); }); it('/services/:serviceName/transactions/view?transactionName=my-transaction-name', () => { - expectBreadcrumbToMatchSnapshot( + mountBreadcrumb( '/services/opbeans-node/transactions/view', 'transactionName=my-transaction-name' ); + const breadcrumbs = setBreadcrumbs.mock.calls[0][0]; + expect(breadcrumbs).toEqual([ + { text: 'APM', href: '#/?kuery=myKuery' }, + { text: 'Services', href: '#/services?kuery=myKuery' }, + { text: 'opbeans-node', href: '#/services/opbeans-node?kuery=myKuery' }, + { + text: 'Transactions', + href: '#/services/opbeans-node/transactions?kuery=myKuery', + }, + { text: 'my-transaction-name', href: undefined }, + ]); expect(window.document.title).toMatchInlineSnapshot( `"my-transaction-name | Transactions | opbeans-node | Services | APM"` ); diff --git a/x-pack/plugins/apm/public/components/app/Main/__snapshots__/UpdateBreadcrumbs.test.tsx.snap b/x-pack/plugins/apm/public/components/app/Main/__snapshots__/UpdateBreadcrumbs.test.tsx.snap deleted file mode 100644 index e7f6cba59318a..0000000000000 --- a/x-pack/plugins/apm/public/components/app/Main/__snapshots__/UpdateBreadcrumbs.test.tsx.snap +++ /dev/null @@ -1,102 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`UpdateBreadcrumbs /services/:serviceName/errors 1`] = ` -Array [ - Object { - "href": "#/?rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0&kuery=myKuery", - "text": "APM", - }, - Object { - "href": "#/services?rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0&kuery=myKuery", - "text": "Services", - }, - Object { - "href": "#/services/opbeans-node?rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0&kuery=myKuery", - "text": "opbeans-node", - }, - Object { - "href": undefined, - "text": "Errors", - }, -] -`; - -exports[`UpdateBreadcrumbs /services/:serviceName/errors/:groupId 1`] = ` -Array [ - Object { - "href": "#/?rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0&kuery=myKuery", - "text": "APM", - }, - Object { - "href": "#/services?rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0&kuery=myKuery", - "text": "Services", - }, - Object { - "href": "#/services/opbeans-node?rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0&kuery=myKuery", - "text": "opbeans-node", - }, - Object { - "href": "#/services/opbeans-node/errors?rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0&kuery=myKuery", - "text": "Errors", - }, - Object { - "href": undefined, - "text": "myGroupId", - }, -] -`; - -exports[`UpdateBreadcrumbs /services/:serviceName/transactions 1`] = ` -Array [ - Object { - "href": "#/?rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0&kuery=myKuery", - "text": "APM", - }, - Object { - "href": "#/services?rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0&kuery=myKuery", - "text": "Services", - }, - Object { - "href": "#/services/opbeans-node?rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0&kuery=myKuery", - "text": "opbeans-node", - }, - Object { - "href": undefined, - "text": "Transactions", - }, -] -`; - -exports[`UpdateBreadcrumbs /services/:serviceName/transactions/view?transactionName=my-transaction-name 1`] = ` -Array [ - Object { - "href": "#/?rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0&kuery=myKuery", - "text": "APM", - }, - Object { - "href": "#/services?rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0&kuery=myKuery", - "text": "Services", - }, - Object { - "href": "#/services/opbeans-node?rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0&kuery=myKuery", - "text": "opbeans-node", - }, - Object { - "href": "#/services/opbeans-node/transactions?rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0&kuery=myKuery", - "text": "Transactions", - }, - Object { - "href": undefined, - "text": "my-transaction-name", - }, -] -`; - -exports[`UpdateBreadcrumbs Homepage 1`] = ` -Array [ - Object { - "href": undefined, - "text": "APM", - }, -] -`; diff --git a/x-pack/plugins/apm/public/components/app/Main/route_config/index.tsx b/x-pack/plugins/apm/public/components/app/Main/route_config/index.tsx index 8379def2a7d9a..057971b1ca3a4 100644 --- a/x-pack/plugins/apm/public/components/app/Main/route_config/index.tsx +++ b/x-pack/plugins/apm/public/components/app/Main/route_config/index.tsx @@ -28,6 +28,7 @@ import { EditAgentConfigurationRouteHandler, CreateAgentConfigurationRouteHandler, } from './route_handlers/agent_configuration'; +import { RumHome } from '../../RumDashboard/RumHome'; const metricsBreadcrumb = i18n.translate('xpack.apm.breadcrumb.metricsTitle', { defaultMessage: 'Metrics', @@ -253,17 +254,8 @@ export const routes: BreadcrumbRoute[] = [ }, { exact: true, - path: '/rum-overview', - component: () => , - breadcrumb: i18n.translate('xpack.apm.home.rumOverview.title', { - defaultMessage: 'Real User Monitoring', - }), - name: RouteName.RUM_OVERVIEW, - }, - { - exact: true, - path: '/services/:serviceName/rum-overview', - component: () => , + path: '/rum-preview', + component: () => , breadcrumb: i18n.translate('xpack.apm.home.rumOverview.title', { defaultMessage: 'Real User Monitoring', }), diff --git a/x-pack/plugins/apm/public/components/app/RumDashboard/Breakdowns/BreakdownGroup.tsx b/x-pack/plugins/apm/public/components/app/RumDashboard/Breakdowns/BreakdownGroup.tsx index 007cdab0d2078..5bf84b6c918c5 100644 --- a/x-pack/plugins/apm/public/components/app/RumDashboard/Breakdowns/BreakdownGroup.tsx +++ b/x-pack/plugins/apm/public/components/app/RumDashboard/Breakdowns/BreakdownGroup.tsx @@ -88,6 +88,7 @@ export const BreakdownGroup = ({ data-cy={`filter-breakdown-item_${name}`} key={name + count} onClick={onFilterItemClick(name)} + disabled={!selected && getSelItems().length > 0} > {name} diff --git a/x-pack/plugins/apm/public/components/app/RumDashboard/Charts/VisitorBreakdownChart.tsx b/x-pack/plugins/apm/public/components/app/RumDashboard/Charts/VisitorBreakdownChart.tsx new file mode 100644 index 0000000000000..1e28fde4aa2b4 --- /dev/null +++ b/x-pack/plugins/apm/public/components/app/RumDashboard/Charts/VisitorBreakdownChart.tsx @@ -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 React from 'react'; +import { + Chart, + DARK_THEME, + Datum, + LIGHT_THEME, + Partition, + PartitionLayout, + Settings, +} from '@elastic/charts'; +import euiLightVars from '@elastic/eui/dist/eui_theme_light.json'; +import { + EUI_CHARTS_THEME_DARK, + EUI_CHARTS_THEME_LIGHT, +} from '@elastic/eui/dist/eui_charts_theme'; +import { useUiSetting$ } from '../../../../../../../../src/plugins/kibana_react/public'; +import { ChartWrapper } from '../ChartWrapper'; + +interface Props { + options?: Array<{ + count: number; + name: string; + }>; +} + +export const VisitorBreakdownChart = ({ options }: Props) => { + const [darkMode] = useUiSetting$('theme:darkMode'); + + return ( + + + + d.count as number} + valueGetter="percent" + percentFormatter={(d: number) => + `${Math.round((d + Number.EPSILON) * 100) / 100}%` + } + layers={[ + { + groupByRollup: (d: Datum) => d.name, + nodeLabel: (d: Datum) => d, + // fillLabel: { textInvertible: true }, + shape: { + fillColor: (d) => { + const clrs = [ + euiLightVars.euiColorVis1_behindText, + euiLightVars.euiColorVis0_behindText, + euiLightVars.euiColorVis2_behindText, + euiLightVars.euiColorVis3_behindText, + euiLightVars.euiColorVis4_behindText, + euiLightVars.euiColorVis5_behindText, + euiLightVars.euiColorVis6_behindText, + euiLightVars.euiColorVis7_behindText, + euiLightVars.euiColorVis8_behindText, + euiLightVars.euiColorVis9_behindText, + ]; + return clrs[d.sortIndex]; + }, + }, + }, + ]} + config={{ + partitionLayout: PartitionLayout.sunburst, + linkLabel: { + maxCount: 32, + fontSize: 14, + }, + fontFamily: 'Arial', + margin: { top: 0, bottom: 0, left: 0, right: 0 }, + minFontSize: 1, + idealFontSizeJump: 1.1, + outerSizeRatio: 0.9, // - 0.5 * Math.random(), + emptySizeRatio: 0, + circlePadding: 4, + }} + /> + + + ); +}; diff --git a/x-pack/plugins/apm/public/components/app/RumDashboard/ClientMetrics/index.tsx b/x-pack/plugins/apm/public/components/app/RumDashboard/ClientMetrics/index.tsx index df72fa604e4b3..5fee2f4195f91 100644 --- a/x-pack/plugins/apm/public/components/app/RumDashboard/ClientMetrics/index.tsx +++ b/x-pack/plugins/apm/public/components/app/RumDashboard/ClientMetrics/index.tsx @@ -34,6 +34,7 @@ export function ClientMetrics() { }, }); } + return Promise.resolve(null); }, [start, end, serviceName, uiFilters] ); diff --git a/x-pack/plugins/apm/public/components/app/RumDashboard/PageLoadDistribution/index.tsx b/x-pack/plugins/apm/public/components/app/RumDashboard/PageLoadDistribution/index.tsx index 81503e16f7bcf..adeff2b31fd93 100644 --- a/x-pack/plugins/apm/public/components/app/RumDashboard/PageLoadDistribution/index.tsx +++ b/x-pack/plugins/apm/public/components/app/RumDashboard/PageLoadDistribution/index.tsx @@ -56,6 +56,7 @@ export const PageLoadDistribution = () => { }, }); } + return Promise.resolve(null); }, [ end, diff --git a/x-pack/plugins/apm/public/components/app/RumDashboard/PageViewsTrend/index.tsx b/x-pack/plugins/apm/public/components/app/RumDashboard/PageViewsTrend/index.tsx index 328b873ef8562..c6ef319f8a666 100644 --- a/x-pack/plugins/apm/public/components/app/RumDashboard/PageViewsTrend/index.tsx +++ b/x-pack/plugins/apm/public/components/app/RumDashboard/PageViewsTrend/index.tsx @@ -39,6 +39,7 @@ export const PageViewsTrend = () => { }, }); } + return Promise.resolve(undefined); }, [end, start, serviceName, uiFilters, breakdowns] ); diff --git a/x-pack/plugins/apm/public/components/app/RumDashboard/RumDashboard.tsx b/x-pack/plugins/apm/public/components/app/RumDashboard/RumDashboard.tsx index 326d4a00fd31f..2eb79257334d7 100644 --- a/x-pack/plugins/apm/public/components/app/RumDashboard/RumDashboard.tsx +++ b/x-pack/plugins/apm/public/components/app/RumDashboard/RumDashboard.tsx @@ -16,8 +16,9 @@ import { ClientMetrics } from './ClientMetrics'; import { PageViewsTrend } from './PageViewsTrend'; import { PageLoadDistribution } from './PageLoadDistribution'; import { I18LABELS } from './translations'; +import { VisitorBreakdown } from './VisitorBreakdown'; -export function RumDashboard() { +export const RumDashboard = () => { return ( @@ -42,7 +43,15 @@ export function RumDashboard() { + + + + + + + + ); -} +}; diff --git a/x-pack/plugins/apm/public/components/app/RumDashboard/RumHeader/index.tsx b/x-pack/plugins/apm/public/components/app/RumDashboard/RumHeader/index.tsx new file mode 100644 index 0000000000000..b1ff38fdd2d79 --- /dev/null +++ b/x-pack/plugins/apm/public/components/app/RumDashboard/RumHeader/index.tsx @@ -0,0 +1,20 @@ +/* + * 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 } from '@elastic/eui'; +import React from 'react'; +import { DatePicker } from '../../../shared/DatePicker'; + +export const RumHeader: React.FC = ({ children }) => ( + <> + + {children} + + + + + +); diff --git a/x-pack/plugins/apm/public/components/app/RumDashboard/RumHome.tsx b/x-pack/plugins/apm/public/components/app/RumDashboard/RumHome.tsx new file mode 100644 index 0000000000000..a1b07640b5c17 --- /dev/null +++ b/x-pack/plugins/apm/public/components/app/RumDashboard/RumHome.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 { EuiFlexGroup, EuiFlexItem, EuiTitle } from '@elastic/eui'; +import React from 'react'; +import { RumOverview } from '../RumDashboard'; +import { RumHeader } from './RumHeader'; + +export function RumHome() { + return ( +
+ + + + +

End User Experience

+
+
+
+
+ +
+ ); +} diff --git a/x-pack/plugins/apm/public/components/app/RumDashboard/VisitorBreakdown/index.tsx b/x-pack/plugins/apm/public/components/app/RumDashboard/VisitorBreakdown/index.tsx new file mode 100644 index 0000000000000..2e17e27587b63 --- /dev/null +++ b/x-pack/plugins/apm/public/components/app/RumDashboard/VisitorBreakdown/index.tsx @@ -0,0 +1,65 @@ +/* + * 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 { EuiFlexGroup, EuiFlexItem, EuiTitle } from '@elastic/eui'; +import { VisitorBreakdownChart } from '../Charts/VisitorBreakdownChart'; +import { VisitorBreakdownLabel } from '../translations'; +import { useFetcher } from '../../../../hooks/useFetcher'; +import { useUrlParams } from '../../../../hooks/useUrlParams'; + +export const VisitorBreakdown = () => { + const { urlParams, uiFilters } = useUrlParams(); + + const { start, end, serviceName } = urlParams; + + const { data } = useFetcher( + (callApmApi) => { + if (start && end && serviceName) { + return callApmApi({ + pathname: '/api/apm/rum-client/visitor-breakdown', + params: { + query: { + start, + end, + uiFilters: JSON.stringify(uiFilters), + }, + }, + }); + } + return Promise.resolve(null); + }, + [end, start, serviceName, uiFilters] + ); + + return ( + <> + +

{VisitorBreakdownLabel}

+
+ + + + +

Browser

+
+
+ + + +

Operating System

+
+
+ + + +

Device

+
+
+
+ + ); +}; diff --git a/x-pack/plugins/apm/public/components/app/RumDashboard/index.tsx b/x-pack/plugins/apm/public/components/app/RumDashboard/index.tsx index 3380a81c7bfab..9b88202b2e5ef 100644 --- a/x-pack/plugins/apm/public/components/app/RumDashboard/index.tsx +++ b/x-pack/plugins/apm/public/components/app/RumDashboard/index.tsx @@ -4,14 +4,13 @@ * you may not use this file except in compliance with the Elastic License. */ +import React, { useMemo } from 'react'; import { EuiFlexGroup, EuiFlexItem, EuiHorizontalRule, EuiSpacer, } from '@elastic/eui'; -import React, { useMemo } from 'react'; -import { useRouteMatch } from 'react-router-dom'; import { useTrackPageview } from '../../../../../observability/public'; import { LocalUIFilters } from '../../shared/LocalUIFilters'; import { PROJECTION } from '../../../../common/projections/typings'; @@ -20,6 +19,7 @@ import { ServiceNameFilter } from '../../shared/LocalUIFilters/ServiceNameFilter import { useUrlParams } from '../../../hooks/useUrlParams'; import { useFetcher } from '../../../hooks/useFetcher'; import { RUM_AGENTS } from '../../../../common/agent_name'; +import { EnvironmentFilter } from '../../shared/EnvironmentFilter'; export function RumOverview() { useTrackPageview({ app: 'apm', path: 'rum_overview' }); @@ -38,11 +38,7 @@ export function RumOverview() { urlParams: { start, end }, } = useUrlParams(); - const isRumServiceRoute = useRouteMatch( - '/services/:serviceName/rum-overview' - ); - - const { data } = useFetcher( + const { data, status } = useFetcher( (callApmApi) => { if (start && end) { return callApmApi({ @@ -65,14 +61,17 @@ export function RumOverview() { + + - {!isRumServiceRoute && ( - <> - - - {' '} - - )} + <> + + + {' '} + diff --git a/x-pack/plugins/apm/public/components/app/RumDashboard/translations.ts b/x-pack/plugins/apm/public/components/app/RumDashboard/translations.ts index 2784d9bfd8efa..96d1b529c52f9 100644 --- a/x-pack/plugins/apm/public/components/app/RumDashboard/translations.ts +++ b/x-pack/plugins/apm/public/components/app/RumDashboard/translations.ts @@ -50,3 +50,10 @@ export const I18LABELS = { defaultMessage: 'seconds', }), }; + +export const VisitorBreakdownLabel = i18n.translate( + 'xpack.apm.rum.visitorBreakdown', + { + defaultMessage: 'Visitor breakdown', + } +); diff --git a/x-pack/plugins/apm/public/components/app/ServiceDetails/ServiceDetailTabs.tsx b/x-pack/plugins/apm/public/components/app/ServiceDetails/ServiceDetailTabs.tsx index ce60ffa4ba4e3..2f35e329720de 100644 --- a/x-pack/plugins/apm/public/components/app/ServiceDetails/ServiceDetailTabs.tsx +++ b/x-pack/plugins/apm/public/components/app/ServiceDetails/ServiceDetailTabs.tsx @@ -22,17 +22,9 @@ import { ServiceMap } from '../ServiceMap'; import { ServiceMetrics } from '../ServiceMetrics'; import { ServiceNodeOverview } from '../ServiceNodeOverview'; import { TransactionOverview } from '../TransactionOverview'; -import { RumOverview } from '../RumDashboard'; -import { RumOverviewLink } from '../../shared/Links/apm/RumOverviewLink'; interface Props { - tab: - | 'transactions' - | 'errors' - | 'metrics' - | 'nodes' - | 'service-map' - | 'rum-overview'; + tab: 'transactions' | 'errors' | 'metrics' | 'nodes' | 'service-map'; } export function ServiceDetailTabs({ tab }: Props) { @@ -118,20 +110,6 @@ export function ServiceDetailTabs({ tab }: Props) { tabs.push(serviceMapTab); } - if (isRumAgentName(agentName)) { - tabs.push({ - link: ( - - {i18n.translate('xpack.apm.home.rumTabLabel', { - defaultMessage: 'Real User Monitoring', - })} - - ), - render: () => , - name: 'rum-overview', - }); - } - const selectedTab = tabs.find((serviceTab) => serviceTab.name === tab); return ( diff --git a/x-pack/plugins/apm/public/components/app/ServiceOverview/__test__/__snapshots__/ServiceOverview.test.tsx.snap b/x-pack/plugins/apm/public/components/app/ServiceOverview/__test__/__snapshots__/ServiceOverview.test.tsx.snap index 241ba8c244496..e46da26f7dcb0 100644 --- a/x-pack/plugins/apm/public/components/app/ServiceOverview/__test__/__snapshots__/ServiceOverview.test.tsx.snap +++ b/x-pack/plugins/apm/public/components/app/ServiceOverview/__test__/__snapshots__/ServiceOverview.test.tsx.snap @@ -157,7 +157,7 @@ NodeList [ >
My Go Service @@ -263,7 +263,7 @@ NodeList [ > My Python Service diff --git a/x-pack/plugins/apm/public/components/shared/DatePicker/__test__/DatePicker.test.tsx b/x-pack/plugins/apm/public/components/shared/DatePicker/__test__/DatePicker.test.tsx index 639277a79ac9a..215e97aebf646 100644 --- a/x-pack/plugins/apm/public/components/shared/DatePicker/__test__/DatePicker.test.tsx +++ b/x-pack/plugins/apm/public/components/shared/DatePicker/__test__/DatePicker.test.tsx @@ -17,6 +17,7 @@ import { mount } from 'enzyme'; import { EuiSuperDatePicker } from '@elastic/eui'; import { MemoryRouter } from 'react-router-dom'; import { wait } from '@testing-library/react'; +import { MockApmPluginContextWrapper } from '../../../../context/ApmPluginContext/MockApmPluginContext'; const mockHistoryPush = jest.spyOn(history, 'push'); const mockRefreshTimeRange = jest.fn(); @@ -35,13 +36,15 @@ const MockUrlParamsProvider: React.FC<{ function mountDatePicker(params?: IUrlParams) { return mount( - - - - - - - + + + + + + + + + ); } @@ -58,6 +61,41 @@ describe('DatePicker', () => { jest.clearAllMocks(); }); + it('should set default query params in the URL', () => { + mountDatePicker(); + expect(mockHistoryPush).toHaveBeenCalledTimes(1); + expect(mockHistoryPush).toHaveBeenCalledWith( + expect.objectContaining({ + search: + 'rangeFrom=now-15m&rangeTo=now&refreshPaused=false&refreshInterval=10000', + }) + ); + }); + + it('should add missing default value', () => { + mountDatePicker({ + rangeTo: 'now', + refreshInterval: 5000, + }); + expect(mockHistoryPush).toHaveBeenCalledTimes(1); + expect(mockHistoryPush).toHaveBeenCalledWith( + expect.objectContaining({ + search: + 'rangeFrom=now-15m&rangeTo=now&refreshInterval=5000&refreshPaused=false', + }) + ); + }); + + it('should not set default query params in the URL when values already defined', () => { + mountDatePicker({ + rangeFrom: 'now-1d', + rangeTo: 'now', + refreshPaused: false, + refreshInterval: 5000, + }); + expect(mockHistoryPush).toHaveBeenCalledTimes(0); + }); + it('should update the URL when the date range changes', () => { const datePicker = mountDatePicker(); datePicker.find(EuiSuperDatePicker).props().onTimeChange({ @@ -66,9 +104,11 @@ describe('DatePicker', () => { isInvalid: false, isQuickSelection: true, }); - expect(mockHistoryPush).toHaveBeenCalledWith( + expect(mockHistoryPush).toHaveBeenCalledTimes(2); + expect(mockHistoryPush).toHaveBeenLastCalledWith( expect.objectContaining({ - search: 'rangeFrom=updated-start&rangeTo=updated-end', + search: + 'rangeFrom=updated-start&rangeTo=updated-end&refreshInterval=5000&refreshPaused=false', }) ); }); diff --git a/x-pack/plugins/apm/public/components/shared/DatePicker/index.tsx b/x-pack/plugins/apm/public/components/shared/DatePicker/index.tsx index 4391e4a5b8952..5201d80de5a12 100644 --- a/x-pack/plugins/apm/public/components/shared/DatePicker/index.tsx +++ b/x-pack/plugins/apm/public/components/shared/DatePicker/index.tsx @@ -5,75 +5,61 @@ */ import { EuiSuperDatePicker } from '@elastic/eui'; -import { i18n } from '@kbn/i18n'; import React from 'react'; +import { isEmpty, isEqual, pickBy } from 'lodash'; import { fromQuery, toQuery } from '../Links/url_helpers'; import { history } from '../../../utils/history'; import { useLocation } from '../../../hooks/useLocation'; import { useUrlParams } from '../../../hooks/useUrlParams'; import { clearCache } from '../../../services/rest/callApi'; +import { useApmPluginContext } from '../../../hooks/useApmPluginContext'; +import { UI_SETTINGS } from '../../../../../../../src/plugins/data/common'; +import { + TimePickerQuickRange, + TimePickerTimeDefaults, + TimePickerRefreshInterval, +} from './typings'; + +function removeUndefinedAndEmptyProps(obj: T): Partial { + return pickBy(obj, (value) => value !== undefined && !isEmpty(String(value))); +} export function DatePicker() { const location = useLocation(); + const { core } = useApmPluginContext(); + + const timePickerQuickRanges = core.uiSettings.get( + UI_SETTINGS.TIMEPICKER_QUICK_RANGES + ); + + const timePickerTimeDefaults = core.uiSettings.get( + UI_SETTINGS.TIMEPICKER_TIME_DEFAULTS + ); + + const timePickerRefreshIntervalDefaults = core.uiSettings.get< + TimePickerRefreshInterval + >(UI_SETTINGS.TIMEPICKER_REFRESH_INTERVAL_DEFAULTS); + + const DEFAULT_VALUES = { + rangeFrom: timePickerTimeDefaults.from, + rangeTo: timePickerTimeDefaults.to, + refreshPaused: timePickerRefreshIntervalDefaults.pause, + /* + * Must be replaced by timePickerRefreshIntervalDefaults.value when this issue is fixed. + * https://github.com/elastic/kibana/issues/70562 + */ + refreshInterval: 10000, + }; + + const commonlyUsedRanges = timePickerQuickRanges.map( + ({ from, to, display }) => ({ + start: from, + end: to, + label: display, + }) + ); + const { urlParams, refreshTimeRange } = useUrlParams(); - const commonlyUsedRanges = [ - { - start: 'now-15m', - end: 'now', - label: i18n.translate('xpack.apm.datePicker.last15MinutesLabel', { - defaultMessage: 'Last 15 minutes', - }), - }, - { - start: 'now-30m', - end: 'now', - label: i18n.translate('xpack.apm.datePicker.last30MinutesLabel', { - defaultMessage: 'Last 30 minutes', - }), - }, - { - start: 'now-1h', - end: 'now', - label: i18n.translate('xpack.apm.datePicker.last1HourLabel', { - defaultMessage: 'Last 1 hour', - }), - }, - { - start: 'now-24h', - end: 'now', - label: i18n.translate('xpack.apm.datePicker.last24HoursLabel', { - defaultMessage: 'Last 24 hours', - }), - }, - { - start: 'now-7d', - end: 'now', - label: i18n.translate('xpack.apm.datePicker.last7DaysLabel', { - defaultMessage: 'Last 7 days', - }), - }, - { - start: 'now-30d', - end: 'now', - label: i18n.translate('xpack.apm.datePicker.last30DaysLabel', { - defaultMessage: 'Last 30 days', - }), - }, - { - start: 'now-90d', - end: 'now', - label: i18n.translate('xpack.apm.datePicker.last90DaysLabel', { - defaultMessage: 'Last 90 days', - }), - }, - { - start: 'now-1y', - end: 'now', - label: i18n.translate('xpack.apm.datePicker.last1YearLabel', { - defaultMessage: 'Last 1 year', - }), - }, - ]; function updateUrl(nextQuery: { rangeFrom?: string; @@ -105,6 +91,20 @@ export function DatePicker() { } const { rangeFrom, rangeTo, refreshPaused, refreshInterval } = urlParams; + const timePickerURLParams = removeUndefinedAndEmptyProps({ + rangeFrom, + rangeTo, + refreshPaused, + refreshInterval, + }); + + const nextParams = { + ...DEFAULT_VALUES, + ...timePickerURLParams, + }; + if (!isEqual(nextParams, timePickerURLParams)) { + updateUrl(nextParams); + } return ( { const href = await getRenderedHref( () => , { - search: '?rangeFrom=now/w&rangeTo=now', + search: + '?rangeFrom=now/w&rangeTo=now&refreshPaused=true&refreshInterval=0', } as Location ); @@ -45,7 +46,8 @@ describe('DiscoverLinks', () => { } as Span; const href = await getRenderedHref(() => , { - search: '?rangeFrom=now/w&rangeTo=now', + search: + '?rangeFrom=now/w&rangeTo=now&refreshPaused=true&refreshInterval=0', } as Location); expect(href).toEqual( @@ -65,7 +67,8 @@ describe('DiscoverLinks', () => { const href = await getRenderedHref( () => , { - search: '?rangeFrom=now/w&rangeTo=now', + search: + '?rangeFrom=now/w&rangeTo=now&refreshPaused=true&refreshInterval=0', } as Location ); @@ -87,7 +90,8 @@ describe('DiscoverLinks', () => { const href = await getRenderedHref( () => , { - search: '?rangeFrom=now/w&rangeTo=now', + search: + '?rangeFrom=now/w&rangeTo=now&refreshPaused=true&refreshInterval=0', } as Location ); diff --git a/x-pack/plugins/apm/public/components/shared/Links/MachineLearningLinks/MLJobLink.test.tsx b/x-pack/plugins/apm/public/components/shared/Links/MachineLearningLinks/MLJobLink.test.tsx index c832d3ded6175..39082c2639a2c 100644 --- a/x-pack/plugins/apm/public/components/shared/Links/MachineLearningLinks/MLJobLink.test.tsx +++ b/x-pack/plugins/apm/public/components/shared/Links/MachineLearningLinks/MLJobLink.test.tsx @@ -15,7 +15,10 @@ describe('MLJobLink', () => { () => ( ), - { search: '?rangeFrom=now/w&rangeTo=now-4h' } as Location + { + search: + '?rangeFrom=now/w&rangeTo=now-4h&refreshPaused=true&refreshInterval=0', + } as Location ); expect(href).toMatchInlineSnapshot( @@ -31,7 +34,10 @@ describe('MLJobLink', () => { transactionType="request" /> ), - { search: '?rangeFrom=now/w&rangeTo=now-4h' } as Location + { + search: + '?rangeFrom=now/w&rangeTo=now-4h&refreshPaused=true&refreshInterval=0', + } as Location ); expect(href).toMatchInlineSnapshot( diff --git a/x-pack/plugins/apm/public/components/shared/Links/MachineLearningLinks/MLLink.test.tsx b/x-pack/plugins/apm/public/components/shared/Links/MachineLearningLinks/MLLink.test.tsx index 840846adae019..b4187b2f797ab 100644 --- a/x-pack/plugins/apm/public/components/shared/Links/MachineLearningLinks/MLLink.test.tsx +++ b/x-pack/plugins/apm/public/components/shared/Links/MachineLearningLinks/MLLink.test.tsx @@ -15,7 +15,8 @@ test('MLLink produces the correct URL', async () => { ), { - search: '?rangeFrom=now-5h&rangeTo=now-2h', + search: + '?rangeFrom=now-5h&rangeTo=now-2h&refreshPaused=true&refreshInterval=0', } as Location ); diff --git a/x-pack/plugins/apm/public/components/shared/Links/apm/APMLink.test.tsx b/x-pack/plugins/apm/public/components/shared/Links/apm/APMLink.test.tsx index d6518e76aa5e9..1e849e8865d0d 100644 --- a/x-pack/plugins/apm/public/components/shared/Links/apm/APMLink.test.tsx +++ b/x-pack/plugins/apm/public/components/shared/Links/apm/APMLink.test.tsx @@ -13,7 +13,8 @@ test('APMLink should produce the correct URL', async () => { const href = await getRenderedHref( () => , { - search: '?rangeFrom=now-5h&rangeTo=now-2h', + search: + '?rangeFrom=now-5h&rangeTo=now-2h&refreshPaused=true&refreshInterval=0', } as Location ); @@ -26,12 +27,13 @@ test('APMLink should retain current kuery value if it exists', async () => { const href = await getRenderedHref( () => , { - search: '?kuery=host.hostname~20~3A~20~22fakehostname~22', + search: + '?kuery=host.hostname~20~3A~20~22fakehostname~22&rangeFrom=now-5h&rangeTo=now-2h&refreshPaused=true&refreshInterval=0', } as Location ); expect(href).toMatchInlineSnapshot( - `"#/some/path?rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0&kuery=host.hostname~20~3A~20~22fakehostname~22&transactionId=blah"` + `"#/some/path?kuery=host.hostname~20~3A~20~22fakehostname~22&rangeFrom=now-5h&rangeTo=now-2h&refreshPaused=true&refreshInterval=0&transactionId=blah"` ); }); @@ -44,11 +46,12 @@ test('APMLink should overwrite current kuery value if new kuery value is provide /> ), { - search: '?kuery=host.hostname~20~3A~20~22fakehostname~22', + search: + '?kuery=host.hostname~20~3A~20~22fakehostname~22&rangeFrom=now-5h&rangeTo=now-2h&refreshPaused=true&refreshInterval=0', } as Location ); expect(href).toMatchInlineSnapshot( - `"#/some/path?rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0&kuery=host.os~20~3A~20~22linux~22"` + `"#/some/path?kuery=host.os~20~3A~20~22linux~22&rangeFrom=now-5h&rangeTo=now-2h&refreshPaused=true&refreshInterval=0"` ); }); diff --git a/x-pack/plugins/apm/public/components/shared/Links/apm/APMLink.tsx b/x-pack/plugins/apm/public/components/shared/Links/apm/APMLink.tsx index 3aff241c6dee2..353f476e3f993 100644 --- a/x-pack/plugins/apm/public/components/shared/Links/apm/APMLink.tsx +++ b/x-pack/plugins/apm/public/components/shared/Links/apm/APMLink.tsx @@ -10,7 +10,6 @@ import url from 'url'; import { pick } from 'lodash'; import { useLocation } from '../../../../hooks/useLocation'; import { APMQueryParams, toQuery, fromQuery } from '../url_helpers'; -import { TIMEPICKER_DEFAULTS } from '../../../../context/UrlParamsContext/constants'; interface Props extends EuiLinkAnchorProps { path?: string; @@ -36,7 +35,6 @@ export function getAPMHref( ) { const currentQuery = toQuery(currentSearch); const nextQuery = { - ...TIMEPICKER_DEFAULTS, ...pick(currentQuery, PERSISTENT_APM_PARAMS), ...query, }; diff --git a/x-pack/plugins/apm/public/components/shared/Links/apm/RumOverviewLink.tsx b/x-pack/plugins/apm/public/components/shared/Links/apm/RumOverviewLink.tsx deleted file mode 100644 index 729ed9b10f827..0000000000000 --- a/x-pack/plugins/apm/public/components/shared/Links/apm/RumOverviewLink.tsx +++ /dev/null @@ -1,27 +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. - */ - -/* - * 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 { APMLink, APMLinkExtendProps } from './APMLink'; - -interface RumOverviewLinkProps extends APMLinkExtendProps { - serviceName?: string; -} -export function RumOverviewLink({ - serviceName, - ...rest -}: RumOverviewLinkProps) { - const path = serviceName - ? `/services/${serviceName}/rum-overview` - : '/rum-overview'; - - return ; -} diff --git a/x-pack/plugins/apm/public/components/shared/Links/rison_helpers.ts b/x-pack/plugins/apm/public/components/shared/Links/rison_helpers.ts index 434bd285029ab..8b4d891dba83b 100644 --- a/x-pack/plugins/apm/public/components/shared/Links/rison_helpers.ts +++ b/x-pack/plugins/apm/public/components/shared/Links/rison_helpers.ts @@ -5,7 +5,6 @@ */ import { Location } from 'history'; -import { TIMEPICKER_DEFAULTS } from '../../../context/UrlParamsContext/constants'; import { toQuery } from './url_helpers'; export interface TimepickerRisonData { @@ -21,18 +20,20 @@ export interface TimepickerRisonData { export function getTimepickerRisonData(currentSearch: Location['search']) { const currentQuery = toQuery(currentSearch); - const nextQuery = { - ...TIMEPICKER_DEFAULTS, - ...currentQuery, - }; return { time: { - from: encodeURIComponent(nextQuery.rangeFrom), - to: encodeURIComponent(nextQuery.rangeTo), + from: currentQuery.rangeFrom + ? encodeURIComponent(currentQuery.rangeFrom) + : '', + to: currentQuery.rangeTo ? encodeURIComponent(currentQuery.rangeTo) : '', }, refreshInterval: { - pause: String(nextQuery.refreshPaused), - value: String(nextQuery.refreshInterval), + pause: currentQuery.refreshPaused + ? String(currentQuery.refreshPaused) + : '', + value: currentQuery.refreshInterval + ? String(currentQuery.refreshInterval) + : '', }, }; } diff --git a/x-pack/plugins/apm/public/components/shared/LocalUIFilters/ServiceNameFilter/index.tsx b/x-pack/plugins/apm/public/components/shared/LocalUIFilters/ServiceNameFilter/index.tsx index 0bb62bd8efcff..405a4cacae714 100644 --- a/x-pack/plugins/apm/public/components/shared/LocalUIFilters/ServiceNameFilter/index.tsx +++ b/x-pack/plugins/apm/public/components/shared/LocalUIFilters/ServiceNameFilter/index.tsx @@ -18,9 +18,10 @@ import { fromQuery, toQuery } from '../../Links/url_helpers'; interface Props { serviceNames: string[]; + loading: boolean; } -const ServiceNameFilter = ({ serviceNames }: Props) => { +const ServiceNameFilter = ({ loading, serviceNames }: Props) => { const { urlParams: { serviceName }, } = useUrlParams(); @@ -60,6 +61,7 @@ const ServiceNameFilter = ({ serviceNames }: Props) => { { const date = '2020-02-06T11:00:00.000Z'; const timestamp = { us: new Date(date).getTime() }; + const urlParams = { + rangeFrom: 'now-24h', + rangeTo: 'now', + refreshPaused: true, + refreshInterval: 0, + }; + + const location = ({ + search: + '?rangeFrom=now-24h&rangeTo=now&refreshPaused=true&refreshInterval=0', + } as unknown) as Location; + it('shows required sections only', () => { const transaction = ({ timestamp, @@ -28,8 +40,8 @@ describe('Transaction action menu', () => { getSections({ transaction, basePath, - location: ({} as unknown) as Location, - urlParams: {}, + location, + urlParams, }) ).toEqual([ [ @@ -77,8 +89,8 @@ describe('Transaction action menu', () => { getSections({ transaction, basePath, - location: ({} as unknown) as Location, - urlParams: {}, + location, + urlParams, }) ).toEqual([ [ @@ -148,8 +160,8 @@ describe('Transaction action menu', () => { getSections({ transaction, basePath, - location: ({} as unknown) as Location, - urlParams: {}, + location, + urlParams, }) ).toEqual([ [ diff --git a/x-pack/plugins/apm/public/components/shared/charts/Timeline/Marker/ErrorMarker.test.tsx b/x-pack/plugins/apm/public/components/shared/charts/Timeline/Marker/ErrorMarker.test.tsx index 922796afd39bf..7a5d0dd5ce877 100644 --- a/x-pack/plugins/apm/public/components/shared/charts/Timeline/Marker/ErrorMarker.test.tsx +++ b/x-pack/plugins/apm/public/components/shared/charts/Timeline/Marker/ErrorMarker.test.tsx @@ -44,7 +44,9 @@ describe('ErrorMarker', () => { return component; } function getKueryDecoded(url: string) { - return decodeURIComponent(url.substring(url.indexOf('kuery='), url.length)); + return decodeURIComponent( + url.substring(url.indexOf('kuery='), url.indexOf('&')) + ); } it('renders link with trace and transaction', () => { const component = openPopover(mark); diff --git a/x-pack/plugins/apm/public/context/ApmPluginContext/MockApmPluginContext.tsx b/x-pack/plugins/apm/public/context/ApmPluginContext/MockApmPluginContext.tsx index 329368e0c80f1..8c38cdcda958d 100644 --- a/x-pack/plugins/apm/public/context/ApmPluginContext/MockApmPluginContext.tsx +++ b/x-pack/plugins/apm/public/context/ApmPluginContext/MockApmPluginContext.tsx @@ -7,6 +7,30 @@ import React from 'react'; import { ApmPluginContext, ApmPluginContextValue } from '.'; import { createCallApmApi } from '../../services/rest/createCallApmApi'; import { ConfigSchema } from '../..'; +import { UI_SETTINGS } from '../../../../../../src/plugins/data/common'; + +const uiSettings: Record = { + [UI_SETTINGS.TIMEPICKER_QUICK_RANGES]: [ + { + from: 'now/d', + to: 'now/d', + display: 'Today', + }, + { + from: 'now/w', + to: 'now/w', + display: 'This week', + }, + ], + [UI_SETTINGS.TIMEPICKER_TIME_DEFAULTS]: { + from: 'now-15m', + to: 'now', + }, + [UI_SETTINGS.TIMEPICKER_REFRESH_INTERVAL_DEFAULTS]: { + pause: false, + value: 100000, + }, +}; const mockCore = { chrome: { @@ -27,6 +51,9 @@ const mockCore = { addDanger: () => {}, }, }, + uiSettings: { + get: (key: string) => uiSettings[key], + }, }; const mockConfig: ConfigSchema = { diff --git a/x-pack/plugins/apm/public/context/UrlParamsContext/__tests__/UrlParamsContext.test.tsx b/x-pack/plugins/apm/public/context/UrlParamsContext/__tests__/UrlParamsContext.test.tsx index b88e0b8e23ea5..fbb79eae6a136 100644 --- a/x-pack/plugins/apm/public/context/UrlParamsContext/__tests__/UrlParamsContext.test.tsx +++ b/x-pack/plugins/apm/public/context/UrlParamsContext/__tests__/UrlParamsContext.test.tsx @@ -53,15 +53,9 @@ describe('UrlParamsContext', () => { const params = getDataFromOutput(wrapper); expect(params).toEqual({ - start: '2000-06-14T12:00:00.000Z', serviceName: 'opbeans-node', - end: '2000-06-15T12:00:00.000Z', page: 0, processorEvent: 'transaction', - rangeFrom: 'now-24h', - rangeTo: 'now', - refreshInterval: 0, - refreshPaused: true, }); }); diff --git a/x-pack/plugins/apm/public/context/UrlParamsContext/constants.ts b/x-pack/plugins/apm/public/context/UrlParamsContext/constants.ts index d654e60077be9..6297a560440d2 100644 --- a/x-pack/plugins/apm/public/context/UrlParamsContext/constants.ts +++ b/x-pack/plugins/apm/public/context/UrlParamsContext/constants.ts @@ -6,9 +6,3 @@ export const TIME_RANGE_REFRESH = 'TIME_RANGE_REFRESH'; export const LOCATION_UPDATE = 'LOCATION_UPDATE'; -export const TIMEPICKER_DEFAULTS = { - rangeFrom: 'now-24h', - rangeTo: 'now', - refreshPaused: 'true', - refreshInterval: '0', -}; diff --git a/x-pack/plugins/apm/public/context/UrlParamsContext/helpers.ts b/x-pack/plugins/apm/public/context/UrlParamsContext/helpers.ts index 9745c9ffdc705..d9781400f2272 100644 --- a/x-pack/plugins/apm/public/context/UrlParamsContext/helpers.ts +++ b/x-pack/plugins/apm/public/context/UrlParamsContext/helpers.ts @@ -104,7 +104,6 @@ export function getPathParams(pathname: string = ''): PathParams { serviceName, }; case 'service-map': - case 'rum-overview': return { serviceName, }; diff --git a/x-pack/plugins/apm/public/context/UrlParamsContext/resolveUrlParams.ts b/x-pack/plugins/apm/public/context/UrlParamsContext/resolveUrlParams.ts index bae7b9a796e19..2201e162904a2 100644 --- a/x-pack/plugins/apm/public/context/UrlParamsContext/resolveUrlParams.ts +++ b/x-pack/plugins/apm/public/context/UrlParamsContext/resolveUrlParams.ts @@ -16,7 +16,6 @@ import { toString, } from './helpers'; import { toQuery } from '../../components/shared/Links/url_helpers'; -import { TIMEPICKER_DEFAULTS } from './constants'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { localUIFilterNames } from '../../../server/lib/ui_filters/local_ui_filters/config'; import { pickKeys } from '../../../common/utils/pick_keys'; @@ -51,10 +50,10 @@ export function resolveUrlParams(location: Location, state: TimeUrlParams) { sortDirection, sortField, kuery, - refreshPaused = TIMEPICKER_DEFAULTS.refreshPaused, - refreshInterval = TIMEPICKER_DEFAULTS.refreshInterval, - rangeFrom = TIMEPICKER_DEFAULTS.rangeFrom, - rangeTo = TIMEPICKER_DEFAULTS.rangeTo, + refreshPaused, + refreshInterval, + rangeFrom, + rangeTo, environment, searchTerm, } = query; @@ -67,8 +66,8 @@ export function resolveUrlParams(location: Location, state: TimeUrlParams) { end: getEnd(state, rangeTo), rangeFrom, rangeTo, - refreshPaused: toBoolean(refreshPaused), - refreshInterval: toNumber(refreshInterval), + refreshPaused: refreshPaused ? toBoolean(refreshPaused) : undefined, + refreshInterval: refreshInterval ? toNumber(refreshInterval) : undefined, // query params sortDirection, diff --git a/x-pack/plugins/apm/scripts/merge-telemetry-mapping.js b/x-pack/plugins/apm/scripts/merge-telemetry-mapping.js deleted file mode 100644 index 741df981a9cb0..0000000000000 --- a/x-pack/plugins/apm/scripts/merge-telemetry-mapping.js +++ /dev/null @@ -1,21 +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. - */ - -// compile typescript on the fly -// eslint-disable-next-line import/no-extraneous-dependencies -require('@babel/register')({ - extensions: ['.ts'], - plugins: [ - '@babel/plugin-proposal-optional-chaining', - '@babel/plugin-proposal-nullish-coalescing-operator', - ], - presets: [ - '@babel/typescript', - ['@babel/preset-env', { targets: { node: 'current' } }], - ], -}); - -require('./merge-telemetry-mapping/index.ts'); diff --git a/x-pack/plugins/apm/scripts/merge-telemetry-mapping/index.ts b/x-pack/plugins/apm/scripts/merge-telemetry-mapping/index.ts deleted file mode 100644 index c06d4cec150dc..0000000000000 --- a/x-pack/plugins/apm/scripts/merge-telemetry-mapping/index.ts +++ /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 { readFileSync, truncateSync, writeFileSync } from 'fs'; -import { resolve } from 'path'; -import { argv } from 'yargs'; -import { mergeApmTelemetryMapping } from '../../common/apm_telemetry'; - -function errorExit(error?: Error) { - console.error(`usage: ${argv.$0} /path/to/xpack-phone-home.json`); // eslint-disable-line no-console - if (error) { - throw error; - } - process.exit(1); -} - -try { - const filename = resolve(argv._[0]); - const xpackPhoneHomeMapping = JSON.parse(readFileSync(filename, 'utf-8')); - - const newMapping = mergeApmTelemetryMapping(xpackPhoneHomeMapping); - - truncateSync(filename); - writeFileSync(filename, JSON.stringify(newMapping, null, 2)); -} catch (error) { - errorExit(error); -} diff --git a/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.test.ts b/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.test.ts index e3161b49b315d..ea2b57c01acff 100644 --- a/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.test.ts +++ b/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.test.ts @@ -16,7 +16,7 @@ describe('data telemetry collection tasks', () => { } as ApmIndicesConfig; describe('cloud', () => { - const cloudTask = tasks.find((task) => task.name === 'cloud'); + const task = tasks.find((t) => t.name === 'cloud'); it('returns a map of cloud provider data', async () => { const search = jest.fn().mockResolvedValueOnce({ @@ -42,7 +42,7 @@ describe('data telemetry collection tasks', () => { }, }); - expect(await cloudTask?.executor({ indices, search } as any)).toEqual({ + expect(await task?.executor({ indices, search } as any)).toEqual({ cloud: { availability_zone: ['us-west-1', 'europe-west1-c'], provider: ['aws', 'gcp'], @@ -55,7 +55,7 @@ describe('data telemetry collection tasks', () => { it('returns an empty map', async () => { const search = jest.fn().mockResolvedValueOnce({}); - expect(await cloudTask?.executor({ indices, search } as any)).toEqual({ + expect(await task?.executor({ indices, search } as any)).toEqual({ cloud: { availability_zone: [], provider: [], @@ -66,8 +66,83 @@ describe('data telemetry collection tasks', () => { }); }); + describe('processor_events', () => { + const task = tasks.find((t) => t.name === 'processor_events'); + + it('returns a map of processor events', async () => { + const getTime = jest + .spyOn(Date.prototype, 'getTime') + .mockReturnValue(1594330792957); + + const search = jest.fn().mockImplementation((params: any) => { + const isTotalHitsQuery = params?.body?.track_total_hits; + + return Promise.resolve( + isTotalHitsQuery + ? { hits: { total: { value: 1 } } } + : { + hits: { + hits: [{ _source: { '@timestamp': 1 } }], + }, + } + ); + }); + + expect(await task?.executor({ indices, search } as any)).toEqual({ + counts: { + error: { + '1d': 1, + all: 1, + }, + metric: { + '1d': 1, + all: 1, + }, + onboarding: { + '1d': 1, + all: 1, + }, + sourcemap: { + '1d': 1, + all: 1, + }, + span: { + '1d': 1, + all: 1, + }, + transaction: { + '1d': 1, + all: 1, + }, + }, + retainment: { + error: { + ms: 0, + }, + metric: { + ms: 0, + }, + onboarding: { + ms: 0, + }, + sourcemap: { + ms: 0, + }, + span: { + ms: 0, + }, + transaction: { + ms: 0, + }, + }, + }); + + getTime.mockRestore(); + }); + }); + describe('integrations', () => { - const integrationsTask = tasks.find((task) => task.name === 'integrations'); + const task = tasks.find((t) => t.name === 'integrations'); it('returns the count of ML jobs', async () => { const transportRequest = jest @@ -75,7 +150,7 @@ describe('data telemetry collection tasks', () => { .mockResolvedValueOnce({ body: { count: 1 } }); expect( - await integrationsTask?.executor({ indices, transportRequest } as any) + await task?.executor({ indices, transportRequest } as any) ).toEqual({ integrations: { ml: { @@ -90,7 +165,7 @@ describe('data telemetry collection tasks', () => { const transportRequest = jest.fn().mockResolvedValueOnce({}); expect( - await integrationsTask?.executor({ indices, transportRequest } as any) + await task?.executor({ indices, transportRequest } as any) ).toEqual({ integrations: { ml: { @@ -101,4 +176,93 @@ describe('data telemetry collection tasks', () => { }); }); }); + + describe('indices_stats', () => { + const task = tasks.find((t) => t.name === 'indices_stats'); + + it('returns a map of index stats', async () => { + const indicesStats = jest.fn().mockResolvedValueOnce({ + _all: { total: { docs: { count: 1 }, store: { size_in_bytes: 1 } } }, + _shards: { total: 1 }, + }); + + expect(await task?.executor({ indices, indicesStats } as any)).toEqual({ + indices: { + shards: { + total: 1, + }, + all: { + total: { + docs: { + count: 1, + }, + store: { + size_in_bytes: 1, + }, + }, + }, + }, + }); + }); + + describe('with no results', () => { + it('returns zero values', async () => { + const indicesStats = jest.fn().mockResolvedValueOnce({}); + + expect(await task?.executor({ indices, indicesStats } as any)).toEqual({ + indices: { + shards: { + total: 0, + }, + all: { + total: { + docs: { + count: 0, + }, + store: { + size_in_bytes: 0, + }, + }, + }, + }, + }); + }); + }); + }); + + describe('cardinality', () => { + const task = tasks.find((t) => t.name === 'cardinality'); + + it('returns cardinalities', async () => { + const search = jest.fn().mockImplementation((params: any) => { + const isRumQuery = params.body.query.bool.filter.length === 2; + if (isRumQuery) { + return Promise.resolve({ + aggregations: { + 'client.geo.country_iso_code': { value: 5 }, + 'transaction.name': { value: 1 }, + 'user_agent.original': { value: 2 }, + }, + }); + } else { + return Promise.resolve({ + aggregations: { + 'transaction.name': { value: 3 }, + 'user_agent.original': { value: 4 }, + }, + }); + } + }); + + expect(await task?.executor({ search } as any)).toEqual({ + cardinality: { + client: { geo: { country_iso_code: { rum: { '1d': 5 } } } }, + transaction: { name: { all_agents: { '1d': 3 }, rum: { '1d': 1 } } }, + user_agent: { + original: { all_agents: { '1d': 4 }, rum: { '1d': 2 } }, + }, + }, + }); + }); + }); }); diff --git a/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.ts b/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.ts index 4bbaaf3e86e78..2ecb5a935893f 100644 --- a/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.ts +++ b/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.ts @@ -9,6 +9,7 @@ import { AGENT_NAMES } from '../../../../common/agent_name'; import { AGENT_NAME, AGENT_VERSION, + CLIENT_GEO_COUNTRY_ISO_CODE, CLOUD_AVAILABILITY_ZONE, CLOUD_PROVIDER, CLOUD_REGION, @@ -34,6 +35,9 @@ import { APMTelemetry } from '../types'; const TIME_RANGES = ['1d', 'all'] as const; type TimeRange = typeof TIME_RANGES[number]; +const range1d = { range: { '@timestamp': { gte: 'now-1d' } } }; +const timeout = '5m'; + export const tasks: TelemetryTask[] = [ { name: 'cloud', @@ -62,6 +66,7 @@ export const tasks: TelemetryTask[] = [ ], body: { size: 0, + timeout, aggs: { [az]: { terms: { @@ -109,15 +114,14 @@ export const tasks: TelemetryTask[] = [ type ProcessorEvent = keyof typeof indicesByProcessorEvent; - const jobs: Array<{ + interface Job { processorEvent: ProcessorEvent; timeRange: TimeRange; - }> = flatten( - (Object.keys( - indicesByProcessorEvent - ) as ProcessorEvent[]).map((processorEvent) => - TIME_RANGES.map((timeRange) => ({ processorEvent, timeRange })) - ) + } + + const events = Object.keys(indicesByProcessorEvent) as ProcessorEvent[]; + const jobs: Job[] = events.flatMap((processorEvent) => + TIME_RANGES.map((timeRange) => ({ processorEvent, timeRange })) ); const allData = await jobs.reduce((prevJob, current) => { @@ -128,21 +132,12 @@ export const tasks: TelemetryTask[] = [ index: indicesByProcessorEvent[processorEvent], body: { size: 0, + timeout, query: { bool: { filter: [ { term: { [PROCESSOR_EVENT]: processorEvent } }, - ...(timeRange !== 'all' - ? [ - { - range: { - '@timestamp': { - gte: `now-${timeRange}`, - }, - }, - }, - ] - : []), + ...(timeRange === '1d' ? [range1d] : []), ], }, }, @@ -155,6 +150,7 @@ export const tasks: TelemetryTask[] = [ ? await search({ index: indicesByProcessorEvent[processorEvent], body: { + timeout, query: { bool: { filter: [ @@ -208,6 +204,7 @@ export const tasks: TelemetryTask[] = [ index: indices.apmAgentConfigurationIndex, body: { size: 0, + timeout, track_total_hits: true, }, }) @@ -237,6 +234,7 @@ export const tasks: TelemetryTask[] = [ ], body: { size: 0, + timeout, query: { bool: { filter: [ @@ -245,13 +243,7 @@ export const tasks: TelemetryTask[] = [ [AGENT_NAME]: agentName, }, }, - { - range: { - '@timestamp': { - gte: 'now-1d', - }, - }, - }, + range1d, ], }, }, @@ -297,6 +289,7 @@ export const tasks: TelemetryTask[] = [ }, }, size: 1, + timeout, sort: { '@timestamp': 'desc', }, @@ -330,12 +323,12 @@ export const tasks: TelemetryTask[] = [ { name: 'groupings', executor: async ({ search, indices }) => { - const range1d = { range: { '@timestamp': { gte: 'now-1d' } } }; const errorGroupsCount = ( await search({ index: indices['apm_oss.errorIndices'], body: { size: 0, + timeout, query: { bool: { filter: [{ term: { [PROCESSOR_EVENT]: 'error' } }, range1d], @@ -368,6 +361,7 @@ export const tasks: TelemetryTask[] = [ index: indices['apm_oss.transactionIndices'], body: { size: 0, + timeout, query: { bool: { filter: [ @@ -415,6 +409,7 @@ export const tasks: TelemetryTask[] = [ }, track_total_hits: true, size: 0, + timeout, }, }) ).hits.total.value; @@ -428,6 +423,7 @@ export const tasks: TelemetryTask[] = [ ], body: { size: 0, + timeout, query: { bool: { filter: [range1d], @@ -497,12 +493,10 @@ export const tasks: TelemetryTask[] = [ ], body: { size: 0, + timeout, query: { bool: { - filter: [ - { term: { [AGENT_NAME]: agentName } }, - { range: { '@timestamp': { gte: 'now-1d' } } }, - ], + filter: [{ term: { [AGENT_NAME]: agentName } }, range1d], }, }, sort: { @@ -699,15 +693,15 @@ export const tasks: TelemetryTask[] = [ return { indices: { shards: { - total: response._shards.total, + total: response._shards?.total ?? 0, }, all: { total: { docs: { - count: response._all.total.docs.count, + count: response._all?.total?.docs?.count ?? 0, }, store: { - size_in_bytes: response._all.total.store.size_in_bytes, + size_in_bytes: response._all?.total?.store?.size_in_bytes ?? 0, }, }, }, @@ -721,9 +715,10 @@ export const tasks: TelemetryTask[] = [ const allAgentsCardinalityResponse = await search({ body: { size: 0, + timeout, query: { bool: { - filter: [{ range: { '@timestamp': { gte: 'now-1d' } } }], + filter: [range1d], }, }, aggs: { @@ -744,15 +739,19 @@ export const tasks: TelemetryTask[] = [ const rumAgentCardinalityResponse = await search({ body: { size: 0, + timeout, query: { bool: { filter: [ - { range: { '@timestamp': { gte: 'now-1d' } } }, + range1d, { terms: { [AGENT_NAME]: ['rum-js', 'js-base'] } }, ], }, }, aggs: { + [CLIENT_GEO_COUNTRY_ISO_CODE]: { + cardinality: { field: CLIENT_GEO_COUNTRY_ISO_CODE }, + }, [TRANSACTION_NAME]: { cardinality: { field: TRANSACTION_NAME, @@ -769,6 +768,18 @@ export const tasks: TelemetryTask[] = [ return { cardinality: { + client: { + geo: { + country_iso_code: { + rum: { + '1d': + rumAgentCardinalityResponse.aggregations?.[ + CLIENT_GEO_COUNTRY_ISO_CODE + ].value, + }, + }, + }, + }, transaction: { name: { all_agents: { diff --git a/x-pack/plugins/apm/server/lib/apm_telemetry/index.ts b/x-pack/plugins/apm/server/lib/apm_telemetry/index.ts index 632e653a2f6e9..2836cf100a432 100644 --- a/x-pack/plugins/apm/server/lib/apm_telemetry/index.ts +++ b/x-pack/plugins/apm/server/lib/apm_telemetry/index.ts @@ -3,25 +3,26 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { CoreSetup, Logger } from 'src/core/server'; import { Observable } from 'rxjs'; import { take } from 'rxjs/operators'; +import { CoreSetup, Logger } from 'src/core/server'; import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; +import { APMConfig } from '../..'; import { - TaskManagerStartContract, TaskManagerSetupContract, + TaskManagerStartContract, } from '../../../../task_manager/server'; -import { getApmIndices } from '../settings/apm_indices/get_apm_indices'; import { APM_TELEMETRY_SAVED_OBJECT_ID, APM_TELEMETRY_SAVED_OBJECT_TYPE, } from '../../../common/apm_saved_object_constants'; +import { getApmTelemetryMapping } from '../../../common/apm_telemetry'; +import { getInternalSavedObjectsClient } from '../helpers/get_internal_saved_objects_client'; +import { getApmIndices } from '../settings/apm_indices/get_apm_indices'; import { collectDataTelemetry, CollectTelemetryParams, } from './collect_data_telemetry'; -import { APMConfig } from '../..'; -import { getInternalSavedObjectsClient } from '../helpers/get_internal_saved_objects_client'; const APM_TELEMETRY_TASK_NAME = 'apm-telemetry-task'; @@ -97,6 +98,7 @@ export async function createApmTelemetry({ const collector = usageCollector.makeUsageCollector({ type: 'apm', + schema: getApmTelemetryMapping(), fetch: async () => { try { const data = ( diff --git a/x-pack/plugins/apm/server/lib/apm_telemetry/types.ts b/x-pack/plugins/apm/server/lib/apm_telemetry/types.ts index a1d94333b1a08..4c376aac52f5b 100644 --- a/x-pack/plugins/apm/server/lib/apm_telemetry/types.ts +++ b/x-pack/plugins/apm/server/lib/apm_telemetry/types.ts @@ -44,6 +44,7 @@ export type APMDataTelemetry = DeepPartial<{ services: TimeframeMap; }; cardinality: { + client: { geo: { country_iso_code: { rum: TimeframeMap1d } } }; user_agent: { original: { all_agents: TimeframeMap1d; diff --git a/x-pack/plugins/apm/server/lib/helpers/setup_request.ts b/x-pack/plugins/apm/server/lib/helpers/setup_request.ts index af073076a812a..6f381d4945ab4 100644 --- a/x-pack/plugins/apm/server/lib/helpers/setup_request.ts +++ b/x-pack/plugins/apm/server/lib/helpers/setup_request.ts @@ -112,7 +112,7 @@ function getMlSetup(context: APMRequestHandlerContext, request: KibanaRequest) { return; } const ml = context.plugins.ml; - const mlClient = ml.mlClient.asScoped(request).callAsCurrentUser; + const mlClient = ml.mlClient.asScoped(request); return { mlSystem: ml.mlSystemProvider(mlClient, request), anomalyDetectors: ml.anomalyDetectorsProvider(mlClient, request), diff --git a/x-pack/plugins/apm/server/lib/rum_client/get_visitor_breakdown.ts b/x-pack/plugins/apm/server/lib/rum_client/get_visitor_breakdown.ts new file mode 100644 index 0000000000000..a14affb6eeec5 --- /dev/null +++ b/x-pack/plugins/apm/server/lib/rum_client/get_visitor_breakdown.ts @@ -0,0 +1,77 @@ +/* + * 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 { getRumOverviewProjection } from '../../../common/projections/rum_overview'; +import { mergeProjection } from '../../../common/projections/util/merge_projection'; +import { + Setup, + SetupTimeRange, + SetupUIFilters, +} from '../helpers/setup_request'; +import { + USER_AGENT_DEVICE, + USER_AGENT_NAME, + USER_AGENT_OS, +} from '../../../common/elasticsearch_fieldnames'; + +export async function getVisitorBreakdown({ + setup, +}: { + setup: Setup & SetupTimeRange & SetupUIFilters; +}) { + const projection = getRumOverviewProjection({ + setup, + }); + + const params = mergeProjection(projection, { + body: { + size: 0, + query: { + bool: projection.body.query.bool, + }, + aggs: { + browsers: { + terms: { + field: USER_AGENT_NAME, + size: 10, + }, + }, + os: { + terms: { + field: USER_AGENT_OS, + size: 10, + }, + }, + devices: { + terms: { + field: USER_AGENT_DEVICE, + size: 10, + }, + }, + }, + }, + }); + + const { client } = setup; + + const response = await client.search(params); + const { browsers, os, devices } = response.aggregations!; + + return { + browsers: browsers.buckets.map((bucket) => ({ + count: bucket.doc_count, + name: bucket.key as string, + })), + os: os.buckets.map((bucket) => ({ + count: bucket.doc_count, + name: bucket.key as string, + })), + devices: devices.buckets.map((bucket) => ({ + count: bucket.doc_count, + name: bucket.key as string, + })), + }; +} diff --git a/x-pack/plugins/apm/server/routes/create_apm_api.ts b/x-pack/plugins/apm/server/routes/create_apm_api.ts index 4e3aa6d4ebe1d..11911cda79c17 100644 --- a/x-pack/plugins/apm/server/routes/create_apm_api.ts +++ b/x-pack/plugins/apm/server/routes/create_apm_api.ts @@ -77,6 +77,7 @@ import { rumPageLoadDistributionRoute, rumPageLoadDistBreakdownRoute, rumServicesRoute, + rumVisitorsBreakdownRoute, } from './rum_client'; import { observabilityOverviewHasDataRoute, @@ -174,6 +175,7 @@ const createApmApi = () => { .add(rumPageLoadDistBreakdownRoute) .add(rumClientMetricsRoute) .add(rumServicesRoute) + .add(rumVisitorsBreakdownRoute) // Observability dashboard .add(observabilityOverviewHasDataRoute) diff --git a/x-pack/plugins/apm/server/routes/rum_client.ts b/x-pack/plugins/apm/server/routes/rum_client.ts index 01e549632a0bc..0781512c6f7a0 100644 --- a/x-pack/plugins/apm/server/routes/rum_client.ts +++ b/x-pack/plugins/apm/server/routes/rum_client.ts @@ -13,6 +13,7 @@ import { getPageViewTrends } from '../lib/rum_client/get_page_view_trends'; import { getPageLoadDistribution } from '../lib/rum_client/get_page_load_distribution'; import { getPageLoadDistBreakdown } from '../lib/rum_client/get_pl_dist_breakdown'; import { getRumServices } from '../lib/rum_client/get_rum_services'; +import { getVisitorBreakdown } from '../lib/rum_client/get_visitor_breakdown'; export const percentileRangeRt = t.partial({ minPercentile: t.string, @@ -104,3 +105,15 @@ export const rumServicesRoute = createRoute(() => ({ return getRumServices({ setup }); }, })); + +export const rumVisitorsBreakdownRoute = createRoute(() => ({ + path: '/api/apm/rum-client/visitor-breakdown', + params: { + query: t.intersection([uiFiltersRt, rangeRt]), + }, + handler: async ({ context, request }) => { + const setup = await setupRequest(context, request); + + return getVisitorBreakdown({ setup }); + }, +})); diff --git a/x-pack/plugins/beats_management/public/lib/adapters/framework/kibana_framework_adapter.ts b/x-pack/plugins/beats_management/public/lib/adapters/framework/kibana_framework_adapter.ts index 6c1783054a312..6008c52d0324b 100644 --- a/x-pack/plugins/beats_management/public/lib/adapters/framework/kibana_framework_adapter.ts +++ b/x-pack/plugins/beats_management/public/lib/adapters/framework/kibana_framework_adapter.ts @@ -22,7 +22,6 @@ import { import { ManagementSetup, RegisterManagementAppArgs, - ManagementSectionId, } from '../../../../../../../src/plugins/management/public'; import { LicensingPluginSetup } from '../../../../../licensing/public'; import { BeatsManagementConfigType } from '../../../../common'; @@ -105,7 +104,7 @@ export class KibanaFrameworkAdapter implements FrameworkAdapter { } public registerManagementUI(mount: RegisterManagementAppArgs['mount']) { - const section = this.management.sections.getSection(ManagementSectionId.Ingest); + const section = this.management.sections.section.ingest; section.registerApp({ id: 'beats_management', title: i18n.translate('xpack.beatsManagement.centralManagementLinkLabel', { diff --git a/x-pack/plugins/canvas/public/components/saved_elements_modal/saved_elements_modal.tsx b/x-pack/plugins/canvas/public/components/saved_elements_modal/saved_elements_modal.tsx index 44d2f70fcdfad..c318743086b44 100644 --- a/x-pack/plugins/canvas/public/components/saved_elements_modal/saved_elements_modal.tsx +++ b/x-pack/plugins/canvas/public/components/saved_elements_modal/saved_elements_modal.tsx @@ -4,7 +4,14 @@ * you may not use this file except in compliance with the Elastic License. */ -import React, { Fragment, ChangeEvent, FunctionComponent, useState, useEffect } from 'react'; +import React, { + Fragment, + ChangeEvent, + FunctionComponent, + useState, + useEffect, + useRef, +} from 'react'; import PropTypes from 'prop-types'; import { EuiModal, @@ -72,12 +79,16 @@ export const SavedElementsModal: FunctionComponent = ({ removeCustomElement, updateCustomElement, }) => { + const hasLoadedElements = useRef(false); const [elementToDelete, setElementToDelete] = useState(null); const [elementToEdit, setElementToEdit] = useState(null); useEffect(() => { - findCustomElements(); - }); + if (!hasLoadedElements.current) { + hasLoadedElements.current = true; + findCustomElements(); + } + }, [findCustomElements, hasLoadedElements]); const showEditModal = (element: CustomElement) => setElementToEdit(element); const hideEditModal = () => setElementToEdit(null); diff --git a/x-pack/plugins/cross_cluster_replication/public/plugin.ts b/x-pack/plugins/cross_cluster_replication/public/plugin.ts index 8bf0d519e685d..7aa0d19fa976f 100644 --- a/x-pack/plugins/cross_cluster_replication/public/plugin.ts +++ b/x-pack/plugins/cross_cluster_replication/public/plugin.ts @@ -9,7 +9,6 @@ import { get } from 'lodash'; import { first } from 'rxjs/operators'; import { CoreSetup, Plugin, PluginInitializerContext } from 'src/core/public'; -import { ManagementSectionId } from '../../../../src/plugins/management/public'; import { PLUGIN, MANAGEMENT_ID } from '../common/constants'; import { init as initUiMetric } from './app/services/track_ui_metric'; import { init as initNotification } from './app/services/notifications'; @@ -23,7 +22,7 @@ export class CrossClusterReplicationPlugin implements Plugin { public setup(coreSetup: CoreSetup, plugins: PluginDependencies) { const { licensing, remoteClusters, usageCollection, management, indexManagement } = plugins; - const esSection = management.sections.getSection(ManagementSectionId.Data); + const esSection = management.sections.section.data; const { http, diff --git a/x-pack/plugins/data_enhanced/server/search/es_search_strategy.ts b/x-pack/plugins/data_enhanced/server/search/es_search_strategy.ts index 7c1001697421f..7b29117495a67 100644 --- a/x-pack/plugins/data_enhanced/server/search/es_search_strategy.ts +++ b/x-pack/plugins/data_enhanced/server/search/es_search_strategy.ts @@ -8,12 +8,13 @@ import { first } from 'rxjs/operators'; import { mapKeys, snakeCase } from 'lodash'; import { SearchResponse } from 'elasticsearch'; import { Observable } from 'rxjs'; -import { LegacyAPICaller, SharedGlobalConfig } from '../../../../../src/core/server'; -import { ES_SEARCH_STRATEGY } from '../../../../../src/plugins/data/common'; import { - ISearch, + LegacyAPICaller, + SharedGlobalConfig, + RequestHandlerContext, +} from '../../../../../src/core/server'; +import { ISearchOptions, - ISearchCancel, getDefaultSearchParams, getTotalLoaded, ISearchStrategy, @@ -30,11 +31,11 @@ export interface AsyncSearchResponse { export const enhancedEsSearchStrategyProvider = ( config$: Observable -): ISearchStrategy => { - const search: ISearch = async ( - context, +): ISearchStrategy => { + const search = async ( + context: RequestHandlerContext, request: IEnhancedEsSearchRequest, - options + options?: ISearchOptions ) => { const config = await config$.pipe(first()).toPromise(); const caller = context.core.elasticsearch.legacy.client.callAsCurrentUser; @@ -46,7 +47,7 @@ export const enhancedEsSearchStrategyProvider = ( : asyncSearch(caller, { ...request, params }, options); }; - const cancel: ISearchCancel = async (context, id) => { + const cancel = async (context: RequestHandlerContext, id: string) => { const method = 'DELETE'; const path = encodeURI(`/_async_search/${id}`); await context.core.elasticsearch.legacy.client.callAsCurrentUser('transport.request', { 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 0c9f7b29b6411..8d5483b88c4fa 100644 --- a/x-pack/plugins/event_log/server/es/context.mock.ts +++ b/x-pack/plugins/event_log/server/es/context.mock.ts @@ -17,7 +17,7 @@ const createContextMock = () => { logger: loggingSystemMock.createLogger(), esNames: namesMock.create(), initialize: jest.fn(), - waitTillReady: jest.fn(), + waitTillReady: jest.fn(async () => true), esAdapter: clusterClientAdapterMock.create(), initialized: true, }; 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 a78e47446fef8..f30b71c99a043 100644 --- a/x-pack/plugins/event_log/server/es/context.test.ts +++ b/x-pack/plugins/event_log/server/es/context.test.ts @@ -7,9 +7,8 @@ import { createEsContext } from './context'; import { LegacyClusterClient, Logger } from '../../../../../src/core/server'; import { elasticsearchServiceMock, loggingSystemMock } from '../../../../../src/core/server/mocks'; -jest.mock('../lib/../../../../package.json', () => ({ - version: '1.2.3', -})); +jest.mock('../lib/../../../../package.json', () => ({ version: '1.2.3' })); +jest.mock('./init'); type EsClusterClient = Pick, 'callAsInternalUser' | 'asScoped'>; let logger: Logger; @@ -92,4 +91,16 @@ describe('createEsContext', () => { ); expect(doesIndexTemplateExist).toBeTruthy(); }); + + test('should handled failed initialization', async () => { + jest.requireMock('./init').initializeEs.mockResolvedValue(false); + const context = createEsContext({ + logger, + clusterClientPromise: Promise.resolve(clusterClient), + indexNameRoot: 'test2', + }); + context.initialize(); + const success = await context.waitTillReady(); + expect(success).toBe(false); + }); }); diff --git a/x-pack/plugins/event_log/server/es/context.ts b/x-pack/plugins/event_log/server/es/context.ts index 16a460be1793b..8c967e68299b5 100644 --- a/x-pack/plugins/event_log/server/es/context.ts +++ b/x-pack/plugins/event_log/server/es/context.ts @@ -64,9 +64,9 @@ class EsContextImpl implements EsContext { setImmediate(async () => { try { - await this._initialize(); - this.logger.debug('readySignal.signal(true)'); - this.readySignal.signal(true); + const success = await this._initialize(); + this.logger.debug(`readySignal.signal(${success})`); + this.readySignal.signal(success); } catch (err) { this.logger.debug('readySignal.signal(false)'); this.readySignal.signal(false); @@ -74,11 +74,13 @@ class EsContextImpl implements EsContext { }); } + // waits till the ES initialization is done, returns true if it was successful, + // false if it was not successful async waitTillReady(): Promise { return await this.readySignal.wait(); } - private async _initialize() { - await initializeEs(this); + private async _initialize(): Promise { + return await initializeEs(this); } } 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 d4d3df3ef8267..fde3b2de8dd36 100644 --- a/x-pack/plugins/event_log/server/event_logger.test.ts +++ b/x-pack/plugins/event_log/server/event_logger.test.ts @@ -14,25 +14,52 @@ import { delay } from './lib/delay'; import { EVENT_LOGGED_PREFIX } from './event_logger'; const KIBANA_SERVER_UUID = '424-24-2424'; +const WRITE_LOG_WAIT_MILLIS = 3000; describe('EventLogger', () => { let systemLogger: ReturnType; - let esContext: EsContext; + let esContext: jest.Mocked; let service: IEventLogService; let eventLogger: IEventLogger; beforeEach(() => { + jest.resetAllMocks(); systemLogger = loggingSystemMock.createLogger(); esContext = contextMock.create(); service = new EventLogService({ esContext, systemLogger, - config: { enabled: true, logEntries: true, indexEntries: false }, + config: { enabled: true, logEntries: true, indexEntries: true }, kibanaUUID: KIBANA_SERVER_UUID, }); eventLogger = service.getLogger({}); }); + test('handles successful initialization', async () => { + service.registerProviderActions('test-provider', ['test-action-1']); + eventLogger = service.getLogger({ + event: { provider: 'test-provider', action: 'test-action-1' }, + }); + + eventLogger.logEvent({}); + await waitForLogEvent(systemLogger); + delay(WRITE_LOG_WAIT_MILLIS); // sleep a bit since event logging is async + expect(esContext.esAdapter.indexDocument).toHaveBeenCalled(); + }); + + test('handles failed initialization', async () => { + service.registerProviderActions('test-provider', ['test-action-1']); + eventLogger = service.getLogger({ + event: { provider: 'test-provider', action: 'test-action-1' }, + }); + esContext.waitTillReady.mockImplementation(async () => false); + + eventLogger.logEvent({}); + await waitForLogEvent(systemLogger); + delay(WRITE_LOG_WAIT_MILLIS); // sleep a bit longer since event logging is async + expect(esContext.esAdapter.indexDocument).not.toHaveBeenCalled(); + }); + test('method logEvent() writes expected default values', async () => { service.registerProviderActions('test-provider', ['test-action-1']); eventLogger = service.getLogger({ diff --git a/x-pack/plugins/event_log/server/event_logger.ts b/x-pack/plugins/event_log/server/event_logger.ts index 1a710a6fa4865..8730870f9620b 100644 --- a/x-pack/plugins/event_log/server/event_logger.ts +++ b/x-pack/plugins/event_log/server/event_logger.ts @@ -183,7 +183,12 @@ function indexEventDoc(esContext: EsContext, doc: Doc): void { // whew, the thing that actually writes the event log document! async function indexLogEventDoc(esContext: EsContext, doc: unknown) { esContext.logger.debug(`writing to event log: ${JSON.stringify(doc)}`); - await esContext.waitTillReady(); + const success = await esContext.waitTillReady(); + if (!success) { + esContext.logger.debug(`event log did not initialize correctly, event not written`); + return; + } + await esContext.esAdapter.indexDocument(doc); esContext.logger.debug(`writing to event log complete`); } diff --git a/x-pack/plugins/graph/public/angular/graph_client_workspace.js b/x-pack/plugins/graph/public/angular/graph_client_workspace.js index cfa125fcc49ee..5cc06bad4c423 100644 --- a/x-pack/plugins/graph/public/angular/graph_client_workspace.js +++ b/x-pack/plugins/graph/public/angular/graph_client_workspace.js @@ -107,7 +107,7 @@ function UnGroupOperation(parent, child) { // The main constructor for our GraphWorkspace function GraphWorkspace(options) { const self = this; - this.blacklistedNodes = []; + this.blocklistedNodes = []; this.options = options; this.undoLog = []; this.redoLog = []; @@ -379,7 +379,7 @@ function GraphWorkspace(options) { this.redoLog = []; this.nodesMap = {}; this.edgesMap = {}; - this.blacklistedNodes = []; + this.blocklistedNodes = []; this.selectedNodes = []; this.lastResponse = null; }; @@ -630,11 +630,11 @@ function GraphWorkspace(options) { self.runLayout(); }; - this.unblacklist = function (node) { - self.arrRemove(self.blacklistedNodes, node); + this.unblocklist = function (node) { + self.arrRemove(self.blocklistedNodes, node); }; - this.blacklistSelection = function () { + this.blocklistSelection = function () { const selection = self.getAllSelectedNodes(); const danglingEdges = []; self.edges.forEach(function (edge) { @@ -645,7 +645,7 @@ function GraphWorkspace(options) { }); selection.forEach((node) => { delete self.nodesMap[node.id]; - self.blacklistedNodes.push(node); + self.blocklistedNodes.push(node); node.isSelected = false; }); self.arrRemoveAll(self.nodes, selection); @@ -671,10 +671,10 @@ function GraphWorkspace(options) { } let step = {}; - //Add any blacklisted nodes to exclusion list + //Add any blocklisted nodes to exclusion list const excludeNodesByField = {}; const nots = []; - const avoidNodes = this.blacklistedNodes; + const avoidNodes = this.blocklistedNodes; for (let i = 0; i < avoidNodes.length; i++) { const n = avoidNodes[i]; let arr = excludeNodesByField[n.data.field]; @@ -914,8 +914,8 @@ function GraphWorkspace(options) { const nodesByField = {}; const excludeNodesByField = {}; - //Add any blacklisted nodes to exclusion list - const avoidNodes = this.blacklistedNodes; + //Add any blocklisted nodes to exclusion list + const avoidNodes = this.blocklistedNodes; for (let i = 0; i < avoidNodes.length; i++) { const n = avoidNodes[i]; let arr = excludeNodesByField[n.data.field]; @@ -1320,12 +1320,12 @@ function GraphWorkspace(options) { allExistingNodes.forEach((existingNode) => { addTermToFieldList(excludeNodesByField, existingNode.data.field, existingNode.data.term); }); - const blacklistedNodes = self.blacklistedNodes; - blacklistedNodes.forEach((blacklistedNode) => { + const blocklistedNodes = self.blocklistedNodes; + blocklistedNodes.forEach((blocklistedNode) => { addTermToFieldList( excludeNodesByField, - blacklistedNode.data.field, - blacklistedNode.data.term + blocklistedNode.data.field, + blocklistedNode.data.term ); }); diff --git a/x-pack/plugins/graph/public/angular/graph_client_workspace.test.js b/x-pack/plugins/graph/public/angular/graph_client_workspace.test.js index fe6a782373eb2..65766cbefaad3 100644 --- a/x-pack/plugins/graph/public/angular/graph_client_workspace.test.js +++ b/x-pack/plugins/graph/public/angular/graph_client_workspace.test.js @@ -82,7 +82,7 @@ describe('graphui-workspace', function () { expect(workspace.nodes.length).toEqual(2); expect(workspace.edges.length).toEqual(1); expect(workspace.selectedNodes.length).toEqual(0); - expect(workspace.blacklistedNodes.length).toEqual(0); + expect(workspace.blocklistedNodes.length).toEqual(0); const nodeA = workspace.getNode(workspace.makeNodeId('field1', 'a')); expect(typeof nodeA).toBe('object'); @@ -124,7 +124,7 @@ describe('graphui-workspace', function () { expect(workspace.nodes.length).toEqual(2); expect(workspace.edges.length).toEqual(1); expect(workspace.selectedNodes.length).toEqual(0); - expect(workspace.blacklistedNodes.length).toEqual(0); + expect(workspace.blocklistedNodes.length).toEqual(0); mockedResult = { vertices: [ diff --git a/x-pack/plugins/graph/public/angular/templates/index.html b/x-pack/plugins/graph/public/angular/templates/index.html index 939d92518e271..50385008d7b2b 100644 --- a/x-pack/plugins/graph/public/angular/templates/index.html +++ b/x-pack/plugins/graph/public/angular/templates/index.html @@ -124,7 +124,7 @@ diff --git a/x-pack/plugins/graph/public/app.js b/x-pack/plugins/graph/public/app.js index 08b13e9d5c541..fd2b96e0570f6 100644 --- a/x-pack/plugins/graph/public/app.js +++ b/x-pack/plugins/graph/public/app.js @@ -562,8 +562,8 @@ export function initGraphApp(angularModule, deps) { run: () => { const settingsObservable = asAngularSyncedObservable( () => ({ - blacklistedNodes: $scope.workspace ? [...$scope.workspace.blacklistedNodes] : undefined, - unblacklistNode: $scope.workspace ? $scope.workspace.unblacklist : undefined, + blocklistedNodes: $scope.workspace ? [...$scope.workspace.blocklistedNodes] : undefined, + unblocklistNode: $scope.workspace ? $scope.workspace.unblocklist : undefined, canEditDrillDownUrls: canEditDrillDownUrls, }), $scope.$digest.bind($scope) diff --git a/x-pack/plugins/graph/public/components/settings/blacklist_form.tsx b/x-pack/plugins/graph/public/components/settings/blocklist_form.tsx similarity index 72% rename from x-pack/plugins/graph/public/components/settings/blacklist_form.tsx rename to x-pack/plugins/graph/public/components/settings/blocklist_form.tsx index 68cdcc1fbb7b1..29ab7611fcee8 100644 --- a/x-pack/plugins/graph/public/components/settings/blacklist_form.tsx +++ b/x-pack/plugins/graph/public/components/settings/blocklist_form.tsx @@ -20,16 +20,16 @@ import { SettingsProps } from './settings'; import { LegacyIcon } from '../legacy_icon'; import { useListKeys } from './use_list_keys'; -export function BlacklistForm({ - blacklistedNodes, - unblacklistNode, -}: Pick) { - const getListKey = useListKeys(blacklistedNodes || []); +export function BlocklistForm({ + blocklistedNodes, + unblocklistNode, +}: Pick) { + const getListKey = useListKeys(blocklistedNodes || []); return ( <> - {blacklistedNodes && blacklistedNodes.length > 0 ? ( + {blocklistedNodes && blocklistedNodes.length > 0 ? ( - {i18n.translate('xpack.graph.settings.blacklist.blacklistHelpText', { + {i18n.translate('xpack.graph.settings.blocklist.blocklistHelpText', { defaultMessage: 'These terms are not allowed in the graph.', })} @@ -37,7 +37,7 @@ export function BlacklistForm({ }} /> @@ -45,25 +45,25 @@ export function BlacklistForm({ /> )} - {blacklistedNodes && unblacklistNode && blacklistedNodes.length > 0 && ( + {blocklistedNodes && unblocklistNode && blocklistedNodes.length > 0 && ( <> - {blacklistedNodes.map((node) => ( + {blocklistedNodes.map((node) => ( } key={getListKey(node)} label={node.label} extraAction={{ iconType: 'trash', - 'aria-label': i18n.translate('xpack.graph.blacklist.removeButtonAriaLabel', { + 'aria-label': i18n.translate('xpack.graph.blocklist.removeButtonAriaLabel', { defaultMessage: 'Delete', }), - title: i18n.translate('xpack.graph.blacklist.removeButtonAriaLabel', { + title: i18n.translate('xpack.graph.blocklist.removeButtonAriaLabel', { defaultMessage: 'Delete', }), color: 'danger', onClick: () => { - unblacklistNode(node); + unblocklistNode(node); }, }} /> @@ -71,18 +71,18 @@ export function BlacklistForm({ { - blacklistedNodes.forEach((node) => { - unblacklistNode(node); + blocklistedNodes.forEach((node) => { + unblocklistNode(node); }); }} > - {i18n.translate('xpack.graph.settings.blacklist.clearButtonLabel', { + {i18n.translate('xpack.graph.settings.blocklist.clearButtonLabel', { defaultMessage: 'Delete all', })} diff --git a/x-pack/plugins/graph/public/components/settings/settings.test.tsx b/x-pack/plugins/graph/public/components/settings/settings.test.tsx index 1efaead002b52..7d13249288d53 100644 --- a/x-pack/plugins/graph/public/components/settings/settings.test.tsx +++ b/x-pack/plugins/graph/public/components/settings/settings.test.tsx @@ -46,7 +46,7 @@ describe('settings', () => { }; const angularProps: jest.Mocked = { - blacklistedNodes: [ + blocklistedNodes: [ { x: 0, y: 0, @@ -57,7 +57,7 @@ describe('settings', () => { field: 'A', term: '1', }, - label: 'blacklisted node 1', + label: 'blocklisted node 1', icon: { class: 'test', code: '1', @@ -74,7 +74,7 @@ describe('settings', () => { field: 'A', term: '1', }, - label: 'blacklisted node 2', + label: 'blocklisted node 2', icon: { class: 'test', code: '1', @@ -82,7 +82,7 @@ describe('settings', () => { }, }, ], - unblacklistNode: jest.fn(), + unblocklistNode: jest.fn(), canEditDrillDownUrls: true, }; @@ -201,15 +201,15 @@ describe('settings', () => { }); }); - describe('blacklist', () => { + describe('blocklist', () => { beforeEach(() => { toTab('Block list'); }); - it('should switch tab to blacklist', () => { + it('should switch tab to blocklist', () => { expect(instance.find(EuiListGroupItem).map((item) => item.prop('label'))).toEqual([ - 'blacklisted node 1', - 'blacklisted node 2', + 'blocklisted node 1', + 'blocklisted node 2', ]); }); @@ -217,7 +217,7 @@ describe('settings', () => { act(() => { subject.next({ ...angularProps, - blacklistedNodes: [ + blocklistedNodes: [ { x: 0, y: 0, @@ -228,7 +228,7 @@ describe('settings', () => { field: 'A', term: '1', }, - label: 'blacklisted node 3', + label: 'blocklisted node 3', icon: { class: 'test', code: '1', @@ -242,21 +242,21 @@ describe('settings', () => { instance.update(); expect(instance.find(EuiListGroupItem).map((item) => item.prop('label'))).toEqual([ - 'blacklisted node 3', + 'blocklisted node 3', ]); }); it('should delete node', () => { instance.find(EuiListGroupItem).at(0).prop('extraAction')!.onClick!({} as any); - expect(angularProps.unblacklistNode).toHaveBeenCalledWith(angularProps.blacklistedNodes![0]); + expect(angularProps.unblocklistNode).toHaveBeenCalledWith(angularProps.blocklistedNodes![0]); }); it('should delete all nodes', () => { - instance.find('[data-test-subj="graphUnblacklistAll"]').find(EuiButton).simulate('click'); + instance.find('[data-test-subj="graphUnblocklistAll"]').find(EuiButton).simulate('click'); - expect(angularProps.unblacklistNode).toHaveBeenCalledWith(angularProps.blacklistedNodes![0]); - expect(angularProps.unblacklistNode).toHaveBeenCalledWith(angularProps.blacklistedNodes![1]); + expect(angularProps.unblocklistNode).toHaveBeenCalledWith(angularProps.blocklistedNodes![0]); + expect(angularProps.unblocklistNode).toHaveBeenCalledWith(angularProps.blocklistedNodes![1]); }); }); diff --git a/x-pack/plugins/graph/public/components/settings/settings.tsx b/x-pack/plugins/graph/public/components/settings/settings.tsx index 3baf6b6a0a2e3..3a9ea6e96859b 100644 --- a/x-pack/plugins/graph/public/components/settings/settings.tsx +++ b/x-pack/plugins/graph/public/components/settings/settings.tsx @@ -11,7 +11,7 @@ import * as Rx from 'rxjs'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import { AdvancedSettingsForm } from './advanced_settings_form'; -import { BlacklistForm } from './blacklist_form'; +import { BlocklistForm } from './blocklist_form'; import { UrlTemplateList } from './url_template_list'; import { WorkspaceNode, AdvancedSettings, UrlTemplate, WorkspaceField } from '../../types'; import { @@ -33,9 +33,9 @@ const tabs = [ component: AdvancedSettingsForm, }, { - id: 'blacklist', - title: i18n.translate('xpack.graph.settings.blacklistTitle', { defaultMessage: 'Block list' }), - component: BlacklistForm, + id: 'blocklist', + title: i18n.translate('xpack.graph.settings.blocklistTitle', { defaultMessage: 'Block list' }), + component: BlocklistForm, }, { id: 'drillDowns', @@ -51,8 +51,8 @@ const tabs = [ * to catch update outside updates */ export interface AngularProps { - blacklistedNodes: WorkspaceNode[]; - unblacklistNode: (node: WorkspaceNode) => void; + blocklistedNodes: WorkspaceNode[]; + unblocklistNode: (node: WorkspaceNode) => void; canEditDrillDownUrls: boolean; } diff --git a/x-pack/plugins/graph/public/services/persistence/deserialize.test.ts b/x-pack/plugins/graph/public/services/persistence/deserialize.test.ts index 3dda41fcdbdb6..e9f116b79f990 100644 --- a/x-pack/plugins/graph/public/services/persistence/deserialize.test.ts +++ b/x-pack/plugins/graph/public/services/persistence/deserialize.test.ts @@ -26,7 +26,7 @@ describe('deserialize', () => { { color: 'black', name: 'field1', selected: true, iconClass: 'a' }, { color: 'black', name: 'field2', selected: true, iconClass: 'b' }, ], - blacklist: [ + blocklist: [ { color: 'black', label: 'Z', @@ -192,7 +192,7 @@ describe('deserialize', () => { it('should deserialize nodes and edges', () => { callSavedWorkspaceToAppState(); - expect(workspace.blacklistedNodes.length).toEqual(1); + expect(workspace.blocklistedNodes.length).toEqual(1); expect(workspace.nodes.length).toEqual(5); expect(workspace.edges.length).toEqual(2); diff --git a/x-pack/plugins/graph/public/services/persistence/deserialize.ts b/x-pack/plugins/graph/public/services/persistence/deserialize.ts index 6fd720a60edc0..324bf10cdd99c 100644 --- a/x-pack/plugins/graph/public/services/persistence/deserialize.ts +++ b/x-pack/plugins/graph/public/services/persistence/deserialize.ts @@ -128,11 +128,11 @@ function getFieldsWithWorkspaceSettings( return allFields; } -function getBlacklistedNodes( +function getBlocklistedNodes( serializedWorkspaceState: SerializedWorkspaceState, allFields: WorkspaceField[] ) { - return serializedWorkspaceState.blacklist.map((serializedNode) => { + return serializedWorkspaceState.blocklist.map((serializedNode) => { const currentField = allFields.find((field) => field.name === serializedNode.field)!; return { x: 0, @@ -235,9 +235,9 @@ export function savedWorkspaceToAppState( workspaceInstance.mergeGraph(graph); resolveGroups(persistedWorkspaceState.vertices, workspaceInstance); - // ================== blacklist ============================= - const blacklistedNodes = getBlacklistedNodes(persistedWorkspaceState, allFields); - workspaceInstance.blacklistedNodes.push(...blacklistedNodes); + // ================== blocklist ============================= + const blocklistedNodes = getBlocklistedNodes(persistedWorkspaceState, allFields); + workspaceInstance.blocklistedNodes.push(...blocklistedNodes); return { urlTemplates, diff --git a/x-pack/plugins/graph/public/services/persistence/serialize.test.ts b/x-pack/plugins/graph/public/services/persistence/serialize.test.ts index a3942eccfdac3..0c9de0418a738 100644 --- a/x-pack/plugins/graph/public/services/persistence/serialize.test.ts +++ b/x-pack/plugins/graph/public/services/persistence/serialize.test.ts @@ -118,7 +118,7 @@ describe('serialize', () => { parent: null, }, ], - blacklistedNodes: [ + blocklistedNodes: [ { color: 'black', data: { field: 'field1', term: 'Z' }, @@ -165,7 +165,7 @@ describe('serialize', () => { const workspaceState = JSON.parse(savedWorkspace.wsState); expect(workspaceState).toMatchInlineSnapshot(` Object { - "blacklist": Array [ + "blocklist": Array [ Object { "color": "black", "field": "field1", diff --git a/x-pack/plugins/graph/public/services/persistence/serialize.ts b/x-pack/plugins/graph/public/services/persistence/serialize.ts index 6cbebc995d84a..a3a76a8a08eba 100644 --- a/x-pack/plugins/graph/public/services/persistence/serialize.ts +++ b/x-pack/plugins/graph/public/services/persistence/serialize.ts @@ -96,8 +96,8 @@ export function appStateToSavedWorkspace( }, canSaveData: boolean ) { - const blacklist: SerializedNode[] = canSaveData - ? workspace.blacklistedNodes.map((node) => serializeNode(node)) + const blocklist: SerializedNode[] = canSaveData + ? workspace.blocklistedNodes.map((node) => serializeNode(node)) : []; const vertices: SerializedNode[] = canSaveData ? workspace.nodes.map((node) => serializeNode(node, workspace.nodes)) @@ -111,7 +111,7 @@ export function appStateToSavedWorkspace( const persistedWorkspaceState: SerializedWorkspaceState = { indexPattern: selectedIndex.title, selectedFields: selectedFields.map(serializeField), - blacklist, + blocklist, vertices, links, urlTemplates: mappedUrlTemplates, diff --git a/x-pack/plugins/graph/public/state_management/mocks.ts b/x-pack/plugins/graph/public/state_management/mocks.ts index 5a0269d691de2..d32bc9a175a47 100644 --- a/x-pack/plugins/graph/public/state_management/mocks.ts +++ b/x-pack/plugins/graph/public/state_management/mocks.ts @@ -46,7 +46,7 @@ export function createMockGraphStore({ nodes: [], edges: [], options: {}, - blacklistedNodes: [], + blocklistedNodes: [], } as unknown) as Workspace; const savedWorkspace = ({ diff --git a/x-pack/plugins/graph/public/state_management/persistence.ts b/x-pack/plugins/graph/public/state_management/persistence.ts index cd2c6680c1fd2..cf6566f0c5f86 100644 --- a/x-pack/plugins/graph/public/state_management/persistence.ts +++ b/x-pack/plugins/graph/public/state_management/persistence.ts @@ -198,7 +198,7 @@ function showModal( openSaveModal({ savePolicy: deps.savePolicy, - hasData: workspace.nodes.length > 0 || workspace.blacklistedNodes.length > 0, + hasData: workspace.nodes.length > 0 || workspace.blocklistedNodes.length > 0, workspace: savedWorkspace, showSaveModal: deps.showSaveModal, saveWorkspace: saveWorkspaceHandler, diff --git a/x-pack/plugins/graph/public/types/persistence.ts b/x-pack/plugins/graph/public/types/persistence.ts index 6847199d5878c..8e7e9c7e8878e 100644 --- a/x-pack/plugins/graph/public/types/persistence.ts +++ b/x-pack/plugins/graph/public/types/persistence.ts @@ -33,7 +33,7 @@ export interface GraphWorkspaceSavedObject { export interface SerializedWorkspaceState { indexPattern: string; selectedFields: SerializedField[]; - blacklist: SerializedNode[]; + blocklist: SerializedNode[]; vertices: SerializedNode[]; links: SerializedEdge[]; urlTemplates: SerializedUrlTemplate[]; diff --git a/x-pack/plugins/graph/public/types/workspace_state.ts b/x-pack/plugins/graph/public/types/workspace_state.ts index 8c4178eda890f..b5ee48311ddc8 100644 --- a/x-pack/plugins/graph/public/types/workspace_state.ts +++ b/x-pack/plugins/graph/public/types/workspace_state.ts @@ -63,7 +63,7 @@ export interface Workspace { nodesMap: Record; nodes: WorkspaceNode[]; edges: WorkspaceEdge[]; - blacklistedNodes: WorkspaceNode[]; + blocklistedNodes: WorkspaceNode[]; getQuery(startNodes?: WorkspaceNode[], loose?: boolean): JsonObject; getSelectedOrAllNodes(): WorkspaceNode[]; diff --git a/x-pack/plugins/graph/server/sample_data/ecommerce.ts b/x-pack/plugins/graph/server/sample_data/ecommerce.ts index 7543e9471f05c..b9b4e063cb28f 100644 --- a/x-pack/plugins/graph/server/sample_data/ecommerce.ts +++ b/x-pack/plugins/graph/server/sample_data/ecommerce.ts @@ -37,7 +37,7 @@ const wsState: any = { iconClass: 'fa-heart', }, ], - blacklist: [ + blocklist: [ { x: 491.3880229084531, y: 572.375603969653, diff --git a/x-pack/plugins/graph/server/sample_data/flights.ts b/x-pack/plugins/graph/server/sample_data/flights.ts index bca1d0d093a8e..209b7108266cf 100644 --- a/x-pack/plugins/graph/server/sample_data/flights.ts +++ b/x-pack/plugins/graph/server/sample_data/flights.ts @@ -37,7 +37,7 @@ const wsState: any = { iconClass: 'fa-cube', }, ], - blacklist: [], + blocklist: [], vertices: [ { x: 324.55695700802687, diff --git a/x-pack/plugins/graph/server/sample_data/logs.ts b/x-pack/plugins/graph/server/sample_data/logs.ts index 5ca810b397cd2..c3cc2ecd2fc65 100644 --- a/x-pack/plugins/graph/server/sample_data/logs.ts +++ b/x-pack/plugins/graph/server/sample_data/logs.ts @@ -45,7 +45,7 @@ const wsState: any = { iconClass: 'fa-key', }, ], - blacklist: [ + blocklist: [ { x: 349.9814471314239, y: 274.1259761174194, diff --git a/x-pack/plugins/graph/server/saved_objects/migrations.ts b/x-pack/plugins/graph/server/saved_objects/migrations.ts index beb31d548c670..34cd59e2220e9 100644 --- a/x-pack/plugins/graph/server/saved_objects/migrations.ts +++ b/x-pack/plugins/graph/server/saved_objects/migrations.ts @@ -37,4 +37,23 @@ export const graphMigrations = { }); return doc; }, + '7.10.0': (doc: SavedObjectUnsanitizedDoc) => { + const wsState = get(doc, 'attributes.wsState'); + if (typeof wsState !== 'string') { + return doc; + } + let state; + try { + state = JSON.parse(JSON.parse(wsState)); + } catch (e) { + // Let it go, the data is invalid and we'll leave it as is + return doc; + } + if (state.blacklist) { + state.blocklist = state.blacklist; + delete state.blacklist; + } + doc.attributes.wsState = JSON.stringify(JSON.stringify(state)); + return doc; + }, }; diff --git a/x-pack/plugins/index_lifecycle_management/public/plugin.tsx b/x-pack/plugins/index_lifecycle_management/public/plugin.tsx index 49856dee47fba..832d066dfa33b 100644 --- a/x-pack/plugins/index_lifecycle_management/public/plugin.tsx +++ b/x-pack/plugins/index_lifecycle_management/public/plugin.tsx @@ -6,7 +6,6 @@ import { CoreSetup, PluginInitializerContext } from 'src/core/public'; -import { ManagementSectionId } from '../../../../src/plugins/management/public'; import { PLUGIN } from '../common/constants'; import { init as initHttp } from './application/services/http'; import { init as initDocumentation } from './application/services/documentation'; @@ -38,7 +37,7 @@ export class IndexLifecycleManagementPlugin { initUiMetric(usageCollection); initNotification(toasts, fatalErrors); - management.sections.getSection(ManagementSectionId.Data).registerApp({ + management.sections.section.data.registerApp({ id: PLUGIN.ID, title: PLUGIN.TITLE, order: 2, diff --git a/x-pack/plugins/index_management/common/types/templates.ts b/x-pack/plugins/index_management/common/types/templates.ts index 32e254e490b2a..eda00ec819159 100644 --- a/x-pack/plugins/index_management/common/types/templates.ts +++ b/x-pack/plugins/index_management/common/types/templates.ts @@ -22,7 +22,7 @@ export interface TemplateSerialized { version?: number; priority?: number; _meta?: { [key: string]: any }; - data_stream?: { timestamp_field: string }; + data_stream?: {}; } /** @@ -46,7 +46,7 @@ export interface TemplateDeserialized { name: string; }; _meta?: { [key: string]: any }; // Composable template only - dataStream?: { timestamp_field: string }; // Composable template only + dataStream?: {}; // Composable template only _kbnMeta: { type: TemplateType; hasDatastream: boolean; diff --git a/x-pack/plugins/index_management/public/application/components/component_templates/component_template_selector/component_templates.scss b/x-pack/plugins/index_management/public/application/components/component_templates/component_template_selector/component_templates.scss index 51e8a829e81b1..026e63b2b4caa 100644 --- a/x-pack/plugins/index_management/public/application/components/component_templates/component_template_selector/component_templates.scss +++ b/x-pack/plugins/index_management/public/application/components/component_templates/component_template_selector/component_templates.scss @@ -7,7 +7,8 @@ $heightHeader: $euiSizeL * 2; .componentTemplates { - @include euiBottomShadowFlat; + border: $euiBorderThin; + border-top: none; height: 100%; &__header { @@ -20,6 +21,7 @@ $heightHeader: $euiSizeL * 2; &__searchBox { border-bottom: $euiBorderThin; + border-top: $euiBorderThin; box-shadow: none; max-width: initial; } diff --git a/x-pack/plugins/index_management/public/application/components/component_templates/component_template_selector/component_templates_selector.scss b/x-pack/plugins/index_management/public/application/components/component_templates/component_template_selector/component_templates_selector.scss index 61d5512da2cd9..041fc1c8bf9a4 100644 --- a/x-pack/plugins/index_management/public/application/components/component_templates/component_template_selector/component_templates_selector.scss +++ b/x-pack/plugins/index_management/public/application/components/component_templates/component_template_selector/component_templates_selector.scss @@ -6,7 +6,7 @@ height: 480px; &__selection { - @include euiBottomShadowFlat; + border: $euiBorderThin; padding: 0 $euiSize $euiSize; color: $euiColorDarkShade; diff --git a/x-pack/plugins/index_management/public/application/components/template_form/steps/step_logistics.tsx b/x-pack/plugins/index_management/public/application/components/template_form/steps/step_logistics.tsx index ad98aee5fb5f1..f3d05ac38108a 100644 --- a/x-pack/plugins/index_management/public/application/components/template_form/steps/step_logistics.tsx +++ b/x-pack/plugins/index_management/public/application/components/template_form/steps/step_logistics.tsx @@ -62,7 +62,7 @@ function getFieldsMeta(esDocsBase: string) { description: ( diff --git a/x-pack/plugins/index_management/public/application/components/template_form/template_form_schemas.tsx b/x-pack/plugins/index_management/public/application/components/template_form/template_form_schemas.tsx index d8c3ad8c259fc..0d9ce57a64c84 100644 --- a/x-pack/plugins/index_management/public/application/components/template_form/template_form_schemas.tsx +++ b/x-pack/plugins/index_management/public/application/components/template_form/template_form_schemas.tsx @@ -136,9 +136,9 @@ export const schemas: Record = { defaultValue: false, serializer: (value) => { if (value === true) { - return { - timestamp_field: '@timestamp', - }; + // For now, ES expects an empty object when defining a data stream + // https://github.com/elastic/elasticsearch/pull/59317 + return {}; } }, deserializer: (value) => { diff --git a/x-pack/plugins/index_management/public/plugin.ts b/x-pack/plugins/index_management/public/plugin.ts index aec25ee3247d6..6139ed5d2e6ad 100644 --- a/x-pack/plugins/index_management/public/plugin.ts +++ b/x-pack/plugins/index_management/public/plugin.ts @@ -7,7 +7,7 @@ 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 { ManagementSetup } from '../../../../src/plugins/management/public'; import { IngestManagerSetup } from '../../ingest_manager/public'; import { UIM_APP_NAME, PLUGIN } from '../common/constants'; @@ -51,7 +51,7 @@ export class IndexMgmtUIPlugin { notificationService.setup(notifications); this.uiMetricService.setup(usageCollection); - management.sections.getSection(ManagementSectionId.Data).registerApp({ + management.sections.section.data.registerApp({ id: PLUGIN.id, title: i18n.translate('xpack.idxMgmt.appTitle', { defaultMessage: 'Index Management' }), order: 0, diff --git a/x-pack/plugins/infra/common/http_api/log_analysis/results/index.ts b/x-pack/plugins/infra/common/http_api/log_analysis/results/index.ts index cbd89db97236f..a01042616a872 100644 --- a/x-pack/plugins/infra/common/http_api/log_analysis/results/index.ts +++ b/x-pack/plugins/infra/common/http_api/log_analysis/results/index.ts @@ -10,3 +10,4 @@ export * from './log_entry_category_examples'; export * from './log_entry_rate'; export * from './log_entry_examples'; export * from './log_entry_anomalies'; +export * from './log_entry_anomalies_datasets'; diff --git a/x-pack/plugins/infra/common/http_api/log_analysis/results/log_entry_anomalies.ts b/x-pack/plugins/infra/common/http_api/log_analysis/results/log_entry_anomalies.ts index 639ac63f9b14d..62b76a0ae475e 100644 --- a/x-pack/plugins/infra/common/http_api/log_analysis/results/log_entry_anomalies.ts +++ b/x-pack/plugins/infra/common/http_api/log_analysis/results/log_entry_anomalies.ts @@ -128,6 +128,8 @@ export const getLogEntryAnomaliesRequestPayloadRT = rt.type({ pagination: paginationRT, // Sort properties sort: sortRT, + // Dataset filters + datasets: rt.array(rt.string), }), ]), }); diff --git a/x-pack/plugins/infra/common/http_api/log_analysis/results/log_entry_anomalies_datasets.ts b/x-pack/plugins/infra/common/http_api/log_analysis/results/log_entry_anomalies_datasets.ts new file mode 100644 index 0000000000000..56784dba1be44 --- /dev/null +++ b/x-pack/plugins/infra/common/http_api/log_analysis/results/log_entry_anomalies_datasets.ts @@ -0,0 +1,63 @@ +/* + * 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 { + badRequestErrorRT, + forbiddenErrorRT, + timeRangeRT, + routeTimingMetadataRT, +} from '../../shared'; + +export const LOG_ANALYSIS_GET_LOG_ENTRY_ANOMALIES_DATASETS_PATH = + '/api/infra/log_analysis/results/log_entry_anomalies_datasets'; + +/** + * request + */ + +export const getLogEntryAnomaliesDatasetsRequestPayloadRT = rt.type({ + data: rt.type({ + // the id of the source configuration + sourceId: rt.string, + // the time range to fetch the anomalies datasets from + timeRange: timeRangeRT, + }), +}); + +export type GetLogEntryAnomaliesDatasetsRequestPayload = rt.TypeOf< + typeof getLogEntryAnomaliesDatasetsRequestPayloadRT +>; + +/** + * response + */ + +export const getLogEntryAnomaliesDatasetsSuccessReponsePayloadRT = rt.intersection([ + rt.type({ + data: rt.type({ + datasets: rt.array(rt.string), + }), + }), + rt.partial({ + timing: routeTimingMetadataRT, + }), +]); + +export type GetLogEntryAnomaliesDatasetsSuccessResponsePayload = rt.TypeOf< + typeof getLogEntryAnomaliesDatasetsSuccessReponsePayloadRT +>; + +export const getLogEntryAnomaliesDatasetsResponsePayloadRT = rt.union([ + getLogEntryAnomaliesDatasetsSuccessReponsePayloadRT, + badRequestErrorRT, + forbiddenErrorRT, +]); + +export type GetLogEntryAnomaliesDatasetsReponsePayload = rt.TypeOf< + typeof getLogEntryAnomaliesDatasetsResponsePayloadRT +>; diff --git a/x-pack/plugins/infra/common/http_api/log_analysis/results/log_entry_rate.ts b/x-pack/plugins/infra/common/http_api/log_analysis/results/log_entry_rate.ts index b7e8a49735152..20a8e5c378cec 100644 --- a/x-pack/plugins/infra/common/http_api/log_analysis/results/log_entry_rate.ts +++ b/x-pack/plugins/infra/common/http_api/log_analysis/results/log_entry_rate.ts @@ -16,11 +16,16 @@ export const LOG_ANALYSIS_GET_LOG_ENTRY_RATE_PATH = */ export const getLogEntryRateRequestPayloadRT = rt.type({ - data: rt.type({ - bucketDuration: rt.number, - sourceId: rt.string, - timeRange: timeRangeRT, - }), + data: rt.intersection([ + rt.type({ + bucketDuration: rt.number, + sourceId: rt.string, + timeRange: timeRangeRT, + }), + rt.partial({ + datasets: rt.array(rt.string), + }), + ]), }); export type GetLogEntryRateRequestPayload = rt.TypeOf; diff --git a/x-pack/plugins/infra/public/pages/logs/log_entry_categories/sections/top_categories/datasets_selector.tsx b/x-pack/plugins/infra/public/components/logging/log_analysis_results/datasets_selector.tsx similarity index 92% rename from x-pack/plugins/infra/public/pages/logs/log_entry_categories/sections/top_categories/datasets_selector.tsx rename to x-pack/plugins/infra/public/components/logging/log_analysis_results/datasets_selector.tsx index ab938ff1d1374..2236dc9e45da6 100644 --- a/x-pack/plugins/infra/public/pages/logs/log_entry_categories/sections/top_categories/datasets_selector.tsx +++ b/x-pack/plugins/infra/public/components/logging/log_analysis_results/datasets_selector.tsx @@ -8,7 +8,7 @@ import { EuiComboBox, EuiComboBoxOptionOption } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import React, { useCallback, useMemo } from 'react'; -import { getFriendlyNameForPartitionId } from '../../../../../../common/log_analysis'; +import { getFriendlyNameForPartitionId } from '../../../../common/log_analysis'; type DatasetOptionProps = EuiComboBoxOptionOption; @@ -51,7 +51,7 @@ export const DatasetsSelector: React.FunctionComponent<{ }; const datasetFilterPlaceholder = i18n.translate( - 'xpack.infra.logs.logEntryCategories.datasetFilterPlaceholder', + 'xpack.infra.logs.analysis.datasetFilterPlaceholder', { defaultMessage: 'Filter by datasets', } diff --git a/x-pack/plugins/infra/public/pages/logs/log_entry_categories/sections/top_categories/top_categories_section.tsx b/x-pack/plugins/infra/public/pages/logs/log_entry_categories/sections/top_categories/top_categories_section.tsx index 37d26de6fce70..ea23bc468bc76 100644 --- a/x-pack/plugins/infra/public/pages/logs/log_entry_categories/sections/top_categories/top_categories_section.tsx +++ b/x-pack/plugins/infra/public/pages/logs/log_entry_categories/sections/top_categories/top_categories_section.tsx @@ -14,7 +14,7 @@ import { BetaBadge } from '../../../../../components/beta_badge'; import { LoadingOverlayWrapper } from '../../../../../components/loading_overlay_wrapper'; import { RecreateJobButton } from '../../../../../components/logging/log_analysis_job_status'; import { AnalyzeInMlButton } from '../../../../../components/logging/log_analysis_results'; -import { DatasetsSelector } from './datasets_selector'; +import { DatasetsSelector } from '../../../../../components/logging/log_analysis_results/datasets_selector'; import { TopCategoriesTable } from './top_categories_table'; export const TopCategoriesSection: React.FunctionComponent<{ diff --git a/x-pack/plugins/infra/public/pages/logs/log_entry_rate/page_results_content.tsx b/x-pack/plugins/infra/public/pages/logs/log_entry_rate/page_results_content.tsx index f2a60541b3b3c..fb1dc7717fed0 100644 --- a/x-pack/plugins/infra/public/pages/logs/log_entry_rate/page_results_content.tsx +++ b/x-pack/plugins/infra/public/pages/logs/log_entry_rate/page_results_content.tsx @@ -27,6 +27,7 @@ import { StringTimeRange, useLogAnalysisResultsUrlState, } from './use_log_entry_rate_results_url_state'; +import { DatasetsSelector } from '../../../components/logging/log_analysis_results/datasets_selector'; export const SORT_DEFAULTS = { direction: 'desc' as const, @@ -80,11 +81,14 @@ export const LogEntryRateResultsContent: React.FunctionComponent = () => { [queryTimeRange.value.endTime, queryTimeRange.value.startTime] ); + const [selectedDatasets, setSelectedDatasets] = useState([]); + const { getLogEntryRate, isLoading, logEntryRate } = useLogEntryRateResults({ sourceId, startTime: queryTimeRange.value.startTime, endTime: queryTimeRange.value.endTime, bucketDuration, + filteredDatasets: selectedDatasets, }); const { @@ -97,12 +101,15 @@ export const LogEntryRateResultsContent: React.FunctionComponent = () => { changePaginationOptions, sortOptions, paginationOptions, + datasets, + isLoadingDatasets, } = useLogEntryAnomaliesResults({ sourceId, startTime: queryTimeRange.value.startTime, endTime: queryTimeRange.value.endTime, defaultSortOptions: SORT_DEFAULTS, defaultPaginationOptions: PAGINATION_DEFAULTS, + filteredDatasets: selectedDatasets, }); const handleQueryTimeRangeChange = useCallback( @@ -175,7 +182,7 @@ export const LogEntryRateResultsContent: React.FunctionComponent = () => { useEffect(() => { getLogEntryRate(); - }, [getLogEntryRate, queryTimeRange.lastChangedTime]); + }, [getLogEntryRate, selectedDatasets, queryTimeRange.lastChangedTime]); useInterval( () => { @@ -191,7 +198,15 @@ export const LogEntryRateResultsContent: React.FunctionComponent = () => { - + + + + { const response = await npStart.http.fetch(LOG_ANALYSIS_GET_LOG_ENTRY_ANOMALIES_PATH, { method: 'POST', @@ -32,6 +33,7 @@ export const callGetLogEntryAnomaliesAPI = async ( }, sort, pagination, + datasets, }, }) ), diff --git a/x-pack/plugins/infra/public/pages/logs/log_entry_rate/service_calls/get_log_entry_anomalies_datasets.ts b/x-pack/plugins/infra/public/pages/logs/log_entry_rate/service_calls/get_log_entry_anomalies_datasets.ts new file mode 100644 index 0000000000000..24be5a646d103 --- /dev/null +++ b/x-pack/plugins/infra/public/pages/logs/log_entry_rate/service_calls/get_log_entry_anomalies_datasets.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 { npStart } from '../../../../legacy_singletons'; +import { decodeOrThrow } from '../../../../../common/runtime_types'; +import { + getLogEntryAnomaliesDatasetsRequestPayloadRT, + getLogEntryAnomaliesDatasetsSuccessReponsePayloadRT, + LOG_ANALYSIS_GET_LOG_ENTRY_ANOMALIES_DATASETS_PATH, +} from '../../../../../common/http_api/log_analysis'; + +export const callGetLogEntryAnomaliesDatasetsAPI = async ( + sourceId: string, + startTime: number, + endTime: number +) => { + const response = await npStart.http.fetch(LOG_ANALYSIS_GET_LOG_ENTRY_ANOMALIES_DATASETS_PATH, { + method: 'POST', + body: JSON.stringify( + getLogEntryAnomaliesDatasetsRequestPayloadRT.encode({ + data: { + sourceId, + timeRange: { + startTime, + endTime, + }, + }, + }) + ), + }); + + return decodeOrThrow(getLogEntryAnomaliesDatasetsSuccessReponsePayloadRT)(response); +}; diff --git a/x-pack/plugins/infra/public/pages/logs/log_entry_rate/service_calls/get_log_entry_rate.ts b/x-pack/plugins/infra/public/pages/logs/log_entry_rate/service_calls/get_log_entry_rate.ts index 794139385f467..77111d279309d 100644 --- a/x-pack/plugins/infra/public/pages/logs/log_entry_rate/service_calls/get_log_entry_rate.ts +++ b/x-pack/plugins/infra/public/pages/logs/log_entry_rate/service_calls/get_log_entry_rate.ts @@ -19,7 +19,8 @@ export const callGetLogEntryRateAPI = async ( sourceId: string, startTime: number, endTime: number, - bucketDuration: number + bucketDuration: number, + datasets?: string[] ) => { const response = await npStart.http.fetch(LOG_ANALYSIS_GET_LOG_ENTRY_RATE_PATH, { method: 'POST', @@ -32,6 +33,7 @@ export const callGetLogEntryRateAPI = async ( endTime, }, bucketDuration, + datasets, }, }) ), diff --git a/x-pack/plugins/infra/public/pages/logs/log_entry_rate/use_log_entry_anomalies_results.ts b/x-pack/plugins/infra/public/pages/logs/log_entry_rate/use_log_entry_anomalies_results.ts index cadb4c420c133..52632e54390a9 100644 --- a/x-pack/plugins/infra/public/pages/logs/log_entry_rate/use_log_entry_anomalies_results.ts +++ b/x-pack/plugins/infra/public/pages/logs/log_entry_rate/use_log_entry_anomalies_results.ts @@ -5,11 +5,17 @@ */ import { useMemo, useState, useCallback, useEffect, useReducer } from 'react'; - -import { LogEntryAnomaly } from '../../../../common/http_api'; -import { useTrackedPromise } from '../../../utils/use_tracked_promise'; +import { useMount } from 'react-use'; +import { useTrackedPromise, CanceledPromiseError } from '../../../utils/use_tracked_promise'; import { callGetLogEntryAnomaliesAPI } from './service_calls/get_log_entry_anomalies'; -import { Sort, Pagination, PaginationCursor } from '../../../../common/http_api/log_analysis'; +import { callGetLogEntryAnomaliesDatasetsAPI } from './service_calls/get_log_entry_anomalies_datasets'; +import { + Sort, + Pagination, + PaginationCursor, + GetLogEntryAnomaliesDatasetsSuccessResponsePayload, + LogEntryAnomaly, +} from '../../../../common/http_api/log_analysis'; export type SortOptions = Sort; export type PaginationOptions = Pick; @@ -19,6 +25,7 @@ export type FetchPreviousPage = () => void; export type ChangeSortOptions = (sortOptions: Sort) => void; export type ChangePaginationOptions = (paginationOptions: PaginationOptions) => void; export type LogEntryAnomalies = LogEntryAnomaly[]; +type LogEntryAnomaliesDatasets = GetLogEntryAnomaliesDatasetsSuccessResponsePayload['data']['datasets']; interface PaginationCursors { previousPageCursor: PaginationCursor; nextPageCursor: PaginationCursor; @@ -35,6 +42,7 @@ interface ReducerState { start: number; end: number; }; + filteredDatasets?: string[]; } type ReducerStateDefaults = Pick< @@ -49,7 +57,8 @@ type ReducerAction = | { type: 'fetchPreviousPage' } | { type: 'changeHasNextPage'; payload: { hasNextPage: boolean } } | { type: 'changeLastReceivedCursors'; payload: { lastReceivedCursors: PaginationCursors } } - | { type: 'changeTimeRange'; payload: { timeRange: { start: number; end: number } } }; + | { type: 'changeTimeRange'; payload: { timeRange: { start: number; end: number } } } + | { type: 'changeFilteredDatasets'; payload: { filteredDatasets?: string[] } }; const stateReducer = (state: ReducerState, action: ReducerAction): ReducerState => { const resetPagination = { @@ -101,6 +110,12 @@ const stateReducer = (state: ReducerState, action: ReducerAction): ReducerState ...resetPagination, ...action.payload, }; + case 'changeFilteredDatasets': + return { + ...state, + ...resetPagination, + ...action.payload, + }; default: return state; } @@ -122,18 +137,23 @@ export const useLogEntryAnomaliesResults = ({ sourceId, defaultSortOptions, defaultPaginationOptions, + onGetLogEntryAnomaliesDatasetsError, + filteredDatasets, }: { endTime: number; startTime: number; sourceId: string; defaultSortOptions: Sort; defaultPaginationOptions: Pick; + onGetLogEntryAnomaliesDatasetsError?: (error: Error) => void; + filteredDatasets?: string[]; }) => { const initStateReducer = (stateDefaults: ReducerStateDefaults): ReducerState => { return { ...stateDefaults, paginationOptions: defaultPaginationOptions, sortOptions: defaultSortOptions, + filteredDatasets, timeRange: { start: startTime, end: endTime, @@ -154,6 +174,7 @@ export const useLogEntryAnomaliesResults = ({ sortOptions, paginationOptions, paginationCursor, + filteredDatasets: queryFilteredDatasets, } = reducerState; return await callGetLogEntryAnomaliesAPI( sourceId, @@ -163,7 +184,8 @@ export const useLogEntryAnomaliesResults = ({ { ...paginationOptions, cursor: paginationCursor, - } + }, + queryFilteredDatasets ); }, onResolve: ({ data: { anomalies, paginationCursors: requestCursors, hasMoreEntries } }) => { @@ -192,6 +214,7 @@ export const useLogEntryAnomaliesResults = ({ reducerState.sortOptions, reducerState.paginationOptions, reducerState.paginationCursor, + reducerState.filteredDatasets, ] ); @@ -220,6 +243,14 @@ export const useLogEntryAnomaliesResults = ({ }); }, [startTime, endTime]); + // Selected datasets have changed + useEffect(() => { + dispatch({ + type: 'changeFilteredDatasets', + payload: { filteredDatasets }, + }); + }, [filteredDatasets]); + useEffect(() => { getLogEntryAnomalies(); }, [getLogEntryAnomalies]); @@ -246,10 +277,53 @@ export const useLogEntryAnomaliesResults = ({ [getLogEntryAnomaliesRequest.state] ); + // Anomalies datasets + const [logEntryAnomaliesDatasets, setLogEntryAnomaliesDatasets] = useState< + LogEntryAnomaliesDatasets + >([]); + + const [getLogEntryAnomaliesDatasetsRequest, getLogEntryAnomaliesDatasets] = useTrackedPromise( + { + cancelPreviousOn: 'creation', + createPromise: async () => { + return await callGetLogEntryAnomaliesDatasetsAPI(sourceId, startTime, endTime); + }, + onResolve: ({ data: { datasets } }) => { + setLogEntryAnomaliesDatasets(datasets); + }, + onReject: (error) => { + if ( + error instanceof Error && + !(error instanceof CanceledPromiseError) && + onGetLogEntryAnomaliesDatasetsError + ) { + onGetLogEntryAnomaliesDatasetsError(error); + } + }, + }, + [endTime, sourceId, startTime] + ); + + const isLoadingDatasets = useMemo(() => getLogEntryAnomaliesDatasetsRequest.state === 'pending', [ + getLogEntryAnomaliesDatasetsRequest.state, + ]); + + const hasFailedLoadingDatasets = useMemo( + () => getLogEntryAnomaliesDatasetsRequest.state === 'rejected', + [getLogEntryAnomaliesDatasetsRequest.state] + ); + + useMount(() => { + getLogEntryAnomaliesDatasets(); + }); + return { logEntryAnomalies, getLogEntryAnomalies, isLoadingLogEntryAnomalies, + isLoadingDatasets, + hasFailedLoadingDatasets, + datasets: logEntryAnomaliesDatasets, hasFailedLoadingLogEntryAnomalies, changeSortOptions, sortOptions: reducerState.sortOptions, diff --git a/x-pack/plugins/infra/public/pages/logs/log_entry_rate/use_log_entry_rate_results.ts b/x-pack/plugins/infra/public/pages/logs/log_entry_rate/use_log_entry_rate_results.ts index 1cd27c64af53f..a52dab58cb018 100644 --- a/x-pack/plugins/infra/public/pages/logs/log_entry_rate/use_log_entry_rate_results.ts +++ b/x-pack/plugins/infra/public/pages/logs/log_entry_rate/use_log_entry_rate_results.ts @@ -41,11 +41,13 @@ export const useLogEntryRateResults = ({ startTime, endTime, bucketDuration = 15 * 60 * 1000, + filteredDatasets, }: { sourceId: string; startTime: number; endTime: number; bucketDuration: number; + filteredDatasets?: string[]; }) => { const [logEntryRate, setLogEntryRate] = useState(null); @@ -53,7 +55,13 @@ export const useLogEntryRateResults = ({ { cancelPreviousOn: 'resolution', createPromise: async () => { - return await callGetLogEntryRateAPI(sourceId, startTime, endTime, bucketDuration); + return await callGetLogEntryRateAPI( + sourceId, + startTime, + endTime, + bucketDuration, + filteredDatasets + ); }, onResolve: ({ data }) => { setLogEntryRate({ @@ -68,7 +76,7 @@ export const useLogEntryRateResults = ({ setLogEntryRate(null); }, }, - [sourceId, startTime, endTime, bucketDuration] + [sourceId, startTime, endTime, bucketDuration, filteredDatasets] ); const isLoading = useMemo(() => getLogEntryRateRequest.state === 'pending', [ diff --git a/x-pack/plugins/infra/server/infra_server.ts b/x-pack/plugins/infra/server/infra_server.ts index 6596e07ebaca5..c080618f2a563 100644 --- a/x-pack/plugins/infra/server/infra_server.ts +++ b/x-pack/plugins/infra/server/infra_server.ts @@ -19,6 +19,7 @@ import { initValidateLogAnalysisDatasetsRoute, initValidateLogAnalysisIndicesRoute, initGetLogEntryAnomaliesRoute, + initGetLogEntryAnomaliesDatasetsRoute, } from './routes/log_analysis'; import { initMetricExplorerRoute } from './routes/metrics_explorer'; import { initMetadataRoute } from './routes/metadata'; @@ -53,6 +54,7 @@ export const initInfraServer = (libs: InfraBackendLibs) => { initGetLogEntryCategoryExamplesRoute(libs); initGetLogEntryRateRoute(libs); initGetLogEntryAnomaliesRoute(libs); + initGetLogEntryAnomaliesDatasetsRoute(libs); initSnapshotRoute(libs); initNodeDetailsRoute(libs); initSourceRoute(libs); diff --git a/x-pack/plugins/infra/server/lib/log_analysis/common.ts b/x-pack/plugins/infra/server/lib/log_analysis/common.ts index 0c0b0a0f19982..218281d875a46 100644 --- a/x-pack/plugins/infra/server/lib/log_analysis/common.ts +++ b/x-pack/plugins/infra/server/lib/log_analysis/common.ts @@ -4,10 +4,19 @@ * you may not use this file except in compliance with the Elastic License. */ -import type { MlAnomalyDetectors } from '../../types'; -import { startTracingSpan } from '../../../common/performance_tracing'; +import type { MlAnomalyDetectors, MlSystem } from '../../types'; import { NoLogAnalysisMlJobError } from './errors'; +import { + CompositeDatasetKey, + createLogEntryDatasetsQuery, + LogEntryDatasetBucket, + logEntryDatasetsResponseRT, +} from './queries/log_entry_data_sets'; +import { decodeOrThrow } from '../../../common/runtime_types'; +import { NoLogAnalysisResultsIndexError } from './errors'; +import { startTracingSpan, TracingSpan } from '../../../common/performance_tracing'; + export async function fetchMlJob(mlAnomalyDetectors: MlAnomalyDetectors, jobId: string) { const finalizeMlGetJobSpan = startTracingSpan('Fetch ml job from ES'); const { @@ -27,3 +36,63 @@ export async function fetchMlJob(mlAnomalyDetectors: MlAnomalyDetectors, jobId: }, }; } + +const COMPOSITE_AGGREGATION_BATCH_SIZE = 1000; + +// Finds datasets related to ML job ids +export async function getLogEntryDatasets( + mlSystem: MlSystem, + startTime: number, + endTime: number, + jobIds: string[] +) { + const finalizeLogEntryDatasetsSpan = startTracingSpan('get data sets'); + + let logEntryDatasetBuckets: LogEntryDatasetBucket[] = []; + let afterLatestBatchKey: CompositeDatasetKey | undefined; + let esSearchSpans: TracingSpan[] = []; + + while (true) { + const finalizeEsSearchSpan = startTracingSpan('fetch log entry dataset batch from ES'); + + const logEntryDatasetsResponse = decodeOrThrow(logEntryDatasetsResponseRT)( + await mlSystem.mlAnomalySearch( + createLogEntryDatasetsQuery( + jobIds, + startTime, + endTime, + COMPOSITE_AGGREGATION_BATCH_SIZE, + afterLatestBatchKey + ) + ) + ); + + if (logEntryDatasetsResponse._shards.total === 0) { + throw new NoLogAnalysisResultsIndexError( + `Failed to find ml indices for jobs: ${jobIds.join(', ')}.` + ); + } + + const { + after_key: afterKey, + buckets: latestBatchBuckets, + } = logEntryDatasetsResponse.aggregations.dataset_buckets; + + logEntryDatasetBuckets = [...logEntryDatasetBuckets, ...latestBatchBuckets]; + afterLatestBatchKey = afterKey; + esSearchSpans = [...esSearchSpans, finalizeEsSearchSpan()]; + + if (latestBatchBuckets.length < COMPOSITE_AGGREGATION_BATCH_SIZE) { + break; + } + } + + const logEntryDatasetsSpan = finalizeLogEntryDatasetsSpan(); + + return { + data: logEntryDatasetBuckets.map((logEntryDatasetBucket) => logEntryDatasetBucket.key.dataset), + timing: { + spans: [logEntryDatasetsSpan, ...esSearchSpans], + }, + }; +} diff --git a/x-pack/plugins/infra/server/lib/log_analysis/log_entry_anomalies.ts b/x-pack/plugins/infra/server/lib/log_analysis/log_entry_anomalies.ts index 12ae516564d66..950de4261bda0 100644 --- a/x-pack/plugins/infra/server/lib/log_analysis/log_entry_anomalies.ts +++ b/x-pack/plugins/infra/server/lib/log_analysis/log_entry_anomalies.ts @@ -7,15 +7,19 @@ import { RequestHandlerContext } from 'src/core/server'; import { InfraRequestHandlerContext } from '../../types'; import { TracingSpan, startTracingSpan } from '../../../common/performance_tracing'; -import { fetchMlJob } from './common'; +import { fetchMlJob, getLogEntryDatasets } from './common'; import { getJobId, logEntryCategoriesJobTypes, logEntryRateJobTypes, jobCustomSettingsRT, } from '../../../common/log_analysis'; -import { Sort, Pagination } from '../../../common/http_api/log_analysis'; -import type { MlSystem } from '../../types'; +import { + Sort, + Pagination, + GetLogEntryAnomaliesRequestPayload, +} from '../../../common/http_api/log_analysis'; +import type { MlSystem, MlAnomalyDetectors } from '../../types'; import { createLogEntryAnomaliesQuery, logEntryAnomaliesResponseRT } from './queries'; import { InsufficientAnomalyMlJobsConfigured, @@ -43,22 +47,13 @@ interface MappedAnomalyHit { categoryId?: string; } -export async function getLogEntryAnomalies( - context: RequestHandlerContext & { infra: Required }, +async function getCompatibleAnomaliesJobIds( + spaceId: string, sourceId: string, - startTime: number, - endTime: number, - sort: Sort, - pagination: Pagination + mlAnomalyDetectors: MlAnomalyDetectors ) { - const finalizeLogEntryAnomaliesSpan = startTracingSpan('get log entry anomalies'); - - const logRateJobId = getJobId(context.infra.spaceId, sourceId, logEntryRateJobTypes[0]); - const logCategoriesJobId = getJobId( - context.infra.spaceId, - sourceId, - logEntryCategoriesJobTypes[0] - ); + const logRateJobId = getJobId(spaceId, sourceId, logEntryRateJobTypes[0]); + const logCategoriesJobId = getJobId(spaceId, sourceId, logEntryCategoriesJobTypes[0]); const jobIds: string[] = []; let jobSpans: TracingSpan[] = []; @@ -66,7 +61,7 @@ export async function getLogEntryAnomalies( try { const { timing: { spans }, - } = await fetchMlJob(context.infra.mlAnomalyDetectors, logRateJobId); + } = await fetchMlJob(mlAnomalyDetectors, logRateJobId); jobIds.push(logRateJobId); jobSpans = [...jobSpans, ...spans]; } catch (e) { @@ -76,13 +71,39 @@ export async function getLogEntryAnomalies( try { const { timing: { spans }, - } = await fetchMlJob(context.infra.mlAnomalyDetectors, logCategoriesJobId); + } = await fetchMlJob(mlAnomalyDetectors, logCategoriesJobId); jobIds.push(logCategoriesJobId); jobSpans = [...jobSpans, ...spans]; } catch (e) { // Job wasn't found } + return { + jobIds, + timing: { spans: jobSpans }, + }; +} + +export async function getLogEntryAnomalies( + context: RequestHandlerContext & { infra: Required }, + sourceId: string, + startTime: number, + endTime: number, + sort: Sort, + pagination: Pagination, + datasets: GetLogEntryAnomaliesRequestPayload['data']['datasets'] +) { + const finalizeLogEntryAnomaliesSpan = startTracingSpan('get log entry anomalies'); + + const { + jobIds, + timing: { spans: jobSpans }, + } = await getCompatibleAnomaliesJobIds( + context.infra.spaceId, + sourceId, + context.infra.mlAnomalyDetectors + ); + if (jobIds.length === 0) { throw new InsufficientAnomalyMlJobsConfigured( 'Log rate or categorisation ML jobs need to be configured to search anomalies' @@ -100,16 +121,17 @@ export async function getLogEntryAnomalies( startTime, endTime, sort, - pagination + pagination, + datasets ); const data = anomalies.map((anomaly) => { const { jobId } = anomaly; - if (jobId === logRateJobId) { - return parseLogRateAnomalyResult(anomaly, logRateJobId); + if (!anomaly.categoryId) { + return parseLogRateAnomalyResult(anomaly, jobId); } else { - return parseCategoryAnomalyResult(anomaly, logCategoriesJobId); + return parseCategoryAnomalyResult(anomaly, jobId); } }); @@ -181,7 +203,8 @@ async function fetchLogEntryAnomalies( startTime: number, endTime: number, sort: Sort, - pagination: Pagination + pagination: Pagination, + datasets: GetLogEntryAnomaliesRequestPayload['data']['datasets'] ) { // We'll request 1 extra entry on top of our pageSize to determine if there are // more entries to be fetched. This avoids scenarios where the client side can't @@ -193,7 +216,7 @@ async function fetchLogEntryAnomalies( const results = decodeOrThrow(logEntryAnomaliesResponseRT)( await mlSystem.mlAnomalySearch( - createLogEntryAnomaliesQuery(jobIds, startTime, endTime, sort, expandedPagination) + createLogEntryAnomaliesQuery(jobIds, startTime, endTime, sort, expandedPagination, datasets) ) ); @@ -396,3 +419,43 @@ export async function fetchLogEntryExamples( }, }; } + +export async function getLogEntryAnomaliesDatasets( + context: { + infra: { + mlSystem: MlSystem; + mlAnomalyDetectors: MlAnomalyDetectors; + spaceId: string; + }; + }, + sourceId: string, + startTime: number, + endTime: number +) { + const { + jobIds, + timing: { spans: jobSpans }, + } = await getCompatibleAnomaliesJobIds( + context.infra.spaceId, + sourceId, + context.infra.mlAnomalyDetectors + ); + + if (jobIds.length === 0) { + throw new InsufficientAnomalyMlJobsConfigured( + 'Log rate or categorisation ML jobs need to be configured to search for anomaly datasets' + ); + } + + const { + data: datasets, + timing: { spans: datasetsSpans }, + } = await getLogEntryDatasets(context.infra.mlSystem, startTime, endTime, jobIds); + + return { + datasets, + timing: { + spans: [...jobSpans, ...datasetsSpans], + }, + }; +} diff --git a/x-pack/plugins/infra/server/lib/log_analysis/log_entry_categories_analysis.ts b/x-pack/plugins/infra/server/lib/log_analysis/log_entry_categories_analysis.ts index 6d00ba56e0e66..a455a03d936a5 100644 --- a/x-pack/plugins/infra/server/lib/log_analysis/log_entry_categories_analysis.ts +++ b/x-pack/plugins/infra/server/lib/log_analysis/log_entry_categories_analysis.ts @@ -12,7 +12,7 @@ import { jobCustomSettingsRT, logEntryCategoriesJobTypes, } from '../../../common/log_analysis'; -import { startTracingSpan, TracingSpan } from '../../../common/performance_tracing'; +import { startTracingSpan } from '../../../common/performance_tracing'; import { decodeOrThrow } from '../../../common/runtime_types'; import type { MlAnomalyDetectors, MlSystem } from '../../types'; import { @@ -33,20 +33,12 @@ import { createLogEntryCategoryHistogramsQuery, logEntryCategoryHistogramsResponseRT, } from './queries/log_entry_category_histograms'; -import { - CompositeDatasetKey, - createLogEntryDatasetsQuery, - LogEntryDatasetBucket, - logEntryDatasetsResponseRT, -} from './queries/log_entry_data_sets'; import { createTopLogEntryCategoriesQuery, topLogEntryCategoriesResponseRT, } from './queries/top_log_entry_categories'; import { InfraSource } from '../sources'; -import { fetchMlJob } from './common'; - -const COMPOSITE_AGGREGATION_BATCH_SIZE = 1000; +import { fetchMlJob, getLogEntryDatasets } from './common'; export async function getTopLogEntryCategories( context: { @@ -129,61 +121,15 @@ export async function getLogEntryCategoryDatasets( startTime: number, endTime: number ) { - const finalizeLogEntryDatasetsSpan = startTracingSpan('get data sets'); - const logEntryCategoriesCountJobId = getJobId( context.infra.spaceId, sourceId, logEntryCategoriesJobTypes[0] ); - let logEntryDatasetBuckets: LogEntryDatasetBucket[] = []; - let afterLatestBatchKey: CompositeDatasetKey | undefined; - let esSearchSpans: TracingSpan[] = []; - - while (true) { - const finalizeEsSearchSpan = startTracingSpan('fetch category dataset batch from ES'); - - const logEntryDatasetsResponse = decodeOrThrow(logEntryDatasetsResponseRT)( - await context.infra.mlSystem.mlAnomalySearch( - createLogEntryDatasetsQuery( - logEntryCategoriesCountJobId, - startTime, - endTime, - COMPOSITE_AGGREGATION_BATCH_SIZE, - afterLatestBatchKey - ) - ) - ); - - if (logEntryDatasetsResponse._shards.total === 0) { - throw new NoLogAnalysisResultsIndexError( - `Failed to find ml result index for job ${logEntryCategoriesCountJobId}.` - ); - } - - const { - after_key: afterKey, - buckets: latestBatchBuckets, - } = logEntryDatasetsResponse.aggregations.dataset_buckets; + const jobIds = [logEntryCategoriesCountJobId]; - logEntryDatasetBuckets = [...logEntryDatasetBuckets, ...latestBatchBuckets]; - afterLatestBatchKey = afterKey; - esSearchSpans = [...esSearchSpans, finalizeEsSearchSpan()]; - - if (latestBatchBuckets.length < COMPOSITE_AGGREGATION_BATCH_SIZE) { - break; - } - } - - const logEntryDatasetsSpan = finalizeLogEntryDatasetsSpan(); - - return { - data: logEntryDatasetBuckets.map((logEntryDatasetBucket) => logEntryDatasetBucket.key.dataset), - timing: { - spans: [logEntryDatasetsSpan, ...esSearchSpans], - }, - }; + return await getLogEntryDatasets(context.infra.mlSystem, startTime, endTime, jobIds); } export async function getLogEntryCategoryExamples( diff --git a/x-pack/plugins/infra/server/lib/log_analysis/log_entry_rate_analysis.ts b/x-pack/plugins/infra/server/lib/log_analysis/log_entry_rate_analysis.ts index 0323980dcd013..7bfc85ba78a0e 100644 --- a/x-pack/plugins/infra/server/lib/log_analysis/log_entry_rate_analysis.ts +++ b/x-pack/plugins/infra/server/lib/log_analysis/log_entry_rate_analysis.ts @@ -30,7 +30,8 @@ export async function getLogEntryRateBuckets( sourceId: string, startTime: number, endTime: number, - bucketDuration: number + bucketDuration: number, + datasets?: string[] ) { const logRateJobId = getJobId(context.infra.spaceId, sourceId, 'log-entry-rate'); let mlModelPlotBuckets: LogRateModelPlotBucket[] = []; @@ -44,7 +45,8 @@ export async function getLogEntryRateBuckets( endTime, bucketDuration, COMPOSITE_AGGREGATION_BATCH_SIZE, - afterLatestBatchKey + afterLatestBatchKey, + datasets ) ); diff --git a/x-pack/plugins/infra/server/lib/log_analysis/queries/common.ts b/x-pack/plugins/infra/server/lib/log_analysis/queries/common.ts index 87394028095de..63e39ef022392 100644 --- a/x-pack/plugins/infra/server/lib/log_analysis/queries/common.ts +++ b/x-pack/plugins/infra/server/lib/log_analysis/queries/common.ts @@ -55,3 +55,14 @@ export const createCategoryIdFilters = (categoryIds: number[]) => [ }, }, ]; + +export const createDatasetsFilters = (datasets?: string[]) => + datasets && datasets.length > 0 + ? [ + { + terms: { + partition_field_value: datasets, + }, + }, + ] + : []; diff --git a/x-pack/plugins/infra/server/lib/log_analysis/queries/log_entry_anomalies.ts b/x-pack/plugins/infra/server/lib/log_analysis/queries/log_entry_anomalies.ts index fc72776ea5cac..c722544c509aa 100644 --- a/x-pack/plugins/infra/server/lib/log_analysis/queries/log_entry_anomalies.ts +++ b/x-pack/plugins/infra/server/lib/log_analysis/queries/log_entry_anomalies.ts @@ -11,8 +11,13 @@ import { createTimeRangeFilters, createResultTypeFilters, defaultRequestParameters, + createDatasetsFilters, } from './common'; -import { Sort, Pagination } from '../../../../common/http_api/log_analysis'; +import { + Sort, + Pagination, + GetLogEntryAnomaliesRequestPayload, +} from '../../../../common/http_api/log_analysis'; // TODO: Reassess validity of this against ML docs const TIEBREAKER_FIELD = '_doc'; @@ -28,7 +33,8 @@ export const createLogEntryAnomaliesQuery = ( startTime: number, endTime: number, sort: Sort, - pagination: Pagination + pagination: Pagination, + datasets: GetLogEntryAnomaliesRequestPayload['data']['datasets'] ) => { const { field } = sort; const { pageSize } = pagination; @@ -37,6 +43,7 @@ export const createLogEntryAnomaliesQuery = ( ...createJobIdsFilters(jobIds), ...createTimeRangeFilters(startTime, endTime), ...createResultTypeFilters(['record']), + ...createDatasetsFilters(datasets), ]; const sourceFields = [ diff --git a/x-pack/plugins/infra/server/lib/log_analysis/queries/log_entry_data_sets.ts b/x-pack/plugins/infra/server/lib/log_analysis/queries/log_entry_data_sets.ts index dd22bedae8b2a..7627ccd8c4996 100644 --- a/x-pack/plugins/infra/server/lib/log_analysis/queries/log_entry_data_sets.ts +++ b/x-pack/plugins/infra/server/lib/log_analysis/queries/log_entry_data_sets.ts @@ -7,14 +7,14 @@ import * as rt from 'io-ts'; import { commonSearchSuccessResponseFieldsRT } from '../../../utils/elasticsearch_runtime_types'; import { - createJobIdFilters, + createJobIdsFilters, createResultTypeFilters, createTimeRangeFilters, defaultRequestParameters, } from './common'; export const createLogEntryDatasetsQuery = ( - logEntryAnalysisJobId: string, + jobIds: string[], startTime: number, endTime: number, size: number, @@ -25,7 +25,7 @@ export const createLogEntryDatasetsQuery = ( query: { bool: { filter: [ - ...createJobIdFilters(logEntryAnalysisJobId), + ...createJobIdsFilters(jobIds), ...createTimeRangeFilters(startTime, endTime), ...createResultTypeFilters(['model_plot']), ], diff --git a/x-pack/plugins/infra/server/lib/log_analysis/queries/log_entry_rate.ts b/x-pack/plugins/infra/server/lib/log_analysis/queries/log_entry_rate.ts index 8d9c586b2ef67..52edcf09cdfc2 100644 --- a/x-pack/plugins/infra/server/lib/log_analysis/queries/log_entry_rate.ts +++ b/x-pack/plugins/infra/server/lib/log_analysis/queries/log_entry_rate.ts @@ -10,6 +10,7 @@ import { createResultTypeFilters, createTimeRangeFilters, defaultRequestParameters, + createDatasetsFilters, } from './common'; export const createLogEntryRateQuery = ( @@ -18,7 +19,8 @@ export const createLogEntryRateQuery = ( endTime: number, bucketDuration: number, size: number, - afterKey?: CompositeTimestampPartitionKey + afterKey?: CompositeTimestampPartitionKey, + datasets?: string[] ) => ({ ...defaultRequestParameters, body: { @@ -28,6 +30,7 @@ export const createLogEntryRateQuery = ( ...createJobIdFilters(logRateJobId), ...createTimeRangeFilters(startTime, endTime), ...createResultTypeFilters(['model_plot', 'record']), + ...createDatasetsFilters(datasets), { term: { detector_index: { diff --git a/x-pack/plugins/infra/server/lib/log_analysis/queries/top_log_entry_categories.ts b/x-pack/plugins/infra/server/lib/log_analysis/queries/top_log_entry_categories.ts index 6fa7156240508..355dde9ec7c4a 100644 --- a/x-pack/plugins/infra/server/lib/log_analysis/queries/top_log_entry_categories.ts +++ b/x-pack/plugins/infra/server/lib/log_analysis/queries/top_log_entry_categories.ts @@ -11,6 +11,7 @@ import { createResultTypeFilters, createTimeRangeFilters, defaultRequestParameters, + createDatasetsFilters, } from './common'; export const createTopLogEntryCategoriesQuery = ( @@ -122,17 +123,6 @@ export const createTopLogEntryCategoriesQuery = ( size: 0, }); -const createDatasetsFilters = (datasets: string[]) => - datasets.length > 0 - ? [ - { - terms: { - partition_field_value: datasets, - }, - }, - ] - : []; - const metricAggregationRT = rt.type({ value: rt.union([rt.number, rt.null]), }); diff --git a/x-pack/plugins/infra/server/plugin.ts b/x-pack/plugins/infra/server/plugin.ts index 5b9fbc2829c72..7cd6383a9b2e5 100644 --- a/x-pack/plugins/infra/server/plugin.ts +++ b/x-pack/plugins/infra/server/plugin.ts @@ -152,12 +152,9 @@ export class InfraServerPlugin { core.http.registerRouteHandlerContext( 'infra', (context, request): InfraRequestHandlerContext => { - const mlSystem = - context.ml && - plugins.ml?.mlSystemProvider(context.ml?.mlClient.callAsCurrentUser, request); + const mlSystem = context.ml && plugins.ml?.mlSystemProvider(context.ml?.mlClient, request); const mlAnomalyDetectors = - context.ml && - plugins.ml?.anomalyDetectorsProvider(context.ml?.mlClient.callAsCurrentUser, request); + context.ml && plugins.ml?.anomalyDetectorsProvider(context.ml?.mlClient, request); const spaceId = plugins.spaces?.spacesService.getSpaceId(request) || 'default'; return { diff --git a/x-pack/plugins/infra/server/routes/log_analysis/results/index.ts b/x-pack/plugins/infra/server/routes/log_analysis/results/index.ts index cbd89db97236f..a01042616a872 100644 --- a/x-pack/plugins/infra/server/routes/log_analysis/results/index.ts +++ b/x-pack/plugins/infra/server/routes/log_analysis/results/index.ts @@ -10,3 +10,4 @@ export * from './log_entry_category_examples'; export * from './log_entry_rate'; export * from './log_entry_examples'; export * from './log_entry_anomalies'; +export * from './log_entry_anomalies_datasets'; diff --git a/x-pack/plugins/infra/server/routes/log_analysis/results/log_entry_anomalies.ts b/x-pack/plugins/infra/server/routes/log_analysis/results/log_entry_anomalies.ts index f4911658ea496..d79c9b9dd2c78 100644 --- a/x-pack/plugins/infra/server/routes/log_analysis/results/log_entry_anomalies.ts +++ b/x-pack/plugins/infra/server/routes/log_analysis/results/log_entry_anomalies.ts @@ -34,6 +34,7 @@ export const initGetLogEntryAnomaliesRoute = ({ framework }: InfraBackendLibs) = timeRange: { startTime, endTime }, sort: sortParam, pagination: paginationParam, + datasets, }, } = request.body; @@ -53,7 +54,8 @@ export const initGetLogEntryAnomaliesRoute = ({ framework }: InfraBackendLibs) = startTime, endTime, sort, - pagination + pagination, + datasets ); return response.ok({ diff --git a/x-pack/plugins/infra/server/routes/log_analysis/results/log_entry_anomalies_datasets.ts b/x-pack/plugins/infra/server/routes/log_analysis/results/log_entry_anomalies_datasets.ts new file mode 100644 index 0000000000000..d3d0862eee9aa --- /dev/null +++ b/x-pack/plugins/infra/server/routes/log_analysis/results/log_entry_anomalies_datasets.ts @@ -0,0 +1,74 @@ +/* + * 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 { + getLogEntryAnomaliesDatasetsRequestPayloadRT, + getLogEntryAnomaliesDatasetsSuccessReponsePayloadRT, + LOG_ANALYSIS_GET_LOG_ENTRY_ANOMALIES_DATASETS_PATH, +} from '../../../../common/http_api/log_analysis'; +import { createValidationFunction } from '../../../../common/runtime_types'; +import type { InfraBackendLibs } from '../../../lib/infra_types'; +import { + getLogEntryAnomaliesDatasets, + NoLogAnalysisResultsIndexError, +} from '../../../lib/log_analysis'; +import { assertHasInfraMlPlugins } from '../../../utils/request_context'; + +export const initGetLogEntryAnomaliesDatasetsRoute = ({ framework }: InfraBackendLibs) => { + framework.registerRoute( + { + method: 'post', + path: LOG_ANALYSIS_GET_LOG_ENTRY_ANOMALIES_DATASETS_PATH, + validate: { + body: createValidationFunction(getLogEntryAnomaliesDatasetsRequestPayloadRT), + }, + }, + framework.router.handleLegacyErrors(async (requestContext, request, response) => { + const { + data: { + sourceId, + timeRange: { startTime, endTime }, + }, + } = request.body; + + try { + assertHasInfraMlPlugins(requestContext); + + const { datasets, timing } = await getLogEntryAnomaliesDatasets( + requestContext, + sourceId, + startTime, + endTime + ); + + return response.ok({ + body: getLogEntryAnomaliesDatasetsSuccessReponsePayloadRT.encode({ + data: { + datasets, + }, + timing, + }), + }); + } catch (error) { + if (Boom.isBoom(error)) { + throw error; + } + + if (error instanceof NoLogAnalysisResultsIndexError) { + return response.notFound({ body: { message: error.message } }); + } + + return response.customError({ + statusCode: error.statusCode ?? 500, + body: { + message: error.message ?? 'An unexpected error occurred', + }, + }); + } + }) + ); +}; diff --git a/x-pack/plugins/infra/server/routes/log_analysis/results/log_entry_rate.ts b/x-pack/plugins/infra/server/routes/log_analysis/results/log_entry_rate.ts index ae86102980c16..3b05f6ed23aae 100644 --- a/x-pack/plugins/infra/server/routes/log_analysis/results/log_entry_rate.ts +++ b/x-pack/plugins/infra/server/routes/log_analysis/results/log_entry_rate.ts @@ -27,7 +27,7 @@ export const initGetLogEntryRateRoute = ({ framework }: InfraBackendLibs) => { }, framework.router.handleLegacyErrors(async (requestContext, request, response) => { const { - data: { sourceId, timeRange, bucketDuration }, + data: { sourceId, timeRange, bucketDuration, datasets }, } = request.body; try { @@ -38,7 +38,8 @@ export const initGetLogEntryRateRoute = ({ framework }: InfraBackendLibs) => { sourceId, timeRange.startTime, timeRange.endTime, - bucketDuration + bucketDuration, + datasets ); return response.ok({ diff --git a/x-pack/plugins/ingest_manager/README.md b/x-pack/plugins/ingest_manager/README.md index 1a19672331035..a523ddeb7c499 100644 --- a/x-pack/plugins/ingest_manager/README.md +++ b/x-pack/plugins/ingest_manager/README.md @@ -2,8 +2,8 @@ ## Plugin -- The plugin is disabled by default. See the TypeScript type for the [the available plugin configuration options](https://github.com/elastic/kibana/blob/master/x-pack/plugins/ingest_manager/common/types/index.ts#L9-L27) -- Setting `xpack.ingestManager.enabled=true` enables the plugin including the EPM and Fleet features. It also adds the `PACKAGE_CONFIG_API_ROUTES` and `AGENT_CONFIG_API_ROUTES` values in [`common/constants/routes.ts`](./common/constants/routes.ts) +- The plugin is enabled by default. See the TypeScript type for the [the available plugin configuration options](https://github.com/elastic/kibana/blob/master/x-pack/plugins/ingest_manager/common/types/index.ts#L9-L27) +- Adding `xpack.ingestManager.enabled=false` will disable the plugin including the EPM and Fleet features. It will also remove the `PACKAGE_CONFIG_API_ROUTES` and `AGENT_CONFIG_API_ROUTES` values in [`common/constants/routes.ts`](./common/constants/routes.ts) - Adding `--xpack.ingestManager.fleet.enabled=false` will disable the Fleet API & UI - [code for adding the routes](https://github.com/elastic/kibana/blob/1f27d349533b1c2865c10c45b2cf705d7416fb36/x-pack/plugins/ingest_manager/server/plugin.ts#L115-L133) - [Integration tests](server/integration_tests/router.test.ts) diff --git a/x-pack/plugins/ingest_manager/common/constants/routes.ts b/x-pack/plugins/ingest_manager/common/constants/routes.ts index 7c3b5a198571c..94265c3920922 100644 --- a/x-pack/plugins/ingest_manager/common/constants/routes.ts +++ b/x-pack/plugins/ingest_manager/common/constants/routes.ts @@ -11,6 +11,8 @@ export const PACKAGE_CONFIG_API_ROOT = `${API_ROOT}/package_configs`; export const AGENT_CONFIG_API_ROOT = `${API_ROOT}/agent_configs`; export const FLEET_API_ROOT = `${API_ROOT}/fleet`; +export const LIMITED_CONCURRENCY_ROUTE_TAG = 'ingest:limited-concurrency'; + // EPM API routes const EPM_PACKAGES_MANY = `${EPM_API_ROOT}/packages`; const EPM_PACKAGES_ONE = `${EPM_PACKAGES_MANY}/{pkgkey}`; diff --git a/x-pack/plugins/ingest_manager/common/types/index.ts b/x-pack/plugins/ingest_manager/common/types/index.ts index 0fce5cfa6226f..d7edc04a35799 100644 --- a/x-pack/plugins/ingest_manager/common/types/index.ts +++ b/x-pack/plugins/ingest_manager/common/types/index.ts @@ -13,6 +13,7 @@ export interface IngestManagerConfigType { enabled: boolean; tlsCheckDisabled: boolean; pollingRequestTimeout: number; + maxConcurrentConnections: number; kibana: { host?: string; ca_sha256?: string; diff --git a/x-pack/plugins/ingest_manager/common/types/models/epm.ts b/x-pack/plugins/ingest_manager/common/types/models/epm.ts index a34038d4fba04..ab6a6c73843c5 100644 --- a/x-pack/plugins/ingest_manager/common/types/models/epm.ts +++ b/x-pack/plugins/ingest_manager/common/types/models/epm.ts @@ -229,7 +229,8 @@ export type PackageInfo = Installable< >; export interface Installation extends SavedObjectAttributes { - installed: AssetReference[]; + installed_kibana: KibanaAssetReference[]; + installed_es: EsAssetReference[]; es_index_patterns: Record; name: string; version: string; @@ -246,19 +247,14 @@ export type NotInstalled = T & { status: InstallationStatus.notInstalled; }; -export type AssetReference = Pick & { - type: AssetType | IngestAssetType; -}; +export type AssetReference = KibanaAssetReference | EsAssetReference; -/** - * Types of assets which can be installed/removed - */ -export enum IngestAssetType { - IlmPolicy = 'ilm_policy', - IndexTemplate = 'index_template', - ComponentTemplate = 'component_template', - IngestPipeline = 'ingest_pipeline', -} +export type KibanaAssetReference = Pick & { + type: KibanaAssetType; +}; +export type EsAssetReference = Pick & { + type: ElasticsearchAssetType; +}; export enum DefaultPackages { system = 'system', diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/components/actions_menu.tsx b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/components/actions_menu.tsx index 86d191d4ff904..a71de4b60c08c 100644 --- a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/components/actions_menu.tsx +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/components/actions_menu.tsx @@ -85,7 +85,7 @@ export const AgentConfigActionMenu = memo<{ > , = () => { setIsEnrollmentFlyoutOpen(true)}> ) : null diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/components/list_layout.tsx b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/components/list_layout.tsx index 60cbc31081302..46190033d4d6b 100644 --- a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/components/list_layout.tsx +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/components/list_layout.tsx @@ -112,7 +112,7 @@ export const ListLayout: React.FunctionComponent<{}> = ({ children }) => { setIsEnrollmentFlyoutOpen(true)}> diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/setup_page/index.tsx b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/setup_page/index.tsx index e9c9ce0c513d2..ffd8591a642c1 100644 --- a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/setup_page/index.tsx +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/setup_page/index.tsx @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ import React, { useState } from 'react'; +import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; import { EuiPageBody, @@ -14,11 +15,39 @@ import { EuiTitle, EuiSpacer, EuiIcon, + EuiCallOut, + EuiFlexItem, + EuiFlexGroup, + EuiCode, + EuiCodeBlock, + EuiLink, } from '@elastic/eui'; import { useCore, sendPostFleetSetup } from '../../../hooks'; import { WithoutHeaderLayout } from '../../../layouts'; import { GetFleetStatusResponse } from '../../../types'; +export const RequirementItem: React.FunctionComponent<{ isMissing: boolean }> = ({ + isMissing, + children, +}) => { + return ( + + + + {isMissing ? ( + + ) : ( + + )} + + + + {children} + + + ); +}; + export const SetupPage: React.FunctionComponent<{ refresh: () => Promise; missingRequirements: GetFleetStatusResponse['missing_requirements']; @@ -26,8 +55,7 @@ export const SetupPage: React.FunctionComponent<{ const [isFormLoading, setIsFormLoading] = useState(false); const core = useCore(); - const onSubmit = async (e: React.FormEvent) => { - e.preventDefault(); + const onSubmit = async () => { setIsFormLoading(true); try { await sendPostFleetSetup({ forceRecreate: true }); @@ -38,84 +66,218 @@ export const SetupPage: React.FunctionComponent<{ } }; - const content = - missingRequirements.includes('tls_required') || - missingRequirements.includes('api_keys') || - missingRequirements.includes('encrypted_saved_object_encryption_key_required') ? ( - <> - - - - -

+ if ( + !missingRequirements.includes('tls_required') && + !missingRequirements.includes('api_keys') && + !missingRequirements.includes('encrypted_saved_object_encryption_key_required') + ) { + return ( + + + + + + + +

+ +

+
+ + + + + + + + + + + +
+
+
+ ); + } + + return ( + + + + -

-
- - +
+ , - }} + id="xpack.ingestManager.setupPage.missingRequirementsElasticsearchTitle" + defaultMessage="In your Elasticsearch configuration, enable:" /> - - - - ) : ( - <> - - - - -

+ + + + + + ), + securityFlag: xpack.security.enabled, + true: true, + }} + /> + + + xpack.security.authc.api_key.enabled, + true: true, + apiKeyLink: ( + + + + ), + }} /> -

-
- - + + + + {`xpack.security.enabled: true +xpack.security.authc.api_key.enabled: true`} + + + + + + + + ), + securityFlag: xpack.security.enabled, + tlsLink: ( + + + + ), + tlsFlag: xpack.ingestManager.fleet.tlsCheckDisabled, + true: true, + }} + /> + + + + + + + ), + keyFlag: xpack.encryptedSavedObjects.encryptionKey, + }} + /> + + + + {`xpack.security.enabled: true +xpack.encryptedSavedObjects.encryptionKey: "something_at_least_32_characters"`} + + + + + + ), + }} /> - - - - - - - - - - - - ); - - return ( - - - - {content} diff --git a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/overview/index.tsx b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/overview/index.tsx index f4b68f0c5107e..ea7ae093ee59a 100644 --- a/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/overview/index.tsx +++ b/x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/overview/index.tsx @@ -71,7 +71,7 @@ export const IngestManagerOverview: React.FunctionComponent = () => {

@@ -84,7 +84,7 @@ export const IngestManagerOverview: React.FunctionComponent = () => { setIsEnrollmentFlyoutOpen(true)}> diff --git a/x-pack/plugins/ingest_manager/public/plugin.ts b/x-pack/plugins/ingest_manager/public/plugin.ts index 172ad2df210c3..670e75f7a241b 100644 --- a/x-pack/plugins/ingest_manager/public/plugin.ts +++ b/x-pack/plugins/ingest_manager/public/plugin.ts @@ -39,7 +39,7 @@ export interface IngestManagerSetup {} */ export interface IngestManagerStart { registerPackageConfigComponent: typeof registerPackageConfigComponent; - success: Promise; + isInitialized: () => Promise; } export interface IngestManagerSetupDeps { @@ -100,27 +100,32 @@ export class IngestManagerPlugin } public async start(core: CoreStart): Promise { - let successPromise: IngestManagerStart['success']; - try { - 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 || 'Unknown permissions error'); - } - } catch (error) { - successPromise = Promise.reject(error); - } + let successPromise: ReturnType; return { - success: successPromise, + isInitialized: () => { + if (!successPromise) { + successPromise = Promise.resolve().then(async () => { + const permissionsResponse = await core.http.get( + appRoutesService.getCheckPermissionsPath() + ); + + if (permissionsResponse?.success) { + return core.http + .post(setupRouteService.getSetupPath()) + .then(({ isInitialized }) => + isInitialized + ? Promise.resolve(true) + : Promise.reject(new Error('Unknown setup error')) + ); + } else { + throw new Error(permissionsResponse?.error || 'Unknown permissions error'); + } + }); + } + + return successPromise; + }, registerPackageConfigComponent, }; } diff --git a/x-pack/plugins/ingest_manager/server/constants/index.ts b/x-pack/plugins/ingest_manager/server/constants/index.ts index d3c074ff2e8d0..ce81736f2e84f 100644 --- a/x-pack/plugins/ingest_manager/server/constants/index.ts +++ b/x-pack/plugins/ingest_manager/server/constants/index.ts @@ -15,6 +15,7 @@ export { AGENT_UPDATE_ACTIONS_INTERVAL_MS, INDEX_PATTERN_PLACEHOLDER_SUFFIX, // Routes + LIMITED_CONCURRENCY_ROUTE_TAG, PLUGIN_ID, EPM_API_ROUTES, DATA_STREAM_API_ROUTES, diff --git a/x-pack/plugins/ingest_manager/server/index.ts b/x-pack/plugins/ingest_manager/server/index.ts index 1823cc3561693..6c72218abc531 100644 --- a/x-pack/plugins/ingest_manager/server/index.ts +++ b/x-pack/plugins/ingest_manager/server/index.ts @@ -20,12 +20,13 @@ export const config = { fleet: true, }, schema: schema.object({ - enabled: schema.boolean({ defaultValue: false }), + enabled: schema.boolean({ defaultValue: true }), registryUrl: schema.maybe(schema.uri()), fleet: schema.object({ enabled: schema.boolean({ defaultValue: true }), tlsCheckDisabled: schema.boolean({ defaultValue: false }), pollingRequestTimeout: schema.number({ defaultValue: 60000 }), + maxConcurrentConnections: schema.number({ defaultValue: 0 }), kibana: schema.object({ host: schema.maybe(schema.string()), ca_sha256: schema.maybe(schema.string()), diff --git a/x-pack/plugins/ingest_manager/server/plugin.ts b/x-pack/plugins/ingest_manager/server/plugin.ts index e32533dc907b9..69af475886bb9 100644 --- a/x-pack/plugins/ingest_manager/server/plugin.ts +++ b/x-pack/plugins/ingest_manager/server/plugin.ts @@ -34,6 +34,7 @@ import { } from './constants'; import { registerSavedObjects, registerEncryptedSavedObjects } from './saved_objects'; import { + registerLimitedConcurrencyRoutes, registerEPMRoutes, registerPackageConfigRoutes, registerDataStreamRoutes, @@ -228,6 +229,9 @@ export class IngestManagerPlugin ); } } else { + // we currently only use this global interceptor if fleet is enabled + // since it would run this func on *every* req (other plugins, CSS, etc) + registerLimitedConcurrencyRoutes(core, config); registerAgentRoutes(router); registerEnrollmentApiKeyRoutes(router); registerInstallScriptRoutes({ 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 d7eec50eac3cf..b85d96186f233 100644 --- a/x-pack/plugins/ingest_manager/server/routes/agent/index.ts +++ b/x-pack/plugins/ingest_manager/server/routes/agent/index.ts @@ -10,7 +10,7 @@ */ import { IRouter } from 'src/core/server'; -import { PLUGIN_ID, AGENT_API_ROUTES } from '../../constants'; +import { PLUGIN_ID, AGENT_API_ROUTES, LIMITED_CONCURRENCY_ROUTE_TAG } from '../../constants'; import { GetAgentsRequestSchema, GetOneAgentRequestSchema, @@ -95,7 +95,7 @@ export const registerRoutes = (router: IRouter) => { { path: AGENT_API_ROUTES.ENROLL_PATTERN, validate: PostAgentEnrollRequestSchema, - options: { tags: [] }, + options: { tags: [LIMITED_CONCURRENCY_ROUTE_TAG] }, }, postAgentEnrollHandler ); @@ -105,7 +105,7 @@ export const registerRoutes = (router: IRouter) => { { path: AGENT_API_ROUTES.ACKS_PATTERN, validate: PostAgentAcksRequestSchema, - options: { tags: [] }, + options: { tags: [LIMITED_CONCURRENCY_ROUTE_TAG] }, }, postAgentAcksHandlerBuilder({ acknowledgeAgentActions: AgentService.acknowledgeAgentActions, diff --git a/x-pack/plugins/ingest_manager/server/routes/app/index.ts b/x-pack/plugins/ingest_manager/server/routes/app/index.ts index 9d666efc7e9ce..ce2bf6fcdaf17 100644 --- a/x-pack/plugins/ingest_manager/server/routes/app/index.ts +++ b/x-pack/plugins/ingest_manager/server/routes/app/index.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ import { IRouter, RequestHandler } from 'src/core/server'; -import { PLUGIN_ID, APP_API_ROUTES } from '../../constants'; +import { APP_API_ROUTES } from '../../constants'; import { appContextService } from '../../services'; import { CheckPermissionsResponse } from '../../../common'; @@ -37,7 +37,7 @@ export const registerRoutes = (router: IRouter) => { { path: APP_API_ROUTES.CHECK_PERMISSIONS_PATTERN, validate: {}, - options: { tags: [`access:${PLUGIN_ID}-read`] }, + options: { tags: [] }, }, getCheckPermissionsHandler ); diff --git a/x-pack/plugins/ingest_manager/server/routes/data_streams/handlers.ts b/x-pack/plugins/ingest_manager/server/routes/data_streams/handlers.ts index 2c65b08a68700..df37aeb27c75c 100644 --- a/x-pack/plugins/ingest_manager/server/routes/data_streams/handlers.ts +++ b/x-pack/plugins/ingest_manager/server/routes/data_streams/handlers.ts @@ -122,7 +122,7 @@ export const getListHandler: RequestHandler = async (context, request, response) if (pkg !== '' && pkgSavedObject.length > 0 && !packageMetadata[pkg]) { // then pick the dashboards from the package saved object const dashboards = - pkgSavedObject[0].attributes?.installed?.filter( + pkgSavedObject[0].attributes?.installed_kibana?.filter( (o) => o.type === KibanaAssetType.dashboard ) || []; // and then pick the human-readable titles from the dashboard saved objects diff --git a/x-pack/plugins/ingest_manager/server/routes/epm/handlers.ts b/x-pack/plugins/ingest_manager/server/routes/epm/handlers.ts index fe813f29b72e6..f54e61280b98a 100644 --- a/x-pack/plugins/ingest_manager/server/routes/epm/handlers.ts +++ b/x-pack/plugins/ingest_manager/server/routes/epm/handlers.ts @@ -5,6 +5,7 @@ */ import { TypeOf } from '@kbn/config-schema'; import { RequestHandler, CustomHttpResponseOptions } from 'src/core/server'; +import { appContextService } from '../../services'; import { GetInfoResponse, InstallPackageResponse, @@ -29,6 +30,7 @@ import { installPackage, removeInstallation, getLimitedPackages, + getInstallationObject, } from '../../services/epm/packages'; export const getCategoriesHandler: RequestHandler< @@ -146,10 +148,12 @@ export const getInfoHandler: RequestHandler> = async (context, request, response) => { + const logger = appContextService.getLogger(); + const savedObjectsClient = context.core.savedObjects.client; + const callCluster = context.core.elasticsearch.legacy.client.callAsCurrentUser; + const { pkgkey } = request.params; + const [pkgName, pkgVersion] = pkgkey.split('-'); try { - const { pkgkey } = request.params; - const savedObjectsClient = context.core.savedObjects.client; - const callCluster = context.core.elasticsearch.legacy.client.callAsCurrentUser; const res = await installPackage({ savedObjectsClient, pkgkey, @@ -161,6 +165,17 @@ export const installPackageHandler: RequestHandler { + test(`doesn't call registerOnPreAuth if maxConcurrentConnections is 0`, async () => { + const mockSetup = coreMock.createSetup(); + const mockConfig = { fleet: { maxConcurrentConnections: 0 } } as IngestManagerConfigType; + registerLimitedConcurrencyRoutes(mockSetup, mockConfig); + + expect(mockSetup.http.registerOnPreAuth).not.toHaveBeenCalled(); + }); + + test(`calls registerOnPreAuth once if maxConcurrentConnections is 1`, async () => { + const mockSetup = coreMock.createSetup(); + const mockConfig = { fleet: { maxConcurrentConnections: 1 } } as IngestManagerConfigType; + registerLimitedConcurrencyRoutes(mockSetup, mockConfig); + + expect(mockSetup.http.registerOnPreAuth).toHaveBeenCalledTimes(1); + }); + + test(`calls registerOnPreAuth once if maxConcurrentConnections is 1000`, async () => { + const mockSetup = coreMock.createSetup(); + const mockConfig = { fleet: { maxConcurrentConnections: 1000 } } as IngestManagerConfigType; + registerLimitedConcurrencyRoutes(mockSetup, mockConfig); + + expect(mockSetup.http.registerOnPreAuth).toHaveBeenCalledTimes(1); + }); +}); diff --git a/x-pack/plugins/ingest_manager/server/routes/limited_concurrency.ts b/x-pack/plugins/ingest_manager/server/routes/limited_concurrency.ts new file mode 100644 index 0000000000000..ec8e2f6c8d436 --- /dev/null +++ b/x-pack/plugins/ingest_manager/server/routes/limited_concurrency.ts @@ -0,0 +1,72 @@ +/* + * 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 { + CoreSetup, + KibanaRequest, + LifecycleResponseFactory, + OnPreAuthToolkit, +} from 'kibana/server'; +import { LIMITED_CONCURRENCY_ROUTE_TAG } from '../../common'; +import { IngestManagerConfigType } from '../index'; +class MaxCounter { + constructor(private readonly max: number = 1) {} + private counter = 0; + valueOf() { + return this.counter; + } + increase() { + if (this.counter < this.max) { + this.counter += 1; + } + } + decrease() { + if (this.counter > 0) { + this.counter -= 1; + } + } + lessThanMax() { + return this.counter < this.max; + } +} + +function shouldHandleRequest(request: KibanaRequest) { + const tags = request.route.options.tags; + return tags.includes(LIMITED_CONCURRENCY_ROUTE_TAG); +} + +export function registerLimitedConcurrencyRoutes(core: CoreSetup, config: IngestManagerConfigType) { + const max = config.fleet.maxConcurrentConnections; + if (!max) return; + + const counter = new MaxCounter(max); + core.http.registerOnPreAuth(function preAuthHandler( + request: KibanaRequest, + response: LifecycleResponseFactory, + toolkit: OnPreAuthToolkit + ) { + if (!shouldHandleRequest(request)) { + return toolkit.next(); + } + + if (!counter.lessThanMax()) { + return response.customError({ + body: 'Too Many Requests', + statusCode: 429, + }); + } + + counter.increase(); + + // requests.events.aborted$ has a bug (but has test which explicitly verifies) where it's fired even when the request completes + // https://github.com/elastic/kibana/pull/70495#issuecomment-656288766 + request.events.aborted$.toPromise().then(() => { + counter.decrease(); + }); + + return toolkit.next(); + }); +} diff --git a/x-pack/plugins/ingest_manager/server/saved_objects/index.ts b/x-pack/plugins/ingest_manager/server/saved_objects/index.ts index 4c58ac57a54a2..aa2b73194067a 100644 --- a/x-pack/plugins/ingest_manager/server/saved_objects/index.ts +++ b/x-pack/plugins/ingest_manager/server/saved_objects/index.ts @@ -249,7 +249,14 @@ const savedObjectTypes: { [key: string]: SavedObjectsType } = { enabled: false, type: 'object', }, - installed: { + installed_es: { + type: 'nested', + properties: { + id: { type: 'keyword' }, + type: { type: 'keyword' }, + }, + }, + installed_kibana: { type: 'nested', properties: { id: { type: 'keyword' }, diff --git a/x-pack/plugins/reporting/server/export_types/common/constants.ts b/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/ingest_pipeline/index.ts similarity index 69% rename from x-pack/plugins/reporting/server/export_types/common/constants.ts rename to x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/ingest_pipeline/index.ts index 76fab923978f8..6450f7303dd88 100644 --- a/x-pack/plugins/reporting/server/export_types/common/constants.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/ingest_pipeline/index.ts @@ -4,4 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -export const DEFAULT_PAGELOAD_SELECTOR = '.application'; +export { installPipelines } from './install'; + +export { deletePipelines, deletePipeline } from './remove'; diff --git a/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/ingest_pipeline/install.ts b/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/ingest_pipeline/install.ts index 0865ee5d59e57..878cecf644032 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/ingest_pipeline/install.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/ingest_pipeline/install.ts @@ -4,15 +4,16 @@ * you may not use this file except in compliance with the Elastic License. */ +import { SavedObjectsClientContract } from 'src/core/server'; import { - AssetReference, + EsAssetReference, Dataset, ElasticsearchAssetType, - IngestAssetType, RegistryPackage, } from '../../../../types'; import * as Registry from '../../registry'; import { CallESAsCurrentUser } from '../../../../types'; +import { saveInstalledEsRefs } from '../../packages/install'; interface RewriteSubstitution { source: string; @@ -23,12 +24,16 @@ interface RewriteSubstitution { export const installPipelines = async ( registryPackage: RegistryPackage, paths: string[], - callCluster: CallESAsCurrentUser + callCluster: CallESAsCurrentUser, + savedObjectsClient: SavedObjectsClientContract ) => { + // unlike other ES assets, pipeline names are versioned so after a template is updated + // it can be created pointing to the new template, without removing the old one and effecting data + // so do not remove the currently installed pipelines here const datasets = registryPackage.datasets; const pipelinePaths = paths.filter((path) => isPipeline(path)); if (datasets) { - const pipelines = datasets.reduce>>((acc, dataset) => { + const pipelines = datasets.reduce>>((acc, dataset) => { if (dataset.ingest_pipeline) { acc.push( installPipelinesForDataset({ @@ -41,7 +46,8 @@ export const installPipelines = async ( } return acc; }, []); - return Promise.all(pipelines).then((results) => results.flat()); + const pipelinesToSave = await Promise.all(pipelines).then((results) => results.flat()); + return saveInstalledEsRefs(savedObjectsClient, registryPackage.name, pipelinesToSave); } return []; }; @@ -77,7 +83,7 @@ export async function installPipelinesForDataset({ pkgVersion: string; paths: string[]; dataset: Dataset; -}): Promise { +}): Promise { const pipelinePaths = paths.filter((path) => isDatasetPipeline(path, dataset.path)); let pipelines: any[] = []; const substitutions: RewriteSubstitution[] = []; @@ -123,7 +129,7 @@ async function installPipeline({ }: { callCluster: CallESAsCurrentUser; pipeline: any; -}): Promise { +}): Promise { const callClusterParams: { method: string; path: string; @@ -146,7 +152,7 @@ async function installPipeline({ // which we could otherwise use. // See src/core/server/elasticsearch/api_types.ts for available endpoints. await callCluster('transport.request', callClusterParams); - return { id: pipeline.nameForInstallation, type: IngestAssetType.IngestPipeline }; + return { id: pipeline.nameForInstallation, type: ElasticsearchAssetType.ingestPipeline }; } const isDirectory = ({ path }: Registry.ArchiveEntry) => path.endsWith('/'); diff --git a/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/ingest_pipeline/remove.ts b/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/ingest_pipeline/remove.ts new file mode 100644 index 0000000000000..8be3a1beab392 --- /dev/null +++ b/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/ingest_pipeline/remove.ts @@ -0,0 +1,60 @@ +/* + * 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 { SavedObjectsClientContract } from 'src/core/server'; +import { appContextService } from '../../../'; +import { CallESAsCurrentUser, ElasticsearchAssetType } from '../../../../types'; +import { getInstallation } from '../../packages/get'; +import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../../../common'; + +export const deletePipelines = async ( + callCluster: CallESAsCurrentUser, + savedObjectsClient: SavedObjectsClientContract, + pkgName: string, + pkgVersion: string +) => { + const logger = appContextService.getLogger(); + const previousPipelinesPattern = `*-${pkgName}.*-${pkgVersion}`; + + try { + await deletePipeline(callCluster, previousPipelinesPattern); + } catch (e) { + logger.error(e); + } + try { + await deletePipelineRefs(savedObjectsClient, pkgName, pkgVersion); + } catch (e) { + logger.error(e); + } +}; + +export const deletePipelineRefs = async ( + savedObjectsClient: SavedObjectsClientContract, + pkgName: string, + pkgVersion: string +) => { + const installation = await getInstallation({ savedObjectsClient, pkgName }); + if (!installation) return; + const installedEsAssets = installation.installed_es; + const filteredAssets = installedEsAssets.filter(({ type, id }) => { + if (type !== ElasticsearchAssetType.ingestPipeline) return true; + if (!id.includes(pkgVersion)) return true; + return false; + }); + return savedObjectsClient.update(PACKAGES_SAVED_OBJECT_TYPE, pkgName, { + installed_es: filteredAssets, + }); +}; +export async function deletePipeline(callCluster: CallESAsCurrentUser, id: string): Promise { + // '*' shouldn't ever appear here, but it still would delete all ingest pipelines + if (id && id !== '*') { + try { + await callCluster('ingest.deletePipeline', { id }); + } catch (err) { + throw new Error(`error deleting pipeline ${id}`); + } + } +} diff --git a/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/install.ts b/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/install.ts index e14645bbbf5fb..436a6a1bdc55d 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/install.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/install.ts @@ -5,6 +5,7 @@ */ import Boom from 'boom'; +import { SavedObjectsClientContract } from 'src/core/server'; import { Dataset, RegistryPackage, @@ -17,13 +18,14 @@ import { Field, loadFieldsFromYaml, processFields } from '../../fields/field'; import { getPipelineNameForInstallation } from '../ingest_pipeline/install'; import { generateMappings, generateTemplateName, getTemplate } from './template'; import * as Registry from '../../registry'; +import { removeAssetsFromInstalledEsByType, saveInstalledEsRefs } from '../../packages/install'; export const installTemplates = async ( registryPackage: RegistryPackage, + isUpdate: boolean, callCluster: CallESAsCurrentUser, - pkgName: string, - pkgVersion: string, - paths: string[] + paths: string[], + savedObjectsClient: SavedObjectsClientContract ): Promise => { // install any pre-built index template assets, // atm, this is only the base package's global index templates @@ -31,6 +33,12 @@ export const installTemplates = async ( await installPreBuiltComponentTemplates(paths, callCluster); await installPreBuiltTemplates(paths, callCluster); + // remove package installation's references to index templates + await removeAssetsFromInstalledEsByType( + savedObjectsClient, + registryPackage.name, + ElasticsearchAssetType.indexTemplate + ); // build templates per dataset from yml files const datasets = registryPackage.datasets; if (datasets) { @@ -46,7 +54,17 @@ export const installTemplates = async ( }, []); const res = await Promise.all(installTemplatePromises); - return res.flat(); + const installedTemplates = res.flat(); + // get template refs to save + const installedTemplateRefs = installedTemplates.map((template) => ({ + id: template.templateName, + type: ElasticsearchAssetType.indexTemplate, + })); + + // add package installation's references to index templates + await saveInstalledEsRefs(savedObjectsClient, registryPackage.name, installedTemplateRefs); + + return installedTemplates; } return []; }; diff --git a/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/template.ts b/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/template.ts index 77ad96952269f..b907c735d2630 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/template.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/elasticsearch/template/template.ts @@ -326,9 +326,10 @@ export const updateCurrentWriteIndices = async ( callCluster: CallESAsCurrentUser, templates: TemplateRef[] ): Promise => { - if (!templates) return; + if (!templates.length) return; const allIndices = await queryIndicesFromTemplates(callCluster, templates); + if (!allIndices.length) return; return updateAllIndices(allIndices, callCluster); }; @@ -358,12 +359,12 @@ const getIndices = async ( method: 'GET', path: `/_data_stream/${templateName}-*`, }); - if (res.length) { - return res.map((datastream: any) => ({ - indexName: datastream.indices[datastream.indices.length - 1].index_name, - indexTemplate, - })); - } + const dataStreams = res.data_streams; + if (!dataStreams.length) return; + return dataStreams.map((dataStream: any) => ({ + indexName: dataStream.indices[dataStream.indices.length - 1].index_name, + indexTemplate, + })); }; const updateAllIndices = async ( diff --git a/x-pack/plugins/ingest_manager/server/services/epm/fields/field.test.ts b/x-pack/plugins/ingest_manager/server/services/epm/fields/field.test.ts index f0ff4c6125452..abd2ba777e516 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/fields/field.test.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/fields/field.test.ts @@ -269,6 +269,181 @@ describe('processFields', () => { expect(processFields(nested)).toEqual(nestedExpanded); }); + test('correctly handles properties of nested and object type fields together', () => { + const fields = [ + { + name: 'a', + type: 'object', + }, + { + name: 'a.b', + type: 'nested', + }, + { + name: 'a.b.c', + type: 'boolean', + }, + { + name: 'a.b.d', + type: 'keyword', + }, + ]; + + const fieldsExpanded = [ + { + name: 'a', + type: 'group', + fields: [ + { + name: 'b', + type: 'group-nested', + fields: [ + { + name: 'c', + type: 'boolean', + }, + { + name: 'd', + type: 'keyword', + }, + ], + }, + ], + }, + ]; + expect(processFields(fields)).toEqual(fieldsExpanded); + }); + + test('correctly handles properties of nested and object type fields in large depth', () => { + const fields = [ + { + name: 'a.h-object', + type: 'object', + dynamic: false, + }, + { + name: 'a.b-nested.c-nested', + type: 'nested', + }, + { + name: 'a.b-nested', + type: 'nested', + }, + { + name: 'a', + type: 'object', + }, + { + name: 'a.b-nested.d', + type: 'keyword', + }, + { + name: 'a.b-nested.c-nested.e', + type: 'boolean', + dynamic: true, + }, + { + name: 'a.b-nested.c-nested.f-object', + type: 'object', + }, + { + name: 'a.b-nested.c-nested.f-object.g', + type: 'keyword', + }, + ]; + + const fieldsExpanded = [ + { + name: 'a', + type: 'group', + fields: [ + { + name: 'h-object', + type: 'object', + dynamic: false, + }, + { + name: 'b-nested', + type: 'group-nested', + fields: [ + { + name: 'c-nested', + type: 'group-nested', + fields: [ + { + name: 'e', + type: 'boolean', + dynamic: true, + }, + { + name: 'f-object', + type: 'group', + fields: [ + { + name: 'g', + type: 'keyword', + }, + ], + }, + ], + }, + { + name: 'd', + type: 'keyword', + }, + ], + }, + ], + }, + ]; + expect(processFields(fields)).toEqual(fieldsExpanded); + }); + + test('correctly handles properties of nested and object type fields together in different order', () => { + const fields = [ + { + name: 'a.b.c', + type: 'boolean', + }, + { + name: 'a.b', + type: 'nested', + }, + { + name: 'a', + type: 'object', + }, + { + name: 'a.b.d', + type: 'keyword', + }, + ]; + + const fieldsExpanded = [ + { + name: 'a', + type: 'group', + fields: [ + { + name: 'b', + type: 'group-nested', + fields: [ + { + name: 'c', + type: 'boolean', + }, + { + name: 'd', + type: 'keyword', + }, + ], + }, + ], + }, + ]; + expect(processFields(fields)).toEqual(fieldsExpanded); + }); + test('correctly handles properties of nested type where nested top level comes second', () => { const nested = [ { diff --git a/x-pack/plugins/ingest_manager/server/services/epm/fields/field.ts b/x-pack/plugins/ingest_manager/server/services/epm/fields/field.ts index e7c0eca2a9613..a44e5e4221f9f 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/fields/field.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/fields/field.ts @@ -126,10 +126,21 @@ function dedupFields(fields: Fields): Fields { if ( // only merge if found is a group and field is object, nested, or group. // Or if found is object, or nested, and field is a group. - // This is to avoid merging two objects, or nested, or object with a nested. + // This is to avoid merging two objects, or two nested, or object with a nested. + + // we do not need to check for group-nested in this part because `field` will never have group-nested + // it can only exist on `found` (found.type === 'group' && (field.type === 'object' || field.type === 'nested' || field.type === 'group')) || - ((found.type === 'object' || found.type === 'nested') && field.type === 'group') + // as part of the loop we will be marking found.type as group-nested so found could be group-nested if it was + // already processed. If we had an explicit definition of nested, and it showed up before a descendant field: + // - name: a + // type: nested + // - name: a.b + // type: keyword + // then found.type will be nested and not group-nested because it won't have any fields yet until a.b is processed + ((found.type === 'object' || found.type === 'nested' || found.type === 'group-nested') && + field.type === 'group') ) { // if the new field has properties let's dedup and concat them with the already existing found variable in // the array @@ -148,10 +159,10 @@ function dedupFields(fields: Fields): Fields { // supposed to be `nested` for when the template is actually generated if (found.type === 'nested' || field.type === 'nested') { found.type = 'group-nested'; - } else { - // found was either `group` already or `object` so just set it to `group` + } else if (found.type === 'object') { found.type = 'group'; } + // found.type could be group-nested or group, in those cases just leave it } // we need to merge in other properties (like `dynamic`) that might exist Object.assign(found, importantFieldProps); diff --git a/x-pack/plugins/ingest_manager/server/services/epm/kibana/assets/install.ts b/x-pack/plugins/ingest_manager/server/services/epm/kibana/assets/install.ts new file mode 100644 index 0000000000000..2a743f244e64d --- /dev/null +++ b/x-pack/plugins/ingest_manager/server/services/epm/kibana/assets/install.ts @@ -0,0 +1,126 @@ +/* + * 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 { + SavedObject, + SavedObjectsBulkCreateObject, + SavedObjectsClientContract, +} from 'src/core/server'; +import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../../../common'; +import * as Registry from '../../registry'; +import { AssetType, KibanaAssetType, AssetReference } from '../../../../types'; +import { deleteKibanaSavedObjectsAssets } from '../../packages/remove'; +import { getInstallationObject, savedObjectTypes } from '../../packages'; +import { saveInstalledKibanaRefs } from '../../packages/install'; + +type SavedObjectToBe = Required & { type: AssetType }; +export type ArchiveAsset = Pick< + SavedObject, + 'id' | 'attributes' | 'migrationVersion' | 'references' +> & { + type: AssetType; +}; + +export async function getKibanaAsset(key: string) { + const buffer = Registry.getAsset(key); + + // cache values are buffers. convert to string / JSON + return JSON.parse(buffer.toString('utf8')); +} + +export function createSavedObjectKibanaAsset(asset: ArchiveAsset): SavedObjectToBe { + // convert that to an object + return { + type: asset.type, + id: asset.id, + attributes: asset.attributes, + references: asset.references || [], + migrationVersion: asset.migrationVersion || {}, + }; +} + +// TODO: make it an exhaustive list +// e.g. switch statement with cases for each enum key returning `never` for default case +export async function installKibanaAssets(options: { + savedObjectsClient: SavedObjectsClientContract; + pkgName: string; + paths: string[]; + isUpdate: boolean; +}): Promise { + const { savedObjectsClient, paths, pkgName, isUpdate } = options; + + if (isUpdate) { + // delete currently installed kibana saved objects and installation references + const installedPkg = await getInstallationObject({ savedObjectsClient, pkgName }); + const installedKibanaRefs = installedPkg?.attributes.installed_kibana; + + if (installedKibanaRefs?.length) { + await deleteKibanaSavedObjectsAssets(savedObjectsClient, installedKibanaRefs); + await deleteKibanaInstalledRefs(savedObjectsClient, pkgName, installedKibanaRefs); + } + } + + // install the new assets and save installation references + const kibanaAssetTypes = Object.values(KibanaAssetType); + const installationPromises = kibanaAssetTypes.map((assetType) => + installKibanaSavedObjects({ savedObjectsClient, assetType, paths }) + ); + // installKibanaSavedObjects returns AssetReference[], so .map creates AssetReference[][] + // call .flat to flatten into one dimensional array + const newInstalledKibanaAssets = await Promise.all(installationPromises).then((results) => + results.flat() + ); + await saveInstalledKibanaRefs(savedObjectsClient, pkgName, newInstalledKibanaAssets); + return newInstalledKibanaAssets; +} +export const deleteKibanaInstalledRefs = async ( + savedObjectsClient: SavedObjectsClientContract, + pkgName: string, + installedKibanaRefs: AssetReference[] +) => { + const installedAssetsToSave = installedKibanaRefs.filter(({ id, type }) => { + const assetType = type as AssetType; + return !savedObjectTypes.includes(assetType); + }); + + return savedObjectsClient.update(PACKAGES_SAVED_OBJECT_TYPE, pkgName, { + installed_kibana: installedAssetsToSave, + }); +}; + +async function installKibanaSavedObjects({ + savedObjectsClient, + assetType, + paths, +}: { + savedObjectsClient: SavedObjectsClientContract; + assetType: KibanaAssetType; + paths: string[]; +}) { + const isSameType = (path: string) => assetType === Registry.pathParts(path).type; + const pathsOfType = paths.filter((path) => isSameType(path)); + const kibanaAssets = await Promise.all(pathsOfType.map((path) => getKibanaAsset(path))); + const toBeSavedObjects = await Promise.all( + kibanaAssets.map((asset) => createSavedObjectKibanaAsset(asset)) + ); + + if (toBeSavedObjects.length === 0) { + return []; + } else { + const createResults = await savedObjectsClient.bulkCreate(toBeSavedObjects, { + overwrite: true, + }); + const createdObjects = createResults.saved_objects; + const installed = createdObjects.map(toAssetReference); + return installed; + } +} + +function toAssetReference({ id, type }: SavedObject) { + const reference: AssetReference = { id, type: type as KibanaAssetType }; + + return reference; +} diff --git a/x-pack/plugins/ingest_manager/server/services/epm/packages/get_objects.ts b/x-pack/plugins/ingest_manager/server/services/epm/packages/get_objects.ts deleted file mode 100644 index b623295c5e060..0000000000000 --- a/x-pack/plugins/ingest_manager/server/services/epm/packages/get_objects.ts +++ /dev/null @@ -1,32 +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 { SavedObject, SavedObjectsBulkCreateObject } from 'src/core/server'; -import { AssetType } from '../../../types'; -import * as Registry from '../registry'; - -type ArchiveAsset = Pick; -type SavedObjectToBe = Required & { type: AssetType }; - -export async function getObject(key: string) { - const buffer = Registry.getAsset(key); - - // cache values are buffers. convert to string / JSON - const json = buffer.toString('utf8'); - // convert that to an object - const asset: ArchiveAsset = JSON.parse(json); - - const { type, file } = Registry.pathParts(key); - const savedObject: SavedObjectToBe = { - type, - id: file.replace('.json', ''), - attributes: asset.attributes, - references: asset.references || [], - migrationVersion: asset.migrationVersion || {}, - }; - - return savedObject; -} diff --git a/x-pack/plugins/ingest_manager/server/services/epm/packages/index.ts b/x-pack/plugins/ingest_manager/server/services/epm/packages/index.ts index 4bb803dfaf912..57c4f77432455 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/packages/index.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/packages/index.ts @@ -23,7 +23,7 @@ export { SearchParams, } from './get'; -export { installKibanaAssets, installPackage, ensureInstalledPackage } from './install'; +export { installPackage, ensureInstalledPackage } from './install'; export { removeInstallation } from './remove'; type RequiredPackage = 'system' | 'endpoint'; diff --git a/x-pack/plugins/ingest_manager/server/services/epm/packages/install.ts b/x-pack/plugins/ingest_manager/server/services/epm/packages/install.ts index 910283549abdf..35c5b58a93710 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/packages/install.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/packages/install.ts @@ -4,27 +4,27 @@ * you may not use this file except in compliance with the Elastic License. */ -import { SavedObject, SavedObjectsClientContract } from 'src/core/server'; +import { SavedObjectsClientContract } from 'src/core/server'; import Boom from 'boom'; import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../constants'; import { AssetReference, Installation, - KibanaAssetType, CallESAsCurrentUser, DefaultPackages, + AssetType, + KibanaAssetReference, + EsAssetReference, ElasticsearchAssetType, - IngestAssetType, } from '../../../types'; import { installIndexPatterns } from '../kibana/index_pattern/install'; import * as Registry from '../registry'; -import { getObject } from './get_objects'; import { getInstallation, getInstallationObject, isRequiredPackage } from './index'; import { installTemplates } from '../elasticsearch/template/install'; import { generateESIndexPatterns } from '../elasticsearch/template/template'; -import { installPipelines } from '../elasticsearch/ingest_pipeline/install'; +import { installPipelines, deletePipelines } from '../elasticsearch/ingest_pipeline/'; import { installILMPolicy } from '../elasticsearch/ilm/install'; -import { deleteAssetsByType, deleteKibanaSavedObjectsAssets } from './remove'; +import { installKibanaAssets } from '../kibana/assets/install'; import { updateCurrentWriteIndices } from '../elasticsearch/template/template'; export async function installLatestPackage(options: { @@ -92,127 +92,113 @@ export async function installPackage(options: { const { savedObjectsClient, pkgkey, callCluster } = options; // TODO: change epm API to /packageName/version so we don't need to do this const [pkgName, pkgVersion] = pkgkey.split('-'); - const paths = await Registry.getArchiveInfo(pkgName, pkgVersion); - // see if some version of this package is already installed // TODO: calls to getInstallationObject, Registry.fetchInfo, and Registry.fetchFindLatestPackge // and be replaced by getPackageInfo after adjusting for it to not group/use archive assets - const installedPkg = await getInstallationObject({ savedObjectsClient, pkgName }); - const registryPackageInfo = await Registry.fetchInfo(pkgName, pkgVersion); const latestPackage = await Registry.fetchFindLatestPackage(pkgName); if (pkgVersion < latestPackage.version) throw Boom.badRequest('Cannot install or update to an out-of-date package'); + const paths = await Registry.getArchiveInfo(pkgName, pkgVersion); + const registryPackageInfo = await Registry.fetchInfo(pkgName, pkgVersion); + + // get the currently installed package + const installedPkg = await getInstallationObject({ savedObjectsClient, pkgName }); + const isUpdate = installedPkg && installedPkg.attributes.version < pkgVersion ? true : false; + const reinstall = pkgVersion === installedPkg?.attributes.version; const removable = !isRequiredPackage(pkgName); const { internal = false } = registryPackageInfo; + const toSaveESIndexPatterns = generateESIndexPatterns(registryPackageInfo.datasets); - // delete the previous version's installation's SO kibana assets before installing new ones - // in case some assets were removed in the new version - if (installedPkg) { - try { - await deleteKibanaSavedObjectsAssets(savedObjectsClient, installedPkg.attributes.installed); - } catch (err) { - // log these errors, some assets may not exist if deleted during a failed update - } - } - - const [installedKibanaAssets, installedPipelines] = await Promise.all([ - installKibanaAssets({ + // add the package installation to the saved object + if (!installedPkg) { + await createInstallation({ savedObjectsClient, pkgName, pkgVersion, - paths, - }), - installPipelines(registryPackageInfo, paths, callCluster), - // index patterns and ilm policies are not currently associated with a particular package - // so we do not save them in the package saved object state. - installIndexPatterns(savedObjectsClient, pkgName, pkgVersion), - // currenly only the base package has an ILM policy - // at some point ILM policies can be installed/modified - // per dataset and we should then save them - installILMPolicy(paths, callCluster), - ]); + internal, + removable, + installed_kibana: [], + installed_es: [], + toSaveESIndexPatterns, + }); + } - // install or update the templates + const installIndexPatternPromise = installIndexPatterns(savedObjectsClient, pkgName, pkgVersion); + const installKibanaAssetsPromise = installKibanaAssets({ + savedObjectsClient, + pkgName, + paths, + isUpdate, + }); + + // the rest of the installation must happen in sequential order + + // currently only the base package has an ILM policy + // at some point ILM policies can be installed/modified + // per dataset and we should then save them + await installILMPolicy(paths, callCluster); + + // installs versionized pipelines without removing currently installed ones + const installedPipelines = await installPipelines( + registryPackageInfo, + paths, + callCluster, + savedObjectsClient + ); + // install or update the templates referencing the newly installed pipelines const installedTemplates = await installTemplates( registryPackageInfo, + isUpdate, callCluster, - pkgName, - pkgVersion, - paths + paths, + savedObjectsClient ); - const toSaveESIndexPatterns = generateESIndexPatterns(registryPackageInfo.datasets); + // update current backing indices of each data stream + await updateCurrentWriteIndices(callCluster, installedTemplates); + + // if this is an update, delete the previous version's pipelines + if (installedPkg && !reinstall) { + await deletePipelines( + callCluster, + savedObjectsClient, + pkgName, + installedPkg.attributes.version + ); + } + + // update to newly installed version when all assets are successfully installed + if (isUpdate) await updateVersion(savedObjectsClient, pkgName, pkgVersion); // get template refs to save const installedTemplateRefs = installedTemplates.map((template) => ({ id: template.templateName, - type: IngestAssetType.IndexTemplate, + type: ElasticsearchAssetType.indexTemplate, })); - - if (installedPkg) { - // update current index for every index template created - await updateCurrentWriteIndices(callCluster, installedTemplates); - if (!reinstall) { - try { - // delete the previous version's installation's pipelines - // this must happen after the template is updated - await deleteAssetsByType({ - savedObjectsClient, - callCluster, - installedObjects: installedPkg.attributes.installed, - assetType: ElasticsearchAssetType.ingestPipeline, - }); - } catch (err) { - throw new Error(err.message); - } - } - } - const toSaveAssetRefs: AssetReference[] = [ - ...installedKibanaAssets, - ...installedPipelines, - ...installedTemplateRefs, - ]; - // Save references to installed assets in the package's saved object state - return saveInstallationReferences({ - savedObjectsClient, - pkgName, - pkgVersion, - internal, - removable, - toSaveAssetRefs, - toSaveESIndexPatterns, - }); -} - -// TODO: make it an exhaustive list -// e.g. switch statement with cases for each enum key returning `never` for default case -export async function installKibanaAssets(options: { - savedObjectsClient: SavedObjectsClientContract; - pkgName: string; - pkgVersion: string; - paths: string[]; -}) { - const { savedObjectsClient, paths } = options; - - // Only install Kibana assets during package installation. - const kibanaAssetTypes = Object.values(KibanaAssetType); - const installationPromises = kibanaAssetTypes.map(async (assetType) => - installKibanaSavedObjects({ savedObjectsClient, assetType, paths }) - ); - - // installKibanaSavedObjects returns AssetReference[], so .map creates AssetReference[][] - // call .flat to flatten into one dimensional array - return Promise.all(installationPromises).then((results) => results.flat()); + const [installedKibanaAssets] = await Promise.all([ + installKibanaAssetsPromise, + installIndexPatternPromise, + ]); + return [...installedKibanaAssets, ...installedPipelines, ...installedTemplateRefs]; } - -export async function saveInstallationReferences(options: { +const updateVersion = async ( + savedObjectsClient: SavedObjectsClientContract, + pkgName: string, + pkgVersion: string +) => { + return savedObjectsClient.update(PACKAGES_SAVED_OBJECT_TYPE, pkgName, { + version: pkgVersion, + }); +}; +export async function createInstallation(options: { savedObjectsClient: SavedObjectsClientContract; pkgName: string; pkgVersion: string; internal: boolean; removable: boolean; - toSaveAssetRefs: AssetReference[]; + installed_kibana: KibanaAssetReference[]; + installed_es: EsAssetReference[]; toSaveESIndexPatterns: Record; }) { const { @@ -221,14 +207,15 @@ export async function saveInstallationReferences(options: { pkgVersion, internal, removable, - toSaveAssetRefs, + installed_kibana: installedKibana, + installed_es: installedEs, toSaveESIndexPatterns, } = options; - await savedObjectsClient.create( PACKAGES_SAVED_OBJECT_TYPE, { - installed: toSaveAssetRefs, + installed_kibana: installedKibana, + installed_es: installedEs, es_index_patterns: toSaveESIndexPatterns, name: pkgName, version: pkgVersion, @@ -237,37 +224,46 @@ export async function saveInstallationReferences(options: { }, { id: pkgName, overwrite: true } ); - - return toSaveAssetRefs; + return [...installedKibana, ...installedEs]; } -async function installKibanaSavedObjects({ - savedObjectsClient, - assetType, - paths, -}: { - savedObjectsClient: SavedObjectsClientContract; - assetType: KibanaAssetType; - paths: string[]; -}) { - const isSameType = (path: string) => assetType === Registry.pathParts(path).type; - const pathsOfType = paths.filter((path) => isSameType(path)); - const toBeSavedObjects = await Promise.all(pathsOfType.map(getObject)); +export const saveInstalledKibanaRefs = async ( + savedObjectsClient: SavedObjectsClientContract, + pkgName: string, + installedAssets: AssetReference[] +) => { + await savedObjectsClient.update(PACKAGES_SAVED_OBJECT_TYPE, pkgName, { + installed_kibana: installedAssets, + }); + return installedAssets; +}; - if (toBeSavedObjects.length === 0) { - return []; - } else { - const createResults = await savedObjectsClient.bulkCreate(toBeSavedObjects, { - overwrite: true, - }); - const createdObjects = createResults.saved_objects; - const installed = createdObjects.map(toAssetReference); - return installed; - } -} +export const saveInstalledEsRefs = async ( + savedObjectsClient: SavedObjectsClientContract, + pkgName: string, + installedAssets: EsAssetReference[] +) => { + const installedPkg = await getInstallationObject({ savedObjectsClient, pkgName }); + const installedAssetsToSave = installedPkg?.attributes.installed_es.concat(installedAssets); + await savedObjectsClient.update(PACKAGES_SAVED_OBJECT_TYPE, pkgName, { + installed_es: installedAssetsToSave, + }); + return installedAssets; +}; -function toAssetReference({ id, type }: SavedObject) { - const reference: AssetReference = { id, type: type as KibanaAssetType }; +export const removeAssetsFromInstalledEsByType = async ( + savedObjectsClient: SavedObjectsClientContract, + pkgName: string, + assetType: AssetType +) => { + const installedPkg = await getInstallationObject({ savedObjectsClient, pkgName }); + const installedAssets = installedPkg?.attributes.installed_es; + if (!installedAssets?.length) return; + const installedAssetsToSave = installedAssets?.filter(({ id, type }) => { + return type !== assetType; + }); - return reference; -} + return savedObjectsClient.update(PACKAGES_SAVED_OBJECT_TYPE, pkgName, { + installed_es: installedAssetsToSave, + }); +}; diff --git a/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts b/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts index 94af672d8e29f..81bc5847e6c0e 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/packages/remove.ts @@ -10,8 +10,9 @@ import { PACKAGES_SAVED_OBJECT_TYPE, PACKAGE_CONFIG_SAVED_OBJECT_TYPE } from '.. import { AssetReference, AssetType, ElasticsearchAssetType } from '../../../types'; import { CallESAsCurrentUser } from '../../../types'; import { getInstallation, savedObjectTypes } from './index'; +import { deletePipeline } from '../elasticsearch/ingest_pipeline/'; import { installIndexPatterns } from '../kibana/index_pattern/install'; -import { packageConfigService } from '../..'; +import { packageConfigService, appContextService } from '../..'; export async function removeInstallation(options: { savedObjectsClient: SavedObjectsClientContract; @@ -25,7 +26,6 @@ export async function removeInstallation(options: { if (!installation) throw Boom.badRequest(`${pkgName} is not installed`); if (installation.removable === false) throw Boom.badRequest(`${pkgName} is installed by default and cannot be removed`); - const installedObjects = installation.installed || []; const { total } = await packageConfigService.list(savedObjectsClient, { kuery: `${PACKAGE_CONFIG_SAVED_OBJECT_TYPE}.package.name:${pkgName}`, @@ -38,48 +38,40 @@ export async function removeInstallation(options: { `unable to remove package with existing package config(s) in use by agent(s)` ); - // Delete the manager saved object with references to the asset objects - // could also update with [] or some other state - await savedObjectsClient.delete(PACKAGES_SAVED_OBJECT_TYPE, pkgName); - // recreate or delete index patterns when a package is uninstalled await installIndexPatterns(savedObjectsClient); - // Delete the installed asset - await deleteAssets(installedObjects, savedObjectsClient, callCluster); + // Delete the installed assets + const installedAssets = [...installation.installed_kibana, ...installation.installed_es]; + await deleteAssets(installedAssets, savedObjectsClient, callCluster); + + // Delete the manager saved object with references to the asset objects + // could also update with [] or some other state + await savedObjectsClient.delete(PACKAGES_SAVED_OBJECT_TYPE, pkgName); // successful delete's in SO client return {}. return something more useful - return installedObjects; + return installedAssets; } async function deleteAssets( installedObjects: AssetReference[], savedObjectsClient: SavedObjectsClientContract, callCluster: CallESAsCurrentUser ) { + const logger = appContextService.getLogger(); const deletePromises = installedObjects.map(async ({ id, type }) => { const assetType = type as AssetType; if (savedObjectTypes.includes(assetType)) { - savedObjectsClient.delete(assetType, id); + return savedObjectsClient.delete(assetType, id); } else if (assetType === ElasticsearchAssetType.ingestPipeline) { - deletePipeline(callCluster, id); + return deletePipeline(callCluster, id); } else if (assetType === ElasticsearchAssetType.indexTemplate) { - deleteTemplate(callCluster, id); + return deleteTemplate(callCluster, id); } }); try { await Promise.all([...deletePromises]); } catch (err) { - throw new Error(err.message); - } -} -async function deletePipeline(callCluster: CallESAsCurrentUser, id: string): Promise { - // '*' shouldn't ever appear here, but it still would delete all ingest pipelines - if (id && id !== '*') { - try { - await callCluster('ingest.deletePipeline', { id }); - } catch (err) { - throw new Error(`error deleting pipeline ${id}`); - } + logger.error(err); } } @@ -108,31 +100,14 @@ async function deleteTemplate(callCluster: CallESAsCurrentUser, name: string): P } } -export async function deleteAssetsByType({ - savedObjectsClient, - callCluster, - installedObjects, - assetType, -}: { - savedObjectsClient: SavedObjectsClientContract; - callCluster: CallESAsCurrentUser; - installedObjects: AssetReference[]; - assetType: ElasticsearchAssetType; -}) { - const toDelete = installedObjects.filter((asset) => asset.type === assetType); - try { - await deleteAssets(toDelete, savedObjectsClient, callCluster); - } catch (err) { - throw new Error(err.message); - } -} - export async function deleteKibanaSavedObjectsAssets( savedObjectsClient: SavedObjectsClientContract, installedObjects: AssetReference[] ) { + const logger = appContextService.getLogger(); const deletePromises = installedObjects.map(({ id, type }) => { const assetType = type as AssetType; + if (savedObjectTypes.includes(assetType)) { return savedObjectsClient.delete(assetType, id); } @@ -140,6 +115,6 @@ export async function deleteKibanaSavedObjectsAssets( try { await Promise.all(deletePromises); } catch (err) { - throw new Error('error deleting saved object asset'); + logger.warn(err); } } diff --git a/x-pack/plugins/ingest_manager/server/services/epm/registry/index.ts b/x-pack/plugins/ingest_manager/server/services/epm/registry/index.ts index ea906517f6dec..7fb13e5e671d0 100644 --- a/x-pack/plugins/ingest_manager/server/services/epm/registry/index.ts +++ b/x-pack/plugins/ingest_manager/server/services/epm/registry/index.ts @@ -21,6 +21,7 @@ import { ArchiveEntry, untarBuffer } from './extract'; import { fetchUrl, getResponse, getResponseStream } from './requests'; import { streamToBuffer } from './streams'; import { getRegistryUrl } from './registry_url'; +import { appContextService } from '../..'; export { ArchiveEntry } from './extract'; @@ -47,6 +48,10 @@ export async function fetchList(params?: SearchParams): Promise - + <> +
)} - - - - {(!emptyExpression || title) && ( - - - {title || - i18n.translate('xpack.lens.chartTitle.unsaved', { defaultMessage: 'Unsaved' })} - - - )} - - {children} - - - - +
+ + {(!emptyExpression || title) && ( + + + {title || + i18n.translate('xpack.lens.chartTitle.unsaved', { defaultMessage: 'Unsaved' })} + + + )} + + {children} + + + ); } diff --git a/x-pack/plugins/license_management/public/plugin.ts b/x-pack/plugins/license_management/public/plugin.ts index 2511337793fea..b99ea387121ee 100644 --- a/x-pack/plugins/license_management/public/plugin.ts +++ b/x-pack/plugins/license_management/public/plugin.ts @@ -7,7 +7,7 @@ import { first } from 'rxjs/operators'; import { CoreSetup, Plugin, PluginInitializerContext } from 'src/core/public'; import { TelemetryPluginStart } from '../../../../src/plugins/telemetry/public'; -import { ManagementSetup, ManagementSectionId } from '../../../../src/plugins/management/public'; +import { ManagementSetup } from '../../../../src/plugins/management/public'; import { LicensingPluginSetup } from '../../../plugins/licensing/public'; import { PLUGIN } from '../common/constants'; import { ClientConfigType } from './types'; @@ -50,7 +50,7 @@ export class LicenseManagementUIPlugin const { getStartServices } = coreSetup; const { management, licensing } = plugins; - management.sections.getSection(ManagementSectionId.Stack).registerApp({ + management.sections.section.stack.registerApp({ id: PLUGIN.id, title: PLUGIN.title, order: 0, diff --git a/x-pack/plugins/lists/server/routes/create_exception_list_item_route.ts b/x-pack/plugins/lists/server/routes/create_exception_list_item_route.ts index 375d25c6fa5f8..c331eeb4bd2d0 100644 --- a/x-pack/plugins/lists/server/routes/create_exception_list_item_route.ts +++ b/x-pack/plugins/lists/server/routes/create_exception_list_item_route.ts @@ -16,6 +16,7 @@ import { } from '../../common/schemas'; import { getExceptionListClient } from './utils/get_exception_list_client'; +import { endpointDisallowedFields } from './endpoint_disallowed_fields'; export const createExceptionListItemRoute = (router: IRouter): void => { router.post( @@ -70,6 +71,22 @@ export const createExceptionListItemRoute = (router: IRouter): void => { statusCode: 409, }); } else { + if (exceptionList.type === 'endpoint') { + for (const entry of entries) { + if (entry.type === 'list') { + return siemResponse.error({ + body: `cannot add exception item with entry of type "list" to endpoint exception list`, + statusCode: 400, + }); + } + if (endpointDisallowedFields.includes(entry.field)) { + return siemResponse.error({ + body: `cannot add endpoint exception item on field ${entry.field}`, + statusCode: 400, + }); + } + } + } const createdList = await exceptionLists.createExceptionListItem({ _tags, comments, diff --git a/x-pack/plugins/monitoring/server/alerts/enums.ts b/x-pack/plugins/lists/server/routes/endpoint_disallowed_fields.ts similarity index 57% rename from x-pack/plugins/monitoring/server/alerts/enums.ts rename to x-pack/plugins/lists/server/routes/endpoint_disallowed_fields.ts index ccff588743af1..cf3389351f61d 100644 --- a/x-pack/plugins/monitoring/server/alerts/enums.ts +++ b/x-pack/plugins/lists/server/routes/endpoint_disallowed_fields.ts @@ -4,13 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ -export enum AlertClusterStateState { - Green = 'green', - Red = 'red', - Yellow = 'yellow', -} - -export enum AlertCommonPerClusterMessageTokenType { - Time = 'time', - Link = 'link', -} +export const endpointDisallowedFields = [ + 'file.Ext.quarantine_path', + 'file.Ext.quarantine_result', + 'process.entity_id', + 'process.parent.entity_id', + 'process.ancestry', +]; diff --git a/x-pack/plugins/lists/server/services/exception_lists/utils.test.ts b/x-pack/plugins/lists/server/services/exception_lists/utils.test.ts index 9cc2aacd88458..6f0c5195f2025 100644 --- a/x-pack/plugins/lists/server/services/exception_lists/utils.test.ts +++ b/x-pack/plugins/lists/server/services/exception_lists/utils.test.ts @@ -6,7 +6,7 @@ import sinon from 'sinon'; import moment from 'moment'; -import { DATE_NOW, USER } from '../../../common/constants.mock'; +import { USER } from '../../../common/constants.mock'; import { isCommentEqual, @@ -16,8 +16,9 @@ import { } from './utils'; describe('utils', () => { - const anchor = '2020-06-17T20:34:51.337Z'; - const unix = moment(anchor).valueOf(); + const oldDate = '2020-03-17T20:34:51.337Z'; + const dateNow = '2020-06-17T20:34:51.337Z'; + const unix = moment(dateNow).valueOf(); let clock: sinon.SinonFakeTimers; beforeEach(() => { @@ -42,11 +43,11 @@ describe('utils', () => { test('it formats newly added comments', () => { const comments = transformUpdateCommentsToComments({ comments: [ - { comment: 'Im an old comment', created_at: DATE_NOW, created_by: 'lily' }, + { comment: 'Im an old comment', created_at: oldDate, created_by: 'bane' }, { comment: 'Im a new comment' }, ], existingComments: [ - { comment: 'Im an old comment', created_at: DATE_NOW, created_by: 'lily' }, + { comment: 'Im an old comment', created_at: oldDate, created_by: 'bane' }, ], user: 'lily', }); @@ -54,12 +55,12 @@ describe('utils', () => { expect(comments).toEqual([ { comment: 'Im an old comment', - created_at: anchor, - created_by: 'lily', + created_at: oldDate, + created_by: 'bane', }, { comment: 'Im a new comment', - created_at: anchor, + created_at: dateNow, created_by: 'lily', }, ]); @@ -68,12 +69,12 @@ describe('utils', () => { test('it formats multiple newly added comments', () => { const comments = transformUpdateCommentsToComments({ comments: [ - { comment: 'Im an old comment', created_at: DATE_NOW, created_by: 'lily' }, + { comment: 'Im an old comment', created_at: oldDate, created_by: 'lily' }, { comment: 'Im a new comment' }, { comment: 'Im another new comment' }, ], existingComments: [ - { comment: 'Im an old comment', created_at: DATE_NOW, created_by: 'lily' }, + { comment: 'Im an old comment', created_at: oldDate, created_by: 'lily' }, ], user: 'lily', }); @@ -81,17 +82,17 @@ describe('utils', () => { expect(comments).toEqual([ { comment: 'Im an old comment', - created_at: anchor, + created_at: oldDate, created_by: 'lily', }, { comment: 'Im a new comment', - created_at: anchor, + created_at: dateNow, created_by: 'lily', }, { comment: 'Im another new comment', - created_at: anchor, + created_at: dateNow, created_by: 'lily', }, ]); @@ -99,9 +100,9 @@ describe('utils', () => { test('it should not throw if comments match existing comments', () => { const comments = transformUpdateCommentsToComments({ - comments: [{ comment: 'Im an old comment', created_at: DATE_NOW, created_by: 'lily' }], + comments: [{ comment: 'Im an old comment', created_at: oldDate, created_by: 'lily' }], existingComments: [ - { comment: 'Im an old comment', created_at: DATE_NOW, created_by: 'lily' }, + { comment: 'Im an old comment', created_at: oldDate, created_by: 'lily' }, ], user: 'lily', }); @@ -109,7 +110,7 @@ describe('utils', () => { expect(comments).toEqual([ { comment: 'Im an old comment', - created_at: anchor, + created_at: oldDate, created_by: 'lily', }, ]); @@ -120,12 +121,12 @@ describe('utils', () => { comments: [ { comment: 'Im an old comment that is trying to be updated', - created_at: DATE_NOW, + created_at: oldDate, created_by: 'lily', }, ], existingComments: [ - { comment: 'Im an old comment', created_at: DATE_NOW, created_by: 'lily' }, + { comment: 'Im an old comment', created_at: oldDate, created_by: 'lily' }, ], user: 'lily', }); @@ -133,9 +134,9 @@ describe('utils', () => { expect(comments).toEqual([ { comment: 'Im an old comment that is trying to be updated', - created_at: DATE_NOW, + created_at: oldDate, created_by: 'lily', - updated_at: anchor, + updated_at: dateNow, updated_by: 'lily', }, ]); @@ -150,7 +151,7 @@ describe('utils', () => { }, ], existingComments: [ - { comment: 'Im an old comment', created_at: DATE_NOW, created_by: 'lily' }, + { comment: 'Im an old comment', created_at: oldDate, created_by: 'lily' }, ], user: 'lily', }) @@ -164,7 +165,7 @@ describe('utils', () => { transformUpdateCommentsToComments({ comments: [], existingComments: [ - { comment: 'Im an old comment', created_at: DATE_NOW, created_by: 'lily' }, + { comment: 'Im an old comment', created_at: oldDate, created_by: 'lily' }, ], user: 'lily', }) @@ -176,9 +177,9 @@ describe('utils', () => { test('it throws if user tries to update existing comment timestamp', () => { expect(() => transformUpdateCommentsToComments({ - comments: [{ comment: 'Im an old comment', created_at: anchor, created_by: 'lily' }], + comments: [{ comment: 'Im an old comment', created_at: dateNow, created_by: 'lily' }], existingComments: [ - { comment: 'Im an old comment', created_at: DATE_NOW, created_by: 'lily' }, + { comment: 'Im an old comment', created_at: oldDate, created_by: 'lily' }, ], user: 'bane', }) @@ -188,9 +189,9 @@ describe('utils', () => { test('it throws if user tries to update existing comment author', () => { expect(() => transformUpdateCommentsToComments({ - comments: [{ comment: 'Im an old comment', created_at: anchor, created_by: 'lily' }], + comments: [{ comment: 'Im an old comment', created_at: oldDate, created_by: 'lily' }], existingComments: [ - { comment: 'Im an old comment', created_at: DATE_NOW, created_by: 'me!' }, + { comment: 'Im an old comment', created_at: oldDate, created_by: 'me!' }, ], user: 'bane', }) @@ -203,12 +204,12 @@ describe('utils', () => { comments: [ { comment: 'Im an old comment that is trying to be updated', - created_at: DATE_NOW, + created_at: oldDate, created_by: 'lily', }, ], existingComments: [ - { comment: 'Im an old comment', created_at: DATE_NOW, created_by: 'lily' }, + { comment: 'Im an old comment', created_at: oldDate, created_by: 'lily' }, ], user: 'bane', }) @@ -220,10 +221,10 @@ describe('utils', () => { transformUpdateCommentsToComments({ comments: [ { comment: 'Im a new comment' }, - { comment: 'Im an old comment', created_at: DATE_NOW, created_by: 'lily' }, + { comment: 'Im an old comment', created_at: oldDate, created_by: 'lily' }, ], existingComments: [ - { comment: 'Im an old comment', created_at: DATE_NOW, created_by: 'lily' }, + { comment: 'Im an old comment', created_at: oldDate, created_by: 'lily' }, ], user: 'lily', }) @@ -236,7 +237,7 @@ describe('utils', () => { expect(() => transformUpdateCommentsToComments({ comments: [ - { comment: 'Im an old comment', created_at: DATE_NOW, created_by: 'lily' }, + { comment: 'Im an old comment', created_at: oldDate, created_by: 'lily' }, { comment: 'Im a new comment' }, ], existingComments: [], @@ -249,11 +250,11 @@ describe('utils', () => { expect(() => transformUpdateCommentsToComments({ comments: [ - { comment: 'Im an old comment', created_at: DATE_NOW, created_by: 'lily' }, + { comment: 'Im an old comment', created_at: oldDate, created_by: 'lily' }, { comment: ' ' }, ], existingComments: [ - { comment: 'Im an old comment', created_at: DATE_NOW, created_by: 'lily' }, + { comment: 'Im an old comment', created_at: oldDate, created_by: 'lily' }, ], user: 'lily', }) @@ -280,12 +281,12 @@ describe('utils', () => { expect(comments).toEqual([ { comment: 'Im a new comment', - created_at: anchor, + created_at: dateNow, created_by: 'lily', }, { comment: 'Im another new comment', - created_at: anchor, + created_at: dateNow, created_by: 'lily', }, ]); @@ -302,16 +303,16 @@ describe('utils', () => { }); describe('#transformUpdateComments', () => { - test('it updates comment and adds "updated_at" and "updated_by"', () => { + test('it updates comment and adds "updated_at" and "updated_by" if content differs', () => { const comments = transformUpdateComments({ comment: { comment: 'Im an old comment that is trying to be updated', - created_at: DATE_NOW, + created_at: oldDate, created_by: 'lily', }, existingComment: { comment: 'Im an old comment', - created_at: DATE_NOW, + created_at: oldDate, created_by: 'lily', }, user: 'lily', @@ -319,24 +320,46 @@ describe('utils', () => { expect(comments).toEqual({ comment: 'Im an old comment that is trying to be updated', - created_at: '2020-04-20T15:25:31.830Z', + created_at: oldDate, created_by: 'lily', - updated_at: anchor, + updated_at: dateNow, updated_by: 'lily', }); }); + test('it does not update comment and add "updated_at" and "updated_by" if content is the same', () => { + const comments = transformUpdateComments({ + comment: { + comment: 'Im an old comment ', + created_at: oldDate, + created_by: 'lily', + }, + existingComment: { + comment: 'Im an old comment', + created_at: oldDate, + created_by: 'lily', + }, + user: 'lily', + }); + + expect(comments).toEqual({ + comment: 'Im an old comment', + created_at: oldDate, + created_by: 'lily', + }); + }); + test('it throws if user tries to update an existing comment that is not their own', () => { expect(() => transformUpdateComments({ comment: { comment: 'Im an old comment that is trying to be updated', - created_at: DATE_NOW, + created_at: oldDate, created_by: 'lily', }, existingComment: { comment: 'Im an old comment', - created_at: DATE_NOW, + created_at: oldDate, created_by: 'lily', }, user: 'bane', @@ -348,13 +371,13 @@ describe('utils', () => { expect(() => transformUpdateComments({ comment: { - comment: 'Im an old comment that is trying to be updated', - created_at: anchor, + comment: 'Im an old comment', + created_at: dateNow, created_by: 'lily', }, existingComment: { comment: 'Im an old comment', - created_at: DATE_NOW, + created_at: oldDate, created_by: 'lily', }, user: 'lily', @@ -368,12 +391,12 @@ describe('utils', () => { const result = isCommentEqual( { comment: 'some old comment', - created_at: DATE_NOW, + created_at: oldDate, created_by: USER, }, { comment: 'some older comment', - created_at: DATE_NOW, + created_at: oldDate, created_by: USER, } ); @@ -385,12 +408,12 @@ describe('utils', () => { const result = isCommentEqual( { comment: 'some old comment', - created_at: DATE_NOW, + created_at: oldDate, created_by: USER, }, { comment: 'some old comment', - created_at: anchor, + created_at: dateNow, created_by: USER, } ); @@ -402,12 +425,12 @@ describe('utils', () => { const result = isCommentEqual( { comment: 'some old comment', - created_at: DATE_NOW, + created_at: oldDate, created_by: USER, }, { comment: 'some old comment', - created_at: DATE_NOW, + created_at: oldDate, created_by: 'lily', } ); @@ -419,11 +442,11 @@ describe('utils', () => { const result = isCommentEqual( { comment: 'some old comment', - created_at: DATE_NOW, + created_at: oldDate, created_by: USER, }, { - created_at: DATE_NOW, + created_at: oldDate, created_by: USER, // Disabling to assure that order doesn't matter // eslint-disable-next-line sort-keys diff --git a/x-pack/plugins/lists/server/services/exception_lists/utils.ts b/x-pack/plugins/lists/server/services/exception_lists/utils.ts index ad1e1a3439d7c..3ef2c337e80b6 100644 --- a/x-pack/plugins/lists/server/services/exception_lists/utils.ts +++ b/x-pack/plugins/lists/server/services/exception_lists/utils.ts @@ -316,13 +316,15 @@ export const transformUpdateCommentsToComments = ({ 'When trying to update a comment, "created_at" and "created_by" must be present', 403 ); - } else if (commentsSchema.is(c) && existingComment == null) { + } else if (existingComment == null && commentsSchema.is(c)) { throw new ErrorWithStatusCode('Only new comments may be added', 403); } else if ( commentsSchema.is(c) && existingComment != null && - !isCommentEqual(c, existingComment) + isCommentEqual(c, existingComment) ) { + return existingComment; + } else if (commentsSchema.is(c) && existingComment != null) { return transformUpdateComments({ comment: c, existingComment, user }); } else { return transformCreateCommentsToComments({ comments: [c], user }) ?? []; @@ -347,14 +349,17 @@ export const transformUpdateComments = ({ throw new ErrorWithStatusCode('Unable to update comment', 403); } else if (comment.comment.trim().length === 0) { throw new ErrorWithStatusCode('Empty comments not allowed', 403); - } else { + } else if (comment.comment.trim() !== existingComment.comment) { const dateNow = new Date().toISOString(); return { - ...comment, + ...existingComment, + comment: comment.comment, updated_at: dateNow, updated_by: user, }; + } else { + return existingComment; } }; diff --git a/x-pack/plugins/logstash/public/plugin.ts b/x-pack/plugins/logstash/public/plugin.ts index ade6abdb63f43..59f92ee0a7ffc 100644 --- a/x-pack/plugins/logstash/public/plugin.ts +++ b/x-pack/plugins/logstash/public/plugin.ts @@ -14,7 +14,7 @@ import { HomePublicPluginSetup, FeatureCatalogueCategory, } from '../../../../src/plugins/home/public'; -import { ManagementSetup, ManagementSectionId } from '../../../../src/plugins/management/public'; +import { ManagementSetup } from '../../../../src/plugins/management/public'; import { LicensingPluginSetup } from '../../licensing/public'; // @ts-ignore @@ -35,22 +35,20 @@ export class LogstashPlugin implements Plugin { map((license) => new LogstashLicenseService(license)) ); - const managementApp = plugins.management.sections - .getSection(ManagementSectionId.Ingest) - .registerApp({ - id: 'pipelines', - title: i18n.translate('xpack.logstash.managementSection.pipelinesTitle', { - defaultMessage: 'Logstash Pipelines', - }), - order: 1, - mount: async (params) => { - const [coreStart] = await core.getStartServices(); - const { renderApp } = await import('./application'); - const isMonitoringEnabled = 'monitoring' in plugins; + const managementApp = plugins.management.sections.section.ingest.registerApp({ + id: 'pipelines', + title: i18n.translate('xpack.logstash.managementSection.pipelinesTitle', { + defaultMessage: 'Logstash Pipelines', + }), + order: 1, + mount: async (params) => { + const [coreStart] = await core.getStartServices(); + const { renderApp } = await import('./application'); + const isMonitoringEnabled = 'monitoring' in plugins; - return renderApp(coreStart, params, isMonitoringEnabled, logstashLicense$); - }, - }); + return renderApp(coreStart, params, isMonitoringEnabled, logstashLicense$); + }, + }); this.licenseSubscription = logstashLicense$.subscribe((license: any) => { if (license.enableLinks) { diff --git a/x-pack/plugins/maps/common/constants.ts b/x-pack/plugins/maps/common/constants.ts index 98464427cc348..cf67ac4dd999f 100644 --- a/x-pack/plugins/maps/common/constants.ts +++ b/x-pack/plugins/maps/common/constants.ts @@ -90,7 +90,7 @@ export const DECIMAL_DEGREES_PRECISION = 5; // meters precision export const ZOOM_PRECISION = 2; export const DEFAULT_MAX_RESULT_WINDOW = 10000; export const DEFAULT_MAX_INNER_RESULT_WINDOW = 100; -export const DEFAULT_MAX_BUCKETS_LIMIT = 10000; +export const DEFAULT_MAX_BUCKETS_LIMIT = 65535; export const FEATURE_ID_PROPERTY_NAME = '__kbn__feature_id__'; export const FEATURE_VISIBLE_PROPERTY_NAME = '__kbn_isvisibleduetojoin__'; diff --git a/x-pack/plugins/maps/common/descriptor_types/data_request_descriptor_types.ts b/x-pack/plugins/maps/common/descriptor_types/data_request_descriptor_types.ts index 1bd8c5401eb1d..35b33da12d384 100644 --- a/x-pack/plugins/maps/common/descriptor_types/data_request_descriptor_types.ts +++ b/x-pack/plugins/maps/common/descriptor_types/data_request_descriptor_types.ts @@ -5,7 +5,7 @@ */ /* eslint-disable @typescript-eslint/consistent-type-definitions */ -import { RENDER_AS, SORT_ORDER, SCALING_TYPES, SOURCE_TYPES } from '../constants'; +import { RENDER_AS, SORT_ORDER, SCALING_TYPES } from '../constants'; import { MapExtent, MapQuery } from './map_descriptor'; import { Filter, TimeRange } from '../../../../../src/plugins/data/common'; @@ -26,12 +26,10 @@ type ESSearchSourceSyncMeta = { scalingType: SCALING_TYPES; topHitsSplitField: string; topHitsSize: number; - sourceType: SOURCE_TYPES.ES_SEARCH; }; type ESGeoGridSourceSyncMeta = { requestType: RENDER_AS; - sourceType: SOURCE_TYPES.ES_GEO_GRID; }; export type VectorSourceSyncMeta = ESSearchSourceSyncMeta | ESGeoGridSourceSyncMeta | null; diff --git a/x-pack/plugins/maps/common/descriptor_types/style_property_descriptor_types.ts b/x-pack/plugins/maps/common/descriptor_types/style_property_descriptor_types.ts index 4846054ca26cb..ce6539c9c4520 100644 --- a/x-pack/plugins/maps/common/descriptor_types/style_property_descriptor_types.ts +++ b/x-pack/plugins/maps/common/descriptor_types/style_property_descriptor_types.ts @@ -95,7 +95,7 @@ export type ColorStylePropertyDescriptor = | ColorDynamicStylePropertyDescriptor; export type IconDynamicOptions = { - iconPaletteId?: string; + iconPaletteId: string | null; customIconStops?: IconStop[]; useCustomIconMap?: boolean; field?: StylePropertyField; diff --git a/x-pack/plugins/maps/config.ts b/x-pack/plugins/maps/config.ts index 8bb0b7551b0e1..b97c09d9b86ba 100644 --- a/x-pack/plugins/maps/config.ts +++ b/x-pack/plugins/maps/config.ts @@ -11,7 +11,6 @@ export interface MapsConfigType { showMapVisualizationTypes: boolean; showMapsInspectorAdapter: boolean; preserveDrawingBuffer: boolean; - enableVectorTiles: boolean; } export const configSchema = schema.object({ @@ -21,8 +20,6 @@ export const configSchema = schema.object({ showMapsInspectorAdapter: schema.boolean({ defaultValue: false }), // flag used in functional testing preserveDrawingBuffer: schema.boolean({ defaultValue: false }), - // flag used to enable/disable vector-tiles - enableVectorTiles: schema.boolean({ defaultValue: false }), }); export type MapsXPackConfig = TypeOf; diff --git a/x-pack/plugins/maps/public/api/index.ts b/x-pack/plugins/maps/public/api/index.ts index 8b45d31b41d44..ec5aa124fb7f9 100644 --- a/x-pack/plugins/maps/public/api/index.ts +++ b/x-pack/plugins/maps/public/api/index.ts @@ -5,3 +5,5 @@ */ export { MapsStartApi } from './start_api'; +export { createSecurityLayerDescriptors } from './create_security_layer_descriptors'; +export { registerLayerWizard, registerSource } from './register'; diff --git a/x-pack/plugins/maps/public/api/register.ts b/x-pack/plugins/maps/public/api/register.ts new file mode 100644 index 0000000000000..4846b6a198c71 --- /dev/null +++ b/x-pack/plugins/maps/public/api/register.ts @@ -0,0 +1,19 @@ +/* + * 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 { SourceRegistryEntry } from '../classes/sources/source_registry'; +import { LayerWizard } from '../classes/layers/layer_wizard_registry'; +import { lazyLoadMapModules } from '../lazy_load_bundle'; + +export async function registerLayerWizard(layerWizard: LayerWizard): Promise { + const mapModules = await lazyLoadMapModules(); + return mapModules.registerLayerWizard(layerWizard); +} + +export async function registerSource(entry: SourceRegistryEntry): Promise { + const mapModules = await lazyLoadMapModules(); + return mapModules.registerSource(entry); +} diff --git a/x-pack/plugins/maps/public/api/start_api.ts b/x-pack/plugins/maps/public/api/start_api.ts index d45b0df63c839..32db3bc771a3b 100644 --- a/x-pack/plugins/maps/public/api/start_api.ts +++ b/x-pack/plugins/maps/public/api/start_api.ts @@ -5,10 +5,14 @@ */ import { LayerDescriptor } from '../../common/descriptor_types'; +import { SourceRegistryEntry } from '../classes/sources/source_registry'; +import { LayerWizard } from '../classes/layers/layer_wizard_registry'; export interface MapsStartApi { createSecurityLayerDescriptors: ( indexPatternId: string, indexPatternTitle: string ) => Promise; + registerLayerWizard(layerWizard: LayerWizard): Promise; + registerSource(entry: SourceRegistryEntry): Promise; } diff --git a/x-pack/plugins/maps/public/classes/fields/es_agg_field.ts b/x-pack/plugins/maps/public/classes/fields/es_agg_field.ts index e0f5c79f1d427..15779d22681c0 100644 --- a/x-pack/plugins/maps/public/classes/fields/es_agg_field.ts +++ b/x-pack/plugins/maps/public/classes/fields/es_agg_field.ts @@ -17,6 +17,8 @@ import { TopTermPercentageField } from './top_term_percentage_field'; import { ITooltipProperty, TooltipProperty } from '../tooltips/tooltip_property'; import { ESAggTooltipProperty } from '../tooltips/es_agg_tooltip_property'; +const TERMS_AGG_SHARD_SIZE = 5; + export interface IESAggField extends IField { getValueAggDsl(indexPattern: IndexPattern): unknown | null; getBucketCount(): number; @@ -100,7 +102,7 @@ export class ESAggField implements IESAggField { const field = getField(indexPattern, this.getRootName()); const aggType = this.getAggType(); - const aggBody = aggType === AGG_TYPE.TERMS ? { size: 1, shard_size: 1 } : {}; + const aggBody = aggType === AGG_TYPE.TERMS ? { size: 1, shard_size: TERMS_AGG_SHARD_SIZE } : {}; return { [aggType]: addFieldToDSL(aggBody, field), }; @@ -108,7 +110,7 @@ export class ESAggField implements IESAggField { getBucketCount(): number { // terms aggregation increases the overall number of buckets per split bucket - return this.getAggType() === AGG_TYPE.TERMS ? 1 : 0; + return this.getAggType() === AGG_TYPE.TERMS ? TERMS_AGG_SHARD_SIZE : 0; } supportsFieldMeta(): boolean { diff --git a/x-pack/plugins/maps/public/classes/layers/blended_vector_layer/blended_vector_layer.ts b/x-pack/plugins/maps/public/classes/layers/blended_vector_layer/blended_vector_layer.ts index 26a0ffc1b1a37..5388a82e5924d 100644 --- a/x-pack/plugins/maps/public/classes/layers/blended_vector_layer/blended_vector_layer.ts +++ b/x-pack/plugins/maps/public/classes/layers/blended_vector_layer/blended_vector_layer.ts @@ -11,7 +11,6 @@ import { getDefaultDynamicProperties } from '../../styles/vector/vector_style_de import { IDynamicStyleProperty } from '../../styles/vector/properties/dynamic_style_property'; import { IStyleProperty } from '../../styles/vector/properties/style_property'; import { - SOURCE_TYPES, COUNT_PROP_LABEL, COUNT_PROP_NAME, LAYER_TYPE, @@ -41,6 +40,10 @@ import { IVectorSource } from '../../sources/vector_source'; const ACTIVE_COUNT_DATA_ID = 'ACTIVE_COUNT_DATA_ID'; +interface CountData { + isSyncClustered: boolean; +} + function getAggType(dynamicProperty: IDynamicStyleProperty): AGG_TYPE { return dynamicProperty.isOrdinal() ? AGG_TYPE.AVG : AGG_TYPE.TERMS; } @@ -187,14 +190,10 @@ export class BlendedVectorLayer extends VectorLayer implements IVectorLayer { this._clusterStyle = new VectorStyle(clusterStyleDescriptor, this._clusterSource, this); let isClustered = false; - const sourceDataRequest = this.getSourceDataRequest(); - if (sourceDataRequest) { - const requestMeta = sourceDataRequest.getMeta(); - if ( - requestMeta && - requestMeta.sourceMeta && - requestMeta.sourceMeta.sourceType === SOURCE_TYPES.ES_GEO_GRID - ) { + const countDataRequest = this.getDataRequest(ACTIVE_COUNT_DATA_ID); + if (countDataRequest) { + const requestData = countDataRequest.getData() as CountData; + if (requestData && requestData.isSyncClustered) { isClustered = true; } } @@ -284,7 +283,8 @@ export class BlendedVectorLayer extends VectorLayer implements IVectorLayer { const resp = await searchSource.fetch(); const maxResultWindow = await this._documentSource.getMaxResultWindow(); isSyncClustered = resp.hits.total > maxResultWindow; - syncContext.stopLoading(dataRequestId, requestToken, { isSyncClustered }, searchFilters); + const countData = { isSyncClustered } as CountData; + syncContext.stopLoading(dataRequestId, requestToken, countData, searchFilters); } catch (error) { if (!(error instanceof DataRequestAbortError)) { syncContext.onLoadError(dataRequestId, requestToken, error.message); diff --git a/x-pack/plugins/maps/public/classes/layers/load_layer_wizards.ts b/x-pack/plugins/maps/public/classes/layers/load_layer_wizards.ts index 9af1684c0bac1..eaef7931b5e6c 100644 --- a/x-pack/plugins/maps/public/classes/layers/load_layer_wizards.ts +++ b/x-pack/plugins/maps/public/classes/layers/load_layer_wizards.ts @@ -27,7 +27,6 @@ import { mvtVectorSourceWizardConfig } from '../sources/mvt_single_layer_vector_ import { ObservabilityLayerWizardConfig } from './solution_layers/observability'; import { SecurityLayerWizardConfig } from './solution_layers/security'; import { choroplethLayerWizardConfig } from './choropleth_layer_wizard'; -import { getEnableVectorTiles } from '../../kibana_services'; let registered = false; export function registerLayerWizards() { @@ -60,10 +59,6 @@ export function registerLayerWizards() { // @ts-ignore registerLayerWizard(wmsLayerWizardConfig); - if (getEnableVectorTiles()) { - // eslint-disable-next-line no-console - console.warn('Vector tiles are an experimental feature and should not be used in production.'); - registerLayerWizard(mvtVectorSourceWizardConfig); - } + registerLayerWizard(mvtVectorSourceWizardConfig); registered = true; } diff --git a/x-pack/plugins/maps/public/classes/layers/solution_layers/observability/create_layer_descriptor.test.ts b/x-pack/plugins/maps/public/classes/layers/solution_layers/observability/create_layer_descriptor.test.ts index 075d19dccdb68..e6349fbe9ab9d 100644 --- a/x-pack/plugins/maps/public/classes/layers/solution_layers/observability/create_layer_descriptor.test.ts +++ b/x-pack/plugins/maps/public/classes/layers/solution_layers/observability/create_layer_descriptor.test.ts @@ -5,14 +5,9 @@ */ jest.mock('../../../../kibana_services', () => { - const mockUiSettings = { - get: () => { - return undefined; - }, - }; return { - getUiSettings: () => { - return mockUiSettings; + getIsDarkMode() { + return false; }, }; }); diff --git a/x-pack/plugins/maps/public/classes/layers/solution_layers/security/create_layer_descriptors.test.ts b/x-pack/plugins/maps/public/classes/layers/solution_layers/security/create_layer_descriptors.test.ts index 49a86f45a681b..d02f07923c682 100644 --- a/x-pack/plugins/maps/public/classes/layers/solution_layers/security/create_layer_descriptors.test.ts +++ b/x-pack/plugins/maps/public/classes/layers/solution_layers/security/create_layer_descriptors.test.ts @@ -5,14 +5,9 @@ */ jest.mock('../../../../kibana_services', () => { - const mockUiSettings = { - get: () => { - return undefined; - }, - }; return { - getUiSettings: () => { - return mockUiSettings; + getIsDarkMode() { + return false; }, }; }); diff --git a/x-pack/plugins/maps/public/classes/layers/tiled_vector_layer/tiled_vector_layer.test.tsx b/x-pack/plugins/maps/public/classes/layers/tiled_vector_layer/tiled_vector_layer.test.tsx index ecd625db34411..faae26cac08e7 100644 --- a/x-pack/plugins/maps/public/classes/layers/tiled_vector_layer/tiled_vector_layer.test.tsx +++ b/x-pack/plugins/maps/public/classes/layers/tiled_vector_layer/tiled_vector_layer.test.tsx @@ -8,12 +8,8 @@ import sinon from 'sinon'; jest.mock('../../../kibana_services', () => { return { - getUiSettings() { - return { - get() { - return false; - }, - }; + getIsDarkMode() { + return false; }, }; }); diff --git a/x-pack/plugins/maps/public/classes/sources/ems_tms_source/ems_base_map_layer_wizard.tsx b/x-pack/plugins/maps/public/classes/sources/ems_tms_source/ems_base_map_layer_wizard.tsx index 49d262cbad1a1..5cc2a1225bbd7 100644 --- a/x-pack/plugins/maps/public/classes/sources/ems_tms_source/ems_base_map_layer_wizard.tsx +++ b/x-pack/plugins/maps/public/classes/sources/ems_tms_source/ems_base_map_layer_wizard.tsx @@ -5,6 +5,7 @@ */ import React from 'react'; import { i18n } from '@kbn/i18n'; +import { EuiPanel } from '@elastic/eui'; import { LayerWizard, RenderWizardArguments } from '../../layers/layer_wizard_registry'; // @ts-ignore import { EMSTMSSource, sourceTitle } from './ems_tms_source'; @@ -32,7 +33,11 @@ export const emsBaseMapLayerWizardConfig: LayerWizard = { previewLayers([layerDescriptor]); }; - return ; + return ( + + + + ); }, title: sourceTitle, }; diff --git a/x-pack/plugins/maps/public/classes/sources/ems_tms_source/ems_tms_source.js b/x-pack/plugins/maps/public/classes/sources/ems_tms_source/ems_tms_source.js index 83c87eb53d4fe..b364dd32860f3 100644 --- a/x-pack/plugins/maps/public/classes/sources/ems_tms_source/ems_tms_source.js +++ b/x-pack/plugins/maps/public/classes/sources/ems_tms_source/ems_tms_source.js @@ -12,7 +12,7 @@ import { UpdateSourceEditor } from './update_source_editor'; import { i18n } from '@kbn/i18n'; import { getDataSourceLabel } from '../../../../common/i18n_getters'; import { SOURCE_TYPES } from '../../../../common/constants'; -import { getEmsTileLayerId, getUiSettings } from '../../../kibana_services'; +import { getEmsTileLayerId, getIsDarkMode } from '../../../kibana_services'; import { registerSource } from '../source_registry'; export const sourceTitle = i18n.translate('xpack.maps.source.emsTileTitle', { @@ -122,9 +122,8 @@ export class EMSTMSSource extends AbstractTMSSource { return this._descriptor.id; } - const isDarkMode = getUiSettings().get('theme:darkMode', false); const emsTileLayerId = getEmsTileLayerId(); - return isDarkMode ? emsTileLayerId.dark : emsTileLayerId.bright; + return getIsDarkMode() ? emsTileLayerId.dark : emsTileLayerId.bright; } } diff --git a/x-pack/plugins/maps/public/classes/sources/ems_tms_source/tile_service_select.js b/x-pack/plugins/maps/public/classes/sources/ems_tms_source/tile_service_select.js index 2b54e00cae739..1eff4bf3786f4 100644 --- a/x-pack/plugins/maps/public/classes/sources/ems_tms_source/tile_service_select.js +++ b/x-pack/plugins/maps/public/classes/sources/ems_tms_source/tile_service_select.js @@ -5,7 +5,7 @@ */ import React from 'react'; -import { EuiSelect, EuiFormRow, EuiPanel } from '@elastic/eui'; +import { EuiSelect, EuiFormRow } from '@elastic/eui'; import { getEmsTmsServices } from '../../../meta'; import { getEmsUnavailableMessage } from '../../../components/ems_unavailable_message'; @@ -71,25 +71,23 @@ export class TileServiceSelect extends React.Component { } return ( - - - - - + + + ); } } diff --git a/x-pack/plugins/maps/public/classes/sources/ems_tms_source/update_source_editor.js b/x-pack/plugins/maps/public/classes/sources/ems_tms_source/update_source_editor.js index 4d567b8dbb32a..f5ef7096d48dd 100644 --- a/x-pack/plugins/maps/public/classes/sources/ems_tms_source/update_source_editor.js +++ b/x-pack/plugins/maps/public/classes/sources/ems_tms_source/update_source_editor.js @@ -26,9 +26,7 @@ export function UpdateSourceEditor({ onChange, config }) { /> - - diff --git a/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.js b/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.js index 1be74140fe1bf..92f6c258af597 100644 --- a/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.js +++ b/x-pack/plugins/maps/public/classes/sources/es_geo_grid_source/es_geo_grid_source.js @@ -63,7 +63,6 @@ export class ESGeoGridSource extends AbstractESAggSource { getSyncMeta() { return { requestType: this._descriptor.requestType, - sourceType: SOURCE_TYPES.ES_GEO_GRID, }; } @@ -162,6 +161,7 @@ export class ESGeoGridSource extends AbstractESAggSource { bounds: makeESBbox(bufferedExtent), field: this._descriptor.geoField, precision, + size: DEFAULT_MAX_BUCKETS_LIMIT, }, }, }, @@ -246,6 +246,8 @@ export class ESGeoGridSource extends AbstractESAggSource { bounds: makeESBbox(bufferedExtent), field: this._descriptor.geoField, precision, + size: DEFAULT_MAX_BUCKETS_LIMIT, + shard_size: DEFAULT_MAX_BUCKETS_LIMIT, }, aggs: { gridCentroid: { diff --git a/x-pack/plugins/maps/public/classes/sources/es_search_source/es_search_source.js b/x-pack/plugins/maps/public/classes/sources/es_search_source/es_search_source.js index 330fa6e8318ed..256becf70ffb0 100644 --- a/x-pack/plugins/maps/public/classes/sources/es_search_source/es_search_source.js +++ b/x-pack/plugins/maps/public/classes/sources/es_search_source/es_search_source.js @@ -540,7 +540,6 @@ export class ESSearchSource extends AbstractESSource { scalingType: this._descriptor.scalingType, topHitsSplitField: this._descriptor.topHitsSplitField, topHitsSize: this._descriptor.topHitsSize, - sourceType: SOURCE_TYPES.ES_SEARCH, }; } diff --git a/x-pack/plugins/maps/public/classes/sources/source_registry.ts b/x-pack/plugins/maps/public/classes/sources/source_registry.ts index 3b334d45092ad..462624dfa6ec9 100644 --- a/x-pack/plugins/maps/public/classes/sources/source_registry.ts +++ b/x-pack/plugins/maps/public/classes/sources/source_registry.ts @@ -7,7 +7,7 @@ import { ISource } from './source'; -type SourceRegistryEntry = { +export type SourceRegistryEntry = { ConstructorFunction: new ( sourceDescriptor: any, // this is the source-descriptor that corresponds specifically to the particular ISource instance inspectorAdapters?: object diff --git a/x-pack/plugins/maps/public/classes/styles/vector/components/color/color_map_select.js b/x-pack/plugins/maps/public/classes/styles/vector/components/color/color_map_select.js index a7d849265d815..69cdb00a01c9c 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/components/color/color_map_select.js +++ b/x-pack/plugins/maps/public/classes/styles/vector/components/color/color_map_select.js @@ -161,7 +161,7 @@ export class ColorMapSelect extends Component { return ( - + {toggle} { - const useCustomMap = selectedValue === CUSTOM_MAP; - this.props.onChange({ - selectedMapId: useCustomMap ? null : selectedValue, - useCustomMap, - }); - }; - - _onCustomMapChange = ({ customMapStops, isInvalid }) => { - // Manage invalid custom map in local state - if (isInvalid) { - this.setState({ customMapStops }); - return; - } - - this.props.onChange({ - useCustomMap: true, - customMapStops, - }); - }; - - _renderCustomStopsInput() { - return !this.props.isCustomOnly && !this.props.useCustomMap - ? null - : this.props.renderCustomStopsInput(this._onCustomMapChange); - } - - _renderMapSelect() { - if (this.props.isCustomOnly) { - return null; - } - - const mapOptionsWithCustom = [ - { - value: CUSTOM_MAP, - inputDisplay: this.props.customOptionLabel, - }, - ...this.props.options, - ]; - - let valueOfSelected; - if (this.props.useCustomMap) { - valueOfSelected = CUSTOM_MAP; - } else { - valueOfSelected = this.props.options.find( - (option) => option.value === this.props.selectedMapId - ) - ? this.props.selectedMapId - : ''; - } - - return ( - - - - - ); - } - - render() { - return ( - - {this._renderMapSelect()} - {this._renderCustomStopsInput()} - - ); - } -} diff --git a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/__snapshots__/icon_map_select.test.tsx.snap b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/__snapshots__/icon_map_select.test.tsx.snap new file mode 100644 index 0000000000000..b0b85268aa1c8 --- /dev/null +++ b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/__snapshots__/icon_map_select.test.tsx.snap @@ -0,0 +1,124 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Should not render icon map select when isCustomOnly 1`] = ` + + + +`; + +exports[`Should render custom stops input when useCustomIconMap 1`] = ` + + + mock filledShapes option + , + "value": "filledShapes", + }, + Object { + "inputDisplay":
+ mock hollowShapes option +
, + "value": "hollowShapes", + }, + ] + } + valueOfSelected="CUSTOM_MAP_ID" + /> + + +
+`; + +exports[`Should render default props 1`] = ` + + + mock filledShapes option + , + "value": "filledShapes", + }, + Object { + "inputDisplay":
+ mock hollowShapes option +
, + "value": "hollowShapes", + }, + ] + } + valueOfSelected="filledShapes" + /> + +
+`; diff --git a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/dynamic_icon_form.js b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/dynamic_icon_form.js index e3724d42a783b..0601922077b4a 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/dynamic_icon_form.js +++ b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/dynamic_icon_form.js @@ -12,11 +12,9 @@ import { IconMapSelect } from './icon_map_select'; export function DynamicIconForm({ fields, - isDarkMode, onDynamicStyleChange, staticDynamicSelect, styleProperty, - symbolOptions, }) { const styleOptions = styleProperty.getOptions(); @@ -44,11 +42,8 @@ export function DynamicIconForm({ return ( ); diff --git a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_map_select.js b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_map_select.js deleted file mode 100644 index 6cfe656d65a1e..0000000000000 --- a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_map_select.js +++ /dev/null @@ -1,59 +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 { StyleMapSelect } from '../style_map_select'; -import { i18n } from '@kbn/i18n'; -import { IconStops } from './icon_stops'; -import { getIconPaletteOptions } from '../../symbol_utils'; - -export function IconMapSelect({ - customIconStops, - iconPaletteId, - isDarkMode, - onChange, - styleProperty, - symbolOptions, - useCustomIconMap, - isCustomOnly, -}) { - function onMapSelectChange({ customMapStops, selectedMapId, useCustomMap }) { - onChange({ - customIconStops: customMapStops, - iconPaletteId: selectedMapId, - useCustomIconMap: useCustomMap, - }); - } - - function renderCustomIconStopsInput(onCustomMapChange) { - return ( - - ); - } - - return ( - - ); -} diff --git a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_map_select.test.tsx b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_map_select.test.tsx new file mode 100644 index 0000000000000..4e68baf0bd7b7 --- /dev/null +++ b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_map_select.test.tsx @@ -0,0 +1,78 @@ +/* + * 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. + */ +/* eslint-disable max-classes-per-file */ + +jest.mock('./icon_stops', () => ({ + IconStops: () => { + return
mockIconStops
; + }, +})); + +jest.mock('../../symbol_utils', () => { + return { + getIconPaletteOptions: () => { + return [ + { value: 'filledShapes', inputDisplay:
mock filledShapes option
}, + { value: 'hollowShapes', inputDisplay:
mock hollowShapes option
}, + ]; + }, + PREFERRED_ICONS: ['circle'], + }; +}); + +import React from 'react'; +import { shallow } from 'enzyme'; + +import { FIELD_ORIGIN } from '../../../../../../common/constants'; +import { AbstractField } from '../../../../fields/field'; +import { IDynamicStyleProperty } from '../../properties/dynamic_style_property'; +import { IconMapSelect } from './icon_map_select'; + +class MockField extends AbstractField {} + +class MockDynamicStyleProperty { + getField() { + return new MockField({ fieldName: 'myField', origin: FIELD_ORIGIN.SOURCE }); + } + + getValueSuggestions() { + return []; + } +} + +const defaultProps = { + iconPaletteId: 'filledShapes', + onChange: () => {}, + styleProperty: (new MockDynamicStyleProperty() as unknown) as IDynamicStyleProperty, + isCustomOnly: false, +}; + +test('Should render default props', () => { + const component = shallow(); + + expect(component).toMatchSnapshot(); +}); + +test('Should render custom stops input when useCustomIconMap', () => { + const component = shallow( + + ); + + expect(component).toMatchSnapshot(); +}); + +test('Should not render icon map select when isCustomOnly', () => { + const component = shallow(); + + expect(component).toMatchSnapshot(); +}); diff --git a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_map_select.tsx b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_map_select.tsx new file mode 100644 index 0000000000000..1dd55bbb47f78 --- /dev/null +++ b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_map_select.tsx @@ -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 React, { Component, Fragment } from 'react'; +import { EuiSuperSelect, EuiSpacer } from '@elastic/eui'; + +import { i18n } from '@kbn/i18n'; +// @ts-expect-error +import { IconStops } from './icon_stops'; +// @ts-expect-error +import { getIconPaletteOptions, PREFERRED_ICONS } from '../../symbol_utils'; +import { IconStop } from '../../../../../../common/descriptor_types'; +import { IDynamicStyleProperty } from '../../properties/dynamic_style_property'; + +const CUSTOM_MAP_ID = 'CUSTOM_MAP_ID'; + +const DEFAULT_ICON_STOPS = [ + { stop: null, icon: PREFERRED_ICONS[0] }, // first stop is the "other" category + { stop: '', icon: PREFERRED_ICONS[1] }, +]; + +interface StyleOptionChanges { + customIconStops?: IconStop[]; + iconPaletteId?: string | null; + useCustomIconMap: boolean; +} + +interface Props { + customIconStops?: IconStop[]; + iconPaletteId: string | null; + onChange: ({ customIconStops, iconPaletteId, useCustomIconMap }: StyleOptionChanges) => void; + styleProperty: IDynamicStyleProperty; + useCustomIconMap?: boolean; + isCustomOnly: boolean; +} + +interface State { + customIconStops: IconStop[]; +} + +export class IconMapSelect extends Component { + state = { + customIconStops: this.props.customIconStops ? this.props.customIconStops : DEFAULT_ICON_STOPS, + }; + + _onMapSelect = (selectedValue: string) => { + const useCustomIconMap = selectedValue === CUSTOM_MAP_ID; + const changes: StyleOptionChanges = { + iconPaletteId: useCustomIconMap ? null : selectedValue, + useCustomIconMap, + }; + // edge case when custom palette is first enabled + // customIconStops is undefined so need to update custom stops with default so icons are rendered. + if (!this.props.customIconStops) { + changes.customIconStops = DEFAULT_ICON_STOPS; + } + this.props.onChange(changes); + }; + + _onCustomMapChange = ({ + customStops, + isInvalid, + }: { + customStops: IconStop[]; + isInvalid: boolean; + }) => { + // Manage invalid custom map in local state + this.setState({ customIconStops: customStops }); + + if (!isInvalid) { + this.props.onChange({ + useCustomIconMap: true, + customIconStops: customStops, + }); + } + }; + + _renderCustomStopsInput() { + return !this.props.isCustomOnly && !this.props.useCustomIconMap ? null : ( + + ); + } + + _renderMapSelect() { + if (this.props.isCustomOnly) { + return null; + } + + const mapOptionsWithCustom = [ + { + value: CUSTOM_MAP_ID, + inputDisplay: i18n.translate('xpack.maps.styles.icon.customMapLabel', { + defaultMessage: 'Custom icon palette', + }), + }, + ...getIconPaletteOptions(), + ]; + + let valueOfSelected = ''; + if (this.props.useCustomIconMap) { + valueOfSelected = CUSTOM_MAP_ID; + } else if (this.props.iconPaletteId) { + valueOfSelected = this.props.iconPaletteId; + } + + return ( + + + + + ); + } + + render() { + return ( + + {this._renderMapSelect()} + {this._renderCustomStopsInput()} + + ); + } +} diff --git a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_select.js b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_select.js index 1ceff3e3ba801..c8ad869d33d33 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_select.js +++ b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_select.js @@ -15,6 +15,8 @@ import { EuiSelectable, } from '@elastic/eui'; import { SymbolIcon } from '../legend/symbol_icon'; +import { SYMBOL_OPTIONS } from '../../symbol_utils'; +import { getIsDarkMode } from '../../../../../kibana_services'; function isKeyboardEvent(event) { return typeof event === 'object' && 'keyCode' in event; @@ -62,7 +64,6 @@ export class IconSelect extends Component { }; _renderPopoverButton() { - const { isDarkMode, value } = this.props; return ( } /> @@ -93,8 +94,7 @@ export class IconSelect extends Component { } _renderIconSelectable() { - const { isDarkMode } = this.props; - const options = this.props.symbolOptions.map(({ value, label }) => { + const options = SYMBOL_OPTIONS.map(({ value, label }) => { return { value, label, @@ -102,7 +102,7 @@ export class IconSelect extends Component { ), }; diff --git a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_select.test.js b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_select.test.js index 56dce6fad8386..8dc2057054e62 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_select.test.js +++ b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_select.test.js @@ -4,25 +4,30 @@ * you may not use this file except in compliance with the Elastic License. */ +jest.mock('../../../../../kibana_services', () => { + return { + getIsDarkMode() { + return false; + }, + }; +}); + +jest.mock('../../symbol_utils', () => { + return { + SYMBOL_OPTIONS: [ + { value: 'symbol1', label: 'symbol1' }, + { value: 'symbol2', label: 'symbol2' }, + ], + }; +}); + import React from 'react'; import { shallow } from 'enzyme'; import { IconSelect } from './icon_select'; -const symbolOptions = [ - { value: 'symbol1', label: 'symbol1' }, - { value: 'symbol2', label: 'symbol2' }, -]; - test('Should render icon select', () => { - const component = shallow( - {}} - symbolOptions={symbolOptions} - isDarkMode={false} - /> - ); + const component = shallow( {}} />); expect(component).toMatchSnapshot(); }); diff --git a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_stops.js b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_stops.js index 81a44fcaadbd3..78fa6c10b899d 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_stops.js +++ b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_stops.js @@ -11,7 +11,7 @@ import { getOtherCategoryLabel } from '../../style_util'; import { EuiButtonIcon, EuiFlexGroup, EuiFlexItem, EuiFormRow, EuiFieldText } from '@elastic/eui'; import { IconSelect } from './icon_select'; import { StopInput } from '../stop_input'; -import { PREFERRED_ICONS } from '../../symbol_utils'; +import { PREFERRED_ICONS, SYMBOL_OPTIONS } from '../../symbol_utils'; function isDuplicateStop(targetStop, iconStops) { const stops = iconStops.filter(({ stop }) => { @@ -20,7 +20,7 @@ function isDuplicateStop(targetStop, iconStops) { return stops.length > 1; } -export function getFirstUnusedSymbol(symbolOptions, iconStops) { +export function getFirstUnusedSymbol(iconStops) { const firstUnusedPreferredIconId = PREFERRED_ICONS.find((iconId) => { const isSymbolBeingUsed = iconStops.some(({ icon }) => { return icon === iconId; @@ -32,7 +32,7 @@ export function getFirstUnusedSymbol(symbolOptions, iconStops) { return firstUnusedPreferredIconId; } - const firstUnusedSymbol = symbolOptions.find(({ value }) => { + const firstUnusedSymbol = SYMBOL_OPTIONS.find(({ value }) => { const isSymbolBeingUsed = iconStops.some(({ icon }) => { return icon === value; }); @@ -42,19 +42,7 @@ export function getFirstUnusedSymbol(symbolOptions, iconStops) { return firstUnusedSymbol ? firstUnusedSymbol.value : DEFAULT_ICON; } -const DEFAULT_ICON_STOPS = [ - { stop: null, icon: PREFERRED_ICONS[0] }, //first stop is the "other" color - { stop: '', icon: PREFERRED_ICONS[1] }, -]; - -export function IconStops({ - field, - getValueSuggestions, - iconStops = DEFAULT_ICON_STOPS, - isDarkMode, - onChange, - symbolOptions, -}) { +export function IconStops({ field, getValueSuggestions, iconStops, onChange }) { return iconStops.map(({ stop, icon }, index) => { const onIconSelect = (selectedIconId) => { const newIconStops = [...iconStops]; @@ -62,7 +50,7 @@ export function IconStops({ ...iconStops[index], icon: selectedIconId, }; - onChange({ customMapStops: newIconStops }); + onChange({ customStops: newIconStops }); }; const onStopChange = (newStopValue) => { const newIconStops = [...iconStops]; @@ -71,17 +59,17 @@ export function IconStops({ stop: newStopValue, }; onChange({ - customMapStops: newIconStops, + customStops: newIconStops, isInvalid: isDuplicateStop(newStopValue, iconStops), }); }; const onAdd = () => { onChange({ - customMapStops: [ + customStops: [ ...iconStops.slice(0, index + 1), { stop: '', - icon: getFirstUnusedSymbol(symbolOptions, iconStops), + icon: getFirstUnusedSymbol(iconStops), }, ...iconStops.slice(index + 1), ], @@ -89,7 +77,7 @@ export function IconStops({ }; const onRemove = () => { onChange({ - customMapStops: [...iconStops.slice(0, index), ...iconStops.slice(index + 1)], + customStops: [...iconStops.slice(0, index), ...iconStops.slice(index + 1)], }); }; @@ -157,13 +145,7 @@ export function IconStops({ {stopInput}
- +
diff --git a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_stops.test.js b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_stops.test.js index ffe9b6feef462..fe73659b0fe58 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_stops.test.js +++ b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/icon_stops.test.js @@ -4,17 +4,41 @@ * you may not use this file except in compliance with the Elastic License. */ +import React from 'react'; import { getFirstUnusedSymbol } from './icon_stops'; -describe('getFirstUnusedSymbol', () => { - const symbolOptions = [{ value: 'icon1' }, { value: 'icon2' }]; +jest.mock('./icon_select', () => ({ + IconSelect: () => { + return
mockIconSelect
; + }, +})); + +jest.mock('../../symbol_utils', () => { + return { + SYMBOL_OPTIONS: [{ value: 'icon1' }, { value: 'icon2' }], + PREFERRED_ICONS: [ + 'circle', + 'marker', + 'square', + 'star', + 'triangle', + 'hospital', + 'circle-stroked', + 'marker-stroked', + 'square-stroked', + 'star-stroked', + 'triangle-stroked', + ], + }; +}); +describe('getFirstUnusedSymbol', () => { test('Should return first unused icon from PREFERRED_ICONS', () => { const iconStops = [ { stop: 'category1', icon: 'circle' }, { stop: 'category2', icon: 'marker' }, ]; - const nextIcon = getFirstUnusedSymbol(symbolOptions, iconStops); + const nextIcon = getFirstUnusedSymbol(iconStops); expect(nextIcon).toBe('square'); }); @@ -33,7 +57,7 @@ describe('getFirstUnusedSymbol', () => { { stop: 'category11', icon: 'triangle-stroked' }, { stop: 'category12', icon: 'icon1' }, ]; - const nextIcon = getFirstUnusedSymbol(symbolOptions, iconStops); + const nextIcon = getFirstUnusedSymbol(iconStops); expect(nextIcon).toBe('icon2'); }); @@ -53,7 +77,7 @@ describe('getFirstUnusedSymbol', () => { { stop: 'category12', icon: 'icon1' }, { stop: 'category13', icon: 'icon2' }, ]; - const nextIcon = getFirstUnusedSymbol(symbolOptions, iconStops); + const nextIcon = getFirstUnusedSymbol(iconStops); expect(nextIcon).toBe('marker'); }); }); diff --git a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/static_icon_form.js b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/static_icon_form.js index 56e5737f72449..986f279dddc1a 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/static_icon_form.js +++ b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/static_icon_form.js @@ -8,13 +8,7 @@ import React from 'react'; import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui'; import { IconSelect } from './icon_select'; -export function StaticIconForm({ - isDarkMode, - onStaticStyleChange, - staticDynamicSelect, - styleProperty, - symbolOptions, -}) { +export function StaticIconForm({ onStaticStyleChange, staticDynamicSelect, styleProperty }) { const onChange = (selectedIconId) => { onStaticStyleChange(styleProperty.getStyleName(), { value: selectedIconId }); }; @@ -25,12 +19,7 @@ export function StaticIconForm({ {staticDynamicSelect}
- + ); diff --git a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/vector_style_icon_editor.js b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/vector_style_icon_editor.js index 36b6c1a76470c..2a983a32f0d82 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/vector_style_icon_editor.js +++ b/x-pack/plugins/maps/public/classes/styles/vector/components/symbol/vector_style_icon_editor.js @@ -6,25 +6,15 @@ import React from 'react'; -import { getUiSettings } from '../../../../../kibana_services'; import { StylePropEditor } from '../style_prop_editor'; import { DynamicIconForm } from './dynamic_icon_form'; import { StaticIconForm } from './static_icon_form'; -import { SYMBOL_OPTIONS } from '../../symbol_utils'; export function VectorStyleIconEditor(props) { const iconForm = props.styleProperty.isDynamic() ? ( - + ) : ( - + ); return {iconForm}; diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.d.ts b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.d.ts index b53623ab52edb..e153b6e4850f7 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.d.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.d.ts @@ -33,4 +33,5 @@ export interface IDynamicStyleProperty extends IStyleProperty { pluckCategoricalStyleMetaFromFeatures(features: unknown[]): CategoryFieldMeta; pluckOrdinalStyleMetaFromFieldMetaData(fieldMetaData: unknown): RangeFieldMeta; pluckCategoricalStyleMetaFromFieldMetaData(fieldMetaData: unknown): CategoryFieldMeta; + getValueSuggestions(query: string): string[]; } diff --git a/x-pack/plugins/maps/public/classes/styles/vector/symbol_utils.js b/x-pack/plugins/maps/public/classes/styles/vector/symbol_utils.js index 04df9d73d75cd..3a5f9b8f6690e 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/symbol_utils.js +++ b/x-pack/plugins/maps/public/classes/styles/vector/symbol_utils.js @@ -9,6 +9,7 @@ import maki from '@elastic/maki'; import xml2js from 'xml2js'; import { parseXmlString } from '../../../../common/parse_xml_string'; import { SymbolIcon } from './components/legend/symbol_icon'; +import { getIsDarkMode } from '../../../kibana_services'; export const LARGE_MAKI_ICON_SIZE = 15; const LARGE_MAKI_ICON_SIZE_AS_STRING = LARGE_MAKI_ICON_SIZE.toString(); @@ -111,7 +112,8 @@ ICON_PALETTES.forEach((iconPalette) => { }); }); -export function getIconPaletteOptions(isDarkMode) { +export function getIconPaletteOptions() { + const isDarkMode = getIsDarkMode(); return ICON_PALETTES.map(({ id, icons }) => { const iconsDisplay = icons.map((iconId) => { const style = { diff --git a/x-pack/plugins/maps/public/classes/styles/vector/vector_style_defaults.test.ts b/x-pack/plugins/maps/public/classes/styles/vector/vector_style_defaults.test.ts index bc032639dd07d..d630d2909b3d8 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/vector_style_defaults.test.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/vector_style_defaults.test.ts @@ -5,14 +5,9 @@ */ jest.mock('../../../kibana_services', () => { - const mockUiSettings = { - get: () => { - return undefined; - }, - }; return { - getUiSettings: () => { - return mockUiSettings; + getIsDarkMode() { + return false; }, }; }); diff --git a/x-pack/plugins/maps/public/classes/styles/vector/vector_style_defaults.ts b/x-pack/plugins/maps/public/classes/styles/vector/vector_style_defaults.ts index a3ae80e0a5935..50321510c2ba8 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/vector_style_defaults.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/vector_style_defaults.ts @@ -18,8 +18,7 @@ import { CATEGORICAL_COLOR_PALETTES, } from '../color_palettes'; import { VectorStylePropertiesDescriptor } from '../../../../common/descriptor_types'; -// @ts-ignore -import { getUiSettings } from '../../../kibana_services'; +import { getIsDarkMode } from '../../../kibana_services'; export const MIN_SIZE = 1; export const MAX_SIZE = 64; @@ -67,7 +66,7 @@ export function getDefaultStaticProperties( const nextFillColor = DEFAULT_FILL_COLORS[nextColorIndex]; const nextLineColor = DEFAULT_LINE_COLORS[nextColorIndex]; - const isDarkMode = getUiSettings().get('theme:darkMode', false); + const isDarkMode = getIsDarkMode(); return { [VECTOR_STYLES.ICON]: { diff --git a/x-pack/plugins/maps/public/connected_components/add_layer_panel/flyout_body/__snapshots__/layer_wizard_select.test.tsx.snap b/x-pack/plugins/maps/public/connected_components/add_layer_panel/flyout_body/__snapshots__/layer_wizard_select.test.tsx.snap index ef11f9958d8db..f8803d6339d9c 100644 --- a/x-pack/plugins/maps/public/connected_components/add_layer_panel/flyout_body/__snapshots__/layer_wizard_select.test.tsx.snap +++ b/x-pack/plugins/maps/public/connected_components/add_layer_panel/flyout_body/__snapshots__/layer_wizard_select.test.tsx.snap @@ -3,6 +3,7 @@ exports[`LayerWizardSelect Should render layer select after layer wizards are loaded 1`] = ` diff --git a/x-pack/plugins/maps/public/connected_components/add_layer_panel/flyout_body/layer_wizard_select.tsx b/x-pack/plugins/maps/public/connected_components/add_layer_panel/flyout_body/layer_wizard_select.tsx index f0195bc5dee2f..6f3a88ce905ce 100644 --- a/x-pack/plugins/maps/public/connected_components/add_layer_panel/flyout_body/layer_wizard_select.tsx +++ b/x-pack/plugins/maps/public/connected_components/add_layer_panel/flyout_body/layer_wizard_select.tsx @@ -115,7 +115,7 @@ export class LayerWizardSelect extends Component { }); return ( - + { { return ( <> {this._renderCategoryFacets()} + {wizardCards} diff --git a/x-pack/plugins/maps/public/connected_components/layer_panel/join_editor/__snapshots__/join_editor.test.tsx.snap b/x-pack/plugins/maps/public/connected_components/layer_panel/join_editor/__snapshots__/join_editor.test.tsx.snap index 00d7f44d6273f..92330c1d1ddce 100644 --- a/x-pack/plugins/maps/public/connected_components/layer_panel/join_editor/__snapshots__/join_editor.test.tsx.snap +++ b/x-pack/plugins/maps/public/connected_components/layer_panel/join_editor/__snapshots__/join_editor.test.tsx.snap @@ -85,7 +85,7 @@ exports[`Should render join editor 1`] = ` > diff --git a/x-pack/plugins/maps/public/connected_components/layer_panel/join_editor/join_editor.tsx b/x-pack/plugins/maps/public/connected_components/layer_panel/join_editor/join_editor.tsx index c589604e85112..2065668858e22 100644 --- a/x-pack/plugins/maps/public/connected_components/layer_panel/join_editor/join_editor.tsx +++ b/x-pack/plugins/maps/public/connected_components/layer_panel/join_editor/join_editor.tsx @@ -85,7 +85,7 @@ export function JoinEditor({ joins, layer, onChange, leftJoinFields, layerDispla ); globalFilterCheckbox = ( - + + + ); } diff --git a/x-pack/plugins/maps/public/elasticsearch_geo_utils.js b/x-pack/plugins/maps/public/elasticsearch_geo_utils.js index efd243595db3e..0d247d389f478 100644 --- a/x-pack/plugins/maps/public/elasticsearch_geo_utils.js +++ b/x-pack/plugins/maps/public/elasticsearch_geo_utils.js @@ -400,8 +400,9 @@ export function getBoundingBoxGeometry(geometry) { export function formatEnvelopeAsPolygon({ maxLat, maxLon, minLat, minLon }) { // GeoJSON mandates that the outer polygon must be counterclockwise to avoid ambiguous polygons // when the shape crosses the dateline - const left = minLon; - const right = maxLon; + const lonDelta = maxLon - minLon; + const left = lonDelta > 360 ? -180 : minLon; + const right = lonDelta > 360 ? 180 : maxLon; const top = clampToLatBounds(maxLat); const bottom = clampToLatBounds(minLat); const topLeft = [left, top]; diff --git a/x-pack/plugins/maps/public/elasticsearch_geo_utils.test.js b/x-pack/plugins/maps/public/elasticsearch_geo_utils.test.js index a1e4e43f3ab75..adaeae66bee14 100644 --- a/x-pack/plugins/maps/public/elasticsearch_geo_utils.test.js +++ b/x-pack/plugins/maps/public/elasticsearch_geo_utils.test.js @@ -421,7 +421,7 @@ describe('createExtentFilter', () => { }); }); - it('should not clamp longitudes to -180 to 180', () => { + it('should clamp longitudes to -180 to 180 when lonitude wraps globe', () => { const mapExtent = { maxLat: 39, maxLon: 209, @@ -436,11 +436,11 @@ describe('createExtentFilter', () => { shape: { coordinates: [ [ - [-191, 39], - [-191, 35], - [209, 35], - [209, 39], - [-191, 39], + [-180, 39], + [-180, 35], + [180, 35], + [180, 39], + [-180, 39], ], ], type: 'Polygon', diff --git a/x-pack/plugins/maps/public/kibana_services.d.ts b/x-pack/plugins/maps/public/kibana_services.d.ts index 8fa52500fb16e..974bccf4942f3 100644 --- a/x-pack/plugins/maps/public/kibana_services.d.ts +++ b/x-pack/plugins/maps/public/kibana_services.d.ts @@ -24,6 +24,7 @@ export function getVisualizations(): any; export function getDocLinks(): any; export function getCoreChrome(): any; export function getUiSettings(): any; +export function getIsDarkMode(): boolean; export function getCoreOverlays(): any; export function getData(): any; export function getUiActions(): any; @@ -46,7 +47,6 @@ export function getEnabled(): boolean; export function getShowMapVisualizationTypes(): boolean; export function getShowMapsInspectorAdapter(): boolean; export function getPreserveDrawingBuffer(): boolean; -export function getEnableVectorTiles(): boolean; export function getProxyElasticMapsServiceInMaps(): boolean; export function getIsGoldPlus(): boolean; diff --git a/x-pack/plugins/maps/public/kibana_services.js b/x-pack/plugins/maps/public/kibana_services.js index 1684acfb0f463..53e128f94dfb6 100644 --- a/x-pack/plugins/maps/public/kibana_services.js +++ b/x-pack/plugins/maps/public/kibana_services.js @@ -40,6 +40,9 @@ export const getFileUploadComponent = () => { let uiSettings; export const setUiSettings = (coreUiSettings) => (uiSettings = coreUiSettings); export const getUiSettings = () => uiSettings; +export const getIsDarkMode = () => { + return getUiSettings().get('theme:darkMode', false); +}; let indexPatternSelectComponent; export const setIndexPatternSelect = (indexPatternSelect) => @@ -149,7 +152,6 @@ export const getEnabled = () => getMapAppConfig().enabled; export const getShowMapVisualizationTypes = () => getMapAppConfig().showMapVisualizationTypes; export const getShowMapsInspectorAdapter = () => getMapAppConfig().showMapsInspectorAdapter; export const getPreserveDrawingBuffer = () => getMapAppConfig().preserveDrawingBuffer; -export const getEnableVectorTiles = () => getMapAppConfig().enableVectorTiles; // map.* kibana.yml settings from maps_legacy plugin that are shared between OSS map visualizations and maps app let kibanaCommonConfig; diff --git a/x-pack/plugins/maps/public/lazy_load_bundle/index.ts b/x-pack/plugins/maps/public/lazy_load_bundle/index.ts index ca4098ebfa805..12d6d75ac57ba 100644 --- a/x-pack/plugins/maps/public/lazy_load_bundle/index.ts +++ b/x-pack/plugins/maps/public/lazy_load_bundle/index.ts @@ -14,6 +14,8 @@ import { MapStore, MapStoreState } from '../reducers/store'; import { EventHandlers } from '../reducers/non_serializable_instances'; import { RenderToolTipContent } from '../classes/tooltips/tooltip_property'; import { MapEmbeddableConfig, MapEmbeddableInput, MapEmbeddableOutput } from '../embeddable/types'; +import { SourceRegistryEntry } from '../classes/sources/source_registry'; +import { LayerWizard } from '../classes/layers/layer_wizard_registry'; let loadModulesPromise: Promise; @@ -42,6 +44,8 @@ interface LazyLoadedMapModules { indexPatternId: string, indexPatternTitle: string ) => LayerDescriptor[]; + registerLayerWizard(layerWizard: LayerWizard): void; + registerSource(entry: SourceRegistryEntry): void; } export async function lazyLoadMapModules(): Promise { @@ -65,6 +69,8 @@ export async function lazyLoadMapModules(): Promise { // @ts-expect-error renderApp, createSecurityLayerDescriptors, + registerLayerWizard, + registerSource, } = await import('./lazy'); resolve({ @@ -80,6 +86,8 @@ export async function lazyLoadMapModules(): Promise { mergeInputWithSavedMap, renderApp, createSecurityLayerDescriptors, + registerLayerWizard, + registerSource, }); }); return loadModulesPromise; diff --git a/x-pack/plugins/maps/public/lazy_load_bundle/lazy/index.ts b/x-pack/plugins/maps/public/lazy_load_bundle/lazy/index.ts index 4f9f01f8a1b37..c839122ab90b1 100644 --- a/x-pack/plugins/maps/public/lazy_load_bundle/lazy/index.ts +++ b/x-pack/plugins/maps/public/lazy_load_bundle/lazy/index.ts @@ -19,3 +19,5 @@ export * from '../../embeddable/merge_input_with_saved_map'; // @ts-expect-error export * from '../../routing/maps_router'; export * from '../../classes/layers/solution_layers/security'; +export { registerLayerWizard } from '../../classes/layers/layer_wizard_registry'; +export { registerSource } from '../../classes/sources/source_registry'; diff --git a/x-pack/plugins/maps/public/plugin.ts b/x-pack/plugins/maps/public/plugin.ts index 412e8832453bc..8428a31d8b408 100644 --- a/x-pack/plugins/maps/public/plugin.ts +++ b/x-pack/plugins/maps/public/plugin.ts @@ -55,7 +55,7 @@ import { getAppTitle } from '../common/i18n_getters'; import { ILicense } from '../../licensing/common/types'; import { lazyLoadMapModules } from './lazy_load_bundle'; import { MapsStartApi } from './api'; -import { createSecurityLayerDescriptors } from './api/create_security_layer_descriptors'; +import { createSecurityLayerDescriptors, registerLayerWizard, registerSource } from './api'; export interface MapsPluginSetupDependencies { inspector: InspectorSetupContract; @@ -170,6 +170,8 @@ export class MapsPlugin bindStartCoreAndPlugins(core, plugins); return { createSecurityLayerDescriptors, + registerLayerWizard, + registerSource, }; } } diff --git a/x-pack/plugins/maps/server/index.ts b/x-pack/plugins/maps/server/index.ts index a73ba91098e90..19ab532262971 100644 --- a/x-pack/plugins/maps/server/index.ts +++ b/x-pack/plugins/maps/server/index.ts @@ -15,7 +15,6 @@ export const config: PluginConfigDescriptor = { enabled: true, showMapVisualizationTypes: true, showMapsInspectorAdapter: true, - enableVectorTiles: true, preserveDrawingBuffer: true, }, schema: configSchema, diff --git a/x-pack/plugins/ml/common/constants/annotations.ts b/x-pack/plugins/ml/common/constants/annotations.ts index 936ff610361af..4929dfb28eb15 100644 --- a/x-pack/plugins/ml/common/constants/annotations.ts +++ b/x-pack/plugins/ml/common/constants/annotations.ts @@ -13,3 +13,6 @@ export const ANNOTATION_USER_UNKNOWN = ''; // UI enforced limit to the maximum number of characters that can be entered for an annotation. export const ANNOTATION_MAX_LENGTH_CHARS = 1000; + +export const ANNOTATION_EVENT_USER = 'user'; +export const ANNOTATION_EVENT_DELAYED_DATA = 'delayed_data'; diff --git a/x-pack/plugins/ml/common/constants/anomalies.ts b/x-pack/plugins/ml/common/constants/anomalies.ts index bbf3616c05880..d15033b738b0f 100644 --- a/x-pack/plugins/ml/common/constants/anomalies.ts +++ b/x-pack/plugins/ml/common/constants/anomalies.ts @@ -20,3 +20,5 @@ export enum ANOMALY_THRESHOLD { WARNING = 3, LOW = 0, } + +export const PARTITION_FIELDS = ['partition_field', 'over_field', 'by_field'] as const; diff --git a/x-pack/plugins/ml/common/types/annotations.ts b/x-pack/plugins/ml/common/types/annotations.ts index f2f6fe111f5cc..159a598f16bf5 100644 --- a/x-pack/plugins/ml/common/types/annotations.ts +++ b/x-pack/plugins/ml/common/types/annotations.ts @@ -58,8 +58,20 @@ // ] // } +import { PartitionFieldsType } from './anomalies'; import { ANNOTATION_TYPE } from '../constants/annotations'; +export type AnnotationFieldName = 'partition_field_name' | 'over_field_name' | 'by_field_name'; +export type AnnotationFieldValue = 'partition_field_value' | 'over_field_value' | 'by_field_value'; + +export function getAnnotationFieldName(fieldType: PartitionFieldsType): AnnotationFieldName { + return `${fieldType}_name` as AnnotationFieldName; +} + +export function getAnnotationFieldValue(fieldType: PartitionFieldsType): AnnotationFieldValue { + return `${fieldType}_value` as AnnotationFieldValue; +} + export interface Annotation { _id?: string; create_time?: number; @@ -73,8 +85,15 @@ export interface Annotation { annotation: string; job_id: string; type: ANNOTATION_TYPE.ANNOTATION | ANNOTATION_TYPE.COMMENT; + event?: string; + detector_index?: number; + partition_field_name?: string; + partition_field_value?: string; + over_field_name?: string; + over_field_value?: string; + by_field_name?: string; + by_field_value?: string; } - export function isAnnotation(arg: any): arg is Annotation { return ( arg.timestamp !== undefined && @@ -93,3 +112,27 @@ export function isAnnotations(arg: any): arg is Annotations { } return arg.every((d: Annotation) => isAnnotation(d)); } + +export interface FieldToBucket { + field: string; + missing?: string | number; +} + +export interface FieldToBucketResult { + key: string; + doc_count: number; +} + +export interface TermAggregationResult { + doc_count_error_upper_bound: number; + sum_other_doc_count: number; + buckets: FieldToBucketResult[]; +} + +export type EsAggregationResult = Record; + +export interface GetAnnotationsResponse { + aggregations?: EsAggregationResult; + annotations: Record; + success: boolean; +} diff --git a/x-pack/plugins/ml/common/types/anomalies.ts b/x-pack/plugins/ml/common/types/anomalies.ts index 639d9b3b25fae..a23886e8fcdc6 100644 --- a/x-pack/plugins/ml/common/types/anomalies.ts +++ b/x-pack/plugins/ml/common/types/anomalies.ts @@ -4,6 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ +import { PARTITION_FIELDS } from '../constants/anomalies'; + export interface Influencer { influencer_field_name: string; influencer_field_values: string[]; @@ -53,3 +55,5 @@ export interface AnomaliesTableRecord { typicalSort?: any; metricDescriptionSort?: number; } + +export type PartitionFieldsType = typeof PARTITION_FIELDS[number]; diff --git a/x-pack/plugins/ml/common/types/capabilities.ts b/x-pack/plugins/ml/common/types/capabilities.ts index f2b8159b6b83d..b46dd87eec15f 100644 --- a/x-pack/plugins/ml/common/types/capabilities.ts +++ b/x-pack/plugins/ml/common/types/capabilities.ts @@ -5,6 +5,7 @@ */ import { KibanaRequest } from 'kibana/server'; +import { PLUGIN_ID } from '../constants/app'; export const userMlCapabilities = { canAccessML: false, @@ -69,16 +70,31 @@ export function getDefaultCapabilities(): MlCapabilities { export function getPluginPrivileges() { const userMlCapabilitiesKeys = Object.keys(userMlCapabilities); const adminMlCapabilitiesKeys = Object.keys(adminMlCapabilities); - const allMlCapabilities = [...adminMlCapabilitiesKeys, ...userMlCapabilitiesKeys]; + const allMlCapabilitiesKeys = [...adminMlCapabilitiesKeys, ...userMlCapabilitiesKeys]; + // TODO: include ML in base privileges for the `8.0` release: https://github.com/elastic/kibana/issues/71422 + const privilege = { + app: [PLUGIN_ID, 'kibana'], + excludeFromBasePrivileges: true, + management: { + insightsAndAlerting: ['jobsListLink'], + }, + catalogue: [PLUGIN_ID], + savedObject: { + all: [], + read: ['index-pattern', 'search'], + }, + }; return { + admin: { + ...privilege, + api: allMlCapabilitiesKeys.map((k) => `ml:${k}`), + ui: allMlCapabilitiesKeys, + }, user: { - ui: userMlCapabilitiesKeys, + ...privilege, api: userMlCapabilitiesKeys.map((k) => `ml:${k}`), - }, - admin: { - ui: allMlCapabilities, - api: allMlCapabilities.map((k) => `ml:${k}`), + ui: userMlCapabilitiesKeys, }, }; } diff --git a/x-pack/plugins/ml/common/types/kibana.ts b/x-pack/plugins/ml/common/types/kibana.ts index 4a2edfebd1bac..f88b843015f17 100644 --- a/x-pack/plugins/ml/common/types/kibana.ts +++ b/x-pack/plugins/ml/common/types/kibana.ts @@ -11,8 +11,6 @@ import { IndexPatternAttributes } from 'src/plugins/data/common'; export type IndexPatternTitle = string; -export type callWithRequestType = (action: string, params?: any) => Promise; - export interface Route { id: string; k7Breadcrumbs: () => any; diff --git a/x-pack/plugins/ml/common/util/errors.test.ts b/x-pack/plugins/ml/common/util/errors.test.ts index 00af27248ccce..0b99799e3b6ec 100644 --- a/x-pack/plugins/ml/common/util/errors.test.ts +++ b/x-pack/plugins/ml/common/util/errors.test.ts @@ -30,6 +30,8 @@ describe('ML - error message utils', () => { const bodyWithStringMsg: MLCustomHttpResponseOptions = { body: { msg: testMsg, + statusCode: 404, + response: `{"error":{"reason":"${testMsg}"}}`, }, statusCode: 404, }; diff --git a/x-pack/plugins/ml/common/util/errors.ts b/x-pack/plugins/ml/common/util/errors.ts index e165e15d7c64e..6c5fa7bd75daf 100644 --- a/x-pack/plugins/ml/common/util/errors.ts +++ b/x-pack/plugins/ml/common/util/errors.ts @@ -41,7 +41,7 @@ export type MLResponseError = msg: string; }; } - | { msg: string }; + | { msg: string; statusCode: number; response: string }; export interface MLCustomHttpResponseOptions< T extends ResponseError | MLResponseError | BoomResponse @@ -53,42 +53,118 @@ export interface MLCustomHttpResponseOptions< statusCode: number; } -export const extractErrorMessage = ( +export interface MLErrorObject { + message: string; + fullErrorMessage?: string; // For use in a 'See full error' popover. + statusCode?: number; +} + +export const extractErrorProperties = ( error: | MLCustomHttpResponseOptions - | undefined | string -): string => { - // extract only the error message within the response error coming from Kibana, Elasticsearch, and our own ML messages + | undefined +): MLErrorObject => { + // extract properties of the error object from within the response error + // coming from Kibana, Elasticsearch, and our own ML messages + let message = ''; + let fullErrorMessage; + let statusCode; if (typeof error === 'string') { - return error; + return { + message: error, + }; + } + if (error?.body === undefined) { + return { + message: '', + }; } - if (error?.body === undefined) return ''; if (typeof error.body === 'string') { - return error.body; + return { + message: error.body, + }; } if ( typeof error.body === 'object' && 'output' in error.body && error.body.output.payload.message ) { - return error.body.output.payload.message; + return { + message: error.body.output.payload.message, + }; + } + + if ( + typeof error.body === 'object' && + 'response' in error.body && + typeof error.body.response === 'string' + ) { + const errorResponse = JSON.parse(error.body.response); + if ('error' in errorResponse && typeof errorResponse === 'object') { + const errorResponseError = errorResponse.error; + if ('reason' in errorResponseError) { + message = errorResponseError.reason; + } + if ('caused_by' in errorResponseError) { + const causedByMessage = JSON.stringify(errorResponseError.caused_by); + // Only add a fullErrorMessage if different to the message. + if (causedByMessage !== message) { + fullErrorMessage = causedByMessage; + } + } + return { + message, + fullErrorMessage, + statusCode: error.statusCode, + }; + } } if (typeof error.body === 'object' && 'msg' in error.body && typeof error.body.msg === 'string') { - return error.body.msg; + return { + message: error.body.msg, + }; } if (typeof error.body === 'object' && 'message' in error.body) { + if ( + 'attributes' in error.body && + typeof error.body.attributes === 'object' && + error.body.attributes.body?.status !== undefined + ) { + statusCode = error.body.attributes.body?.status; + } + if (typeof error.body.message === 'string') { - return error.body.message; + return { + message: error.body.message, + statusCode, + }; } if (!(error.body.message instanceof Error) && typeof (error.body.message.msg === 'string')) { - return error.body.message.msg; + return { + message: error.body.message.msg, + statusCode, + }; } } + // If all else fail return an empty message instead of JSON.stringify - return ''; + return { + message: '', + }; +}; + +export const extractErrorMessage = ( + error: + | MLCustomHttpResponseOptions + | undefined + | string +): string => { + // extract only the error message within the response error coming from Kibana, Elasticsearch, and our own ML messages + const errorObj = extractErrorProperties(error); + return errorObj.message; }; diff --git a/x-pack/plugins/ml/jsconfig.json b/x-pack/plugins/ml/jsconfig.json deleted file mode 100644 index 22e52d752250b..0000000000000 --- a/x-pack/plugins/ml/jsconfig.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "compilerOptions": { - "target": "es6", - "module": "commonjs", - "baseUrl": "../../../.", - "paths": { - "ui/*": ["src/legacy/ui/public/*"], - "plugins/ml/*": ["x-pack/plugins/ml/public/*"] - } - }, - "exclude": ["node_modules", "build"] -} diff --git a/x-pack/plugins/ml/kibana.json b/x-pack/plugins/ml/kibana.json index a08b9b6d97116..c61db9fb1ad8d 100644 --- a/x-pack/plugins/ml/kibana.json +++ b/x-pack/plugins/ml/kibana.json @@ -30,7 +30,6 @@ "esUiShared", "kibanaUtils", "kibanaReact", - "management", "dashboard", "savedObjects" ] diff --git a/x-pack/plugins/ml/public/application/app.tsx b/x-pack/plugins/ml/public/application/app.tsx index 9d5125532e5b8..cf645404860f5 100644 --- a/x-pack/plugins/ml/public/application/app.tsx +++ b/x-pack/plugins/ml/public/application/app.tsx @@ -20,7 +20,7 @@ import { MlRouter } from './routing'; import { mlApiServicesProvider } from './services/ml_api_service'; import { HttpService } from './services/http_service'; -type MlDependencies = MlSetupDependencies & MlStartDependencies; +export type MlDependencies = Omit & MlStartDependencies; interface AppProps { coreStart: CoreStart; diff --git a/x-pack/plugins/ml/public/application/capabilities/check_capabilities.ts b/x-pack/plugins/ml/public/application/capabilities/check_capabilities.ts index 65ea03caef526..56b372ff39919 100644 --- a/x-pack/plugins/ml/public/application/capabilities/check_capabilities.ts +++ b/x-pack/plugins/ml/public/application/capabilities/check_capabilities.ts @@ -16,8 +16,8 @@ let _capabilities: MlCapabilities = getDefaultCapabilities(); export function checkGetManagementMlJobsResolver() { return new Promise<{ mlFeatureEnabledInSpace: boolean }>((resolve, reject) => { - getManageMlCapabilities().then( - ({ capabilities, isPlatinumOrTrialLicense, mlFeatureEnabledInSpace }) => { + getManageMlCapabilities() + .then(({ capabilities, isPlatinumOrTrialLicense, mlFeatureEnabledInSpace }) => { _capabilities = capabilities; // Loop through all capabilities to ensure they are all set to true. const isManageML = Object.values(_capabilities).every((p) => p === true); @@ -28,62 +28,80 @@ export function checkGetManagementMlJobsResolver() { window.location.href = ACCESS_DENIED_PATH; return reject(); } - } - ); + }) + .catch((e) => { + window.location.href = ACCESS_DENIED_PATH; + return reject(); + }); }); } export function checkGetJobsCapabilitiesResolver(): Promise { return new Promise((resolve, reject) => { - getCapabilities().then(({ capabilities, isPlatinumOrTrialLicense }) => { - _capabilities = capabilities; - // the minimum privilege for using ML with a platinum or trial license is being able to get the transforms list. - // all other functionality is controlled by the return capabilities object. - // if the license is basic (isPlatinumOrTrialLicense === false) then do not redirect, - // allow the promise to resolve as the separate license check will redirect then user to - // a basic feature - if (_capabilities.canGetJobs || isPlatinumOrTrialLicense === false) { - return resolve(_capabilities); - } else { + getCapabilities() + .then(({ capabilities, isPlatinumOrTrialLicense }) => { + _capabilities = capabilities; + // the minimum privilege for using ML with a platinum or trial license is being able to get the transforms list. + // all other functionality is controlled by the return capabilities object. + // if the license is basic (isPlatinumOrTrialLicense === false) then do not redirect, + // allow the promise to resolve as the separate license check will redirect then user to + // a basic feature + if (_capabilities.canGetJobs || isPlatinumOrTrialLicense === false) { + return resolve(_capabilities); + } else { + window.location.href = '#/access-denied'; + return reject(); + } + }) + .catch((e) => { window.location.href = '#/access-denied'; return reject(); - } - }); + }); }); } export function checkCreateJobsCapabilitiesResolver(): Promise { return new Promise((resolve, reject) => { - getCapabilities().then(({ capabilities, isPlatinumOrTrialLicense }) => { - _capabilities = capabilities; - // if the license is basic (isPlatinumOrTrialLicense === false) then do not redirect, - // allow the promise to resolve as the separate license check will redirect then user to - // a basic feature - if (_capabilities.canCreateJob || isPlatinumOrTrialLicense === false) { - return resolve(_capabilities); - } else { - // if the user has no permission to create a job, - // redirect them back to the Transforms Management page + getCapabilities() + .then(({ capabilities, isPlatinumOrTrialLicense }) => { + _capabilities = capabilities; + // if the license is basic (isPlatinumOrTrialLicense === false) then do not redirect, + // allow the promise to resolve as the separate license check will redirect then user to + // a basic feature + if (_capabilities.canCreateJob || isPlatinumOrTrialLicense === false) { + return resolve(_capabilities); + } else { + // if the user has no permission to create a job, + // redirect them back to the Transforms Management page + window.location.href = '#/jobs'; + return reject(); + } + }) + .catch((e) => { window.location.href = '#/jobs'; return reject(); - } - }); + }); }); } export function checkFindFileStructurePrivilegeResolver(): Promise { return new Promise((resolve, reject) => { - getCapabilities().then(({ capabilities }) => { - _capabilities = capabilities; - // the minimum privilege for using ML with a basic license is being able to use the datavisualizer. - // all other functionality is controlled by the return _capabilities object - if (_capabilities.canFindFileStructure) { - return resolve(_capabilities); - } else { + getCapabilities() + .then(({ capabilities }) => { + _capabilities = capabilities; + // the minimum privilege for using ML with a basic license is being able to use the datavisualizer. + // all other functionality is controlled by the return _capabilities object + if (_capabilities.canFindFileStructure) { + return resolve(_capabilities); + } else { + window.location.href = '#/access-denied'; + return reject(); + } + }) + .catch((e) => { window.location.href = '#/access-denied'; return reject(); - } - }); + }); }); } diff --git a/x-pack/plugins/ml/public/application/components/annotations/annotation_description_list/index.tsx b/x-pack/plugins/ml/public/application/components/annotations/annotation_description_list/index.tsx index cf8fd299c07d7..eee2f8dca244d 100644 --- a/x-pack/plugins/ml/public/application/components/annotations/annotation_description_list/index.tsx +++ b/x-pack/plugins/ml/public/application/components/annotations/annotation_description_list/index.tsx @@ -19,9 +19,10 @@ import { formatHumanReadableDateTimeSeconds } from '../../../util/date_utils'; interface Props { annotation: Annotation; + detectorDescription?: string; } -export const AnnotationDescriptionList = ({ annotation }: Props) => { +export const AnnotationDescriptionList = ({ annotation, detectorDescription }: Props) => { const listItems = [ { title: i18n.translate('xpack.ml.timeSeriesExplorer.annotationDescriptionList.jobIdTitle', { @@ -81,6 +82,33 @@ export const AnnotationDescriptionList = ({ annotation }: Props) => { description: annotation.modified_username, }); } + if (detectorDescription !== undefined) { + listItems.push({ + title: i18n.translate('xpack.ml.timeSeriesExplorer.annotationDescriptionList.detectorTitle', { + defaultMessage: 'Detector', + }), + description: detectorDescription, + }); + } + + if (annotation.partition_field_name !== undefined) { + listItems.push({ + title: annotation.partition_field_name, + description: annotation.partition_field_value, + }); + } + if (annotation.over_field_name !== undefined) { + listItems.push({ + title: annotation.over_field_name, + description: annotation.over_field_value, + }); + } + if (annotation.by_field_name !== undefined) { + listItems.push({ + title: annotation.by_field_name, + description: annotation.by_field_value, + }); + } return ( { public state: State = { isDeleteModalVisible: false, + applyAnnotationToSeries: true, }; public annotationSub: Rx.Subscription | null = null; @@ -150,11 +178,31 @@ class AnnotationFlyoutUI extends Component { }; public saveOrUpdateAnnotation = () => { - const { annotation } = this.props; - - if (annotation === null) { + const { annotation: originalAnnotation, chartDetails, detectorIndex } = this.props; + if (originalAnnotation === null) { return; } + const annotation = cloneDeep(originalAnnotation); + + if (this.state.applyAnnotationToSeries && chartDetails?.entityData?.entities) { + chartDetails.entityData.entities.forEach((entity: Entity) => { + const { fieldName, fieldValue } = entity; + const fieldType = entity.fieldType as PartitionFieldsType; + annotation[getAnnotationFieldName(fieldType)] = fieldName; + annotation[getAnnotationFieldValue(fieldType)] = fieldValue; + }); + annotation.detector_index = detectorIndex; + } + // if unchecked, remove all the partitions before indexing + if (!this.state.applyAnnotationToSeries) { + delete annotation.detector_index; + PARTITION_FIELDS.forEach((fieldType) => { + delete annotation[getAnnotationFieldName(fieldType)]; + delete annotation[getAnnotationFieldValue(fieldType)]; + }); + } + // Mark the annotation created by `user` if and only if annotation is being created, not updated + annotation.event = annotation.event ?? ANNOTATION_EVENT_USER; annotation$.next(null); @@ -214,7 +262,7 @@ class AnnotationFlyoutUI extends Component { }; public render(): ReactNode { - const { annotation } = this.props; + const { annotation, detectors, detectorIndex } = this.props; const { isDeleteModalVisible } = this.state; if (annotation === null) { @@ -242,10 +290,13 @@ class AnnotationFlyoutUI extends Component { } ); } + const detector = detectors ? detectors.find((d) => d.index === detectorIndex) : undefined; + const detectorDescription = + detector && 'detector_description' in detector ? detector.detector_description : ''; return ( - +

@@ -264,7 +315,10 @@ class AnnotationFlyoutUI extends Component { - + { value={annotation.annotation} /> + + + } + checked={this.state.applyAnnotationToSeries} + onChange={() => + this.setState({ + applyAnnotationToSeries: !this.state.applyAnnotationToSeries, + }) + } + /> + diff --git a/x-pack/plugins/ml/public/application/components/annotations/annotations_table/__snapshots__/annotations_table.test.js.snap b/x-pack/plugins/ml/public/application/components/annotations/annotations_table/__snapshots__/annotations_table.test.js.snap index 3b93213da4033..63ec1744b62d0 100644 --- a/x-pack/plugins/ml/public/application/components/annotations/annotations_table/__snapshots__/annotations_table.test.js.snap +++ b/x-pack/plugins/ml/public/application/components/annotations/annotations_table/__snapshots__/annotations_table.test.js.snap @@ -11,7 +11,7 @@ exports[`AnnotationsTable Initialization with annotations prop. 1`] = ` "name": "Annotation", "scope": "row", "sortable": true, - "width": "50%", + "width": "40%", }, Object { "dataType": "date", @@ -39,6 +39,27 @@ exports[`AnnotationsTable Initialization with annotations prop. 1`] = ` "name": "Last modified by", "sortable": true, }, + Object { + "field": "event", + "name": "Event", + "sortable": true, + "width": "10%", + }, + Object { + "field": "partition_field_value", + "name": "Partition", + "sortable": true, + }, + Object { + "field": "over_field_value", + "name": "Over", + "sortable": true, + }, + Object { + "field": "by_field_value", + "name": "By", + "sortable": true, + }, Object { "actions": Array [ Object { @@ -52,6 +73,12 @@ exports[`AnnotationsTable Initialization with annotations prop. 1`] = ` "name": "Actions", "width": "60px", }, + Object { + "dataType": "boolean", + "field": "current_series", + "name": "current_series", + "width": "0px", + }, ] } compressed={true} @@ -82,6 +109,24 @@ exports[`AnnotationsTable Initialization with annotations prop. 1`] = ` } responsive={true} rowProps={[Function]} + search={ + Object { + "box": Object { + "incremental": true, + "schema": true, + }, + "defaultQuery": "event:(user or delayed_data)", + "filters": Array [ + Object { + "field": "event", + "multiSelect": "or", + "name": "Event", + "options": Array [], + "type": "field_value_selection", + }, + ], + } + } sorting={ Object { "sort": Object { diff --git a/x-pack/plugins/ml/public/application/components/annotations/annotations_table/annotations_table.js b/x-pack/plugins/ml/public/application/components/annotations/annotations_table/annotations_table.js index a091da6c359d1..cf4d25f159a1a 100644 --- a/x-pack/plugins/ml/public/application/components/annotations/annotations_table/annotations_table.js +++ b/x-pack/plugins/ml/public/application/components/annotations/annotations_table/annotations_table.js @@ -9,11 +9,9 @@ * This version supports both fetching the annotations by itself (used in the jobs list) and * getting the annotations via props (used in Anomaly Explorer and Single Series Viewer). */ - import _ from 'lodash'; import PropTypes from 'prop-types'; import rison from 'rison-node'; - import React, { Component, Fragment } from 'react'; import { @@ -50,7 +48,12 @@ import { annotationsRefresh$, annotationsRefreshed, } from '../../../services/annotations_service'; +import { + ANNOTATION_EVENT_USER, + ANNOTATION_EVENT_DELAYED_DATA, +} from '../../../../../common/constants/annotations'; +const CURRENT_SERIES = 'current_series'; /** * Table component for rendering the lists of annotations for an ML job. */ @@ -66,7 +69,10 @@ export class AnnotationsTable extends Component { super(props); this.state = { annotations: [], + aggregations: null, isLoading: false, + queryText: `event:(${ANNOTATION_EVENT_USER} or ${ANNOTATION_EVENT_DELAYED_DATA})`, + searchError: undefined, jobId: Array.isArray(this.props.jobs) && this.props.jobs.length > 0 && @@ -74,6 +80,9 @@ export class AnnotationsTable extends Component { ? this.props.jobs[0].job_id : undefined, }; + this.sorting = { + sort: { field: 'timestamp', direction: 'asc' }, + }; } getAnnotations() { @@ -92,11 +101,18 @@ export class AnnotationsTable extends Component { earliestMs: null, latestMs: null, maxAnnotations: ANNOTATIONS_TABLE_DEFAULT_QUERY_SIZE, + fields: [ + { + field: 'event', + missing: ANNOTATION_EVENT_USER, + }, + ], }) .toPromise() .then((resp) => { this.setState((prevState, props) => ({ annotations: resp.annotations[props.jobs[0].job_id] || [], + aggregations: resp.aggregations, errorMessage: undefined, isLoading: false, jobId: props.jobs[0].job_id, @@ -114,6 +130,25 @@ export class AnnotationsTable extends Component { } } + getAnnotationsWithExtraInfo(annotations) { + // if there is a specific view/chart entities that the annotations can be scoped to + // add a new column called 'current_series' + if (Array.isArray(this.props.chartDetails?.entityData?.entities)) { + return annotations.map((annotation) => { + const allMatched = this.props.chartDetails?.entityData?.entities.every( + ({ fieldType, fieldValue }) => { + const field = `${fieldType}_value`; + return !(!annotation[field] || annotation[field] !== fieldValue); + } + ); + return { ...annotation, [CURRENT_SERIES]: allMatched }; + }); + } else { + // if not make it return the original annotations + return annotations; + } + } + getJob(jobId) { // check if the job was supplied via props and matches the supplied jobId if (Array.isArray(this.props.jobs) && this.props.jobs.length > 0) { @@ -134,9 +169,9 @@ export class AnnotationsTable extends Component { Array.isArray(this.props.jobs) && this.props.jobs.length > 0 ) { - this.annotationsRefreshSubscription = annotationsRefresh$.subscribe(() => - this.getAnnotations() - ); + this.annotationsRefreshSubscription = annotationsRefresh$.subscribe(() => { + this.getAnnotations(); + }); annotationsRefreshed(); } } @@ -198,9 +233,11 @@ export class AnnotationsTable extends Component { }, }, }; + let mlTimeSeriesExplorer = {}; + const entityCondition = {}; if (annotation.timestamp !== undefined && annotation.end_timestamp !== undefined) { - appState.mlTimeSeriesExplorer = { + mlTimeSeriesExplorer = { zoom: { from: new Date(annotation.timestamp).toISOString(), to: new Date(annotation.end_timestamp).toISOString(), @@ -216,6 +253,27 @@ export class AnnotationsTable extends Component { } } + // if the annotation is at the series level + // then pass the partitioning field(s) and detector index to the Single Metric Viewer + if (_.has(annotation, 'detector_index')) { + mlTimeSeriesExplorer.detector_index = annotation.detector_index; + } + if (_.has(annotation, 'partition_field_value')) { + entityCondition[annotation.partition_field_name] = annotation.partition_field_value; + } + + if (_.has(annotation, 'over_field_value')) { + entityCondition[annotation.over_field_name] = annotation.over_field_value; + } + + if (_.has(annotation, 'by_field_value')) { + // Note that analyses with by and over fields, will have a top-level by_field_name, + // but the by_field_value(s) will be in the nested causes array. + entityCondition[annotation.by_field_name] = annotation.by_field_value; + } + mlTimeSeriesExplorer.entities = entityCondition; + appState.mlTimeSeriesExplorer = mlTimeSeriesExplorer; + const _g = rison.encode(globalSettings); const _a = rison.encode(appState); @@ -251,6 +309,8 @@ export class AnnotationsTable extends Component { render() { const { isSingleMetricViewerLinkVisible = true, isNumberBadgeVisible = false } = this.props; + const { queryText, searchError } = this.state; + if (this.props.annotations === undefined) { if (this.state.isLoading === true) { return ( @@ -314,7 +374,7 @@ export class AnnotationsTable extends Component { defaultMessage: 'Annotation', }), sortable: true, - width: '50%', + width: '40%', scope: 'row', }, { @@ -351,6 +411,14 @@ export class AnnotationsTable extends Component { }), sortable: true, }, + { + field: 'event', + name: i18n.translate('xpack.ml.annotationsTable.eventColumnName', { + defaultMessage: 'Event', + }), + sortable: true, + width: '10%', + }, ]; const jobIds = _.uniq(annotations.map((a) => a.job_id)); @@ -382,22 +450,23 @@ export class AnnotationsTable extends Component { actions.push({ render: (annotation) => { + // find the original annotation because the table might not show everything + const annotationId = annotation._id; + const originalAnnotation = annotations.find((d) => d._id === annotationId); const editAnnotationsTooltipText = ( ); - const editAnnotationsTooltipAriaLabelText = ( - + const editAnnotationsTooltipAriaLabelText = i18n.translate( + 'xpack.ml.annotationsTable.editAnnotationsTooltipAriaLabel', + { defaultMessage: 'Edit annotation' } ); return ( annotation$.next(annotation)} + onClick={() => annotation$.next(originalAnnotation ?? annotation)} iconType="pencil" aria-label={editAnnotationsTooltipAriaLabelText} /> @@ -421,17 +490,14 @@ export class AnnotationsTable extends Component { defaultMessage="Job configuration not supported in Single Metric Viewer" /> ); - const openInSingleMetricViewerAriaLabelText = isDrillDownAvailable ? ( - - ) : ( - - ); + const openInSingleMetricViewerAriaLabelText = isDrillDownAvailable + ? i18n.translate('xpack.ml.annotationsTable.openInSingleMetricViewerAriaLabel', { + defaultMessage: 'Open in Single Metric Viewer', + }) + : i18n.translate( + 'xpack.ml.annotationsTable.jobConfigurationNotSupportedInSingleMetricViewerAriaLabel', + { defaultMessage: 'Job configuration not supported in Single Metric Viewer' } + ); return ( @@ -447,38 +513,152 @@ export class AnnotationsTable extends Component { }); } - columns.push({ - align: RIGHT_ALIGNMENT, - width: '60px', - name: i18n.translate('xpack.ml.annotationsTable.actionsColumnName', { - defaultMessage: 'Actions', - }), - actions, - }); - const getRowProps = (item) => { return { onMouseOver: () => this.onMouseOverRow(item), onMouseLeave: () => this.onMouseLeaveRow(), }; }; + let filterOptions = []; + const aggregations = this.props.aggregations ?? this.state.aggregations; + if (aggregations) { + const buckets = aggregations.event.buckets; + const foundUser = buckets.findIndex((d) => d.key === ANNOTATION_EVENT_USER) > -1; + filterOptions = foundUser + ? buckets + : [{ key: ANNOTATION_EVENT_USER, doc_count: 0 }, ...buckets]; + } + const filters = [ + { + type: 'field_value_selection', + field: 'event', + name: 'Event', + multiSelect: 'or', + options: filterOptions.map((field) => ({ + value: field.key, + name: field.key, + view: `${field.key} (${field.doc_count})`, + })), + }, + ]; + + if (this.props.detectors) { + columns.push({ + name: i18n.translate('xpack.ml.annotationsTable.detectorColumnName', { + defaultMessage: 'Detector', + }), + width: '10%', + render: (item) => { + if ('detector_index' in item) { + return this.props.detectors[item.detector_index].detector_description; + } + return ''; + }, + }); + } + if (Array.isArray(this.props.chartDetails?.entityData?.entities)) { + // only show the column if the field exists in that job in SMV + this.props.chartDetails?.entityData?.entities.forEach((entity) => { + if (entity.fieldType === 'partition_field') { + columns.push({ + field: 'partition_field_value', + name: i18n.translate('xpack.ml.annotationsTable.partitionSMVColumnName', { + defaultMessage: 'Partition', + }), + sortable: true, + }); + } + if (entity.fieldType === 'over_field') { + columns.push({ + field: 'over_field_value', + name: i18n.translate('xpack.ml.annotationsTable.overColumnSMVName', { + defaultMessage: 'Over', + }), + sortable: true, + }); + } + if (entity.fieldType === 'by_field') { + columns.push({ + field: 'by_field_value', + name: i18n.translate('xpack.ml.annotationsTable.byColumnSMVName', { + defaultMessage: 'By', + }), + sortable: true, + }); + } + }); + filters.push({ + type: 'is', + field: CURRENT_SERIES, + name: i18n.translate('xpack.ml.annotationsTable.seriesOnlyFilterName', { + defaultMessage: 'Filter to series', + }), + }); + } else { + // else show all the partition columns in AE because there might be multiple jobs + columns.push({ + field: 'partition_field_value', + name: i18n.translate('xpack.ml.annotationsTable.partitionAEColumnName', { + defaultMessage: 'Partition', + }), + sortable: true, + }); + columns.push({ + field: 'over_field_value', + name: i18n.translate('xpack.ml.annotationsTable.overAEColumnName', { + defaultMessage: 'Over', + }), + sortable: true, + }); + + columns.push({ + field: 'by_field_value', + name: i18n.translate('xpack.ml.annotationsTable.byAEColumnName', { + defaultMessage: 'By', + }), + sortable: true, + }); + } + const search = { + defaultQuery: queryText, + box: { + incremental: true, + schema: true, + }, + filters: filters, + }; + + columns.push( + { + align: RIGHT_ALIGNMENT, + width: '60px', + name: i18n.translate('xpack.ml.annotationsTable.actionsColumnName', { + defaultMessage: 'Actions', + }), + actions, + }, + { + // hidden column, for search only + field: CURRENT_SERIES, + name: CURRENT_SERIES, + dataType: 'boolean', + width: '0px', + } + ); return ( diff --git a/x-pack/plugins/ml/public/application/components/job_selector/job_selector_flyout.tsx b/x-pack/plugins/ml/public/application/components/job_selector/job_selector_flyout.tsx index 803281bcd0ce9..62a74ed142ccf 100644 --- a/x-pack/plugins/ml/public/application/components/job_selector/job_selector_flyout.tsx +++ b/x-pack/plugins/ml/public/application/components/job_selector/job_selector_flyout.tsx @@ -193,7 +193,6 @@ export const JobSelectorFlyout: FC = ({ ref={flyoutEl} onClose={onFlyoutClose} aria-labelledby="jobSelectorFlyout" - size="l" data-test-subj="mlFlyoutJobSelector" > diff --git a/x-pack/plugins/ml/public/application/components/job_selector/use_job_selection.ts b/x-pack/plugins/ml/public/application/components/job_selector/use_job_selection.ts index 74c238a0895ca..0717348d1db22 100644 --- a/x-pack/plugins/ml/public/application/components/job_selector/use_job_selection.ts +++ b/x-pack/plugins/ml/public/application/components/job_selector/use_job_selection.ts @@ -5,16 +5,16 @@ */ import { difference } from 'lodash'; -import { useEffect } from 'react'; +import { useEffect, useMemo } from 'react'; import { i18n } from '@kbn/i18n'; -import { getToastNotifications } from '../../util/dependency_cache'; import { MlJobWithTimeRange } from '../../../../common/types/anomaly_detection_jobs'; import { useUrlState } from '../../util/url_state'; import { getTimeRangeFromSelection } from './job_select_service_utils'; +import { useNotifications } from '../../contexts/kibana'; // check that the ids read from the url exist by comparing them to the // jobs loaded via mlJobsService. @@ -25,49 +25,53 @@ function getInvalidJobIds(jobs: MlJobWithTimeRange[], ids: string[]) { }); } -function warnAboutInvalidJobIds(invalidIds: string[]) { - if (invalidIds.length > 0) { - const toastNotifications = getToastNotifications(); - toastNotifications.addWarning( - i18n.translate('xpack.ml.jobSelect.requestedJobsDoesNotExistWarningMessage', { - defaultMessage: `Requested -{invalidIdsLength, plural, one {job {invalidIds} does not exist} other {jobs {invalidIds} do not exist}}`, - values: { - invalidIdsLength: invalidIds.length, - invalidIds: invalidIds.join(), - }, - }) - ); - } -} - export interface JobSelection { jobIds: string[]; selectedGroups: string[]; } -export const useJobSelection = (jobs: MlJobWithTimeRange[], dateFormatTz: string) => { +export const useJobSelection = (jobs: MlJobWithTimeRange[]) => { const [globalState, setGlobalState] = useUrlState('_g'); + const { toasts: toastNotifications } = useNotifications(); - const jobSelection: JobSelection = { jobIds: [], selectedGroups: [] }; + const tmpIds = useMemo(() => { + const ids = globalState?.ml?.jobIds || []; + return (typeof ids === 'string' ? [ids] : ids).map((id: string) => String(id)); + }, [globalState?.ml?.jobIds]); - const ids = globalState?.ml?.jobIds || []; - const tmpIds = (typeof ids === 'string' ? [ids] : ids).map((id: string) => String(id)); - const invalidIds = getInvalidJobIds(jobs, tmpIds); - const validIds = difference(tmpIds, invalidIds); - validIds.sort(); + const invalidIds = useMemo(() => { + return getInvalidJobIds(jobs, tmpIds); + }, [tmpIds]); - jobSelection.jobIds = validIds; - jobSelection.selectedGroups = globalState?.ml?.groups ?? []; + const validIds = useMemo(() => { + const res = difference(tmpIds, invalidIds); + res.sort(); + return res; + }, [tmpIds, invalidIds]); + + const jobSelection: JobSelection = useMemo(() => { + const selectedGroups = globalState?.ml?.groups ?? []; + return { jobIds: validIds, selectedGroups }; + }, [validIds, globalState?.ml?.groups]); useEffect(() => { - warnAboutInvalidJobIds(invalidIds); + if (invalidIds.length > 0) { + toastNotifications.addWarning( + i18n.translate('xpack.ml.jobSelect.requestedJobsDoesNotExistWarningMessage', { + defaultMessage: `Requested +{invalidIdsLength, plural, one {job {invalidIds} does not exist} other {jobs {invalidIds} do not exist}}`, + values: { + invalidIdsLength: invalidIds.length, + invalidIds: invalidIds.join(), + }, + }) + ); + } }, [invalidIds]); useEffect(() => { // if there are no valid ids, warn and then select the first job if (validIds.length === 0 && jobs.length > 0) { - const toastNotifications = getToastNotifications(); toastNotifications.addWarning( i18n.translate('xpack.ml.jobSelect.noJobsSelectedWarningMessage', { defaultMessage: 'No jobs selected, auto selecting first job', diff --git a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/action_delete/action_delete.test.tsx b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/action_delete/action_delete.test.tsx index 8d6272c5df860..6b745a2c5ff3b 100644 --- a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/action_delete/action_delete.test.tsx +++ b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/action_delete/action_delete.test.tsx @@ -31,7 +31,13 @@ jest.mock('../../../../../contexts/kibana', () => ({ useMlKibana: () => ({ services: mockCoreServices.createStart(), }), + useNotifications: () => { + return { + toasts: { addSuccess: jest.fn(), addDanger: jest.fn(), addError: jest.fn() }, + }; + }, })); + export const MockI18nService = i18nServiceMock.create(); export const I18nServiceConstructor = jest.fn().mockImplementation(() => MockI18nService); jest.doMock('@kbn/i18n', () => ({ diff --git a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/action_delete/use_delete_action.ts b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/action_delete/use_delete_action.ts index f924cf3afcba5..4fc7b5e1367c4 100644 --- a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/action_delete/use_delete_action.ts +++ b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/action_delete/use_delete_action.ts @@ -13,6 +13,7 @@ import { IIndexPattern } from 'src/plugins/data/common'; import { extractErrorMessage } from '../../../../../../../common/util/errors'; import { useMlKibana } from '../../../../../contexts/kibana'; +import { useToastNotificationService } from '../../../../../services/toast_notification_service'; import { deleteAnalytics, @@ -37,6 +38,8 @@ export const useDeleteAction = () => { const indexName = item?.config.dest.index ?? ''; + const toastNotificationService = useToastNotificationService(); + const checkIndexPatternExists = async () => { try { const response = await savedObjectsClient.find({ @@ -109,10 +112,11 @@ export const useDeleteAction = () => { deleteAnalyticsAndDestIndex( item, deleteTargetIndex, - indexPatternExists && deleteIndexPattern + indexPatternExists && deleteIndexPattern, + toastNotificationService ); } else { - deleteAnalytics(item); + deleteAnalytics(item, toastNotificationService); } } }; diff --git a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/action_edit/edit_button_flyout.tsx b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/action_edit/edit_button_flyout.tsx index 4b708d48ca0ec..86b1c879417bb 100644 --- a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/action_edit/edit_button_flyout.tsx +++ b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/action_edit/edit_button_flyout.tsx @@ -28,11 +28,11 @@ import { import { useMlKibana } from '../../../../../contexts/kibana'; import { ml } from '../../../../../services/ml_api_service'; +import { useToastNotificationService } from '../../../../../services/toast_notification_service'; import { memoryInputValidator, MemoryInputValidatorResult, } from '../../../../../../../common/util/validators'; -import { extractErrorMessage } from '../../../../../../../common/util/errors'; import { DATA_FRAME_TASK_STATE } from '../analytics_list/common'; import { useRefreshAnalyticsList, @@ -60,6 +60,8 @@ export const EditButtonFlyout: FC> = ({ closeFlyout, item } } = useMlKibana(); const { refresh } = useRefreshAnalyticsList(); + const toastNotificationService = useToastNotificationService(); + // Disable if mml is not valid const updateButtonDisabled = mmlValidationError !== undefined || maxNumThreads === 0; @@ -113,15 +115,15 @@ export const EditButtonFlyout: FC> = ({ closeFlyout, item } // eslint-disable-next-line console.error(e); - notifications.toasts.addDanger({ - title: i18n.translate('xpack.ml.dataframe.analyticsList.editFlyoutErrorMessage', { + toastNotificationService.displayErrorToast( + e, + i18n.translate('xpack.ml.dataframe.analyticsList.editFlyoutErrorMessage', { defaultMessage: 'Could not save changes to analytics job {jobId}', values: { jobId, }, - }), - text: extractErrorMessage(e), - }); + }) + ); } }; diff --git a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/action_start/use_start_action.ts b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/action_start/use_start_action.ts index 8eb6b990827ac..3c1087ff587d8 100644 --- a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/action_start/use_start_action.ts +++ b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/action_start/use_start_action.ts @@ -8,6 +8,7 @@ import { useState } from 'react'; import { DataFrameAnalyticsListRow } from '../analytics_list/common'; import { startAnalytics } from '../../services/analytics_service'; +import { useToastNotificationService } from '../../../../../services/toast_notification_service'; export type StartAction = ReturnType; export const useStartAction = () => { @@ -15,11 +16,13 @@ export const useStartAction = () => { const [item, setItem] = useState(); + const toastNotificationService = useToastNotificationService(); + const closeModal = () => setModalVisible(false); const startAndCloseModal = () => { if (item !== undefined) { setModalVisible(false); - startAnalytics(item); + startAnalytics(item, toastNotificationService); } }; diff --git a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/services/analytics_service/delete_analytics.ts b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/services/analytics_service/delete_analytics.ts index ebd3fa8982604..7d3ee986a4ef1 100644 --- a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/services/analytics_service/delete_analytics.ts +++ b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/services/analytics_service/delete_analytics.ts @@ -7,13 +7,17 @@ import { i18n } from '@kbn/i18n'; import { extractErrorMessage } from '../../../../../../../common/util/errors'; import { getToastNotifications } from '../../../../../util/dependency_cache'; import { ml } from '../../../../../services/ml_api_service'; +import { ToastNotificationService } from '../../../../../services/toast_notification_service'; import { refreshAnalyticsList$, REFRESH_ANALYTICS_LIST_STATE } from '../../../../common'; import { isDataFrameAnalyticsFailed, DataFrameAnalyticsListRow, } from '../../components/analytics_list/common'; -export const deleteAnalytics = async (d: DataFrameAnalyticsListRow) => { +export const deleteAnalytics = async ( + d: DataFrameAnalyticsListRow, + toastNotificationService: ToastNotificationService +) => { const toastNotifications = getToastNotifications(); try { if (isDataFrameAnalyticsFailed(d.stats.state)) { @@ -27,13 +31,11 @@ export const deleteAnalytics = async (d: DataFrameAnalyticsListRow) => { }) ); } catch (e) { - const error = extractErrorMessage(e); - - toastNotifications.addDanger( + toastNotificationService.displayErrorToast( + e, i18n.translate('xpack.ml.dataframe.analyticsList.deleteAnalyticsErrorMessage', { - defaultMessage: - 'An error occurred deleting the data frame analytics job {analyticsId}: {error}', - values: { analyticsId: d.config.id, error }, + defaultMessage: 'An error occurred deleting the data frame analytics job {analyticsId}', + values: { analyticsId: d.config.id }, }) ); } @@ -43,7 +45,8 @@ export const deleteAnalytics = async (d: DataFrameAnalyticsListRow) => { export const deleteAnalyticsAndDestIndex = async ( d: DataFrameAnalyticsListRow, deleteDestIndex: boolean, - deleteDestIndexPattern: boolean + deleteDestIndexPattern: boolean, + toastNotificationService: ToastNotificationService ) => { const toastNotifications = getToastNotifications(); const destinationIndex = Array.isArray(d.config.dest.index) @@ -67,12 +70,11 @@ export const deleteAnalyticsAndDestIndex = async ( ); } if (status.analyticsJobDeleted?.error) { - const error = extractErrorMessage(status.analyticsJobDeleted.error); - toastNotifications.addDanger( + toastNotificationService.displayErrorToast( + status.analyticsJobDeleted.error, i18n.translate('xpack.ml.dataframe.analyticsList.deleteAnalyticsErrorMessage', { - defaultMessage: - 'An error occurred deleting the data frame analytics job {analyticsId}: {error}', - values: { analyticsId: d.config.id, error }, + defaultMessage: 'An error occurred deleting the data frame analytics job {analyticsId}', + values: { analyticsId: d.config.id }, }) ); } @@ -120,13 +122,11 @@ export const deleteAnalyticsAndDestIndex = async ( ); } } catch (e) { - const error = extractErrorMessage(e); - - toastNotifications.addDanger( + toastNotificationService.displayErrorToast( + e, i18n.translate('xpack.ml.dataframe.analyticsList.deleteAnalyticsErrorMessage', { - defaultMessage: - 'An error occurred deleting the data frame analytics job {analyticsId}: {error}', - values: { analyticsId: d.config.id, error }, + defaultMessage: 'An error occurred deleting the data frame analytics job {analyticsId}', + values: { analyticsId: d.config.id }, }) ); } diff --git a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/services/analytics_service/start_analytics.ts b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/services/analytics_service/start_analytics.ts index 6513cad808485..dfaac8f391f3c 100644 --- a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/services/analytics_service/start_analytics.ts +++ b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_management/services/analytics_service/start_analytics.ts @@ -5,29 +5,30 @@ */ import { i18n } from '@kbn/i18n'; -import { getToastNotifications } from '../../../../../util/dependency_cache'; import { ml } from '../../../../../services/ml_api_service'; +import { ToastNotificationService } from '../../../../../services/toast_notification_service'; import { refreshAnalyticsList$, REFRESH_ANALYTICS_LIST_STATE } from '../../../../common'; import { DataFrameAnalyticsListRow } from '../../components/analytics_list/common'; -export const startAnalytics = async (d: DataFrameAnalyticsListRow) => { - const toastNotifications = getToastNotifications(); +export const startAnalytics = async ( + d: DataFrameAnalyticsListRow, + toastNotificationService: ToastNotificationService +) => { try { await ml.dataFrameAnalytics.startDataFrameAnalytics(d.config.id); - toastNotifications.addSuccess( + toastNotificationService.displaySuccessToast( i18n.translate('xpack.ml.dataframe.analyticsList.startAnalyticsSuccessMessage', { defaultMessage: 'Request to start data frame analytics {analyticsId} acknowledged.', values: { analyticsId: d.config.id }, }) ); } catch (e) { - toastNotifications.addDanger( - i18n.translate('xpack.ml.dataframe.analyticsList.startAnalyticsErrorMessage', { - defaultMessage: - 'An error occurred starting the data frame analytics {analyticsId}: {error}', - values: { analyticsId: d.config.id, error: JSON.stringify(e) }, + toastNotificationService.displayErrorToast( + e, + i18n.translate('xpack.ml.dataframe.analyticsList.startAnalyticsErrorTitle', { + defaultMessage: 'Error starting job', }) ); } diff --git a/x-pack/plugins/ml/public/application/explorer/__snapshots__/explorer_swimlane.test.tsx.snap b/x-pack/plugins/ml/public/application/explorer/__snapshots__/explorer_swimlane.test.tsx.snap index 16b5ecc8a4600..4adaac1319d53 100644 --- a/x-pack/plugins/ml/public/application/explorer/__snapshots__/explorer_swimlane.test.tsx.snap +++ b/x-pack/plugins/ml/public/application/explorer/__snapshots__/explorer_swimlane.test.tsx.snap @@ -1,3 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`ExplorerSwimlane Overall swimlane 1`] = `"
Overall
2017-02-07T00:00:00Z2017-02-07T00:30:00Z2017-02-07T01:00:00Z2017-02-07T01:30:00Z2017-02-07T02:00:00Z2017-02-07T02:30:00Z2017-02-07T03:00:00Z2017-02-07T03:30:00Z2017-02-07T04:00:00Z2017-02-07T04:30:00Z2017-02-07T05:00:00Z2017-02-07T05:30:00Z2017-02-07T06:00:00Z2017-02-07T06:30:00Z2017-02-07T07:00:00Z2017-02-07T07:30:00Z2017-02-07T08:00:00Z2017-02-07T08:30:00Z2017-02-07T09:00:00Z2017-02-07T09:30:00Z2017-02-07T10:00:00Z2017-02-07T10:30:00Z2017-02-07T11:00:00Z2017-02-07T11:30:00Z2017-02-07T12:00:00Z2017-02-07T12:30:00Z2017-02-07T13:00:00Z2017-02-07T13:30:00Z2017-02-07T14:00:00Z2017-02-07T14:30:00Z2017-02-07T15:00:00Z2017-02-07T15:30:00Z2017-02-07T16:00:00Z
"`; +exports[`ExplorerSwimlane Overall swimlane 1`] = `"
Overall
2017-02-07T00:00:00Z2017-02-07T00:30:00Z2017-02-07T01:00:00Z2017-02-07T01:30:00Z2017-02-07T02:00:00Z2017-02-07T02:30:00Z2017-02-07T03:00:00Z2017-02-07T03:30:00Z2017-02-07T04:00:00Z2017-02-07T04:30:00Z2017-02-07T05:00:00Z2017-02-07T05:30:00Z2017-02-07T06:00:00Z2017-02-07T06:30:00Z2017-02-07T07:00:00Z2017-02-07T07:30:00Z2017-02-07T08:00:00Z2017-02-07T08:30:00Z2017-02-07T09:00:00Z2017-02-07T09:30:00Z2017-02-07T10:00:00Z2017-02-07T10:30:00Z2017-02-07T11:00:00Z2017-02-07T11:30:00Z2017-02-07T12:00:00Z2017-02-07T12:30:00Z2017-02-07T13:00:00Z2017-02-07T13:30:00Z2017-02-07T14:00:00Z2017-02-07T14:30:00Z2017-02-07T15:00:00Z2017-02-07T15:30:00Z2017-02-07T16:00:00Z
"`; diff --git a/x-pack/plugins/ml/public/application/explorer/actions/load_explorer_data.ts b/x-pack/plugins/ml/public/application/explorer/actions/load_explorer_data.ts index 095b42ffac5b7..3fcb032bd3ce1 100644 --- a/x-pack/plugins/ml/public/application/explorer/actions/load_explorer_data.ts +++ b/x-pack/plugins/ml/public/application/explorer/actions/load_explorer_data.ts @@ -258,7 +258,7 @@ const loadExplorerDataProvider = (anomalyTimelineService: AnomalyTimelineService { influencers, viewBySwimlaneState } ): Partial => { return { - annotationsData, + annotations: annotationsData, influencers, loading: false, viewBySwimlaneDataLoading: false, diff --git a/x-pack/plugins/ml/public/application/explorer/anomaly_timeline.tsx b/x-pack/plugins/ml/public/application/explorer/anomaly_timeline.tsx index e00e2e1e1e2eb..45dada84de20a 100644 --- a/x-pack/plugins/ml/public/application/explorer/anomaly_timeline.tsx +++ b/x-pack/plugins/ml/public/application/explorer/anomaly_timeline.tsx @@ -4,9 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -import React, { FC, useCallback, useMemo, useRef, useState } from 'react'; +import React, { FC, useMemo, useState } from 'react'; import { isEqual } from 'lodash'; -import DragSelect from 'dragselect'; import { EuiPanel, EuiPopover, @@ -22,21 +21,17 @@ import { } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; -import { DRAG_SELECT_ACTION, SWIMLANE_TYPE, VIEW_BY_JOB_LABEL } from './explorer_constants'; +import { OVERALL_LABEL, SWIMLANE_TYPE, VIEW_BY_JOB_LABEL } from './explorer_constants'; import { AddToDashboardControl } from './add_to_dashboard_control'; import { useMlKibana } from '../contexts/kibana'; import { TimeBuckets } from '../util/time_buckets'; import { UI_SETTINGS } from '../../../../../../src/plugins/data/common'; -import { - ALLOW_CELL_RANGE_SELECTION, - dragSelect$, - explorerService, -} from './explorer_dashboard_service'; +import { explorerService } from './explorer_dashboard_service'; import { ExplorerState } from './reducers/explorer_reducer'; import { hasMatchingPoints } from './has_matching_points'; import { ExplorerNoInfluencersFound } from './components/explorer_no_influencers_found/explorer_no_influencers_found'; import { SwimlaneContainer } from './swimlane_container'; -import { OverallSwimlaneData, ViewBySwimLaneData } from './explorer_utils'; +import { AppStateSelectedCells, OverallSwimlaneData, ViewBySwimLaneData } from './explorer_utils'; import { NoOverallData } from './components/no_overall_data'; function mapSwimlaneOptionsToEuiOptions(options: string[]) { @@ -63,10 +58,6 @@ export const AnomalyTimeline: FC = React.memo( const [isMenuOpen, setIsMenuOpen] = useState(false); const [isAddDashboardsActive, setIsAddDashboardActive] = useState(false); - const isSwimlaneSelectActive = useRef(false); - // make sure dragSelect is only available if the mouse pointer is actually over a swimlane - const disableDragSelectOnMouseLeave = useRef(true); - const canEditDashboards = capabilities.dashboard?.createNew ?? false; const timeBuckets = useMemo(() => { @@ -78,48 +69,6 @@ export const AnomalyTimeline: FC = React.memo( }); }, [uiSettings]); - const dragSelect = useMemo( - () => - new DragSelect({ - selectorClass: 'ml-swimlane-selector', - selectables: document.querySelectorAll('.sl-cell'), - callback(elements) { - if (elements.length > 1 && !ALLOW_CELL_RANGE_SELECTION) { - elements = [elements[0]]; - } - - if (elements.length > 0) { - dragSelect$.next({ - action: DRAG_SELECT_ACTION.NEW_SELECTION, - elements, - }); - } - - disableDragSelectOnMouseLeave.current = true; - }, - onDragStart(e) { - let target = e.target as HTMLElement; - while (target && target !== document.body && !target.classList.contains('sl-cell')) { - target = target.parentNode as HTMLElement; - } - if (ALLOW_CELL_RANGE_SELECTION && target !== document.body) { - dragSelect$.next({ - action: DRAG_SELECT_ACTION.DRAG_START, - }); - disableDragSelectOnMouseLeave.current = false; - } - }, - onElementSelect() { - if (ALLOW_CELL_RANGE_SELECTION) { - dragSelect$.next({ - action: DRAG_SELECT_ACTION.ELEMENT_SELECT, - }); - } - }, - }), - [] - ); - const { filterActive, filteredFields, @@ -138,42 +87,6 @@ export const AnomalyTimeline: FC = React.memo( loading, } = explorerState; - const setSwimlaneSelectActive = useCallback((active: boolean) => { - if (isSwimlaneSelectActive.current && !active && disableDragSelectOnMouseLeave.current) { - dragSelect.stop(); - isSwimlaneSelectActive.current = active; - return; - } - if (!isSwimlaneSelectActive.current && active) { - dragSelect.start(); - dragSelect.clearSelection(); - dragSelect.setSelectables(document.querySelectorAll('.sl-cell')); - isSwimlaneSelectActive.current = active; - } - }, []); - const onSwimlaneEnterHandler = () => setSwimlaneSelectActive(true); - const onSwimlaneLeaveHandler = () => setSwimlaneSelectActive(false); - - // Listens to render updates of the swimlanes to update dragSelect - const swimlaneRenderDoneListener = useCallback(() => { - dragSelect.clearSelection(); - dragSelect.setSelectables(document.querySelectorAll('.sl-cell')); - }, []); - - // Listener for click events in the swimlane to load corresponding anomaly data. - const swimlaneCellClick = useCallback( - (selectedCellsUpdate: any) => { - // If selectedCells is an empty object we clear any existing selection, - // otherwise we save the new selection in AppState and update the Explorer. - if (Object.keys(selectedCellsUpdate).length === 0) { - setSelectedCells(); - } else { - setSelectedCells(selectedCellsUpdate); - } - }, - [setSelectedCells] - ); - const menuItems = useMemo(() => { const items = []; if (canEditDashboards) { @@ -193,6 +106,19 @@ export const AnomalyTimeline: FC = React.memo( return items; }, [canEditDashboards]); + // If selecting a cell in the 'view by' swimlane, indicate the corresponding time in the Overall swimlane. + const overallCellSelection: AppStateSelectedCells | undefined = useMemo(() => { + if (!selectedCells) return; + + if (selectedCells.type === SWIMLANE_TYPE.OVERALL) return selectedCells; + + return { + type: SWIMLANE_TYPE.OVERALL, + lanes: [OVERALL_LABEL], + times: selectedCells.times, + }; + }, [selectedCells]); + return ( <> @@ -284,86 +210,68 @@ export const AnomalyTimeline: FC = React.memo( -
+ filterActive={filterActive} + maskAll={maskAll} + timeBuckets={timeBuckets} + swimlaneData={overallSwimlaneData as OverallSwimlaneData} + swimlaneType={SWIMLANE_TYPE.OVERALL} + selection={overallCellSelection} + onCellsSelection={setSelectedCells} + onResize={explorerService.setSwimlaneContainerWidth} + isLoading={loading} + noDataWarning={} + /> + + + + {viewBySwimlaneOptions.length > 0 && ( explorerService.setSwimlaneContainerWidth(width)} - isLoading={loading} - noDataWarning={} + onCellsSelection={setSelectedCells} + onResize={explorerService.setSwimlaneContainerWidth} + fromPage={viewByFromPage} + perPage={viewByPerPage} + swimlaneLimit={swimlaneLimit} + onPaginationChange={({ perPage: perPageUpdate, fromPage: fromPageUpdate }) => { + if (perPageUpdate) { + explorerService.setViewByPerPage(perPageUpdate); + } + if (fromPageUpdate) { + explorerService.setViewByFromPage(fromPageUpdate); + } + }} + isLoading={loading || viewBySwimlaneDataLoading} + noDataWarning={ + typeof viewBySwimlaneFieldName === 'string' ? ( + viewBySwimlaneFieldName === VIEW_BY_JOB_LABEL ? ( + + ) : ( + + ) + ) : null + } /> -
- - - - {viewBySwimlaneOptions.length > 0 && ( - <> - <> -
- explorerService.setSwimlaneContainerWidth(width)} - fromPage={viewByFromPage} - perPage={viewByPerPage} - swimlaneLimit={swimlaneLimit} - onPaginationChange={({ perPage: perPageUpdate, fromPage: fromPageUpdate }) => { - if (perPageUpdate) { - explorerService.setViewByPerPage(perPageUpdate); - } - if (fromPageUpdate) { - explorerService.setViewByFromPage(fromPageUpdate); - } - }} - isLoading={loading || viewBySwimlaneDataLoading} - noDataWarning={ - typeof viewBySwimlaneFieldName === 'string' ? ( - viewBySwimlaneFieldName === VIEW_BY_JOB_LABEL ? ( - - ) : ( - - ) - ) : null - } - /> -
- - )}
{isAddDashboardsActive && selectedJobs && ( diff --git a/x-pack/plugins/ml/public/application/explorer/explorer.d.ts b/x-pack/plugins/ml/public/application/explorer/explorer.d.ts index 90fb46d3cec4a..52181aab40328 100644 --- a/x-pack/plugins/ml/public/application/explorer/explorer.d.ts +++ b/x-pack/plugins/ml/public/application/explorer/explorer.d.ts @@ -5,11 +5,6 @@ */ import { FC } from 'react'; - -import { UrlState } from '../util/url_state'; - -import { JobSelection } from '../components/job_selector/use_job_selection'; - import { ExplorerState } from './reducers'; import { AppStateSelectedCells } from './explorer_utils'; diff --git a/x-pack/plugins/ml/public/application/explorer/explorer.js b/x-pack/plugins/ml/public/application/explorer/explorer.js index df4cea0c07987..4e27c17631506 100644 --- a/x-pack/plugins/ml/public/application/explorer/explorer.js +++ b/x-pack/plugins/ml/public/application/explorer/explorer.js @@ -14,6 +14,7 @@ import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; import { + htmlIdGenerator, EuiFlexGroup, EuiFlexItem, EuiFormRow, @@ -26,6 +27,9 @@ import { EuiSpacer, EuiTitle, EuiLoadingContent, + EuiPanel, + EuiAccordion, + EuiBadge, } from '@elastic/eui'; import { AnnotationFlyout } from '../components/annotations/annotation_flyout'; @@ -138,6 +142,7 @@ export class Explorer extends React.Component { }; state = { filterIconTriggeredQuery: undefined, language: DEFAULT_QUERY_LANG }; + htmlIdGen = htmlIdGenerator(); // Escape regular parens from fieldName as that portion of the query is not wrapped in double quotes // and will cause a syntax error when called with getKqlQueryValues @@ -202,7 +207,7 @@ export class Explorer extends React.Component { const { showCharts, severity } = this.props; const { - annotationsData, + annotations, chartsData, filterActive, filterPlaceHolder, @@ -216,6 +221,7 @@ export class Explorer extends React.Component { selectedJobs, tableData, } = this.props.explorerState; + const { annotationsData, aggregations } = annotations; const jobSelectorProps = { dateFormatTz: getDateFormatTz(), @@ -239,13 +245,12 @@ export class Explorer extends React.Component { ); } - const mainColumnWidthClassName = noInfluencersConfigured === true ? 'col-xs-12' : 'col-xs-10'; const mainColumnClasses = `column ${mainColumnWidthClassName}`; const timefilter = getTimefilter(); const bounds = timefilter.getActiveBounds(); - + const selectedJobIds = Array.isArray(selectedJobs) ? selectedJobs.map((job) => job.id) : []; return ( - {annotationsData.length > 0 && ( <> - -

- -

-
- + + +

+ + + + ), + }} + /> +

+ + } + > + <> + + +
+
- + )} - {loading === false && ( - <> +

+ )} + +
{showCharts && }
+ - +
)} diff --git a/x-pack/plugins/ml/public/application/explorer/explorer_constants.ts b/x-pack/plugins/ml/public/application/explorer/explorer_constants.ts index 21e13cb029d69..7440bf3213413 100644 --- a/x-pack/plugins/ml/public/application/explorer/explorer_constants.ts +++ b/x-pack/plugins/ml/public/application/explorer/explorer_constants.ts @@ -62,6 +62,11 @@ export const MAX_INFLUENCER_FIELD_NAMES = 50; export const VIEW_BY_JOB_LABEL = i18n.translate('xpack.ml.explorer.jobIdLabel', { defaultMessage: 'job ID', }); + +export const OVERALL_LABEL = i18n.translate('xpack.ml.explorer.overallLabel', { + defaultMessage: 'Overall', +}); + /** * Hard limitation for the size of terms * aggregations on influencers values. diff --git a/x-pack/plugins/ml/public/application/explorer/explorer_dashboard_service.ts b/x-pack/plugins/ml/public/application/explorer/explorer_dashboard_service.ts index 1429bf0858361..4d697bcda1a06 100644 --- a/x-pack/plugins/ml/public/application/explorer/explorer_dashboard_service.ts +++ b/x-pack/plugins/ml/public/application/explorer/explorer_dashboard_service.ts @@ -18,17 +18,12 @@ import { DeepPartial } from '../../../common/types/common'; import { jobSelectionActionCreator } from './actions'; import { ExplorerChartsData } from './explorer_charts/explorer_charts_container_service'; -import { DRAG_SELECT_ACTION, EXPLORER_ACTION } from './explorer_constants'; +import { EXPLORER_ACTION } from './explorer_constants'; import { AppStateSelectedCells, TimeRangeBounds } from './explorer_utils'; import { explorerReducer, getExplorerDefaultState, ExplorerState } from './reducers'; export const ALLOW_CELL_RANGE_SELECTION = true; -export const dragSelect$ = new Subject<{ - action: typeof DRAG_SELECT_ACTION[keyof typeof DRAG_SELECT_ACTION]; - elements?: any[]; -}>(); - type ExplorerAction = Action | Observable; export const explorerAction$ = new Subject(); @@ -54,7 +49,7 @@ const explorerState$: Observable = explorerFilteredAction$.pipe( shareReplay(1) ); -interface ExplorerAppState { +export interface ExplorerAppState { mlExplorerSwimlane: { selectedType?: string; selectedLanes?: string[]; diff --git a/x-pack/plugins/ml/public/application/explorer/explorer_swimlane.test.tsx b/x-pack/plugins/ml/public/application/explorer/explorer_swimlane.test.tsx index df450a33a52df..f7ae5f232999e 100644 --- a/x-pack/plugins/ml/public/application/explorer/explorer_swimlane.test.tsx +++ b/x-pack/plugins/ml/public/application/explorer/explorer_swimlane.test.tsx @@ -10,7 +10,6 @@ import moment from 'moment-timezone'; import { mountWithIntl } from 'test_utils/enzyme_helpers'; import React from 'react'; -import { dragSelect$ } from './explorer_dashboard_service'; import { ExplorerSwimlane } from './explorer_swimlane'; import { TimeBuckets as TimeBucketsClass } from '../util/time_buckets'; import { ChartTooltipService } from '../components/chart_tooltip'; @@ -27,13 +26,15 @@ jest.mock('d3', () => { }; }); -jest.mock('./explorer_dashboard_service', () => ({ - dragSelect$: { - subscribe: jest.fn(() => ({ - unsubscribe: jest.fn(), - })), - }, -})); +jest.mock('@elastic/eui', () => { + return { + htmlIdGenerator: jest.fn(() => { + return jest.fn(() => { + return 'test-gen-id'; + }); + }), + }; +}); function getExplorerSwimlaneMocks() { const swimlaneData = ({ laneLabels: [] } as unknown) as OverallSwimlaneData; @@ -52,6 +53,7 @@ function getExplorerSwimlaneMocks() { timeBuckets, swimlaneData, tooltipService, + parentRef: {} as React.RefObject, }; } @@ -74,50 +76,42 @@ describe('ExplorerSwimlane', () => { test('Minimal initialization', () => { const mocks = getExplorerSwimlaneMocks(); - const swimlaneRenderDoneListener = jest.fn(); const wrapper = mountWithIntl( ); expect(wrapper.html()).toBe( - `
` + - `
` + '
' ); // test calls to mock functions // @ts-ignore - expect(dragSelect$.subscribe.mock.calls.length).toBeGreaterThanOrEqual(1); - // @ts-ignore - expect(wrapper.instance().dragSelectSubscriber.unsubscribe.mock.calls).toHaveLength(0); - // @ts-ignore expect(mocks.timeBuckets.setInterval.mock.calls.length).toBeGreaterThanOrEqual(1); // @ts-ignore expect(mocks.timeBuckets.getScaledDateFormat.mock.calls.length).toBeGreaterThanOrEqual(1); - expect(swimlaneRenderDoneListener.mock.calls.length).toBeGreaterThanOrEqual(1); }); test('Overall swimlane', () => { const mocks = getExplorerSwimlaneMocks(); - const swimlaneRenderDoneListener = jest.fn(); const wrapper = mountWithIntl( ); @@ -125,13 +119,8 @@ describe('ExplorerSwimlane', () => { // test calls to mock functions // @ts-ignore - expect(dragSelect$.subscribe.mock.calls.length).toBeGreaterThanOrEqual(1); - // @ts-ignore - expect(wrapper.instance().dragSelectSubscriber.unsubscribe.mock.calls).toHaveLength(0); - // @ts-ignore expect(mocks.timeBuckets.setInterval.mock.calls.length).toBeGreaterThanOrEqual(1); // @ts-ignore expect(mocks.timeBuckets.getScaledDateFormat.mock.calls.length).toBeGreaterThanOrEqual(1); - expect(swimlaneRenderDoneListener.mock.calls.length).toBeGreaterThanOrEqual(1); }); }); diff --git a/x-pack/plugins/ml/public/application/explorer/explorer_swimlane.tsx b/x-pack/plugins/ml/public/application/explorer/explorer_swimlane.tsx index aa386288ac7e0..0f92278e90445 100644 --- a/x-pack/plugins/ml/public/application/explorer/explorer_swimlane.tsx +++ b/x-pack/plugins/ml/public/application/explorer/explorer_swimlane.tsx @@ -13,15 +13,17 @@ import './_explorer.scss'; import _ from 'lodash'; import d3 from 'd3'; import moment from 'moment'; +import DragSelect from 'dragselect'; import { i18n } from '@kbn/i18n'; -import { Subscription } from 'rxjs'; +import { Subject, Subscription } from 'rxjs'; import { TooltipValue } from '@elastic/charts'; +import { htmlIdGenerator } from '@elastic/eui'; import { formatHumanReadableDateTime } from '../util/date_utils'; import { numTicksForDateFormat } from '../util/chart_utils'; import { getSeverityColor } from '../../../common/util/anomaly_utils'; import { mlEscape } from '../util/string_utils'; -import { ALLOW_CELL_RANGE_SELECTION, dragSelect$ } from './explorer_dashboard_service'; +import { ALLOW_CELL_RANGE_SELECTION } from './explorer_dashboard_service'; import { DRAG_SELECT_ACTION, SwimlaneType } from './explorer_constants'; import { EMPTY_FIELD_VALUE_LABEL } from '../timeseriesexplorer/components/entity_control/entity_control'; import { TimeBuckets as TimeBucketsClass } from '../util/time_buckets'; @@ -29,7 +31,7 @@ import { ChartTooltipService, ChartTooltipValue, } from '../components/chart_tooltip/chart_tooltip_service'; -import { OverallSwimlaneData, ViewBySwimLaneData } from './explorer_utils'; +import { AppStateSelectedCells, OverallSwimlaneData, ViewBySwimLaneData } from './explorer_utils'; const SCSS = { mlDragselectDragging: 'mlDragselectDragging', @@ -56,7 +58,6 @@ export interface ExplorerSwimlaneProps { filterActive?: boolean; maskAll?: boolean; timeBuckets: InstanceType; - swimlaneCellClick?: Function; swimlaneData: OverallSwimlaneData | ViewBySwimLaneData; swimlaneType: SwimlaneType; selection?: { @@ -64,8 +65,15 @@ export interface ExplorerSwimlaneProps { type: string; times: number[]; }; - swimlaneRenderDoneListener?: Function; + onCellsSelection: (payload?: AppStateSelectedCells) => void; tooltipService: ChartTooltipService; + 'data-test-subj'?: string; + /** + * We need to be aware of the parent element in order to set + * the height so the swim lane widget doesn't jump during loading + * or page changes. + */ + parentRef: React.RefObject; } export class ExplorerSwimlane extends React.Component { @@ -78,13 +86,70 @@ export class ExplorerSwimlane extends React.Component { rootNode = React.createRef(); + isSwimlaneSelectActive = false; + // make sure dragSelect is only available if the mouse pointer is actually over a swimlane + disableDragSelectOnMouseLeave = true; + + dragSelect$ = new Subject<{ + action: typeof DRAG_SELECT_ACTION[keyof typeof DRAG_SELECT_ACTION]; + elements?: any[]; + }>(); + + /** + * Unique id for swim lane instance + */ + rootNodeId = htmlIdGenerator()(); + + /** + * Initialize drag select instance + */ + dragSelect = new DragSelect({ + selectorClass: 'ml-swimlane-selector', + selectables: document.querySelectorAll(`#${this.rootNodeId} .sl-cell`), + callback: (elements) => { + if (elements.length > 1 && !ALLOW_CELL_RANGE_SELECTION) { + elements = [elements[0]]; + } + + if (elements.length > 0) { + this.dragSelect$.next({ + action: DRAG_SELECT_ACTION.NEW_SELECTION, + elements, + }); + } + + this.disableDragSelectOnMouseLeave = true; + }, + onDragStart: (e) => { + // make sure we don't trigger text selection on label + e.preventDefault(); + let target = e.target as HTMLElement; + while (target && target !== document.body && !target.classList.contains('sl-cell')) { + target = target.parentNode as HTMLElement; + } + if (ALLOW_CELL_RANGE_SELECTION && target !== document.body) { + this.dragSelect$.next({ + action: DRAG_SELECT_ACTION.DRAG_START, + }); + this.disableDragSelectOnMouseLeave = false; + } + }, + onElementSelect: () => { + if (ALLOW_CELL_RANGE_SELECTION) { + this.dragSelect$.next({ + action: DRAG_SELECT_ACTION.ELEMENT_SELECT, + }); + } + }, + }); + componentDidMount() { // property for data comparison to be able to filter // consecutive click events with the same data. let previousSelectedData: any = null; // Listen for dragSelect events - this.dragSelectSubscriber = dragSelect$.subscribe(({ action, elements = [] }) => { + this.dragSelectSubscriber = this.dragSelect$.subscribe(({ action, elements = [] }) => { const element = d3.select(this.rootNode.current!.parentNode!); const { swimlaneType } = this.props; @@ -154,7 +219,7 @@ export class ExplorerSwimlane extends React.Component { } selectCell(cellsToSelect: any[], { laneLabels, bucketScore, times }: SelectedData) { - const { selection, swimlaneCellClick = () => {}, swimlaneData, swimlaneType } = this.props; + const { selection, swimlaneData, swimlaneType } = this.props; let triggerNewSelection = false; @@ -184,7 +249,7 @@ export class ExplorerSwimlane extends React.Component { } if (triggerNewSelection === false) { - swimlaneCellClick({}); + this.swimlaneCellClick(); return; } @@ -194,7 +259,7 @@ export class ExplorerSwimlane extends React.Component { times: d3.extent(times), type: swimlaneType, }; - swimlaneCellClick(selectedCells); + this.swimlaneCellClick(selectedCells); } highlightOverall(times: number[]) { @@ -208,10 +273,8 @@ export class ExplorerSwimlane extends React.Component { } highlightSelection(cellsToSelect: Node[], laneLabels: string[], times: number[]) { - const { swimlaneType } = this.props; - - // This selects both overall and viewby swimlane - const wrapper = d3.selectAll('.mlExplorerSwimlane'); + // This selects the embeddable container + const wrapper = d3.select(`#${this.rootNodeId}`); wrapper.selectAll('.lane-label').classed('lane-label-masked', true); wrapper @@ -232,13 +295,12 @@ export class ExplorerSwimlane extends React.Component { rootParent.selectAll('.lane-label').classed('lane-label-masked', function (this: HTMLElement) { return laneLabels.indexOf(d3.select(this).text()) === -1; }); - - if (swimlaneType === 'viewBy') { - // If selecting a cell in the 'view by' swimlane, indicate the corresponding time in the Overall swimlane. - this.highlightOverall(times); - } } + /** + * TODO should happen with props instead of imperative check + * @param maskAll + */ maskIrrelevantSwimlanes(maskAll: boolean) { if (maskAll === true) { // This selects both overall and viewby swimlane @@ -288,7 +350,6 @@ export class ExplorerSwimlane extends React.Component { filterActive, maskAll, timeBuckets, - swimlaneCellClick, swimlaneData, swimlaneType, selection, @@ -358,9 +419,12 @@ export class ExplorerSwimlane extends React.Component { const numBuckets = Math.round((endTime - startTime) / stepSecs); const cellHeight = 30; const height = (lanes.length + 1) * cellHeight - 10; - const laneLabelWidth = 170; + // Set height for the wrapper element + if (this.props.parentRef.current) { + this.props.parentRef.current.style.height = `${height + 20}px`; + } - element.style('height', `${height + 20}px`); + const laneLabelWidth = 170; const swimlanes = element.select('.ml-swimlanes'); swimlanes.html(''); @@ -413,8 +477,8 @@ export class ExplorerSwimlane extends React.Component { } }) .on('click', () => { - if (selection && typeof selection.lanes !== 'undefined' && swimlaneCellClick) { - swimlaneCellClick({}); + if (selection && typeof selection.lanes !== 'undefined') { + this.swimlaneCellClick(); } }) .each(function (this: HTMLElement) { @@ -567,9 +631,7 @@ export class ExplorerSwimlane extends React.Component { element.selectAll('.sl-cell-inner').classed('sl-cell-inner-masked', true); } - if (this.props.swimlaneRenderDoneListener) { - this.props.swimlaneRenderDoneListener(); - } + this.swimlaneRenderDoneListener(); if ( (swimlaneType !== selectedType || @@ -593,10 +655,7 @@ export class ExplorerSwimlane extends React.Component { selectedTimeExtent[1] <= endTime ) { // Locate matching cell - look for exact time, otherwise closest before. - const swimlaneElements = element.select('.ml-swimlanes'); - const laneCells = swimlaneElements.selectAll( - `div[data-lane-label="${mlEscape(selectedLane)}"]` - ); + const laneCells = element.selectAll(`div[data-lane-label="${mlEscape(selectedLane)}"]`); laneCells.each(function (this: HTMLElement) { const cell = d3.select(this); @@ -632,9 +691,58 @@ export class ExplorerSwimlane extends React.Component { return true; } + /** + * Listener for click events in the swim lane and execute a prop callback. + * @param selectedCellsUpdate + */ + swimlaneCellClick(selectedCellsUpdate?: AppStateSelectedCells) { + // If selectedCells is an empty object we clear any existing selection, + // otherwise we save the new selection in AppState and update the Explorer. + if (!selectedCellsUpdate) { + this.props.onCellsSelection(); + } else { + this.props.onCellsSelection(selectedCellsUpdate); + } + } + + /** + * Listens to render updates of the swim lanes to update dragSelect + */ + swimlaneRenderDoneListener() { + this.dragSelect.clearSelection(); + this.dragSelect.setSelectables(document.querySelectorAll(`#${this.rootNodeId} .sl-cell`)); + } + + setSwimlaneSelectActive(active: boolean) { + if (this.isSwimlaneSelectActive && !active && this.disableDragSelectOnMouseLeave) { + this.dragSelect.stop(); + this.isSwimlaneSelectActive = active; + return; + } + if (!this.isSwimlaneSelectActive && active) { + this.dragSelect.start(); + this.dragSelect.clearSelection(); + this.dragSelect.setSelectables(document.querySelectorAll(`#${this.rootNodeId} .sl-cell`)); + this.isSwimlaneSelectActive = active; + } + } + render() { const { swimlaneType } = this.props; - return
; + return ( +
+
+
+ ); } } diff --git a/x-pack/plugins/ml/public/application/explorer/explorer_utils.d.ts b/x-pack/plugins/ml/public/application/explorer/explorer_utils.d.ts index 05fdb52e1ccb2..0faa20295996c 100644 --- a/x-pack/plugins/ml/public/application/explorer/explorer_utils.d.ts +++ b/x-pack/plugins/ml/public/application/explorer/explorer_utils.d.ts @@ -7,6 +7,7 @@ import { Moment } from 'moment'; import { CombinedJob } from '../../../common/types/anomaly_detection_jobs'; +import { SwimlaneType } from './explorer_constants'; interface ClearedSelectedAnomaliesState { selectedCells: undefined; @@ -182,9 +183,9 @@ export declare interface FilterData { } export declare interface AppStateSelectedCells { - type: string; + type: SwimlaneType; lanes: string[]; times: number[]; - showTopFieldValues: boolean; - viewByFieldName: string; + showTopFieldValues?: boolean; + viewByFieldName?: string; } diff --git a/x-pack/plugins/ml/public/application/explorer/explorer_utils.js b/x-pack/plugins/ml/public/application/explorer/explorer_utils.js index 23da9669ee9a5..6e0863f1a6e5b 100644 --- a/x-pack/plugins/ml/public/application/explorer/explorer_utils.js +++ b/x-pack/plugins/ml/public/application/explorer/explorer_utils.js @@ -34,6 +34,7 @@ import { SWIMLANE_TYPE, VIEW_BY_JOB_LABEL, } from './explorer_constants'; +import { ANNOTATION_EVENT_USER } from '../../../common/constants/annotations'; // create new job objects based on standard job config objects // new job objects just contain job id, bucket span in seconds and a selected flag. @@ -395,6 +396,12 @@ export function loadAnnotationsTableData(selectedCells, selectedJobs, interval, earliestMs: timeRange.earliestMs, latestMs: timeRange.latestMs, maxAnnotations: ANNOTATIONS_TABLE_DEFAULT_QUERY_SIZE, + fields: [ + { + field: 'event', + missing: ANNOTATION_EVENT_USER, + }, + ], }) .toPromise() .then((resp) => { @@ -410,16 +417,17 @@ export function loadAnnotationsTableData(selectedCells, selectedJobs, interval, } }); - return resolve( - annotationsData + return resolve({ + annotationsData: annotationsData .sort((a, b) => { return a.timestamp - b.timestamp; }) .map((d, i) => { d.key = String.fromCharCode(65 + i); return d; - }) - ); + }), + aggregations: resp.aggregations, + }); }) .catch((resp) => { console.log('Error loading list of annotations for jobs list:', resp); diff --git a/x-pack/plugins/ml/public/application/explorer/reducers/explorer_reducer/job_selection_change.ts b/x-pack/plugins/ml/public/application/explorer/reducers/explorer_reducer/job_selection_change.ts index 49f5794273a04..4d5ad65065fc3 100644 --- a/x-pack/plugins/ml/public/application/explorer/reducers/explorer_reducer/job_selection_change.ts +++ b/x-pack/plugins/ml/public/application/explorer/reducers/explorer_reducer/job_selection_change.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ +import { isEqual } from 'lodash'; import { ActionPayload } from '../../explorer_dashboard_service'; import { getDefaultSwimlaneData, getInfluencers } from '../../explorer_utils'; @@ -17,7 +18,11 @@ export const jobSelectionChange = (state: ExplorerState, payload: ActionPayload) noInfluencersConfigured: getInfluencers(selectedJobs).length === 0, overallSwimlaneData: getDefaultSwimlaneData(), selectedJobs, - viewByFromPage: 1, + // currently job selection set asynchronously so + // we want to preserve the pagination from the url state + // on initial load + viewByFromPage: + !state.selectedJobs || isEqual(state.selectedJobs, selectedJobs) ? state.viewByFromPage : 1, }; // clear filter if selected jobs have no influencers diff --git a/x-pack/plugins/ml/public/application/explorer/reducers/explorer_reducer/reducer.ts b/x-pack/plugins/ml/public/application/explorer/reducers/explorer_reducer/reducer.ts index c55c06c80ab81..a38044a8b3425 100644 --- a/x-pack/plugins/ml/public/application/explorer/reducers/explorer_reducer/reducer.ts +++ b/x-pack/plugins/ml/public/application/explorer/reducers/explorer_reducer/reducer.ts @@ -113,7 +113,7 @@ export const explorerReducer = (state: ExplorerState, nextAction: Action): Explo const { annotationsData, overallState, tableData } = payload; nextState = { ...state, - annotationsData, + annotations: annotationsData, overallSwimlaneData: overallState, tableData, viewBySwimlaneData: { diff --git a/x-pack/plugins/ml/public/application/explorer/reducers/explorer_reducer/state.ts b/x-pack/plugins/ml/public/application/explorer/reducers/explorer_reducer/state.ts index 892b46467345b..889d572f4fabc 100644 --- a/x-pack/plugins/ml/public/application/explorer/reducers/explorer_reducer/state.ts +++ b/x-pack/plugins/ml/public/application/explorer/reducers/explorer_reducer/state.ts @@ -21,10 +21,14 @@ import { SwimlaneData, ViewBySwimLaneData, } from '../../explorer_utils'; +import { Annotations, EsAggregationResult } from '../../../../../common/types/annotations'; import { SWIM_LANE_DEFAULT_PAGE_SIZE } from '../../explorer_constants'; export interface ExplorerState { - annotationsData: any[]; + annotations: { + annotationsData: Annotations; + aggregations: EsAggregationResult; + }; bounds: TimeRangeBounds | undefined; chartsData: ExplorerChartsData; fieldFormatsLoading: boolean; @@ -62,7 +66,10 @@ function getDefaultIndexPattern() { export function getExplorerDefaultState(): ExplorerState { return { - annotationsData: [], + annotations: { + annotationsData: [], + aggregations: {}, + }, bounds: undefined, chartsData: getDefaultChartsData(), fieldFormatsLoading: false, diff --git a/x-pack/plugins/ml/public/application/explorer/swimlane_container.tsx b/x-pack/plugins/ml/public/application/explorer/swimlane_container.tsx index e34e1d26c9cab..51ea0f00d5f6a 100644 --- a/x-pack/plugins/ml/public/application/explorer/swimlane_container.tsx +++ b/x-pack/plugins/ml/public/application/explorer/swimlane_container.tsx @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import React, { FC, useCallback, useState } from 'react'; +import React, { FC, useCallback, useRef, useState } from 'react'; import { EuiText, EuiLoadingChart, @@ -49,7 +49,7 @@ export function isViewBySwimLaneData(arg: any): arg is ViewBySwimLaneData { * @constructor */ export const SwimlaneContainer: FC< - Omit & { + Omit & { onResize: (width: number) => void; fromPage?: number; perPage?: number; @@ -70,6 +70,7 @@ export const SwimlaneContainer: FC< ...props }) => { const [chartWidth, setChartWidth] = useState(0); + const wrapperRef = useRef(null); const resizeHandler = useCallback( throttle((e: { width: number; height: number }) => { @@ -111,36 +112,40 @@ export const SwimlaneContainer: FC< data-test-subj="mlSwimLaneContainer" > - - {showSwimlane && !isLoading && ( - - {(tooltipService) => ( - + + {showSwimlane && !isLoading && ( + + {(tooltipService) => ( + + )} + + )} + {isLoading && ( + + - )} - - )} - {isLoading && ( - - + )} + {!isLoading && !showSwimlane && ( + {noDataWarning}

} /> - - )} - {!isLoading && !showSwimlane && ( - {noDataWarning}} - /> - )} - + )} + +
+ {isPaginationVisible && ( { toasts.addSuccess( @@ -270,7 +272,8 @@ export class EditJobFlyoutUI extends Component { }) .catch((error) => { console.error(error); - toasts.addDanger( + toastNotificationService.displayErrorToast( + error, i18n.translate('xpack.ml.jobsList.editJobFlyout.changesNotSavedNotificationMessage', { defaultMessage: 'Could not save changes to {jobId}', values: { @@ -278,7 +281,6 @@ export class EditJobFlyoutUI extends Component { }, }) ); - mlMessageBarService.notify.error(error); }); }; diff --git a/x-pack/plugins/ml/public/application/jobs/jobs_list/components/utils.js b/x-pack/plugins/ml/public/application/jobs/jobs_list/components/utils.js index 569eca4aba949..6fabd0299a936 100644 --- a/x-pack/plugins/ml/public/application/jobs/jobs_list/components/utils.js +++ b/x-pack/plugins/ml/public/application/jobs/jobs_list/components/utils.js @@ -9,6 +9,7 @@ import { mlMessageBarService } from '../../../components/messagebar'; import rison from 'rison-node'; import { mlJobService } from '../../../services/job_service'; +import { toastNotificationServiceProvider } from '../../../services/toast_notification_service'; import { ml } from '../../../services/ml_api_service'; import { getToastNotifications } from '../../../util/dependency_cache'; import { stringMatch } from '../../../util/string_utils'; @@ -158,8 +159,9 @@ function showResults(resp, action) { if (failures.length > 0) { failures.forEach((f) => { - mlMessageBarService.notify.error(f.result.error); - toastNotifications.addDanger( + const toastNotificationService = toastNotificationServiceProvider(toastNotifications); + toastNotificationService.displayErrorToast( + f.result.error, i18n.translate('xpack.ml.jobsList.actionFailedNotificationMessage', { defaultMessage: '{failureId} failed to {actionText}', values: { diff --git a/x-pack/plugins/ml/public/application/management/index.ts b/x-pack/plugins/ml/public/application/management/index.ts index 480e2fe488980..897731304ee7a 100644 --- a/x-pack/plugins/ml/public/application/management/index.ts +++ b/x-pack/plugins/ml/public/application/management/index.ts @@ -16,10 +16,7 @@ import { take } from 'rxjs/operators'; import { CoreSetup } from 'kibana/public'; import { MlStartDependencies, MlSetupDependencies } from '../../plugin'; -import { - ManagementAppMountParams, - ManagementSectionId, -} from '../../../../../../src/plugins/management/public'; +import { ManagementAppMountParams } from '../../../../../../src/plugins/management/public'; import { PLUGIN_ID } from '../../../common/constants/app'; import { MINIMUM_FULL_LICENSE } from '../../../common/license'; @@ -34,7 +31,7 @@ export function initManagementSection( management !== undefined && license.check(PLUGIN_ID, MINIMUM_FULL_LICENSE).state === 'valid' ) { - management.sections.getSection(ManagementSectionId.InsightsAndAlerting).registerApp({ + management.sections.section.insightsAndAlerting.registerApp({ id: 'jobsListLink', title: i18n.translate('xpack.ml.management.jobsListTitle', { defaultMessage: 'Machine Learning Jobs', diff --git a/x-pack/plugins/ml/public/application/routing/routes/explorer.tsx b/x-pack/plugins/ml/public/application/routing/routes/explorer.tsx index 7a7865c9bd738..7d09797a0ff1b 100644 --- a/x-pack/plugins/ml/public/application/routing/routes/explorer.tsx +++ b/x-pack/plugins/ml/public/application/routing/routes/explorer.tsx @@ -20,7 +20,7 @@ import { useSelectedCells } from '../../explorer/hooks/use_selected_cells'; import { mlJobService } from '../../services/job_service'; import { ml } from '../../services/ml_api_service'; import { useExplorerData } from '../../explorer/actions'; -import { explorerService } from '../../explorer/explorer_dashboard_service'; +import { ExplorerAppState, explorerService } from '../../explorer/explorer_dashboard_service'; import { getDateFormatTz } from '../../explorer/explorer_utils'; import { useJobSelection } from '../../components/job_selector/use_job_selection'; import { useShowCharts } from '../../components/controls/checkbox_showcharts'; @@ -72,7 +72,7 @@ const ExplorerUrlStateManager: FC = ({ jobsWithTim const [lastRefresh, setLastRefresh] = useState(0); const timefilter = useTimefilter({ timeRangeSelector: true, autoRefreshSelector: true }); - const { jobIds } = useJobSelection(jobsWithTimeRange, getDateFormatTz()); + const { jobIds } = useJobSelection(jobsWithTimeRange); const refresh = useRefresh(); useEffect(() => { @@ -109,6 +109,14 @@ const ExplorerUrlStateManager: FC = ({ jobsWithTim } }, [globalState?.time?.from, globalState?.time?.to]); + useEffect(() => { + if (jobIds.length > 0) { + explorerService.updateJobSelection(jobIds); + } else { + explorerService.clearJobs(); + } + }, [JSON.stringify(jobIds)]); + useEffect(() => { const viewByFieldName = appState?.mlExplorerSwimlane?.viewByFieldName; if (viewByFieldName !== undefined) { @@ -119,15 +127,17 @@ const ExplorerUrlStateManager: FC = ({ jobsWithTim if (filterData !== undefined) { explorerService.setFilterData(filterData); } - }, []); - useEffect(() => { - if (jobIds.length > 0) { - explorerService.updateJobSelection(jobIds); - } else { - explorerService.clearJobs(); + const viewByPerPage = (appState as ExplorerAppState)?.mlExplorerSwimlane?.viewByPerPage; + if (viewByPerPage) { + explorerService.setViewByPerPage(viewByPerPage); } - }, [JSON.stringify(jobIds)]); + + const viewByFromPage = (appState as ExplorerAppState)?.mlExplorerSwimlane?.viewByFromPage; + if (viewByFromPage) { + explorerService.setViewByFromPage(viewByFromPage); + } + }, []); const [explorerData, loadExplorerData] = useExplorerData(); useEffect(() => { @@ -147,7 +157,6 @@ const ExplorerUrlStateManager: FC = ({ jobsWithTim }, [explorerAppState]); const explorerState = useObservable(explorerService.state$); - const [showCharts] = useShowCharts(); const [tableInterval] = useTableInterval(); const [tableSeverity] = useTableSeverity(); diff --git a/x-pack/plugins/ml/public/application/routing/use_refresh.ts b/x-pack/plugins/ml/public/application/routing/use_refresh.ts index c247fd9765e96..539ce6f88a421 100644 --- a/x-pack/plugins/ml/public/application/routing/use_refresh.ts +++ b/x-pack/plugins/ml/public/application/routing/use_refresh.ts @@ -6,7 +6,7 @@ import { useObservable } from 'react-use'; import { merge } from 'rxjs'; -import { map, skip } from 'rxjs/operators'; +import { map } from 'rxjs/operators'; import { useMemo } from 'react'; import { annotationsRefresh$ } from '../services/annotations_service'; @@ -29,9 +29,7 @@ export const useRefresh = () => { return merge( mlTimefilterRefresh$, timefilter.getTimeUpdate$().pipe( - // skip initially emitted value - skip(1), - map((_) => { + map(() => { const { from, to } = timefilter.getTime(); return { lastRefresh: Date.now(), timeRange: { start: from, end: to } }; }) diff --git a/x-pack/plugins/ml/public/application/services/anomaly_timeline_service.ts b/x-pack/plugins/ml/public/application/services/anomaly_timeline_service.ts index f2e362f754f2b..2bdb758be874c 100644 --- a/x-pack/plugins/ml/public/application/services/anomaly_timeline_service.ts +++ b/x-pack/plugins/ml/public/application/services/anomaly_timeline_service.ts @@ -5,7 +5,6 @@ */ import { IUiSettingsClient } from 'kibana/public'; -import { i18n } from '@kbn/i18n'; import { TimefilterContract, TimeRange, @@ -18,7 +17,7 @@ import { SwimlaneData, ViewBySwimLaneData, } from '../explorer/explorer_utils'; -import { VIEW_BY_JOB_LABEL } from '../explorer/explorer_constants'; +import { OVERALL_LABEL, VIEW_BY_JOB_LABEL } from '../explorer/explorer_constants'; import { MlResultsService } from './results_service'; /** @@ -288,9 +287,7 @@ export class AnomalyTimelineService { searchBounds: Required, interval: number ): OverallSwimlaneData { - const overallLabel = i18n.translate('xpack.ml.explorer.overallLabel', { - defaultMessage: 'Overall', - }); + const overallLabel = OVERALL_LABEL; const dataset: OverallSwimlaneData = { laneLabels: [overallLabel], points: [], @@ -302,7 +299,7 @@ export class AnomalyTimelineService { // Store the earliest and latest times of the data returned by the ES aggregations, // These will be used for calculating the earliest and latest times for the swim lane charts. Object.entries(scoresByTime).forEach(([timeMs, score]) => { - const time = Number(timeMs) / 1000; + const time = +timeMs / 1000; dataset.points.push({ laneLabel: overallLabel, time, @@ -346,7 +343,7 @@ export class AnomalyTimelineService { maxScoreByLaneLabel[influencerFieldValue] = 0; Object.entries(influencerData).forEach(([timeMs, anomalyScore]) => { - const time = Number(timeMs) / 1000; + const time = +timeMs / 1000; dataset.points.push({ laneLabel: influencerFieldValue, time, diff --git a/x-pack/plugins/ml/public/application/services/job_service.js b/x-pack/plugins/ml/public/application/services/job_service.js index 6c0f393c267aa..7e90758ffd7db 100644 --- a/x-pack/plugins/ml/public/application/services/job_service.js +++ b/x-pack/plugins/ml/public/application/services/job_service.js @@ -11,10 +11,12 @@ import { i18n } from '@kbn/i18n'; import { ml } from './ml_api_service'; import { mlMessageBarService } from '../components/messagebar'; +import { getToastNotifications } from '../util/dependency_cache'; import { isWebUrl } from '../util/url_utils'; import { ML_DATA_PREVIEW_COUNT } from '../../../common/util/job_utils'; import { TIME_FORMAT } from '../../../common/constants/time_format'; import { parseInterval } from '../../../common/util/parse_interval'; +import { toastNotificationServiceProvider } from '../services/toast_notification_service'; const msgs = mlMessageBarService; let jobs = []; @@ -417,14 +419,21 @@ class JobService { return { success: true }; }) .catch((err) => { - msgs.notify.error( - i18n.translate('xpack.ml.jobService.couldNotUpdateJobErrorMessage', { + // TODO - all the functions in here should just return the error and not + // display the toast, as currently both the component and this service display + // errors, so we end up with duplicate toasts. + const toastNotifications = getToastNotifications(); + const toastNotificationService = toastNotificationServiceProvider(toastNotifications); + toastNotificationService.displayErrorToast( + err, + i18n.translate('xpack.ml.jobService.updateJobErrorTitle', { defaultMessage: 'Could not update job: {jobId}', values: { jobId }, }) ); + console.error('update job', err); - return { success: false, message: err.message }; + return { success: false, message: err }; }); } @@ -436,12 +445,15 @@ class JobService { return { success: true, messages }; }) .catch((err) => { - msgs.notify.error( - i18n.translate('xpack.ml.jobService.jobValidationErrorMessage', { - defaultMessage: 'Job Validation Error: {errorMessage}', - values: { errorMessage: err.message }, + const toastNotifications = getToastNotifications(); + const toastNotificationService = toastNotificationServiceProvider(toastNotifications); + toastNotificationService.displayErrorToast( + err, + i18n.translate('xpack.ml.jobService.validateJobErrorTitle', { + defaultMessage: 'Job Validation Error', }) ); + console.log('validate job', err); return { success: false, diff --git a/x-pack/plugins/ml/public/application/services/ml_api_service/annotations.ts b/x-pack/plugins/ml/public/application/services/ml_api_service/annotations.ts index 29a5732026761..f9e19ba6f757e 100644 --- a/x-pack/plugins/ml/public/application/services/ml_api_service/annotations.ts +++ b/x-pack/plugins/ml/public/application/services/ml_api_service/annotations.ts @@ -4,7 +4,11 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Annotation } from '../../../../common/types/annotations'; +import { + Annotation, + FieldToBucket, + GetAnnotationsResponse, +} from '../../../../common/types/annotations'; import { http, http$ } from '../http_service'; import { basePath } from './index'; @@ -14,15 +18,19 @@ export const annotations = { earliestMs: number; latestMs: number; maxAnnotations: number; + fields: FieldToBucket[]; + detectorIndex: number; + entities: any[]; }) { const body = JSON.stringify(obj); - return http$<{ annotations: Record }>({ + return http$({ path: `${basePath()}/annotations`, method: 'POST', body, }); }, - indexAnnotation(obj: any) { + + indexAnnotation(obj: Annotation) { const body = JSON.stringify(obj); return http({ path: `${basePath()}/annotations/index`, diff --git a/x-pack/plugins/ml/public/application/services/new_job_capabilities_service.ts b/x-pack/plugins/ml/public/application/services/new_job_capabilities_service.ts index bc65ebe7a5fac..e2313de5c88b0 100644 --- a/x-pack/plugins/ml/public/application/services/new_job_capabilities_service.ts +++ b/x-pack/plugins/ml/public/application/services/new_job_capabilities_service.ts @@ -20,7 +20,7 @@ import { import { ml } from './ml_api_service'; import { getIndexPatternAndSavedSearch } from '../util/index_utils'; -// called in the angular routing resolve block to initialize the +// called in the routing resolve block to initialize the // newJobCapsService with the currently selected index pattern export function loadNewJobCapabilities( indexPatternId: string, diff --git a/x-pack/plugins/ml/public/application/services/toast_notification_service.ts b/x-pack/plugins/ml/public/application/services/toast_notification_service.ts new file mode 100644 index 0000000000000..d93d6833c7cb4 --- /dev/null +++ b/x-pack/plugins/ml/public/application/services/toast_notification_service.ts @@ -0,0 +1,84 @@ +/* + * 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 { ToastInput, ToastOptions, ToastsStart } from 'kibana/public'; +import { ResponseError } from 'kibana/server'; +import { useMemo } from 'react'; +import { useNotifications } from '../contexts/kibana'; +import { + BoomResponse, + extractErrorProperties, + MLCustomHttpResponseOptions, + MLErrorObject, + MLResponseError, +} from '../../../common/util/errors'; + +export type ToastNotificationService = ReturnType; + +export function toastNotificationServiceProvider(toastNotifications: ToastsStart) { + return { + displaySuccessToast(toastOrTitle: ToastInput, options?: ToastOptions) { + toastNotifications.addSuccess(toastOrTitle, options); + }, + + displayErrorToast(error: any, toastTitle: string) { + const errorObj = this.parseErrorMessage(error); + if (errorObj.fullErrorMessage !== undefined) { + // Provide access to the full error message via the 'See full error' button. + toastNotifications.addError(new Error(errorObj.fullErrorMessage), { + title: toastTitle, + toastMessage: errorObj.message, + }); + } else { + toastNotifications.addDanger( + { + title: toastTitle, + text: errorObj.message, + }, + { toastLifeTimeMs: 30000 } + ); + } + }, + + parseErrorMessage( + error: + | MLCustomHttpResponseOptions + | undefined + | string + | MLResponseError + ): MLErrorObject { + if ( + typeof error === 'object' && + 'response' in error && + typeof error.response === 'string' && + error.statusCode !== undefined + ) { + // MLResponseError which has been received back as part of a 'successful' response + // where the error was passed in a separate property in the response. + const wrapMlResponseError = { + body: error, + statusCode: error.statusCode, + }; + return extractErrorProperties(wrapMlResponseError); + } + + return extractErrorProperties( + error as + | MLCustomHttpResponseOptions + | undefined + | string + ); + }, + }; +} + +/** + * Hook to use {@link ToastNotificationService} in React components. + */ +export function useToastNotificationService(): ToastNotificationService { + const { toasts } = useNotifications(); + return useMemo(() => toastNotificationServiceProvider(toasts), []); +} diff --git a/x-pack/plugins/ml/public/application/timeseriesexplorer/timeseriesexplorer.js b/x-pack/plugins/ml/public/application/timeseriesexplorer/timeseriesexplorer.js index d4470e7502e0d..95dc1ed6988f6 100644 --- a/x-pack/plugins/ml/public/application/timeseriesexplorer/timeseriesexplorer.js +++ b/x-pack/plugins/ml/public/application/timeseriesexplorer/timeseriesexplorer.js @@ -28,6 +28,8 @@ import { EuiSelect, EuiSpacer, EuiTitle, + EuiAccordion, + EuiBadge, } from '@elastic/eui'; import { getToastNotifications } from '../util/dependency_cache'; @@ -125,6 +127,8 @@ function getTimeseriesexplorerDefaultState() { entitiesLoading: false, entityValues: {}, focusAnnotationData: [], + focusAggregations: {}, + focusAggregationInterval: {}, focusChartData: undefined, focusForecastData: undefined, fullRefresh: true, @@ -1025,6 +1029,7 @@ export class TimeSeriesExplorer extends React.Component { entityValues, focusAggregationInterval, focusAnnotationData, + focusAggregations, focusChartData, focusForecastData, fullRefresh, @@ -1075,8 +1080,8 @@ export class TimeSeriesExplorer extends React.Component { const entityControls = this.getControlsForDetector(); const fieldNamesWithEmptyValues = this.getFieldNamesWithEmptyValues(); const arePartitioningFieldsProvided = this.arePartitioningFieldsProvided(); - - const detectorSelectOptions = getViewableDetectors(selectedJob).map((d) => ({ + const detectors = getViewableDetectors(selectedJob); + const detectorSelectOptions = detectors.map((d) => ({ value: d.index, text: d.detector_description, })); @@ -1311,25 +1316,49 @@ export class TimeSeriesExplorer extends React.Component { )} - {showAnnotations && focusAnnotationData.length > 0 && ( -
- -

- -

-
+ {focusAnnotationData && focusAnnotationData.length > 0 && ( + +

+ + + + ), + }} + /> +

+ + } + > -
+ )} - +

number; @@ -37,6 +38,7 @@ export interface FocusData { showForecastCheckbox?: any; focusAnnotationData?: any; focusForecastData?: any; + focusAggregations?: any; } export function getFocusData( @@ -84,11 +86,23 @@ export function getFocusData( earliestMs: searchBounds.min.valueOf(), latestMs: searchBounds.max.valueOf(), maxAnnotations: ANNOTATIONS_TABLE_DEFAULT_QUERY_SIZE, + fields: [ + { + field: 'event', + missing: ANNOTATION_EVENT_USER, + }, + ], + detectorIndex, + entities: nonBlankEntities, }) .pipe( catchError(() => { // silent fail - return of({ annotations: {} as Record }); + return of({ + annotations: {} as Record, + aggregations: {}, + success: false, + }); }) ), // Plus query for forecast data if there is a forecastId stored in the appState. @@ -146,13 +160,14 @@ export function getFocusData( d.key = String.fromCharCode(65 + i); return d; }); + + refreshFocusData.focusAggregations = annotations.aggregations; } if (forecastData) { refreshFocusData.focusForecastData = processForecastResults(forecastData.results); refreshFocusData.showForecastCheckbox = refreshFocusData.focusForecastData.length > 0; } - return refreshFocusData; }) ); diff --git a/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/anomaly_swimlane_embeddable.tsx b/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/anomaly_swimlane_embeddable.tsx index 83070a5d94ba0..9f96b73d67c57 100644 --- a/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/anomaly_swimlane_embeddable.tsx +++ b/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/anomaly_swimlane_embeddable.tsx @@ -14,8 +14,8 @@ import { EmbeddableInput, EmbeddableOutput, IContainer, + IEmbeddable, } from '../../../../../../src/plugins/embeddable/public'; -import { MlStartDependencies } from '../../plugin'; import { EmbeddableSwimLaneContainer } from './embeddable_swim_lane_container'; import { AnomalyDetectorService } from '../../application/services/anomaly_detector_service'; import { JobId } from '../../../common/types/anomaly_detection_jobs'; @@ -27,6 +27,9 @@ import { TimeRange, } from '../../../../../../src/plugins/data/common'; import { SwimlaneType } from '../../application/explorer/explorer_constants'; +import { MlDependencies } from '../../application/app'; +import { AppStateSelectedCells } from '../../application/explorer/explorer_utils'; +import { SWIM_LANE_SELECTION_TRIGGER } from '../../ui_actions/triggers'; export const ANOMALY_SWIMLANE_EMBEDDABLE_TYPE = 'ml_anomaly_swimlane'; @@ -49,16 +52,26 @@ export interface AnomalySwimlaneEmbeddableCustomInput { timeRange: TimeRange; } +export interface EditSwimlanePanelContext { + embeddable: IEmbeddable; +} + +export interface SwimLaneDrilldownContext extends EditSwimlanePanelContext { + /** + * Optional data provided by swim lane selection + */ + data?: AppStateSelectedCells; +} + export type AnomalySwimlaneEmbeddableInput = EmbeddableInput & AnomalySwimlaneEmbeddableCustomInput; export type AnomalySwimlaneEmbeddableOutput = EmbeddableOutput & AnomalySwimlaneEmbeddableCustomOutput; export interface AnomalySwimlaneEmbeddableCustomOutput { - jobIds: JobId[]; - swimlaneType: SwimlaneType; - viewBy?: string; perPage?: number; + fromPage?: number; + interval?: number; } export interface AnomalySwimlaneServices { @@ -68,7 +81,7 @@ export interface AnomalySwimlaneServices { export type AnomalySwimlaneEmbeddableServices = [ CoreStart, - MlStartDependencies, + MlDependencies, AnomalySwimlaneServices ]; @@ -82,16 +95,13 @@ export class AnomalySwimlaneEmbeddable extends Embeddable< constructor( initialInput: AnomalySwimlaneEmbeddableInput, - private services: [CoreStart, MlStartDependencies, AnomalySwimlaneServices], + public services: [CoreStart, MlDependencies, AnomalySwimlaneServices], parent?: IContainer ) { super( initialInput, { - jobIds: initialInput.jobIds, - swimlaneType: initialInput.swimlaneType, defaultTitle: initialInput.title, - ...(initialInput.viewBy ? { viewBy: initialInput.viewBy } : {}), }, parent ); @@ -107,12 +117,12 @@ export class AnomalySwimlaneEmbeddable extends Embeddable< { - this.updateInput(input); - }} + onInputChange={this.updateInput.bind(this)} + onOutputChange={this.updateOutput.bind(this)} /> , node @@ -129,4 +139,8 @@ export class AnomalySwimlaneEmbeddable extends Embeddable< public reload() { this.reload$.next(); } + + public supportedTriggers() { + return [SWIM_LANE_SELECTION_TRIGGER as typeof SWIM_LANE_SELECTION_TRIGGER]; + } } diff --git a/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/anomaly_swimlane_embeddable_factory.ts b/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/anomaly_swimlane_embeddable_factory.ts index 0d587b428d89b..14fbf77544b21 100644 --- a/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/anomaly_swimlane_embeddable_factory.ts +++ b/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/anomaly_swimlane_embeddable_factory.ts @@ -19,19 +19,22 @@ import { AnomalySwimlaneEmbeddableInput, AnomalySwimlaneEmbeddableServices, } from './anomaly_swimlane_embeddable'; -import { MlStartDependencies } from '../../plugin'; import { HttpService } from '../../application/services/http_service'; import { AnomalyDetectorService } from '../../application/services/anomaly_detector_service'; import { AnomalyTimelineService } from '../../application/services/anomaly_timeline_service'; import { mlResultsServiceProvider } from '../../application/services/results_service'; import { resolveAnomalySwimlaneUserInput } from './anomaly_swimlane_setup_flyout'; import { mlApiServicesProvider } from '../../application/services/ml_api_service'; +import { MlPluginStart, MlStartDependencies } from '../../plugin'; +import { MlDependencies } from '../../application/app'; export class AnomalySwimlaneEmbeddableFactory implements EmbeddableFactoryDefinition { public readonly type = ANOMALY_SWIMLANE_EMBEDDABLE_TYPE; - constructor(private getStartServices: StartServicesAccessor) {} + constructor( + private getStartServices: StartServicesAccessor + ) {} public async isEditable() { return true; @@ -64,7 +67,11 @@ export class AnomalySwimlaneEmbeddableFactory mlResultsServiceProvider(mlApiServicesProvider(httpService)) ); - return [coreStart, pluginsStart, { anomalyDetectorService, anomalyTimelineService }]; + return [ + coreStart, + pluginsStart as MlDependencies, + { anomalyDetectorService, anomalyTimelineService }, + ]; } public async create( diff --git a/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/anomaly_swimlane_initializer.tsx b/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/anomaly_swimlane_initializer.tsx index be9a332e51dbc..e5a13adca05db 100644 --- a/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/anomaly_swimlane_initializer.tsx +++ b/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/anomaly_swimlane_initializer.tsx @@ -17,6 +17,7 @@ import { EuiModalHeaderTitle, EuiSelect, EuiFieldText, + EuiModal, } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; import { i18n } from '@kbn/i18n'; @@ -33,7 +34,6 @@ export interface AnomalySwimlaneInitializerProps { panelTitle: string; swimlaneType: SwimlaneType; viewBy?: string; - limit?: number; }) => void; onCancel: () => void; } @@ -81,7 +81,7 @@ export const AnomalySwimlaneInitializer: FC = ( (swimlaneType === SWIMLANE_TYPE.VIEW_BY && !!viewBySwimlaneFieldName)); return ( -
+ = ( /> -
+ ); }; diff --git a/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/embeddable_swim_lane_container.test.tsx b/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/embeddable_swim_lane_container.test.tsx index 846a3f543c2d4..23045834eae5f 100644 --- a/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/embeddable_swim_lane_container.test.tsx +++ b/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/embeddable_swim_lane_container.test.tsx @@ -6,18 +6,25 @@ import React from 'react'; import { render } from '@testing-library/react'; -import { EmbeddableSwimLaneContainer } from './embeddable_swim_lane_container'; +import { + EmbeddableSwimLaneContainer, + ExplorerSwimlaneContainerProps, +} from './embeddable_swim_lane_container'; import { BehaviorSubject, Observable } from 'rxjs'; import { I18nProvider } from '@kbn/i18n/react'; import { + AnomalySwimlaneEmbeddable, AnomalySwimlaneEmbeddableInput, AnomalySwimlaneServices, } from './anomaly_swimlane_embeddable'; import { CoreStart } from 'kibana/public'; -import { MlStartDependencies } from '../../plugin'; import { useSwimlaneInputResolver } from './swimlane_input_resolver'; import { SWIMLANE_TYPE } from '../../application/explorer/explorer_constants'; import { SwimlaneContainer } from '../../application/explorer/swimlane_container'; +import { MlDependencies } from '../../application/app'; +import { uiActionsPluginMock } from 'src/plugins/ui_actions/public/mocks'; +import { TriggerContract } from 'src/plugins/ui_actions/public/triggers'; +import { TriggerId } from 'src/plugins/ui_actions/public'; jest.mock('./swimlane_input_resolver', () => ({ useSwimlaneInputResolver: jest.fn(() => { @@ -37,13 +44,30 @@ const defaultOptions = { wrapper: I18nProvider }; describe('ExplorerSwimlaneContainer', () => { let embeddableInput: BehaviorSubject>; let refresh: BehaviorSubject; - let services: [CoreStart, MlStartDependencies, AnomalySwimlaneServices]; + let services: jest.Mocked<[CoreStart, MlDependencies, AnomalySwimlaneServices]>; + let embeddableContext: AnomalySwimlaneEmbeddable; + let trigger: jest.Mocked>; + const onInputChange = jest.fn(); + const onOutputChange = jest.fn(); beforeEach(() => { + embeddableContext = { id: 'test-id' } as AnomalySwimlaneEmbeddable; embeddableInput = new BehaviorSubject({ id: 'test-swimlane-embeddable', } as Partial); + + trigger = ({ exec: jest.fn() } as unknown) as jest.Mocked>; + + const uiActionsMock = uiActionsPluginMock.createStartContract(); + uiActionsMock.getTrigger.mockReturnValue(trigger); + + services = ([ + {}, + { + uiActions: uiActionsMock, + }, + ] as unknown) as ExplorerSwimlaneContainerProps['services']; }); test('should render a swimlane with a valid embeddable input', async () => { @@ -74,12 +98,14 @@ describe('ExplorerSwimlaneContainer', () => { render( } services={services} refresh={refresh} onInputChange={onInputChange} + onOutputChange={onOutputChange} />, defaultOptions ); @@ -110,6 +136,7 @@ describe('ExplorerSwimlaneContainer', () => { const { findByText } = render( @@ -117,6 +144,7 @@ describe('ExplorerSwimlaneContainer', () => { services={services} refresh={refresh} onInputChange={onInputChange} + onOutputChange={onOutputChange} />, defaultOptions ); diff --git a/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/embeddable_swim_lane_container.tsx b/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/embeddable_swim_lane_container.tsx index 5d91bdb41df6a..8ee4e391fcdde 100644 --- a/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/embeddable_swim_lane_container.tsx +++ b/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/embeddable_swim_lane_container.tsx @@ -4,14 +4,14 @@ * you may not use this file except in compliance with the Elastic License. */ -import React, { FC, useState } from 'react'; +import React, { FC, useCallback, useState, useEffect } from 'react'; import { EuiCallOut } from '@elastic/eui'; import { Observable } from 'rxjs'; import { CoreStart } from 'kibana/public'; import { FormattedMessage } from '@kbn/i18n/react'; -import { MlStartDependencies } from '../../plugin'; import { + AnomalySwimlaneEmbeddable, AnomalySwimlaneEmbeddableInput, AnomalySwimlaneEmbeddableOutput, AnomalySwimlaneServices, @@ -22,25 +22,36 @@ import { isViewBySwimLaneData, SwimlaneContainer, } from '../../application/explorer/swimlane_container'; +import { AppStateSelectedCells } from '../../application/explorer/explorer_utils'; +import { MlDependencies } from '../../application/app'; +import { SWIM_LANE_SELECTION_TRIGGER } from '../../ui_actions/triggers'; export interface ExplorerSwimlaneContainerProps { id: string; + embeddableContext: AnomalySwimlaneEmbeddable; embeddableInput: Observable; - services: [CoreStart, MlStartDependencies, AnomalySwimlaneServices]; + services: [CoreStart, MlDependencies, AnomalySwimlaneServices]; refresh: Observable; - onInputChange: (output: Partial) => void; + onInputChange: (input: Partial) => void; + onOutputChange: (output: Partial) => void; } export const EmbeddableSwimLaneContainer: FC = ({ id, + embeddableContext, embeddableInput, services, refresh, onInputChange, + onOutputChange, }) => { const [chartWidth, setChartWidth] = useState(0); const [fromPage, setFromPage] = useState(1); + const [{}, { uiActions }] = services; + + const [selectedCells, setSelectedCells] = useState(); + const [ swimlaneType, swimlaneData, @@ -58,6 +69,28 @@ export const EmbeddableSwimLaneContainer: FC = ( fromPage ); + useEffect(() => { + onOutputChange({ + perPage, + fromPage, + interval: swimlaneData?.interval, + }); + }, [perPage, fromPage, swimlaneData]); + + const onCellsSelection = useCallback( + (update?: AppStateSelectedCells) => { + setSelectedCells(update); + + if (update) { + uiActions.getTrigger(SWIM_LANE_SELECTION_TRIGGER).exec({ + embeddable: embeddableContext, + data: update, + }); + } + }, + [swimlaneData, perPage, fromPage] + ); + if (error) { return ( = ( data-test-subj="mlAnomalySwimlaneEmbeddableWrapper" > { - setChartWidth(width); - }} + onResize={setChartWidth} + selection={selectedCells} + onCellsSelection={onCellsSelection} onPaginationChange={(update) => { if (update.fromPage) { setFromPage(update.fromPage); diff --git a/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/swimlane_input_resolver.ts b/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/swimlane_input_resolver.ts index 9ed6f88150f68..f17c779a00252 100644 --- a/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/swimlane_input_resolver.ts +++ b/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/swimlane_input_resolver.ts @@ -40,6 +40,7 @@ import { parseInterval } from '../../../common/util/parse_interval'; import { AnomalyDetectorService } from '../../application/services/anomaly_detector_service'; import { isViewBySwimLaneData } from '../../application/explorer/swimlane_container'; import { ViewMode } from '../../../../../../src/plugins/embeddable/public'; +import { CONTROLLED_BY_SWIM_LANE_FILTER } from '../../ui_actions/apply_influencer_filters_action'; const FETCH_RESULTS_DEBOUNCE_MS = 500; @@ -240,7 +241,9 @@ export function processFilters(filters: Filter[], query: Query) { const must = [inputQuery]; const mustNot = []; for (const filter of filters) { - if (filter.meta.disabled) continue; + // ignore disabled filters as well as created by swim lane selection + if (filter.meta.disabled || filter.meta.controlledBy === CONTROLLED_BY_SWIM_LANE_FILTER) + continue; const { meta: { negate, type, key: fieldName }, diff --git a/x-pack/plugins/ml/public/embeddables/index.ts b/x-pack/plugins/ml/public/embeddables/index.ts index 5e9d54645b516..db9f094d5721e 100644 --- a/x-pack/plugins/ml/public/embeddables/index.ts +++ b/x-pack/plugins/ml/public/embeddables/index.ts @@ -4,15 +4,11 @@ * you may not use this file except in compliance with the Elastic License. */ -import { CoreSetup } from 'kibana/public'; import { AnomalySwimlaneEmbeddableFactory } from './anomaly_swimlane'; -import { MlPluginStart, MlStartDependencies } from '../plugin'; +import { MlCoreSetup } from '../plugin'; import { EmbeddableSetup } from '../../../../../src/plugins/embeddable/public'; -export function registerEmbeddables( - embeddable: EmbeddableSetup, - core: CoreSetup -) { +export function registerEmbeddables(embeddable: EmbeddableSetup, core: MlCoreSetup) { const anomalySwimlaneEmbeddableFactory = new AnomalySwimlaneEmbeddableFactory( core.getStartServices ); diff --git a/x-pack/plugins/ml/public/plugin.ts b/x-pack/plugins/ml/public/plugin.ts index 7f7544a44efa7..449d8baa2a184 100644 --- a/x-pack/plugins/ml/public/plugin.ts +++ b/x-pack/plugins/ml/public/plugin.ts @@ -13,7 +13,7 @@ import { PluginInitializerContext, } from 'kibana/public'; import { ManagementSetup } from 'src/plugins/management/public'; -import { SharePluginStart } from 'src/plugins/share/public'; +import { SharePluginSetup, SharePluginStart, UrlGeneratorState } from 'src/plugins/share/public'; import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import { DataPublicPluginStart } from 'src/plugins/data/public'; @@ -28,14 +28,16 @@ import { PLUGIN_ID, PLUGIN_ICON } from '../common/constants/app'; import { registerFeature } from './register_feature'; import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/public'; import { registerEmbeddables } from './embeddables'; -import { UiActionsSetup } from '../../../../src/plugins/ui_actions/public'; +import { UiActionsSetup, UiActionsStart } from '../../../../src/plugins/ui_actions/public'; import { registerMlUiActions } from './ui_actions'; import { KibanaLegacyStart } from '../../../../src/plugins/kibana_legacy/public'; +import { MlUrlGenerator, MlUrlGeneratorState, ML_APP_URL_GENERATOR } from './url_generator'; export interface MlStartDependencies { data: DataPublicPluginStart; share: SharePluginStart; kibanaLegacy: KibanaLegacyStart; + uiActions: UiActionsStart; } export interface MlSetupDependencies { security?: SecurityPluginSetup; @@ -47,13 +49,30 @@ export interface MlSetupDependencies { embeddable: EmbeddableSetup; uiActions: UiActionsSetup; kibanaVersion: string; - share: SharePluginStart; + share: SharePluginSetup; +} + +declare module '../../../../src/plugins/share/public' { + export interface UrlGeneratorStateMapping { + [ML_APP_URL_GENERATOR]: UrlGeneratorState; + } } +export type MlCoreSetup = CoreSetup; + export class MlPlugin implements Plugin { constructor(private initializerContext: PluginInitializerContext) {} - setup(core: CoreSetup, pluginsSetup: MlSetupDependencies) { + setup(core: MlCoreSetup, pluginsSetup: MlSetupDependencies) { + const baseUrl = core.http.basePath.prepend('/app/ml'); + + pluginsSetup.share.urlGenerators.registerUrlGenerator( + new MlUrlGenerator({ + appBasePath: baseUrl, + useHash: core.uiSettings.get('state:storeInSessionStorage'), + }) + ); + core.application.register({ id: PLUGIN_ID, title: i18n.translate('xpack.ml.plugin.title', { @@ -80,7 +99,7 @@ export class MlPlugin implements Plugin { licenseManagement: pluginsSetup.licenseManagement, home: pluginsSetup.home, embeddable: pluginsSetup.embeddable, - uiActions: pluginsSetup.uiActions, + uiActions: pluginsStart.uiActions, kibanaVersion, }, { @@ -96,10 +115,8 @@ export class MlPlugin implements Plugin { registerFeature(pluginsSetup.home); initManagementSection(pluginsSetup, core); - - registerMlUiActions(pluginsSetup.uiActions, core); - registerEmbeddables(pluginsSetup.embeddable, core); + registerMlUiActions(pluginsSetup.uiActions, core); return {}; } @@ -113,6 +130,7 @@ export class MlPlugin implements Plugin { }); return {}; } + public stop() {} } diff --git a/x-pack/plugins/ml/public/ui_actions/apply_influencer_filters_action.tsx b/x-pack/plugins/ml/public/ui_actions/apply_influencer_filters_action.tsx new file mode 100644 index 0000000000000..3af39993d39fd --- /dev/null +++ b/x-pack/plugins/ml/public/ui_actions/apply_influencer_filters_action.tsx @@ -0,0 +1,84 @@ +/* + * 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 { ActionContextMapping, createAction } from '../../../../../src/plugins/ui_actions/public'; +import { + AnomalySwimlaneEmbeddable, + SwimLaneDrilldownContext, +} from '../embeddables/anomaly_swimlane/anomaly_swimlane_embeddable'; +import { MlCoreSetup } from '../plugin'; +import { SWIMLANE_TYPE, VIEW_BY_JOB_LABEL } from '../application/explorer/explorer_constants'; +import { Filter, FilterStateStore } from '../../../../../src/plugins/data/common'; + +export const APPLY_INFLUENCER_FILTERS_ACTION = 'applyInfluencerFiltersAction'; + +export const CONTROLLED_BY_SWIM_LANE_FILTER = 'anomaly-swim-lane'; + +export function createApplyInfluencerFiltersAction( + getStartServices: MlCoreSetup['getStartServices'] +) { + return createAction({ + id: 'apply-to-current-view', + type: APPLY_INFLUENCER_FILTERS_ACTION, + getIconType(context: ActionContextMapping[typeof APPLY_INFLUENCER_FILTERS_ACTION]): string { + return 'filter'; + }, + getDisplayName() { + return i18n.translate('xpack.ml.actions.applyInfluencersFiltersTitle', { + defaultMessage: 'Filer for value', + }); + }, + async execute({ data }: SwimLaneDrilldownContext) { + if (!data) { + throw new Error('No swim lane selection data provided'); + } + const [, pluginStart] = await getStartServices(); + const filterManager = pluginStart.data.query.filterManager; + + filterManager.addFilters( + data.lanes.map((influencerValue) => { + return { + $state: { + store: FilterStateStore.APP_STATE, + }, + meta: { + alias: i18n.translate('xpack.ml.actions.influencerFilterAliasLabel', { + defaultMessage: 'Influencer {labelValue}', + values: { + labelValue: `${data.viewByFieldName}:${influencerValue}`, + }, + }), + controlledBy: CONTROLLED_BY_SWIM_LANE_FILTER, + disabled: false, + key: data.viewByFieldName, + negate: false, + params: { + query: influencerValue, + }, + type: 'phrase', + }, + query: { + match_phrase: { + [data.viewByFieldName!]: influencerValue, + }, + }, + }; + }) + ); + }, + async isCompatible({ embeddable, data }: SwimLaneDrilldownContext) { + // Only compatible with view by influencer swim lanes and single selection + return ( + embeddable instanceof AnomalySwimlaneEmbeddable && + data !== undefined && + data.type === SWIMLANE_TYPE.VIEW_BY && + data.viewByFieldName !== VIEW_BY_JOB_LABEL && + data.lanes.length === 1 + ); + }, + }); +} diff --git a/x-pack/plugins/ml/public/ui_actions/apply_time_range_action.tsx b/x-pack/plugins/ml/public/ui_actions/apply_time_range_action.tsx new file mode 100644 index 0000000000000..ec59ba20acf98 --- /dev/null +++ b/x-pack/plugins/ml/public/ui_actions/apply_time_range_action.tsx @@ -0,0 +1,58 @@ +/* + * 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 moment from 'moment'; +import { ActionContextMapping, createAction } from '../../../../../src/plugins/ui_actions/public'; +import { + AnomalySwimlaneEmbeddable, + SwimLaneDrilldownContext, +} from '../embeddables/anomaly_swimlane/anomaly_swimlane_embeddable'; +import { MlCoreSetup } from '../plugin'; + +export const APPLY_TIME_RANGE_SELECTION_ACTION = 'applyTimeRangeSelectionAction'; + +export function createApplyTimeRangeSelectionAction( + getStartServices: MlCoreSetup['getStartServices'] +) { + return createAction({ + id: 'apply-time-range-selection', + type: APPLY_TIME_RANGE_SELECTION_ACTION, + getIconType(context: ActionContextMapping[typeof APPLY_TIME_RANGE_SELECTION_ACTION]): string { + return 'timeline'; + }, + getDisplayName: () => + i18n.translate('xpack.ml.actions.applyTimeRangeSelectionTitle', { + defaultMessage: 'Apply time range selection', + }), + async execute({ embeddable, data }: SwimLaneDrilldownContext) { + if (!data) { + throw new Error('No swim lane selection data provided'); + } + const [, pluginStart] = await getStartServices(); + const timefilter = pluginStart.data.query.timefilter.timefilter; + const { interval } = embeddable.getOutput(); + + if (!interval) { + throw new Error('Interval is required to set a time range'); + } + + let [from, to] = data.times; + from = from * 1000; + // extend bounds with the interval + to = to * 1000 + interval * 1000; + + timefilter.setTime({ + from: moment(from), + to: moment(to), + mode: 'absolute', + }); + }, + async isCompatible({ embeddable, data }: SwimLaneDrilldownContext) { + return embeddable instanceof AnomalySwimlaneEmbeddable && data !== undefined; + }, + }); +} diff --git a/x-pack/plugins/ml/public/ui_actions/edit_swimlane_panel_action.tsx b/x-pack/plugins/ml/public/ui_actions/edit_swimlane_panel_action.tsx index 0db41c1ed104e..cfd90f92e3238 100644 --- a/x-pack/plugins/ml/public/ui_actions/edit_swimlane_panel_action.tsx +++ b/x-pack/plugins/ml/public/ui_actions/edit_swimlane_panel_action.tsx @@ -4,24 +4,19 @@ * you may not use this file except in compliance with the Elastic License. */ -import { CoreSetup } from 'kibana/public'; import { i18n } from '@kbn/i18n'; import { ActionContextMapping, createAction } from '../../../../../src/plugins/ui_actions/public'; -import { IEmbeddable } from '../../../../../src/plugins/embeddable/public'; import { AnomalySwimlaneEmbeddable, - AnomalySwimlaneEmbeddableInput, - AnomalySwimlaneEmbeddableOutput, + EditSwimlanePanelContext, } from '../embeddables/anomaly_swimlane/anomaly_swimlane_embeddable'; import { resolveAnomalySwimlaneUserInput } from '../embeddables/anomaly_swimlane/anomaly_swimlane_setup_flyout'; +import { ViewMode } from '../../../../../src/plugins/embeddable/public'; +import { MlCoreSetup } from '../plugin'; export const EDIT_SWIMLANE_PANEL_ACTION = 'editSwimlanePanelAction'; -export interface EditSwimlanePanelContext { - embeddable: IEmbeddable; -} - -export function createEditSwimlanePanelAction(getStartServices: CoreSetup['getStartServices']) { +export function createEditSwimlanePanelAction(getStartServices: MlCoreSetup['getStartServices']) { return createAction({ id: 'edit-anomaly-swimlane', type: EDIT_SWIMLANE_PANEL_ACTION, @@ -48,7 +43,8 @@ export function createEditSwimlanePanelAction(getStartServices: CoreSetup['getSt }, isCompatible: async ({ embeddable }: EditSwimlanePanelContext) => { return ( - embeddable instanceof AnomalySwimlaneEmbeddable && embeddable.getInput().viewMode === 'edit' + embeddable instanceof AnomalySwimlaneEmbeddable && + embeddable.getInput().viewMode === ViewMode.EDIT ); }, }); diff --git a/x-pack/plugins/ml/public/ui_actions/index.ts b/x-pack/plugins/ml/public/ui_actions/index.ts index 4a1535c4e8c2e..b7262a330b310 100644 --- a/x-pack/plugins/ml/public/ui_actions/index.ts +++ b/x-pack/plugins/ml/public/ui_actions/index.ts @@ -8,23 +8,65 @@ import { CoreSetup } from 'kibana/public'; import { createEditSwimlanePanelAction, EDIT_SWIMLANE_PANEL_ACTION, - EditSwimlanePanelContext, } from './edit_swimlane_panel_action'; +import { + createOpenInExplorerAction, + OPEN_IN_ANOMALY_EXPLORER_ACTION, +} from './open_in_anomaly_explorer_action'; +import { EditSwimlanePanelContext } from '../embeddables/anomaly_swimlane/anomaly_swimlane_embeddable'; import { UiActionsSetup } from '../../../../../src/plugins/ui_actions/public'; import { MlPluginStart, MlStartDependencies } from '../plugin'; import { CONTEXT_MENU_TRIGGER } from '../../../../../src/plugins/embeddable/public'; +import { + APPLY_INFLUENCER_FILTERS_ACTION, + createApplyInfluencerFiltersAction, +} from './apply_influencer_filters_action'; +import { SWIM_LANE_SELECTION_TRIGGER, swimLaneSelectionTrigger } from './triggers'; +import { SwimLaneDrilldownContext } from '../embeddables/anomaly_swimlane/anomaly_swimlane_embeddable'; +import { + APPLY_TIME_RANGE_SELECTION_ACTION, + createApplyTimeRangeSelectionAction, +} from './apply_time_range_action'; +/** + * Register ML UI actions + */ export function registerMlUiActions( uiActions: UiActionsSetup, core: CoreSetup ) { + // Initialize actions const editSwimlanePanelAction = createEditSwimlanePanelAction(core.getStartServices); + const openInExplorerAction = createOpenInExplorerAction(core.getStartServices); + const applyInfluencerFiltersAction = createApplyInfluencerFiltersAction(core.getStartServices); + const applyTimeRangeSelectionAction = createApplyTimeRangeSelectionAction(core.getStartServices); + + // Register actions uiActions.registerAction(editSwimlanePanelAction); + uiActions.registerAction(openInExplorerAction); + uiActions.registerAction(applyInfluencerFiltersAction); + uiActions.registerAction(applyTimeRangeSelectionAction); + + // Assign triggers uiActions.attachAction(CONTEXT_MENU_TRIGGER, editSwimlanePanelAction.id); + uiActions.attachAction(CONTEXT_MENU_TRIGGER, openInExplorerAction.id); + + uiActions.registerTrigger(swimLaneSelectionTrigger); + + uiActions.addTriggerAction(SWIM_LANE_SELECTION_TRIGGER, applyInfluencerFiltersAction); + uiActions.addTriggerAction(SWIM_LANE_SELECTION_TRIGGER, applyTimeRangeSelectionAction); + uiActions.addTriggerAction(SWIM_LANE_SELECTION_TRIGGER, openInExplorerAction); } declare module '../../../../../src/plugins/ui_actions/public' { export interface ActionContextMapping { [EDIT_SWIMLANE_PANEL_ACTION]: EditSwimlanePanelContext; + [OPEN_IN_ANOMALY_EXPLORER_ACTION]: SwimLaneDrilldownContext; + [APPLY_INFLUENCER_FILTERS_ACTION]: SwimLaneDrilldownContext; + [APPLY_TIME_RANGE_SELECTION_ACTION]: SwimLaneDrilldownContext; + } + + export interface TriggerContextMapping { + [SWIM_LANE_SELECTION_TRIGGER]: SwimLaneDrilldownContext; } } diff --git a/x-pack/plugins/ml/public/ui_actions/open_in_anomaly_explorer_action.tsx b/x-pack/plugins/ml/public/ui_actions/open_in_anomaly_explorer_action.tsx new file mode 100644 index 0000000000000..211840467e38c --- /dev/null +++ b/x-pack/plugins/ml/public/ui_actions/open_in_anomaly_explorer_action.tsx @@ -0,0 +1,66 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { i18n } from '@kbn/i18n'; +import { ActionContextMapping, createAction } from '../../../../../src/plugins/ui_actions/public'; +import { + AnomalySwimlaneEmbeddable, + SwimLaneDrilldownContext, +} from '../embeddables/anomaly_swimlane/anomaly_swimlane_embeddable'; +import { MlCoreSetup } from '../plugin'; +import { ML_APP_URL_GENERATOR } from '../url_generator'; + +export const OPEN_IN_ANOMALY_EXPLORER_ACTION = 'openInAnomalyExplorerAction'; + +export function createOpenInExplorerAction(getStartServices: MlCoreSetup['getStartServices']) { + return createAction({ + id: 'open-in-anomaly-explorer', + type: OPEN_IN_ANOMALY_EXPLORER_ACTION, + getIconType(context: ActionContextMapping[typeof OPEN_IN_ANOMALY_EXPLORER_ACTION]): string { + return 'tableOfContents'; + }, + getDisplayName() { + return i18n.translate('xpack.ml.actions.openInAnomalyExplorerTitle', { + defaultMessage: 'Open in Anomaly Explorer', + }); + }, + async getHref({ embeddable, data }: SwimLaneDrilldownContext): Promise { + const [, pluginsStart] = await getStartServices(); + const urlGenerator = pluginsStart.share.urlGenerators.getUrlGenerator(ML_APP_URL_GENERATOR); + const { jobIds, timeRange, viewBy } = embeddable.getInput(); + const { perPage, fromPage } = embeddable.getOutput(); + + return urlGenerator.createUrl({ + page: 'explorer', + jobIds, + timeRange, + mlExplorerSwimlane: { + viewByFromPage: fromPage, + viewByPerPage: perPage, + viewByFieldName: viewBy, + ...(data + ? { + selectedType: data.type, + selectedTimes: data.times, + selectedLanes: data.lanes, + } + : {}), + }, + }); + }, + async execute({ embeddable, data }: SwimLaneDrilldownContext) { + if (!embeddable) { + throw new Error('Not possible to execute an action without the embeddable context'); + } + const [{ application }] = await getStartServices(); + const anomalyExplorerUrl = await this.getHref!({ embeddable, data }); + await application.navigateToUrl(anomalyExplorerUrl!); + }, + async isCompatible({ embeddable }: SwimLaneDrilldownContext) { + return embeddable instanceof AnomalySwimlaneEmbeddable; + }, + }); +} diff --git a/x-pack/plugins/ml/public/ui_actions/triggers.ts b/x-pack/plugins/ml/public/ui_actions/triggers.ts new file mode 100644 index 0000000000000..8a8b2602573a1 --- /dev/null +++ b/x-pack/plugins/ml/public/ui_actions/triggers.ts @@ -0,0 +1,17 @@ +/* + * 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 { Trigger } from '../../../../../src/plugins/ui_actions/public'; + +export const SWIM_LANE_SELECTION_TRIGGER = 'SWIM_LANE_SELECTION_TRIGGER'; + +export const swimLaneSelectionTrigger: Trigger<'SWIM_LANE_SELECTION_TRIGGER'> = { + id: SWIM_LANE_SELECTION_TRIGGER, + // This is empty string to hide title of ui_actions context menu that appears + // when this trigger is executed. + title: '', + description: 'Swim lane selection triggered', +}; diff --git a/x-pack/plugins/ml/public/url_generator.test.ts b/x-pack/plugins/ml/public/url_generator.test.ts new file mode 100644 index 0000000000000..45e2932b7781a --- /dev/null +++ b/x-pack/plugins/ml/public/url_generator.test.ts @@ -0,0 +1,34 @@ +/* + * 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 { MlUrlGenerator } from './url_generator'; + +describe('MlUrlGenerator', () => { + const urlGenerator = new MlUrlGenerator({ + appBasePath: '/app/ml', + useHash: false, + }); + + it('should generate valid URL for the Anomaly Explorer page', async () => { + const url = await urlGenerator.createUrl({ + page: 'explorer', + jobIds: ['test-job'], + mlExplorerSwimlane: { viewByFromPage: 2, viewByPerPage: 20 }, + }); + expect(url).toBe( + '/app/ml#/explorer?_g=(ml:(jobIds:!(test-job)))&_a=(mlExplorerFilter:(),mlExplorerSwimlane:(viewByFromPage:2,viewByPerPage:20))' + ); + }); + + it('should throw an error in case the page is not provided', async () => { + expect.assertions(1); + + // @ts-ignore + await urlGenerator.createUrl({ jobIds: ['test-job'] }).catch((e) => { + expect(e.message).toEqual('Page type is not provided or unknown'); + }); + }); +}); diff --git a/x-pack/plugins/ml/public/url_generator.ts b/x-pack/plugins/ml/public/url_generator.ts new file mode 100644 index 0000000000000..65d5077e081a3 --- /dev/null +++ b/x-pack/plugins/ml/public/url_generator.ts @@ -0,0 +1,90 @@ +/* + * 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 { UrlGeneratorsDefinition } from '../../../../src/plugins/share/public'; +import { TimeRange } from '../../../../src/plugins/data/public'; +import { setStateToKbnUrl } from '../../../../src/plugins/kibana_utils/public'; +import { JobId } from '../../reporting/common/types'; +import { ExplorerAppState } from './application/explorer/explorer_dashboard_service'; + +export const ML_APP_URL_GENERATOR = 'ML_APP_URL_GENERATOR'; + +export interface ExplorerUrlState { + /** + * ML App Page + */ + page: 'explorer'; + /** + * Job IDs + */ + jobIds: JobId[]; + /** + * Optionally set the time range in the time picker. + */ + timeRange?: TimeRange; + /** + * Optional state for the swim lane + */ + mlExplorerSwimlane?: ExplorerAppState['mlExplorerSwimlane']; + mlExplorerFilter?: ExplorerAppState['mlExplorerFilter']; +} + +/** + * Union type of ML URL state based on page + */ +export type MlUrlGeneratorState = ExplorerUrlState; + +export interface ExplorerQueryState { + ml: { jobIds: JobId[] }; + time?: TimeRange; +} + +interface Params { + appBasePath: string; + useHash: boolean; +} + +export class MlUrlGenerator implements UrlGeneratorsDefinition { + constructor(private readonly params: Params) {} + + public readonly id = ML_APP_URL_GENERATOR; + + public readonly createUrl = async ({ page, ...params }: MlUrlGeneratorState): Promise => { + if (page === 'explorer') { + return this.createExplorerUrl(params); + } + throw new Error('Page type is not provided or unknown'); + }; + + /** + * Creates URL to the Anomaly Explorer page + */ + private createExplorerUrl({ + timeRange, + jobIds, + mlExplorerSwimlane = {}, + mlExplorerFilter = {}, + }: Omit): string { + const appState: ExplorerAppState = { + mlExplorerSwimlane, + mlExplorerFilter, + }; + + const queryState: ExplorerQueryState = { + ml: { + jobIds, + }, + }; + + if (timeRange) queryState.time = timeRange; + + let url = `${this.params.appBasePath}#/explorer`; + url = setStateToKbnUrl('_g', queryState, { useHash: false }, url); + url = setStateToKbnUrl('_a', appState, { useHash: false }, url); + + return url; + } +} diff --git a/x-pack/plugins/ml/server/lib/capabilities/check_capabilities.test.ts b/x-pack/plugins/ml/server/lib/capabilities/check_capabilities.test.ts index 8e18b57ac92a8..21d32813c0d51 100644 --- a/x-pack/plugins/ml/server/lib/capabilities/check_capabilities.test.ts +++ b/x-pack/plugins/ml/server/lib/capabilities/check_capabilities.test.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { getAdminCapabilities, getUserCapabilities } from './__mocks__/ml_capabilities'; import { capabilitiesProvider } from './check_capabilities'; import { MlLicense } from '../../../common/license'; @@ -23,18 +23,23 @@ const mlLicenseBasic = { const mlIsEnabled = async () => true; const mlIsNotEnabled = async () => false; -const callWithRequestNonUpgrade = ((async () => ({ - upgrade_mode: false, -})) as unknown) as LegacyAPICaller; -const callWithRequestUpgrade = ((async () => ({ - upgrade_mode: true, -})) as unknown) as LegacyAPICaller; +const mlClusterClientNonUpgrade = ({ + callAsInternalUser: async () => ({ + upgrade_mode: false, + }), +} as unknown) as ILegacyScopedClusterClient; + +const mlClusterClientUpgrade = ({ + callAsInternalUser: async () => ({ + upgrade_mode: true, + }), +} as unknown) as ILegacyScopedClusterClient; describe('check_capabilities', () => { describe('getCapabilities() - right number of capabilities', () => { test('kibana capabilities count', async (done) => { const { getCapabilities } = capabilitiesProvider( - callWithRequestNonUpgrade, + mlClusterClientNonUpgrade, getAdminCapabilities(), mlLicense, mlIsEnabled @@ -49,7 +54,7 @@ describe('check_capabilities', () => { describe('getCapabilities() with security', () => { test('ml_user capabilities only', async (done) => { const { getCapabilities } = capabilitiesProvider( - callWithRequestNonUpgrade, + mlClusterClientNonUpgrade, getUserCapabilities(), mlLicense, mlIsEnabled @@ -98,7 +103,7 @@ describe('check_capabilities', () => { test('full capabilities', async (done) => { const { getCapabilities } = capabilitiesProvider( - callWithRequestNonUpgrade, + mlClusterClientNonUpgrade, getAdminCapabilities(), mlLicense, mlIsEnabled @@ -147,7 +152,7 @@ describe('check_capabilities', () => { test('upgrade in progress with full capabilities', async (done) => { const { getCapabilities } = capabilitiesProvider( - callWithRequestUpgrade, + mlClusterClientUpgrade, getAdminCapabilities(), mlLicense, mlIsEnabled @@ -196,7 +201,7 @@ describe('check_capabilities', () => { test('upgrade in progress with partial capabilities', async (done) => { const { getCapabilities } = capabilitiesProvider( - callWithRequestUpgrade, + mlClusterClientUpgrade, getUserCapabilities(), mlLicense, mlIsEnabled @@ -245,7 +250,7 @@ describe('check_capabilities', () => { test('full capabilities, ml disabled in space', async (done) => { const { getCapabilities } = capabilitiesProvider( - callWithRequestNonUpgrade, + mlClusterClientNonUpgrade, getDefaultCapabilities(), mlLicense, mlIsNotEnabled @@ -295,7 +300,7 @@ describe('check_capabilities', () => { test('full capabilities, basic license, ml disabled in space', async (done) => { const { getCapabilities } = capabilitiesProvider( - callWithRequestNonUpgrade, + mlClusterClientNonUpgrade, getDefaultCapabilities(), mlLicenseBasic, mlIsNotEnabled diff --git a/x-pack/plugins/ml/server/lib/capabilities/check_capabilities.ts b/x-pack/plugins/ml/server/lib/capabilities/check_capabilities.ts index bdcdf50b983f5..c976ab598b28c 100644 --- a/x-pack/plugins/ml/server/lib/capabilities/check_capabilities.ts +++ b/x-pack/plugins/ml/server/lib/capabilities/check_capabilities.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller, KibanaRequest } from 'kibana/server'; +import { ILegacyScopedClusterClient, KibanaRequest } from 'kibana/server'; import { mlLog } from '../../client/log'; import { MlCapabilities, @@ -22,12 +22,12 @@ import { } from './errors'; export function capabilitiesProvider( - callAsCurrentUser: LegacyAPICaller, + mlClusterClient: ILegacyScopedClusterClient, capabilities: MlCapabilities, mlLicense: MlLicense, isMlEnabledInSpace: () => Promise ) { - const { isUpgradeInProgress } = upgradeCheckProvider(callAsCurrentUser); + const { isUpgradeInProgress } = upgradeCheckProvider(mlClusterClient); async function getCapabilities(): Promise { const upgradeInProgress = await isUpgradeInProgress(); const isPlatinumOrTrialLicense = mlLicense.isFullLicense(); diff --git a/x-pack/plugins/ml/server/lib/capabilities/upgrade.ts b/x-pack/plugins/ml/server/lib/capabilities/upgrade.ts index 45f3f3da20c24..6df4d0c87ecf5 100644 --- a/x-pack/plugins/ml/server/lib/capabilities/upgrade.ts +++ b/x-pack/plugins/ml/server/lib/capabilities/upgrade.ts @@ -4,14 +4,14 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { mlLog } from '../../client/log'; -export function upgradeCheckProvider(callAsCurrentUser: LegacyAPICaller) { +export function upgradeCheckProvider({ callAsInternalUser }: ILegacyScopedClusterClient) { async function isUpgradeInProgress(): Promise { let upgradeInProgress = false; try { - const info = await callAsCurrentUser('ml.info'); + const info = await callAsInternalUser('ml.info'); // if ml indices are currently being migrated, upgrade_mode will be set to true // pass this back with the privileges to allow for the disabling of UI controls. upgradeInProgress = info.upgrade_mode === true; diff --git a/x-pack/plugins/ml/server/lib/check_annotations/index.ts b/x-pack/plugins/ml/server/lib/check_annotations/index.ts index 2c46be394cbb2..fb37917c512cb 100644 --- a/x-pack/plugins/ml/server/lib/check_annotations/index.ts +++ b/x-pack/plugins/ml/server/lib/check_annotations/index.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { mlLog } from '../../client/log'; import { @@ -17,7 +17,9 @@ import { // - ML_ANNOTATIONS_INDEX_PATTERN index is present // - ML_ANNOTATIONS_INDEX_ALIAS_READ alias is present // - ML_ANNOTATIONS_INDEX_ALIAS_WRITE alias is present -export async function isAnnotationsFeatureAvailable(callAsCurrentUser: LegacyAPICaller) { +export async function isAnnotationsFeatureAvailable({ + callAsCurrentUser, +}: ILegacyScopedClusterClient) { try { const indexParams = { index: ML_ANNOTATIONS_INDEX_PATTERN }; diff --git a/x-pack/plugins/ml/server/lib/request_authorization.ts b/x-pack/plugins/ml/server/lib/request_authorization.ts new file mode 100644 index 0000000000000..01df0900b96f4 --- /dev/null +++ b/x-pack/plugins/ml/server/lib/request_authorization.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 { KibanaRequest } from 'kibana/server'; + +export function getAuthorizationHeader(request: KibanaRequest) { + return { + headers: { 'es-secondary-authorization': request.headers.authorization }, + }; +} diff --git a/x-pack/plugins/ml/server/models/annotation_service/annotation.test.ts b/x-pack/plugins/ml/server/models/annotation_service/annotation.test.ts index 19db8b7b56aa6..3bf9bd0232a5d 100644 --- a/x-pack/plugins/ml/server/models/annotation_service/annotation.test.ts +++ b/x-pack/plugins/ml/server/models/annotation_service/annotation.test.ts @@ -6,7 +6,6 @@ import getAnnotationsRequestMock from './__mocks__/get_annotations_request.json'; import getAnnotationsResponseMock from './__mocks__/get_annotations_response.json'; -import { LegacyAPICaller } from 'kibana/server'; import { ANNOTATION_TYPE } from '../../../common/constants/annotations'; import { ML_ANNOTATIONS_INDEX_ALIAS_WRITE } from '../../../common/constants/index_patterns'; @@ -20,10 +19,10 @@ const acknowledgedResponseMock = { acknowledged: true }; const jobIdMock = 'jobIdMock'; describe('annotation_service', () => { - let callWithRequestSpy: any; + let mlClusterClientSpy = {} as any; beforeEach(() => { - callWithRequestSpy = (jest.fn((action: string) => { + const callAs = jest.fn((action: string) => { switch (action) { case 'delete': case 'index': @@ -31,13 +30,18 @@ describe('annotation_service', () => { case 'search': return Promise.resolve(getAnnotationsResponseMock); } - }) as unknown) as LegacyAPICaller; + }); + + mlClusterClientSpy = { + callAsCurrentUser: callAs, + callAsInternalUser: callAs, + }; }); describe('deleteAnnotation()', () => { it('should delete annotation', async (done) => { - const { deleteAnnotation } = annotationServiceProvider(callWithRequestSpy); - const mockFunct = callWithRequestSpy; + const { deleteAnnotation } = annotationServiceProvider(mlClusterClientSpy); + const mockFunct = mlClusterClientSpy; const annotationMockId = 'mockId'; const deleteParamsMock: DeleteParams = { @@ -48,8 +52,8 @@ describe('annotation_service', () => { const response = await deleteAnnotation(annotationMockId); - expect(mockFunct.mock.calls[0][0]).toBe('delete'); - expect(mockFunct.mock.calls[0][1]).toEqual(deleteParamsMock); + expect(mockFunct.callAsCurrentUser.mock.calls[0][0]).toBe('delete'); + expect(mockFunct.callAsCurrentUser.mock.calls[0][1]).toEqual(deleteParamsMock); expect(response).toBe(acknowledgedResponseMock); done(); }); @@ -57,8 +61,8 @@ describe('annotation_service', () => { describe('getAnnotation()', () => { it('should get annotations for specific job', async (done) => { - const { getAnnotations } = annotationServiceProvider(callWithRequestSpy); - const mockFunct = callWithRequestSpy; + const { getAnnotations } = annotationServiceProvider(mlClusterClientSpy); + const mockFunct = mlClusterClientSpy; const indexAnnotationArgsMock: IndexAnnotationArgs = { jobIds: [jobIdMock], @@ -69,8 +73,8 @@ describe('annotation_service', () => { const response: GetResponse = await getAnnotations(indexAnnotationArgsMock); - expect(mockFunct.mock.calls[0][0]).toBe('search'); - expect(mockFunct.mock.calls[0][1]).toEqual(getAnnotationsRequestMock); + expect(mockFunct.callAsCurrentUser.mock.calls[0][0]).toBe('search'); + expect(mockFunct.callAsCurrentUser.mock.calls[0][1]).toEqual(getAnnotationsRequestMock); expect(Object.keys(response.annotations)).toHaveLength(1); expect(response.annotations[jobIdMock]).toHaveLength(2); expect(isAnnotations(response.annotations[jobIdMock])).toBeTruthy(); @@ -84,11 +88,13 @@ describe('annotation_service', () => { message: 'mock error message', }; - const callWithRequestSpyError = (jest.fn(() => { - return Promise.resolve(mockEsError); - }) as unknown) as LegacyAPICaller; + const mlClusterClientSpyError: any = { + callAsCurrentUser: jest.fn(() => { + return Promise.resolve(mockEsError); + }), + }; - const { getAnnotations } = annotationServiceProvider(callWithRequestSpyError); + const { getAnnotations } = annotationServiceProvider(mlClusterClientSpyError); const indexAnnotationArgsMock: IndexAnnotationArgs = { jobIds: [jobIdMock], @@ -105,8 +111,8 @@ describe('annotation_service', () => { describe('indexAnnotation()', () => { it('should index annotation', async (done) => { - const { indexAnnotation } = annotationServiceProvider(callWithRequestSpy); - const mockFunct = callWithRequestSpy; + const { indexAnnotation } = annotationServiceProvider(mlClusterClientSpy); + const mockFunct = mlClusterClientSpy; const annotationMock: Annotation = { annotation: 'Annotation text', @@ -118,10 +124,10 @@ describe('annotation_service', () => { const response = await indexAnnotation(annotationMock, usernameMock); - expect(mockFunct.mock.calls[0][0]).toBe('index'); + expect(mockFunct.callAsCurrentUser.mock.calls[0][0]).toBe('index'); // test if the annotation has been correctly augmented - const indexParamsCheck = mockFunct.mock.calls[0][1]; + const indexParamsCheck = mockFunct.callAsCurrentUser.mock.calls[0][1]; const annotation = indexParamsCheck.body; expect(annotation.create_username).toBe(usernameMock); expect(annotation.modified_username).toBe(usernameMock); @@ -133,8 +139,8 @@ describe('annotation_service', () => { }); it('should remove ._id and .key before updating annotation', async (done) => { - const { indexAnnotation } = annotationServiceProvider(callWithRequestSpy); - const mockFunct = callWithRequestSpy; + const { indexAnnotation } = annotationServiceProvider(mlClusterClientSpy); + const mockFunct = mlClusterClientSpy; const annotationMock: Annotation = { _id: 'mockId', @@ -148,10 +154,10 @@ describe('annotation_service', () => { const response = await indexAnnotation(annotationMock, usernameMock); - expect(mockFunct.mock.calls[0][0]).toBe('index'); + expect(mockFunct.callAsCurrentUser.mock.calls[0][0]).toBe('index'); // test if the annotation has been correctly augmented - const indexParamsCheck = mockFunct.mock.calls[0][1]; + const indexParamsCheck = mockFunct.callAsCurrentUser.mock.calls[0][1]; const annotation = indexParamsCheck.body; expect(annotation.create_username).toBe(usernameMock); expect(annotation.modified_username).toBe(usernameMock); @@ -165,8 +171,8 @@ describe('annotation_service', () => { }); it('should update annotation text and the username for modified_username', async (done) => { - const { getAnnotations, indexAnnotation } = annotationServiceProvider(callWithRequestSpy); - const mockFunct = callWithRequestSpy; + const { getAnnotations, indexAnnotation } = annotationServiceProvider(mlClusterClientSpy); + const mockFunct = mlClusterClientSpy; const indexAnnotationArgsMock: IndexAnnotationArgs = { jobIds: [jobIdMock], @@ -190,9 +196,9 @@ describe('annotation_service', () => { await indexAnnotation(annotation, modifiedUsernameMock); - expect(mockFunct.mock.calls[1][0]).toBe('index'); + expect(mockFunct.callAsCurrentUser.mock.calls[1][0]).toBe('index'); // test if the annotation has been correctly updated - const indexParamsCheck = mockFunct.mock.calls[1][1]; + const indexParamsCheck = mockFunct.callAsCurrentUser.mock.calls[1][1]; const modifiedAnnotation = indexParamsCheck.body; expect(modifiedAnnotation.annotation).toBe(modifiedAnnotationText); expect(modifiedAnnotation.create_username).toBe(originalUsernameMock); diff --git a/x-pack/plugins/ml/server/models/annotation_service/annotation.ts b/x-pack/plugins/ml/server/models/annotation_service/annotation.ts index 2808b06103a75..f7353034b7453 100644 --- a/x-pack/plugins/ml/server/models/annotation_service/annotation.ts +++ b/x-pack/plugins/ml/server/models/annotation_service/annotation.ts @@ -6,9 +6,10 @@ import Boom from 'boom'; import _ from 'lodash'; -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; -import { ANNOTATION_TYPE } from '../../../common/constants/annotations'; +import { ANNOTATION_EVENT_USER, ANNOTATION_TYPE } from '../../../common/constants/annotations'; +import { PARTITION_FIELDS } from '../../../common/constants/anomalies'; import { ML_ANNOTATIONS_INDEX_ALIAS_READ, ML_ANNOTATIONS_INDEX_ALIAS_WRITE, @@ -19,20 +20,35 @@ import { Annotations, isAnnotation, isAnnotations, + getAnnotationFieldName, + getAnnotationFieldValue, + EsAggregationResult, } from '../../../common/types/annotations'; // TODO All of the following interface/type definitions should // eventually be replaced by the proper upstream definitions interface EsResult { - _source: object; + _source: Annotation; _id: string; } +export interface FieldToBucket { + field: string; + missing?: string | number; +} + export interface IndexAnnotationArgs { jobIds: string[]; earliestMs: number; latestMs: number; maxAnnotations: number; + fields?: FieldToBucket[]; + detectorIndex?: number; + entities?: any[]; +} + +export interface AggTerm { + terms: FieldToBucket; } export interface GetParams { @@ -43,9 +59,8 @@ export interface GetParams { export interface GetResponse { success: true; - annotations: { - [key: string]: Annotations; - }; + annotations: Record; + aggregations: EsAggregationResult; } export interface IndexParams { @@ -61,14 +76,7 @@ export interface DeleteParams { id: string; } -type annotationProviderParams = DeleteParams | GetParams | IndexParams; - -export type callWithRequestType = ( - action: string, - params: annotationProviderParams -) => Promise; - -export function annotationProvider(callAsCurrentUser: LegacyAPICaller) { +export function annotationProvider({ callAsCurrentUser }: ILegacyScopedClusterClient) { async function indexAnnotation(annotation: Annotation, username: string) { if (isAnnotation(annotation) === false) { // No need to translate, this will not be exposed in the UI. @@ -103,10 +111,14 @@ export function annotationProvider(callAsCurrentUser: LegacyAPICaller) { earliestMs, latestMs, maxAnnotations, + fields, + detectorIndex, + entities, }: IndexAnnotationArgs) { const obj: GetResponse = { success: true, annotations: {}, + aggregations: {}, }; const boolCriteria: object[] = []; @@ -189,6 +201,64 @@ export function annotationProvider(callAsCurrentUser: LegacyAPICaller) { }); } + // Find unique buckets (e.g. events) from the queried annotations to show in dropdowns + const aggs: Record = {}; + if (fields) { + fields.forEach((fieldToBucket) => { + aggs[fieldToBucket.field] = { + terms: { + ...fieldToBucket, + }, + }; + }); + } + + // Build should clause to further query for annotations in SMV + // we want to show either the exact match with detector index and by/over/partition fields + // OR annotations without any partition fields defined + let shouldClauses; + if (detectorIndex !== undefined && Array.isArray(entities)) { + // build clause to get exact match of detector index and by/over/partition fields + const beExactMatch = []; + beExactMatch.push({ + term: { + detector_index: detectorIndex, + }, + }); + + entities.forEach(({ fieldName, fieldType, fieldValue }) => { + beExactMatch.push({ + term: { + [getAnnotationFieldName(fieldType)]: fieldName, + }, + }); + beExactMatch.push({ + term: { + [getAnnotationFieldValue(fieldType)]: fieldValue, + }, + }); + }); + + // clause to get annotations that have no partition fields + const haveAnyPartitionFields: object[] = []; + PARTITION_FIELDS.forEach((field) => { + haveAnyPartitionFields.push({ + exists: { + field: getAnnotationFieldName(field), + }, + }); + haveAnyPartitionFields.push({ + exists: { + field: getAnnotationFieldValue(field), + }, + }); + }); + shouldClauses = [ + { bool: { must_not: haveAnyPartitionFields } }, + { bool: { must: beExactMatch } }, + ]; + } + const params: GetParams = { index: ML_ANNOTATIONS_INDEX_ALIAS_READ, size: maxAnnotations, @@ -208,8 +278,10 @@ export function annotationProvider(callAsCurrentUser: LegacyAPICaller) { }, }, ], + ...(shouldClauses ? { should: shouldClauses, minimum_should_match: 1 } : {}), }, }, + ...(fields ? { aggs } : {}), }, }; @@ -224,9 +296,19 @@ export function annotationProvider(callAsCurrentUser: LegacyAPICaller) { const docs: Annotations = _.get(resp, ['hits', 'hits'], []).map((d: EsResult) => { // get the original source document and the document id, we need it // to identify the annotation when editing/deleting it. - return { ...d._source, _id: d._id } as Annotation; + // if original `event` is undefined then substitute with 'user` by default + // since annotation was probably generated by user on the UI + return { + ...d._source, + event: d._source?.event ?? ANNOTATION_EVENT_USER, + _id: d._id, + } as Annotation; }); + const aggregations = _.get(resp, ['aggregations'], {}) as EsAggregationResult; + if (fields) { + obj.aggregations = aggregations; + } if (isAnnotations(docs) === false) { // No need to translate, this will not be exposed in the UI. throw new Error(`Annotations didn't pass integrity check.`); diff --git a/x-pack/plugins/ml/server/models/annotation_service/index.ts b/x-pack/plugins/ml/server/models/annotation_service/index.ts index efc42c693c24b..e17af2a154b87 100644 --- a/x-pack/plugins/ml/server/models/annotation_service/index.ts +++ b/x-pack/plugins/ml/server/models/annotation_service/index.ts @@ -4,11 +4,11 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { annotationProvider } from './annotation'; -export function annotationServiceProvider(callAsCurrentUser: LegacyAPICaller) { +export function annotationServiceProvider(mlClusterClient: ILegacyScopedClusterClient) { return { - ...annotationProvider(callAsCurrentUser), + ...annotationProvider(mlClusterClient), }; } diff --git a/x-pack/plugins/ml/server/models/bucket_span_estimator/bucket_span_estimator.d.ts b/x-pack/plugins/ml/server/models/bucket_span_estimator/bucket_span_estimator.d.ts index 3e80e79705a5c..eeabb24d9be3b 100644 --- a/x-pack/plugins/ml/server/models/bucket_span_estimator/bucket_span_estimator.d.ts +++ b/x-pack/plugins/ml/server/models/bucket_span_estimator/bucket_span_estimator.d.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { ES_AGGREGATION } from '../../../common/constants/aggregation_types'; export interface BucketSpanEstimatorData { @@ -20,8 +20,7 @@ export interface BucketSpanEstimatorData { timeField: string | undefined; } -export function estimateBucketSpanFactory( - callAsCurrentUser: LegacyAPICaller, - callAsInternalUser: LegacyAPICaller, - isSecurityDisabled: boolean -): (config: BucketSpanEstimatorData) => Promise; +export function estimateBucketSpanFactory({ + callAsCurrentUser, + callAsInternalUser, +}: ILegacyScopedClusterClient): (config: BucketSpanEstimatorData) => Promise; diff --git a/x-pack/plugins/ml/server/models/bucket_span_estimator/bucket_span_estimator.js b/x-pack/plugins/ml/server/models/bucket_span_estimator/bucket_span_estimator.js index 2e03a9532c831..3758547779403 100644 --- a/x-pack/plugins/ml/server/models/bucket_span_estimator/bucket_span_estimator.js +++ b/x-pack/plugins/ml/server/models/bucket_span_estimator/bucket_span_estimator.js @@ -12,13 +12,10 @@ import { INTERVALS } from './intervals'; import { singleSeriesCheckerFactory } from './single_series_checker'; import { polledDataCheckerFactory } from './polled_data_checker'; -export function estimateBucketSpanFactory( - callAsCurrentUser, - callAsInternalUser, - isSecurityDisabled -) { - const PolledDataChecker = polledDataCheckerFactory(callAsCurrentUser); - const SingleSeriesChecker = singleSeriesCheckerFactory(callAsCurrentUser); +export function estimateBucketSpanFactory(mlClusterClient) { + const { callAsCurrentUser, callAsInternalUser } = mlClusterClient; + const PolledDataChecker = polledDataCheckerFactory(mlClusterClient); + const SingleSeriesChecker = singleSeriesCheckerFactory(mlClusterClient); class BucketSpanEstimator { constructor( @@ -334,99 +331,65 @@ export function estimateBucketSpanFactory( } return new Promise((resolve, reject) => { - function getBucketSpanEstimation() { - // fetch the `search.max_buckets` cluster setting so we're able to - // adjust aggregations to not exceed that limit. - callAsInternalUser('cluster.getSettings', { - flatSettings: true, - includeDefaults: true, - filterPath: '*.*max_buckets', - }) - .then((settings) => { - if (typeof settings !== 'object') { - reject('Unable to retrieve cluster settings'); - } - - // search.max_buckets could exist in default, persistent or transient cluster settings - const maxBucketsSetting = (settings.defaults || - settings.persistent || - settings.transient || - {})['search.max_buckets']; - - if (maxBucketsSetting === undefined) { - reject('Unable to retrieve cluster setting search.max_buckets'); - } - - const maxBuckets = parseInt(maxBucketsSetting); + // fetch the `search.max_buckets` cluster setting so we're able to + // adjust aggregations to not exceed that limit. + callAsInternalUser('cluster.getSettings', { + flatSettings: true, + includeDefaults: true, + filterPath: '*.*max_buckets', + }) + .then((settings) => { + if (typeof settings !== 'object') { + reject('Unable to retrieve cluster settings'); + } - const runEstimator = (splitFieldValues = []) => { - const bucketSpanEstimator = new BucketSpanEstimator( - formConfig, - splitFieldValues, - maxBuckets - ); + // search.max_buckets could exist in default, persistent or transient cluster settings + const maxBucketsSetting = (settings.defaults || + settings.persistent || + settings.transient || + {})['search.max_buckets']; - bucketSpanEstimator - .run() - .then((resp) => { - resolve(resp); - }) - .catch((resp) => { - reject(resp); - }); - }; - - // a partition has been selected, so we need to load some field values to use in the - // bucket span tests. - if (formConfig.splitField !== undefined) { - getRandomFieldValues(formConfig.index, formConfig.splitField, formConfig.query) - .then((splitFieldValues) => { - runEstimator(splitFieldValues); - }) - .catch((resp) => { - reject(resp); - }); - } else { - // no partition field selected or we're in the single metric config - runEstimator(); - } - }) - .catch((resp) => { - reject(resp); - }); - } + if (maxBucketsSetting === undefined) { + reject('Unable to retrieve cluster setting search.max_buckets'); + } - if (isSecurityDisabled) { - getBucketSpanEstimation(); - } else { - // if security is enabled, check that the user has permission to - // view jobs before calling getBucketSpanEstimation. - // getBucketSpanEstimation calls the 'cluster.getSettings' endpoint as the internal user - // and so could give the user access to more information than - // they are entitled to. - const body = { - cluster: [ - 'cluster:monitor/xpack/ml/job/get', - 'cluster:monitor/xpack/ml/job/stats/get', - 'cluster:monitor/xpack/ml/datafeeds/get', - 'cluster:monitor/xpack/ml/datafeeds/stats/get', - ], - }; - callAsCurrentUser('ml.privilegeCheck', { body }) - .then((resp) => { - if ( - resp.cluster['cluster:monitor/xpack/ml/job/get'] && - resp.cluster['cluster:monitor/xpack/ml/job/stats/get'] && - resp.cluster['cluster:monitor/xpack/ml/datafeeds/get'] && - resp.cluster['cluster:monitor/xpack/ml/datafeeds/stats/get'] - ) { - getBucketSpanEstimation(); - } else { - reject('Insufficient permissions to call bucket span estimation.'); - } - }) - .catch(reject); - } + const maxBuckets = parseInt(maxBucketsSetting); + + const runEstimator = (splitFieldValues = []) => { + const bucketSpanEstimator = new BucketSpanEstimator( + formConfig, + splitFieldValues, + maxBuckets + ); + + bucketSpanEstimator + .run() + .then((resp) => { + resolve(resp); + }) + .catch((resp) => { + reject(resp); + }); + }; + + // a partition has been selected, so we need to load some field values to use in the + // bucket span tests. + if (formConfig.splitField !== undefined) { + getRandomFieldValues(formConfig.index, formConfig.splitField, formConfig.query) + .then((splitFieldValues) => { + runEstimator(splitFieldValues); + }) + .catch((resp) => { + reject(resp); + }); + } else { + // no partition field selected or we're in the single metric config + runEstimator(); + } + }) + .catch((resp) => { + reject(resp); + }); }); }; } diff --git a/x-pack/plugins/ml/server/models/bucket_span_estimator/bucket_span_estimator.test.ts b/x-pack/plugins/ml/server/models/bucket_span_estimator/bucket_span_estimator.test.ts index 8da1fb69eec34..f7c7dd8172ea5 100644 --- a/x-pack/plugins/ml/server/models/bucket_span_estimator/bucket_span_estimator.test.ts +++ b/x-pack/plugins/ml/server/models/bucket_span_estimator/bucket_span_estimator.test.ts @@ -4,40 +4,21 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { ES_AGGREGATION } from '../../../common/constants/aggregation_types'; import { estimateBucketSpanFactory, BucketSpanEstimatorData } from './bucket_span_estimator'; -// Mock callWithRequest with the ability to simulate returning different -// permission settings. On each call using `ml.privilegeCheck` we retrieve -// the last value from `permissions` and pass that to one of the permission -// settings. The tests call `ml.privilegeCheck` two times, the first time -// sufficient permissions should be returned, the second time insufficient -// permissions. -const permissions = [false, true]; -const callWithRequest: LegacyAPICaller = (method: string) => { +const callAs = () => { return new Promise((resolve) => { - if (method === 'ml.privilegeCheck') { - resolve({ - cluster: { - 'cluster:monitor/xpack/ml/job/get': true, - 'cluster:monitor/xpack/ml/job/stats/get': true, - 'cluster:monitor/xpack/ml/datafeeds/get': true, - 'cluster:monitor/xpack/ml/datafeeds/stats/get': permissions.pop(), - }, - }); - return; - } resolve({}); }) as Promise; }; -const callWithInternalUser: LegacyAPICaller = () => { - return new Promise((resolve) => { - resolve({}); - }) as Promise; +const mlClusterClient: ILegacyScopedClusterClient = { + callAsCurrentUser: callAs, + callAsInternalUser: callAs, }; // mock configuration to be passed to the estimator @@ -59,17 +40,13 @@ const formConfig: BucketSpanEstimatorData = { describe('ML - BucketSpanEstimator', () => { it('call factory', () => { expect(function () { - estimateBucketSpanFactory(callWithRequest, callWithInternalUser, false); + estimateBucketSpanFactory(mlClusterClient); }).not.toThrow('Not initialized.'); }); it('call factory and estimator with security disabled', (done) => { expect(function () { - const estimateBucketSpan = estimateBucketSpanFactory( - callWithRequest, - callWithInternalUser, - true - ); + const estimateBucketSpan = estimateBucketSpanFactory(mlClusterClient); estimateBucketSpan(formConfig).catch((catchData) => { expect(catchData).toBe('Unable to retrieve cluster setting search.max_buckets'); @@ -81,11 +58,7 @@ describe('ML - BucketSpanEstimator', () => { it('call factory and estimator with security enabled.', (done) => { expect(function () { - const estimateBucketSpan = estimateBucketSpanFactory( - callWithRequest, - callWithInternalUser, - false - ); + const estimateBucketSpan = estimateBucketSpanFactory(mlClusterClient); estimateBucketSpan(formConfig).catch((catchData) => { expect(catchData).toBe('Unable to retrieve cluster setting search.max_buckets'); diff --git a/x-pack/plugins/ml/server/models/bucket_span_estimator/polled_data_checker.js b/x-pack/plugins/ml/server/models/bucket_span_estimator/polled_data_checker.js index de9fd06c34e6a..347843e276c36 100644 --- a/x-pack/plugins/ml/server/models/bucket_span_estimator/polled_data_checker.js +++ b/x-pack/plugins/ml/server/models/bucket_span_estimator/polled_data_checker.js @@ -12,7 +12,7 @@ import _ from 'lodash'; -export function polledDataCheckerFactory(callAsCurrentUser) { +export function polledDataCheckerFactory({ callAsCurrentUser }) { class PolledDataChecker { constructor(index, timeField, duration, query) { this.index = index; diff --git a/x-pack/plugins/ml/server/models/bucket_span_estimator/single_series_checker.js b/x-pack/plugins/ml/server/models/bucket_span_estimator/single_series_checker.js index 6ae485fe11307..a5449395501dc 100644 --- a/x-pack/plugins/ml/server/models/bucket_span_estimator/single_series_checker.js +++ b/x-pack/plugins/ml/server/models/bucket_span_estimator/single_series_checker.js @@ -13,7 +13,7 @@ import { mlLog } from '../../client/log'; import { INTERVALS, LONG_INTERVALS } from './intervals'; -export function singleSeriesCheckerFactory(callAsCurrentUser) { +export function singleSeriesCheckerFactory({ callAsCurrentUser }) { const REF_DATA_INTERVAL = { name: '1h', ms: 3600000 }; class SingleSeriesChecker { diff --git a/x-pack/plugins/ml/server/models/calculate_model_memory_limit/calculate_model_memory_limit.ts b/x-pack/plugins/ml/server/models/calculate_model_memory_limit/calculate_model_memory_limit.ts index 61299aa3ae26d..bc3c326e7d070 100644 --- a/x-pack/plugins/ml/server/models/calculate_model_memory_limit/calculate_model_memory_limit.ts +++ b/x-pack/plugins/ml/server/models/calculate_model_memory_limit/calculate_model_memory_limit.ts @@ -5,7 +5,7 @@ */ import numeral from '@elastic/numeral'; -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { MLCATEGORY } from '../../../common/constants/field_types'; import { AnalysisConfig } from '../../../common/types/anomaly_detection_jobs'; import { fieldsServiceProvider } from '../fields_service'; @@ -36,8 +36,8 @@ export interface ModelMemoryEstimate { /** * Retrieves overall and max bucket cardinalities. */ -const cardinalityCheckProvider = (callAsCurrentUser: LegacyAPICaller) => { - const fieldsService = fieldsServiceProvider(callAsCurrentUser); +const cardinalityCheckProvider = (mlClusterClient: ILegacyScopedClusterClient) => { + const fieldsService = fieldsServiceProvider(mlClusterClient); return async ( analysisConfig: AnalysisConfig, @@ -123,8 +123,9 @@ const cardinalityCheckProvider = (callAsCurrentUser: LegacyAPICaller) => { }; }; -export function calculateModelMemoryLimitProvider(callAsCurrentUser: LegacyAPICaller) { - const getCardinalities = cardinalityCheckProvider(callAsCurrentUser); +export function calculateModelMemoryLimitProvider(mlClusterClient: ILegacyScopedClusterClient) { + const { callAsInternalUser } = mlClusterClient; + const getCardinalities = cardinalityCheckProvider(mlClusterClient); /** * Retrieves an estimated size of the model memory limit used in the job config @@ -140,7 +141,7 @@ export function calculateModelMemoryLimitProvider(callAsCurrentUser: LegacyAPICa latestMs: number, allowMMLGreaterThanMax = false ): Promise { - const info = await callAsCurrentUser('ml.info'); + const info = (await callAsInternalUser('ml.info')) as MlInfoResponse; const maxModelMemoryLimit = info.limits.max_model_memory_limit?.toUpperCase(); const effectiveMaxModelMemoryLimit = info.limits.effective_max_model_memory_limit?.toUpperCase(); @@ -153,28 +154,26 @@ export function calculateModelMemoryLimitProvider(callAsCurrentUser: LegacyAPICa latestMs ); - const estimatedModelMemoryLimit = ( - await callAsCurrentUser('ml.estimateModelMemory', { - body: { - analysis_config: analysisConfig, - overall_cardinality: overallCardinality, - max_bucket_cardinality: maxBucketCardinality, - }, - }) - ).model_memory_estimate.toUpperCase(); + const estimatedModelMemoryLimit = ((await callAsInternalUser('ml.estimateModelMemory', { + body: { + analysis_config: analysisConfig, + overall_cardinality: overallCardinality, + max_bucket_cardinality: maxBucketCardinality, + }, + })) as ModelMemoryEstimate).model_memory_estimate.toUpperCase(); let modelMemoryLimit = estimatedModelMemoryLimit; let mmlCappedAtMax = false; // if max_model_memory_limit has been set, // make sure the estimated value is not greater than it. if (allowMMLGreaterThanMax === false) { - // @ts-ignore + // @ts-expect-error const mmlBytes = numeral(estimatedModelMemoryLimit).value(); if (maxModelMemoryLimit !== undefined) { - // @ts-ignore + // @ts-expect-error const maxBytes = numeral(maxModelMemoryLimit).value(); if (mmlBytes > maxBytes) { - // @ts-ignore + // @ts-expect-error modelMemoryLimit = `${Math.floor(maxBytes / numeral('1MB').value())}MB`; mmlCappedAtMax = true; } @@ -183,10 +182,10 @@ export function calculateModelMemoryLimitProvider(callAsCurrentUser: LegacyAPICa // if we've not already capped the estimated mml at the hard max server setting // ensure that the estimated mml isn't greater than the effective max mml if (mmlCappedAtMax === false && effectiveMaxModelMemoryLimit !== undefined) { - // @ts-ignore + // @ts-expect-error const effectiveMaxMmlBytes = numeral(effectiveMaxModelMemoryLimit).value(); if (mmlBytes > effectiveMaxMmlBytes) { - // @ts-ignore + // @ts-expect-error modelMemoryLimit = `${Math.floor(effectiveMaxMmlBytes / numeral('1MB').value())}MB`; } } diff --git a/x-pack/plugins/ml/server/models/calendar/calendar_manager.ts b/x-pack/plugins/ml/server/models/calendar/calendar_manager.ts index 5df9c037b3f83..43f4dc3cba7e2 100644 --- a/x-pack/plugins/ml/server/models/calendar/calendar_manager.ts +++ b/x-pack/plugins/ml/server/models/calendar/calendar_manager.ts @@ -5,7 +5,7 @@ */ import { difference } from 'lodash'; -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { EventManager, CalendarEvent } from './event_manager'; interface BasicCalendar { @@ -23,16 +23,16 @@ export interface FormCalendar extends BasicCalendar { } export class CalendarManager { - private _callAsCurrentUser: LegacyAPICaller; + private _callAsInternalUser: ILegacyScopedClusterClient['callAsInternalUser']; private _eventManager: EventManager; - constructor(callAsCurrentUser: LegacyAPICaller) { - this._callAsCurrentUser = callAsCurrentUser; - this._eventManager = new EventManager(callAsCurrentUser); + constructor(mlClusterClient: ILegacyScopedClusterClient) { + this._callAsInternalUser = mlClusterClient.callAsInternalUser; + this._eventManager = new EventManager(mlClusterClient); } async getCalendar(calendarId: string) { - const resp = await this._callAsCurrentUser('ml.calendars', { + const resp = await this._callAsInternalUser('ml.calendars', { calendarId, }); @@ -43,7 +43,7 @@ export class CalendarManager { } async getAllCalendars() { - const calendarsResp = await this._callAsCurrentUser('ml.calendars'); + const calendarsResp = await this._callAsInternalUser('ml.calendars'); const events: CalendarEvent[] = await this._eventManager.getAllEvents(); const calendars: Calendar[] = calendarsResp.calendars; @@ -74,7 +74,7 @@ export class CalendarManager { const events = calendar.events; delete calendar.calendarId; delete calendar.events; - await this._callAsCurrentUser('ml.addCalendar', { + await this._callAsInternalUser('ml.addCalendar', { calendarId, body: calendar, }); @@ -109,7 +109,7 @@ export class CalendarManager { // add all new jobs if (jobsToAdd.length) { - await this._callAsCurrentUser('ml.addJobToCalendar', { + await this._callAsInternalUser('ml.addJobToCalendar', { calendarId, jobId: jobsToAdd.join(','), }); @@ -117,7 +117,7 @@ export class CalendarManager { // remove all removed jobs if (jobsToRemove.length) { - await this._callAsCurrentUser('ml.removeJobFromCalendar', { + await this._callAsInternalUser('ml.removeJobFromCalendar', { calendarId, jobId: jobsToRemove.join(','), }); @@ -140,6 +140,6 @@ export class CalendarManager { } async deleteCalendar(calendarId: string) { - return this._callAsCurrentUser('ml.deleteCalendar', { calendarId }); + return this._callAsInternalUser('ml.deleteCalendar', { calendarId }); } } diff --git a/x-pack/plugins/ml/server/models/calendar/event_manager.ts b/x-pack/plugins/ml/server/models/calendar/event_manager.ts index 57034ab772710..b670bbe187544 100644 --- a/x-pack/plugins/ml/server/models/calendar/event_manager.ts +++ b/x-pack/plugins/ml/server/models/calendar/event_manager.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { GLOBAL_CALENDAR } from '../../../common/constants/calendars'; export interface CalendarEvent { @@ -16,10 +16,13 @@ export interface CalendarEvent { } export class EventManager { - constructor(private _callAsCurrentUser: LegacyAPICaller) {} + private _callAsInternalUser: ILegacyScopedClusterClient['callAsInternalUser']; + constructor({ callAsInternalUser }: ILegacyScopedClusterClient) { + this._callAsInternalUser = callAsInternalUser; + } async getCalendarEvents(calendarId: string) { - const resp = await this._callAsCurrentUser('ml.events', { calendarId }); + const resp = await this._callAsInternalUser('ml.events', { calendarId }); return resp.events; } @@ -27,7 +30,7 @@ export class EventManager { // jobId is optional async getAllEvents(jobId?: string) { const calendarId = GLOBAL_CALENDAR; - const resp = await this._callAsCurrentUser('ml.events', { + const resp = await this._callAsInternalUser('ml.events', { calendarId, jobId, }); @@ -38,14 +41,14 @@ export class EventManager { async addEvents(calendarId: string, events: CalendarEvent[]) { const body = { events }; - return await this._callAsCurrentUser('ml.addEvent', { + return await this._callAsInternalUser('ml.addEvent', { calendarId, body, }); } async deleteEvent(calendarId: string, eventId: string) { - return this._callAsCurrentUser('ml.deleteEvent', { calendarId, eventId }); + return this._callAsInternalUser('ml.deleteEvent', { calendarId, eventId }); } isEqual(ev1: CalendarEvent, ev2: CalendarEvent) { diff --git a/x-pack/plugins/ml/server/models/data_frame_analytics/analytics_audit_messages.ts b/x-pack/plugins/ml/server/models/data_frame_analytics/analytics_audit_messages.ts index abe389165182f..c8471b5462205 100644 --- a/x-pack/plugins/ml/server/models/data_frame_analytics/analytics_audit_messages.ts +++ b/x-pack/plugins/ml/server/models/data_frame_analytics/analytics_audit_messages.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { callWithRequestType } from '../../../common/types/kibana'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { ML_NOTIFICATION_INDEX_PATTERN } from '../../../common/constants/index_patterns'; import { JobMessage } from '../../../common/types/audit_message'; @@ -23,7 +23,7 @@ interface BoolQuery { bool: { [key: string]: any }; } -export function analyticsAuditMessagesProvider(callWithRequest: callWithRequestType) { +export function analyticsAuditMessagesProvider({ callAsCurrentUser }: ILegacyScopedClusterClient) { // search for audit messages, // analyticsId is optional. without it, all analytics will be listed. async function getAnalyticsAuditMessages(analyticsId: string) { @@ -69,7 +69,7 @@ export function analyticsAuditMessagesProvider(callWithRequest: callWithRequestT } try { - const resp = await callWithRequest('search', { + const resp = await callAsCurrentUser('search', { index: ML_NOTIFICATION_INDEX_PATTERN, ignore_unavailable: true, rest_total_hits_as_int: true, diff --git a/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.test.ts b/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.test.ts index ee8598ad338e3..82d7707464308 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.test.ts +++ b/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.test.ts @@ -4,17 +4,18 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller, SavedObjectsClientContract } from 'kibana/server'; +import { SavedObjectsClientContract, KibanaRequest } from 'kibana/server'; import { Module } from '../../../common/types/modules'; import { DataRecognizer } from '../data_recognizer'; describe('ML - data recognizer', () => { const dr = new DataRecognizer( - jest.fn() as LegacyAPICaller, + { callAsCurrentUser: jest.fn(), callAsInternalUser: jest.fn() }, ({ find: jest.fn(), bulkCreate: jest.fn(), - } as never) as SavedObjectsClientContract + } as unknown) as SavedObjectsClientContract, + { headers: { authorization: '' } } as KibanaRequest ); describe('jobOverrides', () => { diff --git a/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts b/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts index ae9a56f00a5c1..521d04159ca7a 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts +++ b/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts @@ -7,11 +7,16 @@ import fs from 'fs'; import Boom from 'boom'; import numeral from '@elastic/numeral'; -import { LegacyAPICaller, SavedObjectsClientContract } from 'kibana/server'; +import { + KibanaRequest, + ILegacyScopedClusterClient, + SavedObjectsClientContract, +} from 'kibana/server'; import moment from 'moment'; import { IndexPatternAttributes } from 'src/plugins/data/server'; import { merge } from 'lodash'; import { AnalysisLimits, CombinedJobWithStats } from '../../../common/types/anomaly_detection_jobs'; +import { getAuthorizationHeader } from '../../lib/request_authorization'; import { MlInfoResponse } from '../../../common/types/ml_server_info'; import { KibanaObjects, @@ -104,18 +109,28 @@ interface SaveResults { } export class DataRecognizer { - modulesDir = `${__dirname}/modules`; - indexPatternName: string = ''; - indexPatternId: string | undefined = undefined; + private _callAsCurrentUser: ILegacyScopedClusterClient['callAsCurrentUser']; + private _callAsInternalUser: ILegacyScopedClusterClient['callAsInternalUser']; + private _mlClusterClient: ILegacyScopedClusterClient; + private _authorizationHeader: object; + private _modulesDir = `${__dirname}/modules`; + private _indexPatternName: string = ''; + private _indexPatternId: string | undefined = undefined; /** * List of the module jobs that require model memory estimation */ jobsForModelMemoryEstimation: Array<{ job: ModuleJob; query: any }> = []; constructor( - private callAsCurrentUser: LegacyAPICaller, - private savedObjectsClient: SavedObjectsClientContract - ) {} + mlClusterClient: ILegacyScopedClusterClient, + private savedObjectsClient: SavedObjectsClientContract, + request: KibanaRequest + ) { + this._mlClusterClient = mlClusterClient; + this._callAsCurrentUser = mlClusterClient.callAsCurrentUser; + this._callAsInternalUser = mlClusterClient.callAsInternalUser; + this._authorizationHeader = getAuthorizationHeader(request); + } // list all directories under the given directory async listDirs(dirName: string): Promise { @@ -150,12 +165,12 @@ export class DataRecognizer { async loadManifestFiles(): Promise { const configs: Config[] = []; - const dirs = await this.listDirs(this.modulesDir); + const dirs = await this.listDirs(this._modulesDir); await Promise.all( dirs.map(async (dir) => { let file: string | undefined; try { - file = await this.readFile(`${this.modulesDir}/${dir}/manifest.json`); + file = await this.readFile(`${this._modulesDir}/${dir}/manifest.json`); } catch (error) { mlLog.warn(`Data recognizer skipping folder ${dir} as manifest.json cannot be read`); } @@ -204,7 +219,7 @@ export class DataRecognizer { if (moduleConfig.logoFile) { try { logo = await this.readFile( - `${this.modulesDir}/${i.dirName}/${moduleConfig.logoFile}` + `${this._modulesDir}/${i.dirName}/${moduleConfig.logoFile}` ); logo = JSON.parse(logo); } catch (e) { @@ -236,7 +251,7 @@ export class DataRecognizer { query: moduleConfig.query, }; - const resp = await this.callAsCurrentUser('search', { + const resp = await this._callAsCurrentUser('search', { index, rest_total_hits_as_int: true, size, @@ -281,7 +296,7 @@ export class DataRecognizer { manifestJSON.jobs.map(async (job) => { try { const jobConfig = await this.readFile( - `${this.modulesDir}/${dirName}/${ML_DIR}/${job.file}` + `${this._modulesDir}/${dirName}/${ML_DIR}/${job.file}` ); // use the file name for the id jobs.push({ @@ -301,7 +316,7 @@ export class DataRecognizer { manifestJSON.datafeeds.map(async (datafeed) => { try { const datafeedConfig = await this.readFile( - `${this.modulesDir}/${dirName}/${ML_DIR}/${datafeed.file}` + `${this._modulesDir}/${dirName}/${ML_DIR}/${datafeed.file}` ); const config = JSON.parse(datafeedConfig); // use the job id from the manifestFile @@ -329,7 +344,7 @@ export class DataRecognizer { manifestJSON!.kibana[key].map(async (obj) => { try { const kConfig = await this.readFile( - `${this.modulesDir}/${dirName}/${KIBANA_DIR}/${key}/${obj.file}` + `${this._modulesDir}/${dirName}/${KIBANA_DIR}/${key}/${obj.file}` ); // use the file name for the id const kId = obj.file.replace('.json', ''); @@ -385,26 +400,26 @@ export class DataRecognizer { ); } - this.indexPatternName = + this._indexPatternName = indexPatternName === undefined ? moduleConfig.defaultIndexPattern : indexPatternName; - this.indexPatternId = await this.getIndexPatternId(this.indexPatternName); + this._indexPatternId = await this.getIndexPatternId(this._indexPatternName); // the module's jobs contain custom URLs which require an index patten id // but there is no corresponding index pattern, throw an error - if (this.indexPatternId === undefined && this.doJobUrlsContainIndexPatternId(moduleConfig)) { + if (this._indexPatternId === undefined && this.doJobUrlsContainIndexPatternId(moduleConfig)) { throw Boom.badRequest( - `Module's jobs contain custom URLs which require a kibana index pattern (${this.indexPatternName}) which cannot be found.` + `Module's jobs contain custom URLs which require a kibana index pattern (${this._indexPatternName}) which cannot be found.` ); } // the module's saved objects require an index patten id // but there is no corresponding index pattern, throw an error if ( - this.indexPatternId === undefined && + this._indexPatternId === undefined && this.doSavedObjectsContainIndexPatternId(moduleConfig) ) { throw Boom.badRequest( - `Module's saved objects contain custom URLs which require a kibana index pattern (${this.indexPatternName}) which cannot be found.` + `Module's saved objects contain custom URLs which require a kibana index pattern (${this._indexPatternName}) which cannot be found.` ); } @@ -495,7 +510,7 @@ export class DataRecognizer { // Add a wildcard at the front of each of the job IDs in the module, // as a prefix may have been supplied when creating the jobs in the module. const jobIds = module.jobs.map((job) => `*${job.id}`); - const { jobsExist } = jobServiceProvider(this.callAsCurrentUser); + const { jobsExist } = jobServiceProvider(this._mlClusterClient); const jobInfo = await jobsExist(jobIds); // Check if the value for any of the jobs is false. @@ -504,11 +519,13 @@ export class DataRecognizer { if (doJobsExist === true) { // Get the IDs of the jobs created from the module, and their earliest / latest timestamps. - const jobStats: MlJobStats = await this.callAsCurrentUser('ml.jobStats', { jobId: jobIds }); + const jobStats: MlJobStats = await this._callAsInternalUser('ml.jobStats', { + jobId: jobIds, + }); const jobStatsJobs: JobStat[] = []; if (jobStats.jobs && jobStats.jobs.length > 0) { const foundJobIds = jobStats.jobs.map((job) => job.job_id); - const { getLatestBucketTimestampByJob } = resultsServiceProvider(this.callAsCurrentUser); + const { getLatestBucketTimestampByJob } = resultsServiceProvider(this._mlClusterClient); const latestBucketTimestampsByJob = await getLatestBucketTimestampByJob(foundJobIds); jobStats.jobs.forEach((job) => { @@ -669,7 +686,7 @@ export class DataRecognizer { async saveJob(job: ModuleJob) { const { id: jobId, config: body } = job; - return this.callAsCurrentUser('ml.addJob', { jobId, body }); + return this._callAsInternalUser('ml.addJob', { jobId, body }); } // save the datafeeds. @@ -690,7 +707,11 @@ export class DataRecognizer { async saveDatafeed(datafeed: ModuleDataFeed) { const { id: datafeedId, config: body } = datafeed; - return this.callAsCurrentUser('ml.addDatafeed', { datafeedId, body }); + return this._callAsInternalUser('ml.addDatafeed', { + datafeedId, + body, + ...this._authorizationHeader, + }); } async startDatafeeds( @@ -713,7 +734,7 @@ export class DataRecognizer { const result = { started: false } as DatafeedResponse; let opened = false; try { - const openResult = await this.callAsCurrentUser('ml.openJob', { + const openResult = await this._callAsInternalUser('ml.openJob', { jobId: datafeed.config.job_id, }); opened = openResult.opened; @@ -737,7 +758,10 @@ export class DataRecognizer { duration.end = end; } - await this.callAsCurrentUser('ml.startDatafeed', { datafeedId: datafeed.id, ...duration }); + await this._callAsInternalUser('ml.startDatafeed', { + datafeedId: datafeed.id, + ...duration, + }); result.started = true; } catch (error) { result.started = false; @@ -838,7 +862,7 @@ export class DataRecognizer { updateDatafeedIndices(moduleConfig: Module) { // if the supplied index pattern contains a comma, split into multiple indices and // add each one to the datafeed - const indexPatternNames = splitIndexPatternNames(this.indexPatternName); + const indexPatternNames = splitIndexPatternNames(this._indexPatternName); moduleConfig.datafeeds.forEach((df) => { const newIndices: string[] = []; @@ -876,7 +900,7 @@ export class DataRecognizer { if (url.match(INDEX_PATTERN_ID)) { const newUrl = url.replace( new RegExp(INDEX_PATTERN_ID, 'g'), - this.indexPatternId as string + this._indexPatternId as string ); // update the job's url cUrl.url_value = newUrl; @@ -915,7 +939,7 @@ export class DataRecognizer { if (jsonString.match(INDEX_PATTERN_ID)) { jsonString = jsonString.replace( new RegExp(INDEX_PATTERN_ID, 'g'), - this.indexPatternId as string + this._indexPatternId as string ); item.config.kibanaSavedObjectMeta!.searchSourceJSON = jsonString; } @@ -927,7 +951,7 @@ export class DataRecognizer { if (visStateString !== undefined && visStateString.match(INDEX_PATTERN_NAME)) { visStateString = visStateString.replace( new RegExp(INDEX_PATTERN_NAME, 'g'), - this.indexPatternName + this._indexPatternName ); item.config.visState = visStateString; } @@ -944,10 +968,10 @@ export class DataRecognizer { timeField: string, query?: any ): Promise<{ start: number; end: number }> { - const fieldsService = fieldsServiceProvider(this.callAsCurrentUser); + const fieldsService = fieldsServiceProvider(this._mlClusterClient); const timeFieldRange = await fieldsService.getTimeFieldRange( - this.indexPatternName, + this._indexPatternName, timeField, query ); @@ -974,7 +998,7 @@ export class DataRecognizer { if (estimateMML && this.jobsForModelMemoryEstimation.length > 0) { try { - const calculateModelMemoryLimit = calculateModelMemoryLimitProvider(this.callAsCurrentUser); + const calculateModelMemoryLimit = calculateModelMemoryLimitProvider(this._mlClusterClient); // Checks if all jobs in the module have the same time field configured const firstJobTimeField = this.jobsForModelMemoryEstimation[0].job.config.data_description @@ -1009,7 +1033,7 @@ export class DataRecognizer { const { modelMemoryLimit } = await calculateModelMemoryLimit( job.config.analysis_config, - this.indexPatternName, + this._indexPatternName, query, job.config.data_description.time_field, earliestMs, @@ -1027,20 +1051,20 @@ export class DataRecognizer { } } - const { limits } = await this.callAsCurrentUser('ml.info'); + const { limits } = (await this._callAsInternalUser('ml.info')) as MlInfoResponse; const maxMml = limits.max_model_memory_limit; if (!maxMml) { return; } - // @ts-ignore + // @ts-expect-error const maxBytes: number = numeral(maxMml.toUpperCase()).value(); for (const job of moduleConfig.jobs) { const mml = job.config?.analysis_limits?.model_memory_limit; if (mml !== undefined) { - // @ts-ignore + // @ts-expect-error const mmlBytes: number = numeral(mml.toUpperCase()).value(); if (mmlBytes > maxBytes) { // if the job's mml is over the max, diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/manifest.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/manifest.json index 3c7b1c7cfffd4..1e7fcdd4320f8 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/manifest.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/manifest.json @@ -1,7 +1,7 @@ { "id": "siem_auditbeat", - "title": "SIEM Auditbeat", - "description": "Detect suspicious network activity and unusual processes in Auditbeat data (beta).", + "title": "Security: Auditbeat", + "description": "Detect suspicious network activity and unusual processes in Auditbeat data.", "type": "Auditbeat data", "logoFile": "logo.json", "defaultIndexPattern": "auditbeat-*", diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_network_activity_ecs.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_network_activity_ecs.json index e409903a2801e..eab14d7c11ba1 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_network_activity_ecs.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_network_activity_ecs.json @@ -1,8 +1,8 @@ { "job_type": "anomaly_detector", - "description": "SIEM Auditbeat: Looks for unusual processes using the network which could indicate command-and-control, lateral movement, persistence, or data exfiltration activity (beta)", + "description": "Security: Auditbeat - Looks for unusual processes using the network which could indicate command-and-control, lateral movement, persistence, or data exfiltration activity.", "groups": [ - "siem", + "security", "auditbeat", "process" ], @@ -34,19 +34,19 @@ "custom_urls": [ { "url_name": "Host Details by process name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Host Details by user name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by process name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by user name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_network_port_activity_ecs.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_network_port_activity_ecs.json index a87c99da478d2..1891be831837b 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_network_port_activity_ecs.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_network_port_activity_ecs.json @@ -1,8 +1,8 @@ { "job_type": "anomaly_detector", - "description": "SIEM Auditbeat: Looks for unusual destination port activity that could indicate command-and-control, persistence mechanism, or data exfiltration activity (beta)", + "description": "Security: Auditbeat - Looks for unusual destination port activity that could indicate command-and-control, persistence mechanism, or data exfiltration activity.", "groups": [ - "siem", + "security", "auditbeat", "network" ], @@ -34,19 +34,19 @@ "custom_urls": [ { "url_name": "Host Details by process name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Host Details by user name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by process name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by user name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_network_service.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_network_service.json index 9ded51f09200b..8fd24dd817c35 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_network_service.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_network_service.json @@ -1,11 +1,11 @@ { "job_type": "anomaly_detector", "groups": [ - "siem", + "security", "auditbeat", "network" ], - "description": "SIEM Auditbeat: Looks for unusual listening ports that could indicate execution of unauthorized services, backdoors, or persistence mechanisms (beta)", + "description": "Security: Auditbeat - Looks for unusual listening ports that could indicate execution of unauthorized services, backdoors, or persistence mechanisms.", "analysis_config": { "bucket_span": "15m", "detectors": [ @@ -33,20 +33,20 @@ "custom_urls": [ { "url_name": "Host Details by process name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Host Details by user name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by process name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by user name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } -} \ No newline at end of file +} diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_network_url_activity_ecs.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_network_url_activity_ecs.json index 4f8da6c486fff..aa43a50e76863 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_network_url_activity_ecs.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_network_url_activity_ecs.json @@ -1,40 +1,40 @@ { - "job_type": "anomaly_detector", - "groups": [ - "siem", - "auditbeat", - "network" + "job_type": "anomaly_detector", + "groups": [ + "security", + "auditbeat", + "network" + ], + "description": "Security: Auditbeat - Looks for an unusual web URL request from a Linux instance. Curl and wget web request activity is very common but unusual web requests from a Linux server can sometimes be malware delivery or execution.", + "analysis_config": { + "bucket_span": "15m", + "detectors": [ + { + "detector_description": "rare by \"process.title\"", + "function": "rare", + "by_field_name": "process.title" + } ], - "description": "SIEM Auditbeat: Looks for an unusual web URL request from a Linux instance. Curl and wget web request activity is very common but unusual web requests from a Linux server can sometimes be malware delivery or execution (beta)", - "analysis_config": { - "bucket_span": "15m", - "detectors": [ - { - "detector_description": "rare by \"process.title\"", - "function": "rare", - "by_field_name": "process.title" - } - ], - "influencers": [ - "host.name", - "destination.ip", - "destination.port" - ] - }, - "allow_lazy_open": true, - "analysis_limits": { - "model_memory_limit": "32mb" - }, - "data_description": { - "time_field": "@timestamp" - }, - "custom_settings": { - "created_by": "ml-module-siem-auditbeat", - "custom_urls": [ - { - "url_name": "Host Details", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" - } - ] - } + "influencers": [ + "host.name", + "destination.ip", + "destination.port" + ] + }, + "allow_lazy_open": true, + "analysis_limits": { + "model_memory_limit": "32mb" + }, + "data_description": { + "time_field": "@timestamp" + }, + "custom_settings": { + "created_by": "ml-module-siem-auditbeat", + "custom_urls": [ + { + "url_name": "Host Details", + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + } + ] } +} diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_process_all_hosts_ecs.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_process_all_hosts_ecs.json index a204828d2669c..17f38b65de4c6 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_process_all_hosts_ecs.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_process_all_hosts_ecs.json @@ -1,8 +1,8 @@ { "job_type": "anomaly_detector", - "description": "SIEM Auditbeat: Looks for processes that are unusual to all Linux hosts. Such unusual processes may indicate unauthorized services, malware, or persistence mechanisms (beta)", + "description": "Security: Auditbeat - Looks for processes that are unusual to all Linux hosts. Such unusual processes may indicate unauthorized services, malware, or persistence mechanisms.", "groups": [ - "siem", + "security", "auditbeat", "process" ], @@ -33,20 +33,20 @@ "custom_urls": [ { "url_name": "Host Details by process name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Host Details by user name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by process name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by user name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } -} \ No newline at end of file +} diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_user_name_ecs.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_user_name_ecs.json index c7c14a35054b2..8f0eda20a55fc 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_user_name_ecs.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/linux_anomalous_user_name_ecs.json @@ -1,11 +1,11 @@ { "job_type": "anomaly_detector", "groups": [ - "siem", + "security", "auditbeat", "process" ], - "description": "SIEM Auditbeat: Rare and unusual users that are not normally active may indicate unauthorized changes or activity by an unauthorized user which may be credentialed access or lateral movement (beta)", + "description": "Security: Auditbeat - Rare and unusual users that are not normally active may indicate unauthorized changes or activity by an unauthorized user which may be credentialed access or lateral movement.", "analysis_config": { "bucket_span": "15m", "detectors": [ @@ -33,19 +33,19 @@ "custom_urls": [ { "url_name": "Host Details by process name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Host Details by user name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by process name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by user name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/rare_process_by_host_linux_ecs.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/rare_process_by_host_linux_ecs.json index aa9d49137c595..75ac0224dbd5b 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/rare_process_by_host_linux_ecs.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat/ml/rare_process_by_host_linux_ecs.json @@ -1,8 +1,8 @@ { "job_type": "anomaly_detector", - "description": "SIEM Auditbeat: Detect unusually rare processes on Linux (beta)", + "description": "Security: Auditbeat - Detect unusually rare processes on Linux", "groups": [ - "siem", + "security", "auditbeat", "process" ], @@ -34,20 +34,20 @@ "custom_urls": [ { "url_name": "Host Details by process name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Host Details by user name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by process name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by user name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } -} \ No newline at end of file +} diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat_auth/manifest.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat_auth/manifest.json index 4b86752e45a92..f6e878de8169b 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat_auth/manifest.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat_auth/manifest.json @@ -1,7 +1,7 @@ { "id": "siem_auditbeat_auth", - "title": "SIEM Auditbeat Authentication", - "description": "Detect suspicious authentication events in Auditbeat data (beta).", + "title": "Security: Auditbeat Authentication", + "description": "Detect suspicious authentication events in Auditbeat data.", "type": "Auditbeat data", "logoFile": "logo.json", "defaultIndexPattern": "auditbeat-*", diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat_auth/ml/suspicious_login_activity_ecs.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat_auth/ml/suspicious_login_activity_ecs.json index 4f48cd0ffc114..9ee26b314c640 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat_auth/ml/suspicious_login_activity_ecs.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_auditbeat_auth/ml/suspicious_login_activity_ecs.json @@ -1,8 +1,8 @@ { "job_type": "anomaly_detector", - "description": "SIEM Auditbeat: Detect unusually high number of authentication attempts (beta)", + "description": "Security: Auditbeat - Detect unusually high number of authentication attempts.", "groups": [ - "siem", + "security", "auditbeat", "authentication" ], @@ -33,8 +33,8 @@ "custom_urls": [ { "url_name": "IP Address Details", - "url_value": "siem#/ml-network/ip/$source.ip$?_g=()&query=!n&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/network/ml-network/ip/$source.ip$?_g=()&query=!n&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } -} \ No newline at end of file +} diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/manifest.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/manifest.json index b7afe8d2b158a..33940f20db903 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/manifest.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/manifest.json @@ -1,64 +1,64 @@ { - "id": "siem_cloudtrail", - "title": "SIEM Cloudtrail", - "description": "Detect suspicious activity recorded in your cloudtrail logs.", - "type": "Filebeat data", - "logoFile": "logo.json", - "defaultIndexPattern": "filebeat-*", - "query": { - "bool": { - "filter": [ - {"term": {"event.dataset": "aws.cloudtrail"}} - ] - } + "id": "siem_cloudtrail", + "title": "Security: Cloudtrail", + "description": "Detect suspicious activity recorded in your cloudtrail logs.", + "type": "Filebeat data", + "logoFile": "logo.json", + "defaultIndexPattern": "filebeat-*", + "query": { + "bool": { + "filter": [ + {"term": {"event.dataset": "aws.cloudtrail"}} + ] + } + }, + "jobs": [ + { + "id": "rare_method_for_a_city", + "file": "rare_method_for_a_city.json" }, - "jobs": [ - { - "id": "rare_method_for_a_city", - "file": "rare_method_for_a_city.json" - }, - { - "id": "rare_method_for_a_country", - "file": "rare_method_for_a_country.json" - }, - { - "id": "rare_method_for_a_username", - "file": "rare_method_for_a_username.json" - }, - { - "id": "high_distinct_count_error_message", - "file": "high_distinct_count_error_message.json" - }, - { - "id": "rare_error_code", - "file": "rare_error_code.json" - } - ], - "datafeeds": [ - { - "id": "datafeed-rare_method_for_a_city", - "file": "datafeed_rare_method_for_a_city.json", - "job_id": "rare_method_for_a_city" - }, - { - "id": "datafeed-rare_method_for_a_country", - "file": "datafeed_rare_method_for_a_country.json", - "job_id": "rare_method_for_a_country" - }, - { - "id": "datafeed-rare_method_for_a_username", - "file": "datafeed_rare_method_for_a_username.json", - "job_id": "rare_method_for_a_username" - }, - { - "id": "datafeed-high_distinct_count_error_message", - "file": "datafeed_high_distinct_count_error_message.json", - "job_id": "high_distinct_count_error_message" - }, - { - "id": "datafeed-rare_error_code", - "file": "datafeed_rare_error_code.json", - "job_id": "rare_error_code" - } - ] - } \ No newline at end of file + { + "id": "rare_method_for_a_country", + "file": "rare_method_for_a_country.json" + }, + { + "id": "rare_method_for_a_username", + "file": "rare_method_for_a_username.json" + }, + { + "id": "high_distinct_count_error_message", + "file": "high_distinct_count_error_message.json" + }, + { + "id": "rare_error_code", + "file": "rare_error_code.json" + } + ], + "datafeeds": [ + { + "id": "datafeed-rare_method_for_a_city", + "file": "datafeed_rare_method_for_a_city.json", + "job_id": "rare_method_for_a_city" + }, + { + "id": "datafeed-rare_method_for_a_country", + "file": "datafeed_rare_method_for_a_country.json", + "job_id": "rare_method_for_a_country" + }, + { + "id": "datafeed-rare_method_for_a_username", + "file": "datafeed_rare_method_for_a_username.json", + "job_id": "rare_method_for_a_username" + }, + { + "id": "datafeed-high_distinct_count_error_message", + "file": "datafeed_high_distinct_count_error_message.json", + "job_id": "high_distinct_count_error_message" + }, + { + "id": "datafeed-rare_error_code", + "file": "datafeed_rare_error_code.json", + "job_id": "rare_error_code" + } + ] +} diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/high_distinct_count_error_message.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/high_distinct_count_error_message.json index fdabf66ac91b3..98d145a91d9a7 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/high_distinct_count_error_message.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/high_distinct_count_error_message.json @@ -1,33 +1,33 @@ { - "job_type": "anomaly_detector", - "description": "Looks for a spike in the rate of an error message which may simply indicate an impending service failure but these can also be byproducts of attempted or successful persistence, privilege escalation, defense evasion, discovery, lateral movement, or collection activity by a threat actor.", - "groups": [ - "siem", - "cloudtrail" + "job_type": "anomaly_detector", + "description": "Security: Cloudtrail - Looks for a spike in the rate of an error message which may simply indicate an impending service failure but these can also be byproducts of attempted or successful persistence, privilege escalation, defense evasion, discovery, lateral movement, or collection activity by a threat actor.", + "groups": [ + "security", + "cloudtrail" + ], + "analysis_config": { + "bucket_span": "15m", + "detectors": [ + { + "detector_description": "high_distinct_count(\"aws.cloudtrail.error_message\")", + "function": "high_distinct_count", + "field_name": "aws.cloudtrail.error_message" + } ], - "analysis_config": { - "bucket_span": "15m", - "detectors": [ - { - "detector_description": "high_distinct_count(\"aws.cloudtrail.error_message\")", - "function": "high_distinct_count", - "field_name": "aws.cloudtrail.error_message" - } - ], - "influencers": [ - "aws.cloudtrail.user_identity.arn", - "source.ip", - "source.geo.city_name" - ] - }, - "allow_lazy_open": true, - "analysis_limits": { - "model_memory_limit": "16mb" - }, - "data_description": { - "time_field": "@timestamp" - }, - "custom_settings": { - "created_by": "ml-module-siem-cloudtrail" - } - } \ No newline at end of file + "influencers": [ + "aws.cloudtrail.user_identity.arn", + "source.ip", + "source.geo.city_name" + ] + }, + "allow_lazy_open": true, + "analysis_limits": { + "model_memory_limit": "16mb" + }, + "data_description": { + "time_field": "@timestamp" + }, + "custom_settings": { + "created_by": "ml-module-siem-cloudtrail" + } +} diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/rare_error_code.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/rare_error_code.json index 0f8fa814ac60a..0227483f262a4 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/rare_error_code.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/rare_error_code.json @@ -1,33 +1,33 @@ { - "job_type": "anomaly_detector", - "description": "Looks for unsual errors. Rare and unusual errors may simply indicate an impending service failure but they can also be byproducts of attempted or successful persistence, privilege escalation, defense evasion, discovery, lateral movement, or collection activity by a threat actor.", - "groups": [ - "siem", - "cloudtrail" + "job_type": "anomaly_detector", + "description": "Security: Cloudtrail - Looks for unusual errors. Rare and unusual errors may simply indicate an impending service failure but they can also be byproducts of attempted or successful persistence, privilege escalation, defense evasion, discovery, lateral movement, or collection activity by a threat actor.", + "groups": [ + "security", + "cloudtrail" + ], + "analysis_config": { + "bucket_span": "60m", + "detectors": [ + { + "detector_description": "rare by \"aws.cloudtrail.error_code\"", + "function": "rare", + "by_field_name": "aws.cloudtrail.error_code" + } ], - "analysis_config": { - "bucket_span": "60m", - "detectors": [ - { - "detector_description": "rare by \"aws.cloudtrail.error_code\"", - "function": "rare", - "by_field_name": "aws.cloudtrail.error_code" - } - ], - "influencers": [ - "aws.cloudtrail.user_identity.arn", - "source.ip", - "source.geo.city_name" - ] - }, - "allow_lazy_open": true, - "analysis_limits": { - "model_memory_limit": "16mb" - }, - "data_description": { - "time_field": "@timestamp" - }, - "custom_settings": { - "created_by": "ml-module-siem-cloudtrail" - } - } \ No newline at end of file + "influencers": [ + "aws.cloudtrail.user_identity.arn", + "source.ip", + "source.geo.city_name" + ] + }, + "allow_lazy_open": true, + "analysis_limits": { + "model_memory_limit": "16mb" + }, + "data_description": { + "time_field": "@timestamp" + }, + "custom_settings": { + "created_by": "ml-module-siem-cloudtrail" + } +} diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/rare_method_for_a_city.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/rare_method_for_a_city.json index eff4d4cdbb889..228ad07d43532 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/rare_method_for_a_city.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/rare_method_for_a_city.json @@ -1,34 +1,34 @@ { - "job_type": "anomaly_detector", - "description": "Looks for AWS API calls that, while not inherently suspicious or abnormal, are sourcing from a geolocation (city) that is unusual. This can be the result of compromised credentials or keys.", - "groups": [ - "siem", - "cloudtrail" + "job_type": "anomaly_detector", + "description": "Security: Cloudtrail - Looks for AWS API calls that, while not inherently suspicious or abnormal, are sourcing from a geolocation (city) that is unusual. This can be the result of compromised credentials or keys.", + "groups": [ + "security", + "cloudtrail" + ], + "analysis_config": { + "bucket_span": "60m", + "detectors": [ + { + "detector_description": "rare by \"event.action\" partition by \"source.geo.city_name\"", + "function": "rare", + "by_field_name": "event.action", + "partition_field_name": "source.geo.city_name" + } ], - "analysis_config": { - "bucket_span": "60m", - "detectors": [ - { - "detector_description": "rare by \"event.action\" partition by \"source.geo.city_name\"", - "function": "rare", - "by_field_name": "event.action", - "partition_field_name": "source.geo.city_name" - } - ], - "influencers": [ - "aws.cloudtrail.user_identity.arn", - "source.ip", - "source.geo.city_name" - ] - }, - "allow_lazy_open": true, - "analysis_limits": { - "model_memory_limit": "64mb" - }, - "data_description": { - "time_field": "@timestamp" - }, - "custom_settings": { - "created_by": "ml-module-siem-cloudtrail" - } - } \ No newline at end of file + "influencers": [ + "aws.cloudtrail.user_identity.arn", + "source.ip", + "source.geo.city_name" + ] + }, + "allow_lazy_open": true, + "analysis_limits": { + "model_memory_limit": "64mb" + }, + "data_description": { + "time_field": "@timestamp" + }, + "custom_settings": { + "created_by": "ml-module-siem-cloudtrail" + } +} diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/rare_method_for_a_country.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/rare_method_for_a_country.json index 810822c30a5dd..fdba3ff12945c 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/rare_method_for_a_country.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/rare_method_for_a_country.json @@ -1,34 +1,34 @@ { - "job_type": "anomaly_detector", - "description": "Looks for AWS API calls that, while not inherently suspicious or abnormal, are sourcing from a geolocation (country) that is unusual. This can be the result of compromised credentials or keys.", - "groups": [ - "siem", - "cloudtrail" + "job_type": "anomaly_detector", + "description": "Security: Cloudtrail - Looks for AWS API calls that, while not inherently suspicious or abnormal, are sourcing from a geolocation (country) that is unusual. This can be the result of compromised credentials or keys.", + "groups": [ + "security", + "cloudtrail" + ], + "analysis_config": { + "bucket_span": "60m", + "detectors": [ + { + "detector_description": "rare by \"event.action\" partition by \"source.geo.country_iso_code\"", + "function": "rare", + "by_field_name": "event.action", + "partition_field_name": "source.geo.country_iso_code" + } ], - "analysis_config": { - "bucket_span": "60m", - "detectors": [ - { - "detector_description": "rare by \"event.action\" partition by \"source.geo.country_iso_code\"", - "function": "rare", - "by_field_name": "event.action", - "partition_field_name": "source.geo.country_iso_code" - } - ], - "influencers": [ - "aws.cloudtrail.user_identity.arn", - "source.ip", - "source.geo.country_iso_code" - ] - }, - "allow_lazy_open": true, - "analysis_limits": { - "model_memory_limit": "64mb" - }, - "data_description": { - "time_field": "@timestamp" - }, - "custom_settings": { - "created_by": "ml-module-siem-cloudtrail" - } - } \ No newline at end of file + "influencers": [ + "aws.cloudtrail.user_identity.arn", + "source.ip", + "source.geo.country_iso_code" + ] + }, + "allow_lazy_open": true, + "analysis_limits": { + "model_memory_limit": "64mb" + }, + "data_description": { + "time_field": "@timestamp" + }, + "custom_settings": { + "created_by": "ml-module-siem-cloudtrail" + } +} diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/rare_method_for_a_username.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/rare_method_for_a_username.json index 2edf52e8351ed..ea39a889a783e 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/rare_method_for_a_username.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_cloudtrail/ml/rare_method_for_a_username.json @@ -1,34 +1,34 @@ { - "job_type": "anomaly_detector", - "description": "Looks for AWS API calls that, while not inherently suspicious or abnormal, are sourcing from a user context that does not normally call the method. This can be the result of compromised credentials or keys as someone uses a valid account to persist, move laterally, or exfil data.", - "groups": [ - "siem", - "cloudtrail" + "job_type": "anomaly_detector", + "description": "Security: Cloudtrail - Looks for AWS API calls that, while not inherently suspicious or abnormal, are sourcing from a user context that does not normally call the method. This can be the result of compromised credentials or keys as someone uses a valid account to persist, move laterally, or exfil data.", + "groups": [ + "security", + "cloudtrail" + ], + "analysis_config": { + "bucket_span": "60m", + "detectors": [ + { + "detector_description": "rare by \"event.action\" partition by \"user.name\"", + "function": "rare", + "by_field_name": "event.action", + "partition_field_name": "user.name" + } ], - "analysis_config": { - "bucket_span": "60m", - "detectors": [ - { - "detector_description": "rare by \"event.action\" partition by \"user.name\"", - "function": "rare", - "by_field_name": "event.action", - "partition_field_name": "user.name" - } - ], - "influencers": [ - "user.name", - "source.ip", - "source.geo.city_name" - ] - }, - "allow_lazy_open": true, - "analysis_limits": { - "model_memory_limit": "128mb" - }, - "data_description": { - "time_field": "@timestamp" - }, - "custom_settings": { - "created_by": "ml-module-siem-cloudtrail" - } - } \ No newline at end of file + "influencers": [ + "user.name", + "source.ip", + "source.geo.city_name" + ] + }, + "allow_lazy_open": true, + "analysis_limits": { + "model_memory_limit": "128mb" + }, + "data_description": { + "time_field": "@timestamp" + }, + "custom_settings": { + "created_by": "ml-module-siem-cloudtrail" + } +} diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/manifest.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/manifest.json index 9109cbc15ca6f..e11e1726076d9 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/manifest.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/manifest.json @@ -1,7 +1,7 @@ { "id": "siem_packetbeat", - "title": "SIEM Packetbeat", - "description": "Detect suspicious network activity in Packetbeat data (beta).", + "title": "Security: Packetbeat", + "description": "Detect suspicious network activity in Packetbeat data.", "type": "Packetbeat data", "logoFile": "logo.json", "defaultIndexPattern": "packetbeat-*", diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_dns_tunneling.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_dns_tunneling.json index 0f0fca1bf560a..0332fd53814a6 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_dns_tunneling.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_dns_tunneling.json @@ -1,8 +1,8 @@ { "job_type": "anomaly_detector", - "description": "SIEM Packetbeat: Looks for unusual DNS activity that could indicate command-and-control or data exfiltration activity (beta)", + "description": "Security: Packetbeat - Looks for unusual DNS activity that could indicate command-and-control or data exfiltration activity.", "groups": [ - "siem", + "security", "packetbeat", "dns" ], @@ -48,7 +48,7 @@ "custom_urls": [ { "url_name": "Host Details", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_rare_dns_question.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_rare_dns_question.json index d2c4a0ca50dc4..c3c2402e13f72 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_rare_dns_question.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_rare_dns_question.json @@ -1,8 +1,8 @@ { "job_type": "anomaly_detector", - "description": "SIEM Packetbeat: Looks for unusual DNS activity that could indicate command-and-control activity (beta)", + "description": "Security: Packetbeat - Looks for unusual DNS activity that could indicate command-and-control activity.", "groups": [ - "siem", + "security", "packetbeat", "dns" ], @@ -31,7 +31,7 @@ "custom_urls": [ { "url_name": "Host Details", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_rare_server_domain.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_rare_server_domain.json index 132cf9fff04cc..14e01df1285d8 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_rare_server_domain.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_rare_server_domain.json @@ -1,8 +1,8 @@ { "job_type": "anomaly_detector", - "description": "SIEM Packetbeat: Looks for unusual HTTP or TLS destination domain activity that could indicate execution, persistence, command-and-control or data exfiltration activity (beta)", + "description": "Security: Packetbeat - Looks for unusual HTTP or TLS destination domain activity that could indicate execution, persistence, command-and-control or data exfiltration activity.", "groups": [ - "siem", + "security", "packetbeat", "web" ], @@ -33,7 +33,7 @@ "custom_urls": [ { "url_name": "Host Details", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_rare_urls.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_rare_urls.json index e0791ad4eaea9..ad664bed49c55 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_rare_urls.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_rare_urls.json @@ -1,8 +1,8 @@ { "job_type": "anomaly_detector", - "description": "SIEM Packetbeat: Looks for unusual web browsing URL activity that could indicate execution, persistence, command-and-control or data exfiltration activity (beta)", + "description": "Security: Packetbeat - Looks for unusual web browsing URL activity that could indicate execution, persistence, command-and-control or data exfiltration activity.", "groups": [ - "siem", + "security", "packetbeat", "web" ], @@ -32,7 +32,7 @@ "custom_urls": [ { "url_name": "Host Details", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_rare_user_agent.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_rare_user_agent.json index eae29466a6417..0dddf3e5d632e 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_rare_user_agent.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_packetbeat/ml/packetbeat_rare_user_agent.json @@ -1,8 +1,8 @@ { "job_type": "anomaly_detector", - "description": "SIEM Packetbeat: Looks for unusual HTTP user agent activity that could indicate execution, persistence, command-and-control or data exfiltration activity (beta)", + "description": "Security: Packetbeat - Looks for unusual HTTP user agent activity that could indicate execution, persistence, command-and-control or data exfiltration activity.", "groups": [ - "siem", + "security", "packetbeat", "web" ], @@ -14,7 +14,7 @@ "function": "rare", "by_field_name": "user_agent.original" } - ], + ], "influencers": [ "host.name", "destination.ip" @@ -32,7 +32,7 @@ "custom_urls": [ { "url_name": "Host Details", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/manifest.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/manifest.json index 682b9a833f23f..ffbf5aa7d8bb0 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/manifest.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/manifest.json @@ -1,7 +1,7 @@ { "id": "siem_winlogbeat", - "title": "SIEM Winlogbeat", - "description": "Detect unusual processes and network activity in Winlogbeat data (beta).", + "title": "Security: Winlogbeat", + "description": "Detect unusual processes and network activity in Winlogbeat data.", "type": "Winlogbeat data", "logoFile": "logo.json", "defaultIndexPattern": "winlogbeat-*", diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/rare_process_by_host_windows_ecs.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/rare_process_by_host_windows_ecs.json index a0480a94e5356..49c936e33f70f 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/rare_process_by_host_windows_ecs.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/rare_process_by_host_windows_ecs.json @@ -1,8 +1,8 @@ { "job_type": "anomaly_detector", - "description": "SIEM Winlogbeat: Detect unusually rare processes on Windows (beta)", + "description": "Security: Winlogbeat - Detect unusually rare processes on Windows.", "groups": [ - "siem", + "security", "winlogbeat", "process" ], @@ -34,20 +34,20 @@ "custom_urls": [ { "url_name": "Host Details by process name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Host Details by user name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by process name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by user name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } -} \ No newline at end of file +} diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_network_activity_ecs.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_network_activity_ecs.json index c05b1a61e169a..d3fb038f85584 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_network_activity_ecs.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_network_activity_ecs.json @@ -1,8 +1,8 @@ { "job_type": "anomaly_detector", - "description": "SIEM Winlogbeat: Looks for unusual processes using the network which could indicate command-and-control, lateral movement, persistence, or data exfiltration activity (beta)", + "description": "Security: Winlogbeat - Looks for unusual processes using the network which could indicate command-and-control, lateral movement, persistence, or data exfiltration activity.", "groups": [ - "siem", + "security", "winlogbeat", "network" ], @@ -34,19 +34,19 @@ "custom_urls": [ { "url_name": "Host Details by process name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Host Details by user name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by process name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by user name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_path_activity_ecs.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_path_activity_ecs.json index 7133335c44765..6a667527225a9 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_path_activity_ecs.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_path_activity_ecs.json @@ -1,11 +1,11 @@ { "job_type": "anomaly_detector", "groups": [ - "siem", + "security", "winlogbeat", "process" ], - "description": "SIEM Winlogbeat: Looks for activity in unusual paths that may indicate execution of malware or persistence mechanisms. Windows payloads often execute from user profile paths (beta)", + "description": "Security: Winlogbeat - Looks for activity in unusual paths that may indicate execution of malware or persistence mechanisms. Windows payloads often execute from user profile paths.", "analysis_config": { "bucket_span": "15m", "detectors": [ @@ -33,20 +33,20 @@ "custom_urls": [ { "url_name": "Host Details by process name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Host Details by user name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by process name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by user name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } -} \ No newline at end of file +} diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_process_all_hosts_ecs.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_process_all_hosts_ecs.json index c99cb802ca249..9b23aa5a95e6c 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_process_all_hosts_ecs.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_process_all_hosts_ecs.json @@ -1,8 +1,8 @@ { "job_type": "anomaly_detector", - "description": "SIEM Winlogbeat: Looks for processes that are unusual to all Windows hosts. Such unusual processes may indicate execution of unauthorized services, malware, or persistence mechanisms (beta)", + "description": "Security: Winlogbeat - Looks for processes that are unusual to all Windows hosts. Such unusual processes may indicate execution of unauthorized services, malware, or persistence mechanisms.", "groups": [ - "siem", + "security", "winlogbeat", "process" ], @@ -33,19 +33,19 @@ "custom_urls": [ { "url_name": "Host Details by process name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Host Details by user name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by process name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by user name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_process_creation.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_process_creation.json index 98b17c2adb42e..9d90bba824418 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_process_creation.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_process_creation.json @@ -1,11 +1,11 @@ { "job_type": "anomaly_detector", "groups": [ - "siem", + "security", "winlogbeat", "process" ], - "description": "SIEM Winlogbeat: Looks for unusual process relationships which may indicate execution of malware or persistence mechanisms (beta)", + "description": "Security: Winlogbeat - Looks for unusual process relationships which may indicate execution of malware or persistence mechanisms.", "analysis_config": { "bucket_span": "15m", "detectors": [ @@ -33,20 +33,20 @@ "custom_urls": [ { "url_name": "Host Details by process name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Host Details by user name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by process name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by user name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } -} \ No newline at end of file +} diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_script.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_script.json index 9d98855c8e2c5..613a446750e5f 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_script.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_script.json @@ -1,8 +1,8 @@ { "job_type": "anomaly_detector", - "description": "SIEM Winlogbeat: Looks for unusual powershell scripts that may indicate execution of malware, or persistence mechanisms (beta)", + "description": "Security: Winlogbeat - Looks for unusual powershell scripts that may indicate execution of malware, or persistence mechanisms.", "groups": [ - "siem", + "security", "winlogbeat", "powershell" ], @@ -33,12 +33,12 @@ "custom_urls": [ { "url_name": "Host Details by user name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by user name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } -} \ No newline at end of file +} diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_service.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_service.json index 45b66aa7650cb..6debad30c308a 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_service.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_service.json @@ -1,11 +1,11 @@ { "job_type": "anomaly_detector", "groups": [ - "siem", - "winlogbeat", - "system" + "security", + "winlogbeat", + "system" ], - "description": "SIEM Winlogbeat: Looks for rare and unusual Windows services which may indicate execution of unauthorized services, malware, or persistence mechanisms (beta)", + "description": "Security: Winlogbeat - Looks for rare and unusual Windows services which may indicate execution of unauthorized services, malware, or persistence mechanisms.", "analysis_config": { "bucket_span": "15m", "detectors": [ @@ -32,7 +32,7 @@ "custom_urls": [ { "url_name": "Host Details", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_user_name_ecs.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_user_name_ecs.json index 10f60ca1aa4d8..7d9244a230ac3 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_user_name_ecs.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_anomalous_user_name_ecs.json @@ -1,8 +1,8 @@ { "job_type": "anomaly_detector", - "description": "SIEM Winlogbeat: Rare and unusual users that are not normally active may indicate unauthorized changes or activity by an unauthorized user which may be credentialed access or lateral movement (beta)", + "description": "Security: Winlogbeat - Rare and unusual users that are not normally active may indicate unauthorized changes or activity by an unauthorized user which may be credentialed access or lateral movement.", "groups": [ - "siem", + "security", "winlogbeat", "process" ], @@ -33,19 +33,19 @@ "custom_urls": [ { "url_name": "Host Details by process name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Host Details by user name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by process name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by user name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_rare_user_runas_event.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_rare_user_runas_event.json index 20797827eee03..880be0045f84a 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_rare_user_runas_event.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat/ml/windows_rare_user_runas_event.json @@ -1,8 +1,8 @@ { "job_type": "anomaly_detector", - "description": "SIEM Winlogbeat: Unusual user context switches can be due to privilege escalation (beta)", + "description": "Security: Winlogbeat - Unusual user context switches can be due to privilege escalation.", "groups": [ - "siem", + "security", "winlogbeat", "authentication" ], @@ -33,19 +33,19 @@ "custom_urls": [ { "url_name": "Host Details by process name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Host Details by user name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by process name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by user name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat_auth/manifest.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat_auth/manifest.json index b5e65e9638eb2..f08f4da880118 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat_auth/manifest.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat_auth/manifest.json @@ -1,7 +1,7 @@ { "id": "siem_winlogbeat_auth", - "title": "SIEM Winlogbeat Authentication", - "description": "Detect suspicious authentication events in Winlogbeat data (beta).", + "title": "Security: Winlogbeat Authentication", + "description": "Detect suspicious authentication events in Winlogbeat data.", "type": "Winlogbeat data", "logoFile": "logo.json", "defaultIndexPattern": "winlogbeat-*", diff --git a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat_auth/ml/windows_rare_user_type10_remote_login.json b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat_auth/ml/windows_rare_user_type10_remote_login.json index ee009e465ec23..c18bb7a151f53 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat_auth/ml/windows_rare_user_type10_remote_login.json +++ b/x-pack/plugins/ml/server/models/data_recognizer/modules/siem_winlogbeat_auth/ml/windows_rare_user_type10_remote_login.json @@ -1,8 +1,8 @@ { "job_type": "anomaly_detector", - "description": "SIEM Winlogbeat Auth: Unusual RDP (remote desktop protocol) user logins can indicate account takeover or credentialed access (beta)", + "description": "Security: Winlogbeat Auth - Unusual RDP (remote desktop protocol) user logins can indicate account takeover or credentialed access.", "groups": [ - "siem", + "security", "winlogbeat", "authentication" ], @@ -33,19 +33,19 @@ "custom_urls": [ { "url_name": "Host Details by process name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Host Details by user name", - "url_value": "siem#/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts/$host.name$?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by process name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'process.name%20:%20%22$process.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" }, { "url_name": "Hosts Overview by user name", - "url_value": "siem#/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" + "url_value": "security/hosts/ml-hosts?_g=()&query=(query:'user.name%20:%20%22$user.name$%22',language:kuery)&timerange=(global:(linkTo:!(timeline),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')),timeline:(linkTo:!(global),timerange:(from:'$earliest$',kind:absolute,to:'$latest$')))" } ] } diff --git a/x-pack/plugins/ml/server/models/data_visualizer/data_visualizer.ts b/x-pack/plugins/ml/server/models/data_visualizer/data_visualizer.ts index d1a4a0b585fbb..7f19f32373e07 100644 --- a/x-pack/plugins/ml/server/models/data_visualizer/data_visualizer.ts +++ b/x-pack/plugins/ml/server/models/data_visualizer/data_visualizer.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import _ from 'lodash'; import { KBN_FIELD_TYPES } from '../../../../../../src/plugins/data/server'; import { ML_JOB_FIELD_TYPES } from '../../../common/constants/field_types'; @@ -180,7 +180,7 @@ type BatchStats = | FieldExamples; const getAggIntervals = async ( - callAsCurrentUser: LegacyAPICaller, + { callAsCurrentUser }: ILegacyScopedClusterClient, indexPatternTitle: string, query: any, fields: HistogramField[], @@ -238,14 +238,15 @@ const getAggIntervals = async ( // export for re-use by transforms plugin export const getHistogramsForFields = async ( - callAsCurrentUser: LegacyAPICaller, + mlClusterClient: ILegacyScopedClusterClient, indexPatternTitle: string, query: any, fields: HistogramField[], samplerShardSize: number ) => { + const { callAsCurrentUser } = mlClusterClient; const aggIntervals = await getAggIntervals( - callAsCurrentUser, + mlClusterClient, indexPatternTitle, query, fields, @@ -348,10 +349,12 @@ export const getHistogramsForFields = async ( }; export class DataVisualizer { - callAsCurrentUser: LegacyAPICaller; + private _mlClusterClient: ILegacyScopedClusterClient; + private _callAsCurrentUser: ILegacyScopedClusterClient['callAsCurrentUser']; - constructor(callAsCurrentUser: LegacyAPICaller) { - this.callAsCurrentUser = callAsCurrentUser; + constructor(mlClusterClient: ILegacyScopedClusterClient) { + this._callAsCurrentUser = mlClusterClient.callAsCurrentUser; + this._mlClusterClient = mlClusterClient; } // Obtains overall stats on the fields in the supplied index pattern, returning an object @@ -447,7 +450,7 @@ export class DataVisualizer { samplerShardSize: number ): Promise { return await getHistogramsForFields( - this.callAsCurrentUser, + this._mlClusterClient, indexPatternTitle, query, fields, @@ -626,7 +629,7 @@ export class DataVisualizer { aggs: buildSamplerAggregation(aggs, samplerShardSize), }; - const resp = await this.callAsCurrentUser('search', { + const resp = await this._callAsCurrentUser('search', { index, rest_total_hits_as_int: true, size, @@ -693,7 +696,7 @@ export class DataVisualizer { }; filterCriteria.push({ exists: { field } }); - const resp = await this.callAsCurrentUser('search', { + const resp = await this._callAsCurrentUser('search', { index, rest_total_hits_as_int: true, size, @@ -735,7 +738,7 @@ export class DataVisualizer { aggs, }; - const resp = await this.callAsCurrentUser('search', { + const resp = await this._callAsCurrentUser('search', { index, size, body, @@ -838,7 +841,7 @@ export class DataVisualizer { aggs: buildSamplerAggregation(aggs, samplerShardSize), }; - const resp = await this.callAsCurrentUser('search', { + const resp = await this._callAsCurrentUser('search', { index, size, body, @@ -959,7 +962,7 @@ export class DataVisualizer { aggs: buildSamplerAggregation(aggs, samplerShardSize), }; - const resp = await this.callAsCurrentUser('search', { + const resp = await this._callAsCurrentUser('search', { index, size, body, @@ -1033,7 +1036,7 @@ export class DataVisualizer { aggs: buildSamplerAggregation(aggs, samplerShardSize), }; - const resp = await this.callAsCurrentUser('search', { + const resp = await this._callAsCurrentUser('search', { index, size, body, @@ -1100,7 +1103,7 @@ export class DataVisualizer { aggs: buildSamplerAggregation(aggs, samplerShardSize), }; - const resp = await this.callAsCurrentUser('search', { + const resp = await this._callAsCurrentUser('search', { index, size, body, @@ -1162,7 +1165,7 @@ export class DataVisualizer { }, }; - const resp = await this.callAsCurrentUser('search', { + const resp = await this._callAsCurrentUser('search', { index, rest_total_hits_as_int: true, size, diff --git a/x-pack/plugins/ml/server/models/fields_service/fields_service.ts b/x-pack/plugins/ml/server/models/fields_service/fields_service.ts index 661ea6c6fec24..43a6876f76c49 100644 --- a/x-pack/plugins/ml/server/models/fields_service/fields_service.ts +++ b/x-pack/plugins/ml/server/models/fields_service/fields_service.ts @@ -5,7 +5,7 @@ */ import Boom from 'boom'; -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { duration } from 'moment'; import { parseInterval } from '../../../common/util/parse_interval'; import { initCardinalityFieldsCache } from './fields_aggs_cache'; @@ -14,7 +14,7 @@ import { initCardinalityFieldsCache } from './fields_aggs_cache'; * Service for carrying out queries to obtain data * specific to fields in Elasticsearch indices. */ -export function fieldsServiceProvider(callAsCurrentUser: LegacyAPICaller) { +export function fieldsServiceProvider({ callAsCurrentUser }: ILegacyScopedClusterClient) { const fieldsAggsCache = initCardinalityFieldsCache(); /** diff --git a/x-pack/plugins/ml/server/models/file_data_visualizer/file_data_visualizer.ts b/x-pack/plugins/ml/server/models/file_data_visualizer/file_data_visualizer.ts index 978355d098b13..9cd71c046b66c 100644 --- a/x-pack/plugins/ml/server/models/file_data_visualizer/file_data_visualizer.ts +++ b/x-pack/plugins/ml/server/models/file_data_visualizer/file_data_visualizer.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { AnalysisResult, FormattedOverrides, @@ -13,9 +13,9 @@ import { export type InputData = any[]; -export function fileDataVisualizerProvider(callAsCurrentUser: LegacyAPICaller) { +export function fileDataVisualizerProvider({ callAsInternalUser }: ILegacyScopedClusterClient) { async function analyzeFile(data: any, overrides: any): Promise { - const results = await callAsCurrentUser('ml.fileStructure', { + const results = await callAsInternalUser('ml.fileStructure', { body: data, ...overrides, }); diff --git a/x-pack/plugins/ml/server/models/file_data_visualizer/import_data.ts b/x-pack/plugins/ml/server/models/file_data_visualizer/import_data.ts index e082a7462241a..fc9b333298c9d 100644 --- a/x-pack/plugins/ml/server/models/file_data_visualizer/import_data.ts +++ b/x-pack/plugins/ml/server/models/file_data_visualizer/import_data.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { INDEX_META_DATA_CREATED_BY } from '../../../common/constants/file_datavisualizer'; import { ImportResponse, @@ -15,7 +15,7 @@ import { } from '../../../common/types/file_datavisualizer'; import { InputData } from './file_data_visualizer'; -export function importDataProvider(callAsCurrentUser: LegacyAPICaller) { +export function importDataProvider({ callAsCurrentUser }: ILegacyScopedClusterClient) { async function importData( id: string, index: string, diff --git a/x-pack/plugins/ml/server/models/filter/filter_manager.ts b/x-pack/plugins/ml/server/models/filter/filter_manager.ts index 40a20030cb635..20dc95e92a86c 100644 --- a/x-pack/plugins/ml/server/models/filter/filter_manager.ts +++ b/x-pack/plugins/ml/server/models/filter/filter_manager.ts @@ -5,7 +5,7 @@ */ import Boom from 'boom'; -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { DetectorRule, DetectorRuleScope } from '../../../common/types/detector_rules'; @@ -58,14 +58,17 @@ interface PartialJob { } export class FilterManager { - constructor(private callAsCurrentUser: LegacyAPICaller) {} + private _callAsInternalUser: ILegacyScopedClusterClient['callAsInternalUser']; + constructor({ callAsInternalUser }: ILegacyScopedClusterClient) { + this._callAsInternalUser = callAsInternalUser; + } async getFilter(filterId: string) { try { const [JOBS, FILTERS] = [0, 1]; const results = await Promise.all([ - this.callAsCurrentUser('ml.jobs'), - this.callAsCurrentUser('ml.filters', { filterId }), + this._callAsInternalUser('ml.jobs'), + this._callAsInternalUser('ml.filters', { filterId }), ]); if (results[FILTERS] && results[FILTERS].filters.length) { @@ -87,7 +90,7 @@ export class FilterManager { async getAllFilters() { try { - const filtersResp = await this.callAsCurrentUser('ml.filters'); + const filtersResp = await this._callAsInternalUser('ml.filters'); return filtersResp.filters; } catch (error) { throw Boom.badRequest(error); @@ -98,8 +101,8 @@ export class FilterManager { try { const [JOBS, FILTERS] = [0, 1]; const results = await Promise.all([ - this.callAsCurrentUser('ml.jobs'), - this.callAsCurrentUser('ml.filters'), + this._callAsInternalUser('ml.jobs'), + this._callAsInternalUser('ml.filters'), ]); // Build a map of filter_ids against jobs and detectors using that filter. @@ -137,7 +140,7 @@ export class FilterManager { delete filter.filterId; try { // Returns the newly created filter. - return await this.callAsCurrentUser('ml.addFilter', { filterId, body: filter }); + return await this._callAsInternalUser('ml.addFilter', { filterId, body: filter }); } catch (error) { throw Boom.badRequest(error); } @@ -157,7 +160,7 @@ export class FilterManager { } // Returns the newly updated filter. - return await this.callAsCurrentUser('ml.updateFilter', { + return await this._callAsInternalUser('ml.updateFilter', { filterId, body, }); @@ -167,7 +170,7 @@ export class FilterManager { } async deleteFilter(filterId: string) { - return this.callAsCurrentUser('ml.deleteFilter', { filterId }); + return this._callAsInternalUser('ml.deleteFilter', { filterId }); } buildFiltersInUse(jobsList: PartialJob[]) { diff --git a/x-pack/plugins/ml/server/models/job_audit_messages/job_audit_messages.d.ts b/x-pack/plugins/ml/server/models/job_audit_messages/job_audit_messages.d.ts index f11771a88c5c6..d72552b548b82 100644 --- a/x-pack/plugins/ml/server/models/job_audit_messages/job_audit_messages.d.ts +++ b/x-pack/plugins/ml/server/models/job_audit_messages/job_audit_messages.d.ts @@ -4,10 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; export function jobAuditMessagesProvider( - callAsCurrentUser: LegacyAPICaller + mlClusterClient: ILegacyScopedClusterClient ): { getJobAuditMessages: (jobId?: string, from?: string) => any; getAuditMessagesSummary: (jobIds?: string[]) => any; diff --git a/x-pack/plugins/ml/server/models/job_audit_messages/job_audit_messages.js b/x-pack/plugins/ml/server/models/job_audit_messages/job_audit_messages.js index 6b782f8652363..dcbabd879b47a 100644 --- a/x-pack/plugins/ml/server/models/job_audit_messages/job_audit_messages.js +++ b/x-pack/plugins/ml/server/models/job_audit_messages/job_audit_messages.js @@ -34,14 +34,14 @@ const anomalyDetectorTypeFilter = { }, }; -export function jobAuditMessagesProvider(callAsCurrentUser) { +export function jobAuditMessagesProvider({ callAsCurrentUser, callAsInternalUser }) { // search for audit messages, // jobId is optional. without it, all jobs will be listed. // from is optional and should be a string formatted in ES time units. e.g. 12h, 1d, 7d async function getJobAuditMessages(jobId, from) { let gte = null; if (jobId !== undefined && from === undefined) { - const jobs = await callAsCurrentUser('ml.jobs', { jobId }); + const jobs = await callAsInternalUser('ml.jobs', { jobId }); if (jobs.count > 0 && jobs.jobs !== undefined) { gte = moment(jobs.jobs[0].create_time).valueOf(); } diff --git a/x-pack/plugins/ml/server/models/job_service/datafeeds.ts b/x-pack/plugins/ml/server/models/job_service/datafeeds.ts index 0f64f5e0e7b4f..98e1be48bb766 100644 --- a/x-pack/plugins/ml/server/models/job_service/datafeeds.ts +++ b/x-pack/plugins/ml/server/models/job_service/datafeeds.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { i18n } from '@kbn/i18n'; import { JOB_STATE, DATAFEED_STATE } from '../../../common/constants/states'; import { fillResultsWithTimeouts, isRequestTimeout } from './error_utils'; @@ -26,7 +26,7 @@ interface Results { }; } -export function datafeedsProvider(callAsCurrentUser: LegacyAPICaller) { +export function datafeedsProvider({ callAsInternalUser }: ILegacyScopedClusterClient) { async function forceStartDatafeeds(datafeedIds: string[], start?: number, end?: number) { const jobIds = await getJobIdsByDatafeedId(); const doStartsCalled = datafeedIds.reduce((acc, cur) => { @@ -84,7 +84,7 @@ export function datafeedsProvider(callAsCurrentUser: LegacyAPICaller) { async function openJob(jobId: string) { let opened = false; try { - const resp = await callAsCurrentUser('ml.openJob', { jobId }); + const resp = await callAsInternalUser('ml.openJob', { jobId }); opened = resp.opened; } catch (error) { if (error.statusCode === 409) { @@ -97,7 +97,7 @@ export function datafeedsProvider(callAsCurrentUser: LegacyAPICaller) { } async function startDatafeed(datafeedId: string, start?: number, end?: number) { - return callAsCurrentUser('ml.startDatafeed', { datafeedId, start, end }); + return callAsInternalUser('ml.startDatafeed', { datafeedId, start, end }); } async function stopDatafeeds(datafeedIds: string[]) { @@ -105,7 +105,7 @@ export function datafeedsProvider(callAsCurrentUser: LegacyAPICaller) { for (const datafeedId of datafeedIds) { try { - results[datafeedId] = await callAsCurrentUser('ml.stopDatafeed', { datafeedId }); + results[datafeedId] = await callAsInternalUser('ml.stopDatafeed', { datafeedId }); } catch (error) { if (isRequestTimeout(error)) { return fillResultsWithTimeouts(results, datafeedId, datafeedIds, DATAFEED_STATE.STOPPED); @@ -117,11 +117,11 @@ export function datafeedsProvider(callAsCurrentUser: LegacyAPICaller) { } async function forceDeleteDatafeed(datafeedId: string) { - return callAsCurrentUser('ml.deleteDatafeed', { datafeedId, force: true }); + return callAsInternalUser('ml.deleteDatafeed', { datafeedId, force: true }); } async function getDatafeedIdsByJobId() { - const { datafeeds } = await callAsCurrentUser('ml.datafeeds'); + const { datafeeds } = (await callAsInternalUser('ml.datafeeds')) as MlDatafeedsResponse; return datafeeds.reduce((acc, cur) => { acc[cur.job_id] = cur.datafeed_id; return acc; @@ -129,7 +129,7 @@ export function datafeedsProvider(callAsCurrentUser: LegacyAPICaller) { } async function getJobIdsByDatafeedId() { - const { datafeeds } = await callAsCurrentUser('ml.datafeeds'); + const { datafeeds } = (await callAsInternalUser('ml.datafeeds')) as MlDatafeedsResponse; return datafeeds.reduce((acc, cur) => { acc[cur.datafeed_id] = cur.job_id; return acc; diff --git a/x-pack/plugins/ml/server/models/job_service/groups.ts b/x-pack/plugins/ml/server/models/job_service/groups.ts index ab5707ab29e65..c4ea854c14f87 100644 --- a/x-pack/plugins/ml/server/models/job_service/groups.ts +++ b/x-pack/plugins/ml/server/models/job_service/groups.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { CalendarManager } from '../calendar'; import { GLOBAL_CALENDAR } from '../../../common/constants/calendars'; import { Job } from '../../../common/types/anomaly_detection_jobs'; @@ -23,14 +23,15 @@ interface Results { }; } -export function groupsProvider(callAsCurrentUser: LegacyAPICaller) { - const calMngr = new CalendarManager(callAsCurrentUser); +export function groupsProvider(mlClusterClient: ILegacyScopedClusterClient) { + const calMngr = new CalendarManager(mlClusterClient); + const { callAsInternalUser } = mlClusterClient; async function getAllGroups() { const groups: { [id: string]: Group } = {}; const jobIds: { [id: string]: undefined | null } = {}; const [{ jobs }, calendars] = await Promise.all([ - callAsCurrentUser('ml.jobs'), + callAsInternalUser('ml.jobs') as Promise, calMngr.getAllCalendars(), ]); @@ -79,7 +80,7 @@ export function groupsProvider(callAsCurrentUser: LegacyAPICaller) { for (const job of jobs) { const { job_id: jobId, groups } = job; try { - await callAsCurrentUser('ml.updateJob', { jobId, body: { groups } }); + await callAsInternalUser('ml.updateJob', { jobId, body: { groups } }); results[jobId] = { success: true }; } catch (error) { results[jobId] = { success: false, error }; diff --git a/x-pack/plugins/ml/server/models/job_service/index.ts b/x-pack/plugins/ml/server/models/job_service/index.ts index 5d053c1be73e4..1ff33a7b00f0b 100644 --- a/x-pack/plugins/ml/server/models/job_service/index.ts +++ b/x-pack/plugins/ml/server/models/job_service/index.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { datafeedsProvider } from './datafeeds'; import { jobsProvider } from './jobs'; import { groupsProvider } from './groups'; @@ -12,14 +12,14 @@ import { newJobCapsProvider } from './new_job_caps'; import { newJobChartsProvider, topCategoriesProvider } from './new_job'; import { modelSnapshotProvider } from './model_snapshots'; -export function jobServiceProvider(callAsCurrentUser: LegacyAPICaller) { +export function jobServiceProvider(mlClusterClient: ILegacyScopedClusterClient) { return { - ...datafeedsProvider(callAsCurrentUser), - ...jobsProvider(callAsCurrentUser), - ...groupsProvider(callAsCurrentUser), - ...newJobCapsProvider(callAsCurrentUser), - ...newJobChartsProvider(callAsCurrentUser), - ...topCategoriesProvider(callAsCurrentUser), - ...modelSnapshotProvider(callAsCurrentUser), + ...datafeedsProvider(mlClusterClient), + ...jobsProvider(mlClusterClient), + ...groupsProvider(mlClusterClient), + ...newJobCapsProvider(mlClusterClient), + ...newJobChartsProvider(mlClusterClient), + ...topCategoriesProvider(mlClusterClient), + ...modelSnapshotProvider(mlClusterClient), }; } diff --git a/x-pack/plugins/ml/server/models/job_service/jobs.ts b/x-pack/plugins/ml/server/models/job_service/jobs.ts index 2d26b2150edf3..aca0c5d72a9f5 100644 --- a/x-pack/plugins/ml/server/models/job_service/jobs.ts +++ b/x-pack/plugins/ml/server/models/job_service/jobs.ts @@ -7,7 +7,7 @@ import { i18n } from '@kbn/i18n'; import { uniq } from 'lodash'; import Boom from 'boom'; -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { JOB_STATE, DATAFEED_STATE } from '../../../common/constants/states'; import { MlSummaryJob, @@ -46,14 +46,16 @@ interface Results { }; } -export function jobsProvider(callAsCurrentUser: LegacyAPICaller) { - const { forceDeleteDatafeed, getDatafeedIdsByJobId } = datafeedsProvider(callAsCurrentUser); - const { getAuditMessagesSummary } = jobAuditMessagesProvider(callAsCurrentUser); - const { getLatestBucketTimestampByJob } = resultsServiceProvider(callAsCurrentUser); - const calMngr = new CalendarManager(callAsCurrentUser); +export function jobsProvider(mlClusterClient: ILegacyScopedClusterClient) { + const { callAsCurrentUser, callAsInternalUser } = mlClusterClient; + + const { forceDeleteDatafeed, getDatafeedIdsByJobId } = datafeedsProvider(mlClusterClient); + const { getAuditMessagesSummary } = jobAuditMessagesProvider(mlClusterClient); + const { getLatestBucketTimestampByJob } = resultsServiceProvider(mlClusterClient); + const calMngr = new CalendarManager(mlClusterClient); async function forceDeleteJob(jobId: string) { - return callAsCurrentUser('ml.deleteJob', { jobId, force: true }); + return callAsInternalUser('ml.deleteJob', { jobId, force: true }); } async function deleteJobs(jobIds: string[]) { @@ -97,7 +99,7 @@ export function jobsProvider(callAsCurrentUser: LegacyAPICaller) { const results: Results = {}; for (const jobId of jobIds) { try { - await callAsCurrentUser('ml.closeJob', { jobId }); + await callAsInternalUser('ml.closeJob', { jobId }); results[jobId] = { closed: true }; } catch (error) { if (isRequestTimeout(error)) { @@ -113,7 +115,7 @@ export function jobsProvider(callAsCurrentUser: LegacyAPICaller) { // if the job has failed we want to attempt a force close. // however, if we received a 409 due to the datafeed being started we should not attempt a force close. try { - await callAsCurrentUser('ml.closeJob', { jobId, force: true }); + await callAsInternalUser('ml.closeJob', { jobId, force: true }); results[jobId] = { closed: true }; } catch (error2) { if (isRequestTimeout(error)) { @@ -136,12 +138,12 @@ export function jobsProvider(callAsCurrentUser: LegacyAPICaller) { throw Boom.notFound(`Cannot find datafeed for job ${jobId}`); } - const dfResult = await callAsCurrentUser('ml.stopDatafeed', { datafeedId, force: true }); + const dfResult = await callAsInternalUser('ml.stopDatafeed', { datafeedId, force: true }); if (!dfResult || dfResult.stopped !== true) { return { success: false }; } - await callAsCurrentUser('ml.closeJob', { jobId, force: true }); + await callAsInternalUser('ml.closeJob', { jobId, force: true }); return { success: true }; } @@ -257,13 +259,13 @@ export function jobsProvider(callAsCurrentUser: LegacyAPICaller) { Promise<{ [id: string]: number | undefined }> ] = [ jobIds.length > 0 - ? callAsCurrentUser('ml.jobs', { jobId: jobIds }) // move length check in side call - : callAsCurrentUser('ml.jobs'), + ? (callAsInternalUser('ml.jobs', { jobId: jobIds }) as Promise) // move length check in side call + : (callAsInternalUser('ml.jobs') as Promise), jobIds.length > 0 - ? callAsCurrentUser('ml.jobStats', { jobId: jobIds }) - : callAsCurrentUser('ml.jobStats'), - callAsCurrentUser('ml.datafeeds'), - callAsCurrentUser('ml.datafeedStats'), + ? (callAsInternalUser('ml.jobStats', { jobId: jobIds }) as Promise) + : (callAsInternalUser('ml.jobStats') as Promise), + callAsInternalUser('ml.datafeeds') as Promise, + callAsInternalUser('ml.datafeedStats') as Promise, calMngr.getAllCalendars(), getLatestBucketTimestampByJob(), ]; @@ -402,7 +404,7 @@ export function jobsProvider(callAsCurrentUser: LegacyAPICaller) { } catch (e) { // if the user doesn't have permission to load the task list, // use the jobs list to get the ids of deleting jobs - const { jobs } = await callAsCurrentUser('ml.jobs'); + const { jobs } = (await callAsInternalUser('ml.jobs')) as MlJobsResponse; jobIds.push(...jobs.filter((j) => j.deleting === true).map((j) => j.job_id)); } return { jobIds }; @@ -413,9 +415,9 @@ export function jobsProvider(callAsCurrentUser: LegacyAPICaller) { // e.g. *_low_request_rate_ecs async function jobsExist(jobIds: string[] = []) { // Get the list of job IDs. - const jobsInfo = await callAsCurrentUser('ml.jobs', { + const jobsInfo = (await callAsInternalUser('ml.jobs', { jobId: jobIds, - }); + })) as MlJobsResponse; const results: { [id: string]: boolean } = {}; if (jobsInfo.count > 0) { @@ -438,8 +440,8 @@ export function jobsProvider(callAsCurrentUser: LegacyAPICaller) { } async function getAllJobAndGroupIds() { - const { getAllGroups } = groupsProvider(callAsCurrentUser); - const jobs = await callAsCurrentUser('ml.jobs'); + const { getAllGroups } = groupsProvider(mlClusterClient); + const jobs = (await callAsInternalUser('ml.jobs')) as MlJobsResponse; const jobIds = jobs.jobs.map((job) => job.job_id); const groups = await getAllGroups(); const groupIds = groups.map((group) => group.id); @@ -453,7 +455,7 @@ export function jobsProvider(callAsCurrentUser: LegacyAPICaller) { async function getLookBackProgress(jobId: string, start: number, end: number) { const datafeedId = `datafeed-${jobId}`; const [jobStats, isRunning] = await Promise.all([ - callAsCurrentUser('ml.jobStats', { jobId: [jobId] }), + callAsInternalUser('ml.jobStats', { jobId: [jobId] }) as Promise, isDatafeedRunning(datafeedId), ]); @@ -472,9 +474,9 @@ export function jobsProvider(callAsCurrentUser: LegacyAPICaller) { } async function isDatafeedRunning(datafeedId: string) { - const stats = await callAsCurrentUser('ml.datafeedStats', { + const stats = (await callAsInternalUser('ml.datafeedStats', { datafeedId: [datafeedId], - }); + })) as MlDatafeedsStatsResponse; if (stats.datafeeds.length) { const state = stats.datafeeds[0].state; return ( diff --git a/x-pack/plugins/ml/server/models/job_service/model_snapshots.ts b/x-pack/plugins/ml/server/models/job_service/model_snapshots.ts index 136d4f47c7fac..576d6f8cbb160 100644 --- a/x-pack/plugins/ml/server/models/job_service/model_snapshots.ts +++ b/x-pack/plugins/ml/server/models/job_service/model_snapshots.ts @@ -6,10 +6,9 @@ import Boom from 'boom'; import { i18n } from '@kbn/i18n'; -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { ModelSnapshot } from '../../../common/types/anomaly_detection_jobs'; -import { datafeedsProvider, MlDatafeedsResponse } from './datafeeds'; -import { MlJobsResponse } from './jobs'; +import { datafeedsProvider } from './datafeeds'; import { FormCalendar, CalendarManager } from '../calendar'; export interface ModelSnapshotsResponse { @@ -20,8 +19,9 @@ export interface RevertModelSnapshotResponse { model: ModelSnapshot; } -export function modelSnapshotProvider(callAsCurrentUser: LegacyAPICaller) { - const { forceStartDatafeeds, getDatafeedIdsByJobId } = datafeedsProvider(callAsCurrentUser); +export function modelSnapshotProvider(mlClusterClient: ILegacyScopedClusterClient) { + const { callAsInternalUser } = mlClusterClient; + const { forceStartDatafeeds, getDatafeedIdsByJobId } = datafeedsProvider(mlClusterClient); async function revertModelSnapshot( jobId: string, @@ -33,12 +33,12 @@ export function modelSnapshotProvider(callAsCurrentUser: LegacyAPICaller) { ) { let datafeedId = `datafeed-${jobId}`; // ensure job exists - await callAsCurrentUser('ml.jobs', { jobId: [jobId] }); + await callAsInternalUser('ml.jobs', { jobId: [jobId] }); try { // ensure the datafeed exists // the datafeed is probably called datafeed- - await callAsCurrentUser('ml.datafeeds', { + await callAsInternalUser('ml.datafeeds', { datafeedId: [datafeedId], }); } catch (e) { @@ -52,22 +52,19 @@ export function modelSnapshotProvider(callAsCurrentUser: LegacyAPICaller) { } // ensure the snapshot exists - const snapshot = await callAsCurrentUser('ml.modelSnapshots', { + const snapshot = (await callAsInternalUser('ml.modelSnapshots', { jobId, snapshotId, - }); + })) as ModelSnapshotsResponse; // apply the snapshot revert - const { model } = await callAsCurrentUser( - 'ml.revertModelSnapshot', - { - jobId, - snapshotId, - body: { - delete_intervening_results: deleteInterveningResults, - }, - } - ); + const { model } = (await callAsInternalUser('ml.revertModelSnapshot', { + jobId, + snapshotId, + body: { + delete_intervening_results: deleteInterveningResults, + }, + })) as RevertModelSnapshotResponse; // create calendar (if specified) and replay datafeed if (replay && model.snapshot_id === snapshotId && snapshot.model_snapshots.length) { @@ -88,7 +85,7 @@ export function modelSnapshotProvider(callAsCurrentUser: LegacyAPICaller) { end_time: s.end, })), }; - const cm = new CalendarManager(callAsCurrentUser); + const cm = new CalendarManager(mlClusterClient); await cm.newCalendar(calendar); } diff --git a/x-pack/plugins/ml/server/models/job_service/new_job/categorization/examples.ts b/x-pack/plugins/ml/server/models/job_service/new_job/categorization/examples.ts index bf0d79b3ec072..ca3e0cef21049 100644 --- a/x-pack/plugins/ml/server/models/job_service/new_job/categorization/examples.ts +++ b/x-pack/plugins/ml/server/models/job_service/new_job/categorization/examples.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ +import { ILegacyScopedClusterClient } from 'kibana/server'; import { chunk } from 'lodash'; import { SearchResponse } from 'elasticsearch'; import { CATEGORY_EXAMPLES_SAMPLE_SIZE } from '../../../../../common/constants/categorization_job'; @@ -12,15 +13,14 @@ import { CategorizationAnalyzer, CategoryFieldExample, } from '../../../../../common/types/categories'; -import { callWithRequestType } from '../../../../../common/types/kibana'; import { ValidationResults } from './validation_results'; const CHUNK_SIZE = 100; -export function categorizationExamplesProvider( - callWithRequest: callWithRequestType, - callWithInternalUser: callWithRequestType -) { +export function categorizationExamplesProvider({ + callAsCurrentUser, + callAsInternalUser, +}: ILegacyScopedClusterClient) { const validationResults = new ValidationResults(); async function categorizationExamples( @@ -57,7 +57,7 @@ export function categorizationExamplesProvider( } } - const results: SearchResponse<{ [id: string]: string }> = await callWithRequest('search', { + const results: SearchResponse<{ [id: string]: string }> = await callAsCurrentUser('search', { index: indexPatternTitle, size, body: { @@ -112,7 +112,7 @@ export function categorizationExamplesProvider( } async function loadTokens(examples: string[], analyzer: CategorizationAnalyzer) { - const { tokens }: { tokens: Token[] } = await callWithInternalUser('indices.analyze', { + const { tokens }: { tokens: Token[] } = await callAsInternalUser('indices.analyze', { body: { ...getAnalyzer(analyzer), text: examples, diff --git a/x-pack/plugins/ml/server/models/job_service/new_job/categorization/top_categories.ts b/x-pack/plugins/ml/server/models/job_service/new_job/categorization/top_categories.ts index 13c5f107972eb..4f97238a4a0b5 100644 --- a/x-pack/plugins/ml/server/models/job_service/new_job/categorization/top_categories.ts +++ b/x-pack/plugins/ml/server/models/job_service/new_job/categorization/top_categories.ts @@ -5,13 +5,13 @@ */ import { SearchResponse } from 'elasticsearch'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { ML_RESULTS_INDEX_PATTERN } from '../../../../../common/constants/index_patterns'; import { CategoryId, Category } from '../../../../../common/types/categories'; -import { callWithRequestType } from '../../../../../common/types/kibana'; -export function topCategoriesProvider(callWithRequest: callWithRequestType) { +export function topCategoriesProvider({ callAsCurrentUser }: ILegacyScopedClusterClient) { async function getTotalCategories(jobId: string): Promise<{ total: number }> { - const totalResp = await callWithRequest('search', { + const totalResp = await callAsCurrentUser('search', { index: ML_RESULTS_INDEX_PATTERN, size: 0, body: { @@ -37,7 +37,7 @@ export function topCategoriesProvider(callWithRequest: callWithRequestType) { } async function getTopCategoryCounts(jobId: string, numberOfCategories: number) { - const top: SearchResponse = await callWithRequest('search', { + const top: SearchResponse = await callAsCurrentUser('search', { index: ML_RESULTS_INDEX_PATTERN, size: 0, body: { @@ -99,7 +99,7 @@ export function topCategoriesProvider(callWithRequest: callWithRequestType) { field: 'category_id', }, }; - const result: SearchResponse = await callWithRequest('search', { + const result: SearchResponse = await callAsCurrentUser('search', { index: ML_RESULTS_INDEX_PATTERN, size, body: { diff --git a/x-pack/plugins/ml/server/models/job_service/new_job/charts.ts b/x-pack/plugins/ml/server/models/job_service/new_job/charts.ts index 88ae8caa91e4a..63ae2c624ac38 100644 --- a/x-pack/plugins/ml/server/models/job_service/new_job/charts.ts +++ b/x-pack/plugins/ml/server/models/job_service/new_job/charts.ts @@ -4,13 +4,13 @@ * you may not use this file except in compliance with the Elastic License. */ +import { ILegacyScopedClusterClient } from 'kibana/server'; import { newJobLineChartProvider } from './line_chart'; import { newJobPopulationChartProvider } from './population_chart'; -import { callWithRequestType } from '../../../../common/types/kibana'; -export function newJobChartsProvider(callWithRequest: callWithRequestType) { - const { newJobLineChart } = newJobLineChartProvider(callWithRequest); - const { newJobPopulationChart } = newJobPopulationChartProvider(callWithRequest); +export function newJobChartsProvider(mlClusterClient: ILegacyScopedClusterClient) { + const { newJobLineChart } = newJobLineChartProvider(mlClusterClient); + const { newJobPopulationChart } = newJobPopulationChartProvider(mlClusterClient); return { newJobLineChart, diff --git a/x-pack/plugins/ml/server/models/job_service/new_job/line_chart.ts b/x-pack/plugins/ml/server/models/job_service/new_job/line_chart.ts index 4872f0f5e0ea4..3080b37867de5 100644 --- a/x-pack/plugins/ml/server/models/job_service/new_job/line_chart.ts +++ b/x-pack/plugins/ml/server/models/job_service/new_job/line_chart.ts @@ -5,8 +5,8 @@ */ import { get } from 'lodash'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { AggFieldNamePair, EVENT_RATE_FIELD_ID } from '../../../../common/types/fields'; -import { callWithRequestType } from '../../../../common/types/kibana'; import { ML_MEDIAN_PERCENTS } from '../../../../common/util/job_utils'; type DtrIndex = number; @@ -23,7 +23,7 @@ interface ProcessedResults { totalResults: number; } -export function newJobLineChartProvider(callWithRequest: callWithRequestType) { +export function newJobLineChartProvider({ callAsCurrentUser }: ILegacyScopedClusterClient) { async function newJobLineChart( indexPatternTitle: string, timeField: string, @@ -47,7 +47,7 @@ export function newJobLineChartProvider(callWithRequest: callWithRequestType) { splitFieldValue ); - const results = await callWithRequest('search', json); + const results = await callAsCurrentUser('search', json); return processSearchResults( results, aggFieldNamePairs.map((af) => af.field) diff --git a/x-pack/plugins/ml/server/models/job_service/new_job/population_chart.ts b/x-pack/plugins/ml/server/models/job_service/new_job/population_chart.ts index 26609bdcc8f7d..a9a2ce57f966c 100644 --- a/x-pack/plugins/ml/server/models/job_service/new_job/population_chart.ts +++ b/x-pack/plugins/ml/server/models/job_service/new_job/population_chart.ts @@ -5,8 +5,8 @@ */ import { get } from 'lodash'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { AggFieldNamePair, EVENT_RATE_FIELD_ID } from '../../../../common/types/fields'; -import { callWithRequestType } from '../../../../common/types/kibana'; import { ML_MEDIAN_PERCENTS } from '../../../../common/util/job_utils'; const OVER_FIELD_EXAMPLES_COUNT = 40; @@ -29,7 +29,7 @@ interface ProcessedResults { totalResults: number; } -export function newJobPopulationChartProvider(callWithRequest: callWithRequestType) { +export function newJobPopulationChartProvider({ callAsCurrentUser }: ILegacyScopedClusterClient) { async function newJobPopulationChart( indexPatternTitle: string, timeField: string, @@ -52,7 +52,7 @@ export function newJobPopulationChartProvider(callWithRequest: callWithRequestTy ); try { - const results = await callWithRequest('search', json); + const results = await callAsCurrentUser('search', json); return processSearchResults( results, aggFieldNamePairs.map((af) => af.field) diff --git a/x-pack/plugins/ml/server/models/job_service/new_job_caps/field_service.ts b/x-pack/plugins/ml/server/models/job_service/new_job_caps/field_service.ts index a5ed4a18bf51c..fd20610450cc1 100644 --- a/x-pack/plugins/ml/server/models/job_service/new_job_caps/field_service.ts +++ b/x-pack/plugins/ml/server/models/job_service/new_job_caps/field_service.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ +import { ILegacyScopedClusterClient } from 'kibana/server'; import { cloneDeep } from 'lodash'; import { SavedObjectsClientContract } from 'kibana/server'; import { @@ -39,32 +40,32 @@ const supportedTypes: string[] = [ export function fieldServiceProvider( indexPattern: string, isRollup: boolean, - callWithRequest: any, + mlClusterClient: ILegacyScopedClusterClient, savedObjectsClient: SavedObjectsClientContract ) { - return new FieldsService(indexPattern, isRollup, callWithRequest, savedObjectsClient); + return new FieldsService(indexPattern, isRollup, mlClusterClient, savedObjectsClient); } class FieldsService { private _indexPattern: string; private _isRollup: boolean; - private _callWithRequest: any; + private _mlClusterClient: ILegacyScopedClusterClient; private _savedObjectsClient: SavedObjectsClientContract; constructor( indexPattern: string, isRollup: boolean, - callWithRequest: any, - savedObjectsClient: any + mlClusterClient: ILegacyScopedClusterClient, + savedObjectsClient: SavedObjectsClientContract ) { this._indexPattern = indexPattern; this._isRollup = isRollup; - this._callWithRequest = callWithRequest; + this._mlClusterClient = mlClusterClient; this._savedObjectsClient = savedObjectsClient; } private async loadFieldCaps(): Promise { - return this._callWithRequest('fieldCaps', { + return this._mlClusterClient.callAsCurrentUser('fieldCaps', { index: this._indexPattern, fields: '*', }); @@ -108,7 +109,7 @@ class FieldsService { if (this._isRollup) { const rollupService = await rollupServiceProvider( this._indexPattern, - this._callWithRequest, + this._mlClusterClient, this._savedObjectsClient ); const rollupConfigs: RollupJob[] | null = await rollupService.getRollupJobs(); diff --git a/x-pack/plugins/ml/server/models/job_service/new_job_caps/new_job_caps.test.ts b/x-pack/plugins/ml/server/models/job_service/new_job_caps/new_job_caps.test.ts index 02fef16a384d0..38d6481e02a74 100644 --- a/x-pack/plugins/ml/server/models/job_service/new_job_caps/new_job_caps.test.ts +++ b/x-pack/plugins/ml/server/models/job_service/new_job_caps/new_job_caps.test.ts @@ -16,19 +16,23 @@ import farequoteJobCapsEmpty from './__mocks__/results/farequote_job_caps_empty. import cloudwatchJobCaps from './__mocks__/results/cloudwatch_rollup_job_caps.json'; describe('job_service - job_caps', () => { - let callWithRequestNonRollupMock: jest.Mock; - let callWithRequestRollupMock: jest.Mock; + let mlClusterClientNonRollupMock: any; + let mlClusterClientRollupMock: any; let savedObjectsClientMock: any; beforeEach(() => { - callWithRequestNonRollupMock = jest.fn((action: string) => { + const callAsNonRollupMock = jest.fn((action: string) => { switch (action) { case 'fieldCaps': return farequoteFieldCaps; } }); + mlClusterClientNonRollupMock = { + callAsCurrentUser: callAsNonRollupMock, + callAsInternalUser: callAsNonRollupMock, + }; - callWithRequestRollupMock = jest.fn((action: string) => { + const callAsRollupMock = jest.fn((action: string) => { switch (action) { case 'fieldCaps': return cloudwatchFieldCaps; @@ -36,6 +40,10 @@ describe('job_service - job_caps', () => { return Promise.resolve(rollupCaps); } }); + mlClusterClientRollupMock = { + callAsCurrentUser: callAsRollupMock, + callAsInternalUser: callAsRollupMock, + }; savedObjectsClientMock = { async find() { @@ -48,7 +56,7 @@ describe('job_service - job_caps', () => { it('can get job caps for index pattern', async (done) => { const indexPattern = 'farequote-*'; const isRollup = false; - const { newJobCaps } = newJobCapsProvider(callWithRequestNonRollupMock); + const { newJobCaps } = newJobCapsProvider(mlClusterClientNonRollupMock); const response = await newJobCaps(indexPattern, isRollup, savedObjectsClientMock); expect(response).toEqual(farequoteJobCaps); done(); @@ -57,7 +65,7 @@ describe('job_service - job_caps', () => { it('can get rollup job caps for non rollup index pattern', async (done) => { const indexPattern = 'farequote-*'; const isRollup = true; - const { newJobCaps } = newJobCapsProvider(callWithRequestNonRollupMock); + const { newJobCaps } = newJobCapsProvider(mlClusterClientNonRollupMock); const response = await newJobCaps(indexPattern, isRollup, savedObjectsClientMock); expect(response).toEqual(farequoteJobCapsEmpty); done(); @@ -68,7 +76,7 @@ describe('job_service - job_caps', () => { it('can get rollup job caps for rollup index pattern', async (done) => { const indexPattern = 'cloud_roll_index'; const isRollup = true; - const { newJobCaps } = newJobCapsProvider(callWithRequestRollupMock); + const { newJobCaps } = newJobCapsProvider(mlClusterClientRollupMock); const response = await newJobCaps(indexPattern, isRollup, savedObjectsClientMock); expect(response).toEqual(cloudwatchJobCaps); done(); @@ -77,7 +85,7 @@ describe('job_service - job_caps', () => { it('can get non rollup job caps for rollup index pattern', async (done) => { const indexPattern = 'cloud_roll_index'; const isRollup = false; - const { newJobCaps } = newJobCapsProvider(callWithRequestRollupMock); + const { newJobCaps } = newJobCapsProvider(mlClusterClientRollupMock); const response = await newJobCaps(indexPattern, isRollup, savedObjectsClientMock); expect(response).not.toEqual(cloudwatchJobCaps); done(); diff --git a/x-pack/plugins/ml/server/models/job_service/new_job_caps/new_job_caps.ts b/x-pack/plugins/ml/server/models/job_service/new_job_caps/new_job_caps.ts index a0ab4b5cf4e3e..5616dade53a78 100644 --- a/x-pack/plugins/ml/server/models/job_service/new_job_caps/new_job_caps.ts +++ b/x-pack/plugins/ml/server/models/job_service/new_job_caps/new_job_caps.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { SavedObjectsClientContract } from 'kibana/server'; +import { ILegacyScopedClusterClient, SavedObjectsClientContract } from 'kibana/server'; import { Aggregation, Field, NewJobCaps } from '../../../../common/types/fields'; import { fieldServiceProvider } from './field_service'; @@ -12,7 +12,7 @@ interface NewJobCapsResponse { [indexPattern: string]: NewJobCaps; } -export function newJobCapsProvider(callWithRequest: any) { +export function newJobCapsProvider(mlClusterClient: ILegacyScopedClusterClient) { async function newJobCaps( indexPattern: string, isRollup: boolean = false, @@ -21,7 +21,7 @@ export function newJobCapsProvider(callWithRequest: any) { const fieldService = fieldServiceProvider( indexPattern, isRollup, - callWithRequest, + mlClusterClient, savedObjectsClient ); const { aggs, fields } = await fieldService.getData(); diff --git a/x-pack/plugins/ml/server/models/job_service/new_job_caps/rollup.ts b/x-pack/plugins/ml/server/models/job_service/new_job_caps/rollup.ts index f7d846839503d..f3a9bd49c27d6 100644 --- a/x-pack/plugins/ml/server/models/job_service/new_job_caps/rollup.ts +++ b/x-pack/plugins/ml/server/models/job_service/new_job_caps/rollup.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ +import { ILegacyScopedClusterClient } from 'kibana/server'; import { SavedObject } from 'kibana/server'; import { IndexPatternAttributes } from 'src/plugins/data/server'; import { SavedObjectsClientContract } from 'kibana/server'; @@ -21,7 +22,7 @@ export interface RollupJob { export async function rollupServiceProvider( indexPattern: string, - callWithRequest: any, + { callAsCurrentUser }: ILegacyScopedClusterClient, savedObjectsClient: SavedObjectsClientContract ) { const rollupIndexPatternObject = await loadRollupIndexPattern(indexPattern, savedObjectsClient); @@ -31,7 +32,7 @@ export async function rollupServiceProvider( if (rollupIndexPatternObject !== null) { const parsedTypeMetaData = JSON.parse(rollupIndexPatternObject.attributes.typeMeta); const rollUpIndex: string = parsedTypeMetaData.params.rollup_index; - const rollupCaps = await callWithRequest('ml.rollupIndexCapabilities', { + const rollupCaps = await callAsCurrentUser('ml.rollupIndexCapabilities', { indexPattern: rollUpIndex, }); 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 8deaae823e8b3..1c74953e4dda9 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 @@ -4,28 +4,48 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { validateJob, ValidateJobPayload } from './job_validation'; import { JobValidationMessage } from '../../../common/constants/messages'; -// mock callWithRequest -const callWithRequest: LegacyAPICaller = (method: string) => { - return new Promise((resolve) => { - if (method === 'fieldCaps') { - resolve({ fields: [] }); - return; - } else if (method === 'ml.info') { - resolve({ - limits: { - effective_max_model_memory_limit: '100MB', - max_model_memory_limit: '1GB', - }, - }); - } - resolve({}); - }) as Promise; -}; +const mlClusterClient = ({ + // mock callAsCurrentUser + callAsCurrentUser: (method: string) => { + return new Promise((resolve) => { + if (method === 'fieldCaps') { + resolve({ fields: [] }); + return; + } else if (method === 'ml.info') { + resolve({ + limits: { + effective_max_model_memory_limit: '100MB', + max_model_memory_limit: '1GB', + }, + }); + } + resolve({}); + }) as Promise; + }, + + // mock callAsInternalUser + callAsInternalUser: (method: string) => { + return new Promise((resolve) => { + if (method === 'fieldCaps') { + resolve({ fields: [] }); + return; + } else if (method === 'ml.info') { + resolve({ + limits: { + effective_max_model_memory_limit: '100MB', + max_model_memory_limit: '1GB', + }, + }); + } + resolve({}); + }) as Promise; + }, +} as unknown) as ILegacyScopedClusterClient; // Note: The tests cast `payload` as any // so we can simulate possible runtime payloads @@ -36,7 +56,7 @@ describe('ML - validateJob', () => { job: { analysis_config: { detectors: [] } }, } as unknown) as ValidateJobPayload; - return validateJob(callWithRequest, payload).then((messages) => { + return validateJob(mlClusterClient, payload).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual([ @@ -56,7 +76,7 @@ describe('ML - validateJob', () => { job_id: id, }, } as unknown) as ValidateJobPayload; - return validateJob(callWithRequest, payload).catch(() => { + return validateJob(mlClusterClient, payload).catch(() => { new Error('Promise should not fail for jobIdTests.'); }); }); @@ -77,7 +97,7 @@ describe('ML - validateJob', () => { job: { analysis_config: { detectors: [] }, groups: testIds }, } as unknown) as ValidateJobPayload; - return validateJob(callWithRequest, payload).then((messages) => { + return validateJob(mlClusterClient, payload).then((messages) => { const ids = messages.map((m) => m.id); expect(ids.includes(messageId)).toBe(true); }); @@ -117,7 +137,7 @@ describe('ML - validateJob', () => { const payload = ({ job: { analysis_config: { bucket_span: format, detectors: [] } }, } as unknown) as ValidateJobPayload; - return validateJob(callWithRequest, payload).catch(() => { + return validateJob(mlClusterClient, payload).catch(() => { new Error('Promise should not fail for bucketSpanFormatTests.'); }); }); @@ -152,11 +172,11 @@ describe('ML - validateJob', () => { function: '', }); payload.job.analysis_config.detectors.push({ - // @ts-ignore + // @ts-expect-error function: undefined, }); - return validateJob(callWithRequest, payload).then((messages) => { + return validateJob(mlClusterClient, payload).then((messages) => { const ids = messages.map((m) => m.id); expect(ids.includes('detectors_function_empty')).toBe(true); }); @@ -170,7 +190,7 @@ describe('ML - validateJob', () => { function: 'count', }); - return validateJob(callWithRequest, payload).then((messages) => { + return validateJob(mlClusterClient, payload).then((messages) => { const ids = messages.map((m) => m.id); expect(ids.includes('detectors_function_not_empty')).toBe(true); }); @@ -182,7 +202,7 @@ describe('ML - validateJob', () => { fields: {}, } as unknown) as ValidateJobPayload; - return validateJob(callWithRequest, payload).then((messages) => { + return validateJob(mlClusterClient, payload).then((messages) => { const ids = messages.map((m) => m.id); expect(ids.includes('index_fields_invalid')).toBe(true); }); @@ -194,7 +214,7 @@ describe('ML - validateJob', () => { fields: { testField: {} }, } as unknown) as ValidateJobPayload; - return validateJob(callWithRequest, payload).then((messages) => { + return validateJob(mlClusterClient, payload).then((messages) => { const ids = messages.map((m) => m.id); expect(ids.includes('index_fields_valid')).toBe(true); }); @@ -222,7 +242,7 @@ describe('ML - validateJob', () => { const payload = getBasicPayload() as any; delete payload.job.analysis_config.influencers; - validateJob(callWithRequest, payload).then( + validateJob(mlClusterClient, payload).then( () => done( new Error('Promise should not resolve for this test when influencers is not an Array.') @@ -234,7 +254,7 @@ describe('ML - validateJob', () => { it('detect duplicate detectors', () => { const payload = getBasicPayload() as any; payload.job.analysis_config.detectors.push({ function: 'count' }); - return validateJob(callWithRequest, payload).then((messages) => { + return validateJob(mlClusterClient, payload).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual([ 'job_id_valid', @@ -257,7 +277,7 @@ describe('ML - validateJob', () => { { function: 'count', by_field_name: 'airline' }, { function: 'count', partition_field_name: 'airline' }, ]; - return validateJob(callWithRequest, payload).then((messages) => { + return validateJob(mlClusterClient, payload).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual([ 'job_id_valid', @@ -272,7 +292,7 @@ describe('ML - validateJob', () => { // Failing https://github.com/elastic/kibana/issues/65865 it('basic validation passes, extended checks return some messages', () => { const payload = getBasicPayload(); - return validateJob(callWithRequest, payload).then((messages) => { + return validateJob(mlClusterClient, payload).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual([ 'job_id_valid', @@ -305,7 +325,7 @@ describe('ML - validateJob', () => { fields: { testField: {} }, }; - return validateJob(callWithRequest, payload).then((messages) => { + return validateJob(mlClusterClient, payload).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual([ 'job_id_valid', @@ -338,7 +358,7 @@ describe('ML - validateJob', () => { fields: { testField: {} }, }; - return validateJob(callWithRequest, payload).then((messages) => { + return validateJob(mlClusterClient, payload).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual([ 'job_id_valid', @@ -381,7 +401,7 @@ describe('ML - validateJob', () => { fields: { testField: {} }, }; - return validateJob(callWithRequest, payload).then((messages) => { + return validateJob(mlClusterClient, payload).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual([ 'job_id_valid', @@ -400,7 +420,7 @@ describe('ML - validateJob', () => { const docsTestPayload = getBasicPayload() as any; docsTestPayload.job.analysis_config.detectors = [{ function: 'count', by_field_name: 'airline' }]; it('creates a docs url pointing to the current docs version', () => { - return validateJob(callWithRequest, docsTestPayload).then((messages) => { + return validateJob(mlClusterClient, docsTestPayload).then((messages) => { const message = messages[ messages.findIndex((m) => m.id === 'field_not_aggregatable') ] as JobValidationMessage; @@ -409,7 +429,7 @@ describe('ML - validateJob', () => { }); it('creates a docs url pointing to the master docs version', () => { - return validateJob(callWithRequest, docsTestPayload, 'master').then((messages) => { + return validateJob(mlClusterClient, docsTestPayload, 'master').then((messages) => { const message = messages[ messages.findIndex((m) => m.id === 'field_not_aggregatable') ] as JobValidationMessage; diff --git a/x-pack/plugins/ml/server/models/job_validation/job_validation.ts b/x-pack/plugins/ml/server/models/job_validation/job_validation.ts index 6e65e5e64f3b7..118e923283b3f 100644 --- a/x-pack/plugins/ml/server/models/job_validation/job_validation.ts +++ b/x-pack/plugins/ml/server/models/job_validation/job_validation.ts @@ -6,7 +6,7 @@ import { i18n } from '@kbn/i18n'; import Boom from 'boom'; -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { TypeOf } from '@kbn/config-schema'; import { fieldsServiceProvider } from '../fields_service'; @@ -19,7 +19,7 @@ import { import { VALIDATION_STATUS } from '../../../common/constants/validation'; import { basicJobValidation, uniqWithIsEqual } from '../../../common/util/job_utils'; -// @ts-ignore +// @ts-expect-error import { validateBucketSpan } from './validate_bucket_span'; import { validateCardinality } from './validate_cardinality'; import { validateInfluencers } from './validate_influencers'; @@ -35,10 +35,9 @@ export type ValidateJobPayload = TypeOf; * @kbn/config-schema has checked the payload {@link validateJobSchema}. */ export async function validateJob( - callWithRequest: LegacyAPICaller, + mlClusterClient: ILegacyScopedClusterClient, payload: ValidateJobPayload, kbnVersion = 'current', - callAsInternalUser?: LegacyAPICaller, isSecurityDisabled?: boolean ) { const messages = getMessages(); @@ -65,8 +64,8 @@ export async function validateJob( // if no duration was part of the request, fall back to finding out // the time range of the time field of the index, but also check first // if the time field is a valid field of type 'date' using isValidTimeField() - if (typeof duration === 'undefined' && (await isValidTimeField(callWithRequest, job))) { - const fs = fieldsServiceProvider(callWithRequest); + if (typeof duration === 'undefined' && (await isValidTimeField(mlClusterClient, job))) { + const fs = fieldsServiceProvider(mlClusterClient); const index = job.datafeed_config.indices.join(','); const timeField = job.data_description.time_field; const timeRange = await fs.getTimeFieldRange(index, timeField, job.datafeed_config.query); @@ -81,29 +80,23 @@ export async function validateJob( // next run only the cardinality tests to find out if they trigger an error // so we can decide later whether certain additional tests should be run - const cardinalityMessages = await validateCardinality(callWithRequest, job); + const cardinalityMessages = await validateCardinality(mlClusterClient, job); validationMessages.push(...cardinalityMessages); const cardinalityError = cardinalityMessages.some((m) => { return messages[m.id as MessageId].status === VALIDATION_STATUS.ERROR; }); validationMessages.push( - ...(await validateBucketSpan( - callWithRequest, - job, - duration, - callAsInternalUser, - isSecurityDisabled - )) + ...(await validateBucketSpan(mlClusterClient, job, duration, isSecurityDisabled)) ); - validationMessages.push(...(await validateTimeRange(callWithRequest, job, duration))); + validationMessages.push(...(await validateTimeRange(mlClusterClient, job, duration))); // only run the influencer and model memory limit checks // if cardinality checks didn't return a message with an error level if (cardinalityError === false) { - validationMessages.push(...(await validateInfluencers(callWithRequest, job))); + validationMessages.push(...(await validateInfluencers(job))); validationMessages.push( - ...(await validateModelMemoryLimit(callWithRequest, job, duration)) + ...(await validateModelMemoryLimit(mlClusterClient, job, duration)) ); } } else { 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 7dc2ad7ff3b8f..11f8d8967c4e0 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 @@ -45,13 +45,7 @@ const pickBucketSpan = (bucketSpans) => { return bucketSpans[i]; }; -export async function validateBucketSpan( - callWithRequest, - job, - duration, - callAsInternalUser, - isSecurityDisabled -) { +export async function validateBucketSpan(mlClusterClient, job, duration) { validateJobObject(job); // if there is no duration, do not run the estimate test @@ -123,11 +117,7 @@ export async function validateBucketSpan( try { const estimations = estimatorConfigs.map((data) => { return new Promise((resolve) => { - estimateBucketSpanFactory( - callWithRequest, - callAsInternalUser, - isSecurityDisabled - )(data) + estimateBucketSpanFactory(mlClusterClient)(data) .then(resolve) // this catch gets triggered when the estimation code runs without error // but isn't able to come up with a bucket span estimation. diff --git a/x-pack/plugins/ml/server/models/job_validation/validate_bucket_span.test.ts b/x-pack/plugins/ml/server/models/job_validation/validate_bucket_span.test.ts index 8d77fd5a1fd0e..f9145ab576d71 100644 --- a/x-pack/plugins/ml/server/models/job_validation/validate_bucket_span.test.ts +++ b/x-pack/plugins/ml/server/models/job_validation/validate_bucket_span.test.ts @@ -20,32 +20,36 @@ import mockFareQuoteSearchResponse from './__mocks__/mock_farequote_search_respo // sparse data with a low number of buckets import mockItSearchResponse from './__mocks__/mock_it_search_response.json'; -// mock callWithRequestFactory -const callWithRequestFactory = (mockSearchResponse: any) => { - return () => { +// mock mlClusterClientFactory +const mlClusterClientFactory = (mockSearchResponse: any) => { + const callAs = () => { return new Promise((resolve) => { resolve(mockSearchResponse); }); }; + return { + callAsCurrentUser: callAs, + callAsInternalUser: callAs, + }; }; describe('ML - validateBucketSpan', () => { it('called without arguments', (done) => { - validateBucketSpan(callWithRequestFactory(mockFareQuoteSearchResponse)).then( + validateBucketSpan(mlClusterClientFactory(mockFareQuoteSearchResponse)).then( () => done(new Error('Promise should not resolve for this test without job argument.')), () => done() ); }); it('called with non-valid job argument #1, missing datafeed_config', (done) => { - validateBucketSpan(callWithRequestFactory(mockFareQuoteSearchResponse), {}).then( + validateBucketSpan(mlClusterClientFactory(mockFareQuoteSearchResponse), {}).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), () => done() ); }); it('called with non-valid job argument #2, missing datafeed_config.indices', (done) => { - validateBucketSpan(callWithRequestFactory(mockFareQuoteSearchResponse), { + validateBucketSpan(mlClusterClientFactory(mockFareQuoteSearchResponse), { datafeed_config: {}, }).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), @@ -55,7 +59,7 @@ describe('ML - validateBucketSpan', () => { it('called with non-valid job argument #3, missing data_description', (done) => { const job = { datafeed_config: { indices: [] } }; - validateBucketSpan(callWithRequestFactory(mockFareQuoteSearchResponse), job).then( + validateBucketSpan(mlClusterClientFactory(mockFareQuoteSearchResponse), job).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), () => done() ); @@ -63,7 +67,7 @@ describe('ML - validateBucketSpan', () => { it('called with non-valid job argument #4, missing data_description.time_field', (done) => { const job = { datafeed_config: { indices: [] }, data_description: {} }; - validateBucketSpan(callWithRequestFactory(mockFareQuoteSearchResponse), job).then( + validateBucketSpan(mlClusterClientFactory(mockFareQuoteSearchResponse), job).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), () => done() ); @@ -74,7 +78,7 @@ describe('ML - validateBucketSpan', () => { datafeed_config: { indices: [] }, data_description: { time_field: '@timestamp' }, }; - validateBucketSpan(callWithRequestFactory(mockFareQuoteSearchResponse), job).then( + validateBucketSpan(mlClusterClientFactory(mockFareQuoteSearchResponse), job).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), () => done() ); @@ -87,7 +91,7 @@ describe('ML - validateBucketSpan', () => { datafeed_config: { indices: [] }, }; - return validateBucketSpan(callWithRequestFactory(mockFareQuoteSearchResponse), job).then( + return validateBucketSpan(mlClusterClientFactory(mockFareQuoteSearchResponse), job).then( (messages: JobValidationMessage[]) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual([]); @@ -110,7 +114,7 @@ describe('ML - validateBucketSpan', () => { const duration = { start: 0, end: 1 }; return validateBucketSpan( - callWithRequestFactory(mockFareQuoteSearchResponse), + mlClusterClientFactory(mockFareQuoteSearchResponse), job, duration ).then((messages: JobValidationMessage[]) => { @@ -124,7 +128,7 @@ describe('ML - validateBucketSpan', () => { const duration = { start: 0, end: 1 }; return validateBucketSpan( - callWithRequestFactory(mockFareQuoteSearchResponse), + mlClusterClientFactory(mockFareQuoteSearchResponse), job, duration ).then((messages: JobValidationMessage[]) => { @@ -147,7 +151,7 @@ describe('ML - validateBucketSpan', () => { function: 'count', }); - return validateBucketSpan(callWithRequestFactory(mockSearchResponse), job, {}).then( + return validateBucketSpan(mlClusterClientFactory(mockSearchResponse), job, {}).then( (messages: JobValidationMessage[]) => { const ids = messages.map((m) => m.id); test(ids); diff --git a/x-pack/plugins/ml/server/models/job_validation/validate_cardinality.test.ts b/x-pack/plugins/ml/server/models/job_validation/validate_cardinality.test.ts index bcfe4a48a0de0..92933877e2836 100644 --- a/x-pack/plugins/ml/server/models/job_validation/validate_cardinality.test.ts +++ b/x-pack/plugins/ml/server/models/job_validation/validate_cardinality.test.ts @@ -6,7 +6,7 @@ import _ from 'lodash'; -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { CombinedJob } from '../../../common/types/anomaly_detection_jobs'; @@ -20,9 +20,12 @@ const mockResponses = { fieldCaps: mockFieldCaps, }; -// mock callWithRequestFactory -const callWithRequestFactory = (responses: Record, fail = false): LegacyAPICaller => { - return (requestName: string) => { +// mock mlClusterClientFactory +const mlClusterClientFactory = ( + responses: Record, + fail = false +): ILegacyScopedClusterClient => { + const callAs = (requestName: string) => { return new Promise((resolve, reject) => { const response = responses[requestName]; if (fail) { @@ -32,25 +35,29 @@ const callWithRequestFactory = (responses: Record, fail = false): L } }) as Promise; }; + return { + callAsCurrentUser: callAs, + callAsInternalUser: callAs, + }; }; describe('ML - validateCardinality', () => { it('called without arguments', (done) => { - validateCardinality(callWithRequestFactory(mockResponses)).then( + validateCardinality(mlClusterClientFactory(mockResponses)).then( () => done(new Error('Promise should not resolve for this test without job argument.')), () => done() ); }); it('called with non-valid job argument #1, missing analysis_config', (done) => { - validateCardinality(callWithRequestFactory(mockResponses), {} as CombinedJob).then( + validateCardinality(mlClusterClientFactory(mockResponses), {} as CombinedJob).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), () => done() ); }); it('called with non-valid job argument #2, missing datafeed_config', (done) => { - validateCardinality(callWithRequestFactory(mockResponses), { + validateCardinality(mlClusterClientFactory(mockResponses), { analysis_config: {}, } as CombinedJob).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), @@ -60,7 +67,7 @@ describe('ML - validateCardinality', () => { it('called with non-valid job argument #3, missing datafeed_config.indices', (done) => { const job = { analysis_config: {}, datafeed_config: {} } as CombinedJob; - validateCardinality(callWithRequestFactory(mockResponses), job).then( + validateCardinality(mlClusterClientFactory(mockResponses), job).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), () => done() ); @@ -71,7 +78,7 @@ describe('ML - validateCardinality', () => { analysis_config: {}, datafeed_config: { indices: [] }, } as unknown) as CombinedJob; - validateCardinality(callWithRequestFactory(mockResponses), job).then( + validateCardinality(mlClusterClientFactory(mockResponses), job).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), () => done() ); @@ -83,7 +90,7 @@ describe('ML - validateCardinality', () => { data_description: {}, datafeed_config: { indices: [] }, } as unknown) as CombinedJob; - validateCardinality(callWithRequestFactory(mockResponses), job).then( + validateCardinality(mlClusterClientFactory(mockResponses), job).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), () => done() ); @@ -95,7 +102,7 @@ describe('ML - validateCardinality', () => { datafeed_config: { indices: [] }, data_description: { time_field: '@timestamp' }, } as unknown) as CombinedJob; - validateCardinality(callWithRequestFactory(mockResponses), job).then( + validateCardinality(mlClusterClientFactory(mockResponses), job).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), () => done() ); @@ -110,7 +117,7 @@ describe('ML - validateCardinality', () => { }, } as unknown) as CombinedJob; - return validateCardinality(callWithRequestFactory(mockResponses), job).then((messages) => { + return validateCardinality(mlClusterClientFactory(mockResponses), job).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual([]); }); @@ -141,7 +148,7 @@ describe('ML - validateCardinality', () => { const mockCardinality = _.cloneDeep(mockResponses); mockCardinality.search.aggregations.airline_cardinality.value = cardinality; return validateCardinality( - callWithRequestFactory(mockCardinality), + mlClusterClientFactory(mockCardinality), (job as unknown) as CombinedJob ).then((messages) => { const ids = messages.map((m) => m.id); @@ -153,7 +160,7 @@ describe('ML - validateCardinality', () => { const job = getJobConfig('partition_field_name'); job.analysis_config.detectors[0].partition_field_name = '_source'; return validateCardinality( - callWithRequestFactory(mockResponses), + mlClusterClientFactory(mockResponses), (job as unknown) as CombinedJob ).then((messages) => { const ids = messages.map((m) => m.id); @@ -164,7 +171,7 @@ describe('ML - validateCardinality', () => { it(`field 'airline' aggregatable`, () => { const job = getJobConfig('partition_field_name'); return validateCardinality( - callWithRequestFactory(mockResponses), + mlClusterClientFactory(mockResponses), (job as unknown) as CombinedJob ).then((messages) => { const ids = messages.map((m) => m.id); @@ -174,7 +181,7 @@ describe('ML - validateCardinality', () => { it('field not aggregatable', () => { const job = getJobConfig('partition_field_name'); - return validateCardinality(callWithRequestFactory({}), (job as unknown) as CombinedJob).then( + return validateCardinality(mlClusterClientFactory({}), (job as unknown) as CombinedJob).then( (messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual(['field_not_aggregatable']); @@ -189,7 +196,7 @@ describe('ML - validateCardinality', () => { partition_field_name: 'airline', }); return validateCardinality( - callWithRequestFactory({}, true), + mlClusterClientFactory({}, true), (job as unknown) as CombinedJob ).then((messages) => { const ids = messages.map((m) => m.id); @@ -245,7 +252,7 @@ describe('ML - validateCardinality', () => { job.model_plot_config = { enabled: false }; const mockCardinality = _.cloneDeep(mockResponses); mockCardinality.search.aggregations.airline_cardinality.value = cardinality; - return validateCardinality(callWithRequestFactory(mockCardinality), job).then((messages) => { + return validateCardinality(mlClusterClientFactory(mockCardinality), job).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual(['success_cardinality']); }); @@ -256,7 +263,7 @@ describe('ML - validateCardinality', () => { job.model_plot_config = { enabled: true }; const mockCardinality = _.cloneDeep(mockResponses); mockCardinality.search.aggregations.airline_cardinality.value = cardinality; - return validateCardinality(callWithRequestFactory(mockCardinality), job).then((messages) => { + return validateCardinality(mlClusterClientFactory(mockCardinality), job).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual(['cardinality_model_plot_high']); }); @@ -267,7 +274,7 @@ describe('ML - validateCardinality', () => { job.model_plot_config = { enabled: false }; const mockCardinality = _.cloneDeep(mockResponses); mockCardinality.search.aggregations.airline_cardinality.value = cardinality; - return validateCardinality(callWithRequestFactory(mockCardinality), job).then((messages) => { + return validateCardinality(mlClusterClientFactory(mockCardinality), job).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual(['cardinality_by_field']); }); @@ -278,7 +285,7 @@ describe('ML - validateCardinality', () => { job.model_plot_config = { enabled: true }; const mockCardinality = _.cloneDeep(mockResponses); mockCardinality.search.aggregations.airline_cardinality.value = cardinality; - return validateCardinality(callWithRequestFactory(mockCardinality), job).then((messages) => { + return validateCardinality(mlClusterClientFactory(mockCardinality), job).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual(['cardinality_model_plot_high', 'cardinality_by_field']); }); @@ -289,7 +296,7 @@ describe('ML - validateCardinality', () => { job.model_plot_config = { enabled: true, terms: 'AAL,AAB' }; const mockCardinality = _.cloneDeep(mockResponses); mockCardinality.search.aggregations.airline_cardinality.value = cardinality; - return validateCardinality(callWithRequestFactory(mockCardinality), job).then((messages) => { + return validateCardinality(mlClusterClientFactory(mockCardinality), job).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual(['cardinality_by_field']); }); diff --git a/x-pack/plugins/ml/server/models/job_validation/validate_cardinality.ts b/x-pack/plugins/ml/server/models/job_validation/validate_cardinality.ts index d5bc6aa20e32a..1545c4c0062ec 100644 --- a/x-pack/plugins/ml/server/models/job_validation/validate_cardinality.ts +++ b/x-pack/plugins/ml/server/models/job_validation/validate_cardinality.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { DataVisualizer } from '../data_visualizer'; import { validateJobObject } from './validate_job_object'; @@ -43,8 +43,12 @@ type Validator = (obj: { messages: Messages; }>; -const validateFactory = (callWithRequest: LegacyAPICaller, job: CombinedJob): Validator => { - const dv = new DataVisualizer(callWithRequest); +const validateFactory = ( + mlClusterClient: ILegacyScopedClusterClient, + job: CombinedJob +): Validator => { + const { callAsCurrentUser } = mlClusterClient; + const dv = new DataVisualizer(mlClusterClient); const modelPlotConfigTerms = job?.model_plot_config?.terms ?? ''; const modelPlotConfigFieldCount = @@ -73,7 +77,7 @@ const validateFactory = (callWithRequest: LegacyAPICaller, job: CombinedJob): Va ] as string[]; // use fieldCaps endpoint to get data about whether fields are aggregatable - const fieldCaps = await callWithRequest('fieldCaps', { + const fieldCaps = await callAsCurrentUser('fieldCaps', { index: job.datafeed_config.indices.join(','), fields: uniqueFieldNames, }); @@ -150,7 +154,7 @@ const validateFactory = (callWithRequest: LegacyAPICaller, job: CombinedJob): Va }; export async function validateCardinality( - callWithRequest: LegacyAPICaller, + mlClusterClient: ILegacyScopedClusterClient, job?: CombinedJob ): Promise | never { const messages: Messages = []; @@ -170,7 +174,7 @@ export async function validateCardinality( } // validate({ type, isInvalid }) asynchronously returns an array of validation messages - const validate = validateFactory(callWithRequest, job); + const validate = validateFactory(mlClusterClient, job); const modelPlotEnabled = job.model_plot_config?.enabled ?? false; diff --git a/x-pack/plugins/ml/server/models/job_validation/validate_influencers.test.ts b/x-pack/plugins/ml/server/models/job_validation/validate_influencers.test.ts index 594b51a773ada..39f5b86c44b7f 100644 --- a/x-pack/plugins/ml/server/models/job_validation/validate_influencers.test.ts +++ b/x-pack/plugins/ml/server/models/job_validation/validate_influencers.test.ts @@ -4,28 +4,20 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; - import { CombinedJob } from '../../../common/types/anomaly_detection_jobs'; import { validateInfluencers } from './validate_influencers'; describe('ML - validateInfluencers', () => { it('called without arguments throws an error', (done) => { - validateInfluencers( - (undefined as unknown) as LegacyAPICaller, - (undefined as unknown) as CombinedJob - ).then( + validateInfluencers((undefined as unknown) as CombinedJob).then( () => done(new Error('Promise should not resolve for this test without job argument.')), () => done() ); }); it('called with non-valid job argument #1, missing analysis_config', (done) => { - validateInfluencers( - (undefined as unknown) as LegacyAPICaller, - ({} as unknown) as CombinedJob - ).then( + validateInfluencers(({} as unknown) as CombinedJob).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), () => done() ); @@ -37,10 +29,7 @@ describe('ML - validateInfluencers', () => { datafeed_config: { indices: [] }, data_description: { time_field: '@timestamp' }, }; - validateInfluencers( - (undefined as unknown) as LegacyAPICaller, - (job as unknown) as CombinedJob - ).then( + validateInfluencers((job as unknown) as CombinedJob).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), () => done() ); @@ -52,10 +41,7 @@ describe('ML - validateInfluencers', () => { datafeed_config: { indices: [] }, data_description: { time_field: '@timestamp' }, }; - validateInfluencers( - (undefined as unknown) as LegacyAPICaller, - (job as unknown) as CombinedJob - ).then( + validateInfluencers((job as unknown) as CombinedJob).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), () => done() ); @@ -75,7 +61,7 @@ describe('ML - validateInfluencers', () => { it('success_influencer', () => { const job = getJobConfig(['airline']); - return validateInfluencers((undefined as unknown) as LegacyAPICaller, job).then((messages) => { + return validateInfluencers(job).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual(['success_influencers']); }); @@ -93,7 +79,7 @@ describe('ML - validateInfluencers', () => { ] ); - return validateInfluencers((undefined as unknown) as LegacyAPICaller, job).then((messages) => { + return validateInfluencers(job).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual([]); }); @@ -101,7 +87,7 @@ describe('ML - validateInfluencers', () => { it('influencer_low', () => { const job = getJobConfig(); - return validateInfluencers((undefined as unknown) as LegacyAPICaller, job).then((messages) => { + return validateInfluencers(job).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual(['influencer_low']); }); @@ -109,7 +95,7 @@ describe('ML - validateInfluencers', () => { it('influencer_high', () => { const job = getJobConfig(['i1', 'i2', 'i3', 'i4']); - return validateInfluencers((undefined as unknown) as LegacyAPICaller, job).then((messages) => { + return validateInfluencers(job).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual(['influencer_high']); }); @@ -127,7 +113,7 @@ describe('ML - validateInfluencers', () => { }, ] ); - return validateInfluencers((undefined as unknown) as LegacyAPICaller, job).then((messages) => { + return validateInfluencers(job).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toStrictEqual(['influencer_low_suggestion']); }); @@ -157,7 +143,7 @@ describe('ML - validateInfluencers', () => { }, ] ); - return validateInfluencers((undefined as unknown) as LegacyAPICaller, job).then((messages) => { + return validateInfluencers(job).then((messages) => { expect(messages).toStrictEqual([ { id: 'influencer_low_suggestions', diff --git a/x-pack/plugins/ml/server/models/job_validation/validate_influencers.ts b/x-pack/plugins/ml/server/models/job_validation/validate_influencers.ts index 1a77bfaf60811..72995619f6eca 100644 --- a/x-pack/plugins/ml/server/models/job_validation/validate_influencers.ts +++ b/x-pack/plugins/ml/server/models/job_validation/validate_influencers.ts @@ -4,8 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; - import { CombinedJob } from '../../../common/types/anomaly_detection_jobs'; import { validateJobObject } from './validate_job_object'; @@ -14,7 +12,7 @@ const INFLUENCER_LOW_THRESHOLD = 0; const INFLUENCER_HIGH_THRESHOLD = 4; const DETECTOR_FIELD_NAMES_THRESHOLD = 1; -export async function validateInfluencers(callWithRequest: LegacyAPICaller, job: CombinedJob) { +export async function validateInfluencers(job: CombinedJob) { validateJobObject(job); const messages = []; diff --git a/x-pack/plugins/ml/server/models/job_validation/validate_model_memory_limit.test.ts b/x-pack/plugins/ml/server/models/job_validation/validate_model_memory_limit.test.ts index d9be8e282e923..61af960847f7f 100644 --- a/x-pack/plugins/ml/server/models/job_validation/validate_model_memory_limit.test.ts +++ b/x-pack/plugins/ml/server/models/job_validation/validate_model_memory_limit.test.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { CombinedJob, Detector } from '../../../common/types/anomaly_detection_jobs'; import { ModelMemoryEstimate } from '../calculate_model_memory_limit/calculate_model_memory_limit'; import { validateModelMemoryLimit } from './validate_model_memory_limit'; @@ -73,15 +73,15 @@ describe('ML - validateModelMemoryLimit', () => { 'ml.estimateModelMemory'?: ModelMemoryEstimate; } - // mock callWithRequest + // mock callAsCurrentUser // used in three places: // - to retrieve the info endpoint // - to search for cardinality of split field // - to retrieve field capabilities used in search for split field cardinality - const getMockCallWithRequest = ({ + const getMockMlClusterClient = ({ 'ml.estimateModelMemory': estimateModelMemory, - }: MockAPICallResponse = {}) => - ((call: string) => { + }: MockAPICallResponse = {}): ILegacyScopedClusterClient => { + const callAs = (call: string) => { if (typeof call === undefined) { return Promise.reject(); } @@ -97,7 +97,13 @@ describe('ML - validateModelMemoryLimit', () => { response = estimateModelMemory || modelMemoryEstimateResponse; } return Promise.resolve(response); - }) as LegacyAPICaller; + }; + + return { + callAsCurrentUser: callAs, + callAsInternalUser: callAs, + }; + }; function getJobConfig(influencers: string[] = [], detectors: Detector[] = []) { return ({ @@ -129,7 +135,7 @@ describe('ML - validateModelMemoryLimit', () => { const job = getJobConfig(); const duration = undefined; - return validateModelMemoryLimit(getMockCallWithRequest(), job, duration).then((messages) => { + return validateModelMemoryLimit(getMockMlClusterClient(), job, duration).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toEqual([]); }); @@ -138,10 +144,10 @@ describe('ML - validateModelMemoryLimit', () => { it('Called with no duration or split and mml above limit', () => { const job = getJobConfig(); const duration = undefined; - // @ts-ignore + // @ts-expect-error job.analysis_limits.model_memory_limit = '31mb'; - return validateModelMemoryLimit(getMockCallWithRequest(), job, duration).then((messages) => { + return validateModelMemoryLimit(getMockMlClusterClient(), job, duration).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toEqual(['mml_greater_than_max_mml']); }); @@ -151,11 +157,11 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(10); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-ignore + // @ts-expect-error job.analysis_limits.model_memory_limit = '20mb'; return validateModelMemoryLimit( - getMockCallWithRequest({ 'ml.estimateModelMemory': { model_memory_estimate: '66mb' } }), + getMockMlClusterClient({ 'ml.estimateModelMemory': { model_memory_estimate: '66mb' } }), job, duration ).then((messages) => { @@ -168,11 +174,11 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(2); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-ignore + // @ts-expect-error job.analysis_limits.model_memory_limit = '30mb'; return validateModelMemoryLimit( - getMockCallWithRequest({ 'ml.estimateModelMemory': { model_memory_estimate: '24mb' } }), + getMockMlClusterClient({ 'ml.estimateModelMemory': { model_memory_estimate: '24mb' } }), job, duration ).then((messages) => { @@ -185,11 +191,11 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(2); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-ignore + // @ts-expect-error job.analysis_limits.model_memory_limit = '10mb'; return validateModelMemoryLimit( - getMockCallWithRequest({ 'ml.estimateModelMemory': { model_memory_estimate: '22mb' } }), + getMockMlClusterClient({ 'ml.estimateModelMemory': { model_memory_estimate: '22mb' } }), job, duration ).then((messages) => { @@ -203,10 +209,10 @@ describe('ML - validateModelMemoryLimit', () => { const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; delete mlInfoResponse.limits.max_model_memory_limit; - // @ts-ignore + // @ts-expect-error job.analysis_limits.model_memory_limit = '10mb'; - return validateModelMemoryLimit(getMockCallWithRequest(), job, duration).then((messages) => { + return validateModelMemoryLimit(getMockMlClusterClient(), job, duration).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toEqual(['half_estimated_mml_greater_than_mml']); }); @@ -215,10 +221,10 @@ describe('ML - validateModelMemoryLimit', () => { it('Called with no duration or split and mml above limit, no max setting', () => { const job = getJobConfig(); const duration = undefined; - // @ts-ignore + // @ts-expect-error job.analysis_limits.model_memory_limit = '31mb'; - return validateModelMemoryLimit(getMockCallWithRequest(), job, duration).then((messages) => { + return validateModelMemoryLimit(getMockMlClusterClient(), job, duration).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toEqual([]); }); @@ -227,10 +233,10 @@ describe('ML - validateModelMemoryLimit', () => { it('Called with no duration or split and mml above limit, no max setting, above effective max mml', () => { const job = getJobConfig(); const duration = undefined; - // @ts-ignore + // @ts-expect-error job.analysis_limits.model_memory_limit = '41mb'; - return validateModelMemoryLimit(getMockCallWithRequest(), job, duration).then((messages) => { + return validateModelMemoryLimit(getMockMlClusterClient(), job, duration).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toEqual(['mml_greater_than_effective_max_mml']); }); @@ -240,11 +246,11 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-ignore + // @ts-expect-error job.analysis_limits.model_memory_limit = '20mb'; return validateModelMemoryLimit( - getMockCallWithRequest({ 'ml.estimateModelMemory': { model_memory_estimate: '19mb' } }), + getMockMlClusterClient({ 'ml.estimateModelMemory': { model_memory_estimate: '19mb' } }), job, duration ).then((messages) => { @@ -257,10 +263,10 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-ignore + // @ts-expect-error job.analysis_limits.model_memory_limit = '0mb'; - return validateModelMemoryLimit(getMockCallWithRequest(), job, duration).then((messages) => { + return validateModelMemoryLimit(getMockMlClusterClient(), job, duration).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toEqual(['mml_value_invalid']); }); @@ -270,10 +276,10 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-ignore + // @ts-expect-error job.analysis_limits.model_memory_limit = '10mbananas'; - return validateModelMemoryLimit(getMockCallWithRequest(), job, duration).then((messages) => { + return validateModelMemoryLimit(getMockMlClusterClient(), job, duration).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toEqual(['mml_value_invalid']); }); @@ -283,10 +289,10 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-ignore + // @ts-expect-error job.analysis_limits.model_memory_limit = '10'; - return validateModelMemoryLimit(getMockCallWithRequest(), job, duration).then((messages) => { + return validateModelMemoryLimit(getMockMlClusterClient(), job, duration).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toEqual(['mml_value_invalid']); }); @@ -296,10 +302,10 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-ignore + // @ts-expect-error job.analysis_limits.model_memory_limit = 'mb'; - return validateModelMemoryLimit(getMockCallWithRequest(), job, duration).then((messages) => { + return validateModelMemoryLimit(getMockMlClusterClient(), job, duration).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toEqual(['mml_value_invalid']); }); @@ -309,10 +315,10 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-ignore + // @ts-expect-error job.analysis_limits.model_memory_limit = 'asdf'; - return validateModelMemoryLimit(getMockCallWithRequest(), job, duration).then((messages) => { + return validateModelMemoryLimit(getMockMlClusterClient(), job, duration).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toEqual(['mml_value_invalid']); }); @@ -322,10 +328,10 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-ignore + // @ts-expect-error job.analysis_limits.model_memory_limit = '1023KB'; - return validateModelMemoryLimit(getMockCallWithRequest(), job, duration).then((messages) => { + return validateModelMemoryLimit(getMockMlClusterClient(), job, duration).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toEqual(['mml_value_invalid']); }); @@ -335,10 +341,10 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-ignore + // @ts-expect-error job.analysis_limits.model_memory_limit = '1024KB'; - return validateModelMemoryLimit(getMockCallWithRequest(), job, duration).then((messages) => { + return validateModelMemoryLimit(getMockMlClusterClient(), job, duration).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toEqual(['half_estimated_mml_greater_than_mml']); }); @@ -348,10 +354,10 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-ignore + // @ts-expect-error job.analysis_limits.model_memory_limit = '6MB'; - return validateModelMemoryLimit(getMockCallWithRequest(), job, duration).then((messages) => { + return validateModelMemoryLimit(getMockMlClusterClient(), job, duration).then((messages) => { const ids = messages.map((m) => m.id); expect(ids).toEqual(['half_estimated_mml_greater_than_mml']); }); @@ -361,11 +367,11 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-ignore + // @ts-expect-error job.analysis_limits.model_memory_limit = '20MB'; return validateModelMemoryLimit( - getMockCallWithRequest({ 'ml.estimateModelMemory': { model_memory_estimate: '20mb' } }), + getMockMlClusterClient({ 'ml.estimateModelMemory': { model_memory_estimate: '20mb' } }), job, duration ).then((messages) => { diff --git a/x-pack/plugins/ml/server/models/job_validation/validate_model_memory_limit.ts b/x-pack/plugins/ml/server/models/job_validation/validate_model_memory_limit.ts index 2c7d1cc23bbaa..728342294c424 100644 --- a/x-pack/plugins/ml/server/models/job_validation/validate_model_memory_limit.ts +++ b/x-pack/plugins/ml/server/models/job_validation/validate_model_memory_limit.ts @@ -5,7 +5,7 @@ */ import numeral from '@elastic/numeral'; -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { CombinedJob } from '../../../common/types/anomaly_detection_jobs'; import { validateJobObject } from './validate_job_object'; import { calculateModelMemoryLimitProvider } from '../calculate_model_memory_limit'; @@ -16,10 +16,11 @@ import { MlInfoResponse } from '../../../common/types/ml_server_info'; const MODEL_MEMORY_LIMIT_MINIMUM_BYTES = 1048576; export async function validateModelMemoryLimit( - callWithRequest: LegacyAPICaller, + mlClusterClient: ILegacyScopedClusterClient, job: CombinedJob, duration?: { start?: number; end?: number } ) { + const { callAsInternalUser } = mlClusterClient; validateJobObject(job); // retrieve the model memory limit specified by the user in the job config. @@ -51,12 +52,12 @@ export async function validateModelMemoryLimit( // retrieve the max_model_memory_limit value from the server // this will be unset unless the user has set this on their cluster - const info = await callWithRequest('ml.info'); + const info = (await callAsInternalUser('ml.info')) as MlInfoResponse; const maxModelMemoryLimit = info.limits.max_model_memory_limit?.toUpperCase(); const effectiveMaxModelMemoryLimit = info.limits.effective_max_model_memory_limit?.toUpperCase(); if (runCalcModelMemoryTest) { - const { modelMemoryLimit } = await calculateModelMemoryLimitProvider(callWithRequest)( + const { modelMemoryLimit } = await calculateModelMemoryLimitProvider(mlClusterClient)( job.analysis_config, job.datafeed_config.indices.join(','), job.datafeed_config.query, @@ -65,14 +66,14 @@ export async function validateModelMemoryLimit( duration!.end as number, true ); - // @ts-ignore + // @ts-expect-error const mmlEstimateBytes: number = numeral(modelMemoryLimit).value(); let runEstimateGreaterThenMml = true; // if max_model_memory_limit has been set, // make sure the estimated value is not greater than it. if (typeof maxModelMemoryLimit !== 'undefined') { - // @ts-ignore + // @ts-expect-error const maxMmlBytes: number = numeral(maxModelMemoryLimit).value(); if (mmlEstimateBytes > maxMmlBytes) { runEstimateGreaterThenMml = false; @@ -89,7 +90,7 @@ export async function validateModelMemoryLimit( // do not run this if we've already found that it's larger than // the max mml if (runEstimateGreaterThenMml && mml !== null) { - // @ts-ignore + // @ts-expect-error const mmlBytes: number = numeral(mml).value(); if (mmlBytes < MODEL_MEMORY_LIMIT_MINIMUM_BYTES) { messages.push({ @@ -116,11 +117,11 @@ export async function validateModelMemoryLimit( // make sure the user defined MML is not greater than it if (mml !== null) { let maxMmlExceeded = false; - // @ts-ignore + // @ts-expect-error const mmlBytes = numeral(mml).value(); if (maxModelMemoryLimit !== undefined) { - // @ts-ignore + // @ts-expect-error const maxMmlBytes = numeral(maxModelMemoryLimit).value(); if (mmlBytes > maxMmlBytes) { maxMmlExceeded = true; @@ -133,7 +134,7 @@ export async function validateModelMemoryLimit( } if (effectiveMaxModelMemoryLimit !== undefined && maxMmlExceeded === false) { - // @ts-ignore + // @ts-expect-error const effectiveMaxMmlBytes = numeral(effectiveMaxModelMemoryLimit).value(); if (mmlBytes > effectiveMaxMmlBytes) { messages.push({ diff --git a/x-pack/plugins/ml/server/models/job_validation/validate_time_range.test.ts b/x-pack/plugins/ml/server/models/job_validation/validate_time_range.test.ts index d4e1f0cc379fb..f74d8a26ef370 100644 --- a/x-pack/plugins/ml/server/models/job_validation/validate_time_range.test.ts +++ b/x-pack/plugins/ml/server/models/job_validation/validate_time_range.test.ts @@ -6,7 +6,7 @@ import _ from 'lodash'; -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { CombinedJob } from '../../../common/types/anomaly_detection_jobs'; @@ -21,12 +21,16 @@ const mockSearchResponse = { search: mockTimeRange, }; -const callWithRequestFactory = (resp: any): LegacyAPICaller => { - return (path: string) => { +const mlClusterClientFactory = (resp: any): ILegacyScopedClusterClient => { + const callAs = (path: string) => { return new Promise((resolve) => { resolve(resp[path]); }) as Promise; }; + return { + callAsCurrentUser: callAs, + callAsInternalUser: callAs, + }; }; function getMinimalValidJob() { @@ -46,7 +50,7 @@ function getMinimalValidJob() { describe('ML - isValidTimeField', () => { it('called without job config argument triggers Promise rejection', (done) => { isValidTimeField( - callWithRequestFactory(mockSearchResponse), + mlClusterClientFactory(mockSearchResponse), (undefined as unknown) as CombinedJob ).then( () => done(new Error('Promise should not resolve for this test without job argument.')), @@ -55,7 +59,7 @@ describe('ML - isValidTimeField', () => { }); it('time_field `@timestamp`', (done) => { - isValidTimeField(callWithRequestFactory(mockSearchResponse), getMinimalValidJob()).then( + isValidTimeField(mlClusterClientFactory(mockSearchResponse), getMinimalValidJob()).then( (valid) => { expect(valid).toBe(true); done(); @@ -74,7 +78,7 @@ describe('ML - isValidTimeField', () => { }; isValidTimeField( - callWithRequestFactory(mockSearchResponseNestedDate), + mlClusterClientFactory(mockSearchResponseNestedDate), mockJobConfigNestedDate ).then( (valid) => { @@ -89,7 +93,7 @@ describe('ML - isValidTimeField', () => { describe('ML - validateTimeRange', () => { it('called without arguments', (done) => { validateTimeRange( - callWithRequestFactory(mockSearchResponse), + mlClusterClientFactory(mockSearchResponse), (undefined as unknown) as CombinedJob ).then( () => done(new Error('Promise should not resolve for this test without job argument.')), @@ -98,7 +102,7 @@ describe('ML - validateTimeRange', () => { }); it('called with non-valid job argument #2, missing datafeed_config', (done) => { - validateTimeRange(callWithRequestFactory(mockSearchResponse), ({ + validateTimeRange(mlClusterClientFactory(mockSearchResponse), ({ analysis_config: {}, } as unknown) as CombinedJob).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), @@ -109,7 +113,7 @@ describe('ML - validateTimeRange', () => { it('called with non-valid job argument #3, missing datafeed_config.indices', (done) => { const job = { analysis_config: {}, datafeed_config: {} }; validateTimeRange( - callWithRequestFactory(mockSearchResponse), + mlClusterClientFactory(mockSearchResponse), (job as unknown) as CombinedJob ).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), @@ -120,7 +124,7 @@ describe('ML - validateTimeRange', () => { it('called with non-valid job argument #4, missing data_description', (done) => { const job = { analysis_config: {}, datafeed_config: { indices: [] } }; validateTimeRange( - callWithRequestFactory(mockSearchResponse), + mlClusterClientFactory(mockSearchResponse), (job as unknown) as CombinedJob ).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), @@ -131,7 +135,7 @@ describe('ML - validateTimeRange', () => { it('called with non-valid job argument #5, missing data_description.time_field', (done) => { const job = { analysis_config: {}, data_description: {}, datafeed_config: { indices: [] } }; validateTimeRange( - callWithRequestFactory(mockSearchResponse), + mlClusterClientFactory(mockSearchResponse), (job as unknown) as CombinedJob ).then( () => done(new Error('Promise should not resolve for this test without valid job argument.')), @@ -144,7 +148,7 @@ describe('ML - validateTimeRange', () => { mockSearchResponseInvalid.fieldCaps = undefined; const duration = { start: 0, end: 1 }; return validateTimeRange( - callWithRequestFactory(mockSearchResponseInvalid), + mlClusterClientFactory(mockSearchResponseInvalid), getMinimalValidJob(), duration ).then((messages) => { @@ -158,7 +162,7 @@ describe('ML - validateTimeRange', () => { jobShortTimeRange.analysis_config.bucket_span = '1s'; const duration = { start: 0, end: 1 }; return validateTimeRange( - callWithRequestFactory(mockSearchResponse), + mlClusterClientFactory(mockSearchResponse), jobShortTimeRange, duration ).then((messages) => { @@ -170,7 +174,7 @@ describe('ML - validateTimeRange', () => { it('too short time range, 25x bucket span is more than 2h', () => { const duration = { start: 0, end: 1 }; return validateTimeRange( - callWithRequestFactory(mockSearchResponse), + mlClusterClientFactory(mockSearchResponse), getMinimalValidJob(), duration ).then((messages) => { @@ -182,7 +186,7 @@ describe('ML - validateTimeRange', () => { it('time range between 2h and 25x bucket span', () => { const duration = { start: 0, end: 8000000 }; return validateTimeRange( - callWithRequestFactory(mockSearchResponse), + mlClusterClientFactory(mockSearchResponse), getMinimalValidJob(), duration ).then((messages) => { @@ -194,7 +198,7 @@ describe('ML - validateTimeRange', () => { it('valid time range', () => { const duration = { start: 0, end: 100000000 }; return validateTimeRange( - callWithRequestFactory(mockSearchResponse), + mlClusterClientFactory(mockSearchResponse), getMinimalValidJob(), duration ).then((messages) => { @@ -206,7 +210,7 @@ describe('ML - validateTimeRange', () => { it('invalid time range, start time is before the UNIX epoch', () => { const duration = { start: -1, end: 100000000 }; return validateTimeRange( - callWithRequestFactory(mockSearchResponse), + mlClusterClientFactory(mockSearchResponse), getMinimalValidJob(), duration ).then((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 f47938e059ec0..a94ceffa90273 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 @@ -4,11 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { ES_FIELD_TYPES } from '../../../../../../src/plugins/data/server'; import { parseInterval } from '../../../common/util/parse_interval'; import { CombinedJob } from '../../../common/types/anomaly_detection_jobs'; -// @ts-ignore import { validateJobObject } from './validate_job_object'; interface ValidateTimeRangeMessage { @@ -27,7 +26,10 @@ const BUCKET_SPAN_COMPARE_FACTOR = 25; const MIN_TIME_SPAN_MS = 7200000; const MIN_TIME_SPAN_READABLE = '2 hours'; -export async function isValidTimeField(callAsCurrentUser: LegacyAPICaller, job: CombinedJob) { +export async function isValidTimeField( + { callAsCurrentUser }: ILegacyScopedClusterClient, + job: CombinedJob +) { const index = job.datafeed_config.indices.join(','); const timeField = job.data_description.time_field; @@ -45,7 +47,7 @@ export async function isValidTimeField(callAsCurrentUser: LegacyAPICaller, job: } export async function validateTimeRange( - callAsCurrentUser: LegacyAPICaller, + mlClientCluster: ILegacyScopedClusterClient, job: CombinedJob, timeRange?: Partial ) { @@ -54,7 +56,7 @@ export async function validateTimeRange( validateJobObject(job); // check if time_field is a date type - if (!(await isValidTimeField(callAsCurrentUser, job))) { + if (!(await isValidTimeField(mlClientCluster, job))) { messages.push({ id: 'time_field_invalid', timeField: job.data_description.time_field, diff --git a/x-pack/plugins/ml/server/models/results_service/get_partition_fields_values.ts b/x-pack/plugins/ml/server/models/results_service/get_partition_fields_values.ts index 99eeaacc8de9c..663ee846571e7 100644 --- a/x-pack/plugins/ml/server/models/results_service/get_partition_fields_values.ts +++ b/x-pack/plugins/ml/server/models/results_service/get_partition_fields_values.ts @@ -5,14 +5,12 @@ */ import Boom from 'boom'; +import { ILegacyScopedClusterClient } from 'kibana/server'; +import { PARTITION_FIELDS } from '../../../common/constants/anomalies'; +import { PartitionFieldsType } from '../../../common/types/anomalies'; import { ML_RESULTS_INDEX_PATTERN } from '../../../common/constants/index_patterns'; -import { callWithRequestType } from '../../../common/types/kibana'; import { CriteriaField } from './results_service'; -const PARTITION_FIELDS = ['partition_field', 'over_field', 'by_field'] as const; - -type PartitionFieldsType = typeof PARTITION_FIELDS[number]; - type SearchTerm = | { [key in PartitionFieldsType]?: string; @@ -76,7 +74,10 @@ function getFieldObject(fieldType: PartitionFieldsType, aggs: any) { : {}; } -export const getPartitionFieldsValuesFactory = (callWithRequest: callWithRequestType) => +export const getPartitionFieldsValuesFactory = ({ + callAsCurrentUser, + callAsInternalUser, +}: ILegacyScopedClusterClient) => /** * Gets the record of partition fields with possible values that fit the provided queries. * @param jobId - Job ID @@ -92,7 +93,7 @@ export const getPartitionFieldsValuesFactory = (callWithRequest: callWithRequest earliestMs: number, latestMs: number ) { - const jobsResponse = await callWithRequest('ml.jobs', { jobId: [jobId] }); + const jobsResponse = await callAsInternalUser('ml.jobs', { jobId: [jobId] }); if (jobsResponse.count === 0 || jobsResponse.jobs === undefined) { throw Boom.notFound(`Job with the id "${jobId}" not found`); } @@ -101,7 +102,7 @@ export const getPartitionFieldsValuesFactory = (callWithRequest: callWithRequest const isModelPlotEnabled = job?.model_plot_config?.enabled; - const resp = await callWithRequest('search', { + const resp = await callAsCurrentUser('search', { index: ML_RESULTS_INDEX_PATTERN, size: 0, body: { diff --git a/x-pack/plugins/ml/server/models/results_service/results_service.ts b/x-pack/plugins/ml/server/models/results_service/results_service.ts index 8255395000f47..8e904143263d7 100644 --- a/x-pack/plugins/ml/server/models/results_service/results_service.ts +++ b/x-pack/plugins/ml/server/models/results_service/results_service.ts @@ -7,7 +7,7 @@ import _ from 'lodash'; import moment from 'moment'; import { SearchResponse } from 'elasticsearch'; -import { LegacyAPICaller } from 'kibana/server'; +import { ILegacyScopedClusterClient } from 'kibana/server'; import { buildAnomalyTableItems } from './build_anomaly_table_items'; import { ML_RESULTS_INDEX_PATTERN } from '../../../common/constants/index_patterns'; import { ANOMALIES_TABLE_DEFAULT_QUERY_SIZE } from '../../../common/constants/search'; @@ -30,7 +30,8 @@ interface Influencer { fieldValue: any; } -export function resultsServiceProvider(callAsCurrentUser: LegacyAPICaller) { +export function resultsServiceProvider(mlClusterClient: ILegacyScopedClusterClient) { + const { callAsCurrentUser } = mlClusterClient; // Obtains data for the anomalies table, aggregating anomalies by day or hour as requested. // Return an Object with properties 'anomalies' and 'interval' (interval used to aggregate anomalies, // one of day, hour or second. Note 'auto' can be provided as the aggregationInterval in the request, @@ -435,6 +436,6 @@ export function resultsServiceProvider(callAsCurrentUser: LegacyAPICaller) { getCategoryExamples, getLatestBucketTimestampByJob, getMaxAnomalyScore, - getPartitionFieldsValues: getPartitionFieldsValuesFactory(callAsCurrentUser), + getPartitionFieldsValues: getPartitionFieldsValuesFactory(mlClusterClient), }; } diff --git a/x-pack/plugins/ml/server/plugin.ts b/x-pack/plugins/ml/server/plugin.ts index 83b14d60fb416..812db744d1bda 100644 --- a/x-pack/plugins/ml/server/plugin.ts +++ b/x-pack/plugins/ml/server/plugin.ts @@ -75,7 +75,7 @@ export class MlServerPlugin implements Plugin { try { - const { getAnnotations } = annotationServiceProvider( - context.ml!.mlClient.callAsCurrentUser - ); + const { getAnnotations } = annotationServiceProvider(context.ml!.mlClient); const resp = await getAnnotations(request.body); return response.ok({ @@ -96,19 +94,17 @@ export function annotationRoutes( mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable( - context.ml!.mlClient.callAsCurrentUser + context.ml!.mlClient ); if (annotationsFeatureAvailable === false) { throw getAnnotationsFeatureUnavailableErrorMessage(); } - const { indexAnnotation } = annotationServiceProvider( - context.ml!.mlClient.callAsCurrentUser - ); + const { indexAnnotation } = annotationServiceProvider(context.ml!.mlClient); const currentUser = securityPlugin !== undefined ? securityPlugin.authc.getCurrentUser(request) : {}; - // @ts-ignore username doesn't exist on {} + // @ts-expect-error username doesn't exist on {} const username = currentUser?.username ?? ANNOTATION_USER_UNKNOWN; const resp = await indexAnnotation(request.body, username); @@ -143,16 +139,14 @@ export function annotationRoutes( mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable( - context.ml!.mlClient.callAsCurrentUser + context.ml!.mlClient ); if (annotationsFeatureAvailable === false) { throw getAnnotationsFeatureUnavailableErrorMessage(); } const annotationId = request.params.annotationId; - const { deleteAnnotation } = annotationServiceProvider( - context.ml!.mlClient.callAsCurrentUser - ); + const { deleteAnnotation } = annotationServiceProvider(context.ml!.mlClient); const resp = await deleteAnnotation(annotationId); return response.ok({ diff --git a/x-pack/plugins/ml/server/routes/anomaly_detectors.ts b/x-pack/plugins/ml/server/routes/anomaly_detectors.ts index 78e05c9a6d07b..8a59c174eb8e7 100644 --- a/x-pack/plugins/ml/server/routes/anomaly_detectors.ts +++ b/x-pack/plugins/ml/server/routes/anomaly_detectors.ts @@ -45,7 +45,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const results = await context.ml!.mlClient.callAsCurrentUser('ml.jobs'); + const results = await context.ml!.mlClient.callAsInternalUser('ml.jobs'); return response.ok({ body: results, }); @@ -77,7 +77,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const { jobId } = request.params; - const results = await context.ml!.mlClient.callAsCurrentUser('ml.jobs', { jobId }); + const results = await context.ml!.mlClient.callAsInternalUser('ml.jobs', { jobId }); return response.ok({ body: results, }); @@ -107,7 +107,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const results = await context.ml!.mlClient.callAsCurrentUser('ml.jobStats'); + const results = await context.ml!.mlClient.callAsInternalUser('ml.jobStats'); return response.ok({ body: results, }); @@ -139,7 +139,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const { jobId } = request.params; - const results = await context.ml!.mlClient.callAsCurrentUser('ml.jobStats', { jobId }); + const results = await context.ml!.mlClient.callAsInternalUser('ml.jobStats', { jobId }); return response.ok({ body: results, }); @@ -175,11 +175,9 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const { jobId } = request.params; - const body = request.body; - - const results = await context.ml!.mlClient.callAsCurrentUser('ml.addJob', { + const results = await context.ml!.mlClient.callAsInternalUser('ml.addJob', { jobId, - body, + body: request.body, }); return response.ok({ body: results, @@ -214,7 +212,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const { jobId } = request.params; - const results = await context.ml!.mlClient.callAsCurrentUser('ml.updateJob', { + const results = await context.ml!.mlClient.callAsInternalUser('ml.updateJob', { jobId, body: request.body, }); @@ -249,7 +247,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const { jobId } = request.params; - const results = await context.ml!.mlClient.callAsCurrentUser('ml.openJob', { + const results = await context.ml!.mlClient.callAsInternalUser('ml.openJob', { jobId, }); return response.ok({ @@ -289,7 +287,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { if (force !== undefined) { options.force = force; } - const results = await context.ml!.mlClient.callAsCurrentUser('ml.closeJob', options); + const results = await context.ml!.mlClient.callAsInternalUser('ml.closeJob', options); return response.ok({ body: results, }); @@ -327,7 +325,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { if (force !== undefined) { options.force = force; } - const results = await context.ml!.mlClient.callAsCurrentUser('ml.deleteJob', options); + const results = await context.ml!.mlClient.callAsInternalUser('ml.deleteJob', options); return response.ok({ body: results, }); @@ -356,7 +354,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const results = await context.ml!.mlClient.callAsCurrentUser('ml.validateDetector', { + const results = await context.ml!.mlClient.callAsInternalUser('ml.validateDetector', { body: request.body, }); return response.ok({ @@ -393,7 +391,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { try { const jobId = request.params.jobId; const duration = request.body.duration; - const results = await context.ml!.mlClient.callAsCurrentUser('ml.forecast', { + const results = await context.ml!.mlClient.callAsInternalUser('ml.forecast', { jobId, duration, }); @@ -432,7 +430,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const results = await context.ml!.mlClient.callAsCurrentUser('ml.records', { + const results = await context.ml!.mlClient.callAsInternalUser('ml.records', { jobId: request.params.jobId, body: request.body, }); @@ -471,7 +469,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const results = await context.ml!.mlClient.callAsCurrentUser('ml.buckets', { + const results = await context.ml!.mlClient.callAsInternalUser('ml.buckets', { jobId: request.params.jobId, timestamp: request.params.timestamp, body: request.body, @@ -511,7 +509,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const results = await context.ml!.mlClient.callAsCurrentUser('ml.overallBuckets', { + const results = await context.ml!.mlClient.callAsInternalUser('ml.overallBuckets', { jobId: request.params.jobId, top_n: request.body.topN, bucket_span: request.body.bucketSpan, @@ -548,7 +546,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const results = await context.ml!.mlClient.callAsCurrentUser('ml.categories', { + const results = await context.ml!.mlClient.callAsInternalUser('ml.categories', { jobId: request.params.jobId, categoryId: request.params.categoryId, }); @@ -582,7 +580,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const results = await context.ml!.mlClient.callAsCurrentUser('ml.modelSnapshots', { + const results = await context.ml!.mlClient.callAsInternalUser('ml.modelSnapshots', { jobId: request.params.jobId, }); return response.ok({ @@ -615,7 +613,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const results = await context.ml!.mlClient.callAsCurrentUser('ml.modelSnapshots', { + const results = await context.ml!.mlClient.callAsInternalUser('ml.modelSnapshots', { jobId: request.params.jobId, snapshotId: request.params.snapshotId, }); @@ -651,7 +649,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const results = await context.ml!.mlClient.callAsCurrentUser('ml.updateModelSnapshot', { + const results = await context.ml!.mlClient.callAsInternalUser('ml.updateModelSnapshot', { jobId: request.params.jobId, snapshotId: request.params.snapshotId, body: request.body, @@ -686,7 +684,7 @@ export function jobRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const results = await context.ml!.mlClient.callAsCurrentUser('ml.deleteModelSnapshot', { + const results = await context.ml!.mlClient.callAsInternalUser('ml.deleteModelSnapshot', { jobId: request.params.jobId, snapshotId: request.params.snapshotId, }); diff --git a/x-pack/plugins/ml/server/routes/calendars.ts b/x-pack/plugins/ml/server/routes/calendars.ts index 9c80651a13999..f5d129abd515e 100644 --- a/x-pack/plugins/ml/server/routes/calendars.ts +++ b/x-pack/plugins/ml/server/routes/calendars.ts @@ -11,32 +11,32 @@ import { calendarSchema, calendarIdSchema, calendarIdsSchema } from './schemas/c import { CalendarManager, Calendar, FormCalendar } from '../models/calendar'; function getAllCalendars(context: RequestHandlerContext) { - const cal = new CalendarManager(context.ml!.mlClient.callAsCurrentUser); + const cal = new CalendarManager(context.ml!.mlClient); return cal.getAllCalendars(); } function getCalendar(context: RequestHandlerContext, calendarId: string) { - const cal = new CalendarManager(context.ml!.mlClient.callAsCurrentUser); + const cal = new CalendarManager(context.ml!.mlClient); return cal.getCalendar(calendarId); } function newCalendar(context: RequestHandlerContext, calendar: FormCalendar) { - const cal = new CalendarManager(context.ml!.mlClient.callAsCurrentUser); + const cal = new CalendarManager(context.ml!.mlClient); return cal.newCalendar(calendar); } function updateCalendar(context: RequestHandlerContext, calendarId: string, calendar: Calendar) { - const cal = new CalendarManager(context.ml!.mlClient.callAsCurrentUser); + const cal = new CalendarManager(context.ml!.mlClient); return cal.updateCalendar(calendarId, calendar); } function deleteCalendar(context: RequestHandlerContext, calendarId: string) { - const cal = new CalendarManager(context.ml!.mlClient.callAsCurrentUser); + const cal = new CalendarManager(context.ml!.mlClient); return cal.deleteCalendar(calendarId); } function getCalendarsByIds(context: RequestHandlerContext, calendarIds: string) { - const cal = new CalendarManager(context.ml!.mlClient.callAsCurrentUser); + const cal = new CalendarManager(context.ml!.mlClient); return cal.getCalendarsByIds(calendarIds); } diff --git a/x-pack/plugins/ml/server/routes/data_frame_analytics.ts b/x-pack/plugins/ml/server/routes/data_frame_analytics.ts index 24be23332e4cf..3e6c6f5f6a2f8 100644 --- a/x-pack/plugins/ml/server/routes/data_frame_analytics.ts +++ b/x-pack/plugins/ml/server/routes/data_frame_analytics.ts @@ -19,6 +19,7 @@ import { } from './schemas/data_analytics_schema'; import { IndexPatternHandler } from '../models/data_frame_analytics/index_patterns'; import { DeleteDataFrameAnalyticsWithIndexStatus } from '../../common/types/data_frame_analytics'; +import { getAuthorizationHeader } from '../lib/request_authorization'; function getIndexPatternId(context: RequestHandlerContext, patternName: string) { const iph = new IndexPatternHandler(context.core.savedObjects.client); @@ -77,7 +78,7 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const results = await context.ml!.mlClient.callAsCurrentUser('ml.getDataFrameAnalytics'); + const results = await context.ml!.mlClient.callAsInternalUser('ml.getDataFrameAnalytics'); return response.ok({ body: results, }); @@ -109,7 +110,7 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const { analyticsId } = request.params; - const results = await context.ml!.mlClient.callAsCurrentUser('ml.getDataFrameAnalytics', { + const results = await context.ml!.mlClient.callAsInternalUser('ml.getDataFrameAnalytics', { analyticsId, }); return response.ok({ @@ -138,7 +139,7 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const results = await context.ml!.mlClient.callAsCurrentUser( + const results = await context.ml!.mlClient.callAsInternalUser( 'ml.getDataFrameAnalyticsStats' ); return response.ok({ @@ -172,7 +173,7 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const { analyticsId } = request.params; - const results = await context.ml!.mlClient.callAsCurrentUser( + const results = await context.ml!.mlClient.callAsInternalUser( 'ml.getDataFrameAnalyticsStats', { analyticsId, @@ -212,11 +213,12 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const { analyticsId } = request.params; - const results = await context.ml!.mlClient.callAsCurrentUser( + const results = await context.ml!.mlClient.callAsInternalUser( 'ml.createDataFrameAnalytics', { body: request.body, analyticsId, + ...getAuthorizationHeader(request), } ); return response.ok({ @@ -249,10 +251,11 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const results = await context.ml!.mlClient.callAsCurrentUser( + const results = await context.ml!.mlClient.callAsInternalUser( 'ml.evaluateDataFrameAnalytics', { body: request.body, + ...getAuthorizationHeader(request), } ); return response.ok({ @@ -286,7 +289,7 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const results = await context.ml!.mlClient.callAsCurrentUser( + const results = await context.ml!.mlClient.callAsInternalUser( 'ml.explainDataFrameAnalytics', { body: request.body, @@ -335,7 +338,7 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat // Check if analyticsId is valid and get destination index if (deleteDestIndex || deleteDestIndexPattern) { try { - const dfa = await context.ml!.mlClient.callAsCurrentUser('ml.getDataFrameAnalytics', { + const dfa = await context.ml!.mlClient.callAsInternalUser('ml.getDataFrameAnalytics', { analyticsId, }); if (Array.isArray(dfa.data_frame_analytics) && dfa.data_frame_analytics.length > 0) { @@ -381,7 +384,7 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat // Delete the data frame analytics try { - await context.ml!.mlClient.callAsCurrentUser('ml.deleteDataFrameAnalytics', { + await context.ml!.mlClient.callAsInternalUser('ml.deleteDataFrameAnalytics', { analyticsId, }); analyticsJobDeleted.success = true; @@ -427,9 +430,12 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const { analyticsId } = request.params; - const results = await context.ml!.mlClient.callAsCurrentUser('ml.startDataFrameAnalytics', { - analyticsId, - }); + const results = await context.ml!.mlClient.callAsInternalUser( + 'ml.startDataFrameAnalytics', + { + analyticsId, + } + ); return response.ok({ body: results, }); @@ -465,13 +471,13 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat const options: { analyticsId: string; force?: boolean | undefined } = { analyticsId: request.params.analyticsId, }; - // @ts-ignore TODO: update types + // @ts-expect-error TODO: update types if (request.url?.query?.force !== undefined) { - // @ts-ignore TODO: update types + // @ts-expect-error TODO: update types options.force = request.url.query.force; } - const results = await context.ml!.mlClient.callAsCurrentUser( + const results = await context.ml!.mlClient.callAsInternalUser( 'ml.stopDataFrameAnalytics', options ); @@ -545,9 +551,7 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const { analyticsId } = request.params; - const { getAnalyticsAuditMessages } = analyticsAuditMessagesProvider( - context.ml!.mlClient.callAsCurrentUser - ); + const { getAnalyticsAuditMessages } = analyticsAuditMessagesProvider(context.ml!.mlClient); const results = await getAnalyticsAuditMessages(analyticsId); return response.ok({ diff --git a/x-pack/plugins/ml/server/routes/data_visualizer.ts b/x-pack/plugins/ml/server/routes/data_visualizer.ts index 9dd010e105b6e..818e981835ced 100644 --- a/x-pack/plugins/ml/server/routes/data_visualizer.ts +++ b/x-pack/plugins/ml/server/routes/data_visualizer.ts @@ -27,7 +27,7 @@ function getOverallStats( earliestMs: number, latestMs: number ) { - const dv = new DataVisualizer(context.ml!.mlClient.callAsCurrentUser); + const dv = new DataVisualizer(context.ml!.mlClient); return dv.getOverallStats( indexPatternTitle, query, @@ -52,7 +52,7 @@ function getStatsForFields( interval: number, maxExamples: number ) { - const dv = new DataVisualizer(context.ml!.mlClient.callAsCurrentUser); + const dv = new DataVisualizer(context.ml!.mlClient); return dv.getStatsForFields( indexPatternTitle, query, @@ -73,7 +73,7 @@ function getHistogramsForFields( fields: HistogramField[], samplerShardSize: number ) { - const dv = new DataVisualizer(context.ml!.mlClient.callAsCurrentUser); + const dv = new DataVisualizer(context.ml!.mlClient); return dv.getHistogramsForFields(indexPatternTitle, query, fields, samplerShardSize); } diff --git a/x-pack/plugins/ml/server/routes/datafeeds.ts b/x-pack/plugins/ml/server/routes/datafeeds.ts index 1fa1d408372da..855b64b0ffed0 100644 --- a/x-pack/plugins/ml/server/routes/datafeeds.ts +++ b/x-pack/plugins/ml/server/routes/datafeeds.ts @@ -12,6 +12,7 @@ import { datafeedIdSchema, deleteDatafeedQuerySchema, } from './schemas/datafeeds_schema'; +import { getAuthorizationHeader } from '../lib/request_authorization'; /** * Routes for datafeed service @@ -34,7 +35,7 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const resp = await context.ml!.mlClient.callAsCurrentUser('ml.datafeeds'); + const resp = await context.ml!.mlClient.callAsInternalUser('ml.datafeeds'); return response.ok({ body: resp, @@ -67,7 +68,7 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) { mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const datafeedId = request.params.datafeedId; - const resp = await context.ml!.mlClient.callAsCurrentUser('ml.datafeeds', { datafeedId }); + const resp = await context.ml!.mlClient.callAsInternalUser('ml.datafeeds', { datafeedId }); return response.ok({ body: resp, @@ -95,7 +96,7 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const resp = await context.ml!.mlClient.callAsCurrentUser('ml.datafeedStats'); + const resp = await context.ml!.mlClient.callAsInternalUser('ml.datafeedStats'); return response.ok({ body: resp, @@ -128,7 +129,7 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) { mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const datafeedId = request.params.datafeedId; - const resp = await context.ml!.mlClient.callAsCurrentUser('ml.datafeedStats', { + const resp = await context.ml!.mlClient.callAsInternalUser('ml.datafeedStats', { datafeedId, }); @@ -165,9 +166,10 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) { mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const datafeedId = request.params.datafeedId; - const resp = await context.ml!.mlClient.callAsCurrentUser('ml.addDatafeed', { + const resp = await context.ml!.mlClient.callAsInternalUser('ml.addDatafeed', { datafeedId, body: request.body, + ...getAuthorizationHeader(request), }); return response.ok({ @@ -203,9 +205,10 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) { mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const datafeedId = request.params.datafeedId; - const resp = await context.ml!.mlClient.callAsCurrentUser('ml.updateDatafeed', { + const resp = await context.ml!.mlClient.callAsInternalUser('ml.updateDatafeed', { datafeedId, body: request.body, + ...getAuthorizationHeader(request), }); return response.ok({ @@ -248,7 +251,7 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) { options.force = force; } - const resp = await context.ml!.mlClient.callAsCurrentUser('ml.deleteDatafeed', options); + const resp = await context.ml!.mlClient.callAsInternalUser('ml.deleteDatafeed', options); return response.ok({ body: resp, @@ -285,7 +288,7 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) { const datafeedId = request.params.datafeedId; const { start, end } = request.body; - const resp = await context.ml!.mlClient.callAsCurrentUser('ml.startDatafeed', { + const resp = await context.ml!.mlClient.callAsInternalUser('ml.startDatafeed', { datafeedId, start, end, @@ -323,7 +326,7 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) { try { const datafeedId = request.params.datafeedId; - const resp = await context.ml!.mlClient.callAsCurrentUser('ml.stopDatafeed', { + const resp = await context.ml!.mlClient.callAsInternalUser('ml.stopDatafeed', { datafeedId, }); @@ -358,8 +361,9 @@ export function dataFeedRoutes({ router, mlLicense }: RouteInitialization) { mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const datafeedId = request.params.datafeedId; - const resp = await context.ml!.mlClient.callAsCurrentUser('ml.datafeedPreview', { + const resp = await context.ml!.mlClient.callAsInternalUser('ml.datafeedPreview', { datafeedId, + ...getAuthorizationHeader(request), }); return response.ok({ diff --git a/x-pack/plugins/ml/server/routes/fields_service.ts b/x-pack/plugins/ml/server/routes/fields_service.ts index b0f13df294145..b83f846b1685d 100644 --- a/x-pack/plugins/ml/server/routes/fields_service.ts +++ b/x-pack/plugins/ml/server/routes/fields_service.ts @@ -14,13 +14,13 @@ import { import { fieldsServiceProvider } from '../models/fields_service'; function getCardinalityOfFields(context: RequestHandlerContext, payload: any) { - const fs = fieldsServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const fs = fieldsServiceProvider(context.ml!.mlClient); const { index, fieldNames, query, timeFieldName, earliestMs, latestMs } = payload; return fs.getCardinalityOfFields(index, fieldNames, query, timeFieldName, earliestMs, latestMs); } function getTimeFieldRange(context: RequestHandlerContext, payload: any) { - const fs = fieldsServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const fs = fieldsServiceProvider(context.ml!.mlClient); const { index, timeFieldName, query } = payload; return fs.getTimeFieldRange(index, timeFieldName, query); } diff --git a/x-pack/plugins/ml/server/routes/file_data_visualizer.ts b/x-pack/plugins/ml/server/routes/file_data_visualizer.ts index 0f389f9505943..b57eda5ad56a1 100644 --- a/x-pack/plugins/ml/server/routes/file_data_visualizer.ts +++ b/x-pack/plugins/ml/server/routes/file_data_visualizer.ts @@ -29,7 +29,7 @@ import { } from './schemas/file_data_visualizer_schema'; function analyzeFiles(context: RequestHandlerContext, data: InputData, overrides: InputOverrides) { - const { analyzeFile } = fileDataVisualizerProvider(context.ml!.mlClient.callAsCurrentUser); + const { analyzeFile } = fileDataVisualizerProvider(context.ml!.mlClient); return analyzeFile(data, overrides); } @@ -42,7 +42,7 @@ function importData( ingestPipeline: IngestPipelineWrapper, data: InputData ) { - const { importData: importDataFunc } = importDataProvider(context.ml!.mlClient.callAsCurrentUser); + const { importData: importDataFunc } = importDataProvider(context.ml!.mlClient); return importDataFunc(id, index, settings, mappings, ingestPipeline, data); } diff --git a/x-pack/plugins/ml/server/routes/filters.ts b/x-pack/plugins/ml/server/routes/filters.ts index d5287c349a8fc..dcdb4caa6cd3b 100644 --- a/x-pack/plugins/ml/server/routes/filters.ts +++ b/x-pack/plugins/ml/server/routes/filters.ts @@ -13,32 +13,32 @@ import { FilterManager, FormFilter } from '../models/filter'; // TODO - add function for returning a list of just the filter IDs. // TODO - add function for returning a list of filter IDs plus item count. function getAllFilters(context: RequestHandlerContext) { - const mgr = new FilterManager(context.ml!.mlClient.callAsCurrentUser); + const mgr = new FilterManager(context.ml!.mlClient); return mgr.getAllFilters(); } function getAllFilterStats(context: RequestHandlerContext) { - const mgr = new FilterManager(context.ml!.mlClient.callAsCurrentUser); + const mgr = new FilterManager(context.ml!.mlClient); return mgr.getAllFilterStats(); } function getFilter(context: RequestHandlerContext, filterId: string) { - const mgr = new FilterManager(context.ml!.mlClient.callAsCurrentUser); + const mgr = new FilterManager(context.ml!.mlClient); return mgr.getFilter(filterId); } function newFilter(context: RequestHandlerContext, filter: FormFilter) { - const mgr = new FilterManager(context.ml!.mlClient.callAsCurrentUser); + const mgr = new FilterManager(context.ml!.mlClient); return mgr.newFilter(filter); } function updateFilter(context: RequestHandlerContext, filterId: string, filter: FormFilter) { - const mgr = new FilterManager(context.ml!.mlClient.callAsCurrentUser); + const mgr = new FilterManager(context.ml!.mlClient); return mgr.updateFilter(filterId, filter); } function deleteFilter(context: RequestHandlerContext, filterId: string) { - const mgr = new FilterManager(context.ml!.mlClient.callAsCurrentUser); + const mgr = new FilterManager(context.ml!.mlClient); return mgr.deleteFilter(filterId); } diff --git a/x-pack/plugins/ml/server/routes/job_audit_messages.ts b/x-pack/plugins/ml/server/routes/job_audit_messages.ts index 5acc89e7d13be..d4840ed650a32 100644 --- a/x-pack/plugins/ml/server/routes/job_audit_messages.ts +++ b/x-pack/plugins/ml/server/routes/job_audit_messages.ts @@ -39,9 +39,7 @@ export function jobAuditMessagesRoutes({ router, mlLicense }: RouteInitializatio }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { getJobAuditMessages } = jobAuditMessagesProvider( - context.ml!.mlClient.callAsCurrentUser - ); + const { getJobAuditMessages } = jobAuditMessagesProvider(context.ml!.mlClient); const { jobId } = request.params; const { from } = request.query; const resp = await getJobAuditMessages(jobId, from); @@ -76,9 +74,7 @@ export function jobAuditMessagesRoutes({ router, mlLicense }: RouteInitializatio }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { getJobAuditMessages } = jobAuditMessagesProvider( - context.ml!.mlClient.callAsCurrentUser - ); + const { getJobAuditMessages } = jobAuditMessagesProvider(context.ml!.mlClient); const { from } = request.query; const resp = await getJobAuditMessages(undefined, from); diff --git a/x-pack/plugins/ml/server/routes/job_service.ts b/x-pack/plugins/ml/server/routes/job_service.ts index 10d1c9952b540..e03dbb40d623a 100644 --- a/x-pack/plugins/ml/server/routes/job_service.ts +++ b/x-pack/plugins/ml/server/routes/job_service.ts @@ -50,7 +50,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { forceStartDatafeeds } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { forceStartDatafeeds } = jobServiceProvider(context.ml!.mlClient); const { datafeedIds, start, end } = request.body; const resp = await forceStartDatafeeds(datafeedIds, start, end); @@ -84,7 +84,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { stopDatafeeds } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { stopDatafeeds } = jobServiceProvider(context.ml!.mlClient); const { datafeedIds } = request.body; const resp = await stopDatafeeds(datafeedIds); @@ -118,7 +118,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { deleteJobs } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { deleteJobs } = jobServiceProvider(context.ml!.mlClient); const { jobIds } = request.body; const resp = await deleteJobs(jobIds); @@ -152,7 +152,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { closeJobs } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { closeJobs } = jobServiceProvider(context.ml!.mlClient); const { jobIds } = request.body; const resp = await closeJobs(jobIds); @@ -186,7 +186,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { forceStopAndCloseJob } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { forceStopAndCloseJob } = jobServiceProvider(context.ml!.mlClient); const { jobId } = request.body; const resp = await forceStopAndCloseJob(jobId); @@ -225,7 +225,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { jobsSummary } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { jobsSummary } = jobServiceProvider(context.ml!.mlClient); const { jobIds } = request.body; const resp = await jobsSummary(jobIds); @@ -259,7 +259,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { jobsWithTimerange } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { jobsWithTimerange } = jobServiceProvider(context.ml!.mlClient); const resp = await jobsWithTimerange(); return response.ok({ @@ -292,7 +292,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { createFullJobsList } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { createFullJobsList } = jobServiceProvider(context.ml!.mlClient); const { jobIds } = request.body; const resp = await createFullJobsList(jobIds); @@ -322,7 +322,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { getAllGroups } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { getAllGroups } = jobServiceProvider(context.ml!.mlClient); const resp = await getAllGroups(); return response.ok({ @@ -355,7 +355,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { updateGroups } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { updateGroups } = jobServiceProvider(context.ml!.mlClient); const { jobs } = request.body; const resp = await updateGroups(jobs); @@ -385,7 +385,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { deletingJobTasks } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { deletingJobTasks } = jobServiceProvider(context.ml!.mlClient); const resp = await deletingJobTasks(); return response.ok({ @@ -418,7 +418,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { jobsExist } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { jobsExist } = jobServiceProvider(context.ml!.mlClient); const { jobIds } = request.body; const resp = await jobsExist(jobIds); @@ -454,7 +454,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { const { indexPattern } = request.params; const isRollup = request.query.rollup === 'true'; const savedObjectsClient = context.core.savedObjects.client; - const { newJobCaps } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { newJobCaps } = jobServiceProvider(context.ml!.mlClient); const resp = await newJobCaps(indexPattern, isRollup, savedObjectsClient); return response.ok({ @@ -499,7 +499,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { splitFieldValue, } = request.body; - const { newJobLineChart } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { newJobLineChart } = jobServiceProvider(context.ml!.mlClient); const resp = await newJobLineChart( indexPatternTitle, timeField, @@ -553,9 +553,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { splitFieldName, } = request.body; - const { newJobPopulationChart } = jobServiceProvider( - context.ml!.mlClient.callAsCurrentUser - ); + const { newJobPopulationChart } = jobServiceProvider(context.ml!.mlClient); const resp = await newJobPopulationChart( indexPatternTitle, timeField, @@ -593,7 +591,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { getAllJobAndGroupIds } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { getAllJobAndGroupIds } = jobServiceProvider(context.ml!.mlClient); const resp = await getAllJobAndGroupIds(); return response.ok({ @@ -626,7 +624,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { getLookBackProgress } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { getLookBackProgress } = jobServiceProvider(context.ml!.mlClient); const { jobId, start, end } = request.body; const resp = await getLookBackProgress(jobId, start, end); @@ -660,10 +658,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { validateCategoryExamples } = categorizationExamplesProvider( - context.ml!.mlClient.callAsCurrentUser, - context.ml!.mlClient.callAsInternalUser - ); + const { validateCategoryExamples } = categorizationExamplesProvider(context.ml!.mlClient); const { indexPatternTitle, timeField, @@ -716,7 +711,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { topCategories } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { topCategories } = jobServiceProvider(context.ml!.mlClient); const { jobId, count } = request.body; const resp = await topCategories(jobId, count); @@ -750,7 +745,7 @@ export function jobServiceRoutes({ router, mlLicense }: RouteInitialization) { }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const { revertModelSnapshot } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const { revertModelSnapshot } = jobServiceProvider(context.ml!.mlClient); const { jobId, snapshotId, diff --git a/x-pack/plugins/ml/server/routes/job_validation.ts b/x-pack/plugins/ml/server/routes/job_validation.ts index 0af8141a2a641..e52c6b76e918b 100644 --- a/x-pack/plugins/ml/server/routes/job_validation.ts +++ b/x-pack/plugins/ml/server/routes/job_validation.ts @@ -32,7 +32,7 @@ export function jobValidationRoutes({ router, mlLicense }: RouteInitialization, ) { const { analysisConfig, indexPattern, query, timeFieldName, earliestMs, latestMs } = payload; - return calculateModelMemoryLimitProvider(context.ml!.mlClient.callAsCurrentUser)( + return calculateModelMemoryLimitProvider(context.ml!.mlClient)( analysisConfig as AnalysisConfig, indexPattern, query, @@ -64,11 +64,7 @@ export function jobValidationRoutes({ router, mlLicense }: RouteInitialization, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { let errorResp; - const resp = await estimateBucketSpanFactory( - context.ml!.mlClient.callAsCurrentUser, - context.ml!.mlClient.callAsInternalUser, - mlLicense.isSecurityEnabled() === false - )(request.body) + const resp = await estimateBucketSpanFactory(context.ml!.mlClient)(request.body) // this catch gets triggered when the estimation code runs without error // but isn't able to come up with a bucket span estimation. // this doesn't return a HTTP error but an object with an error message @@ -147,10 +143,7 @@ export function jobValidationRoutes({ router, mlLicense }: RouteInitialization, }, mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { - const resp = await validateCardinality( - context.ml!.mlClient.callAsCurrentUser, - request.body - ); + const resp = await validateCardinality(context.ml!.mlClient, request.body); return response.ok({ body: resp, @@ -184,10 +177,9 @@ export function jobValidationRoutes({ router, mlLicense }: RouteInitialization, try { // version corresponds to the version used in documentation links. const resp = await validateJob( - context.ml!.mlClient.callAsCurrentUser, + context.ml!.mlClient, request.body, version, - context.ml!.mlClient.callAsInternalUser, mlLicense.isSecurityEnabled() === false ); diff --git a/x-pack/plugins/ml/server/routes/modules.ts b/x-pack/plugins/ml/server/routes/modules.ts index 88d24a1b86b6d..463babb86304f 100644 --- a/x-pack/plugins/ml/server/routes/modules.ts +++ b/x-pack/plugins/ml/server/routes/modules.ts @@ -6,7 +6,7 @@ import { TypeOf } from '@kbn/config-schema'; -import { RequestHandlerContext } from 'kibana/server'; +import { RequestHandlerContext, KibanaRequest } from 'kibana/server'; import { DatafeedOverride, JobOverride } from '../../common/types/modules'; import { wrapError } from '../client/error_wrapper'; import { DataRecognizer } from '../models/data_recognizer'; @@ -18,19 +18,17 @@ import { } from './schemas/modules'; import { RouteInitialization } from '../types'; -function recognize(context: RequestHandlerContext, indexPatternTitle: string) { - const dr = new DataRecognizer( - context.ml!.mlClient.callAsCurrentUser, - context.core.savedObjects.client - ); +function recognize( + context: RequestHandlerContext, + request: KibanaRequest, + indexPatternTitle: string +) { + const dr = new DataRecognizer(context.ml!.mlClient, context.core.savedObjects.client, request); return dr.findMatches(indexPatternTitle); } -function getModule(context: RequestHandlerContext, moduleId: string) { - const dr = new DataRecognizer( - context.ml!.mlClient.callAsCurrentUser, - context.core.savedObjects.client - ); +function getModule(context: RequestHandlerContext, request: KibanaRequest, moduleId: string) { + const dr = new DataRecognizer(context.ml!.mlClient, context.core.savedObjects.client, request); if (moduleId === undefined) { return dr.listModules(); } else { @@ -40,6 +38,7 @@ function getModule(context: RequestHandlerContext, moduleId: string) { function setup( context: RequestHandlerContext, + request: KibanaRequest, moduleId: string, prefix?: string, groups?: string[], @@ -53,10 +52,7 @@ function setup( datafeedOverrides?: DatafeedOverride | DatafeedOverride[], estimateModelMemory?: boolean ) { - const dr = new DataRecognizer( - context.ml!.mlClient.callAsCurrentUser, - context.core.savedObjects.client - ); + const dr = new DataRecognizer(context.ml!.mlClient, context.core.savedObjects.client, request); return dr.setup( moduleId, prefix, @@ -73,11 +69,12 @@ function setup( ); } -function dataRecognizerJobsExist(context: RequestHandlerContext, moduleId: string) { - const dr = new DataRecognizer( - context.ml!.mlClient.callAsCurrentUser, - context.core.savedObjects.client - ); +function dataRecognizerJobsExist( + context: RequestHandlerContext, + request: KibanaRequest, + moduleId: string +) { + const dr = new DataRecognizer(context.ml!.mlClient, context.core.savedObjects.client, request); return dr.dataRecognizerJobsExist(moduleId); } @@ -125,7 +122,7 @@ export function dataRecognizer({ router, mlLicense }: RouteInitialization) { mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const { indexPatternTitle } = request.params; - const results = await recognize(context, indexPatternTitle); + const results = await recognize(context, request, indexPatternTitle); return response.ok({ body: results }); } catch (e) { @@ -260,7 +257,7 @@ export function dataRecognizer({ router, mlLicense }: RouteInitialization) { // the moduleId will be an empty string. moduleId = undefined; } - const results = await getModule(context, moduleId); + const results = await getModule(context, request, moduleId); return response.ok({ body: results }); } catch (e) { @@ -440,6 +437,7 @@ export function dataRecognizer({ router, mlLicense }: RouteInitialization) { const result = await setup( context, + request, moduleId, prefix, groups, @@ -526,7 +524,7 @@ export function dataRecognizer({ router, mlLicense }: RouteInitialization) { mlLicense.fullLicenseAPIGuard(async (context, request, response) => { try { const { moduleId } = request.params; - const result = await dataRecognizerJobsExist(context, moduleId); + const result = await dataRecognizerJobsExist(context, request, moduleId); return response.ok({ body: result }); } catch (e) { diff --git a/x-pack/plugins/ml/server/routes/results_service.ts b/x-pack/plugins/ml/server/routes/results_service.ts index 94ca0827ccfa5..c7fcebd2a29a5 100644 --- a/x-pack/plugins/ml/server/routes/results_service.ts +++ b/x-pack/plugins/ml/server/routes/results_service.ts @@ -17,7 +17,7 @@ import { import { resultsServiceProvider } from '../models/results_service'; function getAnomaliesTableData(context: RequestHandlerContext, payload: any) { - const rs = resultsServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const rs = resultsServiceProvider(context.ml!.mlClient); const { jobIds, criteriaFields, @@ -47,24 +47,24 @@ function getAnomaliesTableData(context: RequestHandlerContext, payload: any) { } function getCategoryDefinition(context: RequestHandlerContext, payload: any) { - const rs = resultsServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const rs = resultsServiceProvider(context.ml!.mlClient); return rs.getCategoryDefinition(payload.jobId, payload.categoryId); } function getCategoryExamples(context: RequestHandlerContext, payload: any) { - const rs = resultsServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const rs = resultsServiceProvider(context.ml!.mlClient); const { jobId, categoryIds, maxExamples } = payload; return rs.getCategoryExamples(jobId, categoryIds, maxExamples); } function getMaxAnomalyScore(context: RequestHandlerContext, payload: any) { - const rs = resultsServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const rs = resultsServiceProvider(context.ml!.mlClient); const { jobIds, earliestMs, latestMs } = payload; return rs.getMaxAnomalyScore(jobIds, earliestMs, latestMs); } function getPartitionFieldsValues(context: RequestHandlerContext, payload: any) { - const rs = resultsServiceProvider(context.ml!.mlClient.callAsCurrentUser); + const rs = resultsServiceProvider(context.ml!.mlClient); const { jobId, searchTerm, criteriaFields, earliestMs, latestMs } = payload; return rs.getPartitionFieldsValues(jobId, searchTerm, criteriaFields, earliestMs, latestMs); } diff --git a/x-pack/plugins/ml/server/routes/schemas/annotations_schema.ts b/x-pack/plugins/ml/server/routes/schemas/annotations_schema.ts index fade2093ac842..14a2f632419bc 100644 --- a/x-pack/plugins/ml/server/routes/schemas/annotations_schema.ts +++ b/x-pack/plugins/ml/server/routes/schemas/annotations_schema.ts @@ -16,6 +16,14 @@ export const indexAnnotationSchema = schema.object({ create_username: schema.maybe(schema.string()), modified_time: schema.maybe(schema.number()), modified_username: schema.maybe(schema.string()), + event: schema.maybe(schema.string()), + detector_index: schema.maybe(schema.number()), + partition_field_name: schema.maybe(schema.string()), + partition_field_value: schema.maybe(schema.string()), + over_field_name: schema.maybe(schema.string()), + over_field_value: schema.maybe(schema.string()), + by_field_name: schema.maybe(schema.string()), + by_field_value: schema.maybe(schema.string()), /** Document id */ _id: schema.maybe(schema.string()), key: schema.maybe(schema.string()), @@ -26,6 +34,25 @@ export const getAnnotationsSchema = schema.object({ earliestMs: schema.oneOf([schema.nullable(schema.number()), schema.maybe(schema.number())]), latestMs: schema.oneOf([schema.nullable(schema.number()), schema.maybe(schema.number())]), maxAnnotations: schema.number(), + /** Fields to find unique values for (e.g. events or created_by) */ + fields: schema.maybe( + schema.arrayOf( + schema.object({ + field: schema.string(), + missing: schema.maybe(schema.string()), + }) + ) + ), + detectorIndex: schema.maybe(schema.number()), + entities: schema.maybe( + schema.arrayOf( + schema.object({ + fieldType: schema.maybe(schema.string()), + fieldName: schema.maybe(schema.string()), + fieldValue: schema.maybe(schema.string()), + }) + ) + ), }); export const deleteAnnotationSchema = schema.object({ annotationId: schema.string() }); diff --git a/x-pack/plugins/ml/server/routes/system.ts b/x-pack/plugins/ml/server/routes/system.ts index d78c1cf3aa6af..410d540ecb8f7 100644 --- a/x-pack/plugins/ml/server/routes/system.ts +++ b/x-pack/plugins/ml/server/routes/system.ts @@ -60,9 +60,10 @@ export function systemRoutes( }, mlLicense.basicLicenseAPIGuard(async (context, request, response) => { try { + const { callAsCurrentUser, callAsInternalUser } = context.ml!.mlClient; let upgradeInProgress = false; try { - const info = await context.ml!.mlClient.callAsCurrentUser('ml.info'); + const info = await callAsInternalUser('ml.info'); // if ml indices are currently being migrated, upgrade_mode will be set to true // pass this back with the privileges to allow for the disabling of UI controls. upgradeInProgress = info.upgrade_mode === true; @@ -90,7 +91,7 @@ export function systemRoutes( }); } else { const body = request.body; - const resp = await context.ml!.mlClient.callAsCurrentUser('ml.privilegeCheck', { body }); + const resp = await callAsCurrentUser('ml.privilegeCheck', { body }); resp.upgradeInProgress = upgradeInProgress; return response.ok({ body: resp, @@ -128,7 +129,7 @@ export function systemRoutes( } const { getCapabilities } = capabilitiesProvider( - context.ml!.mlClient.callAsCurrentUser, + context.ml!.mlClient, mlCapabilities, mlLicense, isMlEnabledInSpace @@ -154,43 +155,15 @@ export function systemRoutes( path: '/api/ml/ml_node_count', validate: false, options: { - tags: ['access:ml:canGetJobs'], + tags: ['access:ml:canGetJobs', 'access:ml:canGetDatafeeds'], }, }, mlLicense.basicLicenseAPIGuard(async (context, request, response) => { try { - // check for basic license first for consistency with other - // security disabled checks - if (mlLicense.isSecurityEnabled() === false) { - return response.ok({ - body: await getNodeCount(context), - }); - } else { - // if security is enabled, check that the user has permission to - // view jobs before calling getNodeCount. - // getNodeCount calls the _nodes endpoint as the internal user - // and so could give the user access to more information than - // they are entitled to. - const requiredPrivileges = [ - 'cluster:monitor/xpack/ml/job/get', - 'cluster:monitor/xpack/ml/job/stats/get', - 'cluster:monitor/xpack/ml/datafeeds/get', - 'cluster:monitor/xpack/ml/datafeeds/stats/get', - ]; - const body = { cluster: requiredPrivileges }; - const resp = await context.ml!.mlClient.callAsCurrentUser('ml.privilegeCheck', { body }); - - if (resp.has_all_requested) { - return response.ok({ - body: await getNodeCount(context), - }); - } else { - // if the user doesn't have permission to create jobs - // return a 403 - return response.forbidden(); - } - } + return response.ok({ + body: await getNodeCount(context), + }); } catch (e) { return response.customError(wrapError(e)); } @@ -214,7 +187,7 @@ export function systemRoutes( }, mlLicense.basicLicenseAPIGuard(async (context, request, response) => { try { - const info = await context.ml!.mlClient.callAsCurrentUser('ml.info'); + const info = await context.ml!.mlClient.callAsInternalUser('ml.info'); const cloudId = cloud && cloud.cloudId; return response.ok({ body: { ...info, cloudId }, diff --git a/x-pack/plugins/ml/server/shared_services/providers/anomaly_detectors.ts b/x-pack/plugins/ml/server/shared_services/providers/anomaly_detectors.ts index 3ae05152ae630..1140af0b76404 100644 --- a/x-pack/plugins/ml/server/shared_services/providers/anomaly_detectors.ts +++ b/x-pack/plugins/ml/server/shared_services/providers/anomaly_detectors.ts @@ -4,13 +4,13 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller, KibanaRequest } from 'kibana/server'; +import { ILegacyScopedClusterClient, KibanaRequest } from 'kibana/server'; import { Job } from '../../../common/types/anomaly_detection_jobs'; import { SharedServicesChecks } from '../shared_services'; export interface AnomalyDetectorsProvider { anomalyDetectorsProvider( - callAsCurrentUser: LegacyAPICaller, + mlClusterClient: ILegacyScopedClusterClient, request: KibanaRequest ): { jobs(jobId?: string): Promise<{ count: number; jobs: Job[] }>; @@ -22,13 +22,16 @@ export function getAnomalyDetectorsProvider({ getHasMlCapabilities, }: SharedServicesChecks): AnomalyDetectorsProvider { return { - anomalyDetectorsProvider(callAsCurrentUser: LegacyAPICaller, request: KibanaRequest) { + anomalyDetectorsProvider(mlClusterClient: ILegacyScopedClusterClient, request: KibanaRequest) { const hasMlCapabilities = getHasMlCapabilities(request); return { async jobs(jobId?: string) { isFullLicense(); await hasMlCapabilities(['canGetJobs']); - return callAsCurrentUser('ml.jobs', jobId !== undefined ? { jobId } : {}); + return mlClusterClient.callAsInternalUser( + 'ml.jobs', + jobId !== undefined ? { jobId } : {} + ); }, }; }, diff --git a/x-pack/plugins/ml/server/shared_services/providers/job_service.ts b/x-pack/plugins/ml/server/shared_services/providers/job_service.ts index e5a42090163f8..c734dcc1583a1 100644 --- a/x-pack/plugins/ml/server/shared_services/providers/job_service.ts +++ b/x-pack/plugins/ml/server/shared_services/providers/job_service.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller, KibanaRequest } from 'kibana/server'; +import { ILegacyScopedClusterClient, KibanaRequest } from 'kibana/server'; import { jobServiceProvider } from '../../models/job_service'; import { SharedServicesChecks } from '../shared_services'; @@ -12,7 +12,7 @@ type OrigJobServiceProvider = ReturnType; export interface JobServiceProvider { jobServiceProvider( - callAsCurrentUser: LegacyAPICaller, + mlClusterClient: ILegacyScopedClusterClient, request: KibanaRequest ): { jobsSummary: OrigJobServiceProvider['jobsSummary']; @@ -24,9 +24,9 @@ export function getJobServiceProvider({ getHasMlCapabilities, }: SharedServicesChecks): JobServiceProvider { return { - jobServiceProvider(callAsCurrentUser: LegacyAPICaller, request: KibanaRequest) { + jobServiceProvider(mlClusterClient: ILegacyScopedClusterClient, request: KibanaRequest) { // const hasMlCapabilities = getHasMlCapabilities(request); - const { jobsSummary } = jobServiceProvider(callAsCurrentUser); + const { jobsSummary } = jobServiceProvider(mlClusterClient); return { async jobsSummary(...args) { isFullLicense(); diff --git a/x-pack/plugins/ml/server/shared_services/providers/modules.ts b/x-pack/plugins/ml/server/shared_services/providers/modules.ts index 27935fd6fe21d..fb7d59f9c8218 100644 --- a/x-pack/plugins/ml/server/shared_services/providers/modules.ts +++ b/x-pack/plugins/ml/server/shared_services/providers/modules.ts @@ -4,18 +4,23 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller, KibanaRequest, SavedObjectsClientContract } from 'kibana/server'; +import { + ILegacyScopedClusterClient, + KibanaRequest, + SavedObjectsClientContract, +} from 'kibana/server'; import { TypeOf } from '@kbn/config-schema'; import { DataRecognizer } from '../../models/data_recognizer'; import { SharedServicesChecks } from '../shared_services'; import { moduleIdParamSchema, setupModuleBodySchema } from '../../routes/schemas/modules'; +import { HasMlCapabilities } from '../../lib/capabilities'; export type ModuleSetupPayload = TypeOf & TypeOf; export interface ModulesProvider { modulesProvider( - callAsCurrentUser: LegacyAPICaller, + mlClusterClient: ILegacyScopedClusterClient, request: KibanaRequest, savedObjectsClient: SavedObjectsClientContract ): { @@ -32,12 +37,18 @@ export function getModulesProvider({ }: SharedServicesChecks): ModulesProvider { return { modulesProvider( - callAsCurrentUser: LegacyAPICaller, + mlClusterClient: ILegacyScopedClusterClient, request: KibanaRequest, savedObjectsClient: SavedObjectsClientContract ) { - const hasMlCapabilities = getHasMlCapabilities(request); - const dr = dataRecognizerFactory(callAsCurrentUser, savedObjectsClient); + let hasMlCapabilities: HasMlCapabilities; + if (request.params === 'DummyKibanaRequest') { + hasMlCapabilities = () => Promise.resolve(); + } else { + hasMlCapabilities = getHasMlCapabilities(request); + } + const dr = dataRecognizerFactory(mlClusterClient, savedObjectsClient, request); + return { async recognize(...args) { isFullLicense(); @@ -82,8 +93,9 @@ export function getModulesProvider({ } function dataRecognizerFactory( - callAsCurrentUser: LegacyAPICaller, - savedObjectsClient: SavedObjectsClientContract + mlClusterClient: ILegacyScopedClusterClient, + savedObjectsClient: SavedObjectsClientContract, + request: KibanaRequest ) { - return new DataRecognizer(callAsCurrentUser, savedObjectsClient); + return new DataRecognizer(mlClusterClient, savedObjectsClient, request); } diff --git a/x-pack/plugins/ml/server/shared_services/providers/results_service.ts b/x-pack/plugins/ml/server/shared_services/providers/results_service.ts index e9448a67cd98a..6af4eb008567a 100644 --- a/x-pack/plugins/ml/server/shared_services/providers/results_service.ts +++ b/x-pack/plugins/ml/server/shared_services/providers/results_service.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller, KibanaRequest } from 'kibana/server'; +import { ILegacyScopedClusterClient, KibanaRequest } from 'kibana/server'; import { resultsServiceProvider } from '../../models/results_service'; import { SharedServicesChecks } from '../shared_services'; @@ -12,7 +12,7 @@ type OrigResultsServiceProvider = ReturnType; export interface ResultsServiceProvider { resultsServiceProvider( - callAsCurrentUser: LegacyAPICaller, + mlClusterClient: ILegacyScopedClusterClient, request: KibanaRequest ): { getAnomaliesTableData: OrigResultsServiceProvider['getAnomaliesTableData']; @@ -24,9 +24,16 @@ export function getResultsServiceProvider({ getHasMlCapabilities, }: SharedServicesChecks): ResultsServiceProvider { return { - resultsServiceProvider(callAsCurrentUser: LegacyAPICaller, request: KibanaRequest) { - const hasMlCapabilities = getHasMlCapabilities(request); - const { getAnomaliesTableData } = resultsServiceProvider(callAsCurrentUser); + resultsServiceProvider(mlClusterClient: ILegacyScopedClusterClient, request: KibanaRequest) { + // Uptime is using this service in anomaly alert, kibana alerting doesn't provide request object + // So we are adding a dummy request for now + // TODO: Remove this once kibana alerting provides request object + const hasMlCapabilities = + request.params !== 'DummyKibanaRequest' + ? getHasMlCapabilities(request) + : (_caps: string[]) => Promise.resolve(); + + const { getAnomaliesTableData } = resultsServiceProvider(mlClusterClient); return { async getAnomaliesTableData(...args) { isFullLicense(); diff --git a/x-pack/plugins/ml/server/shared_services/providers/system.ts b/x-pack/plugins/ml/server/shared_services/providers/system.ts index 00124a67e5237..ec2662014546e 100644 --- a/x-pack/plugins/ml/server/shared_services/providers/system.ts +++ b/x-pack/plugins/ml/server/shared_services/providers/system.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LegacyAPICaller, KibanaRequest } from 'kibana/server'; +import { ILegacyScopedClusterClient, KibanaRequest } from 'kibana/server'; import { SearchResponse, SearchParams } from 'elasticsearch'; import { MlServerLicense } from '../../lib/license'; import { CloudSetup } from '../../../../cloud/server'; @@ -18,7 +18,7 @@ import { SharedServicesChecks } from '../shared_services'; export interface MlSystemProvider { mlSystemProvider( - callAsCurrentUser: LegacyAPICaller, + mlClusterClient: ILegacyScopedClusterClient, request: KibanaRequest ): { mlCapabilities(): Promise; @@ -35,8 +35,9 @@ export function getMlSystemProvider( resolveMlCapabilities: ResolveMlCapabilities ): MlSystemProvider { return { - mlSystemProvider(callAsCurrentUser: LegacyAPICaller, request: KibanaRequest) { + mlSystemProvider(mlClusterClient: ILegacyScopedClusterClient, request: KibanaRequest) { // const hasMlCapabilities = getHasMlCapabilities(request); + const { callAsCurrentUser, callAsInternalUser } = mlClusterClient; return { async mlCapabilities() { isMinimumLicense(); @@ -52,7 +53,7 @@ export function getMlSystemProvider( } const { getCapabilities } = capabilitiesProvider( - callAsCurrentUser, + mlClusterClient, mlCapabilities, mlLicense, isMlEnabledInSpace @@ -62,7 +63,7 @@ export function getMlSystemProvider( async mlInfo(): Promise { isMinimumLicense(); - const info = await callAsCurrentUser('ml.info'); + const info = await callAsInternalUser('ml.info'); const cloudId = cloud && cloud.cloudId; return { ...info, diff --git a/x-pack/plugins/monitoring/common/constants.ts b/x-pack/plugins/monitoring/common/constants.ts index eeed7b4d5acf6..2c714080969e4 100644 --- a/x-pack/plugins/monitoring/common/constants.ts +++ b/x-pack/plugins/monitoring/common/constants.ts @@ -139,7 +139,7 @@ export const INDEX_PATTERN = '.monitoring-*-6-*,.monitoring-*-7-*'; export const INDEX_PATTERN_KIBANA = '.monitoring-kibana-6-*,.monitoring-kibana-7-*'; export const INDEX_PATTERN_LOGSTASH = '.monitoring-logstash-6-*,.monitoring-logstash-7-*'; export const INDEX_PATTERN_BEATS = '.monitoring-beats-6-*,.monitoring-beats-7-*'; -export const INDEX_ALERTS = '.monitoring-alerts-6,.monitoring-alerts-7'; +export const INDEX_ALERTS = '.monitoring-alerts-6*,.monitoring-alerts-7*'; export const INDEX_PATTERN_ELASTICSEARCH = '.monitoring-es-6-*,.monitoring-es-7-*'; // This is the unique token that exists in monitoring indices collected by metricbeat @@ -222,41 +222,54 @@ export const TELEMETRY_COLLECTION_INTERVAL = 86400000; * as the only way to see the new UI and actually run Kibana alerts. It will * be false until all alerts have been migrated, then it will be removed */ -export const KIBANA_ALERTING_ENABLED = false; +export const KIBANA_CLUSTER_ALERTS_ENABLED = false; /** * The prefix for all alert types used by monitoring */ -export const ALERT_TYPE_PREFIX = 'monitoring_'; +export const ALERT_PREFIX = 'monitoring_'; +export const ALERT_LICENSE_EXPIRATION = `${ALERT_PREFIX}alert_license_expiration`; +export const ALERT_CLUSTER_HEALTH = `${ALERT_PREFIX}alert_cluster_health`; +export const ALERT_CPU_USAGE = `${ALERT_PREFIX}alert_cpu_usage`; +export const ALERT_NODES_CHANGED = `${ALERT_PREFIX}alert_nodes_changed`; +export const ALERT_ELASTICSEARCH_VERSION_MISMATCH = `${ALERT_PREFIX}alert_elasticsearch_version_mismatch`; +export const ALERT_KIBANA_VERSION_MISMATCH = `${ALERT_PREFIX}alert_kibana_version_mismatch`; +export const ALERT_LOGSTASH_VERSION_MISMATCH = `${ALERT_PREFIX}alert_logstash_version_mismatch`; /** - * This is the alert type id for the license expiration alert - */ -export const ALERT_TYPE_LICENSE_EXPIRATION = `${ALERT_TYPE_PREFIX}alert_type_license_expiration`; -/** - * This is the alert type id for the cluster state alert + * A listing of all alert types */ -export const ALERT_TYPE_CLUSTER_STATE = `${ALERT_TYPE_PREFIX}alert_type_cluster_state`; +export const ALERTS = [ + ALERT_LICENSE_EXPIRATION, + ALERT_CLUSTER_HEALTH, + ALERT_CPU_USAGE, + ALERT_NODES_CHANGED, + ALERT_ELASTICSEARCH_VERSION_MISMATCH, + ALERT_KIBANA_VERSION_MISMATCH, + ALERT_LOGSTASH_VERSION_MISMATCH, +]; /** - * A listing of all alert types + * A list of all legacy alerts, which means they are powered by watcher */ -export const ALERT_TYPES = [ALERT_TYPE_LICENSE_EXPIRATION, ALERT_TYPE_CLUSTER_STATE]; +export const LEGACY_ALERTS = [ + ALERT_LICENSE_EXPIRATION, + ALERT_CLUSTER_HEALTH, + ALERT_NODES_CHANGED, + ALERT_ELASTICSEARCH_VERSION_MISMATCH, + ALERT_KIBANA_VERSION_MISMATCH, + ALERT_LOGSTASH_VERSION_MISMATCH, +]; /** * Matches the id for the built-in in email action type * See x-pack/plugins/actions/server/builtin_action_types/email.ts */ export const ALERT_ACTION_TYPE_EMAIL = '.email'; - -/** - * The number of alerts that have been migrated - */ -export const NUMBER_OF_MIGRATED_ALERTS = 2; - /** - * The advanced settings config name for the email address + * Matches the id for the built-in in log action type + * See x-pack/plugins/actions/server/builtin_action_types/log.ts */ -export const MONITORING_CONFIG_ALERTING_EMAIL_ADDRESS = 'monitoring:alertingEmailAddress'; +export const ALERT_ACTION_TYPE_LOG = '.server-log'; export const ALERT_EMAIL_SERVICES = ['gmail', 'hotmail', 'icloud', 'outlook365', 'ses', 'yahoo']; diff --git a/x-pack/plugins/monitoring/common/enums.ts b/x-pack/plugins/monitoring/common/enums.ts new file mode 100644 index 0000000000000..74711b31756be --- /dev/null +++ b/x-pack/plugins/monitoring/common/enums.ts @@ -0,0 +1,28 @@ +/* + * 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 enum AlertClusterHealthType { + Green = 'green', + Red = 'red', + Yellow = 'yellow', +} + +export enum AlertSeverity { + Success = 'success', + Danger = 'danger', + Warning = 'warning', +} + +export enum AlertMessageTokenType { + Time = 'time', + Link = 'link', + DocLink = 'docLink', +} + +export enum AlertParamType { + Duration = 'duration', + Percentage = 'percentage', +} diff --git a/x-pack/plugins/monitoring/common/formatting.js b/x-pack/plugins/monitoring/common/formatting.js index a3b3ce07c8c76..b2a67b3cd48da 100644 --- a/x-pack/plugins/monitoring/common/formatting.js +++ b/x-pack/plugins/monitoring/common/formatting.js @@ -17,10 +17,10 @@ export const LARGE_ABBREVIATED = '0,0.[0]a'; * @param date Either a numeric Unix timestamp or a {@code Date} object * @returns The date formatted using 'LL LTS' */ -export function formatDateTimeLocal(date, useUTC = false) { +export function formatDateTimeLocal(date, useUTC = false, timezone = null) { return useUTC ? moment.utc(date).format('LL LTS') - : moment.tz(date, moment.tz.guess()).format('LL LTS'); + : moment.tz(date, timezone || moment.tz.guess()).format('LL LTS'); } /** diff --git a/x-pack/plugins/monitoring/common/types.ts b/x-pack/plugins/monitoring/common/types.ts new file mode 100644 index 0000000000000..f5dc85dce32e1 --- /dev/null +++ b/x-pack/plugins/monitoring/common/types.ts @@ -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 { Alert } from '../../alerts/common'; +import { AlertParamType } from './enums'; + +export interface CommonBaseAlert { + type: string; + label: string; + paramDetails: CommonAlertParamDetails; + rawAlert: Alert; + isLegacy: boolean; +} + +export interface CommonAlertStatus { + exists: boolean; + enabled: boolean; + states: CommonAlertState[]; + alert: CommonBaseAlert; +} + +export interface CommonAlertState { + firing: boolean; + state: any; + meta: any; +} + +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface CommonAlertFilter {} + +export interface CommonAlertCpuUsageFilter extends CommonAlertFilter { + nodeUuid: string; +} + +export interface CommonAlertParamDetail { + label: string; + type: AlertParamType; +} + +export interface CommonAlertParamDetails { + [name: string]: CommonAlertParamDetail; +} + +export interface CommonAlertParams { + [name: string]: string | number; +} diff --git a/x-pack/plugins/monitoring/kibana.json b/x-pack/plugins/monitoring/kibana.json index 65dd4b373a71a..3b9e60124b034 100644 --- a/x-pack/plugins/monitoring/kibana.json +++ b/x-pack/plugins/monitoring/kibana.json @@ -3,8 +3,17 @@ "version": "8.0.0", "kibanaVersion": "kibana", "configPath": ["monitoring"], - "requiredPlugins": ["licensing", "features", "data", "navigation", "kibanaLegacy"], - "optionalPlugins": ["alerts", "actions", "infra", "telemetryCollectionManager", "usageCollection", "home", "cloud"], + "requiredPlugins": [ + "licensing", + "features", + "data", + "navigation", + "kibanaLegacy", + "triggers_actions_ui", + "alerts", + "actions" + ], + "optionalPlugins": ["infra", "telemetryCollectionManager", "usageCollection", "home", "cloud"], "server": true, "ui": true, "requiredBundles": ["kibanaUtils", "home", "alerts", "kibanaReact", "licenseManagement"] diff --git a/x-pack/plugins/monitoring/public/alerts/badge.tsx b/x-pack/plugins/monitoring/public/alerts/badge.tsx new file mode 100644 index 0000000000000..4518d2c56cabb --- /dev/null +++ b/x-pack/plugins/monitoring/public/alerts/badge.tsx @@ -0,0 +1,179 @@ +/* + * 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 { i18n } from '@kbn/i18n'; +import { + EuiContextMenu, + EuiPopover, + EuiBadge, + EuiFlexGrid, + EuiFlexItem, + EuiText, +} from '@elastic/eui'; +import { CommonAlertStatus, CommonAlertState } from '../../common/types'; +import { AlertSeverity } from '../../common/enums'; +// @ts-ignore +import { formatDateTimeLocal } from '../../common/formatting'; +import { AlertState } from '../../server/alerts/types'; +import { AlertPanel } from './panel'; +import { Legacy } from '../legacy_shims'; +import { isInSetupMode } from '../lib/setup_mode'; + +function getDateFromState(states: CommonAlertState[]) { + const timestamp = states[0].state.ui.triggeredMS; + const tz = Legacy.shims.uiSettings.get('dateFormat:tz'); + return formatDateTimeLocal(timestamp, false, tz === 'Browser' ? null : tz); +} + +export const numberOfAlertsLabel = (count: number) => `${count} alert${count > 1 ? 's' : ''}`; + +interface Props { + alerts: { [alertTypeId: string]: CommonAlertStatus }; +} +export const AlertsBadge: React.FC = (props: Props) => { + const [showPopover, setShowPopover] = React.useState(null); + const inSetupMode = isInSetupMode(); + const alerts = Object.values(props.alerts).filter(Boolean); + + if (alerts.length === 0) { + return null; + } + + const badges = []; + + if (inSetupMode) { + const button = ( + setShowPopover(true)} + > + {numberOfAlertsLabel(alerts.length)} + + ); + const panels = [ + { + id: 0, + title: i18n.translate('xpack.monitoring.alerts.badge.panelTitle', { + defaultMessage: 'Alerts', + }), + items: alerts.map(({ alert }, index) => { + return { + name: {alert.label}, + panel: index + 1, + }; + }), + }, + ...alerts.map((alertStatus, index) => { + return { + id: index + 1, + title: alertStatus.alert.label, + width: 400, + content: , + }; + }), + ]; + + badges.push( + setShowPopover(null)} + panelPaddingSize="none" + withTitle + anchorPosition="downLeft" + > + + + ); + } else { + const byType = { + [AlertSeverity.Danger]: [] as CommonAlertStatus[], + [AlertSeverity.Warning]: [] as CommonAlertStatus[], + [AlertSeverity.Success]: [] as CommonAlertStatus[], + }; + + for (const alert of alerts) { + for (const alertState of alert.states) { + const state = alertState.state as AlertState; + byType[state.ui.severity].push(alert); + } + } + + const typesToShow = [AlertSeverity.Danger, AlertSeverity.Warning]; + for (const type of typesToShow) { + const list = byType[type]; + if (list.length === 0) { + continue; + } + + const button = ( + setShowPopover(type)} + > + {numberOfAlertsLabel(list.length)} + + ); + + const panels = [ + { + id: 0, + title: `Alerts`, + items: list.map(({ alert, states }, index) => { + return { + name: ( + + +

{getDateFromState(states)}

+
+ {alert.label} +
+ ), + panel: index + 1, + }; + }), + }, + ...list.map((alertStatus, index) => { + return { + id: index + 1, + title: getDateFromState(alertStatus.states), + width: 400, + content: , + }; + }), + ]; + + badges.push( + setShowPopover(null)} + panelPaddingSize="none" + withTitle + anchorPosition="downLeft" + > + + + ); + } + } + + return ( + + {badges.map((badge, index) => ( + + {badge} + + ))} + + ); +}; diff --git a/x-pack/plugins/monitoring/public/alerts/callout.tsx b/x-pack/plugins/monitoring/public/alerts/callout.tsx new file mode 100644 index 0000000000000..748ec257ea765 --- /dev/null +++ b/x-pack/plugins/monitoring/public/alerts/callout.tsx @@ -0,0 +1,81 @@ +/* + * 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 { i18n } from '@kbn/i18n'; +import { EuiCallOut, EuiSpacer } from '@elastic/eui'; +import { CommonAlertStatus } from '../../common/types'; +import { AlertSeverity } from '../../common/enums'; +import { replaceTokens } from './lib/replace_tokens'; +import { AlertMessage } from '../../server/alerts/types'; + +const TYPES = [ + { + severity: AlertSeverity.Warning, + color: 'warning', + label: i18n.translate('xpack.monitoring.alerts.callout.warningLabel', { + defaultMessage: 'Warning alert(s)', + }), + }, + { + severity: AlertSeverity.Danger, + color: 'danger', + label: i18n.translate('xpack.monitoring.alerts.callout.dangerLabel', { + defaultMessage: 'DAnger alert(s)', + }), + }, +]; + +interface Props { + alerts: { [alertTypeId: string]: CommonAlertStatus }; +} +export const AlertsCallout: React.FC = (props: Props) => { + const { alerts } = props; + + const callouts = TYPES.map((type) => { + const list = []; + for (const alertTypeId of Object.keys(alerts)) { + const alertInstance = alerts[alertTypeId]; + for (const { state } of alertInstance.states) { + if (state.ui.severity === type.severity) { + list.push(state); + } + } + } + + if (list.length) { + return ( + + +
    + {list.map((state, index) => { + const nextStepsUi = + state.ui.message.nextSteps && state.ui.message.nextSteps.length ? ( +
      + {state.ui.message.nextSteps.map( + (step: AlertMessage, nextStepIndex: number) => ( +
    • {replaceTokens(step)}
    • + ) + )} +
    + ) : null; + + return ( +
  • + {replaceTokens(state.ui.message)} + {nextStepsUi} +
  • + ); + })} +
+
+ +
+ ); + } + }); + return {callouts}; +}; diff --git a/x-pack/plugins/monitoring/public/alerts/cpu_usage_alert/cpu_usage_alert.tsx b/x-pack/plugins/monitoring/public/alerts/cpu_usage_alert/cpu_usage_alert.tsx new file mode 100644 index 0000000000000..56cba83813a63 --- /dev/null +++ b/x-pack/plugins/monitoring/public/alerts/cpu_usage_alert/cpu_usage_alert.tsx @@ -0,0 +1,28 @@ +/* + * 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'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths +import { AlertTypeModel } from '../../../../triggers_actions_ui/public/types'; +import { validate } from './validation'; +import { ALERT_CPU_USAGE } from '../../../common/constants'; +import { Expression } from './expression'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths +import { CpuUsageAlert } from '../../../server/alerts'; + +export function createCpuUsageAlertType(): AlertTypeModel { + const alert = new CpuUsageAlert(); + return { + id: ALERT_CPU_USAGE, + name: alert.label, + iconClass: 'bell', + alertParamsExpression: (props: any) => ( + + ), + validate, + defaultActionMessage: '{{context.internalFullMessage}}', + requiresAppContext: false, + }; +} diff --git a/x-pack/plugins/monitoring/public/alerts/cpu_usage_alert/expression.tsx b/x-pack/plugins/monitoring/public/alerts/cpu_usage_alert/expression.tsx new file mode 100644 index 0000000000000..7dc6155de529e --- /dev/null +++ b/x-pack/plugins/monitoring/public/alerts/cpu_usage_alert/expression.tsx @@ -0,0 +1,61 @@ +/* + * 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 { EuiForm, EuiSpacer } from '@elastic/eui'; +import { CommonAlertParamDetails } from '../../../common/types'; +import { AlertParamDuration } from '../flyout_expressions/alert_param_duration'; +import { AlertParamType } from '../../../common/enums'; +import { AlertParamPercentage } from '../flyout_expressions/alert_param_percentage'; + +export interface Props { + alertParams: { [property: string]: any }; + setAlertParams: (property: string, value: any) => void; + setAlertProperty: (property: string, value: any) => void; + errors: { [key: string]: string[] }; + paramDetails: CommonAlertParamDetails; +} + +export const Expression: React.FC = (props) => { + const { alertParams, paramDetails, setAlertParams, errors } = props; + + const alertParamsUi = Object.keys(alertParams).map((alertParamName) => { + const details = paramDetails[alertParamName]; + const value = alertParams[alertParamName]; + + switch (details.type) { + case AlertParamType.Duration: + return ( + + ); + case AlertParamType.Percentage: + return ( + + ); + } + }); + + return ( + + {alertParamsUi} + + + ); +}; diff --git a/x-pack/plugins/monitoring/public/components/alerts/index.js b/x-pack/plugins/monitoring/public/alerts/cpu_usage_alert/index.ts similarity index 79% rename from x-pack/plugins/monitoring/public/components/alerts/index.js rename to x-pack/plugins/monitoring/public/alerts/cpu_usage_alert/index.ts index c4eda37c2b252..6ef31ee472c61 100644 --- a/x-pack/plugins/monitoring/public/components/alerts/index.js +++ b/x-pack/plugins/monitoring/public/alerts/cpu_usage_alert/index.ts @@ -4,4 +4,4 @@ * you may not use this file except in compliance with the Elastic License. */ -export { Alerts } from './alerts'; +export { createCpuUsageAlertType } from './cpu_usage_alert'; diff --git a/x-pack/plugins/monitoring/public/alerts/cpu_usage_alert/validation.tsx b/x-pack/plugins/monitoring/public/alerts/cpu_usage_alert/validation.tsx new file mode 100644 index 0000000000000..577ec12e634ed --- /dev/null +++ b/x-pack/plugins/monitoring/public/alerts/cpu_usage_alert/validation.tsx @@ -0,0 +1,35 @@ +/* + * 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'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths +import { ValidationResult } from '../../../../triggers_actions_ui/public/types'; + +export function validate(opts: any): ValidationResult { + const validationResult = { errors: {} }; + + const errors: { [key: string]: string[] } = { + duration: [], + threshold: [], + }; + if (!opts.duration) { + errors.duration.push( + i18n.translate('xpack.monitoring.alerts.cpuUsage.validation.duration', { + defaultMessage: 'A valid duration is required.', + }) + ); + } + if (isNaN(opts.threshold)) { + errors.threshold.push( + i18n.translate('xpack.monitoring.alerts.cpuUsage.validation.threshold', { + defaultMessage: 'A valid number is required.', + }) + ); + } + + validationResult.errors = errors; + return validationResult; +} diff --git a/x-pack/plugins/monitoring/public/alerts/flyout_expressions/alert_param_duration.tsx b/x-pack/plugins/monitoring/public/alerts/flyout_expressions/alert_param_duration.tsx new file mode 100644 index 0000000000000..23a9ea1facbc9 --- /dev/null +++ b/x-pack/plugins/monitoring/public/alerts/flyout_expressions/alert_param_duration.tsx @@ -0,0 +1,98 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +import React from 'react'; +import { i18n } from '@kbn/i18n'; +import { EuiFlexItem, EuiFlexGroup, EuiFieldNumber, EuiSelect, EuiFormRow } from '@elastic/eui'; + +enum TIME_UNITS { + SECOND = 's', + MINUTE = 'm', + HOUR = 'h', + DAY = 'd', +} +function getTimeUnitLabel(timeUnit = TIME_UNITS.SECOND, timeValue = '0') { + switch (timeUnit) { + case TIME_UNITS.SECOND: + return i18n.translate('xpack.monitoring.alerts.flyoutExpressions.timeUnits.secondLabel', { + defaultMessage: '{timeValue, plural, one {second} other {seconds}}', + values: { timeValue }, + }); + case TIME_UNITS.MINUTE: + return i18n.translate('xpack.monitoring.alerts.flyoutExpressions.timeUnits.minuteLabel', { + defaultMessage: '{timeValue, plural, one {minute} other {minutes}}', + values: { timeValue }, + }); + case TIME_UNITS.HOUR: + return i18n.translate('xpack.monitoring.alerts.flyoutExpressions.timeUnits.hourLabel', { + defaultMessage: '{timeValue, plural, one {hour} other {hours}}', + values: { timeValue }, + }); + case TIME_UNITS.DAY: + return i18n.translate('xpack.monitoring.alerts.flyoutExpressions.timeUnits.dayLabel', { + defaultMessage: '{timeValue, plural, one {day} other {days}}', + values: { timeValue }, + }); + } +} + +// TODO: WHY does this not work? +// import { getTimeUnitLabel, TIME_UNITS } from '../../../triggers_actions_ui/public'; + +interface Props { + name: string; + duration: string; + label: string; + errors: string[]; + setAlertParams: (property: string, value: any) => void; +} + +const parseRegex = /(\d+)(\smhd)/; +export const AlertParamDuration: React.FC = (props: Props) => { + const { name, label, setAlertParams, errors } = props; + const parsed = parseRegex.exec(props.duration); + const defaultValue = parsed && parsed[1] ? parseInt(parsed[1], 10) : 1; + const defaultUnit = parsed && parsed[2] ? parsed[2] : TIME_UNITS.MINUTE; + const [value, setValue] = React.useState(defaultValue); + const [unit, setUnit] = React.useState(defaultUnit); + + const timeUnits = Object.values(TIME_UNITS).map((timeUnit) => ({ + value: timeUnit, + text: getTimeUnitLabel(timeUnit), + })); + + React.useEffect(() => { + setAlertParams(name, `${value}${unit}`); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [unit, value]); + + return ( + 0}> + + + { + let newValue = parseInt(e.target.value, 10); + if (isNaN(newValue)) { + newValue = 0; + } + setValue(newValue); + }} + /> + + + setUnit(e.target.value)} + options={timeUnits} + /> + + + + ); +}; diff --git a/x-pack/plugins/monitoring/public/alerts/flyout_expressions/alert_param_percentage.tsx b/x-pack/plugins/monitoring/public/alerts/flyout_expressions/alert_param_percentage.tsx new file mode 100644 index 0000000000000..352fb72557498 --- /dev/null +++ b/x-pack/plugins/monitoring/public/alerts/flyout_expressions/alert_param_percentage.tsx @@ -0,0 +1,41 @@ +/* + * 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 { EuiFormRow, EuiFieldNumber, EuiText } from '@elastic/eui'; + +interface Props { + name: string; + percentage: number; + label: string; + errors: string[]; + setAlertParams: (property: string, value: any) => void; +} +export const AlertParamPercentage: React.FC = (props: Props) => { + const { name, label, setAlertParams, errors } = props; + const [value, setValue] = React.useState(props.percentage); + + return ( + 0}> + + % + + } + onChange={(e) => { + let newValue = parseInt(e.target.value, 10); + if (isNaN(newValue)) { + newValue = 0; + } + setValue(newValue); + setAlertParams(name, newValue); + }} + /> + + ); +}; diff --git a/x-pack/plugins/monitoring/public/components/alerts/configuration/index.ts b/x-pack/plugins/monitoring/public/alerts/legacy_alert/index.ts similarity index 81% rename from x-pack/plugins/monitoring/public/components/alerts/configuration/index.ts rename to x-pack/plugins/monitoring/public/alerts/legacy_alert/index.ts index 7a96c6e324ab3..6370ed66f0c30 100644 --- a/x-pack/plugins/monitoring/public/components/alerts/configuration/index.ts +++ b/x-pack/plugins/monitoring/public/alerts/legacy_alert/index.ts @@ -4,4 +4,4 @@ * you may not use this file except in compliance with the Elastic License. */ -export { AlertsConfiguration } from './configuration'; +export { createLegacyAlertTypes } from './legacy_alert'; diff --git a/x-pack/plugins/monitoring/public/alerts/legacy_alert/legacy_alert.tsx b/x-pack/plugins/monitoring/public/alerts/legacy_alert/legacy_alert.tsx new file mode 100644 index 0000000000000..58b37e43085ff --- /dev/null +++ b/x-pack/plugins/monitoring/public/alerts/legacy_alert/legacy_alert.tsx @@ -0,0 +1,39 @@ +/* + * 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 { i18n } from '@kbn/i18n'; +import { EuiTextColor, EuiSpacer } from '@elastic/eui'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths +import { AlertTypeModel } from '../../../../triggers_actions_ui/public/types'; +import { LEGACY_ALERTS } from '../../../common/constants'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths +import { BY_TYPE } from '../../../server/alerts'; + +export function createLegacyAlertTypes(): AlertTypeModel[] { + return LEGACY_ALERTS.map((legacyAlert) => { + const alertCls = BY_TYPE[legacyAlert]; + const alert = new alertCls(); + return { + id: legacyAlert, + name: alert.label, + iconClass: 'bell', + alertParamsExpression: (props: any) => ( + + + + {i18n.translate('xpack.monitoring.alerts.legacyAlert.expressionText', { + defaultMessage: 'There is nothing to configure.', + })} + + + + ), + defaultActionMessage: '{{context.internalFullMessage}}', + validate: () => ({ errors: {} }), + requiresAppContext: false, + }; + }); +} diff --git a/x-pack/plugins/monitoring/public/alerts/lib/replace_tokens.tsx b/x-pack/plugins/monitoring/public/alerts/lib/replace_tokens.tsx new file mode 100644 index 0000000000000..29e0822ad684d --- /dev/null +++ b/x-pack/plugins/monitoring/public/alerts/lib/replace_tokens.tsx @@ -0,0 +1,93 @@ +/* + * 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 moment from 'moment'; +import { EuiLink } from '@elastic/eui'; +import { + AlertMessage, + AlertMessageTimeToken, + AlertMessageLinkToken, + AlertMessageDocLinkToken, +} from '../../../server/alerts/types'; +// @ts-ignore +import { formatTimestampToDuration } from '../../../common'; +import { CALCULATE_DURATION_UNTIL } from '../../../common/constants'; +import { AlertMessageTokenType } from '../../../common/enums'; +import { Legacy } from '../../legacy_shims'; + +export function replaceTokens(alertMessage: AlertMessage): JSX.Element | string | null { + if (!alertMessage) { + return null; + } + + let text = alertMessage.text; + if (!alertMessage.tokens || !alertMessage.tokens.length) { + return text; + } + + const timeTokens = alertMessage.tokens.filter( + (token) => token.type === AlertMessageTokenType.Time + ); + const linkTokens = alertMessage.tokens.filter( + (token) => token.type === AlertMessageTokenType.Link + ); + const docLinkTokens = alertMessage.tokens.filter( + (token) => token.type === AlertMessageTokenType.DocLink + ); + + for (const token of timeTokens) { + const timeToken = token as AlertMessageTimeToken; + text = text.replace( + timeToken.startToken, + timeToken.isRelative + ? formatTimestampToDuration(timeToken.timestamp, CALCULATE_DURATION_UNTIL) + : moment.tz(timeToken.timestamp, moment.tz.guess()).format('LLL z') + ); + } + + let element: JSX.Element = {text}; + for (const token of linkTokens) { + const linkToken = token as AlertMessageLinkToken; + const linkPart = new RegExp(`${linkToken.startToken}(.+?)${linkToken.endToken}`).exec(text); + if (!linkPart || linkPart.length < 2) { + continue; + } + const index = text.indexOf(linkPart[0]); + const preString = text.substring(0, index); + const postString = text.substring(index + linkPart[0].length); + element = ( + + {preString} + {linkPart[1]} + {postString} + + ); + } + + for (const token of docLinkTokens) { + const linkToken = token as AlertMessageDocLinkToken; + const linkPart = new RegExp(`${linkToken.startToken}(.+?)${linkToken.endToken}`).exec(text); + if (!linkPart || linkPart.length < 2) { + continue; + } + + const url = linkToken.partialUrl + .replace('{elasticWebsiteUrl}', Legacy.shims.docLinks.ELASTIC_WEBSITE_URL) + .replace('{docLinkVersion}', Legacy.shims.docLinks.DOC_LINK_VERSION); + const index = text.indexOf(linkPart[0]); + const preString = text.substring(0, index); + const postString = text.substring(index + linkPart[0].length); + element = ( + + {preString} + {linkPart[1]} + {postString} + + ); + } + + return element; +} diff --git a/x-pack/plugins/monitoring/public/alerts/lib/should_show_alert_badge.ts b/x-pack/plugins/monitoring/public/alerts/lib/should_show_alert_badge.ts new file mode 100644 index 0000000000000..c6773e9ca0156 --- /dev/null +++ b/x-pack/plugins/monitoring/public/alerts/lib/should_show_alert_badge.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 { isInSetupMode } from '../../lib/setup_mode'; +import { CommonAlertStatus } from '../../../common/types'; + +export function shouldShowAlertBadge( + alerts: { [alertTypeId: string]: CommonAlertStatus }, + alertTypeIds: string[] +) { + const inSetupMode = isInSetupMode(); + return inSetupMode || alertTypeIds.find((name) => alerts[name] && alerts[name].states.length); +} diff --git a/x-pack/plugins/monitoring/public/alerts/panel.tsx b/x-pack/plugins/monitoring/public/alerts/panel.tsx new file mode 100644 index 0000000000000..3c5a4ef55a96b --- /dev/null +++ b/x-pack/plugins/monitoring/public/alerts/panel.tsx @@ -0,0 +1,225 @@ +/* + * 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 { i18n } from '@kbn/i18n'; +import { FormattedMessage } from '@kbn/i18n/react'; +import { + EuiSpacer, + EuiButton, + EuiFlexGroup, + EuiFlexItem, + EuiSwitch, + EuiTitle, + EuiHorizontalRule, + EuiListGroup, + EuiListGroupItem, +} from '@elastic/eui'; + +import { CommonAlertStatus } from '../../common/types'; +import { AlertMessage } from '../../server/alerts/types'; +import { Legacy } from '../legacy_shims'; +import { replaceTokens } from './lib/replace_tokens'; +import { AlertsContextProvider } from '../../../triggers_actions_ui/public'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths +import { AlertEdit } from '../../../triggers_actions_ui/public'; +import { isInSetupMode, hideBottomBar, showBottomBar } from '../lib/setup_mode'; +import { BASE_ALERT_API_PATH } from '../../../alerts/common'; + +interface Props { + alert: CommonAlertStatus; +} +export const AlertPanel: React.FC = (props: Props) => { + const { + alert: { states, alert }, + } = props; + const [showFlyout, setShowFlyout] = React.useState(false); + const [isEnabled, setIsEnabled] = React.useState(alert.rawAlert.enabled); + const [isMuted, setIsMuted] = React.useState(alert.rawAlert.muteAll); + const [isSaving, setIsSaving] = React.useState(false); + const inSetupMode = isInSetupMode(); + + if (!alert.rawAlert) { + return null; + } + + async function disableAlert() { + setIsSaving(true); + try { + await Legacy.shims.http.post(`${BASE_ALERT_API_PATH}/alert/${alert.rawAlert.id}/_disable`); + } catch (err) { + Legacy.shims.toastNotifications.addDanger({ + title: i18n.translate('xpack.monitoring.alerts.panel.disableAlert.errorTitle', { + defaultMessage: `Unable to disable alert`, + }), + text: err.message, + }); + } + setIsSaving(false); + } + async function enableAlert() { + setIsSaving(true); + try { + await Legacy.shims.http.post(`${BASE_ALERT_API_PATH}/alert/${alert.rawAlert.id}/_enable`); + } catch (err) { + Legacy.shims.toastNotifications.addDanger({ + title: i18n.translate('xpack.monitoring.alerts.panel.enableAlert.errorTitle', { + defaultMessage: `Unable to enable alert`, + }), + text: err.message, + }); + } + setIsSaving(false); + } + async function muteAlert() { + setIsSaving(true); + try { + await Legacy.shims.http.post(`${BASE_ALERT_API_PATH}/alert/${alert.rawAlert.id}/_mute_all`); + } catch (err) { + Legacy.shims.toastNotifications.addDanger({ + title: i18n.translate('xpack.monitoring.alerts.panel.muteAlert.errorTitle', { + defaultMessage: `Unable to mute alert`, + }), + text: err.message, + }); + } + setIsSaving(false); + } + async function unmuteAlert() { + setIsSaving(true); + try { + await Legacy.shims.http.post(`${BASE_ALERT_API_PATH}/alert/${alert.rawAlert.id}/_unmute_all`); + } catch (err) { + Legacy.shims.toastNotifications.addDanger({ + title: i18n.translate('xpack.monitoring.alerts.panel.ummuteAlert.errorTitle', { + defaultMessage: `Unable to unmute alert`, + }), + text: err.message, + }); + } + setIsSaving(false); + } + + const flyoutUi = showFlyout ? ( + {}, + capabilities: Legacy.shims.capabilities, + }} + > + { + setShowFlyout(false); + showBottomBar(); + }} + /> + + ) : null; + + const configurationUi = ( + + + + { + setShowFlyout(true); + hideBottomBar(); + }} + > + {i18n.translate('xpack.monitoring.alerts.panel.editAlert', { + defaultMessage: `Edit alert`, + })} + + + + { + if (isEnabled) { + setIsEnabled(false); + await disableAlert(); + } else { + setIsEnabled(true); + await enableAlert(); + } + }} + label={ + + } + /> + + + { + if (isMuted) { + setIsMuted(false); + await unmuteAlert(); + } else { + setIsMuted(true); + await muteAlert(); + } + }} + label={ + + } + /> + + + {flyoutUi} + + ); + + if (inSetupMode) { + return
{configurationUi}
; + } + + const firingStates = states.filter((state) => state.firing); + if (!firingStates.length) { + return
{configurationUi}
; + } + + const firingState = firingStates[0]; + const nextStepsUi = + firingState.state.ui.message.nextSteps && firingState.state.ui.message.nextSteps.length ? ( + + {firingState.state.ui.message.nextSteps.map((step: AlertMessage, index: number) => ( + + ))} + + ) : null; + + return ( + +
+ +
{replaceTokens(firingState.state.ui.message)}
+
+ {nextStepsUi ? : null} + {nextStepsUi} +
+ +
{configurationUi}
+
+ ); +}; diff --git a/x-pack/plugins/monitoring/public/alerts/status.tsx b/x-pack/plugins/monitoring/public/alerts/status.tsx new file mode 100644 index 0000000000000..d15dcc9974863 --- /dev/null +++ b/x-pack/plugins/monitoring/public/alerts/status.tsx @@ -0,0 +1,99 @@ +/* + * 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 { EuiToolTip, EuiHealth } from '@elastic/eui'; +import { FormattedMessage } from '@kbn/i18n/react'; +import { i18n } from '@kbn/i18n'; +import { CommonAlertStatus } from '../../common/types'; +import { AlertSeverity } from '../../common/enums'; +import { AlertState } from '../../server/alerts/types'; +import { AlertsBadge } from './badge'; + +interface Props { + alerts: { [alertTypeId: string]: CommonAlertStatus }; + showBadge: boolean; + showOnlyCount: boolean; +} +export const AlertsStatus: React.FC = (props: Props) => { + const { alerts, showBadge = false, showOnlyCount = false } = props; + + let atLeastOneDanger = false; + const count = Object.values(alerts).reduce((cnt, alertStatus) => { + if (alertStatus.states.length) { + if (!atLeastOneDanger) { + for (const state of alertStatus.states) { + if ((state.state as AlertState).ui.severity === AlertSeverity.Danger) { + atLeastOneDanger = true; + break; + } + } + } + cnt++; + } + return cnt; + }, 0); + + if (count === 0) { + return ( + + + {showOnlyCount ? ( + count + ) : ( + + )} + + + ); + } + + if (showBadge) { + return ; + } + + const severity = atLeastOneDanger ? AlertSeverity.Danger : AlertSeverity.Warning; + + const tooltipText = (() => { + switch (severity) { + case AlertSeverity.Danger: + return i18n.translate('xpack.monitoring.alerts.status.highSeverityTooltip', { + defaultMessage: 'There are some critical issues that require your immediate attention!', + }); + case AlertSeverity.Warning: + return i18n.translate('xpack.monitoring.alerts.status.mediumSeverityTooltip', { + defaultMessage: 'There are some issues that might have impact on the stack.', + }); + default: + // might never show + return i18n.translate('xpack.monitoring.alerts.status.lowSeverityTooltip', { + defaultMessage: 'There are some low-severity issues.', + }); + } + })(); + + return ( + + + {showOnlyCount ? ( + count + ) : ( + + )} + + + ); +}; diff --git a/x-pack/plugins/monitoring/public/angular/app_modules.ts b/x-pack/plugins/monitoring/public/angular/app_modules.ts index 9ebb074ec7c3b..f3d77b196b26e 100644 --- a/x-pack/plugins/monitoring/public/angular/app_modules.ts +++ b/x-pack/plugins/monitoring/public/angular/app_modules.ts @@ -18,7 +18,7 @@ import { createTopNavDirective, createTopNavHelper, } from '../../../../../src/plugins/kibana_legacy/public'; -import { MonitoringPluginDependencies } from '../types'; +import { MonitoringStartPluginDependencies } from '../types'; import { GlobalState } from '../url_state'; import { getSafeForExternalLink } from '../lib/get_safe_for_external_link'; @@ -60,7 +60,7 @@ export const localAppModule = ({ data: { query }, navigation, externalConfig, -}: MonitoringPluginDependencies) => { +}: MonitoringStartPluginDependencies) => { createLocalI18nModule(); createLocalPrivateModule(); createLocalStorage(); @@ -90,7 +90,9 @@ export const localAppModule = ({ return appModule; }; -function createMonitoringAppConfigConstants(keys: MonitoringPluginDependencies['externalConfig']) { +function createMonitoringAppConfigConstants( + keys: MonitoringStartPluginDependencies['externalConfig'] +) { let constantsModule = angular.module('monitoring/constants', []); keys.map(([key, value]) => (constantsModule = constantsModule.constant(key as string, value))); } @@ -173,7 +175,7 @@ function createMonitoringAppFilters() { }); } -function createLocalConfigModule(core: MonitoringPluginDependencies['core']) { +function createLocalConfigModule(core: MonitoringStartPluginDependencies['core']) { angular.module('monitoring/Config', []).provider('config', function () { return { $get: () => ({ @@ -201,7 +203,7 @@ function createLocalPrivateModule() { angular.module('monitoring/Private', []).provider('Private', PrivateProvider); } -function createLocalTopNavModule({ ui }: MonitoringPluginDependencies['navigation']) { +function createLocalTopNavModule({ ui }: MonitoringStartPluginDependencies['navigation']) { angular .module('monitoring/TopNav', ['react']) .directive('kbnTopNav', createTopNavDirective) diff --git a/x-pack/plugins/monitoring/public/angular/index.ts b/x-pack/plugins/monitoring/public/angular/index.ts index 69d97a5e3bdc3..da57c028643a5 100644 --- a/x-pack/plugins/monitoring/public/angular/index.ts +++ b/x-pack/plugins/monitoring/public/angular/index.ts @@ -10,13 +10,13 @@ import { Legacy } from '../legacy_shims'; import { configureAppAngularModule } from '../../../../../src/plugins/kibana_legacy/public'; import { localAppModule, appModuleName } from './app_modules'; -import { MonitoringPluginDependencies } from '../types'; +import { MonitoringStartPluginDependencies } from '../types'; const APP_WRAPPER_CLASS = 'monApplicationWrapper'; export class AngularApp { private injector?: angular.auto.IInjectorService; - constructor(deps: MonitoringPluginDependencies) { + constructor(deps: MonitoringStartPluginDependencies) { const { core, element, @@ -25,6 +25,7 @@ export class AngularApp { isCloud, pluginInitializerContext, externalConfig, + triggersActionsUi, kibanaLegacy, } = deps; const app: IModule = localAppModule(deps); @@ -40,6 +41,7 @@ export class AngularApp { pluginInitializerContext, externalConfig, kibanaLegacy, + triggersActionsUi, }, this.injector ); diff --git a/x-pack/plugins/monitoring/public/components/alerts/__snapshots__/status.test.tsx.snap b/x-pack/plugins/monitoring/public/components/alerts/__snapshots__/status.test.tsx.snap deleted file mode 100644 index 5562d4bae9b14..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/__snapshots__/status.test.tsx.snap +++ /dev/null @@ -1,65 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Status should render a flyout when clicking the link 1`] = ` - - - -

- Monitoring alerts -

-
- -

- Configure an email server and email address to receive alerts. -

-
-
- - - -
-`; - -exports[`Status should render a success message if all alerts have been migrated and in setup mode 1`] = ` - -

- - Want to make changes? Click here. - -

-
-`; - -exports[`Status should render without setup mode 1`] = ` - - -

- - Migrate cluster alerts to our new alerting platform. - -

-
- -
-`; diff --git a/x-pack/plugins/monitoring/public/components/alerts/__tests__/map_severity.js b/x-pack/plugins/monitoring/public/components/alerts/__tests__/map_severity.js deleted file mode 100644 index 8f454e7d765c4..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/__tests__/map_severity.js +++ /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 expect from '@kbn/expect'; -import { mapSeverity } from '../map_severity'; - -describe('mapSeverity', () => { - it('maps [0, 1000) as low', () => { - const low = { - value: 'low', - color: 'warning', - iconType: 'iInCircle', - title: 'Low severity alert', - }; - - expect(mapSeverity(-1)).to.not.eql(low); - expect(mapSeverity(0)).to.eql(low); - expect(mapSeverity(1)).to.eql(low); - expect(mapSeverity(500)).to.eql(low); - expect(mapSeverity(998)).to.eql(low); - expect(mapSeverity(999)).to.eql(low); - expect(mapSeverity(1000)).to.not.eql(low); - }); - - it('maps [1000, 2000) as medium', () => { - const medium = { - value: 'medium', - color: 'warning', - iconType: 'alert', - title: 'Medium severity alert', - }; - - expect(mapSeverity(999)).to.not.eql(medium); - expect(mapSeverity(1000)).to.eql(medium); - expect(mapSeverity(1001)).to.eql(medium); - expect(mapSeverity(1500)).to.eql(medium); - expect(mapSeverity(1998)).to.eql(medium); - expect(mapSeverity(1999)).to.eql(medium); - expect(mapSeverity(2000)).to.not.eql(medium); - }); - - it('maps (-INF, 0) and [2000, +INF) as high', () => { - const high = { - value: 'high', - color: 'danger', - iconType: 'bell', - title: 'High severity alert', - }; - - expect(mapSeverity(-123412456)).to.eql(high); - expect(mapSeverity(-1)).to.eql(high); - expect(mapSeverity(0)).to.not.eql(high); - expect(mapSeverity(1999)).to.not.eql(high); - expect(mapSeverity(2000)).to.eql(high); - expect(mapSeverity(2001)).to.eql(high); - expect(mapSeverity(2500)).to.eql(high); - expect(mapSeverity(2998)).to.eql(high); - expect(mapSeverity(2999)).to.eql(high); - expect(mapSeverity(3000)).to.eql(high); - expect(mapSeverity(123412456)).to.eql(high); - }); -}); diff --git a/x-pack/plugins/monitoring/public/components/alerts/alerts.js b/x-pack/plugins/monitoring/public/components/alerts/alerts.js deleted file mode 100644 index 59e838c449a3b..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/alerts.js +++ /dev/null @@ -1,191 +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 { Legacy } from '../../legacy_shims'; -import { upperFirst, get } from 'lodash'; -import { formatDateTimeLocal } from '../../../common/formatting'; -import { formatTimestampToDuration } from '../../../common'; -import { - CALCULATE_DURATION_SINCE, - EUI_SORT_DESCENDING, - ALERT_TYPE_LICENSE_EXPIRATION, - ALERT_TYPE_CLUSTER_STATE, -} from '../../../common/constants'; -import { mapSeverity } from './map_severity'; -import { FormattedAlert } from '../../components/alerts/formatted_alert'; -import { EuiMonitoringTable } from '../../components/table'; -import { EuiHealth, EuiIcon, EuiToolTip } from '@elastic/eui'; -import { i18n } from '@kbn/i18n'; - -const linkToCategories = { - 'elasticsearch/nodes': 'Elasticsearch Nodes', - 'elasticsearch/indices': 'Elasticsearch Indices', - 'kibana/instances': 'Kibana Instances', - 'logstash/instances': 'Logstash Nodes', - [ALERT_TYPE_LICENSE_EXPIRATION]: 'License expiration', - [ALERT_TYPE_CLUSTER_STATE]: 'Cluster state', -}; -const getColumns = (timezone) => [ - { - name: i18n.translate('xpack.monitoring.alerts.statusColumnTitle', { - defaultMessage: 'Status', - }), - field: 'status', - sortable: true, - render: (severity) => { - const severityIconDefaults = { - title: i18n.translate('xpack.monitoring.alerts.severityTitle.unknown', { - defaultMessage: 'Unknown', - }), - color: 'subdued', - value: i18n.translate('xpack.monitoring.alerts.severityValue.unknown', { - defaultMessage: 'N/A', - }), - }; - const severityIcon = { ...severityIconDefaults, ...mapSeverity(severity) }; - - return ( - - - {upperFirst(severityIcon.value)} - - - ); - }, - }, - { - name: i18n.translate('xpack.monitoring.alerts.resolvedColumnTitle', { - defaultMessage: 'Resolved', - }), - field: 'resolved_timestamp', - sortable: true, - render: (resolvedTimestamp) => { - const notResolvedLabel = i18n.translate('xpack.monitoring.alerts.notResolvedDescription', { - defaultMessage: 'Not Resolved', - }); - - const resolution = { - icon: null, - text: notResolvedLabel, - }; - - if (resolvedTimestamp) { - resolution.text = i18n.translate('xpack.monitoring.alerts.resolvedAgoDescription', { - defaultMessage: '{duration} ago', - values: { - duration: formatTimestampToDuration(resolvedTimestamp, CALCULATE_DURATION_SINCE), - }, - }); - } else { - resolution.icon = ; - } - - return ( - - {resolution.icon} {resolution.text} - - ); - }, - }, - { - name: i18n.translate('xpack.monitoring.alerts.messageColumnTitle', { - defaultMessage: 'Message', - }), - field: 'message', - sortable: true, - render: (_message, alert) => { - const message = get(alert, 'message.text', get(alert, 'message', '')); - return ( - - ); - }, - }, - { - name: i18n.translate('xpack.monitoring.alerts.categoryColumnTitle', { - defaultMessage: 'Category', - }), - field: 'category', - sortable: true, - render: (link) => - linkToCategories[link] - ? linkToCategories[link] - : i18n.translate('xpack.monitoring.alerts.categoryColumn.generalLabel', { - defaultMessage: 'General', - }), - }, - { - name: i18n.translate('xpack.monitoring.alerts.lastCheckedColumnTitle', { - defaultMessage: 'Last Checked', - }), - field: 'update_timestamp', - sortable: true, - render: (timestamp) => formatDateTimeLocal(timestamp, timezone), - }, - { - name: i18n.translate('xpack.monitoring.alerts.triggeredColumnTitle', { - defaultMessage: 'Triggered', - }), - field: 'timestamp', - sortable: true, - render: (timestamp) => - i18n.translate('xpack.monitoring.alerts.triggeredColumnValue', { - defaultMessage: '{timestamp} ago', - values: { - timestamp: formatTimestampToDuration(timestamp, CALCULATE_DURATION_SINCE), - }, - }), - }, -]; - -export const Alerts = ({ alerts, sorting, pagination, onTableChange }) => { - const alertsFlattened = alerts.map((alert) => ({ - ...alert, - status: get(alert, 'metadata.severity', get(alert, 'severity', 0)), - category: get(alert, 'metadata.link', get(alert, 'type', null)), - })); - - const injector = Legacy.shims.getAngularInjector(); - const timezone = injector.get('config').get('dateFormat:tz'); - - return ( - - ); -}; diff --git a/x-pack/plugins/monitoring/public/components/alerts/configuration/__snapshots__/configuration.test.tsx.snap b/x-pack/plugins/monitoring/public/components/alerts/configuration/__snapshots__/configuration.test.tsx.snap deleted file mode 100644 index 429d19fbb887e..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/configuration/__snapshots__/configuration.test.tsx.snap +++ /dev/null @@ -1,121 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Configuration shallow view should render step 1 1`] = ` - - - Create new email action... - , - "inputDisplay": - Create new email action... - , - "value": "__new__", - }, - ] - } - valueOfSelected="" - /> - -`; - -exports[`Configuration shallow view should render step 2 1`] = ` - - - - - -`; - -exports[`Configuration shallow view should render step 3 1`] = ` - - - Save - - -`; - -exports[`Configuration should render high level steps 1`] = ` -
- - - - - - - - - -
-`; diff --git a/x-pack/plugins/monitoring/public/components/alerts/configuration/__snapshots__/step1.test.tsx.snap b/x-pack/plugins/monitoring/public/components/alerts/configuration/__snapshots__/step1.test.tsx.snap deleted file mode 100644 index cb1081c0c14da..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/configuration/__snapshots__/step1.test.tsx.snap +++ /dev/null @@ -1,301 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Step1 creating should render a create form 1`] = ` - - - - - -`; - -exports[`Step1 editing should allow for editing 1`] = ` - - -

- Edit the action below. -

-
- - -
-`; - -exports[`Step1 should render normally 1`] = ` - - - From: , Service: - , - "inputDisplay": - From: , Service: - , - "value": "1", - }, - Object { - "dropdownDisplay": - Create new email action... - , - "inputDisplay": - Create new email action... - , - "value": "__new__", - }, - ] - } - valueOfSelected="1" - /> - - - - - Edit - - - - - Test - - - - - Delete - - - - -`; - -exports[`Step1 testing should should a tooltip if there is no email address 1`] = ` - - - Test - - -`; - -exports[`Step1 testing should show a failed test error 1`] = ` - - - From: , Service: - , - "inputDisplay": - From: , Service: - , - "value": "1", - }, - Object { - "dropdownDisplay": - Create new email action... - , - "inputDisplay": - Create new email action... - , - "value": "__new__", - }, - ] - } - valueOfSelected="1" - /> - - - - - Edit - - - - - Test - - - - - Delete - - - - - -

- Very detailed error message -

-
-
-`; - -exports[`Step1 testing should show a successful test 1`] = ` - - - From: , Service: - , - "inputDisplay": - From: , Service: - , - "value": "1", - }, - Object { - "dropdownDisplay": - Create new email action... - , - "inputDisplay": - Create new email action... - , - "value": "__new__", - }, - ] - } - valueOfSelected="1" - /> - - - - - Edit - - - - - Test - - - - - Delete - - - - - -

- Looks good on our end! -

-
-
-`; diff --git a/x-pack/plugins/monitoring/public/components/alerts/configuration/__snapshots__/step2.test.tsx.snap b/x-pack/plugins/monitoring/public/components/alerts/configuration/__snapshots__/step2.test.tsx.snap deleted file mode 100644 index bac183618b491..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/configuration/__snapshots__/step2.test.tsx.snap +++ /dev/null @@ -1,49 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Step2 should render normally 1`] = ` - - - - - -`; - -exports[`Step2 should show form errors 1`] = ` - - - - - -`; diff --git a/x-pack/plugins/monitoring/public/components/alerts/configuration/__snapshots__/step3.test.tsx.snap b/x-pack/plugins/monitoring/public/components/alerts/configuration/__snapshots__/step3.test.tsx.snap deleted file mode 100644 index ed15ae9a9cff7..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/configuration/__snapshots__/step3.test.tsx.snap +++ /dev/null @@ -1,95 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Step3 should render normally 1`] = ` - - - Save - - -`; - -exports[`Step3 should show a disabled state 1`] = ` - - - Save - - -`; - -exports[`Step3 should show a saving state 1`] = ` - - - Save - - -`; - -exports[`Step3 should show an error 1`] = ` - - -

- Test error -

-
- - - Save - -
-`; diff --git a/x-pack/plugins/monitoring/public/components/alerts/configuration/configuration.test.tsx b/x-pack/plugins/monitoring/public/components/alerts/configuration/configuration.test.tsx deleted file mode 100644 index 7caef8c230bf4..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/configuration/configuration.test.tsx +++ /dev/null @@ -1,140 +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 { mockUseEffects } from '../../../jest.helpers'; -import { shallow, ShallowWrapper } from 'enzyme'; -import { Legacy } from '../../../legacy_shims'; -import { AlertsConfiguration, AlertsConfigurationProps } from './configuration'; - -jest.mock('../../../legacy_shims', () => ({ - Legacy: { - shims: { - kfetch: jest.fn(), - }, - }, -})); - -const defaultProps: AlertsConfigurationProps = { - emailAddress: 'test@elastic.co', - onDone: jest.fn(), -}; - -describe('Configuration', () => { - it('should render high level steps', () => { - const component = shallow(); - expect(component.find('EuiSteps').shallow()).toMatchSnapshot(); - }); - - function getStep(component: ShallowWrapper, index: number) { - return component.find('EuiSteps').shallow().find('EuiStep').at(index).children().shallow(); - } - - describe('shallow view', () => { - it('should render step 1', () => { - const component = shallow(); - const stepOne = getStep(component, 0); - expect(stepOne).toMatchSnapshot(); - }); - - it('should render step 2', () => { - const component = shallow(); - const stepTwo = getStep(component, 1); - expect(stepTwo).toMatchSnapshot(); - }); - - it('should render step 3', () => { - const component = shallow(); - const stepThree = getStep(component, 2); - expect(stepThree).toMatchSnapshot(); - }); - }); - - describe('selected action', () => { - const actionId = 'a123b'; - let component: ShallowWrapper; - beforeEach(async () => { - mockUseEffects(2); - - (Legacy.shims.kfetch as jest.Mock).mockImplementation(() => { - return { - data: [ - { - actionTypeId: '.email', - id: actionId, - config: {}, - }, - ], - }; - }); - - component = shallow(); - }); - - it('reflect in Step1', async () => { - const steps = component.find('EuiSteps').dive(); - expect(steps.find('EuiStep').at(0).prop('title')).toBe('Select email action'); - expect(steps.find('Step1').prop('selectedEmailActionId')).toBe(actionId); - }); - - it('should enable Step2', async () => { - const steps = component.find('EuiSteps').dive(); - expect(steps.find('Step2').prop('isDisabled')).toBe(false); - }); - - it('should enable Step3', async () => { - const steps = component.find('EuiSteps').dive(); - expect(steps.find('Step3').prop('isDisabled')).toBe(false); - }); - }); - - describe('edit action', () => { - let component: ShallowWrapper; - beforeEach(async () => { - (Legacy.shims.kfetch as jest.Mock).mockImplementation(() => { - return { - data: [], - }; - }); - - component = shallow(); - }); - - it('disable Step2', async () => { - const steps = component.find('EuiSteps').dive(); - expect(steps.find('Step2').prop('isDisabled')).toBe(true); - }); - - it('disable Step3', async () => { - const steps = component.find('EuiSteps').dive(); - expect(steps.find('Step3').prop('isDisabled')).toBe(true); - }); - }); - - describe('no email address', () => { - let component: ShallowWrapper; - beforeEach(async () => { - (Legacy.shims.kfetch as jest.Mock).mockImplementation(() => { - return { - data: [ - { - actionTypeId: '.email', - id: 'actionId', - config: {}, - }, - ], - }; - }); - - component = shallow(); - }); - - it('should disable Step3', async () => { - const steps = component.find('EuiSteps').dive(); - expect(steps.find('Step3').prop('isDisabled')).toBe(true); - }); - }); -}); diff --git a/x-pack/plugins/monitoring/public/components/alerts/configuration/configuration.tsx b/x-pack/plugins/monitoring/public/components/alerts/configuration/configuration.tsx deleted file mode 100644 index f248e20493a24..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/configuration/configuration.tsx +++ /dev/null @@ -1,193 +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, { ReactNode } from 'react'; -import { EuiSteps } from '@elastic/eui'; -import { i18n } from '@kbn/i18n'; -import { Legacy } from '../../../legacy_shims'; -import { ActionResult } from '../../../../../../plugins/actions/common'; -import { ALERT_ACTION_TYPE_EMAIL } from '../../../../common/constants'; -import { getMissingFieldErrors } from '../../../lib/form_validation'; -import { Step1 } from './step1'; -import { Step2 } from './step2'; -import { Step3 } from './step3'; - -export interface AlertsConfigurationProps { - emailAddress: string; - onDone: Function; -} - -export interface StepResult { - title: string; - children: ReactNode; - status: any; -} - -export interface AlertsConfigurationForm { - email: string | null; -} - -export const NEW_ACTION_ID = '__new__'; - -export const AlertsConfiguration: React.FC = ( - props: AlertsConfigurationProps -) => { - const { onDone } = props; - - const [emailActions, setEmailActions] = React.useState([]); - const [selectedEmailActionId, setSelectedEmailActionId] = React.useState(''); - const [editAction, setEditAction] = React.useState(null); - const [emailAddress, setEmailAddress] = React.useState(props.emailAddress); - const [formErrors, setFormErrors] = React.useState({ email: null }); - const [showFormErrors, setShowFormErrors] = React.useState(false); - const [isSaving, setIsSaving] = React.useState(false); - const [saveError, setSaveError] = React.useState(''); - - React.useEffect(() => { - async function fetchData() { - await fetchEmailActions(); - } - - fetchData(); - }, []); - - React.useEffect(() => { - setFormErrors(getMissingFieldErrors({ email: emailAddress }, { email: '' })); - }, [emailAddress]); - - async function fetchEmailActions() { - const kibanaActions = await Legacy.shims.kfetch({ - method: 'GET', - pathname: `/api/actions`, - }); - - const actions = kibanaActions.data.filter( - (action: ActionResult) => action.actionTypeId === ALERT_ACTION_TYPE_EMAIL - ); - if (actions.length > 0) { - setSelectedEmailActionId(actions[0].id); - } else { - setSelectedEmailActionId(NEW_ACTION_ID); - } - setEmailActions(actions); - } - - async function save() { - if (emailAddress.length === 0) { - setShowFormErrors(true); - return; - } - setIsSaving(true); - setShowFormErrors(false); - - try { - await Legacy.shims.kfetch({ - method: 'POST', - pathname: `/api/monitoring/v1/alerts`, - body: JSON.stringify({ selectedEmailActionId, emailAddress }), - }); - } catch (err) { - setIsSaving(false); - setSaveError( - err?.body?.message || - i18n.translate('xpack.monitoring.alerts.configuration.unknownError', { - defaultMessage: 'Something went wrong. Please consult the server logs.', - }) - ); - return; - } - - onDone(); - } - - function isStep2Disabled() { - return isStep2AndStep3Disabled(); - } - - function isStep3Disabled() { - return isStep2AndStep3Disabled() || !emailAddress || emailAddress.length === 0; - } - - function isStep2AndStep3Disabled() { - return !!editAction || !selectedEmailActionId || selectedEmailActionId === NEW_ACTION_ID; - } - - function getStep2Status() { - const isDisabled = isStep2AndStep3Disabled(); - - if (isDisabled) { - return 'disabled' as const; - } - - if (emailAddress && emailAddress.length) { - return 'complete' as const; - } - - return 'incomplete' as const; - } - - function getStep1Status() { - if (editAction) { - return 'incomplete' as const; - } - - return selectedEmailActionId ? ('complete' as const) : ('incomplete' as const); - } - - const steps = [ - { - title: emailActions.length - ? i18n.translate('xpack.monitoring.alerts.configuration.selectEmailAction', { - defaultMessage: 'Select email action', - }) - : i18n.translate('xpack.monitoring.alerts.configuration.createEmailAction', { - defaultMessage: 'Create email action', - }), - children: ( - await fetchEmailActions()} - emailActions={emailActions} - selectedEmailActionId={selectedEmailActionId} - setSelectedEmailActionId={setSelectedEmailActionId} - emailAddress={emailAddress} - editAction={editAction} - setEditAction={setEditAction} - /> - ), - status: getStep1Status(), - }, - { - title: i18n.translate('xpack.monitoring.alerts.configuration.setEmailAddress', { - defaultMessage: 'Set the email to receive alerts', - }), - status: getStep2Status(), - children: ( - - ), - }, - { - title: i18n.translate('xpack.monitoring.alerts.configuration.confirm', { - defaultMessage: 'Confirm and save', - }), - status: getStep2Status(), - children: ( - - ), - }, - ]; - - return ( -
- -
- ); -}; diff --git a/x-pack/plugins/monitoring/public/components/alerts/configuration/step1.test.tsx b/x-pack/plugins/monitoring/public/components/alerts/configuration/step1.test.tsx deleted file mode 100644 index 1be66ce4ccfef..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/configuration/step1.test.tsx +++ /dev/null @@ -1,331 +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 { omit, pick } from 'lodash'; -import '../../../jest.helpers'; -import { shallow } from 'enzyme'; -import { GetStep1Props } from './step1'; -import { EmailActionData } from '../manage_email_action'; -import { ALERT_ACTION_TYPE_EMAIL } from '../../../../common/constants'; - -let Step1: React.FC; -let NEW_ACTION_ID: string; - -function setModules() { - Step1 = require('./step1').Step1; - NEW_ACTION_ID = require('./configuration').NEW_ACTION_ID; -} - -describe('Step1', () => { - const emailActions = [ - { - id: '1', - actionTypeId: '1abc', - name: 'Testing', - config: {}, - isPreconfigured: false, - }, - ]; - const selectedEmailActionId = emailActions[0].id; - const setSelectedEmailActionId = jest.fn(); - const emailAddress = 'test@test.com'; - const editAction = null; - const setEditAction = jest.fn(); - const onActionDone = jest.fn(); - - const defaultProps: GetStep1Props = { - onActionDone, - emailActions, - selectedEmailActionId, - setSelectedEmailActionId, - emailAddress, - editAction, - setEditAction, - }; - - beforeEach(() => { - jest.isolateModules(() => { - jest.doMock('../../../legacy_shims', () => ({ - Legacy: { - shims: { - kfetch: () => { - return {}; - }, - }, - }, - })); - setModules(); - }); - }); - - it('should render normally', () => { - const component = shallow(); - - expect(component).toMatchSnapshot(); - }); - - describe('creating', () => { - it('should render a create form', () => { - const customProps = { - emailActions: [], - selectedEmailActionId: NEW_ACTION_ID, - }; - - const component = shallow(); - - expect(component).toMatchSnapshot(); - }); - - it('should render the select box if at least one action exists', () => { - const customProps = { - emailActions: [ - { - id: 'foo', - actionTypeId: '.email', - name: '', - config: {}, - isPreconfigured: false, - }, - ], - selectedEmailActionId: NEW_ACTION_ID, - }; - - const component = shallow(); - expect(component.find('EuiSuperSelect').exists()).toBe(true); - }); - - it('should send up the create to the server', async () => { - const kfetch = jest.fn().mockImplementation(() => {}); - jest.isolateModules(() => { - jest.doMock('../../../legacy_shims', () => ({ - Legacy: { - shims: { - kfetch, - }, - }, - })); - setModules(); - }); - - const customProps = { - emailActions: [], - selectedEmailActionId: NEW_ACTION_ID, - }; - - const component = shallow(); - - const data: EmailActionData = { - service: 'gmail', - host: 'smtp.gmail.com', - port: 465, - secure: true, - from: 'test@test.com', - user: 'user@user.com', - password: 'password', - }; - - const createEmailAction: (data: EmailActionData) => void = component - .find('ManageEmailAction') - .prop('createEmailAction'); - createEmailAction(data); - - expect(kfetch).toHaveBeenCalledWith({ - method: 'POST', - pathname: `/api/actions/action`, - body: JSON.stringify({ - name: 'Email action for Stack Monitoring alerts', - actionTypeId: ALERT_ACTION_TYPE_EMAIL, - config: omit(data, ['user', 'password']), - secrets: pick(data, ['user', 'password']), - }), - }); - }); - }); - - describe('editing', () => { - it('should allow for editing', () => { - const customProps = { - editAction: emailActions[0], - }; - - const component = shallow(); - - expect(component).toMatchSnapshot(); - }); - - it('should send up the edit to the server', async () => { - const kfetch = jest.fn().mockImplementation(() => {}); - jest.isolateModules(() => { - jest.doMock('../../../legacy_shims', () => ({ - Legacy: { - shims: { - kfetch, - }, - }, - })); - setModules(); - }); - - const customProps = { - editAction: emailActions[0], - }; - - const component = shallow(); - - const data: EmailActionData = { - service: 'gmail', - host: 'smtp.gmail.com', - port: 465, - secure: true, - from: 'test@test.com', - user: 'user@user.com', - password: 'password', - }; - - const createEmailAction: (data: EmailActionData) => void = component - .find('ManageEmailAction') - .prop('createEmailAction'); - createEmailAction(data); - - expect(kfetch).toHaveBeenCalledWith({ - method: 'PUT', - pathname: `/api/actions/action/${emailActions[0].id}`, - body: JSON.stringify({ - name: emailActions[0].name, - config: omit(data, ['user', 'password']), - secrets: pick(data, ['user', 'password']), - }), - }); - }); - }); - - describe('testing', () => { - it('should allow for testing', async () => { - jest.isolateModules(() => { - jest.doMock('../../../legacy_shims', () => ({ - Legacy: { - shims: { - kfetch: jest.fn().mockImplementation((arg) => { - if (arg.pathname === '/api/actions/action/1/_execute') { - return { status: 'ok' }; - } - return {}; - }), - }, - }, - })); - setModules(); - }); - - const component = shallow(); - - expect(component.find('EuiButton').at(1).prop('isLoading')).toBe(false); - component.find('EuiButton').at(1).simulate('click'); - expect(component.find('EuiButton').at(1).prop('isLoading')).toBe(true); - await component.update(); - expect(component.find('EuiButton').at(1).prop('isLoading')).toBe(false); - }); - - it('should show a successful test', async () => { - jest.isolateModules(() => { - jest.doMock('../../../legacy_shims', () => ({ - Legacy: { - shims: { - kfetch: (arg: any) => { - if (arg.pathname === '/api/actions/action/1/_execute') { - return { status: 'ok' }; - } - return {}; - }, - }, - }, - })); - setModules(); - }); - - const component = shallow(); - - component.find('EuiButton').at(1).simulate('click'); - await component.update(); - expect(component).toMatchSnapshot(); - }); - - it('should show a failed test error', async () => { - jest.isolateModules(() => { - jest.doMock('../../../legacy_shims', () => ({ - Legacy: { - shims: { - kfetch: (arg: any) => { - if (arg.pathname === '/api/actions/action/1/_execute') { - return { message: 'Very detailed error message' }; - } - return {}; - }, - }, - }, - })); - setModules(); - }); - - const component = shallow(); - - component.find('EuiButton').at(1).simulate('click'); - await component.update(); - expect(component).toMatchSnapshot(); - }); - - it('should not allow testing if there is no email address', () => { - const customProps = { - emailAddress: '', - }; - const component = shallow(); - expect(component.find('EuiButton').at(1).prop('isDisabled')).toBe(true); - }); - - it('should should a tooltip if there is no email address', () => { - const customProps = { - emailAddress: '', - }; - const component = shallow(); - expect(component.find('EuiToolTip')).toMatchSnapshot(); - }); - }); - - describe('deleting', () => { - it('should send up the delete to the server', async () => { - const kfetch = jest.fn().mockImplementation(() => {}); - jest.isolateModules(() => { - jest.doMock('../../../legacy_shims', () => ({ - Legacy: { - shims: { - kfetch, - }, - }, - })); - setModules(); - }); - - const customProps = { - setSelectedEmailActionId: jest.fn(), - onActionDone: jest.fn(), - }; - const component = shallow(); - - await component.find('EuiButton').at(2).simulate('click'); - await component.update(); - - expect(kfetch).toHaveBeenCalledWith({ - method: 'DELETE', - pathname: `/api/actions/action/${emailActions[0].id}`, - }); - - expect(customProps.setSelectedEmailActionId).toHaveBeenCalledWith(''); - expect(customProps.onActionDone).toHaveBeenCalled(); - expect(component.find('EuiButton').at(2).prop('isLoading')).toBe(false); - }); - }); -}); diff --git a/x-pack/plugins/monitoring/public/components/alerts/configuration/step1.tsx b/x-pack/plugins/monitoring/public/components/alerts/configuration/step1.tsx deleted file mode 100644 index b3e6c079378ef..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/configuration/step1.tsx +++ /dev/null @@ -1,334 +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, { Fragment } from 'react'; -import { - EuiText, - EuiSpacer, - EuiPanel, - EuiFlexGroup, - EuiFlexItem, - EuiButton, - EuiSuperSelect, - EuiToolTip, - EuiCallOut, -} from '@elastic/eui'; -import { omit, pick } from 'lodash'; -import { i18n } from '@kbn/i18n'; -import { Legacy } from '../../../legacy_shims'; -import { ActionResult, BASE_ACTION_API_PATH } from '../../../../../../plugins/actions/common'; -import { ManageEmailAction, EmailActionData } from '../manage_email_action'; -import { ALERT_ACTION_TYPE_EMAIL } from '../../../../common/constants'; -import { NEW_ACTION_ID } from './configuration'; - -export interface GetStep1Props { - onActionDone: () => Promise; - emailActions: ActionResult[]; - selectedEmailActionId: string; - setSelectedEmailActionId: (id: string) => void; - emailAddress: string; - editAction: ActionResult | null; - setEditAction: (action: ActionResult | null) => void; -} - -export const Step1: React.FC = (props: GetStep1Props) => { - const [isTesting, setIsTesting] = React.useState(false); - const [isDeleting, setIsDeleting] = React.useState(false); - const [testingStatus, setTestingStatus] = React.useState(null); - const [fullTestingError, setFullTestingError] = React.useState(''); - - async function createEmailAction(data: EmailActionData) { - if (props.editAction) { - await Legacy.shims.kfetch({ - method: 'PUT', - pathname: `${BASE_ACTION_API_PATH}/action/${props.editAction.id}`, - body: JSON.stringify({ - name: props.editAction.name, - config: omit(data, ['user', 'password']), - secrets: pick(data, ['user', 'password']), - }), - }); - props.setEditAction(null); - } else { - await Legacy.shims.kfetch({ - method: 'POST', - pathname: `${BASE_ACTION_API_PATH}/action`, - body: JSON.stringify({ - name: i18n.translate('xpack.monitoring.alerts.configuration.emailAction.name', { - defaultMessage: 'Email action for Stack Monitoring alerts', - }), - actionTypeId: ALERT_ACTION_TYPE_EMAIL, - config: omit(data, ['user', 'password']), - secrets: pick(data, ['user', 'password']), - }), - }); - } - - await props.onActionDone(); - } - - async function deleteEmailAction(id: string) { - setIsDeleting(true); - - await Legacy.shims.kfetch({ - method: 'DELETE', - pathname: `${BASE_ACTION_API_PATH}/action/${id}`, - }); - - if (props.editAction && props.editAction.id === id) { - props.setEditAction(null); - } - if (props.selectedEmailActionId === id) { - props.setSelectedEmailActionId(''); - } - await props.onActionDone(); - setIsDeleting(false); - setTestingStatus(null); - } - - async function testEmailAction() { - setIsTesting(true); - setTestingStatus(null); - - const params = { - subject: 'Kibana alerting test configuration', - message: `This is a test for the configured email action for Kibana alerting.`, - to: [props.emailAddress], - }; - - const result = await Legacy.shims.kfetch({ - method: 'POST', - pathname: `${BASE_ACTION_API_PATH}/action/${props.selectedEmailActionId}/_execute`, - body: JSON.stringify({ params }), - }); - if (result.status === 'ok') { - setTestingStatus(true); - } else { - setTestingStatus(false); - setFullTestingError(result.message); - } - setIsTesting(false); - } - - function getTestButton() { - const isTestingDisabled = !props.emailAddress || props.emailAddress.length === 0; - const testBtn = ( - - {i18n.translate('xpack.monitoring.alerts.configuration.testConfiguration.buttonText', { - defaultMessage: 'Test', - })} - - ); - - if (isTestingDisabled) { - return ( - - {testBtn} - - ); - } - - return testBtn; - } - - if (props.editAction) { - return ( - - -

- {i18n.translate('xpack.monitoring.alerts.configuration.step1.editAction', { - defaultMessage: 'Edit the action below.', - })} -

-
- - await createEmailAction(data)} - cancel={() => props.setEditAction(null)} - isNew={false} - action={props.editAction} - /> -
- ); - } - - const newAction = ( - - {i18n.translate('xpack.monitoring.alerts.configuration.newActionDropdownDisplay', { - defaultMessage: 'Create new email action...', - })} - - ); - - const options = [ - ...props.emailActions.map((action) => { - const actionLabel = i18n.translate( - 'xpack.monitoring.alerts.configuration.selectAction.inputDisplay', - { - defaultMessage: 'From: {from}, Service: {service}', - values: { - service: action.config.service, - from: action.config.from, - }, - } - ); - - return { - value: action.id, - inputDisplay: {actionLabel}, - dropdownDisplay: {actionLabel}, - }; - }), - { - value: NEW_ACTION_ID, - inputDisplay: newAction, - dropdownDisplay: newAction, - }, - ]; - - let selectBox: React.ReactNode | null = ( - props.setSelectedEmailActionId(id)} - hasDividers - /> - ); - let createNew = null; - if (props.selectedEmailActionId === NEW_ACTION_ID) { - createNew = ( - - await createEmailAction(data)} - isNew={true} - /> - - ); - - // If there are no actions, do not show the select box as there are no choices - if (props.emailActions.length === 0) { - selectBox = null; - } else { - // Otherwise, add a spacer - selectBox = ( - - {selectBox} - - - ); - } - } - - let manageConfiguration = null; - const selectedEmailAction = props.emailActions.find( - (action) => action.id === props.selectedEmailActionId - ); - - if ( - props.selectedEmailActionId !== NEW_ACTION_ID && - props.selectedEmailActionId && - selectedEmailAction - ) { - let testingStatusUi = null; - if (testingStatus === true) { - testingStatusUi = ( - - - -

- {i18n.translate('xpack.monitoring.alerts.configuration.testConfiguration.success', { - defaultMessage: 'Looks good on our end!', - })} -

-
-
- ); - } else if (testingStatus === false) { - testingStatusUi = ( - - - -

{fullTestingError}

-
-
- ); - } - - manageConfiguration = ( - - - - - { - const editAction = - props.emailActions.find((action) => action.id === props.selectedEmailActionId) || - null; - props.setEditAction(editAction); - }} - > - {i18n.translate( - 'xpack.monitoring.alerts.configuration.editConfiguration.buttonText', - { - defaultMessage: 'Edit', - } - )} - - - {getTestButton()} - - deleteEmailAction(props.selectedEmailActionId)} - isLoading={isDeleting} - > - {i18n.translate( - 'xpack.monitoring.alerts.configuration.deleteConfiguration.buttonText', - { - defaultMessage: 'Delete', - } - )} - - - - {testingStatusUi} - - ); - } - - return ( - - {selectBox} - {manageConfiguration} - {createNew} - - ); -}; diff --git a/x-pack/plugins/monitoring/public/components/alerts/configuration/step2.test.tsx b/x-pack/plugins/monitoring/public/components/alerts/configuration/step2.test.tsx deleted file mode 100644 index 14e3cb078f9cc..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/configuration/step2.test.tsx +++ /dev/null @@ -1,51 +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 '../../../jest.helpers'; -import { shallow } from 'enzyme'; -import { Step2, GetStep2Props } from './step2'; - -describe('Step2', () => { - const defaultProps: GetStep2Props = { - emailAddress: 'test@test.com', - setEmailAddress: jest.fn(), - showFormErrors: false, - formErrors: { email: null }, - isDisabled: false, - }; - - it('should render normally', () => { - const component = shallow(); - expect(component).toMatchSnapshot(); - }); - - it('should set the email address properly', () => { - const newEmail = 'email@email.com'; - const component = shallow(); - component.find('EuiFieldText').simulate('change', { target: { value: newEmail } }); - expect(defaultProps.setEmailAddress).toHaveBeenCalledWith(newEmail); - }); - - it('should show form errors', () => { - const customProps = { - showFormErrors: true, - formErrors: { - email: 'This is required', - }, - }; - const component = shallow(); - expect(component).toMatchSnapshot(); - }); - - it('should disable properly', () => { - const customProps = { - isDisabled: true, - }; - const component = shallow(); - expect(component.find('EuiFieldText').prop('disabled')).toBe(true); - }); -}); diff --git a/x-pack/plugins/monitoring/public/components/alerts/configuration/step2.tsx b/x-pack/plugins/monitoring/public/components/alerts/configuration/step2.tsx deleted file mode 100644 index 2c215e310af69..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/configuration/step2.tsx +++ /dev/null @@ -1,38 +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 { EuiForm, EuiFormRow, EuiFieldText } from '@elastic/eui'; -import { i18n } from '@kbn/i18n'; -import { AlertsConfigurationForm } from './configuration'; - -export interface GetStep2Props { - emailAddress: string; - setEmailAddress: (email: string) => void; - showFormErrors: boolean; - formErrors: AlertsConfigurationForm; - isDisabled: boolean; -} - -export const Step2: React.FC = (props: GetStep2Props) => { - return ( - - - props.setEmailAddress(e.target.value)} - /> - - - ); -}; diff --git a/x-pack/plugins/monitoring/public/components/alerts/configuration/step3.test.tsx b/x-pack/plugins/monitoring/public/components/alerts/configuration/step3.test.tsx deleted file mode 100644 index 9b1304c42a507..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/configuration/step3.test.tsx +++ /dev/null @@ -1,48 +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 '../../../jest.helpers'; -import { shallow } from 'enzyme'; -import { Step3 } from './step3'; - -describe('Step3', () => { - const defaultProps = { - isSaving: false, - isDisabled: false, - save: jest.fn(), - error: null, - }; - - it('should render normally', () => { - const component = shallow(); - expect(component).toMatchSnapshot(); - }); - - it('should save properly', () => { - const component = shallow(); - component.find('EuiButton').simulate('click'); - expect(defaultProps.save).toHaveBeenCalledWith(); - }); - - it('should show a saving state', () => { - const customProps = { isSaving: true }; - const component = shallow(); - expect(component).toMatchSnapshot(); - }); - - it('should show a disabled state', () => { - const customProps = { isDisabled: true }; - const component = shallow(); - expect(component).toMatchSnapshot(); - }); - - it('should show an error', () => { - const customProps = { error: 'Test error' }; - const component = shallow(); - expect(component).toMatchSnapshot(); - }); -}); diff --git a/x-pack/plugins/monitoring/public/components/alerts/configuration/step3.tsx b/x-pack/plugins/monitoring/public/components/alerts/configuration/step3.tsx deleted file mode 100644 index 80acb8992cbc1..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/configuration/step3.tsx +++ /dev/null @@ -1,47 +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, { Fragment } from 'react'; -import { EuiButton, EuiSpacer, EuiCallOut } from '@elastic/eui'; -import { i18n } from '@kbn/i18n'; - -export interface GetStep3Props { - isSaving: boolean; - isDisabled: boolean; - save: () => void; - error: string | null; -} - -export const Step3: React.FC = (props: GetStep3Props) => { - let errorUi = null; - if (props.error) { - errorUi = ( - - -

{props.error}

-
- -
- ); - } - - return ( - - {errorUi} - - {i18n.translate('xpack.monitoring.alerts.configuration.save', { - defaultMessage: 'Save', - })} - - - ); -}; diff --git a/x-pack/plugins/monitoring/public/components/alerts/formatted_alert.js b/x-pack/plugins/monitoring/public/components/alerts/formatted_alert.js deleted file mode 100644 index d23b5b60318c1..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/formatted_alert.js +++ /dev/null @@ -1,63 +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 moment from 'moment-timezone'; -import 'moment-duration-format'; -import React from 'react'; -import { formatTimestampToDuration } from '../../../common/format_timestamp_to_duration'; -import { CALCULATE_DURATION_UNTIL } from '../../../common/constants'; -import { EuiLink } from '@elastic/eui'; -import { getSafeForExternalLink } from '../../lib/get_safe_for_external_link'; - -export function FormattedAlert({ prefix, suffix, message, metadata }) { - const formattedAlert = (() => { - if (metadata && metadata.link) { - if (metadata.link.startsWith('https')) { - return ( - - {message} - - ); - } - - return ( - - {message} - - ); - } - - return message; - })(); - - if (metadata && metadata.time) { - // scan message prefix and replace relative times - // \w: Matches any alphanumeric character from the basic Latin alphabet, including the underscore. Equivalent to [A-Za-z0-9_]. - prefix = prefix.replace( - /{{#relativeTime}}metadata\.([\w\.]+){{\/relativeTime}}/, - (_match, field) => { - return formatTimestampToDuration(metadata[field], CALCULATE_DURATION_UNTIL); - } - ); - prefix = prefix.replace( - /{{#absoluteTime}}metadata\.([\w\.]+){{\/absoluteTime}}/, - (_match, field) => { - return moment.tz(metadata[field], moment.tz.guess()).format('LLL z'); - } - ); - } - - // suffix and prefix don't contain spaces - const formattedPrefix = prefix ? `${prefix} ` : null; - const formattedSuffix = suffix ? ` ${suffix}` : null; - return ( - - {formattedPrefix} - {formattedAlert} - {formattedSuffix} - - ); -} diff --git a/x-pack/plugins/monitoring/public/components/alerts/manage_email_action.tsx b/x-pack/plugins/monitoring/public/components/alerts/manage_email_action.tsx deleted file mode 100644 index 87588a435078d..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/manage_email_action.tsx +++ /dev/null @@ -1,301 +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, { Fragment } from 'react'; -import { - EuiForm, - EuiFormRow, - EuiFieldText, - EuiLink, - EuiSpacer, - EuiFieldNumber, - EuiFieldPassword, - EuiSwitch, - EuiButton, - EuiFlexGroup, - EuiFlexItem, - EuiSuperSelect, - EuiText, -} from '@elastic/eui'; -import { i18n } from '@kbn/i18n'; -import { ActionResult } from '../../../../../plugins/actions/common'; -import { getMissingFieldErrors, hasErrors, getRequiredFieldError } from '../../lib/form_validation'; -import { ALERT_EMAIL_SERVICES } from '../../../common/constants'; - -export interface EmailActionData { - service: string; - host: string; - port?: number; - secure: boolean; - from: string; - user: string; - password: string; -} - -interface ManageActionModalProps { - createEmailAction: (handler: EmailActionData) => void; - cancel?: () => void; - isNew: boolean; - action?: ActionResult | null; -} - -const DEFAULT_DATA: EmailActionData = { - service: '', - host: '', - port: 0, - secure: false, - from: '', - user: '', - password: '', -}; - -const CREATE_LABEL = i18n.translate('xpack.monitoring.alerts.migrate.manageAction.createLabel', { - defaultMessage: 'Create email action', -}); -const SAVE_LABEL = i18n.translate('xpack.monitoring.alerts.migrate.manageAction.saveLabel', { - defaultMessage: 'Save email action', -}); -const CANCEL_LABEL = i18n.translate('xpack.monitoring.alerts.migrate.manageAction.cancelLabel', { - defaultMessage: 'Cancel', -}); - -const NEW_SERVICE_ID = '__new__'; - -export const ManageEmailAction: React.FC = ( - props: ManageActionModalProps -) => { - const { createEmailAction, cancel, isNew, action } = props; - - const defaultData = Object.assign({}, DEFAULT_DATA, action ? action.config : {}); - const [isSaving, setIsSaving] = React.useState(false); - const [showErrors, setShowErrors] = React.useState(false); - const [errors, setErrors] = React.useState( - getMissingFieldErrors(defaultData, DEFAULT_DATA) - ); - const [data, setData] = React.useState(defaultData); - const [createNewService, setCreateNewService] = React.useState(false); - const [newService, setNewService] = React.useState(''); - - React.useEffect(() => { - const missingFieldErrors = getMissingFieldErrors(data, DEFAULT_DATA); - if (!missingFieldErrors.service) { - if (data.service === NEW_SERVICE_ID && !newService) { - missingFieldErrors.service = getRequiredFieldError('service'); - } - } - setErrors(missingFieldErrors); - }, [data, newService]); - - async function saveEmailAction() { - setShowErrors(true); - if (!hasErrors(errors)) { - setShowErrors(false); - setIsSaving(true); - const mergedData = { - ...data, - service: data.service === NEW_SERVICE_ID ? newService : data.service, - }; - try { - await createEmailAction(mergedData); - } catch (err) { - setErrors({ - general: err.body.message, - }); - } - } - } - - const serviceOptions = ALERT_EMAIL_SERVICES.map((service) => ({ - value: service, - inputDisplay: {service}, - dropdownDisplay: {service}, - })); - - serviceOptions.push({ - value: NEW_SERVICE_ID, - inputDisplay: ( - - {i18n.translate('xpack.monitoring.alerts.migrate.manageAction.addingNewServiceText', { - defaultMessage: 'Adding new service...', - })} - - ), - dropdownDisplay: ( - - {i18n.translate('xpack.monitoring.alerts.migrate.manageAction.addNewServiceText', { - defaultMessage: 'Add new service...', - })} - - ), - }); - - let addNewServiceUi = null; - if (createNewService) { - addNewServiceUi = ( - - - setNewService(e.target.value)} - isInvalid={showErrors} - /> - - ); - } - - return ( - - - {i18n.translate('xpack.monitoring.alerts.migrate.manageAction.serviceHelpText', { - defaultMessage: 'Find out more', - })} - - } - error={errors.service} - isInvalid={showErrors && !!errors.service} - > - - { - if (id === NEW_SERVICE_ID) { - setCreateNewService(true); - setData({ ...data, service: NEW_SERVICE_ID }); - } else { - setCreateNewService(false); - setData({ ...data, service: id }); - } - }} - hasDividers - isInvalid={showErrors && !!errors.service} - /> - {addNewServiceUi} - - - - - setData({ ...data, host: e.target.value })} - isInvalid={showErrors && !!errors.host} - /> - - - - setData({ ...data, port: parseInt(e.target.value, 10) })} - isInvalid={showErrors && !!errors.port} - /> - - - - setData({ ...data, secure: e.target.checked })} - /> - - - - setData({ ...data, from: e.target.value })} - isInvalid={showErrors && !!errors.from} - /> - - - - setData({ ...data, user: e.target.value })} - isInvalid={showErrors && !!errors.user} - /> - - - - setData({ ...data, password: e.target.value })} - isInvalid={showErrors && !!errors.password} - /> - - - - - - - - {isNew ? CREATE_LABEL : SAVE_LABEL} - - - {!action || isNew ? null : ( - - {CANCEL_LABEL} - - )} - - - ); -}; diff --git a/x-pack/plugins/monitoring/public/components/alerts/map_severity.js b/x-pack/plugins/monitoring/public/components/alerts/map_severity.js deleted file mode 100644 index 8232e0a8908d0..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/map_severity.js +++ /dev/null @@ -1,75 +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 { upperFirst } from 'lodash'; - -/** - * Map the {@code severity} value to the associated alert level to be usable within the UI. - * - *
    - *
  1. Low: [0, 999) represents an informational level alert.
  2. - *
  3. Medium: [1000, 1999) represents a warning level alert.
  4. - *
  5. High: Any other value.
  6. - *
- * - * The object returned is in the form of: - * - * - * { - * value: 'medium', - * color: 'warning', - * iconType: 'dot', - * title: 'Warning severity alert' - * } - * - * - * @param {Number} severity The number representing the severity. Higher is "worse". - * @return {Object} An object containing details about the severity. - */ - -import { i18n } from '@kbn/i18n'; - -export function mapSeverity(severity) { - const floor = Math.floor(severity / 1000); - let mapped; - - switch (floor) { - case 0: - mapped = { - value: i18n.translate('xpack.monitoring.alerts.lowSeverityName', { defaultMessage: 'low' }), - color: 'warning', - iconType: 'iInCircle', - }; - break; - case 1: - mapped = { - value: i18n.translate('xpack.monitoring.alerts.mediumSeverityName', { - defaultMessage: 'medium', - }), - color: 'warning', - iconType: 'alert', - }; - break; - default: - // severity >= 2000 - mapped = { - value: i18n.translate('xpack.monitoring.alerts.highSeverityName', { - defaultMessage: 'high', - }), - color: 'danger', - iconType: 'bell', - }; - break; - } - - return { - title: i18n.translate('xpack.monitoring.alerts.severityTitle', { - defaultMessage: '{severity} severity alert', - values: { severity: upperFirst(mapped.value) }, - }), - ...mapped, - }; -} diff --git a/x-pack/plugins/monitoring/public/components/alerts/status.test.tsx b/x-pack/plugins/monitoring/public/components/alerts/status.test.tsx deleted file mode 100644 index 1c35328d2f881..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/status.test.tsx +++ /dev/null @@ -1,85 +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 { shallow } from 'enzyme'; -import { Legacy } from '../../legacy_shims'; -import { AlertsStatus, AlertsStatusProps } from './status'; -import { ALERT_TYPES } from '../../../common/constants'; -import { getSetupModeState } from '../../lib/setup_mode'; -import { mockUseEffects } from '../../jest.helpers'; - -jest.mock('../../lib/setup_mode', () => ({ - getSetupModeState: jest.fn(), - addSetupModeCallback: jest.fn(), - toggleSetupMode: jest.fn(), -})); - -jest.mock('../../legacy_shims', () => ({ - Legacy: { - shims: { - kfetch: jest.fn(), - docLinks: { - ELASTIC_WEBSITE_URL: 'https://www.elastic.co/', - DOC_LINK_VERSION: 'current', - }, - }, - }, -})); - -const defaultProps: AlertsStatusProps = { - clusterUuid: '1adsb23', - emailAddress: 'test@elastic.co', -}; - -describe('Status', () => { - beforeEach(() => { - mockUseEffects(2); - - (getSetupModeState as jest.Mock).mockReturnValue({ - enabled: false, - }); - - (Legacy.shims.kfetch as jest.Mock).mockImplementation(({ pathname }) => { - if (pathname === '/internal/security/api_key/privileges') { - return { areApiKeysEnabled: true }; - } - return { - data: [], - }; - }); - }); - - it('should render without setup mode', () => { - const component = shallow(); - expect(component).toMatchSnapshot(); - }); - - it('should render a flyout when clicking the link', async () => { - (getSetupModeState as jest.Mock).mockReturnValue({ - enabled: true, - }); - - const component = shallow(); - component.find('EuiLink').simulate('click'); - await component.update(); - expect(component.find('EuiFlyout')).toMatchSnapshot(); - }); - - it('should render a success message if all alerts have been migrated and in setup mode', async () => { - (Legacy.shims.kfetch as jest.Mock).mockReturnValue({ - data: ALERT_TYPES.map((type) => ({ alertTypeId: type })), - }); - - (getSetupModeState as jest.Mock).mockReturnValue({ - enabled: true, - }); - - const component = shallow(); - await component.update(); - expect(component.find('EuiCallOut')).toMatchSnapshot(); - }); -}); diff --git a/x-pack/plugins/monitoring/public/components/alerts/status.tsx b/x-pack/plugins/monitoring/public/components/alerts/status.tsx deleted file mode 100644 index 6f72168f5069b..0000000000000 --- a/x-pack/plugins/monitoring/public/components/alerts/status.tsx +++ /dev/null @@ -1,207 +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, { Fragment } from 'react'; -import { - EuiSpacer, - EuiCallOut, - EuiTitle, - EuiFlyout, - EuiFlyoutBody, - EuiFlyoutHeader, - EuiLink, - EuiText, -} from '@elastic/eui'; -import { i18n } from '@kbn/i18n'; -import { FormattedMessage } from '@kbn/i18n/react'; -import { Legacy } from '../../legacy_shims'; -import { Alert, BASE_ALERT_API_PATH } from '../../../../alerts/common'; -import { getSetupModeState, addSetupModeCallback, toggleSetupMode } from '../../lib/setup_mode'; -import { NUMBER_OF_MIGRATED_ALERTS, ALERT_TYPE_PREFIX } from '../../../common/constants'; -import { AlertsConfiguration } from './configuration'; - -export interface AlertsStatusProps { - clusterUuid: string; - emailAddress: string; -} - -export const AlertsStatus: React.FC = (props: AlertsStatusProps) => { - const { emailAddress } = props; - - const [setupModeEnabled, setSetupModeEnabled] = React.useState(getSetupModeState().enabled); - const [kibanaAlerts, setKibanaAlerts] = React.useState([]); - const [showMigrationFlyout, setShowMigrationFlyout] = React.useState(false); - const [isSecurityConfigured, setIsSecurityConfigured] = React.useState(false); - - React.useEffect(() => { - async function fetchAlertsStatus() { - const alerts = await Legacy.shims.kfetch({ - method: 'GET', - pathname: `${BASE_ALERT_API_PATH}/_find`, - }); - const monitoringAlerts = alerts.data.filter((alert: Alert) => - alert.alertTypeId.startsWith(ALERT_TYPE_PREFIX) - ); - setKibanaAlerts(monitoringAlerts); - } - - fetchAlertsStatus(); - fetchSecurityConfigured(); - }, [setupModeEnabled, showMigrationFlyout]); - - React.useEffect(() => { - if (!setupModeEnabled && showMigrationFlyout) { - setShowMigrationFlyout(false); - } - }, [setupModeEnabled, showMigrationFlyout]); - - async function fetchSecurityConfigured() { - const response = await Legacy.shims.kfetch({ - pathname: '/internal/security/api_key/privileges', - }); - setIsSecurityConfigured(response.areApiKeysEnabled); - } - - addSetupModeCallback(() => setSetupModeEnabled(getSetupModeState().enabled)); - - function enterSetupModeAndOpenFlyout() { - toggleSetupMode(true); - setShowMigrationFlyout(true); - } - - function getSecurityConfigurationErrorUi() { - if (isSecurityConfigured) { - return null; - } - const { ELASTIC_WEBSITE_URL, DOC_LINK_VERSION } = Legacy.shims.docLinks; - const link = `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/reference/${DOC_LINK_VERSION}/security-settings.html#api-key-service-settings`; - return ( - - - -

- - {i18n.translate( - 'xpack.monitoring.alerts.configuration.securityConfigurationError.docsLinkLabel', - { - defaultMessage: 'docs', - } - )} - - ), - }} - /> -

-
-
- ); - } - - function renderContent() { - let flyout = null; - if (showMigrationFlyout) { - flyout = ( - setShowMigrationFlyout(false)} aria-labelledby="flyoutTitle"> - - -

- {i18n.translate('xpack.monitoring.alerts.status.flyoutTitle', { - defaultMessage: 'Monitoring alerts', - })} -

-
- -

- {i18n.translate('xpack.monitoring.alerts.status.flyoutSubtitle', { - defaultMessage: 'Configure an email server and email address to receive alerts.', - })} -

-
- {getSecurityConfigurationErrorUi()} -
- - setShowMigrationFlyout(false)} - /> - -
- ); - } - - const allMigrated = kibanaAlerts.length >= NUMBER_OF_MIGRATED_ALERTS; - if (allMigrated) { - if (setupModeEnabled) { - return ( - - -

- - {i18n.translate('xpack.monitoring.alerts.status.manage', { - defaultMessage: 'Want to make changes? Click here.', - })} - -

-
- {flyout} -
- ); - } - } else { - return ( - - -

- - {i18n.translate('xpack.monitoring.alerts.status.needToMigrate', { - defaultMessage: 'Migrate cluster alerts to our new alerting platform.', - })} - -

-
- {flyout} -
- ); - } - } - - const content = renderContent(); - if (content) { - return ( - - {content} - - - ); - } - - return null; -}; diff --git a/x-pack/plugins/monitoring/public/components/chart/monitoring_timeseries_container.js b/x-pack/plugins/monitoring/public/components/chart/monitoring_timeseries_container.js index c6bd0773343e0..b760d35cfa2dc 100644 --- a/x-pack/plugins/monitoring/public/components/chart/monitoring_timeseries_container.js +++ b/x-pack/plugins/monitoring/public/components/chart/monitoring_timeseries_container.js @@ -23,6 +23,7 @@ import { } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; import { i18n } from '@kbn/i18n'; +import { AlertsBadge } from '../../alerts/badge'; const zoomOutBtn = (zoomInfo) => { if (!zoomInfo || !zoomInfo.showZoomOutBtn()) { @@ -67,42 +68,56 @@ export function MonitoringTimeseriesContainer({ series, onBrush, zoomInfo }) { }), ].concat(series.map((item) => `${item.metric.label}: ${item.metric.description}`)); + let alertStatus = null; + if (series.alerts) { + alertStatus = ( + + + + ); + } + return ( - + - - - -

- {getTitle(series)} - {units ? ` (${units})` : ''} - - - - - -

-
-
+ - - } - /> - - - {seriesScreenReaderTextList.join('. ')} - - - + + + +

+ {getTitle(series)} + {units ? ` (${units})` : ''} + + + + + +

+
+
+ + + } + /> + + + {seriesScreenReaderTextList.join('. ')} + + + + + {zoomOutBtn(zoomInfo)} +
- {zoomOutBtn(zoomInfo)} + {alertStatus}
diff --git a/x-pack/plugins/monitoring/public/components/cluster/listing/alerts_indicator.js b/x-pack/plugins/monitoring/public/components/cluster/listing/alerts_indicator.js deleted file mode 100644 index 68d7a5a94e42f..0000000000000 --- a/x-pack/plugins/monitoring/public/components/cluster/listing/alerts_indicator.js +++ /dev/null @@ -1,87 +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 { mapSeverity } from '../../alerts/map_severity'; -import { EuiHealth, EuiToolTip } from '@elastic/eui'; -import { FormattedMessage } from '@kbn/i18n/react'; -import { i18n } from '@kbn/i18n'; - -const HIGH_SEVERITY = 2000; -const MEDIUM_SEVERITY = 1000; -const LOW_SEVERITY = 0; - -export function AlertsIndicator({ alerts }) { - if (alerts && alerts.count > 0) { - const severity = (() => { - if (alerts.high > 0) { - return HIGH_SEVERITY; - } - if (alerts.medium > 0) { - return MEDIUM_SEVERITY; - } - return LOW_SEVERITY; - })(); - const severityIcon = mapSeverity(severity); - const tooltipText = (() => { - switch (severity) { - case HIGH_SEVERITY: - return i18n.translate( - 'xpack.monitoring.cluster.listing.alertsInticator.highSeverityTooltip', - { - defaultMessage: - 'There are some critical cluster issues that require your immediate attention!', - } - ); - case MEDIUM_SEVERITY: - return i18n.translate( - 'xpack.monitoring.cluster.listing.alertsInticator.mediumSeverityTooltip', - { - defaultMessage: 'There are some issues that might have impact on your cluster.', - } - ); - default: - // might never show - return i18n.translate( - 'xpack.monitoring.cluster.listing.alertsInticator.lowSeverityTooltip', - { - defaultMessage: 'There are some low-severity cluster issues', - } - ); - } - })(); - - return ( - - - - - - ); - } - - return ( - - - - - - ); -} diff --git a/x-pack/plugins/monitoring/public/components/cluster/listing/listing.js b/x-pack/plugins/monitoring/public/components/cluster/listing/listing.js index b90e7b52f4962..4dc4201e358fb 100644 --- a/x-pack/plugins/monitoring/public/components/cluster/listing/listing.js +++ b/x-pack/plugins/monitoring/public/components/cluster/listing/listing.js @@ -14,16 +14,16 @@ import { EuiPage, EuiPageBody, EuiPageContent, - EuiToolTip, EuiCallOut, EuiSpacer, EuiIcon, + EuiToolTip, } from '@elastic/eui'; import { EuiMonitoringTable } from '../../table'; -import { AlertsIndicator } from '../../cluster/listing/alerts_indicator'; import { FormattedMessage } from '@kbn/i18n/react'; import { i18n } from '@kbn/i18n'; import { toMountPoint } from '../../../../../../../src/plugins/kibana_react/public'; +import { AlertsStatus } from '../../../alerts/status'; import { STANDALONE_CLUSTER_CLUSTER_UUID } from '../../../../common/constants'; import './listing.scss'; @@ -31,8 +31,6 @@ const IsClusterSupported = ({ isSupported, children }) => { return isSupported ? children : '-'; }; -const STANDALONE_CLUSTER_STORAGE_KEY = 'viewedStandaloneCluster'; - /* * This checks if alerts feature is supported via monitoring cluster * license. If the alerts feature is not supported because the prod cluster @@ -61,6 +59,8 @@ const IsAlertsSupported = (props) => { ); }; +const STANDALONE_CLUSTER_STORAGE_KEY = 'viewedStandaloneCluster'; + const getColumns = ( showLicenseExpiration, changeCluster, @@ -119,7 +119,7 @@ const getColumns = ( render: (_status, cluster) => ( - + ), diff --git a/x-pack/plugins/monitoring/public/components/cluster/overview/alerts_panel.js b/x-pack/plugins/monitoring/public/components/cluster/overview/alerts_panel.js deleted file mode 100644 index 2dc76aa7e4496..0000000000000 --- a/x-pack/plugins/monitoring/public/components/cluster/overview/alerts_panel.js +++ /dev/null @@ -1,201 +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, { Fragment } from 'react'; -import moment from 'moment-timezone'; -import { FormattedAlert } from '../../alerts/formatted_alert'; -import { mapSeverity } from '../../alerts/map_severity'; -import { formatTimestampToDuration } from '../../../../common/format_timestamp_to_duration'; -import { - CALCULATE_DURATION_SINCE, - KIBANA_ALERTING_ENABLED, - CALCULATE_DURATION_UNTIL, -} from '../../../../common/constants'; -import { formatDateTimeLocal } from '../../../../common/formatting'; -import { FormattedMessage } from '@kbn/i18n/react'; -import { i18n } from '@kbn/i18n'; -import { getSafeForExternalLink } from '../../../lib/get_safe_for_external_link'; - -import { - EuiFlexGroup, - EuiFlexItem, - EuiTitle, - EuiButton, - EuiText, - EuiSpacer, - EuiCallOut, - EuiLink, -} from '@elastic/eui'; - -function replaceTokens(alert) { - if (!alert.message.tokens) { - return alert.message.text; - } - - let text = alert.message.text; - - for (const token of alert.message.tokens) { - if (token.type === 'time') { - text = text.replace( - token.startToken, - token.isRelative - ? formatTimestampToDuration(alert.expirationTime, CALCULATE_DURATION_UNTIL) - : moment.tz(alert.expirationTime, moment.tz.guess()).format('LLL z') - ); - } else if (token.type === 'link') { - const linkPart = new RegExp(`${token.startToken}(.+?)${token.endToken}`).exec(text); - // TODO: we assume this is at the end, which works for now but will not always work - const nonLinkText = text.replace(linkPart[0], ''); - text = ( - - {nonLinkText} - {linkPart[1]} - - ); - } - } - - return text; -} - -export function AlertsPanel({ alerts }) { - if (!alerts || !alerts.length) { - // no-op - return null; - } - - // enclosed component for accessing - function TopAlertItem({ item, index }) { - const severityIcon = mapSeverity(item.metadata.severity); - - if (item.resolved_timestamp) { - severityIcon.title = i18n.translate( - 'xpack.monitoring.cluster.overview.alertsPanel.severityIconTitle', - { - defaultMessage: '{severityIconTitle} (resolved {time} ago)', - values: { - severityIconTitle: severityIcon.title, - time: formatTimestampToDuration(item.resolved_timestamp, CALCULATE_DURATION_SINCE), - }, - } - ); - severityIcon.color = 'success'; - severityIcon.iconType = 'check'; - } - - return ( - - - - -

- -

-
-
- ); - } - - const alertsList = KIBANA_ALERTING_ENABLED - ? alerts.map((alert, idx) => { - const callOutProps = mapSeverity(alert.severity); - const message = replaceTokens(alert); - - if (!alert.isFiring) { - callOutProps.title = i18n.translate( - 'xpack.monitoring.cluster.overview.alertsPanel.severityIconTitle', - { - defaultMessage: '{severityIconTitle} (resolved {time} ago)', - values: { - severityIconTitle: callOutProps.title, - time: formatTimestampToDuration(alert.resolvedMS, CALCULATE_DURATION_SINCE), - }, - } - ); - callOutProps.color = 'success'; - callOutProps.iconType = 'check'; - } - - return ( - - -

{message}

- - -

- -

-
-
- -
- ); - }) - : alerts.map((item, index) => ( - - )); - - return ( -
- - - -

- -

-
-
- - - - - -
- - {alertsList} - -
- ); -} diff --git a/x-pack/plugins/monitoring/public/components/cluster/overview/elasticsearch_panel.js b/x-pack/plugins/monitoring/public/components/cluster/overview/elasticsearch_panel.js index 034bacfb3bf62..edf4c5d73f837 100644 --- a/x-pack/plugins/monitoring/public/components/cluster/overview/elasticsearch_panel.js +++ b/x-pack/plugins/monitoring/public/components/cluster/overview/elasticsearch_panel.js @@ -5,11 +5,11 @@ */ import React, { Fragment } from 'react'; +import moment from 'moment-timezone'; import { get, capitalize } from 'lodash'; import { formatNumber } from '../../../lib/format_number'; import { ClusterItemContainer, - HealthStatusIndicator, BytesPercentageUsage, DisabledIfNoDataAndInSetupModeLink, } from './helpers'; @@ -26,14 +26,24 @@ import { EuiBadge, EuiToolTip, EuiFlexGroup, + EuiHealth, + EuiText, } from '@elastic/eui'; -import { LicenseText } from './license_text'; import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; import { Reason } from '../../logs/reason'; import { SetupModeTooltip } from '../../setup_mode/tooltip'; import { getSafeForExternalLink } from '../../../lib/get_safe_for_external_link'; -import { ELASTICSEARCH_SYSTEM_ID } from '../../../../common/constants'; +import { + ELASTICSEARCH_SYSTEM_ID, + ALERT_LICENSE_EXPIRATION, + ALERT_CLUSTER_HEALTH, + ALERT_CPU_USAGE, + ALERT_NODES_CHANGED, + ALERT_ELASTICSEARCH_VERSION_MISMATCH, +} from '../../../../common/constants'; +import { AlertsBadge } from '../../../alerts/badge'; +import { shouldShowAlertBadge } from '../../../alerts/lib/should_show_alert_badge'; const calculateShards = (shards) => { const total = get(shards, 'total', 0); @@ -53,6 +63,8 @@ const calculateShards = (shards) => { }; }; +const formatDateLocal = (input) => moment.tz(input, moment.tz.guess()).format('LL'); + function getBadgeColorFromLogLevel(level) { switch (level) { case 'warn': @@ -138,11 +150,20 @@ function renderLog(log) { ); } +const OVERVIEW_PANEL_ALERTS = [ALERT_CLUSTER_HEALTH, ALERT_LICENSE_EXPIRATION]; + +const NODES_PANEL_ALERTS = [ + ALERT_CPU_USAGE, + ALERT_NODES_CHANGED, + ALERT_ELASTICSEARCH_VERSION_MISMATCH, +]; + export function ElasticsearchPanel(props) { const clusterStats = props.cluster_stats || {}; const nodes = clusterStats.nodes; const indices = clusterStats.indices; const setupMode = props.setupMode; + const alerts = props.alerts; const goToElasticsearch = () => getSafeForExternalLink('#/elasticsearch'); const goToNodes = () => getSafeForExternalLink('#/elasticsearch/nodes'); @@ -150,12 +171,6 @@ export function ElasticsearchPanel(props) { const { primaries, replicas } = calculateShards(get(props, 'cluster_stats.indices.shards', {})); - const statusIndicator = ; - - const licenseText = ( - - ); - const setupModeData = get(setupMode.data, 'elasticsearch'); const setupModeTooltip = setupMode && setupMode.enabled ? ( @@ -199,40 +214,80 @@ export function ElasticsearchPanel(props) { return null; }; + const statusColorMap = { + green: 'success', + yellow: 'warning', + red: 'danger', + }; + + let nodesAlertStatus = null; + if (shouldShowAlertBadge(alerts, NODES_PANEL_ALERTS)) { + const alertsList = NODES_PANEL_ALERTS.map((alertType) => alerts[alertType]); + nodesAlertStatus = ( + + + + ); + } + + let overviewAlertStatus = null; + if (shouldShowAlertBadge(alerts, OVERVIEW_PANEL_ALERTS)) { + const alertsList = OVERVIEW_PANEL_ALERTS.map((alertType) => alerts[alertType]); + overviewAlertStatus = ( + + + + ); + } + return ( - + - -

- - - -

-
+ + + +

+ + + +

+
+
+ {overviewAlertStatus} +
+ + + + + + + + {showMlJobs()} + + + + + + + + {capitalize(props.license.type)} + + + + + {props.license.expiry_date_in_millis === undefined ? ( + '' + ) : ( + + )} + + + +
- +

@@ -280,7 +365,12 @@ export function ElasticsearchPanel(props) {

- {setupModeTooltip} + + + {setupModeTooltip} + {nodesAlertStatus} + +
diff --git a/x-pack/plugins/monitoring/public/components/cluster/overview/helpers.js b/x-pack/plugins/monitoring/public/components/cluster/overview/helpers.js index 0d9290225cd5f..4f6fa520750bd 100644 --- a/x-pack/plugins/monitoring/public/components/cluster/overview/helpers.js +++ b/x-pack/plugins/monitoring/public/components/cluster/overview/helpers.js @@ -29,13 +29,17 @@ export function HealthStatusIndicator(props) { const statusColor = statusColorMap[props.status] || 'n/a'; return ( - - - + + + + + + + ); } diff --git a/x-pack/plugins/monitoring/public/components/cluster/overview/index.js b/x-pack/plugins/monitoring/public/components/cluster/overview/index.js index 88c626b5ad5ae..66701c1dfd95a 100644 --- a/x-pack/plugins/monitoring/public/components/cluster/overview/index.js +++ b/x-pack/plugins/monitoring/public/components/cluster/overview/index.js @@ -8,24 +8,14 @@ import React, { Fragment } from 'react'; import { ElasticsearchPanel } from './elasticsearch_panel'; import { KibanaPanel } from './kibana_panel'; import { LogstashPanel } from './logstash_panel'; -import { AlertsPanel } from './alerts_panel'; import { BeatsPanel } from './beats_panel'; import { EuiPage, EuiPageBody, EuiScreenReaderOnly } from '@elastic/eui'; import { ApmPanel } from './apm_panel'; import { FormattedMessage } from '@kbn/i18n/react'; -import { AlertsStatus } from '../../alerts/status'; -import { - STANDALONE_CLUSTER_CLUSTER_UUID, - KIBANA_ALERTING_ENABLED, -} from '../../../../common/constants'; +import { STANDALONE_CLUSTER_CLUSTER_UUID } from '../../../../common/constants'; export function Overview(props) { const isFromStandaloneCluster = props.cluster.cluster_uuid === STANDALONE_CLUSTER_CLUSTER_UUID; - - const kibanaAlerts = KIBANA_ALERTING_ENABLED ? ( - - ) : null; - return ( @@ -38,10 +28,6 @@ export function Overview(props) {

- {kibanaAlerts} - - - {!isFromStandaloneCluster ? ( + - ) : null} - + diff --git a/x-pack/plugins/monitoring/public/components/cluster/overview/kibana_panel.js b/x-pack/plugins/monitoring/public/components/cluster/overview/kibana_panel.js index 8bf2bc472b8fd..eb1f82eb5550d 100644 --- a/x-pack/plugins/monitoring/public/components/cluster/overview/kibana_panel.js +++ b/x-pack/plugins/monitoring/public/components/cluster/overview/kibana_panel.js @@ -28,11 +28,16 @@ import { import { FormattedMessage } from '@kbn/i18n/react'; import { i18n } from '@kbn/i18n'; import { SetupModeTooltip } from '../../setup_mode/tooltip'; -import { KIBANA_SYSTEM_ID } from '../../../../common/constants'; +import { KIBANA_SYSTEM_ID, ALERT_KIBANA_VERSION_MISMATCH } from '../../../../common/constants'; import { getSafeForExternalLink } from '../../../lib/get_safe_for_external_link'; +import { AlertsBadge } from '../../../alerts/badge'; +import { shouldShowAlertBadge } from '../../../alerts/lib/should_show_alert_badge'; + +const INSTANCES_PANEL_ALERTS = [ALERT_KIBANA_VERSION_MISMATCH]; export function KibanaPanel(props) { const setupMode = props.setupMode; + const alerts = props.alerts; const showDetectedKibanas = setupMode.enabled && get(setupMode.data, 'kibana.detected.doesExist', false); if (!props.count && !showDetectedKibanas) { @@ -54,6 +59,16 @@ export function KibanaPanel(props) { /> ) : null; + let instancesAlertStatus = null; + if (shouldShowAlertBadge(alerts, INSTANCES_PANEL_ALERTS)) { + const alertsList = INSTANCES_PANEL_ALERTS.map((alertType) => alerts[alertType]); + instancesAlertStatus = ( + + + + ); + } + return ( - +

@@ -148,7 +163,12 @@ export function KibanaPanel(props) {

- {setupModeTooltip} + + + {setupModeTooltip} + {instancesAlertStatus} + +
diff --git a/x-pack/plugins/monitoring/public/components/cluster/overview/license_text.js b/x-pack/plugins/monitoring/public/components/cluster/overview/license_text.js deleted file mode 100644 index 19905b9d7791a..0000000000000 --- a/x-pack/plugins/monitoring/public/components/cluster/overview/license_text.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 moment from 'moment-timezone'; -import { getSafeForExternalLink } from '../../../lib/get_safe_for_external_link'; -import { capitalize } from 'lodash'; -import { EuiLink } from '@elastic/eui'; -import { FormattedMessage } from '@kbn/i18n/react'; - -const formatDateLocal = (input) => moment.tz(input, moment.tz.guess()).format('LL'); - -export function LicenseText({ license, showLicenseExpiration }) { - if (!showLicenseExpiration) { - return null; - } - - return ( - - - ), - }} - /> - - ); -} diff --git a/x-pack/plugins/monitoring/public/components/cluster/overview/logstash_panel.js b/x-pack/plugins/monitoring/public/components/cluster/overview/logstash_panel.js index e81f9b64dcb4b..7c9758bc0ddb6 100644 --- a/x-pack/plugins/monitoring/public/components/cluster/overview/logstash_panel.js +++ b/x-pack/plugins/monitoring/public/components/cluster/overview/logstash_panel.js @@ -11,7 +11,11 @@ import { BytesPercentageUsage, DisabledIfNoDataAndInSetupModeLink, } from './helpers'; -import { LOGSTASH, LOGSTASH_SYSTEM_ID } from '../../../../common/constants'; +import { + LOGSTASH, + LOGSTASH_SYSTEM_ID, + ALERT_LOGSTASH_VERSION_MISMATCH, +} from '../../../../common/constants'; import { EuiFlexGrid, @@ -31,11 +35,16 @@ import { i18n } from '@kbn/i18n'; import { get } from 'lodash'; import { SetupModeTooltip } from '../../setup_mode/tooltip'; import { getSafeForExternalLink } from '../../../lib/get_safe_for_external_link'; +import { AlertsBadge } from '../../../alerts/badge'; +import { shouldShowAlertBadge } from '../../../alerts/lib/should_show_alert_badge'; + +const NODES_PANEL_ALERTS = [ALERT_LOGSTASH_VERSION_MISMATCH]; export function LogstashPanel(props) { const { setupMode } = props; const nodesCount = props.node_count || 0; const queueTypes = props.queue_types || {}; + const alerts = props.alerts; // Do not show if we are not in setup mode if (!nodesCount && !setupMode.enabled) { @@ -56,6 +65,16 @@ export function LogstashPanel(props) { /> ) : null; + let nodesAlertStatus = null; + if (shouldShowAlertBadge(alerts, NODES_PANEL_ALERTS)) { + const alertsList = NODES_PANEL_ALERTS.map((alertType) => alerts[alertType]); + nodesAlertStatus = ( + + + + ); + } + return ( - +

@@ -141,7 +160,12 @@ export function LogstashPanel(props) {

- {setupModeTooltip} + + + {setupModeTooltip} + {nodesAlertStatus} + +
diff --git a/x-pack/plugins/monitoring/public/components/elasticsearch/cluster_status/index.js b/x-pack/plugins/monitoring/public/components/elasticsearch/cluster_status/index.js index aea2456a3f3d4..ba19ed0ae1913 100644 --- a/x-pack/plugins/monitoring/public/components/elasticsearch/cluster_status/index.js +++ b/x-pack/plugins/monitoring/public/components/elasticsearch/cluster_status/index.js @@ -10,7 +10,7 @@ import { ElasticsearchStatusIcon } from '../status_icon'; import { formatMetric } from '../../../lib/format_number'; import { i18n } from '@kbn/i18n'; -export function ClusterStatus({ stats }) { +export function ClusterStatus({ stats, alerts }) { const { dataSize, nodesCount, @@ -81,6 +81,7 @@ export function ClusterStatus({ stats }) { diff --git a/x-pack/plugins/monitoring/public/components/elasticsearch/node/node.js b/x-pack/plugins/monitoring/public/components/elasticsearch/node/node.js index 418661ff322e4..f91e251030d76 100644 --- a/x-pack/plugins/monitoring/public/components/elasticsearch/node/node.js +++ b/x-pack/plugins/monitoring/public/components/elasticsearch/node/node.js @@ -5,6 +5,7 @@ */ import React from 'react'; +import { get } from 'lodash'; import { EuiPage, EuiPageContent, @@ -20,8 +21,33 @@ import { Logs } from '../../logs/'; import { MonitoringTimeseriesContainer } from '../../chart'; import { ShardAllocation } from '../shard_allocation/shard_allocation'; import { FormattedMessage } from '@kbn/i18n/react'; +import { AlertsCallout } from '../../../alerts/callout'; + +export const Node = ({ + nodeSummary, + metrics, + logs, + alerts, + nodeId, + clusterUuid, + scope, + ...props +}) => { + if (alerts) { + for (const alertTypeId of Object.keys(alerts)) { + const alertInstance = alerts[alertTypeId]; + for (const { meta } of alertInstance.states) { + const metricList = get(meta, 'metrics', []); + for (const metric of metricList) { + if (metrics[metric]) { + metrics[metric].alerts = metrics[metric].alerts || {}; + metrics[metric].alerts[alertTypeId] = alertInstance; + } + } + } + } + } -export const Node = ({ nodeSummary, metrics, logs, nodeId, clusterUuid, scope, ...props }) => { const metricsToShow = [ metrics.node_jvm_mem, metrics.node_mem, @@ -31,6 +57,7 @@ export const Node = ({ nodeSummary, metrics, logs, nodeId, clusterUuid, scope, . metrics.node_latency, metrics.node_segment_count, ]; + return ( @@ -43,9 +70,10 @@ export const Node = ({ nodeSummary, metrics, logs, nodeId, clusterUuid, scope, . - + + {metricsToShow.map((metric, index) => ( diff --git a/x-pack/plugins/monitoring/public/components/elasticsearch/node_detail_status/index.js b/x-pack/plugins/monitoring/public/components/elasticsearch/node_detail_status/index.js index f912d2755b0c7..18533b3bd4b5e 100644 --- a/x-pack/plugins/monitoring/public/components/elasticsearch/node_detail_status/index.js +++ b/x-pack/plugins/monitoring/public/components/elasticsearch/node_detail_status/index.js @@ -10,7 +10,7 @@ import { NodeStatusIcon } from '../node'; import { formatMetric } from '../../../lib/format_number'; import { i18n } from '@kbn/i18n'; -export function NodeDetailStatus({ stats }) { +export function NodeDetailStatus({ stats, alerts }) { const { transport_address: transportAddress, usedHeap, @@ -28,6 +28,10 @@ export function NodeDetailStatus({ stats }) { const percentSpaceUsed = (freeSpace / totalSpace) * 100; const metrics = [ + { + label: 'Alerts', + value: {Object.values(alerts).length}, + }, { label: i18n.translate('xpack.monitoring.elasticsearch.nodeDetailStatus.transportAddress', { defaultMessage: 'Transport Address', diff --git a/x-pack/plugins/monitoring/public/components/elasticsearch/nodes/nodes.js b/x-pack/plugins/monitoring/public/components/elasticsearch/nodes/nodes.js index 8844388f8647a..c2e5c8e22a1c0 100644 --- a/x-pack/plugins/monitoring/public/components/elasticsearch/nodes/nodes.js +++ b/x-pack/plugins/monitoring/public/components/elasticsearch/nodes/nodes.js @@ -5,7 +5,6 @@ */ import React, { Fragment } from 'react'; -import { NodeStatusIcon } from '../node'; import { extractIp } from '../../../lib/extract_ip'; // TODO this is only used for elasticsearch nodes summary / node detail, so it should be moved to components/elasticsearch/nodes/lib import { getSafeForExternalLink } from '../../../lib/get_safe_for_external_link'; import { ClusterStatus } from '../cluster_status'; @@ -25,12 +24,14 @@ import { EuiButton, EuiText, EuiScreenReaderOnly, + EuiHealth, } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import _ from 'lodash'; import { ELASTICSEARCH_SYSTEM_ID } from '../../../../common/constants'; import { FormattedMessage } from '@kbn/i18n/react'; import { ListingCallOut } from '../../setup_mode/listing_callout'; +import { AlertsStatus } from '../../../alerts/status'; const getNodeTooltip = (node) => { const { nodeTypeLabel, nodeTypeClass } = node; @@ -56,7 +57,7 @@ const getNodeTooltip = (node) => { }; const getSortHandler = (type) => (item) => _.get(item, [type, 'summary', 'lastVal']); -const getColumns = (showCgroupMetricsElasticsearch, setupMode, clusterUuid) => { +const getColumns = (showCgroupMetricsElasticsearch, setupMode, clusterUuid, alerts) => { const cols = []; const cpuUsageColumnTitle = i18n.translate( @@ -123,6 +124,18 @@ const getColumns = (showCgroupMetricsElasticsearch, setupMode, clusterUuid) => { }, }); + cols.push({ + name: i18n.translate('xpack.monitoring.elasticsearch.nodes.alertsColumnTitle', { + defaultMessage: 'Alerts', + }), + field: 'alerts', + width: '175px', + sortable: true, + render: () => { + return ; + }, + }); + cols.push({ name: i18n.translate('xpack.monitoring.elasticsearch.nodes.statusColumnTitle', { defaultMessage: 'Status', @@ -138,9 +151,20 @@ const getColumns = (showCgroupMetricsElasticsearch, setupMode, clusterUuid) => { defaultMessage: 'Offline', }); return ( -
- {status} -
+ + + {status} + + ); }, }); @@ -197,14 +221,16 @@ const getColumns = (showCgroupMetricsElasticsearch, setupMode, clusterUuid) => { name: cpuUsageColumnTitle, field: 'node_cpu_utilization', sortable: getSortHandler('node_cpu_utilization'), - render: (value, node) => ( - - ), + render: (value, node) => { + return ( + + ); + }, }); cols.push({ @@ -263,8 +289,17 @@ const getColumns = (showCgroupMetricsElasticsearch, setupMode, clusterUuid) => { }; export function ElasticsearchNodes({ clusterStatus, showCgroupMetricsElasticsearch, ...props }) { - const { sorting, pagination, onTableChange, clusterUuid, setupMode, fetchMoreData } = props; - const columns = getColumns(showCgroupMetricsElasticsearch, setupMode, clusterUuid); + const { + sorting, + pagination, + onTableChange, + clusterUuid, + setupMode, + fetchMoreData, + alerts, + } = props; + + const columns = getColumns(showCgroupMetricsElasticsearch, setupMode, clusterUuid, alerts); // Merge the nodes data with the setup data if enabled const nodes = props.nodes || []; @@ -392,7 +427,7 @@ export function ElasticsearchNodes({ clusterStatus, showCgroupMetricsElasticsear return ( - + diff --git a/x-pack/plugins/monitoring/public/components/kibana/cluster_status/index.js b/x-pack/plugins/monitoring/public/components/kibana/cluster_status/index.js index c9b95eb4876d8..32d2bdadcea96 100644 --- a/x-pack/plugins/monitoring/public/components/kibana/cluster_status/index.js +++ b/x-pack/plugins/monitoring/public/components/kibana/cluster_status/index.js @@ -10,7 +10,7 @@ import { KibanaStatusIcon } from '../status_icon'; import { formatMetric } from '../../../lib/format_number'; import { i18n } from '@kbn/i18n'; -export function ClusterStatus({ stats }) { +export function ClusterStatus({ stats, alerts }) { const { concurrent_connections: connections, count: instances, @@ -65,6 +65,7 @@ export function ClusterStatus({ stats }) { diff --git a/x-pack/plugins/monitoring/public/components/kibana/instances/instances.js b/x-pack/plugins/monitoring/public/components/kibana/instances/instances.js index 9f960c8ddea09..95a9276569bb1 100644 --- a/x-pack/plugins/monitoring/public/components/kibana/instances/instances.js +++ b/x-pack/plugins/monitoring/public/components/kibana/instances/instances.js @@ -14,11 +14,12 @@ import { EuiLink, EuiCallOut, EuiScreenReaderOnly, + EuiToolTip, + EuiHealth, } from '@elastic/eui'; import { capitalize, get } from 'lodash'; import { ClusterStatus } from '../cluster_status'; import { EuiMonitoringTable } from '../../table'; -import { KibanaStatusIcon } from '../status_icon'; import { StatusIcon } from '../../status_icon'; import { formatMetric, formatNumber } from '../../../lib/format_number'; import { getSafeForExternalLink } from '../../../lib/get_safe_for_external_link'; @@ -27,8 +28,9 @@ import { FormattedMessage } from '@kbn/i18n/react'; import { SetupModeBadge } from '../../setup_mode/badge'; import { KIBANA_SYSTEM_ID } from '../../../../common/constants'; import { ListingCallOut } from '../../setup_mode/listing_callout'; +import { AlertsStatus } from '../../../alerts/status'; -const getColumns = (setupMode) => { +const getColumns = (setupMode, alerts) => { const columns = [ { name: i18n.translate('xpack.monitoring.kibana.listing.nameColumnTitle', { @@ -79,33 +81,34 @@ const getColumns = (setupMode) => { ); }, }, + { + name: i18n.translate('xpack.monitoring.kibana.listing.alertsColumnTitle', { + defaultMessage: 'Alerts', + }), + field: 'isOnline', + width: '175px', + sortable: true, + render: () => { + return ; + }, + }, { name: i18n.translate('xpack.monitoring.kibana.listing.statusColumnTitle', { defaultMessage: 'Status', }), field: 'status', - render: (status, kibana) => ( -
- -   - {!kibana.availability ? ( - - ) : ( - capitalize(status) - )} -
- ), + render: (status, kibana) => { + return ( + + + {capitalize(status)} + + + ); + }, }, { name: i18n.translate('xpack.monitoring.kibana.listing.loadAverageColumnTitle', { @@ -158,7 +161,7 @@ const getColumns = (setupMode) => { export class KibanaInstances extends PureComponent { render() { - const { clusterStatus, setupMode, sorting, pagination, onTableChange } = this.props; + const { clusterStatus, alerts, setupMode, sorting, pagination, onTableChange } = this.props; let setupModeCallOut = null; // Merge the instances data with the setup data if enabled @@ -254,7 +257,7 @@ export class KibanaInstances extends PureComponent { - + {setupModeCallOut} @@ -262,7 +265,7 @@ export class KibanaInstances extends PureComponent { ({ Legacy: { shims: { getBasePath: () => '', - capabilities: { - get: () => ({ logs: { show: true } }), - }, + capabilities: { logs: { show: true } }, }, }, })); diff --git a/x-pack/plugins/monitoring/public/components/logstash/cluster_status/index.js b/x-pack/plugins/monitoring/public/components/logstash/cluster_status/index.js index 9d5a6a184b4e8..abd18b61da8ff 100644 --- a/x-pack/plugins/monitoring/public/components/logstash/cluster_status/index.js +++ b/x-pack/plugins/monitoring/public/components/logstash/cluster_status/index.js @@ -9,7 +9,7 @@ import { SummaryStatus } from '../../summary_status'; import { formatMetric } from '../../../lib/format_number'; import { i18n } from '@kbn/i18n'; -export function ClusterStatus({ stats }) { +export function ClusterStatus({ stats, alerts }) { const { node_count: nodeCount, avg_memory_used: avgMemoryUsed, @@ -49,5 +49,5 @@ export function ClusterStatus({ stats }) { }, ]; - return ; + return ; } diff --git a/x-pack/plugins/monitoring/public/components/logstash/listing/__snapshots__/listing.test.js.snap b/x-pack/plugins/monitoring/public/components/logstash/listing/__snapshots__/listing.test.js.snap index edb7d139bb935..2e01fce7247dc 100644 --- a/x-pack/plugins/monitoring/public/components/logstash/listing/__snapshots__/listing.test.js.snap +++ b/x-pack/plugins/monitoring/public/components/logstash/listing/__snapshots__/listing.test.js.snap @@ -11,6 +11,13 @@ exports[`Listing should render with certain data pieces missing 1`] = ` "render": [Function], "sortable": true, }, + Object { + "field": "isOnline", + "name": "Alerts", + "render": [Function], + "sortable": true, + "width": "175px", + }, Object { "field": "cpu_usage", "name": "CPU Usage", @@ -106,6 +113,13 @@ exports[`Listing should render with expected props 1`] = ` "render": [Function], "sortable": true, }, + Object { + "field": "isOnline", + "name": "Alerts", + "render": [Function], + "sortable": true, + "width": "175px", + }, Object { "field": "cpu_usage", "name": "CPU Usage", 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 78eb982a95dd7..caa21e5e69292 100644 --- a/x-pack/plugins/monitoring/public/components/logstash/listing/listing.js +++ b/x-pack/plugins/monitoring/public/components/logstash/listing/listing.js @@ -16,7 +16,7 @@ import { EuiScreenReaderOnly, } from '@elastic/eui'; import { formatPercentageUsage, formatNumber } from '../../../lib/format_number'; -import { ClusterStatus } from '..//cluster_status'; +import { ClusterStatus } from '../cluster_status'; import { EuiMonitoringTable } from '../../table'; import { FormattedMessage } from '@kbn/i18n/react'; import { i18n } from '@kbn/i18n'; @@ -24,10 +24,12 @@ import { LOGSTASH_SYSTEM_ID } from '../../../../common/constants'; import { SetupModeBadge } from '../../setup_mode/badge'; import { ListingCallOut } from '../../setup_mode/listing_callout'; import { getSafeForExternalLink } from '../../../lib/get_safe_for_external_link'; +import { AlertsStatus } from '../../../alerts/status'; export class Listing extends PureComponent { getColumns() { const setupMode = this.props.setupMode; + const alerts = this.props.alerts; return [ { @@ -72,6 +74,17 @@ export class Listing extends PureComponent { ); }, }, + { + name: i18n.translate('xpack.monitoring.logstash.nodes.alertsColumnTitle', { + defaultMessage: 'Alerts', + }), + field: 'isOnline', + width: '175px', + sortable: true, + render: () => { + return ; + }, + }, { name: i18n.translate('xpack.monitoring.logstash.nodes.cpuUsageTitle', { defaultMessage: 'CPU Usage', @@ -141,7 +154,7 @@ export class Listing extends PureComponent { } render() { - const { stats, sorting, pagination, onTableChange, data, setupMode } = this.props; + const { stats, alerts, sorting, pagination, onTableChange, data, setupMode } = this.props; const columns = this.getColumns(); const flattenedData = data.map((item) => ({ ...item, @@ -176,7 +189,7 @@ export class Listing extends PureComponent { - + {setupModeCallOut} diff --git a/x-pack/plugins/monitoring/public/components/renderers/setup_mode.js b/x-pack/plugins/monitoring/public/components/renderers/setup_mode.js index 5b52f5d85d44d..21e5c1708a05c 100644 --- a/x-pack/plugins/monitoring/public/components/renderers/setup_mode.js +++ b/x-pack/plugins/monitoring/public/components/renderers/setup_mode.js @@ -116,7 +116,7 @@ export class SetupModeRenderer extends React.Component { } getBottomBar(setupModeState) { - if (!setupModeState.enabled) { + if (!setupModeState.enabled || setupModeState.hideBottomBar) { return null; } diff --git a/x-pack/plugins/monitoring/public/components/summary_status/summary_status.js b/x-pack/plugins/monitoring/public/components/summary_status/summary_status.js index 943e100dc5409..8175806cb192a 100644 --- a/x-pack/plugins/monitoring/public/components/summary_status/summary_status.js +++ b/x-pack/plugins/monitoring/public/components/summary_status/summary_status.js @@ -9,6 +9,7 @@ import PropTypes from 'prop-types'; import { isEmpty, capitalize } from 'lodash'; import { EuiFlexGroup, EuiFlexItem, EuiStat } from '@elastic/eui'; import { StatusIcon } from '../status_icon/index.js'; +import { AlertsStatus } from '../../alerts/status'; import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; import './summary_status.scss'; @@ -86,6 +87,7 @@ const StatusIndicator = ({ status, isOnline, IconComponent }) => { export function SummaryStatus({ metrics, status, + alerts, isOnline, IconComponent = DefaultIconComponent, ...props @@ -94,6 +96,19 @@ export function SummaryStatus({
+ {alerts ? ( + + } + titleSize="xxxs" + textAlign="left" + className="monSummaryStatusNoWrap__stat" + description={i18n.translate('xpack.monitoring.summaryStatus.alertsDescription', { + defaultMessage: 'Alerts', + })} + /> + + ) : null} {metrics.map(wrapChild)}
diff --git a/x-pack/plugins/monitoring/public/legacy_shims.ts b/x-pack/plugins/monitoring/public/legacy_shims.ts index 450a34b797c38..0f979e5637d68 100644 --- a/x-pack/plugins/monitoring/public/legacy_shims.ts +++ b/x-pack/plugins/monitoring/public/legacy_shims.ts @@ -4,11 +4,16 @@ * you may not use this file except in compliance with the Elastic License. */ -import { CoreStart } from 'kibana/public'; +import { CoreStart, HttpSetup, IUiSettingsClient } from 'kibana/public'; import angular from 'angular'; import { Observable } from 'rxjs'; import { HttpRequestInit } from '../../../../src/core/public'; -import { MonitoringPluginDependencies } from './types'; +import { MonitoringStartPluginDependencies } from './types'; +import { TriggersAndActionsUIPublicPluginSetup } from '../../triggers_actions_ui/public'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths +import { TypeRegistry } from '../../triggers_actions_ui/public/application/type_registry'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths +import { ActionTypeModel, AlertTypeModel } from '../../triggers_actions_ui/public/types'; interface BreadcrumbItem { ['data-test-subj']?: string; @@ -32,7 +37,7 @@ export interface KFetchKibanaOptions { export interface IShims { toastNotifications: CoreStart['notifications']['toasts']; - capabilities: { get: () => CoreStart['application']['capabilities'] }; + capabilities: CoreStart['application']['capabilities']; getAngularInjector: () => angular.auto.IInjectorService; getBasePath: () => string; getInjected: (name: string, defaultValue?: unknown) => unknown; @@ -43,24 +48,29 @@ export interface IShims { I18nContext: CoreStart['i18n']['Context']; docLinks: CoreStart['docLinks']; docTitle: CoreStart['chrome']['docTitle']; - timefilter: MonitoringPluginDependencies['data']['query']['timefilter']['timefilter']; + timefilter: MonitoringStartPluginDependencies['data']['query']['timefilter']['timefilter']; + actionTypeRegistry: TypeRegistry; + alertTypeRegistry: TypeRegistry; + uiSettings: IUiSettingsClient; + http: HttpSetup; kfetch: ( { pathname, ...options }: KFetchOptions, kfetchOptions?: KFetchKibanaOptions | undefined ) => Promise; isCloud: boolean; + triggersActionsUi: TriggersAndActionsUIPublicPluginSetup; } export class Legacy { private static _shims: IShims; public static init( - { core, data, isCloud }: MonitoringPluginDependencies, + { core, data, isCloud, triggersActionsUi }: MonitoringStartPluginDependencies, ngInjector: angular.auto.IInjectorService ) { this._shims = { toastNotifications: core.notifications.toasts, - capabilities: { get: () => core.application.capabilities }, + capabilities: core.application.capabilities, getAngularInjector: (): angular.auto.IInjectorService => ngInjector, getBasePath: (): string => core.http.basePath.get(), getInjected: (name: string, defaultValue?: unknown): string | unknown => @@ -95,6 +105,10 @@ export class Legacy { docLinks: core.docLinks, docTitle: core.chrome.docTitle, timefilter: data.query.timefilter.timefilter, + actionTypeRegistry: triggersActionsUi?.actionTypeRegistry, + alertTypeRegistry: triggersActionsUi?.alertTypeRegistry, + uiSettings: core.uiSettings, + http: core.http, kfetch: async ( { pathname, ...options }: KFetchOptions, kfetchOptions?: KFetchKibanaOptions @@ -104,6 +118,7 @@ export class Legacy { ...options, }), isCloud, + triggersActionsUi, }; } diff --git a/x-pack/plugins/monitoring/public/lib/setup_mode.tsx b/x-pack/plugins/monitoring/public/lib/setup_mode.tsx index 2a4caf17515e1..a36b945e82ef7 100644 --- a/x-pack/plugins/monitoring/public/lib/setup_mode.tsx +++ b/x-pack/plugins/monitoring/public/lib/setup_mode.tsx @@ -39,11 +39,13 @@ interface ISetupModeState { enabled: boolean; data: any; callback?: (() => void) | null; + hideBottomBar: boolean; } const setupModeState: ISetupModeState = { enabled: false, data: null, callback: null, + hideBottomBar: false, }; export const getSetupModeState = () => setupModeState; @@ -128,6 +130,15 @@ export const updateSetupModeData = async (uuid?: string, fetchWithoutClusterUuid } }; +export const hideBottomBar = () => { + setupModeState.hideBottomBar = true; + notifySetupModeDataChange(); +}; +export const showBottomBar = () => { + setupModeState.hideBottomBar = false; + notifySetupModeDataChange(); +}; + export const disableElasticsearchInternalCollection = async () => { checkAngularState(); diff --git a/x-pack/plugins/monitoring/public/plugin.ts b/x-pack/plugins/monitoring/public/plugin.ts index de8c8d59b78bf..1b9ae75a0968e 100644 --- a/x-pack/plugins/monitoring/public/plugin.ts +++ b/x-pack/plugins/monitoring/public/plugin.ts @@ -19,19 +19,25 @@ import { } from '../../../../src/plugins/home/public'; import { UI_SETTINGS } from '../../../../src/plugins/data/public'; import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/public'; -import { MonitoringPluginDependencies, MonitoringConfig } from './types'; -import { - MONITORING_CONFIG_ALERTING_EMAIL_ADDRESS, - KIBANA_ALERTING_ENABLED, -} from '../common/constants'; +import { MonitoringStartPluginDependencies, MonitoringConfig } from './types'; +import { TriggersAndActionsUIPublicPluginSetup } from '../../triggers_actions_ui/public'; +import { createCpuUsageAlertType } from './alerts/cpu_usage_alert'; +import { createLegacyAlertTypes } from './alerts/legacy_alert'; + +interface MonitoringSetupPluginDependencies { + home?: HomePublicPluginSetup; + cloud?: { isCloudEnabled: boolean }; + triggers_actions_ui: TriggersAndActionsUIPublicPluginSetup; +} export class MonitoringPlugin - implements Plugin { + implements + Plugin { constructor(private initializerContext: PluginInitializerContext) {} public setup( - core: CoreSetup, - plugins: object & { home?: HomePublicPluginSetup; cloud?: { isCloudEnabled: boolean } } + core: CoreSetup, + plugins: MonitoringSetupPluginDependencies ) { const { home } = plugins; const id = 'monitoring'; @@ -59,6 +65,12 @@ export class MonitoringPlugin }); } + plugins.triggers_actions_ui.alertTypeRegistry.register(createCpuUsageAlertType()); + const legacyAlertTypes = createLegacyAlertTypes(); + for (const legacyAlertType of legacyAlertTypes) { + plugins.triggers_actions_ui.alertTypeRegistry.register(legacyAlertType); + } + const app: App = { id, title, @@ -68,7 +80,7 @@ export class MonitoringPlugin mount: async (params: AppMountParameters) => { const [coreStart, pluginsStart] = await core.getStartServices(); const { AngularApp } = await import('./angular'); - const deps: MonitoringPluginDependencies = { + const deps: MonitoringStartPluginDependencies = { navigation: pluginsStart.navigation, kibanaLegacy: pluginsStart.kibanaLegacy, element: params.element, @@ -77,11 +89,11 @@ export class MonitoringPlugin isCloud: Boolean(plugins.cloud?.isCloudEnabled), pluginInitializerContext: this.initializerContext, externalConfig: this.getExternalConfig(), + triggersActionsUi: plugins.triggers_actions_ui, }; pluginsStart.kibanaLegacy.loadFontAwesome(); this.setInitialTimefilter(deps); - this.overrideAlertingEmailDefaults(deps); const monitoringApp = new AngularApp(deps); const removeHistoryListener = params.history.listen((location) => { @@ -105,7 +117,7 @@ export class MonitoringPlugin public stop() {} - private setInitialTimefilter({ core: coreContext, data }: MonitoringPluginDependencies) { + private setInitialTimefilter({ core: coreContext, data }: MonitoringStartPluginDependencies) { const { timefilter } = data.query.timefilter; const { uiSettings } = coreContext; const refreshInterval = { value: 10000, pause: false }; @@ -119,25 +131,6 @@ export class MonitoringPlugin uiSettings.overrideLocalDefault('timepicker:timeDefaults', JSON.stringify(time)); } - private overrideAlertingEmailDefaults({ core: coreContext }: MonitoringPluginDependencies) { - const { uiSettings } = coreContext; - if (KIBANA_ALERTING_ENABLED && !uiSettings.get(MONITORING_CONFIG_ALERTING_EMAIL_ADDRESS)) { - uiSettings.overrideLocalDefault( - MONITORING_CONFIG_ALERTING_EMAIL_ADDRESS, - JSON.stringify({ - name: i18n.translate('xpack.monitoring.alertingEmailAddress.name', { - defaultMessage: 'Alerting email address', - }), - value: '', - description: i18n.translate('xpack.monitoring.alertingEmailAddress.description', { - defaultMessage: `The default email address to receive alerts from Stack Monitoring`, - }), - category: ['monitoring'], - }) - ); - } - } - private getExternalConfig() { const monitoring = this.initializerContext.config.get(); return [ diff --git a/x-pack/plugins/monitoring/public/services/clusters.js b/x-pack/plugins/monitoring/public/services/clusters.js index 2862c6f424927..f3eadcaf9831b 100644 --- a/x-pack/plugins/monitoring/public/services/clusters.js +++ b/x-pack/plugins/monitoring/public/services/clusters.js @@ -19,6 +19,8 @@ function formatCluster(cluster) { return cluster; } +let once = false; + export function monitoringClustersProvider($injector) { return (clusterUuid, ccs, codePaths) => { const { min, max } = Legacy.shims.timefilter.getBounds(); @@ -30,23 +32,52 @@ export function monitoringClustersProvider($injector) { } const $http = $injector.get('$http'); - return $http - .post(url, { - ccs, - timeRange: { - min: min.toISOString(), - max: max.toISOString(), - }, - codePaths, - }) - .then((response) => response.data) - .then((data) => { - return formatClusters(data); // return set of clusters - }) - .catch((err) => { + + function getClusters() { + return $http + .post(url, { + ccs, + timeRange: { + min: min.toISOString(), + max: max.toISOString(), + }, + codePaths, + }) + .then((response) => response.data) + .then((data) => { + return formatClusters(data); // return set of clusters + }) + .catch((err) => { + const Private = $injector.get('Private'); + const ajaxErrorHandlers = Private(ajaxErrorHandlersProvider); + return ajaxErrorHandlers(err); + }); + } + + function ensureAlertsEnabled() { + return $http.post('../api/monitoring/v1/alerts/enable', {}).catch((err) => { const Private = $injector.get('Private'); const ajaxErrorHandlers = Private(ajaxErrorHandlersProvider); return ajaxErrorHandlers(err); }); + } + + if (!once) { + return getClusters().then((clusters) => { + if (clusters.length) { + return ensureAlertsEnabled() + .then(() => { + once = true; + return clusters; + }) + .catch(() => { + // Intentionally swallow the error as this will retry the next page load + return clusters; + }); + } + return clusters; + }); + } + return getClusters(); }; } diff --git a/x-pack/plugins/monitoring/public/types.ts b/x-pack/plugins/monitoring/public/types.ts index 6266755a04120..f911af2db8c58 100644 --- a/x-pack/plugins/monitoring/public/types.ts +++ b/x-pack/plugins/monitoring/public/types.ts @@ -7,12 +7,13 @@ import { PluginInitializerContext, CoreStart } from 'kibana/public'; import { NavigationPublicPluginStart as NavigationStart } from '../../../../src/plugins/navigation/public'; import { DataPublicPluginStart } from '../../../../src/plugins/data/public'; +import { TriggersAndActionsUIPublicPluginSetup } from '../../triggers_actions_ui/public'; import { KibanaLegacyStart } from '../../../../src/plugins/kibana_legacy/public'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths export { MonitoringConfig } from '../server'; -export interface MonitoringPluginDependencies { +export interface MonitoringStartPluginDependencies { navigation: NavigationStart; data: DataPublicPluginStart; kibanaLegacy: KibanaLegacyStart; @@ -21,4 +22,5 @@ export interface MonitoringPluginDependencies { isCloud: boolean; pluginInitializerContext: PluginInitializerContext; externalConfig: Array | Array>; + triggersActionsUi: TriggersAndActionsUIPublicPluginSetup; } diff --git a/x-pack/plugins/monitoring/public/url_state.ts b/x-pack/plugins/monitoring/public/url_state.ts index f2ae0a93d5df0..e53497d751f9b 100644 --- a/x-pack/plugins/monitoring/public/url_state.ts +++ b/x-pack/plugins/monitoring/public/url_state.ts @@ -6,7 +6,7 @@ import { Subscription } from 'rxjs'; import { History, createHashHistory } from 'history'; -import { MonitoringPluginDependencies } from './types'; +import { MonitoringStartPluginDependencies } from './types'; import { Legacy } from './legacy_shims'; import { @@ -64,13 +64,13 @@ export class GlobalState { private readonly stateStorage: IKbnUrlStateStorage; private readonly stateContainerChangeSub: Subscription; private readonly syncQueryStateWithUrlManager: { stop: () => void }; - private readonly timefilterRef: MonitoringPluginDependencies['data']['query']['timefilter']['timefilter']; + private readonly timefilterRef: MonitoringStartPluginDependencies['data']['query']['timefilter']['timefilter']; private lastAssignedState: MonitoringAppState = {}; private lastKnownGlobalState?: string; constructor( - queryService: MonitoringPluginDependencies['data']['query'], + queryService: MonitoringStartPluginDependencies['data']['query'], rootScope: ng.IRootScopeService, ngLocation: ng.ILocationService, externalState: RawObject diff --git a/x-pack/plugins/monitoring/public/views/alerts/index.html b/x-pack/plugins/monitoring/public/views/alerts/index.html deleted file mode 100644 index 4a764634d86fa..0000000000000 --- a/x-pack/plugins/monitoring/public/views/alerts/index.html +++ /dev/null @@ -1,3 +0,0 @@ - -
-
diff --git a/x-pack/plugins/monitoring/public/views/alerts/index.js b/x-pack/plugins/monitoring/public/views/alerts/index.js deleted file mode 100644 index ea857cb69d22b..0000000000000 --- a/x-pack/plugins/monitoring/public/views/alerts/index.js +++ /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 React from 'react'; -import { i18n } from '@kbn/i18n'; -import { render } from 'react-dom'; -import { find, get } from 'lodash'; -import { uiRoutes } from '../../angular/helpers/routes'; -import template from './index.html'; -import { routeInitProvider } from '../../lib/route_init'; -import { ajaxErrorHandlersProvider } from '../../lib/ajax_error_handler'; -import { Legacy } from '../../legacy_shims'; -import { Alerts } from '../../components/alerts'; -import { MonitoringViewBaseEuiTableController } from '../base_eui_table_controller'; -import { FormattedMessage } from '@kbn/i18n/react'; -import { EuiPage, EuiPageBody, EuiPageContent, EuiSpacer, EuiLink } from '@elastic/eui'; -import { CODE_PATH_ALERTS, KIBANA_ALERTING_ENABLED } from '../../../common/constants'; - -function getPageData($injector) { - const globalState = $injector.get('globalState'); - const $http = $injector.get('$http'); - const Private = $injector.get('Private'); - const url = KIBANA_ALERTING_ENABLED - ? `../api/monitoring/v1/alert_status` - : `../api/monitoring/v1/clusters/${globalState.cluster_uuid}/legacy_alerts`; - - const timeBounds = Legacy.shims.timefilter.getBounds(); - const data = { - timeRange: { - min: timeBounds.min.toISOString(), - max: timeBounds.max.toISOString(), - }, - }; - - if (!KIBANA_ALERTING_ENABLED) { - data.ccs = globalState.ccs; - } - - return $http - .post(url, data) - .then((response) => { - const result = get(response, 'data', []); - if (KIBANA_ALERTING_ENABLED) { - return result.alerts; - } - return result; - }) - .catch((err) => { - const ajaxErrorHandlers = Private(ajaxErrorHandlersProvider); - return ajaxErrorHandlers(err); - }); -} - -uiRoutes.when('/alerts', { - template, - resolve: { - clusters(Private) { - const routeInit = Private(routeInitProvider); - return routeInit({ codePaths: [CODE_PATH_ALERTS] }); - }, - alerts: getPageData, - }, - controllerAs: 'alerts', - controller: class AlertsView extends MonitoringViewBaseEuiTableController { - constructor($injector, $scope) { - const $route = $injector.get('$route'); - const globalState = $injector.get('globalState'); - - // breadcrumbs + page title - $scope.cluster = find($route.current.locals.clusters, { - cluster_uuid: globalState.cluster_uuid, - }); - - super({ - title: i18n.translate('xpack.monitoring.alerts.clusterAlertsTitle', { - defaultMessage: 'Cluster Alerts', - }), - getPageData, - $scope, - $injector, - storageKey: 'alertsTable', - reactNodeId: 'monitoringAlertsApp', - }); - - this.data = $route.current.locals.alerts; - - const renderReact = (data) => { - const app = data.message ? ( -

{data.message}

- ) : ( - - ); - - render( - - - - {app} - - - - - - - , - document.getElementById('monitoringAlertsApp') - ); - }; - $scope.$watch( - () => this.data, - (data) => renderReact(data) - ); - } - }, -}); diff --git a/x-pack/plugins/monitoring/public/views/all.js b/x-pack/plugins/monitoring/public/views/all.js index 51dcce751863c..d192b366fec33 100644 --- a/x-pack/plugins/monitoring/public/views/all.js +++ b/x-pack/plugins/monitoring/public/views/all.js @@ -6,7 +6,6 @@ import './no_data'; import './access_denied'; -import './alerts'; import './license'; import './cluster/listing'; import './cluster/overview'; diff --git a/x-pack/plugins/monitoring/public/views/base_controller.js b/x-pack/plugins/monitoring/public/views/base_controller.js index e189491a3be03..2f88245d88c4a 100644 --- a/x-pack/plugins/monitoring/public/views/base_controller.js +++ b/x-pack/plugins/monitoring/public/views/base_controller.js @@ -85,6 +85,7 @@ export class MonitoringViewBaseController { $scope, $injector, options = {}, + alerts = { shouldFetch: false, options: {} }, fetchDataImmediately = true, }) { const titleService = $injector.get('title'); @@ -112,6 +113,34 @@ export class MonitoringViewBaseController { const { enableTimeFilter = true, enableAutoRefresh = true } = options; + async function fetchAlerts() { + const globalState = $injector.get('globalState'); + const bounds = Legacy.shims.timefilter.getBounds(); + const min = bounds.min?.valueOf(); + const max = bounds.max?.valueOf(); + const options = alerts.options || {}; + try { + return await Legacy.shims.http.post( + `/api/monitoring/v1/alert/${globalState.cluster_uuid}/status`, + { + body: JSON.stringify({ + alertTypeIds: options.alertTypeIds, + filters: options.filters, + timeRange: { + min, + max, + }, + }), + } + ); + } catch (err) { + Legacy.shims.toastNotifications.addDanger({ + title: 'Error fetching alert status', + text: err.message, + }); + } + } + this.updateData = () => { if (this.updateDataPromise) { // Do not sent another request if one is inflight @@ -122,14 +151,18 @@ export class MonitoringViewBaseController { const _api = apiUrlFn ? apiUrlFn() : api; const promises = [_getPageData($injector, _api, this.getPaginationRouteOptions())]; const setupMode = getSetupModeState(); + if (alerts.shouldFetch) { + promises.push(fetchAlerts()); + } if (setupMode.enabled) { promises.push(updateSetupModeData()); } this.updateDataPromise = new PromiseWithCancel(Promise.all(promises)); - return this.updateDataPromise.promise().then(([pageData]) => { + return this.updateDataPromise.promise().then(([pageData, alerts]) => { $scope.$apply(() => { this._isDataInitialized = true; // render will replace loading screen with the react component $scope.pageData = this.data = pageData; // update the view's data with the fetch result + $scope.alerts = this.alerts = alerts; }); }); }; diff --git a/x-pack/plugins/monitoring/public/views/cluster/overview/index.js b/x-pack/plugins/monitoring/public/views/cluster/overview/index.js index d47b31cfb5b79..f3e6d5def9b6f 100644 --- a/x-pack/plugins/monitoring/public/views/cluster/overview/index.js +++ b/x-pack/plugins/monitoring/public/views/cluster/overview/index.js @@ -5,7 +5,6 @@ */ import React, { Fragment } from 'react'; import { isEmpty } from 'lodash'; -import { Legacy } from '../../../legacy_shims'; import { i18n } from '@kbn/i18n'; import { uiRoutes } from '../../../angular/helpers/routes'; import { routeInitProvider } from '../../../lib/route_init'; @@ -13,11 +12,7 @@ import template from './index.html'; import { MonitoringViewBaseController } from '../../'; import { Overview } from '../../../components/cluster/overview'; import { SetupModeRenderer } from '../../../components/renderers'; -import { - CODE_PATH_ALL, - MONITORING_CONFIG_ALERTING_EMAIL_ADDRESS, - KIBANA_ALERTING_ENABLED, -} from '../../../../common/constants'; +import { CODE_PATH_ALL } from '../../../../common/constants'; const CODE_PATHS = [CODE_PATH_ALL]; @@ -35,7 +30,6 @@ uiRoutes.when('/overview', { const monitoringClusters = $injector.get('monitoringClusters'); const globalState = $injector.get('globalState'); const showLicenseExpiration = $injector.get('showLicenseExpiration'); - const config = $injector.get('config'); super({ title: i18n.translate('xpack.monitoring.cluster.overviewTitle', { @@ -53,6 +47,9 @@ uiRoutes.when('/overview', { reactNodeId: 'monitoringClusterOverviewApp', $scope, $injector, + alerts: { + shouldFetch: true, + }, }); $scope.$watch( @@ -62,11 +59,6 @@ uiRoutes.when('/overview', { return; } - let emailAddress = Legacy.shims.getInjected('monitoringLegacyEmailAddress') || ''; - if (KIBANA_ALERTING_ENABLED) { - emailAddress = config.get(MONITORING_CONFIG_ALERTING_EMAIL_ADDRESS) || emailAddress; - } - this.renderReact( diff --git a/x-pack/plugins/monitoring/public/views/elasticsearch/node/index.js b/x-pack/plugins/monitoring/public/views/elasticsearch/node/index.js index a1ce9bda16cdc..f6f7a01690529 100644 --- a/x-pack/plugins/monitoring/public/views/elasticsearch/node/index.js +++ b/x-pack/plugins/monitoring/public/views/elasticsearch/node/index.js @@ -18,7 +18,7 @@ import { Node } from '../../../components/elasticsearch/node/node'; import { labels } from '../../../components/elasticsearch/shard_allocation/lib/labels'; import { nodesByIndices } from '../../../components/elasticsearch/shard_allocation/transformers/nodes_by_indices'; import { MonitoringViewBaseController } from '../../base_controller'; -import { CODE_PATH_ELASTICSEARCH } from '../../../../common/constants'; +import { CODE_PATH_ELASTICSEARCH, ALERT_CPU_USAGE } from '../../../../common/constants'; uiRoutes.when('/elasticsearch/nodes/:node', { template, @@ -47,6 +47,17 @@ uiRoutes.when('/elasticsearch/nodes/:node', { reactNodeId: 'monitoringElasticsearchNodeApp', $scope, $injector, + alerts: { + shouldFetch: true, + options: { + alertTypeIds: [ALERT_CPU_USAGE], + filters: [ + { + nodeUuid: nodeName, + }, + ], + }, + }, }); this.nodeName = nodeName; @@ -79,6 +90,7 @@ uiRoutes.when('/elasticsearch/nodes/:node', { this.renderReact( diff --git a/x-pack/plugins/monitoring/public/views/kibana/instance/index.js b/x-pack/plugins/monitoring/public/views/kibana/instance/index.js index 802c0e3d30d5b..a7cb6c8094f74 100644 --- a/x-pack/plugins/monitoring/public/views/kibana/instance/index.js +++ b/x-pack/plugins/monitoring/public/views/kibana/instance/index.js @@ -26,7 +26,8 @@ import { import { MonitoringTimeseriesContainer } from '../../../components/chart'; import { DetailStatus } from '../../../components/kibana/detail_status'; import { MonitoringViewBaseController } from '../../base_controller'; -import { CODE_PATH_KIBANA } from '../../../../common/constants'; +import { CODE_PATH_KIBANA, ALERT_KIBANA_VERSION_MISMATCH } from '../../../../common/constants'; +import { AlertsCallout } from '../../../alerts/callout'; function getPageData($injector) { const $http = $injector.get('$http'); @@ -70,6 +71,12 @@ uiRoutes.when('/kibana/instances/:uuid', { reactNodeId: 'monitoringKibanaInstanceApp', $scope, $injector, + alerts: { + shouldFetch: true, + options: { + alertTypeIds: [ALERT_KIBANA_VERSION_MISMATCH], + }, + }, }); $scope.$watch( @@ -88,6 +95,7 @@ uiRoutes.when('/kibana/instances/:uuid', {
+ diff --git a/x-pack/plugins/monitoring/public/views/kibana/instances/index.js b/x-pack/plugins/monitoring/public/views/kibana/instances/index.js index 8556103e47c30..7106da0fdabd3 100644 --- a/x-pack/plugins/monitoring/public/views/kibana/instances/index.js +++ b/x-pack/plugins/monitoring/public/views/kibana/instances/index.js @@ -12,7 +12,11 @@ import { getPageData } from './get_page_data'; import template from './index.html'; import { KibanaInstances } from '../../../components/kibana/instances'; import { SetupModeRenderer } from '../../../components/renderers'; -import { KIBANA_SYSTEM_ID, CODE_PATH_KIBANA } from '../../../../common/constants'; +import { + KIBANA_SYSTEM_ID, + CODE_PATH_KIBANA, + ALERT_KIBANA_VERSION_MISMATCH, +} from '../../../../common/constants'; uiRoutes.when('/kibana/instances', { template, @@ -33,6 +37,12 @@ uiRoutes.when('/kibana/instances', { reactNodeId: 'monitoringKibanaInstancesApp', $scope, $injector, + alerts: { + shouldFetch: true, + options: { + alertTypeIds: [ALERT_KIBANA_VERSION_MISMATCH], + }, + }, }); const renderReact = () => { @@ -46,6 +56,7 @@ uiRoutes.when('/kibana/instances', { {flyoutComponent}
+ {metricsToShow.map((metric, index) => ( diff --git a/x-pack/plugins/monitoring/public/views/logstash/nodes/index.js b/x-pack/plugins/monitoring/public/views/logstash/nodes/index.js index f78a426b9b7c3..563d04af55bb2 100644 --- a/x-pack/plugins/monitoring/public/views/logstash/nodes/index.js +++ b/x-pack/plugins/monitoring/public/views/logstash/nodes/index.js @@ -11,7 +11,11 @@ import { getPageData } from './get_page_data'; import template from './index.html'; import { Listing } from '../../../components/logstash/listing'; import { SetupModeRenderer } from '../../../components/renderers'; -import { CODE_PATH_LOGSTASH, LOGSTASH_SYSTEM_ID } from '../../../../common/constants'; +import { + CODE_PATH_LOGSTASH, + LOGSTASH_SYSTEM_ID, + ALERT_LOGSTASH_VERSION_MISMATCH, +} from '../../../../common/constants'; uiRoutes.when('/logstash/nodes', { template, @@ -32,6 +36,12 @@ uiRoutes.when('/logstash/nodes', { reactNodeId: 'monitoringLogstashNodesApp', $scope, $injector, + alerts: { + shouldFetch: true, + options: { + alertTypeIds: [ALERT_LOGSTASH_VERSION_MISMATCH], + }, + }, }); $scope.$watch( @@ -49,6 +59,7 @@ uiRoutes.when('/logstash/nodes', { data={data.nodes} setupMode={setupMode} stats={data.clusterStatus} + alerts={this.alerts} sorting={this.sorting} pagination={this.pagination} onTableChange={this.onTableChange} diff --git a/x-pack/plugins/monitoring/server/alerts/alerts_factory.test.ts b/x-pack/plugins/monitoring/server/alerts/alerts_factory.test.ts new file mode 100644 index 0000000000000..d8fa703c7f785 --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/alerts_factory.test.ts @@ -0,0 +1,68 @@ +/* + * 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 { AlertsFactory } from './alerts_factory'; +import { ALERT_CPU_USAGE } from '../../common/constants'; + +describe('AlertsFactory', () => { + const alertsClient = { + find: jest.fn(), + }; + + afterEach(() => { + alertsClient.find.mockReset(); + }); + + it('should get by type', async () => { + const id = '1abc'; + alertsClient.find = jest.fn().mockImplementation(() => { + return { + total: 1, + data: [ + { + id, + }, + ], + }; + }); + const alert = await AlertsFactory.getByType(ALERT_CPU_USAGE, alertsClient as any); + expect(alert).not.toBeNull(); + expect(alert?.type).toBe(ALERT_CPU_USAGE); + }); + + it('should handle no alert found', async () => { + alertsClient.find = jest.fn().mockImplementation(() => { + return { + total: 0, + }; + }); + const alert = await AlertsFactory.getByType(ALERT_CPU_USAGE, alertsClient as any); + expect(alert).not.toBeNull(); + expect(alert?.type).toBe(ALERT_CPU_USAGE); + }); + + it('should pass in the correct filters', async () => { + let filter = null; + alertsClient.find = jest.fn().mockImplementation(({ options }) => { + filter = options.filter; + return { + total: 0, + }; + }); + await AlertsFactory.getByType(ALERT_CPU_USAGE, alertsClient as any); + expect(filter).toBe(`alert.attributes.alertTypeId:${ALERT_CPU_USAGE}`); + }); + + it('should handle no alerts client', async () => { + const alert = await AlertsFactory.getByType(ALERT_CPU_USAGE, undefined); + expect(alert).not.toBeNull(); + expect(alert?.type).toBe(ALERT_CPU_USAGE); + }); + + it('should get all', () => { + const alerts = AlertsFactory.getAll(); + expect(alerts.length).toBe(7); + }); +}); diff --git a/x-pack/plugins/monitoring/server/alerts/alerts_factory.ts b/x-pack/plugins/monitoring/server/alerts/alerts_factory.ts new file mode 100644 index 0000000000000..b91eab05cf912 --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/alerts_factory.ts @@ -0,0 +1,68 @@ +/* + * 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 { + CpuUsageAlert, + NodesChangedAlert, + ClusterHealthAlert, + LicenseExpirationAlert, + LogstashVersionMismatchAlert, + KibanaVersionMismatchAlert, + ElasticsearchVersionMismatchAlert, + BaseAlert, +} from './'; +import { + ALERT_CLUSTER_HEALTH, + ALERT_LICENSE_EXPIRATION, + ALERT_CPU_USAGE, + ALERT_NODES_CHANGED, + ALERT_LOGSTASH_VERSION_MISMATCH, + ALERT_KIBANA_VERSION_MISMATCH, + ALERT_ELASTICSEARCH_VERSION_MISMATCH, +} from '../../common/constants'; +import { AlertsClient } from '../../../alerts/server'; + +export const BY_TYPE = { + [ALERT_CLUSTER_HEALTH]: ClusterHealthAlert, + [ALERT_LICENSE_EXPIRATION]: LicenseExpirationAlert, + [ALERT_CPU_USAGE]: CpuUsageAlert, + [ALERT_NODES_CHANGED]: NodesChangedAlert, + [ALERT_LOGSTASH_VERSION_MISMATCH]: LogstashVersionMismatchAlert, + [ALERT_KIBANA_VERSION_MISMATCH]: KibanaVersionMismatchAlert, + [ALERT_ELASTICSEARCH_VERSION_MISMATCH]: ElasticsearchVersionMismatchAlert, +}; + +export class AlertsFactory { + public static async getByType( + type: string, + alertsClient: AlertsClient | undefined + ): Promise { + const alertCls = BY_TYPE[type]; + if (!alertCls) { + return null; + } + if (alertsClient) { + const alertClientAlerts = await alertsClient.find({ + options: { + filter: `alert.attributes.alertTypeId:${type}`, + }, + }); + + if (alertClientAlerts.total === 0) { + return new alertCls(); + } + + const rawAlert = alertClientAlerts.data[0]; + return new alertCls(rawAlert as BaseAlert['rawAlert']); + } + + return new alertCls(); + } + + public static getAll() { + return Object.values(BY_TYPE).map((alert) => new alert()); + } +} diff --git a/x-pack/plugins/monitoring/server/alerts/base_alert.test.ts b/x-pack/plugins/monitoring/server/alerts/base_alert.test.ts new file mode 100644 index 0000000000000..8fd31db421a30 --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/base_alert.test.ts @@ -0,0 +1,138 @@ +/* + * 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 { BaseAlert } from './base_alert'; + +describe('BaseAlert', () => { + describe('serialize', () => { + it('should serialize with a raw alert provided', () => { + const alert = new BaseAlert({} as any); + expect(alert.serialize()).not.toBeNull(); + }); + it('should not serialize without a raw alert provided', () => { + const alert = new BaseAlert(); + expect(alert.serialize()).toBeNull(); + }); + }); + + describe('create', () => { + it('should create an alert if it does not exist', async () => { + const alert = new BaseAlert(); + const alertsClient = { + create: jest.fn(), + find: jest.fn().mockImplementation(() => { + return { + total: 0, + }; + }), + }; + const actionsClient = { + get: jest.fn().mockImplementation(() => { + return { + actionTypeId: 'foo', + }; + }), + }; + const actions = [ + { + id: '1abc', + config: {}, + }, + ]; + + await alert.createIfDoesNotExist(alertsClient as any, actionsClient as any, actions); + expect(alertsClient.create).toHaveBeenCalledWith({ + data: { + actions: [ + { + group: 'default', + id: '1abc', + params: { + message: '{{context.internalShortMessage}}', + }, + }, + ], + alertTypeId: undefined, + consumer: 'monitoring', + enabled: true, + name: undefined, + params: {}, + schedule: { + interval: '1m', + }, + tags: [], + throttle: '1m', + }, + }); + }); + + it('should not create an alert if it exists', async () => { + const alert = new BaseAlert(); + const alertsClient = { + create: jest.fn(), + find: jest.fn().mockImplementation(() => { + return { + total: 1, + data: [], + }; + }), + }; + const actionsClient = { + get: jest.fn().mockImplementation(() => { + return { + actionTypeId: 'foo', + }; + }), + }; + const actions = [ + { + id: '1abc', + config: {}, + }, + ]; + + await alert.createIfDoesNotExist(alertsClient as any, actionsClient as any, actions); + expect(alertsClient.create).not.toHaveBeenCalled(); + }); + }); + + describe('getStates', () => { + it('should get alert states', async () => { + const alertsClient = { + getAlertState: jest.fn().mockImplementation(() => { + return { + alertInstances: { + abc123: { + id: 'foobar', + }, + }, + }; + }), + }; + const id = '456def'; + const filters: any[] = []; + const alert = new BaseAlert(); + const states = await alert.getStates(alertsClient as any, id, filters); + expect(states).toStrictEqual({ + abc123: { + id: 'foobar', + }, + }); + }); + + it('should return nothing if no states are available', async () => { + const alertsClient = { + getAlertState: jest.fn().mockImplementation(() => { + return null; + }), + }; + const id = '456def'; + const filters: any[] = []; + const alert = new BaseAlert(); + const states = await alert.getStates(alertsClient as any, id, filters); + expect(states).toStrictEqual({}); + }); + }); +}); diff --git a/x-pack/plugins/monitoring/server/alerts/base_alert.ts b/x-pack/plugins/monitoring/server/alerts/base_alert.ts new file mode 100644 index 0000000000000..622ee7dc51af1 --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/base_alert.ts @@ -0,0 +1,339 @@ +/* + * 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 { + UiSettingsServiceStart, + ILegacyCustomClusterClient, + Logger, + IUiSettingsClient, +} from 'kibana/server'; +import { i18n } from '@kbn/i18n'; +import { + AlertType, + AlertExecutorOptions, + AlertInstance, + AlertsClient, + AlertServices, +} from '../../../alerts/server'; +import { Alert, RawAlertInstance } from '../../../alerts/common'; +import { ActionsClient } from '../../../actions/server'; +import { + AlertState, + AlertCluster, + AlertMessage, + AlertData, + AlertInstanceState, + AlertEnableAction, +} from './types'; +import { fetchAvailableCcs } from '../lib/alerts/fetch_available_ccs'; +import { fetchClusters } from '../lib/alerts/fetch_clusters'; +import { getCcsIndexPattern } from '../lib/alerts/get_ccs_index_pattern'; +import { INDEX_PATTERN_ELASTICSEARCH } from '../../common/constants'; +import { MonitoringConfig } from '../config'; +import { AlertSeverity } from '../../common/enums'; +import { CommonAlertFilter, CommonAlertParams, CommonBaseAlert } from '../../common/types'; +import { MonitoringLicenseService } from '../types'; + +export class BaseAlert { + public type!: string; + public label!: string; + public defaultThrottle: string = '1m'; + public defaultInterval: string = '1m'; + public rawAlert: Alert | undefined; + public isLegacy: boolean = false; + + protected getUiSettingsService!: () => Promise; + protected monitoringCluster!: ILegacyCustomClusterClient; + protected getLogger!: (...scopes: string[]) => Logger; + protected config!: MonitoringConfig; + protected kibanaUrl!: string; + protected defaultParams: CommonAlertParams | {} = {}; + public get paramDetails() { + return {}; + } + protected actionVariables: Array<{ name: string; description: string }> = []; + protected alertType!: AlertType; + + constructor(rawAlert: Alert | undefined = undefined) { + if (rawAlert) { + this.rawAlert = rawAlert; + } + } + + public serialize(): CommonBaseAlert | null { + if (!this.rawAlert) { + return null; + } + + return { + type: this.type, + label: this.label, + rawAlert: this.rawAlert, + paramDetails: this.paramDetails, + isLegacy: this.isLegacy, + }; + } + + public initializeAlertType( + getUiSettingsService: () => Promise, + monitoringCluster: ILegacyCustomClusterClient, + getLogger: (...scopes: string[]) => Logger, + config: MonitoringConfig, + kibanaUrl: string + ) { + this.getUiSettingsService = getUiSettingsService; + this.monitoringCluster = monitoringCluster; + this.config = config; + this.kibanaUrl = kibanaUrl; + this.getLogger = getLogger; + } + + public getAlertType(): AlertType { + return { + id: this.type, + name: this.label, + actionGroups: [ + { + id: 'default', + name: i18n.translate('xpack.monitoring.alerts.actionGroups.default', { + defaultMessage: 'Default', + }), + }, + ], + defaultActionGroupId: 'default', + executor: (options: AlertExecutorOptions): Promise => this.execute(options), + producer: 'monitoring', + actionVariables: { + context: this.actionVariables, + }, + }; + } + + public isEnabled(licenseService: MonitoringLicenseService) { + if (this.isLegacy) { + const watcherFeature = licenseService.getWatcherFeature(); + if (!watcherFeature.isAvailable || !watcherFeature.isEnabled) { + return false; + } + } + return true; + } + + public getId() { + return this.rawAlert ? this.rawAlert.id : null; + } + + public async createIfDoesNotExist( + alertsClient: AlertsClient, + actionsClient: ActionsClient, + actions: AlertEnableAction[] + ): Promise { + const existingAlertData = await alertsClient.find({ + options: { + search: this.type, + }, + }); + + if (existingAlertData.total > 0) { + const existingAlert = existingAlertData.data[0] as Alert; + return existingAlert; + } + + const alertActions = []; + for (const actionData of actions) { + const action = await actionsClient.get({ id: actionData.id }); + if (!action) { + continue; + } + alertActions.push({ + group: 'default', + id: actionData.id, + params: { + // This is just a server log right now, but will get more robut over time + message: this.getDefaultActionMessage(true), + ...actionData.config, + }, + }); + } + + return await alertsClient.create({ + data: { + enabled: true, + tags: [], + params: this.defaultParams, + consumer: 'monitoring', + name: this.label, + alertTypeId: this.type, + throttle: this.defaultThrottle, + schedule: { interval: this.defaultInterval }, + actions: alertActions, + }, + }); + } + + public async getStates( + alertsClient: AlertsClient, + id: string, + filters: CommonAlertFilter[] + ): Promise<{ [instanceId: string]: RawAlertInstance }> { + const states = await alertsClient.getAlertState({ id }); + if (!states || !states.alertInstances) { + return {}; + } + + return Object.keys(states.alertInstances).reduce( + (accum: { [instanceId: string]: RawAlertInstance }, instanceId) => { + if (!states.alertInstances) { + return accum; + } + const alertInstance: RawAlertInstance = states.alertInstances[instanceId]; + if (alertInstance && this.filterAlertInstance(alertInstance, filters)) { + accum[instanceId] = alertInstance; + } + return accum; + }, + {} + ); + } + + protected filterAlertInstance(alertInstance: RawAlertInstance, filters: CommonAlertFilter[]) { + return true; + } + + protected async execute({ services, params, state }: AlertExecutorOptions): Promise { + const logger = this.getLogger(this.type); + logger.debug( + `Executing alert with params: ${JSON.stringify(params)} and state: ${JSON.stringify(state)}` + ); + + const callCluster = this.monitoringCluster + ? this.monitoringCluster.callAsInternalUser + : services.callCluster; + const availableCcs = this.config.ui.ccs.enabled ? await fetchAvailableCcs(callCluster) : []; + // Support CCS use cases by querying to find available remote clusters + // and then adding those to the index pattern we are searching against + let esIndexPattern = INDEX_PATTERN_ELASTICSEARCH; + if (availableCcs) { + esIndexPattern = getCcsIndexPattern(esIndexPattern, availableCcs); + } + const clusters = await fetchClusters(callCluster, esIndexPattern); + const uiSettings = (await this.getUiSettingsService()).asScopedToClient( + services.savedObjectsClient + ); + + const data = await this.fetchData(params, callCluster, clusters, uiSettings, availableCcs); + this.processData(data, clusters, services, logger); + } + + protected async fetchData( + params: CommonAlertParams, + callCluster: any, + clusters: AlertCluster[], + uiSettings: IUiSettingsClient, + availableCcs: string[] + ): Promise { + // Child should implement + throw new Error('Child classes must implement `fetchData`'); + } + + protected processData( + data: AlertData[], + clusters: AlertCluster[], + services: AlertServices, + logger: Logger + ) { + for (const item of data) { + const cluster = clusters.find((c: AlertCluster) => c.clusterUuid === item.clusterUuid); + if (!cluster) { + logger.warn(`Unable to find cluster for clusterUuid='${item.clusterUuid}'`); + continue; + } + + const instance = services.alertInstanceFactory(`${this.type}:${item.instanceKey}`); + const state = (instance.getState() as unknown) as AlertInstanceState; + const alertInstanceState: AlertInstanceState = { alertStates: state?.alertStates || [] }; + let alertState: AlertState; + const indexInState = this.findIndexInInstanceState(alertInstanceState, cluster); + if (indexInState > -1) { + alertState = state.alertStates[indexInState]; + } else { + alertState = this.getDefaultAlertState(cluster, item); + } + + let shouldExecuteActions = false; + if (item.shouldFire) { + logger.debug(`${this.type} is firing`); + alertState.ui.triggeredMS = +new Date(); + alertState.ui.isFiring = true; + alertState.ui.message = this.getUiMessage(alertState, item); + alertState.ui.severity = item.severity; + alertState.ui.resolvedMS = 0; + shouldExecuteActions = true; + } else if (!item.shouldFire && alertState.ui.isFiring) { + logger.debug(`${this.type} is not firing anymore`); + alertState.ui.isFiring = false; + alertState.ui.resolvedMS = +new Date(); + alertState.ui.message = this.getUiMessage(alertState, item); + shouldExecuteActions = true; + } + + if (indexInState === -1) { + alertInstanceState.alertStates.push(alertState); + } else { + alertInstanceState.alertStates = [ + ...alertInstanceState.alertStates.slice(0, indexInState), + alertState, + ...alertInstanceState.alertStates.slice(indexInState + 1), + ]; + } + + instance.replaceState(alertInstanceState); + if (shouldExecuteActions) { + this.executeActions(instance, alertInstanceState, item, cluster); + } + } + } + + public getDefaultActionMessage(forDefaultServerLog: boolean): string { + return forDefaultServerLog + ? '{{context.internalShortMessage}}' + : '{{context.internalFullMessage}}'; + } + + protected findIndexInInstanceState(stateInstance: AlertInstanceState, cluster: AlertCluster) { + return stateInstance.alertStates.findIndex( + (alertState) => alertState.cluster.clusterUuid === cluster.clusterUuid + ); + } + + protected getDefaultAlertState(cluster: AlertCluster, item: AlertData): AlertState { + return { + cluster, + ccs: item.ccs, + ui: { + isFiring: false, + message: null, + severity: AlertSeverity.Success, + resolvedMS: 0, + triggeredMS: 0, + lastCheckedMS: 0, + }, + }; + } + + protected getUiMessage(alertState: AlertState, item: AlertData): AlertMessage { + throw new Error('Child classes must implement `getUiMessage`'); + } + + protected executeActions( + instance: AlertInstance, + instanceState: AlertInstanceState, + item: AlertData, + cluster: AlertCluster + ) { + throw new Error('Child classes must implement `executeActions`'); + } +} diff --git a/x-pack/plugins/monitoring/server/alerts/cluster_health_alert.test.ts b/x-pack/plugins/monitoring/server/alerts/cluster_health_alert.test.ts new file mode 100644 index 0000000000000..10b75c43ac879 --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/cluster_health_alert.test.ts @@ -0,0 +1,261 @@ +/* + * 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 { ClusterHealthAlert } from './cluster_health_alert'; +import { ALERT_CLUSTER_HEALTH } from '../../common/constants'; +import { fetchLegacyAlerts } from '../lib/alerts/fetch_legacy_alerts'; +import { fetchClusters } from '../lib/alerts/fetch_clusters'; + +const RealDate = Date; + +jest.mock('../lib/alerts/fetch_legacy_alerts', () => ({ + fetchLegacyAlerts: jest.fn(), +})); +jest.mock('../lib/alerts/fetch_clusters', () => ({ + fetchClusters: jest.fn(), +})); + +describe('ClusterHealthAlert', () => { + it('should have defaults', () => { + const alert = new ClusterHealthAlert(); + expect(alert.type).toBe(ALERT_CLUSTER_HEALTH); + expect(alert.label).toBe('Cluster health'); + expect(alert.defaultThrottle).toBe('1m'); + // @ts-ignore + expect(alert.actionVariables).toStrictEqual([ + { + name: 'internalShortMessage', + description: 'The short internal message generated by Elastic.', + }, + { + name: 'internalFullMessage', + description: 'The full internal message generated by Elastic.', + }, + { name: 'state', description: 'The current state of the alert.' }, + { name: 'clusterHealth', description: 'The health of the cluster.' }, + { name: 'clusterName', description: 'The cluster to which the nodes belong.' }, + { name: 'action', description: 'The recommended action for this alert.' }, + { + name: 'actionPlain', + description: 'The recommended action for this alert, without any markdown.', + }, + ]); + }); + + describe('execute', () => { + function FakeDate() {} + FakeDate.prototype.valueOf = () => 1; + + const clusterUuid = 'abc123'; + const clusterName = 'testCluster'; + const legacyAlert = { + prefix: 'Elasticsearch cluster status is yellow.', + message: 'Allocate missing replica shards.', + metadata: { + severity: 2000, + cluster_uuid: clusterUuid, + }, + }; + const getUiSettingsService = () => ({ + asScopedToClient: jest.fn(), + }); + const getLogger = () => ({ + debug: jest.fn(), + }); + const monitoringCluster = null; + const config = { + ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } }, + }; + const kibanaUrl = 'http://localhost:5601'; + + const replaceState = jest.fn(); + const scheduleActions = jest.fn(); + const getState = jest.fn(); + const executorOptions = { + services: { + callCluster: jest.fn(), + alertInstanceFactory: jest.fn().mockImplementation(() => { + return { + replaceState, + scheduleActions, + getState, + }; + }), + }, + state: {}, + }; + + beforeEach(() => { + // @ts-ignore + Date = FakeDate; + (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + return [legacyAlert]; + }); + (fetchClusters as jest.Mock).mockImplementation(() => { + return [{ clusterUuid, clusterName }]; + }); + }); + + afterEach(() => { + Date = RealDate; + replaceState.mockReset(); + scheduleActions.mockReset(); + getState.mockReset(); + }); + + it('should fire actions', async () => { + const alert = new ClusterHealthAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).toHaveBeenCalledWith({ + alertStates: [ + { + cluster: { clusterUuid: 'abc123', clusterName: 'testCluster' }, + ccs: null, + ui: { + isFiring: true, + message: { + text: 'Elasticsearch cluster health is yellow.', + nextSteps: [ + { + text: 'Allocate missing replica shards. #start_linkView now#end_link', + tokens: [ + { + startToken: '#start_link', + endToken: '#end_link', + type: 'link', + url: 'elasticsearch/indices', + }, + ], + }, + ], + }, + severity: 'danger', + resolvedMS: 0, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }); + expect(scheduleActions).toHaveBeenCalledWith('default', { + action: + '[Allocate missing replica shards.](http://localhost:5601/app/monitoring#elasticsearch/indices?_g=(cluster_uuid:abc123))', + actionPlain: 'Allocate missing replica shards.', + internalFullMessage: + 'Cluster health alert is firing for testCluster. Current health is yellow. [Allocate missing replica shards.](http://localhost:5601/app/monitoring#elasticsearch/indices?_g=(cluster_uuid:abc123))', + internalShortMessage: + 'Cluster health alert is firing for testCluster. Current health is yellow. Allocate missing replica shards.', + clusterName, + clusterHealth: 'yellow', + state: 'firing', + }); + }); + + it('should not fire actions if there is no legacy alert', async () => { + (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + return []; + }); + const alert = new ClusterHealthAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).not.toHaveBeenCalledWith({}); + expect(scheduleActions).not.toHaveBeenCalled(); + }); + + it('should resolve with a resolved message', async () => { + (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + return [ + { + ...legacyAlert, + resolved_timestamp: 1, + }, + ]; + }); + (getState as jest.Mock).mockImplementation(() => { + return { + alertStates: [ + { + cluster: { + clusterUuid, + clusterName, + }, + ccs: null, + ui: { + isFiring: true, + message: null, + severity: 'danger', + resolvedMS: 0, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }; + }); + const alert = new ClusterHealthAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).toHaveBeenCalledWith({ + alertStates: [ + { + cluster: { clusterUuid, clusterName }, + ccs: null, + ui: { + isFiring: false, + message: { + text: 'Elasticsearch cluster health is green.', + }, + severity: 'danger', + resolvedMS: 1, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }); + expect(scheduleActions).toHaveBeenCalledWith('default', { + internalFullMessage: 'Cluster health alert is resolved for testCluster.', + internalShortMessage: 'Cluster health alert is resolved for testCluster.', + clusterName, + clusterHealth: 'yellow', + state: 'resolved', + }); + }); + }); +}); diff --git a/x-pack/plugins/monitoring/server/alerts/cluster_health_alert.ts b/x-pack/plugins/monitoring/server/alerts/cluster_health_alert.ts new file mode 100644 index 0000000000000..bb6c471591417 --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/cluster_health_alert.ts @@ -0,0 +1,273 @@ +/* + * 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 { IUiSettingsClient } from 'kibana/server'; +import { i18n } from '@kbn/i18n'; +import { BaseAlert } from './base_alert'; +import { + AlertData, + AlertCluster, + AlertState, + AlertMessage, + AlertMessageLinkToken, + AlertInstanceState, + LegacyAlert, +} from './types'; +import { AlertInstance } from '../../../alerts/server'; +import { INDEX_ALERTS, ALERT_CLUSTER_HEALTH } from '../../common/constants'; +import { getCcsIndexPattern } from '../lib/alerts/get_ccs_index_pattern'; +import { AlertMessageTokenType, AlertClusterHealthType } from '../../common/enums'; +import { fetchLegacyAlerts } from '../lib/alerts/fetch_legacy_alerts'; +import { mapLegacySeverity } from '../lib/alerts/map_legacy_severity'; +import { CommonAlertParams } from '../../common/types'; + +const RED_STATUS_MESSAGE = i18n.translate('xpack.monitoring.alerts.clusterHealth.redMessage', { + defaultMessage: 'Allocate missing primary and replica shards', +}); + +const YELLOW_STATUS_MESSAGE = i18n.translate( + 'xpack.monitoring.alerts.clusterHealth.yellowMessage', + { + defaultMessage: 'Allocate missing replica shards', + } +); + +const WATCH_NAME = 'elasticsearch_cluster_status'; + +export class ClusterHealthAlert extends BaseAlert { + public type = ALERT_CLUSTER_HEALTH; + public label = i18n.translate('xpack.monitoring.alerts.clusterHealth.label', { + defaultMessage: 'Cluster health', + }); + public isLegacy = true; + + protected actionVariables = [ + { + name: 'internalShortMessage', + description: i18n.translate( + 'xpack.monitoring.alerts.clusterHealth.actionVariables.internalShortMessage', + { + defaultMessage: 'The short internal message generated by Elastic.', + } + ), + }, + { + name: 'internalFullMessage', + description: i18n.translate( + 'xpack.monitoring.alerts.clusterHealth.actionVariables.internalFullMessage', + { + defaultMessage: 'The full internal message generated by Elastic.', + } + ), + }, + { + name: 'state', + description: i18n.translate('xpack.monitoring.alerts.clusterHealth.actionVariables.state', { + defaultMessage: 'The current state of the alert.', + }), + }, + { + name: 'clusterHealth', + description: i18n.translate( + 'xpack.monitoring.alerts.clusterHealth.actionVariables.clusterHealth', + { + defaultMessage: 'The health of the cluster.', + } + ), + }, + { + name: 'clusterName', + description: i18n.translate( + 'xpack.monitoring.alerts.clusterHealth.actionVariables.clusterName', + { + defaultMessage: 'The cluster to which the nodes belong.', + } + ), + }, + { + name: 'action', + description: i18n.translate('xpack.monitoring.alerts.clusterHealth.actionVariables.action', { + defaultMessage: 'The recommended action for this alert.', + }), + }, + { + name: 'actionPlain', + description: i18n.translate( + 'xpack.monitoring.alerts.clusterHealth.actionVariables.actionPlain', + { + defaultMessage: 'The recommended action for this alert, without any markdown.', + } + ), + }, + ]; + + protected async fetchData( + params: CommonAlertParams, + callCluster: any, + clusters: AlertCluster[], + uiSettings: IUiSettingsClient, + availableCcs: string[] + ): Promise { + let alertIndexPattern = INDEX_ALERTS; + if (availableCcs) { + alertIndexPattern = getCcsIndexPattern(alertIndexPattern, availableCcs); + } + const legacyAlerts = await fetchLegacyAlerts( + callCluster, + clusters, + alertIndexPattern, + WATCH_NAME, + this.config.ui.max_bucket_size + ); + return legacyAlerts.reduce((accum: AlertData[], legacyAlert) => { + accum.push({ + instanceKey: `${legacyAlert.metadata.cluster_uuid}`, + clusterUuid: legacyAlert.metadata.cluster_uuid, + shouldFire: !legacyAlert.resolved_timestamp, + severity: mapLegacySeverity(legacyAlert.metadata.severity), + meta: legacyAlert, + ccs: null, + }); + return accum; + }, []); + } + + private getHealth(legacyAlert: LegacyAlert) { + const prefixStr = 'Elasticsearch cluster status is '; + return legacyAlert.prefix.slice( + legacyAlert.prefix.indexOf(prefixStr) + prefixStr.length, + legacyAlert.prefix.length - 1 + ) as AlertClusterHealthType; + } + + protected getUiMessage(alertState: AlertState, item: AlertData): AlertMessage { + const legacyAlert = item.meta as LegacyAlert; + const health = this.getHealth(legacyAlert); + if (!alertState.ui.isFiring) { + return { + text: i18n.translate('xpack.monitoring.alerts.clusterHealth.ui.resolvedMessage', { + defaultMessage: `Elasticsearch cluster health is green.`, + }), + }; + } + + return { + text: i18n.translate('xpack.monitoring.alerts.clusterHealth.ui.firingMessage', { + defaultMessage: `Elasticsearch cluster health is {health}.`, + values: { + health, + }, + }), + nextSteps: [ + { + text: i18n.translate('xpack.monitoring.alerts.clusterHealth.ui.nextSteps.message1', { + defaultMessage: `{message}. #start_linkView now#end_link`, + values: { + message: + health === AlertClusterHealthType.Red ? RED_STATUS_MESSAGE : YELLOW_STATUS_MESSAGE, + }, + }), + tokens: [ + { + startToken: '#start_link', + endToken: '#end_link', + type: AlertMessageTokenType.Link, + url: 'elasticsearch/indices', + } as AlertMessageLinkToken, + ], + }, + ], + }; + } + + protected async executeActions( + instance: AlertInstance, + instanceState: AlertInstanceState, + item: AlertData, + cluster: AlertCluster + ) { + if (instanceState.alertStates.length === 0) { + return; + } + const alertState = instanceState.alertStates[0]; + const legacyAlert = item.meta as LegacyAlert; + const health = this.getHealth(legacyAlert); + if (!alertState.ui.isFiring) { + instance.scheduleActions('default', { + internalShortMessage: i18n.translate( + 'xpack.monitoring.alerts.clusterHealth.resolved.internalShortMessage', + { + defaultMessage: `Cluster health alert is resolved for {clusterName}.`, + values: { + clusterName: cluster.clusterName, + }, + } + ), + internalFullMessage: i18n.translate( + 'xpack.monitoring.alerts.clusterHealth.resolved.internalFullMessage', + { + defaultMessage: `Cluster health alert is resolved for {clusterName}.`, + values: { + clusterName: cluster.clusterName, + }, + } + ), + state: i18n.translate('xpack.monitoring.alerts.clusterHealth.resolved', { + defaultMessage: `resolved`, + }), + clusterHealth: health, + clusterName: cluster.clusterName, + }); + } else { + const actionText = + health === AlertClusterHealthType.Red + ? i18n.translate('xpack.monitoring.alerts.clusterHealth.action.danger', { + defaultMessage: `Allocate missing primary and replica shards.`, + }) + : i18n.translate('xpack.monitoring.alerts.clusterHealth.action.warning', { + defaultMessage: `Allocate missing replica shards.`, + }); + const globalState = [`cluster_uuid:${cluster.clusterUuid}`]; + if (alertState.ccs) { + globalState.push(`ccs:${alertState.ccs}`); + } + const url = `${this.kibanaUrl}/app/monitoring#elasticsearch/indices?_g=(${globalState.join( + ',' + )})`; + const action = `[${actionText}](${url})`; + instance.scheduleActions('default', { + internalShortMessage: i18n.translate( + 'xpack.monitoring.alerts.clusterHealth.firing.internalShortMessage', + { + defaultMessage: `Cluster health alert is firing for {clusterName}. Current health is {health}. {actionText}`, + values: { + clusterName: cluster.clusterName, + health, + actionText, + }, + } + ), + internalFullMessage: i18n.translate( + 'xpack.monitoring.alerts.clusterHealth.firing.internalFullMessage', + { + defaultMessage: `Cluster health alert is firing for {clusterName}. Current health is {health}. {action}`, + values: { + clusterName: cluster.clusterName, + health, + action, + }, + } + ), + state: i18n.translate('xpack.monitoring.alerts.clusterHealth.firing', { + defaultMessage: `firing`, + }), + clusterHealth: health, + clusterName: cluster.clusterName, + action, + actionPlain: actionText, + }); + } + } +} diff --git a/x-pack/plugins/monitoring/server/alerts/cluster_state.test.ts b/x-pack/plugins/monitoring/server/alerts/cluster_state.test.ts deleted file mode 100644 index 6262036037712..0000000000000 --- a/x-pack/plugins/monitoring/server/alerts/cluster_state.test.ts +++ /dev/null @@ -1,175 +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 { Logger } from 'src/core/server'; -import { getClusterState } from './cluster_state'; -import { ALERT_TYPE_CLUSTER_STATE } from '../../common/constants'; -import { AlertCommonParams, AlertCommonState, AlertClusterStatePerClusterState } from './types'; -import { getPreparedAlert } from '../lib/alerts/get_prepared_alert'; -import { executeActions } from '../lib/alerts/cluster_state.lib'; -import { AlertClusterStateState } from './enums'; -import { alertsMock, AlertServicesMock } from '../../../alerts/server/mocks'; - -jest.mock('../lib/alerts/cluster_state.lib', () => ({ - executeActions: jest.fn(), - getUiMessage: jest.fn(), -})); - -jest.mock('../lib/alerts/get_prepared_alert', () => ({ - getPreparedAlert: jest.fn(() => { - return { - emailAddress: 'foo@foo.com', - }; - }), -})); - -describe('getClusterState', () => { - const services: AlertServicesMock = alertsMock.createAlertServices(); - - const params: AlertCommonParams = { - dateFormat: 'YYYY', - timezone: 'UTC', - }; - - const emailAddress = 'foo@foo.com'; - const clusterUuid = 'kdksdfj434'; - const clusterName = 'monitoring_test'; - const cluster = { clusterUuid, clusterName }; - - async function setupAlert( - previousState: AlertClusterStateState, - newState: AlertClusterStateState - ): Promise { - const logger: Logger = { - warn: jest.fn(), - log: jest.fn(), - debug: jest.fn(), - trace: jest.fn(), - error: jest.fn(), - fatal: jest.fn(), - info: jest.fn(), - get: jest.fn(), - }; - const getLogger = (): Logger => logger; - const ccrEnabled = false; - (getPreparedAlert as jest.Mock).mockImplementation(() => ({ - emailAddress, - data: [ - { - state: newState, - clusterUuid, - }, - ], - clusters: [cluster], - })); - - const alert = getClusterState(null as any, null as any, getLogger, ccrEnabled); - const state: AlertCommonState = { - [clusterUuid]: { - state: previousState, - ui: { - isFiring: false, - severity: 0, - message: null, - resolvedMS: 0, - lastCheckedMS: 0, - triggeredMS: 0, - }, - } as AlertClusterStatePerClusterState, - }; - - return (await alert.executor({ services, params, state } as any)) as AlertCommonState; - } - - afterEach(() => { - (executeActions as jest.Mock).mockClear(); - }); - - it('should configure the alert properly', () => { - const alert = getClusterState(null as any, null as any, jest.fn(), false); - expect(alert.id).toBe(ALERT_TYPE_CLUSTER_STATE); - expect(alert.actionGroups).toEqual([{ id: 'default', name: 'Default' }]); - }); - - it('should alert if green -> yellow', async () => { - const result = await setupAlert(AlertClusterStateState.Green, AlertClusterStateState.Yellow); - expect(executeActions).toHaveBeenCalledWith( - services.alertInstanceFactory(ALERT_TYPE_CLUSTER_STATE), - cluster, - AlertClusterStateState.Yellow, - emailAddress - ); - const clusterResult = result[clusterUuid] as AlertClusterStatePerClusterState; - expect(clusterResult.state).toBe(AlertClusterStateState.Yellow); - expect(clusterResult.ui.isFiring).toBe(true); - expect(clusterResult.ui.resolvedMS).toBe(0); - }); - - it('should alert if yellow -> green', async () => { - const result = await setupAlert(AlertClusterStateState.Yellow, AlertClusterStateState.Green); - expect(executeActions).toHaveBeenCalledWith( - services.alertInstanceFactory(ALERT_TYPE_CLUSTER_STATE), - cluster, - AlertClusterStateState.Green, - emailAddress, - true - ); - const clusterResult = result[clusterUuid] as AlertClusterStatePerClusterState; - expect(clusterResult.state).toBe(AlertClusterStateState.Green); - expect(clusterResult.ui.resolvedMS).toBeGreaterThan(0); - }); - - it('should alert if green -> red', async () => { - const result = await setupAlert(AlertClusterStateState.Green, AlertClusterStateState.Red); - expect(executeActions).toHaveBeenCalledWith( - services.alertInstanceFactory(ALERT_TYPE_CLUSTER_STATE), - cluster, - AlertClusterStateState.Red, - emailAddress - ); - const clusterResult = result[clusterUuid] as AlertClusterStatePerClusterState; - expect(clusterResult.state).toBe(AlertClusterStateState.Red); - expect(clusterResult.ui.isFiring).toBe(true); - expect(clusterResult.ui.resolvedMS).toBe(0); - }); - - it('should alert if red -> green', async () => { - const result = await setupAlert(AlertClusterStateState.Red, AlertClusterStateState.Green); - expect(executeActions).toHaveBeenCalledWith( - services.alertInstanceFactory(ALERT_TYPE_CLUSTER_STATE), - cluster, - AlertClusterStateState.Green, - emailAddress, - true - ); - const clusterResult = result[clusterUuid] as AlertClusterStatePerClusterState; - expect(clusterResult.state).toBe(AlertClusterStateState.Green); - expect(clusterResult.ui.resolvedMS).toBeGreaterThan(0); - }); - - it('should not alert if red -> yellow', async () => { - const result = await setupAlert(AlertClusterStateState.Red, AlertClusterStateState.Yellow); - expect(executeActions).not.toHaveBeenCalled(); - const clusterResult = result[clusterUuid] as AlertClusterStatePerClusterState; - expect(clusterResult.state).toBe(AlertClusterStateState.Red); - expect(clusterResult.ui.resolvedMS).toBe(0); - }); - - it('should not alert if yellow -> red', async () => { - const result = await setupAlert(AlertClusterStateState.Yellow, AlertClusterStateState.Red); - expect(executeActions).not.toHaveBeenCalled(); - const clusterResult = result[clusterUuid] as AlertClusterStatePerClusterState; - expect(clusterResult.state).toBe(AlertClusterStateState.Yellow); - expect(clusterResult.ui.resolvedMS).toBe(0); - }); - - it('should not alert if green -> green', async () => { - const result = await setupAlert(AlertClusterStateState.Green, AlertClusterStateState.Green); - expect(executeActions).not.toHaveBeenCalled(); - const clusterResult = result[clusterUuid] as AlertClusterStatePerClusterState; - expect(clusterResult.state).toBe(AlertClusterStateState.Green); - expect(clusterResult.ui.resolvedMS).toBe(0); - }); -}); diff --git a/x-pack/plugins/monitoring/server/alerts/cluster_state.ts b/x-pack/plugins/monitoring/server/alerts/cluster_state.ts deleted file mode 100644 index c357a5878b93a..0000000000000 --- a/x-pack/plugins/monitoring/server/alerts/cluster_state.ts +++ /dev/null @@ -1,135 +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 moment from 'moment-timezone'; -import { i18n } from '@kbn/i18n'; -import { Logger, ILegacyCustomClusterClient, UiSettingsServiceStart } from 'src/core/server'; -import { ALERT_TYPE_CLUSTER_STATE } from '../../common/constants'; -import { AlertType } from '../../../alerts/server'; -import { executeActions, getUiMessage } from '../lib/alerts/cluster_state.lib'; -import { - AlertCommonExecutorOptions, - AlertCommonState, - AlertClusterStatePerClusterState, - AlertCommonCluster, -} from './types'; -import { AlertClusterStateState } from './enums'; -import { getPreparedAlert } from '../lib/alerts/get_prepared_alert'; -import { fetchClusterState } from '../lib/alerts/fetch_cluster_state'; - -export const getClusterState = ( - getUiSettingsService: () => Promise, - monitoringCluster: ILegacyCustomClusterClient, - getLogger: (...scopes: string[]) => Logger, - ccsEnabled: boolean -): AlertType => { - const logger = getLogger(ALERT_TYPE_CLUSTER_STATE); - return { - id: ALERT_TYPE_CLUSTER_STATE, - name: 'Monitoring Alert - Cluster Status', - actionGroups: [ - { - id: 'default', - name: i18n.translate('xpack.monitoring.alerts.clusterState.actionGroups.default', { - defaultMessage: 'Default', - }), - }, - ], - producer: 'monitoring', - defaultActionGroupId: 'default', - async executor({ - services, - params, - state, - }: AlertCommonExecutorOptions): Promise { - logger.debug( - `Firing alert with params: ${JSON.stringify(params)} and state: ${JSON.stringify(state)}` - ); - - const preparedAlert = await getPreparedAlert( - ALERT_TYPE_CLUSTER_STATE, - getUiSettingsService, - monitoringCluster, - logger, - ccsEnabled, - services, - fetchClusterState - ); - - if (!preparedAlert) { - return state; - } - - const { emailAddress, data: states, clusters } = preparedAlert; - - const result: AlertCommonState = { ...state }; - const defaultAlertState: AlertClusterStatePerClusterState = { - state: AlertClusterStateState.Green, - ui: { - isFiring: false, - message: null, - severity: 0, - resolvedMS: 0, - triggeredMS: 0, - lastCheckedMS: 0, - }, - }; - - for (const clusterState of states) { - const alertState: AlertClusterStatePerClusterState = - (state[clusterState.clusterUuid] as AlertClusterStatePerClusterState) || - defaultAlertState; - const cluster = clusters.find( - (c: AlertCommonCluster) => c.clusterUuid === clusterState.clusterUuid - ); - if (!cluster) { - logger.warn(`Unable to find cluster for clusterUuid='${clusterState.clusterUuid}'`); - continue; - } - const isNonGreen = clusterState.state !== AlertClusterStateState.Green; - const severity = clusterState.state === AlertClusterStateState.Red ? 2100 : 1100; - - const ui = alertState.ui; - let triggered = ui.triggeredMS; - let resolved = ui.resolvedMS; - let message = ui.message || {}; - let lastState = alertState.state; - const instance = services.alertInstanceFactory(ALERT_TYPE_CLUSTER_STATE); - - if (isNonGreen) { - if (lastState === AlertClusterStateState.Green) { - logger.debug(`Cluster state changed from green to ${clusterState.state}`); - executeActions(instance, cluster, clusterState.state, emailAddress); - lastState = clusterState.state; - triggered = moment().valueOf(); - } - message = getUiMessage(clusterState.state); - resolved = 0; - } else if (!isNonGreen && lastState !== AlertClusterStateState.Green) { - logger.debug(`Cluster state changed from ${lastState} to green`); - executeActions(instance, cluster, clusterState.state, emailAddress, true); - lastState = clusterState.state; - message = getUiMessage(clusterState.state, true); - resolved = moment().valueOf(); - } - - result[clusterState.clusterUuid] = { - state: lastState, - ui: { - message, - isFiring: isNonGreen, - severity, - resolvedMS: resolved, - triggeredMS: triggered, - lastCheckedMS: moment().valueOf(), - }, - } as AlertClusterStatePerClusterState; - } - - return result; - }, - }; -}; diff --git a/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.test.ts b/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.test.ts new file mode 100644 index 0000000000000..f0d11abab1492 --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.test.ts @@ -0,0 +1,376 @@ +/* + * 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 { CpuUsageAlert } from './cpu_usage_alert'; +import { ALERT_CPU_USAGE } from '../../common/constants'; +import { fetchCpuUsageNodeStats } from '../lib/alerts/fetch_cpu_usage_node_stats'; +import { fetchClusters } from '../lib/alerts/fetch_clusters'; + +const RealDate = Date; + +jest.mock('../lib/alerts/fetch_cpu_usage_node_stats', () => ({ + fetchCpuUsageNodeStats: jest.fn(), +})); +jest.mock('../lib/alerts/fetch_clusters', () => ({ + fetchClusters: jest.fn(), +})); + +describe('CpuUsageAlert', () => { + it('should have defaults', () => { + const alert = new CpuUsageAlert(); + expect(alert.type).toBe(ALERT_CPU_USAGE); + expect(alert.label).toBe('CPU Usage'); + expect(alert.defaultThrottle).toBe('1m'); + // @ts-ignore + expect(alert.defaultParams).toStrictEqual({ threshold: 90, duration: '5m' }); + // @ts-ignore + expect(alert.actionVariables).toStrictEqual([ + { + name: 'internalShortMessage', + description: 'The short internal message generated by Elastic.', + }, + { + name: 'internalFullMessage', + description: 'The full internal message generated by Elastic.', + }, + { name: 'state', description: 'The current state of the alert.' }, + { name: 'nodes', description: 'The list of nodes reporting high cpu usage.' }, + { name: 'count', description: 'The number of nodes reporting high cpu usage.' }, + { name: 'clusterName', description: 'The cluster to which the nodes belong.' }, + { name: 'action', description: 'The recommended action for this alert.' }, + { + name: 'actionPlain', + description: 'The recommended action for this alert, without any markdown.', + }, + ]); + }); + + describe('execute', () => { + function FakeDate() {} + FakeDate.prototype.valueOf = () => 1; + + const clusterUuid = 'abc123'; + const clusterName = 'testCluster'; + const nodeId = 'myNodeId'; + const nodeName = 'myNodeName'; + const cpuUsage = 91; + const stat = { + clusterUuid, + nodeId, + nodeName, + cpuUsage, + }; + const getUiSettingsService = () => ({ + asScopedToClient: jest.fn(), + }); + const getLogger = () => ({ + debug: jest.fn(), + }); + const monitoringCluster = null; + const config = { + ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } }, + }; + const kibanaUrl = 'http://localhost:5601'; + + const replaceState = jest.fn(); + const scheduleActions = jest.fn(); + const getState = jest.fn(); + const executorOptions = { + services: { + callCluster: jest.fn(), + alertInstanceFactory: jest.fn().mockImplementation(() => { + return { + replaceState, + scheduleActions, + getState, + }; + }), + }, + state: {}, + }; + + beforeEach(() => { + // @ts-ignore + Date = FakeDate; + (fetchCpuUsageNodeStats as jest.Mock).mockImplementation(() => { + return [stat]; + }); + (fetchClusters as jest.Mock).mockImplementation(() => { + return [{ clusterUuid, clusterName }]; + }); + }); + + afterEach(() => { + Date = RealDate; + replaceState.mockReset(); + scheduleActions.mockReset(); + getState.mockReset(); + }); + + it('should fire actions', async () => { + const alert = new CpuUsageAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + const count = 1; + expect(replaceState).toHaveBeenCalledWith({ + alertStates: [ + { + cluster: { clusterUuid, clusterName }, + cpuUsage, + nodeId, + nodeName, + ui: { + isFiring: true, + message: { + text: + 'Node #start_linkmyNodeName#end_link is reporting cpu usage of 91.00% at #absolute', + nextSteps: [ + { + text: '#start_linkCheck hot threads#end_link', + tokens: [ + { + startToken: '#start_link', + endToken: '#end_link', + type: 'docLink', + partialUrl: + '{elasticWebsiteUrl}/guide/en/elasticsearch/reference/{docLinkVersion}/cluster-nodes-hot-threads.html', + }, + ], + }, + { + text: '#start_linkCheck long running tasks#end_link', + tokens: [ + { + startToken: '#start_link', + endToken: '#end_link', + type: 'docLink', + partialUrl: + '{elasticWebsiteUrl}/guide/en/elasticsearch/reference/{docLinkVersion}/tasks.html', + }, + ], + }, + ], + tokens: [ + { + startToken: '#absolute', + type: 'time', + isAbsolute: true, + isRelative: false, + timestamp: 1, + }, + { + startToken: '#start_link', + endToken: '#end_link', + type: 'link', + url: 'elasticsearch/nodes/myNodeId', + }, + ], + }, + severity: 'danger', + resolvedMS: 0, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }); + expect(scheduleActions).toHaveBeenCalledWith('default', { + internalFullMessage: `CPU usage alert is firing for ${count} node(s) in cluster: ${clusterName}. [View nodes](http://localhost:5601/app/monitoring#elasticsearch/nodes?_g=(cluster_uuid:${clusterUuid}))`, + internalShortMessage: `CPU usage alert is firing for ${count} node(s) in cluster: ${clusterName}. Verify CPU levels across affected nodes.`, + action: `[View nodes](http://localhost:5601/app/monitoring#elasticsearch/nodes?_g=(cluster_uuid:${clusterUuid}))`, + actionPlain: 'Verify CPU levels across affected nodes.', + clusterName, + count, + nodes: `${nodeName}:${cpuUsage.toFixed(2)}`, + state: 'firing', + }); + }); + + it('should not fire actions if under threshold', async () => { + (fetchCpuUsageNodeStats as jest.Mock).mockImplementation(() => { + return [ + { + ...stat, + cpuUsage: 1, + }, + ]; + }); + const alert = new CpuUsageAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).toHaveBeenCalledWith({ + alertStates: [ + { + ccs: undefined, + cluster: { + clusterUuid, + clusterName, + }, + cpuUsage: 1, + nodeId, + nodeName, + ui: { + isFiring: false, + lastCheckedMS: 0, + message: null, + resolvedMS: 0, + severity: 'danger', + triggeredMS: 0, + }, + }, + ], + }); + expect(scheduleActions).not.toHaveBeenCalled(); + }); + + it('should resolve with a resolved message', async () => { + (fetchCpuUsageNodeStats as jest.Mock).mockImplementation(() => { + return [ + { + ...stat, + cpuUsage: 1, + }, + ]; + }); + (getState as jest.Mock).mockImplementation(() => { + return { + alertStates: [ + { + cluster: { + clusterUuid, + clusterName, + }, + ccs: null, + cpuUsage: 91, + nodeId, + nodeName, + ui: { + isFiring: true, + message: null, + severity: 'danger', + resolvedMS: 0, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }; + }); + const alert = new CpuUsageAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + const count = 1; + expect(replaceState).toHaveBeenCalledWith({ + alertStates: [ + { + cluster: { clusterUuid, clusterName }, + ccs: null, + cpuUsage: 1, + nodeId, + nodeName, + ui: { + isFiring: false, + message: { + text: + 'The cpu usage on node myNodeName is now under the threshold, currently reporting at 1.00% as of #resolved', + tokens: [ + { + startToken: '#resolved', + type: 'time', + isAbsolute: true, + isRelative: false, + timestamp: 1, + }, + ], + }, + severity: 'danger', + resolvedMS: 1, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }); + expect(scheduleActions).toHaveBeenCalledWith('default', { + internalFullMessage: `CPU usage alert is resolved for ${count} node(s) in cluster: ${clusterName}.`, + internalShortMessage: `CPU usage alert is resolved for ${count} node(s) in cluster: ${clusterName}.`, + clusterName, + count, + nodes: `${nodeName}:1.00`, + state: 'resolved', + }); + }); + + it('should handle ccs', async () => { + const ccs = 'testCluster'; + (fetchCpuUsageNodeStats as jest.Mock).mockImplementation(() => { + return [ + { + ...stat, + ccs, + }, + ]; + }); + const alert = new CpuUsageAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + const count = 1; + expect(scheduleActions).toHaveBeenCalledWith('default', { + internalFullMessage: `CPU usage alert is firing for ${count} node(s) in cluster: ${clusterName}. [View nodes](http://localhost:5601/app/monitoring#elasticsearch/nodes?_g=(cluster_uuid:${clusterUuid},ccs:${ccs}))`, + internalShortMessage: `CPU usage alert is firing for ${count} node(s) in cluster: ${clusterName}. Verify CPU levels across affected nodes.`, + action: `[View nodes](http://localhost:5601/app/monitoring#elasticsearch/nodes?_g=(cluster_uuid:${clusterUuid},ccs:${ccs}))`, + actionPlain: 'Verify CPU levels across affected nodes.', + clusterName, + count, + nodes: `${nodeName}:${cpuUsage.toFixed(2)}`, + state: 'firing', + }); + }); + }); +}); diff --git a/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts b/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts new file mode 100644 index 0000000000000..9171745fba747 --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts @@ -0,0 +1,451 @@ +/* + * 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 { IUiSettingsClient, Logger } from 'kibana/server'; +import { i18n } from '@kbn/i18n'; +import { BaseAlert } from './base_alert'; +import { + AlertData, + AlertCluster, + AlertState, + AlertMessage, + AlertCpuUsageState, + AlertCpuUsageNodeStats, + AlertMessageTimeToken, + AlertMessageLinkToken, + AlertInstanceState, + AlertMessageDocLinkToken, +} from './types'; +import { AlertInstance, AlertServices } from '../../../alerts/server'; +import { INDEX_PATTERN_ELASTICSEARCH, ALERT_CPU_USAGE } from '../../common/constants'; +import { fetchCpuUsageNodeStats } from '../lib/alerts/fetch_cpu_usage_node_stats'; +import { getCcsIndexPattern } from '../lib/alerts/get_ccs_index_pattern'; +import { AlertMessageTokenType, AlertSeverity, AlertParamType } from '../../common/enums'; +import { RawAlertInstance } from '../../../alerts/common'; +import { parseDuration } from '../../../alerts/common/parse_duration'; +import { + CommonAlertFilter, + CommonAlertCpuUsageFilter, + CommonAlertParams, + CommonAlertParamDetail, +} from '../../common/types'; + +const RESOLVED = i18n.translate('xpack.monitoring.alerts.cpuUsage.resolved', { + defaultMessage: 'resolved', +}); +const FIRING = i18n.translate('xpack.monitoring.alerts.cpuUsage.firing', { + defaultMessage: 'firing', +}); + +const DEFAULT_THRESHOLD = 90; +const DEFAULT_DURATION = '5m'; + +interface CpuUsageParams { + threshold: number; + duration: string; +} + +export class CpuUsageAlert extends BaseAlert { + public static paramDetails = { + threshold: { + label: i18n.translate('xpack.monitoring.alerts.cpuUsage.paramDetails.threshold.label', { + defaultMessage: `Notify when CPU is over`, + }), + type: AlertParamType.Percentage, + } as CommonAlertParamDetail, + duration: { + label: i18n.translate('xpack.monitoring.alerts.cpuUsage.paramDetails.duration.label', { + defaultMessage: `Look at the average over`, + }), + type: AlertParamType.Duration, + } as CommonAlertParamDetail, + }; + + public type = ALERT_CPU_USAGE; + public label = i18n.translate('xpack.monitoring.alerts.cpuUsage.label', { + defaultMessage: 'CPU Usage', + }); + + protected defaultParams: CpuUsageParams = { + threshold: DEFAULT_THRESHOLD, + duration: DEFAULT_DURATION, + }; + + protected actionVariables = [ + { + name: 'internalShortMessage', + description: i18n.translate( + 'xpack.monitoring.alerts.cpuUsage.actionVariables.internalShortMessage', + { + defaultMessage: 'The short internal message generated by Elastic.', + } + ), + }, + { + name: 'internalFullMessage', + description: i18n.translate( + 'xpack.monitoring.alerts.cpuUsage.actionVariables.internalFullMessage', + { + defaultMessage: 'The full internal message generated by Elastic.', + } + ), + }, + { + name: 'state', + description: i18n.translate('xpack.monitoring.alerts.cpuUsage.actionVariables.state', { + defaultMessage: 'The current state of the alert.', + }), + }, + { + name: 'nodes', + description: i18n.translate('xpack.monitoring.alerts.cpuUsage.actionVariables.nodes', { + defaultMessage: 'The list of nodes reporting high cpu usage.', + }), + }, + { + name: 'count', + description: i18n.translate('xpack.monitoring.alerts.cpuUsage.actionVariables.count', { + defaultMessage: 'The number of nodes reporting high cpu usage.', + }), + }, + { + name: 'clusterName', + description: i18n.translate('xpack.monitoring.alerts.cpuUsage.actionVariables.clusterName', { + defaultMessage: 'The cluster to which the nodes belong.', + }), + }, + { + name: 'action', + description: i18n.translate('xpack.monitoring.alerts.cpuUsage.actionVariables.action', { + defaultMessage: 'The recommended action for this alert.', + }), + }, + { + name: 'actionPlain', + description: i18n.translate('xpack.monitoring.alerts.cpuUsage.actionVariables.actionPlain', { + defaultMessage: 'The recommended action for this alert, without any markdown.', + }), + }, + ]; + + protected async fetchData( + params: CommonAlertParams, + callCluster: any, + clusters: AlertCluster[], + uiSettings: IUiSettingsClient, + availableCcs: string[] + ): Promise { + let esIndexPattern = INDEX_PATTERN_ELASTICSEARCH; + if (availableCcs) { + esIndexPattern = getCcsIndexPattern(esIndexPattern, availableCcs); + } + const duration = parseDuration(((params as unknown) as CpuUsageParams).duration); + const endMs = +new Date(); + const startMs = endMs - duration; + const stats = await fetchCpuUsageNodeStats( + callCluster, + clusters, + esIndexPattern, + startMs, + endMs, + this.config.ui.max_bucket_size + ); + return stats.map((stat) => { + let cpuUsage = 0; + if (this.config.ui.container.elasticsearch.enabled) { + cpuUsage = + (stat.containerUsage / (stat.containerPeriods * stat.containerQuota * 1000)) * 100; + } else { + cpuUsage = stat.cpuUsage; + } + + return { + instanceKey: `${stat.clusterUuid}:${stat.nodeId}`, + clusterUuid: stat.clusterUuid, + shouldFire: cpuUsage > params.threshold, + severity: AlertSeverity.Danger, + meta: stat, + ccs: stat.ccs, + }; + }); + } + + protected filterAlertInstance(alertInstance: RawAlertInstance, filters: CommonAlertFilter[]) { + const alertInstanceState = (alertInstance.state as unknown) as AlertInstanceState; + if (filters && filters.length) { + for (const _filter of filters) { + const filter = _filter as CommonAlertCpuUsageFilter; + if (filter && filter.nodeUuid) { + let nodeExistsInStates = false; + for (const state of alertInstanceState.alertStates) { + if ((state as AlertCpuUsageState).nodeId === filter.nodeUuid) { + nodeExistsInStates = true; + break; + } + } + if (!nodeExistsInStates) { + return false; + } + } + } + } + return true; + } + + protected getDefaultAlertState(cluster: AlertCluster, item: AlertData): AlertState { + const base = super.getDefaultAlertState(cluster, item); + return { + ...base, + ui: { + ...base.ui, + severity: AlertSeverity.Danger, + }, + }; + } + + protected getUiMessage(alertState: AlertState, item: AlertData): AlertMessage { + const stat = item.meta as AlertCpuUsageNodeStats; + if (!alertState.ui.isFiring) { + return { + text: i18n.translate('xpack.monitoring.alerts.cpuUsage.ui.resolvedMessage', { + defaultMessage: `The cpu usage on node {nodeName} is now under the threshold, currently reporting at {cpuUsage}% as of #resolved`, + values: { + nodeName: stat.nodeName, + cpuUsage: stat.cpuUsage.toFixed(2), + }, + }), + tokens: [ + { + startToken: '#resolved', + type: AlertMessageTokenType.Time, + isAbsolute: true, + isRelative: false, + timestamp: alertState.ui.resolvedMS, + } as AlertMessageTimeToken, + ], + }; + } + return { + text: i18n.translate('xpack.monitoring.alerts.cpuUsage.ui.firingMessage', { + defaultMessage: `Node #start_link{nodeName}#end_link is reporting cpu usage of {cpuUsage}% at #absolute`, + values: { + nodeName: stat.nodeName, + cpuUsage: stat.cpuUsage.toFixed(2), + }, + }), + nextSteps: [ + { + text: i18n.translate('xpack.monitoring.alerts.cpuUsage.ui.nextSteps.hotThreads', { + defaultMessage: `#start_linkCheck hot threads#end_link`, + }), + tokens: [ + { + startToken: '#start_link', + endToken: '#end_link', + type: AlertMessageTokenType.DocLink, + partialUrl: `{elasticWebsiteUrl}/guide/en/elasticsearch/reference/{docLinkVersion}/cluster-nodes-hot-threads.html`, + } as AlertMessageDocLinkToken, + ], + }, + { + text: i18n.translate('xpack.monitoring.alerts.cpuUsage.ui.nextSteps.runningTasks', { + defaultMessage: `#start_linkCheck long running tasks#end_link`, + }), + tokens: [ + { + startToken: '#start_link', + endToken: '#end_link', + type: AlertMessageTokenType.DocLink, + partialUrl: `{elasticWebsiteUrl}/guide/en/elasticsearch/reference/{docLinkVersion}/tasks.html`, + } as AlertMessageDocLinkToken, + ], + }, + ], + tokens: [ + { + startToken: '#absolute', + type: AlertMessageTokenType.Time, + isAbsolute: true, + isRelative: false, + timestamp: alertState.ui.triggeredMS, + } as AlertMessageTimeToken, + { + startToken: '#start_link', + endToken: '#end_link', + type: AlertMessageTokenType.Link, + url: `elasticsearch/nodes/${stat.nodeId}`, + } as AlertMessageLinkToken, + ], + }; + } + + protected executeActions( + instance: AlertInstance, + instanceState: AlertInstanceState, + item: AlertData | null, + cluster: AlertCluster + ) { + if (instanceState.alertStates.length === 0) { + return; + } + + const nodes = instanceState.alertStates + .map((_state) => { + const state = _state as AlertCpuUsageState; + return `${state.nodeName}:${state.cpuUsage.toFixed(2)}`; + }) + .join(','); + + const ccs = instanceState.alertStates.reduce((accum: string, state): string => { + if (state.ccs) { + return state.ccs; + } + return accum; + }, ''); + + const count = instanceState.alertStates.length; + if (!instanceState.alertStates[0].ui.isFiring) { + instance.scheduleActions('default', { + internalShortMessage: i18n.translate( + 'xpack.monitoring.alerts.cpuUsage.resolved.internalShortMessage', + { + defaultMessage: `CPU usage alert is resolved for {count} node(s) in cluster: {clusterName}.`, + values: { + count, + clusterName: cluster.clusterName, + }, + } + ), + internalFullMessage: i18n.translate( + 'xpack.monitoring.alerts.cpuUsage.resolved.internalFullMessage', + { + defaultMessage: `CPU usage alert is resolved for {count} node(s) in cluster: {clusterName}.`, + values: { + count, + clusterName: cluster.clusterName, + }, + } + ), + state: RESOLVED, + nodes, + count, + clusterName: cluster.clusterName, + }); + } else { + const shortActionText = i18n.translate('xpack.monitoring.alerts.cpuUsage.shortAction', { + defaultMessage: 'Verify CPU levels across affected nodes.', + }); + const fullActionText = i18n.translate('xpack.monitoring.alerts.cpuUsage.fullAction', { + defaultMessage: 'View nodes', + }); + const globalState = [`cluster_uuid:${cluster.clusterUuid}`]; + if (ccs) { + globalState.push(`ccs:${ccs}`); + } + const url = `${this.kibanaUrl}/app/monitoring#elasticsearch/nodes?_g=(${globalState.join( + ',' + )})`; + const action = `[${fullActionText}](${url})`; + instance.scheduleActions('default', { + internalShortMessage: i18n.translate( + 'xpack.monitoring.alerts.cpuUsage.firing.internalShortMessage', + { + defaultMessage: `CPU usage alert is firing for {count} node(s) in cluster: {clusterName}. {shortActionText}`, + values: { + count, + clusterName: cluster.clusterName, + shortActionText, + }, + } + ), + internalFullMessage: i18n.translate( + 'xpack.monitoring.alerts.cpuUsage.firing.internalFullMessage', + { + defaultMessage: `CPU usage alert is firing for {count} node(s) in cluster: {clusterName}. {action}`, + values: { + count, + clusterName: cluster.clusterName, + action, + }, + } + ), + state: FIRING, + nodes, + count, + clusterName: cluster.clusterName, + action, + actionPlain: shortActionText, + }); + } + } + + protected processData( + data: AlertData[], + clusters: AlertCluster[], + services: AlertServices, + logger: Logger + ) { + for (const cluster of clusters) { + const nodes = data.filter((_item) => _item.clusterUuid === cluster.clusterUuid); + if (nodes.length === 0) { + continue; + } + + const instance = services.alertInstanceFactory(`${this.type}:${cluster.clusterUuid}`); + const state = (instance.getState() as unknown) as AlertInstanceState; + const alertInstanceState: AlertInstanceState = { alertStates: state?.alertStates || [] }; + let shouldExecuteActions = false; + for (const node of nodes) { + const stat = node.meta as AlertCpuUsageNodeStats; + let nodeState: AlertCpuUsageState; + const indexInState = alertInstanceState.alertStates.findIndex((alertState) => { + const nodeAlertState = alertState as AlertCpuUsageState; + return ( + nodeAlertState.cluster.clusterUuid === cluster.clusterUuid && + nodeAlertState.nodeId === (node.meta as AlertCpuUsageNodeStats).nodeId + ); + }); + if (indexInState > -1) { + nodeState = alertInstanceState.alertStates[indexInState] as AlertCpuUsageState; + } else { + nodeState = this.getDefaultAlertState(cluster, node) as AlertCpuUsageState; + } + + nodeState.cpuUsage = stat.cpuUsage; + nodeState.nodeId = stat.nodeId; + nodeState.nodeName = stat.nodeName; + + if (node.shouldFire) { + nodeState.ui.triggeredMS = new Date().valueOf(); + nodeState.ui.isFiring = true; + nodeState.ui.message = this.getUiMessage(nodeState, node); + nodeState.ui.severity = node.severity; + nodeState.ui.resolvedMS = 0; + shouldExecuteActions = true; + } else if (!node.shouldFire && nodeState.ui.isFiring) { + nodeState.ui.isFiring = false; + nodeState.ui.resolvedMS = new Date().valueOf(); + nodeState.ui.message = this.getUiMessage(nodeState, node); + shouldExecuteActions = true; + } + + if (indexInState === -1) { + alertInstanceState.alertStates.push(nodeState); + } else { + alertInstanceState.alertStates = [ + ...alertInstanceState.alertStates.slice(0, indexInState), + nodeState, + ...alertInstanceState.alertStates.slice(indexInState + 1), + ]; + } + } + + instance.replaceState(alertInstanceState); + if (shouldExecuteActions) { + this.executeActions(instance, alertInstanceState, null, cluster); + } + } + } +} diff --git a/x-pack/plugins/monitoring/server/alerts/elasticsearch_version_mismatch_alert.test.ts b/x-pack/plugins/monitoring/server/alerts/elasticsearch_version_mismatch_alert.test.ts new file mode 100644 index 0000000000000..44684939ca261 --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/elasticsearch_version_mismatch_alert.test.ts @@ -0,0 +1,251 @@ +/* + * 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 { ElasticsearchVersionMismatchAlert } from './elasticsearch_version_mismatch_alert'; +import { ALERT_ELASTICSEARCH_VERSION_MISMATCH } from '../../common/constants'; +import { fetchLegacyAlerts } from '../lib/alerts/fetch_legacy_alerts'; +import { fetchClusters } from '../lib/alerts/fetch_clusters'; + +const RealDate = Date; + +jest.mock('../lib/alerts/fetch_legacy_alerts', () => ({ + fetchLegacyAlerts: jest.fn(), +})); +jest.mock('../lib/alerts/fetch_clusters', () => ({ + fetchClusters: jest.fn(), +})); + +describe('ElasticsearchVersionMismatchAlert', () => { + it('should have defaults', () => { + const alert = new ElasticsearchVersionMismatchAlert(); + expect(alert.type).toBe(ALERT_ELASTICSEARCH_VERSION_MISMATCH); + expect(alert.label).toBe('Elasticsearch version mismatch'); + expect(alert.defaultThrottle).toBe('1m'); + // @ts-ignore + expect(alert.actionVariables).toStrictEqual([ + { + name: 'internalShortMessage', + description: 'The short internal message generated by Elastic.', + }, + { + name: 'internalFullMessage', + description: 'The full internal message generated by Elastic.', + }, + { name: 'state', description: 'The current state of the alert.' }, + { + name: 'versionList', + description: 'The versions of Elasticsearch running in this cluster.', + }, + { name: 'clusterName', description: 'The cluster to which the nodes belong.' }, + { name: 'action', description: 'The recommended action for this alert.' }, + { + name: 'actionPlain', + description: 'The recommended action for this alert, without any markdown.', + }, + ]); + }); + + describe('execute', () => { + function FakeDate() {} + FakeDate.prototype.valueOf = () => 1; + + const clusterUuid = 'abc123'; + const clusterName = 'testCluster'; + const legacyAlert = { + prefix: 'This cluster is running with multiple versions of Elasticsearch.', + message: 'Versions: [8.0.0, 7.2.1].', + metadata: { + severity: 1000, + cluster_uuid: clusterUuid, + }, + }; + const getUiSettingsService = () => ({ + asScopedToClient: jest.fn(), + }); + const getLogger = () => ({ + debug: jest.fn(), + }); + const monitoringCluster = null; + const config = { + ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } }, + }; + const kibanaUrl = 'http://localhost:5601'; + + const replaceState = jest.fn(); + const scheduleActions = jest.fn(); + const getState = jest.fn(); + const executorOptions = { + services: { + callCluster: jest.fn(), + alertInstanceFactory: jest.fn().mockImplementation(() => { + return { + replaceState, + scheduleActions, + getState, + }; + }), + }, + state: {}, + }; + + beforeEach(() => { + // @ts-ignore + Date = FakeDate; + (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + return [legacyAlert]; + }); + (fetchClusters as jest.Mock).mockImplementation(() => { + return [{ clusterUuid, clusterName }]; + }); + }); + + afterEach(() => { + Date = RealDate; + replaceState.mockReset(); + scheduleActions.mockReset(); + getState.mockReset(); + }); + + it('should fire actions', async () => { + const alert = new ElasticsearchVersionMismatchAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).toHaveBeenCalledWith({ + alertStates: [ + { + cluster: { clusterUuid: 'abc123', clusterName: 'testCluster' }, + ccs: null, + ui: { + isFiring: true, + message: { + text: + 'Multiple versions of Elasticsearch ([8.0.0, 7.2.1]) running in this cluster.', + }, + severity: 'warning', + resolvedMS: 0, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }); + expect(scheduleActions).toHaveBeenCalledWith('default', { + action: + '[View nodes](http://localhost:5601/app/monitoring#elasticsearch/nodes?_g=(cluster_uuid:abc123))', + actionPlain: 'Verify you have the same version across all nodes.', + internalFullMessage: + 'Elasticsearch version mismatch alert is firing for testCluster. Elasticsearch is running [8.0.0, 7.2.1]. [View nodes](http://localhost:5601/app/monitoring#elasticsearch/nodes?_g=(cluster_uuid:abc123))', + internalShortMessage: + 'Elasticsearch version mismatch alert is firing for testCluster. Verify you have the same version across all nodes.', + versionList: '[8.0.0, 7.2.1]', + clusterName, + state: 'firing', + }); + }); + + it('should not fire actions if there is no legacy alert', async () => { + (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + return []; + }); + const alert = new ElasticsearchVersionMismatchAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).not.toHaveBeenCalledWith({}); + expect(scheduleActions).not.toHaveBeenCalled(); + }); + + it('should resolve with a resolved message', async () => { + (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + return [ + { + ...legacyAlert, + resolved_timestamp: 1, + }, + ]; + }); + (getState as jest.Mock).mockImplementation(() => { + return { + alertStates: [ + { + cluster: { + clusterUuid, + clusterName, + }, + ccs: null, + ui: { + isFiring: true, + message: null, + severity: 'danger', + resolvedMS: 0, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }; + }); + const alert = new ElasticsearchVersionMismatchAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).toHaveBeenCalledWith({ + alertStates: [ + { + cluster: { clusterUuid, clusterName }, + ccs: null, + ui: { + isFiring: false, + message: { + text: 'All versions of Elasticsearch are the same in this cluster.', + }, + severity: 'danger', + resolvedMS: 1, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }); + expect(scheduleActions).toHaveBeenCalledWith('default', { + internalFullMessage: 'Elasticsearch version mismatch alert is resolved for testCluster.', + internalShortMessage: 'Elasticsearch version mismatch alert is resolved for testCluster.', + clusterName, + state: 'resolved', + }); + }); + }); +}); diff --git a/x-pack/plugins/monitoring/server/alerts/elasticsearch_version_mismatch_alert.ts b/x-pack/plugins/monitoring/server/alerts/elasticsearch_version_mismatch_alert.ts new file mode 100644 index 0000000000000..e3b952fbbe5d3 --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/elasticsearch_version_mismatch_alert.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 { IUiSettingsClient } from 'kibana/server'; +import { i18n } from '@kbn/i18n'; +import { BaseAlert } from './base_alert'; +import { + AlertData, + AlertCluster, + AlertState, + AlertMessage, + AlertInstanceState, + LegacyAlert, +} from './types'; +import { AlertInstance } from '../../../alerts/server'; +import { INDEX_ALERTS, ALERT_ELASTICSEARCH_VERSION_MISMATCH } from '../../common/constants'; +import { getCcsIndexPattern } from '../lib/alerts/get_ccs_index_pattern'; +import { AlertSeverity } from '../../common/enums'; +import { CommonAlertParams } from '../../common/types'; +import { fetchLegacyAlerts } from '../lib/alerts/fetch_legacy_alerts'; + +const WATCH_NAME = 'elasticsearch_version_mismatch'; +const RESOLVED = i18n.translate('xpack.monitoring.alerts.elasticsearchVersionMismatch.resolved', { + defaultMessage: 'resolved', +}); +const FIRING = i18n.translate('xpack.monitoring.alerts.elasticsearchVersionMismatch.firing', { + defaultMessage: 'firing', +}); + +export class ElasticsearchVersionMismatchAlert extends BaseAlert { + public type = ALERT_ELASTICSEARCH_VERSION_MISMATCH; + public label = i18n.translate('xpack.monitoring.alerts.elasticsearchVersionMismatch.label', { + defaultMessage: 'Elasticsearch version mismatch', + }); + public isLegacy = true; + + protected actionVariables = [ + { + name: 'internalShortMessage', + description: i18n.translate( + 'xpack.monitoring.alerts.elasticsearchVersionMismatch.actionVariables.internalShortMessage', + { + defaultMessage: 'The short internal message generated by Elastic.', + } + ), + }, + { + name: 'internalFullMessage', + description: i18n.translate( + 'xpack.monitoring.alerts.elasticsearchVersionMismatch.actionVariables.internalFullMessage', + { + defaultMessage: 'The full internal message generated by Elastic.', + } + ), + }, + { + name: 'state', + description: i18n.translate( + 'xpack.monitoring.alerts.elasticsearchVersionMismatch.actionVariables.state', + { + defaultMessage: 'The current state of the alert.', + } + ), + }, + { + name: 'versionList', + description: i18n.translate( + 'xpack.monitoring.alerts.elasticsearchVersionMismatch.actionVariables.clusterHealth', + { + defaultMessage: 'The versions of Elasticsearch running in this cluster.', + } + ), + }, + { + name: 'clusterName', + description: i18n.translate( + 'xpack.monitoring.alerts.elasticsearchVersionMismatch.actionVariables.clusterName', + { + defaultMessage: 'The cluster to which the nodes belong.', + } + ), + }, + { + name: 'action', + description: i18n.translate( + 'xpack.monitoring.alerts.elasticsearchVersionMismatch.actionVariables.action', + { + defaultMessage: 'The recommended action for this alert.', + } + ), + }, + { + name: 'actionPlain', + description: i18n.translate( + 'xpack.monitoring.alerts.elasticsearchVersionMismatch.actionVariables.actionPlain', + { + defaultMessage: 'The recommended action for this alert, without any markdown.', + } + ), + }, + ]; + + protected async fetchData( + params: CommonAlertParams, + callCluster: any, + clusters: AlertCluster[], + uiSettings: IUiSettingsClient, + availableCcs: string[] + ): Promise { + let alertIndexPattern = INDEX_ALERTS; + if (availableCcs) { + alertIndexPattern = getCcsIndexPattern(alertIndexPattern, availableCcs); + } + + const legacyAlerts = await fetchLegacyAlerts( + callCluster, + clusters, + alertIndexPattern, + WATCH_NAME, + this.config.ui.max_bucket_size + ); + + return legacyAlerts.reduce((accum: AlertData[], legacyAlert) => { + const severity = AlertSeverity.Warning; + + accum.push({ + instanceKey: `${legacyAlert.metadata.cluster_uuid}`, + clusterUuid: legacyAlert.metadata.cluster_uuid, + shouldFire: !legacyAlert.resolved_timestamp, + severity, + meta: legacyAlert, + ccs: null, + }); + return accum; + }, []); + } + + private getVersions(legacyAlert: LegacyAlert) { + const prefixStr = 'Versions: '; + return legacyAlert.message.slice( + legacyAlert.message.indexOf(prefixStr) + prefixStr.length, + legacyAlert.message.length - 1 + ); + } + + protected getUiMessage(alertState: AlertState, item: AlertData): AlertMessage { + const legacyAlert = item.meta as LegacyAlert; + const versions = this.getVersions(legacyAlert); + if (!alertState.ui.isFiring) { + return { + text: i18n.translate( + 'xpack.monitoring.alerts.elasticsearchVersionMismatch.ui.resolvedMessage', + { + defaultMessage: `All versions of Elasticsearch are the same in this cluster.`, + } + ), + }; + } + + const text = i18n.translate( + 'xpack.monitoring.alerts.elasticsearchVersionMismatch.ui.firingMessage', + { + defaultMessage: `Multiple versions of Elasticsearch ({versions}) running in this cluster.`, + values: { + versions, + }, + } + ); + + return { + text, + }; + } + + protected async executeActions( + instance: AlertInstance, + instanceState: AlertInstanceState, + item: AlertData, + cluster: AlertCluster + ) { + if (instanceState.alertStates.length === 0) { + return; + } + const alertState = instanceState.alertStates[0]; + const legacyAlert = item.meta as LegacyAlert; + const versions = this.getVersions(legacyAlert); + if (!alertState.ui.isFiring) { + instance.scheduleActions('default', { + internalShortMessage: i18n.translate( + 'xpack.monitoring.alerts.elasticsearchVersionMismatch.resolved.internalShortMessage', + { + defaultMessage: `Elasticsearch version mismatch alert is resolved for {clusterName}.`, + values: { + clusterName: cluster.clusterName, + }, + } + ), + internalFullMessage: i18n.translate( + 'xpack.monitoring.alerts.elasticsearchVersionMismatch.resolved.internalFullMessage', + { + defaultMessage: `Elasticsearch version mismatch alert is resolved for {clusterName}.`, + values: { + clusterName: cluster.clusterName, + }, + } + ), + state: RESOLVED, + clusterName: cluster.clusterName, + }); + } else { + const shortActionText = i18n.translate( + 'xpack.monitoring.alerts.elasticsearchVersionMismatch.shortAction', + { + defaultMessage: 'Verify you have the same version across all nodes.', + } + ); + const fullActionText = i18n.translate( + 'xpack.monitoring.alerts.elasticsearchVersionMismatch.fullAction', + { + defaultMessage: 'View nodes', + } + ); + const globalState = [`cluster_uuid:${cluster.clusterUuid}`]; + if (alertState.ccs) { + globalState.push(`ccs:${alertState.ccs}`); + } + const url = `${this.kibanaUrl}/app/monitoring#elasticsearch/nodes?_g=(${globalState.join( + ',' + )})`; + const action = `[${fullActionText}](${url})`; + instance.scheduleActions('default', { + internalShortMessage: i18n.translate( + 'xpack.monitoring.alerts.elasticsearchVersionMismatch.firing.internalShortMessage', + { + defaultMessage: `Elasticsearch version mismatch alert is firing for {clusterName}. {shortActionText}`, + values: { + clusterName: cluster.clusterName, + shortActionText, + }, + } + ), + internalFullMessage: i18n.translate( + 'xpack.monitoring.alerts.elasticsearchVersionMismatch.firing.internalFullMessage', + { + defaultMessage: `Elasticsearch version mismatch alert is firing for {clusterName}. Elasticsearch is running {versions}. {action}`, + values: { + clusterName: cluster.clusterName, + versions, + action, + }, + } + ), + state: FIRING, + clusterName: cluster.clusterName, + versionList: versions, + action, + actionPlain: shortActionText, + }); + } + } +} diff --git a/x-pack/plugins/monitoring/server/alerts/index.ts b/x-pack/plugins/monitoring/server/alerts/index.ts new file mode 100644 index 0000000000000..048e703d2222c --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/index.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. + */ + +export { BaseAlert } from './base_alert'; +export { CpuUsageAlert } from './cpu_usage_alert'; +export { ClusterHealthAlert } from './cluster_health_alert'; +export { LicenseExpirationAlert } from './license_expiration_alert'; +export { NodesChangedAlert } from './nodes_changed_alert'; +export { ElasticsearchVersionMismatchAlert } from './elasticsearch_version_mismatch_alert'; +export { KibanaVersionMismatchAlert } from './kibana_version_mismatch_alert'; +export { LogstashVersionMismatchAlert } from './logstash_version_mismatch_alert'; +export { AlertsFactory, BY_TYPE } from './alerts_factory'; diff --git a/x-pack/plugins/monitoring/server/alerts/kibana_version_mismatch_alert.test.ts b/x-pack/plugins/monitoring/server/alerts/kibana_version_mismatch_alert.test.ts new file mode 100644 index 0000000000000..6c56c7aa08d71 --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/kibana_version_mismatch_alert.test.ts @@ -0,0 +1,253 @@ +/* + * 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 { KibanaVersionMismatchAlert } from './kibana_version_mismatch_alert'; +import { ALERT_KIBANA_VERSION_MISMATCH } from '../../common/constants'; +import { fetchLegacyAlerts } from '../lib/alerts/fetch_legacy_alerts'; +import { fetchClusters } from '../lib/alerts/fetch_clusters'; + +const RealDate = Date; + +jest.mock('../lib/alerts/fetch_legacy_alerts', () => ({ + fetchLegacyAlerts: jest.fn(), +})); +jest.mock('../lib/alerts/fetch_clusters', () => ({ + fetchClusters: jest.fn(), +})); + +describe('KibanaVersionMismatchAlert', () => { + it('should have defaults', () => { + const alert = new KibanaVersionMismatchAlert(); + expect(alert.type).toBe(ALERT_KIBANA_VERSION_MISMATCH); + expect(alert.label).toBe('Kibana version mismatch'); + expect(alert.defaultThrottle).toBe('1m'); + // @ts-ignore + expect(alert.actionVariables).toStrictEqual([ + { + name: 'internalShortMessage', + description: 'The short internal message generated by Elastic.', + }, + { + name: 'internalFullMessage', + description: 'The full internal message generated by Elastic.', + }, + { name: 'state', description: 'The current state of the alert.' }, + { + name: 'versionList', + description: 'The versions of Kibana running in this cluster.', + }, + { + name: 'clusterName', + description: 'The cluster to which the instances belong.', + }, + { name: 'action', description: 'The recommended action for this alert.' }, + { + name: 'actionPlain', + description: 'The recommended action for this alert, without any markdown.', + }, + ]); + }); + + describe('execute', () => { + function FakeDate() {} + FakeDate.prototype.valueOf = () => 1; + + const clusterUuid = 'abc123'; + const clusterName = 'testCluster'; + const legacyAlert = { + prefix: 'This cluster is running with multiple versions of Kibana.', + message: 'Versions: [8.0.0, 7.2.1].', + metadata: { + severity: 1000, + cluster_uuid: clusterUuid, + }, + }; + const getUiSettingsService = () => ({ + asScopedToClient: jest.fn(), + }); + const getLogger = () => ({ + debug: jest.fn(), + }); + const monitoringCluster = null; + const config = { + ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } }, + }; + const kibanaUrl = 'http://localhost:5601'; + + const replaceState = jest.fn(); + const scheduleActions = jest.fn(); + const getState = jest.fn(); + const executorOptions = { + services: { + callCluster: jest.fn(), + alertInstanceFactory: jest.fn().mockImplementation(() => { + return { + replaceState, + scheduleActions, + getState, + }; + }), + }, + state: {}, + }; + + beforeEach(() => { + // @ts-ignore + Date = FakeDate; + (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + return [legacyAlert]; + }); + (fetchClusters as jest.Mock).mockImplementation(() => { + return [{ clusterUuid, clusterName }]; + }); + }); + + afterEach(() => { + Date = RealDate; + replaceState.mockReset(); + scheduleActions.mockReset(); + getState.mockReset(); + }); + + it('should fire actions', async () => { + const alert = new KibanaVersionMismatchAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).toHaveBeenCalledWith({ + alertStates: [ + { + cluster: { clusterUuid: 'abc123', clusterName: 'testCluster' }, + ccs: null, + ui: { + isFiring: true, + message: { + text: 'Multiple versions of Kibana ([8.0.0, 7.2.1]) running in this cluster.', + }, + severity: 'warning', + resolvedMS: 0, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }); + expect(scheduleActions).toHaveBeenCalledWith('default', { + action: + '[View instances](http://localhost:5601/app/monitoring#kibana/instances?_g=(cluster_uuid:abc123))', + actionPlain: 'Verify you have the same version across all instances.', + internalFullMessage: + 'Kibana version mismatch alert is firing for testCluster. Kibana is running [8.0.0, 7.2.1]. [View instances](http://localhost:5601/app/monitoring#kibana/instances?_g=(cluster_uuid:abc123))', + internalShortMessage: + 'Kibana version mismatch alert is firing for testCluster. Verify you have the same version across all instances.', + versionList: '[8.0.0, 7.2.1]', + clusterName, + state: 'firing', + }); + }); + + it('should not fire actions if there is no legacy alert', async () => { + (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + return []; + }); + const alert = new KibanaVersionMismatchAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).not.toHaveBeenCalledWith({}); + expect(scheduleActions).not.toHaveBeenCalled(); + }); + + it('should resolve with a resolved message', async () => { + (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + return [ + { + ...legacyAlert, + resolved_timestamp: 1, + }, + ]; + }); + (getState as jest.Mock).mockImplementation(() => { + return { + alertStates: [ + { + cluster: { + clusterUuid, + clusterName, + }, + ccs: null, + ui: { + isFiring: true, + message: null, + severity: 'danger', + resolvedMS: 0, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }; + }); + const alert = new KibanaVersionMismatchAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).toHaveBeenCalledWith({ + alertStates: [ + { + cluster: { clusterUuid, clusterName }, + ccs: null, + ui: { + isFiring: false, + message: { + text: 'All versions of Kibana are the same in this cluster.', + }, + severity: 'danger', + resolvedMS: 1, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }); + expect(scheduleActions).toHaveBeenCalledWith('default', { + internalFullMessage: 'Kibana version mismatch alert is resolved for testCluster.', + internalShortMessage: 'Kibana version mismatch alert is resolved for testCluster.', + clusterName, + state: 'resolved', + }); + }); + }); +}); diff --git a/x-pack/plugins/monitoring/server/alerts/kibana_version_mismatch_alert.ts b/x-pack/plugins/monitoring/server/alerts/kibana_version_mismatch_alert.ts new file mode 100644 index 0000000000000..80e8701933f56 --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/kibana_version_mismatch_alert.ts @@ -0,0 +1,253 @@ +/* + * 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 { IUiSettingsClient } from 'kibana/server'; +import { i18n } from '@kbn/i18n'; +import { BaseAlert } from './base_alert'; +import { + AlertData, + AlertCluster, + AlertState, + AlertMessage, + AlertInstanceState, + LegacyAlert, +} from './types'; +import { AlertInstance } from '../../../alerts/server'; +import { INDEX_ALERTS, ALERT_KIBANA_VERSION_MISMATCH } from '../../common/constants'; +import { getCcsIndexPattern } from '../lib/alerts/get_ccs_index_pattern'; +import { AlertSeverity } from '../../common/enums'; +import { CommonAlertParams } from '../../common/types'; +import { fetchLegacyAlerts } from '../lib/alerts/fetch_legacy_alerts'; + +const WATCH_NAME = 'kibana_version_mismatch'; +const RESOLVED = i18n.translate('xpack.monitoring.alerts.kibanaVersionMismatch.resolved', { + defaultMessage: 'resolved', +}); +const FIRING = i18n.translate('xpack.monitoring.alerts.kibanaVersionMismatch.firing', { + defaultMessage: 'firing', +}); + +export class KibanaVersionMismatchAlert extends BaseAlert { + public type = ALERT_KIBANA_VERSION_MISMATCH; + public label = i18n.translate('xpack.monitoring.alerts.kibanaVersionMismatch.label', { + defaultMessage: 'Kibana version mismatch', + }); + public isLegacy = true; + + protected actionVariables = [ + { + name: 'internalShortMessage', + description: i18n.translate( + 'xpack.monitoring.alerts.kibanaVersionMismatch.actionVariables.internalShortMessage', + { + defaultMessage: 'The short internal message generated by Elastic.', + } + ), + }, + { + name: 'internalFullMessage', + description: i18n.translate( + 'xpack.monitoring.alerts.kibanaVersionMismatch.actionVariables.internalFullMessage', + { + defaultMessage: 'The full internal message generated by Elastic.', + } + ), + }, + { + name: 'state', + description: i18n.translate( + 'xpack.monitoring.alerts.kibanaVersionMismatch.actionVariables.state', + { + defaultMessage: 'The current state of the alert.', + } + ), + }, + { + name: 'versionList', + description: i18n.translate( + 'xpack.monitoring.alerts.kibanaVersionMismatch.actionVariables.clusterHealth', + { + defaultMessage: 'The versions of Kibana running in this cluster.', + } + ), + }, + { + name: 'clusterName', + description: i18n.translate( + 'xpack.monitoring.alerts.kibanaVersionMismatch.actionVariables.clusterName', + { + defaultMessage: 'The cluster to which the instances belong.', + } + ), + }, + { + name: 'action', + description: i18n.translate( + 'xpack.monitoring.alerts.kibanaVersionMismatch.actionVariables.action', + { + defaultMessage: 'The recommended action for this alert.', + } + ), + }, + { + name: 'actionPlain', + description: i18n.translate( + 'xpack.monitoring.alerts.kibanaVersionMismatch.actionVariables.actionPlain', + { + defaultMessage: 'The recommended action for this alert, without any markdown.', + } + ), + }, + ]; + + protected async fetchData( + params: CommonAlertParams, + callCluster: any, + clusters: AlertCluster[], + uiSettings: IUiSettingsClient, + availableCcs: string[] + ): Promise { + let alertIndexPattern = INDEX_ALERTS; + if (availableCcs) { + alertIndexPattern = getCcsIndexPattern(alertIndexPattern, availableCcs); + } + const legacyAlerts = await fetchLegacyAlerts( + callCluster, + clusters, + alertIndexPattern, + WATCH_NAME, + this.config.ui.max_bucket_size + ); + + return legacyAlerts.reduce((accum: AlertData[], legacyAlert) => { + const severity = AlertSeverity.Warning; + accum.push({ + instanceKey: `${legacyAlert.metadata.cluster_uuid}`, + clusterUuid: legacyAlert.metadata.cluster_uuid, + shouldFire: !legacyAlert.resolved_timestamp, + severity, + meta: legacyAlert, + ccs: null, + }); + return accum; + }, []); + } + + private getVersions(legacyAlert: LegacyAlert) { + const prefixStr = 'Versions: '; + return legacyAlert.message.slice( + legacyAlert.message.indexOf(prefixStr) + prefixStr.length, + legacyAlert.message.length - 1 + ); + } + + protected getUiMessage(alertState: AlertState, item: AlertData): AlertMessage { + const legacyAlert = item.meta as LegacyAlert; + const versions = this.getVersions(legacyAlert); + if (!alertState.ui.isFiring) { + return { + text: i18n.translate('xpack.monitoring.alerts.kibanaVersionMismatch.ui.resolvedMessage', { + defaultMessage: `All versions of Kibana are the same in this cluster.`, + }), + }; + } + + const text = i18n.translate('xpack.monitoring.alerts.kibanaVersionMismatch.ui.firingMessage', { + defaultMessage: `Multiple versions of Kibana ({versions}) running in this cluster.`, + values: { + versions, + }, + }); + + return { + text, + }; + } + + protected async executeActions( + instance: AlertInstance, + instanceState: AlertInstanceState, + item: AlertData, + cluster: AlertCluster + ) { + if (instanceState.alertStates.length === 0) { + return; + } + const alertState = instanceState.alertStates[0]; + const legacyAlert = item.meta as LegacyAlert; + const versions = this.getVersions(legacyAlert); + if (!alertState.ui.isFiring) { + instance.scheduleActions('default', { + internalShortMessage: i18n.translate( + 'xpack.monitoring.alerts.kibanaVersionMismatch.resolved.internalShortMessage', + { + defaultMessage: `Kibana version mismatch alert is resolved for {clusterName}.`, + values: { + clusterName: cluster.clusterName, + }, + } + ), + internalFullMessage: i18n.translate( + 'xpack.monitoring.alerts.kibanaVersionMismatch.resolved.internalFullMessage', + { + defaultMessage: `Kibana version mismatch alert is resolved for {clusterName}.`, + values: { + clusterName: cluster.clusterName, + }, + } + ), + state: RESOLVED, + clusterName: cluster.clusterName, + }); + } else { + const shortActionText = i18n.translate( + 'xpack.monitoring.alerts.kibanaVersionMismatch.shortAction', + { + defaultMessage: 'Verify you have the same version across all instances.', + } + ); + const fullActionText = i18n.translate( + 'xpack.monitoring.alerts.kibanaVersionMismatch.fullAction', + { + defaultMessage: 'View instances', + } + ); + const globalState = [`cluster_uuid:${cluster.clusterUuid}`]; + if (alertState.ccs) { + globalState.push(`ccs:${alertState.ccs}`); + } + const url = `${this.kibanaUrl}/app/monitoring#kibana/instances?_g=(${globalState.join(',')})`; + const action = `[${fullActionText}](${url})`; + instance.scheduleActions('default', { + internalShortMessage: i18n.translate( + 'xpack.monitoring.alerts.kibanaVersionMismatch.firing.internalShortMessage', + { + defaultMessage: `Kibana version mismatch alert is firing for {clusterName}. {shortActionText}`, + values: { + clusterName: cluster.clusterName, + shortActionText, + }, + } + ), + internalFullMessage: i18n.translate( + 'xpack.monitoring.alerts.kibanaVersionMismatch.firing.internalFullMessage', + { + defaultMessage: `Kibana version mismatch alert is firing for {clusterName}. Kibana is running {versions}. {action}`, + values: { + clusterName: cluster.clusterName, + versions, + action, + }, + } + ), + state: FIRING, + clusterName: cluster.clusterName, + versionList: versions, + action, + actionPlain: shortActionText, + }); + } + } +} diff --git a/x-pack/plugins/monitoring/server/alerts/license_expiration.test.ts b/x-pack/plugins/monitoring/server/alerts/license_expiration.test.ts deleted file mode 100644 index fb8d10884fdc7..0000000000000 --- a/x-pack/plugins/monitoring/server/alerts/license_expiration.test.ts +++ /dev/null @@ -1,188 +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 moment from 'moment-timezone'; -import { getLicenseExpiration } from './license_expiration'; -import { ALERT_TYPE_LICENSE_EXPIRATION } from '../../common/constants'; -import { Logger } from 'src/core/server'; -import { - AlertCommonParams, - AlertCommonState, - AlertLicensePerClusterState, - AlertLicense, -} from './types'; -import { executeActions } from '../lib/alerts/license_expiration.lib'; -import { PreparedAlert, getPreparedAlert } from '../lib/alerts/get_prepared_alert'; -import { alertsMock, AlertServicesMock } from '../../../alerts/server/mocks'; - -jest.mock('../lib/alerts/license_expiration.lib', () => ({ - executeActions: jest.fn(), - getUiMessage: jest.fn(), -})); - -jest.mock('../lib/alerts/get_prepared_alert', () => ({ - getPreparedAlert: jest.fn(() => { - return { - emailAddress: 'foo@foo.com', - }; - }), -})); - -describe('getLicenseExpiration', () => { - const services: AlertServicesMock = alertsMock.createAlertServices(); - - const params: AlertCommonParams = { - dateFormat: 'YYYY', - timezone: 'UTC', - }; - - const emailAddress = 'foo@foo.com'; - const clusterUuid = 'kdksdfj434'; - const clusterName = 'monitoring_test'; - const dateFormat = 'YYYY-MM-DD'; - const cluster = { clusterUuid, clusterName }; - const defaultUiState = { - isFiring: false, - severity: 0, - message: null, - resolvedMS: 0, - lastCheckedMS: 0, - triggeredMS: 0, - }; - - async function setupAlert( - license: AlertLicense | null, - expiredCheckDateMS: number, - preparedAlertResponse: PreparedAlert | null | undefined = undefined - ): Promise { - const logger: Logger = { - warn: jest.fn(), - log: jest.fn(), - debug: jest.fn(), - trace: jest.fn(), - error: jest.fn(), - fatal: jest.fn(), - info: jest.fn(), - get: jest.fn(), - }; - const getLogger = (): Logger => logger; - const ccrEnabled = false; - (getPreparedAlert as jest.Mock).mockImplementation(() => { - if (preparedAlertResponse !== undefined) { - return preparedAlertResponse; - } - - return { - emailAddress, - data: [license], - clusters: [cluster], - dateFormat, - }; - }); - - const alert = getLicenseExpiration(null as any, null as any, getLogger, ccrEnabled); - const state: AlertCommonState = { - [clusterUuid]: { - expiredCheckDateMS, - ui: { ...defaultUiState }, - } as AlertLicensePerClusterState, - }; - - return (await alert.executor({ services, params, state } as any)) as AlertCommonState; - } - - afterEach(() => { - jest.clearAllMocks(); - (executeActions as jest.Mock).mockClear(); - (getPreparedAlert as jest.Mock).mockClear(); - }); - - it('should have the right id and actionGroups', () => { - const alert = getLicenseExpiration(null as any, null as any, jest.fn(), false); - expect(alert.id).toBe(ALERT_TYPE_LICENSE_EXPIRATION); - expect(alert.actionGroups).toEqual([{ id: 'default', name: 'Default' }]); - }); - - it('should return the state if no license is provided', async () => { - const result = await setupAlert(null, 0, null); - expect(result[clusterUuid].ui).toEqual(defaultUiState); - }); - - it('should fire actions if going to expire', async () => { - const expiryDateMS = moment().add(7, 'days').valueOf(); - const license = { - status: 'active', - type: 'gold', - expiryDateMS, - clusterUuid, - }; - const result = await setupAlert(license, 0); - const newState = result[clusterUuid] as AlertLicensePerClusterState; - expect(newState.expiredCheckDateMS > 0).toBe(true); - expect(executeActions).toHaveBeenCalledWith( - services.alertInstanceFactory(ALERT_TYPE_LICENSE_EXPIRATION), - cluster, - moment.utc(expiryDateMS), - dateFormat, - emailAddress - ); - }); - - it('should fire actions if the user fixed their license', async () => { - const expiryDateMS = moment().add(365, 'days').valueOf(); - const license = { - status: 'active', - type: 'gold', - expiryDateMS, - clusterUuid, - }; - const result = await setupAlert(license, 100); - const newState = result[clusterUuid] as AlertLicensePerClusterState; - expect(newState.expiredCheckDateMS).toBe(0); - expect(executeActions).toHaveBeenCalledWith( - services.alertInstanceFactory(ALERT_TYPE_LICENSE_EXPIRATION), - cluster, - moment.utc(expiryDateMS), - dateFormat, - emailAddress, - true - ); - }); - - it('should not fire actions for trial license that expire in more than 14 days', async () => { - const expiryDateMS = moment().add(20, 'days').valueOf(); - const license = { - status: 'active', - type: 'trial', - expiryDateMS, - clusterUuid, - }; - const result = await setupAlert(license, 0); - const newState = result[clusterUuid] as AlertLicensePerClusterState; - expect(newState.expiredCheckDateMS).toBe(0); - expect(executeActions).not.toHaveBeenCalled(); - }); - - it('should fire actions for trial license that in 14 days or less', async () => { - const expiryDateMS = moment().add(7, 'days').valueOf(); - const license = { - status: 'active', - type: 'trial', - expiryDateMS, - clusterUuid, - }; - const result = await setupAlert(license, 0); - const newState = result[clusterUuid] as AlertLicensePerClusterState; - expect(newState.expiredCheckDateMS > 0).toBe(true); - expect(executeActions).toHaveBeenCalledWith( - services.alertInstanceFactory(ALERT_TYPE_LICENSE_EXPIRATION), - cluster, - moment.utc(expiryDateMS), - dateFormat, - emailAddress - ); - }); -}); diff --git a/x-pack/plugins/monitoring/server/alerts/license_expiration.ts b/x-pack/plugins/monitoring/server/alerts/license_expiration.ts deleted file mode 100644 index 277e108e8f0c0..0000000000000 --- a/x-pack/plugins/monitoring/server/alerts/license_expiration.ts +++ /dev/null @@ -1,151 +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 moment from 'moment-timezone'; -import { Logger, ILegacyCustomClusterClient, UiSettingsServiceStart } from 'src/core/server'; -import { i18n } from '@kbn/i18n'; -import { ALERT_TYPE_LICENSE_EXPIRATION } from '../../common/constants'; -import { AlertType } from '../../../alerts/server'; -import { fetchLicenses } from '../lib/alerts/fetch_licenses'; -import { - AlertCommonState, - AlertLicensePerClusterState, - AlertCommonExecutorOptions, - AlertCommonCluster, - AlertLicensePerClusterUiState, -} from './types'; -import { executeActions, getUiMessage } from '../lib/alerts/license_expiration.lib'; -import { getPreparedAlert } from '../lib/alerts/get_prepared_alert'; - -const EXPIRES_DAYS = [60, 30, 14, 7]; - -export const getLicenseExpiration = ( - getUiSettingsService: () => Promise, - monitoringCluster: ILegacyCustomClusterClient, - getLogger: (...scopes: string[]) => Logger, - ccsEnabled: boolean -): AlertType => { - const logger = getLogger(ALERT_TYPE_LICENSE_EXPIRATION); - return { - id: ALERT_TYPE_LICENSE_EXPIRATION, - name: 'Monitoring Alert - License Expiration', - actionGroups: [ - { - id: 'default', - name: i18n.translate('xpack.monitoring.alerts.licenseExpiration.actionGroups.default', { - defaultMessage: 'Default', - }), - }, - ], - defaultActionGroupId: 'default', - producer: 'monitoring', - async executor({ services, params, state }: AlertCommonExecutorOptions): Promise { - logger.debug( - `Firing alert with params: ${JSON.stringify(params)} and state: ${JSON.stringify(state)}` - ); - - const preparedAlert = await getPreparedAlert( - ALERT_TYPE_LICENSE_EXPIRATION, - getUiSettingsService, - monitoringCluster, - logger, - ccsEnabled, - services, - fetchLicenses - ); - - if (!preparedAlert) { - return state; - } - - const { emailAddress, data: licenses, clusters, dateFormat } = preparedAlert; - - const result: AlertCommonState = { ...state }; - const defaultAlertState: AlertLicensePerClusterState = { - expiredCheckDateMS: 0, - ui: { - isFiring: false, - message: null, - severity: 0, - resolvedMS: 0, - lastCheckedMS: 0, - triggeredMS: 0, - }, - }; - - for (const license of licenses) { - const alertState: AlertLicensePerClusterState = - (state[license.clusterUuid] as AlertLicensePerClusterState) || defaultAlertState; - const cluster = clusters.find( - (c: AlertCommonCluster) => c.clusterUuid === license.clusterUuid - ); - if (!cluster) { - logger.warn(`Unable to find cluster for clusterUuid='${license.clusterUuid}'`); - continue; - } - const $expiry = moment.utc(license.expiryDateMS); - let isExpired = false; - let severity = 0; - - if (license.status !== 'active') { - isExpired = true; - severity = 2001; - } else if (license.expiryDateMS) { - for (let i = EXPIRES_DAYS.length - 1; i >= 0; i--) { - if (license.type === 'trial' && i < 2) { - break; - } - - const $fromNow = moment.utc().add(EXPIRES_DAYS[i], 'days'); - if ($fromNow.isAfter($expiry)) { - isExpired = true; - severity = 1000 * i; - break; - } - } - } - - const ui = alertState.ui; - let triggered = ui.triggeredMS; - let resolved = ui.resolvedMS; - let message = ui.message; - let expiredCheckDate = alertState.expiredCheckDateMS; - const instance = services.alertInstanceFactory(ALERT_TYPE_LICENSE_EXPIRATION); - - if (isExpired) { - if (!alertState.expiredCheckDateMS) { - logger.debug(`License will expire soon, sending email`); - executeActions(instance, cluster, $expiry, dateFormat, emailAddress); - expiredCheckDate = triggered = moment().valueOf(); - } - message = getUiMessage(); - resolved = 0; - } else if (!isExpired && alertState.expiredCheckDateMS) { - logger.debug(`License expiration has been resolved, sending email`); - executeActions(instance, cluster, $expiry, dateFormat, emailAddress, true); - expiredCheckDate = 0; - message = getUiMessage(true); - resolved = moment().valueOf(); - } - - result[license.clusterUuid] = { - expiredCheckDateMS: expiredCheckDate, - ui: { - message, - expirationTime: license.expiryDateMS, - isFiring: expiredCheckDate > 0, - severity, - resolvedMS: resolved, - triggeredMS: triggered, - lastCheckedMS: moment().valueOf(), - } as AlertLicensePerClusterUiState, - } as AlertLicensePerClusterState; - } - - return result; - }, - }; -}; diff --git a/x-pack/plugins/monitoring/server/alerts/license_expiration_alert.test.ts b/x-pack/plugins/monitoring/server/alerts/license_expiration_alert.test.ts new file mode 100644 index 0000000000000..09173df1d88b1 --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/license_expiration_alert.test.ts @@ -0,0 +1,281 @@ +/* + * 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 { LicenseExpirationAlert } from './license_expiration_alert'; +import { ALERT_LICENSE_EXPIRATION } from '../../common/constants'; +import { fetchLegacyAlerts } from '../lib/alerts/fetch_legacy_alerts'; +import { fetchClusters } from '../lib/alerts/fetch_clusters'; + +const RealDate = Date; + +jest.mock('../lib/alerts/fetch_legacy_alerts', () => ({ + fetchLegacyAlerts: jest.fn(), +})); +jest.mock('../lib/alerts/fetch_clusters', () => ({ + fetchClusters: jest.fn(), +})); +jest.mock('moment', () => { + return function () { + return { + format: () => 'THE_DATE', + }; + }; +}); + +describe('LicenseExpirationAlert', () => { + it('should have defaults', () => { + const alert = new LicenseExpirationAlert(); + expect(alert.type).toBe(ALERT_LICENSE_EXPIRATION); + expect(alert.label).toBe('License expiration'); + expect(alert.defaultThrottle).toBe('1m'); + // @ts-ignore + expect(alert.actionVariables).toStrictEqual([ + { + name: 'internalShortMessage', + description: 'The short internal message generated by Elastic.', + }, + { + name: 'internalFullMessage', + description: 'The full internal message generated by Elastic.', + }, + { name: 'state', description: 'The current state of the alert.' }, + { name: 'expiredDate', description: 'The date when the license expires.' }, + + { name: 'clusterName', description: 'The cluster to which the license belong.' }, + { name: 'action', description: 'The recommended action for this alert.' }, + { + name: 'actionPlain', + description: 'The recommended action for this alert, without any markdown.', + }, + ]); + }); + + describe('execute', () => { + function FakeDate() {} + FakeDate.prototype.valueOf = () => 1; + + const clusterUuid = 'abc123'; + const clusterName = 'testCluster'; + const legacyAlert = { + prefix: + 'The license for this cluster expires in {{#relativeTime}}metadata.time{{/relativeTime}} at {{#absoluteTime}}metadata.time{{/absoluteTime}}.', + message: 'Update your license.', + metadata: { + severity: 1000, + cluster_uuid: clusterUuid, + time: 1, + }, + }; + const getUiSettingsService = () => ({ + asScopedToClient: jest.fn(), + }); + const getLogger = () => ({ + debug: jest.fn(), + }); + const monitoringCluster = null; + const config = { + ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } }, + }; + const kibanaUrl = 'http://localhost:5601'; + + const replaceState = jest.fn(); + const scheduleActions = jest.fn(); + const getState = jest.fn(); + const executorOptions = { + services: { + callCluster: jest.fn(), + alertInstanceFactory: jest.fn().mockImplementation(() => { + return { + replaceState, + scheduleActions, + getState, + }; + }), + }, + state: {}, + }; + + beforeEach(() => { + // @ts-ignore + Date = FakeDate; + (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + return [legacyAlert]; + }); + (fetchClusters as jest.Mock).mockImplementation(() => { + return [{ clusterUuid, clusterName }]; + }); + }); + + afterEach(() => { + Date = RealDate; + replaceState.mockReset(); + scheduleActions.mockReset(); + getState.mockReset(); + }); + + it('should fire actions', async () => { + const alert = new LicenseExpirationAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).toHaveBeenCalledWith({ + alertStates: [ + { + cluster: { clusterUuid, clusterName }, + ccs: null, + ui: { + isFiring: true, + message: { + text: + 'The license for this cluster expires in #relative at #absolute. #start_linkPlease update your license.#end_link', + tokens: [ + { + startToken: '#relative', + type: 'time', + isRelative: true, + isAbsolute: false, + timestamp: 1, + }, + { + startToken: '#absolute', + type: 'time', + isAbsolute: true, + isRelative: false, + timestamp: 1, + }, + { + startToken: '#start_link', + endToken: '#end_link', + type: 'link', + url: 'license', + }, + ], + }, + severity: 'warning', + resolvedMS: 0, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }); + expect(scheduleActions).toHaveBeenCalledWith('default', { + action: + '[Please update your license.](http://localhost:5601/app/monitoring#elasticsearch/nodes?_g=(cluster_uuid:abc123))', + actionPlain: 'Please update your license.', + internalFullMessage: + 'License expiration alert is firing for testCluster. Your license expires in THE_DATE. [Please update your license.](http://localhost:5601/app/monitoring#elasticsearch/nodes?_g=(cluster_uuid:abc123))', + internalShortMessage: + 'License expiration alert is firing for testCluster. Your license expires in THE_DATE. Please update your license.', + clusterName, + expiredDate: 'THE_DATE', + state: 'firing', + }); + }); + + it('should not fire actions if there is no legacy alert', async () => { + (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + return []; + }); + const alert = new LicenseExpirationAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).not.toHaveBeenCalledWith({}); + expect(scheduleActions).not.toHaveBeenCalled(); + }); + + it('should resolve with a resolved message', async () => { + (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + return [ + { + ...legacyAlert, + resolved_timestamp: 1, + }, + ]; + }); + (getState as jest.Mock).mockImplementation(() => { + return { + alertStates: [ + { + cluster: { + clusterUuid, + clusterName, + }, + ccs: null, + ui: { + isFiring: true, + message: null, + severity: 'danger', + resolvedMS: 0, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }; + }); + const alert = new LicenseExpirationAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).toHaveBeenCalledWith({ + alertStates: [ + { + cluster: { clusterUuid, clusterName }, + ccs: null, + ui: { + isFiring: false, + message: { + text: 'The license for this cluster is active.', + }, + severity: 'danger', + resolvedMS: 1, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }); + expect(scheduleActions).toHaveBeenCalledWith('default', { + internalFullMessage: 'License expiration alert is resolved for testCluster.', + internalShortMessage: 'License expiration alert is resolved for testCluster.', + clusterName, + expiredDate: 'THE_DATE', + state: 'resolved', + }); + }); + }); +}); diff --git a/x-pack/plugins/monitoring/server/alerts/license_expiration_alert.ts b/x-pack/plugins/monitoring/server/alerts/license_expiration_alert.ts new file mode 100644 index 0000000000000..7a249db28d2db --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/license_expiration_alert.ts @@ -0,0 +1,262 @@ +/* + * 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 moment from 'moment'; +import { IUiSettingsClient } from 'kibana/server'; +import { i18n } from '@kbn/i18n'; +import { BaseAlert } from './base_alert'; +import { + AlertData, + AlertCluster, + AlertState, + AlertMessage, + AlertMessageTimeToken, + AlertMessageLinkToken, + AlertInstanceState, + LegacyAlert, +} from './types'; +import { AlertInstance } from '../../../alerts/server'; +import { + INDEX_ALERTS, + ALERT_LICENSE_EXPIRATION, + FORMAT_DURATION_TEMPLATE_SHORT, +} from '../../common/constants'; +import { getCcsIndexPattern } from '../lib/alerts/get_ccs_index_pattern'; +import { AlertMessageTokenType } from '../../common/enums'; +import { CommonAlertParams } from '../../common/types'; +import { fetchLegacyAlerts } from '../lib/alerts/fetch_legacy_alerts'; +import { mapLegacySeverity } from '../lib/alerts/map_legacy_severity'; + +const RESOLVED = i18n.translate('xpack.monitoring.alerts.licenseExpiration.resolved', { + defaultMessage: 'resolved', +}); +const FIRING = i18n.translate('xpack.monitoring.alerts.licenseExpiration.firing', { + defaultMessage: 'firing', +}); + +const WATCH_NAME = 'xpack_license_expiration'; + +export class LicenseExpirationAlert extends BaseAlert { + public type = ALERT_LICENSE_EXPIRATION; + public label = i18n.translate('xpack.monitoring.alerts.licenseExpiration.label', { + defaultMessage: 'License expiration', + }); + public isLegacy = true; + protected actionVariables = [ + { + name: 'internalShortMessage', + description: i18n.translate( + 'xpack.monitoring.alerts.licenseExpiration.actionVariables.internalShortMessage', + { + defaultMessage: 'The short internal message generated by Elastic.', + } + ), + }, + { + name: 'internalFullMessage', + description: i18n.translate( + 'xpack.monitoring.alerts.licenseExpiration.actionVariables.internalFullMessage', + { + defaultMessage: 'The full internal message generated by Elastic.', + } + ), + }, + { + name: 'state', + description: i18n.translate( + 'xpack.monitoring.alerts.licenseExpiration.actionVariables.state', + { + defaultMessage: 'The current state of the alert.', + } + ), + }, + { + name: 'expiredDate', + description: i18n.translate( + 'xpack.monitoring.alerts.licenseExpiration.actionVariables.expiredDate', + { + defaultMessage: 'The date when the license expires.', + } + ), + }, + { + name: 'clusterName', + description: i18n.translate( + 'xpack.monitoring.alerts.licenseExpiration.actionVariables.clusterName', + { + defaultMessage: 'The cluster to which the license belong.', + } + ), + }, + { + name: 'action', + description: i18n.translate( + 'xpack.monitoring.alerts.licenseExpiration.actionVariables.action', + { + defaultMessage: 'The recommended action for this alert.', + } + ), + }, + { + name: 'actionPlain', + description: i18n.translate( + 'xpack.monitoring.alerts.licenseExpiration.actionVariables.actionPlain', + { + defaultMessage: 'The recommended action for this alert, without any markdown.', + } + ), + }, + ]; + + protected async fetchData( + params: CommonAlertParams, + callCluster: any, + clusters: AlertCluster[], + uiSettings: IUiSettingsClient, + availableCcs: string[] + ): Promise { + let alertIndexPattern = INDEX_ALERTS; + if (availableCcs) { + alertIndexPattern = getCcsIndexPattern(alertIndexPattern, availableCcs); + } + const legacyAlerts = await fetchLegacyAlerts( + callCluster, + clusters, + alertIndexPattern, + WATCH_NAME, + this.config.ui.max_bucket_size + ); + return legacyAlerts.reduce((accum: AlertData[], legacyAlert) => { + accum.push({ + instanceKey: `${legacyAlert.metadata.cluster_uuid}`, + clusterUuid: legacyAlert.metadata.cluster_uuid, + shouldFire: !legacyAlert.resolved_timestamp, + severity: mapLegacySeverity(legacyAlert.metadata.severity), + meta: legacyAlert, + ccs: null, + }); + return accum; + }, []); + } + + protected getUiMessage(alertState: AlertState, item: AlertData): AlertMessage { + const legacyAlert = item.meta as LegacyAlert; + if (!alertState.ui.isFiring) { + return { + text: i18n.translate('xpack.monitoring.alerts.licenseExpiration.ui.resolvedMessage', { + defaultMessage: `The license for this cluster is active.`, + }), + }; + } + return { + text: i18n.translate('xpack.monitoring.alerts.licenseExpiration.ui.firingMessage', { + defaultMessage: `The license for this cluster expires in #relative at #absolute. #start_linkPlease update your license.#end_link`, + }), + tokens: [ + { + startToken: '#relative', + type: AlertMessageTokenType.Time, + isRelative: true, + isAbsolute: false, + timestamp: legacyAlert.metadata.time, + } as AlertMessageTimeToken, + { + startToken: '#absolute', + type: AlertMessageTokenType.Time, + isAbsolute: true, + isRelative: false, + timestamp: legacyAlert.metadata.time, + } as AlertMessageTimeToken, + { + startToken: '#start_link', + endToken: '#end_link', + type: AlertMessageTokenType.Link, + url: 'license', + } as AlertMessageLinkToken, + ], + }; + } + + protected async executeActions( + instance: AlertInstance, + instanceState: AlertInstanceState, + item: AlertData, + cluster: AlertCluster + ) { + if (instanceState.alertStates.length === 0) { + return; + } + const alertState = instanceState.alertStates[0]; + const legacyAlert = item.meta as LegacyAlert; + const $expiry = moment(legacyAlert.metadata.time); + if (!alertState.ui.isFiring) { + instance.scheduleActions('default', { + internalShortMessage: i18n.translate( + 'xpack.monitoring.alerts.licenseExpiration.resolved.internalShortMessage', + { + defaultMessage: `License expiration alert is resolved for {clusterName}.`, + values: { + clusterName: cluster.clusterName, + }, + } + ), + internalFullMessage: i18n.translate( + 'xpack.monitoring.alerts.licenseExpiration.resolved.internalFullMessage', + { + defaultMessage: `License expiration alert is resolved for {clusterName}.`, + values: { + clusterName: cluster.clusterName, + }, + } + ), + state: RESOLVED, + expiredDate: $expiry.format(FORMAT_DURATION_TEMPLATE_SHORT).trim(), + clusterName: cluster.clusterName, + }); + } else { + const actionText = i18n.translate('xpack.monitoring.alerts.licenseExpiration.action', { + defaultMessage: 'Please update your license.', + }); + const globalState = [`cluster_uuid:${cluster.clusterUuid}`]; + if (alertState.ccs) { + globalState.push(`ccs:${alertState.ccs}`); + } + const url = `${this.kibanaUrl}/app/monitoring#elasticsearch/nodes?_g=(${globalState.join( + ',' + )})`; + const action = `[${actionText}](${url})`; + const expiredDate = $expiry.format(FORMAT_DURATION_TEMPLATE_SHORT).trim(); + instance.scheduleActions('default', { + internalShortMessage: i18n.translate( + 'xpack.monitoring.alerts.licenseExpiration.firing.internalShortMessage', + { + defaultMessage: `License expiration alert is firing for {clusterName}. Your license expires in {expiredDate}. {actionText}`, + values: { + clusterName: cluster.clusterName, + expiredDate, + actionText, + }, + } + ), + internalFullMessage: i18n.translate( + 'xpack.monitoring.alerts.licenseExpiration.firing.internalFullMessage', + { + defaultMessage: `License expiration alert is firing for {clusterName}. Your license expires in {expiredDate}. {action}`, + values: { + clusterName: cluster.clusterName, + expiredDate, + action, + }, + } + ), + state: FIRING, + expiredDate, + clusterName: cluster.clusterName, + action, + actionPlain: actionText, + }); + } + } +} diff --git a/x-pack/plugins/monitoring/server/alerts/logstash_version_mismatch_alert.test.ts b/x-pack/plugins/monitoring/server/alerts/logstash_version_mismatch_alert.test.ts new file mode 100644 index 0000000000000..3f6d38809a949 --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/logstash_version_mismatch_alert.test.ts @@ -0,0 +1,250 @@ +/* + * 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 { LogstashVersionMismatchAlert } from './logstash_version_mismatch_alert'; +import { ALERT_LOGSTASH_VERSION_MISMATCH } from '../../common/constants'; +import { fetchLegacyAlerts } from '../lib/alerts/fetch_legacy_alerts'; +import { fetchClusters } from '../lib/alerts/fetch_clusters'; + +const RealDate = Date; + +jest.mock('../lib/alerts/fetch_legacy_alerts', () => ({ + fetchLegacyAlerts: jest.fn(), +})); +jest.mock('../lib/alerts/fetch_clusters', () => ({ + fetchClusters: jest.fn(), +})); + +describe('LogstashVersionMismatchAlert', () => { + it('should have defaults', () => { + const alert = new LogstashVersionMismatchAlert(); + expect(alert.type).toBe(ALERT_LOGSTASH_VERSION_MISMATCH); + expect(alert.label).toBe('Logstash version mismatch'); + expect(alert.defaultThrottle).toBe('1m'); + // @ts-ignore + expect(alert.actionVariables).toStrictEqual([ + { + name: 'internalShortMessage', + description: 'The short internal message generated by Elastic.', + }, + { + name: 'internalFullMessage', + description: 'The full internal message generated by Elastic.', + }, + { name: 'state', description: 'The current state of the alert.' }, + { + name: 'versionList', + description: 'The versions of Logstash running in this cluster.', + }, + { name: 'clusterName', description: 'The cluster to which the nodes belong.' }, + { name: 'action', description: 'The recommended action for this alert.' }, + { + name: 'actionPlain', + description: 'The recommended action for this alert, without any markdown.', + }, + ]); + }); + + describe('execute', () => { + function FakeDate() {} + FakeDate.prototype.valueOf = () => 1; + + const clusterUuid = 'abc123'; + const clusterName = 'testCluster'; + const legacyAlert = { + prefix: 'This cluster is running with multiple versions of Logstash.', + message: 'Versions: [8.0.0, 7.2.1].', + metadata: { + severity: 1000, + cluster_uuid: clusterUuid, + }, + }; + const getUiSettingsService = () => ({ + asScopedToClient: jest.fn(), + }); + const getLogger = () => ({ + debug: jest.fn(), + }); + const monitoringCluster = null; + const config = { + ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } }, + }; + const kibanaUrl = 'http://localhost:5601'; + + const replaceState = jest.fn(); + const scheduleActions = jest.fn(); + const getState = jest.fn(); + const executorOptions = { + services: { + callCluster: jest.fn(), + alertInstanceFactory: jest.fn().mockImplementation(() => { + return { + replaceState, + scheduleActions, + getState, + }; + }), + }, + state: {}, + }; + + beforeEach(() => { + // @ts-ignore + Date = FakeDate; + (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + return [legacyAlert]; + }); + (fetchClusters as jest.Mock).mockImplementation(() => { + return [{ clusterUuid, clusterName }]; + }); + }); + + afterEach(() => { + Date = RealDate; + replaceState.mockReset(); + scheduleActions.mockReset(); + getState.mockReset(); + }); + + it('should fire actions', async () => { + const alert = new LogstashVersionMismatchAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).toHaveBeenCalledWith({ + alertStates: [ + { + cluster: { clusterUuid: 'abc123', clusterName: 'testCluster' }, + ccs: null, + ui: { + isFiring: true, + message: { + text: 'Multiple versions of Logstash ([8.0.0, 7.2.1]) running in this cluster.', + }, + severity: 'warning', + resolvedMS: 0, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }); + expect(scheduleActions).toHaveBeenCalledWith('default', { + action: + '[View nodes](http://localhost:5601/app/monitoring#logstash/nodes?_g=(cluster_uuid:abc123))', + actionPlain: 'Verify you have the same version across all nodes.', + internalFullMessage: + 'Logstash version mismatch alert is firing for testCluster. Logstash is running [8.0.0, 7.2.1]. [View nodes](http://localhost:5601/app/monitoring#logstash/nodes?_g=(cluster_uuid:abc123))', + internalShortMessage: + 'Logstash version mismatch alert is firing for testCluster. Verify you have the same version across all nodes.', + versionList: '[8.0.0, 7.2.1]', + clusterName, + state: 'firing', + }); + }); + + it('should not fire actions if there is no legacy alert', async () => { + (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + return []; + }); + const alert = new LogstashVersionMismatchAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).not.toHaveBeenCalledWith({}); + expect(scheduleActions).not.toHaveBeenCalled(); + }); + + it('should resolve with a resolved message', async () => { + (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + return [ + { + ...legacyAlert, + resolved_timestamp: 1, + }, + ]; + }); + (getState as jest.Mock).mockImplementation(() => { + return { + alertStates: [ + { + cluster: { + clusterUuid, + clusterName, + }, + ccs: null, + ui: { + isFiring: true, + message: null, + severity: 'danger', + resolvedMS: 0, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }; + }); + const alert = new LogstashVersionMismatchAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).toHaveBeenCalledWith({ + alertStates: [ + { + cluster: { clusterUuid, clusterName }, + ccs: null, + ui: { + isFiring: false, + message: { + text: 'All versions of Logstash are the same in this cluster.', + }, + severity: 'danger', + resolvedMS: 1, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }); + expect(scheduleActions).toHaveBeenCalledWith('default', { + internalFullMessage: 'Logstash version mismatch alert is resolved for testCluster.', + internalShortMessage: 'Logstash version mismatch alert is resolved for testCluster.', + clusterName, + state: 'resolved', + }); + }); + }); +}); diff --git a/x-pack/plugins/monitoring/server/alerts/logstash_version_mismatch_alert.ts b/x-pack/plugins/monitoring/server/alerts/logstash_version_mismatch_alert.ts new file mode 100644 index 0000000000000..f996e54de28ef --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/logstash_version_mismatch_alert.ts @@ -0,0 +1,257 @@ +/* + * 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 { IUiSettingsClient } from 'kibana/server'; +import { i18n } from '@kbn/i18n'; +import { BaseAlert } from './base_alert'; +import { + AlertData, + AlertCluster, + AlertState, + AlertMessage, + AlertInstanceState, + LegacyAlert, +} from './types'; +import { AlertInstance } from '../../../alerts/server'; +import { INDEX_ALERTS, ALERT_LOGSTASH_VERSION_MISMATCH } from '../../common/constants'; +import { getCcsIndexPattern } from '../lib/alerts/get_ccs_index_pattern'; +import { AlertSeverity } from '../../common/enums'; +import { CommonAlertParams } from '../../common/types'; +import { fetchLegacyAlerts } from '../lib/alerts/fetch_legacy_alerts'; + +const WATCH_NAME = 'logstash_version_mismatch'; +const RESOLVED = i18n.translate('xpack.monitoring.alerts.logstashVersionMismatch.resolved', { + defaultMessage: 'resolved', +}); +const FIRING = i18n.translate('xpack.monitoring.alerts.logstashVersionMismatch.firing', { + defaultMessage: 'firing', +}); + +export class LogstashVersionMismatchAlert extends BaseAlert { + public type = ALERT_LOGSTASH_VERSION_MISMATCH; + public label = i18n.translate('xpack.monitoring.alerts.logstashVersionMismatch.label', { + defaultMessage: 'Logstash version mismatch', + }); + public isLegacy = true; + + protected actionVariables = [ + { + name: 'internalShortMessage', + description: i18n.translate( + 'xpack.monitoring.alerts.logstashVersionMismatch.actionVariables.internalShortMessage', + { + defaultMessage: 'The short internal message generated by Elastic.', + } + ), + }, + { + name: 'internalFullMessage', + description: i18n.translate( + 'xpack.monitoring.alerts.logstashVersionMismatch.actionVariables.internalFullMessage', + { + defaultMessage: 'The full internal message generated by Elastic.', + } + ), + }, + { + name: 'state', + description: i18n.translate( + 'xpack.monitoring.alerts.logstashVersionMismatch.actionVariables.state', + { + defaultMessage: 'The current state of the alert.', + } + ), + }, + { + name: 'versionList', + description: i18n.translate( + 'xpack.monitoring.alerts.logstashVersionMismatch.actionVariables.clusterHealth', + { + defaultMessage: 'The versions of Logstash running in this cluster.', + } + ), + }, + { + name: 'clusterName', + description: i18n.translate( + 'xpack.monitoring.alerts.logstashVersionMismatch.actionVariables.clusterName', + { + defaultMessage: 'The cluster to which the nodes belong.', + } + ), + }, + { + name: 'action', + description: i18n.translate( + 'xpack.monitoring.alerts.logstashVersionMismatch.actionVariables.action', + { + defaultMessage: 'The recommended action for this alert.', + } + ), + }, + { + name: 'actionPlain', + description: i18n.translate( + 'xpack.monitoring.alerts.logstashVersionMismatch.actionVariables.actionPlain', + { + defaultMessage: 'The recommended action for this alert, without any markdown.', + } + ), + }, + ]; + + protected async fetchData( + params: CommonAlertParams, + callCluster: any, + clusters: AlertCluster[], + uiSettings: IUiSettingsClient, + availableCcs: string[] + ): Promise { + let alertIndexPattern = INDEX_ALERTS; + if (availableCcs) { + alertIndexPattern = getCcsIndexPattern(alertIndexPattern, availableCcs); + } + const legacyAlerts = await fetchLegacyAlerts( + callCluster, + clusters, + alertIndexPattern, + WATCH_NAME, + this.config.ui.max_bucket_size + ); + + return legacyAlerts.reduce((accum: AlertData[], legacyAlert) => { + const severity = AlertSeverity.Warning; + + accum.push({ + instanceKey: `${legacyAlert.metadata.cluster_uuid}`, + clusterUuid: legacyAlert.metadata.cluster_uuid, + shouldFire: !legacyAlert.resolved_timestamp, + severity, + meta: legacyAlert, + ccs: null, + }); + return accum; + }, []); + } + + private getVersions(legacyAlert: LegacyAlert) { + const prefixStr = 'Versions: '; + return legacyAlert.message.slice( + legacyAlert.message.indexOf(prefixStr) + prefixStr.length, + legacyAlert.message.length - 1 + ); + } + + protected getUiMessage(alertState: AlertState, item: AlertData): AlertMessage { + const legacyAlert = item.meta as LegacyAlert; + const versions = this.getVersions(legacyAlert); + if (!alertState.ui.isFiring) { + return { + text: i18n.translate('xpack.monitoring.alerts.logstashVersionMismatch.ui.resolvedMessage', { + defaultMessage: `All versions of Logstash are the same in this cluster.`, + }), + }; + } + + const text = i18n.translate( + 'xpack.monitoring.alerts.logstashVersionMismatch.ui.firingMessage', + { + defaultMessage: `Multiple versions of Logstash ({versions}) running in this cluster.`, + values: { + versions, + }, + } + ); + + return { + text, + }; + } + + protected async executeActions( + instance: AlertInstance, + instanceState: AlertInstanceState, + item: AlertData, + cluster: AlertCluster + ) { + if (instanceState.alertStates.length === 0) { + return; + } + const alertState = instanceState.alertStates[0]; + const legacyAlert = item.meta as LegacyAlert; + const versions = this.getVersions(legacyAlert); + if (!alertState.ui.isFiring) { + instance.scheduleActions('default', { + internalShortMessage: i18n.translate( + 'xpack.monitoring.alerts.logstashVersionMismatch.resolved.internalShortMessage', + { + defaultMessage: `Logstash version mismatch alert is resolved for {clusterName}.`, + values: { + clusterName: cluster.clusterName, + }, + } + ), + internalFullMessage: i18n.translate( + 'xpack.monitoring.alerts.logstashVersionMismatch.resolved.internalFullMessage', + { + defaultMessage: `Logstash version mismatch alert is resolved for {clusterName}.`, + values: { + clusterName: cluster.clusterName, + }, + } + ), + state: RESOLVED, + clusterName: cluster.clusterName, + }); + } else { + const shortActionText = i18n.translate( + 'xpack.monitoring.alerts.logstashVersionMismatch.shortAction', + { + defaultMessage: 'Verify you have the same version across all nodes.', + } + ); + const fullActionText = i18n.translate( + 'xpack.monitoring.alerts.logstashVersionMismatch.fullAction', + { + defaultMessage: 'View nodes', + } + ); + const globalState = [`cluster_uuid:${cluster.clusterUuid}`]; + if (alertState.ccs) { + globalState.push(`ccs:${alertState.ccs}`); + } + const url = `${this.kibanaUrl}/app/monitoring#logstash/nodes?_g=(${globalState.join(',')})`; + const action = `[${fullActionText}](${url})`; + instance.scheduleActions('default', { + internalShortMessage: i18n.translate( + 'xpack.monitoring.alerts.logstashVersionMismatch.firing.internalShortMessage', + { + defaultMessage: `Logstash version mismatch alert is firing for {clusterName}. {shortActionText}`, + values: { + clusterName: cluster.clusterName, + shortActionText, + }, + } + ), + internalFullMessage: i18n.translate( + 'xpack.monitoring.alerts.logstashVersionMismatch.firing.internalFullMessage', + { + defaultMessage: `Logstash version mismatch alert is firing for {clusterName}. Logstash is running {versions}. {action}`, + values: { + clusterName: cluster.clusterName, + versions, + action, + }, + } + ), + state: FIRING, + clusterName: cluster.clusterName, + versionList: versions, + action, + actionPlain: shortActionText, + }); + } + } +} diff --git a/x-pack/plugins/monitoring/server/alerts/nodes_changed_alert.test.ts b/x-pack/plugins/monitoring/server/alerts/nodes_changed_alert.test.ts new file mode 100644 index 0000000000000..13c3dbbbe6e8a --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/nodes_changed_alert.test.ts @@ -0,0 +1,261 @@ +/* + * 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 { NodesChangedAlert } from './nodes_changed_alert'; +import { ALERT_NODES_CHANGED } from '../../common/constants'; +import { fetchLegacyAlerts } from '../lib/alerts/fetch_legacy_alerts'; +import { fetchClusters } from '../lib/alerts/fetch_clusters'; + +const RealDate = Date; + +jest.mock('../lib/alerts/fetch_legacy_alerts', () => ({ + fetchLegacyAlerts: jest.fn(), +})); +jest.mock('../lib/alerts/fetch_clusters', () => ({ + fetchClusters: jest.fn(), +})); +jest.mock('moment', () => { + return function () { + return { + format: () => 'THE_DATE', + }; + }; +}); + +describe('NodesChangedAlert', () => { + it('should have defaults', () => { + const alert = new NodesChangedAlert(); + expect(alert.type).toBe(ALERT_NODES_CHANGED); + expect(alert.label).toBe('Nodes changed'); + expect(alert.defaultThrottle).toBe('1m'); + // @ts-ignore + expect(alert.actionVariables).toStrictEqual([ + { + name: 'internalShortMessage', + description: 'The short internal message generated by Elastic.', + }, + { + name: 'internalFullMessage', + description: 'The full internal message generated by Elastic.', + }, + { name: 'state', description: 'The current state of the alert.' }, + { name: 'clusterName', description: 'The cluster to which the nodes belong.' }, + { name: 'added', description: 'The list of nodes added to the cluster.' }, + { name: 'removed', description: 'The list of nodes removed from the cluster.' }, + { name: 'restarted', description: 'The list of nodes restarted in the cluster.' }, + { name: 'action', description: 'The recommended action for this alert.' }, + { + name: 'actionPlain', + description: 'The recommended action for this alert, without any markdown.', + }, + ]); + }); + + describe('execute', () => { + function FakeDate() {} + FakeDate.prototype.valueOf = () => 1; + + const clusterUuid = 'abc123'; + const clusterName = 'testCluster'; + const legacyAlert = { + prefix: 'Elasticsearch cluster nodes have changed!', + message: 'Node was restarted [1]: [test].', + metadata: { + severity: 1000, + cluster_uuid: clusterUuid, + }, + nodes: { + added: {}, + removed: {}, + restarted: { + test: 'test', + }, + }, + }; + const getUiSettingsService = () => ({ + asScopedToClient: jest.fn(), + }); + const getLogger = () => ({ + debug: jest.fn(), + }); + const monitoringCluster = null; + const config = { + ui: { ccs: { enabled: true }, container: { elasticsearch: { enabled: false } } }, + }; + const kibanaUrl = 'http://localhost:5601'; + + const replaceState = jest.fn(); + const scheduleActions = jest.fn(); + const getState = jest.fn(); + const executorOptions = { + services: { + callCluster: jest.fn(), + alertInstanceFactory: jest.fn().mockImplementation(() => { + return { + replaceState, + scheduleActions, + getState, + }; + }), + }, + state: {}, + }; + + beforeEach(() => { + // @ts-ignore + Date = FakeDate; + (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + return [legacyAlert]; + }); + (fetchClusters as jest.Mock).mockImplementation(() => { + return [{ clusterUuid, clusterName }]; + }); + }); + + afterEach(() => { + Date = RealDate; + replaceState.mockReset(); + scheduleActions.mockReset(); + getState.mockReset(); + }); + + it('should fire actions', async () => { + const alert = new NodesChangedAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).toHaveBeenCalledWith({ + alertStates: [ + { + cluster: { clusterUuid, clusterName }, + ccs: null, + ui: { + isFiring: true, + message: { + text: "Elasticsearch nodes 'test' restarted in this cluster.", + }, + severity: 'warning', + resolvedMS: 0, + triggeredMS: 1, + lastCheckedMS: 0, + }, + }, + ], + }); + expect(scheduleActions).toHaveBeenCalledWith('default', { + action: + '[View nodes](http://localhost:5601/app/monitoring#elasticsearch/nodes?_g=(cluster_uuid:abc123))', + actionPlain: 'Verify that you added, removed, or restarted nodes.', + internalFullMessage: + 'Nodes changed alert is firing for testCluster. The following Elasticsearch nodes have been added: removed: restarted:test. [View nodes](http://localhost:5601/app/monitoring#elasticsearch/nodes?_g=(cluster_uuid:abc123))', + internalShortMessage: + 'Nodes changed alert is firing for testCluster. Verify that you added, removed, or restarted nodes.', + added: '', + removed: '', + restarted: 'test', + clusterName, + state: 'firing', + }); + }); + + it('should not fire actions if there is no legacy alert', async () => { + (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + return []; + }); + const alert = new NodesChangedAlert(); + alert.initializeAlertType( + getUiSettingsService as any, + monitoringCluster as any, + getLogger as any, + config as any, + kibanaUrl + ); + const type = alert.getAlertType(); + await type.executor({ + ...executorOptions, + // @ts-ignore + params: alert.defaultParams, + } as any); + expect(replaceState).not.toHaveBeenCalledWith({}); + expect(scheduleActions).not.toHaveBeenCalled(); + }); + + // This doesn't work because this watch is weird where it sets the resolved timestamp right away + // It is not really worth fixing as this watch will go away in 8.0 + // it('should resolve with a resolved message', async () => { + // (fetchLegacyAlerts as jest.Mock).mockImplementation(() => { + // return []; + // }); + // (getState as jest.Mock).mockImplementation(() => { + // return { + // alertStates: [ + // { + // cluster: { + // clusterUuid, + // clusterName, + // }, + // ccs: null, + // ui: { + // isFiring: true, + // message: null, + // severity: 'danger', + // resolvedMS: 0, + // triggeredMS: 1, + // lastCheckedMS: 0, + // }, + // }, + // ], + // }; + // }); + // const alert = new NodesChangedAlert(); + // alert.initializeAlertType( + // getUiSettingsService as any, + // monitoringCluster as any, + // getLogger as any, + // config as any, + // kibanaUrl + // ); + // const type = alert.getAlertType(); + // await type.executor({ + // ...executorOptions, + // // @ts-ignore + // params: alert.defaultParams, + // } as any); + // expect(replaceState).toHaveBeenCalledWith({ + // alertStates: [ + // { + // cluster: { clusterUuid, clusterName }, + // ccs: null, + // ui: { + // isFiring: false, + // message: { + // text: "The license for this cluster is active.", + // }, + // severity: 'danger', + // resolvedMS: 1, + // triggeredMS: 1, + // lastCheckedMS: 0, + // }, + // }, + // ], + // }); + // expect(scheduleActions).toHaveBeenCalledWith('default', { + // clusterName, + // expiredDate: 'THE_DATE', + // state: 'resolved', + // }); + // }); + }); +}); diff --git a/x-pack/plugins/monitoring/server/alerts/nodes_changed_alert.ts b/x-pack/plugins/monitoring/server/alerts/nodes_changed_alert.ts new file mode 100644 index 0000000000000..5b38503c7ece4 --- /dev/null +++ b/x-pack/plugins/monitoring/server/alerts/nodes_changed_alert.ts @@ -0,0 +1,278 @@ +/* + * 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 { IUiSettingsClient } from 'kibana/server'; +import { i18n } from '@kbn/i18n'; +import { BaseAlert } from './base_alert'; +import { + AlertData, + AlertCluster, + AlertState, + AlertMessage, + AlertInstanceState, + LegacyAlert, + LegacyAlertNodesChangedList, +} from './types'; +import { AlertInstance } from '../../../alerts/server'; +import { INDEX_ALERTS, ALERT_NODES_CHANGED } from '../../common/constants'; +import { getCcsIndexPattern } from '../lib/alerts/get_ccs_index_pattern'; +import { CommonAlertParams } from '../../common/types'; +import { fetchLegacyAlerts } from '../lib/alerts/fetch_legacy_alerts'; +import { mapLegacySeverity } from '../lib/alerts/map_legacy_severity'; + +const WATCH_NAME = 'elasticsearch_nodes'; +const RESOLVED = i18n.translate('xpack.monitoring.alerts.nodesChanged.resolved', { + defaultMessage: 'resolved', +}); +const FIRING = i18n.translate('xpack.monitoring.alerts.nodesChanged.firing', { + defaultMessage: 'firing', +}); + +export class NodesChangedAlert extends BaseAlert { + public type = ALERT_NODES_CHANGED; + public label = i18n.translate('xpack.monitoring.alerts.nodesChanged.label', { + defaultMessage: 'Nodes changed', + }); + public isLegacy = true; + + protected actionVariables = [ + { + name: 'internalShortMessage', + description: i18n.translate( + 'xpack.monitoring.alerts.nodesChanged.actionVariables.internalShortMessage', + { + defaultMessage: 'The short internal message generated by Elastic.', + } + ), + }, + { + name: 'internalFullMessage', + description: i18n.translate( + 'xpack.monitoring.alerts.nodesChanged.actionVariables.internalFullMessage', + { + defaultMessage: 'The full internal message generated by Elastic.', + } + ), + }, + { + name: 'state', + description: i18n.translate('xpack.monitoring.alerts.nodesChanged.actionVariables.state', { + defaultMessage: 'The current state of the alert.', + }), + }, + { + name: 'clusterName', + description: i18n.translate( + 'xpack.monitoring.alerts.nodesChanged.actionVariables.clusterName', + { + defaultMessage: 'The cluster to which the nodes belong.', + } + ), + }, + { + name: 'added', + description: i18n.translate('xpack.monitoring.alerts.nodesChanged.actionVariables.added', { + defaultMessage: 'The list of nodes added to the cluster.', + }), + }, + { + name: 'removed', + description: i18n.translate('xpack.monitoring.alerts.nodesChanged.actionVariables.removed', { + defaultMessage: 'The list of nodes removed from the cluster.', + }), + }, + { + name: 'restarted', + description: i18n.translate( + 'xpack.monitoring.alerts.nodesChanged.actionVariables.restarted', + { + defaultMessage: 'The list of nodes restarted in the cluster.', + } + ), + }, + { + name: 'action', + description: i18n.translate('xpack.monitoring.alerts.nodesChanged.actionVariables.action', { + defaultMessage: 'The recommended action for this alert.', + }), + }, + { + name: 'actionPlain', + description: i18n.translate( + 'xpack.monitoring.alerts.nodesChanged.actionVariables.actionPlain', + { + defaultMessage: 'The recommended action for this alert, without any markdown.', + } + ), + }, + ]; + + private getNodeStates(legacyAlert: LegacyAlert): LegacyAlertNodesChangedList | undefined { + return legacyAlert.nodes; + } + + protected async fetchData( + params: CommonAlertParams, + callCluster: any, + clusters: AlertCluster[], + uiSettings: IUiSettingsClient, + availableCcs: string[] + ): Promise { + let alertIndexPattern = INDEX_ALERTS; + if (availableCcs) { + alertIndexPattern = getCcsIndexPattern(alertIndexPattern, availableCcs); + } + const legacyAlerts = await fetchLegacyAlerts( + callCluster, + clusters, + alertIndexPattern, + WATCH_NAME, + this.config.ui.max_bucket_size + ); + return legacyAlerts.reduce((accum: AlertData[], legacyAlert) => { + accum.push({ + instanceKey: `${legacyAlert.metadata.cluster_uuid}`, + clusterUuid: legacyAlert.metadata.cluster_uuid, + shouldFire: true, // This alert always has a resolved timestamp + severity: mapLegacySeverity(legacyAlert.metadata.severity), + meta: legacyAlert, + ccs: null, + }); + return accum; + }, []); + } + + protected getUiMessage(alertState: AlertState, item: AlertData): AlertMessage { + const legacyAlert = item.meta as LegacyAlert; + const states = this.getNodeStates(legacyAlert) || { added: {}, removed: {}, restarted: {} }; + if (!alertState.ui.isFiring) { + return { + text: i18n.translate('xpack.monitoring.alerts.nodesChanged.ui.resolvedMessage', { + defaultMessage: `No changes in Elasticsearch nodes for this cluster.`, + }), + }; + } + + const addedText = + Object.values(states.added).length > 0 + ? i18n.translate('xpack.monitoring.alerts.nodesChanged.ui.addedFiringMessage', { + defaultMessage: `Elasticsearch nodes '{added}' added to this cluster.`, + values: { + added: Object.values(states.added).join(','), + }, + }) + : null; + const removedText = + Object.values(states.removed).length > 0 + ? i18n.translate('xpack.monitoring.alerts.nodesChanged.ui.removedFiringMessage', { + defaultMessage: `Elasticsearch nodes '{removed}' removed from this cluster.`, + values: { + removed: Object.values(states.removed).join(','), + }, + }) + : null; + const restartedText = + Object.values(states.restarted).length > 0 + ? i18n.translate('xpack.monitoring.alerts.nodesChanged.ui.restartedFiringMessage', { + defaultMessage: `Elasticsearch nodes '{restarted}' restarted in this cluster.`, + values: { + restarted: Object.values(states.restarted).join(','), + }, + }) + : null; + + return { + text: [addedText, removedText, restartedText].filter(Boolean).join(' '), + }; + } + + protected async executeActions( + instance: AlertInstance, + instanceState: AlertInstanceState, + item: AlertData, + cluster: AlertCluster + ) { + if (instanceState.alertStates.length === 0) { + return; + } + const alertState = instanceState.alertStates[0]; + const legacyAlert = item.meta as LegacyAlert; + if (!alertState.ui.isFiring) { + instance.scheduleActions('default', { + internalShortMessage: i18n.translate( + 'xpack.monitoring.alerts.nodesChanged.resolved.internalShortMessage', + { + defaultMessage: `Elasticsearch nodes changed alert is resolved for {clusterName}.`, + values: { + clusterName: cluster.clusterName, + }, + } + ), + internalFullMessage: i18n.translate( + 'xpack.monitoring.alerts.nodesChanged.resolved.internalFullMessage', + { + defaultMessage: `Elasticsearch nodes changed alert is resolved for {clusterName}.`, + values: { + clusterName: cluster.clusterName, + }, + } + ), + state: RESOLVED, + clusterName: cluster.clusterName, + }); + } else { + const shortActionText = i18n.translate('xpack.monitoring.alerts.nodesChanged.shortAction', { + defaultMessage: 'Verify that you added, removed, or restarted nodes.', + }); + const fullActionText = i18n.translate('xpack.monitoring.alerts.nodesChanged.fullAction', { + defaultMessage: 'View nodes', + }); + const globalState = [`cluster_uuid:${cluster.clusterUuid}`]; + if (alertState.ccs) { + globalState.push(`ccs:${alertState.ccs}`); + } + const url = `${this.kibanaUrl}/app/monitoring#elasticsearch/nodes?_g=(${globalState.join( + ',' + )})`; + const action = `[${fullActionText}](${url})`; + const states = this.getNodeStates(legacyAlert) || { added: {}, removed: {}, restarted: {} }; + const added = Object.values(states.added).join(','); + const removed = Object.values(states.removed).join(','); + const restarted = Object.values(states.restarted).join(','); + instance.scheduleActions('default', { + internalShortMessage: i18n.translate( + 'xpack.monitoring.alerts.nodesChanged.firing.internalShortMessage', + { + defaultMessage: `Nodes changed alert is firing for {clusterName}. {shortActionText}`, + values: { + clusterName: cluster.clusterName, + shortActionText, + }, + } + ), + internalFullMessage: i18n.translate( + 'xpack.monitoring.alerts.nodesChanged.firing.internalFullMessage', + { + defaultMessage: `Nodes changed alert is firing for {clusterName}. The following Elasticsearch nodes have been added:{added} removed:{removed} restarted:{restarted}. {action}`, + values: { + clusterName: cluster.clusterName, + added, + removed, + restarted, + action, + }, + } + ), + state: FIRING, + clusterName: cluster.clusterName, + added, + removed, + restarted, + action, + actionPlain: shortActionText, + }); + } + } +} diff --git a/x-pack/plugins/monitoring/server/alerts/types.d.ts b/x-pack/plugins/monitoring/server/alerts/types.d.ts index 67c74635b4e36..06988002a2034 100644 --- a/x-pack/plugins/monitoring/server/alerts/types.d.ts +++ b/x-pack/plugins/monitoring/server/alerts/types.d.ts @@ -3,81 +3,106 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { Moment } from 'moment'; -import { AlertExecutorOptions } from '../../../alerts/server'; -import { AlertClusterStateState, AlertCommonPerClusterMessageTokenType } from './enums'; - -export interface AlertLicense { - status: string; - type: string; - expiryDateMS: number; - clusterUuid: string; -} - -export interface AlertClusterState { - state: AlertClusterStateState; - clusterUuid: string; -} +import { AlertMessageTokenType, AlertSeverity } from '../../common/enums'; -export interface AlertCommonState { - [clusterUuid: string]: AlertCommonPerClusterState; +export interface AlertEnableAction { + id: string; + config: { [key: string]: any }; } -export interface AlertCommonPerClusterState { - ui: AlertCommonPerClusterUiState; +export interface AlertInstanceState { + alertStates: AlertState[]; } -export interface AlertClusterStatePerClusterState extends AlertCommonPerClusterState { - state: AlertClusterStateState; +export interface AlertState { + cluster: AlertCluster; + ccs: string | null; + ui: AlertUiState; } -export interface AlertLicensePerClusterState extends AlertCommonPerClusterState { - expiredCheckDateMS: number; +export interface AlertCpuUsageState extends AlertState { + cpuUsage: number; + nodeId: string; + nodeName: string; } -export interface AlertCommonPerClusterUiState { +export interface AlertUiState { isFiring: boolean; - severity: number; - message: AlertCommonPerClusterMessage | null; + severity: AlertSeverity; + message: AlertMessage | null; resolvedMS: number; lastCheckedMS: number; triggeredMS: number; } -export interface AlertCommonPerClusterMessage { +export interface AlertMessage { text: string; // Do this. #link this is a link #link - tokens?: AlertCommonPerClusterMessageToken[]; + nextSteps?: AlertMessage[]; + tokens?: AlertMessageToken[]; } -export interface AlertCommonPerClusterMessageToken { +export interface AlertMessageToken { startToken: string; endToken?: string; - type: AlertCommonPerClusterMessageTokenType; + type: AlertMessageTokenType; } -export interface AlertCommonPerClusterMessageLinkToken extends AlertCommonPerClusterMessageToken { +export interface AlertMessageLinkToken extends AlertMessageToken { url?: string; } -export interface AlertCommonPerClusterMessageTimeToken extends AlertCommonPerClusterMessageToken { +export interface AlertMessageTimeToken extends AlertMessageToken { isRelative: boolean; isAbsolute: boolean; + timestamp: string | number; } -export interface AlertLicensePerClusterUiState extends AlertCommonPerClusterUiState { - expirationTime: number; +export interface AlertMessageDocLinkToken extends AlertMessageToken { + partialUrl: string; } -export interface AlertCommonCluster { +export interface AlertCluster { clusterUuid: string; clusterName: string; } -export interface AlertCommonExecutorOptions extends AlertExecutorOptions { - state: AlertCommonState; +export interface AlertCpuUsageNodeStats { + clusterUuid: string; + nodeId: string; + nodeName: string; + cpuUsage: number; + containerUsage: number; + containerPeriods: number; + containerQuota: number; + ccs: string | null; +} + +export interface AlertData { + instanceKey: string; + clusterUuid: string; + ccs: string | null; + shouldFire: boolean; + severity: AlertSeverity; + meta: any; +} + +export interface LegacyAlert { + prefix: string; + message: string; + resolved_timestamp: string; + metadata: LegacyAlertMetadata; + nodes?: LegacyAlertNodesChangedList; +} + +export interface LegacyAlertMetadata { + severity: number; + cluster_uuid: string; + time: string; + link: string; } -export interface AlertCommonParams { - dateFormat: string; - timezone: string; +export interface LegacyAlertNodesChangedList { + removed: { [nodeName: string]: string }; + added: { [nodeName: string]: string }; + restarted: { [nodeName: string]: string }; } diff --git a/x-pack/plugins/monitoring/server/lib/alerts/cluster_state.lib.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/cluster_state.lib.test.ts deleted file mode 100644 index 81e375734cc50..0000000000000 --- a/x-pack/plugins/monitoring/server/lib/alerts/cluster_state.lib.test.ts +++ /dev/null @@ -1,70 +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 { executeActions, getUiMessage } from './cluster_state.lib'; -import { AlertClusterStateState } from '../../alerts/enums'; -import { AlertCommonPerClusterMessageLinkToken } from '../../alerts/types'; - -describe('clusterState lib', () => { - describe('executeActions', () => { - const clusterName = 'clusterA'; - const instance: any = { scheduleActions: jest.fn() }; - const license: any = { clusterName }; - const status = AlertClusterStateState.Green; - const emailAddress = 'test@test.com'; - - beforeEach(() => { - instance.scheduleActions.mockClear(); - }); - - it('should schedule actions when firing', () => { - executeActions(instance, license, status, emailAddress, false); - expect(instance.scheduleActions).toHaveBeenCalledWith('default', { - subject: 'NEW X-Pack Monitoring: Cluster Status', - message: `Allocate missing replica shards for cluster '${clusterName}'`, - to: emailAddress, - }); - }); - - it('should have a different message for red state', () => { - executeActions(instance, license, AlertClusterStateState.Red, emailAddress, false); - expect(instance.scheduleActions).toHaveBeenCalledWith('default', { - subject: 'NEW X-Pack Monitoring: Cluster Status', - message: `Allocate missing primary and replica shards for cluster '${clusterName}'`, - to: emailAddress, - }); - }); - - it('should schedule actions when resolved', () => { - executeActions(instance, license, status, emailAddress, true); - expect(instance.scheduleActions).toHaveBeenCalledWith('default', { - subject: 'RESOLVED X-Pack Monitoring: Cluster Status', - message: `This cluster alert has been resolved: Allocate missing replica shards for cluster '${clusterName}'`, - to: emailAddress, - }); - }); - }); - - describe('getUiMessage', () => { - it('should return a message when firing', () => { - const message = getUiMessage(AlertClusterStateState.Red, false); - expect(message.text).toBe( - `Elasticsearch cluster status is red. #start_linkAllocate missing primary and replica shards#end_link` - ); - expect(message.tokens && message.tokens.length).toBe(1); - expect(message.tokens && message.tokens[0].startToken).toBe('#start_link'); - expect(message.tokens && message.tokens[0].endToken).toBe('#end_link'); - expect( - message.tokens && (message.tokens[0] as AlertCommonPerClusterMessageLinkToken).url - ).toBe('elasticsearch/indices'); - }); - - it('should return a message when resolved', () => { - const message = getUiMessage(AlertClusterStateState.Green, true); - expect(message.text).toBe(`Elasticsearch cluster status is green.`); - expect(message.tokens).not.toBeDefined(); - }); - }); -}); diff --git a/x-pack/plugins/monitoring/server/lib/alerts/cluster_state.lib.ts b/x-pack/plugins/monitoring/server/lib/alerts/cluster_state.lib.ts deleted file mode 100644 index c4553d87980da..0000000000000 --- a/x-pack/plugins/monitoring/server/lib/alerts/cluster_state.lib.ts +++ /dev/null @@ -1,88 +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 { AlertInstance } from '../../../../alerts/server'; -import { - AlertCommonCluster, - AlertCommonPerClusterMessage, - AlertCommonPerClusterMessageLinkToken, -} from '../../alerts/types'; -import { AlertClusterStateState, AlertCommonPerClusterMessageTokenType } from '../../alerts/enums'; - -const RESOLVED_SUBJECT = i18n.translate('xpack.monitoring.alerts.clusterStatus.resolvedSubject', { - defaultMessage: 'RESOLVED X-Pack Monitoring: Cluster Status', -}); - -const NEW_SUBJECT = i18n.translate('xpack.monitoring.alerts.clusterStatus.newSubject', { - defaultMessage: 'NEW X-Pack Monitoring: Cluster Status', -}); - -const RED_STATUS_MESSAGE = i18n.translate('xpack.monitoring.alerts.clusterStatus.redMessage', { - defaultMessage: 'Allocate missing primary and replica shards', -}); - -const YELLOW_STATUS_MESSAGE = i18n.translate( - 'xpack.monitoring.alerts.clusterStatus.yellowMessage', - { - defaultMessage: 'Allocate missing replica shards', - } -); - -export function executeActions( - instance: AlertInstance, - cluster: AlertCommonCluster, - status: AlertClusterStateState, - emailAddress: string, - resolved: boolean = false -) { - const message = - status === AlertClusterStateState.Red ? RED_STATUS_MESSAGE : YELLOW_STATUS_MESSAGE; - if (resolved) { - instance.scheduleActions('default', { - subject: RESOLVED_SUBJECT, - message: `This cluster alert has been resolved: ${message} for cluster '${cluster.clusterName}'`, - to: emailAddress, - }); - } else { - instance.scheduleActions('default', { - subject: NEW_SUBJECT, - message: `${message} for cluster '${cluster.clusterName}'`, - to: emailAddress, - }); - } -} - -export function getUiMessage( - status: AlertClusterStateState, - resolved: boolean = false -): AlertCommonPerClusterMessage { - if (resolved) { - return { - text: i18n.translate('xpack.monitoring.alerts.clusterStatus.ui.resolvedMessage', { - defaultMessage: `Elasticsearch cluster status is green.`, - }), - }; - } - const message = - status === AlertClusterStateState.Red ? RED_STATUS_MESSAGE : YELLOW_STATUS_MESSAGE; - return { - text: i18n.translate('xpack.monitoring.alerts.clusterStatus.ui.firingMessage', { - defaultMessage: `Elasticsearch cluster status is {status}. #start_link{message}#end_link`, - values: { - status, - message, - }, - }), - tokens: [ - { - startToken: '#start_link', - endToken: '#end_link', - type: AlertCommonPerClusterMessageTokenType.Link, - url: 'elasticsearch/indices', - } as AlertCommonPerClusterMessageLinkToken, - ], - }; -} diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_state.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_state.test.ts deleted file mode 100644 index 642ae3c39a027..0000000000000 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_state.test.ts +++ /dev/null @@ -1,39 +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 { fetchClusterState } from './fetch_cluster_state'; - -describe('fetchClusterState', () => { - it('should return the cluster state', async () => { - const status = 'green'; - const clusterUuid = 'sdfdsaj34434'; - const callCluster = jest.fn(() => ({ - hits: { - hits: [ - { - _source: { - cluster_state: { - status, - }, - cluster_uuid: clusterUuid, - }, - }, - ], - }, - })); - - const clusters = [{ clusterUuid, clusterName: 'foo' }]; - const index = '.monitoring-es-*'; - - const state = await fetchClusterState(callCluster, clusters, index); - expect(state).toEqual([ - { - state: status, - clusterUuid, - }, - ]); - }); -}); diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_state.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_state.ts deleted file mode 100644 index 3fcc3a2c98993..0000000000000 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_state.ts +++ /dev/null @@ -1,53 +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 { get } from 'lodash'; -import { AlertCommonCluster, AlertClusterState } from '../../alerts/types'; - -export async function fetchClusterState( - callCluster: any, - clusters: AlertCommonCluster[], - index: string -): Promise { - const params = { - index, - filterPath: ['hits.hits._source.cluster_state.status', 'hits.hits._source.cluster_uuid'], - body: { - size: 1, - sort: [{ timestamp: { order: 'desc' } }], - query: { - bool: { - filter: [ - { - terms: { - cluster_uuid: clusters.map((cluster) => cluster.clusterUuid), - }, - }, - { - term: { - type: 'cluster_stats', - }, - }, - { - range: { - timestamp: { - gte: 'now-2m', - }, - }, - }, - ], - }, - }, - }, - }; - - const response = await callCluster('search', params); - return get(response, 'hits.hits', []).map((hit: any) => { - return { - state: get(hit, '_source.cluster_state.status'), - clusterUuid: get(hit, '_source.cluster_uuid'), - }; - }); -} diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_clusters.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_clusters.ts index d1513ac16fb15..48ad31d20a395 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_clusters.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_clusters.ts @@ -4,12 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ import { get } from 'lodash'; -import { AlertCommonCluster } from '../../alerts/types'; +import { AlertCluster } from '../../alerts/types'; -export async function fetchClusters( - callCluster: any, - index: string -): Promise { +export async function fetchClusters(callCluster: any, index: string): Promise { const params = { index, filterPath: [ diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.test.ts new file mode 100644 index 0000000000000..12926a30efa1b --- /dev/null +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.test.ts @@ -0,0 +1,228 @@ +/* + * 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 { fetchCpuUsageNodeStats } from './fetch_cpu_usage_node_stats'; + +describe('fetchCpuUsageNodeStats', () => { + let callCluster = jest.fn(); + const clusters = [ + { + clusterUuid: 'abc123', + clusterName: 'test', + }, + ]; + const index = '.monitoring-es-*'; + const startMs = 0; + const endMs = 0; + const size = 10; + + it('fetch normal stats', async () => { + callCluster = jest.fn().mockImplementation((...args) => { + return { + aggregations: { + clusters: { + buckets: [ + { + key: clusters[0].clusterUuid, + nodes: { + buckets: [ + { + key: 'theNodeId', + index: { + buckets: [ + { + key: '.monitoring-es-TODAY', + }, + ], + }, + name: { + buckets: [ + { + key: 'theNodeName', + }, + ], + }, + average_cpu: { + value: 10, + }, + }, + ], + }, + }, + ], + }, + }, + }; + }); + const result = await fetchCpuUsageNodeStats(callCluster, clusters, index, startMs, endMs, size); + expect(result).toEqual([ + { + clusterUuid: clusters[0].clusterUuid, + nodeName: 'theNodeName', + nodeId: 'theNodeId', + cpuUsage: 10, + containerUsage: undefined, + containerPeriods: undefined, + containerQuota: undefined, + ccs: null, + }, + ]); + }); + + it('fetch container stats', async () => { + callCluster = jest.fn().mockImplementation((...args) => { + return { + aggregations: { + clusters: { + buckets: [ + { + key: clusters[0].clusterUuid, + nodes: { + buckets: [ + { + key: 'theNodeId', + index: { + buckets: [ + { + key: '.monitoring-es-TODAY', + }, + ], + }, + name: { + buckets: [ + { + key: 'theNodeName', + }, + ], + }, + average_usage: { + value: 10, + }, + average_periods: { + value: 5, + }, + average_quota: { + value: 50, + }, + }, + ], + }, + }, + ], + }, + }, + }; + }); + const result = await fetchCpuUsageNodeStats(callCluster, clusters, index, startMs, endMs, size); + expect(result).toEqual([ + { + clusterUuid: clusters[0].clusterUuid, + nodeName: 'theNodeName', + nodeId: 'theNodeId', + cpuUsage: undefined, + containerUsage: 10, + containerPeriods: 5, + containerQuota: 50, + ccs: null, + }, + ]); + }); + + it('fetch properly return ccs', async () => { + callCluster = jest.fn().mockImplementation((...args) => { + return { + aggregations: { + clusters: { + buckets: [ + { + key: clusters[0].clusterUuid, + nodes: { + buckets: [ + { + key: 'theNodeId', + index: { + buckets: [ + { + key: 'foo:.monitoring-es-TODAY', + }, + ], + }, + name: { + buckets: [ + { + key: 'theNodeName', + }, + ], + }, + average_usage: { + value: 10, + }, + average_periods: { + value: 5, + }, + average_quota: { + value: 50, + }, + }, + ], + }, + }, + ], + }, + }, + }; + }); + const result = await fetchCpuUsageNodeStats(callCluster, clusters, index, startMs, endMs, size); + expect(result[0].ccs).toBe('foo'); + }); + + it('should use consistent params', async () => { + let params = null; + callCluster = jest.fn().mockImplementation((...args) => { + params = args[1]; + }); + await fetchCpuUsageNodeStats(callCluster, clusters, index, startMs, endMs, size); + expect(params).toStrictEqual({ + index, + filterPath: ['aggregations'], + body: { + size: 0, + query: { + bool: { + filter: [ + { terms: { cluster_uuid: clusters.map((cluster) => cluster.clusterUuid) } }, + { term: { type: 'node_stats' } }, + { range: { timestamp: { format: 'epoch_millis', gte: 0, lte: 0 } } }, + ], + }, + }, + aggs: { + clusters: { + terms: { + field: 'cluster_uuid', + size, + include: clusters.map((cluster) => cluster.clusterUuid), + }, + aggs: { + nodes: { + terms: { field: 'node_stats.node_id', size }, + aggs: { + index: { terms: { field: '_index', size: 1 } }, + average_cpu: { avg: { field: 'node_stats.process.cpu.percent' } }, + average_usage: { avg: { field: 'node_stats.os.cgroup.cpuacct.usage_nanos' } }, + average_periods: { + avg: { field: 'node_stats.os.cgroup.cpu.stat.number_of_elapsed_periods' }, + }, + average_quota: { avg: { field: 'node_stats.os.cgroup.cpu.cfs_quota_micros' } }, + name: { terms: { field: 'source_node.name', size: 1 } }, + }, + }, + }, + }, + }, + }, + }); + }); +}); diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.ts new file mode 100644 index 0000000000000..4fdb03b61950e --- /dev/null +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.ts @@ -0,0 +1,137 @@ +/* + * 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 { get } from 'lodash'; +import { AlertCluster, AlertCpuUsageNodeStats } from '../../alerts/types'; + +interface NodeBucketESResponse { + key: string; + average_cpu: { value: number }; +} + +interface ClusterBucketESResponse { + key: string; + nodes: { + buckets: NodeBucketESResponse[]; + }; +} + +export async function fetchCpuUsageNodeStats( + callCluster: any, + clusters: AlertCluster[], + index: string, + startMs: number, + endMs: number, + size: number +): Promise { + const filterPath = ['aggregations']; + const params = { + index, + filterPath, + body: { + size: 0, + query: { + bool: { + filter: [ + { + terms: { + cluster_uuid: clusters.map((cluster) => cluster.clusterUuid), + }, + }, + { + term: { + type: 'node_stats', + }, + }, + { + range: { + timestamp: { + format: 'epoch_millis', + gte: startMs, + lte: endMs, + }, + }, + }, + ], + }, + }, + aggs: { + clusters: { + terms: { + field: 'cluster_uuid', + size, + include: clusters.map((cluster) => cluster.clusterUuid), + }, + aggs: { + nodes: { + terms: { + field: 'node_stats.node_id', + size, + }, + aggs: { + index: { + terms: { + field: '_index', + size: 1, + }, + }, + average_cpu: { + avg: { + field: 'node_stats.process.cpu.percent', + }, + }, + average_usage: { + avg: { + field: 'node_stats.os.cgroup.cpuacct.usage_nanos', + }, + }, + average_periods: { + avg: { + field: 'node_stats.os.cgroup.cpu.stat.number_of_elapsed_periods', + }, + }, + average_quota: { + avg: { + field: 'node_stats.os.cgroup.cpu.cfs_quota_micros', + }, + }, + name: { + terms: { + field: 'source_node.name', + size: 1, + }, + }, + }, + }, + }, + }, + }, + }, + }; + + const response = await callCluster('search', params); + const stats: AlertCpuUsageNodeStats[] = []; + const clusterBuckets = get( + response, + 'aggregations.clusters.buckets', + [] + ) as ClusterBucketESResponse[]; + for (const clusterBucket of clusterBuckets) { + for (const node of clusterBucket.nodes.buckets) { + const indexName = get(node, 'index.buckets[0].key', ''); + stats.push({ + clusterUuid: clusterBucket.key, + nodeId: node.key, + nodeName: get(node, 'name.buckets[0].key'), + cpuUsage: get(node, 'average_cpu.value'), + containerUsage: get(node, 'average_usage.value'), + containerPeriods: get(node, 'average_periods.value'), + containerQuota: get(node, 'average_quota.value'), + ccs: indexName.includes(':') ? indexName.split(':')[0] : null, + }); + } + } + return stats; +} diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_default_email_address.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_default_email_address.test.ts deleted file mode 100644 index ae914c7a2ace1..0000000000000 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_default_email_address.test.ts +++ /dev/null @@ -1,17 +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 { fetchDefaultEmailAddress } from './fetch_default_email_address'; -import { uiSettingsServiceMock } from '../../../../../../src/core/server/mocks'; - -describe('fetchDefaultEmailAddress', () => { - it('get the email address', async () => { - const email = 'test@test.com'; - const uiSettingsClient = uiSettingsServiceMock.createClient(); - uiSettingsClient.get.mockResolvedValue(email); - const result = await fetchDefaultEmailAddress(uiSettingsClient); - expect(result).toBe(email); - }); -}); diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_default_email_address.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_default_email_address.ts deleted file mode 100644 index 88e4199a88256..0000000000000 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_default_email_address.ts +++ /dev/null @@ -1,13 +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 { IUiSettingsClient } from 'src/core/server'; -import { MONITORING_CONFIG_ALERTING_EMAIL_ADDRESS } from '../../../common/constants'; - -export async function fetchDefaultEmailAddress( - uiSettingsClient: IUiSettingsClient -): Promise { - return await uiSettingsClient.get(MONITORING_CONFIG_ALERTING_EMAIL_ADDRESS); -} diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_legacy_alerts.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_legacy_alerts.test.ts new file mode 100644 index 0000000000000..a3743a8ff206f --- /dev/null +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_legacy_alerts.test.ts @@ -0,0 +1,93 @@ +/* + * 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 { fetchLegacyAlerts } from './fetch_legacy_alerts'; + +describe('fetchLegacyAlerts', () => { + let callCluster = jest.fn(); + const clusters = [ + { + clusterUuid: 'abc123', + clusterName: 'test', + }, + ]; + const index = '.monitoring-es-*'; + const size = 10; + + it('fetch legacy alerts', async () => { + const prefix = 'thePrefix'; + const message = 'theMessage'; + const nodes = {}; + const metadata = { + severity: 2000, + cluster_uuid: clusters[0].clusterUuid, + metadata: {}, + }; + callCluster = jest.fn().mockImplementation(() => { + return { + hits: { + hits: [ + { + _source: { + prefix, + message, + nodes, + metadata, + }, + }, + ], + }, + }; + }); + const result = await fetchLegacyAlerts(callCluster, clusters, index, 'myWatch', size); + expect(result).toEqual([ + { + message, + metadata, + nodes, + prefix, + }, + ]); + }); + + it('should use consistent params', async () => { + let params = null; + callCluster = jest.fn().mockImplementation((...args) => { + params = args[1]; + }); + await fetchLegacyAlerts(callCluster, clusters, index, 'myWatch', size); + expect(params).toStrictEqual({ + index, + filterPath: [ + 'hits.hits._source.prefix', + 'hits.hits._source.message', + 'hits.hits._source.resolved_timestamp', + 'hits.hits._source.nodes', + 'hits.hits._source.metadata.*', + ], + body: { + size, + sort: [{ timestamp: { order: 'desc' } }], + query: { + bool: { + minimum_should_match: 1, + filter: [ + { + terms: { 'metadata.cluster_uuid': clusters.map((cluster) => cluster.clusterUuid) }, + }, + { term: { 'metadata.watch': 'myWatch' } }, + ], + should: [ + { range: { timestamp: { gte: 'now-2m' } } }, + { range: { resolved_timestamp: { gte: 'now-2m' } } }, + { bool: { must_not: { exists: { field: 'resolved_timestamp' } } } }, + ], + }, + }, + collapse: { field: 'metadata.cluster_uuid' }, + }, + }); + }); +}); diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_legacy_alerts.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_legacy_alerts.ts new file mode 100644 index 0000000000000..fe01a1b921c2e --- /dev/null +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_legacy_alerts.ts @@ -0,0 +1,93 @@ +/* + * 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 { get } from 'lodash'; +import { LegacyAlert, AlertCluster, LegacyAlertMetadata } from '../../alerts/types'; + +export async function fetchLegacyAlerts( + callCluster: any, + clusters: AlertCluster[], + index: string, + watchName: string, + size: number +): Promise { + const params = { + index, + filterPath: [ + 'hits.hits._source.prefix', + 'hits.hits._source.message', + 'hits.hits._source.resolved_timestamp', + 'hits.hits._source.nodes', + 'hits.hits._source.metadata.*', + ], + body: { + size, + sort: [ + { + timestamp: { + order: 'desc', + }, + }, + ], + query: { + bool: { + minimum_should_match: 1, + filter: [ + { + terms: { + 'metadata.cluster_uuid': clusters.map((cluster) => cluster.clusterUuid), + }, + }, + { + term: { + 'metadata.watch': watchName, + }, + }, + ], + should: [ + { + range: { + timestamp: { + gte: 'now-2m', + }, + }, + }, + { + range: { + resolved_timestamp: { + gte: 'now-2m', + }, + }, + }, + { + bool: { + must_not: { + exists: { + field: 'resolved_timestamp', + }, + }, + }, + }, + ], + }, + }, + collapse: { + field: 'metadata.cluster_uuid', + }, + }, + }; + + const response = await callCluster('search', params); + return get(response, 'hits.hits', []).map((hit: any) => { + const legacyAlert: LegacyAlert = { + prefix: get(hit, '_source.prefix'), + message: get(hit, '_source.message'), + resolved_timestamp: get(hit, '_source.resolved_timestamp'), + nodes: get(hit, '_source.nodes'), + metadata: get(hit, '_source.metadata') as LegacyAlertMetadata, + }; + return legacyAlert; + }); +} diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.test.ts deleted file mode 100644 index 9dcb4ffb82a5f..0000000000000 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.test.ts +++ /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 { fetchLicenses } from './fetch_licenses'; - -describe('fetchLicenses', () => { - const clusterName = 'MyCluster'; - const clusterUuid = 'clusterA'; - const license = { - status: 'active', - expiry_date_in_millis: 1579532493876, - type: 'basic', - }; - - it('return a list of licenses', async () => { - const callCluster = jest.fn().mockImplementation(() => ({ - hits: { - hits: [ - { - _source: { - license, - cluster_uuid: clusterUuid, - }, - }, - ], - }, - })); - const clusters = [{ clusterUuid, clusterName }]; - const index = '.monitoring-es-*'; - const result = await fetchLicenses(callCluster, clusters, index); - expect(result).toEqual([ - { - status: license.status, - type: license.type, - expiryDateMS: license.expiry_date_in_millis, - clusterUuid, - }, - ]); - }); - - it('should only search for the clusters provided', async () => { - const callCluster = jest.fn(); - const clusters = [{ clusterUuid, clusterName }]; - const index = '.monitoring-es-*'; - await fetchLicenses(callCluster, clusters, index); - const params = callCluster.mock.calls[0][1]; - expect(params.body.query.bool.filter[0].terms.cluster_uuid).toEqual([clusterUuid]); - }); - - it('should limit the time period in the query', async () => { - const callCluster = jest.fn(); - const clusters = [{ clusterUuid, clusterName }]; - const index = '.monitoring-es-*'; - await fetchLicenses(callCluster, clusters, index); - const params = callCluster.mock.calls[0][1]; - expect(params.body.query.bool.filter[2].range.timestamp.gte).toBe('now-2m'); - }); -}); diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.ts deleted file mode 100644 index a65cba493dab9..0000000000000 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.ts +++ /dev/null @@ -1,57 +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 { get } from 'lodash'; -import { AlertLicense, AlertCommonCluster } from '../../alerts/types'; - -export async function fetchLicenses( - callCluster: any, - clusters: AlertCommonCluster[], - index: string -): Promise { - const params = { - index, - filterPath: ['hits.hits._source.license.*', 'hits.hits._source.cluster_uuid'], - body: { - size: 1, - sort: [{ timestamp: { order: 'desc' } }], - query: { - bool: { - filter: [ - { - terms: { - cluster_uuid: clusters.map((cluster) => cluster.clusterUuid), - }, - }, - { - term: { - type: 'cluster_stats', - }, - }, - { - range: { - timestamp: { - gte: 'now-2m', - }, - }, - }, - ], - }, - }, - }, - }; - - const response = await callCluster('search', params); - return get(response, 'hits.hits', []).map((hit: any) => { - const rawLicense: any = get(hit, '_source.license', {}); - const license: AlertLicense = { - status: rawLicense.status, - type: rawLicense.type, - expiryDateMS: rawLicense.expiry_date_in_millis, - clusterUuid: get(hit, '_source.cluster_uuid'), - }; - return license; - }); -} diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_status.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_status.test.ts index a3bcb61afacd6..ff674195f0730 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_status.test.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_status.test.ts @@ -5,22 +5,31 @@ */ import { fetchStatus } from './fetch_status'; -import { AlertCommonPerClusterState } from '../../alerts/types'; +import { AlertUiState, AlertState } from '../../alerts/types'; +import { AlertSeverity } from '../../../common/enums'; +import { ALERT_CPU_USAGE, ALERT_CLUSTER_HEALTH } from '../../../common/constants'; describe('fetchStatus', () => { - const alertType = 'monitoringTest'; + const alertType = ALERT_CPU_USAGE; + const alertTypes = [alertType]; const log = { warn: jest.fn() }; const start = 0; const end = 0; const id = 1; - const defaultUiState = { + const defaultClusterState = { + clusterUuid: 'abc', + clusterName: 'test', + }; + const defaultUiState: AlertUiState = { isFiring: false, - severity: 0, + severity: AlertSeverity.Success, message: null, resolvedMS: 0, lastCheckedMS: 0, triggeredMS: 0, }; + let alertStates: AlertState[] = []; + const licenseService = null; const alertsClient = { find: jest.fn(() => ({ total: 1, @@ -31,10 +40,12 @@ describe('fetchStatus', () => { ], })), getAlertState: jest.fn(() => ({ - alertTypeState: { - state: { - ui: defaultUiState, - } as AlertCommonPerClusterState, + alertInstances: { + abc: { + state: { + alertStates, + }, + }, }, })), }; @@ -45,57 +56,96 @@ describe('fetchStatus', () => { }); it('should fetch from the alerts client', async () => { - const status = await fetchStatus(alertsClient as any, [alertType], start, end, log as any); - expect(status).toEqual([]); + const status = await fetchStatus( + alertsClient as any, + licenseService as any, + alertTypes, + defaultClusterState.clusterUuid, + start, + end, + log as any + ); + expect(status).toEqual({ + monitoring_alert_cpu_usage: { + alert: { + isLegacy: false, + label: 'CPU Usage', + paramDetails: {}, + rawAlert: { id: 1 }, + type: 'monitoring_alert_cpu_usage', + }, + enabled: true, + exists: true, + states: [], + }, + }); }); it('should return alerts that are firing', async () => { - alertsClient.getAlertState = jest.fn(() => ({ - alertTypeState: { - state: { - ui: { - ...defaultUiState, - isFiring: true, - }, - } as AlertCommonPerClusterState, + alertStates = [ + { + cluster: defaultClusterState, + ccs: null, + ui: { + ...defaultUiState, + isFiring: true, + }, }, - })); + ]; - const status = await fetchStatus(alertsClient as any, [alertType], start, end, log as any); - expect(status.length).toBe(1); - expect(status[0].type).toBe(alertType); - expect(status[0].isFiring).toBe(true); + const status = await fetchStatus( + alertsClient as any, + licenseService as any, + alertTypes, + defaultClusterState.clusterUuid, + start, + end, + log as any + ); + expect(Object.values(status).length).toBe(1); + expect(Object.keys(status)).toEqual(alertTypes); + expect(status[alertType].states[0].state.ui.isFiring).toBe(true); }); it('should return alerts that have been resolved in the time period', async () => { - alertsClient.getAlertState = jest.fn(() => ({ - alertTypeState: { - state: { - ui: { - ...defaultUiState, - resolvedMS: 1500, - }, - } as AlertCommonPerClusterState, + alertStates = [ + { + cluster: defaultClusterState, + ccs: null, + ui: { + ...defaultUiState, + resolvedMS: 1500, + }, }, - })); + ]; const customStart = 1000; const customEnd = 2000; const status = await fetchStatus( alertsClient as any, - [alertType], + licenseService as any, + alertTypes, + defaultClusterState.clusterUuid, customStart, customEnd, log as any ); - expect(status.length).toBe(1); - expect(status[0].type).toBe(alertType); - expect(status[0].isFiring).toBe(false); + expect(Object.values(status).length).toBe(1); + expect(Object.keys(status)).toEqual(alertTypes); + expect(status[alertType].states[0].state.ui.isFiring).toBe(false); }); it('should pass in the right filter to the alerts client', async () => { - await fetchStatus(alertsClient as any, [alertType], start, end, log as any); + await fetchStatus( + alertsClient as any, + licenseService as any, + alertTypes, + defaultClusterState.clusterUuid, + start, + end, + log as any + ); expect((alertsClient.find as jest.Mock).mock.calls[0][0].options.filter).toBe( `alert.attributes.alertTypeId:${alertType}` ); @@ -106,8 +156,16 @@ describe('fetchStatus', () => { alertTypeState: null, })) as any; - const status = await fetchStatus(alertsClient as any, [alertType], start, end, log as any); - expect(status).toEqual([]); + const status = await fetchStatus( + alertsClient as any, + licenseService as any, + alertTypes, + defaultClusterState.clusterUuid, + start, + end, + log as any + ); + expect(status[alertType].states.length).toEqual(0); }); it('should return nothing if no alerts are found', async () => { @@ -116,7 +174,34 @@ describe('fetchStatus', () => { data: [], })) as any; - const status = await fetchStatus(alertsClient as any, [alertType], start, end, log as any); - expect(status).toEqual([]); + const status = await fetchStatus( + alertsClient as any, + licenseService as any, + alertTypes, + defaultClusterState.clusterUuid, + start, + end, + log as any + ); + expect(status).toEqual({}); + }); + + it('should pass along the license service', async () => { + const customLicenseService = { + getWatcherFeature: jest.fn().mockImplementation(() => ({ + isAvailable: true, + isEnabled: true, + })), + }; + await fetchStatus( + alertsClient as any, + customLicenseService as any, + [ALERT_CLUSTER_HEALTH], + defaultClusterState.clusterUuid, + start, + end, + log as any + ); + expect(customLicenseService.getWatcherFeature).toHaveBeenCalled(); }); }); diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_status.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_status.ts index 614658baf5c79..49e688fafbee5 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_status.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_status.ts @@ -4,56 +4,76 @@ * you may not use this file except in compliance with the Elastic License. */ import moment from 'moment'; -import { Logger } from '../../../../../../src/core/server'; -import { AlertCommonPerClusterState } from '../../alerts/types'; +import { AlertInstanceState } from '../../alerts/types'; import { AlertsClient } from '../../../../alerts/server'; +import { AlertsFactory } from '../../alerts'; +import { CommonAlertStatus, CommonAlertState, CommonAlertFilter } from '../../../common/types'; +import { ALERTS } from '../../../common/constants'; +import { MonitoringLicenseService } from '../../types'; export async function fetchStatus( alertsClient: AlertsClient, - alertTypes: string[], + licenseService: MonitoringLicenseService, + alertTypes: string[] | undefined, + clusterUuid: string, start: number, end: number, - log: Logger -): Promise { - const statuses = await Promise.all( - alertTypes.map( - (type) => - new Promise(async (resolve, reject) => { - // We need to get the id from the alertTypeId - const alerts = await alertsClient.find({ - options: { - filter: `alert.attributes.alertTypeId:${type}`, - }, - }); - if (alerts.total === 0) { - return resolve(false); - } + filters: CommonAlertFilter[] +): Promise<{ [type: string]: CommonAlertStatus }> { + const byType: { [type: string]: CommonAlertStatus } = {}; + await Promise.all( + (alertTypes || ALERTS).map(async (type) => { + const alert = await AlertsFactory.getByType(type, alertsClient); + if (!alert || !alert.isEnabled(licenseService)) { + return; + } + const serialized = alert.serialize(); + if (!serialized) { + return; + } - if (alerts.total !== 1) { - log.warn(`Found more than one alert for type ${type} which is unexpected.`); - } + const result: CommonAlertStatus = { + exists: false, + enabled: false, + states: [], + alert: serialized, + }; + + byType[type] = result; + + const id = alert.getId(); + if (!id) { + return result; + } + + result.exists = true; + result.enabled = true; - const id = alerts.data[0].id; + // Now that we have the id, we can get the state + const states = await alert.getStates(alertsClient, id, filters); + if (!states) { + return result; + } - // Now that we have the id, we can get the state - const states = await alertsClient.getAlertState({ id }); - if (!states || !states.alertTypeState) { - log.warn(`No alert states found for type ${type} which is unexpected.`); - return resolve(false); + result.states = Object.values(states).reduce((accum: CommonAlertState[], instance: any) => { + const alertInstanceState = instance.state as AlertInstanceState; + for (const state of alertInstanceState.alertStates) { + const meta = instance.meta; + if (clusterUuid && state.cluster.clusterUuid !== clusterUuid) { + return accum; } - const state = Object.values(states.alertTypeState)[0] as AlertCommonPerClusterState; + let firing = false; const isInBetween = moment(state.ui.resolvedMS).isBetween(start, end); if (state.ui.isFiring || isInBetween) { - return resolve({ - type, - ...state.ui, - }); + firing = true; } - return resolve(false); - }) - ) + accum.push({ firing, state, meta }); + } + return accum; + }, []); + }) ); - return statuses.filter(Boolean); + return byType; } diff --git a/x-pack/plugins/monitoring/server/lib/alerts/get_prepared_alert.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/get_prepared_alert.test.ts deleted file mode 100644 index 1840a2026a753..0000000000000 --- a/x-pack/plugins/monitoring/server/lib/alerts/get_prepared_alert.test.ts +++ /dev/null @@ -1,163 +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 { getPreparedAlert } from './get_prepared_alert'; -import { fetchClusters } from './fetch_clusters'; -import { fetchDefaultEmailAddress } from './fetch_default_email_address'; - -jest.mock('./fetch_clusters', () => ({ - fetchClusters: jest.fn(), -})); - -jest.mock('./fetch_default_email_address', () => ({ - fetchDefaultEmailAddress: jest.fn(), -})); - -describe('getPreparedAlert', () => { - const uiSettings = { get: jest.fn() }; - const alertType = 'test'; - const getUiSettingsService = async () => ({ - asScopedToClient: () => uiSettings, - }); - const monitoringCluster = null; - const logger = { warn: jest.fn() }; - const ccsEnabled = false; - const services = { - callCluster: jest.fn(), - savedObjectsClient: null, - }; - const emailAddress = 'foo@foo.com'; - const data = [{ foo: 1 }]; - const dataFetcher = () => data; - const clusterName = 'MonitoringCluster'; - const clusterUuid = 'sdf34sdf'; - const clusters = [{ clusterName, clusterUuid }]; - - afterEach(() => { - (uiSettings.get as jest.Mock).mockClear(); - (services.callCluster as jest.Mock).mockClear(); - (fetchClusters as jest.Mock).mockClear(); - (fetchDefaultEmailAddress as jest.Mock).mockClear(); - }); - - beforeEach(() => { - (fetchClusters as jest.Mock).mockImplementation(() => clusters); - (fetchDefaultEmailAddress as jest.Mock).mockImplementation(() => emailAddress); - }); - - it('should return fields as expected', async () => { - (uiSettings.get as jest.Mock).mockImplementation(() => { - return emailAddress; - }); - - const alert = await getPreparedAlert( - alertType, - getUiSettingsService as any, - monitoringCluster as any, - logger as any, - ccsEnabled, - services as any, - dataFetcher as any - ); - - expect(alert && alert.emailAddress).toBe(emailAddress); - expect(alert && alert.data).toBe(data); - }); - - it('should add ccs if specified', async () => { - const ccsClusterName = 'remoteCluster'; - (services.callCluster as jest.Mock).mockImplementation(() => { - return { - [ccsClusterName]: { - connected: true, - }, - }; - }); - - await getPreparedAlert( - alertType, - getUiSettingsService as any, - monitoringCluster as any, - logger as any, - true, - services as any, - dataFetcher as any - ); - - expect((fetchClusters as jest.Mock).mock.calls[0][1].includes(ccsClusterName)).toBe(true); - }); - - it('should ignore ccs if no remote clusters are available', async () => { - const ccsClusterName = 'remoteCluster'; - (services.callCluster as jest.Mock).mockImplementation(() => { - return { - [ccsClusterName]: { - connected: false, - }, - }; - }); - - await getPreparedAlert( - alertType, - getUiSettingsService as any, - monitoringCluster as any, - logger as any, - true, - services as any, - dataFetcher as any - ); - - expect((fetchClusters as jest.Mock).mock.calls[0][1].includes(ccsClusterName)).toBe(false); - }); - - it('should pass in the clusters into the data fetcher', async () => { - const customDataFetcher = jest.fn(() => data); - - await getPreparedAlert( - alertType, - getUiSettingsService as any, - monitoringCluster as any, - logger as any, - true, - services as any, - customDataFetcher as any - ); - - expect((customDataFetcher as jest.Mock).mock.calls[0][1]).toBe(clusters); - }); - - it('should return nothing if the data fetcher returns nothing', async () => { - const customDataFetcher = jest.fn(() => []); - - const result = await getPreparedAlert( - alertType, - getUiSettingsService as any, - monitoringCluster as any, - logger as any, - true, - services as any, - customDataFetcher as any - ); - - expect(result).toBe(null); - }); - - it('should return nothing if there is no email address', async () => { - (fetchDefaultEmailAddress as jest.Mock).mockImplementation(() => null); - - const result = await getPreparedAlert( - alertType, - getUiSettingsService as any, - monitoringCluster as any, - logger as any, - true, - services as any, - dataFetcher as any - ); - - expect(result).toBe(null); - }); -}); diff --git a/x-pack/plugins/monitoring/server/lib/alerts/get_prepared_alert.ts b/x-pack/plugins/monitoring/server/lib/alerts/get_prepared_alert.ts deleted file mode 100644 index 1d307bc018a7b..0000000000000 --- a/x-pack/plugins/monitoring/server/lib/alerts/get_prepared_alert.ts +++ /dev/null @@ -1,87 +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 { Logger, ILegacyCustomClusterClient, UiSettingsServiceStart } from 'kibana/server'; -import { CallCluster } from 'src/legacy/core_plugins/elasticsearch'; -import { AlertServices } from '../../../../alerts/server'; -import { AlertCommonCluster } from '../../alerts/types'; -import { INDEX_PATTERN_ELASTICSEARCH } from '../../../common/constants'; -import { fetchAvailableCcs } from './fetch_available_ccs'; -import { getCcsIndexPattern } from './get_ccs_index_pattern'; -import { fetchClusters } from './fetch_clusters'; -import { fetchDefaultEmailAddress } from './fetch_default_email_address'; - -export interface PreparedAlert { - emailAddress: string; - clusters: AlertCommonCluster[]; - data: any[]; - timezone: string; - dateFormat: string; -} - -async function getCallCluster( - monitoringCluster: ILegacyCustomClusterClient, - services: Pick -): Promise { - if (!monitoringCluster) { - return services.callCluster; - } - - return monitoringCluster.callAsInternalUser; -} - -export async function getPreparedAlert( - alertType: string, - getUiSettingsService: () => Promise, - monitoringCluster: ILegacyCustomClusterClient, - logger: Logger, - ccsEnabled: boolean, - services: Pick, - dataFetcher: ( - callCluster: CallCluster, - clusters: AlertCommonCluster[], - esIndexPattern: string - ) => Promise -): Promise { - const callCluster = await getCallCluster(monitoringCluster, services); - - // Support CCS use cases by querying to find available remote clusters - // and then adding those to the index pattern we are searching against - let esIndexPattern = INDEX_PATTERN_ELASTICSEARCH; - if (ccsEnabled) { - const availableCcs = await fetchAvailableCcs(callCluster); - if (availableCcs.length > 0) { - esIndexPattern = getCcsIndexPattern(esIndexPattern, availableCcs); - } - } - - const clusters = await fetchClusters(callCluster, esIndexPattern); - - // Fetch the specific data - const data = await dataFetcher(callCluster, clusters, esIndexPattern); - if (data.length === 0) { - logger.warn(`No data found for ${alertType}.`); - return null; - } - - const uiSettings = (await getUiSettingsService()).asScopedToClient(services.savedObjectsClient); - const dateFormat: string = await uiSettings.get('dateFormat'); - const timezone: string = await uiSettings.get('dateFormat:tz'); - const emailAddress = await fetchDefaultEmailAddress(uiSettings); - if (!emailAddress) { - // TODO: we can do more here - logger.warn(`Unable to send email for ${alertType} because there is no email configured.`); - return null; - } - - return { - emailAddress, - data, - clusters, - dateFormat, - timezone, - }; -} diff --git a/x-pack/plugins/monitoring/server/lib/alerts/license_expiration.lib.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/license_expiration.lib.test.ts deleted file mode 100644 index b99208bdde2c8..0000000000000 --- a/x-pack/plugins/monitoring/server/lib/alerts/license_expiration.lib.test.ts +++ /dev/null @@ -1,64 +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 moment from 'moment-timezone'; -import { executeActions, getUiMessage } from './license_expiration.lib'; - -describe('licenseExpiration lib', () => { - describe('executeActions', () => { - const clusterName = 'clusterA'; - const instance: any = { scheduleActions: jest.fn() }; - const license: any = { clusterName }; - const $expiry = moment('2020-01-20'); - const dateFormat = 'dddd, MMMM Do YYYY, h:mm:ss a'; - const emailAddress = 'test@test.com'; - - beforeEach(() => { - instance.scheduleActions.mockClear(); - }); - - it('should schedule actions when firing', () => { - executeActions(instance, license, $expiry, dateFormat, emailAddress, false); - expect(instance.scheduleActions).toHaveBeenCalledWith('default', { - subject: 'NEW X-Pack Monitoring: License Expiration', - message: `Cluster '${clusterName}' license is going to expire on Monday, January 20th 2020, 12:00:00 am. Please update your license.`, - to: emailAddress, - }); - }); - - it('should schedule actions when resolved', () => { - executeActions(instance, license, $expiry, dateFormat, emailAddress, true); - expect(instance.scheduleActions).toHaveBeenCalledWith('default', { - subject: 'RESOLVED X-Pack Monitoring: License Expiration', - message: `This cluster alert has been resolved: Cluster '${clusterName}' license was going to expire on Monday, January 20th 2020, 12:00:00 am.`, - to: emailAddress, - }); - }); - }); - - describe('getUiMessage', () => { - it('should return a message when firing', () => { - const message = getUiMessage(false); - expect(message.text).toBe( - `This cluster's license is going to expire in #relative at #absolute. #start_linkPlease update your license.#end_link` - ); - // LOL How do I avoid this in TS???? - if (!message.tokens) { - return expect(false).toBe(true); - } - expect(message.tokens.length).toBe(3); - expect(message.tokens[0].startToken).toBe('#relative'); - expect(message.tokens[1].startToken).toBe('#absolute'); - expect(message.tokens[2].startToken).toBe('#start_link'); - expect(message.tokens[2].endToken).toBe('#end_link'); - }); - - it('should return a message when resolved', () => { - const message = getUiMessage(true); - expect(message.text).toBe(`This cluster's license is active.`); - expect(message.tokens).not.toBeDefined(); - }); - }); -}); diff --git a/x-pack/plugins/monitoring/server/lib/alerts/license_expiration.lib.ts b/x-pack/plugins/monitoring/server/lib/alerts/license_expiration.lib.ts deleted file mode 100644 index 97ef2790b516d..0000000000000 --- a/x-pack/plugins/monitoring/server/lib/alerts/license_expiration.lib.ts +++ /dev/null @@ -1,88 +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 { Moment } from 'moment-timezone'; -import { i18n } from '@kbn/i18n'; -import { AlertInstance } from '../../../../alerts/server'; -import { - AlertCommonPerClusterMessageLinkToken, - AlertCommonPerClusterMessageTimeToken, - AlertCommonCluster, - AlertCommonPerClusterMessage, -} from '../../alerts/types'; -import { AlertCommonPerClusterMessageTokenType } from '../../alerts/enums'; - -const RESOLVED_SUBJECT = i18n.translate( - 'xpack.monitoring.alerts.licenseExpiration.resolvedSubject', - { - defaultMessage: 'RESOLVED X-Pack Monitoring: License Expiration', - } -); - -const NEW_SUBJECT = i18n.translate('xpack.monitoring.alerts.licenseExpiration.newSubject', { - defaultMessage: 'NEW X-Pack Monitoring: License Expiration', -}); - -export function executeActions( - instance: AlertInstance, - cluster: AlertCommonCluster, - $expiry: Moment, - dateFormat: string, - emailAddress: string, - resolved: boolean = false -) { - if (resolved) { - instance.scheduleActions('default', { - subject: RESOLVED_SUBJECT, - message: `This cluster alert has been resolved: Cluster '${ - cluster.clusterName - }' license was going to expire on ${$expiry.format(dateFormat)}.`, - to: emailAddress, - }); - } else { - instance.scheduleActions('default', { - subject: NEW_SUBJECT, - message: `Cluster '${cluster.clusterName}' license is going to expire on ${$expiry.format( - dateFormat - )}. Please update your license.`, - to: emailAddress, - }); - } -} - -export function getUiMessage(resolved: boolean = false): AlertCommonPerClusterMessage { - if (resolved) { - return { - text: i18n.translate('xpack.monitoring.alerts.licenseExpiration.ui.resolvedMessage', { - defaultMessage: `This cluster's license is active.`, - }), - }; - } - return { - text: i18n.translate('xpack.monitoring.alerts.licenseExpiration.ui.firingMessage', { - defaultMessage: `This cluster's license is going to expire in #relative at #absolute. #start_linkPlease update your license.#end_link`, - }), - tokens: [ - { - startToken: '#relative', - type: AlertCommonPerClusterMessageTokenType.Time, - isRelative: true, - isAbsolute: false, - } as AlertCommonPerClusterMessageTimeToken, - { - startToken: '#absolute', - type: AlertCommonPerClusterMessageTokenType.Time, - isAbsolute: true, - isRelative: false, - } as AlertCommonPerClusterMessageTimeToken, - { - startToken: '#start_link', - endToken: '#end_link', - type: AlertCommonPerClusterMessageTokenType.Link, - url: 'license', - } as AlertCommonPerClusterMessageLinkToken, - ], - }; -} diff --git a/x-pack/plugins/monitoring/server/lib/alerts/map_legacy_severity.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/map_legacy_severity.test.ts new file mode 100644 index 0000000000000..11a1c6eb1a6d6 --- /dev/null +++ b/x-pack/plugins/monitoring/server/lib/alerts/map_legacy_severity.test.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 { AlertSeverity } from '../../../common/enums'; +import { mapLegacySeverity } from './map_legacy_severity'; + +describe('mapLegacySeverity', () => { + it('should map it', () => { + expect(mapLegacySeverity(500)).toBe(AlertSeverity.Warning); + expect(mapLegacySeverity(1000)).toBe(AlertSeverity.Warning); + expect(mapLegacySeverity(2000)).toBe(AlertSeverity.Danger); + }); +}); diff --git a/x-pack/plugins/monitoring/server/lib/alerts/map_legacy_severity.ts b/x-pack/plugins/monitoring/server/lib/alerts/map_legacy_severity.ts new file mode 100644 index 0000000000000..5687c0c15b03b --- /dev/null +++ b/x-pack/plugins/monitoring/server/lib/alerts/map_legacy_severity.ts @@ -0,0 +1,14 @@ +/* + * 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 { AlertSeverity } from '../../../common/enums'; + +export function mapLegacySeverity(severity: number) { + const floor = Math.floor(severity / 1000); + if (floor <= 1) { + return AlertSeverity.Warning; + } + return AlertSeverity.Danger; +} diff --git a/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_from_request.js b/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_from_request.js index 5ed8d6b01aba5..50a4df8a3ff57 100644 --- a/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_from_request.js +++ b/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_from_request.js @@ -13,13 +13,10 @@ import { getKibanasForClusters } from '../kibana'; import { getLogstashForClusters } from '../logstash'; import { getLogstashPipelineIds } from '../logstash/get_pipeline_ids'; import { getBeatsForClusters } from '../beats'; -import { alertsClustersAggregation } from '../../cluster_alerts/alerts_clusters_aggregation'; -import { alertsClusterSearch } from '../../cluster_alerts/alerts_cluster_search'; +import { verifyMonitoringLicense } from '../../cluster_alerts/verify_monitoring_license'; import { checkLicense as checkLicenseForAlerts } from '../../cluster_alerts/check_license'; -import { fetchStatus } from '../alerts/fetch_status'; import { getClustersSummary } from './get_clusters_summary'; import { - CLUSTER_ALERTS_SEARCH_SIZE, STANDALONE_CLUSTER_CLUSTER_UUID, CODE_PATH_ML, CODE_PATH_ALERTS, @@ -28,12 +25,11 @@ import { CODE_PATH_LOGSTASH, CODE_PATH_BEATS, CODE_PATH_APM, - KIBANA_ALERTING_ENABLED, - ALERT_TYPES, } from '../../../common/constants'; import { getApmsForClusters } from '../apm/get_apms_for_clusters'; import { i18n } from '@kbn/i18n'; import { checkCcrEnabled } from '../elasticsearch/ccr'; +import { fetchStatus } from '../alerts/fetch_status'; import { getStandaloneClusterDefinition, hasStandaloneClusters } from '../standalone_clusters'; import { getLogTypes } from '../logs'; import { isInCodePath } from './is_in_code_path'; @@ -52,7 +48,6 @@ export async function getClustersFromRequest( lsIndexPattern, beatsIndexPattern, apmIndexPattern, - alertsIndex, filebeatIndexPattern, } = indexPatterns; @@ -101,25 +96,6 @@ export async function getClustersFromRequest( cluster.ml = { jobs: mlJobs }; } - if (isInCodePath(codePaths, [CODE_PATH_ALERTS])) { - if (KIBANA_ALERTING_ENABLED) { - const alertsClient = req.getAlertsClient ? req.getAlertsClient() : null; - cluster.alerts = await fetchStatus(alertsClient, ALERT_TYPES, start, end, req.logger); - } else { - cluster.alerts = await alertsClusterSearch( - req, - alertsIndex, - cluster, - checkLicenseForAlerts, - { - start, - end, - size: CLUSTER_ALERTS_SEARCH_SIZE, - } - ); - } - } - cluster.logs = isInCodePath(codePaths, [CODE_PATH_LOGS]) ? await getLogTypes(req, filebeatIndexPattern, { clusterUuid: cluster.cluster_uuid, @@ -141,21 +117,67 @@ export async function getClustersFromRequest( // add alerts data if (isInCodePath(codePaths, [CODE_PATH_ALERTS])) { - const clustersAlerts = await alertsClustersAggregation( - req, - alertsIndex, - clusters, - checkLicenseForAlerts - ); - clusters.forEach((cluster) => { + const alertsClient = req.getAlertsClient(); + for (const cluster of clusters) { + const verification = verifyMonitoringLicense(req.server); + if (!verification.enabled) { + // return metadata detailing that alerts is disabled because of the monitoring cluster license + cluster.alerts = { + alertsMeta: { + enabled: verification.enabled, + message: verification.message, // NOTE: this is only defined when the alert feature is disabled + }, + list: {}, + }; + continue; + } + + // check the license type of the production cluster for alerts feature support + const license = cluster.license || {}; + const prodLicenseInfo = checkLicenseForAlerts( + license.type, + license.status === 'active', + 'production' + ); + if (prodLicenseInfo.clusterAlerts.enabled) { + cluster.alerts = { + list: await fetchStatus( + alertsClient, + req.server.plugins.monitoring.info, + undefined, + cluster.cluster_uuid, + start, + end, + [] + ), + alertsMeta: { + enabled: true, + }, + }; + continue; + } + cluster.alerts = { + list: {}, alertsMeta: { - enabled: clustersAlerts.alertsMeta.enabled, - message: clustersAlerts.alertsMeta.message, // NOTE: this is only defined when the alert feature is disabled + enabled: true, + }, + clusterMeta: { + enabled: false, + message: i18n.translate( + 'xpack.monitoring.clusterAlerts.unsupportedClusterAlertsDescription', + { + defaultMessage: + 'Cluster [{clusterName}] license type [{licenseType}] does not support Cluster Alerts', + values: { + clusterName: cluster.cluster_name, + licenseType: `${license.type}`, + }, + } + ), }, - ...clustersAlerts[cluster.cluster_uuid], }; - }); + } } } diff --git a/x-pack/plugins/monitoring/server/lib/errors/handle_error.js b/x-pack/plugins/monitoring/server/lib/errors/handle_error.js index d6549a8fa98e9..4726020210ce7 100644 --- a/x-pack/plugins/monitoring/server/lib/errors/handle_error.js +++ b/x-pack/plugins/monitoring/server/lib/errors/handle_error.js @@ -9,7 +9,7 @@ import { isKnownError, handleKnownError } from './known_errors'; import { isAuthError, handleAuthError } from './auth_errors'; export function handleError(err, req) { - req.logger.error(err); + req && req.logger && req.logger.error(err); // specially handle auth errors if (isAuthError(err)) { diff --git a/x-pack/plugins/monitoring/server/license_service.ts b/x-pack/plugins/monitoring/server/license_service.ts index 7dcdf8897f6a1..fb45abc22afa4 100644 --- a/x-pack/plugins/monitoring/server/license_service.ts +++ b/x-pack/plugins/monitoring/server/license_service.ts @@ -46,7 +46,7 @@ export class LicenseService { license$, getMessage: () => rawLicense?.getUnavailableReason() || 'N/A', getMonitoringFeature: () => rawLicense?.getFeature('monitoring') || defaultLicenseFeature, - getWatcherFeature: () => rawLicense?.getFeature('monitoring') || defaultLicenseFeature, + getWatcherFeature: () => rawLicense?.getFeature('watcher') || defaultLicenseFeature, getSecurityFeature: () => rawLicense?.getFeature('security') || defaultLicenseFeature, stop: () => { if (licenseSubscription) { diff --git a/x-pack/plugins/monitoring/server/plugin.ts b/x-pack/plugins/monitoring/server/plugin.ts index 7c346e007da23..5f358badde401 100644 --- a/x-pack/plugins/monitoring/server/plugin.ts +++ b/x-pack/plugins/monitoring/server/plugin.ts @@ -9,8 +9,6 @@ import { first, map } from 'rxjs/operators'; import { i18n } from '@kbn/i18n'; import { has, get } from 'lodash'; import { TypeOf } from '@kbn/config-schema'; -import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; -import { TelemetryCollectionManagerPluginSetup } from 'src/plugins/telemetry_collection_manager/server'; import { Logger, PluginInitializerContext, @@ -20,15 +18,12 @@ import { CoreSetup, ILegacyCustomClusterClient, CoreStart, - IRouter, - ILegacyClusterClient, CustomHttpResponseOptions, ResponseError, } from 'kibana/server'; import { LOGGING_TAG, KIBANA_MONITORING_LOGGING_TAG, - KIBANA_ALERTING_ENABLED, KIBANA_STATS_TYPE_MONITORING, } from '../common/constants'; import { MonitoringConfig, createConfig, configSchema } from './config'; @@ -41,56 +36,18 @@ import { initInfraSource } from './lib/logs/init_infra_source'; import { instantiateClient } from './es_client/instantiate_client'; import { registerCollectors } from './kibana_monitoring/collectors'; import { registerMonitoringCollection } from './telemetry_collection'; -import { LicensingPluginSetup } from '../../licensing/server'; -import { PluginSetupContract as FeaturesPluginSetupContract } from '../../features/server'; import { LicenseService } from './license_service'; -import { MonitoringLicenseService } from './types'; +import { AlertsFactory } from './alerts'; import { - PluginStartContract as AlertingPluginStartContract, - PluginSetupContract as AlertingPluginSetupContract, -} from '../../alerts/server'; -import { getLicenseExpiration } from './alerts/license_expiration'; -import { getClusterState } from './alerts/cluster_state'; -import { InfraPluginSetup } from '../../infra/server'; - -export interface LegacyAPI { - getServerStatus: () => string; -} - -interface PluginsSetup { - telemetryCollectionManager?: TelemetryCollectionManagerPluginSetup; - usageCollection?: UsageCollectionSetup; - licensing: LicensingPluginSetup; - features: FeaturesPluginSetupContract; - alerts: AlertingPluginSetupContract; - infra: InfraPluginSetup; -} - -interface PluginsStart { - alerts: AlertingPluginStartContract; -} - -interface MonitoringCoreConfig { - get: (key: string) => string | undefined; -} - -interface MonitoringCore { - config: () => MonitoringCoreConfig; - log: Logger; - route: (options: any) => void; -} - -interface LegacyShimDependencies { - router: IRouter; - instanceUuid: string; - esDataClient: ILegacyClusterClient; - kibanaStatsCollector: any; -} - -interface IBulkUploader { - setKibanaStatusGetter: (getter: () => string | undefined) => void; - getKibanaStats: () => any; -} + MonitoringCore, + MonitoringLicenseService, + LegacyShimDependencies, + IBulkUploader, + PluginsSetup, + PluginsStart, + LegacyAPI, + LegacyRequest, +} from './types'; // This is used to test the version of kibana const snapshotRegex = /-snapshot/i; @@ -131,8 +88,9 @@ export class Plugin { .pipe(first()) .toPromise(); + const router = core.http.createRouter(); this.legacyShimDependencies = { - router: core.http.createRouter(), + router, instanceUuid: core.uuid.getInstanceUuid(), esDataClient: core.elasticsearch.legacy.client, kibanaStatsCollector: plugins.usageCollection?.getCollectorByType( @@ -158,29 +116,20 @@ export class Plugin { }); await this.licenseService.refresh(); - if (KIBANA_ALERTING_ENABLED) { - plugins.alerts.registerType( - getLicenseExpiration( - async () => { - const coreStart = (await core.getStartServices())[0]; - return coreStart.uiSettings; - }, - cluster, - this.getLogger, - config.ui.ccs.enabled - ) - ); - plugins.alerts.registerType( - getClusterState( - async () => { - const coreStart = (await core.getStartServices())[0]; - return coreStart.uiSettings; - }, - cluster, - this.getLogger, - config.ui.ccs.enabled - ) - ); + const serverInfo = core.http.getServerInfo(); + let kibanaUrl = `${serverInfo.protocol}://${serverInfo.hostname}:${serverInfo.port}`; + if (core.http.basePath.serverBasePath) { + kibanaUrl += `/${core.http.basePath.serverBasePath}`; + } + const getUiSettingsService = async () => { + const coreStart = (await core.getStartServices())[0]; + return coreStart.uiSettings; + }; + + const alerts = AlertsFactory.getAll(); + for (const alert of alerts) { + alert.initializeAlertType(getUiSettingsService, cluster, this.getLogger, config, kibanaUrl); + plugins.alerts.registerType(alert.getAlertType()); } // Initialize telemetry @@ -200,7 +149,6 @@ export class Plugin { const kibanaCollectionEnabled = config.kibana.collection.enabled; if (kibanaCollectionEnabled) { // Start kibana internal collection - const serverInfo = core.http.getServerInfo(); const bulkUploader = (this.bulkUploader = initBulkUploader({ elasticsearch: core.elasticsearch, config, @@ -252,7 +200,10 @@ export class Plugin { ); this.registerPluginInUI(plugins); - requireUIRoutes(this.monitoringCore); + requireUIRoutes(this.monitoringCore, { + router, + licenseService: this.licenseService, + }); initInfraSource(config, plugins.infra); } @@ -353,14 +304,16 @@ export class Plugin { res: KibanaResponseFactory ) => { const plugins = (await getCoreServices())[1]; - const legacyRequest = { + const legacyRequest: LegacyRequest = { ...req, logger: this.log, getLogger: this.getLogger, payload: req.body, getKibanaStatsCollector: () => this.legacyShimDependencies.kibanaStatsCollector, getUiSettingsService: () => context.core.uiSettings.client, + getActionTypeRegistry: () => context.actions?.listTypes(), getAlertsClient: () => plugins.alerts.getAlertsClientWithRequest(req), + getActionsClient: () => plugins.actions.getActionsClientWithRequest(req), server: { config: legacyConfigWrapper, newPlatform: { @@ -388,7 +341,8 @@ export class Plugin { const result = await options.handler(legacyRequest); return res.ok({ body: result }); } catch (err) { - const statusCode: number = err.output?.statusCode || err.statusCode || err.status; + const statusCode: number = + err.output?.statusCode || err.statusCode || err.status || 500; if (Boom.isBoom(err) || statusCode !== 500) { return res.customError({ statusCode, body: err }); } diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/alerts/alerts.js b/x-pack/plugins/monitoring/server/routes/api/v1/alerts/alerts.js deleted file mode 100644 index d5a43d32f600a..0000000000000 --- a/x-pack/plugins/monitoring/server/routes/api/v1/alerts/alerts.js +++ /dev/null @@ -1,140 +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 { schema } from '@kbn/config-schema'; -import { isFunction } from 'lodash'; -import { - ALERT_TYPE_LICENSE_EXPIRATION, - ALERT_TYPE_CLUSTER_STATE, - MONITORING_CONFIG_ALERTING_EMAIL_ADDRESS, - ALERT_TYPES, -} from '../../../../../common/constants'; -import { handleError } from '../../../../lib/errors'; -import { fetchStatus } from '../../../../lib/alerts/fetch_status'; - -async function createAlerts(req, alertsClient, { selectedEmailActionId }) { - const createdAlerts = []; - - // Create alerts - const ALERT_TYPES = { - [ALERT_TYPE_LICENSE_EXPIRATION]: { - schedule: { interval: '1m' }, - actions: [ - { - group: 'default', - id: selectedEmailActionId, - params: { - subject: '{{context.subject}}', - message: `{{context.message}}`, - to: ['{{context.to}}'], - }, - }, - ], - }, - [ALERT_TYPE_CLUSTER_STATE]: { - schedule: { interval: '1m' }, - actions: [ - { - group: 'default', - id: selectedEmailActionId, - params: { - subject: '{{context.subject}}', - message: `{{context.message}}`, - to: ['{{context.to}}'], - }, - }, - ], - }, - }; - - for (const alertTypeId of Object.keys(ALERT_TYPES)) { - const existingAlert = await alertsClient.find({ - options: { - search: alertTypeId, - }, - }); - if (existingAlert.total === 1) { - await alertsClient.delete({ id: existingAlert.data[0].id }); - } - - const result = await alertsClient.create({ - data: { - enabled: true, - alertTypeId, - ...ALERT_TYPES[alertTypeId], - }, - }); - createdAlerts.push(result); - } - - return createdAlerts; -} - -async function saveEmailAddress(emailAddress, uiSettingsService) { - await uiSettingsService.set(MONITORING_CONFIG_ALERTING_EMAIL_ADDRESS, emailAddress); -} - -export function createKibanaAlertsRoute(server) { - server.route({ - method: 'POST', - path: '/api/monitoring/v1/alerts', - config: { - validate: { - payload: schema.object({ - selectedEmailActionId: schema.string(), - emailAddress: schema.string(), - }), - }, - }, - async handler(req, headers) { - const { emailAddress, selectedEmailActionId } = req.payload; - const alertsClient = isFunction(req.getAlertsClient) ? req.getAlertsClient() : null; - if (!alertsClient) { - return headers.response().code(404); - } - - const [alerts, emailResponse] = await Promise.all([ - createAlerts(req, alertsClient, { ...req.params, selectedEmailActionId }), - saveEmailAddress(emailAddress, req.getUiSettingsService()), - ]); - - return { alerts, emailResponse }; - }, - }); - - server.route({ - method: 'POST', - path: '/api/monitoring/v1/alert_status', - config: { - validate: { - payload: schema.object({ - timeRange: schema.object({ - min: schema.string(), - max: schema.string(), - }), - }), - }, - }, - async handler(req, headers) { - const alertsClient = isFunction(req.getAlertsClient) ? req.getAlertsClient() : null; - if (!alertsClient) { - return headers.response().code(404); - } - - const start = req.payload.timeRange.min; - const end = req.payload.timeRange.max; - let alerts; - - try { - alerts = await fetchStatus(alertsClient, ALERT_TYPES, start, end, req.logger); - } catch (err) { - throw handleError(err, req); - } - - return { alerts }; - }, - }); -} diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/alerts/enable.ts b/x-pack/plugins/monitoring/server/routes/api/v1/alerts/enable.ts new file mode 100644 index 0000000000000..1d83644fce756 --- /dev/null +++ b/x-pack/plugins/monitoring/server/routes/api/v1/alerts/enable.ts @@ -0,0 +1,73 @@ +/* + * 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. + */ + +// @ts-ignore +import { handleError } from '../../../../lib/errors'; +import { AlertsFactory } from '../../../../alerts'; +import { RouteDependencies } from '../../../../types'; +import { ALERT_ACTION_TYPE_LOG } from '../../../../../common/constants'; +import { ActionResult } from '../../../../../../actions/common'; +// import { fetchDefaultEmailAddress } from '../../../../lib/alerts/fetch_default_email_address'; + +const DEFAULT_SERVER_LOG_NAME = 'Monitoring: Write to Kibana log'; + +export function enableAlertsRoute(server: any, npRoute: RouteDependencies) { + npRoute.router.post( + { + path: '/api/monitoring/v1/alerts/enable', + options: { tags: ['access:monitoring'] }, + validate: false, + }, + async (context, request, response) => { + try { + const alertsClient = context.alerting?.getAlertsClient(); + const actionsClient = context.actions?.getActionsClient(); + const types = context.actions?.listTypes(); + if (!alertsClient || !actionsClient || !types) { + return response.notFound(); + } + + // Get or create the default log action + let serverLogAction; + const allActions = await actionsClient.getAll(); + for (const action of allActions) { + if (action.name === DEFAULT_SERVER_LOG_NAME) { + serverLogAction = action as ActionResult; + break; + } + } + + if (!serverLogAction) { + serverLogAction = await actionsClient.create({ + action: { + name: DEFAULT_SERVER_LOG_NAME, + actionTypeId: ALERT_ACTION_TYPE_LOG, + config: {}, + secrets: {}, + }, + }); + } + + const actions = [ + { + id: serverLogAction.id, + config: {}, + }, + ]; + + const alerts = AlertsFactory.getAll().filter((a) => a.isEnabled(npRoute.licenseService)); + const createdAlerts = await Promise.all( + alerts.map( + async (alert) => await alert.createIfDoesNotExist(alertsClient, actionsClient, actions) + ) + ); + return response.ok({ body: createdAlerts }); + } catch (err) { + throw handleError(err); + } + } + ); +} diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/alerts/index.js b/x-pack/plugins/monitoring/server/routes/api/v1/alerts/index.js index 246cdfde97cff..a41562dd29a88 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/alerts/index.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/alerts/index.js @@ -4,5 +4,5 @@ * you may not use this file except in compliance with the Elastic License. */ -export * from './legacy_alerts'; -export * from './alerts'; +export { enableAlertsRoute } from './enable'; +export { alertStatusRoute } from './status'; diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/alerts/legacy_alerts.js b/x-pack/plugins/monitoring/server/routes/api/v1/alerts/legacy_alerts.js deleted file mode 100644 index 688caac9b60b1..0000000000000 --- a/x-pack/plugins/monitoring/server/routes/api/v1/alerts/legacy_alerts.js +++ /dev/null @@ -1,57 +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 { schema } from '@kbn/config-schema'; -import { alertsClusterSearch } from '../../../../cluster_alerts/alerts_cluster_search'; -import { checkLicense } from '../../../../cluster_alerts/check_license'; -import { getClusterLicense } from '../../../../lib/cluster/get_cluster_license'; -import { prefixIndexPattern } from '../../../../lib/ccs_utils'; -import { INDEX_PATTERN_ELASTICSEARCH, INDEX_ALERTS } from '../../../../../common/constants'; - -/* - * Cluster Alerts route. - */ -export function legacyClusterAlertsRoute(server) { - server.route({ - method: 'POST', - path: '/api/monitoring/v1/clusters/{clusterUuid}/legacy_alerts', - config: { - validate: { - params: schema.object({ - clusterUuid: schema.string(), - }), - payload: schema.object({ - ccs: schema.maybe(schema.string()), - timeRange: schema.object({ - min: schema.string(), - max: schema.string(), - }), - }), - }, - }, - handler(req) { - const config = server.config(); - const ccs = req.payload.ccs; - const clusterUuid = req.params.clusterUuid; - const esIndexPattern = prefixIndexPattern(config, INDEX_PATTERN_ELASTICSEARCH, ccs); - const alertsIndex = prefixIndexPattern(config, INDEX_ALERTS, ccs); - const options = { - start: req.payload.timeRange.min, - end: req.payload.timeRange.max, - }; - - return getClusterLicense(req, esIndexPattern, clusterUuid).then((license) => - alertsClusterSearch( - req, - alertsIndex, - { cluster_uuid: clusterUuid, license }, - checkLicense, - options - ) - ); - }, - }); -} diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/alerts/status.ts b/x-pack/plugins/monitoring/server/routes/api/v1/alerts/status.ts new file mode 100644 index 0000000000000..eef99bbc4ac68 --- /dev/null +++ b/x-pack/plugins/monitoring/server/routes/api/v1/alerts/status.ts @@ -0,0 +1,61 @@ +/* + * 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 } from '@kbn/config-schema'; +// @ts-ignore +import { handleError } from '../../../../lib/errors'; +import { RouteDependencies } from '../../../../types'; +import { fetchStatus } from '../../../../lib/alerts/fetch_status'; +import { CommonAlertFilter } from '../../../../../common/types'; + +export function alertStatusRoute(server: any, npRoute: RouteDependencies) { + npRoute.router.post( + { + path: '/api/monitoring/v1/alert/{clusterUuid}/status', + options: { tags: ['access:monitoring'] }, + validate: { + params: schema.object({ + clusterUuid: schema.string(), + }), + body: schema.object({ + alertTypeIds: schema.maybe(schema.arrayOf(schema.string())), + filters: schema.maybe(schema.arrayOf(schema.any())), + timeRange: schema.object({ + min: schema.number(), + max: schema.number(), + }), + }), + }, + }, + async (context, request, response) => { + try { + const { clusterUuid } = request.params; + const { + alertTypeIds, + timeRange: { min, max }, + filters, + } = request.body; + const alertsClient = context.alerting?.getAlertsClient(); + if (!alertsClient) { + return response.notFound(); + } + + const status = await fetchStatus( + alertsClient, + npRoute.licenseService, + alertTypeIds, + clusterUuid, + min, + max, + filters as CommonAlertFilter[] + ); + return response.ok({ body: status }); + } catch (err) { + throw handleError(err); + } + } + ); +} diff --git a/x-pack/plugins/monitoring/server/routes/index.js b/x-pack/plugins/monitoring/server/routes/index.ts similarity index 67% rename from x-pack/plugins/monitoring/server/routes/index.js rename to x-pack/plugins/monitoring/server/routes/index.ts index 0aefed4d9a507..69ded6ad5a5f0 100644 --- a/x-pack/plugins/monitoring/server/routes/index.js +++ b/x-pack/plugins/monitoring/server/routes/index.ts @@ -4,14 +4,16 @@ * you may not use this file except in compliance with the Elastic License. */ -/*eslint import/namespace: ['error', { allowComputed: true }]*/ +/* eslint import/namespace: ['error', { allowComputed: true }]*/ +// @ts-ignore import * as uiRoutes from './api/v1/ui'; // namespace import +import { RouteDependencies } from '../types'; -export function requireUIRoutes(server) { +export function requireUIRoutes(server: any, npRoute: RouteDependencies) { const routes = Object.keys(uiRoutes); routes.forEach((route) => { const registerRoute = uiRoutes[route]; // computed reference to module objects imported via namespace - registerRoute(server); + registerRoute(server, npRoute); }); } diff --git a/x-pack/plugins/monitoring/server/types.ts b/x-pack/plugins/monitoring/server/types.ts index 9b3725d007fd9..0c346c8082475 100644 --- a/x-pack/plugins/monitoring/server/types.ts +++ b/x-pack/plugins/monitoring/server/types.ts @@ -4,7 +4,18 @@ * you may not use this file except in compliance with the Elastic License. */ import { Observable } from 'rxjs'; +import { IRouter, ILegacyClusterClient, Logger } from 'kibana/server'; +import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; +import { TelemetryCollectionManagerPluginSetup } from 'src/plugins/telemetry_collection_manager/server'; import { LicenseFeature, ILicense } from '../../licensing/server'; +import { PluginStartContract as ActionsPluginsStartContact } from '../../actions/server'; +import { + PluginStartContract as AlertingPluginStartContract, + PluginSetupContract as AlertingPluginSetupContract, +} from '../../alerts/server'; +import { InfraPluginSetup } from '../../infra/server'; +import { LicensingPluginSetup } from '../../licensing/server'; +import { PluginSetupContract as FeaturesPluginSetupContract } from '../../features/server'; export interface MonitoringLicenseService { refresh: () => Promise; @@ -15,3 +26,85 @@ export interface MonitoringLicenseService { getSecurityFeature: () => LicenseFeature; stop: () => void; } + +export interface MonitoringElasticsearchConfig { + hosts: string[]; +} + +export interface LegacyAPI { + getServerStatus: () => string; +} + +export interface PluginsSetup { + telemetryCollectionManager?: TelemetryCollectionManagerPluginSetup; + usageCollection?: UsageCollectionSetup; + licensing: LicensingPluginSetup; + features: FeaturesPluginSetupContract; + alerts: AlertingPluginSetupContract; + infra: InfraPluginSetup; +} + +export interface PluginsStart { + alerts: AlertingPluginStartContract; + actions: ActionsPluginsStartContact; +} + +export interface MonitoringCoreConfig { + get: (key: string) => string | undefined; +} + +export interface RouteDependencies { + router: IRouter; + licenseService: MonitoringLicenseService; +} + +export interface MonitoringCore { + config: () => MonitoringCoreConfig; + log: Logger; + route: (options: any) => void; +} + +export interface LegacyShimDependencies { + router: IRouter; + instanceUuid: string; + esDataClient: ILegacyClusterClient; + kibanaStatsCollector: any; +} + +export interface IBulkUploader { + setKibanaStatusGetter: (getter: () => string | undefined) => void; + getKibanaStats: () => any; +} + +export interface LegacyRequest { + logger: Logger; + getLogger: (...scopes: string[]) => Logger; + payload: unknown; + getKibanaStatsCollector: () => any; + getUiSettingsService: () => any; + getActionTypeRegistry: () => any; + getAlertsClient: () => any; + getActionsClient: () => any; + server: { + config: () => { + get: (key: string) => string | undefined; + }; + newPlatform: { + setup: { + plugins: PluginsStart; + }; + }; + plugins: { + monitoring: { + info: MonitoringLicenseService; + }; + elasticsearch: { + getCluster: ( + name: string + ) => { + callWithRequest: (req: any, endpoint: string, params: any) => Promise; + }; + }; + }; + }; +} diff --git a/x-pack/plugins/observability/public/application/index.tsx b/x-pack/plugins/observability/public/application/index.tsx index 5bc8d96656ed4..8cfbca37e8d05 100644 --- a/x-pack/plugins/observability/public/application/index.tsx +++ b/x-pack/plugins/observability/public/application/index.tsx @@ -12,7 +12,7 @@ import { RedirectAppLinks } from '../../../../../src/plugins/kibana_react/public import { AppMountParameters, CoreStart } from '../../../../../src/core/public'; import { EuiThemeProvider } from '../../../../legacy/common/eui_styled_components'; import { PluginContext } from '../context/plugin_context'; -import { useUrlParams } from '../hooks/use_url_params'; +import { useRouteParams } from '../hooks/use_route_params'; import { routes } from '../routes'; import { usePluginContext } from '../hooks/use_plugin_context'; @@ -36,7 +36,7 @@ const App = () => { ]); }, [core]); - const { query, path: pathParams } = useUrlParams(route.params); + const { query, path: pathParams } = useRouteParams(route.params); return route.handler({ query, path: pathParams }); }; return ; diff --git a/x-pack/plugins/observability/public/components/app/ingest_manager_panel/index.tsx b/x-pack/plugins/observability/public/components/app/ingest_manager_panel/index.tsx new file mode 100644 index 0000000000000..f7a1deb83fbe4 --- /dev/null +++ b/x-pack/plugins/observability/public/components/app/ingest_manager_panel/index.tsx @@ -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 React from 'react'; +import { EuiPanel } from '@elastic/eui'; +import { EuiFlexGroup } from '@elastic/eui'; +import { EuiFlexItem } from '@elastic/eui'; +import { EuiTitle } from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; +import { EuiText } from '@elastic/eui'; +import { EuiLink } from '@elastic/eui'; + +export const IngestManagerPanel = () => { + return ( + + + + +

+ {i18n.translate('xpack.observability.ingestManafer.title', { + defaultMessage: 'Have you seen our new Ingest Manager?', + })} +

+
+
+ + + {i18n.translate('xpack.observability.ingestManafer.text', { + defaultMessage: + 'The Elastic Agent provides a simple, unified way to add monitoring for logs, metrics, and other types of data to your hosts. You no longer need to install multiple Beats and other agents, making it easier and faster to deploy configurations across your infrastructure.', + })} + + + + + {i18n.translate('xpack.observability.ingestManafer.button', { + defaultMessage: 'Try Ingest Manager Beta', + })} + + +
+
+ ); +}; diff --git a/x-pack/plugins/observability/public/data_handler.ts b/x-pack/plugins/observability/public/data_handler.ts index d7f8c471ad9aa..73e34f214da28 100644 --- a/x-pack/plugins/observability/public/data_handler.ts +++ b/x-pack/plugins/observability/public/data_handler.ts @@ -31,6 +31,6 @@ export function getDataHandler(appName: T) { export async function fetchHasData() { const apps: ObservabilityApp[] = ['apm', 'uptime', 'infra_logs', 'infra_metrics']; const promises = apps.map((app) => getDataHandler(app)?.hasData()); - const [apm, uptime, logs, metrics] = await Promise.all(promises); + const [apm, uptime, logs, metrics] = await Promise.allSettled(promises); return { apm, uptime, infra_logs: logs, infra_metrics: metrics }; } diff --git a/x-pack/plugins/observability/public/hooks/use_url_params.tsx b/x-pack/plugins/observability/public/hooks/use_route_params.tsx similarity index 97% rename from x-pack/plugins/observability/public/hooks/use_url_params.tsx rename to x-pack/plugins/observability/public/hooks/use_route_params.tsx index 680a32fb49677..93a79bfda7fc1 100644 --- a/x-pack/plugins/observability/public/hooks/use_url_params.tsx +++ b/x-pack/plugins/observability/public/hooks/use_route_params.tsx @@ -23,7 +23,7 @@ function getQueryParams(location: ReturnType) { * It removes any aditional item which is not declared in the type. * @param params */ -export function useUrlParams(params: Params) { +export function useRouteParams(params: Params) { const location = useLocation(); const pathParams = useParams(); const queryParams = getQueryParams(location); diff --git a/x-pack/plugins/observability/public/pages/landing/index.tsx b/x-pack/plugins/observability/public/pages/landing/index.tsx index 512f4428d9bf2..da46791d9e855 100644 --- a/x-pack/plugins/observability/public/pages/landing/index.tsx +++ b/x-pack/plugins/observability/public/pages/landing/index.tsx @@ -22,6 +22,7 @@ import styled, { ThemeContext } from 'styled-components'; import { WithHeaderLayout } from '../../components/app/layout/with_header'; import { usePluginContext } from '../../hooks/use_plugin_context'; import { appsSection } from '../home/section'; +import { IngestManagerPanel } from '../../components/app/ingest_manager_panel'; const EuiCardWithoutPadding = styled(EuiCard)` padding: 0; @@ -112,6 +113,16 @@ export const LandingPage = () => {
+ + + + + + + + + + ); diff --git a/x-pack/plugins/observability/public/plugin.ts b/x-pack/plugins/observability/public/plugin.ts index bbda1026606f1..335ce897dce7b 100644 --- a/x-pack/plugins/observability/public/plugin.ts +++ b/x-pack/plugins/observability/public/plugin.ts @@ -9,8 +9,10 @@ import { DEFAULT_APP_CATEGORIES, Plugin as PluginClass, PluginInitializerContext, + CoreStart, } from '../../../../src/core/public'; import { registerDataHandler } from './data_handler'; +import { toggleOverviewLinkInNav } from './toggle_overview_link_in_nav'; export interface ObservabilityPluginSetup { dashboard: { register: typeof registerDataHandler }; @@ -43,5 +45,7 @@ export class Plugin implements PluginClass path }; + describe('getObservabilityAlerts', () => { it('Returns empty array when api throws exception', async () => { const core = ({ @@ -14,6 +16,7 @@ describe('getObservabilityAlerts', () => { get: async () => { throw new Error('Boom'); }, + basePath, }, } as unknown) as AppMountContext['core']; @@ -29,6 +32,7 @@ describe('getObservabilityAlerts', () => { data: undefined, }; }, + basePath, }, } as unknown) as AppMountContext['core']; @@ -65,6 +69,7 @@ describe('getObservabilityAlerts', () => { ], }; }, + basePath, }, } as unknown) as AppMountContext['core']; diff --git a/x-pack/plugins/observability/public/services/get_observability_alerts.ts b/x-pack/plugins/observability/public/services/get_observability_alerts.ts index 49855a30c16f6..58ff9c92acbff 100644 --- a/x-pack/plugins/observability/public/services/get_observability_alerts.ts +++ b/x-pack/plugins/observability/public/services/get_observability_alerts.ts @@ -9,12 +9,15 @@ import { Alert } from '../../../alerts/common'; export async function getObservabilityAlerts({ core }: { core: AppMountContext['core'] }) { try { - const { data = [] }: { data: Alert[] } = await core.http.get('/api/alerts/_find', { - query: { - page: 1, - per_page: 20, - }, - }); + const { data = [] }: { data: Alert[] } = await core.http.get( + core.http.basePath.prepend('/api/alerts/_find'), + { + query: { + page: 1, + per_page: 20, + }, + } + ); return data.filter(({ consumer }) => { return ( diff --git a/x-pack/plugins/observability/public/toggle_overview_link_in_nav.test.tsx b/x-pack/plugins/observability/public/toggle_overview_link_in_nav.test.tsx new file mode 100644 index 0000000000000..d3218e6f00bd2 --- /dev/null +++ b/x-pack/plugins/observability/public/toggle_overview_link_in_nav.test.tsx @@ -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 { CoreStart } from 'kibana/public'; + +import { toggleOverviewLinkInNav } from './toggle_overview_link_in_nav'; + +describe('toggleOverviewLinkInNav', () => { + const update = jest.fn(); + afterEach(() => { + update.mockClear(); + }); + it('hides overview menu', () => { + const core = ({ + application: { + capabilities: { + navLinks: { + apm: false, + logs: false, + metrics: false, + uptime: false, + }, + }, + }, + chrome: { navLinks: { update } }, + } as unknown) as CoreStart; + toggleOverviewLinkInNav(core); + expect(update).toHaveBeenCalledWith('observability-overview', { hidden: true }); + }); + it('shows overview menu', () => { + const core = ({ + application: { + capabilities: { + navLinks: { + apm: true, + logs: false, + metrics: false, + uptime: false, + }, + }, + }, + chrome: { navLinks: { update } }, + } as unknown) as CoreStart; + toggleOverviewLinkInNav(core); + expect(update).not.toHaveBeenCalled(); + }); +}); diff --git a/x-pack/plugins/observability/public/toggle_overview_link_in_nav.tsx b/x-pack/plugins/observability/public/toggle_overview_link_in_nav.tsx new file mode 100644 index 0000000000000..c33ca45e4fcd8 --- /dev/null +++ b/x-pack/plugins/observability/public/toggle_overview_link_in_nav.tsx @@ -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 { CoreStart } from 'kibana/public'; + +export function toggleOverviewLinkInNav(core: CoreStart) { + const { apm, logs, metrics, uptime } = core.application.capabilities.navLinks; + const someVisible = Object.values({ apm, logs, metrics, uptime }).some((visible) => visible); + if (!someVisible) { + core.chrome.navLinks.update('observability-overview', { hidden: true }); + } +} diff --git a/x-pack/plugins/remote_clusters/public/plugin.ts b/x-pack/plugins/remote_clusters/public/plugin.ts index 8881db0f9196e..33222dd7052e9 100644 --- a/x-pack/plugins/remote_clusters/public/plugin.ts +++ b/x-pack/plugins/remote_clusters/public/plugin.ts @@ -7,7 +7,6 @@ import { i18n } from '@kbn/i18n'; import { CoreSetup, Plugin, CoreStart, PluginInitializerContext } from 'kibana/public'; -import { ManagementSectionId } from '../../../../src/plugins/management/public'; import { init as initBreadcrumbs } from './application/services/breadcrumb'; import { init as initDocumentation } from './application/services/documentation'; import { init as initHttp } from './application/services/http'; @@ -33,7 +32,7 @@ export class RemoteClustersUIPlugin } = this.initializerContext.config.get(); if (isRemoteClustersUiEnabled) { - const esSection = management.sections.getSection(ManagementSectionId.Data); + const esSection = management.sections.section.data; esSection.registerApp({ id: 'remote_clusters', diff --git a/x-pack/plugins/reporting/common/types.ts b/x-pack/plugins/reporting/common/types.ts index 2819c28cfb54f..18b0ac2a72802 100644 --- a/x-pack/plugins/reporting/common/types.ts +++ b/x-pack/plugins/reporting/common/types.ts @@ -7,7 +7,7 @@ // eslint-disable-next-line @kbn/eslint/no-restricted-paths export { ReportingConfigType } from '../server/config'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths -export { LayoutInstance } from '../server/export_types/common/layouts'; +export { LayoutInstance } from '../server/lib/layouts'; export type JobId = string; export type JobStatus = diff --git a/x-pack/plugins/reporting/public/plugin.tsx b/x-pack/plugins/reporting/public/plugin.tsx index 8a25df0a74bbf..d003d4c581699 100644 --- a/x-pack/plugins/reporting/public/plugin.tsx +++ b/x-pack/plugins/reporting/public/plugin.tsx @@ -23,7 +23,7 @@ import { FeatureCatalogueCategory, HomePublicPluginSetup, } from '../../../../src/plugins/home/public'; -import { ManagementSectionId, ManagementSetup } from '../../../../src/plugins/management/public'; +import { ManagementSetup } from '../../../../src/plugins/management/public'; import { SharePluginSetup } from '../../../../src/plugins/share/public'; import { LicensingPluginSetup } from '../../licensing/public'; import { JobId, JobStatusBuckets, ReportingConfigType } from '../common/types'; @@ -115,8 +115,7 @@ export class ReportingPublicPlugin implements Plugin { showOnHomePage: false, category: FeatureCatalogueCategory.ADMIN, }); - - management.sections.getSection(ManagementSectionId.InsightsAndAlerting).registerApp({ + management.sections.section.insightsAndAlerting.registerApp({ id: 'reporting', title: this.title, order: 1, diff --git a/x-pack/plugins/reporting/server/browsers/chromium/driver/chromium_driver.ts b/x-pack/plugins/reporting/server/browsers/chromium/driver/chromium_driver.ts index bca9496bc9add..eb16a9d6de1a8 100644 --- a/x-pack/plugins/reporting/server/browsers/chromium/driver/chromium_driver.ts +++ b/x-pack/plugins/reporting/server/browsers/chromium/driver/chromium_driver.ts @@ -9,8 +9,8 @@ import { map, truncate } from 'lodash'; import open from 'opn'; import { ElementHandle, EvaluateFn, Page, Response, SerializableOrJSHandle } from 'puppeteer'; import { parse as parseUrl } from 'url'; -import { ViewZoomWidthHeight } from '../../../export_types/common/layouts/layout'; import { LevelLogger } from '../../../lib'; +import { ViewZoomWidthHeight } from '../../../lib/layouts/layout'; import { ConditionalHeaders, ElementPosition } from '../../../types'; import { allowRequest, NetworkPolicy } from '../../network_policy'; diff --git a/x-pack/plugins/reporting/server/core.ts b/x-pack/plugins/reporting/server/core.ts index eccd6c7db1698..95dc7586ad4a6 100644 --- a/x-pack/plugins/reporting/server/core.ts +++ b/x-pack/plugins/reporting/server/core.ts @@ -20,7 +20,7 @@ import { SecurityPluginSetup } from '../../security/server'; import { ScreenshotsObservableFn } from '../server/types'; import { ReportingConfig } from './'; import { HeadlessChromiumDriverFactory } from './browsers/chromium/driver_factory'; -import { screenshotsObservableFactory } from './export_types/common/lib/screenshots'; +import { screenshotsObservableFactory } from './lib/screenshots'; import { checkLicense, getExportTypesRegistry } from './lib'; import { ESQueueInstance } from './lib/create_queue'; import { EnqueueJobFn } from './lib/enqueue_job'; diff --git a/x-pack/plugins/reporting/server/export_types/common/execute_job/decrypt_job_headers.test.ts b/x-pack/plugins/reporting/server/export_types/common/decrypt_job_headers.test.ts similarity index 93% rename from x-pack/plugins/reporting/server/export_types/common/execute_job/decrypt_job_headers.test.ts rename to x-pack/plugins/reporting/server/export_types/common/decrypt_job_headers.test.ts index 4998d936c9b16..908817a2ccf81 100644 --- a/x-pack/plugins/reporting/server/export_types/common/execute_job/decrypt_job_headers.test.ts +++ b/x-pack/plugins/reporting/server/export_types/common/decrypt_job_headers.test.ts @@ -4,8 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -import { cryptoFactory, LevelLogger } from '../../../lib'; -import { decryptJobHeaders } from './decrypt_job_headers'; +import { cryptoFactory, LevelLogger } from '../../lib'; +import { decryptJobHeaders } from './'; const encryptHeaders = async (encryptionKey: string, headers: Record) => { const crypto = cryptoFactory(encryptionKey); diff --git a/x-pack/plugins/reporting/server/export_types/common/execute_job/decrypt_job_headers.ts b/x-pack/plugins/reporting/server/export_types/common/decrypt_job_headers.ts similarity index 96% rename from x-pack/plugins/reporting/server/export_types/common/execute_job/decrypt_job_headers.ts rename to x-pack/plugins/reporting/server/export_types/common/decrypt_job_headers.ts index 579b5196ad4d9..845b9adb38be9 100644 --- a/x-pack/plugins/reporting/server/export_types/common/execute_job/decrypt_job_headers.ts +++ b/x-pack/plugins/reporting/server/export_types/common/decrypt_job_headers.ts @@ -5,7 +5,7 @@ */ import { i18n } from '@kbn/i18n'; -import { cryptoFactory, LevelLogger } from '../../../lib'; +import { cryptoFactory, LevelLogger } from '../../lib'; interface HasEncryptedHeaders { headers?: string; diff --git a/x-pack/plugins/reporting/common/get_absolute_url.test.ts b/x-pack/plugins/reporting/server/export_types/common/get_absolute_url.test.ts similarity index 100% rename from x-pack/plugins/reporting/common/get_absolute_url.test.ts rename to x-pack/plugins/reporting/server/export_types/common/get_absolute_url.test.ts diff --git a/x-pack/plugins/reporting/common/get_absolute_url.ts b/x-pack/plugins/reporting/server/export_types/common/get_absolute_url.ts similarity index 100% rename from x-pack/plugins/reporting/common/get_absolute_url.ts rename to x-pack/plugins/reporting/server/export_types/common/get_absolute_url.ts diff --git a/x-pack/plugins/reporting/server/export_types/common/execute_job/get_conditional_headers.test.ts b/x-pack/plugins/reporting/server/export_types/common/get_conditional_headers.test.ts similarity index 93% rename from x-pack/plugins/reporting/server/export_types/common/execute_job/get_conditional_headers.test.ts rename to x-pack/plugins/reporting/server/export_types/common/get_conditional_headers.test.ts index 030ced5dc4b80..0372d515c21a8 100644 --- a/x-pack/plugins/reporting/server/export_types/common/execute_job/get_conditional_headers.test.ts +++ b/x-pack/plugins/reporting/server/export_types/common/get_conditional_headers.test.ts @@ -5,12 +5,12 @@ */ import sinon from 'sinon'; -import { ReportingConfig } from '../../../'; -import { ReportingCore } from '../../../core'; -import { createMockReportingCore } from '../../../test_helpers'; -import { ScheduledTaskParams } from '../../../types'; -import { ScheduledTaskParamsPDF } from '../../printable_pdf/types'; -import { getConditionalHeaders, getCustomLogo } from './index'; +import { ReportingConfig } from '../../'; +import { ReportingCore } from '../../core'; +import { createMockReportingCore } from '../../test_helpers'; +import { ScheduledTaskParams } from '../../types'; +import { ScheduledTaskParamsPDF } from '../printable_pdf/types'; +import { getConditionalHeaders, getCustomLogo } from './'; let mockConfig: ReportingConfig; let mockReportingPlugin: ReportingCore; diff --git a/x-pack/plugins/reporting/server/export_types/common/execute_job/get_conditional_headers.ts b/x-pack/plugins/reporting/server/export_types/common/get_conditional_headers.ts similarity index 91% rename from x-pack/plugins/reporting/server/export_types/common/execute_job/get_conditional_headers.ts rename to x-pack/plugins/reporting/server/export_types/common/get_conditional_headers.ts index 7a50eaac80d85..799d023486832 100644 --- a/x-pack/plugins/reporting/server/export_types/common/execute_job/get_conditional_headers.ts +++ b/x-pack/plugins/reporting/server/export_types/common/get_conditional_headers.ts @@ -4,8 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -import { ReportingConfig } from '../../../'; -import { ConditionalHeaders } from '../../../types'; +import { ReportingConfig } from '../../'; +import { ConditionalHeaders } from '../../types'; export const getConditionalHeaders = ({ config, diff --git a/x-pack/plugins/reporting/server/export_types/common/execute_job/get_custom_logo.test.ts b/x-pack/plugins/reporting/server/export_types/common/get_custom_logo.test.ts similarity index 85% rename from x-pack/plugins/reporting/server/export_types/common/execute_job/get_custom_logo.test.ts rename to x-pack/plugins/reporting/server/export_types/common/get_custom_logo.test.ts index c364752c8dd0f..a3d65a1398a20 100644 --- a/x-pack/plugins/reporting/server/export_types/common/execute_job/get_custom_logo.test.ts +++ b/x-pack/plugins/reporting/server/export_types/common/get_custom_logo.test.ts @@ -4,10 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ -import { ReportingCore } from '../../../core'; -import { createMockReportingCore } from '../../../test_helpers'; -import { ScheduledTaskParamsPDF } from '../../printable_pdf/types'; -import { getConditionalHeaders, getCustomLogo } from './index'; +import { ReportingCore } from '../../core'; +import { createMockReportingCore } from '../../test_helpers'; +import { ScheduledTaskParamsPDF } from '../printable_pdf/types'; +import { getConditionalHeaders, getCustomLogo } from './'; const mockConfigGet = jest.fn().mockImplementation((key: string) => { return 'localhost'; diff --git a/x-pack/plugins/reporting/server/export_types/common/execute_job/get_custom_logo.ts b/x-pack/plugins/reporting/server/export_types/common/get_custom_logo.ts similarity index 82% rename from x-pack/plugins/reporting/server/export_types/common/execute_job/get_custom_logo.ts rename to x-pack/plugins/reporting/server/export_types/common/get_custom_logo.ts index 36c02eb47565c..547cc45258dae 100644 --- a/x-pack/plugins/reporting/server/export_types/common/execute_job/get_custom_logo.ts +++ b/x-pack/plugins/reporting/server/export_types/common/get_custom_logo.ts @@ -4,10 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ -import { ReportingConfig, ReportingCore } from '../../../'; -import { UI_SETTINGS_CUSTOM_PDF_LOGO } from '../../../../common/constants'; -import { ConditionalHeaders } from '../../../types'; -import { ScheduledTaskParamsPDF } from '../../printable_pdf/types'; // Logo is PDF only +import { ReportingConfig, ReportingCore } from '../../'; +import { UI_SETTINGS_CUSTOM_PDF_LOGO } from '../../../common/constants'; +import { ConditionalHeaders } from '../../types'; +import { ScheduledTaskParamsPDF } from '../printable_pdf/types'; // Logo is PDF only export const getCustomLogo = async ({ reporting, diff --git a/x-pack/plugins/reporting/server/export_types/common/execute_job/get_full_urls.test.ts b/x-pack/plugins/reporting/server/export_types/common/get_full_urls.test.ts similarity index 97% rename from x-pack/plugins/reporting/server/export_types/common/execute_job/get_full_urls.test.ts rename to x-pack/plugins/reporting/server/export_types/common/get_full_urls.test.ts index ad952c084d4f3..73d7c7b03c128 100644 --- a/x-pack/plugins/reporting/server/export_types/common/execute_job/get_full_urls.test.ts +++ b/x-pack/plugins/reporting/server/export_types/common/get_full_urls.test.ts @@ -4,9 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { ReportingConfig } from '../../../'; -import { ScheduledTaskParamsPNG } from '../../png/types'; -import { ScheduledTaskParamsPDF } from '../../printable_pdf/types'; +import { ReportingConfig } from '../../'; +import { ScheduledTaskParamsPNG } from '../png/types'; +import { ScheduledTaskParamsPDF } from '../printable_pdf/types'; import { getFullUrls } from './get_full_urls'; interface FullUrlsOpts { diff --git a/x-pack/plugins/reporting/server/export_types/common/execute_job/get_full_urls.ts b/x-pack/plugins/reporting/server/export_types/common/get_full_urls.ts similarity index 90% rename from x-pack/plugins/reporting/server/export_types/common/execute_job/get_full_urls.ts rename to x-pack/plugins/reporting/server/export_types/common/get_full_urls.ts index 67bc8d16fa758..d3362fd190680 100644 --- a/x-pack/plugins/reporting/server/export_types/common/execute_job/get_full_urls.ts +++ b/x-pack/plugins/reporting/server/export_types/common/get_full_urls.ts @@ -10,11 +10,11 @@ import { UrlWithParsedQuery, UrlWithStringQuery, } from 'url'; -import { ReportingConfig } from '../../..'; -import { getAbsoluteUrlFactory } from '../../../../common/get_absolute_url'; -import { validateUrls } from '../../../../common/validate_urls'; -import { ScheduledTaskParamsPNG } from '../../png/types'; -import { ScheduledTaskParamsPDF } from '../../printable_pdf/types'; +import { ReportingConfig } from '../../'; +import { ScheduledTaskParamsPNG } from '../png/types'; +import { ScheduledTaskParamsPDF } from '../printable_pdf/types'; +import { getAbsoluteUrlFactory } from './get_absolute_url'; +import { validateUrls } from './validate_urls'; function isPngJob( job: ScheduledTaskParamsPNG | ScheduledTaskParamsPDF diff --git a/x-pack/plugins/reporting/server/export_types/common/execute_job/index.ts b/x-pack/plugins/reporting/server/export_types/common/index.ts similarity index 91% rename from x-pack/plugins/reporting/server/export_types/common/execute_job/index.ts rename to x-pack/plugins/reporting/server/export_types/common/index.ts index b9d59b2be1296..a4e114d6b2f2e 100644 --- a/x-pack/plugins/reporting/server/export_types/common/execute_job/index.ts +++ b/x-pack/plugins/reporting/server/export_types/common/index.ts @@ -9,3 +9,4 @@ export { getConditionalHeaders } from './get_conditional_headers'; export { getCustomLogo } from './get_custom_logo'; export { getFullUrls } from './get_full_urls'; export { omitBlacklistedHeaders } from './omit_blacklisted_headers'; +export { validateUrls } from './validate_urls'; diff --git a/x-pack/plugins/reporting/server/export_types/common/execute_job/omit_blacklisted_headers.test.ts b/x-pack/plugins/reporting/server/export_types/common/omit_blacklisted_headers.test.ts similarity index 100% rename from x-pack/plugins/reporting/server/export_types/common/execute_job/omit_blacklisted_headers.test.ts rename to x-pack/plugins/reporting/server/export_types/common/omit_blacklisted_headers.test.ts diff --git a/x-pack/plugins/reporting/server/export_types/common/execute_job/omit_blacklisted_headers.ts b/x-pack/plugins/reporting/server/export_types/common/omit_blacklisted_headers.ts similarity index 95% rename from x-pack/plugins/reporting/server/export_types/common/execute_job/omit_blacklisted_headers.ts rename to x-pack/plugins/reporting/server/export_types/common/omit_blacklisted_headers.ts index 305fb6bab5478..e56ffc737764c 100644 --- a/x-pack/plugins/reporting/server/export_types/common/execute_job/omit_blacklisted_headers.ts +++ b/x-pack/plugins/reporting/server/export_types/common/omit_blacklisted_headers.ts @@ -7,7 +7,7 @@ import { omitBy } from 'lodash'; import { KBN_SCREENSHOT_HEADER_BLACKLIST, KBN_SCREENSHOT_HEADER_BLACKLIST_STARTS_WITH_PATTERN, -} from '../../../../common/constants'; +} from '../../../common/constants'; export const omitBlacklistedHeaders = ({ job, diff --git a/x-pack/plugins/reporting/common/validate_urls.test.ts b/x-pack/plugins/reporting/server/export_types/common/validate_urls.test.ts similarity index 100% rename from x-pack/plugins/reporting/common/validate_urls.test.ts rename to x-pack/plugins/reporting/server/export_types/common/validate_urls.test.ts diff --git a/x-pack/plugins/reporting/common/validate_urls.ts b/x-pack/plugins/reporting/server/export_types/common/validate_urls.ts similarity index 100% rename from x-pack/plugins/reporting/common/validate_urls.ts rename to x-pack/plugins/reporting/server/export_types/common/validate_urls.ts diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/create_job.ts b/x-pack/plugins/reporting/server/export_types/csv/create_job.ts similarity index 90% rename from x-pack/plugins/reporting/server/export_types/csv/server/create_job.ts rename to x-pack/plugins/reporting/server/export_types/csv/create_job.ts index fb2d9bfdc5838..5e8ce923a79e0 100644 --- a/x-pack/plugins/reporting/server/export_types/csv/server/create_job.ts +++ b/x-pack/plugins/reporting/server/export_types/csv/create_job.ts @@ -4,9 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { cryptoFactory } from '../../../lib'; -import { ESQueueCreateJobFn, ScheduleTaskFnFactory } from '../../../types'; -import { JobParamsDiscoverCsv } from '../types'; +import { cryptoFactory } from '../../lib'; +import { ESQueueCreateJobFn, ScheduleTaskFnFactory } from '../../types'; +import { JobParamsDiscoverCsv } from './types'; export const scheduleTaskFnFactory: ScheduleTaskFnFactory new Promise((resolve) => setTimeout(() => resolve(), ms)); diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/execute_job.ts b/x-pack/plugins/reporting/server/export_types/csv/execute_job.ts similarity index 92% rename from x-pack/plugins/reporting/server/export_types/csv/server/execute_job.ts rename to x-pack/plugins/reporting/server/export_types/csv/execute_job.ts index b38cd8c5af9e7..f0c41a6a49703 100644 --- a/x-pack/plugins/reporting/server/export_types/csv/server/execute_job.ts +++ b/x-pack/plugins/reporting/server/export_types/csv/execute_job.ts @@ -7,11 +7,11 @@ import { Crypto } from '@elastic/node-crypto'; import { i18n } from '@kbn/i18n'; import Hapi from 'hapi'; -import { KibanaRequest } from '../../../../../../../src/core/server'; -import { CONTENT_TYPE_CSV, CSV_JOB_TYPE } from '../../../../common/constants'; -import { cryptoFactory, LevelLogger } from '../../../lib'; -import { ESQueueWorkerExecuteFn, RunTaskFnFactory } from '../../../types'; -import { ScheduledTaskParamsCSV } from '../types'; +import { KibanaRequest } from '../../../../../../src/core/server'; +import { CONTENT_TYPE_CSV, CSV_JOB_TYPE } from '../../../common/constants'; +import { cryptoFactory, LevelLogger } from '../../lib'; +import { ESQueueWorkerExecuteFn, RunTaskFnFactory } from '../../types'; +import { ScheduledTaskParamsCSV } from './types'; import { createGenerateCsv } from './generate_csv'; const getRequest = async (headers: string | undefined, crypto: Crypto, logger: LevelLogger) => { diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/cell_has_formula.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/cell_has_formula.ts similarity index 85% rename from x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/cell_has_formula.ts rename to x-pack/plugins/reporting/server/export_types/csv/generate_csv/cell_has_formula.ts index 659aef85ed593..1433d852ce630 100644 --- a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/cell_has_formula.ts +++ b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/cell_has_formula.ts @@ -5,7 +5,7 @@ */ import { startsWith } from 'lodash'; -import { CSV_FORMULA_CHARS } from '../../../../../common/constants'; +import { CSV_FORMULA_CHARS } from '../../../../common/constants'; export const cellHasFormulas = (val: string) => CSV_FORMULA_CHARS.some((formulaChar) => startsWith(val, formulaChar)); diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/check_cells_for_formulas.test.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/check_cells_for_formulas.test.ts similarity index 100% rename from x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/check_cells_for_formulas.test.ts rename to x-pack/plugins/reporting/server/export_types/csv/generate_csv/check_cells_for_formulas.test.ts diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/check_cells_for_formulas.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/check_cells_for_formulas.ts similarity index 100% rename from x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/check_cells_for_formulas.ts rename to x-pack/plugins/reporting/server/export_types/csv/generate_csv/check_cells_for_formulas.ts diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/escape_value.test.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/escape_value.test.ts similarity index 100% rename from x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/escape_value.test.ts rename to x-pack/plugins/reporting/server/export_types/csv/generate_csv/escape_value.test.ts diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/escape_value.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/escape_value.ts similarity index 95% rename from x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/escape_value.ts rename to x-pack/plugins/reporting/server/export_types/csv/generate_csv/escape_value.ts index 344091ee18268..c850d8b2dc741 100644 --- a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/escape_value.ts +++ b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/escape_value.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { RawValue } from '../../types'; +import { RawValue } from '../types'; import { cellHasFormulas } from './cell_has_formula'; const nonAlphaNumRE = /[^a-zA-Z0-9]/; diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/field_format_map.test.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/field_format_map.test.ts similarity index 97% rename from x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/field_format_map.test.ts rename to x-pack/plugins/reporting/server/export_types/csv/generate_csv/field_format_map.test.ts index 1f0e450da698f..4cb8de5810584 100644 --- a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/field_format_map.test.ts +++ b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/field_format_map.test.ts @@ -6,7 +6,7 @@ import expect from '@kbn/expect'; import { fieldFormats, FieldFormatsGetConfigFn, UI_SETTINGS } from 'src/plugins/data/server'; -import { IndexPatternSavedObject } from '../../types'; +import { IndexPatternSavedObject } from '../types'; import { fieldFormatMapFactory } from './field_format_map'; type ConfigValue = { number: { id: string; params: {} } } | string; diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/field_format_map.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/field_format_map.ts similarity index 97% rename from x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/field_format_map.ts rename to x-pack/plugins/reporting/server/export_types/csv/generate_csv/field_format_map.ts index 848cf569bc8d7..e01fee530fc65 100644 --- a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/field_format_map.ts +++ b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/field_format_map.ts @@ -7,7 +7,7 @@ import _ from 'lodash'; import { FieldFormat } from 'src/plugins/data/common'; import { FieldFormatConfig, IFieldFormatsRegistry } from 'src/plugins/data/server'; -import { IndexPatternSavedObject } from '../../types'; +import { IndexPatternSavedObject } from '../types'; /** * Create a map of FieldFormat instances for index pattern fields diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/flatten_hit.test.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/flatten_hit.test.ts similarity index 100% rename from x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/flatten_hit.test.ts rename to x-pack/plugins/reporting/server/export_types/csv/generate_csv/flatten_hit.test.ts diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/flatten_hit.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/flatten_hit.ts similarity index 100% rename from x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/flatten_hit.ts rename to x-pack/plugins/reporting/server/export_types/csv/generate_csv/flatten_hit.ts diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/format_csv_values.test.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/format_csv_values.test.ts similarity index 100% rename from x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/format_csv_values.test.ts rename to x-pack/plugins/reporting/server/export_types/csv/generate_csv/format_csv_values.test.ts diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/format_csv_values.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/format_csv_values.ts similarity index 97% rename from x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/format_csv_values.ts rename to x-pack/plugins/reporting/server/export_types/csv/generate_csv/format_csv_values.ts index 387066415a1bc..d0294072112bf 100644 --- a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/format_csv_values.ts +++ b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/format_csv_values.ts @@ -6,7 +6,7 @@ import { isNull, isObject, isUndefined } from 'lodash'; import { FieldFormat } from 'src/plugins/data/common'; -import { RawValue } from '../../types'; +import { RawValue } from '../types'; export function createFormatCsvValues( escapeValue: (value: RawValue, index: number, array: RawValue[]) => string, diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/get_ui_settings.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/get_ui_settings.ts similarity index 94% rename from x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/get_ui_settings.ts rename to x-pack/plugins/reporting/server/export_types/csv/generate_csv/get_ui_settings.ts index 8f72c467b0711..915d5010a4885 100644 --- a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/get_ui_settings.ts +++ b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/get_ui_settings.ts @@ -6,8 +6,8 @@ import { i18n } from '@kbn/i18n'; import { IUiSettingsClient } from 'kibana/server'; -import { ReportingConfig } from '../../../..'; -import { LevelLogger } from '../../../../lib'; +import { ReportingConfig } from '../../../'; +import { LevelLogger } from '../../../lib'; export const getUiSettings = async ( timezone: string | undefined, diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/hit_iterator.test.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/hit_iterator.test.ts similarity index 96% rename from x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/hit_iterator.test.ts rename to x-pack/plugins/reporting/server/export_types/csv/generate_csv/hit_iterator.test.ts index 479879e3c8b01..831bf45cf72ea 100644 --- a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/hit_iterator.test.ts +++ b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/hit_iterator.test.ts @@ -6,9 +6,9 @@ import expect from '@kbn/expect'; import sinon from 'sinon'; -import { CancellationToken } from '../../../../../common'; -import { LevelLogger } from '../../../../lib'; -import { ScrollConfig } from '../../../../types'; +import { CancellationToken } from '../../../../common'; +import { LevelLogger } from '../../../lib'; +import { ScrollConfig } from '../../../types'; import { createHitIterator } from './hit_iterator'; const mockLogger = { diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/hit_iterator.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/hit_iterator.ts similarity index 95% rename from x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/hit_iterator.ts rename to x-pack/plugins/reporting/server/export_types/csv/generate_csv/hit_iterator.ts index b877023064ac6..dee653cf30007 100644 --- a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/hit_iterator.ts +++ b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/hit_iterator.ts @@ -6,9 +6,9 @@ import { i18n } from '@kbn/i18n'; import { SearchParams, SearchResponse } from 'elasticsearch'; -import { CancellationToken } from '../../../../../common'; -import { LevelLogger } from '../../../../lib'; -import { ScrollConfig } from '../../../../types'; +import { CancellationToken } from '../../../../common'; +import { LevelLogger } from '../../../lib'; +import { ScrollConfig } from '../../../types'; export type EndpointCaller = (method: string, params: object) => Promise>; diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/index.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/index.ts similarity index 93% rename from x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/index.ts rename to x-pack/plugins/reporting/server/export_types/csv/generate_csv/index.ts index 2cb10e291619c..8da27100ac31c 100644 --- a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/index.ts +++ b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/index.ts @@ -6,12 +6,12 @@ import { i18n } from '@kbn/i18n'; import { IUiSettingsClient } from 'src/core/server'; -import { getFieldFormats } from '../../../../services'; -import { ReportingConfig } from '../../../..'; -import { CancellationToken } from '../../../../../../../plugins/reporting/common'; -import { CSV_BOM_CHARS } from '../../../../../common/constants'; -import { LevelLogger } from '../../../../lib'; -import { IndexPatternSavedObject, SavedSearchGeneratorResult } from '../../types'; +import { getFieldFormats } from '../../../services'; +import { ReportingConfig } from '../../../'; +import { CancellationToken } from '../../../../../../plugins/reporting/common'; +import { CSV_BOM_CHARS } from '../../../../common/constants'; +import { LevelLogger } from '../../../lib'; +import { IndexPatternSavedObject, SavedSearchGeneratorResult } from '../types'; import { checkIfRowsHaveFormulas } from './check_cells_for_formulas'; import { createEscapeValue } from './escape_value'; import { fieldFormatMapFactory } from './field_format_map'; diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/max_size_string_builder.test.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/max_size_string_builder.test.ts similarity index 100% rename from x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/max_size_string_builder.test.ts rename to x-pack/plugins/reporting/server/export_types/csv/generate_csv/max_size_string_builder.test.ts diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/max_size_string_builder.ts b/x-pack/plugins/reporting/server/export_types/csv/generate_csv/max_size_string_builder.ts similarity index 100% rename from x-pack/plugins/reporting/server/export_types/csv/server/generate_csv/max_size_string_builder.ts rename to x-pack/plugins/reporting/server/export_types/csv/generate_csv/max_size_string_builder.ts diff --git a/x-pack/plugins/reporting/server/export_types/csv/index.ts b/x-pack/plugins/reporting/server/export_types/csv/index.ts index b5eacdfc62c8b..dffc874831dc2 100644 --- a/x-pack/plugins/reporting/server/export_types/csv/index.ts +++ b/x-pack/plugins/reporting/server/export_types/csv/index.ts @@ -15,8 +15,8 @@ import { import { CSV_JOB_TYPE as jobType } from '../../../constants'; import { ESQueueCreateJobFn, ESQueueWorkerExecuteFn, ExportTypeDefinition } from '../../types'; import { metadata } from './metadata'; -import { scheduleTaskFnFactory } from './server/create_job'; -import { runTaskFnFactory } from './server/execute_job'; +import { scheduleTaskFnFactory } from './create_job'; +import { runTaskFnFactory } from './execute_job'; import { JobParamsDiscoverCsv, ScheduledTaskParamsCSV } from './types'; export const getExportType = (): ExportTypeDefinition< diff --git a/x-pack/plugins/reporting/server/export_types/csv/server/lib/get_request.ts b/x-pack/plugins/reporting/server/export_types/csv/lib/get_request.ts similarity index 93% rename from x-pack/plugins/reporting/server/export_types/csv/server/lib/get_request.ts rename to x-pack/plugins/reporting/server/export_types/csv/lib/get_request.ts index 21e49bd62ccc7..09e6becc2baec 100644 --- a/x-pack/plugins/reporting/server/export_types/csv/server/lib/get_request.ts +++ b/x-pack/plugins/reporting/server/export_types/csv/lib/get_request.ts @@ -7,8 +7,8 @@ import { Crypto } from '@elastic/node-crypto'; import { i18n } from '@kbn/i18n'; import Hapi from 'hapi'; -import { KibanaRequest } from '../../../../../../../../src/core/server'; -import { LevelLogger } from '../../../../lib'; +import { KibanaRequest } from '../../../../../../../src/core/server'; +import { LevelLogger } from '../../../lib'; export const getRequest = async ( headers: string | undefined, diff --git a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/create_job.ts b/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/create_job.ts similarity index 94% rename from x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/create_job.ts rename to x-pack/plugins/reporting/server/export_types/csv_from_savedobject/create_job.ts index 96fb2033f0954..e7fb0c6e2cb99 100644 --- a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/create_job.ts +++ b/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/create_job.ts @@ -7,9 +7,9 @@ import { notFound, notImplemented } from 'boom'; import { get } from 'lodash'; import { KibanaRequest, RequestHandlerContext } from 'src/core/server'; -import { CSV_FROM_SAVEDOBJECT_JOB_TYPE } from '../../../../common/constants'; -import { cryptoFactory } from '../../../lib'; -import { ScheduleTaskFnFactory, TimeRangeParams } from '../../../types'; +import { CSV_FROM_SAVEDOBJECT_JOB_TYPE } from '../../../common/constants'; +import { cryptoFactory } from '../../lib'; +import { ScheduleTaskFnFactory, TimeRangeParams } from '../../types'; import { JobParamsPanelCsv, SavedObject, @@ -18,7 +18,7 @@ import { SavedSearchObjectAttributesJSON, SearchPanel, VisObjectAttributesJSON, -} from '../types'; +} from './types'; export type ImmediateCreateJobFn = ( jobParams: JobParamsPanelCsv, diff --git a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/execute_job.ts b/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/execute_job.ts similarity index 80% rename from x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/execute_job.ts rename to x-pack/plugins/reporting/server/export_types/csv_from_savedobject/execute_job.ts index a7992c34a88f1..0cc9ec16ed71b 100644 --- a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/execute_job.ts +++ b/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/execute_job.ts @@ -5,12 +5,11 @@ */ import { KibanaRequest, RequestHandlerContext } from 'src/core/server'; -import { CancellationToken } from '../../../../common'; -import { CONTENT_TYPE_CSV, CSV_FROM_SAVEDOBJECT_JOB_TYPE } from '../../../../common/constants'; -import { RunTaskFnFactory, ScheduledTaskParams, TaskRunResult } from '../../../types'; -import { createGenerateCsv } from '../../csv/server/generate_csv'; -import { JobParamsPanelCsv, SearchPanel } from '../types'; -import { getFakeRequest } from './lib/get_fake_request'; +import { CancellationToken } from '../../../common'; +import { CONTENT_TYPE_CSV, CSV_FROM_SAVEDOBJECT_JOB_TYPE } from '../../../common/constants'; +import { RunTaskFnFactory, ScheduledTaskParams, TaskRunResult } from '../../types'; +import { createGenerateCsv } from '../csv/generate_csv'; +import { JobParamsPanelCsv, SearchPanel } from './types'; import { getGenerateCsvParams } from './lib/get_csv_job'; /* @@ -44,19 +43,10 @@ export const runTaskFnFactory: RunTaskFnFactory = function e const { jobParams } = jobPayload; const jobLogger = logger.clone([jobId === null ? 'immediate' : jobId]); const generateCsv = createGenerateCsv(jobLogger); - const { isImmediate, panel, visType } = jobParams as JobParamsPanelCsv & { - panel: SearchPanel; - }; + const { panel, visType } = jobParams as JobParamsPanelCsv & { panel: SearchPanel }; jobLogger.debug(`Execute job generating [${visType}] csv`); - if (isImmediate && req) { - jobLogger.info(`Executing job from Immediate API using request context`); - } else { - jobLogger.info(`Executing job async using encrypted headers`); - req = await getFakeRequest(jobPayload, config.get('encryptionKey')!, jobLogger); - } - const savedObjectsClient = context.core.savedObjects.client; const uiConfig = await reporting.getUiSettingsServiceFactory(savedObjectsClient); diff --git a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/index.ts b/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/index.ts index 9a9f445de0b13..7467f415299fa 100644 --- a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/index.ts +++ b/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/index.ts @@ -15,16 +15,16 @@ import { import { CSV_FROM_SAVEDOBJECT_JOB_TYPE } from '../../../constants'; import { ExportTypeDefinition } from '../../types'; import { metadata } from './metadata'; -import { ImmediateCreateJobFn, scheduleTaskFnFactory } from './server/create_job'; -import { ImmediateExecuteFn, runTaskFnFactory } from './server/execute_job'; +import { ImmediateCreateJobFn, scheduleTaskFnFactory } from './create_job'; +import { ImmediateExecuteFn, runTaskFnFactory } from './execute_job'; import { JobParamsPanelCsv } from './types'; /* * These functions are exported to share with the API route handler that * generates csv from saved object immediately on request. */ -export { scheduleTaskFnFactory } from './server/create_job'; -export { runTaskFnFactory } from './server/execute_job'; +export { scheduleTaskFnFactory } from './create_job'; +export { runTaskFnFactory } from './execute_job'; export const getExportType = (): ExportTypeDefinition< JobParamsPanelCsv, diff --git a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_csv_job.test.ts b/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/lib/get_csv_job.test.ts similarity index 99% rename from x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_csv_job.test.ts rename to x-pack/plugins/reporting/server/export_types/csv_from_savedobject/lib/get_csv_job.test.ts index 3271c6fdae24d..9646d7eecd5b5 100644 --- a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_csv_job.test.ts +++ b/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/lib/get_csv_job.test.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { JobParamsPanelCsv, SearchPanel } from '../../types'; +import { JobParamsPanelCsv, SearchPanel } from '../types'; import { getGenerateCsvParams } from './get_csv_job'; describe('Get CSV Job', () => { diff --git a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_csv_job.ts b/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/lib/get_csv_job.ts similarity index 96% rename from x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_csv_job.ts rename to x-pack/plugins/reporting/server/export_types/csv_from_savedobject/lib/get_csv_job.ts index 5f1954b80e1bc..0fc29c5b208d9 100644 --- a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_csv_job.ts +++ b/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/lib/get_csv_job.ts @@ -11,7 +11,7 @@ import { Filter, IIndexPattern, Query, -} from '../../../../../../../../src/plugins/data/server'; +} from '../../../../../../../src/plugins/data/server'; import { DocValueFields, IndexPatternField, @@ -20,10 +20,10 @@ import { SavedSearchObjectAttributes, SearchPanel, SearchSource, -} from '../../types'; +} from '../types'; import { getDataSource } from './get_data_source'; import { getFilters } from './get_filters'; -import { GenerateCsvParams } from '../../../csv/server/generate_csv'; +import { GenerateCsvParams } from '../../csv/generate_csv'; export const getEsQueryConfig = async (config: IUiSettingsClient) => { const configs = await Promise.all([ diff --git a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_data_source.ts b/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/lib/get_data_source.ts similarity index 95% rename from x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_data_source.ts rename to x-pack/plugins/reporting/server/export_types/csv_from_savedobject/lib/get_data_source.ts index bf915696c8974..e3631b9c89724 100644 --- a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_data_source.ts +++ b/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/lib/get_data_source.ts @@ -4,8 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -import { IndexPatternSavedObject } from '../../../csv/types'; -import { SavedObjectReference, SavedSearchObjectAttributesJSON, SearchSource } from '../../types'; +import { IndexPatternSavedObject } from '../../csv/types'; +import { SavedObjectReference, SavedSearchObjectAttributesJSON, SearchSource } from '../types'; export async function getDataSource( savedObjectsClient: any, diff --git a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_filters.test.ts b/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/lib/get_filters.test.ts similarity index 98% rename from x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_filters.test.ts rename to x-pack/plugins/reporting/server/export_types/csv_from_savedobject/lib/get_filters.test.ts index b5d564d93d0d6..429b2c518cf14 100644 --- a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_filters.test.ts +++ b/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/lib/get_filters.test.ts @@ -4,8 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -import { TimeRangeParams } from '../../../../types'; -import { QueryFilter, SavedSearchObjectAttributes, SearchSourceFilter } from '../../types'; +import { TimeRangeParams } from '../../../types'; +import { QueryFilter, SavedSearchObjectAttributes, SearchSourceFilter } from '../types'; import { getFilters } from './get_filters'; interface Args { diff --git a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_filters.ts b/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/lib/get_filters.ts similarity index 95% rename from x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_filters.ts rename to x-pack/plugins/reporting/server/export_types/csv_from_savedobject/lib/get_filters.ts index 1258b03d3051b..a1b04cca0419d 100644 --- a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_filters.ts +++ b/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/lib/get_filters.ts @@ -6,8 +6,8 @@ import { badRequest } from 'boom'; import moment from 'moment-timezone'; -import { TimeRangeParams } from '../../../../types'; -import { Filter, QueryFilter, SavedSearchObjectAttributes, SearchSourceFilter } from '../../types'; +import { TimeRangeParams } from '../../../types'; +import { Filter, QueryFilter, SavedSearchObjectAttributes, SearchSourceFilter } from '../types'; export function getFilters( indexPatternId: string, diff --git a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_fake_request.ts b/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_fake_request.ts deleted file mode 100644 index 09c58806de120..0000000000000 --- a/x-pack/plugins/reporting/server/export_types/csv_from_savedobject/server/lib/get_fake_request.ts +++ /dev/null @@ -1,51 +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 { KibanaRequest } from 'kibana/server'; -import { cryptoFactory, LevelLogger } from '../../../../lib'; -import { ScheduledTaskParams } from '../../../../types'; -import { JobParamsPanelCsv } from '../../types'; - -export const getFakeRequest = async ( - job: ScheduledTaskParams, - encryptionKey: string, - jobLogger: LevelLogger -) => { - // TODO remove this block: csv from savedobject download is always "sync" - const crypto = cryptoFactory(encryptionKey); - let decryptedHeaders: KibanaRequest['headers']; - const serializedEncryptedHeaders = job.headers; - try { - if (typeof serializedEncryptedHeaders !== 'string') { - throw new Error( - i18n.translate( - 'xpack.reporting.exportTypes.csv_from_savedobject.executeJob.missingJobHeadersErrorMessage', - { - defaultMessage: 'Job headers are missing', - } - ) - ); - } - decryptedHeaders = (await crypto.decrypt( - serializedEncryptedHeaders - )) as KibanaRequest['headers']; - } catch (err) { - jobLogger.error(err); - throw new Error( - i18n.translate( - 'xpack.reporting.exportTypes.csv_from_savedobject.executeJob.failedToDecryptReportJobDataErrorMessage', - { - defaultMessage: - 'Failed to decrypt report job data. Please ensure that {encryptionKey} is set and re-generate this report. {err}', - values: { encryptionKey: 'xpack.reporting.encryptionKey', err }, - } - ) - ); - } - - return { headers: decryptedHeaders } as KibanaRequest; -}; diff --git a/x-pack/plugins/reporting/server/export_types/png/server/create_job/index.ts b/x-pack/plugins/reporting/server/export_types/png/create_job/index.ts similarity index 85% rename from x-pack/plugins/reporting/server/export_types/png/server/create_job/index.ts rename to x-pack/plugins/reporting/server/export_types/png/create_job/index.ts index f459b8f249c70..b63f2a09041b3 100644 --- a/x-pack/plugins/reporting/server/export_types/png/server/create_job/index.ts +++ b/x-pack/plugins/reporting/server/export_types/png/create_job/index.ts @@ -4,10 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ -import { validateUrls } from '../../../../../common/validate_urls'; -import { cryptoFactory } from '../../../../lib'; -import { ESQueueCreateJobFn, ScheduleTaskFnFactory } from '../../../../types'; -import { JobParamsPNG } from '../../types'; +import { cryptoFactory } from '../../../lib'; +import { ESQueueCreateJobFn, ScheduleTaskFnFactory } from '../../../types'; +import { validateUrls } from '../../common'; +import { JobParamsPNG } from '../types'; export const scheduleTaskFnFactory: ScheduleTaskFnFactory>; diff --git a/x-pack/plugins/reporting/server/export_types/png/index.ts b/x-pack/plugins/reporting/server/export_types/png/index.ts index b708448b0f8b2..25b4dbd60535b 100644 --- a/x-pack/plugins/reporting/server/export_types/png/index.ts +++ b/x-pack/plugins/reporting/server/export_types/png/index.ts @@ -12,10 +12,10 @@ import { LICENSE_TYPE_TRIAL, PNG_JOB_TYPE as jobType, } from '../../../common/constants'; -import { ESQueueCreateJobFn, ESQueueWorkerExecuteFn, ExportTypeDefinition } from '../..//types'; +import { ESQueueCreateJobFn, ESQueueWorkerExecuteFn, ExportTypeDefinition } from '../../types'; import { metadata } from './metadata'; -import { scheduleTaskFnFactory } from './server/create_job'; -import { runTaskFnFactory } from './server/execute_job'; +import { scheduleTaskFnFactory } from './create_job'; +import { runTaskFnFactory } from './execute_job'; import { JobParamsPNG, ScheduledTaskParamsPNG } from './types'; export const getExportType = (): ExportTypeDefinition< diff --git a/x-pack/plugins/reporting/server/export_types/png/server/lib/generate_png.ts b/x-pack/plugins/reporting/server/export_types/png/lib/generate_png.ts similarity index 89% rename from x-pack/plugins/reporting/server/export_types/png/server/lib/generate_png.ts rename to x-pack/plugins/reporting/server/export_types/png/lib/generate_png.ts index d7e9d0f812b37..5969b5b8abc00 100644 --- a/x-pack/plugins/reporting/server/export_types/png/server/lib/generate_png.ts +++ b/x-pack/plugins/reporting/server/export_types/png/lib/generate_png.ts @@ -7,11 +7,10 @@ import apm from 'elastic-apm-node'; import * as Rx from 'rxjs'; import { map } from 'rxjs/operators'; -import { ReportingCore } from '../../../../'; -import { LevelLogger } from '../../../../lib'; -import { ConditionalHeaders, ScreenshotResults } from '../../../../types'; -import { LayoutParams } from '../../../common/layouts'; -import { PreserveLayout } from '../../../common/layouts/preserve_layout'; +import { ReportingCore } from '../../../'; +import { LevelLogger } from '../../../lib'; +import { LayoutParams, PreserveLayout } from '../../../lib/layouts'; +import { ConditionalHeaders, ScreenshotResults } from '../../../types'; export async function generatePngObservableFactory(reporting: ReportingCore) { const getScreenshots = await reporting.getScreenshotsObservable(); diff --git a/x-pack/plugins/reporting/server/export_types/png/types.d.ts b/x-pack/plugins/reporting/server/export_types/png/types.d.ts index 7a25f4ed8fe73..4c40f55f0f0d6 100644 --- a/x-pack/plugins/reporting/server/export_types/png/types.d.ts +++ b/x-pack/plugins/reporting/server/export_types/png/types.d.ts @@ -5,7 +5,7 @@ */ import { ScheduledTaskParams } from '../../../server/types'; -import { LayoutInstance, LayoutParams } from '../common/layouts'; +import { LayoutInstance, LayoutParams } from '../../lib/layouts'; // Job params: structure of incoming user request data export interface JobParamsPNG { diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/create_job/index.ts b/x-pack/plugins/reporting/server/export_types/printable_pdf/create_job/index.ts similarity index 86% rename from x-pack/plugins/reporting/server/export_types/printable_pdf/server/create_job/index.ts rename to x-pack/plugins/reporting/server/export_types/printable_pdf/create_job/index.ts index 76c5718249720..aa88ef863d32b 100644 --- a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/create_job/index.ts +++ b/x-pack/plugins/reporting/server/export_types/printable_pdf/create_job/index.ts @@ -4,10 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ -import { validateUrls } from '../../../../../common/validate_urls'; -import { cryptoFactory } from '../../../../lib'; -import { ESQueueCreateJobFn, ScheduleTaskFnFactory } from '../../../../types'; -import { JobParamsPDF } from '../../types'; +import { validateUrls } from '../../common'; +import { cryptoFactory } from '../../../lib'; +import { ESQueueCreateJobFn, ScheduleTaskFnFactory } from '../../../types'; +import { JobParamsPDF } from '../types'; export const scheduleTaskFnFactory: ScheduleTaskFnFactory ({ generatePdfObservableFactory: jest.fn() })); import * as Rx from 'rxjs'; -import { ReportingCore } from '../../../../'; -import { CancellationToken } from '../../../../../common'; -import { cryptoFactory, LevelLogger } from '../../../../lib'; -import { createMockReportingCore } from '../../../../test_helpers'; -import { ScheduledTaskParamsPDF } from '../../types'; +import { ReportingCore } from '../../../'; +import { CancellationToken } from '../../../../common'; +import { cryptoFactory, LevelLogger } from '../../../lib'; +import { createMockReportingCore } from '../../../test_helpers'; import { generatePdfObservableFactory } from '../lib/generate_pdf'; +import { ScheduledTaskParamsPDF } from '../types'; import { runTaskFnFactory } from './'; let mockReporting: ReportingCore; diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/execute_job/index.ts b/x-pack/plugins/reporting/server/export_types/printable_pdf/execute_job/index.ts similarity index 94% rename from x-pack/plugins/reporting/server/export_types/printable_pdf/server/execute_job/index.ts rename to x-pack/plugins/reporting/server/export_types/printable_pdf/execute_job/index.ts index 7f8f2f4f6906a..eb15c0a71ca3f 100644 --- a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/execute_job/index.ts +++ b/x-pack/plugins/reporting/server/export_types/printable_pdf/execute_job/index.ts @@ -7,17 +7,17 @@ import apm from 'elastic-apm-node'; import * as Rx from 'rxjs'; import { catchError, map, mergeMap, takeUntil } from 'rxjs/operators'; -import { PDF_JOB_TYPE } from '../../../../../common/constants'; -import { ESQueueWorkerExecuteFn, RunTaskFnFactory, TaskRunResult } from '../../../../types'; +import { PDF_JOB_TYPE } from '../../../../common/constants'; +import { ESQueueWorkerExecuteFn, RunTaskFnFactory, TaskRunResult } from '../../../types'; import { decryptJobHeaders, getConditionalHeaders, getCustomLogo, getFullUrls, omitBlacklistedHeaders, -} from '../../../common/execute_job'; -import { ScheduledTaskParamsPDF } from '../../types'; +} from '../../common'; import { generatePdfObservableFactory } from '../lib/generate_pdf'; +import { ScheduledTaskParamsPDF } from '../types'; type QueuedPdfExecutorFactory = RunTaskFnFactory>; diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/index.ts b/x-pack/plugins/reporting/server/export_types/printable_pdf/index.ts index 073bd38b538fb..e5115c243c697 100644 --- a/x-pack/plugins/reporting/server/export_types/printable_pdf/index.ts +++ b/x-pack/plugins/reporting/server/export_types/printable_pdf/index.ts @@ -14,8 +14,8 @@ import { } from '../../../common/constants'; import { ESQueueCreateJobFn, ESQueueWorkerExecuteFn, ExportTypeDefinition } from '../../types'; import { metadata } from './metadata'; -import { scheduleTaskFnFactory } from './server/create_job'; -import { runTaskFnFactory } from './server/execute_job'; +import { scheduleTaskFnFactory } from './create_job'; +import { runTaskFnFactory } from './execute_job'; import { JobParamsPDF, ScheduledTaskParamsPDF } from './types'; export const getExportType = (): ExportTypeDefinition< diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/generate_pdf.ts b/x-pack/plugins/reporting/server/export_types/printable_pdf/lib/generate_pdf.ts similarity index 96% rename from x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/generate_pdf.ts rename to x-pack/plugins/reporting/server/export_types/printable_pdf/lib/generate_pdf.ts index 366949a033757..f2ce423566c46 100644 --- a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/generate_pdf.ts +++ b/x-pack/plugins/reporting/server/export_types/printable_pdf/lib/generate_pdf.ts @@ -7,10 +7,10 @@ import { groupBy } from 'lodash'; import * as Rx from 'rxjs'; import { mergeMap } from 'rxjs/operators'; -import { ReportingCore } from '../../../../'; -import { LevelLogger } from '../../../../lib'; -import { ConditionalHeaders, ScreenshotResults } from '../../../../types'; -import { createLayout, LayoutInstance, LayoutParams } from '../../../common/layouts'; +import { ReportingCore } from '../../../'; +import { LevelLogger } from '../../../lib'; +import { createLayout, LayoutInstance, LayoutParams } from '../../../lib/layouts'; +import { ConditionalHeaders, ScreenshotResults } from '../../../types'; // @ts-ignore untyped module import { pdf } from './pdf'; import { getTracker } from './tracker'; diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/noto/LICENSE_OFL.txt b/x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/noto/LICENSE_OFL.txt similarity index 100% rename from x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/noto/LICENSE_OFL.txt rename to x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/noto/LICENSE_OFL.txt diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/noto/NotoSansCJKtc-Medium.ttf b/x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/noto/NotoSansCJKtc-Medium.ttf similarity index 100% rename from x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/noto/NotoSansCJKtc-Medium.ttf rename to x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/noto/NotoSansCJKtc-Medium.ttf diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/noto/NotoSansCJKtc-Regular.ttf b/x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/noto/NotoSansCJKtc-Regular.ttf similarity index 100% rename from x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/noto/NotoSansCJKtc-Regular.ttf rename to x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/noto/NotoSansCJKtc-Regular.ttf diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/noto/index.js b/x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/noto/index.js similarity index 100% rename from x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/noto/index.js rename to x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/noto/index.js diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/roboto/LICENSE.txt b/x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/roboto/LICENSE.txt similarity index 100% rename from x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/roboto/LICENSE.txt rename to x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/roboto/LICENSE.txt diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/roboto/Roboto-Italic.ttf b/x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/roboto/Roboto-Italic.ttf similarity index 100% rename from x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/roboto/Roboto-Italic.ttf rename to x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/roboto/Roboto-Italic.ttf diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/roboto/Roboto-Medium.ttf b/x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/roboto/Roboto-Medium.ttf similarity index 100% rename from x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/roboto/Roboto-Medium.ttf rename to x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/roboto/Roboto-Medium.ttf diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/roboto/Roboto-Regular.ttf b/x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/roboto/Roboto-Regular.ttf similarity index 100% rename from x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/fonts/roboto/Roboto-Regular.ttf rename to x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/fonts/roboto/Roboto-Regular.ttf diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/img/logo-grey.png b/x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/img/logo-grey.png similarity index 100% rename from x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/assets/img/logo-grey.png rename to x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/assets/img/logo-grey.png diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/index.js b/x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/index.js similarity index 100% rename from x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/pdf/index.js rename to x-pack/plugins/reporting/server/export_types/printable_pdf/lib/pdf/index.js diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/tracker.ts b/x-pack/plugins/reporting/server/export_types/printable_pdf/lib/tracker.ts similarity index 100% rename from x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/tracker.ts rename to x-pack/plugins/reporting/server/export_types/printable_pdf/lib/tracker.ts diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/uri_encode.js b/x-pack/plugins/reporting/server/export_types/printable_pdf/lib/uri_encode.js similarity index 92% rename from x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/uri_encode.js rename to x-pack/plugins/reporting/server/export_types/printable_pdf/lib/uri_encode.js index d057cfba4ef30..657af71c42c83 100644 --- a/x-pack/plugins/reporting/server/export_types/printable_pdf/server/lib/uri_encode.js +++ b/x-pack/plugins/reporting/server/export_types/printable_pdf/lib/uri_encode.js @@ -5,7 +5,7 @@ */ import { forEach, isArray } from 'lodash'; -import { url } from '../../../../../../../../src/plugins/kibana_utils/server'; +import { url } from '../../../../../../../src/plugins/kibana_utils/server'; function toKeyValue(obj) { const parts = []; diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/types.d.ts b/x-pack/plugins/reporting/server/export_types/printable_pdf/types.d.ts index 5399781a77753..cba0f41f07536 100644 --- a/x-pack/plugins/reporting/server/export_types/printable_pdf/types.d.ts +++ b/x-pack/plugins/reporting/server/export_types/printable_pdf/types.d.ts @@ -5,7 +5,7 @@ */ import { ScheduledTaskParams } from '../../../server/types'; -import { LayoutInstance, LayoutParams } from '../common/layouts'; +import { LayoutInstance, LayoutParams } from '../../lib/layouts'; // Job params: structure of incoming user request data, after being parsed from RISON export interface JobParamsPDF { diff --git a/x-pack/plugins/reporting/server/lib/create_queue.ts b/x-pack/plugins/reporting/server/lib/create_queue.ts index a8dcb92c55b2d..2da3d8bd47ccb 100644 --- a/x-pack/plugins/reporting/server/lib/create_queue.ts +++ b/x-pack/plugins/reporting/server/lib/create_queue.ts @@ -6,10 +6,10 @@ import { ReportingCore } from '../core'; import { JobSource, TaskRunResult } from '../types'; -import { createTaggedLogger } from './create_tagged_logger'; // TODO remove createTaggedLogger once esqueue is removed import { createWorkerFactory } from './create_worker'; // @ts-ignore import { Esqueue } from './esqueue'; +import { createTaggedLogger } from './esqueue/create_tagged_logger'; import { LevelLogger } from './level_logger'; import { ReportingStore } from './store'; diff --git a/x-pack/plugins/reporting/server/lib/create_tagged_logger.ts b/x-pack/plugins/reporting/server/lib/esqueue/create_tagged_logger.ts similarity index 95% rename from x-pack/plugins/reporting/server/lib/create_tagged_logger.ts rename to x-pack/plugins/reporting/server/lib/esqueue/create_tagged_logger.ts index 775930ec83bdf..2b97f3f25217a 100644 --- a/x-pack/plugins/reporting/server/lib/create_tagged_logger.ts +++ b/x-pack/plugins/reporting/server/lib/esqueue/create_tagged_logger.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { LevelLogger } from './level_logger'; +import { LevelLogger } from '../level_logger'; export function createTaggedLogger(logger: LevelLogger, tags: string[]) { return (msg: string, additionalTags = []) => { diff --git a/x-pack/plugins/reporting/server/lib/index.ts b/x-pack/plugins/reporting/server/lib/index.ts index f5a50fca28b7a..e4adb1188e3fc 100644 --- a/x-pack/plugins/reporting/server/lib/index.ts +++ b/x-pack/plugins/reporting/server/lib/index.ts @@ -4,12 +4,12 @@ * you may not use this file except in compliance with the Elastic License. */ -export { LevelLogger } from './level_logger'; export { checkLicense } from './check_license'; export { createQueueFactory } from './create_queue'; export { cryptoFactory } from './crypto'; export { enqueueJobFactory } from './enqueue_job'; export { getExportTypesRegistry } from './export_types_registry'; -export { runValidations } from './validate'; -export { startTrace } from './trace'; +export { LevelLogger } from './level_logger'; export { ReportingStore } from './store'; +export { startTrace } from './trace'; +export { runValidations } from './validate'; diff --git a/x-pack/plugins/reporting/server/export_types/common/layouts/create_layout.ts b/x-pack/plugins/reporting/server/lib/layouts/create_layout.ts similarity index 94% rename from x-pack/plugins/reporting/server/export_types/common/layouts/create_layout.ts rename to x-pack/plugins/reporting/server/lib/layouts/create_layout.ts index 216a59d41cec0..921d302387edf 100644 --- a/x-pack/plugins/reporting/server/export_types/common/layouts/create_layout.ts +++ b/x-pack/plugins/reporting/server/lib/layouts/create_layout.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { CaptureConfig } from '../../../types'; +import { CaptureConfig } from '../../types'; import { LayoutParams, LayoutTypes } from './'; import { Layout } from './layout'; import { PreserveLayout } from './preserve_layout'; diff --git a/x-pack/plugins/reporting/server/export_types/common/layouts/index.ts b/x-pack/plugins/reporting/server/lib/layouts/index.ts similarity index 94% rename from x-pack/plugins/reporting/server/export_types/common/layouts/index.ts rename to x-pack/plugins/reporting/server/lib/layouts/index.ts index 23e4c095afe61..d46f088475222 100644 --- a/x-pack/plugins/reporting/server/export_types/common/layouts/index.ts +++ b/x-pack/plugins/reporting/server/lib/layouts/index.ts @@ -4,8 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -import { HeadlessChromiumDriver } from '../../../browsers'; -import { LevelLogger } from '../../../lib'; +import { HeadlessChromiumDriver } from '../../browsers'; +import { LevelLogger } from '../'; import { Layout } from './layout'; export { createLayout } from './create_layout'; diff --git a/x-pack/plugins/reporting/server/export_types/common/layouts/layout.ts b/x-pack/plugins/reporting/server/lib/layouts/layout.ts similarity index 100% rename from x-pack/plugins/reporting/server/export_types/common/layouts/layout.ts rename to x-pack/plugins/reporting/server/lib/layouts/layout.ts diff --git a/x-pack/plugins/reporting/server/export_types/common/layouts/preserve_layout.css b/x-pack/plugins/reporting/server/lib/layouts/preserve_layout.css similarity index 100% rename from x-pack/plugins/reporting/server/export_types/common/layouts/preserve_layout.css rename to x-pack/plugins/reporting/server/lib/layouts/preserve_layout.css diff --git a/x-pack/plugins/reporting/server/export_types/common/layouts/preserve_layout.ts b/x-pack/plugins/reporting/server/lib/layouts/preserve_layout.ts similarity index 100% rename from x-pack/plugins/reporting/server/export_types/common/layouts/preserve_layout.ts rename to x-pack/plugins/reporting/server/lib/layouts/preserve_layout.ts diff --git a/x-pack/plugins/reporting/server/export_types/common/layouts/print.css b/x-pack/plugins/reporting/server/lib/layouts/print.css similarity index 96% rename from x-pack/plugins/reporting/server/export_types/common/layouts/print.css rename to x-pack/plugins/reporting/server/lib/layouts/print.css index b5b6eae5e1ff6..4f1e3f4e5abd0 100644 --- a/x-pack/plugins/reporting/server/export_types/common/layouts/print.css +++ b/x-pack/plugins/reporting/server/lib/layouts/print.css @@ -110,7 +110,7 @@ discover-app .discover-table-footer { /** * 1. Reporting manually makes each visualization it wants to screenshot larger, so we need to hide * the visualizations in the other panels. We can only use properties that will be manually set in - * reporting/export_types/printable_pdf/server/lib/screenshot.js or this will also hide the visualization + * reporting/export_types/printable_pdf/lib/screenshot.js or this will also hide the visualization * we want to capture. * 2. React grid item's transform affects the visualizations, even when they are using fixed positioning. Chrome seems * to handle this fine, but firefox moves the visualizations around. diff --git a/x-pack/plugins/reporting/server/export_types/common/layouts/print_layout.ts b/x-pack/plugins/reporting/server/lib/layouts/print_layout.ts similarity index 91% rename from x-pack/plugins/reporting/server/export_types/common/layouts/print_layout.ts rename to x-pack/plugins/reporting/server/lib/layouts/print_layout.ts index 30c83771aa3c9..b055fae8a780d 100644 --- a/x-pack/plugins/reporting/server/export_types/common/layouts/print_layout.ts +++ b/x-pack/plugins/reporting/server/lib/layouts/print_layout.ts @@ -6,10 +6,10 @@ import path from 'path'; import { EvaluateFn, SerializableOrJSHandle } from 'puppeteer'; -import { CaptureConfig } from '../../../types'; -import { HeadlessChromiumDriver } from '../../../browsers'; -import { LevelLogger } from '../../../lib'; -import { getDefaultLayoutSelectors, LayoutSelectorDictionary, Size, LayoutTypes } from './'; +import { LevelLogger } from '../'; +import { HeadlessChromiumDriver } from '../../browsers'; +import { CaptureConfig } from '../../types'; +import { getDefaultLayoutSelectors, LayoutSelectorDictionary, LayoutTypes, Size } from './'; import { Layout } from './layout'; export class PrintLayout extends Layout { diff --git a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/constants.ts b/x-pack/plugins/reporting/server/lib/screenshots/constants.ts similarity index 92% rename from x-pack/plugins/reporting/server/export_types/common/lib/screenshots/constants.ts rename to x-pack/plugins/reporting/server/lib/screenshots/constants.ts index a3faf9337524e..854763e499135 100644 --- a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/constants.ts +++ b/x-pack/plugins/reporting/server/lib/screenshots/constants.ts @@ -4,6 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ +export const DEFAULT_PAGELOAD_SELECTOR = '.application'; + export const CONTEXT_GETNUMBEROFITEMS = 'GetNumberOfItems'; export const CONTEXT_INJECTCSS = 'InjectCss'; export const CONTEXT_WAITFORRENDER = 'WaitForRender'; diff --git a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/get_element_position_data.ts b/x-pack/plugins/reporting/server/lib/screenshots/get_element_position_data.ts similarity index 93% rename from x-pack/plugins/reporting/server/export_types/common/lib/screenshots/get_element_position_data.ts rename to x-pack/plugins/reporting/server/lib/screenshots/get_element_position_data.ts index 140d76f8d1cd6..4fb9fd96ecfe6 100644 --- a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/get_element_position_data.ts +++ b/x-pack/plugins/reporting/server/lib/screenshots/get_element_position_data.ts @@ -5,10 +5,10 @@ */ import { i18n } from '@kbn/i18n'; -import { HeadlessChromiumDriver } from '../../../../browsers'; -import { LevelLogger, startTrace } from '../../../../lib'; -import { AttributesMap, ElementsPositionAndAttribute } from '../../../../types'; -import { LayoutInstance } from '../../layouts'; +import { HeadlessChromiumDriver } from '../../browsers'; +import { AttributesMap, ElementsPositionAndAttribute } from '../../types'; +import { LevelLogger, startTrace } from '../'; +import { LayoutInstance } from '../layouts'; import { CONTEXT_ELEMENTATTRIBUTES } from './constants'; export const getElementPositionAndAttributes = async ( diff --git a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/get_number_of_items.ts b/x-pack/plugins/reporting/server/lib/screenshots/get_number_of_items.ts similarity index 91% rename from x-pack/plugins/reporting/server/export_types/common/lib/screenshots/get_number_of_items.ts rename to x-pack/plugins/reporting/server/lib/screenshots/get_number_of_items.ts index 42eb91ecba830..49c690e8c024d 100644 --- a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/get_number_of_items.ts +++ b/x-pack/plugins/reporting/server/lib/screenshots/get_number_of_items.ts @@ -5,10 +5,10 @@ */ import { i18n } from '@kbn/i18n'; -import { HeadlessChromiumDriver } from '../../../../browsers'; -import { LevelLogger, startTrace } from '../../../../lib'; -import { CaptureConfig } from '../../../../types'; -import { LayoutInstance } from '../../layouts'; +import { HeadlessChromiumDriver } from '../../browsers'; +import { CaptureConfig } from '../../types'; +import { LevelLogger, startTrace } from '../'; +import { LayoutInstance } from '../layouts'; import { CONTEXT_GETNUMBEROFITEMS, CONTEXT_READMETADATA } from './constants'; export const getNumberOfItems = async ( @@ -68,7 +68,6 @@ export const getNumberOfItems = async ( }, }) ); - itemsCount = 1; } endTrace(); diff --git a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/get_screenshots.ts b/x-pack/plugins/reporting/server/lib/screenshots/get_screenshots.ts similarity index 91% rename from x-pack/plugins/reporting/server/export_types/common/lib/screenshots/get_screenshots.ts rename to x-pack/plugins/reporting/server/lib/screenshots/get_screenshots.ts index 05c315b8341a3..bc7b7005674a7 100644 --- a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/get_screenshots.ts +++ b/x-pack/plugins/reporting/server/lib/screenshots/get_screenshots.ts @@ -5,9 +5,9 @@ */ import { i18n } from '@kbn/i18n'; -import { HeadlessChromiumDriver } from '../../../../browsers'; -import { LevelLogger, startTrace } from '../../../../lib'; -import { ElementsPositionAndAttribute, Screenshot } from '../../../../types'; +import { HeadlessChromiumDriver } from '../../browsers'; +import { ElementsPositionAndAttribute, Screenshot } from '../../types'; +import { LevelLogger, startTrace } from '../'; export const getScreenshots = async ( browser: HeadlessChromiumDriver, diff --git a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/get_time_range.ts b/x-pack/plugins/reporting/server/lib/screenshots/get_time_range.ts similarity index 87% rename from x-pack/plugins/reporting/server/export_types/common/lib/screenshots/get_time_range.ts rename to x-pack/plugins/reporting/server/lib/screenshots/get_time_range.ts index ba68a5fec4e4c..afd6364454835 100644 --- a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/get_time_range.ts +++ b/x-pack/plugins/reporting/server/lib/screenshots/get_time_range.ts @@ -4,9 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { HeadlessChromiumDriver } from '../../../../browsers'; -import { LevelLogger, startTrace } from '../../../../lib'; -import { LayoutInstance } from '../../layouts'; +import { LevelLogger, startTrace } from '../'; +import { LayoutInstance } from '../../../common/types'; +import { HeadlessChromiumDriver } from '../../browsers'; import { CONTEXT_GETTIMERANGE } from './constants'; export const getTimeRange = async ( diff --git a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/index.ts b/x-pack/plugins/reporting/server/lib/screenshots/index.ts similarity index 100% rename from x-pack/plugins/reporting/server/export_types/common/lib/screenshots/index.ts rename to x-pack/plugins/reporting/server/lib/screenshots/index.ts diff --git a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/inject_css.ts b/x-pack/plugins/reporting/server/lib/screenshots/inject_css.ts similarity index 90% rename from x-pack/plugins/reporting/server/export_types/common/lib/screenshots/inject_css.ts rename to x-pack/plugins/reporting/server/lib/screenshots/inject_css.ts index d72afacc1bef3..f893951815e9e 100644 --- a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/inject_css.ts +++ b/x-pack/plugins/reporting/server/lib/screenshots/inject_css.ts @@ -7,9 +7,9 @@ import { i18n } from '@kbn/i18n'; import fs from 'fs'; import { promisify } from 'util'; -import { HeadlessChromiumDriver } from '../../../../browsers'; -import { LevelLogger, startTrace } from '../../../../lib'; -import { Layout } from '../../layouts/layout'; +import { LevelLogger, startTrace } from '../'; +import { HeadlessChromiumDriver } from '../../browsers'; +import { Layout } from '../layouts'; import { CONTEXT_INJECTCSS } from './constants'; const fsp = { readFile: promisify(fs.readFile) }; diff --git a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/observable.test.ts b/x-pack/plugins/reporting/server/lib/screenshots/observable.test.ts similarity index 97% rename from x-pack/plugins/reporting/server/export_types/common/lib/screenshots/observable.test.ts rename to x-pack/plugins/reporting/server/lib/screenshots/observable.test.ts index b00233137943d..1b72be6c92f43 100644 --- a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/observable.test.ts +++ b/x-pack/plugins/reporting/server/lib/screenshots/observable.test.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -jest.mock('../../../../browsers/chromium/puppeteer', () => ({ +jest.mock('../../browsers/chromium/puppeteer', () => ({ puppeteerLaunch: () => ({ // Fixme needs event emitters newPage: () => ({ @@ -17,11 +17,11 @@ jest.mock('../../../../browsers/chromium/puppeteer', () => ({ import * as Rx from 'rxjs'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { loggingSystemMock } from '../../../../../../../../src/core/server/mocks'; -import { HeadlessChromiumDriver } from '../../../../browsers'; -import { LevelLogger } from '../../../../lib'; -import { createMockBrowserDriverFactory, createMockLayoutInstance } from '../../../../test_helpers'; -import { CaptureConfig, ConditionalHeaders, ElementsPositionAndAttribute } from '../../../../types'; +import { loggingSystemMock } from '../../../../../../src/core/server/mocks'; +import { HeadlessChromiumDriver } from '../../browsers'; +import { LevelLogger } from '../'; +import { createMockBrowserDriverFactory, createMockLayoutInstance } from '../../test_helpers'; +import { CaptureConfig, ConditionalHeaders, ElementsPositionAndAttribute } from '../../types'; import * as contexts from './constants'; import { screenshotsObservableFactory } from './observable'; diff --git a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/observable.ts b/x-pack/plugins/reporting/server/lib/screenshots/observable.ts similarity index 97% rename from x-pack/plugins/reporting/server/export_types/common/lib/screenshots/observable.ts rename to x-pack/plugins/reporting/server/lib/screenshots/observable.ts index 028bff4aaa5ee..ab4dabf9ed2c2 100644 --- a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/observable.ts +++ b/x-pack/plugins/reporting/server/lib/screenshots/observable.ts @@ -16,15 +16,15 @@ import { tap, toArray, } from 'rxjs/operators'; -import { HeadlessChromiumDriverFactory } from '../../../../browsers'; +import { HeadlessChromiumDriverFactory } from '../../browsers'; import { CaptureConfig, ElementsPositionAndAttribute, ScreenshotObservableOpts, ScreenshotResults, ScreenshotsObservableFn, -} from '../../../../types'; -import { DEFAULT_PAGELOAD_SELECTOR } from '../../constants'; +} from '../../types'; +import { DEFAULT_PAGELOAD_SELECTOR } from './constants'; import { getElementPositionAndAttributes } from './get_element_position_data'; import { getNumberOfItems } from './get_number_of_items'; import { getScreenshots } from './get_screenshots'; diff --git a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/open_url.ts b/x-pack/plugins/reporting/server/lib/screenshots/open_url.ts similarity index 85% rename from x-pack/plugins/reporting/server/export_types/common/lib/screenshots/open_url.ts rename to x-pack/plugins/reporting/server/lib/screenshots/open_url.ts index bd7e8c508c118..c21ef3b91fab3 100644 --- a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/open_url.ts +++ b/x-pack/plugins/reporting/server/lib/screenshots/open_url.ts @@ -5,9 +5,9 @@ */ import { i18n } from '@kbn/i18n'; -import { HeadlessChromiumDriver } from '../../../../browsers'; -import { LevelLogger, startTrace } from '../../../../lib'; -import { CaptureConfig, ConditionalHeaders } from '../../../../types'; +import { HeadlessChromiumDriver } from '../../browsers'; +import { CaptureConfig, ConditionalHeaders } from '../../types'; +import { LevelLogger, startTrace } from '../'; export const openUrl = async ( captureConfig: CaptureConfig, diff --git a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/wait_for_render.ts b/x-pack/plugins/reporting/server/lib/screenshots/wait_for_render.ts similarity index 92% rename from x-pack/plugins/reporting/server/export_types/common/lib/screenshots/wait_for_render.ts rename to x-pack/plugins/reporting/server/lib/screenshots/wait_for_render.ts index b6519e914430a..f36a7b6f73664 100644 --- a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/wait_for_render.ts +++ b/x-pack/plugins/reporting/server/lib/screenshots/wait_for_render.ts @@ -5,10 +5,10 @@ */ import { i18n } from '@kbn/i18n'; -import { HeadlessChromiumDriver } from '../../../../browsers'; -import { LevelLogger, startTrace } from '../../../../lib'; -import { CaptureConfig } from '../../../../types'; -import { LayoutInstance } from '../../layouts'; +import { HeadlessChromiumDriver } from '../../browsers'; +import { CaptureConfig } from '../../types'; +import { LevelLogger, startTrace } from '../'; +import { LayoutInstance } from '../layouts'; import { CONTEXT_WAITFORRENDER } from './constants'; export const waitForRenderComplete = async ( diff --git a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/wait_for_visualizations.ts b/x-pack/plugins/reporting/server/lib/screenshots/wait_for_visualizations.ts similarity index 90% rename from x-pack/plugins/reporting/server/export_types/common/lib/screenshots/wait_for_visualizations.ts rename to x-pack/plugins/reporting/server/lib/screenshots/wait_for_visualizations.ts index 75a7b6516473c..779d00442522d 100644 --- a/x-pack/plugins/reporting/server/export_types/common/lib/screenshots/wait_for_visualizations.ts +++ b/x-pack/plugins/reporting/server/lib/screenshots/wait_for_visualizations.ts @@ -5,10 +5,10 @@ */ import { i18n } from '@kbn/i18n'; -import { HeadlessChromiumDriver } from '../../../../browsers'; -import { LevelLogger, startTrace } from '../../../../lib'; -import { CaptureConfig } from '../../../../types'; -import { LayoutInstance } from '../../layouts'; +import { HeadlessChromiumDriver } from '../../browsers'; +import { LevelLogger, startTrace } from '../'; +import { CaptureConfig } from '../../types'; +import { LayoutInstance } from '../layouts'; import { CONTEXT_WAITFORELEMENTSTOBEINDOM } from './constants'; type SelectorArgs = Record; diff --git a/x-pack/plugins/reporting/server/lib/store/store.ts b/x-pack/plugins/reporting/server/lib/store/store.ts index 1cb964a7bbfac..0f1ed83b71767 100644 --- a/x-pack/plugins/reporting/server/lib/store/store.ts +++ b/x-pack/plugins/reporting/server/lib/store/store.ts @@ -7,8 +7,8 @@ import { ElasticsearchServiceSetup } from 'src/core/server'; import { LevelLogger } from '../'; import { ReportingCore } from '../../'; -import { LayoutInstance } from '../../export_types/common/layouts'; import { indexTimestamp } from './index_timestamp'; +import { LayoutInstance } from '../layouts'; import { mapping } from './mapping'; import { Report } from './report'; diff --git a/x-pack/plugins/reporting/server/lib/validate/index.ts b/x-pack/plugins/reporting/server/lib/validate/index.ts index 7c439d6023d5f..d20df6b7315be 100644 --- a/x-pack/plugins/reporting/server/lib/validate/index.ts +++ b/x-pack/plugins/reporting/server/lib/validate/index.ts @@ -8,7 +8,7 @@ import { i18n } from '@kbn/i18n'; import { ElasticsearchServiceSetup } from 'kibana/server'; import { ReportingConfig } from '../../'; import { HeadlessChromiumDriverFactory } from '../../browsers/chromium/driver_factory'; -import { LevelLogger } from '../../lib'; +import { LevelLogger } from '../'; import { validateBrowser } from './validate_browser'; import { validateMaxContentLength } from './validate_max_content_length'; diff --git a/x-pack/plugins/reporting/server/lib/validate/validate_max_content_length.ts b/x-pack/plugins/reporting/server/lib/validate/validate_max_content_length.ts index 6d34937d9bd75..c38c6e5297854 100644 --- a/x-pack/plugins/reporting/server/lib/validate/validate_max_content_length.ts +++ b/x-pack/plugins/reporting/server/lib/validate/validate_max_content_length.ts @@ -8,7 +8,7 @@ import numeral from '@elastic/numeral'; import { ElasticsearchServiceSetup } from 'kibana/server'; import { defaults, get } from 'lodash'; import { ReportingConfig } from '../../'; -import { LevelLogger } from '../../lib'; +import { LevelLogger } from '../'; const KIBANA_MAX_SIZE_BYTES_PATH = 'csv.maxSizeBytes'; const ES_MAX_SIZE_BYTES_PATH = 'http.max_content_length'; diff --git a/x-pack/plugins/reporting/server/routes/generate_from_savedobject_immediate.ts b/x-pack/plugins/reporting/server/routes/generate_from_savedobject_immediate.ts index 773295deea954..8250ca462049b 100644 --- a/x-pack/plugins/reporting/server/routes/generate_from_savedobject_immediate.ts +++ b/x-pack/plugins/reporting/server/routes/generate_from_savedobject_immediate.ts @@ -7,8 +7,8 @@ import { schema } from '@kbn/config-schema'; import { ReportingCore } from '../'; import { API_BASE_GENERATE_V1 } from '../../common/constants'; -import { scheduleTaskFnFactory } from '../export_types/csv_from_savedobject/server/create_job'; -import { runTaskFnFactory } from '../export_types/csv_from_savedobject/server/execute_job'; +import { scheduleTaskFnFactory } from '../export_types/csv_from_savedobject/create_job'; +import { runTaskFnFactory } from '../export_types/csv_from_savedobject/execute_job'; import { LevelLogger as Logger } from '../lib'; import { TaskRunResult } from '../types'; import { authorizedUserPreRoutingFactory } from './lib/authorized_user_pre_routing'; diff --git a/x-pack/plugins/reporting/server/routes/jobs.ts b/x-pack/plugins/reporting/server/routes/jobs.ts index 90185f0736ed8..4033719b053ba 100644 --- a/x-pack/plugins/reporting/server/routes/jobs.ts +++ b/x-pack/plugins/reporting/server/routes/jobs.ts @@ -8,8 +8,8 @@ import { schema } from '@kbn/config-schema'; import Boom from 'boom'; import { ReportingCore } from '../'; import { API_BASE_URL } from '../../common/constants'; -import { jobsQueryFactory } from '../lib/jobs_query'; import { authorizedUserPreRoutingFactory } from './lib/authorized_user_pre_routing'; +import { jobsQueryFactory } from './lib/jobs_query'; import { deleteJobResponseHandlerFactory, downloadJobResponseHandlerFactory, diff --git a/x-pack/plugins/reporting/server/routes/lib/authorized_user_pre_routing.ts b/x-pack/plugins/reporting/server/routes/lib/authorized_user_pre_routing.ts index 74737b0a5d1e2..3758eafc6d718 100644 --- a/x-pack/plugins/reporting/server/routes/lib/authorized_user_pre_routing.ts +++ b/x-pack/plugins/reporting/server/routes/lib/authorized_user_pre_routing.ts @@ -6,8 +6,8 @@ import { RequestHandler, RouteMethod } from 'src/core/server'; import { AuthenticatedUser } from '../../../../security/server'; -import { getUserFactory } from '../../lib/get_user'; import { ReportingCore } from '../../core'; +import { getUserFactory } from './get_user'; type ReportingUser = AuthenticatedUser | null; const superuserRole = 'superuser'; diff --git a/x-pack/plugins/reporting/server/lib/get_user.ts b/x-pack/plugins/reporting/server/routes/lib/get_user.ts similarity index 87% rename from x-pack/plugins/reporting/server/lib/get_user.ts rename to x-pack/plugins/reporting/server/routes/lib/get_user.ts index 49d15a7c55100..fd56e8cfc28c7 100644 --- a/x-pack/plugins/reporting/server/lib/get_user.ts +++ b/x-pack/plugins/reporting/server/routes/lib/get_user.ts @@ -5,7 +5,7 @@ */ import { KibanaRequest } from 'kibana/server'; -import { SecurityPluginSetup } from '../../../security/server'; +import { SecurityPluginSetup } from '../../../../security/server'; export function getUserFactory(security?: SecurityPluginSetup) { return (request: KibanaRequest) => { diff --git a/x-pack/plugins/reporting/server/routes/lib/job_response_handler.ts b/x-pack/plugins/reporting/server/routes/lib/job_response_handler.ts index 651f1c34fee6c..df346c8b9b832 100644 --- a/x-pack/plugins/reporting/server/routes/lib/job_response_handler.ts +++ b/x-pack/plugins/reporting/server/routes/lib/job_response_handler.ts @@ -8,8 +8,8 @@ import { kibanaResponseFactory } from 'kibana/server'; import { ReportingCore } from '../../'; import { AuthenticatedUser } from '../../../../security/server'; import { WHITELISTED_JOB_CONTENT_TYPES } from '../../../common/constants'; -import { jobsQueryFactory } from '../../lib/jobs_query'; import { getDocumentPayloadFactory } from './get_document_payload'; +import { jobsQueryFactory } from './jobs_query'; interface JobResponseHandlerParams { docId: string; diff --git a/x-pack/plugins/reporting/server/lib/jobs_query.ts b/x-pack/plugins/reporting/server/routes/lib/jobs_query.ts similarity index 96% rename from x-pack/plugins/reporting/server/lib/jobs_query.ts rename to x-pack/plugins/reporting/server/routes/lib/jobs_query.ts index f4670847260ee..f3955b4871b31 100644 --- a/x-pack/plugins/reporting/server/lib/jobs_query.ts +++ b/x-pack/plugins/reporting/server/routes/lib/jobs_query.ts @@ -7,9 +7,9 @@ import { i18n } from '@kbn/i18n'; import { errors as elasticsearchErrors } from 'elasticsearch'; import { get } from 'lodash'; -import { ReportingCore } from '../'; -import { AuthenticatedUser } from '../../../security/server'; -import { JobSource } from '../types'; +import { ReportingCore } from '../../'; +import { AuthenticatedUser } from '../../../../security/server'; +import { JobSource } from '../../types'; const esErrors = elasticsearchErrors as Record; const defaultSize = 10; diff --git a/x-pack/plugins/reporting/server/test_helpers/create_mock_browserdriverfactory.ts b/x-pack/plugins/reporting/server/test_helpers/create_mock_browserdriverfactory.ts index 97e22e2ca2863..db10d96db2263 100644 --- a/x-pack/plugins/reporting/server/test_helpers/create_mock_browserdriverfactory.ts +++ b/x-pack/plugins/reporting/server/test_helpers/create_mock_browserdriverfactory.ts @@ -7,8 +7,8 @@ import { Page } from 'puppeteer'; import * as Rx from 'rxjs'; import { chromium, HeadlessChromiumDriver, HeadlessChromiumDriverFactory } from '../browsers'; -import * as contexts from '../export_types/common/lib/screenshots/constants'; import { LevelLogger } from '../lib'; +import * as contexts from '../lib/screenshots/constants'; import { CaptureConfig, ElementsPositionAndAttribute } from '../types'; interface CreateMockBrowserDriverFactoryOpts { diff --git a/x-pack/plugins/reporting/server/test_helpers/create_mock_layoutinstance.ts b/x-pack/plugins/reporting/server/test_helpers/create_mock_layoutinstance.ts index 22da9eb418e9a..c9dbbda9fd68d 100644 --- a/x-pack/plugins/reporting/server/test_helpers/create_mock_layoutinstance.ts +++ b/x-pack/plugins/reporting/server/test_helpers/create_mock_layoutinstance.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { createLayout, LayoutInstance, LayoutTypes } from '../export_types/common/layouts'; +import { createLayout, LayoutInstance, LayoutTypes } from '../lib/layouts'; import { CaptureConfig } from '../types'; export const createMockLayoutInstance = (captureConfig: CaptureConfig) => { diff --git a/x-pack/plugins/reporting/server/types.ts b/x-pack/plugins/reporting/server/types.ts index 667c1546c6147..ff597b53ea0b0 100644 --- a/x-pack/plugins/reporting/server/types.ts +++ b/x-pack/plugins/reporting/server/types.ts @@ -15,8 +15,8 @@ import { SecurityPluginSetup } from '../../security/server'; import { JobStatus } from '../common/types'; import { ReportingConfigType } from './config'; import { ReportingCore } from './core'; -import { LayoutInstance } from './export_types/common/layouts'; import { LevelLogger } from './lib'; +import { LayoutInstance } from './lib/layouts'; /* * Routing / API types diff --git a/x-pack/plugins/rollup/public/plugin.ts b/x-pack/plugins/rollup/public/plugin.ts index b55760c5cc5aa..73ee675b089c8 100644 --- a/x-pack/plugins/rollup/public/plugin.ts +++ b/x-pack/plugins/rollup/public/plugin.ts @@ -16,7 +16,7 @@ import { FeatureCatalogueCategory, HomePublicPluginSetup, } from '../../../../src/plugins/home/public'; -import { ManagementSetup, ManagementSectionId } from '../../../../src/plugins/management/public'; +import { ManagementSetup } from '../../../../src/plugins/management/public'; import { IndexManagementPluginSetup } from '../../index_management/public'; import { IndexPatternManagementSetup } from '../../../../src/plugins/index_pattern_management/public'; // @ts-ignore @@ -75,7 +75,7 @@ export class RollupPlugin implements Plugin { }); } - management.sections.getSection(ManagementSectionId.Data).registerApp({ + management.sections.section.data.registerApp({ id: 'rollup_jobs', title: i18n.translate('xpack.rollupJobs.appTitle', { defaultMessage: 'Rollup Jobs' }), order: 4, diff --git a/x-pack/plugins/security/kibana.json b/x-pack/plugins/security/kibana.json index 0daab9d5dbce3..064ff5b6a6711 100644 --- a/x-pack/plugins/security/kibana.json +++ b/x-pack/plugins/security/kibana.json @@ -9,10 +9,8 @@ "ui": true, "requiredBundles": [ "home", - "management", "kibanaReact", "spaces", - "esUiShared", - "management" + "esUiShared" ] } diff --git a/x-pack/plugins/security/public/management/management_service.test.ts b/x-pack/plugins/security/public/management/management_service.test.ts index c707206569bf5..ce93fb7c98f41 100644 --- a/x-pack/plugins/security/public/management/management_service.test.ts +++ b/x-pack/plugins/security/public/management/management_service.test.ts @@ -8,8 +8,9 @@ import { BehaviorSubject } from 'rxjs'; import { ManagementApp, ManagementSetup, - ManagementStart, + DefinedSections, } from '../../../../../src/plugins/management/public'; +import { createManagementSectionMock } from '../../../../../src/plugins/management/public/mocks'; import { SecurityLicenseFeatures } from '../../common/licensing/license_features'; import { ManagementService } from './management_service'; import { usersManagementApp } from './users'; @@ -21,7 +22,7 @@ import { rolesManagementApp } from './roles'; import { apiKeysManagementApp } from './api_keys'; import { roleMappingsManagementApp } from './role_mappings'; -const mockSection = { registerApp: jest.fn() }; +const mockSection = createManagementSectionMock(); describe('ManagementService', () => { describe('setup()', () => { @@ -32,8 +33,10 @@ describe('ManagementService', () => { const managementSetup: ManagementSetup = { sections: { - register: jest.fn(), - getSection: jest.fn().mockReturnValue(mockSection), + register: jest.fn(() => mockSection), + section: { + security: mockSection, + } as DefinedSections, }, }; @@ -88,8 +91,10 @@ describe('ManagementService', () => { const managementSetup: ManagementSetup = { sections: { - register: jest.fn(), - getSection: jest.fn().mockReturnValue(mockSection), + register: jest.fn(() => mockSection), + section: { + security: mockSection, + } as DefinedSections, }, }; @@ -116,6 +121,7 @@ describe('ManagementService', () => { }), } as unknown) as jest.Mocked; }; + mockSection.getApp = jest.fn().mockImplementation((id) => mockApps.get(id)); const mockApps = new Map>([ [usersManagementApp.id, getMockedApp()], [rolesManagementApp.id, getMockedApp()], @@ -123,19 +129,7 @@ describe('ManagementService', () => { [roleMappingsManagementApp.id, getMockedApp()], ] as Array<[string, jest.Mocked]>); - const managementStart: ManagementStart = { - sections: { - getSection: jest - .fn() - .mockReturnValue({ getApp: jest.fn().mockImplementation((id) => mockApps.get(id)) }), - getAllSections: jest.fn(), - getSectionsEnabled: jest.fn(), - }, - }; - - service.start({ - management: managementStart, - }); + service.start(); return { mockApps, diff --git a/x-pack/plugins/security/public/management/management_service.ts b/x-pack/plugins/security/public/management/management_service.ts index 148d2855ba9b7..199fd917da071 100644 --- a/x-pack/plugins/security/public/management/management_service.ts +++ b/x-pack/plugins/security/public/management/management_service.ts @@ -9,8 +9,7 @@ import { StartServicesAccessor, FatalErrorsSetup } from 'src/core/public'; import { ManagementApp, ManagementSetup, - ManagementStart, - ManagementSectionId, + ManagementSection, } from '../../../../../src/plugins/management/public'; import { SecurityLicense } from '../../common/licensing'; import { AuthenticationServiceSetup } from '../authentication'; @@ -28,30 +27,26 @@ interface SetupParams { getStartServices: StartServicesAccessor; } -interface StartParams { - management: ManagementStart; -} - export class ManagementService { private license!: SecurityLicense; private licenseFeaturesSubscription?: Subscription; + private securitySection?: ManagementSection; setup({ getStartServices, management, authc, license, fatalErrors }: SetupParams) { this.license = license; + this.securitySection = management.sections.section.security; - const securitySection = management.sections.getSection(ManagementSectionId.Security); - - securitySection.registerApp(usersManagementApp.create({ authc, getStartServices })); - securitySection.registerApp( + this.securitySection.registerApp(usersManagementApp.create({ authc, getStartServices })); + this.securitySection.registerApp( rolesManagementApp.create({ fatalErrors, license, getStartServices }) ); - securitySection.registerApp(apiKeysManagementApp.create({ getStartServices })); - securitySection.registerApp(roleMappingsManagementApp.create({ getStartServices })); + this.securitySection.registerApp(apiKeysManagementApp.create({ getStartServices })); + this.securitySection.registerApp(roleMappingsManagementApp.create({ getStartServices })); } - start({ management }: StartParams) { + start() { this.licenseFeaturesSubscription = this.license.features$.subscribe(async (features) => { - const securitySection = management.sections.getSection(ManagementSectionId.Security); + const securitySection = this.securitySection!; const securityManagementAppsStatuses: Array<[ManagementApp, boolean]> = [ [securitySection.getApp(usersManagementApp.id)!, features.showLinks], diff --git a/x-pack/plugins/security/public/plugin.test.tsx b/x-pack/plugins/security/public/plugin.test.tsx index 7c57c4dd997a2..8cec4fbc2f5a2 100644 --- a/x-pack/plugins/security/public/plugin.test.tsx +++ b/x-pack/plugins/security/public/plugin.test.tsx @@ -33,7 +33,9 @@ describe('Security Plugin', () => { coreMock.createSetup({ basePath: '/some-base-path' }) as CoreSetup< PluginStartDependencies >, - { licensing: licensingMock.createSetup() } + { + licensing: licensingMock.createSetup(), + } ) ).toEqual({ __legacyCompat: { logoutUrl: '/some-base-path/logout', tenant: '/some-base-path' }, @@ -117,7 +119,6 @@ describe('Security Plugin', () => { }); expect(startManagementServiceMock).toHaveBeenCalledTimes(1); - expect(startManagementServiceMock).toHaveBeenCalledWith({ management: managementStartMock }); }); }); diff --git a/x-pack/plugins/security/public/plugin.tsx b/x-pack/plugins/security/public/plugin.tsx index da69dd051c11d..bef183bd97e8c 100644 --- a/x-pack/plugins/security/public/plugin.tsx +++ b/x-pack/plugins/security/public/plugin.tsx @@ -139,9 +139,8 @@ export class SecurityPlugin public start(core: CoreStart, { management }: PluginStartDependencies) { this.sessionTimeout.start(); this.navControlService.start({ core }); - if (management) { - this.managementService.start({ management }); + this.managementService.start(); } } 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 631a6f9ab213c..5164099f9ff67 100644 --- a/x-pack/plugins/security/server/authentication/api_keys.test.ts +++ b/x-pack/plugins/security/server/authentication/api_keys.test.ts @@ -162,7 +162,10 @@ describe('API Keys', () => { describe('grantAsInternalUser()', () => { it('returns null when security feature is disabled', async () => { mockLicense.isEnabled.mockReturnValue(false); - const result = await apiKeys.grantAsInternalUser(httpServerMock.createKibanaRequest()); + const result = await apiKeys.grantAsInternalUser(httpServerMock.createKibanaRequest(), { + name: 'test_api_key', + role_descriptors: {}, + }); expect(result).toBeNull(); expect(mockClusterClient.callAsInternalUser).not.toHaveBeenCalled(); @@ -174,21 +177,33 @@ describe('API Keys', () => { id: '123', name: 'key-name', api_key: 'abc123', + expires: '1d', }); const result = await apiKeys.grantAsInternalUser( httpServerMock.createKibanaRequest({ headers: { authorization: `Basic ${encodeToBase64('foo:bar')}`, }, - }) + }), + { + name: 'test_api_key', + role_descriptors: { foo: true }, + expiration: '1d', + } ); expect(result).toEqual({ api_key: 'abc123', id: '123', name: 'key-name', + expires: '1d', }); expect(mockClusterClient.callAsInternalUser).toHaveBeenCalledWith('shield.grantAPIKey', { body: { + api_key: { + name: 'test_api_key', + role_descriptors: { foo: true }, + expiration: '1d', + }, grant_type: 'password', username: 'foo', password: 'bar', @@ -208,7 +223,12 @@ describe('API Keys', () => { headers: { authorization: `Bearer foo-access-token`, }, - }) + }), + { + name: 'test_api_key', + role_descriptors: { foo: true }, + expiration: '1d', + } ); expect(result).toEqual({ api_key: 'abc123', @@ -217,6 +237,11 @@ describe('API Keys', () => { }); expect(mockClusterClient.callAsInternalUser).toHaveBeenCalledWith('shield.grantAPIKey', { body: { + api_key: { + name: 'test_api_key', + role_descriptors: { foo: true }, + expiration: '1d', + }, grant_type: 'access_token', access_token: 'foo-access-token', }, @@ -231,7 +256,12 @@ describe('API Keys', () => { headers: { authorization: `Digest username="foo"`, }, - }) + }), + { + name: 'test_api_key', + role_descriptors: { foo: true }, + expiration: '1d', + } ) ).rejects.toThrowErrorMatchingInlineSnapshot( `"Unsupported scheme \\"Digest\\" for granting API Key"` diff --git a/x-pack/plugins/security/server/authentication/api_keys.ts b/x-pack/plugins/security/server/authentication/api_keys.ts index 3b6aee72651e2..19922ce3c890d 100644 --- a/x-pack/plugins/security/server/authentication/api_keys.ts +++ b/x-pack/plugins/security/server/authentication/api_keys.ts @@ -29,6 +29,7 @@ export interface CreateAPIKeyParams { } interface GrantAPIKeyParams { + api_key: CreateAPIKeyParams; grant_type: 'password' | 'access_token'; username?: string; password?: string; @@ -188,7 +189,7 @@ export class APIKeys { * Tries to grant an API key for the current user. * @param request Request instance. */ - async grantAsInternalUser(request: KibanaRequest) { + async grantAsInternalUser(request: KibanaRequest, createParams: CreateAPIKeyParams) { if (!this.license.isEnabled()) { return null; } @@ -200,7 +201,7 @@ export class APIKeys { `Unable to grant an API Key, request does not contain an authorization header` ); } - const params = this.getGrantParams(authorizationHeader); + const params = this.getGrantParams(createParams, authorizationHeader); // User needs `manage_api_key` or `grant_api_key` privilege to use this API let result: GrantAPIKeyResult; @@ -281,9 +282,13 @@ export class APIKeys { return disabledFeature === 'api_keys'; } - private getGrantParams(authorizationHeader: HTTPAuthorizationHeader): GrantAPIKeyParams { + private getGrantParams( + createParams: CreateAPIKeyParams, + authorizationHeader: HTTPAuthorizationHeader + ): GrantAPIKeyParams { if (authorizationHeader.scheme.toLowerCase() === 'bearer') { return { + api_key: createParams, grant_type: 'access_token', access_token: authorizationHeader.credentials, }; @@ -294,6 +299,7 @@ export class APIKeys { authorizationHeader.credentials ); return { + api_key: createParams, grant_type: 'password', username: basicCredentials.username, password: basicCredentials.password, diff --git a/x-pack/plugins/security/server/authentication/index.test.ts b/x-pack/plugins/security/server/authentication/index.test.ts index 56d44e6628a87..a125d9a62afb7 100644 --- a/x-pack/plugins/security/server/authentication/index.test.ts +++ b/x-pack/plugins/security/server/authentication/index.test.ts @@ -374,7 +374,10 @@ describe('setupAuthentication()', () => { }); describe('grantAPIKeyAsInternalUser()', () => { - let grantAPIKeyAsInternalUser: (request: KibanaRequest) => Promise; + let grantAPIKeyAsInternalUser: ( + request: KibanaRequest, + params: CreateAPIKeyParams + ) => Promise; beforeEach(async () => { grantAPIKeyAsInternalUser = (await setupAuthentication(mockSetupAuthenticationParams)) .grantAPIKeyAsInternalUser; @@ -384,10 +387,13 @@ describe('setupAuthentication()', () => { const request = httpServerMock.createKibanaRequest(); const apiKeysInstance = jest.requireMock('./api_keys').APIKeys.mock.instances[0]; apiKeysInstance.grantAsInternalUser.mockResolvedValueOnce({ api_key: 'foo' }); - await expect(grantAPIKeyAsInternalUser(request)).resolves.toEqual({ + + const createParams = { name: 'test_key', role_descriptors: {} }; + + await expect(grantAPIKeyAsInternalUser(request, createParams)).resolves.toEqual({ api_key: 'foo', }); - expect(apiKeysInstance.grantAsInternalUser).toHaveBeenCalledWith(request); + expect(apiKeysInstance.grantAsInternalUser).toHaveBeenCalledWith(request, createParams); }); }); diff --git a/x-pack/plugins/security/server/authentication/index.ts b/x-pack/plugins/security/server/authentication/index.ts index 659a378388a13..ed631e221b7a3 100644 --- a/x-pack/plugins/security/server/authentication/index.ts +++ b/x-pack/plugins/security/server/authentication/index.ts @@ -187,7 +187,8 @@ export async function setupAuthentication({ areAPIKeysEnabled: () => apiKeys.areAPIKeysEnabled(), createAPIKey: (request: KibanaRequest, params: CreateAPIKeyParams) => apiKeys.create(request, params), - grantAPIKeyAsInternalUser: (request: KibanaRequest) => apiKeys.grantAsInternalUser(request), + grantAPIKeyAsInternalUser: (request: KibanaRequest, params: CreateAPIKeyParams) => + apiKeys.grantAsInternalUser(request, params), invalidateAPIKey: (request: KibanaRequest, params: InvalidateAPIKeyParams) => apiKeys.invalidate(request, params), invalidateAPIKeyAsInternalUser: (params: InvalidateAPIKeyParams) => diff --git a/x-pack/plugins/security_solution/common/constants.ts b/x-pack/plugins/security_solution/common/constants.ts index 516ee19dd3b03..e5dd109007eab 100644 --- a/x-pack/plugins/security_solution/common/constants.ts +++ b/x-pack/plugins/security_solution/common/constants.ts @@ -117,6 +117,7 @@ export const TIMELINE_URL = '/api/timeline'; export const TIMELINE_DRAFT_URL = `${TIMELINE_URL}/_draft`; export const TIMELINE_EXPORT_URL = `${TIMELINE_URL}/_export`; export const TIMELINE_IMPORT_URL = `${TIMELINE_URL}/_import`; +export const TIMELINE_PREPACKAGED_URL = `${TIMELINE_URL}/_prepackaged`; /** * Default signals index key for kibana.dev.yml diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/common/schemas.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/common/schemas.ts index 6e43bd645fd7b..273ea72a2ffe3 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/common/schemas.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/common/schemas.ts @@ -255,6 +255,7 @@ export const severity_mapping_item = t.exact( severity, }) ); +export type SeverityMappingItem = t.TypeOf; export const severity_mapping = t.array(severity_mapping_item); export type SeverityMapping = t.TypeOf; @@ -275,7 +276,12 @@ export type To = t.TypeOf; export const toOrUndefined = t.union([to, t.undefined]); export type ToOrUndefined = t.TypeOf; -export const type = t.keyof({ machine_learning: null, query: null, saved_query: null }); +export const type = t.keyof({ + machine_learning: null, + query: null, + saved_query: null, + threshold: null, +}); export type Type = t.TypeOf; export const typeOrUndefined = t.union([type, t.undefined]); @@ -369,6 +375,17 @@ export type Threat = t.TypeOf; export const threatOrUndefined = t.union([threat, t.undefined]); export type ThreatOrUndefined = t.TypeOf; +export const threshold = t.exact( + t.type({ + field: t.string, + value: PositiveIntegerGreaterThanZero, + }) +); +export type Threshold = t.TypeOf; + +export const thresholdOrUndefined = t.union([threshold, t.undefined]); +export type ThresholdOrUndefined = t.TypeOf; + export const created_at = IsoDateString; export const updated_at = IsoDateString; export const updated_by = t.string; @@ -407,6 +424,11 @@ export const rules_custom_installed = PositiveInteger; export const rules_not_installed = PositiveInteger; export const rules_not_updated = PositiveInteger; +export const timelines_installed = PositiveInteger; +export const timelines_updated = PositiveInteger; +export const timelines_not_installed = PositiveInteger; +export const timelines_not_updated = PositiveInteger; + export const note = t.string; export type Note = t.TypeOf; diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/add_prepackaged_rules_schema.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/add_prepackaged_rules_schema.ts index bf96be5e688fa..aebc3361f6e49 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/add_prepackaged_rules_schema.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/add_prepackaged_rules_schema.ts @@ -25,6 +25,7 @@ import { To, type, Threat, + threshold, ThrottleOrNull, note, References, @@ -111,6 +112,7 @@ export const addPrepackagedRulesSchema = t.intersection([ tags: DefaultStringArray, // defaults to empty string array if not set during decode to: DefaultToString, // defaults to "now" if not set during decode threat: DefaultThreatArray, // defaults to empty array if not set during decode + threshold, // defaults to "undefined" if not set during decode throttle: DefaultThrottleNull, // defaults to "null" if not set during decode timestamp_override, // defaults to "undefined" if not set during decode references: DefaultStringArray, // defaults to empty array of strings if not set during decode diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/add_prepackaged_rules_type_dependents.test.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/add_prepackaged_rules_type_dependents.test.ts index 793d4b04ed0e5..f844d0e86e1f9 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/add_prepackaged_rules_type_dependents.test.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/add_prepackaged_rules_type_dependents.test.ts @@ -8,7 +8,7 @@ import { AddPrepackagedRulesSchema } from './add_prepackaged_rules_schema'; import { addPrepackagedRuleValidateTypeDependents } from './add_prepackaged_rules_type_dependents'; import { getAddPrepackagedRulesSchemaMock } from './add_prepackaged_rules_schema.mock'; -describe('create_rules_type_dependents', () => { +describe('add_prepackaged_rules_type_dependents', () => { test('saved_id is required when type is saved_query and will not validate without out', () => { const schema: AddPrepackagedRulesSchema = { ...getAddPrepackagedRulesSchemaMock(), @@ -68,4 +68,26 @@ describe('create_rules_type_dependents', () => { const errors = addPrepackagedRuleValidateTypeDependents(schema); expect(errors).toEqual(['when "timeline_title" exists, "timeline_id" must also exist']); }); + + test('threshold is required when type is threshold and validates with it', () => { + const schema: AddPrepackagedRulesSchema = { + ...getAddPrepackagedRulesSchemaMock(), + type: 'threshold', + }; + const errors = addPrepackagedRuleValidateTypeDependents(schema); + expect(errors).toEqual(['when "type" is "threshold", "threshold" is required']); + }); + + test('threshold.value is required and has to be bigger than 0 when type is threshold and validates with it', () => { + const schema: AddPrepackagedRulesSchema = { + ...getAddPrepackagedRulesSchemaMock(), + type: 'threshold', + threshold: { + field: '', + value: -1, + }, + }; + const errors = addPrepackagedRuleValidateTypeDependents(schema); + expect(errors).toEqual(['"threshold.value" has to be bigger than 0']); + }); }); diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/add_prepackaged_rules_type_dependents.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/add_prepackaged_rules_type_dependents.ts index 2788c331154d2..6a51f724fc9e6 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/add_prepackaged_rules_type_dependents.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/add_prepackaged_rules_type_dependents.ts @@ -92,6 +92,19 @@ export const validateTimelineTitle = (rule: AddPrepackagedRulesSchema): string[] return []; }; +export const validateThreshold = (rule: AddPrepackagedRulesSchema): string[] => { + if (rule.type === 'threshold') { + if (!rule.threshold) { + return ['when "type" is "threshold", "threshold" is required']; + } else if (rule.threshold.value <= 0) { + return ['"threshold.value" has to be bigger than 0']; + } else { + return []; + } + } + return []; +}; + export const addPrepackagedRuleValidateTypeDependents = ( schema: AddPrepackagedRulesSchema ): string[] => { @@ -103,5 +116,6 @@ export const addPrepackagedRuleValidateTypeDependents = ( ...validateMachineLearningJobId(schema), ...validateTimelineId(schema), ...validateTimelineTitle(schema), + ...validateThreshold(schema), ]; }; diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/create_rules_schema.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/create_rules_schema.ts index 0debe01e5a4d7..308b3c24010fb 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/create_rules_schema.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/create_rules_schema.ts @@ -28,6 +28,7 @@ import { To, type, Threat, + threshold, ThrottleOrNull, note, Version, @@ -106,6 +107,7 @@ export const createRulesSchema = t.intersection([ tags: DefaultStringArray, // defaults to empty string array if not set during decode to: DefaultToString, // defaults to "now" if not set during decode threat: DefaultThreatArray, // defaults to empty array if not set during decode + threshold, // defaults to "undefined" if not set during decode throttle: DefaultThrottleNull, // defaults to "null" if not set during decode timestamp_override, // defaults to "undefined" if not set during decode references: DefaultStringArray, // defaults to empty array of strings if not set during decode diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/create_rules_type_dependents.test.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/create_rules_type_dependents.test.ts index ebf0b2e591ca9..43f0901912271 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/create_rules_type_dependents.test.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/create_rules_type_dependents.test.ts @@ -65,4 +65,26 @@ describe('create_rules_type_dependents', () => { const errors = createRuleValidateTypeDependents(schema); expect(errors).toEqual(['when "timeline_title" exists, "timeline_id" must also exist']); }); + + test('threshold is required when type is threshold and validates with it', () => { + const schema: CreateRulesSchema = { + ...getCreateRulesSchemaMock(), + type: 'threshold', + }; + const errors = createRuleValidateTypeDependents(schema); + expect(errors).toEqual(['when "type" is "threshold", "threshold" is required']); + }); + + test('threshold.value is required and has to be bigger than 0 when type is threshold and validates with it', () => { + const schema: CreateRulesSchema = { + ...getCreateRulesSchemaMock(), + type: 'threshold', + threshold: { + field: '', + value: -1, + }, + }; + const errors = createRuleValidateTypeDependents(schema); + expect(errors).toEqual(['"threshold.value" has to be bigger than 0']); + }); }); diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/create_rules_type_dependents.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/create_rules_type_dependents.ts index aad2a2c4a9206..af665ff8c81d2 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/create_rules_type_dependents.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/create_rules_type_dependents.ts @@ -92,6 +92,19 @@ export const validateTimelineTitle = (rule: CreateRulesSchema): string[] => { return []; }; +export const validateThreshold = (rule: CreateRulesSchema): string[] => { + if (rule.type === 'threshold') { + if (!rule.threshold) { + return ['when "type" is "threshold", "threshold" is required']; + } else if (rule.threshold.value <= 0) { + return ['"threshold.value" has to be bigger than 0']; + } else { + return []; + } + } + return []; +}; + export const createRuleValidateTypeDependents = (schema: CreateRulesSchema): string[] => { return [ ...validateAnomalyThreshold(schema), @@ -101,5 +114,6 @@ export const createRuleValidateTypeDependents = (schema: CreateRulesSchema): str ...validateMachineLearningJobId(schema), ...validateTimelineId(schema), ...validateTimelineTitle(schema), + ...validateThreshold(schema), ]; }; diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/import_rules_schema.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/import_rules_schema.ts index f61a1546e3e8a..d141ca56828b6 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/import_rules_schema.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/import_rules_schema.ts @@ -27,6 +27,7 @@ import { To, type, Threat, + threshold, ThrottleOrNull, note, Version, @@ -125,6 +126,7 @@ export const importRulesSchema = t.intersection([ tags: DefaultStringArray, // defaults to empty string array if not set during decode to: DefaultToString, // defaults to "now" if not set during decode threat: DefaultThreatArray, // defaults to empty array if not set during decode + threshold, // defaults to "undefined" if not set during decode throttle: DefaultThrottleNull, // defaults to "null" if not set during decode timestamp_override, // defaults to "undefined" if not set during decode references: DefaultStringArray, // defaults to empty array of strings if not set during decode diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/import_rules_type_dependents.test.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/import_rules_type_dependents.test.ts index f9b989c81e533..4b047ee6b7198 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/import_rules_type_dependents.test.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/import_rules_type_dependents.test.ts @@ -65,4 +65,26 @@ describe('import_rules_type_dependents', () => { const errors = importRuleValidateTypeDependents(schema); expect(errors).toEqual(['when "timeline_title" exists, "timeline_id" must also exist']); }); + + test('threshold is required when type is threshold and validates with it', () => { + const schema: ImportRulesSchema = { + ...getImportRulesSchemaMock(), + type: 'threshold', + }; + const errors = importRuleValidateTypeDependents(schema); + expect(errors).toEqual(['when "type" is "threshold", "threshold" is required']); + }); + + test('threshold.value is required and has to be bigger than 0 when type is threshold and validates with it', () => { + const schema: ImportRulesSchema = { + ...getImportRulesSchemaMock(), + type: 'threshold', + threshold: { + field: '', + value: -1, + }, + }; + const errors = importRuleValidateTypeDependents(schema); + expect(errors).toEqual(['"threshold.value" has to be bigger than 0']); + }); }); diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/import_rules_type_dependents.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/import_rules_type_dependents.ts index 59191a4fe3121..269181449e9e9 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/import_rules_type_dependents.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/import_rules_type_dependents.ts @@ -92,6 +92,19 @@ export const validateTimelineTitle = (rule: ImportRulesSchema): string[] => { return []; }; +export const validateThreshold = (rule: ImportRulesSchema): string[] => { + if (rule.type === 'threshold') { + if (!rule.threshold) { + return ['when "type" is "threshold", "threshold" is required']; + } else if (rule.threshold.value <= 0) { + return ['"threshold.value" has to be bigger than 0']; + } else { + return []; + } + } + return []; +}; + export const importRuleValidateTypeDependents = (schema: ImportRulesSchema): string[] => { return [ ...validateAnomalyThreshold(schema), @@ -101,5 +114,6 @@ export const importRuleValidateTypeDependents = (schema: ImportRulesSchema): str ...validateMachineLearningJobId(schema), ...validateTimelineId(schema), ...validateTimelineTitle(schema), + ...validateThreshold(schema), ]; }; diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/patch_rules_schema.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/patch_rules_schema.ts index 070f3ccfd03b0..dd325c1a5034f 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/patch_rules_schema.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/patch_rules_schema.ts @@ -33,6 +33,7 @@ import { enabled, tags, threat, + threshold, throttle, references, to, @@ -89,6 +90,7 @@ export const patchRulesSchema = t.exact( tags, to, threat, + threshold, throttle, timestamp_override, references, diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/patch_rule_type_dependents.test.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/patch_rules_type_dependents.test.ts similarity index 79% rename from x-pack/plugins/security_solution/common/detection_engine/schemas/request/patch_rule_type_dependents.test.ts rename to x-pack/plugins/security_solution/common/detection_engine/schemas/request/patch_rules_type_dependents.test.ts index a388e69332072..bafaf6f9e2203 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/patch_rule_type_dependents.test.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/patch_rules_type_dependents.test.ts @@ -78,4 +78,26 @@ describe('patch_rules_type_dependents', () => { const errors = patchRuleValidateTypeDependents(schema); expect(errors).toEqual(['either "id" or "rule_id" must be set']); }); + + test('threshold is required when type is threshold and validates with it', () => { + const schema: PatchRulesSchema = { + ...getPatchRulesSchemaMock(), + type: 'threshold', + }; + const errors = patchRuleValidateTypeDependents(schema); + expect(errors).toEqual(['when "type" is "threshold", "threshold" is required']); + }); + + test('threshold.value is required and has to be bigger than 0 when type is threshold and validates with it', () => { + const schema: PatchRulesSchema = { + ...getPatchRulesSchemaMock(), + type: 'threshold', + threshold: { + field: '', + value: -1, + }, + }; + const errors = patchRuleValidateTypeDependents(schema); + expect(errors).toEqual(['"threshold.value" has to be bigger than 0']); + }); }); diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/patch_rules_type_dependents.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/patch_rules_type_dependents.ts index 554cdb822762f..a229771a7c05c 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/patch_rules_type_dependents.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/patch_rules_type_dependents.ts @@ -66,6 +66,19 @@ export const validateId = (rule: PatchRulesSchema): string[] => { } }; +export const validateThreshold = (rule: PatchRulesSchema): string[] => { + if (rule.type === 'threshold') { + if (!rule.threshold) { + return ['when "type" is "threshold", "threshold" is required']; + } else if (rule.threshold.value <= 0) { + return ['"threshold.value" has to be bigger than 0']; + } else { + return []; + } + } + return []; +}; + export const patchRuleValidateTypeDependents = (schema: PatchRulesSchema): string[] => { return [ ...validateId(schema), @@ -73,5 +86,6 @@ export const patchRuleValidateTypeDependents = (schema: PatchRulesSchema): strin ...validateLanguage(schema), ...validateTimelineId(schema), ...validateTimelineTitle(schema), + ...validateThreshold(schema), ]; }; diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/update_rules_schema.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/update_rules_schema.ts index 98082c2de838a..4f284eedef3fd 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/update_rules_schema.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/update_rules_schema.ts @@ -28,6 +28,7 @@ import { To, type, Threat, + threshold, ThrottleOrNull, note, version, @@ -114,6 +115,7 @@ export const updateRulesSchema = t.intersection([ tags: DefaultStringArray, // defaults to empty string array if not set during decode to: DefaultToString, // defaults to "now" if not set during decode threat: DefaultThreatArray, // defaults to empty array if not set during decode + threshold, // defaults to "undefined" if not set during decode throttle: DefaultThrottleNull, // defaults to "null" if not set during decode timestamp_override, // defaults to "undefined" if not set during decode references: DefaultStringArray, // defaults to empty array of strings if not set during decode diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/update_rules_type_dependents.test.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/update_rules_type_dependents.test.ts index a63c8243cb5f1..91b11ea758e93 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/update_rules_type_dependents.test.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/update_rules_type_dependents.test.ts @@ -85,4 +85,26 @@ describe('update_rules_type_dependents', () => { const errors = updateRuleValidateTypeDependents(schema); expect(errors).toEqual(['either "id" or "rule_id" must be set']); }); + + test('threshold is required when type is threshold and validates with it', () => { + const schema: UpdateRulesSchema = { + ...getUpdateRulesSchemaMock(), + type: 'threshold', + }; + const errors = updateRuleValidateTypeDependents(schema); + expect(errors).toEqual(['when "type" is "threshold", "threshold" is required']); + }); + + test('threshold.value is required and has to be bigger than 0 when type is threshold and validates with it', () => { + const schema: UpdateRulesSchema = { + ...getUpdateRulesSchemaMock(), + type: 'threshold', + threshold: { + field: '', + value: -1, + }, + }; + const errors = updateRuleValidateTypeDependents(schema); + expect(errors).toEqual(['"threshold.value" has to be bigger than 0']); + }); }); diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/update_rules_type_dependents.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/update_rules_type_dependents.ts index 9204f727b2660..44182d250c801 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/request/update_rules_type_dependents.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/request/update_rules_type_dependents.ts @@ -102,6 +102,19 @@ export const validateId = (rule: UpdateRulesSchema): string[] => { } }; +export const validateThreshold = (rule: UpdateRulesSchema): string[] => { + if (rule.type === 'threshold') { + if (!rule.threshold) { + return ['when "type" is "threshold", "threshold" is required']; + } else if (rule.threshold.value <= 0) { + return ['"threshold.value" has to be bigger than 0']; + } else { + return []; + } + } + return []; +}; + export const updateRuleValidateTypeDependents = (schema: UpdateRulesSchema): string[] => { return [ ...validateId(schema), @@ -112,5 +125,6 @@ export const updateRuleValidateTypeDependents = (schema: UpdateRulesSchema): str ...validateMachineLearningJobId(schema), ...validateTimelineId(schema), ...validateTimelineTitle(schema), + ...validateThreshold(schema), ]; }; diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/response/prepackaged_rules_schema.test.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/prepackaged_rules_schema.test.ts index fc3f89996daf1..61d3ede852ee1 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/response/prepackaged_rules_schema.test.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/prepackaged_rules_schema.test.ts @@ -6,14 +6,22 @@ import { pipe } from 'fp-ts/lib/pipeable'; import { left } from 'fp-ts/lib/Either'; -import { PrePackagedRulesSchema, prePackagedRulesSchema } from './prepackaged_rules_schema'; +import { + PrePackagedRulesAndTimelinesSchema, + prePackagedRulesAndTimelinesSchema, +} from './prepackaged_rules_schema'; import { exactCheck } from '../../../exact_check'; import { foldLeftRight, getPaths } from '../../../test_utils'; describe('prepackaged_rules_schema', () => { test('it should validate an empty prepackaged response with defaults', () => { - const payload: PrePackagedRulesSchema = { rules_installed: 0, rules_updated: 0 }; - const decoded = prePackagedRulesSchema.decode(payload); + const payload: PrePackagedRulesAndTimelinesSchema = { + rules_installed: 0, + rules_updated: 0, + timelines_installed: 0, + timelines_updated: 0, + }; + const decoded = prePackagedRulesAndTimelinesSchema.decode(payload); const checked = exactCheck(payload, decoded); const message = pipe(checked, foldLeftRight); @@ -22,12 +30,14 @@ describe('prepackaged_rules_schema', () => { }); test('it should not validate an extra invalid field added', () => { - const payload: PrePackagedRulesSchema & { invalid_field: string } = { + const payload: PrePackagedRulesAndTimelinesSchema & { invalid_field: string } = { rules_installed: 0, rules_updated: 0, invalid_field: 'invalid', + timelines_installed: 0, + timelines_updated: 0, }; - const decoded = prePackagedRulesSchema.decode(payload); + const decoded = prePackagedRulesAndTimelinesSchema.decode(payload); const checked = exactCheck(payload, decoded); const message = pipe(checked, foldLeftRight); @@ -36,8 +46,13 @@ describe('prepackaged_rules_schema', () => { }); test('it should NOT validate an empty prepackaged response with a negative "rules_installed" number', () => { - const payload: PrePackagedRulesSchema = { rules_installed: -1, rules_updated: 0 }; - const decoded = prePackagedRulesSchema.decode(payload); + const payload: PrePackagedRulesAndTimelinesSchema = { + rules_installed: -1, + rules_updated: 0, + timelines_installed: 0, + timelines_updated: 0, + }; + const decoded = prePackagedRulesAndTimelinesSchema.decode(payload); const checked = exactCheck(payload, decoded); const message = pipe(checked, foldLeftRight); @@ -48,8 +63,13 @@ describe('prepackaged_rules_schema', () => { }); test('it should NOT validate an empty prepackaged response with a negative "rules_updated"', () => { - const payload: PrePackagedRulesSchema = { rules_installed: 0, rules_updated: -1 }; - const decoded = prePackagedRulesSchema.decode(payload); + const payload: PrePackagedRulesAndTimelinesSchema = { + rules_installed: 0, + rules_updated: -1, + timelines_installed: 0, + timelines_updated: 0, + }; + const decoded = prePackagedRulesAndTimelinesSchema.decode(payload); const checked = exactCheck(payload, decoded); const message = pipe(checked, foldLeftRight); @@ -60,9 +80,14 @@ describe('prepackaged_rules_schema', () => { }); test('it should NOT validate an empty prepackaged response if "rules_installed" is not there', () => { - const payload: PrePackagedRulesSchema = { rules_installed: 0, rules_updated: 0 }; + const payload: PrePackagedRulesAndTimelinesSchema = { + rules_installed: 0, + rules_updated: 0, + timelines_installed: 0, + timelines_updated: 0, + }; delete payload.rules_installed; - const decoded = prePackagedRulesSchema.decode(payload); + const decoded = prePackagedRulesAndTimelinesSchema.decode(payload); const checked = exactCheck(payload, decoded); const message = pipe(checked, foldLeftRight); @@ -73,9 +98,14 @@ describe('prepackaged_rules_schema', () => { }); test('it should NOT validate an empty prepackaged response if "rules_updated" is not there', () => { - const payload: PrePackagedRulesSchema = { rules_installed: 0, rules_updated: 0 }; + const payload: PrePackagedRulesAndTimelinesSchema = { + rules_installed: 0, + rules_updated: 0, + timelines_installed: 0, + timelines_updated: 0, + }; delete payload.rules_updated; - const decoded = prePackagedRulesSchema.decode(payload); + const decoded = prePackagedRulesAndTimelinesSchema.decode(payload); const checked = exactCheck(payload, decoded); const message = pipe(checked, foldLeftRight); diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/response/prepackaged_rules_schema.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/prepackaged_rules_schema.ts index 3b0107c91fee0..73d144500e003 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/response/prepackaged_rules_schema.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/prepackaged_rules_schema.ts @@ -7,14 +7,28 @@ import * as t from 'io-ts'; /* eslint-disable @typescript-eslint/camelcase */ -import { rules_installed, rules_updated } from '../common/schemas'; +import { + rules_installed, + rules_updated, + timelines_installed, + timelines_updated, +} from '../common/schemas'; /* eslint-enable @typescript-eslint/camelcase */ -export const prePackagedRulesSchema = t.exact( - t.type({ - rules_installed, - rules_updated, - }) +const prePackagedRulesSchema = t.type({ + rules_installed, + rules_updated, +}); + +const prePackagedTimelinesSchema = t.type({ + timelines_installed, + timelines_updated, +}); + +export const prePackagedRulesAndTimelinesSchema = t.exact( + t.intersection([prePackagedRulesSchema, prePackagedTimelinesSchema]) ); -export type PrePackagedRulesSchema = t.TypeOf; +export type PrePackagedRulesAndTimelinesSchema = t.TypeOf< + typeof prePackagedRulesAndTimelinesSchema +>; diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/response/prepackaged_rules_status_schema.test.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/prepackaged_rules_status_schema.test.ts index eeae72209829e..09cb7148fe90a 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/response/prepackaged_rules_status_schema.test.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/prepackaged_rules_status_schema.test.ts @@ -7,21 +7,24 @@ import { pipe } from 'fp-ts/lib/pipeable'; import { left } from 'fp-ts/lib/Either'; import { - PrePackagedRulesStatusSchema, - prePackagedRulesStatusSchema, + PrePackagedRulesAndTimelinesStatusSchema, + prePackagedRulesAndTimelinesStatusSchema, } from './prepackaged_rules_status_schema'; import { exactCheck } from '../../../exact_check'; import { foldLeftRight, getPaths } from '../../../test_utils'; describe('prepackaged_rules_schema', () => { test('it should validate an empty prepackaged response with defaults', () => { - const payload: PrePackagedRulesStatusSchema = { + const payload: PrePackagedRulesAndTimelinesStatusSchema = { rules_installed: 0, rules_not_installed: 0, rules_not_updated: 0, rules_custom_installed: 0, + timelines_installed: 0, + timelines_not_installed: 0, + timelines_not_updated: 0, }; - const decoded = prePackagedRulesStatusSchema.decode(payload); + const decoded = prePackagedRulesAndTimelinesStatusSchema.decode(payload); const checked = exactCheck(payload, decoded); const message = pipe(checked, foldLeftRight); @@ -30,14 +33,17 @@ describe('prepackaged_rules_schema', () => { }); test('it should not validate an extra invalid field added', () => { - const payload: PrePackagedRulesStatusSchema & { invalid_field: string } = { + const payload: PrePackagedRulesAndTimelinesStatusSchema & { invalid_field: string } = { rules_installed: 0, rules_not_installed: 0, rules_not_updated: 0, rules_custom_installed: 0, invalid_field: 'invalid', + timelines_installed: 0, + timelines_not_installed: 0, + timelines_not_updated: 0, }; - const decoded = prePackagedRulesStatusSchema.decode(payload); + const decoded = prePackagedRulesAndTimelinesStatusSchema.decode(payload); const checked = exactCheck(payload, decoded); const message = pipe(checked, foldLeftRight); @@ -46,13 +52,16 @@ describe('prepackaged_rules_schema', () => { }); test('it should NOT validate an empty prepackaged response with a negative "rules_installed" number', () => { - const payload: PrePackagedRulesStatusSchema = { + const payload: PrePackagedRulesAndTimelinesStatusSchema = { rules_installed: -1, rules_not_installed: 0, rules_not_updated: 0, rules_custom_installed: 0, + timelines_installed: 0, + timelines_not_installed: 0, + timelines_not_updated: 0, }; - const decoded = prePackagedRulesStatusSchema.decode(payload); + const decoded = prePackagedRulesAndTimelinesStatusSchema.decode(payload); const checked = exactCheck(payload, decoded); const message = pipe(checked, foldLeftRight); @@ -63,13 +72,16 @@ describe('prepackaged_rules_schema', () => { }); test('it should NOT validate an empty prepackaged response with a negative "rules_not_installed"', () => { - const payload: PrePackagedRulesStatusSchema = { + const payload: PrePackagedRulesAndTimelinesStatusSchema = { rules_installed: 0, rules_not_installed: -1, rules_not_updated: 0, rules_custom_installed: 0, + timelines_installed: 0, + timelines_not_installed: 0, + timelines_not_updated: 0, }; - const decoded = prePackagedRulesStatusSchema.decode(payload); + const decoded = prePackagedRulesAndTimelinesStatusSchema.decode(payload); const checked = exactCheck(payload, decoded); const message = pipe(checked, foldLeftRight); @@ -80,13 +92,16 @@ describe('prepackaged_rules_schema', () => { }); test('it should NOT validate an empty prepackaged response with a negative "rules_not_updated"', () => { - const payload: PrePackagedRulesStatusSchema = { + const payload: PrePackagedRulesAndTimelinesStatusSchema = { rules_installed: 0, rules_not_installed: 0, rules_not_updated: -1, rules_custom_installed: 0, + timelines_installed: 0, + timelines_not_installed: 0, + timelines_not_updated: 0, }; - const decoded = prePackagedRulesStatusSchema.decode(payload); + const decoded = prePackagedRulesAndTimelinesStatusSchema.decode(payload); const checked = exactCheck(payload, decoded); const message = pipe(checked, foldLeftRight); @@ -97,13 +112,16 @@ describe('prepackaged_rules_schema', () => { }); test('it should NOT validate an empty prepackaged response with a negative "rules_custom_installed"', () => { - const payload: PrePackagedRulesStatusSchema = { + const payload: PrePackagedRulesAndTimelinesStatusSchema = { rules_installed: 0, rules_not_installed: 0, rules_not_updated: 0, rules_custom_installed: -1, + timelines_installed: 0, + timelines_not_installed: 0, + timelines_not_updated: 0, }; - const decoded = prePackagedRulesStatusSchema.decode(payload); + const decoded = prePackagedRulesAndTimelinesStatusSchema.decode(payload); const checked = exactCheck(payload, decoded); const message = pipe(checked, foldLeftRight); @@ -114,14 +132,17 @@ describe('prepackaged_rules_schema', () => { }); test('it should NOT validate an empty prepackaged response if "rules_installed" is not there', () => { - const payload: PrePackagedRulesStatusSchema = { + const payload: PrePackagedRulesAndTimelinesStatusSchema = { rules_installed: 0, rules_not_installed: 0, rules_not_updated: 0, rules_custom_installed: 0, + timelines_installed: 0, + timelines_not_installed: 0, + timelines_not_updated: 0, }; delete payload.rules_installed; - const decoded = prePackagedRulesStatusSchema.decode(payload); + const decoded = prePackagedRulesAndTimelinesStatusSchema.decode(payload); const checked = exactCheck(payload, decoded); const message = pipe(checked, foldLeftRight); diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/response/prepackaged_rules_status_schema.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/prepackaged_rules_status_schema.ts index ee8e7b48a58bc..aabdbdd7300f4 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/response/prepackaged_rules_status_schema.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/prepackaged_rules_status_schema.ts @@ -12,16 +12,29 @@ import { rules_custom_installed, rules_not_installed, rules_not_updated, + timelines_installed, + timelines_not_installed, + timelines_not_updated, } from '../common/schemas'; /* eslint-enable @typescript-eslint/camelcase */ -export const prePackagedRulesStatusSchema = t.exact( - t.type({ - rules_custom_installed, - rules_installed, - rules_not_installed, - rules_not_updated, - }) +export const prePackagedTimelinesStatusSchema = t.type({ + timelines_installed, + timelines_not_installed, + timelines_not_updated, +}); + +const prePackagedRulesStatusSchema = t.type({ + rules_custom_installed, + rules_installed, + rules_not_installed, + rules_not_updated, +}); + +export const prePackagedRulesAndTimelinesStatusSchema = t.exact( + t.intersection([prePackagedRulesStatusSchema, prePackagedTimelinesStatusSchema]) ); -export type PrePackagedRulesStatusSchema = t.TypeOf; +export type PrePackagedRulesAndTimelinesStatusSchema = t.TypeOf< + typeof prePackagedRulesAndTimelinesStatusSchema +>; diff --git a/x-pack/plugins/security_solution/common/detection_engine/schemas/response/rules_schema.ts b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/rules_schema.ts index c0fec2b2eefc2..4bd18a13e4ebb 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/schemas/response/rules_schema.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/schemas/response/rules_schema.ts @@ -44,6 +44,7 @@ import { timeline_title, type, threat, + threshold, throttle, job_status, status_date, @@ -123,6 +124,9 @@ export const dependentRulesSchema = t.partial({ // ML fields anomaly_threshold, machine_learning_job_id, + + // Threshold fields + threshold, }); /** @@ -202,7 +206,7 @@ export const addTimelineTitle = (typeAndTimelineOnly: TypeAndTimelineOnly): t.Mi }; export const addQueryFields = (typeAndTimelineOnly: TypeAndTimelineOnly): t.Mixed[] => { - if (typeAndTimelineOnly.type === 'query' || typeAndTimelineOnly.type === 'saved_query') { + if (['query', 'saved_query', 'threshold'].includes(typeAndTimelineOnly.type)) { return [ t.exact(t.type({ query: dependentRulesSchema.props.query })), t.exact(t.type({ language: dependentRulesSchema.props.language })), @@ -225,6 +229,17 @@ export const addMlFields = (typeAndTimelineOnly: TypeAndTimelineOnly): t.Mixed[] } }; +export const addThresholdFields = (typeAndTimelineOnly: TypeAndTimelineOnly): t.Mixed[] => { + if (typeAndTimelineOnly.type === 'threshold') { + return [ + t.exact(t.type({ threshold: dependentRulesSchema.props.threshold })), + t.exact(t.partial({ saved_id: dependentRulesSchema.props.saved_id })), + ]; + } else { + return []; + } +}; + export const getDependents = (typeAndTimelineOnly: TypeAndTimelineOnly): t.Mixed => { const dependents: t.Mixed[] = [ t.exact(requiredRulesSchema), @@ -233,6 +248,7 @@ export const getDependents = (typeAndTimelineOnly: TypeAndTimelineOnly): t.Mixed ...addTimelineTitle(typeAndTimelineOnly), ...addQueryFields(typeAndTimelineOnly), ...addMlFields(typeAndTimelineOnly), + ...addThresholdFields(typeAndTimelineOnly), ]; if (dependents.length > 1) { diff --git a/x-pack/plugins/security_solution/common/detection_engine/types.ts b/x-pack/plugins/security_solution/common/detection_engine/types.ts index 431d716a9f205..7c752bca49dbd 100644 --- a/x-pack/plugins/security_solution/common/detection_engine/types.ts +++ b/x-pack/plugins/security_solution/common/detection_engine/types.ts @@ -15,5 +15,6 @@ export const RuleTypeSchema = t.keyof({ query: null, saved_query: null, machine_learning: null, + threshold: null, }); export type RuleType = t.TypeOf; diff --git a/x-pack/plugins/security_solution/common/endpoint/generate_data.test.ts b/x-pack/plugins/security_solution/common/endpoint/generate_data.test.ts index f64462f71a87b..fcea86be4ae9e 100644 --- a/x-pack/plugins/security_solution/common/endpoint/generate_data.test.ts +++ b/x-pack/plugins/security_solution/common/endpoint/generate_data.test.ts @@ -120,7 +120,7 @@ describe('data generator', () => { it('creates all events with an empty ancestry array', () => { for (const event of tree.allEvents) { - expect(event.process.Ext.ancestry.length).toEqual(0); + expect(event.process.Ext!.ancestry!.length).toEqual(0); } }); }); @@ -188,24 +188,24 @@ describe('data generator', () => { }; const verifyAncestry = (event: Event, genTree: Tree) => { - if (event.process.Ext.ancestry!.length > 0) { - expect(event.process.parent?.entity_id).toBe(event.process.Ext.ancestry![0]); + if (event.process.Ext!.ancestry!.length > 0) { + expect(event.process.parent?.entity_id).toBe(event.process.Ext!.ancestry![0]); } - for (let i = 0; i < event.process.Ext.ancestry!.length; i++) { - const ancestor = event.process.Ext.ancestry![i]; + for (let i = 0; i < event.process.Ext!.ancestry!.length; i++) { + const ancestor = event.process.Ext!.ancestry![i]; const parent = genTree.children.get(ancestor) || genTree.ancestry.get(ancestor); expect(ancestor).toBe(parent?.lifecycle[0].process.entity_id); // the next ancestor should be the grandparent - if (i + 1 < event.process.Ext.ancestry!.length) { - const grandparent = event.process.Ext.ancestry![i + 1]; + if (i + 1 < event.process.Ext!.ancestry!.length) { + const grandparent = event.process.Ext!.ancestry![i + 1]; expect(grandparent).toBe(parent?.lifecycle[0].process.parent?.entity_id); } } }; it('has ancestry array defined', () => { - expect(tree.origin.lifecycle[0].process.Ext.ancestry!.length).toBe(ANCESTRY_LIMIT); + expect(tree.origin.lifecycle[0].process.Ext!.ancestry!.length).toBe(ANCESTRY_LIMIT); for (const event of tree.allEvents) { verifyAncestry(event, tree); } diff --git a/x-pack/plugins/security_solution/common/endpoint/generate_data.ts b/x-pack/plugins/security_solution/common/endpoint/generate_data.ts index 339e5554ccb12..66e786cb02e63 100644 --- a/x-pack/plugins/security_solution/common/endpoint/generate_data.ts +++ b/x-pack/plugins/security_solution/common/endpoint/generate_data.ts @@ -823,7 +823,7 @@ export class EndpointDocGenerator { timestamp, parentEntityID: ancestor.process.entity_id, // add the parent to the ancestry array - ancestry: [ancestor.process.entity_id, ...(ancestor.process.Ext.ancestry ?? [])], + ancestry: [ancestor.process.entity_id, ...(ancestor.process.Ext?.ancestry ?? [])], ancestryArrayLimit: opts.ancestryArraySize, parentPid: ancestor.process.pid, pid: this.randomN(5000), @@ -840,7 +840,7 @@ export class EndpointDocGenerator { parentEntityID: ancestor.process.parent?.entity_id, eventCategory: 'process', eventType: 'end', - ancestry: ancestor.process.Ext.ancestry, + ancestry: ancestor.process.Ext?.ancestry, ancestryArrayLimit: opts.ancestryArraySize, }) ); @@ -864,7 +864,7 @@ export class EndpointDocGenerator { timestamp, ancestor.process.entity_id, ancestor.process.parent?.entity_id, - ancestor.process.Ext.ancestry + ancestor.process.Ext?.ancestry ) ); return events; @@ -914,7 +914,7 @@ export class EndpointDocGenerator { parentEntityID: currentState.event.process.entity_id, ancestry: [ currentState.event.process.entity_id, - ...(currentState.event.process.Ext.ancestry ?? []), + ...(currentState.event.process.Ext?.ancestry ?? []), ], ancestryArrayLimit: opts.ancestryArraySize, }); @@ -938,7 +938,7 @@ export class EndpointDocGenerator { parentEntityID: child.process.parent?.entity_id, eventCategory: 'process', eventType: 'end', - ancestry: child.process.Ext.ancestry, + ancestry: child.process.Ext?.ancestry, ancestryArrayLimit: opts.ancestryArraySize, }); } @@ -984,7 +984,7 @@ export class EndpointDocGenerator { parentEntityID: node.process.parent?.entity_id, eventCategory: eventInfo.category, eventType: eventInfo.creationType, - ancestry: node.process.Ext.ancestry, + ancestry: node.process.Ext?.ancestry, }); } } @@ -1007,7 +1007,7 @@ export class EndpointDocGenerator { ts, node.process.entity_id, node.process.parent?.entity_id, - node.process.Ext.ancestry + node.process.Ext?.ancestry ); } } diff --git a/x-pack/plugins/security_solution/common/endpoint/models/event.ts b/x-pack/plugins/security_solution/common/endpoint/models/event.ts index 9b4550f52ff22..f8a6807196557 100644 --- a/x-pack/plugins/security_solution/common/endpoint/models/event.ts +++ b/x-pack/plugins/security_solution/common/endpoint/models/event.ts @@ -57,7 +57,9 @@ export function ancestryArray(event: ResolverEvent): string[] | undefined { if (isLegacyEvent(event)) { return undefined; } - return event.process.Ext.ancestry; + // this is to guard against the endpoint accidentally not sending the ancestry array + // otherwise the request will fail when really we should just try using the parent entity id + return event.process.Ext?.ancestry; } export function getAncestryAsArray(event: ResolverEvent | undefined): string[] { diff --git a/x-pack/plugins/security_solution/common/endpoint/types.ts b/x-pack/plugins/security_solution/common/endpoint/types.ts index b75d4b2190fe8..b477207b1c5a3 100644 --- a/x-pack/plugins/security_solution/common/endpoint/types.ts +++ b/x-pack/plugins/security_solution/common/endpoint/types.ts @@ -334,13 +334,13 @@ export interface AlertEvent { start: number; thread?: ThreadFields[]; uptime: number; - Ext: { + Ext?: { /* * The array has a special format. The entity_ids towards the beginning of the array are closer ancestors and the * values towards the end of the array are more distant ancestors (grandparents). Therefore * ancestry_array[0] == process.parent.entity_id and ancestry_array[1] == process.parent.parent.entity_id */ - ancestry: string[]; + ancestry?: string[]; code_signature: Array<{ subject_name: string; trusted: boolean; @@ -539,8 +539,8 @@ export interface EndpointEvent { * values towards the end of the array are more distant ancestors (grandparents). Therefore * ancestry_array[0] == process.parent.entity_id and ancestry_array[1] == process.parent.parent.entity_id */ - Ext: { - ancestry: string[]; + Ext?: { + ancestry?: string[]; }; }; user?: { diff --git a/x-pack/plugins/security_solution/common/graphql/shared/schema.gql.ts b/x-pack/plugins/security_solution/common/graphql/shared/schema.gql.ts index d043c1587d3c3..546fdd68b4257 100644 --- a/x-pack/plugins/security_solution/common/graphql/shared/schema.gql.ts +++ b/x-pack/plugins/security_solution/common/graphql/shared/schema.gql.ts @@ -11,9 +11,14 @@ export const sharedSchema = gql` "The interval string to use for last bucket. The format is '{value}{unit}'. For example '5m' would return the metrics for the last 5 minutes of the timespan." interval: String! "The end of the timerange" - to: Float! + to: String! "The beginning of the timerange" - from: Float! + from: String! + } + + input docValueFieldsInput { + field: String! + format: String! } type CursorType { diff --git a/x-pack/plugins/security_solution/common/types/timeline/index.ts b/x-pack/plugins/security_solution/common/types/timeline/index.ts index 9e7a6f46bbcec..98d17fc87f6ce 100644 --- a/x-pack/plugins/security_solution/common/types/timeline/index.ts +++ b/x-pack/plugins/security_solution/common/types/timeline/index.ts @@ -7,11 +7,16 @@ /* eslint-disable @typescript-eslint/camelcase, @typescript-eslint/no-empty-interface */ import * as runtimeTypes from 'io-ts'; -import { SavedObjectsClient } from 'kibana/server'; import { stringEnum, unionWithNullType } from '../../utility_types'; import { NoteSavedObject, NoteSavedObjectToReturnRuntimeType } from './note'; import { PinnedEventToReturnSavedObjectRuntimeType, PinnedEventSavedObject } from './pinned_event'; +import { + success, + success_count as successCount, +} from '../../detection_engine/schemas/common/schemas'; +import { PositiveInteger } from '../../detection_engine/schemas/types'; +import { errorSchema } from '../../detection_engine/schemas/response/error_schema'; /* * ColumnHeader Types @@ -119,8 +124,12 @@ const SavedFilterQueryQueryRuntimeType = runtimeTypes.partial({ * DatePicker Range Types */ const SavedDateRangePickerRuntimeType = runtimeTypes.partial({ - start: unionWithNullType(runtimeTypes.number), - end: unionWithNullType(runtimeTypes.number), + /* Before the change of all timestamp to ISO string the values of start and from + * attributes where a number. Specifically UNIX timestamps. + * To support old timeline's saved object we need to add the number io-ts type + */ + start: unionWithNullType(runtimeTypes.union([runtimeTypes.string, runtimeTypes.number])), + end: unionWithNullType(runtimeTypes.union([runtimeTypes.string, runtimeTypes.number])), }); /* @@ -353,19 +362,6 @@ export interface AllTimelineSavedObject * Import/export timelines */ -export type ExportTimelineSavedObjectsClient = Pick< - SavedObjectsClient, - | 'get' - | 'errors' - | 'create' - | 'bulkCreate' - | 'delete' - | 'find' - | 'bulkGet' - | 'update' - | 'bulkUpdate' ->; - export type ExportedGlobalNotes = Array>; export type ExportedEventNotes = NoteSavedObject[]; @@ -393,3 +389,15 @@ export type NotesAndPinnedEventsByTimelineId = Record< string, { notes: NoteSavedObject[]; pinnedEvents: PinnedEventSavedObject[] } >; + +export const importTimelineResultSchema = runtimeTypes.exact( + runtimeTypes.type({ + success, + success_count: successCount, + timelines_installed: PositiveInteger, + timelines_updated: PositiveInteger, + errors: runtimeTypes.array(errorSchema), + }) +); + +export type ImportTimelineResultSchema = runtimeTypes.TypeOf; diff --git a/x-pack/plugins/security_solution/cypress/integration/ml_conditional_links.spec.ts b/x-pack/plugins/security_solution/cypress/integration/ml_conditional_links.spec.ts index 6b3fc9e751ea4..0b302efd655a8 100644 --- a/x-pack/plugins/security_solution/cypress/integration/ml_conditional_links.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/ml_conditional_links.spec.ts @@ -94,7 +94,7 @@ describe('ml conditional links', () => { loginAndWaitForPageWithoutDateRange(mlNetworkSingleIpNullKqlQuery); cy.url().should( 'include', - '/app/security/network/ip/127.0.0.1/source?timerange=(global:(linkTo:!(timeline),timerange:(from:1566990000000,kind:absolute,to:1567000799999)),timeline:(linkTo:!(global),timerange:(from:1566990000000,kind:absolute,to:1567000799999)))' + '/app/security/network/ip/127.0.0.1/source?timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-08-28T11:00:00.000Z%27,kind:absolute,to:%272019-08-28T13:59:59.999Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-08-28T11:00:00.000Z%27,kind:absolute,to:%272019-08-28T13:59:59.999Z%27)))' ); }); @@ -102,7 +102,7 @@ describe('ml conditional links', () => { loginAndWaitForPageWithoutDateRange(mlNetworkSingleIpKqlQuery); cy.url().should( 'include', - '/app/security/network/ip/127.0.0.1/source?query=(language:kuery,query:%27(process.name:%20%22conhost.exe%22%20or%20process.name:%20%22sc.exe%22)%27)&timerange=(global:(linkTo:!(timeline),timerange:(from:1566990000000,kind:absolute,to:1567000799999)),timeline:(linkTo:!(global),timerange:(from:1566990000000,kind:absolute,to:1567000799999)))' + '/app/security/network/ip/127.0.0.1/source?query=(language:kuery,query:%27(process.name:%20%22conhost.exe%22%20or%20process.name:%20%22sc.exe%22)%27)&timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-08-28T11:00:00.000Z%27,kind:absolute,to:%272019-08-28T13:59:59.999Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-08-28T11:00:00.000Z%27,kind:absolute,to:%272019-08-28T13:59:59.999Z%27)))' ); }); @@ -110,7 +110,7 @@ describe('ml conditional links', () => { loginAndWaitForPageWithoutDateRange(mlNetworkMultipleIpNullKqlQuery); cy.url().should( 'include', - 'app/security/network/flows?query=(language:kuery,query:%27((source.ip:%20%22127.0.0.1%22%20or%20destination.ip:%20%22127.0.0.1%22)%20or%20(source.ip:%20%22127.0.0.2%22%20or%20destination.ip:%20%22127.0.0.2%22))%27)&timerange=(global:(linkTo:!(timeline),timerange:(from:1566990000000,kind:absolute,to:1567000799999)),timeline:(linkTo:!(global),timerange:(from:1566990000000,kind:absolute,to:1567000799999))' + 'app/security/network/flows?query=(language:kuery,query:%27((source.ip:%20%22127.0.0.1%22%20or%20destination.ip:%20%22127.0.0.1%22)%20or%20(source.ip:%20%22127.0.0.2%22%20or%20destination.ip:%20%22127.0.0.2%22))%27)&timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-08-28T11:00:00.000Z%27,kind:absolute,to:%272019-08-28T13:59:59.999Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-08-28T11:00:00.000Z%27,kind:absolute,to:%272019-08-28T13:59:59.999Z%27))' ); }); @@ -118,7 +118,7 @@ describe('ml conditional links', () => { loginAndWaitForPageWithoutDateRange(mlNetworkMultipleIpKqlQuery); cy.url().should( 'include', - '/app/security/network/flows?query=(language:kuery,query:%27((source.ip:%20%22127.0.0.1%22%20or%20destination.ip:%20%22127.0.0.1%22)%20or%20(source.ip:%20%22127.0.0.2%22%20or%20destination.ip:%20%22127.0.0.2%22))%20and%20((process.name:%20%22conhost.exe%22%20or%20process.name:%20%22sc.exe%22))%27)&timerange=(global:(linkTo:!(timeline),timerange:(from:1566990000000,kind:absolute,to:1567000799999)),timeline:(linkTo:!(global),timerange:(from:1566990000000,kind:absolute,to:1567000799999)))' + '/app/security/network/flows?query=(language:kuery,query:%27((source.ip:%20%22127.0.0.1%22%20or%20destination.ip:%20%22127.0.0.1%22)%20or%20(source.ip:%20%22127.0.0.2%22%20or%20destination.ip:%20%22127.0.0.2%22))%20and%20((process.name:%20%22conhost.exe%22%20or%20process.name:%20%22sc.exe%22))%27)&timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-08-28T11:00:00.000Z%27,kind:absolute,to:%272019-08-28T13:59:59.999Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-08-28T11:00:00.000Z%27,kind:absolute,to:%272019-08-28T13:59:59.999Z%27)))' ); }); @@ -126,7 +126,7 @@ describe('ml conditional links', () => { loginAndWaitForPageWithoutDateRange(mlNetworkNullKqlQuery); cy.url().should( 'include', - '/app/security/network/flows?timerange=(global:(linkTo:!(timeline),timerange:(from:1566990000000,kind:absolute,to:1567000799999)),timeline:(linkTo:!(global),timerange:(from:1566990000000,kind:absolute,to:1567000799999)))' + '/app/security/network/flows?timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-08-28T11:00:00.000Z%27,kind:absolute,to:%272019-08-28T13:59:59.999Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-08-28T11:00:00.000Z%27,kind:absolute,to:%272019-08-28T13:59:59.999Z%27)))' ); }); @@ -134,7 +134,7 @@ describe('ml conditional links', () => { loginAndWaitForPageWithoutDateRange(mlNetworkKqlQuery); cy.url().should( 'include', - '/app/security/network/flows?query=(language:kuery,query:%27(process.name:%20%22conhost.exe%22%20or%20process.name:%20%22sc.exe%22)%27)&timerange=(global:(linkTo:!(timeline),timerange:(from:1566990000000,kind:absolute,to:1567000799999)),timeline:(linkTo:!(global),timerange:(from:1566990000000,kind:absolute,to:1567000799999)))' + '/app/security/network/flows?query=(language:kuery,query:%27(process.name:%20%22conhost.exe%22%20or%20process.name:%20%22sc.exe%22)%27)&timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-08-28T11:00:00.000Z%27,kind:absolute,to:%272019-08-28T13:59:59.999Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-08-28T11:00:00.000Z%27,kind:absolute,to:%272019-08-28T13:59:59.999Z%27)))' ); }); @@ -142,7 +142,7 @@ describe('ml conditional links', () => { loginAndWaitForPageWithoutDateRange(mlHostSingleHostNullKqlQuery); cy.url().should( 'include', - '/app/security/hosts/siem-windows/anomalies?timerange=(global:(linkTo:!(timeline),timerange:(from:1559800800000,kind:absolute,to:1559887199999)),timeline:(linkTo:!(global),timerange:(from:1559800800000,kind:absolute,to:1559887199999)))' + '/app/security/hosts/siem-windows/anomalies?timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-06-06T06:00:00.000Z%27,kind:absolute,to:%272019-06-07T05:59:59.999Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-06-06T06:00:00.000Z%27,kind:absolute,to:%272019-06-07T05:59:59.999Z%27)))' ); }); @@ -150,7 +150,7 @@ describe('ml conditional links', () => { loginAndWaitForPageWithoutDateRange(mlHostSingleHostKqlQueryVariable); cy.url().should( 'include', - '/app/security/hosts/siem-windows/anomalies?timerange=(global:(linkTo:!(timeline),timerange:(from:1559800800000,kind:absolute,to:1559887199999)),timeline:(linkTo:!(global),timerange:(from:1559800800000,kind:absolute,to:1559887199999)))' + '/app/security/hosts/siem-windows/anomalies?timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-06-06T06:00:00.000Z%27,kind:absolute,to:%272019-06-07T05:59:59.999Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-06-06T06:00:00.000Z%27,kind:absolute,to:%272019-06-07T05:59:59.999Z%27)))' ); }); @@ -158,7 +158,7 @@ describe('ml conditional links', () => { loginAndWaitForPageWithoutDateRange(mlHostSingleHostKqlQuery); cy.url().should( 'include', - '/app/security/hosts/siem-windows/anomalies?query=(language:kuery,query:%27(process.name:%20%22conhost.exe%22%20or%20process.name:%20%22sc.exe%22)%27)&timerange=(global:(linkTo:!(timeline),timerange:(from:1559800800000,kind:absolute,to:1559887199999)),timeline:(linkTo:!(global),timerange:(from:1559800800000,kind:absolute,to:1559887199999)))' + '/app/security/hosts/siem-windows/anomalies?query=(language:kuery,query:%27(process.name:%20%22conhost.exe%22%20or%20process.name:%20%22sc.exe%22)%27)&timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-06-06T06:00:00.000Z%27,kind:absolute,to:%272019-06-07T05:59:59.999Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-06-06T06:00:00.000Z%27,kind:absolute,to:%272019-06-07T05:59:59.999Z%27)))' ); }); @@ -166,7 +166,7 @@ describe('ml conditional links', () => { loginAndWaitForPageWithoutDateRange(mlHostMultiHostNullKqlQuery); cy.url().should( 'include', - '/app/security/hosts/anomalies?query=(language:kuery,query:%27(host.name:%20%22siem-windows%22%20or%20host.name:%20%22siem-suricata%22)%27)&timerange=(global:(linkTo:!(timeline),timerange:(from:1559800800000,kind:absolute,to:1559887199999)),timeline:(linkTo:!(global),timerange:(from:1559800800000,kind:absolute,to:1559887199999)))' + '/app/security/hosts/anomalies?query=(language:kuery,query:%27(host.name:%20%22siem-windows%22%20or%20host.name:%20%22siem-suricata%22)%27)&timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-06-06T06:00:00.000Z%27,kind:absolute,to:%272019-06-07T05:59:59.999Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-06-06T06:00:00.000Z%27,kind:absolute,to:%272019-06-07T05:59:59.999Z%27)))' ); }); @@ -174,7 +174,7 @@ describe('ml conditional links', () => { loginAndWaitForPageWithoutDateRange(mlHostMultiHostKqlQuery); cy.url().should( 'include', - '/app/security/hosts/anomalies?query=(language:kuery,query:%27(host.name:%20%22siem-windows%22%20or%20host.name:%20%22siem-suricata%22)%20and%20((process.name:%20%22conhost.exe%22%20or%20process.name:%20%22sc.exe%22))%27)&timerange=(global:(linkTo:!(timeline),timerange:(from:1559800800000,kind:absolute,to:1559887199999)),timeline:(linkTo:!(global),timerange:(from:1559800800000,kind:absolute,to:1559887199999)))' + '/app/security/hosts/anomalies?query=(language:kuery,query:%27(host.name:%20%22siem-windows%22%20or%20host.name:%20%22siem-suricata%22)%20and%20((process.name:%20%22conhost.exe%22%20or%20process.name:%20%22sc.exe%22))%27)&timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-06-06T06:00:00.000Z%27,kind:absolute,to:%272019-06-07T05:59:59.999Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-06-06T06:00:00.000Z%27,kind:absolute,to:%272019-06-07T05:59:59.999Z%27)))' ); }); @@ -182,7 +182,7 @@ describe('ml conditional links', () => { loginAndWaitForPageWithoutDateRange(mlHostVariableHostNullKqlQuery); cy.url().should( 'include', - '/app/security/hosts/anomalies?timerange=(global:(linkTo:!(timeline),timerange:(from:1559800800000,kind:absolute,to:1559887199999)),timeline:(linkTo:!(global),timerange:(from:1559800800000,kind:absolute,to:1559887199999)))' + '/app/security/hosts/anomalies?timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-06-06T06:00:00.000Z%27,kind:absolute,to:%272019-06-07T05:59:59.999Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-06-06T06:00:00.000Z%27,kind:absolute,to:%272019-06-07T05:59:59.999Z%27)))' ); }); @@ -190,7 +190,7 @@ describe('ml conditional links', () => { loginAndWaitForPageWithoutDateRange(mlHostVariableHostKqlQuery); cy.url().should( 'include', - '/app/security/hosts/anomalies?query=(language:kuery,query:%27(process.name:%20%22conhost.exe%22%20or%20process.name:%20%22sc.exe%22)%27)&timerange=(global:(linkTo:!(timeline),timerange:(from:1559800800000,kind:absolute,to:1559887199999)),timeline:(linkTo:!(global),timerange:(from:1559800800000,kind:absolute,to:1559887199999)))' + '/app/security/hosts/anomalies?query=(language:kuery,query:%27(process.name:%20%22conhost.exe%22%20or%20process.name:%20%22sc.exe%22)%27)&timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-06-06T06:00:00.000Z%27,kind:absolute,to:%272019-06-07T05:59:59.999Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-06-06T06:00:00.000Z%27,kind:absolute,to:%272019-06-07T05:59:59.999Z%27)))' ); }); }); diff --git a/x-pack/plugins/security_solution/cypress/integration/url_compatibility.spec.ts b/x-pack/plugins/security_solution/cypress/integration/url_compatibility.spec.ts index 205a49fc771cf..5b42897b065e3 100644 --- a/x-pack/plugins/security_solution/cypress/integration/url_compatibility.spec.ts +++ b/x-pack/plugins/security_solution/cypress/integration/url_compatibility.spec.ts @@ -4,9 +4,19 @@ * you may not use this file except in compliance with the Elastic License. */ -import { loginAndWaitForPage } from '../tasks/login'; +import { loginAndWaitForPage, loginAndWaitForPageWithoutDateRange } from '../tasks/login'; import { DETECTIONS } from '../urls/navigation'; +import { ABSOLUTE_DATE_RANGE } from '../urls/state'; +import { + DATE_PICKER_START_DATE_POPOVER_BUTTON, + DATE_PICKER_END_DATE_POPOVER_BUTTON, +} from '../screens/date_picker'; + +const ABSOLUTE_DATE = { + endTime: '2019-08-01T20:33:29.186Z', + startTime: '2019-08-01T20:03:29.186Z', +}; describe('URL compatibility', () => { it('Redirects to Detection alerts from old Detections URL', () => { @@ -14,4 +24,14 @@ describe('URL compatibility', () => { cy.url().should('include', '/security/detections'); }); + + it('sets the global start and end dates from the url with timestamps', () => { + loginAndWaitForPageWithoutDateRange(ABSOLUTE_DATE_RANGE.urlWithTimestamps); + cy.get(DATE_PICKER_START_DATE_POPOVER_BUTTON).should( + 'have.attr', + 'title', + ABSOLUTE_DATE.startTime + ); + cy.get(DATE_PICKER_END_DATE_POPOVER_BUTTON).should('have.attr', 'title', ABSOLUTE_DATE.endTime); + }); }); 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 81af9ece9ed45..cdcdde252d6d6 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 @@ -42,24 +42,12 @@ import { HOSTS_URL } from '../urls/navigation'; import { ABSOLUTE_DATE_RANGE } from '../urls/state'; const ABSOLUTE_DATE = { - endTime: '1564691609186', - endTimeFormat: '2019-08-01T20:33:29.186Z', - endTimeTimeline: '1564779809186', - endTimeTimelineFormat: '2019-08-02T21:03:29.186Z', - endTimeTimelineTyped: 'Aug 02, 2019 @ 21:03:29.186', - endTimeTyped: 'Aug 01, 2019 @ 14:33:29.186', - newEndTime: '1564693409186', - newEndTimeFormat: '2019-08-01T21:03:29.186Z', + endTime: '2019-08-01T20:33:29.186Z', + endTimeTimeline: '2019-08-02T21:03:29.186Z', newEndTimeTyped: 'Aug 01, 2019 @ 15:03:29.186', - newStartTime: '1564691609186', - newStartTimeFormat: '2019-08-01T20:33:29.186Z', newStartTimeTyped: 'Aug 01, 2019 @ 14:33:29.186', - startTime: '1564689809186', - startTimeFormat: '2019-08-01T20:03:29.186Z', - startTimeTimeline: '1564776209186', - startTimeTimelineFormat: '2019-08-02T20:03:29.186Z', - startTimeTimelineTyped: 'Aug 02, 2019 @ 14:03:29.186', - startTimeTyped: 'Aug 01, 2019 @ 14:03:29.186', + startTime: '2019-08-01T20:03:29.186Z', + startTimeTimeline: '2019-08-02T20:03:29.186Z', }; describe('url state', () => { @@ -68,13 +56,9 @@ describe('url state', () => { cy.get(DATE_PICKER_START_DATE_POPOVER_BUTTON).should( 'have.attr', 'title', - ABSOLUTE_DATE.startTimeFormat - ); - cy.get(DATE_PICKER_END_DATE_POPOVER_BUTTON).should( - 'have.attr', - 'title', - ABSOLUTE_DATE.endTimeFormat + ABSOLUTE_DATE.startTime ); + cy.get(DATE_PICKER_END_DATE_POPOVER_BUTTON).should('have.attr', 'title', ABSOLUTE_DATE.endTime); }); it('sets the url state when start and end date are set', () => { @@ -87,9 +71,11 @@ describe('url state', () => { cy.url().should( 'include', - `(global:(linkTo:!(timeline),timerange:(from:${new Date( + `(global:(linkTo:!(timeline),timerange:(from:%27${new Date( ABSOLUTE_DATE.newStartTimeTyped - ).valueOf()},kind:absolute,to:${new Date(ABSOLUTE_DATE.newEndTimeTyped).valueOf()}))` + ).toISOString()}%27,kind:absolute,to:%27${new Date( + ABSOLUTE_DATE.newEndTimeTyped + ).toISOString()}%27))` ); }); @@ -100,12 +86,12 @@ describe('url state', () => { cy.get(DATE_PICKER_START_DATE_POPOVER_BUTTON_TIMELINE).should( 'have.attr', 'title', - ABSOLUTE_DATE.startTimeFormat + ABSOLUTE_DATE.startTime ); cy.get(DATE_PICKER_END_DATE_POPOVER_BUTTON_TIMELINE).should( 'have.attr', 'title', - ABSOLUTE_DATE.endTimeFormat + ABSOLUTE_DATE.endTime ); }); @@ -114,25 +100,21 @@ describe('url state', () => { cy.get(DATE_PICKER_START_DATE_POPOVER_BUTTON).should( 'have.attr', 'title', - ABSOLUTE_DATE.startTimeFormat - ); - cy.get(DATE_PICKER_END_DATE_POPOVER_BUTTON).should( - 'have.attr', - 'title', - ABSOLUTE_DATE.endTimeFormat + ABSOLUTE_DATE.startTime ); + cy.get(DATE_PICKER_END_DATE_POPOVER_BUTTON).should('have.attr', 'title', ABSOLUTE_DATE.endTime); openTimeline(); cy.get(DATE_PICKER_START_DATE_POPOVER_BUTTON_TIMELINE).should( 'have.attr', 'title', - ABSOLUTE_DATE.startTimeTimelineFormat + ABSOLUTE_DATE.startTimeTimeline ); cy.get(DATE_PICKER_END_DATE_POPOVER_BUTTON_TIMELINE).should( 'have.attr', 'title', - ABSOLUTE_DATE.endTimeTimelineFormat + ABSOLUTE_DATE.endTimeTimeline ); }); @@ -146,9 +128,11 @@ describe('url state', () => { cy.url().should( 'include', - `timeline:(linkTo:!(),timerange:(from:${new Date( + `timeline:(linkTo:!(),timerange:(from:%27${new Date( ABSOLUTE_DATE.newStartTimeTyped - ).valueOf()},kind:absolute,to:${new Date(ABSOLUTE_DATE.newEndTimeTyped).valueOf()}))` + ).toISOString()}%27,kind:absolute,to:%27${new Date( + ABSOLUTE_DATE.newEndTimeTyped + ).toISOString()}%27))` ); }); @@ -180,7 +164,7 @@ describe('url state', () => { cy.get(NETWORK).should( 'have.attr', 'href', - `/app/security/network?query=(language:kuery,query:'source.ip:%20%2210.142.0.9%22%20')&timerange=(global:(linkTo:!(timeline),timerange:(from:1564689809186,kind:absolute,to:1564691609186)),timeline:(linkTo:!(global),timerange:(from:1564689809186,kind:absolute,to:1564691609186)))` + `/app/security/network?query=(language:kuery,query:'source.ip:%20%2210.142.0.9%22%20')&timerange=(global:(linkTo:!(timeline),timerange:(from:'2019-08-01T20:03:29.186Z',kind:absolute,to:'2019-08-01T20:33:29.186Z')),timeline:(linkTo:!(global),timerange:(from:'2019-08-01T20:03:29.186Z',kind:absolute,to:'2019-08-01T20:33:29.186Z')))` ); }); @@ -193,12 +177,12 @@ describe('url state', () => { cy.get(HOSTS).should( 'have.attr', 'href', - `/app/security/hosts?query=(language:kuery,query:'host.name:%20%22siem-kibana%22%20')&timerange=(global:(linkTo:!(timeline),timerange:(from:1564689809186,kind:absolute,to:1577914409186)),timeline:(linkTo:!(global),timerange:(from:1564689809186,kind:absolute,to:1577914409186)))` + `/app/security/hosts?query=(language:kuery,query:'host.name:%20%22siem-kibana%22%20')&timerange=(global:(linkTo:!(timeline),timerange:(from:'2019-08-01T20:03:29.186Z',kind:absolute,to:'2020-01-01T21:33:29.186Z')),timeline:(linkTo:!(global),timerange:(from:'2019-08-01T20:03:29.186Z',kind:absolute,to:'2020-01-01T21:33:29.186Z')))` ); cy.get(NETWORK).should( 'have.attr', 'href', - `/app/security/network?query=(language:kuery,query:'host.name:%20%22siem-kibana%22%20')&timerange=(global:(linkTo:!(timeline),timerange:(from:1564689809186,kind:absolute,to:1577914409186)),timeline:(linkTo:!(global),timerange:(from:1564689809186,kind:absolute,to:1577914409186)))` + `/app/security/network?query=(language:kuery,query:'host.name:%20%22siem-kibana%22%20')&timerange=(global:(linkTo:!(timeline),timerange:(from:'2019-08-01T20:03:29.186Z',kind:absolute,to:'2020-01-01T21:33:29.186Z')),timeline:(linkTo:!(global),timerange:(from:'2019-08-01T20:03:29.186Z',kind:absolute,to:'2020-01-01T21:33:29.186Z')))` ); cy.get(HOSTS_NAMES).first().invoke('text').should('eq', 'siem-kibana'); @@ -209,21 +193,21 @@ describe('url state', () => { cy.get(ANOMALIES_TAB).should( 'have.attr', 'href', - "/app/security/hosts/siem-kibana/anomalies?query=(language:kuery,query:'agent.type:%20%22auditbeat%22%20')&timerange=(global:(linkTo:!(timeline),timerange:(from:1564689809186,kind:absolute,to:1577914409186)),timeline:(linkTo:!(global),timerange:(from:1564689809186,kind:absolute,to:1577914409186)))" + "/app/security/hosts/siem-kibana/anomalies?query=(language:kuery,query:'agent.type:%20%22auditbeat%22%20')&timerange=(global:(linkTo:!(timeline),timerange:(from:'2019-08-01T20:03:29.186Z',kind:absolute,to:'2020-01-01T21:33:29.186Z')),timeline:(linkTo:!(global),timerange:(from:'2019-08-01T20:03:29.186Z',kind:absolute,to:'2020-01-01T21:33:29.186Z')))" ); cy.get(BREADCRUMBS) .eq(1) .should( 'have.attr', 'href', - `/app/security/hosts?query=(language:kuery,query:'agent.type:%20%22auditbeat%22%20')&timerange=(global:(linkTo:!(timeline),timerange:(from:1564689809186,kind:absolute,to:1577914409186)),timeline:(linkTo:!(global),timerange:(from:1564689809186,kind:absolute,to:1577914409186)))` + `/app/security/hosts?query=(language:kuery,query:'agent.type:%20%22auditbeat%22%20')&timerange=(global:(linkTo:!(timeline),timerange:(from:'2019-08-01T20:03:29.186Z',kind:absolute,to:'2020-01-01T21:33:29.186Z')),timeline:(linkTo:!(global),timerange:(from:'2019-08-01T20:03:29.186Z',kind:absolute,to:'2020-01-01T21:33:29.186Z')))` ); cy.get(BREADCRUMBS) .eq(2) .should( 'have.attr', 'href', - `/app/security/hosts/siem-kibana?query=(language:kuery,query:'agent.type:%20%22auditbeat%22%20')&timerange=(global:(linkTo:!(timeline),timerange:(from:1564689809186,kind:absolute,to:1577914409186)),timeline:(linkTo:!(global),timerange:(from:1564689809186,kind:absolute,to:1577914409186)))` + `/app/security/hosts/siem-kibana?query=(language:kuery,query:'agent.type:%20%22auditbeat%22%20')&timerange=(global:(linkTo:!(timeline),timerange:(from:'2019-08-01T20:03:29.186Z',kind:absolute,to:'2020-01-01T21:33:29.186Z')),timeline:(linkTo:!(global),timerange:(from:'2019-08-01T20:03:29.186Z',kind:absolute,to:'2020-01-01T21:33:29.186Z')))` ); }); diff --git a/x-pack/plugins/security_solution/cypress/urls/state.ts b/x-pack/plugins/security_solution/cypress/urls/state.ts index bdd90c21fbedf..7825be08e38e1 100644 --- a/x-pack/plugins/security_solution/cypress/urls/state.ts +++ b/x-pack/plugins/security_solution/cypress/urls/state.ts @@ -6,16 +6,18 @@ export const ABSOLUTE_DATE_RANGE = { url: - '/app/security/network/flows/?timerange=(global:(linkTo:!(timeline),timerange:(from:1564689809186,kind:absolute,to:1564691609186)),timeline:(linkTo:!(global),timerange:(from:1564689809186,kind:absolute,to:1564691609186)))', + '/app/security/network/flows/?timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-08-01T20:03:29.186Z%27,kind:absolute,to:%272019-08-01T20:33:29.186Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-08-01T20:03:29.186Z%27,kind:absolute,to:%272019-08-01T20:33:29.186Z%27)))', + urlWithTimestamps: + '/app/security/network/flows/?timerange=(global:(linkTo:!(timeline),timerange:(from:1564689809186,kind:absolute,to:1564691609186)),timeline:(linkTo:!(global),timerange:(from:1564689809186,kind:absolute,to:1564691609186)))', urlUnlinked: - '/app/security/network/flows/?timerange=(global:(linkTo:!(),timerange:(from:1564689809186,kind:absolute,to:1564691609186)),timeline:(linkTo:!(),timerange:(from:1564776209186,kind:absolute,to:1564779809186)))', - urlKqlNetworkNetwork: `/app/security/network/flows/?query=(language:kuery,query:'source.ip:%20"10.142.0.9"')&timerange=(global:(linkTo:!(timeline),timerange:(from:1564689809186,kind:absolute,to:1564691609186)),timeline:(linkTo:!(global),timerange:(from:1564689809186,kind:absolute,to:1564691609186)))`, - urlKqlNetworkHosts: `/app/security/network/flows/?query=(language:kuery,query:'source.ip:%20"10.142.0.9"')&timerange=(global:(linkTo:!(timeline),timerange:(from:1564689809186,kind:absolute,to:1564691609186)),timeline:(linkTo:!(global),timerange:(from:1564689809186,kind:absolute,to:1564691609186)))`, - urlKqlHostsNetwork: `/app/security/hosts/allHosts?query=(language:kuery,query:'source.ip:%20"10.142.0.9"')&timerange=(global:(linkTo:!(timeline),timerange:(from:1564689809186,kind:absolute,to:1564691609186)),timeline:(linkTo:!(global),timerange:(from:1564689809186,kind:absolute,to:1564691609186)))`, - urlKqlHostsHosts: `/app/security/hosts/allHosts?query=(language:kuery,query:'source.ip:%20"10.142.0.9"')&timerange=(global:(linkTo:!(timeline),timerange:(from:1564689809186,kind:absolute,to:1564691609186)),timeline:(linkTo:!(global),timerange:(from:1564689809186,kind:absolute,to:1564691609186)))`, + '/app/security/network/flows/?timerange=(global:(linkTo:!(),timerange:(from:%272019-08-01T20:03:29.186Z%27,kind:absolute,to:%272019-08-01T20:33:29.186Z%27)),timeline:(linkTo:!(),timerange:(from:%272019-08-02T20:03:29.186Z%27,kind:absolute,to:%272019-08-02T21:03:29.186Z%27)))', + urlKqlNetworkNetwork: `/app/security/network/flows/?query=(language:kuery,query:'source.ip:%20"10.142.0.9"')&timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-08-01T20:03:29.186Z%27,kind:absolute,to:%272019-08-01T20:33:29.186Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-08-01T20:03:29.186Z%27,kind:absolute,to:%272019-08-01T20:33:29.186Z%27)))`, + urlKqlNetworkHosts: `/app/security/network/flows/?query=(language:kuery,query:'source.ip:%20"10.142.0.9"')&timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-08-01T20:03:29.186Z%27,kind:absolute,to:%272019-08-01T20:33:29.186Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-08-01T20:03:29.186Z%27,kind:absolute,to:%272019-08-01T20:33:29.186Z%27)))`, + urlKqlHostsNetwork: `/app/security/hosts/allHosts?query=(language:kuery,query:'source.ip:%20"10.142.0.9"')&timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-08-01T20:03:29.186Z%27,kind:absolute,to:%272019-08-01T20:33:29.186Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-08-01T20:03:29.186Z%27,kind:absolute,to:%272019-08-01T20:33:29.186Z%27)))`, + urlKqlHostsHosts: `/app/security/hosts/allHosts?query=(language:kuery,query:'source.ip:%20"10.142.0.9"')&timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-08-01T20:03:29.186Z%27,kind:absolute,to:%272019-08-01T20:33:29.186Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-08-01T20:03:29.186Z%27,kind:absolute,to:%272019-08-01T20:33:29.186Z%27)))`, urlHost: - '/app/security/hosts/authentications?timerange=(global:(linkTo:!(timeline),timerange:(from:1564689809186,kind:absolute,to:1564691609186)),timeline:(linkTo:!(global),timerange:(from:1564689809186,kind:absolute,to:1564691609186)))', + '/app/security/hosts/authentications?timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-08-01T20:03:29.186Z%27,kind:absolute,to:%272019-08-01T20:33:29.186Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-08-01T20:03:29.186Z%27,kind:absolute,to:%272019-08-01T20:33:29.186Z%27)))', urlHostNew: - '/app/security/hosts/authentications?timerange=(global:(linkTo:!(timeline),timerange:(from:1564689809186,kind:absolute,to:1577914409186)),timeline:(linkTo:!(global),timerange:(from:1564689809186,kind:absolute,to:1577914409186)))', + '/app/security/hosts/authentications?timerange=(global:(linkTo:!(timeline),timerange:(from:%272019-08-01T20:03:29.186Z%27,kind:absolute,to:%272020-01-01T21:33:29.186Z%27)),timeline:(linkTo:!(global),timerange:(from:%272019-08-01T20:03:29.186Z%27,kind:absolute,to:%272020-01-01T21:33:29.186Z%27)))', }; 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 bf7ce2ddf8b50..3f4b0c19e7035 100644 --- a/x-pack/plugins/security_solution/public/app/home/setup.tsx +++ b/x-pack/plugins/security_solution/public/app/home/setup.tsx @@ -32,7 +32,7 @@ export const Setup: React.FunctionComponent<{ }); }; - ingestManager.success.catch((error: Error) => displayToastWithModal(error.message)); + ingestManager.isInitialized().catch((error: Error) => displayToastWithModal(error.message)); }, [ingestManager, notifications.toasts]); return null; diff --git a/x-pack/plugins/security_solution/public/common/components/alerts_viewer/alerts_table.tsx b/x-pack/plugins/security_solution/public/common/components/alerts_viewer/alerts_table.tsx index bf2d8948b7292..841a1ef09ede6 100644 --- a/x-pack/plugins/security_solution/public/common/components/alerts_viewer/alerts_table.tsx +++ b/x-pack/plugins/security_solution/public/common/components/alerts_viewer/alerts_table.tsx @@ -17,9 +17,9 @@ import * as i18n from './translations'; import { useKibana } from '../../lib/kibana'; export interface OwnProps { - end: number; + end: string; id: string; - start: number; + start: string; } const defaultAlertsFilters: Filter[] = [ @@ -57,8 +57,8 @@ const defaultAlertsFilters: Filter[] = [ interface Props { timelineId: TimelineIdLiteral; - endDate: number; - startDate: number; + endDate: string; + startDate: string; pageFilters?: Filter[]; } diff --git a/x-pack/plugins/security_solution/public/common/components/autocomplete/field.tsx b/x-pack/plugins/security_solution/public/common/components/autocomplete/field.tsx index 8a6f049c96037..ed844b5130c77 100644 --- a/x-pack/plugins/security_solution/public/common/components/autocomplete/field.tsx +++ b/x-pack/plugins/security_solution/public/common/components/autocomplete/field.tsx @@ -17,6 +17,7 @@ interface OperatorProps { isLoading: boolean; isDisabled: boolean; isClearable: boolean; + fieldTypeFilter?: string[]; fieldInputWidth?: number; onChange: (a: IFieldType[]) => void; } @@ -28,13 +29,22 @@ export const FieldComponent: React.FC = ({ isLoading = false, isDisabled = false, isClearable = false, + fieldTypeFilter = [], fieldInputWidth = 190, onChange, }): JSX.Element => { const getLabel = useCallback((field): string => field.name, []); - const optionsMemo = useMemo((): IFieldType[] => (indexPattern ? indexPattern.fields : []), [ - indexPattern, - ]); + const optionsMemo = useMemo((): IFieldType[] => { + if (indexPattern != null) { + if (fieldTypeFilter.length > 0) { + return indexPattern.fields.filter((f) => fieldTypeFilter.includes(f.type)); + } else { + return indexPattern.fields; + } + } else { + return []; + } + }, [fieldTypeFilter, indexPattern]); const selectedOptionsMemo = useMemo((): IFieldType[] => (selectedField ? [selectedField] : []), [ selectedField, ]); diff --git a/x-pack/plugins/security_solution/public/common/components/autocomplete/field_value_match.tsx b/x-pack/plugins/security_solution/public/common/components/autocomplete/field_value_match.tsx index 4d96d6638132b..32a82af114bae 100644 --- a/x-pack/plugins/security_solution/public/common/components/autocomplete/field_value_match.tsx +++ b/x-pack/plugins/security_solution/public/common/components/autocomplete/field_value_match.tsx @@ -22,6 +22,7 @@ interface AutocompleteFieldMatchProps { isLoading: boolean; isDisabled: boolean; isClearable: boolean; + fieldInputWidth?: number; onChange: (arg: string) => void; } @@ -33,6 +34,7 @@ export const AutocompleteFieldMatchComponent: React.FC { const [isLoadingSuggestions, suggestions, updateSuggestions] = useFieldValueAutocomplete({ @@ -97,6 +99,7 @@ export const AutocompleteFieldMatchComponent: React.FC diff --git a/x-pack/plugins/security_solution/public/common/components/events_viewer/events_viewer.test.tsx b/x-pack/plugins/security_solution/public/common/components/events_viewer/events_viewer.test.tsx index 38ca1176d1700..674eb3325efc2 100644 --- a/x-pack/plugins/security_solution/public/common/components/events_viewer/events_viewer.test.tsx +++ b/x-pack/plugins/security_solution/public/common/components/events_viewer/events_viewer.test.tsx @@ -15,29 +15,36 @@ import { mockEventViewerResponse } from './mock'; import { StatefulEventsViewer } from '.'; import { defaultHeaders } from './default_headers'; import { useFetchIndexPatterns } from '../../../detections/containers/detection_engine/rules/fetch_index_patterns'; -import { mockBrowserFields } from '../../containers/source/mock'; +import { mockBrowserFields, mockDocValueFields } from '../../containers/source/mock'; import { eventsDefaultModel } from './default_model'; import { useMountAppended } from '../../utils/use_mount_appended'; +jest.mock('../../components/url_state/normalize_time_range.ts'); + const mockUseFetchIndexPatterns: jest.Mock = useFetchIndexPatterns as jest.Mock; jest.mock('../../../detections/containers/detection_engine/rules/fetch_index_patterns'); -mockUseFetchIndexPatterns.mockImplementation(() => [ - { - browserFields: mockBrowserFields, - indexPatterns: mockIndexPattern, - }, -]); const mockUseResizeObserver: jest.Mock = useResizeObserver as jest.Mock; jest.mock('use-resize-observer/polyfilled'); mockUseResizeObserver.mockImplementation(() => ({})); -const from = 1566943856794; -const to = 1566857456791; +const from = '2019-08-26T22:10:56.791Z'; +const to = '2019-08-27T22:10:56.794Z'; describe('EventsViewer', () => { const mount = useMountAppended(); + beforeEach(() => { + mockUseFetchIndexPatterns.mockImplementation(() => [ + { + browserFields: mockBrowserFields, + indexPatterns: mockIndexPattern, + docValueFields: mockDocValueFields, + isLoading: false, + }, + ]); + }); + test('it renders the "Showing..." subtitle with the expected event count', async () => { const wrapper = mount( @@ -60,6 +67,93 @@ describe('EventsViewer', () => { ); }); + test('it does NOT render fetch index pattern is loading', async () => { + mockUseFetchIndexPatterns.mockImplementation(() => [ + { + browserFields: mockBrowserFields, + indexPatterns: mockIndexPattern, + docValueFields: mockDocValueFields, + isLoading: true, + }, + ]); + + const wrapper = mount( + + + + + + ); + + await wait(); + wrapper.update(); + + expect(wrapper.find(`[data-test-subj="header-section-subtitle"]`).first().exists()).toBe(false); + }); + + test('it does NOT render when start is empty', async () => { + mockUseFetchIndexPatterns.mockImplementation(() => [ + { + browserFields: mockBrowserFields, + indexPatterns: mockIndexPattern, + docValueFields: mockDocValueFields, + isLoading: true, + }, + ]); + + const wrapper = mount( + + + + + + ); + + await wait(); + wrapper.update(); + + expect(wrapper.find(`[data-test-subj="header-section-subtitle"]`).first().exists()).toBe(false); + }); + + test('it does NOT render when end is empty', async () => { + mockUseFetchIndexPatterns.mockImplementation(() => [ + { + browserFields: mockBrowserFields, + indexPatterns: mockIndexPattern, + docValueFields: mockDocValueFields, + isLoading: true, + }, + ]); + + const wrapper = mount( + + + + + + ); + + await wait(); + wrapper.update(); + + expect(wrapper.find(`[data-test-subj="header-section-subtitle"]`).first().exists()).toBe(false); + }); + test('it renders the Fields Browser as a settings gear', async () => { const wrapper = mount( diff --git a/x-pack/plugins/security_solution/public/common/components/events_viewer/events_viewer.tsx b/x-pack/plugins/security_solution/public/common/components/events_viewer/events_viewer.tsx index 0a1f95d51e300..5e0d5a6e9b099 100644 --- a/x-pack/plugins/security_solution/public/common/components/events_viewer/events_viewer.tsx +++ b/x-pack/plugins/security_solution/public/common/components/events_viewer/events_viewer.tsx @@ -10,7 +10,7 @@ import React, { useEffect, useMemo, useState } from 'react'; import styled from 'styled-components'; import deepEqual from 'fast-deep-equal'; -import { BrowserFields } from '../../containers/source'; +import { BrowserFields, DocValueFields } from '../../containers/source'; import { TimelineQuery } from '../../../timelines/containers'; import { Direction } from '../../../graphql/types'; import { useKibana } from '../../lib/kibana'; @@ -51,22 +51,26 @@ interface Props { columns: ColumnHeaderOptions[]; dataProviders: DataProvider[]; deletedEventIds: Readonly; - end: number; + docValueFields: DocValueFields[]; + end: string; filters: Filter[]; headerFilterGroup?: React.ReactNode; height?: number; id: string; indexPattern: IIndexPattern; isLive: boolean; + isLoadingIndexPattern: boolean; itemsPerPage: number; itemsPerPageOptions: number[]; kqlMode: KqlMode; onChangeItemsPerPage: OnChangeItemsPerPage; query: Query; - start: number; + start: string; sort: Sort; toggleColumn: (column: ColumnHeaderOptions) => void; utilityBar?: (refetch: inputsModel.Refetch, totalCount: number) => React.ReactNode; + // If truthy, the graph viewer (Resolver) is showing + graphEventId: string | undefined; } const EventsViewerComponent: React.FC = ({ @@ -74,6 +78,7 @@ const EventsViewerComponent: React.FC = ({ columns, dataProviders, deletedEventIds, + docValueFields, end, filters, headerFilterGroup, @@ -81,6 +86,7 @@ const EventsViewerComponent: React.FC = ({ id, indexPattern, isLive, + isLoadingIndexPattern, itemsPerPage, itemsPerPageOptions, kqlMode, @@ -90,6 +96,7 @@ const EventsViewerComponent: React.FC = ({ sort, toggleColumn, utilityBar, + graphEventId, }) => { const columnsHeader = isEmpty(columns) ? defaultHeaders : columns; const kibana = useKibana(); @@ -119,6 +126,17 @@ const EventsViewerComponent: React.FC = ({ end, isEventViewer: true, }); + + const canQueryTimeline = useMemo( + () => + combinedQueries != null && + isLoadingIndexPattern != null && + !isLoadingIndexPattern && + !isEmpty(start) && + !isEmpty(end), + [isLoadingIndexPattern, combinedQueries, start, end] + ); + const fields = useMemo( () => union( @@ -137,16 +155,19 @@ const EventsViewerComponent: React.FC = ({ return ( - {combinedQueries != null ? ( + {canQueryTimeline ? ( {({ events, @@ -184,6 +205,7 @@ const EventsViewerComponent: React.FC = ({ !deletedEventIds.includes(e._id))} + docValueFields={docValueFields} id={id} isEventViewer={true} height={height} @@ -191,22 +213,28 @@ const EventsViewerComponent: React.FC = ({ toggleColumn={toggleColumn} /> -