From 8e1fcc126ac1b5b012b5730d51ca1ba6304020d5 Mon Sep 17 00:00:00 2001 From: Saikat Sarkar <132922331+saikatsarkar056@users.noreply.github.com> Date: Tue, 22 Oct 2024 09:10:40 -0600 Subject: [PATCH 01/22] Add watsonx icon for inference endpoints management page (#197116) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR adds IBM Watsonx ai support for the Inference Endpoints Management UI. Screenshot 2024-10-21 at 12 22 09 PM --- .../src/constants/trained_models.ts | 10 ++++++++++ .../public/assets/images/providers/watsonx_ai.svg | 3 +++ .../render_service_provider/service_provider.tsx | 5 +++++ .../public/components/all_inference_endpoints/types.ts | 1 + 4 files changed, 19 insertions(+) create mode 100644 x-pack/plugins/search_inference_endpoints/public/assets/images/providers/watsonx_ai.svg diff --git a/x-pack/packages/ml/trained_models_utils/src/constants/trained_models.ts b/x-pack/packages/ml/trained_models_utils/src/constants/trained_models.ts index 9fd3483771a9f..c2eb7d0ed8ef3 100644 --- a/x-pack/packages/ml/trained_models_utils/src/constants/trained_models.ts +++ b/x-pack/packages/ml/trained_models_utils/src/constants/trained_models.ts @@ -287,6 +287,16 @@ export type InferenceServiceSettings = }; }; } + | { + service: 'watsonxai'; + service_settings: { + api_key: string; + url: string; + model_id: string; + project_id: string; + api_version: string; + }; + } | { service: 'amazonbedrock'; service_settings: { diff --git a/x-pack/plugins/search_inference_endpoints/public/assets/images/providers/watsonx_ai.svg b/x-pack/plugins/search_inference_endpoints/public/assets/images/providers/watsonx_ai.svg new file mode 100644 index 0000000000000..29f7a735e6614 --- /dev/null +++ b/x-pack/plugins/search_inference_endpoints/public/assets/images/providers/watsonx_ai.svg @@ -0,0 +1,3 @@ + + + diff --git a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_service_provider/service_provider.tsx b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_service_provider/service_provider.tsx index 574b3881f121b..74f15f22762f1 100644 --- a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_service_provider/service_provider.tsx +++ b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_service_provider/service_provider.tsx @@ -21,6 +21,7 @@ import googleAIStudioIcon from '../../../../assets/images/providers/google_ai_st import mistralIcon from '../../../../assets/images/providers/mistral.svg'; import amazonBedrockIcon from '../../../../assets/images/providers/amazon_bedrock.svg'; import alibabaCloudAISearchIcon from '../../../../assets/images/providers/alibaba_cloud_ai_search.svg'; +import watsonxAIIcon from '../../../../assets/images/providers/watsonx_ai.svg'; import { ServiceProviderKeys } from '../../types'; import * as i18n from './translations'; @@ -78,6 +79,10 @@ export const SERVICE_PROVIDERS: Record = ({ providerEndpoint }) => { diff --git a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/types.ts b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/types.ts index c1f23a3a4f2e3..b5c5fc49aa1fa 100644 --- a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/types.ts +++ b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/types.ts @@ -21,6 +21,7 @@ export enum ServiceProviderKeys { hugging_face = 'hugging_face', mistral = 'mistral', openai = 'openai', + watsonxai = 'watsonxai', } export enum SortFieldInferenceEndpoint { From f629d96b85b222fbc3116e6a0a4308882d144aef Mon Sep 17 00:00:00 2001 From: Jusheng Huang <117657272+viajes7@users.noreply.github.com> Date: Tue, 22 Oct 2024 23:12:12 +0800 Subject: [PATCH 02/22] [Index Management] Fix Flashing error banner in Index Template form (#196786) ## Summary Fixes #194751 In `EuiForm`, if the value of `isInvalid` is true, it will render addressFormErrors tip messages. And In `step_logistics.tsx`, `isInValid` is setted by `isInvalid={isSubmitted && !isFormValid}` But form `submit` method set `isSubmitted` to true first, then validate form. At this time, `isSubmitted is true` and `isFormValid is unedfined`. So addressFormErrors shows immediately. ![image](https://github.com/user-attachments/assets/687a359d-ed33-4860-8d96-4543f2ef4898) **So, maybe when `isSubmitting` is true, addressFormErrors should not be shown.** Co-authored-by: Elastic Machine --- .../components/template_form/steps/step_logistics.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 84cb0fa027ad1..d73d95600e5b1 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 @@ -194,6 +194,7 @@ export const StepLogistics: React.FunctionComponent = React.memo( const { submit, isSubmitted, + isSubmitting, isValid: isFormValid, getErrors: getFormErrors, getFormData, @@ -275,7 +276,7 @@ export const StepLogistics: React.FunctionComponent = React.memo(
From 2664ccf028ab8784e57a11f380cf6ae1d2b19c90 Mon Sep 17 00:00:00 2001 From: Tre Date: Tue, 22 Oct 2024 16:27:32 +0100 Subject: [PATCH 03/22] [FTR][Ownership] Assign ally/grps 1,2 & 3 (#195964) ## Summary Assign files within `x-pack/test/accessibility/apps/group[1|2|3]` ### Why I assigned them: Assigned advanced_settings due to https://github.com/elastic/kibana/pull/175255 Assigned dashboard_controls due to https://github.com/elastic/kibana/pull/190797 Assigned dashboard_links due to https://github.com/elastic/kibana/pull/174772 Assigned dashboard_panel_options due to https://github.com/elastic/kibana/pull/178596 Assigned grok_debugger due to https://github.com/elastic/kibana/blob/main/x-pack/plugins/grokdebugger/kibana.jsonc#L4 Assigned helpers due to https://github.com/elastic/kibana/pull/164341 (call site) Assigned home due to https://github.com/elastic/kibana/pull/103192 Assigned index_lifecycle_management due to https://github.com/elastic/kibana/pull/116207 Assigned ingest_node_pipelines due to https://github.com/elastic/kibana/pull/113783 Assigned kibana_overview due to https://github.com/elastic/kibana/blob/f00ac7a8a21463e6bb4a2784c3a3884f36c62900/x-pack/plugins/grokdebugger/kibana.jsonc#L4 Assigned management due to https://github.com/elastic/kibana/pull/165705 Assigned painless_lab due to https://github.com/elastic/kibana/blob/main/x-pack/plugins/painless_lab/kibana.jsonc#L4 Assigned search_profiler due to https://github.com/elastic/kibana/pull/195343 Assigned uptime due to https://github.com/elastic/kibana/blob/main/x-pack/plugins/observability_solution/uptime/kibana.jsonc#L4 Assigned lens due to https://github.com/elastic/kibana/pull/175893 Assigned ml_anomaly_detection due to https://github.com/elastic/kibana/pull/162126 Assigned canvas due to https://github.com/elastic/kibana/pull/164376 Assigned cc replication due to https://github.com/elastic/kibana/pull/149069 Assigned enterpise search due to https://github.com/elastic/kibana/issues/79359 Assigned graph to due https://github.com/elastic/kibana/pull/190797 Assigned license_management due to https://github.com/elastic/kibana/blob/main/x-pack/plugins/license_management/kibana.jsonc#L4 Assigned maps due to https://github.com/elastic/kibana/pull/155161 Assigned observability due to https://github.com/elastic/kibana/blob/main/x-pack/plugins/observability_solution/observability/kibana.jsonc#L4 Assigned remote clusters due to https://github.com/elastic/kibana/pull/96989 Assigned reporting due to https://github.com/elastic/kibana/pull/121435 Assigned rollup_jobs due to https://github.com/elastic/kibana/blob/d57bc9a5d7d64f86b550eff7997605a3090aee9a/x-pack/plugins/rollup/kibana.jsonc#L4 Assigned watcher due to https://github.com/elastic/kibana/pull/119717 Contributes to: https://github.com/elastic/kibana/issues/194817 --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Elastic Machine Co-authored-by: Rodney Norris --- .github/CODEOWNERS | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index c000628cf9c52..4dc62da409d25 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1088,6 +1088,8 @@ src/plugins/discover/public/context_awareness/profile_providers/security @elasti /x-pack/test_serverless/functional/test_suites/security/config.screenshots.ts @elastic/platform-docs # Visualizations +/x-pack/test/accessibility/apps/group3/graph.ts @elastic/kibana-visualizations +/x-pack/test/accessibility/apps/group2/lens.ts @elastic/kibana-visualizations /src/plugins/visualize/ @elastic/kibana-visualizations /x-pack/test/functional/apps/lens @elastic/kibana-visualizations /x-pack/test/api_integration/apis/lens/ @elastic/kibana-visualizations @@ -1115,6 +1117,7 @@ packages/kbn-monaco/src/esql @elastic/kibana-esql /docs/settings/reporting-settings.asciidoc @elastic/appex-sharedux /docs/setup/configuring-reporting.asciidoc @elastic/appex-sharedux /x-pack/test_serverless/**/test_suites/common/reporting/ @elastic/appex-sharedux +/x-pack/test/accessibility/apps/group3/reporting.ts @elastic/appex-sharedux ### Global Experience Tagging /x-pack/test/saved_object_tagging/ @elastic/appex-sharedux @@ -1201,6 +1204,8 @@ x-pack/test_serverless/**/test_suites/observability/ai_assistant @elastic/obs-ai /x-pack/test/functional/apps/infra/logs @elastic/obs-ux-logs-team # Observability UX management team +/x-pack/test/accessibility/apps/group1/uptime.ts @elastic/obs-ux-management-team +/x-pack/test/accessibility/apps/group3/observability.ts @elastic/obs-ux-management-team /x-pack/packages/observability/alert_details @elastic/obs-ux-management-team /x-pack/test/observability_functional @elastic/obs-ux-management-team /x-pack/plugins/observability_solution/infra/public/alerting @elastic/obs-ux-management-team @@ -1217,6 +1222,8 @@ x-pack/test_serverless/**/test_suites/observability/ai_assistant @elastic/obs-ai /x-pack/test/functional/apps/monitoring @elastic/stack-monitoring /x-pack/test/api_integration/apis/monitoring @elastic/stack-monitoring /x-pack/test/api_integration/apis/monitoring_collection @elastic/stack-monitoring +/x-pack/test/accessibility/apps/group1/kibana_overview.ts @elastic/stack-monitoring +/x-pack/test/accessibility/apps/group3/stack_monitoring.ts @elastic/stack-monitoring # Fleet /x-pack/test/fleet_api_integration @elastic/fleet @@ -1269,6 +1276,10 @@ x-pack/test_serverless/**/test_suites/observability/ai_assistant @elastic/obs-ai ### END Observability Plugins # Presentation +/x-pack/test/accessibility/apps/group3/maps.ts @elastic/kibana-presentation +/x-pack/test/accessibility/apps/group1/dashboard_panel_options.ts @elastic/kibana-presentation +/x-pack/test/accessibility/apps/group1/dashboard_links.ts @elastic/kibana-presentation +/x-pack/test/accessibility/apps/group1/dashboard_controls.ts @elastic/kibana-presentation /test/functional/apps/dashboard/ @elastic/kibana-presentation /test/functional/apps/dashboard_elements/ @elastic/kibana-presentation /test/functional/services/dashboard/ @elastic/kibana-presentation @@ -1342,6 +1353,9 @@ x-pack/test_serverless/**/test_suites/observability/ai_assistant @elastic/obs-ai /.eslintignore @elastic/kibana-operations # Appex QA +/x-pack/test/accessibility/services.ts @elastic/appex-qa +/x-pack/test/accessibility/page_objects.ts @elastic/appex-qa +/x-pack/test/accessibility/ftr_provider_context.d.ts @elastic/appex-qa /x-pack/test_serverless/tsconfig.json @elastic/appex-qa /x-pack/test_serverless/kibana.jsonc @elastic/appex-qa /x-pack/test_serverless/functional/test_suites/common/README.md @elastic/appex-qa @@ -1458,6 +1472,7 @@ x-pack/plugins/cloud_integrations/cloud_full_story/server/config.ts @elastic/kib #CC# /x-pack/plugins/security/ @elastic/kibana-security # Response Ops team +/x-pack/test/accessibility/apps/group3/rules_connectors.ts @elastic/response-ops /x-pack/test/functional/es_archives/cases/default @elastic/response-ops /x-pack/test_serverless/api_integration/test_suites/observability/config.ts @elastic/response-ops /x-pack/test_serverless/api_integration/test_suites/observability/index.ts @elastic/response-ops @@ -1530,10 +1545,26 @@ x-pack/test/api_integration/apis/management/index_management/inference_endpoints /x-pack/test/api_integration/apis/management/ @elastic/kibana-management /x-pack/test/functional/apps/rollup_job/ @elastic/kibana-management /x-pack/test/api_integration/apis/grok_debugger @elastic/kibana-management +/x-pack/test/accessibility/apps/group1/advanced_settings.ts @elastic/kibana-management +/x-pack/test/accessibility/apps/**/grok_debugger.ts @elastic/kibana-management +/x-pack/test/accessibility/apps/group1/helpers.ts @elastic/kibana-management +/x-pack/test/accessibility/apps/group1/home.ts @elastic/kibana-management +/x-pack/test/accessibility/apps/group1/index_lifecycle_management.ts @elastic/kibana-management +/x-pack/test/accessibility/apps/group1/ingest_node_pipelines.ts @elastic/kibana-management +/x-pack/test/accessibility/apps/group1/management.ts @elastic/kibana-management +/x-pack/test/accessibility/apps/group1/painless_lab.ts @elastic/kibana-management +/x-pack/test/accessibility/apps/group1/search_profiler.ts @elastic/kibana-management +/x-pack/test/accessibility/apps/group3/cross_cluster_replication.ts @elastic/kibana-management +/x-pack/test/accessibility/apps/group3/license_management.ts @elastic/kibana-management +/x-pack/test/accessibility/apps/group3/remote_clusters.ts @elastic/kibana-management +/x-pack/test/accessibility/apps/group3/rollup_jobs.ts @elastic/kibana-management +/x-pack/test/accessibility/apps/group3/upgrade_assistant.ts @elastic/kibana-management +/x-pack/test/accessibility/apps/group3/watcher.ts @elastic/kibana-management #CC# /x-pack/plugins/cross_cluster_replication/ @elastic/kibana-management # Security Solution +/x-pack/test/accessibility/apps/group3/security_solution.ts @elastic/security-solution /x-pack/test_serverless/functional/test_suites/security/config.ts @elastic/security-solution @elastic/appex-qa /x-pack/test_serverless/functional/test_suites/security/config.feature_flags.ts @elastic/security-solution /x-pack/test_serverless/api_integration/test_suites/observability/config.feature_flags.ts @elastic/security-solution @@ -1890,6 +1921,7 @@ x-pack/plugins/security_solution/server/lib/security_integrations @elastic/secur # Ent. Search design /x-pack/plugins/enterprise_search/**/*.scss @elastic/search-design +/x-pack/test/accessibility/apps/group3/enterprise_search.ts @elastic/search-kibana # Security design /x-pack/plugins/endpoint/**/*.scss @elastic/security-design @@ -1917,6 +1949,8 @@ x-pack/plugins/observability_solution/observability_shared/public/components/pro # Shared UX /x-pack/test/api_integration/apis/content_management @elastic/appex-sharedux +/x-pack/test/accessibility/apps/group3/tags.ts @elastic/appex-sharedux +/x-pack/test/accessibility/apps/group3/snapshot_and_restore.ts @elastic/appex-sharedux /x-pack/test_serverless/functional/test_suites/common/spaces/spaces_selection.ts @elastic/appex-sharedux /x-pack/test_serverless/functional/test_suites/common/spaces/index.ts @elastic/appex-sharedux packages/react @elastic/appex-sharedux From 5e40320d9fe2d2c0b676071edf011c5831d43662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Marcondes?= <55978943+cauemarcondes@users.noreply.github.com> Date: Tue, 22 Oct 2024 16:43:29 +0100 Subject: [PATCH 04/22] [Synthtrace][Inventory] K8s entities support (#197077) Part of https://github.com/elastic/kibana/issues/196155 New Synthtrace scenario created: [k8s_entities.ts](https://github.com/elastic/kibana/pull/197077/files#diff-33a935a5fb8848d743e40d89b018ed8ac82ae992c0778cd6cea072d091aa0647) ``` node scripts/synthtrace k8s_entities.ts --clean --live ``` Cluster sample data: ``` { "_index": ".entities.v1.latest.builtin_kubernetes_cluster_ecss_from_ecs_data", "_id": "2060900000000000", "_score": 1, "_source": { "entity": { "type": "kubernetes_cluster_ecs", "id": "2060900000000000", "definitionId": "builtin_kubernetes_cluster_ecs", "displayName": "cluster_foo", "lastSeenTimestamp": "2024-10-21T16:15:17.570Z" }, "orchestrator": { "cluster": { "name": "cluster_foo" } }, "event": { "ingested": "2024-10-21T16:15:17.570Z" } } }, { "_index": ".entities.v1.latest.builtin_kubernetes_cluster_semconvs_from_ecs_data", "_id": "2060900000000000", "_score": 1, "_source": { "entity": { "type": "kubernetes_cluster_semconv", "id": "2060900000000000", "definitionId": "builtin_kubernetes_cluster_semconv", "displayName": "cluster_foo", "lastSeenTimestamp": "2024-10-21T16:15:17.570Z" }, "k8s": { "cluster": { "uid": "cluster_foo" } }, "event": { "ingested": "2024-10-21T16:15:17.570Z" } } }, ``` --- .../src/lib/entities/index.ts | 29 +++- .../lib/entities/kubernetes/cluster_entity.ts | 39 +++++ .../entities/kubernetes/container_entity.ts | 39 +++++ .../entities/kubernetes/cron_job_entity.ts | 47 ++++++ .../entities/kubernetes/daemon_set_entity.ts | 47 ++++++ .../entities/kubernetes/deployment_entity.ts | 47 ++++++ .../src/lib/entities/kubernetes/index.ts | 76 +++++++++ .../lib/entities/kubernetes/job_set_entity.ts | 47 ++++++ .../lib/entities/kubernetes/node_entity.ts | 46 ++++++ .../src/lib/entities/kubernetes/pod_entity.ts | 47 ++++++ .../lib/entities/kubernetes/replica_set.ts | 47 ++++++ .../lib/entities/kubernetes/stateful_set.ts | 47 ++++++ .../entities/entities_synthtrace_es_client.ts | 3 +- .../src/scenarios/k8s_entities.ts | 155 ++++++++++++++++++ 14 files changed, 714 insertions(+), 2 deletions(-) create mode 100644 packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/cluster_entity.ts create mode 100644 packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/container_entity.ts create mode 100644 packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/cron_job_entity.ts create mode 100644 packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/daemon_set_entity.ts create mode 100644 packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/deployment_entity.ts create mode 100644 packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/index.ts create mode 100644 packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/job_set_entity.ts create mode 100644 packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/node_entity.ts create mode 100644 packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/pod_entity.ts create mode 100644 packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/replica_set.ts create mode 100644 packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/stateful_set.ts create mode 100644 packages/kbn-apm-synthtrace/src/scenarios/k8s_entities.ts diff --git a/packages/kbn-apm-synthtrace-client/src/lib/entities/index.ts b/packages/kbn-apm-synthtrace-client/src/lib/entities/index.ts index 10cf982ff41ee..4242476735a11 100644 --- a/packages/kbn-apm-synthtrace-client/src/lib/entities/index.ts +++ b/packages/kbn-apm-synthtrace-client/src/lib/entities/index.ts @@ -11,8 +11,19 @@ import { Fields } from '../entity'; import { serviceEntity } from './service_entity'; import { hostEntity } from './host_entity'; import { containerEntity } from './container_entity'; +import { k8sClusterJobEntity } from './kubernetes/cluster_entity'; +import { k8sCronJobEntity } from './kubernetes/cron_job_entity'; +import { k8sDaemonSetEntity } from './kubernetes/daemon_set_entity'; +import { k8sDeploymentEntity } from './kubernetes/deployment_entity'; +import { k8sJobSetEntity } from './kubernetes/job_set_entity'; +import { k8sNodeEntity } from './kubernetes/node_entity'; +import { k8sPodEntity } from './kubernetes/pod_entity'; +import { k8sReplicaSetEntity } from './kubernetes/replica_set'; +import { k8sStatefulSetEntity } from './kubernetes/stateful_set'; +import { k8sContainerEntity } from './kubernetes/container_entity'; export type EntityDataStreamType = 'metrics' | 'logs' | 'traces'; +export type Schema = 'ecs' | 'semconv'; export type EntityFields = Fields & Partial<{ @@ -32,4 +43,20 @@ export type EntityFields = Fields & [key: string]: any; }>; -export const entities = { serviceEntity, hostEntity, containerEntity }; +export const entities = { + serviceEntity, + hostEntity, + containerEntity, + k8s: { + k8sClusterJobEntity, + k8sCronJobEntity, + k8sDaemonSetEntity, + k8sDeploymentEntity, + k8sJobSetEntity, + k8sNodeEntity, + k8sPodEntity, + k8sReplicaSetEntity, + k8sStatefulSetEntity, + k8sContainerEntity, + }, +}; diff --git a/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/cluster_entity.ts b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/cluster_entity.ts new file mode 100644 index 0000000000000..9fa4c81d86ffb --- /dev/null +++ b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/cluster_entity.ts @@ -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 + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { Schema } from '..'; +import { K8sEntity } from '.'; + +export function k8sClusterJobEntity({ + schema, + name, + entityId, + ...others +}: { + schema: Schema; + name: string; + entityId: string; + [key: string]: any; +}) { + if (schema === 'ecs') { + return new K8sEntity(schema, { + 'entity.type': 'cluster', + 'orchestrator.cluster.name': name, + 'entity.id': entityId, + ...others, + }); + } + + return new K8sEntity(schema, { + 'entity.type': 'cluster', + 'k8s.cluster.uid': name, + 'entity.id': entityId, + ...others, + }); +} diff --git a/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/container_entity.ts b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/container_entity.ts new file mode 100644 index 0000000000000..b05d412b0dd5c --- /dev/null +++ b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/container_entity.ts @@ -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 + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { Schema } from '..'; +import { K8sEntity } from '.'; + +export function k8sContainerEntity({ + schema, + id, + entityId, + ...others +}: { + schema: Schema; + id: string; + entityId: string; + [key: string]: any; +}) { + if (schema === 'ecs') { + return new K8sEntity(schema, { + 'entity.type': 'container', + 'kubernetes.container.id': id, + 'entity.id': entityId, + ...others, + }); + } + + return new K8sEntity(schema, { + 'entity.type': 'container', + 'container.id': id, + 'entity.id': entityId, + ...others, + }); +} diff --git a/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/cron_job_entity.ts b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/cron_job_entity.ts new file mode 100644 index 0000000000000..8590378e699fb --- /dev/null +++ b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/cron_job_entity.ts @@ -0,0 +1,47 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { Schema } from '..'; +import { K8sEntity } from '.'; + +export function k8sCronJobEntity({ + schema, + name, + uid, + clusterName, + entityId, + ...others +}: { + schema: Schema; + name: string; + uid?: string; + clusterName?: string; + entityId: string; + [key: string]: any; +}) { + if (schema === 'ecs') { + return new K8sEntity(schema, { + 'entity.type': 'cron_job', + 'kubernetes.cronjob.name': name, + 'kubernetes.cronjob.uid': uid, + 'kubernetes.namespace': clusterName, + 'entity.id': entityId, + ...others, + }); + } + + return new K8sEntity(schema, { + 'entity.type': 'cron_job', + 'k8s.cronjob.name': name, + 'k8s.cronjob.uid': uid, + 'k8s.cluster.name': clusterName, + 'entity.id': entityId, + ...others, + }); +} diff --git a/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/daemon_set_entity.ts b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/daemon_set_entity.ts new file mode 100644 index 0000000000000..7e20b1c6d506f --- /dev/null +++ b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/daemon_set_entity.ts @@ -0,0 +1,47 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { Schema } from '..'; +import { K8sEntity } from '.'; + +export function k8sDaemonSetEntity({ + schema, + name, + uid, + clusterName, + entityId, + ...others +}: { + schema: Schema; + name: string; + uid?: string; + clusterName?: string; + entityId: string; + [key: string]: any; +}) { + if (schema === 'ecs') { + return new K8sEntity(schema, { + 'entity.type': 'daemon_set', + 'kubernetes.daemonset.name': name, + 'kubernetes.daemonset.uid': uid, + 'kubernetes.namespace': clusterName, + 'entity.id': entityId, + ...others, + }); + } + + return new K8sEntity(schema, { + 'entity.type': 'daemon_set', + 'k8s.daemonset.name': name, + 'k8s.daemonset.uid': uid, + 'k8s.cluster.name': clusterName, + 'entity.id': entityId, + ...others, + }); +} diff --git a/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/deployment_entity.ts b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/deployment_entity.ts new file mode 100644 index 0000000000000..7eabdd0827325 --- /dev/null +++ b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/deployment_entity.ts @@ -0,0 +1,47 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { Schema } from '..'; +import { K8sEntity } from '.'; + +export function k8sDeploymentEntity({ + schema, + name, + uid, + clusterName, + entityId, + ...others +}: { + schema: Schema; + name: string; + uid?: string; + clusterName?: string; + entityId: string; + [key: string]: any; +}) { + if (schema === 'ecs') { + return new K8sEntity(schema, { + 'entity.type': 'deployment', + 'kubernetes.deployment.name': name, + 'kubernetes.deployment.uid': uid, + 'kubernetes.namespace': clusterName, + 'entity.id': entityId, + ...others, + }); + } + + return new K8sEntity(schema, { + 'entity.type': 'deployment', + 'k8s.deployment.name': name, + 'k8s.deployment.uid': uid, + 'k8s.cluster.name': clusterName, + 'entity.id': entityId, + ...others, + }); +} diff --git a/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/index.ts b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/index.ts new file mode 100644 index 0000000000000..36d7f8caf9601 --- /dev/null +++ b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/index.ts @@ -0,0 +1,76 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import type { EntityFields, Schema } from '..'; +import { Serializable } from '../../serializable'; + +const identityFieldsMap: Record> = { + ecs: { + pod: ['kubernetes.pod.name'], + cluster: ['orchestrator.cluster.name'], + cron_job: ['kubernetes.cronjob.name'], + daemon_set: ['kubernetes.daemonset.name'], + deployment: ['kubernetes.deployment.name'], + job: ['kubernetes.job.name'], + node: ['kubernetes.node.name'], + replica_set: ['kubernetes.replicaset.name'], + stateful_set: ['kubernetes.statefulset.name'], + container: ['kubernetes.container.id'], + }, + semconv: { + pod: ['k8s.pod.name'], + cluster: ['k8s.cluster.uid'], + cron_job: ['k8s.cronjob.name'], + daemon_set: ['k8s.daemonset.name'], + deployment: ['k8s.deployment.name'], + job: ['k8s.job.name'], + node: ['k8s.node.uid'], + replica_set: ['k8s.replicaset.name'], + stateful_set: ['k8s.statefulset.name'], + container: ['container.id'], + }, +}; + +export class K8sEntity extends Serializable { + constructor(schema: Schema, fields: EntityFields) { + const entityType = fields['entity.type']; + if (entityType === undefined) { + throw new Error(`Entity type not defined: ${entityType}`); + } + + const entityTypeWithSchema = `kubernetes_${entityType}_${schema}`; + const identityFields = identityFieldsMap[schema][entityType]; + if (identityFields === undefined || identityFields.length === 0) { + throw new Error( + `Identity fields not defined for schema: ${schema} and entity type: ${entityType}` + ); + } + + super({ + ...fields, + 'entity.type': entityTypeWithSchema, + 'entity.definitionId': `builtin_${entityTypeWithSchema}`, + 'entity.identityFields': identityFields, + 'entity.displayName': getDisplayName({ identityFields, fields }), + }); + } +} + +function getDisplayName({ + identityFields, + fields, +}: { + identityFields: string[]; + fields: EntityFields; +}) { + return identityFields + .map((field) => fields[field]) + .filter((_) => _) + .join(':'); +} diff --git a/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/job_set_entity.ts b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/job_set_entity.ts new file mode 100644 index 0000000000000..e0383563c7266 --- /dev/null +++ b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/job_set_entity.ts @@ -0,0 +1,47 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { Schema } from '..'; +import { K8sEntity } from '.'; + +export function k8sJobSetEntity({ + schema, + name, + uid, + clusterName, + entityId, + ...others +}: { + schema: Schema; + name: string; + uid?: string; + clusterName?: string; + entityId: string; + [key: string]: any; +}) { + if (schema === 'ecs') { + return new K8sEntity(schema, { + 'entity.type': 'job', + 'kubernetes.job.name': name, + 'kubernetes.job.uid': uid, + 'kubernetes.namespace': clusterName, + 'entity.id': entityId, + ...others, + }); + } + + return new K8sEntity(schema, { + 'entity.type': 'job', + 'k8s.job.name': name, + 'k8s.job.uid': uid, + 'k8s.cluster.name': clusterName, + 'entity.id': entityId, + ...others, + }); +} diff --git a/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/node_entity.ts b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/node_entity.ts new file mode 100644 index 0000000000000..283df5250d41d --- /dev/null +++ b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/node_entity.ts @@ -0,0 +1,46 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { Schema } from '..'; +import { K8sEntity } from '.'; + +export function k8sNodeEntity({ + schema, + name, + uid, + clusterName, + entityId, + ...others +}: { + schema: Schema; + name: string; + uid?: string; + clusterName?: string; + entityId: string; + [key: string]: any; +}) { + if (schema === 'ecs') { + return new K8sEntity(schema, { + 'entity.type': 'node', + 'kubernetes.node.name': name, + 'kubernetes.node.uid': uid, + 'kubernetes.namespace': clusterName, + 'entity.id': entityId, + ...others, + }); + } + + return new K8sEntity(schema, { + 'entity.type': 'node', + 'k8s.node.uid': uid, + 'k8s.cluster.name': clusterName, + 'entity.id': entityId, + ...others, + }); +} diff --git a/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/pod_entity.ts b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/pod_entity.ts new file mode 100644 index 0000000000000..1b71c4e39a4fc --- /dev/null +++ b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/pod_entity.ts @@ -0,0 +1,47 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { Schema } from '..'; +import { K8sEntity } from '.'; + +export function k8sPodEntity({ + schema, + name, + uid, + clusterName, + entityId, + ...others +}: { + schema: Schema; + name: string; + uid?: string; + clusterName?: string; + entityId: string; + [key: string]: any; +}) { + if (schema === 'ecs') { + return new K8sEntity(schema, { + 'entity.type': 'pod', + 'kubernetes.pod.name': name, + 'kubernetes.pod.uid': uid, + 'kubernetes.namespace': clusterName, + 'entity.id': entityId, + ...others, + }); + } + + return new K8sEntity(schema, { + 'entity.type': 'pod', + 'k8s.pod.name': name, + 'k8s.pod.uid': uid, + 'k8s.cluster.name': clusterName, + 'entity.id': entityId, + ...others, + }); +} diff --git a/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/replica_set.ts b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/replica_set.ts new file mode 100644 index 0000000000000..fcf20c0530c30 --- /dev/null +++ b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/replica_set.ts @@ -0,0 +1,47 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { Schema } from '..'; +import { K8sEntity } from '.'; + +export function k8sReplicaSetEntity({ + schema, + name, + uid, + clusterName, + entityId, + ...others +}: { + schema: Schema; + name: string; + uid?: string; + clusterName?: string; + entityId: string; + [key: string]: any; +}) { + if (schema === 'ecs') { + return new K8sEntity(schema, { + 'entity.type': 'replica_set', + 'kubernetes.replicaset.name': name, + 'kubernetes.replicaset.uid': uid, + 'kubernetes.namespace': clusterName, + 'entity.id': entityId, + ...others, + }); + } + + return new K8sEntity(schema, { + 'entity.type': 'replica_set', + 'k8s.replicaset.name': name, + 'k8s.replicaset.uid': uid, + 'k8s.cluster.name': clusterName, + 'entity.id': entityId, + ...others, + }); +} diff --git a/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/stateful_set.ts b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/stateful_set.ts new file mode 100644 index 0000000000000..58c603704ebc0 --- /dev/null +++ b/packages/kbn-apm-synthtrace-client/src/lib/entities/kubernetes/stateful_set.ts @@ -0,0 +1,47 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { Schema } from '..'; +import { K8sEntity } from '.'; + +export function k8sStatefulSetEntity({ + schema, + name, + uid, + clusterName, + entityId, + ...others +}: { + schema: Schema; + name: string; + uid?: string; + clusterName?: string; + entityId: string; + [key: string]: any; +}) { + if (schema === 'ecs') { + return new K8sEntity(schema, { + 'entity.type': 'stateful_set', + 'kubernetes.statefulset.name': name, + 'kubernetes.statefulset.uid': uid, + 'kubernetes.namespace': clusterName, + 'entity.id': entityId, + ...others, + }); + } + + return new K8sEntity(schema, { + 'entity.type': 'stateful_set', + 'k8s.statefulset.name': name, + 'k8s.statefulset.uid': uid, + 'k8s.cluster.name': clusterName, + 'entity.id': entityId, + ...others, + }); +} diff --git a/packages/kbn-apm-synthtrace/src/lib/entities/entities_synthtrace_es_client.ts b/packages/kbn-apm-synthtrace/src/lib/entities/entities_synthtrace_es_client.ts index 684e3efc0f372..65bf290eae32f 100644 --- a/packages/kbn-apm-synthtrace/src/lib/entities/entities_synthtrace_es_client.ts +++ b/packages/kbn-apm-synthtrace/src/lib/entities/entities_synthtrace_es_client.ts @@ -82,7 +82,8 @@ function getRoutingTransform() { const entityIndexName = `${entityType}s`; document._action = { index: { - _index: `.entities.v1.latest.builtin_${entityIndexName}_from_ecs_data`, + _index: + `.entities.v1.latest.builtin_${entityIndexName}_from_ecs_data`.toLocaleLowerCase(), _id: document['entity.id'], }, }; diff --git a/packages/kbn-apm-synthtrace/src/scenarios/k8s_entities.ts b/packages/kbn-apm-synthtrace/src/scenarios/k8s_entities.ts new file mode 100644 index 0000000000000..7d94cc3180a7e --- /dev/null +++ b/packages/kbn-apm-synthtrace/src/scenarios/k8s_entities.ts @@ -0,0 +1,155 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { EntityFields, entities, generateShortId } from '@kbn/apm-synthtrace-client'; +import { Schema } from '@kbn/apm-synthtrace-client/src/lib/entities'; +import { Scenario } from '../cli/scenario'; +import { withClient } from '../lib/utils/with_client'; + +const CLUSTER_NAME = 'cluster_foo'; + +const CLUSTER_ENTITY_ID = generateShortId(); +const POD_ENTITY_ID = generateShortId(); +const POD_UID = generateShortId(); +const REPLICA_SET_ENTITY_ID = generateShortId(); +const REPLICA_SET_UID = generateShortId(); +const DEPLOYMENT_ENTITY_ID = generateShortId(); +const DEPLOYMENT_UID = generateShortId(); +const STATEFUL_SET_ENTITY_ID = generateShortId(); +const STATEFUL_SET_UID = generateShortId(); +const DAEMON_SET_ENTITY_ID = generateShortId(); +const DAEMON_SET_UID = generateShortId(); +const JOB_SET_ENTITY_ID = generateShortId(); +const JOB_SET_UID = generateShortId(); +const CRON_JOB_ENTITY_ID = generateShortId(); +const CRON_JOB_UID = generateShortId(); +const NODE_ENTITY_ID = generateShortId(); +const NODE_UID = generateShortId(); + +const scenario: Scenario> = async (runOptions) => { + const { logger } = runOptions; + + return { + bootstrap: async ({ entitiesKibanaClient }) => { + await entitiesKibanaClient.installEntityIndexPatterns(); + }, + generate: ({ range, clients: { entitiesEsClient } }) => { + const getK8sEntitiesEvents = (schema: Schema) => + range + .interval('1m') + .rate(1) + .generator((timestamp) => { + return [ + entities.k8s + .k8sClusterJobEntity({ + schema, + name: CLUSTER_NAME, + entityId: CLUSTER_ENTITY_ID, + }) + .timestamp(timestamp), + entities.k8s + .k8sPodEntity({ + schema, + clusterName: CLUSTER_NAME, + name: 'pod_foo', + uid: POD_UID, + entityId: POD_ENTITY_ID, + }) + .timestamp(timestamp), + entities.k8s + .k8sReplicaSetEntity({ + clusterName: CLUSTER_NAME, + name: 'replica_set_foo', + schema, + uid: REPLICA_SET_UID, + entityId: REPLICA_SET_ENTITY_ID, + }) + .timestamp(timestamp), + entities.k8s + .k8sDeploymentEntity({ + clusterName: CLUSTER_NAME, + name: 'deployment_foo', + schema, + uid: DEPLOYMENT_UID, + entityId: DEPLOYMENT_ENTITY_ID, + }) + .timestamp(timestamp), + entities.k8s + .k8sStatefulSetEntity({ + clusterName: CLUSTER_NAME, + name: 'stateful_set_foo', + schema, + uid: STATEFUL_SET_UID, + entityId: STATEFUL_SET_ENTITY_ID, + }) + .timestamp(timestamp), + entities.k8s + .k8sDaemonSetEntity({ + clusterName: CLUSTER_NAME, + name: 'daemon_set_foo', + schema, + uid: DAEMON_SET_UID, + entityId: DAEMON_SET_ENTITY_ID, + }) + .timestamp(timestamp), + entities.k8s + .k8sJobSetEntity({ + clusterName: CLUSTER_NAME, + name: 'job_set_foo', + schema, + uid: JOB_SET_UID, + entityId: JOB_SET_ENTITY_ID, + }) + .timestamp(timestamp), + entities.k8s + .k8sCronJobEntity({ + clusterName: CLUSTER_NAME, + name: 'cron_job_foo', + schema, + uid: CRON_JOB_UID, + entityId: CRON_JOB_ENTITY_ID, + }) + .timestamp(timestamp), + entities.k8s + .k8sNodeEntity({ + clusterName: CLUSTER_NAME, + name: 'node_job_foo', + schema, + uid: NODE_UID, + entityId: NODE_ENTITY_ID, + }) + .timestamp(timestamp), + entities.k8s + .k8sContainerEntity({ + id: '123', + schema, + entityId: NODE_ENTITY_ID, + }) + .timestamp(timestamp), + ]; + }); + + const ecsEntities = getK8sEntitiesEvents('ecs'); + const otelEntities = getK8sEntitiesEvents('semconv'); + + return [ + withClient( + entitiesEsClient, + logger.perf('generating_entities_ecs_events', () => ecsEntities) + ), + withClient( + entitiesEsClient, + logger.perf('generating_entities_otel_events', () => otelEntities) + ), + ]; + }, + }; +}; + +export default scenario; From b562288289f326084511c68759db84e20cee2791 Mon Sep 17 00:00:00 2001 From: Abhishek Bhatia <117628830+abhishekbhatia1710@users.noreply.github.com> Date: Tue, 22 Oct 2024 21:15:52 +0530 Subject: [PATCH 05/22] Kibana space scoped component-template for risk engine (#197170) ## Summary The component template used when enabling the risk engine is not Kibana space-agnostic, as the component template in every space is named `.risk-score-mappings`. This caused issues during the cleanup process, where it attempted to delete the same component template from each space but failed because other spaces' index templates were still referencing it. ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [x] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed ### For maintainers - [ ] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#_add_your_labels) - [ ] This will appear in the **Release Notes** and follow the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> --- .../risk_score/configurations.ts | 3 + .../risk_score/risk_score_data_client.test.ts | 676 +++++++++--------- .../risk_score/risk_score_data_client.ts | 45 +- .../init_and_status_apis.ts | 247 ++++++- 4 files changed, 641 insertions(+), 330 deletions(-) diff --git a/x-pack/plugins/security_solution/server/lib/entity_analytics/risk_score/configurations.ts b/x-pack/plugins/security_solution/server/lib/entity_analytics/risk_score/configurations.ts index 6dbf68c699fd5..610ada87f5159 100644 --- a/x-pack/plugins/security_solution/server/lib/entity_analytics/risk_score/configurations.ts +++ b/x-pack/plugins/security_solution/server/lib/entity_analytics/risk_score/configurations.ts @@ -129,6 +129,9 @@ export const riskScoreFieldMap: FieldMap = { } as const; export const mappingComponentName = '.risk-score-mappings'; +export const nameSpaceAwareMappingsComponentName = (namespace: string): string => { + return `${mappingComponentName}-${namespace}`; +}; export const totalFieldsLimit = 1000; export const getIndexPatternDataStream = (namespace: string): IIndexPatternString => ({ diff --git a/x-pack/plugins/security_solution/server/lib/entity_analytics/risk_score/risk_score_data_client.test.ts b/x-pack/plugins/security_solution/server/lib/entity_analytics/risk_score/risk_score_data_client.test.ts index 2ddd04a766944..cec3330af52e6 100644 --- a/x-pack/plugins/security_solution/server/lib/entity_analytics/risk_score/risk_score_data_client.test.ts +++ b/x-pack/plugins/security_solution/server/lib/entity_analytics/risk_score/risk_score_data_client.test.ts @@ -40,6 +40,7 @@ jest.spyOn(transforms, 'scheduleTransformNow').mockResolvedValue(Promise.resolve describe('RiskScoreDataClient', () => { let riskScoreDataClient: RiskScoreDataClient; + let riskScoreDataClientWithNameSpace: RiskScoreDataClient; let mockSavedObjectClient: ReturnType; let logger: ReturnType; const esClient = elasticsearchServiceMock.createScopedClusterClient().asCurrentUser; @@ -56,6 +57,8 @@ describe('RiskScoreDataClient', () => { namespace: 'default', }; riskScoreDataClient = new RiskScoreDataClient(options); + const optionsWithNamespace = { ...options, namespace: 'space-1' }; + riskScoreDataClientWithNameSpace = new RiskScoreDataClient(optionsWithNamespace); }); afterEach(() => { @@ -80,369 +83,396 @@ describe('RiskScoreDataClient', () => { }); describe('init success', () => { - it('should initialize risk engine resources', async () => { - await riskScoreDataClient.init(); + it('should initialize risk engine resources in the appropriate space', async () => { + const assertComponentTemplate = (namespace: string) => { + expect(createOrUpdateComponentTemplate).toHaveBeenCalledWith( + expect.objectContaining({ + logger, + esClient, + template: expect.objectContaining({ + name: `.risk-score-mappings-${namespace}`, + _meta: { + managed: true, + }, + }), + totalFieldsLimit: 1000, + }) + ); + }; - expect(createOrUpdateComponentTemplate).toHaveBeenCalledWith( - expect.objectContaining({ + const assertIndexTemplate = (namespace: string) => { + expect(createOrUpdateIndexTemplate).toHaveBeenCalledWith({ logger, esClient, - template: expect.objectContaining({ - name: '.risk-score-mappings', - _meta: { - managed: true, - }, - }), - totalFieldsLimit: 1000, - }) - ); - expect((createOrUpdateComponentTemplate as jest.Mock).mock.lastCall[0].template.template) - .toMatchInlineSnapshot(` - Object { - "mappings": Object { - "dynamic": "strict", - "properties": Object { - "@timestamp": Object { - "ignore_malformed": false, - "type": "date", + template: { + name: `.risk-score.risk-score-${namespace}-index-template`, + body: { + data_stream: { hidden: true }, + index_patterns: [`risk-score.risk-score-${namespace}`], + composed_of: [`.risk-score-mappings-${namespace}`], + template: { + lifecycle: {}, + settings: { + 'index.mapping.total_fields.limit': totalFieldsLimit, + }, + mappings: { + dynamic: false, + _meta: { + kibana: { + version: '8.9.0', + }, + managed: true, + namespace, }, - "host": Object { - "properties": Object { - "name": Object { - "type": "keyword", - }, - "risk": Object { - "properties": Object { - "calculated_level": Object { - "type": "keyword", - }, - "calculated_score": Object { - "type": "float", - }, - "calculated_score_norm": Object { - "type": "float", - }, - "category_1_count": Object { - "type": "long", - }, - "category_1_score": Object { - "type": "float", - }, - "id_field": Object { - "type": "keyword", - }, - "id_value": Object { - "type": "keyword", - }, - "inputs": Object { - "properties": Object { - "category": Object { - "type": "keyword", - }, - "description": Object { - "type": "keyword", - }, - "id": Object { - "type": "keyword", - }, - "index": Object { - "type": "keyword", - }, - "risk_score": Object { - "type": "float", - }, - "timestamp": Object { - "type": "date", - }, + }, + }, + _meta: { + kibana: { + version: '8.9.0', + }, + managed: true, + namespace, + }, + }, + }, + }); + }; + + const assertDataStream = (namespace: string) => { + expect(createDataStream).toHaveBeenCalledWith({ + logger, + esClient, + totalFieldsLimit, + indexPatterns: { + template: `.risk-score.risk-score-${namespace}-index-template`, + alias: `risk-score.risk-score-${namespace}`, + }, + }); + }; + + const assertIndex = (namespace: string) => { + expect(createOrUpdateIndex).toHaveBeenCalledWith({ + logger, + esClient, + options: { + index: `risk-score.risk-score-latest-${namespace}`, + mappings: { + dynamic: false, + properties: { + '@timestamp': { + ignore_malformed: false, + type: 'date', + }, + host: { + properties: { + name: { + type: 'keyword', + }, + risk: { + properties: { + calculated_level: { + type: 'keyword', + }, + calculated_score: { + type: 'float', + }, + calculated_score_norm: { + type: 'float', + }, + category_1_count: { + type: 'long', + }, + category_1_score: { + type: 'float', + }, + id_field: { + type: 'keyword', + }, + id_value: { + type: 'keyword', + }, + inputs: { + properties: { + category: { + type: 'keyword', + }, + description: { + type: 'keyword', + }, + id: { + type: 'keyword', + }, + index: { + type: 'keyword', + }, + risk_score: { + type: 'float', + }, + timestamp: { + type: 'date', }, - "type": "object", - }, - "notes": Object { - "type": "keyword", }, + type: 'object', + }, + notes: { + type: 'keyword', }, - "type": "object", }, + type: 'object', }, }, - "user": Object { - "properties": Object { - "name": Object { - "type": "keyword", - }, - "risk": Object { - "properties": Object { - "calculated_level": Object { - "type": "keyword", - }, - "calculated_score": Object { - "type": "float", - }, - "calculated_score_norm": Object { - "type": "float", - }, - "category_1_count": Object { - "type": "long", - }, - "category_1_score": Object { - "type": "float", - }, - "id_field": Object { - "type": "keyword", - }, - "id_value": Object { - "type": "keyword", - }, - "inputs": Object { - "properties": Object { - "category": Object { - "type": "keyword", - }, - "description": Object { - "type": "keyword", - }, - "id": Object { - "type": "keyword", - }, - "index": Object { - "type": "keyword", - }, - "risk_score": Object { - "type": "float", - }, - "timestamp": Object { - "type": "date", - }, + }, + user: { + properties: { + name: { + type: 'keyword', + }, + risk: { + properties: { + calculated_level: { + type: 'keyword', + }, + calculated_score: { + type: 'float', + }, + calculated_score_norm: { + type: 'float', + }, + category_1_count: { + type: 'long', + }, + category_1_score: { + type: 'float', + }, + id_field: { + type: 'keyword', + }, + id_value: { + type: 'keyword', + }, + inputs: { + properties: { + category: { + type: 'keyword', + }, + description: { + type: 'keyword', + }, + id: { + type: 'keyword', + }, + index: { + type: 'keyword', + }, + risk_score: { + type: 'float', + }, + timestamp: { + type: 'date', }, - "type": "object", - }, - "notes": Object { - "type": "keyword", }, + type: 'object', + }, + notes: { + type: 'keyword', }, - "type": "object", }, + type: 'object', }, }, }, }, - "settings": Object {}, - } - `); + }, + }, + }); + }; - expect(createOrUpdateIndexTemplate).toHaveBeenCalledWith({ - logger, - esClient, - template: { - name: '.risk-score.risk-score-default-index-template', - body: { - data_stream: { hidden: true }, - index_patterns: ['risk-score.risk-score-default'], - composed_of: ['.risk-score-mappings'], - template: { - lifecycle: {}, - settings: { - 'index.mapping.total_fields.limit': totalFieldsLimit, - }, - mappings: { - dynamic: false, - _meta: { - kibana: { - version: '8.9.0', - }, - managed: true, - namespace: 'default', - }, + const assertTransform = (namespace: string) => { + expect(transforms.createTransform).toHaveBeenCalledWith({ + logger, + esClient, + transform: { + dest: { + index: `risk-score.risk-score-latest-${namespace}`, + }, + frequency: '1h', + latest: { + sort: '@timestamp', + unique_key: ['host.name', 'user.name'], + }, + source: { + index: [`risk-score.risk-score-${namespace}`], + }, + sync: { + time: { + delay: '0s', + field: '@timestamp', }, }, + transform_id: `risk_score_latest_transform_${namespace}`, + settings: { + unattended: true, + }, _meta: { - kibana: { - version: '8.9.0', - }, + version: 2, managed: true, - namespace: 'default', + managed_by: 'security-entity-analytics', }, }, - }, - }); + }); + }; - expect(createDataStream).toHaveBeenCalledWith({ - logger, - esClient, - totalFieldsLimit, - indexPatterns: { - template: `.risk-score.risk-score-default-index-template`, - alias: `risk-score.risk-score-default`, - }, - }); + // Default namespace + esClient.cluster.existsComponentTemplate.mockResolvedValue(false); + await riskScoreDataClient.init(); + assertComponentTemplate('default'); + assertIndexTemplate('default'); + assertDataStream('default'); + assertIndex('default'); + assertTransform('default'); - expect(createOrUpdateIndex).toHaveBeenCalledWith({ - logger, - esClient, - options: { - index: `risk-score.risk-score-latest-default`, - mappings: { - dynamic: false, - properties: { - '@timestamp': { - ignore_malformed: false, - type: 'date', - }, - host: { - properties: { - name: { - type: 'keyword', - }, - risk: { - properties: { - calculated_level: { - type: 'keyword', - }, - calculated_score: { - type: 'float', - }, - calculated_score_norm: { - type: 'float', - }, - category_1_count: { - type: 'long', - }, - category_1_score: { - type: 'float', - }, - id_field: { - type: 'keyword', - }, - id_value: { - type: 'keyword', - }, - inputs: { - properties: { - category: { - type: 'keyword', - }, - description: { - type: 'keyword', - }, - id: { - type: 'keyword', - }, - index: { - type: 'keyword', - }, - risk_score: { - type: 'float', - }, - timestamp: { - type: 'date', + // Space-1 namespace + esClient.cluster.existsComponentTemplate.mockResolvedValue(false); + await riskScoreDataClientWithNameSpace.init(); + assertComponentTemplate('space-1'); + assertIndexTemplate('space-1'); + assertDataStream('space-1'); + assertIndex('space-1'); + assertTransform('space-1'); + + expect((createOrUpdateComponentTemplate as jest.Mock).mock.lastCall[0].template.template) + .toMatchInlineSnapshot(` + Object { + "mappings": Object { + "dynamic": "strict", + "properties": Object { + "@timestamp": Object { + "ignore_malformed": false, + "type": "date", + }, + "host": Object { + "properties": Object { + "name": Object { + "type": "keyword", + }, + "risk": Object { + "properties": Object { + "calculated_level": Object { + "type": "keyword", + }, + "calculated_score": Object { + "type": "float", + }, + "calculated_score_norm": Object { + "type": "float", + }, + "category_1_count": Object { + "type": "long", + }, + "category_1_score": Object { + "type": "float", + }, + "id_field": Object { + "type": "keyword", + }, + "id_value": Object { + "type": "keyword", + }, + "inputs": Object { + "properties": Object { + "category": Object { + "type": "keyword", + }, + "description": Object { + "type": "keyword", + }, + "id": Object { + "type": "keyword", + }, + "index": Object { + "type": "keyword", + }, + "risk_score": Object { + "type": "float", + }, + "timestamp": Object { + "type": "date", + }, }, + "type": "object", + }, + "notes": Object { + "type": "keyword", }, - type: 'object', - }, - notes: { - type: 'keyword', }, + "type": "object", }, - type: 'object', }, }, - }, - user: { - properties: { - name: { - type: 'keyword', - }, - risk: { - properties: { - calculated_level: { - type: 'keyword', - }, - calculated_score: { - type: 'float', - }, - calculated_score_norm: { - type: 'float', - }, - category_1_count: { - type: 'long', - }, - category_1_score: { - type: 'float', - }, - id_field: { - type: 'keyword', - }, - id_value: { - type: 'keyword', - }, - inputs: { - properties: { - category: { - type: 'keyword', - }, - description: { - type: 'keyword', - }, - id: { - type: 'keyword', - }, - index: { - type: 'keyword', - }, - risk_score: { - type: 'float', - }, - timestamp: { - type: 'date', + "user": Object { + "properties": Object { + "name": Object { + "type": "keyword", + }, + "risk": Object { + "properties": Object { + "calculated_level": Object { + "type": "keyword", + }, + "calculated_score": Object { + "type": "float", + }, + "calculated_score_norm": Object { + "type": "float", + }, + "category_1_count": Object { + "type": "long", + }, + "category_1_score": Object { + "type": "float", + }, + "id_field": Object { + "type": "keyword", + }, + "id_value": Object { + "type": "keyword", + }, + "inputs": Object { + "properties": Object { + "category": Object { + "type": "keyword", + }, + "description": Object { + "type": "keyword", + }, + "id": Object { + "type": "keyword", + }, + "index": Object { + "type": "keyword", + }, + "risk_score": Object { + "type": "float", + }, + "timestamp": Object { + "type": "date", + }, }, + "type": "object", + }, + "notes": Object { + "type": "keyword", }, - type: 'object', - }, - notes: { - type: 'keyword', }, + "type": "object", }, - type: 'object', }, }, }, }, - }, - }, - }); - - expect(transforms.createTransform).toHaveBeenCalledWith({ - logger, - esClient, - transform: { - dest: { - index: 'risk-score.risk-score-latest-default', - }, - frequency: '1h', - latest: { - sort: '@timestamp', - unique_key: ['host.name', 'user.name'], - }, - source: { - index: ['risk-score.risk-score-default'], - }, - sync: { - time: { - delay: '0s', - field: '@timestamp', - }, - }, - transform_id: 'risk_score_latest_transform_default', - settings: { - unattended: true, - }, - _meta: { - version: 2, - managed: true, - managed_by: 'security-entity-analytics', - }, - }, - }); + "settings": Object {}, + } + `); }); }); @@ -479,7 +509,7 @@ describe('RiskScoreDataClient', () => { expect(esClient.transform.deleteTransform).toHaveBeenCalledTimes(1); expect(esClient.indices.deleteDataStream).toHaveBeenCalledTimes(1); expect(esClient.indices.deleteIndexTemplate).toHaveBeenCalledTimes(1); - expect(esClient.cluster.deleteComponentTemplate).toHaveBeenCalledTimes(1); + expect(esClient.cluster.deleteComponentTemplate).toHaveBeenCalledTimes(2); expect(errors).toEqual([]); }); diff --git a/x-pack/plugins/security_solution/server/lib/entity_analytics/risk_score/risk_score_data_client.ts b/x-pack/plugins/security_solution/server/lib/entity_analytics/risk_score/risk_score_data_client.ts index 0b3c38f3602fd..2ae05e4c86227 100644 --- a/x-pack/plugins/security_solution/server/lib/entity_analytics/risk_score/risk_score_data_client.ts +++ b/x-pack/plugins/security_solution/server/lib/entity_analytics/risk_score/risk_score_data_client.ts @@ -22,6 +22,7 @@ import { getIndexPatternDataStream, getTransformOptions, mappingComponentName, + nameSpaceAwareMappingsComponentName, riskScoreFieldMap, totalFieldsLimit, } from './configurations'; @@ -114,12 +115,42 @@ export class RiskScoreDataClient { namespace, }; + // Check if there are any existing component templates with the namespace in the name + + const oldComponentTemplateExists = await esClient.cluster.existsComponentTemplate({ + name: mappingComponentName, + }); + // If present then copy the contents to a new component template with the namespace in the name + if (oldComponentTemplateExists) { + const oldComponentTemplateResponse = await esClient.cluster.getComponentTemplate( + { + name: mappingComponentName, + }, + { ignore: [404] } + ); + const oldComponentTemplate = oldComponentTemplateResponse?.component_templates[0]; + const newComponentTemplateName = nameSpaceAwareMappingsComponentName(namespace); + await esClient.cluster.putComponentTemplate({ + name: newComponentTemplateName, + body: oldComponentTemplate.component_template, + }); + } + + // Delete the component template without the namespace in the name + await esClient.cluster.deleteComponentTemplate( + { + name: mappingComponentName, + }, + { ignore: [404] } + ); + + // Update the new component template with the required data await Promise.all([ createOrUpdateComponentTemplate({ logger: this.options.logger, esClient, template: { - name: mappingComponentName, + name: nameSpaceAwareMappingsComponentName(namespace), _meta: { managed: true, }, @@ -132,6 +163,7 @@ export class RiskScoreDataClient { }), ]); + // Reference the new component template in the index template await createOrUpdateIndexTemplate({ logger: this.options.logger, esClient, @@ -140,7 +172,7 @@ export class RiskScoreDataClient { body: { data_stream: { hidden: true }, index_patterns: [indexPatterns.alias], - composed_of: [mappingComponentName], + composed_of: [nameSpaceAwareMappingsComponentName(namespace)], template: { lifecycle: {}, settings: { @@ -235,6 +267,15 @@ export class RiskScoreDataClient { ) .catch(addError); + await esClient.cluster + .deleteComponentTemplate( + { + name: nameSpaceAwareMappingsComponentName(namespace), + }, + { ignore: [404] } + ) + .catch(addError); + await esClient.cluster .deleteComponentTemplate( { diff --git a/x-pack/test/security_solution_api_integration/test_suites/entity_analytics/risk_engine/trial_license_complete_tier/init_and_status_apis.ts b/x-pack/test/security_solution_api_integration/test_suites/entity_analytics/risk_engine/trial_license_complete_tier/init_and_status_apis.ts index dd1fe34cd050a..b793ec1cb1306 100644 --- a/x-pack/test/security_solution_api_integration/test_suites/entity_analytics/risk_engine/trial_license_complete_tier/init_and_status_apis.ts +++ b/x-pack/test/security_solution_api_integration/test_suites/entity_analytics/risk_engine/trial_license_complete_tier/init_and_status_apis.ts @@ -27,19 +27,30 @@ export default ({ getService }: FtrProviderContext) => { const es = getService('es'); const supertest = getService('supertest'); const kibanaServer = getService('kibanaServer'); + const spaces = getService('spaces'); + const customSpaceName = 'ea-customspace-it'; const riskEngineRoutes = riskEngineRouteHelpersFactory(supertest); + const riskEngineRoutesWithNamespace = riskEngineRouteHelpersFactory(supertest, customSpaceName); const log = getService('log'); - // Failing: See https://github.com/elastic/kibana/issues/196319 - describe.skip('@ess @serverless @serverlessQA init_and_status_apis', () => { + describe('@ess @serverless @serverlessQA init_and_status_apis', () => { before(async () => { + await spaces.create({ + id: customSpaceName, + name: customSpaceName, + description: 'Space for ${customSpaceName}', + disabledFeatures: [], + }); await riskEngineRoutes.cleanUp(); + await riskEngineRoutesWithNamespace.cleanUp(); }); afterEach(async () => { await riskEngineRoutes.cleanUp(); + await riskEngineRoutesWithNamespace.cleanUp(); await clearLegacyTransforms({ es, log }); await clearLegacyDashboards({ supertest, log }); + await spaces.delete(customSpaceName); }); describe('init api', () => { @@ -54,10 +65,21 @@ export default ({ getService }: FtrProviderContext) => { risk_engine_resources_installed: true, }, }); + + const customNamespaceResponse = await riskEngineRoutesWithNamespace.init(); + expect(customNamespaceResponse.body).to.eql({ + result: { + errors: [], + legacy_risk_engine_disabled: true, + risk_engine_configuration_created: true, + risk_engine_enabled: true, + risk_engine_resources_installed: true, + }, + }); }); - it('should install resources on init call', async () => { - const componentTemplateName = '.risk-score-mappings'; + it('should install resources on init call in the default namespace', async () => { + const componentTemplateName = '.risk-score-mappings-default'; const indexTemplateName = '.risk-score.risk-score-default-index-template'; const dataStreamName = 'risk-score.risk-score-default'; const latestIndexName = 'risk-score.risk-score-latest-default'; @@ -210,7 +232,7 @@ export default ({ getService }: FtrProviderContext) => { expect(indexTemplate.index_template.index_patterns).to.eql([ 'risk-score.risk-score-default', ]); - expect(indexTemplate.index_template.composed_of).to.eql(['.risk-score-mappings']); + expect(indexTemplate.index_template.composed_of).to.eql(['.risk-score-mappings-default']); expect(indexTemplate.index_template.template!.mappings?.dynamic).to.eql(false); expect(indexTemplate.index_template.template!.mappings?._meta?.managed).to.eql(true); expect(indexTemplate.index_template.template!.mappings?._meta?.namespace).to.eql('default'); @@ -267,6 +289,221 @@ export default ({ getService }: FtrProviderContext) => { expect(transformStats.transforms[0].state).to.eql('stopped'); }); + it('should install resources on init call in the custom namespace', async () => { + const componentTemplateName = `.risk-score-mappings-${customSpaceName}`; + const indexTemplateName = `.risk-score.risk-score-${customSpaceName}-index-template`; + const dataStreamName = `risk-score.risk-score-${customSpaceName}`; + const latestIndexName = `risk-score.risk-score-latest-${customSpaceName}`; + const transformId = `risk_score_latest_transform_${customSpaceName}`; + + await riskEngineRoutesWithNamespace.init(); + + const { component_templates: componentTemplates1 } = await es.cluster.getComponentTemplate({ + name: componentTemplateName, + }); + + expect(componentTemplates1.length).to.eql(1); + const componentTemplate = componentTemplates1[0]; + + expect(componentTemplate.name).to.eql(componentTemplateName); + expect(componentTemplate.component_template.template.mappings).to.eql({ + dynamic: 'strict', + properties: { + '@timestamp': { + ignore_malformed: false, + type: 'date', + }, + host: { + properties: { + name: { + type: 'keyword', + }, + risk: { + properties: { + calculated_level: { + type: 'keyword', + }, + calculated_score: { + type: 'float', + }, + calculated_score_norm: { + type: 'float', + }, + category_1_count: { + type: 'long', + }, + category_1_score: { + type: 'float', + }, + id_field: { + type: 'keyword', + }, + id_value: { + type: 'keyword', + }, + notes: { + type: 'keyword', + }, + inputs: { + properties: { + id: { + type: 'keyword', + }, + index: { + type: 'keyword', + }, + category: { + type: 'keyword', + }, + description: { + type: 'keyword', + }, + risk_score: { + type: 'float', + }, + timestamp: { + type: 'date', + }, + }, + type: 'object', + }, + }, + type: 'object', + }, + }, + }, + user: { + properties: { + name: { + type: 'keyword', + }, + risk: { + properties: { + calculated_level: { + type: 'keyword', + }, + calculated_score: { + type: 'float', + }, + calculated_score_norm: { + type: 'float', + }, + category_1_count: { + type: 'long', + }, + category_1_score: { + type: 'float', + }, + id_field: { + type: 'keyword', + }, + id_value: { + type: 'keyword', + }, + notes: { + type: 'keyword', + }, + inputs: { + properties: { + id: { + type: 'keyword', + }, + index: { + type: 'keyword', + }, + category: { + type: 'keyword', + }, + description: { + type: 'keyword', + }, + risk_score: { + type: 'float', + }, + timestamp: { + type: 'date', + }, + }, + type: 'object', + }, + }, + type: 'object', + }, + }, + }, + }, + }); + + const { index_templates: indexTemplates } = await es.indices.getIndexTemplate({ + name: indexTemplateName, + }); + expect(indexTemplates.length).to.eql(1); + const indexTemplate = indexTemplates[0]; + expect(indexTemplate.name).to.eql(indexTemplateName); + expect(indexTemplate.index_template.index_patterns).to.eql([ + `risk-score.risk-score-${customSpaceName}`, + ]); + expect(indexTemplate.index_template.composed_of).to.eql([ + `.risk-score-mappings-${customSpaceName}`, + ]); + expect(indexTemplate.index_template.template!.mappings?.dynamic).to.eql(false); + expect(indexTemplate.index_template.template!.mappings?._meta?.managed).to.eql(true); + expect(indexTemplate.index_template.template!.mappings?._meta?.namespace).to.eql( + customSpaceName + ); + expect(indexTemplate.index_template.template!.mappings?._meta?.kibana?.version).to.be.a( + 'string' + ); + + expect(indexTemplate.index_template.template!.settings).to.eql({ + index: { + mapping: { + total_fields: { + limit: '1000', + }, + }, + }, + }); + + expect(indexTemplate.index_template.template!.lifecycle).to.eql({ + enabled: true, + }); + + const dsResponse = await es.indices.get({ + index: dataStreamName, + }); + + const dataStream = Object.values(dsResponse).find( + (ds) => ds.data_stream === dataStreamName + ); + + expect(dataStream?.mappings?._meta?.managed).to.eql(true); + expect(dataStream?.mappings?._meta?.namespace).to.eql(customSpaceName); + expect(dataStream?.mappings?._meta?.kibana?.version).to.be.a('string'); + expect(dataStream?.mappings?.dynamic).to.eql('false'); + + expect(dataStream?.settings?.index?.mapping).to.eql({ + total_fields: { + limit: '1000', + }, + }); + + expect(dataStream?.settings?.index?.hidden).to.eql('true'); + expect(dataStream?.settings?.index?.number_of_shards).to.eql(1); + + const indexExist = await es.indices.exists({ + index: latestIndexName, + }); + + expect(indexExist).to.eql(true); + + const transformStats = await es.transform.getTransformStats({ + transform_id: transformId, + }); + + expect(transformStats.transforms[0].state).to.eql('stopped'); + }); + it('should create configuration saved object', async () => { await riskEngineRoutes.init(); const response = await kibanaServer.savedObjects.find({ From 2b270897a3f22ed5ca04ef173895cfa8660f9ea2 Mon Sep 17 00:00:00 2001 From: Angela Chuang <6295984+angorayc@users.noreply.github.com> Date: Tue, 22 Oct 2024 16:52:43 +0100 Subject: [PATCH 06/22] [SecuritySolution][Onboarding] Send Telemetry when integration tabs or cards clicked (#196291) ## Summary https://github.com/elastic/kibana/issues/196145 To verify: 1. Add these lines to `kibana.dev.yml` ``` logging.browser.root.level: debug telemetry.optIn: true ``` 2. In the onboarding hub, expand the integration card. It should log `onboarding_tab_${tabId}` on tabs clicked. https://github.com/user-attachments/assets/bd30c9ed-7c99-4ca0-93e7-6d9bf0146e62 It should log `onboarding_card_${integrationId}` on integration cards clicked. https://github.com/user-attachments/assets/58750d88-7bbf-4b27-8e54-587f3f6f32c2 3. Manage integrations callout link clicked:: `onboarding_manage_integrations`; 4. Endpoint callout link clicked: `onboarding_endpoint_learn_more`; 5. Agentless callout link clicked: `onboarding_agentless_learn_more`; 6. Agent still required callout link clicked: `onboarding_agent_required`; ### Checklist Delete any items that are not applicable to this PR. - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --- .../public/common/lib/telemetry/constants.ts | 2 + .../common/lib/__mocks__/telemetry.ts | 8 ++++ .../public/onboarding/common/lib/telemetry.ts | 12 ++++++ .../callouts/agent_required_callout.test.tsx | 10 +++++ .../callouts/agent_required_callout.tsx | 14 ++++-- .../agentless_available_callout.test.tsx | 16 +++++-- .../callouts/agentless_available_callout.tsx | 9 +++- .../callouts/endpoint_callout.test.tsx | 43 +++++++++++++++++++ .../callouts/endpoint_callout.tsx | 8 +++- .../callouts/manage_integrations_callout.tsx | 15 ++++++- .../cards/integrations/constants.ts | 6 +++ .../integration_card_grid_tabs.test.tsx | 29 +++++++++++++ .../integration_card_grid_tabs.tsx | 4 ++ .../use_integration_card_list.test.ts | 17 +++++++- .../integrations/use_integration_card_list.ts | 4 ++ 15 files changed, 185 insertions(+), 12 deletions(-) create mode 100644 x-pack/plugins/security_solution/public/onboarding/common/lib/__mocks__/telemetry.ts create mode 100644 x-pack/plugins/security_solution/public/onboarding/common/lib/telemetry.ts create mode 100644 x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/callouts/endpoint_callout.test.tsx diff --git a/x-pack/plugins/security_solution/public/common/lib/telemetry/constants.ts b/x-pack/plugins/security_solution/public/common/lib/telemetry/constants.ts index 5126d75178f5f..cb247891d79b3 100644 --- a/x-pack/plugins/security_solution/public/common/lib/telemetry/constants.ts +++ b/x-pack/plugins/security_solution/public/common/lib/telemetry/constants.ts @@ -35,6 +35,8 @@ export enum TELEMETRY_EVENT { DASHBOARD = 'navigate_to_dashboard', CREATE_DASHBOARD = 'create_dashboard', + ONBOARDING = 'onboarding', + // value list OPEN_VALUE_LIST_MODAL = 'open_value_list_modal', CREATE_VALUE_LIST_ITEM = 'create_value_list_item', diff --git a/x-pack/plugins/security_solution/public/onboarding/common/lib/__mocks__/telemetry.ts b/x-pack/plugins/security_solution/public/onboarding/common/lib/__mocks__/telemetry.ts new file mode 100644 index 0000000000000..5d1c3feb56ed9 --- /dev/null +++ b/x-pack/plugins/security_solution/public/onboarding/common/lib/__mocks__/telemetry.ts @@ -0,0 +1,8 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export const trackOnboardingLinkClick = jest.fn(); diff --git a/x-pack/plugins/security_solution/public/onboarding/common/lib/telemetry.ts b/x-pack/plugins/security_solution/public/onboarding/common/lib/telemetry.ts new file mode 100644 index 0000000000000..a88ae651ae600 --- /dev/null +++ b/x-pack/plugins/security_solution/public/onboarding/common/lib/telemetry.ts @@ -0,0 +1,12 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { METRIC_TYPE, TELEMETRY_EVENT, track } from '../../../common/lib/telemetry'; + +export const trackOnboardingLinkClick = (linkId: string) => { + track(METRIC_TYPE.CLICK, `${TELEMETRY_EVENT.ONBOARDING}_${linkId}`); +}; diff --git a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/callouts/agent_required_callout.test.tsx b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/callouts/agent_required_callout.test.tsx index dbd0c105d27a1..53e8b6c34e8f2 100644 --- a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/callouts/agent_required_callout.test.tsx +++ b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/callouts/agent_required_callout.test.tsx @@ -14,8 +14,10 @@ import React from 'react'; import { render } from '@testing-library/react'; import { AgentRequiredCallout } from './agent_required_callout'; import { TestProviders } from '../../../../../../common/mock/test_providers'; +import { trackOnboardingLinkClick } from '../../../../../common/lib/telemetry'; jest.mock('../../../../../../common/lib/kibana'); +jest.mock('../../../../../common/lib/telemetry'); describe('AgentRequiredCallout', () => { beforeEach(() => { @@ -30,4 +32,12 @@ describe('AgentRequiredCallout', () => { ).toBeInTheDocument(); expect(getByTestId('agentLink')).toBeInTheDocument(); }); + + it('should track the agent link click', () => { + const { getByTestId } = render(, { wrapper: TestProviders }); + + getByTestId('agentLink').click(); + + expect(trackOnboardingLinkClick).toHaveBeenCalledWith('agent_required'); + }); }); diff --git a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/callouts/agent_required_callout.tsx b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/callouts/agent_required_callout.tsx index aad22c959bc65..b1d18b138487b 100644 --- a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/callouts/agent_required_callout.tsx +++ b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/callouts/agent_required_callout.tsx @@ -11,16 +11,22 @@ import { EuiIcon } from '@elastic/eui'; import { LinkAnchor } from '../../../../../../common/components/links'; import { CardCallOut } from '../../common/card_callout'; import { useNavigation } from '../../../../../../common/lib/kibana'; -import { FLEET_APP_ID, ADD_AGENT_PATH } from '../constants'; +import { FLEET_APP_ID, ADD_AGENT_PATH, TELEMETRY_AGENT_REQUIRED } from '../constants'; +import { trackOnboardingLinkClick } from '../../../../../common/lib/telemetry'; const fleetAgentLinkProps = { appId: FLEET_APP_ID, path: ADD_AGENT_PATH }; export const AgentRequiredCallout = React.memo(() => { const { getAppUrl, navigateTo } = useNavigation(); const addAgentLink = getAppUrl(fleetAgentLinkProps); - const onAddAgentClick = useCallback(() => { - navigateTo(fleetAgentLinkProps); - }, [navigateTo]); + const onAddAgentClick = useCallback( + (e: React.MouseEvent) => { + e.preventDefault(); + trackOnboardingLinkClick(TELEMETRY_AGENT_REQUIRED); + navigateTo(fleetAgentLinkProps); + }, + [navigateTo] + ); return ( ({ - useKibana: jest.fn(), -})); +jest.mock('../../../../../../common/lib/kibana'); +jest.mock('../../../../../common/lib/telemetry'); describe('AgentlessAvailableCallout', () => { const mockUseKibana = useKibana as jest.Mock; @@ -62,4 +62,14 @@ describe('AgentlessAvailableCallout', () => { ).toBeInTheDocument(); expect(getByTestId('agentlessLearnMoreLink')).toBeInTheDocument(); }); + + it('should track the agentless learn more link click', () => { + const { getByTestId } = render(, { + wrapper: TestProviders, + }); + + getByTestId('agentlessLearnMoreLink').click(); + + expect(trackOnboardingLinkClick).toHaveBeenCalledWith('agentless_learn_more'); + }); }); diff --git a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/callouts/agentless_available_callout.tsx b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/callouts/agentless_available_callout.tsx index f802f83efb7e5..eaf8cbaa3b287 100644 --- a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/callouts/agentless_available_callout.tsx +++ b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/callouts/agentless_available_callout.tsx @@ -5,19 +5,25 @@ * 2.0. */ -import React from 'react'; +import React, { useCallback } from 'react'; import { FormattedMessage } from '@kbn/i18n-react'; import { EuiIcon, useEuiTheme } from '@elastic/eui'; import { css } from '@emotion/react'; import { useKibana } from '../../../../../../common/lib/kibana'; import { LinkAnchor } from '../../../../../../common/components/links'; +import { trackOnboardingLinkClick } from '../../../../../common/lib/telemetry'; import { CardCallOut } from '../../common/card_callout'; +import { TELEMETRY_AGENTLESS_LEARN_MORE } from '../constants'; export const AgentlessAvailableCallout = React.memo(() => { const { euiTheme } = useEuiTheme(); const { docLinks } = useKibana().services; + const onClick = useCallback(() => { + trackOnboardingLinkClick(TELEMETRY_AGENTLESS_LEARN_MORE); + }, []); + /* @ts-expect-error: add the blog link to `packages/kbn-doc-links/src/get_doc_links.ts` when it is ready and remove this exit condition*/ if (!docLinks.links.fleet.agentlessBlog) { return null; @@ -54,6 +60,7 @@ export const AgentlessAvailableCallout = React.memo(() => { { + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('renders the callout', () => { + const { getByTestId, getByText } = render(, { wrapper: TestProviders }); + + expect( + getByText('Orchestrate response across endpoint vendors with bidirectional integrations') + ).toBeInTheDocument(); + expect(getByTestId('endpointLearnMoreLink')).toBeInTheDocument(); + }); + + it('should track the agent link click', () => { + const { getByTestId } = render(, { wrapper: TestProviders }); + + getByTestId('endpointLearnMoreLink').click(); + + expect(trackOnboardingLinkClick).toHaveBeenCalledWith('endpoint_learn_more'); + }); +}); diff --git a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/callouts/endpoint_callout.tsx b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/callouts/endpoint_callout.tsx index 2ff48a1992d1d..d5b0199c9f401 100644 --- a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/callouts/endpoint_callout.tsx +++ b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/callouts/endpoint_callout.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import React from 'react'; +import React, { useCallback } from 'react'; import { FormattedMessage } from '@kbn/i18n-react'; import { EuiIcon, useEuiTheme } from '@elastic/eui'; import { css } from '@emotion/react'; @@ -13,10 +13,15 @@ import { css } from '@emotion/react'; import { useKibana } from '../../../../../../common/lib/kibana/kibana_react'; import { LinkAnchor } from '../../../../../../common/components/links'; import { CardCallOut } from '../../common/card_callout'; +import { trackOnboardingLinkClick } from '../../../../../common/lib/telemetry'; +import { TELEMETRY_ENDPOINT_LEARN_MORE } from '../constants'; export const EndpointCallout = React.memo(() => { const { euiTheme } = useEuiTheme(); const { docLinks } = useKibana().services; + const onClick = useCallback(() => { + trackOnboardingLinkClick(TELEMETRY_ENDPOINT_LEARN_MORE); + }, []); return ( { data-test-subj="endpointLearnMoreLink" external={true} target="_blank" + onClick={onClick} > { const { href: integrationUrl, onClick: onAddIntegrationClicked } = useAddIntegrationsUrl(); + const onClick = useCallback( + (e: React.MouseEvent) => { + e.preventDefault(); + trackOnboardingLinkClick(TELEMETRY_MANAGE_INTEGRATIONS); + onAddIntegrationClicked(e); + }, + [onAddIntegrationClicked] + ); + if (!installedIntegrationsCount) { return null; } @@ -41,7 +52,7 @@ export const ManageIntegrationsCallout = React.memo( ), link: ( diff --git a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/constants.ts b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/constants.ts index e245de6129478..c748f5205e7aa 100644 --- a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/constants.ts +++ b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/constants.ts @@ -24,3 +24,9 @@ export const SCROLL_ELEMENT_ID = 'integrations-scroll-container'; export const SEARCH_FILTER_CATEGORIES: CategoryFacet[] = []; export const WITH_SEARCH_BOX_HEIGHT = '568px'; export const WITHOUT_SEARCH_BOX_HEIGHT = '513px'; +export const TELEMETRY_MANAGE_INTEGRATIONS = `manage_integrations`; +export const TELEMETRY_ENDPOINT_LEARN_MORE = `endpoint_learn_more`; +export const TELEMETRY_AGENTLESS_LEARN_MORE = `agentless_learn_more`; +export const TELEMETRY_AGENT_REQUIRED = `agent_required`; +export const TELEMETRY_INTEGRATION_CARD = `card`; +export const TELEMETRY_INTEGRATION_TAB = `tab`; diff --git a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/integration_card_grid_tabs.test.tsx b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/integration_card_grid_tabs.test.tsx index f55cc8cd50b2d..c88ffb6a598b7 100644 --- a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/integration_card_grid_tabs.test.tsx +++ b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/integration_card_grid_tabs.test.tsx @@ -15,9 +15,11 @@ import { useStoredIntegrationTabId, } from '../../../../hooks/use_stored_state'; import { DEFAULT_TAB } from './constants'; +import { trackOnboardingLinkClick } from '../../../../common/lib/telemetry'; jest.mock('../../../onboarding_context'); jest.mock('../../../../hooks/use_stored_state'); +jest.mock('../../../../common/lib/telemetry'); jest.mock('../../../../../common/lib/kibana', () => ({ ...jest.requireActual('../../../../../common/lib/kibana'), @@ -118,6 +120,33 @@ describe('IntegrationsCardGridTabsComponent', () => { expect(mockSetTabId).toHaveBeenCalledWith('user'); }); + it('tracks the tab clicks', () => { + (useStoredIntegrationTabId as jest.Mock).mockReturnValue(['recommended', mockSetTabId]); + + mockUseAvailablePackages.mockReturnValue({ + isLoading: false, + filteredCards: [], + setCategory: mockSetCategory, + setSelectedSubCategory: mockSetSelectedSubCategory, + setSearchTerm: mockSetSearchTerm, + }); + + const { getByTestId } = render( + + ); + + const tabButton = getByTestId('user'); + + act(() => { + fireEvent.click(tabButton); + }); + + expect(trackOnboardingLinkClick).toHaveBeenCalledWith('tab_user'); + }); + it('renders no search tools when showSearchTools is false', async () => { mockUseAvailablePackages.mockReturnValue({ isLoading: false, diff --git a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/integration_card_grid_tabs.tsx b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/integration_card_grid_tabs.tsx index fc30fb0d6c617..e1ce7f5cdecf1 100644 --- a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/integration_card_grid_tabs.tsx +++ b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/integration_card_grid_tabs.tsx @@ -21,6 +21,7 @@ import { LOADING_SKELETON_TEXT_LINES, SCROLL_ELEMENT_ID, SEARCH_FILTER_CATEGORIES, + TELEMETRY_INTEGRATION_TAB, WITHOUT_SEARCH_BOX_HEIGHT, WITH_SEARCH_BOX_HEIGHT, } from './constants'; @@ -28,6 +29,7 @@ import { INTEGRATION_TABS, INTEGRATION_TABS_BY_ID } from './integration_tabs_con import { useIntegrationCardList } from './use_integration_card_list'; import { IntegrationTabId } from './types'; import { IntegrationCardTopCallout } from './callouts/integration_card_top_callout'; +import { trackOnboardingLinkClick } from '../../../../common/lib/telemetry'; export interface IntegrationsCardGridTabsProps { installedIntegrationsCount: number; @@ -55,8 +57,10 @@ export const IntegrationsCardGridTabsComponent = React.memo { const id = stringId as IntegrationTabId; + const trackId = `${TELEMETRY_INTEGRATION_TAB}_${id}`; scrollElement.current?.scrollTo?.(0, 0); setSelectedTabIdToStorage(id); + trackOnboardingLinkClick(trackId); }, [setSelectedTabIdToStorage] ); diff --git a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/use_integration_card_list.test.ts b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/use_integration_card_list.test.ts index 9c4e1978f27b7..19ab340276b83 100644 --- a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/use_integration_card_list.test.ts +++ b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/use_integration_card_list.test.ts @@ -6,12 +6,14 @@ */ import { renderHook } from '@testing-library/react-hooks'; import { useIntegrationCardList } from './use_integration_card_list'; +import { trackOnboardingLinkClick } from '../../../../common/lib/telemetry'; +jest.mock('../../../../common/lib/telemetry'); jest.mock('../../../../../common/lib/kibana', () => ({ ...jest.requireActual('../../../../../common/lib/kibana'), useNavigation: jest.fn().mockReturnValue({ navigateTo: jest.fn(), - getAppUrl: jest.fn(), + getAppUrl: jest.fn().mockReturnValue(''), }), })); @@ -73,4 +75,17 @@ describe('useIntegrationCardList', () => { expect(result.current).toEqual([mockFilteredCards.featuredCards['epr:endpoint']]); }); + + it('tracks integration card click', () => { + const { result } = renderHook(() => + useIntegrationCardList({ + integrationsList: mockIntegrationsList, + }) + ); + + const card = result.current[0]; + card.onCardClick?.(); + + expect(trackOnboardingLinkClick).toHaveBeenCalledWith('card_epr:endpoint'); + }); }); diff --git a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/use_integration_card_list.ts b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/use_integration_card_list.ts index 2a9675f91e9a8..ccea5299551c1 100644 --- a/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/use_integration_card_list.ts +++ b/x-pack/plugins/security_solution/public/onboarding/components/onboarding_body/cards/integrations/use_integration_card_list.ts @@ -20,8 +20,10 @@ import { MAX_CARD_HEIGHT_IN_PX, ONBOARDING_APP_ID, ONBOARDING_LINK, + TELEMETRY_INTEGRATION_CARD, } from './constants'; import type { GetAppUrl, NavigateTo } from '../../../../../common/lib/kibana'; +import { trackOnboardingLinkClick } from '../../../../common/lib/telemetry'; const addPathParamToUrl = (url: string, onboardingLink: string) => { const encoded = encodeURIComponent(onboardingLink); @@ -97,6 +99,8 @@ const addSecuritySpecificProps = ({ showInstallationStatus: true, url, onCardClick: () => { + const trackId = `${TELEMETRY_INTEGRATION_CARD}_${card.id}`; + trackOnboardingLinkClick(trackId); if (url.startsWith(APP_INTEGRATIONS_PATH)) { navigateTo({ appId: INTEGRATION_APP_ID, From e1c4a604e1b66dfd7348cd28a451313c44c996df Mon Sep 17 00:00:00 2001 From: Thom Heymann <190132+thomheymann@users.noreply.github.com> Date: Tue, 22 Oct 2024 17:03:41 +0100 Subject: [PATCH 07/22] [Observability Onboarding] Fix EDOT collector permissions (#197248) ## Summary Fix EDOT collector permissions. ## Details Adds `traces-*-*` index privilege and removed unnecessary `apm` application privileges: ```json { "standalone_agent": { "cluster": [ "monitor" ], "indices": [ { "names": [ "logs-*-*", "metrics-*-*", "traces-*-*" ], "privileges": [ "auto_configure", "create_doc" ], "allow_restricted_indices": false } ], "applications": [] } } ``` --- .../server/lib/api_key/create_shipper_api_key.ts | 9 ++++++--- .../server/lib/api_key/has_log_monitoring_privileges.ts | 9 ++++++--- .../server/lib/api_key/privileges.ts | 9 ++++----- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/x-pack/plugins/observability_solution/observability_onboarding/server/lib/api_key/create_shipper_api_key.ts b/x-pack/plugins/observability_solution/observability_onboarding/server/lib/api_key/create_shipper_api_key.ts index bdfdd202a962e..9279ae0e1dfd1 100644 --- a/x-pack/plugins/observability_solution/observability_onboarding/server/lib/api_key/create_shipper_api_key.ts +++ b/x-pack/plugins/observability_solution/observability_onboarding/server/lib/api_key/create_shipper_api_key.ts @@ -6,7 +6,11 @@ */ import { ElasticsearchClient } from '@kbn/core/server'; -import { MONITOR_CLUSTER, INDEX_LOGS_AND_METRICS, WRITE_APM_EVENTS } from './privileges'; +import { + MONITOR_CLUSTER, + INDEX_LOGS_AND_METRICS, + INDEX_LOGS_METRICS_AND_TRACES, +} from './privileges'; export function createShipperApiKey(esClient: ElasticsearchClient, name: string, withAPM = false) { // Based on https://www.elastic.co/guide/en/fleet/master/grant-access-to-elasticsearch.html#create-api-key-standalone-agent @@ -20,8 +24,7 @@ export function createShipperApiKey(esClient: ElasticsearchClient, name: string, role_descriptors: { standalone_agent: { cluster: [MONITOR_CLUSTER], - indices: [INDEX_LOGS_AND_METRICS], - applications: withAPM ? [WRITE_APM_EVENTS] : undefined, + indices: [withAPM ? INDEX_LOGS_METRICS_AND_TRACES : INDEX_LOGS_AND_METRICS], }, }, }, diff --git a/x-pack/plugins/observability_solution/observability_onboarding/server/lib/api_key/has_log_monitoring_privileges.ts b/x-pack/plugins/observability_solution/observability_onboarding/server/lib/api_key/has_log_monitoring_privileges.ts index 0593a7f761e1e..ce5897936b741 100644 --- a/x-pack/plugins/observability_solution/observability_onboarding/server/lib/api_key/has_log_monitoring_privileges.ts +++ b/x-pack/plugins/observability_solution/observability_onboarding/server/lib/api_key/has_log_monitoring_privileges.ts @@ -6,14 +6,17 @@ */ import { ElasticsearchClient } from '@kbn/core/server'; -import { MONITOR_CLUSTER, INDEX_LOGS_AND_METRICS, WRITE_APM_EVENTS } from './privileges'; +import { + MONITOR_CLUSTER, + INDEX_LOGS_AND_METRICS, + INDEX_LOGS_METRICS_AND_TRACES, +} from './privileges'; export async function hasLogMonitoringPrivileges(esClient: ElasticsearchClient, withAPM = false) { const res = await esClient.security.hasPrivileges({ body: { cluster: [MONITOR_CLUSTER, 'manage_own_api_key'], - index: [INDEX_LOGS_AND_METRICS], - application: withAPM ? [WRITE_APM_EVENTS] : undefined, + index: [withAPM ? INDEX_LOGS_METRICS_AND_TRACES : INDEX_LOGS_AND_METRICS], }, }); diff --git a/x-pack/plugins/observability_solution/observability_onboarding/server/lib/api_key/privileges.ts b/x-pack/plugins/observability_solution/observability_onboarding/server/lib/api_key/privileges.ts index 7c3b5999842bd..8a28849ef1003 100644 --- a/x-pack/plugins/observability_solution/observability_onboarding/server/lib/api_key/privileges.ts +++ b/x-pack/plugins/observability_solution/observability_onboarding/server/lib/api_key/privileges.ts @@ -18,9 +18,8 @@ export const INDEX_LOGS_AND_METRICS: estypes.SecurityIndicesPrivileges = { privileges: ['auto_configure', 'create_doc'], }; -// https://www.elastic.co/guide/en/observability/master/apm-api-key.html#apm-create-api-key-workflow-es -export const WRITE_APM_EVENTS: estypes.SecurityApplicationPrivileges = { - application: 'apm', - privileges: ['event:write', 'config_agent:read'], - resources: ['*'], +// https://www.elastic.co/guide/en/fleet/master/grant-access-to-elasticsearch.html#create-api-key-standalone-agent +export const INDEX_LOGS_METRICS_AND_TRACES: estypes.SecurityIndicesPrivileges = { + names: ['logs-*-*', 'metrics-*-*', 'traces-*-*'], + privileges: ['auto_configure', 'create_doc'], }; From 1e05086ea4ce850bf0c56ce7f8315d50fdfc4a01 Mon Sep 17 00:00:00 2001 From: Larry Gregory Date: Tue, 22 Oct 2024 12:04:01 -0400 Subject: [PATCH 08/22] Remove unused dependencies (#197058) ## Summary Removes the following unused dependencies: - `@cfworker/json-schema` - `flat` - `font-awesome` - `html2canvas` - `@formatjs/cli` - `@formatjs/cli-lib` - `@types/babel__core` - `@types/babel__generator` - `@types/babel__helper-plugin-utils` - `@types/event-stream` - `@types/flat` - `@types/md5` - `@types/pegjs` - `@types/tapable` - `@types/zen-observable` - `debug` - `html` Also removes `pbf` from `devDependencies`, as it already exists in the production `dependencies` list. --- package.json | 18 ----- yarn.lock | 184 +++++---------------------------------------------- 2 files changed, 17 insertions(+), 185 deletions(-) diff --git a/package.json b/package.json index 51d1b7472c6dc..f2eb15a482ae2 100644 --- a/package.json +++ b/package.json @@ -105,7 +105,6 @@ "@aws-crypto/sha256-js": "^5.2.0", "@aws-crypto/util": "^5.2.0", "@babel/runtime": "^7.24.7", - "@cfworker/json-schema": "^1.12.7", "@dagrejs/dagre": "^1.1.4", "@dnd-kit/core": "^6.1.0", "@dnd-kit/sortable": "^8.0.0", @@ -1121,9 +1120,7 @@ "fastest-levenshtein": "^1.0.12", "fflate": "^0.6.9", "file-saver": "^1.3.8", - "flat": "5", "fnv-plus": "^1.3.1", - "font-awesome": "4.7.0", "formik": "^2.4.6", "fp-ts": "^2.3.1", "get-port": "^5.0.0", @@ -1136,7 +1133,6 @@ "he": "^1.2.0", "history": "^4.9.0", "hjson": "3.2.1", - "html2canvas": "^1.4.1", "http-proxy-agent": "^5.0.0", "https-proxy-agent": "^5.0.1", "i18n-iso-countries": "^4.3.1", @@ -1334,8 +1330,6 @@ "@elastic/synthetics": "^1.12.1", "@emotion/babel-preset-css-prop": "^11.11.0", "@emotion/jest": "^11.11.0", - "@formatjs/cli": "^6.2.8", - "@formatjs/cli-lib": "^6.3.8", "@frsource/cypress-plugin-visual-regression-diff": "^3.3.10", "@istanbuljs/nyc-config-typescript": "^1.0.2", "@istanbuljs/schema": "^0.1.2", @@ -1531,9 +1525,6 @@ "@types/archiver": "^5.3.1", "@types/async": "^3.2.3", "@types/aws4": "^1.5.0", - "@types/babel__core": "^7.20.5", - "@types/babel__generator": "^7.6.4", - "@types/babel__helper-plugin-utils": "^7.10.0", "@types/base64-js": "^1.2.5", "@types/byte-size": "^8.1.0", "@types/chance": "^1.0.0", @@ -1559,13 +1550,11 @@ "@types/ejs": "^3.0.6", "@types/enzyme": "^3.10.12", "@types/eslint": "^8.44.2", - "@types/event-stream": "^4.0.5", "@types/express": "^4.17.21", "@types/extract-zip": "^1.6.2", "@types/faker": "^5.1.5", "@types/fetch-mock": "^7.3.1", "@types/file-saver": "^2.0.0", - "@types/flat": "^5.0.5", "@types/flot": "^0.0.31", "@types/fnv-plus": "^1.3.0", "@types/geojson": "^7946.0.10", @@ -1594,7 +1583,6 @@ "@types/lz-string": "^1.3.34", "@types/mapbox__vector-tile": "1.3.0", "@types/markdown-it": "^12.2.3", - "@types/md5": "^2.2.0", "@types/micromatch": "^4.0.2", "@types/mime": "^2.0.1", "@types/mime-types": "^2.1.0", @@ -1617,7 +1605,6 @@ "@types/papaparse": "^5.0.3", "@types/pbf": "3.0.2", "@types/pdfmake": "^0.2.2", - "@types/pegjs": "^0.10.1", "@types/picomatch": "^2.3.0", "@types/pidusage": "^2.0.2", "@types/pixelmatch": "^5.2.4", @@ -1649,7 +1636,6 @@ "@types/stats-lite": "^2.2.0", "@types/styled-components": "^5.1.0", "@types/supertest": "^6.0.2", - "@types/tapable": "^1.0.6", "@types/textarea-caret": "^3.0.1", "@types/tinycolor2": "^1.4.1", "@types/tough-cookie": "^4.0.5", @@ -1667,7 +1653,6 @@ "@types/yargs": "^15.0.0", "@types/yauzl": "^2.9.1", "@types/yazl": "^2.4.2", - "@types/zen-observable": "^0.8.0", "@typescript-eslint/eslint-plugin": "^5.62.0", "@typescript-eslint/parser": "^5.62.0", "@typescript-eslint/typescript-estree": "^5.62.0", @@ -1708,7 +1693,6 @@ "cypress-real-events": "^1.11.0", "cypress-recurse": "^1.35.2", "date-fns": "^2.29.3", - "debug": "^2.6.9", "dependency-check": "^4.1.0", "ejs": "^3.1.10", "enzyme": "^3.11.0", @@ -1745,7 +1729,6 @@ "gulp-postcss": "^9.0.1", "gulp-terser": "^2.1.0", "has-ansi": "^3.0.0", - "html": "1.0.0", "html-loader": "^1.3.2", "http-proxy": "^1.18.1", "http2-proxy": "^5.0.53", @@ -1792,7 +1775,6 @@ "oboe": "^2.1.4", "openapi-types": "^10.0.0", "p-reflect": "2.1.0", - "pbf": "3.2.1", "peggy": "^1.2.0", "picomatch": "^2.3.1", "pidusage": "^3.0.2", diff --git a/yarn.lock b/yarn.lock index b5b1294c39f7e..67d502560380f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -435,7 +435,7 @@ js-tokens "^4.0.0" picocolors "^1.0.0" -"@babel/parser@^7.1.0", "@babel/parser@^7.10.3", "@babel/parser@^7.12.11", "@babel/parser@^7.12.7", "@babel/parser@^7.14.7", "@babel/parser@^7.20.7", "@babel/parser@^7.21.8", "@babel/parser@^7.23.0", "@babel/parser@^7.23.9", "@babel/parser@^7.24.7": +"@babel/parser@^7.1.0", "@babel/parser@^7.10.3", "@babel/parser@^7.12.11", "@babel/parser@^7.12.7", "@babel/parser@^7.14.7", "@babel/parser@^7.21.8", "@babel/parser@^7.23.0", "@babel/parser@^7.23.9", "@babel/parser@^7.24.7": version "7.24.7" resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.24.7.tgz#9a5226f92f0c5c8ead550b750f5608e766c8ce85" integrity sha512-9uUYRm6OqQrCqQdG1iCBwBPZgN8ciDBro2nIOFaiRz1/BCxaI7CNvQbDHvsArAC7Tw9Hda/B3U+6ui9u4HWXPw== @@ -1366,7 +1366,7 @@ debug "^4.3.1" globals "^11.1.0" -"@babel/types@7.21.2", "@babel/types@^7.0.0", "@babel/types@^7.10.3", "@babel/types@^7.12.11", "@babel/types@^7.12.7", "@babel/types@^7.20.7", "@babel/types@^7.3.0", "@babel/types@^7.3.3", "@babel/types@^7.4.4": +"@babel/types@7.21.2", "@babel/types@^7.0.0", "@babel/types@^7.10.3", "@babel/types@^7.12.11", "@babel/types@^7.12.7", "@babel/types@^7.3.0", "@babel/types@^7.3.3", "@babel/types@^7.4.4": version "7.21.2" resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.21.2.tgz#92246f6e00f91755893c2876ad653db70c8310d1" integrity sha512-3wRZSs7jiFaB8AjxiiD+VqN5DTG2iRvJGQ+qYFrs/654lg6kGTQWIOFjlBo5RaXuAZjBmP3+OQH4dmhqiiyYxw== @@ -1460,11 +1460,6 @@ react-is "^18.2.0" react-shallow-renderer "^16.15.0" -"@cfworker/json-schema@^1.12.7": - version "1.12.7" - resolved "https://registry.yarnpkg.com/@cfworker/json-schema/-/json-schema-1.12.7.tgz#064d082a11881f684300bc7e6d3021e9d98f9a59" - integrity sha512-KEJUW22arGRQVoS6Ti8SvgXnme6NNMMcGBugdir1hf32ofWUXC8guwrFbepO2+YtqxNBUo5oO0pLYM5d4pyjOg== - "@cnakazawa/watch@^1.0.3": version "1.0.3" resolved "https://registry.yarnpkg.com/@cnakazawa/watch/-/watch-1.0.3.tgz#099139eaec7ebf07a27c1786a3ff64f39464d2ef" @@ -2377,31 +2372,6 @@ resolved "https://registry.yarnpkg.com/@foliojs-fork/restructure/-/restructure-2.0.2.tgz#73759aba2aff1da87b7c4554e6839c70d43c92b4" integrity sha512-59SgoZ3EXbkfSX7b63tsou/SDGzwUEK6MuB5sKqgVK1/XE0fxmpsOb9DQI8LXW3KfGnAjImCGhhEb7uPPAUVNA== -"@formatjs/cli-lib@^6.3.8": - version "6.3.8" - resolved "https://registry.yarnpkg.com/@formatjs/cli-lib/-/cli-lib-6.3.8.tgz#10b685cf833c870d342f24de0f324acd7b02d31b" - integrity sha512-7+40WfAs9UE1BcjpUvtAkWk0REGchANpJZdHhXcMEVcKeDQKk0+OPhlueTnvFmYwENcJ+m7Leb26CRZDzGsc+Q== - dependencies: - "@formatjs/icu-messageformat-parser" "2.7.6" - "@formatjs/ts-transformer" "3.13.12" - "@types/estree" "^1.0.0" - "@types/fs-extra" "^9.0.1" - "@types/json-stable-stringify" "^1.0.32" - "@types/node" "14 || 16 || 17" - chalk "^4.0.0" - commander "8" - fast-glob "^3.2.7" - fs-extra "10" - json-stable-stringify "^1.0.1" - loud-rejection "^2.2.0" - tslib "^2.4.0" - typescript "5" - -"@formatjs/cli@^6.2.8": - version "6.2.8" - resolved "https://registry.yarnpkg.com/@formatjs/cli/-/cli-6.2.8.tgz#df471f79b30935d6c5e5956ad1e8321076ade767" - integrity sha512-sGtFehlHpNL5xbCkP5/zz5lgIVPPdQFqhY6Pcc9+whpBawln6VDzbaGA5ttWjtBDVVKZbEukiCFk8HW2H0OVxQ== - "@formatjs/ecma402-abstract@1.18.2": version "1.18.2" resolved "https://registry.yarnpkg.com/@formatjs/ecma402-abstract/-/ecma402-abstract-1.18.2.tgz#bf103712a406874eb1e387858d5be2371ab3aa14" @@ -10165,7 +10135,7 @@ dependencies: "@types/node" "*" -"@types/babel__core@*", "@types/babel__core@^7.1.14": +"@types/babel__core@^7.1.14": version "7.1.20" resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.20.tgz#e168cdd612c92a2d335029ed62ac94c95b362359" integrity sha512-PVb6Bg2QuscZ30FvOU7z4guG6c926D9YRvOxEaelzndpMsvP+YM74Q/dAFASpg2l6+XLalxSGxcq/lrgYWZtyQ== @@ -10176,17 +10146,6 @@ "@types/babel__template" "*" "@types/babel__traverse" "*" -"@types/babel__core@^7.20.5": - version "7.20.5" - resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.20.5.tgz#3df15f27ba85319caa07ba08d0721889bb39c017" - integrity sha512-qoQprZvz5wQFJwMDqeseRXWv3rqMvhgpbXFfVyWhbx9X47POIA6i/+dXefEmZKoAgOaTdaIgNSMqMIU61yRyzA== - dependencies: - "@babel/parser" "^7.20.7" - "@babel/types" "^7.20.7" - "@types/babel__generator" "*" - "@types/babel__template" "*" - "@types/babel__traverse" "*" - "@types/babel__generator@*": version "7.0.2" resolved "https://registry.yarnpkg.com/@types/babel__generator/-/babel__generator-7.0.2.tgz#d2112a6b21fad600d7674274293c85dce0cb47fc" @@ -10194,20 +10153,6 @@ dependencies: "@babel/types" "^7.0.0" -"@types/babel__generator@^7.6.4": - version "7.6.4" - resolved "https://registry.yarnpkg.com/@types/babel__generator/-/babel__generator-7.6.4.tgz#1f20ce4c5b1990b37900b63f050182d28c2439b7" - integrity sha512-tFkciB9j2K755yrTALxD44McOrk+gfpIpvC3sxHjRawj6PfnQxrse4Clq5y/Rq+G3mrBurMax/lG8Qn2t9mSsg== - dependencies: - "@babel/types" "^7.0.0" - -"@types/babel__helper-plugin-utils@^7.10.0": - version "7.10.0" - resolved "https://registry.yarnpkg.com/@types/babel__helper-plugin-utils/-/babel__helper-plugin-utils-7.10.0.tgz#dcd2416f9c189d5837ab2a276368cf67134efe78" - integrity sha512-60YtHzhQ9HAkToHVV+TB4VLzBn9lrfgrsOjiJMtbv/c1jPdekBxaByd6DMsGBzROXWoIL6U3lEFvvbu69RkUoA== - dependencies: - "@types/babel__core" "*" - "@types/babel__template@*": version "7.0.2" resolved "https://registry.yarnpkg.com/@types/babel__template/-/babel__template-7.0.2.tgz#4ff63d6b52eddac1de7b975a5223ed32ecea9307" @@ -10537,13 +10482,6 @@ resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.5.tgz#a6ce3e556e00fd9895dd872dd172ad0d4bd687f4" integrity sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw== -"@types/event-stream@^4.0.5": - version "4.0.5" - resolved "https://registry.yarnpkg.com/@types/event-stream/-/event-stream-4.0.5.tgz#29f1be5f4c0de2e0312cf3b5f7146c975c08d918" - integrity sha512-pQ/RR/iuBW8K8WmwYaaC1nkZH0cHonNAIw6ktG8BCNrNuqNeERfBzNIAOq6Z7tvLzpjcMV02SZ5pxAekAYQpWA== - dependencies: - "@types/node" "*" - "@types/expect@^1.20.4": version "1.20.4" resolved "https://registry.yarnpkg.com/@types/expect/-/expect-1.20.4.tgz#8288e51737bf7e3ab5d7c77bfa695883745264e5" @@ -10589,11 +10527,6 @@ resolved "https://registry.yarnpkg.com/@types/file-saver/-/file-saver-2.0.0.tgz#cbb49815a5e1129d5f23836a98d65d93822409af" integrity sha512-dxdRrUov2HVTbSRFX+7xwUPlbGYVEZK6PrSqClg2QPos3PNe0bCajkDDkDeeC1znjSH03KOEqVbXpnJuWa2wgQ== -"@types/flat@^5.0.5": - version "5.0.5" - resolved "https://registry.yarnpkg.com/@types/flat/-/flat-5.0.5.tgz#2304df0b2b1e6dde50d81f029593e0a1bc2474d3" - integrity sha512-nPLljZQKSnac53KDUDzuzdRfGI0TDb5qPrb+SrQyN3MtdQrOnGsKniHN1iYZsJEBIVQve94Y6gNz22sgISZq+Q== - "@types/flot@^0.0.31": version "0.0.31" resolved "https://registry.yarnpkg.com/@types/flot/-/flot-0.0.31.tgz#0daca37c6c855b69a0a7e2e37dd0f84b3db8c8c1" @@ -10606,13 +10539,6 @@ resolved "https://registry.yarnpkg.com/@types/fnv-plus/-/fnv-plus-1.3.0.tgz#0f43f0b7e7b4b24de3a1cab69bfa009508f4c084" integrity sha512-ijls8MsO6Q9JUSd5w1v4y2ijM6S4D/nmOyI/FwcepvrZfym0wZhLdYGFD5TJID7tga0O3I7SmtK69RzpSJ1Fcw== -"@types/fs-extra@^9.0.1": - version "9.0.13" - resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-9.0.13.tgz#7594fbae04fe7f1918ce8b3d213f74ff44ac1f45" - integrity sha512-nEnwB++1u5lVDM2UI4c1+5R+FYaKfaAzS4OococimjVm3nQw3TuzH5UNsocrcTBbhnerblyHj4A49qXbIiZdpA== - dependencies: - "@types/node" "*" - "@types/geojson@*", "@types/geojson@^7946.0.10", "@types/geojson@^7946.0.7": version "7946.0.10" resolved "https://registry.yarnpkg.com/@types/geojson/-/geojson-7946.0.10.tgz#6dfbf5ea17142f7f9a043809f1cd4c448cb68249" @@ -10918,13 +10844,6 @@ "@types/linkify-it" "*" "@types/mdurl" "*" -"@types/md5@^2.2.0": - version "2.2.0" - resolved "https://registry.yarnpkg.com/@types/md5/-/md5-2.2.0.tgz#cd82e16b95973f94bb03dee40c5b6be4a7fb7fb4" - integrity sha512-JN8OVL/wiDlCWTPzplsgMPu0uE9Q6blwp68rYsfk2G8aokRUQ8XD9MEhZwihfAiQvoyE+m31m6i3GFXwYWomKQ== - dependencies: - "@types/node" "*" - "@types/md5@^2.3.2": version "2.3.5" resolved "https://registry.yarnpkg.com/@types/md5/-/md5-2.3.5.tgz#481cef0a896e3a5dcbfc5a8a8b02c05958af48a5" @@ -11156,11 +11075,6 @@ "@types/node" "*" "@types/pdfkit" "*" -"@types/pegjs@^0.10.1": - version "0.10.1" - resolved "https://registry.yarnpkg.com/@types/pegjs/-/pegjs-0.10.1.tgz#9a2f3961dc62430fdb21061eb0ddbd890f9e3b94" - integrity sha512-ra8IchO9odGQmYKbm+94K58UyKCEKdZh9y0vxhG4pIpOJOBlC1C+ZtBVr6jLs+/oJ4pl+1p/4t3JtBA8J10Vvw== - "@types/picomatch@^2.3.0": version "2.3.0" resolved "https://registry.yarnpkg.com/@types/picomatch/-/picomatch-2.3.0.tgz#75db5e75a713c5a83d5b76780c3da84a82806003" @@ -11525,7 +11439,7 @@ "@types/methods" "^1.1.4" "@types/superagent" "^8.1.0" -"@types/tapable@^1", "@types/tapable@^1.0.5", "@types/tapable@^1.0.6": +"@types/tapable@^1", "@types/tapable@^1.0.5": version "1.0.6" resolved "https://registry.yarnpkg.com/@types/tapable/-/tapable-1.0.6.tgz#a9ca4b70a18b270ccb2bc0aaafefd1d486b7ea74" integrity sha512-W+bw9ds02rAQaMvaLYxAbJ6cvguW/iJXNT6lTssS1ps6QdrMKttqEAMEG/b5CR8TZl3/L7/lH0ZV5nNR1LXikA== @@ -11747,11 +11661,6 @@ dependencies: "@types/node" "*" -"@types/zen-observable@^0.8.0": - version "0.8.0" - resolved "https://registry.yarnpkg.com/@types/zen-observable/-/zen-observable-0.8.0.tgz#8b63ab7f1aa5321248aad5ac890a485656dcea4d" - integrity sha512-te5lMAWii1uEJ4FwLjzdlbw3+n0FZNOvFXHxQDKeT0dilh7HOzdMzV2TrJVUzq8ep7J4Na8OUYPRLSQkJHAlrg== - "@typescript-eslint/eslint-plugin@^5.62.0": version "5.62.0" resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.62.0.tgz#aeef0328d172b9e37d9bab6dbc13b87ed88977db" @@ -13406,11 +13315,6 @@ bare-path@^2.0.0, bare-path@^2.1.0: dependencies: bare-os "^2.1.0" -base64-arraybuffer@^1.0.2: - version "1.0.2" - resolved "https://registry.yarnpkg.com/base64-arraybuffer/-/base64-arraybuffer-1.0.2.tgz#1c37589a7c4b0746e34bd1feb951da2df01c1bdc" - integrity sha512-I3yl4r9QB5ZRY3XuJVEPfc2XhZO6YweFPI+UovAzn+8/hb3oJ6lnysaFcjVpkCPfVWFUDvoZ8kmVDP7WyRtYtQ== - base64-js@1.3.1: version "1.3.1" resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.3.1.tgz#58ece8cb75dd07e71ed08c736abc5fac4dbf8df1" @@ -14772,11 +14676,6 @@ commander@7, commander@^7.0.0, commander@^7.2.0: resolved "https://registry.yarnpkg.com/commander/-/commander-7.2.0.tgz#a36cb57d0b501ce108e4d20559a150a391d97ab7" integrity sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw== -commander@8: - version "8.3.0" - resolved "https://registry.yarnpkg.com/commander/-/commander-8.3.0.tgz#4837ea1b2da67b9c616a67afbb0fafee567bca66" - integrity sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww== - commander@^10.0.1: version "10.0.1" resolved "https://registry.yarnpkg.com/commander/-/commander-10.0.1.tgz#881ee46b4f77d1c1dccc5823433aa39b022cbe06" @@ -14878,7 +14777,7 @@ concat-map@0.0.1: resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" integrity sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg== -concat-stream@^1.4.7, concat-stream@^1.5.0, concat-stream@~1.6.0: +concat-stream@^1.5.0, concat-stream@~1.6.0: version "1.6.2" resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-1.6.2.tgz#904bdf194cd3122fc675c77fc4ac3d4ff0fd1a34" integrity sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw== @@ -15278,13 +15177,6 @@ css-in-js-utils@^2.0.0: hyphenate-style-name "^1.0.2" isobject "^3.0.1" -css-line-break@^2.1.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/css-line-break/-/css-line-break-2.1.0.tgz#bfef660dfa6f5397ea54116bb3cb4873edbc4fa0" - integrity sha512-FHcKFCZcAha3LwfVBhCQbW2nCNbkZXn7KVUJcsT5/P8YmfsVja0FMPJr0B903j/E69HUphKiV9iQArX8SDYA4w== - dependencies: - utrie "^1.0.2" - css-loader@^3.4.2, css-loader@^3.6.0: version "3.6.0" resolved "https://registry.yarnpkg.com/css-loader/-/css-loader-3.6.0.tgz#2e4b2c7e6e2d27f8c8f28f61bffcd2e6c91ef645" @@ -15940,7 +15832,7 @@ dayjs@^1.10.4: resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.10.tgz#68acea85317a6e164457d6d6947564029a6a16a0" integrity sha512-vjAczensTgRcqDERK0SR2XMwsF/tSvnvlv6VcF2GIhg6Sx4yOIt/irsr1RDJsKiIyBzJDpCoXiWWq28MqH2cnQ== -debug@2.6.9, debug@^2.2.0, debug@^2.3.3, debug@^2.6.0, debug@^2.6.9: +debug@2.6.9, debug@^2.2.0, debug@^2.3.3, debug@^2.6.0: version "2.6.9" resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" integrity sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA== @@ -18118,7 +18010,7 @@ fast-glob@^2.2.6: merge2 "^1.2.3" micromatch "^3.1.10" -fast-glob@^3.0.3, fast-glob@^3.2.11, fast-glob@^3.2.7, fast-glob@^3.2.9, fast-glob@^3.3.2: +fast-glob@^3.0.3, fast-glob@^3.2.11, fast-glob@^3.2.9, fast-glob@^3.3.2: version "3.3.2" resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.3.2.tgz#a904501e57cfdd2ffcded45e99a54fef55e46129" integrity sha512-oX2ruAFQwf/Orj8m737Y5adxDQO0LAB7/S5MnxCdTNDd4p6BsyIVsv9JQsATbTSq8KHRpLwIHbVlUNatxd+1Ow== @@ -18465,7 +18357,7 @@ flat-cache@^3.0.4: flatted "^3.1.0" rimraf "^3.0.2" -flat@5, flat@^5.0.2: +flat@^5.0.2: version "5.0.2" resolved "https://registry.yarnpkg.com/flat/-/flat-5.0.2.tgz#8ca6fe332069ffa9d324c327198c598259ceb241" integrity sha512-b6suED+5/3rTpUBdG1gupIl8MPFCAMA0QXwmljLhvCUKcUvdE4gWky9zpuGCcXHOsz4J9wPGNWq6OKpmIzz3hQ== @@ -18510,11 +18402,6 @@ follow-redirects@^1.0.0, follow-redirects@^1.15.6: resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.6.tgz#7f815c0cda4249c74ff09e95ef97c23b5fd0399b" integrity sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA== -font-awesome@4.7.0: - version "4.7.0" - resolved "https://registry.yarnpkg.com/font-awesome/-/font-awesome-4.7.0.tgz#8fa8cf0411a1a31afd07b06d2902bb9fc815a133" - integrity sha1-j6jPBBGhoxr9B7BtKQK7n8gVoTM= - for-each@^0.3.3: version "0.3.3" resolved "https://registry.yarnpkg.com/for-each/-/for-each-0.3.3.tgz#69b447e88a0a5d32c3e7084f3f1710034b21376e" @@ -18710,15 +18597,6 @@ fs-constants@^1.0.0: resolved "https://registry.yarnpkg.com/fs-constants/-/fs-constants-1.0.0.tgz#6be0de9be998ce16af8afc24497b9ee9b7ccd9ad" integrity sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow== -fs-extra@10, fs-extra@^10.0.0: - version "10.1.0" - resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-10.1.0.tgz#02873cfbc4084dde127eaa5f9905eef2325d1abf" - integrity sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ== - dependencies: - graceful-fs "^4.2.0" - jsonfile "^6.0.1" - universalify "^2.0.0" - fs-extra@^0.30.0: version "0.30.0" resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-0.30.0.tgz#f233ffcc08d4da7d432daa449776989db1df93f0" @@ -18730,6 +18608,15 @@ fs-extra@^0.30.0: path-is-absolute "^1.0.0" rimraf "^2.2.8" +fs-extra@^10.0.0: + version "10.1.0" + resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-10.1.0.tgz#02873cfbc4084dde127eaa5f9905eef2325d1abf" + integrity sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ== + dependencies: + graceful-fs "^4.2.0" + jsonfile "^6.0.1" + universalify "^2.0.0" + fs-extra@^7.0.1: version "7.0.1" resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-7.0.1.tgz#4f189c44aa123b895f722804f55ea23eadc348e9" @@ -19946,21 +19833,6 @@ html-webpack-plugin@^4.0.0: tapable "^1.1.3" util.promisify "1.0.0" -html2canvas@^1.4.1: - version "1.4.1" - resolved "https://registry.yarnpkg.com/html2canvas/-/html2canvas-1.4.1.tgz#7cef1888311b5011d507794a066041b14669a543" - integrity sha512-fPU6BHNpsyIhr8yyMpTLLxAbkaK8ArIBcmZIRiBLiDhjeqvXolaEmDGmELFuX9I4xDcaKKcJl+TKZLqruBbmWA== - dependencies: - css-line-break "^2.1.0" - text-segmentation "^1.0.3" - -html@1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/html/-/html-1.0.0.tgz#a544fa9ea5492bfb3a2cca8210a10be7b5af1f61" - integrity sha1-pUT6nqVJK/s6LMqCEKEL57WvH2E= - dependencies: - concat-stream "^1.4.7" - htmlparser2@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/htmlparser2/-/htmlparser2-4.1.0.tgz#9a4ef161f2e4625ebf7dfbe6c0a2f52d18a59e78" @@ -22816,14 +22688,6 @@ loud-rejection@^1.0.0: currently-unhandled "^0.4.1" signal-exit "^3.0.0" -loud-rejection@^2.2.0: - version "2.2.0" - resolved "https://registry.yarnpkg.com/loud-rejection/-/loud-rejection-2.2.0.tgz#4255eb6e9c74045b0edc021fa7397ab655a8517c" - integrity sha512-S0FayMXku80toa5sZ6Ro4C+s+EtFDCsyJNG/AzFMfX3AxD5Si4dZsgzm/kKnbOxHl5Cv8jBlno8+3XYIh2pNjQ== - dependencies: - currently-unhandled "^0.4.1" - signal-exit "^3.0.2" - lower-case@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/lower-case/-/lower-case-2.0.2.tgz#6fa237c63dbdc4a82ca0fd882e4722dc5e634e28" @@ -30500,13 +30364,6 @@ text-hex@1.0.x: resolved "https://registry.yarnpkg.com/text-hex/-/text-hex-1.0.0.tgz#69dc9c1b17446ee79a92bf5b884bb4b9127506f5" integrity sha512-uuVGNWzgJ4yhRaNSiubPY7OjISw4sw4E5Uv0wbjp+OzcbmVU/rsT8ujgcXJhn9ypzsgr5vlzpPqP+MBBKcGvbg== -text-segmentation@^1.0.3: - version "1.0.3" - resolved "https://registry.yarnpkg.com/text-segmentation/-/text-segmentation-1.0.3.tgz#52a388159efffe746b24a63ba311b6ac9f2d7943" - integrity sha512-iOiPUo/BGnZ6+54OsWxZidGCsdU8YbE4PSpdPinp7DeMtUJNJBoJ/ouUSTJjHkh1KntHaltHl/gDs2FC4i5+Nw== - dependencies: - utrie "^1.0.2" - text-table@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4" @@ -31631,13 +31488,6 @@ utils-merge@1.0.1: resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713" integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM= -utrie@^1.0.2: - version "1.0.2" - resolved "https://registry.yarnpkg.com/utrie/-/utrie-1.0.2.tgz#d42fe44de9bc0119c25de7f564a6ed1b2c87a645" - integrity sha512-1MLa5ouZiOmQzUbjbu9VmjLzn1QLXBhwpUa7kdLUQK+KQ5KA9I1vk5U4YHe/X2Ch7PYnJfWuWT+VbuxbGwljhw== - dependencies: - base64-arraybuffer "^1.0.2" - uuid-browser@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/uuid-browser/-/uuid-browser-3.1.0.tgz#0f05a40aef74f9e5951e20efbf44b11871e56410" From c417196905278c590539476b72f6422104a1d4d7 Mon Sep 17 00:00:00 2001 From: Ahmad Bamieh Date: Tue, 22 Oct 2024 19:57:37 +0300 Subject: [PATCH 09/22] [Core] [UA] Support API Deprecations (#196081) # Summary Adds a new API deprecations feature inside core. This feature enabled plugin developers to mark their versioned and unversioned public routes as deprecated. These deprecations will be surfaced to the users through UA to help them understand the deprecation and address it before upgrading. This PR also surfaces these deprecations to UA. Closes https://github.com/elastic/kibana/issues/117241 1. Core service to flag deprecated routes 2. UA code to surface and resolve deprecated routes ## Flagging a deprecated Route ### The route deprecation option We have three types of route deprecations: - `type: bump`: A version bump deprecation means the API has a new version and the current version will be removed in the future in favor of the newer version. - `type: remove`: This API will be completely removed. You will no longer be able to use it in the future. - `type: migrate`: This API will be migrated to a different API and will be removed in the future in favor of the other API. All route deprecations expect a documentation link to help users navigate. We might add a generic documentation link and drop this requirement in the future but for now this is required. ### Deprecated Route Example Full examples can be found in the `routing_example` example plugin located in this directory: `examples/routing_example/server/routes/deprecated_routes` ```ts router[versioned?].get( { path: '/', options: { deprecated: { documentationUrl: 'https://google.com', severity: 'warning', reason: { type: 'bump', newApiVersion: '2024-10-13', }, }, }, }, async (context, req, res) => { ... ``` ## Surfaced API deprecations in UA The list of deprecated APIs will be listed inside Kibana deprecations along with the already supported config deprecations. image Users can click on the list item to learn more about each deprecation and mark it as resolved image ### Marking as resolved Users can click on mark as resolved button in the UA to hide the deprecation from the Kiban deprecations list. We keep track on when this button was clicked and how many times the API has been called. If the API is called again the deprecation will re-appear inside the list. We might add a feature in the future to permenantly supress the API deprecation from showing in the list through a configuration (https://github.com/elastic/kibana/issues/196089) If the API has been marked as resolved before we show this in the flyout message: > The API GET /api/deprecations/ has been called 25 times. The last time the API was called was on Monday, October 14, 2024 1:08 PM +03:00. > The api has been called 2 times since the last time it was marked as resolved on Monday, October 14, 2024 1:08 PM +03:00 Once marked as resolved the flyout exists and we show this to the user until they refresh the page image ## Telemetry: We keep track of 2 new things for telemetry purposes: 1. The number of times the deprecated API has been called 2. The number of times the deprecated API has been resolved (how many times the mark as resolved button in UA was clicked) ## Code review - [x] Core team is expected to review the whole PR - [ ] Docs team to review the copy and update the UA displayed texts (title, description, and manual steps) - [x] kibana-management team is expected to review the UA code changes and UI - [ ] A few teams are only required to approve this PR and update their `deprecated: true` route param to the new deprecationInfo object we now expect. There is an issue tracker to address those in separate PRs later on: https://github.com/elastic/kibana/issues/196095 ## Testing Run kibana locally with the test example plugin that has deprecated routes ``` yarn start --plugin-path=examples/routing_example --plugin-path=examples/developer_examples ``` The following comprehensive deprecated routes examples are registered inside the folder: `examples/routing_example/server/routes/deprecated_routes` Run them in the console to trigger the deprecation condition so they show up in the UA: ``` # Versioned routes: Version 1 is deprecated GET kbn:/api/routing_example/d/versioned?apiVersion=1 GET kbn:/api/routing_example/d/versioned?apiVersion=2 # Non-versioned routes GET kbn:/api/routing_example/d/removed_route POST kbn:/api/routing_example/d/migrated_route {} ``` 1. You can also mark as deprecated in the UA to remove the deprecation from the list. 2. Check the telemetry response to see the reported data about the deprecated route. 3. Calling version 2 of the API does not do anything since it is not deprecated unlike version `1` (`GET kbn:/api/routing_example/d/versioned?apiVersion=2`) 4. Internally you can see the deprecations counters from the dev console by running the following: ``` GET .kibana_usage_counters/_search { "query": { "bool": { "should": [ {"match": { "usage-counter.counterType": "deprecated_api_call:total"}}, {"match": { "usage-counter.counterType": "deprecated_api_call:resolved"}}, {"match": { "usage-counter.counterType": "deprecated_api_call:marked_as_resolved"}} ] } } } ``` --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: florent-leborgne Co-authored-by: Elastic Machine --- .buildkite/ftr_platform_stateful_configs.yml | 2 +- .eslintrc.js | 1 + examples/routing_example/common/index.ts | 6 + examples/routing_example/server/plugin.ts | 3 +- .../server/routes/deprecated_routes/index.ts | 17 + .../routes/deprecated_routes/unversioned.ts | 62 +++ .../routes/deprecated_routes/versioned.ts | 53 +++ .../routing_example/server/routes/index.ts | 1 + examples/routing_example/tsconfig.json | 1 + oas_docs/bundle.json | 11 - oas_docs/bundle.serverless.json | 11 - .../output/kibana.serverless.staging.yaml | 11 - oas_docs/output/kibana.serverless.yaml | 11 - oas_docs/output/kibana.staging.yaml | 11 - oas_docs/output/kibana.yaml | 11 - .../src/deprecations_client.test.ts | 2 +- .../src/deprecations_client.ts | 71 +++- .../core-deprecations-common/src/types.ts | 31 +- .../src/deprecations/api_deprecations.test.ts | 353 ++++++++++++++++++ .../src/deprecations/api_deprecations.ts | 96 +++++ .../deprecations/config_deprecations.test.ts | 115 ++++++ .../src/deprecations/config_deprecations.ts | 50 +++ .../src/deprecations/i18n_texts.ts | 132 +++++++ .../src/deprecations/index.ts | 11 + .../src/deprecations_service.test.mocks.ts | 8 + .../src/deprecations_service.test.ts | 106 +----- .../src/deprecations_service.ts | 54 +-- .../src/routes/index.ts | 14 +- .../src/routes/post_validation_handler.ts | 51 +++ .../src/routes/resolve_deprecated_api.ts | 52 +++ .../tsconfig.json | 5 + .../core-http-router-server-internal/index.ts | 8 +- .../src/request.ts | 16 +- .../src/router.test.ts | 14 +- .../src/router.ts | 78 ++-- .../core_versioned_route.test.ts | 2 +- .../versioned_router/core_versioned_route.ts | 16 +- .../core_versioned_router.test.ts | 5 +- .../versioned_router/core_versioned_router.ts | 14 +- .../src/versioned_router/index.ts | 2 +- .../src/versioned_router/mocks.ts | 1 + .../src/versioned_router/types.ts | 17 +- .../src/versioned_router.mock.ts | 15 +- .../src/http_server.test.ts | 2 + .../src/http_server.ts | 48 ++- .../src/http_service.ts | 8 +- .../core-http-server-internal/src/types.ts | 7 + .../src/http_service.mock.ts | 4 + packages/core/http/core-http-server/index.ts | 2 + .../core-http-server/src/http_contract.ts | 9 + .../http/core-http-server/src/router/index.ts | 2 +- .../core-http-server/src/router/request.ts | 9 +- .../http/core-http-server/src/router/route.ts | 51 ++- .../core-http-server/src/router/router.ts | 13 +- .../core-http-server/src/versioning/index.ts | 1 + .../core-http-server/src/versioning/types.ts | 29 +- .../core/http/core-http-server/tsconfig.json | 2 +- .../src/core_setup.mock.ts | 2 + .../src/plugin_context.ts | 2 + .../core-root-server-internal/src/server.ts | 9 +- .../src/mocks/internal_mocks.ts | 1 + .../src/routes/bulk_create.ts | 1 + .../src/routes/bulk_delete.ts | 1 + .../src/routes/bulk_get.ts | 1 + .../src/routes/bulk_resolve.ts | 1 + .../src/routes/bulk_update.ts | 1 + .../src/routes/create.ts | 1 + .../src/routes/delete.ts | 1 + .../src/routes/find.ts | 1 + .../src/routes/get.ts | 1 + .../src/routes/resolve.ts | 1 + .../src/routes/update.ts | 1 + .../src/usage_stats_client.ts | 9 +- .../src/core_usage_data_service.ts | 13 + .../src/core_usage_stats_client.test.ts | 1 + .../src/core_usage_stats_client.ts | 52 +++ .../src/core_usage_data_service.mock.ts | 1 + .../src/core_usage_stats_client.mock.ts | 1 + .../core-usage-data-server/index.ts | 2 + .../src/core_usage_stats.ts | 13 + .../core-usage-data-server/src/index.ts | 3 +- .../src/setup_contract.ts | 15 +- .../core-usage-data-server/tsconfig.json | 3 + .../__snapshots__/generate_oas.test.ts.snap | 1 + .../src/generate_oas.test.ts | 2 + .../src/generate_oas.test.util.ts | 14 +- .../src/process_router.ts | 3 +- .../src/process_versioned_router.test.ts | 7 +- .../src/process_versioned_router.ts | 7 +- .../http/request_representation.test.ts | 6 +- .../server/collectors/common/counters.test.ts | 1 - .../server/collectors/common/counters.ts | 2 +- .../core/fetch_deprecated_api_counters.ts | 69 ++++ .../server/collectors/core/index.ts | 1 + .../server/collectors/index.ts | 2 +- .../kibana_usage_collection/server/plugin.ts | 5 + .../test_suites/core/deprecations.ts | 2 +- .../actions/server/routes/legacy/create.ts | 1 + .../actions/server/routes/legacy/delete.ts | 1 + .../actions/server/routes/legacy/execute.ts | 1 + .../actions/server/routes/legacy/get.ts | 1 + .../actions/server/routes/legacy/get_all.ts | 1 + .../server/routes/legacy/list_action_types.ts | 1 + .../actions/server/routes/legacy/update.ts | 1 + .../alerting/server/routes/legacy/create.ts | 1 + .../alerting/server/routes/legacy/delete.ts | 1 + .../alerting/server/routes/legacy/disable.ts | 1 + .../alerting/server/routes/legacy/enable.ts | 1 + .../alerting/server/routes/legacy/find.ts | 1 + .../alerting/server/routes/legacy/get.ts | 1 + .../legacy/get_alert_instance_summary.ts | 1 + .../server/routes/legacy/get_alert_state.ts | 1 + .../alerting/server/routes/legacy/health.ts | 1 + .../server/routes/legacy/list_alert_types.ts | 1 + .../alerting/server/routes/legacy/mute_all.ts | 1 + .../server/routes/legacy/mute_instance.ts | 1 + .../server/routes/legacy/unmute_all.ts | 1 + .../server/routes/legacy/unmute_instance.ts | 1 + .../alerting/server/routes/legacy/update.ts | 1 + .../server/routes/legacy/update_api_key.ts | 1 + .../routes/api/comments/get_all_comment.ts | 1 + .../server/routes/api/stats/get_status.ts | 1 + .../api/user_actions/get_all_user_actions.ts | 1 + .../fleet/server/routes/agent/index.ts | 2 + .../plugins/fleet/server/routes/app/index.ts | 1 + .../server/routes/enrollment_api_key/index.ts | 4 + .../plugins/fleet/server/routes/epm/index.ts | 4 + x-pack/plugins/ml/server/routes/system.ts | 3 +- .../server/endpoint/routes/metadata/index.ts | 1 + .../endpoint/routes/suggestions/index.ts | 1 + x-pack/plugins/upgrade_assistant/README.md | 43 ++- .../deprecation_details_flyout.tsx | 44 ++- .../kibana_deprecations.tsx | 37 +- .../kibana_deprecations_table.tsx | 19 +- .../resolution_table_cell.tsx | 71 +++- x-pack/test/tsconfig.json | 4 +- .../{config.js => config.ts} | 13 +- .../upgrade_assistant/api_deprecations.ts | 162 ++++++++ .../upgrade_assistant/{index.js => index.ts} | 5 +- 139 files changed, 2057 insertions(+), 381 deletions(-) create mode 100644 examples/routing_example/server/routes/deprecated_routes/index.ts create mode 100644 examples/routing_example/server/routes/deprecated_routes/unversioned.ts create mode 100644 examples/routing_example/server/routes/deprecated_routes/versioned.ts create mode 100644 packages/core/deprecations/core-deprecations-server-internal/src/deprecations/api_deprecations.test.ts create mode 100644 packages/core/deprecations/core-deprecations-server-internal/src/deprecations/api_deprecations.ts create mode 100644 packages/core/deprecations/core-deprecations-server-internal/src/deprecations/config_deprecations.test.ts create mode 100644 packages/core/deprecations/core-deprecations-server-internal/src/deprecations/config_deprecations.ts create mode 100644 packages/core/deprecations/core-deprecations-server-internal/src/deprecations/i18n_texts.ts create mode 100644 packages/core/deprecations/core-deprecations-server-internal/src/deprecations/index.ts create mode 100644 packages/core/deprecations/core-deprecations-server-internal/src/routes/post_validation_handler.ts create mode 100644 packages/core/deprecations/core-deprecations-server-internal/src/routes/resolve_deprecated_api.ts create mode 100644 src/plugins/kibana_usage_collection/server/collectors/core/fetch_deprecated_api_counters.ts rename x-pack/test/upgrade_assistant_integration/{config.js => config.ts} (70%) create mode 100644 x-pack/test/upgrade_assistant_integration/upgrade_assistant/api_deprecations.ts rename x-pack/test/upgrade_assistant_integration/upgrade_assistant/{index.js => index.ts} (64%) diff --git a/.buildkite/ftr_platform_stateful_configs.yml b/.buildkite/ftr_platform_stateful_configs.yml index 6958ec4530ae8..244f04257ef92 100644 --- a/.buildkite/ftr_platform_stateful_configs.yml +++ b/.buildkite/ftr_platform_stateful_configs.yml @@ -344,7 +344,7 @@ enabled: - x-pack/test/task_manager_claimer_mget/config.ts - x-pack/test/ui_capabilities/security_and_spaces/config.ts - x-pack/test/ui_capabilities/spaces_only/config.ts - - x-pack/test/upgrade_assistant_integration/config.js + - x-pack/test/upgrade_assistant_integration/config.ts - x-pack/test/usage_collection/config.ts - x-pack/performance/journeys_e2e/aiops_log_rate_analysis.ts - x-pack/performance/journeys_e2e/ecommerce_dashboard.ts diff --git a/.eslintrc.js b/.eslintrc.js index 006f39ce1026c..3c67594513c0e 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -643,6 +643,7 @@ module.exports = { 'x-pack/test/*/*config.*ts', 'x-pack/test/saved_object_api_integration/*/apis/**/*', 'x-pack/test/ui_capabilities/*/tests/**/*', + 'x-pack/test/upgrade_assistant_integration/**/*', 'x-pack/test/performance/**/*.ts', '**/cypress.config.{js,ts}', 'x-pack/test_serverless/**/config*.ts', diff --git a/examples/routing_example/common/index.ts b/examples/routing_example/common/index.ts index c86a994f469f1..b83582b66ff08 100644 --- a/examples/routing_example/common/index.ts +++ b/examples/routing_example/common/index.ts @@ -15,3 +15,9 @@ export const POST_MESSAGE_ROUTE_PATH = '/api/post_message'; // Internal APIs should use the `internal` prefix, instead of the `api` prefix. export const INTERNAL_GET_MESSAGE_BY_ID_ROUTE = '/internal/get_message'; + +export const DEPRECATED_ROUTES = { + REMOVED_ROUTE: '/api/routing_example/d/removed_route', + MIGRATED_ROUTE: '/api/routing_example/d/migrated_route', + VERSIONED_ROUTE: '/api/routing_example/d/versioned', +}; diff --git a/examples/routing_example/server/plugin.ts b/examples/routing_example/server/plugin.ts index cb6a00920dd05..d4036afc58b5b 100644 --- a/examples/routing_example/server/plugin.ts +++ b/examples/routing_example/server/plugin.ts @@ -8,13 +8,14 @@ */ import { Plugin, CoreSetup, CoreStart } from '@kbn/core/server'; -import { registerRoutes } from './routes'; +import { registerRoutes, registerDeprecatedRoutes } from './routes'; export class RoutingExamplePlugin implements Plugin<{}, {}> { public setup(core: CoreSetup) { const router = core.http.createRouter(); registerRoutes(router); + registerDeprecatedRoutes(router); return {}; } diff --git a/examples/routing_example/server/routes/deprecated_routes/index.ts b/examples/routing_example/server/routes/deprecated_routes/index.ts new file mode 100644 index 0000000000000..75dc0261ed1b9 --- /dev/null +++ b/examples/routing_example/server/routes/deprecated_routes/index.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 + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { IRouter } from '@kbn/core/server'; +import { registerDeprecatedRoute } from './unversioned'; +import { registerVersionedDeprecatedRoute } from './versioned'; + +export function registerDeprecatedRoutes(router: IRouter) { + registerDeprecatedRoute(router); + registerVersionedDeprecatedRoute(router); +} diff --git a/examples/routing_example/server/routes/deprecated_routes/unversioned.ts b/examples/routing_example/server/routes/deprecated_routes/unversioned.ts new file mode 100644 index 0000000000000..4e1451a91fc38 --- /dev/null +++ b/examples/routing_example/server/routes/deprecated_routes/unversioned.ts @@ -0,0 +1,62 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import type { IRouter } from '@kbn/core/server'; +import { schema } from '@kbn/config-schema'; +import { DEPRECATED_ROUTES } from '../../../common'; + +export const registerDeprecatedRoute = (router: IRouter) => { + router.get( + { + path: DEPRECATED_ROUTES.REMOVED_ROUTE, + validate: false, + options: { + access: 'public', + deprecated: { + documentationUrl: 'https://elastic.co/', + severity: 'critical', + reason: { type: 'remove' }, + }, + }, + }, + async (ctx, req, res) => { + return res.ok({ + body: { result: 'Called deprecated route. Check UA to see the deprecation.' }, + }); + } + ); + + router.post( + { + path: DEPRECATED_ROUTES.MIGRATED_ROUTE, + validate: { + body: schema.object({ + test: schema.maybe(schema.boolean()), + }), + }, + options: { + access: 'public', + deprecated: { + documentationUrl: 'https://elastic.co/', + severity: 'critical', + reason: { + type: 'migrate', + newApiMethod: 'GET', + newApiPath: `${DEPRECATED_ROUTES.VERSIONED_ROUTE}?apiVersion=2`, + }, + }, + }, + }, + async (ctx, req, res) => { + return res.ok({ + body: { result: 'Called deprecated route. Check UA to see the deprecation.' }, + }); + } + ); +}; diff --git a/examples/routing_example/server/routes/deprecated_routes/versioned.ts b/examples/routing_example/server/routes/deprecated_routes/versioned.ts new file mode 100644 index 0000000000000..54d6f779f77c3 --- /dev/null +++ b/examples/routing_example/server/routes/deprecated_routes/versioned.ts @@ -0,0 +1,53 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import type { RequestHandler } from '@kbn/core-http-server'; +import type { IRouter } from '@kbn/core/server'; +import { DEPRECATED_ROUTES } from '../../../common'; + +const createDummyHandler = + (version: string): RequestHandler => + (ctx, req, res) => { + return res.ok({ body: { result: `API version ${version}.` } }); + }; + +export const registerVersionedDeprecatedRoute = (router: IRouter) => { + const versionedRoute = router.versioned.get({ + path: DEPRECATED_ROUTES.VERSIONED_ROUTE, + description: 'Routing example plugin deprecated versioned route.', + access: 'internal', + options: { + excludeFromOAS: true, + }, + enableQueryVersion: true, + }); + + versionedRoute.addVersion( + { + options: { + deprecated: { + documentationUrl: 'https://elastic.co/', + severity: 'warning', + reason: { type: 'bump', newApiVersion: '2' }, + }, + }, + validate: false, + version: '1', + }, + createDummyHandler('1') + ); + + versionedRoute.addVersion( + { + version: '2', + validate: false, + }, + createDummyHandler('2') + ); +}; diff --git a/examples/routing_example/server/routes/index.ts b/examples/routing_example/server/routes/index.ts index 2f43c85b0d471..1c1ccb9e191ff 100644 --- a/examples/routing_example/server/routes/index.ts +++ b/examples/routing_example/server/routes/index.ts @@ -8,3 +8,4 @@ */ export { registerRoutes } from './register_routes'; +export { registerDeprecatedRoutes } from './deprecated_routes'; diff --git a/examples/routing_example/tsconfig.json b/examples/routing_example/tsconfig.json index b35e8dbd34f4a..86bfda9d3d529 100644 --- a/examples/routing_example/tsconfig.json +++ b/examples/routing_example/tsconfig.json @@ -20,5 +20,6 @@ "@kbn/core-http-browser", "@kbn/config-schema", "@kbn/react-kibana-context-render", + "@kbn/core-http-server", ] } diff --git a/oas_docs/bundle.json b/oas_docs/bundle.json index 098fb1de18699..6bca9024e77ea 100644 --- a/oas_docs/bundle.json +++ b/oas_docs/bundle.json @@ -6409,7 +6409,6 @@ }, "/api/fleet/agent-status": { "get": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fagent-status#0", "parameters": [ { @@ -17479,7 +17478,6 @@ ] }, "put": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fagents%2F%7BagentId%7D%2Freassign#0", "parameters": [ { @@ -18179,7 +18177,6 @@ }, "/api/fleet/enrollment-api-keys": { "get": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fenrollment-api-keys#0", "parameters": [ { @@ -18226,7 +18223,6 @@ "tags": [] }, "post": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fenrollment-api-keys#1", "parameters": [ { @@ -18283,7 +18279,6 @@ }, "/api/fleet/enrollment-api-keys/{keyId}": { "delete": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fenrollment-api-keys%2F%7BkeyId%7D#1", "parameters": [ { @@ -18322,7 +18317,6 @@ "tags": [] }, "get": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fenrollment-api-keys%2F%7BkeyId%7D#0", "parameters": [ { @@ -25053,7 +25047,6 @@ }, "/api/fleet/epm/packages/{pkgkey}": { "delete": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#3", "parameters": [ { @@ -25111,7 +25104,6 @@ "tags": [] }, "get": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#0", "parameters": [ { @@ -25173,7 +25165,6 @@ "tags": [] }, "post": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#2", "parameters": [ { @@ -25257,7 +25248,6 @@ "tags": [] }, "put": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#1", "parameters": [ { @@ -40472,7 +40462,6 @@ }, "/api/fleet/service-tokens": { "post": { - "deprecated": true, "description": "Create a service token", "operationId": "%2Fapi%2Ffleet%2Fservice-tokens#0", "parameters": [ diff --git a/oas_docs/bundle.serverless.json b/oas_docs/bundle.serverless.json index 479f79922fea8..a8d428d5404fc 100644 --- a/oas_docs/bundle.serverless.json +++ b/oas_docs/bundle.serverless.json @@ -6409,7 +6409,6 @@ }, "/api/fleet/agent-status": { "get": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fagent-status#0", "parameters": [ { @@ -17479,7 +17478,6 @@ ] }, "put": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fagents%2F%7BagentId%7D%2Freassign#0", "parameters": [ { @@ -18179,7 +18177,6 @@ }, "/api/fleet/enrollment-api-keys": { "get": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fenrollment-api-keys#0", "parameters": [ { @@ -18226,7 +18223,6 @@ "tags": [] }, "post": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fenrollment-api-keys#1", "parameters": [ { @@ -18283,7 +18279,6 @@ }, "/api/fleet/enrollment-api-keys/{keyId}": { "delete": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fenrollment-api-keys%2F%7BkeyId%7D#1", "parameters": [ { @@ -18322,7 +18317,6 @@ "tags": [] }, "get": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fenrollment-api-keys%2F%7BkeyId%7D#0", "parameters": [ { @@ -25053,7 +25047,6 @@ }, "/api/fleet/epm/packages/{pkgkey}": { "delete": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#3", "parameters": [ { @@ -25111,7 +25104,6 @@ "tags": [] }, "get": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#0", "parameters": [ { @@ -25173,7 +25165,6 @@ "tags": [] }, "post": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#2", "parameters": [ { @@ -25257,7 +25248,6 @@ "tags": [] }, "put": { - "deprecated": true, "operationId": "%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#1", "parameters": [ { @@ -40472,7 +40462,6 @@ }, "/api/fleet/service-tokens": { "post": { - "deprecated": true, "description": "Create a service token", "operationId": "%2Fapi%2Ffleet%2Fservice-tokens#0", "parameters": [ diff --git a/oas_docs/output/kibana.serverless.staging.yaml b/oas_docs/output/kibana.serverless.staging.yaml index b1f6938936fbd..3f37d19f8e5e3 100644 --- a/oas_docs/output/kibana.serverless.staging.yaml +++ b/oas_docs/output/kibana.serverless.staging.yaml @@ -15066,7 +15066,6 @@ paths: - Elastic Agents /api/fleet/agent-status: get: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fagent-status#0' parameters: - description: The version of the API to use @@ -16769,7 +16768,6 @@ paths: tags: - Elastic Agent actions put: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fagents%2F%7BagentId%7D%2Freassign#0' parameters: - description: The version of the API to use @@ -18622,7 +18620,6 @@ paths: - Fleet enrollment API keys /api/fleet/enrollment-api-keys: get: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fenrollment-api-keys#0' parameters: - description: The version of the API to use @@ -18654,7 +18651,6 @@ paths: summary: '' tags: [] post: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fenrollment-api-keys#1' parameters: - description: The version of the API to use @@ -18692,7 +18688,6 @@ paths: tags: [] /api/fleet/enrollment-api-keys/{keyId}: delete: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fenrollment-api-keys%2F%7BkeyId%7D#1' parameters: - description: The version of the API to use @@ -18719,7 +18714,6 @@ paths: summary: '' tags: [] get: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fenrollment-api-keys%2F%7BkeyId%7D#0' parameters: - description: The version of the API to use @@ -20434,7 +20428,6 @@ paths: - Elastic Package Manager (EPM) /api/fleet/epm/packages/{pkgkey}: delete: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#3' parameters: - description: The version of the API to use @@ -20473,7 +20466,6 @@ paths: summary: '' tags: [] get: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#0' parameters: - description: The version of the API to use @@ -20514,7 +20506,6 @@ paths: summary: '' tags: [] post: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#2' parameters: - description: The version of the API to use @@ -20570,7 +20561,6 @@ paths: summary: '' tags: [] put: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#1' parameters: - description: The version of the API to use @@ -33533,7 +33523,6 @@ paths: - Fleet service tokens /api/fleet/service-tokens: post: - deprecated: true description: Create a service token operationId: '%2Fapi%2Ffleet%2Fservice-tokens#0' parameters: diff --git a/oas_docs/output/kibana.serverless.yaml b/oas_docs/output/kibana.serverless.yaml index b1f6938936fbd..3f37d19f8e5e3 100644 --- a/oas_docs/output/kibana.serverless.yaml +++ b/oas_docs/output/kibana.serverless.yaml @@ -15066,7 +15066,6 @@ paths: - Elastic Agents /api/fleet/agent-status: get: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fagent-status#0' parameters: - description: The version of the API to use @@ -16769,7 +16768,6 @@ paths: tags: - Elastic Agent actions put: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fagents%2F%7BagentId%7D%2Freassign#0' parameters: - description: The version of the API to use @@ -18622,7 +18620,6 @@ paths: - Fleet enrollment API keys /api/fleet/enrollment-api-keys: get: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fenrollment-api-keys#0' parameters: - description: The version of the API to use @@ -18654,7 +18651,6 @@ paths: summary: '' tags: [] post: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fenrollment-api-keys#1' parameters: - description: The version of the API to use @@ -18692,7 +18688,6 @@ paths: tags: [] /api/fleet/enrollment-api-keys/{keyId}: delete: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fenrollment-api-keys%2F%7BkeyId%7D#1' parameters: - description: The version of the API to use @@ -18719,7 +18714,6 @@ paths: summary: '' tags: [] get: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fenrollment-api-keys%2F%7BkeyId%7D#0' parameters: - description: The version of the API to use @@ -20434,7 +20428,6 @@ paths: - Elastic Package Manager (EPM) /api/fleet/epm/packages/{pkgkey}: delete: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#3' parameters: - description: The version of the API to use @@ -20473,7 +20466,6 @@ paths: summary: '' tags: [] get: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#0' parameters: - description: The version of the API to use @@ -20514,7 +20506,6 @@ paths: summary: '' tags: [] post: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#2' parameters: - description: The version of the API to use @@ -20570,7 +20561,6 @@ paths: summary: '' tags: [] put: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#1' parameters: - description: The version of the API to use @@ -33533,7 +33523,6 @@ paths: - Fleet service tokens /api/fleet/service-tokens: post: - deprecated: true description: Create a service token operationId: '%2Fapi%2Ffleet%2Fservice-tokens#0' parameters: diff --git a/oas_docs/output/kibana.staging.yaml b/oas_docs/output/kibana.staging.yaml index ac76216c78801..76ccd5ef75775 100644 --- a/oas_docs/output/kibana.staging.yaml +++ b/oas_docs/output/kibana.staging.yaml @@ -18495,7 +18495,6 @@ paths: - Elastic Agents /api/fleet/agent-status: get: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fagent-status#0' parameters: - description: The version of the API to use @@ -20198,7 +20197,6 @@ paths: tags: - Elastic Agent actions put: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fagents%2F%7BagentId%7D%2Freassign#0' parameters: - description: The version of the API to use @@ -22051,7 +22049,6 @@ paths: - Fleet enrollment API keys /api/fleet/enrollment-api-keys: get: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fenrollment-api-keys#0' parameters: - description: The version of the API to use @@ -22083,7 +22080,6 @@ paths: summary: '' tags: [] post: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fenrollment-api-keys#1' parameters: - description: The version of the API to use @@ -22121,7 +22117,6 @@ paths: tags: [] /api/fleet/enrollment-api-keys/{keyId}: delete: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fenrollment-api-keys%2F%7BkeyId%7D#1' parameters: - description: The version of the API to use @@ -22148,7 +22143,6 @@ paths: summary: '' tags: [] get: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fenrollment-api-keys%2F%7BkeyId%7D#0' parameters: - description: The version of the API to use @@ -23863,7 +23857,6 @@ paths: - Elastic Package Manager (EPM) /api/fleet/epm/packages/{pkgkey}: delete: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#3' parameters: - description: The version of the API to use @@ -23902,7 +23895,6 @@ paths: summary: '' tags: [] get: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#0' parameters: - description: The version of the API to use @@ -23943,7 +23935,6 @@ paths: summary: '' tags: [] post: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#2' parameters: - description: The version of the API to use @@ -23999,7 +23990,6 @@ paths: summary: '' tags: [] put: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#1' parameters: - description: The version of the API to use @@ -36962,7 +36952,6 @@ paths: - Fleet service tokens /api/fleet/service-tokens: post: - deprecated: true description: Create a service token operationId: '%2Fapi%2Ffleet%2Fservice-tokens#0' parameters: diff --git a/oas_docs/output/kibana.yaml b/oas_docs/output/kibana.yaml index ac76216c78801..76ccd5ef75775 100644 --- a/oas_docs/output/kibana.yaml +++ b/oas_docs/output/kibana.yaml @@ -18495,7 +18495,6 @@ paths: - Elastic Agents /api/fleet/agent-status: get: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fagent-status#0' parameters: - description: The version of the API to use @@ -20198,7 +20197,6 @@ paths: tags: - Elastic Agent actions put: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fagents%2F%7BagentId%7D%2Freassign#0' parameters: - description: The version of the API to use @@ -22051,7 +22049,6 @@ paths: - Fleet enrollment API keys /api/fleet/enrollment-api-keys: get: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fenrollment-api-keys#0' parameters: - description: The version of the API to use @@ -22083,7 +22080,6 @@ paths: summary: '' tags: [] post: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fenrollment-api-keys#1' parameters: - description: The version of the API to use @@ -22121,7 +22117,6 @@ paths: tags: [] /api/fleet/enrollment-api-keys/{keyId}: delete: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fenrollment-api-keys%2F%7BkeyId%7D#1' parameters: - description: The version of the API to use @@ -22148,7 +22143,6 @@ paths: summary: '' tags: [] get: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fenrollment-api-keys%2F%7BkeyId%7D#0' parameters: - description: The version of the API to use @@ -23863,7 +23857,6 @@ paths: - Elastic Package Manager (EPM) /api/fleet/epm/packages/{pkgkey}: delete: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#3' parameters: - description: The version of the API to use @@ -23902,7 +23895,6 @@ paths: summary: '' tags: [] get: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#0' parameters: - description: The version of the API to use @@ -23943,7 +23935,6 @@ paths: summary: '' tags: [] post: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#2' parameters: - description: The version of the API to use @@ -23999,7 +23990,6 @@ paths: summary: '' tags: [] put: - deprecated: true operationId: '%2Fapi%2Ffleet%2Fepm%2Fpackages%2F%7Bpkgkey%7D#1' parameters: - description: The version of the API to use @@ -36962,7 +36952,6 @@ paths: - Fleet service tokens /api/fleet/service-tokens: post: - deprecated: true description: Create a service token operationId: '%2Fapi%2Ffleet%2Fservice-tokens#0' parameters: diff --git a/packages/core/deprecations/core-deprecations-browser-internal/src/deprecations_client.test.ts b/packages/core/deprecations/core-deprecations-browser-internal/src/deprecations_client.test.ts index bfb4f35fa93f1..38777474f136b 100644 --- a/packages/core/deprecations/core-deprecations-browser-internal/src/deprecations_client.test.ts +++ b/packages/core/deprecations/core-deprecations-browser-internal/src/deprecations_client.test.ts @@ -135,7 +135,7 @@ describe('DeprecationsClient', () => { expect(result).toMatchInlineSnapshot(` Object { - "reason": "This deprecation cannot be resolved automatically.", + "reason": "This deprecation cannot be resolved automatically or marked as resolved.", "status": "fail", } `); diff --git a/packages/core/deprecations/core-deprecations-browser-internal/src/deprecations_client.ts b/packages/core/deprecations/core-deprecations-browser-internal/src/deprecations_client.ts index f1f473d726b7e..bdcad406995a9 100644 --- a/packages/core/deprecations/core-deprecations-browser-internal/src/deprecations_client.ts +++ b/packages/core/deprecations/core-deprecations-browser-internal/src/deprecations_client.ts @@ -8,7 +8,7 @@ */ import { i18n } from '@kbn/i18n'; -import type { HttpStart } from '@kbn/core-http-browser'; +import type { HttpFetchOptionsWithPath, HttpStart } from '@kbn/core-http-browser'; import type { DomainDeprecationDetails, DeprecationsGetResponse, @@ -47,23 +47,15 @@ export class DeprecationsClient { return typeof details.correctiveActions.api === 'object'; }; - public resolveDeprecation = async ( + private getResolveFetchDetails = ( details: DomainDeprecationDetails - ): Promise => { + ): HttpFetchOptionsWithPath | undefined => { const { domainId, correctiveActions } = details; - // explicit check required for TS type guard - if (typeof correctiveActions.api !== 'object') { - return { - status: 'fail', - reason: i18n.translate('core.deprecations.noCorrectiveAction', { - defaultMessage: 'This deprecation cannot be resolved automatically.', - }), - }; - } - const { body, method, path, omitContextFromBody = false } = correctiveActions.api; - try { - await this.http.fetch({ + if (correctiveActions.api) { + const { body, method, path, omitContextFromBody = false } = correctiveActions.api; + + return { path, method, asSystemRequest: true, @@ -71,7 +63,54 @@ export class DeprecationsClient { ...body, ...(omitContextFromBody ? {} : { deprecationDetails: { domainId } }), }), - }); + }; + } + + if (correctiveActions.mark_as_resolved_api) { + const { routeMethod, routePath, routeVersion, apiTotalCalls, totalMarkedAsResolved } = + correctiveActions.mark_as_resolved_api; + const incrementBy = apiTotalCalls - totalMarkedAsResolved; + + return { + path: '/api/deprecations/mark_as_resolved', + method: 'POST', + asSystemRequest: true, + body: JSON.stringify({ + domainId, + routeMethod, + routePath, + routeVersion, + incrementBy, + }), + }; + } + }; + + public resolveDeprecation = async ( + details: DomainDeprecationDetails + ): Promise => { + const { correctiveActions } = details; + const noCorrectiveActionFail = { + status: 'fail' as const, + reason: i18n.translate('core.deprecations.noCorrectiveAction', { + defaultMessage: 'This deprecation cannot be resolved automatically or marked as resolved.', + }), + }; + + if ( + typeof correctiveActions.api !== 'object' && + typeof correctiveActions.mark_as_resolved_api !== 'object' + ) { + return noCorrectiveActionFail; + } + + try { + const fetchParams = this.getResolveFetchDetails(details); + if (!fetchParams) { + return noCorrectiveActionFail; + } + + await this.http.fetch(fetchParams); return { status: 'ok' }; } catch (err) { return { diff --git a/packages/core/deprecations/core-deprecations-common/src/types.ts b/packages/core/deprecations/core-deprecations-common/src/types.ts index bf9a4a673d721..85da30b2c1287 100644 --- a/packages/core/deprecations/core-deprecations-common/src/types.ts +++ b/packages/core/deprecations/core-deprecations-common/src/types.ts @@ -22,7 +22,7 @@ export interface BaseDeprecationDetails { * The description message to be displayed for the deprecation. * Check the README for writing deprecations in `src/core/server/deprecations/README.mdx` */ - message: string; + message: string | string[]; /** * levels: * - warning: will not break deployment upon upgrade @@ -39,7 +39,7 @@ export interface BaseDeprecationDetails { * Predefined types are necessary to reduce having similar definitions with different keywords * across kibana deprecations. */ - deprecationType?: 'config' | 'feature'; + deprecationType?: 'config' | 'api' | 'feature'; /** (optional) link to the documentation for more details on the deprecation. */ documentationUrl?: string; /** (optional) specify the fix for this deprecation requires a full kibana restart. */ @@ -70,9 +70,31 @@ export interface BaseDeprecationDetails { * Check the README for writing deprecations in `src/core/server/deprecations/README.mdx` */ manualSteps: string[]; + /** + * (optional) The api to be called to mark the deprecation as resolved + * This corrective action when called should not resolve the deprecation + * instead it helps users track manually deprecated apis + * If the API used does resolve the deprecation use `correctiveActions.api` + */ + mark_as_resolved_api?: { + apiTotalCalls: number; + totalMarkedAsResolved: number; + timestamp: Date | number | string; + routePath: string; + routeMethod: string; + routeVersion?: string; + }; }; } +/** + * @public + */ +export interface ApiDeprecationDetails extends BaseDeprecationDetails { + apiId: string; + deprecationType: 'api'; +} + /** * @public */ @@ -91,7 +113,10 @@ export interface FeatureDeprecationDetails extends BaseDeprecationDetails { /** * @public */ -export type DeprecationsDetails = ConfigDeprecationDetails | FeatureDeprecationDetails; +export type DeprecationsDetails = + | ConfigDeprecationDetails + | ApiDeprecationDetails + | FeatureDeprecationDetails; /** * @public diff --git a/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/api_deprecations.test.ts b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/api_deprecations.test.ts new file mode 100644 index 0000000000000..b431088152f3e --- /dev/null +++ b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/api_deprecations.test.ts @@ -0,0 +1,353 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import type { DeepPartial } from '@kbn/utility-types'; +import { mockDeprecationsRegistry, mockDeprecationsFactory } from '../mocks'; +import { + registerApiDeprecationsInfo, + buildApiDeprecationId, + createGetApiDeprecations, +} from './api_deprecations'; +import { RouterDeprecatedRouteDetails } from '@kbn/core-http-server'; +import { httpServiceMock } from '@kbn/core-http-server-mocks'; +import { + coreUsageDataServiceMock, + coreUsageStatsClientMock, +} from '@kbn/core-usage-data-server-mocks'; +import _ from 'lodash'; +import { CoreDeprecatedApiUsageStats } from '@kbn/core-usage-data-server'; + +describe('#registerApiDeprecationsInfo', () => { + const deprecationsFactory = mockDeprecationsFactory.create(); + const deprecationsRegistry = mockDeprecationsRegistry.create(); + let usageClientMock: ReturnType; + let http: ReturnType; + let coreUsageData: ReturnType; + + beforeEach(() => { + jest.clearAllMocks(); + usageClientMock = coreUsageStatsClientMock.create(); + http = httpServiceMock.createInternalSetupContract(); + coreUsageData = coreUsageDataServiceMock.createSetupContract(usageClientMock); + }); + + beforeAll(() => { + jest.useFakeTimers().setSystemTime(new Date('2024-10-17T12:06:41.224Z')); + }); + + afterAll(() => { + jest.useRealTimers(); + }); + + it('registers api deprecations', async () => { + deprecationsFactory.getRegistry.mockReturnValue(deprecationsRegistry); + registerApiDeprecationsInfo({ deprecationsFactory, coreUsageData, http }); + + expect(deprecationsFactory.getRegistry).toBeCalledWith('core.api_deprecations'); + expect(deprecationsRegistry.registerDeprecations).toBeCalledTimes(1); + expect(deprecationsRegistry.registerDeprecations).toBeCalledWith({ + getDeprecations: expect.any(Function), + }); + }); + + describe('#createGetApiDeprecations', () => { + const createDeprecatedRouteDetails = ( + overrides?: DeepPartial + ): RouterDeprecatedRouteDetails => + _.merge( + { + routeDeprecationOptions: { + documentationUrl: 'https://fake-url', + severity: 'critical', + reason: { + type: 'remove', + }, + }, + routeMethod: 'get', + routePath: '/api/test/', + routeVersion: '123', + } as RouterDeprecatedRouteDetails, + overrides + ); + + const createApiUsageStat = ( + apiId: string, + overrides?: DeepPartial + ): CoreDeprecatedApiUsageStats => + _.merge( + { + apiId, + totalMarkedAsResolved: 1, + markedAsResolvedLastCalledAt: '2024-10-17T12:06:41.224Z', + apiTotalCalls: 13, + apiLastCalledAt: '2024-09-01T10:06:41.224Z', + }, + overrides + ); + + it('returns removed type deprecated route', async () => { + const getDeprecations = createGetApiDeprecations({ coreUsageData, http }); + const deprecatedRoute = createDeprecatedRouteDetails({ + routePath: '/api/test_removed/', + routeDeprecationOptions: { reason: { type: 'remove' } }, + }); + http.getRegisteredDeprecatedApis.mockReturnValue([deprecatedRoute]); + usageClientMock.getDeprecatedApiUsageStats.mockResolvedValue([ + createApiUsageStat(buildApiDeprecationId(deprecatedRoute)), + ]); + + const deprecations = await getDeprecations(); + expect(deprecations).toMatchInlineSnapshot(` + Array [ + Object { + "apiId": "123|get|/api/test_removed", + "correctiveActions": Object { + "manualSteps": Array [ + "Identify the origin of these API calls.", + "This API no longer exists and no replacement is available. Delete any requests you have that use this API.", + "Check that you are no longer using the old API in any requests, and mark this issue as resolved. It will no longer appear in the Upgrade Assistant unless another call using this API is detected.", + ], + "mark_as_resolved_api": Object { + "apiTotalCalls": 13, + "routeMethod": "get", + "routePath": "/api/test_removed/", + "routeVersion": "123", + "timestamp": 2024-10-17T12:06:41.224Z, + "totalMarkedAsResolved": 1, + }, + }, + "deprecationType": "api", + "documentationUrl": "https://fake-url", + "domainId": "core.routes-deprecations", + "level": "critical", + "message": Array [ + "The API \\"GET /api/test_removed/\\" has been called 13 times. The last call was on Sunday, September 1, 2024 6:06 AM -04:00.", + "This issue has been marked as resolved on Thursday, October 17, 2024 8:06 AM -04:00 but the API has been called 12 times since.", + ], + "title": "The \\"GET /api/test_removed/\\" route is removed", + }, + ] + `); + }); + + it('returns migrated type deprecated route', async () => { + const getDeprecations = createGetApiDeprecations({ coreUsageData, http }); + const deprecatedRoute = createDeprecatedRouteDetails({ + routePath: '/api/test_migrated/', + routeDeprecationOptions: { + reason: { type: 'migrate', newApiMethod: 'post', newApiPath: '/api/new_path' }, + }, + }); + http.getRegisteredDeprecatedApis.mockReturnValue([deprecatedRoute]); + usageClientMock.getDeprecatedApiUsageStats.mockResolvedValue([ + createApiUsageStat(buildApiDeprecationId(deprecatedRoute)), + ]); + + const deprecations = await getDeprecations(); + expect(deprecations).toMatchInlineSnapshot(` + Array [ + Object { + "apiId": "123|get|/api/test_migrated", + "correctiveActions": Object { + "manualSteps": Array [ + "Identify the origin of these API calls.", + "Update the requests to use the following new API instead: \\"POST /api/new_path\\".", + "Check that you are no longer using the old API in any requests, and mark this issue as resolved. It will no longer appear in the Upgrade Assistant unless another call using this API is detected.", + ], + "mark_as_resolved_api": Object { + "apiTotalCalls": 13, + "routeMethod": "get", + "routePath": "/api/test_migrated/", + "routeVersion": "123", + "timestamp": 2024-10-17T12:06:41.224Z, + "totalMarkedAsResolved": 1, + }, + }, + "deprecationType": "api", + "documentationUrl": "https://fake-url", + "domainId": "core.routes-deprecations", + "level": "critical", + "message": Array [ + "The API \\"GET /api/test_migrated/\\" has been called 13 times. The last call was on Sunday, September 1, 2024 6:06 AM -04:00.", + "This issue has been marked as resolved on Thursday, October 17, 2024 8:06 AM -04:00 but the API has been called 12 times since.", + ], + "title": "The \\"GET /api/test_migrated/\\" route is migrated to a different API", + }, + ] + `); + }); + + it('returns bumped type deprecated route', async () => { + const getDeprecations = createGetApiDeprecations({ coreUsageData, http }); + const deprecatedRoute = createDeprecatedRouteDetails({ + routePath: '/api/test_bumped/', + routeDeprecationOptions: { reason: { type: 'bump', newApiVersion: '444' } }, + }); + http.getRegisteredDeprecatedApis.mockReturnValue([deprecatedRoute]); + usageClientMock.getDeprecatedApiUsageStats.mockResolvedValue([ + createApiUsageStat(buildApiDeprecationId(deprecatedRoute)), + ]); + + const deprecations = await getDeprecations(); + expect(deprecations).toMatchInlineSnapshot(` + Array [ + Object { + "apiId": "123|get|/api/test_bumped", + "correctiveActions": Object { + "manualSteps": Array [ + "Identify the origin of these API calls.", + "Update the requests to use the following new version of the API instead: \\"444\\".", + "Check that you are no longer using the old API in any requests, and mark this issue as resolved. It will no longer appear in the Upgrade Assistant unless another call using this API is detected.", + ], + "mark_as_resolved_api": Object { + "apiTotalCalls": 13, + "routeMethod": "get", + "routePath": "/api/test_bumped/", + "routeVersion": "123", + "timestamp": 2024-10-17T12:06:41.224Z, + "totalMarkedAsResolved": 1, + }, + }, + "deprecationType": "api", + "documentationUrl": "https://fake-url", + "domainId": "core.routes-deprecations", + "level": "critical", + "message": Array [ + "The API \\"GET /api/test_bumped/\\" has been called 13 times. The last call was on Sunday, September 1, 2024 6:06 AM -04:00.", + "This issue has been marked as resolved on Thursday, October 17, 2024 8:06 AM -04:00 but the API has been called 12 times since.", + ], + "title": "The \\"GET /api/test_bumped/\\" route has a newer version available", + }, + ] + `); + }); + + it('does not return resolved deprecated route', async () => { + const getDeprecations = createGetApiDeprecations({ coreUsageData, http }); + const deprecatedRoute = createDeprecatedRouteDetails({ routePath: '/api/test_resolved/' }); + http.getRegisteredDeprecatedApis.mockReturnValue([deprecatedRoute]); + usageClientMock.getDeprecatedApiUsageStats.mockResolvedValue([ + createApiUsageStat(buildApiDeprecationId(deprecatedRoute), { + apiTotalCalls: 5, + totalMarkedAsResolved: 5, + }), + ]); + + const deprecations = await getDeprecations(); + expect(deprecations).toEqual([]); + }); + + it('returns never resolved deprecated route', async () => { + const getDeprecations = createGetApiDeprecations({ coreUsageData, http }); + const deprecatedRoute = createDeprecatedRouteDetails({ + routePath: '/api/test_never_resolved/', + }); + http.getRegisteredDeprecatedApis.mockReturnValue([deprecatedRoute]); + usageClientMock.getDeprecatedApiUsageStats.mockResolvedValue([ + createApiUsageStat(buildApiDeprecationId(deprecatedRoute), { + totalMarkedAsResolved: 0, + markedAsResolvedLastCalledAt: undefined, + }), + ]); + + const deprecations = await getDeprecations(); + expect(deprecations).toMatchInlineSnapshot(` + Array [ + Object { + "apiId": "123|get|/api/test_never_resolved", + "correctiveActions": Object { + "manualSteps": Array [ + "Identify the origin of these API calls.", + "This API no longer exists and no replacement is available. Delete any requests you have that use this API.", + "Check that you are no longer using the old API in any requests, and mark this issue as resolved. It will no longer appear in the Upgrade Assistant unless another call using this API is detected.", + ], + "mark_as_resolved_api": Object { + "apiTotalCalls": 13, + "routeMethod": "get", + "routePath": "/api/test_never_resolved/", + "routeVersion": "123", + "timestamp": 2024-10-17T12:06:41.224Z, + "totalMarkedAsResolved": 0, + }, + }, + "deprecationType": "api", + "documentationUrl": "https://fake-url", + "domainId": "core.routes-deprecations", + "level": "critical", + "message": Array [ + "The API \\"GET /api/test_never_resolved/\\" has been called 13 times. The last call was on Sunday, September 1, 2024 6:06 AM -04:00.", + ], + "title": "The \\"GET /api/test_never_resolved/\\" route is removed", + }, + ] + `); + }); + + it('does not return deprecated routes that have never been called', async () => { + const getDeprecations = createGetApiDeprecations({ coreUsageData, http }); + const deprecatedRoute = createDeprecatedRouteDetails({ + routePath: '/api/test_never_resolved/', + }); + http.getRegisteredDeprecatedApis.mockReturnValue([deprecatedRoute]); + usageClientMock.getDeprecatedApiUsageStats.mockResolvedValue([]); + expect(await getDeprecations()).toEqual([]); + + usageClientMock.getDeprecatedApiUsageStats.mockResolvedValue([ + createApiUsageStat(buildApiDeprecationId(deprecatedRoute), { + apiTotalCalls: 0, + apiLastCalledAt: undefined, + totalMarkedAsResolved: 0, + markedAsResolvedLastCalledAt: undefined, + }), + ]); + expect(await getDeprecations()).toEqual([]); + }); + }); +}); + +describe('#buildApiDeprecationId', () => { + it('returns apiDeprecationId string for versioned routes', () => { + const apiDeprecationId = buildApiDeprecationId({ + routeMethod: 'get', + routePath: '/api/test', + routeVersion: '10-10-2023', + }); + expect(apiDeprecationId).toBe('10-10-2023|get|/api/test'); + }); + + it('returns apiDeprecationId string for unversioned routes', () => { + const apiDeprecationId = buildApiDeprecationId({ + routeMethod: 'get', + routePath: '/api/test', + }); + expect(apiDeprecationId).toBe('unversioned|get|/api/test'); + }); + + it('gives the same ID the route method is capitalized or not', () => { + const apiDeprecationId = buildApiDeprecationId({ + // @ts-expect-error + routeMethod: 'GeT', + routePath: '/api/test', + routeVersion: '10-10-2023', + }); + + expect(apiDeprecationId).toBe('10-10-2023|get|/api/test'); + }); + + it('gives the same ID the route path has a trailing slash or not', () => { + const apiDeprecationId = buildApiDeprecationId({ + // @ts-expect-error + routeMethod: 'GeT', + routePath: '/api/test/', + routeVersion: '10-10-2023', + }); + + expect(apiDeprecationId).toBe('10-10-2023|get|/api/test'); + }); +}); diff --git a/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/api_deprecations.ts b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/api_deprecations.ts new file mode 100644 index 0000000000000..45893987ddf92 --- /dev/null +++ b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/api_deprecations.ts @@ -0,0 +1,96 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import type { InternalHttpServiceSetup } from '@kbn/core-http-server-internal'; +import type { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-server-internal'; +import type { RouterDeprecatedRouteDetails } from '@kbn/core-http-server'; +import { DeprecationsDetails } from '@kbn/core-deprecations-common'; +import type { DeprecationsFactory } from '../deprecations_factory'; +import { + getApiDeprecationMessage, + getApiDeprecationsManualSteps, + getApiDeprecationTitle, +} from './i18n_texts'; + +interface ApiDeprecationsServiceDeps { + deprecationsFactory: DeprecationsFactory; + http: InternalHttpServiceSetup; + coreUsageData: InternalCoreUsageDataSetup; +} + +export const buildApiDeprecationId = ({ + routePath, + routeMethod, + routeVersion, +}: Pick): string => { + return [ + routeVersion || 'unversioned', + routeMethod.toLocaleLowerCase(), + routePath.replace(/\/$/, ''), + ].join('|'); +}; + +export const createGetApiDeprecations = + ({ http, coreUsageData }: Pick) => + async (): Promise => { + const deprecatedRoutes = http.getRegisteredDeprecatedApis(); + const usageClient = coreUsageData.getClient(); + const deprecatedApiUsageStats = await usageClient.getDeprecatedApiUsageStats(); + + return deprecatedApiUsageStats + .filter(({ apiTotalCalls, totalMarkedAsResolved }) => { + return apiTotalCalls > totalMarkedAsResolved; + }) + .filter(({ apiId }) => + deprecatedRoutes.some((routeDetails) => buildApiDeprecationId(routeDetails) === apiId) + ) + .map((apiUsageStats) => { + const { apiId, apiTotalCalls, totalMarkedAsResolved } = apiUsageStats; + const routeDeprecationDetails = deprecatedRoutes.find( + (routeDetails) => buildApiDeprecationId(routeDetails) === apiId + )!; + const { routeVersion, routePath, routeDeprecationOptions, routeMethod } = + routeDeprecationDetails; + + const deprecationLevel = routeDeprecationOptions.severity || 'warning'; + + return { + apiId, + title: getApiDeprecationTitle(routeDeprecationDetails), + level: deprecationLevel, + message: getApiDeprecationMessage(routeDeprecationDetails, apiUsageStats), + documentationUrl: routeDeprecationOptions.documentationUrl, + correctiveActions: { + manualSteps: getApiDeprecationsManualSteps(routeDeprecationDetails), + mark_as_resolved_api: { + routePath, + routeMethod, + routeVersion, + apiTotalCalls, + totalMarkedAsResolved, + timestamp: new Date(), + }, + }, + deprecationType: 'api', + domainId: 'core.routes-deprecations', + }; + }); + }; + +export const registerApiDeprecationsInfo = ({ + deprecationsFactory, + http, + coreUsageData, +}: ApiDeprecationsServiceDeps): void => { + const deprecationsRegistery = deprecationsFactory.getRegistry('core.api_deprecations'); + + deprecationsRegistery.registerDeprecations({ + getDeprecations: createGetApiDeprecations({ http, coreUsageData }), + }); +}; diff --git a/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/config_deprecations.test.ts b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/config_deprecations.test.ts new file mode 100644 index 0000000000000..92d0703c8037e --- /dev/null +++ b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/config_deprecations.test.ts @@ -0,0 +1,115 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { registerConfigDeprecationsInfo } from './config_deprecations'; +import { mockDeprecationsRegistry, mockDeprecationsFactory } from '../mocks'; +import { mockCoreContext } from '@kbn/core-base-server-mocks'; +import { configServiceMock } from '@kbn/config-mocks'; + +describe('#registerConfigDeprecationsInfo', () => { + let coreContext: ReturnType; + + const deprecationsFactory = mockDeprecationsFactory.create(); + const deprecationsRegistry = mockDeprecationsRegistry.create(); + const getDeprecationsContext = mockDeprecationsRegistry.createGetDeprecationsContext(); + + beforeEach(() => { + const configService = configServiceMock.create({ + atPath: { skip_deprecated_settings: ['hello', 'world'] }, + }); + jest.clearAllMocks(); + coreContext = mockCoreContext.create({ configService }); + }); + + it('registers config deprecations', async () => { + coreContext.configService.getHandledDeprecatedConfigs.mockReturnValue([ + [ + 'testDomain', + [ + { + configPath: 'test', + level: 'critical', + message: 'testMessage', + documentationUrl: 'testDocUrl', + correctiveActions: { + manualSteps: [ + 'Using Kibana user management, change all users using the kibana_user role to the kibana_admin role.', + 'Using Kibana role-mapping management, change all role-mappings which assign the kibana_user role to the kibana_admin role.', + ], + }, + }, + ], + ], + ]); + + deprecationsFactory.getRegistry.mockReturnValue(deprecationsRegistry); + registerConfigDeprecationsInfo({ + deprecationsFactory, + configService: coreContext.configService, + }); + + expect(coreContext.configService.getHandledDeprecatedConfigs).toBeCalledTimes(1); + expect(deprecationsFactory.getRegistry).toBeCalledTimes(1); + expect(deprecationsFactory.getRegistry).toBeCalledWith('testDomain'); + expect(deprecationsRegistry.registerDeprecations).toBeCalledTimes(1); + const configDeprecations = + await deprecationsRegistry.registerDeprecations.mock.calls[0][0].getDeprecations( + getDeprecationsContext + ); + expect(configDeprecations).toMatchInlineSnapshot(` + Array [ + Object { + "configPath": "test", + "correctiveActions": Object { + "manualSteps": Array [ + "Using Kibana user management, change all users using the kibana_user role to the kibana_admin role.", + "Using Kibana role-mapping management, change all role-mappings which assign the kibana_user role to the kibana_admin role.", + ], + }, + "deprecationType": "config", + "documentationUrl": "testDocUrl", + "level": "critical", + "message": "testMessage", + "requireRestart": true, + "title": "testDomain has a deprecated setting", + }, + ] + `); + }); + + it('accepts `level` field overrides', async () => { + coreContext.configService.getHandledDeprecatedConfigs.mockReturnValue([ + [ + 'testDomain', + [ + { + configPath: 'test', + message: 'testMessage', + level: 'warning', + correctiveActions: { + manualSteps: ['step a'], + }, + }, + ], + ], + ]); + + deprecationsFactory.getRegistry.mockReturnValue(deprecationsRegistry); + registerConfigDeprecationsInfo({ + deprecationsFactory, + configService: coreContext.configService, + }); + + const configDeprecations = + await deprecationsRegistry.registerDeprecations.mock.calls[0][0].getDeprecations( + getDeprecationsContext + ); + expect(configDeprecations[0].level).toBe('warning'); + }); +}); diff --git a/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/config_deprecations.ts b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/config_deprecations.ts new file mode 100644 index 0000000000000..f9df0edacd9d1 --- /dev/null +++ b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/config_deprecations.ts @@ -0,0 +1,50 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import type { IConfigService } from '@kbn/config'; +import { DeprecationsFactory } from '../deprecations_factory'; + +interface RegisterConfigDeprecationsInfo { + deprecationsFactory: DeprecationsFactory; + configService: IConfigService; +} + +export const registerConfigDeprecationsInfo = ({ + deprecationsFactory, + configService, +}: RegisterConfigDeprecationsInfo) => { + const handledDeprecatedConfigs = configService.getHandledDeprecatedConfigs(); + + for (const [domainId, deprecationsContexts] of handledDeprecatedConfigs) { + const deprecationsRegistry = deprecationsFactory.getRegistry(domainId); + deprecationsRegistry.registerDeprecations({ + getDeprecations: () => { + return deprecationsContexts.map( + ({ + configPath, + title = `${domainId} has a deprecated setting`, + level, + message, + correctiveActions, + documentationUrl, + }) => ({ + configPath, + title, + level, + message, + correctiveActions, + documentationUrl, + deprecationType: 'config', + requireRestart: true, + }) + ); + }, + }); + } +}; diff --git a/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/i18n_texts.ts b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/i18n_texts.ts new file mode 100644 index 0000000000000..cb1dacc97bd91 --- /dev/null +++ b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/i18n_texts.ts @@ -0,0 +1,132 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { RouterDeprecatedRouteDetails } from '@kbn/core-http-server'; +import { CoreDeprecatedApiUsageStats } from '@kbn/core-usage-data-server'; +import { i18n } from '@kbn/i18n'; +import moment from 'moment'; + +export const getApiDeprecationTitle = (details: RouterDeprecatedRouteDetails) => { + const { routePath, routeMethod, routeDeprecationOptions } = details; + const deprecationType = routeDeprecationOptions.reason.type; + const routeWithMethod = `${routeMethod.toUpperCase()} ${routePath}`; + const deprecationTypeText = i18n.translate('core.deprecations.deprecations.apiDeprecationType', { + defaultMessage: + '{deprecationType, select, remove {is removed} bump {has a newer version available} migrate {is migrated to a different API} other {is deprecated}}', + values: { deprecationType }, + }); + + return i18n.translate('core.deprecations.deprecations.apiDeprecationInfoTitle', { + defaultMessage: 'The "{routeWithMethod}" route {deprecationTypeText}', + values: { + routeWithMethod, + deprecationTypeText, + }, + }); +}; + +export const getApiDeprecationMessage = ( + details: RouterDeprecatedRouteDetails, + apiUsageStats: CoreDeprecatedApiUsageStats +): string[] => { + const { routePath, routeMethod } = details; + const { apiLastCalledAt, apiTotalCalls, markedAsResolvedLastCalledAt, totalMarkedAsResolved } = + apiUsageStats; + + const diff = apiTotalCalls - totalMarkedAsResolved; + const wasResolvedBefore = totalMarkedAsResolved > 0; + const routeWithMethod = `${routeMethod.toUpperCase()} ${routePath}`; + + const messages = [ + i18n.translate('core.deprecations.deprecations.apiDeprecationApiCallsDetailsMessage', { + defaultMessage: + 'The API "{routeWithMethod}" has been called {apiTotalCalls} times. The last call was on {apiLastCalledAt}.', + values: { + routeWithMethod, + apiTotalCalls, + apiLastCalledAt: moment(apiLastCalledAt).format('LLLL Z'), + }, + }), + ]; + + if (wasResolvedBefore) { + messages.push( + i18n.translate( + 'core.deprecations.deprecations.apiDeprecationPreviouslyMarkedAsResolvedMessage', + { + defaultMessage: + 'This issue has been marked as resolved on {markedAsResolvedLastCalledAt} but the API has been called {timeSinceLastResolved, plural, one {# time} other {# times}} since.', + values: { + timeSinceLastResolved: diff, + markedAsResolvedLastCalledAt: moment(markedAsResolvedLastCalledAt).format('LLLL Z'), + }, + } + ) + ); + } + + return messages; +}; + +export const getApiDeprecationsManualSteps = (details: RouterDeprecatedRouteDetails): string[] => { + const { routeDeprecationOptions } = details; + const deprecationType = routeDeprecationOptions.reason.type; + + const manualSteps = [ + i18n.translate('core.deprecations.deprecations.manualSteps.apiIseprecatedStep', { + defaultMessage: 'Identify the origin of these API calls.', + }), + ]; + + switch (deprecationType) { + case 'bump': { + const { newApiVersion } = routeDeprecationOptions.reason; + manualSteps.push( + i18n.translate('core.deprecations.deprecations.manualSteps.bumpDetailsStep', { + defaultMessage: + 'Update the requests to use the following new version of the API instead: "{newApiVersion}".', + values: { newApiVersion }, + }) + ); + break; + } + + case 'remove': { + manualSteps.push( + i18n.translate('core.deprecations.deprecations.manualSteps.removeTypeExplainationStep', { + defaultMessage: + 'This API no longer exists and no replacement is available. Delete any requests you have that use this API.', + }) + ); + break; + } + case 'migrate': { + const { newApiPath, newApiMethod } = routeDeprecationOptions.reason; + const newRouteWithMethod = `${newApiMethod.toUpperCase()} ${newApiPath}`; + + manualSteps.push( + i18n.translate('core.deprecations.deprecations.manualSteps.migrateDetailsStep', { + defaultMessage: + 'Update the requests to use the following new API instead: "{newRouteWithMethod}".', + values: { newRouteWithMethod }, + }) + ); + break; + } + } + + manualSteps.push( + i18n.translate('core.deprecations.deprecations.manualSteps.markAsResolvedStep', { + defaultMessage: + 'Check that you are no longer using the old API in any requests, and mark this issue as resolved. It will no longer appear in the Upgrade Assistant unless another call using this API is detected.', + }) + ); + + return manualSteps; +}; diff --git a/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/index.ts b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/index.ts new file mode 100644 index 0000000000000..aecf3d5b299a2 --- /dev/null +++ b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations/index.ts @@ -0,0 +1,11 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +export { buildApiDeprecationId, registerApiDeprecationsInfo } from './api_deprecations'; +export { registerConfigDeprecationsInfo } from './config_deprecations'; diff --git a/packages/core/deprecations/core-deprecations-server-internal/src/deprecations_service.test.mocks.ts b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations_service.test.mocks.ts index 9ce9f52fb7a50..93550539343e3 100644 --- a/packages/core/deprecations/core-deprecations-server-internal/src/deprecations_service.test.mocks.ts +++ b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations_service.test.mocks.ts @@ -14,6 +14,14 @@ export const DeprecationsFactoryMock = jest .fn() .mockImplementation(() => mockedDeprecationFactoryInstance); +export const registerConfigDeprecationsInfoMock = jest.fn(); +export const registerApiDeprecationsInfoMock = jest.fn(); + +jest.doMock('./deprecations', () => ({ + registerConfigDeprecationsInfo: registerConfigDeprecationsInfoMock, + registerApiDeprecationsInfo: registerApiDeprecationsInfoMock, +})); + jest.doMock('./deprecations_factory', () => ({ DeprecationsFactory: DeprecationsFactoryMock, })); diff --git a/packages/core/deprecations/core-deprecations-server-internal/src/deprecations_service.test.ts b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations_service.test.ts index 14a131ca8e563..39c299d980531 100644 --- a/packages/core/deprecations/core-deprecations-server-internal/src/deprecations_service.test.ts +++ b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations_service.test.ts @@ -7,22 +7,24 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import { DeprecationsFactoryMock } from './deprecations_service.test.mocks'; - +import { + DeprecationsFactoryMock, + registerConfigDeprecationsInfoMock, +} from './deprecations_service.test.mocks'; import { mockCoreContext } from '@kbn/core-base-server-mocks'; import { httpServiceMock } from '@kbn/core-http-server-mocks'; +import { coreUsageDataServiceMock } from '@kbn/core-usage-data-server-mocks'; import { configServiceMock } from '@kbn/config-mocks'; import { savedObjectsClientMock } from '@kbn/core-saved-objects-api-server-mocks'; import { elasticsearchServiceMock } from '@kbn/core-elasticsearch-server-mocks'; import { DeprecationsService, DeprecationsSetupDeps } from './deprecations_service'; -import { mockDeprecationsRegistry, mockDeprecationsFactory } from './mocks'; -/* eslint-disable dot-notation */ describe('DeprecationsService', () => { let coreContext: ReturnType; let http: ReturnType; let router: ReturnType; let deprecationsCoreSetupDeps: DeprecationsSetupDeps; + let coreUsageData: ReturnType; beforeEach(() => { const configService = configServiceMock.create({ @@ -30,14 +32,16 @@ describe('DeprecationsService', () => { }); coreContext = mockCoreContext.create({ configService }); http = httpServiceMock.createInternalSetupContract(); + coreUsageData = coreUsageDataServiceMock.createSetupContract(); router = httpServiceMock.createRouter(); http.createRouter.mockReturnValue(router); - deprecationsCoreSetupDeps = { http }; + deprecationsCoreSetupDeps = { http, coreUsageData }; }); afterEach(() => { jest.clearAllMocks(); DeprecationsFactoryMock.mockClear(); + registerConfigDeprecationsInfoMock.mockClear(); }); describe('#setup', () => { @@ -53,10 +57,8 @@ describe('DeprecationsService', () => { it('calls registerConfigDeprecationsInfo', async () => { const deprecationsService = new DeprecationsService(coreContext); - const mockRegisterConfigDeprecationsInfo = jest.fn(); - deprecationsService['registerConfigDeprecationsInfo'] = mockRegisterConfigDeprecationsInfo; await deprecationsService.setup(deprecationsCoreSetupDeps); - expect(mockRegisterConfigDeprecationsInfo).toBeCalledTimes(1); + expect(registerConfigDeprecationsInfoMock).toBeCalledTimes(1); }); it('creates DeprecationsFactory with the correct parameters', async () => { @@ -89,92 +91,4 @@ describe('DeprecationsService', () => { }); }); }); - - describe('#registerConfigDeprecationsInfo', () => { - const deprecationsFactory = mockDeprecationsFactory.create(); - const deprecationsRegistry = mockDeprecationsRegistry.create(); - const getDeprecationsContext = mockDeprecationsRegistry.createGetDeprecationsContext(); - - it('registers config deprecations', async () => { - const deprecationsService = new DeprecationsService(coreContext); - coreContext.configService.getHandledDeprecatedConfigs.mockReturnValue([ - [ - 'testDomain', - [ - { - configPath: 'test', - level: 'critical', - message: 'testMessage', - documentationUrl: 'testDocUrl', - correctiveActions: { - manualSteps: [ - 'Using Kibana user management, change all users using the kibana_user role to the kibana_admin role.', - 'Using Kibana role-mapping management, change all role-mappings which assign the kibana_user role to the kibana_admin role.', - ], - }, - }, - ], - ], - ]); - - deprecationsFactory.getRegistry.mockReturnValue(deprecationsRegistry); - deprecationsService['registerConfigDeprecationsInfo'](deprecationsFactory); - - expect(coreContext.configService.getHandledDeprecatedConfigs).toBeCalledTimes(1); - expect(deprecationsFactory.getRegistry).toBeCalledTimes(1); - expect(deprecationsFactory.getRegistry).toBeCalledWith('testDomain'); - expect(deprecationsRegistry.registerDeprecations).toBeCalledTimes(1); - const configDeprecations = - await deprecationsRegistry.registerDeprecations.mock.calls[0][0].getDeprecations( - getDeprecationsContext - ); - expect(configDeprecations).toMatchInlineSnapshot(` - Array [ - Object { - "configPath": "test", - "correctiveActions": Object { - "manualSteps": Array [ - "Using Kibana user management, change all users using the kibana_user role to the kibana_admin role.", - "Using Kibana role-mapping management, change all role-mappings which assign the kibana_user role to the kibana_admin role.", - ], - }, - "deprecationType": "config", - "documentationUrl": "testDocUrl", - "level": "critical", - "message": "testMessage", - "requireRestart": true, - "title": "testDomain has a deprecated setting", - }, - ] - `); - }); - - it('accepts `level` field overrides', async () => { - const deprecationsService = new DeprecationsService(coreContext); - coreContext.configService.getHandledDeprecatedConfigs.mockReturnValue([ - [ - 'testDomain', - [ - { - configPath: 'test', - message: 'testMessage', - level: 'warning', - correctiveActions: { - manualSteps: ['step a'], - }, - }, - ], - ], - ]); - - deprecationsFactory.getRegistry.mockReturnValue(deprecationsRegistry); - deprecationsService['registerConfigDeprecationsInfo'](deprecationsFactory); - - const configDeprecations = - await deprecationsRegistry.registerDeprecations.mock.calls[0][0].getDeprecations( - getDeprecationsContext - ); - expect(configDeprecations[0].level).toBe('warning'); - }); - }); }); diff --git a/packages/core/deprecations/core-deprecations-server-internal/src/deprecations_service.ts b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations_service.ts index 4c8f564943ab1..c0a0ef0f88c7b 100644 --- a/packages/core/deprecations/core-deprecations-server-internal/src/deprecations_service.ts +++ b/packages/core/deprecations/core-deprecations-server-internal/src/deprecations_service.ts @@ -19,9 +19,11 @@ import type { DeprecationRegistryProvider, DeprecationsClient, } from '@kbn/core-deprecations-server'; +import { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-server-internal'; import { DeprecationsFactory } from './deprecations_factory'; import { registerRoutes } from './routes'; import { config as deprecationConfig, DeprecationConfigType } from './deprecation_config'; +import { registerApiDeprecationsInfo, registerConfigDeprecationsInfo } from './deprecations'; export interface InternalDeprecationsServiceStart { /** @@ -40,6 +42,7 @@ export type InternalDeprecationsServiceSetup = DeprecationRegistryProvider; /** @internal */ export interface DeprecationsSetupDeps { http: InternalHttpServiceSetup; + coreUsageData: InternalCoreUsageDataSetup; } /** @internal */ @@ -55,7 +58,10 @@ export class DeprecationsService this.configService = coreContext.configService; } - public async setup({ http }: DeprecationsSetupDeps): Promise { + public async setup({ + http, + coreUsageData, + }: DeprecationsSetupDeps): Promise { this.logger.debug('Setting up Deprecations service'); const config = await firstValueFrom( @@ -69,8 +75,18 @@ export class DeprecationsService }, }); - registerRoutes({ http }); - this.registerConfigDeprecationsInfo(this.deprecationsFactory); + registerRoutes({ http, coreUsageData }); + + registerConfigDeprecationsInfo({ + deprecationsFactory: this.deprecationsFactory, + configService: this.configService, + }); + + registerApiDeprecationsInfo({ + deprecationsFactory: this.deprecationsFactory, + http, + coreUsageData, + }); const deprecationsFactory = this.deprecationsFactory; return { @@ -87,6 +103,7 @@ export class DeprecationsService if (!this.deprecationsFactory) { throw new Error('`setup` must be called before `start`'); } + return { asScopedToClient: this.createScopedDeprecations(), }; @@ -107,35 +124,4 @@ export class DeprecationsService }; }; } - - private registerConfigDeprecationsInfo(deprecationsFactory: DeprecationsFactory) { - const handledDeprecatedConfigs = this.configService.getHandledDeprecatedConfigs(); - - for (const [domainId, deprecationsContexts] of handledDeprecatedConfigs) { - const deprecationsRegistry = deprecationsFactory.getRegistry(domainId); - deprecationsRegistry.registerDeprecations({ - getDeprecations: () => { - return deprecationsContexts.map( - ({ - configPath, - title = `${domainId} has a deprecated setting`, - level, - message, - correctiveActions, - documentationUrl, - }) => ({ - configPath, - title, - level, - message, - correctiveActions, - documentationUrl, - deprecationType: 'config', - requireRestart: true, - }) - ); - }, - }); - } - } } diff --git a/packages/core/deprecations/core-deprecations-server-internal/src/routes/index.ts b/packages/core/deprecations/core-deprecations-server-internal/src/routes/index.ts index e1a925610327f..f812bbfd15acd 100644 --- a/packages/core/deprecations/core-deprecations-server-internal/src/routes/index.ts +++ b/packages/core/deprecations/core-deprecations-server-internal/src/routes/index.ts @@ -8,10 +8,22 @@ */ import type { InternalHttpServiceSetup } from '@kbn/core-http-server-internal'; +import { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-server-internal'; import type { InternalDeprecationRequestHandlerContext } from '../internal_types'; import { registerGetRoute } from './get'; +import { registerMarkAsResolvedRoute } from './resolve_deprecated_api'; +import { registerApiDeprecationsPostValidationHandler } from './post_validation_handler'; -export function registerRoutes({ http }: { http: InternalHttpServiceSetup }) { +export function registerRoutes({ + http, + coreUsageData, +}: { + http: InternalHttpServiceSetup; + coreUsageData: InternalCoreUsageDataSetup; +}) { const router = http.createRouter('/api/deprecations'); registerGetRoute(router); + + registerApiDeprecationsPostValidationHandler({ http, coreUsageData }); + registerMarkAsResolvedRoute(router, { coreUsageData }); } diff --git a/packages/core/deprecations/core-deprecations-server-internal/src/routes/post_validation_handler.ts b/packages/core/deprecations/core-deprecations-server-internal/src/routes/post_validation_handler.ts new file mode 100644 index 0000000000000..b93c17af2f536 --- /dev/null +++ b/packages/core/deprecations/core-deprecations-server-internal/src/routes/post_validation_handler.ts @@ -0,0 +1,51 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import type { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-server-internal'; +import type { CoreKibanaRequest } from '@kbn/core-http-router-server-internal'; +import type { InternalHttpServiceSetup } from '@kbn/core-http-server-internal'; +import { isObject } from 'lodash'; +import { RouteDeprecationInfo } from '@kbn/core-http-server/src/router/route'; +import { buildApiDeprecationId } from '../deprecations'; + +interface Dependencies { + coreUsageData: InternalCoreUsageDataSetup; + http: InternalHttpServiceSetup; +} + +/** + * listens to http post validation events to increment deprecated api calls + * This will keep track of any called deprecated API. + */ +export const registerApiDeprecationsPostValidationHandler = ({ + coreUsageData, + http, +}: Dependencies) => { + http.registerOnPostValidation(createRouteDeprecationsHandler({ coreUsageData })); +}; + +export function createRouteDeprecationsHandler({ + coreUsageData, +}: { + coreUsageData: InternalCoreUsageDataSetup; +}) { + return (req: CoreKibanaRequest, { deprecated }: { deprecated?: RouteDeprecationInfo }) => { + if (deprecated && isObject(deprecated) && req.route.routePath) { + const counterName = buildApiDeprecationId({ + routeMethod: req.route.method, + routePath: req.route.routePath, + routeVersion: req.apiVersion, + }); + + const client = coreUsageData.getClient(); + // no await we just fire it off. + void client.incrementDeprecatedApi(counterName, { resolved: false }); + } + }; +} diff --git a/packages/core/deprecations/core-deprecations-server-internal/src/routes/resolve_deprecated_api.ts b/packages/core/deprecations/core-deprecations-server-internal/src/routes/resolve_deprecated_api.ts new file mode 100644 index 0000000000000..840bc5ac22d23 --- /dev/null +++ b/packages/core/deprecations/core-deprecations-server-internal/src/routes/resolve_deprecated_api.ts @@ -0,0 +1,52 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { schema } from '@kbn/config-schema'; +import { InternalCoreUsageDataSetup } from '@kbn/core-usage-data-base-server-internal'; +import type { InternalDeprecationRouter } from '../internal_types'; +import { buildApiDeprecationId } from '../deprecations'; + +export const registerMarkAsResolvedRoute = ( + router: InternalDeprecationRouter, + { coreUsageData }: { coreUsageData: InternalCoreUsageDataSetup } +) => { + router.post( + { + path: '/mark_as_resolved', + validate: { + body: schema.object({ + domainId: schema.string(), + routePath: schema.string(), + routeMethod: schema.oneOf([ + schema.literal('post'), + schema.literal('put'), + schema.literal('delete'), + schema.literal('patch'), + schema.literal('get'), + schema.literal('options'), + ]), + routeVersion: schema.maybe(schema.string()), + incrementBy: schema.number(), + }), + }, + }, + async (_, req, res) => { + const usageClient = coreUsageData.getClient(); + const { routeMethod, routePath, routeVersion, incrementBy } = req.body; + const counterName = buildApiDeprecationId({ + routeMethod, + routePath, + routeVersion, + }); + + await usageClient.incrementDeprecatedApi(counterName, { resolved: true, incrementBy }); + return res.ok(); + } + ); +}; diff --git a/packages/core/deprecations/core-deprecations-server-internal/tsconfig.json b/packages/core/deprecations/core-deprecations-server-internal/tsconfig.json index ba06a3e9ec2f7..02be6b7cb8198 100644 --- a/packages/core/deprecations/core-deprecations-server-internal/tsconfig.json +++ b/packages/core/deprecations/core-deprecations-server-internal/tsconfig.json @@ -33,6 +33,11 @@ "@kbn/core-elasticsearch-server-mocks", "@kbn/core-http-server", "@kbn/core-elasticsearch-client-server-mocks", + "@kbn/core-usage-data-base-server-internal", + "@kbn/core-usage-data-server", + "@kbn/core-usage-data-server-internal", + "@kbn/core-usage-data-server-mocks", + "@kbn/core-http-router-server-internal", ], "exclude": [ "target/**/*", diff --git a/packages/core/http/core-http-router-server-internal/index.ts b/packages/core/http/core-http-router-server-internal/index.ts index 6c684d5f8169c..6aa6ac117f533 100644 --- a/packages/core/http/core-http-router-server-internal/index.ts +++ b/packages/core/http/core-http-router-server-internal/index.ts @@ -13,16 +13,10 @@ export { CoreVersionedRouter, ALLOWED_PUBLIC_VERSION, unwrapVersionedResponseBodyValidation, - type VersionedRouterRoute, type HandlerResolutionStrategy, } from './src/versioned_router'; export { Router } from './src/router'; -export type { - RouterOptions, - InternalRegistrar, - InternalRegistrarOptions, - InternalRouterRoute, -} from './src/router'; +export type { RouterOptions, InternalRegistrar, InternalRegistrarOptions } from './src/router'; export { isKibanaRequest, isRealRequest, ensureRawRequest, CoreKibanaRequest } from './src/request'; export { isSafeMethod } from './src/route'; export { HapiResponseAdapter } from './src/response_adapter'; diff --git a/packages/core/http/core-http-router-server-internal/src/request.ts b/packages/core/http/core-http-router-server-internal/src/request.ts index 286b900fc24f5..9f89f1a70bb47 100644 --- a/packages/core/http/core-http-router-server-internal/src/request.ts +++ b/packages/core/http/core-http-router-server-internal/src/request.ts @@ -143,6 +143,8 @@ export class CoreKibanaRequest< public readonly rewrittenUrl?: URL; /** {@inheritDoc KibanaRequest.httpVersion} */ public readonly httpVersion: string; + /** {@inheritDoc KibanaRequest.apiVersion} */ + public readonly apiVersion: undefined; /** {@inheritDoc KibanaRequest.protocol} */ public readonly protocol: HttpProtocol; /** {@inheritDoc KibanaRequest.authzResult} */ @@ -185,6 +187,7 @@ export class CoreKibanaRequest< }); this.httpVersion = isRealReq ? request.raw.req.httpVersion : '1.0'; + this.apiVersion = undefined; this.protocol = getProtocolFromHttpVersion(this.httpVersion); this.route = deepFreeze(this.getRouteInfo(request)); @@ -216,6 +219,7 @@ export class CoreKibanaRequest< }, route: this.route, authzResult: this.authzResult, + apiVersion: this.apiVersion, }; } @@ -252,7 +256,14 @@ export class CoreKibanaRequest< } = request.route?.settings?.payload || {}; // the socket is undefined when using @hapi/shot, or when a "fake request" is used - const socketTimeout = isRealRawRequest(request) ? request.raw.req.socket?.timeout : undefined; + let socketTimeout: undefined | number; + let routePath: undefined | string; + + if (isRealRawRequest(request)) { + socketTimeout = request.raw.req.socket?.timeout; + routePath = request.route.path; + } + const options = { authRequired: this.getAuthRequired(request), // TypeScript note: Casting to `RouterOptions` to fix the following error: @@ -266,6 +277,8 @@ export class CoreKibanaRequest< xsrfRequired: ((request.route?.settings as RouteOptions)?.app as KibanaRouteOptions)?.xsrfRequired ?? true, // some places in LP call KibanaRequest.from(request) manually. remove fallback to true before v8 + deprecated: ((request.route?.settings as RouteOptions)?.app as KibanaRouteOptions) + ?.deprecated, access: this.getAccess(request), tags: request.route?.settings?.tags || [], security: this.getSecurity(request), @@ -285,6 +298,7 @@ export class CoreKibanaRequest< return { path: request.path ?? '/', + routePath, method, options, }; diff --git a/packages/core/http/core-http-router-server-internal/src/router.test.ts b/packages/core/http/core-http-router-server-internal/src/router.test.ts index f611e3b6308fe..2c702fb3ef702 100644 --- a/packages/core/http/core-http-router-server-internal/src/router.test.ts +++ b/packages/core/http/core-http-router-server-internal/src/router.test.ts @@ -50,7 +50,11 @@ describe('Router', () => { path: '/', validate: { body: validation, query: validation, params: validation }, options: { - deprecated: true, + deprecated: { + documentationUrl: 'https://fake-url.com', + reason: { type: 'remove' }, + severity: 'warning', + }, discontinued: 'post test discontinued', summary: 'post test summary', description: 'post test description', @@ -72,7 +76,11 @@ describe('Router', () => { validationSchemas: { body: validation, query: validation, params: validation }, isVersioned: false, options: { - deprecated: true, + deprecated: { + documentationUrl: 'https://fake-url.com', + reason: { type: 'remove' }, + severity: 'warning', + }, discontinued: 'post test discontinued', summary: 'post test summary', description: 'post test description', @@ -93,7 +101,7 @@ describe('Router', () => { validate: { body: validation, query: validation, params: validation }, }, (context, req, res) => res.ok(), - { isVersioned: true } + { isVersioned: true, events: false } ); router.get( { diff --git a/packages/core/http/core-http-router-server-internal/src/router.ts b/packages/core/http/core-http-router-server-internal/src/router.ts index 36f324236a4d2..e7ad85fcda33a 100644 --- a/packages/core/http/core-http-router-server-internal/src/router.ts +++ b/packages/core/http/core-http-router-server-internal/src/router.ts @@ -7,6 +7,7 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ +import { EventEmitter } from 'node:events'; import type { Request, ResponseToolkit } from '@hapi/hapi'; import apm from 'elastic-apm-node'; import { isConfigSchema } from '@kbn/config-schema'; @@ -32,6 +33,7 @@ import { isZod } from '@kbn/zod'; import { validBodyOutput, getRequestValidation } from '@kbn/core-http-server'; import type { RouteSecurityGetter } from '@kbn/core-http-server'; import type { DeepPartial } from '@kbn/utility-types'; +import { RouteDeprecationInfo } from '@kbn/core-http-server/src/router/route'; import { RouteValidator } from './validator'; import { ALLOWED_PUBLIC_VERSION, CoreVersionedRouter } from './versioned_router'; import { CoreKibanaRequest } from './request'; @@ -52,7 +54,7 @@ export type ContextEnhancer< Context extends RequestHandlerContextBase > = (handler: RequestHandler) => RequestHandlerEnhanced; -function getRouteFullPath(routerPath: string, routePath: string) { +export function getRouteFullPath(routerPath: string, routePath: string) { // If router's path ends with slash and route's path starts with slash, // we should omit one of them to have a valid concatenated path. const routePathStartIndex = routerPath.endsWith('/') && routePath.startsWith('/') ? 1 : 0; @@ -147,7 +149,13 @@ export interface RouterOptions { /** @internal */ export interface InternalRegistrarOptions { + /** @default false */ isVersioned: boolean; + /** + * Whether this route should emit "route events" like postValidate + * @default true + */ + events: boolean; } /** @internal */ @@ -166,15 +174,9 @@ export type InternalRegistrar ReturnType>; /** @internal */ -export interface InternalRouterRoute extends RouterRoute { - readonly isVersioned: boolean; -} - -/** @internal */ -interface InternalGetRoutesOptions { - /** @default false */ - excludeVersionedRoutes?: boolean; -} +type RouterEvents = + /** Called after route validation, regardless of success or failure */ + 'onPostValidate'; /** * @internal @@ -182,7 +184,8 @@ interface InternalGetRoutesOptions { export class Router implements IRouter { - public routes: Array> = []; + private static ee = new EventEmitter(); + public routes: Array> = []; public pluginId?: symbol; public get: InternalRegistrar<'get', Context>; public post: InternalRegistrar<'post', Context>; @@ -202,25 +205,27 @@ export class Router( route: InternalRouteConfig, handler: RequestHandler, - { isVersioned }: InternalRegistrarOptions = { isVersioned: false } + { isVersioned, events }: InternalRegistrarOptions = { isVersioned: false, events: true } ) => { route = prepareRouteConfigValidation(route); const routeSchemas = routeSchemasFromRouteConfig(route, method); - const isPublicUnversionedApi = + const isPublicUnversionedRoute = !isVersioned && route.options?.access === 'public' && // We do not consider HTTP resource routes as APIs route.options?.httpResource !== true; this.routes.push({ - handler: async (req, responseToolkit) => - await this.handle({ + handler: async (req, responseToolkit) => { + return await this.handle({ routeSchemas, request: req, responseToolkit, - isPublicUnversionedApi, + isPublicUnversionedRoute, handler: this.enhanceWithContext(handler), - }), + emit: events ? { onPostValidation: this.emitPostValidate } : undefined, + }); + }, method, path: getRouteFullPath(this.routerPath, route.path), options: validOptions(method, route), @@ -229,6 +234,8 @@ export class Router, route.options), validationSchemas: route.validate, + // @ts-ignore using isVersioned: false in the type instead of boolean + // for typeguarding between versioned and unversioned RouterRoute types isVersioned, }); }; @@ -240,7 +247,15 @@ export class Router void) { + Router.ee.on(event, cb); + } + + public static off(event: RouterEvents, cb: (req: CoreKibanaRequest, ...args: any[]) => void) { + Router.ee.off(event, cb); + } + + public getRoutes({ excludeVersionedRoutes }: { excludeVersionedRoutes?: boolean } = {}) { if (excludeVersionedRoutes) { return this.routes.filter((route) => !route.isVersioned); } @@ -269,16 +284,29 @@ export class Router { + const postValidate: RouterEvents = 'onPostValidate'; + Router.ee.emit(postValidate, request, routeOptions); + }; + private async handle({ routeSchemas, request, responseToolkit, - isPublicUnversionedApi, + emit, + isPublicUnversionedRoute, handler, }: { request: Request; responseToolkit: ResponseToolkit; - isPublicUnversionedApi: boolean; + emit?: { + onPostValidation: (req: KibanaRequest, reqOptions: any) => void; + }; + isPublicUnversionedRoute: boolean; handler: RequestHandlerEnhanced< P, Q, @@ -300,18 +328,24 @@ export class Router { expect(router.post).toHaveBeenCalledWith( expect.objectContaining(expectedRouteConfig), expect.any(Function), - { isVersioned: true } + { isVersioned: true, events: false } ); }); diff --git a/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_route.ts b/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_route.ts index e9a9e60de8193..45654696ba0cf 100644 --- a/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_route.ts +++ b/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_route.ts @@ -18,7 +18,6 @@ import type { KibanaRequest, KibanaResponseFactory, ApiVersion, - AddVersionOpts, VersionedRoute, VersionedRouteConfig, IKibanaResponse, @@ -26,9 +25,10 @@ import type { RouteSecurityGetter, RouteSecurity, RouteMethod, + VersionedRouterRoute, } from '@kbn/core-http-server'; import type { Mutable } from 'utility-types'; -import type { HandlerResolutionStrategy, Method, VersionedRouterRoute } from './types'; +import type { HandlerResolutionStrategy, Method, Options } from './types'; import { validate } from './validate'; import { @@ -46,8 +46,6 @@ import { prepareVersionedRouteValidation, unwrapVersionedResponseBodyValidation import type { RequestLike } from './route_version_utils'; import { Router } from '../router'; -type Options = AddVersionOpts; - interface InternalVersionedRouteConfig extends VersionedRouteConfig { isDev: boolean; useVersionResolutionStrategyForInternalPaths: Map; @@ -68,7 +66,7 @@ function extractValidationSchemaFromHandler(handler: VersionedRouterRoute['handl } export class CoreVersionedRoute implements VersionedRoute { - private readonly handlers = new Map< + public readonly handlers = new Map< ApiVersion, { fn: RequestHandler; @@ -127,7 +125,7 @@ export class CoreVersionedRoute implements VersionedRoute { security: this.getSecurity, }, this.requestHandler, - { isVersioned: true } + { isVersioned: true, events: false } ); } @@ -181,6 +179,7 @@ export class CoreVersionedRoute implements VersionedRoute { } const req = originalReq as Mutable; const version = this.getVersion(req); + req.apiVersion = version; if (!version) { return res.badRequest({ @@ -221,6 +220,8 @@ export class CoreVersionedRoute implements VersionedRoute { req.params = params; req.query = query; } catch (e) { + // Emit onPostValidation even if validation fails. + this.router.emitPostValidate(req, handler.options.options); return res.badRequest({ body: e.message, headers: getVersionHeader(version) }); } } else { @@ -230,6 +231,8 @@ export class CoreVersionedRoute implements VersionedRoute { req.query = {}; } + this.router.emitPostValidate(req, handler.options.options); + const response = await handler.fn(ctx, req, res); if (this.isDev && validation?.response?.[response.status]?.body) { @@ -280,7 +283,6 @@ export class CoreVersionedRoute implements VersionedRoute { public addVersion(options: Options, handler: RequestHandler): VersionedRoute { this.validateVersion(options.version); options = prepareVersionedRouteValidation(options); - this.handlers.set(options.version, { fn: handler, options, diff --git a/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_router.test.ts b/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_router.test.ts index d56de36ba9a29..a3ffffc0ef219 100644 --- a/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_router.test.ts +++ b/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_router.test.ts @@ -36,7 +36,6 @@ describe('Versioned router', () => { versionedRouter.get({ path: '/test/{id}', access: 'internal', - deprecated: true, discontinued: 'x.y.z', }); versionedRouter.post({ @@ -50,16 +49,17 @@ describe('Versioned router', () => { Array [ Object { "handlers": Array [], + "isVersioned": true, "method": "get", "options": Object { "access": "internal", - "deprecated": true, "discontinued": "x.y.z", }, "path": "/test/{id}", }, Object { "handlers": Array [], + "isVersioned": true, "method": "post", "options": Object { "access": "internal", @@ -70,6 +70,7 @@ describe('Versioned router', () => { }, Object { "handlers": Array [], + "isVersioned": true, "method": "delete", "options": Object { "access": "internal", diff --git a/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_router.ts b/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_router.ts index e9272e17ab18e..ef1f8255420ae 100644 --- a/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_router.ts +++ b/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_router.ts @@ -7,11 +7,16 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import type { VersionedRouter, VersionedRoute, VersionedRouteConfig } from '@kbn/core-http-server'; +import type { + VersionedRouter, + VersionedRoute, + VersionedRouteConfig, + VersionedRouterRoute, +} from '@kbn/core-http-server'; import { omit } from 'lodash'; import { CoreVersionedRoute } from './core_versioned_route'; -import type { HandlerResolutionStrategy, Method, VersionedRouterRoute } from './types'; -import type { Router } from '../router'; +import type { HandlerResolutionStrategy, Method } from './types'; +import { getRouteFullPath, type Router } from '../router'; /** @internal */ export interface VersionedRouterArgs { @@ -98,10 +103,11 @@ export class CoreVersionedRouter implements VersionedRouter { public getRoutes(): VersionedRouterRoute[] { return [...this.routes].map((route) => { return { - path: route.path, + path: getRouteFullPath(this.router.routerPath, route.path), method: route.method, options: omit(route.options, 'path'), handlers: route.getHandlers(), + isVersioned: true, }; }); } diff --git a/packages/core/http/core-http-router-server-internal/src/versioned_router/index.ts b/packages/core/http/core-http-router-server-internal/src/versioned_router/index.ts index e283fcc2a590f..14c08076faae0 100644 --- a/packages/core/http/core-http-router-server-internal/src/versioned_router/index.ts +++ b/packages/core/http/core-http-router-server-internal/src/versioned_router/index.ts @@ -9,6 +9,6 @@ export { resolvers as versionHandlerResolvers } from './handler_resolvers'; export { CoreVersionedRouter } from './core_versioned_router'; -export type { HandlerResolutionStrategy, VersionedRouterRoute } from './types'; +export type { HandlerResolutionStrategy } from './types'; export { ALLOWED_PUBLIC_VERSION } from './route_version_utils'; export { unwrapVersionedResponseBodyValidation } from './util'; diff --git a/packages/core/http/core-http-router-server-internal/src/versioned_router/mocks.ts b/packages/core/http/core-http-router-server-internal/src/versioned_router/mocks.ts index 5a958fa9251f7..36a672ca6a9f7 100644 --- a/packages/core/http/core-http-router-server-internal/src/versioned_router/mocks.ts +++ b/packages/core/http/core-http-router-server-internal/src/versioned_router/mocks.ts @@ -20,6 +20,7 @@ export function createRouter(opts: CreateMockRouterOptions = {}) { put: jest.fn(), getRoutes: jest.fn(), handleLegacyErrors: jest.fn(), + emitPostValidate: jest.fn(), patch: jest.fn(), routerPath: '', versioned: {} as any, diff --git a/packages/core/http/core-http-router-server-internal/src/versioned_router/types.ts b/packages/core/http/core-http-router-server-internal/src/versioned_router/types.ts index aec1f8b0cf0ab..bdcaae486cd9c 100644 --- a/packages/core/http/core-http-router-server-internal/src/versioned_router/types.ts +++ b/packages/core/http/core-http-router-server-internal/src/versioned_router/types.ts @@ -7,25 +7,12 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import type { - AddVersionOpts, - RequestHandler, - RouteMethod, - VersionedRouteConfig, -} from '@kbn/core-http-server'; +import type { AddVersionOpts, RouteMethod } from '@kbn/core-http-server'; export type Method = Exclude; /** @internal */ -export interface VersionedRouterRoute { - method: string; - path: string; - options: Omit, 'path'>; - handlers: Array<{ - fn: RequestHandler; - options: AddVersionOpts; - }>; -} +export type Options = AddVersionOpts; /** * Specifies resolution strategy to use if a request does not provide a version. diff --git a/packages/core/http/core-http-router-server-mocks/src/versioned_router.mock.ts b/packages/core/http/core-http-router-server-mocks/src/versioned_router.mock.ts index 987288cf372bd..fec80b06963a0 100644 --- a/packages/core/http/core-http-router-server-mocks/src/versioned_router.mock.ts +++ b/packages/core/http/core-http-router-server-mocks/src/versioned_router.mock.ts @@ -14,6 +14,7 @@ import type { AddVersionOpts, RequestHandler, KibanaResponseFactory, + VersionedRouterRoute, } from '@kbn/core-http-server'; export type MockedVersionedRoute = jest.Mocked; @@ -24,14 +25,16 @@ const createMockVersionedRoute = (): MockedVersionedRoute => { return api; }; +type VersionedRouterMethods = keyof Omit; + export type MockedVersionedRouter = jest.Mocked> & { - getRoute: (method: keyof VersionedRouter, path: string) => RegisteredVersionedRoute; + getRoute: (method: VersionedRouterMethods, path: string) => RegisteredVersionedRoute; }; const createMethodHandler = () => jest.fn((_) => createMockVersionedRoute()); - +const createMockGetRoutes = () => jest.fn(() => [] as VersionedRouterRoute[]); export const createVersionedRouterMock = (): MockedVersionedRouter => { - const router: Omit = { + const router: Omit = { delete: createMethodHandler(), get: createMethodHandler(), patch: createMethodHandler(), @@ -42,6 +45,7 @@ export const createVersionedRouterMock = (): MockedVersionedRouter => { return { ...router, getRoute: getRoute.bind(null, router), + getRoutes: createMockGetRoutes(), }; }; @@ -54,9 +58,10 @@ export interface RegisteredVersionedRoute { }; }; } + const getRoute = ( - router: Omit, - method: keyof VersionedRouter, + router: Omit, + method: VersionedRouterMethods, path: string ): RegisteredVersionedRoute => { if (!router[method].mock.calls.length) { diff --git a/packages/core/http/core-http-server-internal/src/http_server.test.ts b/packages/core/http/core-http-server-internal/src/http_server.test.ts index c374ff7ca2107..69e69f784e65e 100644 --- a/packages/core/http/core-http-server-internal/src/http_server.test.ts +++ b/packages/core/http/core-http-server-internal/src/http_server.test.ts @@ -906,6 +906,7 @@ test('exposes route details of incoming request to a route handler', async () => .expect(200, { method: 'get', path: '/', + routePath: '/', options: { authRequired: true, xsrfRequired: false, @@ -1088,6 +1089,7 @@ test('exposes route details of incoming request to a route handler (POST + paylo .expect(200, { method: 'post', path: '/', + routePath: '/', options: { authRequired: true, xsrfRequired: true, diff --git a/packages/core/http/core-http-server-internal/src/http_server.ts b/packages/core/http/core-http-server-internal/src/http_server.ts index 46470bac7c504..1dee6a3286788 100644 --- a/packages/core/http/core-http-server-internal/src/http_server.ts +++ b/packages/core/http/core-http-server-internal/src/http_server.ts @@ -35,10 +35,12 @@ import type { HttpServerInfo, HttpAuth, IAuthHeadersStorage, + RouterDeprecatedRouteDetails, + RouteMethod, } from '@kbn/core-http-server'; import { performance } from 'perf_hooks'; import { isBoom } from '@hapi/boom'; -import { identity } from 'lodash'; +import { identity, isObject } from 'lodash'; import { IHttpEluMonitorConfig } from '@kbn/core-http-server/src/elu_monitor'; import { Env } from '@kbn/config'; import { CoreContext } from '@kbn/core-base-server-internal'; @@ -140,6 +142,7 @@ export interface HttpServerSetup { registerAuth: HttpServiceSetup['registerAuth']; registerOnPostAuth: HttpServiceSetup['registerOnPostAuth']; registerOnPreResponse: HttpServiceSetup['registerOnPreResponse']; + getDeprecatedRoutes: HttpServiceSetup['getDeprecatedRoutes']; authRequestHeaders: IAuthHeadersStorage; auth: HttpAuth; getServerInfo: () => HttpServerInfo; @@ -280,6 +283,7 @@ export class HttpServer { return { registerRouter: this.registerRouter.bind(this), + getDeprecatedRoutes: this.getDeprecatedRoutes.bind(this), registerRouterAfterListening: this.registerRouterAfterListening.bind(this), registerStaticDir: this.registerStaticDir.bind(this), staticAssets, @@ -385,6 +389,45 @@ export class HttpServer { } } + private getDeprecatedRoutes(): RouterDeprecatedRouteDetails[] { + const deprecatedRoutes: RouterDeprecatedRouteDetails[] = []; + + for (const router of this.registeredRouters) { + const allRouterRoutes = [ + // exclude so we dont get double entries. + // we need to call the versioned getRoutes to grab the full version options details + router.getRoutes({ excludeVersionedRoutes: true }), + router.versioned.getRoutes(), + ].flat(); + + deprecatedRoutes.push( + ...allRouterRoutes + .flat() + .map((route) => { + if (route.isVersioned === true) { + return [...route.handlers.entries()].map(([_, { options }]) => { + const deprecated = options.options?.deprecated; + return { route, version: `${options.version}`, deprecated }; + }); + } + return { route, version: undefined, deprecated: route.options.deprecated }; + }) + .flat() + .filter(({ deprecated }) => isObject(deprecated)) + .flatMap(({ route, deprecated, version }) => { + return { + routeDeprecationOptions: deprecated!, + routeMethod: route.method as RouteMethod, + routePath: route.path, + routeVersion: version, + }; + }) + ); + } + + return deprecatedRoutes; + } + private setupGracefulShutdownHandlers() { this.registerOnPreRouting((request, response, toolkit) => { if (this.stopping || this.stopped) { @@ -693,12 +736,13 @@ export class HttpServer { this.log.debug(`registering route handler for [${route.path}]`); // Hapi does not allow payload validation to be specified for 'head' or 'get' requests const validate = isSafeMethod(route.method) ? undefined : { payload: true }; - const { authRequired, tags, body = {}, timeout } = route.options; + const { authRequired, tags, body = {}, timeout, deprecated } = route.options; const { accepts: allow, override, maxBytes, output, parse } = body; const kibanaRouteOptions: KibanaRouteOptions = { xsrfRequired: route.options.xsrfRequired ?? !isSafeMethod(route.method), access: route.options.access ?? 'internal', + deprecated, security: route.security, }; // Log HTTP API target consumer. diff --git a/packages/core/http/core-http-server-internal/src/http_service.ts b/packages/core/http/core-http-server-internal/src/http_service.ts index 3f803b06f15fd..af310a6792057 100644 --- a/packages/core/http/core-http-server-internal/src/http_service.ts +++ b/packages/core/http/core-http-server-internal/src/http_service.ts @@ -162,7 +162,7 @@ export class HttpService return this.internalPreboot; } - public async setup(deps: SetupDeps) { + public async setup(deps: SetupDeps): Promise { this.requestHandlerContext = deps.context.createContextContainer(); this.configSubscription = this.config$.subscribe(() => { if (this.httpServer.isListening()) { @@ -185,9 +185,11 @@ export class HttpService this.internalSetup = { ...serverContract, - + registerOnPostValidation: (cb) => { + Router.on('onPostValidate', cb); + }, + getRegisteredDeprecatedApis: () => serverContract.getDeprecatedRoutes(), externalUrl: new ExternalUrlConfig(config.externalUrl), - createRouter: ( path: string, pluginId: PluginOpaqueId = this.coreContext.coreId diff --git a/packages/core/http/core-http-server-internal/src/types.ts b/packages/core/http/core-http-server-internal/src/types.ts index 70dde23f035d0..0706af9ad73a2 100644 --- a/packages/core/http/core-http-server-internal/src/types.ts +++ b/packages/core/http/core-http-server-internal/src/types.ts @@ -16,7 +16,10 @@ import type { IContextContainer, HttpServiceSetup, HttpServiceStart, + RouterDeprecatedRouteDetails, } from '@kbn/core-http-server'; +import { CoreKibanaRequest } from '@kbn/core-http-router-server-internal'; +import { RouteDeprecationInfo } from '@kbn/core-http-server/src/router/route'; import type { HttpServerSetup } from './http_server'; import type { ExternalUrlConfig } from './external_url'; import type { InternalStaticAssets } from './static_assets'; @@ -54,6 +57,9 @@ export interface InternalHttpServiceSetup path: string, plugin?: PluginOpaqueId ) => IRouter; + registerOnPostValidation( + cb: (req: CoreKibanaRequest, metadata: { deprecated: RouteDeprecationInfo }) => void + ): void; registerRouterAfterListening: (router: IRouter) => void; registerStaticDir: (path: string, dirPath: string) => void; authRequestHeaders: IAuthHeadersStorage; @@ -65,6 +71,7 @@ export interface InternalHttpServiceSetup contextName: ContextName, provider: IContextProvider ) => IContextContainer; + getRegisteredDeprecatedApis: () => RouterDeprecatedRouteDetails[]; } /** @internal */ diff --git a/packages/core/http/core-http-server-mocks/src/http_service.mock.ts b/packages/core/http/core-http-server-mocks/src/http_service.mock.ts index 4e803ee5f86a8..116db3648f120 100644 --- a/packages/core/http/core-http-server-mocks/src/http_service.mock.ts +++ b/packages/core/http/core-http-server-mocks/src/http_service.mock.ts @@ -171,6 +171,9 @@ const createInternalSetupContractMock = () => { createCookieSessionStorageFactory: jest.fn(), registerOnPreRouting: jest.fn(), registerOnPreAuth: jest.fn(), + getDeprecatedRoutes: jest.fn(), + getRegisteredDeprecatedApis: jest.fn(), + registerOnPostValidation: jest.fn(), registerAuth: jest.fn(), registerOnPostAuth: jest.fn(), registerRouteHandlerContext: jest.fn(), @@ -207,6 +210,7 @@ const createSetupContractMock = < createCookieSessionStorageFactory: internalMock.createCookieSessionStorageFactory, registerOnPreRouting: internalMock.registerOnPreRouting, registerOnPreAuth: jest.fn(), + getDeprecatedRoutes: jest.fn(), registerAuth: internalMock.registerAuth, registerOnPostAuth: internalMock.registerOnPostAuth, registerOnPreResponse: internalMock.registerOnPreResponse, diff --git a/packages/core/http/core-http-server/index.ts b/packages/core/http/core-http-server/index.ts index 4ba653fbd534c..7fe125c6aa9a7 100644 --- a/packages/core/http/core-http-server/index.ts +++ b/packages/core/http/core-http-server/index.ts @@ -93,6 +93,7 @@ export type { IRouter, RouteRegistrar, RouterRoute, + RouterDeprecatedRouteDetails, IKibanaSocket, KibanaErrorResponseFactory, KibanaRedirectionResponseFactory, @@ -170,6 +171,7 @@ export type { VersionedRouter, VersionedRouteCustomResponseBodyValidation, VersionedResponseBodyValidation, + VersionedRouterRoute, } from './src/versioning'; export type { IStaticAssets } from './src/static_assets'; diff --git a/packages/core/http/core-http-server/src/http_contract.ts b/packages/core/http/core-http-server/src/http_contract.ts index 72eb70149f529..e2f675bd8d0c0 100644 --- a/packages/core/http/core-http-server/src/http_contract.ts +++ b/packages/core/http/core-http-server/src/http_contract.ts @@ -12,6 +12,7 @@ import type { IContextProvider, IRouter, RequestHandlerContextBase, + RouterDeprecatedRouteDetails, } from './router'; import type { AuthenticationHandler, @@ -359,6 +360,14 @@ export interface HttpServiceSetup< * Provides common {@link HttpServerInfo | information} about the running http server. */ getServerInfo: () => HttpServerInfo; + + /** + * Provides a list of all registered deprecated routes {{@link RouterDeprecatedRouteDetails | information}}. + * The routers will be evaluated everytime this function gets called to + * accommodate for any late route registrations + * @returns {RouterDeprecatedRouteDetails[]} + */ + getDeprecatedRoutes: () => RouterDeprecatedRouteDetails[]; } /** @public */ diff --git a/packages/core/http/core-http-server/src/router/index.ts b/packages/core/http/core-http-server/src/router/index.ts index c26212fa0de81..8384ed838d5fc 100644 --- a/packages/core/http/core-http-server/src/router/index.ts +++ b/packages/core/http/core-http-server/src/router/index.ts @@ -80,7 +80,7 @@ export type { LazyValidator, } from './route_validator'; export { RouteValidationError } from './route_validator'; -export type { IRouter, RouteRegistrar, RouterRoute } from './router'; +export type { IRouter, RouteRegistrar, RouterRoute, RouterDeprecatedRouteDetails } from './router'; export type { IKibanaSocket } from './socket'; export type { KibanaErrorResponseFactory, diff --git a/packages/core/http/core-http-server/src/router/request.ts b/packages/core/http/core-http-server/src/router/request.ts index 5cb84a21be0c3..066372faca1e4 100644 --- a/packages/core/http/core-http-server/src/router/request.ts +++ b/packages/core/http/core-http-server/src/router/request.ts @@ -13,7 +13,7 @@ import type { Observable } from 'rxjs'; import type { RecursiveReadonly } from '@kbn/utility-types'; import type { HttpProtocol } from '../http_contract'; import type { IKibanaSocket } from './socket'; -import type { RouteMethod, RouteConfigOptions, RouteSecurity } from './route'; +import type { RouteMethod, RouteConfigOptions, RouteSecurity, RouteDeprecationInfo } from './route'; import type { Headers } from './headers'; export type RouteSecurityGetter = (request: { @@ -26,6 +26,7 @@ export type InternalRouteSecurity = RouteSecurity | RouteSecurityGetter; * @public */ export interface KibanaRouteOptions extends RouteOptionsApp { + deprecated?: RouteDeprecationInfo; xsrfRequired: boolean; access: 'internal' | 'public'; security?: InternalRouteSecurity; @@ -59,6 +60,7 @@ export interface KibanaRequestRoute { path: string; method: Method; options: KibanaRequestRouteOptions; + routePath?: string; } /** @@ -190,6 +192,11 @@ export interface KibanaRequest< */ readonly rewrittenUrl?: URL; + /** + * The versioned route API version of this request. + */ + readonly apiVersion: string | undefined; + /** * The path parameter of this request. */ diff --git a/packages/core/http/core-http-server/src/router/route.ts b/packages/core/http/core-http-server/src/router/route.ts index ac723db924a5a..6696babce41e1 100644 --- a/packages/core/http/core-http-server/src/router/route.ts +++ b/packages/core/http/core-http-server/src/router/route.ts @@ -113,6 +113,43 @@ export type RouteAccess = 'public' | 'internal'; export type Privilege = string; +/** + * Route Deprecation info + * This information will assist Kibana HTTP API users when upgrading to new versions + * of the Elastic stack (via Upgrade Assistant) and will be surfaced in documentation + * created from HTTP API introspection (like OAS). + */ +export interface RouteDeprecationInfo { + documentationUrl: string; + severity: 'warning' | 'critical'; + reason: VersionBumpDeprecationType | RemovalApiDeprecationType | MigrationApiDeprecationType; +} + +/** + * bump deprecation reason denotes a new version of the API is available + */ +interface VersionBumpDeprecationType { + type: 'bump'; + newApiVersion: string; +} + +/** + * remove deprecation reason denotes the API was fully removed with no replacement + */ +interface RemovalApiDeprecationType { + type: 'remove'; +} + +/** + * migrate deprecation reason denotes the API has been migrated to a different API path + * Please make sure that if you are only incrementing the version of the API to use 'bump' instead + */ +interface MigrationApiDeprecationType { + type: 'migrate'; + newApiPath: string; + newApiMethod: string; +} + /** * A set of privileges that can be used to define complex authorization requirements. * @@ -277,12 +314,18 @@ export interface RouteConfigOptions { description?: string; /** - * Setting this to `true` declares this route to be deprecated. Consumers SHOULD - * refrain from usage of this route. + * Description of deprecations for this HTTP API. * - * @remarks This will be surfaced in OAS documentation. + * @remark This will assist Kibana HTTP API users when upgrading to new versions + * of the Elastic stack (via Upgrade Assistant) and will be surfaced in documentation + * created from HTTP API introspection (like OAS). + * + * Setting this object marks the route as deprecated. + * + * @remarks This may be surfaced in OAS documentation. + * @public */ - deprecated?: boolean; + deprecated?: RouteDeprecationInfo; /** * Whether this route should be treated as "invisible" and excluded from router diff --git a/packages/core/http/core-http-server/src/router/router.ts b/packages/core/http/core-http-server/src/router/router.ts index ba2b5eb906a93..d8b79bee13025 100644 --- a/packages/core/http/core-http-server/src/router/router.ts +++ b/packages/core/http/core-http-server/src/router/router.ts @@ -10,7 +10,7 @@ import type { Request, ResponseObject, ResponseToolkit } from '@hapi/hapi'; import type Boom from '@hapi/boom'; import type { VersionedRouter } from '../versioning'; -import type { RouteConfig, RouteMethod } from './route'; +import type { RouteConfig, RouteDeprecationInfo, RouteMethod } from './route'; import type { RequestHandler, RequestHandlerWrapper } from './request_handler'; import type { RequestHandlerContextBase } from './request_handler_context'; import type { RouteConfigOptions } from './route'; @@ -98,7 +98,7 @@ export interface IRouter RouterRoute[]; + getRoutes: (options?: { excludeVersionedRoutes?: boolean }) => RouterRoute[]; /** * An instance very similar to {@link IRouter} that can be used for versioning HTTP routes @@ -139,4 +139,13 @@ export interface RouterRoute { req: Request, responseToolkit: ResponseToolkit ) => Promise>; + isVersioned: false; +} + +/** @public */ +export interface RouterDeprecatedRouteDetails { + routeDeprecationOptions: RouteDeprecationInfo; + routeMethod: RouteMethod; + routePath: string; + routeVersion?: string; } diff --git a/packages/core/http/core-http-server/src/versioning/index.ts b/packages/core/http/core-http-server/src/versioning/index.ts index 94b60bd105aac..8d8a664c769ac 100644 --- a/packages/core/http/core-http-server/src/versioning/index.ts +++ b/packages/core/http/core-http-server/src/versioning/index.ts @@ -19,4 +19,5 @@ export type { VersionedRouter, VersionedRouteCustomResponseBodyValidation, VersionedResponseBodyValidation, + VersionedRouterRoute, } from './types'; diff --git a/packages/core/http/core-http-server/src/versioning/types.ts b/packages/core/http/core-http-server/src/versioning/types.ts index 7998c9cc91fa9..63e1e37754803 100644 --- a/packages/core/http/core-http-server/src/versioning/types.ts +++ b/packages/core/http/core-http-server/src/versioning/types.ts @@ -20,7 +20,7 @@ import type { RouteValidationFunction, LazyValidator, } from '../..'; - +import type { RouteDeprecationInfo } from '../router/route'; type RqCtx = RequestHandlerContextBase; export type { ApiVersion }; @@ -89,17 +89,9 @@ export type VersionedRouteConfig = Omit< */ description?: string; - /** - * Declares this operation to be deprecated. Consumers SHOULD refrain from usage - * of this route. This will be surfaced in OAS documentation. - * - * @default false - */ - deprecated?: boolean; - /** * Release version or date that this route will be removed - * Use with `deprecated: true` + * Use with `deprecated: {@link RouteDeprecationInfo}` * * @default undefined */ @@ -234,6 +226,11 @@ export interface VersionedRouter { * @track-adoption */ delete: VersionedRouteRegistrar<'delete', Ctx>; + + /** + * @public + */ + getRoutes: () => VersionedRouterRoute[]; } /** @public */ @@ -341,6 +338,10 @@ export interface AddVersionOpts { validate: false | VersionedRouteValidation | (() => VersionedRouteValidation); // Provide a way to lazily load validation schemas security?: Exclude['security'], undefined>; + + options?: { + deprecated?: RouteDeprecationInfo; + }; } /** @@ -363,3 +364,11 @@ export interface VersionedRoute< handler: (...params: Parameters>) => MaybePromise ): VersionedRoute; } + +export interface VersionedRouterRoute

{ + method: string; + path: string; + options: Omit, 'path'>; + handlers: Array<{ fn: RequestHandler; options: AddVersionOpts }>; + isVersioned: true; +} diff --git a/packages/core/http/core-http-server/tsconfig.json b/packages/core/http/core-http-server/tsconfig.json index 64b2dacf2f292..50a0ce973eb4e 100644 --- a/packages/core/http/core-http-server/tsconfig.json +++ b/packages/core/http/core-http-server/tsconfig.json @@ -15,7 +15,7 @@ "@kbn/utility-types", "@kbn/core-base-common", "@kbn/core-http-common", - "@kbn/zod" + "@kbn/zod", ], "exclude": [ "target/**/*", diff --git a/packages/core/lifecycle/core-lifecycle-server-mocks/src/core_setup.mock.ts b/packages/core/lifecycle/core-lifecycle-server-mocks/src/core_setup.mock.ts index b50e9279d4721..30f5958bd92c5 100644 --- a/packages/core/lifecycle/core-lifecycle-server-mocks/src/core_setup.mock.ts +++ b/packages/core/lifecycle/core-lifecycle-server-mocks/src/core_setup.mock.ts @@ -76,6 +76,8 @@ export function createCoreSetupMock({ userProfile: userProfileServiceMock.createSetup(), coreUsageData: { registerUsageCounter: coreUsageDataServiceMock.createSetupContract().registerUsageCounter, + registerDeprecatedUsageFetch: + coreUsageDataServiceMock.createSetupContract().registerDeprecatedUsageFetch, }, plugins: { onSetup: jest.fn(), diff --git a/packages/core/plugins/core-plugins-server-internal/src/plugin_context.ts b/packages/core/plugins/core-plugins-server-internal/src/plugin_context.ts index 2fcdf384cb897..d7d40c9b792f7 100644 --- a/packages/core/plugins/core-plugins-server-internal/src/plugin_context.ts +++ b/packages/core/plugins/core-plugins-server-internal/src/plugin_context.ts @@ -225,6 +225,7 @@ export function createPluginSetupContext({ }, http: { createCookieSessionStorageFactory: deps.http.createCookieSessionStorageFactory, + getDeprecatedRoutes: deps.http.getDeprecatedRoutes, registerRouteHandlerContext: < Context extends RequestHandlerContext, ContextName extends keyof Omit @@ -283,6 +284,7 @@ export function createPluginSetupContext({ deprecations: deps.deprecations.getRegistry(plugin.name), coreUsageData: { registerUsageCounter: deps.coreUsageData.registerUsageCounter, + registerDeprecatedUsageFetch: deps.coreUsageData.registerDeprecatedUsageFetch, }, plugins: { onSetup: (...dependencyNames) => runtimeResolver.onSetup(plugin.name, dependencyNames), diff --git a/packages/core/root/core-root-server-internal/src/server.ts b/packages/core/root/core-root-server-internal/src/server.ts index 447db192c3048..5082a27930e87 100644 --- a/packages/core/root/core-root-server-internal/src/server.ts +++ b/packages/core/root/core-root-server-internal/src/server.ts @@ -276,10 +276,6 @@ export class Server { executionContext: executionContextSetup, }); - const deprecationsSetup = await this.deprecations.setup({ - http: httpSetup, - }); - // setup i18n prior to any other service, to have translations ready const i18nServiceSetup = await this.i18n.setup({ http: httpSetup, pluginPaths }); @@ -303,6 +299,11 @@ export class Server { changedDeprecatedConfigPath$: this.configService.getDeprecatedConfigPath$(), }); + const deprecationsSetup = await this.deprecations.setup({ + http: httpSetup, + coreUsageData: coreUsageDataSetup, + }); + const savedObjectsSetup = await this.savedObjects.setup({ http: httpSetup, elasticsearch: elasticsearchServiceSetup, diff --git a/packages/core/saved-objects/core-saved-objects-server-internal/src/mocks/internal_mocks.ts b/packages/core/saved-objects/core-saved-objects-server-internal/src/mocks/internal_mocks.ts index 625c8ed77fb48..ff5fe86df1173 100644 --- a/packages/core/saved-objects/core-saved-objects-server-internal/src/mocks/internal_mocks.ts +++ b/packages/core/saved-objects/core-saved-objects-server-internal/src/mocks/internal_mocks.ts @@ -36,6 +36,7 @@ export const createCoreUsageDataSetupMock = () => { getClient: jest.fn(), registerUsageCounter: jest.fn(), incrementUsageCounter: jest.fn(), + registerDeprecatedUsageFetch: jest.fn(), }; return setupContract; }; diff --git a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_create.ts b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_create.ts index b90aa0226d71c..c9a8656b3f753 100644 --- a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_create.ts +++ b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_create.ts @@ -38,6 +38,7 @@ export const registerBulkCreateRoute = ( summary: `Create saved objects`, tags: ['oas-tag:saved objects'], access, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, validate: { diff --git a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_delete.ts b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_delete.ts index 0f33ddc384bed..65209a6072748 100644 --- a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_delete.ts +++ b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_delete.ts @@ -38,6 +38,7 @@ export const registerBulkDeleteRoute = ( summary: `Delete saved objects`, tags: ['oas-tag:saved objects'], access, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, validate: { diff --git a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_get.ts b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_get.ts index 95fd9f5eab10a..3f87ca12248ae 100644 --- a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_get.ts +++ b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_get.ts @@ -38,6 +38,7 @@ export const registerBulkGetRoute = ( summary: `Get saved objects`, tags: ['oas-tag:saved objects'], access, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, validate: { diff --git a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_resolve.ts b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_resolve.ts index d6b74131fb74d..8e19114e798e0 100644 --- a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_resolve.ts +++ b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_resolve.ts @@ -38,6 +38,7 @@ export const registerBulkResolveRoute = ( summary: `Resolve saved objects`, tags: ['oas-tag:saved objects'], access, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, description: `Retrieve multiple Kibana saved objects by ID, using any legacy URL aliases if they exist. Under certain circumstances, when Kibana is upgraded, saved object migrations may necessitate regenerating some object IDs to enable new features. When an object's ID is regenerated, a legacy URL alias is created for that object, preserving its old ID. In such a scenario, that object can be retrieved with the bulk resolve API using either its new ID or its old ID.`, diff --git a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_update.ts b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_update.ts index 7a7ec340d98ca..825a5f95482c0 100644 --- a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_update.ts +++ b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/bulk_update.ts @@ -38,6 +38,7 @@ export const registerBulkUpdateRoute = ( summary: `Update saved objects`, tags: ['oas-tag:saved objects'], access, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, validate: { diff --git a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/create.ts b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/create.ts index c8bfd4c0feaf9..57f4a10ed9377 100644 --- a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/create.ts +++ b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/create.ts @@ -38,6 +38,7 @@ export const registerCreateRoute = ( summary: `Create a saved object`, tags: ['oas-tag:saved objects'], access, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, validate: { diff --git a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/delete.ts b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/delete.ts index 7ef8aac3fa1b1..69287821d8049 100644 --- a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/delete.ts +++ b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/delete.ts @@ -38,6 +38,7 @@ export const registerDeleteRoute = ( summary: `Delete a saved object`, tags: ['oas-tag:saved objects'], access, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, validate: { diff --git a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/find.ts b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/find.ts index ac3b0555a7694..884ba1ed5c423 100644 --- a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/find.ts +++ b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/find.ts @@ -42,6 +42,7 @@ export const registerFindRoute = ( summary: `Search for saved objects`, tags: ['oas-tag:saved objects'], access, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, validate: { diff --git a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/get.ts b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/get.ts index 9784ef1c79ff4..9fe3aa8ff20c7 100644 --- a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/get.ts +++ b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/get.ts @@ -38,6 +38,7 @@ export const registerGetRoute = ( summary: `Get a saved object`, tags: ['oas-tag:saved objects'], access, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, validate: { diff --git a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/resolve.ts b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/resolve.ts index 295acacc0ba0e..28a6c82e9ffdf 100644 --- a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/resolve.ts +++ b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/resolve.ts @@ -34,6 +34,7 @@ export const registerResolveRoute = ( summary: `Resolve a saved object`, tags: ['oas-tag:saved objects'], access, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, description: `Retrieve a single Kibana saved object by ID, using any legacy URL alias if it exists. Under certain circumstances, when Kibana is upgraded, saved object migrations may necessitate regenerating some object IDs to enable new features. When an object's ID is regenerated, a legacy URL alias is created for that object, preserving its old ID. In such a scenario, that object can be retrieved with the resolve API using either its new ID or its old ID.`, diff --git a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/update.ts b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/update.ts index 9eeea40a29b68..cfedc3ce03d2a 100644 --- a/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/update.ts +++ b/packages/core/saved-objects/core-saved-objects-server-internal/src/routes/update.ts @@ -39,6 +39,7 @@ export const registerUpdateRoute = ( summary: `Update a saved object`, tags: ['oas-tag:saved objects'], access, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, validate: { diff --git a/packages/core/usage-data/core-usage-data-base-server-internal/src/usage_stats_client.ts b/packages/core/usage-data/core-usage-data-base-server-internal/src/usage_stats_client.ts index 649e972af2abc..044fb683fb69a 100644 --- a/packages/core/usage-data/core-usage-data-base-server-internal/src/usage_stats_client.ts +++ b/packages/core/usage-data/core-usage-data-base-server-internal/src/usage_stats_client.ts @@ -8,7 +8,7 @@ */ import type { KibanaRequest } from '@kbn/core-http-server'; -import type { CoreUsageStats } from '@kbn/core-usage-data-server'; +import type { CoreUsageStats, CoreDeprecatedApiUsageStats } from '@kbn/core-usage-data-server'; /** @internal */ export interface BaseIncrementOptions { @@ -38,6 +38,13 @@ export type IncrementSavedObjectsExportOptions = BaseIncrementOptions & { export interface ICoreUsageStatsClient { getUsageStats(): Promise; + getDeprecatedApiUsageStats(): Promise; + + incrementDeprecatedApi( + counterName: string, + options: { resolved?: boolean; incrementBy?: number } + ): Promise; + incrementSavedObjectsBulkCreate(options: BaseIncrementOptions): Promise; incrementSavedObjectsBulkGet(options: BaseIncrementOptions): Promise; diff --git a/packages/core/usage-data/core-usage-data-server-internal/src/core_usage_data_service.ts b/packages/core/usage-data/core-usage-data-server-internal/src/core_usage_data_service.ts index 2a10e06567d02..7ac7cbb7fbb57 100644 --- a/packages/core/usage-data/core-usage-data-server-internal/src/core_usage_data_service.ts +++ b/packages/core/usage-data/core-usage-data-server-internal/src/core_usage_data_service.ts @@ -37,6 +37,7 @@ import type { CoreConfigUsageData, CoreIncrementCounterParams, CoreUsageCounter, + DeprecatedApiUsageFetcher, } from '@kbn/core-usage-data-server'; import { CORE_USAGE_STATS_TYPE, @@ -48,6 +49,7 @@ import { type SavedObjectsServiceStart, } from '@kbn/core-saved-objects-server'; +import { ISavedObjectsRepository } from '@kbn/core-saved-objects-api-server'; import { isConfigured } from './is_configured'; import { coreUsageStatsType } from './saved_objects'; import { CoreUsageStatsClient } from './core_usage_stats_client'; @@ -88,6 +90,7 @@ export class CoreUsageDataService private coreUsageStatsClient?: CoreUsageStatsClient; private deprecatedConfigPaths: ChangedDeprecatedPaths = { set: [], unset: [] }; private incrementUsageCounter: CoreIncrementUsageCounter = () => {}; // Initially set to noop + private deprecatedApiUsageFetcher: DeprecatedApiUsageFetcher = async () => []; // Initially set to noop constructor(core: CoreContext) { this.logger = core.logger.get('core-usage-stats-service'); @@ -513,12 +516,21 @@ export class CoreUsageDataService } }; + const registerDeprecatedUsageFetch = (fetchFn: DeprecatedApiUsageFetcher) => { + this.deprecatedApiUsageFetcher = fetchFn; + }; + + const fetchDeprecatedUsageStats = (params: { soClient: ISavedObjectsRepository }) => { + return this.deprecatedApiUsageFetcher(params); + }; + this.coreUsageStatsClient = new CoreUsageStatsClient({ debugLogger: (message: string) => this.logger.debug(message), basePath: http.basePath, repositoryPromise: internalRepositoryPromise, stop$: this.stop$, incrementUsageCounter, + fetchDeprecatedUsageStats, }); const contract: InternalCoreUsageDataSetup = { @@ -526,6 +538,7 @@ export class CoreUsageDataService getClient: () => this.coreUsageStatsClient!, registerUsageCounter, incrementUsageCounter, + registerDeprecatedUsageFetch, }; return contract; diff --git a/packages/core/usage-data/core-usage-data-server-internal/src/core_usage_stats_client.test.ts b/packages/core/usage-data/core-usage-data-server-internal/src/core_usage_stats_client.test.ts index 948332c71f59a..9702e4b512345 100644 --- a/packages/core/usage-data/core-usage-data-server-internal/src/core_usage_stats_client.test.ts +++ b/packages/core/usage-data/core-usage-data-server-internal/src/core_usage_stats_client.test.ts @@ -52,6 +52,7 @@ describe('CoreUsageStatsClient', () => { debugLogger: debugLoggerMock, basePath: basePathMock, repositoryPromise: Promise.resolve(repositoryMock), + fetchDeprecatedUsageStats: jest.fn(), stop$, incrementUsageCounter: incrementUsageCounterMock, }); diff --git a/packages/core/usage-data/core-usage-data-server-internal/src/core_usage_stats_client.ts b/packages/core/usage-data/core-usage-data-server-internal/src/core_usage_stats_client.ts index 67ab6d9b30c9c..69eba9e1abf23 100644 --- a/packages/core/usage-data/core-usage-data-server-internal/src/core_usage_stats_client.ts +++ b/packages/core/usage-data/core-usage-data-server-internal/src/core_usage_stats_client.ts @@ -37,6 +37,7 @@ import { takeUntil, tap, } from 'rxjs'; +import type { DeprecatedApiUsageFetcher } from '@kbn/core-usage-data-server'; export const BULK_CREATE_STATS_PREFIX = 'apiCalls.savedObjectsBulkCreate'; export const BULK_GET_STATS_PREFIX = 'apiCalls.savedObjectsBulkGet'; @@ -108,6 +109,16 @@ export interface CoreUsageEvent { types?: string[]; } +/** + * Interface that models core events triggered by API deprecations. (e.g. SO HTTP API calls) + * @internal + */ +export interface CoreUsageDeprecatedApiEvent { + id: string; + resolved: boolean; + incrementBy: number; +} + /** @internal */ export interface CoreUsageStatsClientParams { debugLogger: (message: string) => void; @@ -116,6 +127,7 @@ export interface CoreUsageStatsClientParams { stop$: Observable; incrementUsageCounter: (params: CoreIncrementCounterParams) => void; bufferTimeMs?: number; + fetchDeprecatedUsageStats: DeprecatedApiUsageFetcher; } /** @internal */ @@ -126,6 +138,8 @@ export class CoreUsageStatsClient implements ICoreUsageStatsClient { private readonly fieldsToIncrement$ = new Subject(); private readonly flush$ = new Subject(); private readonly coreUsageEvents$ = new Subject(); + private readonly coreUsageDeprecatedApiCalls$ = new Subject(); + private readonly fetchDeprecatedUsageStats: DeprecatedApiUsageFetcher; constructor({ debugLogger, @@ -134,10 +148,12 @@ export class CoreUsageStatsClient implements ICoreUsageStatsClient { stop$, incrementUsageCounter, bufferTimeMs = DEFAULT_BUFFER_TIME_MS, + fetchDeprecatedUsageStats, }: CoreUsageStatsClientParams) { this.debugLogger = debugLogger; this.basePath = basePath; this.repositoryPromise = repositoryPromise; + this.fetchDeprecatedUsageStats = fetchDeprecatedUsageStats; this.fieldsToIncrement$ .pipe( takeUntil(stop$), @@ -180,6 +196,28 @@ export class CoreUsageStatsClient implements ICoreUsageStatsClient { ) .subscribe(); + this.coreUsageDeprecatedApiCalls$ + .pipe( + takeUntil(stop$), + tap(({ id, incrementBy, resolved }) => { + incrementUsageCounter({ + counterName: id, + counterType: `deprecated_api_call:${resolved ? 'resolved' : 'total'}`, + incrementBy, + }); + + if (resolved) { + // increment number of times the marked_as_resolve has been called + incrementUsageCounter({ + counterName: id, + counterType: 'deprecated_api_call:marked_as_resolved', + incrementBy: 1, + }); + } + }) + ) + .subscribe(); + this.coreUsageEvents$ .pipe( takeUntil(stop$), @@ -215,6 +253,20 @@ export class CoreUsageStatsClient implements ICoreUsageStatsClient { return coreUsageStats; } + public async incrementDeprecatedApi( + id: string, + { resolved = false, incrementBy = 1 }: { resolved: boolean; incrementBy: number } + ) { + const deprecatedField = resolved ? 'deprecated_api_calls_resolved' : 'deprecated_api_calls'; + this.coreUsageDeprecatedApiCalls$.next({ id, resolved, incrementBy }); + this.fieldsToIncrement$.next([`${deprecatedField}.total`]); + } + + public async getDeprecatedApiUsageStats() { + const repository = await this.repositoryPromise; + return await this.fetchDeprecatedUsageStats({ soClient: repository }); + } + public async incrementSavedObjectsBulkCreate(options: BaseIncrementOptions) { await this.updateUsageStats([], BULK_CREATE_STATS_PREFIX, options); } diff --git a/packages/core/usage-data/core-usage-data-server-mocks/src/core_usage_data_service.mock.ts b/packages/core/usage-data/core-usage-data-server-mocks/src/core_usage_data_service.mock.ts index c85aee50653d2..57628901c1a60 100644 --- a/packages/core/usage-data/core-usage-data-server-mocks/src/core_usage_data_service.mock.ts +++ b/packages/core/usage-data/core-usage-data-server-mocks/src/core_usage_data_service.mock.ts @@ -17,6 +17,7 @@ import { coreUsageStatsClientMock } from './core_usage_stats_client.mock'; const createSetupContractMock = (usageStatsClient = coreUsageStatsClientMock.create()) => { const setupContract: jest.Mocked = { registerType: jest.fn(), + registerDeprecatedUsageFetch: jest.fn(), getClient: jest.fn().mockReturnValue(usageStatsClient), registerUsageCounter: jest.fn(), incrementUsageCounter: jest.fn(), diff --git a/packages/core/usage-data/core-usage-data-server-mocks/src/core_usage_stats_client.mock.ts b/packages/core/usage-data/core-usage-data-server-mocks/src/core_usage_stats_client.mock.ts index f6b11204ca040..8896e9066ef78 100644 --- a/packages/core/usage-data/core-usage-data-server-mocks/src/core_usage_stats_client.mock.ts +++ b/packages/core/usage-data/core-usage-data-server-mocks/src/core_usage_stats_client.mock.ts @@ -12,6 +12,7 @@ import type { ICoreUsageStatsClient } from '@kbn/core-usage-data-base-server-int const createUsageStatsClientMock = () => ({ getUsageStats: jest.fn().mockResolvedValue({}), + getDeprecatedApiUsageStats: jest.fn().mockResolvedValue([]), incrementSavedObjectsBulkCreate: jest.fn().mockResolvedValue(null), incrementSavedObjectsBulkGet: jest.fn().mockResolvedValue(null), incrementSavedObjectsBulkResolve: jest.fn().mockResolvedValue(null), diff --git a/packages/core/usage-data/core-usage-data-server/index.ts b/packages/core/usage-data/core-usage-data-server/index.ts index 77fb0b1066750..45ed369ca8381 100644 --- a/packages/core/usage-data/core-usage-data-server/index.ts +++ b/packages/core/usage-data/core-usage-data-server/index.ts @@ -19,4 +19,6 @@ export type { CoreConfigUsageData, CoreServicesUsageData, CoreUsageStats, + CoreDeprecatedApiUsageStats, + DeprecatedApiUsageFetcher, } from './src'; diff --git a/packages/core/usage-data/core-usage-data-server/src/core_usage_stats.ts b/packages/core/usage-data/core-usage-data-server/src/core_usage_stats.ts index 36409a097129c..39df9d30d19c9 100644 --- a/packages/core/usage-data/core-usage-data-server/src/core_usage_stats.ts +++ b/packages/core/usage-data/core-usage-data-server/src/core_usage_stats.ts @@ -146,3 +146,16 @@ export interface CoreUsageStats { 'savedObjectsRepository.resolvedOutcome.notFound'?: number; 'savedObjectsRepository.resolvedOutcome.total'?: number; } + +/** + * @public + * + * CoreDeprecatedApiUsageStats are collected over time while Kibana is running. + */ +export interface CoreDeprecatedApiUsageStats { + apiId: string; + totalMarkedAsResolved: number; + markedAsResolvedLastCalledAt: string; + apiTotalCalls: number; + apiLastCalledAt: string; +} diff --git a/packages/core/usage-data/core-usage-data-server/src/index.ts b/packages/core/usage-data/core-usage-data-server/src/index.ts index 01cd52adbe986..05d0f02500053 100644 --- a/packages/core/usage-data/core-usage-data-server/src/index.ts +++ b/packages/core/usage-data/core-usage-data-server/src/index.ts @@ -12,11 +12,12 @@ export type { CoreEnvironmentUsageData, CoreConfigUsageData, } from './core_usage_data'; -export type { CoreUsageStats } from './core_usage_stats'; +export type { CoreUsageStats, CoreDeprecatedApiUsageStats } from './core_usage_stats'; export type { CoreUsageDataSetup, CoreUsageCounter, CoreIncrementUsageCounter, CoreIncrementCounterParams, + DeprecatedApiUsageFetcher, } from './setup_contract'; export type { CoreUsageData, ConfigUsageData, CoreUsageDataStart } from './start_contract'; diff --git a/packages/core/usage-data/core-usage-data-server/src/setup_contract.ts b/packages/core/usage-data/core-usage-data-server/src/setup_contract.ts index bd87563792e6d..30ed7edb6ce1d 100644 --- a/packages/core/usage-data/core-usage-data-server/src/setup_contract.ts +++ b/packages/core/usage-data/core-usage-data-server/src/setup_contract.ts @@ -7,12 +7,12 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ +import type { ISavedObjectsRepository } from '@kbn/core-saved-objects-api-server'; +import type { CoreDeprecatedApiUsageStats } from './core_usage_stats'; + /** * Internal API for registering the Usage Tracker used for Core's usage data payload. * - * @note This API should never be used to drive application logic and is only - * intended for telemetry purposes. - * * @public */ export interface CoreUsageDataSetup { @@ -21,6 +21,7 @@ export interface CoreUsageDataSetup { * when tracking events. */ registerUsageCounter: (usageCounter: CoreUsageCounter) => void; + registerDeprecatedUsageFetch: (fetchFn: DeprecatedApiUsageFetcher) => void; } /** @@ -49,3 +50,11 @@ export interface CoreIncrementCounterParams { * Method to call whenever an event occurs, so the counter can be increased. */ export type CoreIncrementUsageCounter = (params: CoreIncrementCounterParams) => void; + +/** + * @public + * Registers the deprecated API fetcher to be called to grab all the deprecated API usage details. + */ +export type DeprecatedApiUsageFetcher = (params: { + soClient: ISavedObjectsRepository; +}) => Promise; diff --git a/packages/core/usage-data/core-usage-data-server/tsconfig.json b/packages/core/usage-data/core-usage-data-server/tsconfig.json index 77d0aa6ade3b1..83abb04d150ac 100644 --- a/packages/core/usage-data/core-usage-data-server/tsconfig.json +++ b/packages/core/usage-data/core-usage-data-server/tsconfig.json @@ -12,5 +12,8 @@ ], "exclude": [ "target/**/*", + ], + "kbn_references": [ + "@kbn/core-saved-objects-api-server", ] } diff --git a/packages/kbn-router-to-openapispec/src/__snapshots__/generate_oas.test.ts.snap b/packages/kbn-router-to-openapispec/src/__snapshots__/generate_oas.test.ts.snap index 818c0502ad774..fef935624ae64 100644 --- a/packages/kbn-router-to-openapispec/src/__snapshots__/generate_oas.test.ts.snap +++ b/packages/kbn-router-to-openapispec/src/__snapshots__/generate_oas.test.ts.snap @@ -572,6 +572,7 @@ OK response oas-test-version-2", }, "/no-xsrf/{id}/{path*}": Object { "post": Object { + "deprecated": true, "operationId": "%2Fno-xsrf%2F%7Bid%7D%2F%7Bpath*%7D#1", "parameters": Array [ Object { diff --git a/packages/kbn-router-to-openapispec/src/generate_oas.test.ts b/packages/kbn-router-to-openapispec/src/generate_oas.test.ts index c3532312d3088..25b786ac7c2c7 100644 --- a/packages/kbn-router-to-openapispec/src/generate_oas.test.ts +++ b/packages/kbn-router-to-openapispec/src/generate_oas.test.ts @@ -189,6 +189,7 @@ describe('generateOpenApiDocument', () => { versionedRouters: { testVersionedRouter: { routes: [{}] } }, bodySchema: createSharedZodSchema(), }); + expect( generateOpenApiDocument( { @@ -240,6 +241,7 @@ describe('generateOpenApiDocument', () => { { method: 'get', path: '/test', + isVersioned: true, options: { access: 'public' }, handlers: [ { diff --git a/packages/kbn-router-to-openapispec/src/generate_oas.test.util.ts b/packages/kbn-router-to-openapispec/src/generate_oas.test.util.ts index 898f234cdc310..a39afb6357bfc 100644 --- a/packages/kbn-router-to-openapispec/src/generate_oas.test.util.ts +++ b/packages/kbn-router-to-openapispec/src/generate_oas.test.util.ts @@ -10,6 +10,7 @@ import type { ZodType } from '@kbn/zod'; import { schema, Type } from '@kbn/config-schema'; import type { CoreVersionedRouter, Router } from '@kbn/core-http-router-server-internal'; +import type { RouterRoute, VersionedRouterRoute } from '@kbn/core-http-server'; import { createLargeSchema } from './oas_converter/kbn_config_schema/lib.test.util'; type RoutesMeta = ReturnType[number]; @@ -27,7 +28,7 @@ export const createVersionedRouter = (args: { routes: VersionedRoutesMeta[] }) = } as unknown as CoreVersionedRouter; }; -export const getRouterDefaults = (bodySchema?: RuntimeSchema) => ({ +export const getRouterDefaults = (bodySchema?: RuntimeSchema): RouterRoute => ({ isVersioned: false, path: '/foo/{id}/{path*}', method: 'get', @@ -57,22 +58,29 @@ export const getRouterDefaults = (bodySchema?: RuntimeSchema) => ({ handler: jest.fn(), }); -export const getVersionedRouterDefaults = (bodySchema?: RuntimeSchema) => ({ +export const getVersionedRouterDefaults = (bodySchema?: RuntimeSchema): VersionedRouterRoute => ({ method: 'get', path: '/bar', options: { summary: 'versioned route', access: 'public', - deprecated: true, discontinued: 'route discontinued version or date', options: { tags: ['ignore-me', 'oas-tag:versioned'], }, }, + isVersioned: true, handlers: [ { fn: jest.fn(), options: { + options: { + deprecated: { + documentationUrl: 'https://fake-url', + reason: { type: 'remove' }, + severity: 'critical', + }, + }, validate: { request: { body: diff --git a/packages/kbn-router-to-openapispec/src/process_router.ts b/packages/kbn-router-to-openapispec/src/process_router.ts index 1884b6dddf863..f0d37fd208b7b 100644 --- a/packages/kbn-router-to-openapispec/src/process_router.ts +++ b/packages/kbn-router-to-openapispec/src/process_router.ts @@ -63,11 +63,12 @@ export const processRouter = ( parameters.push(...pathObjects, ...queryObjects); } + const hasDeprecations = !!route.options.deprecated; const operation: CustomOperationObject = { summary: route.options.summary ?? '', tags: route.options.tags ? extractTags(route.options.tags) : [], ...(route.options.description ? { description: route.options.description } : {}), - ...(route.options.deprecated ? { deprecated: route.options.deprecated } : {}), + ...(hasDeprecations ? { deprecated: true } : {}), ...(route.options.discontinued ? { 'x-discontinued': route.options.discontinued } : {}), requestBody: !!validationSchemas?.body ? { diff --git a/packages/kbn-router-to-openapispec/src/process_versioned_router.test.ts b/packages/kbn-router-to-openapispec/src/process_versioned_router.test.ts index 6452c2cf3c2cc..f9f4f4898c1d0 100644 --- a/packages/kbn-router-to-openapispec/src/process_versioned_router.test.ts +++ b/packages/kbn-router-to-openapispec/src/process_versioned_router.test.ts @@ -8,10 +8,7 @@ */ import { schema } from '@kbn/config-schema'; -import type { - CoreVersionedRouter, - VersionedRouterRoute, -} from '@kbn/core-http-router-server-internal'; +import type { CoreVersionedRouter } from '@kbn/core-http-router-server-internal'; import { get } from 'lodash'; import { OasConverter } from './oas_converter'; import { createOperationIdCounter } from './operation_id_counter'; @@ -20,6 +17,7 @@ import { extractVersionedResponses, extractVersionedRequestBodies, } from './process_versioned_router'; +import { VersionedRouterRoute } from '@kbn/core-http-server'; let oasConverter: OasConverter; beforeEach(() => { @@ -151,6 +149,7 @@ describe('processVersionedRouter', () => { const createTestRoute: () => VersionedRouterRoute = () => ({ path: '/foo', method: 'get', + isVersioned: true, options: { access: 'public', deprecated: true, diff --git a/packages/kbn-router-to-openapispec/src/process_versioned_router.ts b/packages/kbn-router-to-openapispec/src/process_versioned_router.ts index 5446f1409760d..7eee0d20c11d2 100644 --- a/packages/kbn-router-to-openapispec/src/process_versioned_router.ts +++ b/packages/kbn-router-to-openapispec/src/process_versioned_router.ts @@ -10,10 +10,9 @@ import { type CoreVersionedRouter, versionHandlerResolvers, - VersionedRouterRoute, unwrapVersionedResponseBodyValidation, } from '@kbn/core-http-router-server-internal'; -import type { RouteMethod } from '@kbn/core-http-server'; +import type { RouteMethod, VersionedRouterRoute } from '@kbn/core-http-server'; import type { OpenAPIV3 } from 'openapi-types'; import type { GenerateOpenApiDocumentOptionsFilters } from './generate_oas'; import type { OasConverter } from './oas_converter'; @@ -94,11 +93,13 @@ export const processVersionedRouter = ( const hasBody = Boolean(extractValidationSchemaFromVersionedHandler(handler)?.request?.body); const contentType = extractContentType(route.options.options?.body); const hasVersionFilter = Boolean(filters?.version); + // If any handler is deprecated we show deprecated: true in the spec + const hasDeprecations = route.handlers.some(({ options }) => !!options.options?.deprecated); const operation: OpenAPIV3.OperationObject = { summary: route.options.summary ?? '', tags: route.options.options?.tags ? extractTags(route.options.options.tags) : [], ...(route.options.description ? { description: route.options.description } : {}), - ...(route.options.deprecated ? { deprecated: route.options.deprecated } : {}), + ...(hasDeprecations ? { deprecated: true } : {}), ...(route.options.discontinued ? { 'x-discontinued': route.options.discontinued } : {}), requestBody: hasBody ? { diff --git a/src/core/server/integration_tests/http/request_representation.test.ts b/src/core/server/integration_tests/http/request_representation.test.ts index f180a3a49ce0f..82300eceec774 100644 --- a/src/core/server/integration_tests/http/request_representation.test.ts +++ b/src/core/server/integration_tests/http/request_representation.test.ts @@ -87,6 +87,7 @@ describe('request logging', () => { route: { method: 'get', path: '/', + routePath: '/', options: expect.any(Object), }, uuid: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', @@ -116,10 +117,12 @@ describe('request logging', () => { auth: { isAuthenticated: false }, route: { path: '/', + routePath: '/', method: 'get', options: { authRequired: true, xsrfRequired: false, + deprecated: undefined, access: 'internal', tags: [], security: undefined, @@ -127,7 +130,8 @@ describe('request logging', () => { body: undefined } }, - authzResult: undefined + authzResult: undefined, + apiVersion: undefined }" `); }); diff --git a/src/plugins/kibana_usage_collection/server/collectors/common/counters.test.ts b/src/plugins/kibana_usage_collection/server/collectors/common/counters.test.ts index 6a64e6b597e6a..33e9b68f59c4a 100644 --- a/src/plugins/kibana_usage_collection/server/collectors/common/counters.test.ts +++ b/src/plugins/kibana_usage_collection/server/collectors/common/counters.test.ts @@ -118,7 +118,6 @@ describe('createCounterFetcher', () => { dailyEvents, }) ); - // @ts-expect-error incomplete mock implementation const { dailyEvents } = await fetch({ soClient: soClientMock }); expect(dailyEvents).toHaveLength(5); const intersectingEntry = dailyEvents.find( diff --git a/src/plugins/kibana_usage_collection/server/collectors/common/counters.ts b/src/plugins/kibana_usage_collection/server/collectors/common/counters.ts index 9300ddcb959aa..df0ca67a85198 100644 --- a/src/plugins/kibana_usage_collection/server/collectors/common/counters.ts +++ b/src/plugins/kibana_usage_collection/server/collectors/common/counters.ts @@ -30,7 +30,7 @@ export function createCounterFetcher( filter: string, transform: (counters: CounterEvent[]) => T ) { - return async ({ soClient }: CollectorFetchContext) => { + return async ({ soClient }: Pick) => { const finder = soClient.createPointInTimeFinder({ type: USAGE_COUNTERS_SAVED_OBJECT_TYPE, namespaces: ['*'], diff --git a/src/plugins/kibana_usage_collection/server/collectors/core/fetch_deprecated_api_counters.ts b/src/plugins/kibana_usage_collection/server/collectors/core/fetch_deprecated_api_counters.ts new file mode 100644 index 0000000000000..1c90b23e40b16 --- /dev/null +++ b/src/plugins/kibana_usage_collection/server/collectors/core/fetch_deprecated_api_counters.ts @@ -0,0 +1,69 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import type { Logger } from '@kbn/logging'; +import type { CoreDeprecatedApiUsageStats } from '@kbn/core-usage-data-server'; +import { USAGE_COUNTERS_SAVED_OBJECT_TYPE } from '@kbn/usage-collection-plugin/server'; + +import { createCounterFetcher, type CounterEvent } from '../common/counters'; + +const DEPRECATED_API_COUNTERS_FILTER = `${USAGE_COUNTERS_SAVED_OBJECT_TYPE}.attributes.counterType: deprecated_api_call\\:*`; + +const mergeCounter = (counter: CounterEvent, acc?: CoreDeprecatedApiUsageStats) => { + if (acc && acc?.apiId !== counter.counterName) { + throw new Error( + `Failed to merge mismatching counterNames: ${acc.apiId} with ${counter.counterName}` + ); + } + const isMarkedCounter = counter.counterType.endsWith(':marked_as_resolved'); + + const finalCounter = { + apiId: counter.counterName, + apiTotalCalls: 0, + apiLastCalledAt: 'unknown', + totalMarkedAsResolved: 0, + markedAsResolvedLastCalledAt: 'unknown', + ...(acc || {}), + }; + + if (isMarkedCounter) { + return finalCounter; + } + + const isResolvedCounter = counter.counterType.endsWith(':resolved'); + const totalKey = isResolvedCounter ? 'totalMarkedAsResolved' : 'apiTotalCalls'; + const lastUpdatedKey = isResolvedCounter ? 'markedAsResolvedLastCalledAt' : 'apiLastCalledAt'; + + const newPayload = { + [totalKey]: (finalCounter[totalKey] || 0) + counter.total, + [lastUpdatedKey]: counter.lastUpdatedAt, + }; + + return { + ...finalCounter, + ...newPayload, + }; +}; + +function mergeCounters(counters: CounterEvent[]): CoreDeprecatedApiUsageStats[] { + const mergedCounters = counters.reduce((acc, counter) => { + const { counterName } = counter; + const existingCounter = acc[counterName]; + + acc[counterName] = mergeCounter(counter, existingCounter); + + return acc; + }, {} as Record); + + return Object.values(mergedCounters); +} + +export const fetchDeprecatedApiCounterStats = (logger: Logger) => { + return createCounterFetcher(logger, DEPRECATED_API_COUNTERS_FILTER, mergeCounters); +}; diff --git a/src/plugins/kibana_usage_collection/server/collectors/core/index.ts b/src/plugins/kibana_usage_collection/server/collectors/core/index.ts index 0e0f783b0f847..e298560893ccb 100644 --- a/src/plugins/kibana_usage_collection/server/collectors/core/index.ts +++ b/src/plugins/kibana_usage_collection/server/collectors/core/index.ts @@ -8,3 +8,4 @@ */ export { registerCoreUsageCollector } from './core_usage_collector'; +export { fetchDeprecatedApiCounterStats } from './fetch_deprecated_api_counters'; diff --git a/src/plugins/kibana_usage_collection/server/collectors/index.ts b/src/plugins/kibana_usage_collection/server/collectors/index.ts index ef5287324ee59..c22fb3b5697f8 100644 --- a/src/plugins/kibana_usage_collection/server/collectors/index.ts +++ b/src/plugins/kibana_usage_collection/server/collectors/index.ts @@ -17,7 +17,7 @@ export { export { registerOpsStatsCollector } from './ops_stats'; export { registerCloudProviderUsageCollector } from './cloud'; export { registerCspCollector } from './csp'; -export { registerCoreUsageCollector } from './core'; +export { registerCoreUsageCollector, fetchDeprecatedApiCounterStats } from './core'; export { registerLocalizationUsageCollector } from './localization'; export { registerConfigUsageCollector } from './config_usage'; export { registerUiCountersUsageCollector } from './ui_counters'; diff --git a/src/plugins/kibana_usage_collection/server/plugin.ts b/src/plugins/kibana_usage_collection/server/plugin.ts index bbfc010c0e065..48fb1c6ff7b9b 100644 --- a/src/plugins/kibana_usage_collection/server/plugin.ts +++ b/src/plugins/kibana_usage_collection/server/plugin.ts @@ -43,6 +43,7 @@ import { registerUsageCountersUsageCollector, registerSavedObjectsCountUsageCollector, registerEventLoopDelaysCollector, + fetchDeprecatedApiCounterStats, } from './collectors'; interface KibanaUsageCollectionPluginsDepsSetup { @@ -74,6 +75,10 @@ export class KibanaUsageCollectionPlugin implements Plugin { registerEbtCounters(coreSetup.analytics, usageCollection); this.eventLoopUsageCounter = usageCollection.createUsageCounter('eventLoop'); coreSetup.coreUsageData.registerUsageCounter(usageCollection.createUsageCounter('core')); + const deprecatedUsageFetch = fetchDeprecatedApiCounterStats( + this.logger.get('deprecated-api-usage') + ); + coreSetup.coreUsageData.registerDeprecatedUsageFetch(deprecatedUsageFetch); this.registerUsageCollectors( usageCollection, coreSetup, diff --git a/test/plugin_functional/test_suites/core/deprecations.ts b/test/plugin_functional/test_suites/core/deprecations.ts index 081209e6c7dce..b41adc7ffefa5 100644 --- a/test/plugin_functional/test_suites/core/deprecations.ts +++ b/test/plugin_functional/test_suites/core/deprecations.ts @@ -151,7 +151,7 @@ export default function ({ getService, getPageObjects }: PluginFunctionalProvide }); expect(resolveResult).to.eql({ - reason: 'This deprecation cannot be resolved automatically.', + reason: 'This deprecation cannot be resolved automatically or marked as resolved.', status: 'fail', }); }); diff --git a/x-pack/plugins/actions/server/routes/legacy/create.ts b/x-pack/plugins/actions/server/routes/legacy/create.ts index 6da0d25c2e59c..f667a9e003a77 100644 --- a/x-pack/plugins/actions/server/routes/legacy/create.ts +++ b/x-pack/plugins/actions/server/routes/legacy/create.ts @@ -38,6 +38,7 @@ export const createActionRoute = ( access: 'public', summary: `Create a connector`, tags: ['oas-tag:connectors'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, validate: { diff --git a/x-pack/plugins/actions/server/routes/legacy/delete.ts b/x-pack/plugins/actions/server/routes/legacy/delete.ts index 2204095e03801..c7e1e985cc6f0 100644 --- a/x-pack/plugins/actions/server/routes/legacy/delete.ts +++ b/x-pack/plugins/actions/server/routes/legacy/delete.ts @@ -32,6 +32,7 @@ export const deleteActionRoute = ( summary: `Delete a connector`, description: 'WARNING: When you delete a connector, it cannot be recovered.', tags: ['oas-tag:connectors'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, validate: { diff --git a/x-pack/plugins/actions/server/routes/legacy/execute.ts b/x-pack/plugins/actions/server/routes/legacy/execute.ts index 8083f884c1186..71b04262075d4 100644 --- a/x-pack/plugins/actions/server/routes/legacy/execute.ts +++ b/x-pack/plugins/actions/server/routes/legacy/execute.ts @@ -37,6 +37,7 @@ export const executeActionRoute = ( options: { access: 'public', summary: `Run a connector`, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, tags: ['oas-tag:connectors'], }, diff --git a/x-pack/plugins/actions/server/routes/legacy/get.ts b/x-pack/plugins/actions/server/routes/legacy/get.ts index 2adf6413b9248..571849ccaf478 100644 --- a/x-pack/plugins/actions/server/routes/legacy/get.ts +++ b/x-pack/plugins/actions/server/routes/legacy/get.ts @@ -31,6 +31,7 @@ export const getActionRoute = ( options: { access: 'public', summary: `Get connector information`, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, tags: ['oas-tag:connectors'], }, diff --git a/x-pack/plugins/actions/server/routes/legacy/get_all.ts b/x-pack/plugins/actions/server/routes/legacy/get_all.ts index 04ba20f4fb3c8..f0a17acb96691 100644 --- a/x-pack/plugins/actions/server/routes/legacy/get_all.ts +++ b/x-pack/plugins/actions/server/routes/legacy/get_all.ts @@ -23,6 +23,7 @@ export const getAllActionRoute = ( options: { access: 'public', summary: `Get all connectors`, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, tags: ['oas-tag:connectors'], }, diff --git a/x-pack/plugins/actions/server/routes/legacy/list_action_types.ts b/x-pack/plugins/actions/server/routes/legacy/list_action_types.ts index f42cd7479286c..cc3e9c23f240d 100644 --- a/x-pack/plugins/actions/server/routes/legacy/list_action_types.ts +++ b/x-pack/plugins/actions/server/routes/legacy/list_action_types.ts @@ -27,6 +27,7 @@ export const listActionTypesRoute = ( options: { access: 'public', summary: `Get connector types`, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, tags: ['oas-tag:connectors'], }, diff --git a/x-pack/plugins/actions/server/routes/legacy/update.ts b/x-pack/plugins/actions/server/routes/legacy/update.ts index 81106c2cdc73b..0bf1ec7ece55d 100644 --- a/x-pack/plugins/actions/server/routes/legacy/update.ts +++ b/x-pack/plugins/actions/server/routes/legacy/update.ts @@ -37,6 +37,7 @@ export const updateActionRoute = ( options: { access: 'public', summary: `Update a connector`, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, tags: ['oas-tag:connectors'], }, diff --git a/x-pack/plugins/alerting/server/routes/legacy/create.ts b/x-pack/plugins/alerting/server/routes/legacy/create.ts index d8505fdc8453d..333877b7df49e 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/create.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/create.ts @@ -65,6 +65,7 @@ export const createAlertRoute = ({ access: isServerless ? 'internal' : 'public', summary: 'Create an alert', tags: ['oas-tag:alerting'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, }, diff --git a/x-pack/plugins/alerting/server/routes/legacy/delete.ts b/x-pack/plugins/alerting/server/routes/legacy/delete.ts index f931af10ccbbf..2b63de9e4ee73 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/delete.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/delete.ts @@ -33,6 +33,7 @@ export const deleteAlertRoute = ( access: isServerless ? 'internal' : 'public', summary: 'Delete an alert', tags: ['oas-tag:alerting'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, }, diff --git a/x-pack/plugins/alerting/server/routes/legacy/disable.ts b/x-pack/plugins/alerting/server/routes/legacy/disable.ts index 486bef89dd197..0c6f3cf062a0c 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/disable.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/disable.ts @@ -34,6 +34,7 @@ export const disableAlertRoute = ( access: isServerless ? 'internal' : 'public', summary: 'Disable an alert', tags: ['oas-tag:alerting'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, }, diff --git a/x-pack/plugins/alerting/server/routes/legacy/enable.ts b/x-pack/plugins/alerting/server/routes/legacy/enable.ts index c5076b3de1a54..d52eaa784f670 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/enable.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/enable.ts @@ -35,6 +35,7 @@ export const enableAlertRoute = ( access: isServerless ? 'internal' : 'public', summary: 'Enable an alert', tags: ['oas-tag:alerting'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, }, diff --git a/x-pack/plugins/alerting/server/routes/legacy/find.ts b/x-pack/plugins/alerting/server/routes/legacy/find.ts index ad85f3c7333b0..fa309ae51f2e4 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/find.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/find.ts @@ -79,6 +79,7 @@ export const findAlertRoute = ( tags: ['oas-tag:alerting'], description: 'Gets a paginated set of alerts. Alert `params` are stored as a flattened field type and analyzed as keywords. As alerts change in Kibana, the results on each page of the response also change. Use the find API for traditional paginated results, but avoid using it to export large amounts of data.', + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, }, diff --git a/x-pack/plugins/alerting/server/routes/legacy/get.ts b/x-pack/plugins/alerting/server/routes/legacy/get.ts index 8437b888f7c0f..e5eff52bf02d6 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/get.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/get.ts @@ -33,6 +33,7 @@ export const getAlertRoute = ( access: isServerless ? 'internal' : 'public', summary: 'Get an alert', tags: ['oas-tag:alerting'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, }, diff --git a/x-pack/plugins/alerting/server/routes/legacy/get_alert_instance_summary.ts b/x-pack/plugins/alerting/server/routes/legacy/get_alert_instance_summary.ts index fd1fae64b538f..58a75dd68dce7 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/get_alert_instance_summary.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/get_alert_instance_summary.ts @@ -44,6 +44,7 @@ export const getAlertInstanceSummaryRoute = ( access: isServerless ? 'internal' : 'public', summary: 'Get an alert summary', tags: ['oas-tag:alerting'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, }, diff --git a/x-pack/plugins/alerting/server/routes/legacy/get_alert_state.ts b/x-pack/plugins/alerting/server/routes/legacy/get_alert_state.ts index 5943ab7203599..e952ef8719667 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/get_alert_state.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/get_alert_state.ts @@ -33,6 +33,7 @@ export const getAlertStateRoute = ( access: isServerless ? 'internal' : 'public', summary: 'Get the state of an alert', tags: ['oas-tag:alerting'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, }, diff --git a/x-pack/plugins/alerting/server/routes/legacy/health.ts b/x-pack/plugins/alerting/server/routes/legacy/health.ts index 90bfda371932a..8f67767941fd2 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/health.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/health.ts @@ -29,6 +29,7 @@ export function healthRoute( access: isServerless ? 'internal' : 'public', summary: 'Get the alerting framework health', tags: ['oas-tag:alerting'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, }, diff --git a/x-pack/plugins/alerting/server/routes/legacy/list_alert_types.ts b/x-pack/plugins/alerting/server/routes/legacy/list_alert_types.ts index 6668ff219ade0..35d6a7efeeee3 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/list_alert_types.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/list_alert_types.ts @@ -25,6 +25,7 @@ export const listAlertTypesRoute = ( access: isServerless ? 'internal' : 'public', summary: 'Get the alert types', tags: ['oas-tag:alerting'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, }, diff --git a/x-pack/plugins/alerting/server/routes/legacy/mute_all.ts b/x-pack/plugins/alerting/server/routes/legacy/mute_all.ts index eaa989dc8fb6a..5c4fc1542ef5b 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/mute_all.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/mute_all.ts @@ -34,6 +34,7 @@ export const muteAllAlertRoute = ( access: isServerless ? 'internal' : 'public', summary: 'Mute all alert instances', tags: ['oas-tag:alerting'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, }, diff --git a/x-pack/plugins/alerting/server/routes/legacy/mute_instance.ts b/x-pack/plugins/alerting/server/routes/legacy/mute_instance.ts index c309dd36b7744..ab0b52d41de29 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/mute_instance.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/mute_instance.ts @@ -37,6 +37,7 @@ export const muteAlertInstanceRoute = ( access: isServerless ? 'internal' : 'public', summary: 'Mute an alert', tags: ['oas-tag:alerting'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, }, diff --git a/x-pack/plugins/alerting/server/routes/legacy/unmute_all.ts b/x-pack/plugins/alerting/server/routes/legacy/unmute_all.ts index 70dfd65e33c79..0681e7d2cf01e 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/unmute_all.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/unmute_all.ts @@ -34,6 +34,7 @@ export const unmuteAllAlertRoute = ( access: isServerless ? 'internal' : 'public', summary: 'Unmute all alert instances', tags: ['oas-tag:alerting'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, }, diff --git a/x-pack/plugins/alerting/server/routes/legacy/unmute_instance.ts b/x-pack/plugins/alerting/server/routes/legacy/unmute_instance.ts index 7990539d6c20d..1101a2b5092e7 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/unmute_instance.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/unmute_instance.ts @@ -35,6 +35,7 @@ export const unmuteAlertInstanceRoute = ( access: isServerless ? 'internal' : 'public', summary: 'Unmute an alert', tags: ['oas-tag:alerting'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, }, diff --git a/x-pack/plugins/alerting/server/routes/legacy/update.ts b/x-pack/plugins/alerting/server/routes/legacy/update.ts index b65579e17b087..01adeb5c634dc 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/update.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/update.ts @@ -61,6 +61,7 @@ export const updateAlertRoute = ( access: isServerless ? 'internal' : 'public', summary: 'Update an alert', tags: ['oas-tag:alerting'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, }, diff --git a/x-pack/plugins/alerting/server/routes/legacy/update_api_key.ts b/x-pack/plugins/alerting/server/routes/legacy/update_api_key.ts index 06c466333967c..30c51d3cdcf5c 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/update_api_key.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/update_api_key.ts @@ -35,6 +35,7 @@ export const updateApiKeyRoute = ( access: isServerless ? 'internal' : 'public', summary: 'Update the API key for an alert', tags: ['oas-tag:alerting'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, }, diff --git a/x-pack/plugins/cases/server/routes/api/comments/get_all_comment.ts b/x-pack/plugins/cases/server/routes/api/comments/get_all_comment.ts index a33b638f5310e..6e8ac79bffec9 100644 --- a/x-pack/plugins/cases/server/routes/api/comments/get_all_comment.ts +++ b/x-pack/plugins/cases/server/routes/api/comments/get_all_comment.ts @@ -31,6 +31,7 @@ export const getAllCommentsRoute = createCasesRoute({ summary: `Gets all case comments`, tags: ['oas-tag:cases'], // description: 'You must have `read` privileges for the **Cases** feature in the **Management**, **Observability**, or **Security** section of the Kibana feature privileges, depending on the owner of the cases with the comments you\'re seeking.', + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, handler: async ({ context, request, response }) => { diff --git a/x-pack/plugins/cases/server/routes/api/stats/get_status.ts b/x-pack/plugins/cases/server/routes/api/stats/get_status.ts index 07d02c0b6713f..dce369e4a0f45 100644 --- a/x-pack/plugins/cases/server/routes/api/stats/get_status.ts +++ b/x-pack/plugins/cases/server/routes/api/stats/get_status.ts @@ -26,6 +26,7 @@ export const getStatusRoute: CaseRoute = createCasesRoute({ description: 'Returns the number of cases that are open, closed, and in progress in the default space.', // You must have `read` privileges for the **Cases** feature in the **Management**, **Observability**, or **Security** section of the Kibana feature privileges, depending on the owner of the cases you're seeking. + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, handler: async ({ context, request, response }) => { diff --git a/x-pack/plugins/cases/server/routes/api/user_actions/get_all_user_actions.ts b/x-pack/plugins/cases/server/routes/api/user_actions/get_all_user_actions.ts index 5edc7a261b3c4..17fe0dcdb9012 100644 --- a/x-pack/plugins/cases/server/routes/api/user_actions/get_all_user_actions.ts +++ b/x-pack/plugins/cases/server/routes/api/user_actions/get_all_user_actions.ts @@ -30,6 +30,7 @@ export const getUserActionsRoute = createCasesRoute({ description: `Returns all user activity for a case.`, // You must have `read` privileges for the **Cases** feature in the **Management**, **Observability**, or **Security** section of the Kibana feature privileges, depending on the owner of the case you're seeking. tags: ['oas-tag:cases'], + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }, handler: async ({ context, request, response }) => { diff --git a/x-pack/plugins/fleet/server/routes/agent/index.ts b/x-pack/plugins/fleet/server/routes/agent/index.ts index f3b6e28a90f37..fc45869dc1219 100644 --- a/x-pack/plugins/fleet/server/routes/agent/index.ts +++ b/x-pack/plugins/fleet/server/routes/agent/index.ts @@ -397,6 +397,7 @@ export const registerAPIRoutes = (router: FleetAuthzRouter, config: FleetConfigT fleetAuthz: { fleet: { allAgents: true }, }, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }) .addVersion( @@ -618,6 +619,7 @@ export const registerAPIRoutes = (router: FleetAuthzRouter, config: FleetConfigT fleetAuthz: { fleet: { readAgents: true }, }, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }) .addVersion( diff --git a/x-pack/plugins/fleet/server/routes/app/index.ts b/x-pack/plugins/fleet/server/routes/app/index.ts index ea5d7be8156d5..c0b7dbcfa1743 100644 --- a/x-pack/plugins/fleet/server/routes/app/index.ts +++ b/x-pack/plugins/fleet/server/routes/app/index.ts @@ -293,6 +293,7 @@ export const registerRoutes = (router: FleetAuthzRouter, config: FleetConfigType fleet: { allAgents: true }, }, description: `Create a service token`, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }) .addVersion( diff --git a/x-pack/plugins/fleet/server/routes/enrollment_api_key/index.ts b/x-pack/plugins/fleet/server/routes/enrollment_api_key/index.ts index a93f63822e5b4..bcf4448420919 100644 --- a/x-pack/plugins/fleet/server/routes/enrollment_api_key/index.ts +++ b/x-pack/plugins/fleet/server/routes/enrollment_api_key/index.ts @@ -161,6 +161,7 @@ export const registerRoutes = (router: FleetAuthzRouter) => { fleetAuthz: { fleet: { readEnrollmentTokens: true }, }, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }) .addVersion( @@ -177,6 +178,7 @@ export const registerRoutes = (router: FleetAuthzRouter) => { fleetAuthz: { fleet: { allAgents: true }, }, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }) .addVersion( @@ -193,6 +195,7 @@ export const registerRoutes = (router: FleetAuthzRouter) => { fleetAuthz: { fleet: { readEnrollmentTokens: true }, }, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }) .addVersion( @@ -209,6 +212,7 @@ export const registerRoutes = (router: FleetAuthzRouter) => { fleetAuthz: { fleet: { allAgents: true }, }, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }) .addVersion( diff --git a/x-pack/plugins/fleet/server/routes/epm/index.ts b/x-pack/plugins/fleet/server/routes/epm/index.ts index c7eddee19b9e0..0e3c5e76eb825 100644 --- a/x-pack/plugins/fleet/server/routes/epm/index.ts +++ b/x-pack/plugins/fleet/server/routes/epm/index.ts @@ -659,6 +659,7 @@ export const registerRoutes = (router: FleetAuthzRouter, config: FleetConfigType fleetAuthz, getRouteRequiredAuthz('get', EPM_API_ROUTES.INFO_PATTERN_DEPRECATED) ).granted, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }) .addVersion( @@ -687,6 +688,7 @@ export const registerRoutes = (router: FleetAuthzRouter, config: FleetConfigType fleetAuthz: { integrations: { writePackageSettings: true }, }, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }) .addVersion( @@ -713,6 +715,7 @@ export const registerRoutes = (router: FleetAuthzRouter, config: FleetConfigType .post({ path: EPM_API_ROUTES.INSTALL_FROM_REGISTRY_PATTERN_DEPRECATED, fleetAuthz: INSTALL_PACKAGES_AUTHZ, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }) .addVersion( @@ -741,6 +744,7 @@ export const registerRoutes = (router: FleetAuthzRouter, config: FleetConfigType fleetAuthz: { integrations: { removePackages: true }, }, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }) .addVersion( diff --git a/x-pack/plugins/ml/server/routes/system.ts b/x-pack/plugins/ml/server/routes/system.ts index 0804a8dc02348..bf4fa3161f5b9 100644 --- a/x-pack/plugins/ml/server/routes/system.ts +++ b/x-pack/plugins/ml/server/routes/system.ts @@ -202,8 +202,9 @@ export function systemRoutes( options: { tags: ['access:ml:canGetJobs'], }, - deprecated: true, summary: 'ES Search wrapper', + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} + deprecated: true, }) .addVersion( { diff --git a/x-pack/plugins/security_solution/server/endpoint/routes/metadata/index.ts b/x-pack/plugins/security_solution/server/endpoint/routes/metadata/index.ts index 9603e9e9a6d48..2f6e46d1d7727 100644 --- a/x-pack/plugins/security_solution/server/endpoint/routes/metadata/index.ts +++ b/x-pack/plugins/security_solution/server/endpoint/routes/metadata/index.ts @@ -95,6 +95,7 @@ export function registerEndpointRoutes( access: 'public', path: METADATA_TRANSFORMS_STATUS_ROUTE, options: { authRequired: true, tags: ['access:securitySolution'] }, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }) .addVersion( diff --git a/x-pack/plugins/security_solution/server/endpoint/routes/suggestions/index.ts b/x-pack/plugins/security_solution/server/endpoint/routes/suggestions/index.ts index a4853d9772ad7..677fb004ee862 100644 --- a/x-pack/plugins/security_solution/server/endpoint/routes/suggestions/index.ts +++ b/x-pack/plugins/security_solution/server/endpoint/routes/suggestions/index.ts @@ -43,6 +43,7 @@ export function registerEndpointSuggestionsRoutes( access: 'public', path: SUGGESTIONS_ROUTE, options: { authRequired: true, tags: ['access:securitySolution'] }, + // @ts-expect-error TODO(https://github.com/elastic/kibana/issues/196095): Replace {RouteDeprecationInfo} deprecated: true, }) .addVersion( diff --git a/x-pack/plugins/upgrade_assistant/README.md b/x-pack/plugins/upgrade_assistant/README.md index 30b403f6d6230..2acac8e3e734d 100644 --- a/x-pack/plugins/upgrade_assistant/README.md +++ b/x-pack/plugins/upgrade_assistant/README.md @@ -17,7 +17,7 @@ When we want to enable ML model snapshot deprecation warnings again we need to c * Migrating system indices (`featureSet.migrateSystemIndices`): Migrating system indices should only be enabled for major version upgrades. This config hides the second step from the UA UI for migrating system indices. * Reindex corrective actions (`featureSet.reindexCorrectiveActions`): Deprecations with reindexing corrective actions are only enabled for major version upgrades. Currently, the reindex actions include some logic that is specific to the [8.0 upgrade](https://github.com/elastic/kibana/blob/main/x-pack/plugins/upgrade_assistant/server/lib/reindexing/index_settings.ts). End users could get into a bad situation if this is enabled before this logic is fixed. -### Deprecations +## Deprecations There are three sources of deprecation information: @@ -275,6 +275,47 @@ PUT .reporting-*/_settings } ``` +#### Kibana API deprecations: +Run kibana locally with the test example plugin that has deprecated routes +``` +yarn start --plugin-path=examples/routing_example --plugin-path=examples/developer_examples +``` + +The following comprehensive deprecated routes examples are registered inside the folder: `examples/routing_example/server/routes/deprecated_routes` + +Run them in the console to trigger the deprecation condition so they show up in the UA: + +``` +# Versioned routes: Version 1 is deprecated +GET kbn:/api/routing_example/d/versioned?apiVersion=1 +GET kbn:/api/routing_example/d/versioned?apiVersion=2 + +# Non-versioned routes +GET kbn:/api/routing_example/d/removed_route +POST kbn:/api/routing_example/d/migrated_route +{} +``` + +1. You can also mark as deprecated in the UA to remove the deprecation from the list. +2. Check the telemetry response to see the reported data about the deprecated route. +3. Calling version 2 of the API does not do anything since it is not deprecated unlike version `1` (`GET kbn:/api/routing_example/d/versioned?apiVersion=2`) +4. Internally you can see the deprecations counters from the dev console by running the following: +``` +GET .kibana_usage_counters/_search +{ + "query": { + "bool": { + "should": [ + {"match": { "usage-counter.counterType": "deprecated_api_call:total"}}, + {"match": { "usage-counter.counterType": "deprecated_api_call:resolved"}}, + {"match": { "usage-counter.counterType": "deprecated_api_call:marked_as_resolved"}} + ] + } + } +} + +``` + For a complete list of Kibana deprecations, refer to the [8.0 Kibana deprecations meta issue](https://github.com/elastic/kibana/issues/109166). ### Errors diff --git a/x-pack/plugins/upgrade_assistant/public/application/components/kibana_deprecations/deprecation_details_flyout.tsx b/x-pack/plugins/upgrade_assistant/public/application/components/kibana_deprecations/deprecation_details_flyout.tsx index a6ea9a26c9bb8..beb4c7c0c678c 100644 --- a/x-pack/plugins/upgrade_assistant/public/application/components/kibana_deprecations/deprecation_details_flyout.tsx +++ b/x-pack/plugins/upgrade_assistant/public/application/components/kibana_deprecations/deprecation_details_flyout.tsx @@ -50,6 +50,12 @@ const i18nTexts = { defaultMessage: 'Quick resolve', } ), + markAsResolvedButtonLabel: i18n.translate( + 'xpack.upgradeAssistant.kibanaDeprecations.flyout.quickResolveButtonLabel', + { + defaultMessage: 'Mark as Resolved', + } + ), retryQuickResolveButtonLabel: i18n.translate( 'xpack.upgradeAssistant.kibanaDeprecations.flyout.retryQuickResolveButtonLabel', { @@ -97,7 +103,15 @@ const i18nTexts = { ), }; -const getQuickResolveButtonLabel = (deprecationResolutionState?: DeprecationResolutionState) => { +interface AvailableCorrectiveActions { + api: boolean; + manual: boolean; + markAsResolved: boolean; +} +const getQuickResolveButtonLabel = ( + deprecationResolutionState: DeprecationResolutionState | undefined, + avilableCorrectiveActions: AvailableCorrectiveActions +) => { if (deprecationResolutionState?.resolveDeprecationStatus === 'in_progress') { return i18nTexts.quickResolveInProgressButtonLabel; } @@ -110,7 +124,13 @@ const getQuickResolveButtonLabel = (deprecationResolutionState?: DeprecationReso return i18nTexts.retryQuickResolveButtonLabel; } - return i18nTexts.quickResolveButtonLabel; + if (avilableCorrectiveActions.api) { + return i18nTexts.quickResolveButtonLabel; + } + + if (avilableCorrectiveActions.markAsResolved) { + return i18nTexts.markAsResolvedButtonLabel; + } }; export const DeprecationDetailsFlyout = ({ @@ -120,9 +140,19 @@ export const DeprecationDetailsFlyout = ({ deprecationResolutionState, }: DeprecationDetailsFlyoutProps) => { const { documentationUrl, message, correctiveActions, title } = deprecation; + const messages = Array.isArray(message) ? message : [message]; + const isCurrent = deprecationResolutionState?.id === deprecation.id; + const avilableCorrectiveActions: AvailableCorrectiveActions = { + api: !!correctiveActions.api, + manual: correctiveActions.manualSteps && correctiveActions.manualSteps.length > 0, + markAsResolved: !!correctiveActions.mark_as_resolved_api, + }; const isResolved = isCurrent && deprecationResolutionState?.resolveDeprecationStatus === 'ok'; + const hasResolveButton = + avilableCorrectiveActions.api || avilableCorrectiveActions.markAsResolved; + const onResolveDeprecation = useCallback(() => { uiMetricService.trackUiMetric(METRIC_TYPE.CLICK, UIM_KIBANA_QUICK_RESOLVE_CLICK); resolveDeprecation(deprecation); @@ -155,7 +185,11 @@ export const DeprecationDetailsFlyout = ({ )} -

{message}

+ {messages.map((m, i) => ( +

+ {m} +

+ ))} {documentationUrl && (

@@ -221,7 +255,7 @@ export const DeprecationDetailsFlyout = ({ {/* Only show the "Quick resolve" button if deprecation supports it and deprecation is not yet resolved */} - {correctiveActions.api && !isResolved && ( + {hasResolveButton && !isResolved && ( - {getQuickResolveButtonLabel(deprecationResolutionState)} + {getQuickResolveButtonLabel(deprecationResolutionState, avilableCorrectiveActions)} )} diff --git a/x-pack/plugins/upgrade_assistant/public/application/components/kibana_deprecations/kibana_deprecations.tsx b/x-pack/plugins/upgrade_assistant/public/application/components/kibana_deprecations/kibana_deprecations.tsx index d76f1afa9e612..0d433a59ee2d9 100644 --- a/x-pack/plugins/upgrade_assistant/public/application/components/kibana_deprecations/kibana_deprecations.tsx +++ b/x-pack/plugins/upgrade_assistant/public/application/components/kibana_deprecations/kibana_deprecations.tsx @@ -126,9 +126,9 @@ export const KibanaDeprecationsList = ({ const [flyoutContent, setFlyoutContent] = useState( undefined ); - const [deprecationResolutionState, setDeprecationResolutionState] = useState< - DeprecationResolutionState | undefined - >(undefined); + const [deprecationResolutionStates, setDeprecationResolutionStates] = useState< + Record + >({}); const { services: { @@ -194,17 +194,25 @@ export const KibanaDeprecationsList = ({ const resolveDeprecation = useCallback( async (deprecationDetails: KibanaDeprecationDetails) => { - setDeprecationResolutionState({ - id: deprecationDetails.id, - resolveDeprecationStatus: 'in_progress', + setDeprecationResolutionStates((states) => { + states[deprecationDetails.id] = { + id: deprecationDetails.id, + resolveDeprecationStatus: 'in_progress', + }; + + return states; }); const response = await deprecations.resolveDeprecation(deprecationDetails); - setDeprecationResolutionState({ - id: deprecationDetails.id, - resolveDeprecationStatus: response.status, - resolveDeprecationError: response.status === 'fail' ? response.reason : undefined, + setDeprecationResolutionStates((states) => { + states[deprecationDetails.id] = { + id: deprecationDetails.id, + resolveDeprecationStatus: response.status, + resolveDeprecationError: response.status === 'fail' ? response.reason : undefined, + }; + + return states; }); closeFlyout(); @@ -221,10 +229,7 @@ export const KibanaDeprecationsList = ({ deprecation: flyoutContent, closeFlyout, resolveDeprecation, - deprecationResolutionState: - deprecationResolutionState && flyoutContent.id === deprecationResolutionState.id - ? deprecationResolutionState - : undefined, + deprecationResolutionState: deprecationResolutionStates[flyoutContent.id], }, flyoutProps: { onClose: closeFlyout, @@ -236,7 +241,7 @@ export const KibanaDeprecationsList = ({ }, [ addContentToGlobalFlyout, closeFlyout, - deprecationResolutionState, + deprecationResolutionStates, flyoutContent, resolveDeprecation, ]); @@ -310,7 +315,7 @@ export const KibanaDeprecationsList = ({ deprecations={kibanaDeprecations} reload={getAllDeprecations} toggleFlyout={toggleFlyout} - deprecationResolutionState={deprecationResolutionState} + deprecationResolutionStates={deprecationResolutionStates} /> ); diff --git a/x-pack/plugins/upgrade_assistant/public/application/components/kibana_deprecations/kibana_deprecations_table.tsx b/x-pack/plugins/upgrade_assistant/public/application/components/kibana_deprecations/kibana_deprecations_table.tsx index 6a757d0cb2b0b..8d223dedca490 100644 --- a/x-pack/plugins/upgrade_assistant/public/application/components/kibana_deprecations/kibana_deprecations_table.tsx +++ b/x-pack/plugins/upgrade_assistant/public/application/components/kibana_deprecations/kibana_deprecations_table.tsx @@ -56,6 +56,12 @@ const i18nTexts = { defaultMessage: 'Feature', } ), + apiDeprecationTypeCellLabel: i18n.translate( + 'xpack.upgradeAssistant.kibanaDeprecations.table.apiDeprecationTypeCellLabel', + { + defaultMessage: 'API', + } + ), unknownDeprecationTypeCellLabel: i18n.translate( 'xpack.upgradeAssistant.kibanaDeprecations.table.unknownDeprecationTypeCellLabel', { @@ -86,14 +92,14 @@ interface Props { deprecations?: KibanaDeprecationDetails[]; reload: () => void; toggleFlyout: (newFlyoutContent?: KibanaDeprecationDetails) => void; - deprecationResolutionState?: DeprecationResolutionState; + deprecationResolutionStates: Record; } export const KibanaDeprecationsTable: React.FunctionComponent = ({ deprecations, reload, toggleFlyout, - deprecationResolutionState, + deprecationResolutionStates, }) => { const columns: Array> = [ { @@ -135,6 +141,8 @@ export const KibanaDeprecationsTable: React.FunctionComponent = ({ return i18nTexts.configDeprecationTypeCellLabel; case 'feature': return i18nTexts.featureDeprecationTypeCellLabel; + case 'api': + return i18nTexts.apiDeprecationTypeCellLabel; case 'uncategorized': default: return i18nTexts.unknownDeprecationTypeCellLabel; @@ -155,7 +163,8 @@ export const KibanaDeprecationsTable: React.FunctionComponent = ({ ); }, @@ -191,6 +200,10 @@ export const KibanaDeprecationsTable: React.FunctionComponent = ({ value: 'feature', name: i18nTexts.featureDeprecationTypeCellLabel, }, + { + value: 'api', + name: i18nTexts.apiDeprecationTypeCellLabel, + }, { value: 'uncategorized', name: i18nTexts.unknownDeprecationTypeCellLabel, diff --git a/x-pack/plugins/upgrade_assistant/public/application/components/kibana_deprecations/resolution_table_cell.tsx b/x-pack/plugins/upgrade_assistant/public/application/components/kibana_deprecations/resolution_table_cell.tsx index 373c9e7b43f52..502c31ae90744 100644 --- a/x-pack/plugins/upgrade_assistant/public/application/components/kibana_deprecations/resolution_table_cell.tsx +++ b/x-pack/plugins/upgrade_assistant/public/application/components/kibana_deprecations/resolution_table_cell.tsx @@ -19,7 +19,7 @@ import { i18n } from '@kbn/i18n'; import type { DeprecationResolutionState } from './kibana_deprecations'; -const i18nTexts = { +const manualI18nTexts = { manualCellLabel: i18n.translate( 'xpack.upgradeAssistant.kibanaDeprecations.table.manualCellLabel', { @@ -32,31 +32,34 @@ const i18nTexts = { defaultMessage: 'This issue needs to be resolved manually.', } ), - automatedCellLabel: i18n.translate( +}; + +const automatedI18nTexts = { + resolutionTypeCellLabel: i18n.translate( 'xpack.upgradeAssistant.kibanaDeprecations.table.automatedCellLabel', { defaultMessage: 'Automated', } ), - automationInProgressCellLabel: i18n.translate( + resolutionProgressCellLabel: i18n.translate( 'xpack.upgradeAssistant.kibanaDeprecations.table.automationInProgressCellLabel', { defaultMessage: 'Resolution in progress…', } ), - automationCompleteCellLabel: i18n.translate( + resolutionCompleteCellLabel: i18n.translate( 'xpack.upgradeAssistant.kibanaDeprecations.table.automationCompleteCellLabel', { defaultMessage: 'Resolved', } ), - automationFailedCellLabel: i18n.translate( + resolutionFailedCellLabel: i18n.translate( 'xpack.upgradeAssistant.kibanaDeprecations.table.automationFailedCellLabel', { defaultMessage: 'Resolution failed', } ), - automatedCellTooltipLabel: i18n.translate( + resolutionCellTooltipLabel: i18n.translate( 'xpack.upgradeAssistant.kibanaDeprecations.table.automatedCellTooltipLabel', { defaultMessage: 'This issue can be resolved automatically.', @@ -64,18 +67,56 @@ const i18nTexts = { ), }; +const markAsResolvedI18nTexts = { + resolutionTypeCellLabel: i18n.translate( + 'xpack.upgradeAssistant.kibanaDeprecations.table.markAsResolvedCellLabel', + { + defaultMessage: 'Mark as resolved', + } + ), + resolutionProgressCellLabel: i18n.translate( + 'xpack.upgradeAssistant.kibanaDeprecations.table.markAsResolvedInProgressCellLabel', + { + defaultMessage: 'Marking as resolved…', + } + ), + resolutionCompleteCellLabel: i18n.translate( + 'xpack.upgradeAssistant.kibanaDeprecations.table.markAsResolvedCompleteCellLabel', + { + defaultMessage: 'Marked as resolved', + } + ), + resolutionFailedCellLabel: i18n.translate( + 'xpack.upgradeAssistant.kibanaDeprecations.table.markAsResolvedFailedCellLabel', + { + defaultMessage: 'Failed to mark as resolved', + } + ), + resolutionCellTooltipLabel: i18n.translate( + 'xpack.upgradeAssistant.kibanaDeprecations.table.markAsResolvedCellTooltipLabel', + { + defaultMessage: 'This issue can be marked as resolved.', + } + ), +}; + interface Props { deprecationId: string; isAutomated: boolean; + canBeMarkedAsResolved: boolean; deprecationResolutionState?: DeprecationResolutionState; } export const ResolutionTableCell: React.FunctionComponent = ({ deprecationId, isAutomated, + canBeMarkedAsResolved, deprecationResolutionState, }) => { - if (isAutomated) { + if (isAutomated || canBeMarkedAsResolved) { + const resolutionI18nTexts = isAutomated ? automatedI18nTexts : markAsResolvedI18nTexts; + const euiIconType = isAutomated ? 'indexSettings' : 'clickLeft'; + if (deprecationResolutionState?.id === deprecationId) { const { resolveDeprecationStatus } = deprecationResolutionState; @@ -87,7 +128,7 @@ export const ResolutionTableCell: React.FunctionComponent = ({ - {i18nTexts.automationInProgressCellLabel} + {resolutionI18nTexts.resolutionProgressCellLabel} ); @@ -98,7 +139,7 @@ export const ResolutionTableCell: React.FunctionComponent = ({ - {i18nTexts.automationFailedCellLabel} + {resolutionI18nTexts.resolutionFailedCellLabel} ); @@ -110,7 +151,7 @@ export const ResolutionTableCell: React.FunctionComponent = ({ - {i18nTexts.automationCompleteCellLabel} + {resolutionI18nTexts.resolutionCompleteCellLabel} ); @@ -118,13 +159,13 @@ export const ResolutionTableCell: React.FunctionComponent = ({ } return ( - + - + - {i18nTexts.automatedCellLabel} + {resolutionI18nTexts.resolutionTypeCellLabel} @@ -134,11 +175,11 @@ export const ResolutionTableCell: React.FunctionComponent = ({ return ( - {i18nTexts.manualCellLabel} + {manualI18nTexts.manualCellLabel} ); diff --git a/x-pack/test/tsconfig.json b/x-pack/test/tsconfig.json index 03fbddf161f00..2ba14ceb1218c 100644 --- a/x-pack/test/tsconfig.json +++ b/x-pack/test/tsconfig.json @@ -185,6 +185,8 @@ "@kbn/cloud-security-posture-common", "@kbn/saved-objects-management-plugin", "@kbn/alerting-types", - "@kbn/ai-assistant-common" + "@kbn/ai-assistant-common", + "@kbn/core-deprecations-common", + "@kbn/usage-collection-plugin" ] } diff --git a/x-pack/test/upgrade_assistant_integration/config.js b/x-pack/test/upgrade_assistant_integration/config.ts similarity index 70% rename from x-pack/test/upgrade_assistant_integration/config.js rename to x-pack/test/upgrade_assistant_integration/config.ts index 9529e4bc568d3..0794f4d0b9ada 100644 --- a/x-pack/test/upgrade_assistant_integration/config.js +++ b/x-pack/test/upgrade_assistant_integration/config.ts @@ -6,8 +6,10 @@ */ import { commonFunctionalServices } from '@kbn/ftr-common-functional-services'; +import { FtrConfigProviderContext } from '@kbn/test'; +import path from 'node:path'; -export default async function ({ readConfigFile }) { +export default async function ({ readConfigFile }: FtrConfigProviderContext) { // Read the Kibana API integration tests config file so that we can utilize its services. const kibanaAPITestsConfig = await readConfigFile( require.resolve('@kbn/test-suites-src/api_integration/config') @@ -26,7 +28,14 @@ export default async function ({ readConfigFile }) { junit: { reportName: 'X-Pack Upgrade Assistant Integration Tests', }, - kbnTestServer: xPackFunctionalTestsConfig.get('kbnTestServer'), + kbnTestServer: { + ...xPackFunctionalTestsConfig.get('kbnTestServer'), + serverArgs: [ + ...xPackFunctionalTestsConfig.get('kbnTestServer.serverArgs'), + `--plugin-path=${path.resolve(__dirname, '../../../examples/routing_example')}`, + `--plugin-path=${path.resolve(__dirname, '../../../examples/developer_examples')}`, + ], + }, esTestCluster: { ...xPackFunctionalTestsConfig.get('esTestCluster'), // this archive can not be loaded into 8.0+ diff --git a/x-pack/test/upgrade_assistant_integration/upgrade_assistant/api_deprecations.ts b/x-pack/test/upgrade_assistant_integration/upgrade_assistant/api_deprecations.ts new file mode 100644 index 0000000000000..f146bf38f5f26 --- /dev/null +++ b/x-pack/test/upgrade_assistant_integration/upgrade_assistant/api_deprecations.ts @@ -0,0 +1,162 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import expect from '@kbn/expect'; +import { expect as expectExpect } from 'expect'; +import type { DomainDeprecationDetails } from '@kbn/core-deprecations-common'; +import { ApiDeprecationDetails } from '@kbn/core-deprecations-common/src/types'; +import { setTimeout as setTimeoutAsync } from 'timers/promises'; +import { UsageCountersSavedObject } from '@kbn/usage-collection-plugin/server'; +import _ from 'lodash'; +import { FtrProviderContext } from '../../common/ftr_provider_context'; + +interface DomainApiDeprecationDetails extends ApiDeprecationDetails { + domainId: string; +} + +const getApiDeprecations = (allDeprecations: DomainDeprecationDetails[]) => { + return allDeprecations.filter( + (deprecation) => deprecation.deprecationType === 'api' + ) as unknown as DomainApiDeprecationDetails[]; +}; + +export default function ({ getService }: FtrProviderContext) { + const supertest = getService('supertest'); + const esArchiver = getService('esArchiver'); + const retry = getService('retry'); + const es = getService('es'); + + describe('Kibana API Deprecations', () => { + before(async () => { + // await kibanaServer.savedObjects.cleanStandardList(); + await esArchiver.emptyKibanaIndex(); + }); + it('returns does not return api deprecations if the routes are not called', async () => { + const { deprecations } = (await supertest.get(`/api/deprecations/`).expect(200)).body; + const apiDeprecations = getApiDeprecations(deprecations); + expect(apiDeprecations.length).to.equal(0); + }); + + it('returns deprecated APIs when the api is called', async () => { + await supertest.get(`/api/routing_example/d/removed_route`).expect(200); + + // sleep a little until the usage counter is synced into ES + await setTimeoutAsync(3000); + await retry.tryForTime( + 15 * 1000, + async () => { + const { deprecations } = (await supertest.get(`/api/deprecations/`).expect(200)).body; + const apiDeprecations = getApiDeprecations(deprecations); + expect(apiDeprecations.length).to.equal(1); + + expectExpect(apiDeprecations[0].correctiveActions.mark_as_resolved_api).toEqual({ + routePath: '/api/routing_example/d/removed_route', + routeMethod: 'get', + apiTotalCalls: 1, + totalMarkedAsResolved: 0, + timestamp: expectExpect.any(String), + }); + + expectExpect(apiDeprecations[0].domainId).toEqual('core.api_deprecations'); + expectExpect(apiDeprecations[0].apiId).toEqual( + 'unversioned|get|/api/routing_example/d/removed_route' + ); + expectExpect(apiDeprecations[0].title).toEqual( + 'The "GET /api/routing_example/d/removed_route" route is removed' + ); + }, + undefined, + 2000 + ); + }); + + it('no longer returns deprecated API when it is marked as resolved', async () => { + await supertest + .post(`/api/deprecations/mark_as_resolved`) + .set('kbn-xsrf', 'xxx') + .send({ + domainId: 'core.api_deprecations', + routePath: '/api/routing_example/d/removed_route', + routeMethod: 'get', + incrementBy: 1, + }) + .expect(200); + + // sleep a little until the usage counter is synced into ES + await setTimeoutAsync(5000); + await retry.tryForTime(15 * 1000, async () => { + const { deprecations } = (await supertest.get(`/api/deprecations/`).expect(200)).body; + const apiDeprecations = getApiDeprecations(deprecations); + expect(apiDeprecations.length).to.equal(0); + }); + }); + + it('returns deprecated API when it is called again after resolved, but with a different message', async () => { + await supertest.get(`/api/routing_example/d/removed_route`).expect(200); + + // sleep a little until the usage counter is synced into ES + await setTimeoutAsync(3000); + await retry.tryForTime( + 15 * 1000, + async () => { + const { deprecations } = (await supertest.get(`/api/deprecations/`).expect(200)).body; + const apiDeprecations = getApiDeprecations(deprecations); + expect(apiDeprecations.length).to.equal(1); + + expectExpect(apiDeprecations[0].correctiveActions.mark_as_resolved_api).toEqual({ + routePath: '/api/routing_example/d/removed_route', + routeMethod: 'get', + apiTotalCalls: 2, + totalMarkedAsResolved: 1, + timestamp: expectExpect.any(String), + }); + }, + undefined, + 2000 + ); + }); + + it('keeps track of all counters via saved objects and core usage counters', async () => { + const should = ['total', 'resolved', 'marked_as_resolved'].map((type) => ({ + match: { 'usage-counter.counterType': `deprecated_api_call:${type}` }, + })); + + const { hits } = await es.search<{ 'usage-counter': UsageCountersSavedObject }>({ + index: '.kibana_usage_counters', + body: { + query: { bool: { should } }, + }, + }); + + expect(hits.hits.length).to.equal(3); + const counters = hits.hits.map((hit) => hit._source!['usage-counter']).sort(); + expectExpect(_.sortBy(counters, 'counterType')).toEqual([ + { + count: 1, + counterName: 'unversioned|get|/api/routing_example/d/removed_route', + counterType: 'deprecated_api_call:marked_as_resolved', + domainId: 'core', + source: 'server', + }, + { + count: 1, + counterName: 'unversioned|get|/api/routing_example/d/removed_route', + counterType: 'deprecated_api_call:resolved', + domainId: 'core', + source: 'server', + }, + { + count: 2, + counterName: 'unversioned|get|/api/routing_example/d/removed_route', + counterType: 'deprecated_api_call:total', + domainId: 'core', + source: 'server', + }, + ]); + }); + }); +} diff --git a/x-pack/test/upgrade_assistant_integration/upgrade_assistant/index.js b/x-pack/test/upgrade_assistant_integration/upgrade_assistant/index.ts similarity index 64% rename from x-pack/test/upgrade_assistant_integration/upgrade_assistant/index.js rename to x-pack/test/upgrade_assistant_integration/upgrade_assistant/index.ts index eb09d24b79b6a..2aaddc7d6f669 100644 --- a/x-pack/test/upgrade_assistant_integration/upgrade_assistant/index.js +++ b/x-pack/test/upgrade_assistant_integration/upgrade_assistant/index.ts @@ -5,8 +5,11 @@ * 2.0. */ -export default function ({ loadTestFile }) { +import { FtrProviderContext } from '../../common/ftr_provider_context'; + +export default function ({ loadTestFile }: FtrProviderContext) { describe('upgrade assistant', function () { loadTestFile(require.resolve('./reindexing')); + loadTestFile(require.resolve('./api_deprecations')); }); } From ae0ac7410de3cfc311d3750d957a81e2a5785d33 Mon Sep 17 00:00:00 2001 From: Krzysztof Kowalczyk Date: Tue, 22 Oct 2024 19:42:20 +0200 Subject: [PATCH 10/22] [Reporting] Add searchSourceStart.create error handling (#197238) ## Summary This PR adds a new type of error (`ReportingSavedObjectNotFoundError`) which gets thrown when passed in saved object doesn't eixst. This produces a log like this: ``` [2024-10-22T15:09:26.768+02:00][ERROR][plugins.reporting.runTask] Error: ReportingError(code: reporting_saved_object_not_found) "Error: Saved object [index-pattern/ff959d40-b880-11e8-a6d9-e546fe2bba5f] not found" ``` Closes: #191548 Closes: #196620 --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> --- packages/kbn-generate-csv/src/generate_csv.ts | 19 ++++++++++++++++++- packages/kbn-generate-csv/tsconfig.json | 1 + packages/kbn-reporting/common/errors.ts | 7 +++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/kbn-generate-csv/src/generate_csv.ts b/packages/kbn-generate-csv/src/generate_csv.ts index cafc6600f51a3..5ed92df84c581 100644 --- a/packages/kbn-generate-csv/src/generate_csv.ts +++ b/packages/kbn-generate-csv/src/generate_csv.ts @@ -26,10 +26,12 @@ import { byteSizeValueToNumber, CancellationToken, ReportingError, + ReportingSavedObjectNotFoundError, } from '@kbn/reporting-common'; import type { TaskInstanceFields, TaskRunResult } from '@kbn/reporting-common/types'; import type { ReportingConfigType } from '@kbn/reporting-server'; +import { TaskErrorSource, createTaskRunError } from '@kbn/task-manager-plugin/server'; import { CONTENT_TYPE_CSV } from '../constants'; import type { JobParamsCSV } from '../types'; import { getExportSettings, type CsvExportSettings } from './lib/get_export_settings'; @@ -235,6 +237,21 @@ export class CsvGenerator { public async generateData(): Promise { const logger = this.logger; + + const createSearchSource = async () => { + try { + const source = await this.dependencies.searchSourceStart.create(this.job.searchSource); + return source; + } catch (err) { + // Saved object not found + if (err?.output?.statusCode === 404) { + const reportingError = new ReportingSavedObjectNotFoundError(err); + throw createTaskRunError(reportingError, TaskErrorSource.USER); + } + throw err; + } + }; + const [settings, searchSource] = await Promise.all([ getExportSettings( this.clients.uiSettings, @@ -243,7 +260,7 @@ export class CsvGenerator { this.job.browserTimezone, logger ), - this.dependencies.searchSourceStart.create(this.job.searchSource), + createSearchSource(), ]); const { startedAt, retryAt } = this.taskInstanceFields; diff --git a/packages/kbn-generate-csv/tsconfig.json b/packages/kbn-generate-csv/tsconfig.json index b57990c20eb4a..4216438b6689a 100644 --- a/packages/kbn-generate-csv/tsconfig.json +++ b/packages/kbn-generate-csv/tsconfig.json @@ -30,5 +30,6 @@ "@kbn/es-types", "@kbn/data-views-plugin", "@kbn/search-types", + "@kbn/task-manager-plugin", ] } diff --git a/packages/kbn-reporting/common/errors.ts b/packages/kbn-reporting/common/errors.ts index 9f45a1b6ae1d5..45a299115bf1b 100644 --- a/packages/kbn-reporting/common/errors.ts +++ b/packages/kbn-reporting/common/errors.ts @@ -152,3 +152,10 @@ export class VisualReportingSoftDisabledError extends ReportingError { }); } } + +export class ReportingSavedObjectNotFoundError extends ReportingError { + static code = 'reporting_saved_object_not_found_error' as const; + public get code(): string { + return ReportingSavedObjectNotFoundError.code; + } +} From c25599ee983c65357a9d63eb843cec101c27f1ea Mon Sep 17 00:00:00 2001 From: Saarika Bhasi <55930906+saarikabhasi@users.noreply.github.com> Date: Tue, 22 Oct 2024 13:59:28 -0400 Subject: [PATCH 11/22] [Search] Enable semantic_text feature in es3 (#197140) ## Summary Semantic text feature was disabled by default due to ML node unable to scale down. With the relevant [PR](https://github.com/elastic/elasticsearch/pull/114323) & subsequent [fix](https://github.com/elastic/elasticsearch/pull/115189) merged, ML node now auto scales when there is no activity. Therefore enabling semantic_text feature in es3. ### Testing instructions * start serverless instance * visit index management index details page -> mappings * Click Add field * Confirm `semantic_text` is shown in the field type form --- config/serverless.es.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/serverless.es.yml b/config/serverless.es.yml index eb3af92c89963..b3b953e5316ac 100644 --- a/config/serverless.es.yml +++ b/config/serverless.es.yml @@ -120,4 +120,4 @@ xpack.searchInferenceEndpoints.ui.enabled: false xpack.search.notebooks.catalog.url: https://elastic-enterprise-search.s3.us-east-2.amazonaws.com/serverless/catalog.json # Semantic text UI -xpack.index_management.dev.enableSemanticText: false +xpack.index_management.dev.enableSemanticText: true From 848c9f48dc101744c8fae69951916dc511dfdc8a Mon Sep 17 00:00:00 2001 From: Vadim Kibana <82822460+vadimkibana@users.noreply.github.com> Date: Tue, 22 Oct 2024 20:05:09 +0200 Subject: [PATCH 12/22] [ES|QL] Normalize multiplication by one when pretty-printing (#197182) ## Summary Partially addresses https://github.com/elastic/kibana/issues/189258 This change will likely supercede https://github.com/elastic/kibana/pull/196817 When parsing, currently ES|QL parsers adds extraneous multiply by 1 or multiply by -1 nodes when parsing arithmetic unary expressions. For example, `-(x)` is parsed as `-1 * x`. This change, reverts these when pretty-printing using the `BasicPrettyPrinter`: `-1 * x` is pretty printed as `-x`. ### Checklist Delete any items that are not applicable to this PR. - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios ### For maintainers - [x] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#_add_your_labels) --- packages/kbn-esql-ast/src/ast/helpers.ts | 27 ++++++- .../__tests__/basic_pretty_printer.test.ts | 62 ++++++++++++++- .../src/pretty_print/basic_pretty_printer.ts | 79 ++++++++++++++++++- .../kbn-esql-ast/src/pretty_print/helpers.ts | 2 +- packages/kbn-esql-ast/src/visitor/utils.ts | 4 + 5 files changed, 169 insertions(+), 5 deletions(-) diff --git a/packages/kbn-esql-ast/src/ast/helpers.ts b/packages/kbn-esql-ast/src/ast/helpers.ts index 9ca49dcb38822..74a7b5c0991e8 100644 --- a/packages/kbn-esql-ast/src/ast/helpers.ts +++ b/packages/kbn-esql-ast/src/ast/helpers.ts @@ -7,11 +7,22 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import { ESQLAstNode, ESQLBinaryExpression, ESQLFunction } from '../types'; +import type { + ESQLAstNode, + ESQLBinaryExpression, + ESQLColumn, + ESQLFunction, + ESQLIntegerLiteral, + ESQLLiteral, + ESQLProperNode, +} from '../types'; import { BinaryExpressionGroup } from './constants'; +export const isProperNode = (node: unknown): node is ESQLProperNode => + !!node && typeof node === 'object' && !Array.isArray(node); + export const isFunctionExpression = (node: unknown): node is ESQLFunction => - !!node && typeof node === 'object' && !Array.isArray(node) && (node as any).type === 'function'; + isProperNode(node) && node.type === 'function'; /** * Returns true if the given node is a binary expression, i.e. an operator @@ -28,6 +39,18 @@ export const isFunctionExpression = (node: unknown): node is ESQLFunction => export const isBinaryExpression = (node: unknown): node is ESQLBinaryExpression => isFunctionExpression(node) && node.subtype === 'binary-expression'; +export const isLiteral = (node: unknown): node is ESQLLiteral => + isProperNode(node) && node.type === 'literal'; + +export const isIntegerLiteral = (node: unknown): node is ESQLIntegerLiteral => + isLiteral(node) && node.literalType === 'integer'; + +export const isDoubleLiteral = (node: unknown): node is ESQLIntegerLiteral => + isLiteral(node) && node.literalType === 'double'; + +export const isColumn = (node: unknown): node is ESQLColumn => + isProperNode(node) && node.type === 'column'; + /** * Returns the group of a binary expression: * diff --git a/packages/kbn-esql-ast/src/pretty_print/__tests__/basic_pretty_printer.test.ts b/packages/kbn-esql-ast/src/pretty_print/__tests__/basic_pretty_printer.test.ts index 20db9e729f094..9e21c45f75b4b 100644 --- a/packages/kbn-esql-ast/src/pretty_print/__tests__/basic_pretty_printer.test.ts +++ b/packages/kbn-esql-ast/src/pretty_print/__tests__/basic_pretty_printer.test.ts @@ -16,7 +16,7 @@ const reprint = (src: string) => { const { root } = parse(src); const text = BasicPrettyPrinter.print(root); - // console.log(JSON.stringify(ast, null, 2)); + // console.log(JSON.stringify(root, null, 2)); return { text }; }; @@ -194,6 +194,66 @@ describe('single line query', () => { expect(text).toBe('ROW NOT a'); }); + + test('negative numbers', () => { + const { text } = reprint('ROW -1'); + + expect(text).toBe('ROW -1'); + }); + + test('negative numbers in brackets', () => { + const { text } = reprint('ROW -(1)'); + + expect(text).toBe('ROW -1'); + }); + + test('negative column names', () => { + const { text } = reprint('ROW -col'); + + expect(text).toBe('ROW -col'); + }); + + test('plus unary expression', () => { + const { text } = reprint('ROW +(23)'); + + expect(text).toBe('ROW 23'); + }); + + test('chained multiple unary expressions', () => { + const { text } = reprint('ROW ----+-+(23)'); + + expect(text).toBe('ROW -23'); + }); + + test('before another expression', () => { + const { text } = reprint('ROW ----+-+(1 + 1)'); + + expect(text).toBe('ROW -(1 + 1)'); + }); + + test('negative one from the right side', () => { + const { text } = reprint('ROW 2 * -1'); + + expect(text).toBe('ROW -2'); + }); + + test('two minuses is plus', () => { + const { text } = reprint('ROW --123'); + + expect(text).toBe('ROW 123'); + }); + + test('two minuses is plus (float)', () => { + const { text } = reprint('ROW --1.23'); + + expect(text).toBe('ROW 1.23'); + }); + + test('two minuses is plus (with brackets)', () => { + const { text } = reprint('ROW --(123)'); + + expect(text).toBe('ROW 123'); + }); }); describe('postfix unary expression', () => { diff --git a/packages/kbn-esql-ast/src/pretty_print/basic_pretty_printer.ts b/packages/kbn-esql-ast/src/pretty_print/basic_pretty_printer.ts index ec744c65f636e..2f1e3439cd3a3 100644 --- a/packages/kbn-esql-ast/src/pretty_print/basic_pretty_printer.ts +++ b/packages/kbn-esql-ast/src/pretty_print/basic_pretty_printer.ts @@ -7,9 +7,18 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import { binaryExpressionGroup } from '../ast/helpers'; +import { + binaryExpressionGroup, + isBinaryExpression, + isColumn, + isDoubleLiteral, + isIntegerLiteral, + isLiteral, + isProperNode, +} from '../ast/helpers'; import { ESQLAstBaseItem, ESQLAstCommand, ESQLAstQueryExpression } from '../types'; import { ESQLAstExpressionNode, Visitor } from '../visitor'; +import { resolveItem } from '../visitor/utils'; import { LeafPrinter } from './leaf_printer'; export interface BasicPrettyPrinterOptions { @@ -152,6 +161,62 @@ export class BasicPrettyPrinter { return formatted; } + protected simplifyMultiplicationByOne( + node: ESQLAstExpressionNode, + minusCount: number = 0 + ): string | undefined { + if (isBinaryExpression(node) && node.name === '*') { + let [left, right] = node.args; + left = resolveItem(left); + right = resolveItem(right); + + if (isProperNode(left) && isProperNode(right)) { + if (!!left.formatting || !!right.formatting) { + return undefined; + } + if (isIntegerLiteral(left)) { + if (left.value === 1) { + return this.simplifyMultiplicationByOne(right, minusCount); + } else if (left.value === -1) { + return this.simplifyMultiplicationByOne(right, minusCount + 1); + } + } + if (isIntegerLiteral(right)) { + if (right.value === 1) { + return this.simplifyMultiplicationByOne(left, minusCount); + } else if (right.value === -1) { + return this.simplifyMultiplicationByOne(left, minusCount + 1); + } + } + return undefined; + } else { + return undefined; + } + } + + const isNegative = minusCount % 2 === 1; + + if (isNegative && (isIntegerLiteral(node) || isDoubleLiteral(node)) && node.value < 0) { + return BasicPrettyPrinter.expression( + { + ...node, + value: Math.abs(node.value), + }, + this.opts + ); + } + + let expression = BasicPrettyPrinter.expression(node, this.opts); + const sign = isNegative ? '-' : ''; + const needsBrackets = !!sign && !isColumn(node) && !isLiteral(node); + + if (needsBrackets) { + expression = `(${expression})`; + } + + return sign ? `${sign}${expression}` : expression; + } + protected readonly visitor: Visitor = new Visitor() .on('visitExpression', (ctx) => { return ''; @@ -237,6 +302,18 @@ export class BasicPrettyPrinter { const groupLeft = binaryExpressionGroup(left); const groupRight = binaryExpressionGroup(right); + if ( + node.name === '*' && + ((isIntegerLiteral(left) && Math.abs(left.value) === 1) || + (isIntegerLiteral(right) && Math.abs(right.value) === 1)) + ) { + const formatted = this.simplifyMultiplicationByOne(node); + + if (formatted) { + return formatted; + } + } + let leftFormatted = ctx.visitArgument(0); let rightFormatted = ctx.visitArgument(1); diff --git a/packages/kbn-esql-ast/src/pretty_print/helpers.ts b/packages/kbn-esql-ast/src/pretty_print/helpers.ts index f9d9daac84e7a..1b4a75a119cb2 100644 --- a/packages/kbn-esql-ast/src/pretty_print/helpers.ts +++ b/packages/kbn-esql-ast/src/pretty_print/helpers.ts @@ -7,7 +7,7 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import { ESQLAstBaseItem, ESQLProperNode } from '../types'; +import type { ESQLAstBaseItem, ESQLProperNode } from '../types'; import { Walker } from '../walker'; export interface QueryPrettyPrintStats { diff --git a/packages/kbn-esql-ast/src/visitor/utils.ts b/packages/kbn-esql-ast/src/visitor/utils.ts index 0dc95b73cf9d7..da8544ef46c90 100644 --- a/packages/kbn-esql-ast/src/visitor/utils.ts +++ b/packages/kbn-esql-ast/src/visitor/utils.ts @@ -36,6 +36,10 @@ export const firstItem = (items: ESQLAstItem[]): ESQLSingleAstItem | undefined = } }; +export const resolveItem = (items: ESQLAstItem | ESQLAstItem[]): ESQLAstItem => { + return Array.isArray(items) ? resolveItem(items[0]) : items; +}; + /** * Returns the last normalized "single item" from the "item" list. * From 6a764e726569ed2ec5cd1bccd113b20212a8fdab Mon Sep 17 00:00:00 2001 From: Samiul Monir <150824886+Samiul-TheSoccerFan@users.noreply.github.com> Date: Tue, 22 Oct 2024 14:17:15 -0400 Subject: [PATCH 13/22] [Search: Inference Management UI] Fixing design issues and Removing Duplicate Service Name (#196431) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary This PR includes: - Addressed the comments from https://github.com/elastic/kibana/pull/193642 - Fix CSS to display usage items of inference endpoints ![Screenshot 2024-10-22 at 9 50 47 AM](https://github.com/user-attachments/assets/02cd6323-f9aa-4af6-9a4a-c410ca38b2d9) - Aligned header buttons with title ![Screenshot 2024-10-22 at 9 51 18 AM](https://github.com/user-attachments/assets/98cea438-634d-4cfe-b55a-a07bf56ecc8c) - removed duplicate "Elasticsearch" from Service dropdown ![Screenshot 2024-10-22 at 9 51 54 AM](https://github.com/user-attachments/assets/e4eb0d91-9440-4730-bf63-28695d1060b2) ### Checklist Delete any items that are not applicable to this PR. - [X] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [X] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [X] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [X] Any UI touched in this PR is usable by keyboard only (learn more about [keyboard accessibility](https://webaim.org/techniques/keyboard/)) - [X] Any UI touched in this PR does not create any new axe failures (run axe in browser: [FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/), [Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US)) - [X] This renders correctly on smaller devices using a responsive layout. (You can test this [in your browser](https://www.browserstack.com/guide/responsive-testing-on-local-server)) - [X] This was checked for [cross-browser compatibility](https://www.elastic.co/support/matrix#matrix_browsers) --------- Co-authored-by: Elastic Machine Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> --- .../all_inference_endpoints/constants.ts | 2 ++ .../filter/multi_select_filter.tsx | 14 +++++++---- .../component/list_usage_results.test.tsx | 4 ++-- .../actions/component/list_usage_results.tsx | 2 +- .../component/scan_usage_results.test.tsx | 10 ++++---- .../actions/component/scan_usage_results.tsx | 19 ++++++++------- .../actions/component/usage_item.test.tsx | 8 +++---- .../actions/component/usage_item.tsx | 19 +++++++++------ .../delete/confirm_delete_endpoint/index.tsx | 11 +++++---- .../confirm_delete_endpoint/translations.ts | 4 ++-- .../search/table_search.tsx | 1 + .../all_inference_endpoints/types.ts | 2 +- .../components/inference_endpoints_header.tsx | 24 ++++++++++++++----- .../search_inference_endpoints/tsconfig.json | 4 +++- 14 files changed, 77 insertions(+), 47 deletions(-) diff --git a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/constants.ts b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/constants.ts index 3e60bc33b049c..7ce1e578f1db0 100644 --- a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/constants.ts +++ b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/constants.ts @@ -32,3 +32,5 @@ export const DEFAULT_INFERENCE_ENDPOINTS_TABLE_STATE: AllInferenceEndpointsTable filterOptions: DEFAULT_FILTER_OPTIONS, queryParams: DEFAULT_QUERY_PARAMS, }; + +export const PIPELINE_URL = 'ingest/ingest_pipelines'; diff --git a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/filter/multi_select_filter.tsx b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/filter/multi_select_filter.tsx index 84883c4e85432..790bb5ec09913 100644 --- a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/filter/multi_select_filter.tsx +++ b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/filter/multi_select_filter.tsx @@ -18,6 +18,7 @@ import { } from '@elastic/eui'; import { css } from '@emotion/react'; import React, { useState } from 'react'; +import _ from 'lodash'; import * as i18n from './translations'; export interface MultiSelectFilterOption { @@ -44,11 +45,14 @@ export const MultiSelectFilter: React.FC = ({ const { euiTheme } = useEuiTheme(); const [isPopoverOpen, setIsPopoverOpen] = useState(false); const toggleIsPopoverOpen = () => setIsPopoverOpen((prevValue) => !prevValue); - const options: MultiSelectFilterOption[] = rawOptions.map(({ key, label }) => ({ - label, - key, - checked: selectedOptionKeys.includes(key) ? 'on' : undefined, - })); + const options: MultiSelectFilterOption[] = _.uniqBy( + rawOptions.map(({ key, label }) => ({ + label, + key, + checked: selectedOptionKeys.includes(key) ? 'on' : undefined, + })), + 'label' + ); return ( diff --git a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/list_usage_results.test.tsx b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/list_usage_results.test.tsx index 40f821bc104ae..2907cc7ef8014 100644 --- a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/list_usage_results.test.tsx +++ b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/list_usage_results.test.tsx @@ -12,11 +12,11 @@ import { render, screen, fireEvent } from '@testing-library/react'; describe('ListUsageResults', () => { const items = [ { - label: 'index-1', + id: 'index-1', type: 'Index', }, { - label: 'pipeline-1', + id: 'pipeline-1', type: 'Pipeline', }, ]; diff --git a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/list_usage_results.tsx b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/list_usage_results.tsx index d20520345a8ba..d42b0f6735252 100644 --- a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/list_usage_results.tsx +++ b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/list_usage_results.tsx @@ -34,7 +34,7 @@ export const ListUsageResults: React.FC = ({ list }) => { {list - .filter((item) => item.label.toLowerCase().includes(term.toLowerCase())) + .filter((item) => item.id.toLowerCase().includes(term.toLowerCase())) .map((item, id) => ( ))} diff --git a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/scan_usage_results.test.tsx b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/scan_usage_results.test.tsx index d2ec41680d249..a9cb3f1f8d389 100644 --- a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/scan_usage_results.test.tsx +++ b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/scan_usage_results.test.tsx @@ -19,11 +19,11 @@ const mockOnCheckboxChange = jest.fn(); describe('ScanUsageResults', () => { const items = [ { - label: 'index-1', + id: 'index-1', type: 'Index', }, { - label: 'pipeline-1', + id: 'pipeline-1', type: 'Pipeline', }, ]; @@ -40,7 +40,7 @@ describe('ScanUsageResults', () => { ); }); @@ -58,9 +58,9 @@ describe('ScanUsageResults', () => { it('opens index management in a new tab', () => { fireEvent.click(screen.getByTestId('inferenceManagementOpenIndexManagement')); - expect(mockNavigateToApp).toHaveBeenCalledWith('enterprise_search', { + expect(mockNavigateToApp).toHaveBeenCalledWith('enterpriseSearchContent', { openInNewTab: true, - path: 'content/search_indices', + path: 'search_indices', }); }); diff --git a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/scan_usage_results.tsx b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/scan_usage_results.tsx index 0f4aa09c12be4..33d7a4dae891f 100644 --- a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/scan_usage_results.tsx +++ b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/scan_usage_results.tsx @@ -17,30 +17,31 @@ import { import React from 'react'; import { euiThemeVars } from '@kbn/ui-theme'; import { css } from '@emotion/react'; +import { ENTERPRISE_SEARCH_CONTENT_APP_ID } from '@kbn/deeplinks-search'; + import { InferenceUsageInfo } from '../../../../types'; import { useKibana } from '../../../../../../hooks/use_kibana'; import { RenderMessageWithIcon } from './render_message_with_icon'; - import * as i18n from '../delete/confirm_delete_endpoint/translations'; import { ListUsageResults } from './list_usage_results'; interface ScanUsageResultsProps { list: InferenceUsageInfo[]; ignoreWarningCheckbox: boolean; - onCheckboxChange: (state: boolean) => void; + onIgnoreWarningCheckboxChange: (state: boolean) => void; } export const ScanUsageResults: React.FC = ({ list, ignoreWarningCheckbox, - onCheckboxChange, + onIgnoreWarningCheckboxChange, }) => { const { services: { application }, } = useKibana(); - const handleNavigateToIndex = () => { - application?.navigateToApp('enterprise_search', { - path: 'content/search_indices', + const handleNavigateToIndexManagement = () => { + application?.navigateToApp(ENTERPRISE_SEARCH_CONTENT_APP_ID, { + path: 'search_indices', openInNewTab: true, }); }; @@ -59,7 +60,7 @@ export const ScanUsageResults: React.FC = ({ - + = ({ = ({ id={'ignoreWarningCheckbox'} label={i18n.IGNORE_POTENTIAL_ERRORS_LABEL} checked={ignoreWarningCheckbox} - onChange={(e) => onCheckboxChange(e.target.checked)} + onChange={(e) => onIgnoreWarningCheckboxChange(e.target.checked)} /> diff --git a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/usage_item.test.tsx b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/usage_item.test.tsx index 7315de521a1c3..6c6899c71922d 100644 --- a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/usage_item.test.tsx +++ b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/usage_item.test.tsx @@ -29,7 +29,7 @@ describe('UsageItem', () => { describe('index', () => { const item: InferenceUsageInfo = { - label: 'index-1', + id: 'index-1', type: 'Index', }; @@ -44,16 +44,16 @@ describe('UsageItem', () => { it('opens index in a new tab', () => { fireEvent.click(screen.getByRole('button')); - expect(mockNavigateToApp).toHaveBeenCalledWith('enterprise_search', { + expect(mockNavigateToApp).toHaveBeenCalledWith('enterpriseSearchContent', { openInNewTab: true, - path: 'content/search_indices/index-1', + path: 'search_indices/index-1', }); }); }); describe('pipeline', () => { const item: InferenceUsageInfo = { - label: 'pipeline-1', + id: 'pipeline-1', type: 'Pipeline', }; diff --git a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/usage_item.tsx b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/usage_item.tsx index 90bd050d67b81..577b9f8aa0e29 100644 --- a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/usage_item.tsx +++ b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/component/usage_item.tsx @@ -14,11 +14,15 @@ import { EuiText, EuiTextTruncate, EuiIcon, + EuiSpacer, } from '@elastic/eui'; import React from 'react'; +import { ENTERPRISE_SEARCH_CONTENT_APP_ID } from '@kbn/deeplinks-search'; +import { MANAGEMENT_APP_ID } from '@kbn/deeplinks-management/constants'; import { useKibana } from '../../../../../../hooks/use_kibana'; import { InferenceUsageInfo } from '../../../../types'; +import { PIPELINE_URL } from '../../../../constants'; interface UsageProps { usageItem: InferenceUsageInfo; @@ -29,27 +33,27 @@ export const UsageItem: React.FC = ({ usageItem }) => { } = useKibana(); const handleNavigateToIndex = () => { if (usageItem.type === 'Index') { - application?.navigateToApp('enterprise_search', { - path: `content/search_indices/${usageItem.label}`, + application?.navigateToApp(ENTERPRISE_SEARCH_CONTENT_APP_ID, { + path: `search_indices/${usageItem.id}`, openInNewTab: true, }); } else if (usageItem.type === 'Pipeline') { - application?.navigateToApp('management', { - path: `ingest/ingest_pipelines?pipeline=${usageItem.label}`, + application?.navigateToApp(MANAGEMENT_APP_ID, { + path: `${PIPELINE_URL}?pipeline=${usageItem.id}`, openInNewTab: true, }); } }; return ( - + - + @@ -58,7 +62,7 @@ export const UsageItem: React.FC = ({ usageItem }) => { - + @@ -66,6 +70,7 @@ export const UsageItem: React.FC = ({ usageItem }) => { + ); diff --git a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/delete/confirm_delete_endpoint/index.tsx b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/delete/confirm_delete_endpoint/index.tsx index 965f512b32d7d..ea5c35178f9bf 100644 --- a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/delete/confirm_delete_endpoint/index.tsx +++ b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/delete/confirm_delete_endpoint/index.tsx @@ -37,7 +37,7 @@ export const ConfirmDeleteEndpointModal: React.FC { + const onIgnoreWarningCheckboxChange = (state: boolean) => { setIgnoreWarningCheckbox(state); if (state) { setDeleteDisabled(false); @@ -50,8 +50,11 @@ export const ConfirmDeleteEndpointModal: React.FC ({ label: index, type: 'Index' })); - const pipelines = data.pipelines.map((pipeline, id) => ({ label: pipeline, type: 'Pipeline' })); + const indices = data.indexes.map((index, id) => ({ id: index, type: 'Index' })); + const pipelines = data.pipelines.map((pipeline, id) => ({ + id: pipeline, + type: 'Pipeline', + })); const usages: InferenceUsageInfo[] = [...indices, ...pipelines]; if (usages.length > 0) { setDeleteDisabled(true); @@ -106,7 +109,7 @@ export const ConfirmDeleteEndpointModal: React.FC )} diff --git a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/delete/confirm_delete_endpoint/translations.ts b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/delete/confirm_delete_endpoint/translations.ts index d606e6f3c1b0e..b82fbcdf6b425 100644 --- a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/delete/confirm_delete_endpoint/translations.ts +++ b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/render_table_columns/render_actions/actions/delete/confirm_delete_endpoint/translations.ts @@ -19,7 +19,7 @@ export const CONFIRM_DELETE_WARNING = i18n.translate( 'xpack.searchInferenceEndpoints.confirmDeleteEndpoint.confirmQuestion', { defaultMessage: - 'Deleting an inference endpoint currently in use will cause failures in the ingest and query attempts.', + 'Deleting an inference endpoint currently in use will cause failures in ingest and query attempts.', } ); @@ -54,7 +54,7 @@ export const POTENTIAL_FAILURE_LABEL = i18n.translate( export const IGNORE_POTENTIAL_ERRORS_LABEL = i18n.translate( 'xpack.searchInferenceEndpoints.confirmDeleteEndpoint.ignoreErrors', { - defaultMessage: 'Ignore potential errors and force deletion', + defaultMessage: 'Ignore errors and force deletion', } ); diff --git a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/search/table_search.tsx b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/search/table_search.tsx index b74e8050e5f92..b3989d60d9123 100644 --- a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/search/table_search.tsx +++ b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/search/table_search.tsx @@ -29,6 +29,7 @@ export const TableSearch: React.FC = ({ searchKey, se onChange={(e) => setSearchKey(e.target.value)} onSearch={onSearch} value={searchKey} + data-test-subj="search-field-endpoints" /> ); }; diff --git a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/types.ts b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/types.ts index b5c5fc49aa1fa..0eec8a0cb177d 100644 --- a/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/types.ts +++ b/x-pack/plugins/search_inference_endpoints/public/components/all_inference_endpoints/types.ts @@ -64,6 +64,6 @@ export interface InferenceEndpointUI { } export interface InferenceUsageInfo { - label: string; + id: string; type: string; } diff --git a/x-pack/plugins/search_inference_endpoints/public/components/inference_endpoints_header.tsx b/x-pack/plugins/search_inference_endpoints/public/components/inference_endpoints_header.tsx index f12ef3e9fe8cd..acb7e82db13b2 100644 --- a/x-pack/plugins/search_inference_endpoints/public/components/inference_endpoints_header.tsx +++ b/x-pack/plugins/search_inference_endpoints/public/components/inference_endpoints_header.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { EuiPageTemplate, EuiLink } from '@elastic/eui'; +import { EuiPageTemplate, EuiButtonEmpty } from '@elastic/eui'; import React from 'react'; import * as i18n from '../../common/translations'; import { docLinks } from '../../common/doc_links'; @@ -21,16 +21,28 @@ export const InferenceEndpointsHeader: React.FC = () => { description={i18n.MANAGE_INFERENCE_ENDPOINTS_LABEL} bottomBorder={true} rightSideItems={[ - {i18n.API_DOCUMENTATION_LINK} - , - + , + {i18n.VIEW_YOUR_MODELS_LINK} - , + , ]} /> ); diff --git a/x-pack/plugins/search_inference_endpoints/tsconfig.json b/x-pack/plugins/search_inference_endpoints/tsconfig.json index 5b4a66e37d2f5..d454be99b65f0 100644 --- a/x-pack/plugins/search_inference_endpoints/tsconfig.json +++ b/x-pack/plugins/search_inference_endpoints/tsconfig.json @@ -31,7 +31,9 @@ "@kbn/test-jest-helpers", "@kbn/kibana-utils-plugin", "@kbn/features-plugin", - "@kbn/ui-theme" + "@kbn/ui-theme", + "@kbn/deeplinks-search", + "@kbn/deeplinks-management" ], "exclude": [ "target/**/*", From 790613f37b848ed83ebd41a3e07a61deb3d30513 Mon Sep 17 00:00:00 2001 From: Stratoula Kalafateli Date: Tue, 22 Oct 2024 20:21:17 +0200 Subject: [PATCH 14/22] [ES|QL] Supports _index_mode in the metadata options (#197167) --- .../src/autocomplete/autocomplete.test.ts | 2 +- .../kbn-esql-validation-autocomplete/src/shared/constants.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.test.ts b/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.test.ts index e463902554074..deb4592428089 100644 --- a/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.test.ts +++ b/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.test.ts @@ -1123,7 +1123,7 @@ describe('autocomplete', () => { { filterText: '_source', text: '_source, ', command: TRIGGER_SUGGESTION_COMMAND }, ]); // no comma if there are no more fields - testSuggestions('FROM a METADATA _id, _ignored, _index, _source, _version/', [ + testSuggestions('FROM a METADATA _id, _ignored, _index, _source, _index_mode, _version/', [ { filterText: '_version', text: '_version | ', command: TRIGGER_SUGGESTION_COMMAND }, ]); }); diff --git a/packages/kbn-esql-validation-autocomplete/src/shared/constants.ts b/packages/kbn-esql-validation-autocomplete/src/shared/constants.ts index c1942118a41e2..1a9f382d32a6d 100644 --- a/packages/kbn-esql-validation-autocomplete/src/shared/constants.ts +++ b/packages/kbn-esql-validation-autocomplete/src/shared/constants.ts @@ -15,4 +15,4 @@ export const SINGLE_TICK_REGEX = /`/g; export const DOUBLE_BACKTICK = '``'; export const SINGLE_BACKTICK = '`'; -export const METADATA_FIELDS = ['_version', '_id', '_index', '_source', '_ignored']; +export const METADATA_FIELDS = ['_version', '_id', '_index', '_source', '_ignored', '_index_mode']; From 87bda295e50aea4ec3372a2cbaed052b68c2c08f Mon Sep 17 00:00:00 2001 From: Rachel Shen Date: Tue, 22 Oct 2024 13:01:38 -0600 Subject: [PATCH 15/22] [Canvas] Update kbn/flot to remove table.replace() issue (#195643) ## Summary Closing https://github.com/elastic/kibana-team/issues/1132 to avoid an issue with Canvas, that is planned to be removed in 10.x. @kbn/flot is being used by the monitoring team so I'm not removing it from Kibana in this PR. --- packages/kbn-flot-charts/lib/jquery_flot.js | 2415 +++++++++++-------- 1 file changed, 1345 insertions(+), 1070 deletions(-) diff --git a/packages/kbn-flot-charts/lib/jquery_flot.js b/packages/kbn-flot-charts/lib/jquery_flot.js index 3b13b317c616c..50524fd8f4926 100644 --- a/packages/kbn-flot-charts/lib/jquery_flot.js +++ b/packages/kbn-flot-charts/lib/jquery_flot.js @@ -1,8 +1,6 @@ /* 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 @@ -29,482 +27,602 @@ Licensed under the MIT license. * 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 +/* Javascript plotting library for jQuery, version 0.9.0-alpha. + +Copyright (c) 2007-2013 IOLA and Ole Laursen. +Licensed under the MIT license. + +*/ + (function($) { - // Cache the prototype hasOwnProperty for faster access + // A jquery-esque isNumeric method since we currently support 1.4.4 + // and $.isNumeric was introduced on in 1.7 + var isNumeric = $.isNumeric || function(obj) { + return obj - parseFloat( obj ) >= 0; + }; + + /** + * 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]; - var hasOwnProperty = Object.prototype.hasOwnProperty; + if (element == null) { - // 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. + element = document.createElement("canvas"); + element.className = cls; - // Do not merge this into Flot 0.9, since it requires jQuery 1.4.4+. + $(element).css({ direction: "ltr", position: "absolute", left: 0, top: 0 }) + .appendTo(container); - if (!$.fn.detach) { - $.fn.detach = function() { - return this.each(function() { - if (this.parentNode) { - this.parentNode.removeChild( this ); + // 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 = {}; } - /////////////////////////////////////////////////////////////////////////// - // 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. + /** + * 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 (Object.prototype.hasOwnProperty.call(cache, layerKey)) { + + var layer = this.getTextLayer(layerKey), + layerCache = cache[layerKey]; + + layer.hide(); + + for (var styleKey in layerCache) { + if (Object.prototype.hasOwnProperty.call(layerCache, styleKey)) { + var styleCache = layerCache[styleKey]; + for (var angleKey in styleCache) { + if (Object.prototype.hasOwnProperty.call(styleCache, angleKey)) { + var angleCache = styleCache[angleKey]; + for (var key in angleCache) { + if (Object.prototype.hasOwnProperty.call(angleCache, key)) { + + var positions = angleCache[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 angleCache[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); + } - function Canvas(cls, container) { + return layer; + }; - var element = container.children("." + cls)[0]; + /** + * 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. + * @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, angleCache, info; + + text = "" + text; // Cast to string in case we have a number or such + angle = (360 + (angle || 0)) % 360; // Normalize the angle to 0...359 + + // 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; + } - if (element == null) { + // Retrieve or create the caches for the text's layer, style, and angle - element = document.createElement("canvas"); - element.className = cls; + layerCache = this._textCache[layer]; + if (layerCache == null) { + layerCache = this._textCache[layer] = {}; + } - $(element).css({ direction: "ltr", position: "absolute", left: 0, top: 0 }) - .appendTo(container); + styleCache = layerCache[textStyle]; + if (styleCache == null) { + styleCache = layerCache[textStyle] = {}; + } - // If HTML5 Canvas isn't available, fall back to [Ex|Flash]canvas + angleCache = styleCache[angle]; + if (angleCache == null) { + angleCache = styleCache[angle] = {}; + } - 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."); - } - } - } + info = angleCache[text]; - this.element = element; + // If we can't find a matching element in our cache, create a new one - var context = this.context = element.getContext("2d"); + if (info == null) { - // 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. + var element = $("
").html(text) + .css({ + position: "absolute", + "max-width": width, + top: -9999 + }) + .appendTo(this.getTextLayer(layer)); - // 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. + if (typeof font === "object") { + element.css({ + font: textStyle, + color: font.color + }); + } else if (typeof font === "string") { + element.addClass(font); + } + + // Save the original dimensions of the text; we'll modify these + // later to take into account rotation, if there is any. + + var textWidth = element.outerWidth(true), + textHeight = element.outerHeight(true); + + // Apply rotation to the text using CSS3/IE matrix transforms + + // Note how we also set the element's width, as a work-around for + // the way most browsers resize the div on rotate, which may cause + // the contents to wrap differently. The extra +1 is because IE + // rounds the width differently and needs a little extra help. + + if (angle) { + + var radians = angle * Math.PI / 180, + sin = Math.sin(radians), + cos = Math.cos(radians), + a = cos.toFixed(6), // Use fixed-point so these don't + b = (-sin).toFixed(6), // show up in scientific notation + c = sin.toFixed(6), // when we add them to the string + transformRule; + + if ($.support.leadingWhitespace) { + + // The transform origin defaults to '50% 50%', producing + // blurry text on some browsers (Chrome) when the width or + // height happens to be odd, making 50% fractional. Avoid + // this by setting the origin to rounded values. + + var cx = textWidth / 2, + cy = textHeight / 2, + transformOrigin = Math.floor(cx) + "px " + Math.floor(cy) + "px"; + + // Transforms alter the div's appearance without changing + // its origin. This will make it difficult to position it + // later, since we'll be positioning the new bounding box + // with respect to the old origin. We can work around this + // by adding a translation to align the new bounding box's + // top-left corner with the origin, using the same matrix. + + // Rather than examining all four points, we can use the + // angle to figure out in advance which two points are in + // the top-left quadrant; we can then use the x-coordinate + // of the first (left-most) point and the y-coordinate of + // the second (top-most) point as the bounding box corner. + + var x, y; + if (angle < 90) { + x = Math.floor(cx * cos + cy * sin - cx); + y = Math.floor(cx * sin + cy * cos - cy); + } else if (angle < 180) { + x = Math.floor(cy * sin - cx * cos - cx); + y = Math.floor(cx * sin - cy * cos - cy); + } else if (angle < 270) { + x = Math.floor(-cx * cos - cy * sin - cx); + y = Math.floor(-cx * sin - cy * cos - cy); + } else { + x = Math.floor(cx * cos - cy * sin - cx); + y = Math.floor(cy * cos - cx * sin - cy); + } + + transformRule = "matrix(" + a + "," + c + "," + b + "," + a + "," + x + "," + y + ")"; + + element.css({ + width: textWidth + 1, + transform: transformRule, + "-o-transform": transformRule, + "-ms-transform": transformRule, + "-moz-transform": transformRule, + "-webkit-transform": transformRule, + "transform-origin": transformOrigin, + "-o-transform-origin": transformOrigin, + "-ms-transform-origin": transformOrigin, + "-moz-transform-origin": transformOrigin, + "-webkit-transform-origin": transformOrigin + }); - var devicePixelRatio = window.devicePixelRatio || 1, - backingStoreRatio = - context.webkitBackingStorePixelRatio || - context.mozBackingStorePixelRatio || - context.msBackingStorePixelRatio || - context.oBackingStorePixelRatio || - context.backingStorePixelRatio || 1; + } else { - this.pixelRatio = devicePixelRatio / backingStoreRatio; + // The IE7/8 matrix filter produces very ugly aliasing for + // text with a transparent background. Using a solid color + // greatly improves text clarity, although it does result + // in ugly boxes for plots using a non-white background. - // Size the canvas to match the internal dimensions of its container + // TODO: Instead of white use the actual background color? + // This still wouldn't solve the problem when the plot has + // a gradient background, but it would at least help. - this.resize(container.width(), container.height()); + transformRule = "progid:DXImageTransform.Microsoft.Matrix(M11=" + a + ", M12=" + b + ", M21=" + c + ", M22=" + a + ",sizingMethod='auto expand')"; - // Collection of HTML div layers for text overlaid onto the canvas + element.css({ + width: textWidth + 1, + filter: transformRule, + "-ms-filter": transformRule, + "background-color": "#fff" + }); + } - this.textContainer = null; - this.text = {}; + // Compute the final dimensions of the text's bounding box - // Cache of text fragments and metrics, so we can avoid expensively - // re-calculating them when the plot is re-rendered in a loop. + var ac = Math.abs(cos), + as = Math.abs(sin), + originalWidth = textWidth; + textWidth = Math.round(ac * textWidth + as * textHeight); + textHeight = Math.round(as * originalWidth + ac * textHeight); + } - this._textCache = {}; - } + info = angleCache[text] = { + width: textWidth, + height: textHeight, + element: element, + positions: [] + }; - // 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. + element.detach(); + } - Canvas.prototype.resize = function(width, height) { + return info; + }; - if (width <= 0 || height <= 0) { - throw new Error("Invalid dimensions for plot, width = " + width + ", height = " + height); - } + /** + * 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. + * @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; + } - var element = this.element, - context = this.context, - pixelRatio = this.pixelRatio; + if (valign === "middle") { + y -= info.height / 2; + } else if (valign === "bottom") { + y -= info.height; + } - // 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. + // Determine whether this text already exists at this position. + // If so, mark it for inclusion in the next render pass. - // Resizing should reset the state (excanvas seems to be buggy though) + for (var i = 0, position; position = positions[i]; i++) { + if (position.x === x && position.y === y) { + position.active = true; + return; + } + } - if (this.width != width) { - element.width = width * pixelRatio; - element.style.width = width + "px"; - this.width = width; - } + // If the text doesn't exist at this position, create a new entry - if (this.height != height) { - element.height = height * pixelRatio; - element.style.height = height + "px"; - this.height = height; - } + // For the very first position we'll re-use the original element, + // while for subsequent ones we'll clone it. - // 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. + position = { + active: true, + rendered: false, + element: positions.length ? info.element.clone() : info.element, + x: x, + y: y + }; - context.restore(); - context.save(); + positions.push(position); - // 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. + // Move the element to its final position within the container - context.scale(pixelRatio, pixelRatio); - }; + position.element.css({ + top: Math.round(y), + left: Math.round(x), + "text-align": halign // In case the text wraps + }); + }; - // 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 = $("
").text(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. + /** + * 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) { + var i, positions, position; + if (text == null) { + var layerCache = this._textCache[layer]; + if (layerCache != null) { + for (var styleKey in layerCache) { + if (Object.prototype.hasOwnProperty.call(layerCache, styleKey)) { + var styleCache = layerCache[styleKey]; + for (var angleKey in styleCache) { + if (Object.prototype.hasOwnProperty.call(styleCache, angleKey)) { + var angleCache = styleCache[angleKey]; + for (var key in angleCache) { + if (Object.prototype.hasOwnProperty.call(angleCache, key)) { + positions = angleCache[key].positions; + for (i = 0; position = positions[i]; i++) { + position.active = false; + } + } + } + } + } + } + } + } + } else { + positions = this.getTextInfo(layer, text, font, angle).positions; + for (i = 0; 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 ... ] @@ -517,7 +635,7 @@ Licensed under the MIT license. colors: ["#edc240", "#afd8f8", "#cb4b4b", "#4da74d", "#9440ed"], legend: { show: true, - noColumns: 1, // number of columns in legend table + noColumns: 1, // number of colums 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 @@ -528,31 +646,45 @@ Licensed under the MIT license. 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 + + show: null, // null = auto-detect, true = always, false = never + position: "bottom", // or "top" + mode: null, // null or "time" + + color: null, // base color, labels, ticks + 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" } + + 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 + + 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"] + + ticks: null, // either [1, 3] or [[1, "a"], 3] or (fn: axis info -> ticks) or app. number of ticks for auto-ticks + tickSize: null, // number or [number, "unit"] + minTickSize: null, // number or [number, "unit"] + tickFormatter: null, // fn: number -> string + tickDecimals: null, // no. of decimals, null means auto + + tickColor: null, // possibly different color of ticks, e.g. "rgba(0,0,0,0.15)" + tickLength: null, // size in pixels of ticks, or "full" for whole line + + tickWidth: null, // width of tick labels in pixels + tickHeight: null, // height of tick labels in pixels + tickFont: null, // null or font-spec object (see font, above) + + label: null, // null or an axis label string + labelFont: null, // null or font-spec object (see font, above) + labelPadding: 2, // spacing between the axis and its label + + reserveSpace: null, // whether to reserve space even if axis isn't shown + alignTicksWithAxis: null // axis number or null for no sync }, yaxis: { + position: "left", // or "right" autoscaleMargin: 0.02, - position: "left" // or "right" + labelPadding: 2 }, xaxes: [], yaxes: [], @@ -563,6 +695,7 @@ Licensed under the MIT license. lineWidth: 2, // in pixels fill: true, fillColor: "#ffffff", + strokeColor: null, symbol: "circle" // or callback }, lines: { @@ -614,26 +747,26 @@ Licensed under the MIT license. }, 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; + 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; @@ -652,10 +785,11 @@ Licensed under the MIT license. }; plot.getData = function () { return series; }; plot.getAxes = function () { - var res = {}, i; + var res = {}; $.each(xaxes.concat(yaxes), function (_, axis) { - if (axis) - res[axis.direction + (axis.n != 1 ? axis.n : "") + "axis"] = axis; + if (axis) { + res[axis.direction + (axis.n !== 1 ? axis.n : "") + "axis"] = axis; + } }); return res; }; @@ -674,26 +808,9 @@ Licensed under the MIT license. }; }; 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(); + var width = placeholder.width(), + height = placeholder.height(); surface.resize(width, height); overlay.resize(width, height); }; @@ -713,8 +830,9 @@ Licensed under the MIT license. function executeHooks(hook, args) { args = [plot].concat(args); - for (var i = 0; i < hook.length; ++i) + for (var i = 0; i < hook.length; ++i) { hook[i].apply(this, args); + } } function initPlugins() { @@ -728,8 +846,9 @@ Licensed under the MIT license. for (var i = 0; i < plugins.length; ++i) { var p = plugins[i]; p.init(plot, classes); - if (p.options) + if (p.options) { $.extend(true, options, p.options); + } } } @@ -743,23 +862,29 @@ Licensed under the MIT license. // not expected behavior; avoid it by replacing them here. if (opts && opts.colors) { - options.colors = 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.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 + 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 + } + if (options.yaxis.tickColor == null) { // grid.tickColor for back-compatibility options.yaxis.tickColor = options.grid.tickColor || options.yaxis.color; + } - if (options.grid.borderColor == null) + 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(); + } + 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. @@ -768,16 +893,16 @@ Licensed under the MIT license. // 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), + size: Math.round(0.8 * (+placeholder.css("font-size").replace("px", "") || 13)), variant: placeholder.css("font-variant"), weight: placeholder.css("font-weight"), family: placeholder.css("font-family") }; + fontDefaults.lineHeight = fontDefaults.size * 1.15; + axisCount = options.xaxes.length || 1; for (i = 0; i < axisCount; ++i) { @@ -786,17 +911,29 @@ Licensed under the MIT license. axisOptions.tickColor = axisOptions.color; } + // Compatibility with markrcote/flot-axislabels + + if (axisOptions) { + if (!axisOptions.label && axisOptions.axisLabel) { + axisOptions.label = axisOptions.axisLabel; + } + if (!axisOptions.labelPadding && axisOptions.axisLabelPadding) { + axisOptions.labelPadding = axisOptions.axisLabelPadding; + } + } + axisOptions = $.extend(true, {}, options.xaxis, axisOptions); options.xaxes[i] = axisOptions; + fontDefaults.color = axisOptions.color; 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); - } + } + if (axisOptions.tickFont || axisOptions.font) { + axisOptions.tickFont = $.extend({}, axisOptions.font || fontDefaults, axisOptions.tickFont); + } + if (axisOptions.label && (axisOptions.labelFont || axisOptions.font)) { + axisOptions.labelFont = $.extend({}, axisOptions.font || fontDefaults, axisOptions.labelFont); } } @@ -808,72 +945,83 @@ Licensed under the MIT license. axisOptions.tickColor = axisOptions.color; } + // Compatibility with markrcote/flot-axislabels + + if (axisOptions) { + if (!axisOptions.label && axisOptions.axisLabel) { + axisOptions.label = axisOptions.axisLabel; + } + if (!axisOptions.labelPadding && axisOptions.axisLabelPadding) { + axisOptions.labelPadding = axisOptions.axisLabelPadding; + } + } + axisOptions = $.extend(true, {}, options.yaxis, axisOptions); options.yaxes[i] = axisOptions; + fontDefaults.color = axisOptions.color; 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); - } + } + if (axisOptions.tickFont || axisOptions.font) { + axisOptions.tickFont = $.extend({}, axisOptions.font || fontDefaults, axisOptions.tickFont); + } + if (axisOptions.label && (axisOptions.labelFont || axisOptions.font)) { + axisOptions.labelFont = $.extend({}, axisOptions.font || fontDefaults, axisOptions.labelFont); } } // backwards compatibility, to be removed in future - if (options.xaxis.noTicks && options.xaxis.ticks == null) + if (options.xaxis.noTicks && options.xaxis.ticks == null) { options.xaxis.ticks = options.xaxis.noTicks; - if (options.yaxis.noTicks && options.yaxis.ticks == null) + } + 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) + if (options.grid.coloredAreas) { options.grid.markings = options.grid.coloredAreas; - if (options.grid.coloredAreasColor) + } + if (options.grid.coloredAreasColor) { options.grid.markingsColor = options.grid.coloredAreasColor; - if (options.lines) + } + if (options.lines) { $.extend(true, options.series.lines, options.lines); - if (options.points) + } + if (options.points) { $.extend(true, options.series.points, options.points); - if (options.bars) + } + if (options.bars) { $.extend(true, options.series.bars, options.bars); - if (options.shadowSize != null) + } + if (options.shadowSize != null) { options.series.shadowSize = options.shadowSize; - if (options.highlightColor != null) + } + if (options.highlightColor != null) { options.series.highlightColor = options.highlightColor; + } // save options on axes for future reference - for (i = 0; i < options.xaxes.length; ++i) + for (i = 0; i < options.xaxes.length; ++i) { getOrCreateAxis(xaxes, i + 1).options = options.xaxes[i]; - for (i = 0; i < options.yaxes.length; ++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) + 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]); } @@ -896,9 +1044,9 @@ Licensed under the MIT license. $.extend(true, s, d[i]); d[i].data = s.data; - } - else + } else { s.data = d[i]; + } res.push(s); } @@ -907,10 +1055,12 @@ Licensed under the MIT license. function axisNumber(obj, coord) { var a = obj[coord + "axis"]; - if (typeof a == "object") // if we got a real axis, extract number + if (typeof a === "object") { // if we got a real axis, extract number a = a.n; - if (typeof a != "number") + } + if (!isNumeric(a)) { a = 1; // default to first axis + } return a; } @@ -924,20 +1074,24 @@ Licensed under the MIT license. var res = {}, i, axis; for (i = 0; i < xaxes.length; ++i) { axis = xaxes[i]; - if (axis && axis.used) + 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) + if (axis && axis.used) { res["y" + axis.n] = axis.c2p(pos.top); + } } - if (res.x1 !== undefined) + if (res.x1 !== undefined) { res.x = res.x1; - if (res.y1 !== undefined) + } + if (res.y1 !== undefined) { res.y = res.y1; + } return res; } @@ -950,8 +1104,9 @@ Licensed under the MIT license. axis = xaxes[i]; if (axis && axis.used) { key = "x" + axis.n; - if (pos[key] == null && axis.n == 1) + if (pos[key] == null && axis.n === 1) { key = "x"; + } if (pos[key] != null) { res.left = axis.p2c(pos[key]); @@ -964,8 +1119,9 @@ Licensed under the MIT license. axis = yaxes[i]; if (axis && axis.used) { key = "y" + axis.n; - if (pos[key] == null && axis.n == 1) + if (pos[key] == null && axis.n === 1) { key = "y"; + } if (pos[key] != null) { res.top = axis.p2c(pos[key]); @@ -978,12 +1134,13 @@ Licensed under the MIT license. } function getOrCreateAxis(axes, number) { - if (!axes[number - 1]) + 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) + direction: axes === xaxes ? "x" : "y", + options: $.extend(true, {}, axes === xaxes ? options.xaxis : options.yaxis) }; + } return axes[number - 1]; } @@ -999,7 +1156,7 @@ Licensed under the MIT license. var sc = series[i].color; if (sc != null) { neededColors--; - if (typeof sc == "number" && sc > maxIndex) { + if (isNumeric(sc) && sc > maxIndex) { maxIndex = sc; } } @@ -1030,15 +1187,19 @@ Licensed under the MIT license. // 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 (i % colorPoolSize === 0 && i) { if (variation >= 0) { if (variation < 0.5) { variation = -variation - 0.2; - } else variation = 0; - } else variation = -variation; + } else { + variation = 0; + } + } else { + variation = -variation; + } } - colors[i] = c.scale('rgb', 1 + variation); + colors[i] = c.scale("rgb", 1 + variation); } // Finalize the series options, filling in their colors @@ -1051,20 +1212,22 @@ Licensed under the MIT license. if (s.color == null) { s.color = colors[colori].toString(); ++colori; - } - else if (typeof s.color == "number") + } else if (isNumeric(s.color)) { 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) + for (v in s) { if (s[v] && s[v].show) { show = false; break; } - if (show) + } + if (show) { s.lines.show = true; + } } // If nothing was provided for lines.zero, default it to match @@ -1084,15 +1247,15 @@ Licensed under the MIT license. 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; + i, j, k, m, s, points, ps, val, f, p, data, format; function updateAxis(axis, min, max) { - if (min < axis.datamin && min != -fakeInfinity) + if (min < axis.datamin && min !== -fakeInfinity) { axis.datamin = min; - if (max > axis.datamax && max != fakeInfinity) + } + if (max > axis.datamax && max !== fakeInfinity) { axis.datamax = max; + } } $.each(allAxes(), function (_, axis) { @@ -1105,7 +1268,6 @@ Licensed under the MIT license. for (i = 0; i < series.length; ++i) { s = series[i]; s.datapoints = { points: [] }; - executeHooks(hooks.processRawData, [ s, s.data, s.datapoints ]); } @@ -1134,8 +1296,9 @@ Licensed under the MIT license. s.datapoints.format = format; } - if (s.datapoints.pointsize != null) + if (s.datapoints.pointsize != null) { continue; // already filled in + } s.datapoints.pointsize = format.length; @@ -1157,20 +1320,23 @@ Licensed under the MIT license. if (f) { if (f.number && val != null) { val = +val; // convert to number - if (isNaN(val)) + if (isNaN(val)) { val = null; - else if (val == Infinity) + } else if (val === Infinity) { val = fakeInfinity; - else if (val == -Infinity) + } else if (val === -Infinity) { val = -fakeInfinity; + } } if (val == null) { - if (f.required) + if (f.required) { nullify = true; + } - if (f.defaultValue != null) + if (f.defaultValue != null) { val = f.defaultValue; + } } } @@ -1184,7 +1350,7 @@ Licensed under the MIT license. if (val != null) { f = format[m]; // extract min/max info - if (f.autoscale !== false) { + if (f.autoscale) { if (f.x) { updateAxis(s.xaxis, val, val); } @@ -1195,18 +1361,18 @@ Licensed under the MIT license. } points[k + m] = null; } - } - else { + } 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]) { + 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) + 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]; @@ -1236,26 +1402,32 @@ Licensed under the MIT license. xmax = bottomSentry, ymax = bottomSentry; for (j = 0; j < points.length; j += ps) { - if (points[j] == null) + 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) + if (!f || f.autoscale === false || val === fakeInfinity || val === -fakeInfinity) { continue; + } if (f.x) { - if (val < xmin) + if (val < xmin) { xmin = val; - if (val > xmax) + } + if (val > xmax) { xmax = val; + } } if (f.y) { - if (val < ymin) + if (val < ymin) { ymin = val; - if (val > ymax) + } + if (val > ymax) { ymax = val; + } } } } @@ -1265,21 +1437,23 @@ Licensed under the MIT license. var delta; switch (s.bars.align) { - case "left": - delta = 0; - break; - case "right": - delta = -s.bars.barWidth; - break; - default: - delta = -s.bars.barWidth / 2; + case "left": + delta = 0; + break; + case "right": + delta = -s.bars.barWidth; + break; + case "center": + delta = -s.bars.barWidth / 2; + break; + default: + throw new Error("Invalid bar alignment: " + s.bars.align); } if (s.bars.horizontal) { ymin += delta; ymax += delta + s.bars.barWidth; - } - else { + } else { xmin += delta; xmax += delta + s.bars.barWidth; } @@ -1290,10 +1464,12 @@ Licensed under the MIT license. } $.each(allAxes(), function (_, axis) { - if (axis.datamin == topSentry) + if (axis.datamin === topSentry) { axis.datamin = null; - if (axis.datamax == bottomSentry) + } + if (axis.datamax === bottomSentry) { axis.datamax = null; + } }); } @@ -1303,12 +1479,11 @@ Licensed under the MIT license. // 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(); + .children(":not(.flot-base,.flot-overlay)").remove(); - if (placeholder.css("position") == 'static') + 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 @@ -1346,15 +1521,17 @@ Licensed under the MIT license. eventHolder.bind("mouseleave", onMouseLeave); } - if (options.grid.clickable) + if (options.grid.clickable) { eventHolder.click(onClick); + } executeHooks(hooks.bindEvents, [eventHolder]); } function shutdown() { - if (redrawTimeout) + if (redrawTimeout) { clearTimeout(redrawTimeout); + } eventHolder.unbind("mousemove", onMouseMove); eventHolder.unbind("mouseleave", onMouseLeave); @@ -1374,136 +1551,143 @@ Licensed under the MIT license. // precompute how much the axis is scaling a point // in canvas space - if (axis.direction == "x") { + 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 { + } 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 + if (t === identity) { // slight optimization axis.p2c = function (p) { return (p - m) * s; }; - else + } else { axis.p2c = function (p) { return (t(p) - m) * s; }; + } // canvas coordinate to data point - if (!it) + if (!it) { axis.c2p = function (c) { return m + c / s; }; - else + } 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"; + // Label width & height are deprecated; remove in 1.0! + tickWidth = opts.tickWidth || opts.labelWidth || 0, + tickHeight = opts.tickHeight || opts.labelHeight || 0, + maxWidth = tickWidth || axis.direction === "x" ? Math.floor(surface.width / (ticks.length || 1)) : null, + layer = "flot-" + axis.direction + "-axis flot-" + axis.direction + axis.n + "-axis " + axis.direction + "Axis " + axis.direction + axis.n + "Axis", + font = opts.tickFont || "flot-tick-label tickLabel"; for (var i = 0; i < ticks.length; ++i) { var t = ticks[i]; - if (!t.label) + 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); + tickWidth = Math.max(tickWidth, info.width); + tickHeight = Math.max(tickHeight, info.height); } - axis.labelWidth = opts.labelWidth || labelWidth; - axis.labelHeight = opts.labelHeight || labelHeight; + axis.tickWidth = opts.tickWidth || opts.labelWidth || tickWidth; + axis.tickHeight = opts.tickHeight || opts.labelHeight || tickHeight; + + // Label width/height properties are deprecated; remove in 1.0! + + axis.labelWidth = axis.tickWidth; + axis.labelHeight = axis.tickHeight; } + /////////////////////////////////////////////////////////////////////// + // Compute the axis bounding box based on the dimensions of its label + // and tick labels, then adjust the plotOffset to make room for it. + // + // This first phase only considers one dimension per axis; the other + // dimension depends on the other axes, and will be calculated later. + 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, + + var contentWidth = axis.tickWidth, + contentHeight = axis.tickHeight, + axisOptions = axis.options, + tickLength = axisOptions.tickLength, + axisPosition = axisOptions.position, 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; - } - } - }); + all = axis.direction === "x" ? xaxes : yaxes, + innermost; - // The outermost axis on each side has no margin + // Determine the margin around the axis - if (outermost) { - axisMargin = 0; + var samePosition = $.grep(all, function(axis) { + return axis && axis.options.position === axisPosition && axis.reserveSpace; + }); + if ($.inArray(axis, samePosition) === samePosition.length - 1) { + axisMargin = 0; // outermost } - // The ticks for the first axis in each direction stretch across + // Determine whether the axis is the first (innermost) on its side + + innermost = $.inArray(axis, samePosition) === 0; + + // Determine the length of the tick marks if (tickLength == null) { - tickLength = first ? "full" : 5; + if (innermost) { + tickLength = "full"; + } else { + tickLength = 5; + } } - if (!isNaN(+tickLength)) + if (!isNaN(+tickLength)) { padding += +tickLength; + } - if (isXAxis) { - lh += padding; + // Measure the dimensions of the axis label, if it has one - 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; - } + if (axisOptions.label) { + var layer = "flot-" + axis.direction + "-axis flot-" + axis.direction + axis.n + "-axis " + axis.direction + "Axis " + axis.direction + axis.n + "Axis", + font = axisOptions.labelFont || "flot-axis-label axisLabels " + axis.direction + axis.n + "axisLabel", + angle = axis.direction === "x" ? 0 : axisOptions.position === "right" ? 90 : -90, + labelInfo = surface.getTextInfo(layer, axisOptions.label, font, angle); + contentWidth += labelInfo.width + axisOptions.labelPadding; + contentHeight += labelInfo.height + axisOptions.labelPadding; } - else { - lw += padding; - if (pos == "left") { - axis.box = { left: plotOffset.left + axisMargin, width: lw }; - plotOffset.left += lw + axisMargin; + // Compute the axis bounding box and update the plot bounds + + if (axis.direction === "x") { + contentHeight += padding; + if (axisPosition === "top") { + axis.box = { top: plotOffset.top + axisMargin, height: contentHeight }; + plotOffset.top += contentHeight + axisMargin; + } else { + plotOffset.bottom += contentHeight + axisMargin; + axis.box = { top: surface.height - plotOffset.bottom, height: contentHeight }; } - else { - plotOffset.right += lw + axisMargin; - axis.box = { left: surface.width - plotOffset.right, width: lw }; + } else { + contentWidth += padding; + if (axisPosition === "right") { + plotOffset.right += contentWidth + axisMargin; + axis.box = { left: surface.width - plotOffset.right, width: contentWidth }; + } else { + axis.box = { left: plotOffset.left + axisMargin, width: contentWidth }; + plotOffset.left += contentWidth + axisMargin; } } - // save for future reference - axis.position = pos; + axis.position = axisPosition; axis.tickLength = tickLength; axis.box.padding = padding; axis.innermost = innermost; @@ -1512,13 +1696,12 @@ Licensed under the MIT license. 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; + if (axis.direction === "x") { + axis.box.left = plotOffset.left - axis.tickWidth / 2; + axis.box.width = surface.width - plotOffset.left - plotOffset.right + axis.tickWidth; + } else { + axis.box.top = plotOffset.top - axis.tickHeight / 2; + axis.box.height = surface.height - plotOffset.bottom - plotOffset.top + axis.tickHeight; } } @@ -1527,95 +1710,92 @@ Licensed under the MIT license. // inside the canvas and isn't clipped off var minMargin = options.grid.minBorderMargin, - axis, i; + margins = { x: 0, y: 0 }, 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) + 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 - }; + margins.x = margins.y = Math.ceil(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); - } + var dir = axis.direction; + if (axis.reserveSpace) { + margins[dir] = Math.ceil(Math.max(margins[dir], (dir === "x" ? axis.tickWidth : axis.tickHeight) / 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)); + plotOffset.left = Math.max(margins.x, plotOffset.left); + plotOffset.right = Math.max(margins.x, plotOffset.right); + plotOffset.top = Math.max(margins.y, plotOffset.top); + plotOffset.bottom = Math.max(margins.y, plotOffset.bottom); } function setupGrid() { - var i, axes = allAxes(), showGrid = options.grid.show; + var axes = allAxes(), + showGrid = options.grid.show, + margin = options.grid.margin || 0, + i, a; // 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; + for (a in plotOffset) { + if (Object.prototype.hasOwnProperty.call(plotOffset, a)) { + plotOffset[a] = isNumeric(margin) ? 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") { + for (a in plotOffset) { + if(typeof(options.grid.borderWidth) === "object") { plotOffset[a] += showGrid ? options.grid.borderWidth[a] : 0; - } - else { + } else { plotOffset[a] += showGrid ? options.grid.borderWidth : 0; } } + // init axes $.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; + axis.show = axis.options.show; + if (axis.show == null) { + axis.show = axis.used; // by default an axis is visible if it's got data + } + + axis.reserveSpace = axis.show || axis.options.reserveSpace; + setRange(axis); }); if (showGrid) { - var allocatedAxes = $.grep(axes, function (axis) { - return axis.show || axis.reserveSpace; - }); + var allocatedAxes = $.grep(axes, function (axis) { return 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) + for (i = allocatedAxes.length - 1; i >= 0; --i) { allocateAxisBoxFirstPhase(allocatedAxes[i]); + } // make sure we've got enough space for things that // might stick out @@ -1647,18 +1827,19 @@ Licensed under the MIT license. max = +(opts.max != null ? opts.max : axis.datamax), delta = max - min; - if (delta == 0.0) { + if (delta === 0.0) { // degenerate case - var widen = max == 0 ? 1 : 0.01; + var widen = max === 0 ? 1 : 0.01; - if (opts.min == null) + 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) + if (opts.max == null || opts.min != null) { max += widen; - } - else { + } + } else { // consider autoscaling var margin = opts.autoscaleMargin; if (margin != null) { @@ -1666,13 +1847,15 @@ Licensed under the MIT license. 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) + 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) + if (max > 0 && axis.datamax != null && axis.datamax <= 0) { max = 0; + } } } } @@ -1685,12 +1868,13 @@ Licensed under the MIT license. // estimate number of ticks var noTicks; - if (typeof opts.ticks == "number" && opts.ticks > 0) + if (isNumeric(opts.ticks) && opts.ticks > 0) { noTicks = opts.ticks; - else + } 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); + 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), @@ -1729,10 +1913,10 @@ Licensed under the MIT license. 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. + // Time mode was moved to a plug-in in 0.8, but since so many people use this + // we'll add an especially friendly make sure they remembered to include it. - if (opts.mode == "time" && !axis.tickGenerator) { + if (opts.mode === "time" && !axis.tickGenerator) { throw new Error("Time mode requires the flot.time plugin."); } @@ -1754,43 +1938,46 @@ Licensed under the MIT license. v = start + i * axis.tickSize; ticks.push(v); ++i; - } while (v < axis.max && v != prev); + } while (v < axis.max && v !== prev); return ticks; }; - axis.tickFormatter = function (value, axis) { + axis.tickFormatter = function (value, axis) { - var factor = axis.tickDecimals ? Math.pow(10, axis.tickDecimals) : 1; - var formatted = "" + Math.round(value * factor) / factor; + 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 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); - } - } + 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)) + 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) { + 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) + if (opts.min == null) { axis.min = Math.min(axis.min, niceTicks[0]); - if (opts.max == null && niceTicks.length > 1) + } + if (opts.max == null && niceTicks.length > 1) { axis.max = Math.max(axis.max, niceTicks[niceTicks.length - 1]); + } } axis.tickGenerator = function (axis) { @@ -1813,8 +2000,9 @@ Licensed under the MIT license. // 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)))) + if (!(ts.length > 1 && /\..*0$/.test((ts[1] - ts[0]).toFixed(extraDec)))) { axis.tickDecimals = extraDec; + } } } } @@ -1822,14 +2010,15 @@ Licensed under the MIT license. function setTicks(axis) { var oticks = axis.options.ticks, ticks = []; - if (oticks == null || (typeof oticks == "number" && oticks > 0)) + if (oticks == null || (isNumeric(oticks) && oticks > 0)) { ticks = axis.tickGenerator(axis); - else if (oticks) { - if ($.isFunction(oticks)) + } else if (oticks) { + if ($.isFunction(oticks)) { // generate the ticks ticks = oticks(axis); - else + } else { ticks = oticks; + } } // clean up/labelify the supplied ticks, copy them over @@ -1838,27 +2027,32 @@ Licensed under the MIT license. for (i = 0; i < ticks.length; ++i) { var label = null; var t = ticks[i]; - if (typeof t == "object") { + if (typeof t === "object") { v = +t[0]; - if (t.length > 1) + if (t.length > 1) { label = t[1]; - } - else + } + } else { v = +t; - if (label == null) + } + if (label == null) { label = axis.tickFormatter(v, axis); - if (!isNaN(v)) + } + 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) + if (axis.options.min == null) { axis.min = Math.min(axis.min, ticks[0].v); - if (axis.options.max == null && ticks.length > 1) + } + if (axis.options.max == null && ticks.length > 1) { axis.max = Math.max(axis.max, ticks[ticks.length - 1].v); + } } } @@ -1871,8 +2065,9 @@ Licensed under the MIT license. var grid = options.grid; // draw background, if any - if (grid.show && grid.backgroundColor) + if (grid.show && grid.backgroundColor) { drawBackground(); + } if (grid.show && !grid.aboveData) { drawGrid(); @@ -1902,10 +2097,11 @@ Licensed under the MIT license. for (var i = 0; i < axes.length; ++i) { axis = axes[i]; - if (axis.direction == coord) { + if (axis.direction === coord) { key = coord + axis.n + "axis"; - if (!ranges[key] && axis.n == 1) + if (!ranges[key] && axis.n === 1) { key = coord + "axis"; // support x1axis as xaxis + } if (ranges[key]) { from = ranges[key].from; to = ranges[key].to; @@ -1916,7 +2112,7 @@ Licensed under the MIT license. // backwards-compat stuff - to be removed in future if (!ranges[key]) { - axis = coord == "x" ? xaxes[0] : yaxes[0]; + axis = coord === "x" ? xaxes[0] : yaxes[0]; from = ranges[coord + "1"]; to = ranges[coord + "2"]; } @@ -1967,53 +2163,50 @@ Licensed under the MIT license. yrange = extractRange(m, "y"); // fill in missing - if (xrange.from == null) + if (xrange.from == null) { xrange.from = xrange.axis.min; - if (xrange.to == null) + } + if (xrange.to == null) { xrange.to = xrange.axis.max; - if (yrange.from == null) + } + if (yrange.from == null) { yrange.from = yrange.axis.min; - if (yrange.to == null) + } + 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) + 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) { + if (xrange.from === xrange.to && yrange.from === yrange.to) { 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; + xrange.from = xrange.axis.p2c(xrange.from); + xrange.to = xrange.axis.p2c(xrange.to); + yrange.from = yrange.axis.p2c(yrange.from); + yrange.to = yrange.axis.p2c(yrange.to); + + if (xrange.from === xrange.to || yrange.from === yrange.to) { + // draw line 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.lineWidth = m.lineWidth || options.grid.markingsLineWidth; + ctx.moveTo(xrange.from, yrange.from); + ctx.lineTo(xrange.to, yrange.to); ctx.stroke(); } else { + // fill area ctx.fillStyle = m.color || options.grid.markingsColor; ctx.fillRect(xrange.from, yrange.to, xrange.to - xrange.from, @@ -2029,25 +2222,27 @@ Licensed under the MIT license. 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) + if (!axis.show || axis.ticks.length === 0) { continue; + } ctx.lineWidth = 1; // find the edges - if (axis.direction == "x") { + 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 { + 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); + if (t === "full") { + x = (axis.position === "left" ? 0 : plotWidth); + } else { + x = box.left - plotOffset.left + (axis.position === "left" ? box.width : 0); + } } // draw tick bar @@ -2055,13 +2250,14 @@ Licensed under the MIT license. ctx.strokeStyle = axis.options.color; ctx.beginPath(); xoff = yoff = 0; - if (axis.direction == "x") + if (axis.direction === "x") { xoff = plotWidth + 1; - else + } else { yoff = plotHeight + 1; + } - if (ctx.lineWidth == 1) { - if (axis.direction == "x") { + if (ctx.lineWidth === 1) { + if (axis.direction === "x") { y = Math.floor(y) + 0.5; } else { x = Math.floor(x) + 0.5; @@ -2083,33 +2279,36 @@ Licensed under the MIT license. xoff = yoff = 0; - if (isNaN(v) || v < axis.min || v > axis.max + 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))) + t === "full" && ((typeof bw === "object" && bw[axis.position] > 0) || bw > 0) && + (v === axis.min || v === axis.max) + )) { continue; + } - if (axis.direction == "x") { + if (axis.direction === "x") { x = axis.p2c(v); - yoff = t == "full" ? -plotHeight : t; + yoff = t === "full" ? -plotHeight : t; - if (axis.position == "top") + if (axis.position === "top") { yoff = -yoff; - } - else { + } + } else { y = axis.p2c(v); - xoff = t == "full" ? -plotWidth : t; + xoff = t === "full" ? -plotWidth : t; - if (axis.position == "left") + if (axis.position === "left") { xoff = -xoff; + } } - if (ctx.lineWidth == 1) { - if (axis.direction == "x") + if (ctx.lineWidth === 1) { + if (axis.direction === "x") { x = Math.floor(x) + 0.5; - else + } else { y = Math.floor(y) + 0.5; + } } ctx.moveTo(x, y); @@ -2125,7 +2324,7 @@ Licensed under the MIT license. // 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" || typeof bc === "object") { if (typeof bw !== "object") { bw = {top: bw, right: bw, bottom: bw, left: bw}; } @@ -2168,8 +2367,7 @@ Licensed under the MIT license. ctx.lineTo(0- bw.left/2, 0); ctx.stroke(); } - } - else { + } else { ctx.lineWidth = bw; ctx.strokeStyle = options.grid.borderColor; ctx.strokeRect(-bw/2, -bw/2, plotWidth + bw, plotHeight + bw); @@ -2182,31 +2380,48 @@ Licensed under the MIT license. function drawAxisLabels() { $.each(allAxes(), function (_, axis) { + if (!axis.show || axis.ticks.length === 0) { + return; + } + 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", + axisOptions = axis.options, + layer = "flot-" + axis.direction + "-axis flot-" + axis.direction + axis.n + "-axis " + axis.direction + "Axis " + axis.direction + axis.n + "Axis", + labelFont = axisOptions.labelFont || "flot-axis-label axisLabels " + axis.direction + axis.n + "axisLabel", + tickFont = axisOptions.tickFont || "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; + if (axisOptions.label) { + if (axis.direction === "x") { + if (axisOptions.position === "top") { + surface.addText(layer, box.left + box.width / 2, box.top, axisOptions.label, labelFont, 0, null, "center", "top"); + } else { + surface.addText(layer, box.left + box.width / 2, box.top + box.height, axisOptions.label, labelFont, 0, null, "center", "bottom"); + } + } else { + if (axisOptions.position === "right") { + surface.addText(layer, box.left + box.width, box.top + box.height / 2, axisOptions.label, labelFont, 90, null, "right", "middle"); + } else { + surface.addText(layer, box.left, box.top + box.height / 2, axisOptions.label, labelFont, -90, null, "left", "middle"); + } + } + } + + // Add labels for the ticks on this axis for (var i = 0; i < axis.ticks.length; ++i) { tick = axis.ticks[i]; - if (!tick.label || tick.v < axis.min || tick.v > axis.max) + if (!tick.label || tick.v < axis.min || tick.v > axis.max) { continue; + } - if (axis.direction == "x") { + if (axis.direction === "x") { halign = "center"; x = plotOffset.left + axis.p2c(tick.v); - if (axis.position == "bottom") { + if (axis.position === "bottom") { y = box.top + box.padding; } else { y = box.top + box.height - box.padding; @@ -2215,7 +2430,7 @@ Licensed under the MIT license. } else { valign = "middle"; y = plotOffset.top + axis.p2c(tick.v); - if (axis.position == "left") { + if (axis.position === "left") { x = box.left + box.width - box.padding; halign = "right"; } else { @@ -2223,18 +2438,21 @@ Licensed under the MIT license. } } - surface.addText(layer, x, y, tick.label, font, null, null, halign, valign); + surface.addText(layer, x, y, tick.label, tickFont, null, null, halign, valign); } }); } function drawSeries(series) { - if (series.lines.show) + if (series.lines.show) { drawSeriesLines(series); - if (series.bars.show) + } + if (series.bars.show) { drawSeriesBars(series); - if (series.points.show) + } + if (series.points.show) { drawSeriesPoints(series); + } } function drawSeriesLines(series) { @@ -2248,68 +2466,74 @@ Licensed under the MIT license. var x1 = points[i - ps], y1 = points[i - ps + 1], x2 = points[i], y2 = points[i + 1]; - if (x1 == null || x2 == null) + if (x1 == null || x2 == null) { continue; + } // clip with ymin if (y1 <= y2 && y1 < axisy.min) { - if (y2 < 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) + } 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) + 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) + } 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) + 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) + } 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) + 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) + } 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) + if (x1 !== prevx || y1 !== prevy) { ctx.moveTo(axisx.p2c(x1) + xoffset, axisy.p2c(y1) + yoffset); + } prevx = x2; prevy = y2; @@ -2322,15 +2546,16 @@ Licensed under the MIT license. var points = datapoints.points, ps = datapoints.pointsize, bottom = Math.min(Math.max(0, axisy.min), axisy.max), - i = 0, top, areaOpen = false, + i = 0, 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) + if (ps > 0 && i > points.length + ps) { break; + } i += ps; // ps is negative if going backwards @@ -2347,7 +2572,7 @@ Licensed under the MIT license. continue; } - if (ps < 0 && i == segmentStart + ps) { + if (ps < 0 && i === segmentStart + ps) { // done with the reverse sweep ctx.fill(); areaOpen = false; @@ -2358,35 +2583,38 @@ Licensed under the MIT license. } } - if (x1 == null || x2 == null) + if (x1 == null || x2 == null) { continue; + } // clip x values // clip with xmin if (x1 <= x2 && x1 < axisx.min) { - if (x2 < 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) + } 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) + 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) + } else if (x2 >= x1 && x2 > axisx.max) { + if (x1 > axisx.max) { continue; + } y2 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1; x2 = axisx.max; } @@ -2403,8 +2631,7 @@ Licensed under the MIT license. 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) { + } 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; @@ -2423,8 +2650,7 @@ Licensed under the MIT license. 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) { + } else if (y2 <= y1 && y2 < axisy.min && y1 >= axisy.min) { x2 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1; y2 = axisy.min; } @@ -2433,15 +2659,14 @@ Licensed under the MIT license. 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) { + } 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) { + if (x1 !== x1old) { ctx.lineTo(axisx.p2c(x1old), axisy.p2c(y1)); // it goes to (x1, y1), but we fill that below } @@ -2453,7 +2678,7 @@ Licensed under the MIT license. ctx.lineTo(axisx.p2c(x2), axisy.p2c(y2)); // fill the other rectangle if it's there - if (x2 != x2old) { + if (x2 !== x2old) { ctx.lineTo(axisx.p2c(x2), axisy.p2c(y2)); ctx.lineTo(axisx.p2c(x2old), axisy.p2c(y2)); } @@ -2486,8 +2711,9 @@ Licensed under the MIT license. plotLineArea(series.datapoints, series.xaxis, series.yaxis); } - if (lw > 0) + if (lw > 0) { plotLine(series.datapoints, 0, 0, series.xaxis, series.yaxis); + } ctx.restore(); } @@ -2497,16 +2723,18 @@ Licensed under the MIT license. 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) + 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") + if (symbol === "circle") { ctx.arc(x, y, radius, 0, shadow ? Math.PI : Math.PI * 2, false); - else + } else { symbol(ctx, x, y, radius, shadow); + } ctx.closePath(); if (fillStyle) { @@ -2530,8 +2758,9 @@ Licensed under the MIT license. // Doing the conditional here allows the shadow setting to still be // optional even with a lineWidth of 0. - if( lw == 0 ) + if( lw === 0 ) { lw = 0.0001; + } if (lw > 0 && sw > 0) { // draw shadow in two steps @@ -2547,14 +2776,14 @@ Licensed under the MIT license. } ctx.lineWidth = lw; - ctx.strokeStyle = series.color; + ctx.strokeStyle = series.points.strokeColor || 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) { + function drawBar(x, y, b, barLeft, barRight, offset, fillStyleCallback, axisx, axisy, c, horizontal, lineWidth) { var left, right, bottom, top, drawLeft, drawRight, drawTop, drawBottom, tmp; @@ -2578,8 +2807,7 @@ Licensed under the MIT license. drawLeft = true; drawRight = false; } - } - else { + } else { drawLeft = drawRight = drawTop = true; drawBottom = false; left = x + barLeft; @@ -2599,8 +2827,9 @@ Licensed under the MIT license. // clip if (right < axisx.min || left > axisx.max || - top < axisy.min || bottom > axisy.max) + top < axisy.min || bottom > axisy.max) { return; + } if (left < axisx.min) { left = axisx.min; @@ -2629,8 +2858,13 @@ Licensed under the MIT license. // fill the bar if (fillStyleCallback) { + c.beginPath(); + c.moveTo(left, bottom); + c.lineTo(left, top); + c.lineTo(right, top); + c.lineTo(right, bottom); c.fillStyle = fillStyleCallback(bottom, top); - c.fillRect(left, top, right - left, bottom - top) + c.fill(); } // draw outline @@ -2638,35 +2872,40 @@ Licensed under the MIT license. 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.moveTo(left, bottom + offset); + if (drawLeft) { + c.lineTo(left, top + offset); + } else { + c.moveTo(left, top + offset); + } + if (drawTop) { + c.lineTo(right, top + offset); + } else { + c.moveTo(right, top + offset); + } + if (drawRight) { + c.lineTo(right, bottom + offset); + } else { + c.moveTo(right, bottom + offset); + } + if (drawBottom) { + c.lineTo(left, bottom + offset); + } else { + c.moveTo(left, bottom + offset); + } c.stroke(); } } function drawSeriesBars(series) { - function plotBars(datapoints, barLeft, barRight, fillStyleCallback, axisx, axisy) { + function plotBars(datapoints, barLeft, barRight, offset, fillStyleCallback, axisx, axisy) { var points = datapoints.points, ps = datapoints.pointsize; for (var i = 0; i < points.length; i += ps) { - if (points[i] == null) + 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); + } + drawBar(points[i], points[i + 1], points[i + 2], barLeft, barRight, offset, fillStyleCallback, axisx, axisy, ctx, series.bars.horizontal, series.bars.lineWidth); } } @@ -2680,53 +2919,53 @@ Licensed under the MIT license. var barLeft; switch (series.bars.align) { - case "left": - barLeft = 0; - break; - case "right": - barLeft = -series.bars.barWidth; - break; - default: - barLeft = -series.bars.barWidth / 2; + case "left": + barLeft = 0; + break; + case "right": + barLeft = -series.bars.barWidth; + break; + case "center": + barLeft = -series.bars.barWidth / 2; + break; + default: + throw new Error("Invalid bar alignment: " + series.bars.align); } 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); + plotBars(series.datapoints, barLeft, barLeft + series.bars.barWidth, 0, fillStyleCallback, series.xaxis, series.yaxis); ctx.restore(); } function getFillStyle(filloptions, seriesColor, bottom, top) { var fill = filloptions.fill; - if (!fill) + if (!fill) { return null; + } - if (filloptions.fillColor) + if (filloptions.fillColor) { return getColorOrGradient(filloptions.fillColor, bottom, top, seriesColor); + } var c = $.color.parse(seriesColor); - c.a = typeof fill == "number" ? fill : 0.4; + c.a = isNumeric(fill) ? fill : 0.4; c.normalize(); return c.toString(); } function insertLegend() { - if (options.legend.container != null) { - $(options.legend.container).html(""); - } else { - placeholder.find(".legend").remove(); - } + placeholder.find(".legend").remove(); if (!options.legend.show) { return; } - var fragments = [], entries = [], rowStarted = false, - lf = options.legend.labelFormatter, s, label; + var entries = [], lf = options.legend.labelFormatter, s, label, i; // Build a list of legend entries, with each having a label and a color - for (var i = 0; i < series.length; ++i) { + for (i = 0; i < series.length; ++i) { s = series[i]; if (s.label) { label = lf ? lf(s.label, s) : s.label; @@ -2739,18 +2978,24 @@ Licensed under the MIT license. } } + // No entries implies no legend + + if (entries.length === 0) { + return; + } + // 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 if (options.legend.sorted === "reverse") { + entries.reverse(); } else { - var ascending = options.legend.sorted != "descending"; + 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 + return a.label === b.label ? 0 : ( + (a.label < b.label) !== ascending ? 1 : -1 // Logical XOR ); }); } @@ -2758,63 +3003,86 @@ Licensed under the MIT license. // Generate markup for the list of entries, in their final order - for (var i = 0; i < entries.length; ++i) { + var table = $("
").css({ + "font-size": "smaller", + "color": options.grid.color + }), rowBuffer = null; + + for (i = 0; i < entries.length; ++i) { var entry = entries[i]; - if (i % options.legend.noColumns == 0) { - if (rowStarted) - fragments.push(''); - fragments.push(''); - rowStarted = true; + if (i % options.legend.noColumns === 0) { + if (rowBuffer !== null) { + table.append(rowBuffer); + } + rowBuffer = $(""); } - fragments.push( - '
' + - '' + entry.label + '' + var colorbox = $("
").css({ + "width": "4px", + "height": 0, + "border": "5px solid " + entry.color, + "overflow": "hidden" + }), + + borderbox = $("
").css({ + "border": "1px solid " + options.legend.labelBoxBorderColor, + "padding": "1px" + }); + + rowBuffer.append( + $("").addClass("legendColorBox").append(borderbox.append(colorbox)), + $("").addClass("legendLabel").html(entry.label) ); } - if (rowStarted) - fragments.push(''); + table.append(rowBuffer); - if (fragments.length == 0) - return; - - var table = '' + fragments.join("") + '
'; - if (options.legend.container != null) + if (options.legend.container != null) { $(options.legend.container).html(table); - else { - var pos = "", + } else { + var pos = {"position": "absolute"}, p = options.legend.position, m = options.legend.margin; - if (m[0] == null) + 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) { + } + 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 = $("
").addClass("legend").append(table.css(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") + if (c && typeof c === "string") { c = $.color.parse(c); - else - c = $.color.extract(legend, 'background-color'); + } else { + c = $.color.extract(legend, "background-color"); + } c.a = 1; c = c.toString(); } var div = legend.children(); - $('
').prependTo(legend).css('opacity', options.legend.backgroundOpacity); + + // Position also applies to this + $("
").css(pos).css({ + "width": div.width() + "px", + "height": div.height() + "px", + "background-color": c, + "opacity": options.legend.backgroundOpacity + }).prependTo(legend); } } } @@ -2829,11 +3097,12 @@ Licensed under the MIT license. function findNearbyItem(mouseX, mouseY, seriesFilter) { var maxDistance = options.grid.mouseActiveRadius, smallestDistance = maxDistance * maxDistance + 1, - item = null, foundPoint = false, i, j, ps; + item = null, i, j, ps; for (i = series.length - 1; i >= 0; --i) { - if (!seriesFilter(series[i])) + if (!seriesFilter(series[i])) { continue; + } var s = series[i], axisx = s.xaxis, @@ -2842,27 +3111,35 @@ Licensed under the MIT license. mx = axisx.c2p(mouseX), // precompute some stuff to make the loop faster my = axisy.c2p(mouseY), maxx = maxDistance / axisx.scale, - maxy = maxDistance / axisy.scale; + maxy = maxDistance / axisy.scale, + x, y; ps = s.datapoints.pointsize; // with inverse transforms, we can't use the maxx/maxy // optimization, sadly - if (axisx.options.inverseTransform) + if (axisx.options.inverseTransform) { maxx = Number.MAX_VALUE; - if (axisy.options.inverseTransform) + } + 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) + + 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) + 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 @@ -2880,34 +3157,25 @@ Licensed under the MIT license. } 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; + var barLeft = s.bars.align === "left" ? 0 : -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) + x = points[j]; + y = points[j + 1]; + var 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]; + my >= Math.min(b, y) && my <= Math.max(b, y))) { + item = [i, j / ps]; + } } } } @@ -2927,20 +3195,22 @@ Licensed under the MIT license. } function onMouseMove(e) { - if (options.grid.hoverable) + if (options.grid.hoverable) { triggerClickHoverEvent("plothover", e, - function (s) { return s["hoverable"] != false; }); + function (s) { return s.hoverable !== false; }); + } } function onMouseLeave(e) { - if (options.grid.hoverable) + if (options.grid.hoverable) { triggerClickHoverEvent("plothover", e, - function (s) { return false; }); + function () { return false; }); + } } function onClick(e) { triggerClickHoverEvent("plotclick", e, - function (s) { return s["clickable"] != false; }); + function (s) { return s.clickable !== false; }); } // trigger click or hover event (they send the same parameters @@ -2966,15 +3236,18 @@ Licensed under the MIT license. // 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])) + 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) + if (item) { highlight(item.series, item.datapoint, eventname); + } } placeholder.trigger(eventname, [ pos, item ]); @@ -2982,13 +3255,14 @@ Licensed under the MIT license. function triggerRedrawOverlay() { var t = options.interaction.redrawOverlayInterval; - if (t == -1) { // skip event queue + if (t === -1) { // skip event queue drawOverlay(); return; } - if (!redrawTimeout) + if (!redrawTimeout) { redrawTimeout = setTimeout(drawOverlay, t); + } } function drawOverlay() { @@ -3003,10 +3277,11 @@ Licensed under the MIT license. for (i = 0; i < highlights.length; ++i) { hi = highlights[i]; - if (hi.series.bars.show) + if (hi.series.bars.show) { drawBarHighlight(hi.series, hi.point); - else + } else { drawPointHighlight(hi.series, hi.point); + } } octx.restore(); @@ -3014,22 +3289,22 @@ Licensed under the MIT license. } function highlight(s, point, auto) { - if (typeof s == "number") + if (isNumeric(s)) { s = series[s]; + } - if (typeof point == "number") { + if (isNumeric(point)) { var ps = s.datapoints.pointsize; point = s.datapoints.points.slice(ps * point, ps * (point + 1)); } var i = indexOfHighlight(s, point); - if (i == -1) { + if (i === -1) { highlights.push({ series: s, point: point, auto: auto }); - triggerRedrawOverlay(); - } - else if (!auto) + } else if (!auto) { highlights[i].auto = false; + } } function unhighlight(s, point) { @@ -3039,18 +3314,18 @@ Licensed under the MIT license. return; } - if (typeof s == "number") + if (isNumeric(s)) { s = series[s]; + } - if (typeof point == "number") { + if (isNumeric(point)) { var ps = s.datapoints.pointsize; point = s.datapoints.points.slice(ps * point, ps * (point + 1)); } var i = indexOfHighlight(s, point); - if (i != -1) { + if (i !== -1) { highlights.splice(i, 1); - triggerRedrawOverlay(); } } @@ -3058,9 +3333,9 @@ Licensed under the MIT license. 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]) + if (h.series === s && h.point[0] === p[0] && h.point[1] === p[1]) { return i; + } } return -1; } @@ -3068,54 +3343,52 @@ Licensed under the MIT license. 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(); + 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) + if (x < axisx.min || x > axisx.max || y < axisy.min || y > axisy.max) { return; + } - var pointRadius = series.points.radius + series.points.lineWidth / 2; + var pointRadius; + var radius; + if (series.points.show) { + pointRadius = series.points.radius + series.points.lineWidth / 2; + radius = 1.5 * pointRadius; + } else { + pointRadius = series.points.radius; + radius = 0.5 * pointRadius; + } 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") + if (series.points.symbol === "circle") { octx.arc(x, y, radius, 0, 2 * Math.PI, false); - else + } 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(), + 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; - } + barLeft = series.bars.align === "left" ? 0 : -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); + 0, function () { return fillStyle; }, series.xaxis, series.yaxis, octx, series.bars.horizontal, series.bars.lineWidth); } function getColorOrGradient(spec, bottom, top, defaultColor) { - if (typeof spec == "string") + if (typeof spec === "string") { return spec; - else { + } else { // assume this is a gradient spec; IE currently only // supports a simple vertical gradient properly, so that's // what we support too @@ -3123,12 +3396,14 @@ Licensed under the MIT license. for (var i = 0, l = spec.colors.length; i < l; ++i) { var c = spec.colors[i]; - if (typeof c != "string") { + if (typeof c !== "string") { var co = $.color.parse(defaultColor); - if (c.brightness != null) - co = co.scale('rgb', c.brightness); - if (c.opacity != null) + 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); @@ -3148,7 +3423,7 @@ Licensed under the MIT license. return plot; }; - $.plot.version = "0.8.3"; + $.plot.version = "0.9.0-alpha"; $.plot.plugins = []; @@ -3165,4 +3440,4 @@ Licensed under the MIT license. return base * Math.floor(n / base); } -})(jQuery); +})(jQuery); \ No newline at end of file From cb56679c140138624e606bfe2ca8e91b3267a5fe Mon Sep 17 00:00:00 2001 From: Hannah Mudge Date: Tue, 22 Oct 2024 13:06:48 -0600 Subject: [PATCH 16/22] [Dashboard] [Collapsable Panels] Interact with real panel element (#196756) Closes https://github.com/elastic/kibana/issues/190647 ## Summary This PR swaps the interaction of `kbn-grid-layout` so that you are now dragging/resizing the **actual element** rather than the preview - i.e. the preview now shows the **real** / grid aligned location (i.e. where the panel will land once the interaction stops), while the element shows the non-grid-aligned location. **Dragging** | Before | After | |--------|--------| | ![Oct-18-2024 09-10-52](https://github.com/user-attachments/assets/f117124d-3200-4c7b-a5f7-6a4bc767ebff) | ![Oct-18-2024 09-07-25](https://github.com/user-attachments/assets/483d481a-a752-4455-b9bd-2d89ec273454) | **Resizing** | Before | After | |--------|--------| | ![Oct-18-2024 09-11-21](https://github.com/user-attachments/assets/64e4314d-b641-4b0c-a4a9-93e3f0d21cbc) | ![Oct-18-2024 09-07-55](https://github.com/user-attachments/assets/755be726-38bc-475b-a85d-7696262c4b4f) | This serves as more than just a visual update - because we are dragging the real element, the mouse stays "locked" to the drag and/or resize handler, which means we have introduced the possibility for an `onDrop` event. This is necessary in order to keep the current "unsaved changes" behaviour on Dashboard, where changes are triggered only once the panel is actually **dropped** and not when other panels move as a consequence of a drag event. To make this possible, I also removed the `GridOverlay` component - it was creating a transparent `div` **over the entire grid** on interaction, which meant that focus was lost as soon as the interaction started. If we want to restore the "scroll up" and "scroll down" buttons (which we were unsure about, anyway), we would need to rethink this (i.e. just render two fixed-position buttons without any overlay). ### Checklist - [x] This was checked for [cross-browser compatibility](https://www.elastic.co/support/matrix#matrix_browsers) ### For maintainers - [ ] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#_add_your_labels) - [ ] This will appear in the **Release Notes** and follow the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --- .../grid/grid_height_smoother.tsx | 2 +- packages/kbn-grid-layout/grid/grid_layout.tsx | 15 +- .../kbn-grid-layout/grid/grid_overlay.tsx | 135 ---------------- packages/kbn-grid-layout/grid/grid_panel.tsx | 77 ++++----- packages/kbn-grid-layout/grid/grid_row.tsx | 77 +++++++-- packages/kbn-grid-layout/grid/types.ts | 18 ++- .../grid/use_grid_layout_events.ts | 22 +-- .../grid/use_grid_layout_state.ts | 150 ++++++++++++++---- 8 files changed, 239 insertions(+), 257 deletions(-) delete mode 100644 packages/kbn-grid-layout/grid/grid_overlay.tsx diff --git a/packages/kbn-grid-layout/grid/grid_height_smoother.tsx b/packages/kbn-grid-layout/grid/grid_height_smoother.tsx index 960fe4f52e735..7693fac72918a 100644 --- a/packages/kbn-grid-layout/grid/grid_height_smoother.tsx +++ b/packages/kbn-grid-layout/grid/grid_height_smoother.tsx @@ -24,7 +24,7 @@ export const GridHeightSmoother = ({ gridLayoutStateManager.interactionEvent$, ]).subscribe(([dimensions, interactionEvent]) => { if (!smoothHeightRef.current) return; - if (!interactionEvent) { + if (!interactionEvent || interactionEvent.type === 'drop') { smoothHeightRef.current.style.height = `${dimensions.height}px`; return; } diff --git a/packages/kbn-grid-layout/grid/grid_layout.tsx b/packages/kbn-grid-layout/grid/grid_layout.tsx index 7587e57e9dd1c..c6bbd94dabe56 100644 --- a/packages/kbn-grid-layout/grid/grid_layout.tsx +++ b/packages/kbn-grid-layout/grid/grid_layout.tsx @@ -7,10 +7,11 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import { useBatchedPublishingSubjects } from '@kbn/presentation-publishing'; import React from 'react'; + +import { useBatchedPublishingSubjects } from '@kbn/presentation-publishing'; + import { GridHeightSmoother } from './grid_height_smoother'; -import { GridOverlay } from './grid_overlay'; import { GridRow } from './grid_row'; import { GridLayoutData, GridSettings } from './types'; import { useGridLayoutEvents } from './use_grid_layout_events'; @@ -49,17 +50,17 @@ export const GridLayout = ({ key={rowData.title} rowIndex={rowIndex} runtimeSettings={runtimeSettings} - activePanelId={interactionEvent?.id} renderPanelContents={renderPanelContents} targetRowIndex={interactionEvent?.targetRowIndex} + gridLayoutStateManager={gridLayoutStateManager} toggleIsCollapsed={() => { const currentLayout = gridLayoutStateManager.gridLayout$.value; currentLayout[rowIndex].isCollapsed = !currentLayout[rowIndex].isCollapsed; gridLayoutStateManager.gridLayout$.next(currentLayout); }} setInteractionEvent={(nextInteractionEvent) => { - if (!nextInteractionEvent) { - gridLayoutStateManager.hideDragPreview(); + if (nextInteractionEvent?.type === 'drop') { + gridLayoutStateManager.activePanel$.next(undefined); } gridLayoutStateManager.interactionEvent$.next(nextInteractionEvent); }} @@ -69,10 +70,6 @@ export const GridLayout = ({ })} - ); }; diff --git a/packages/kbn-grid-layout/grid/grid_overlay.tsx b/packages/kbn-grid-layout/grid/grid_overlay.tsx deleted file mode 100644 index 958618d2f8f38..0000000000000 --- a/packages/kbn-grid-layout/grid/grid_overlay.tsx +++ /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 - * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side - * Public License v 1"; you may not use this file except in compliance with, at - * your election, the "Elastic License 2.0", the "GNU Affero General Public - * License v3.0 only", or the "Server Side Public License, v 1". - */ - -import { EuiPortal, EuiText, transparentize } from '@elastic/eui'; -import { css } from '@emotion/react'; -import { i18n } from '@kbn/i18n'; -import { euiThemeVars } from '@kbn/ui-theme'; -import React, { useRef, useState } from 'react'; -import { GridLayoutStateManager, PanelInteractionEvent } from './types'; - -type ScrollDirection = 'up' | 'down'; - -const scrollLabels: { [key in ScrollDirection]: string } = { - up: i18n.translate('kbnGridLayout.overlays.scrollUpLabel', { defaultMessage: 'Scroll up' }), - down: i18n.translate('kbnGridLayout.overlays.scrollDownLabel', { defaultMessage: 'Scroll down' }), -}; - -const scrollOnInterval = (direction: ScrollDirection) => { - const interval = setInterval(() => { - window.scroll({ - top: window.scrollY + (direction === 'down' ? 50 : -50), - behavior: 'smooth', - }); - }, 100); - return interval; -}; - -const ScrollOnHover = ({ direction, hide }: { hide: boolean; direction: ScrollDirection }) => { - const [isActive, setIsActive] = useState(false); - const scrollInterval = useRef(null); - const stopScrollInterval = () => { - if (scrollInterval.current) { - clearInterval(scrollInterval.current); - } - }; - - return ( -
{ - setIsActive(true); - scrollInterval.current = scrollOnInterval(direction); - }} - onMouseLeave={() => { - setIsActive(false); - stopScrollInterval(); - }} - css={css` - width: 100%; - position: fixed; - display: flex; - align-items: center; - flex-direction: column; - justify-content: center; - opacity: ${hide ? 0 : 1}; - transition: opacity 100ms linear; - padding: ${euiThemeVars.euiSizeM}; - ${direction === 'down' ? 'bottom: 0;' : 'top: 0;'} - `} - > - {direction === 'up' && ( -
- )} -
- - {scrollLabels[direction]} - -
-
- ); -}; - -export const GridOverlay = ({ - interactionEvent, - gridLayoutStateManager, -}: { - interactionEvent?: PanelInteractionEvent; - gridLayoutStateManager: GridLayoutStateManager; -}) => { - return ( - -
- - -
-
- - ); -}; diff --git a/packages/kbn-grid-layout/grid/grid_panel.tsx b/packages/kbn-grid-layout/grid/grid_panel.tsx index 7bad4d1af037c..fbe34c4b68e15 100644 --- a/packages/kbn-grid-layout/grid/grid_panel.tsx +++ b/packages/kbn-grid-layout/grid/grid_panel.tsx @@ -7,63 +7,38 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ +import React, { forwardRef } from 'react'; + import { EuiIcon, EuiPanel, euiFullHeight, transparentize, useEuiOverflowScroll, + useEuiTheme, } from '@elastic/eui'; import { css } from '@emotion/react'; import { euiThemeVars } from '@kbn/ui-theme'; -import React, { useCallback, useRef } from 'react'; + import { GridPanelData, PanelInteractionEvent } from './types'; -export const GridPanel = ({ - activePanelId, - panelData, - renderPanelContents, - setInteractionEvent, -}: { - panelData: GridPanelData; - activePanelId: string | undefined; - renderPanelContents: (panelId: string) => React.ReactNode; - setInteractionEvent: (interactionData?: Omit) => void; -}) => { - const panelRef = useRef(null); +export const GridPanel = forwardRef< + HTMLDivElement, + { + panelData: GridPanelData; + activePanelId: string | undefined; + renderPanelContents: (panelId: string) => React.ReactNode; + interactionStart: ( + type: PanelInteractionEvent['type'], + e: React.MouseEvent + ) => void; + } +>(({ activePanelId, panelData, renderPanelContents, interactionStart }, panelRef) => { + const { euiTheme } = useEuiTheme(); const thisPanelActive = activePanelId === panelData.id; - const interactionStart = useCallback( - (type: 'drag' | 'resize', e: React.MouseEvent) => { - if (!panelRef.current) return; - e.preventDefault(); - e.stopPropagation(); - const panelRect = panelRef.current.getBoundingClientRect(); - setInteractionEvent({ - type, - id: panelData.id, - panelDiv: panelRef.current, - mouseOffsets: { - top: e.clientY - panelRect.top, - left: e.clientX - panelRect.left, - right: e.clientX - panelRect.right, - bottom: e.clientY - panelRect.bottom, - }, - }); - }, - [panelData.id, setInteractionEvent] - ); - return ( -
+
interactionStart('drag', e)} + onMouseUp={(e) => interactionStart('drop', e)} >
@@ -109,6 +93,7 @@ export const GridPanel = ({
interactionStart('resize', e)} + onMouseUp={(e) => interactionStart('drop', e)} css={css` right: 0; bottom: 0; @@ -139,4 +124,4 @@ export const GridPanel = ({
); -}; +}); diff --git a/packages/kbn-grid-layout/grid/grid_row.tsx b/packages/kbn-grid-layout/grid/grid_row.tsx index b2a9375c390b1..917f661c91740 100644 --- a/packages/kbn-grid-layout/grid/grid_row.tsx +++ b/packages/kbn-grid-layout/grid/grid_row.tsx @@ -7,13 +7,21 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ +import React, { forwardRef, useMemo, useRef } from 'react'; + import { EuiButtonIcon, EuiFlexGroup, EuiSpacer, EuiTitle, transparentize } from '@elastic/eui'; import { css } from '@emotion/react'; import { i18n } from '@kbn/i18n'; import { euiThemeVars } from '@kbn/ui-theme'; -import React, { forwardRef, useMemo } from 'react'; +import { useStateFromPublishingSubject } from '@kbn/presentation-publishing'; + import { GridPanel } from './grid_panel'; -import { GridRowData, PanelInteractionEvent, RuntimeGridSettings } from './types'; +import { + GridLayoutStateManager, + GridRowData, + PanelInteractionEvent, + RuntimeGridSettings, +} from './types'; const gridColor = transparentize(euiThemeVars.euiColorSuccess, 0.2); const getGridBackgroundCSS = (settings: RuntimeGridSettings) => { @@ -32,28 +40,31 @@ export const GridRow = forwardRef< rowIndex: number; rowData: GridRowData; toggleIsCollapsed: () => void; - activePanelId: string | undefined; targetRowIndex: number | undefined; runtimeSettings: RuntimeGridSettings; renderPanelContents: (panelId: string) => React.ReactNode; setInteractionEvent: (interactionData?: PanelInteractionEvent) => void; + gridLayoutStateManager: GridLayoutStateManager; } >( ( { rowData, rowIndex, - activePanelId, targetRowIndex, runtimeSettings, toggleIsCollapsed, renderPanelContents, setInteractionEvent, + gridLayoutStateManager, }, gridRef ) => { + const dragPreviewRef = useRef(null); + const activePanel = useStateFromPublishingSubject(gridLayoutStateManager.activePanel$); + const { gutterSize, columnCount, rowHeight } = runtimeSettings; - const isGridTargeted = activePanelId && targetRowIndex === rowIndex; + const isGridTargeted = activePanel?.id && targetRowIndex === rowIndex; // calculate row count based on the number of rows needed to fit all panels const rowCount = useMemo(() => { @@ -107,20 +118,58 @@ export const GridRow = forwardRef< { - if (partialInteractionEvent) { - setInteractionEvent({ - ...partialInteractionEvent, - targetRowIndex: rowIndex, - }); - return; + interactionStart={(type, e) => { + e.preventDefault(); + e.stopPropagation(); + const panelRef = gridLayoutStateManager.panelRefs.current[rowIndex][panelData.id]; + if (!panelRef) return; + + const panelRect = panelRef.getBoundingClientRect(); + setInteractionEvent({ + type, + id: panelData.id, + panelDiv: panelRef, + targetRowIndex: rowIndex, + mouseOffsets: { + top: e.clientY - panelRect.top, + left: e.clientX - panelRect.left, + right: e.clientX - panelRect.right, + bottom: e.clientY - panelRect.bottom, + }, + }); + }} + ref={(element) => { + if (!gridLayoutStateManager.panelRefs.current[rowIndex]) { + gridLayoutStateManager.panelRefs.current[rowIndex] = {}; } - setInteractionEvent(); + gridLayoutStateManager.panelRefs.current[rowIndex][panelData.id] = element; }} /> ))} + + {/* render the drag preview if this row is currently being targetted */} + {isGridTargeted && ( +
+ )}
)} diff --git a/packages/kbn-grid-layout/grid/types.ts b/packages/kbn-grid-layout/grid/types.ts index 3d63c9f6ea01a..3a88eeb33baba 100644 --- a/packages/kbn-grid-layout/grid/types.ts +++ b/packages/kbn-grid-layout/grid/types.ts @@ -46,21 +46,25 @@ export interface GridSettings { */ export type RuntimeGridSettings = GridSettings & { columnPixelWidth: number }; -export interface GridLayoutStateManager { - hideDragPreview: () => void; - updatePreviewElement: (rect: { +export interface ActivePanel { + id: string; + position: { top: number; left: number; bottom: number; right: number; - }) => void; + }; +} +export interface GridLayoutStateManager { gridDimensions$: BehaviorSubject; gridLayout$: BehaviorSubject; runtimeSettings$: BehaviorSubject; - rowRefs: React.MutableRefObject>; - dragPreviewRef: React.MutableRefObject; + activePanel$: BehaviorSubject; interactionEvent$: BehaviorSubject; + + rowRefs: React.MutableRefObject>; + panelRefs: React.MutableRefObject>; } /** @@ -70,7 +74,7 @@ export interface PanelInteractionEvent { /** * The type of interaction being performed. */ - type: 'drag' | 'resize'; + type: 'drag' | 'resize' | 'drop'; /** * The id of the panel being interacted with. diff --git a/packages/kbn-grid-layout/grid/use_grid_layout_events.ts b/packages/kbn-grid-layout/grid/use_grid_layout_events.ts index 34fa84eeb131e..dfbd013d3642a 100644 --- a/packages/kbn-grid-layout/grid/use_grid_layout_events.ts +++ b/packages/kbn-grid-layout/grid/use_grid_layout_events.ts @@ -8,8 +8,9 @@ */ import { useEffect, useRef } from 'react'; + import { resolveGridRow } from './resolve_grid_row'; -import { GridPanelData, GridLayoutStateManager } from './types'; +import { GridLayoutStateManager, GridPanelData } from './types'; export const isGridDataEqual = (a?: GridPanelData, b?: GridPanelData) => { return ( @@ -35,12 +36,11 @@ export const useGridLayoutEvents = ({ useEffect(() => { const { runtimeSettings$, interactionEvent$, gridLayout$ } = gridLayoutStateManager; const calculateUserEvent = (e: Event) => { - if (!interactionEvent$.value) return; + if (!interactionEvent$.value || interactionEvent$.value.type === 'drop') return; e.preventDefault(); e.stopPropagation(); const gridRowElements = gridLayoutStateManager.rowRefs.current; - const previewElement = gridLayoutStateManager.dragPreviewRef.current; const interactionEvent = interactionEvent$.value; const isResize = interactionEvent?.type === 'resize'; @@ -53,7 +53,7 @@ export const useGridLayoutEvents = ({ } })(); - if (!runtimeSettings$.value || !previewElement || !gridRowElements || !currentGridData) { + if (!runtimeSettings$.value || !gridRowElements || !currentGridData) { return; } @@ -68,7 +68,7 @@ export const useGridLayoutEvents = ({ bottom: mouseTargetPixel.y - interactionEvent.mouseOffsets.bottom, right: mouseTargetPixel.x - interactionEvent.mouseOffsets.right, }; - gridLayoutStateManager.updatePreviewElement(previewRect); + gridLayoutStateManager.activePanel$.next({ id: interactionEvent.id, position: previewRect }); // find the grid that the preview rect is over const previewBottom = @@ -154,29 +154,19 @@ export const useGridLayoutEvents = ({ const resolvedOriginGrid = resolveGridRow(originGrid); nextLayout[lastRowIndex] = resolvedOriginGrid; } + gridLayout$.next(nextLayout); } }; - const onMouseUp = (e: MouseEvent) => { - if (!interactionEvent$.value) return; - e.preventDefault(); - e.stopPropagation(); - - interactionEvent$.next(undefined); - gridLayoutStateManager.hideDragPreview(); - }; - const onMouseMove = (e: MouseEvent) => { mouseClientPosition.current = { x: e.clientX, y: e.clientY }; calculateUserEvent(e); }; - document.addEventListener('mouseup', onMouseUp); document.addEventListener('mousemove', onMouseMove); document.addEventListener('scroll', calculateUserEvent); return () => { - document.removeEventListener('mouseup', onMouseUp); document.removeEventListener('mousemove', onMouseMove); document.removeEventListener('scroll', calculateUserEvent); }; diff --git a/packages/kbn-grid-layout/grid/use_grid_layout_state.ts b/packages/kbn-grid-layout/grid/use_grid_layout_state.ts index e10ce12fe5b6b..cdb99a9ebbfd0 100644 --- a/packages/kbn-grid-layout/grid/use_grid_layout_state.ts +++ b/packages/kbn-grid-layout/grid/use_grid_layout_state.ts @@ -7,10 +7,12 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ +import { i18n } from '@kbn/i18n'; import { useEffect, useMemo, useRef } from 'react'; -import { BehaviorSubject, debounceTime } from 'rxjs'; +import { BehaviorSubject, combineLatest, debounceTime, map, retry } from 'rxjs'; import useResizeObserver, { type ObservedSize } from 'use-resize-observer/polyfilled'; import { + ActivePanel, GridLayoutData, GridLayoutStateManager, GridSettings, @@ -27,7 +29,7 @@ export const useGridLayoutState = ({ setDimensionsRef: (instance: HTMLDivElement | null) => void; } => { const rowRefs = useRef>([]); - const dragPreviewRef = useRef(null); + const panelRefs = useRef>([]); // eslint-disable-next-line react-hooks/exhaustive-deps const { initialLayout, gridSettings } = useMemo(() => getCreationOptions(), []); @@ -36,6 +38,7 @@ export const useGridLayoutState = ({ const gridLayout$ = new BehaviorSubject(initialLayout); const gridDimensions$ = new BehaviorSubject({ width: 0, height: 0 }); const interactionEvent$ = new BehaviorSubject(undefined); + const activePanel$ = new BehaviorSubject(undefined); const runtimeSettings$ = new BehaviorSubject({ ...gridSettings, columnPixelWidth: 0, @@ -43,41 +46,21 @@ export const useGridLayoutState = ({ return { rowRefs, + panelRefs, gridLayout$, - dragPreviewRef, + activePanel$, gridDimensions$, runtimeSettings$, interactionEvent$, - updatePreviewElement: (previewRect: { - top: number; - bottom: number; - left: number; - right: number; - }) => { - if (!dragPreviewRef.current) return; - dragPreviewRef.current.style.opacity = '1'; - dragPreviewRef.current.style.left = `${previewRect.left}px`; - dragPreviewRef.current.style.top = `${previewRect.top}px`; - dragPreviewRef.current.style.width = `${Math.max( - previewRect.right - previewRect.left, - runtimeSettings$.value.columnPixelWidth - )}px`; - dragPreviewRef.current.style.height = `${Math.max( - previewRect.bottom - previewRect.top, - runtimeSettings$.value.rowHeight - )}px`; - }, - hideDragPreview: () => { - if (!dragPreviewRef.current) return; - dragPreviewRef.current.style.opacity = '0'; - }, }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); useEffect(() => { - // debounce width changes to avoid unnecessary column width recalculation. - const subscription = gridLayoutStateManager.gridDimensions$ + /** + * debounce width changes to avoid unnecessary column width recalculation. + */ + const resizeSubscription = gridLayoutStateManager.gridDimensions$ .pipe(debounceTime(250)) .subscribe((dimensions) => { const elementWidth = dimensions.width ?? 0; @@ -86,7 +69,116 @@ export const useGridLayoutState = ({ gridSettings.columnCount; gridLayoutStateManager.runtimeSettings$.next({ ...gridSettings, columnPixelWidth }); }); - return () => subscription.unsubscribe(); + + /** + * on layout change, update the styles of every panel so that it renders as expected + */ + const onLayoutChangeSubscription = combineLatest([ + gridLayoutStateManager.gridLayout$, + gridLayoutStateManager.activePanel$, + ]) + .pipe( + map(([gridLayout, activePanel]) => { + // wait for all panel refs to be ready before continuing + for (let rowIndex = 0; rowIndex < gridLayout.length; rowIndex++) { + const currentRow = gridLayout[rowIndex]; + Object.keys(currentRow.panels).forEach((key) => { + const panelRef = gridLayoutStateManager.panelRefs.current[rowIndex][key]; + if (!panelRef && !currentRow.isCollapsed) { + throw new Error( + i18n.translate('kbnGridLayout.panelRefNotFoundError', { + defaultMessage: 'Panel reference does not exist', // the retry will catch this error + }) + ); + } + }); + } + return { gridLayout, activePanel }; + }), + retry({ delay: 10 }) // retry until panel references all exist + ) + .subscribe(({ gridLayout, activePanel }) => { + const runtimeSettings = gridLayoutStateManager.runtimeSettings$.getValue(); + const currentInteractionEvent = gridLayoutStateManager.interactionEvent$.getValue(); + + for (let rowIndex = 0; rowIndex < gridLayout.length; rowIndex++) { + if (activePanel && rowIndex !== currentInteractionEvent?.targetRowIndex) { + /** + * If there is an interaction event happening but the current row is not being targetted, it + * does not need to be re-rendered; so, skip setting the panel styles of this row. + * + * If there is **no** interaction event, then this is the initial render so the styles of every + * panel should be initialized; so, don't skip setting the panel styles. + */ + continue; + } + + // re-render the targetted row + const currentRow = gridLayout[rowIndex]; + Object.keys(currentRow.panels).forEach((key) => { + const panel = currentRow.panels[key]; + const panelRef = gridLayoutStateManager.panelRefs.current[rowIndex][key]; + if (!panelRef) { + return; + } + + const isResize = currentInteractionEvent?.type === 'resize'; + if (panel.id === activePanel?.id) { + // if the current panel is active, give it fixed positioning depending on the interaction event + const { position: draggingPosition } = activePanel; + + if (isResize) { + // if the current panel is being resized, ensure it is not shrunk past the size of a single cell + panelRef.style.width = `${Math.max( + draggingPosition.right - draggingPosition.left, + runtimeSettings.columnPixelWidth + )}px`; + panelRef.style.height = `${Math.max( + draggingPosition.bottom - draggingPosition.top, + runtimeSettings.rowHeight + )}px`; + + // undo any "lock to grid" styles **except** for the top left corner, which stays locked + panelRef.style.gridColumnStart = `${panel.column + 1}`; + panelRef.style.gridRowStart = `${panel.row + 1}`; + panelRef.style.gridColumnEnd = ``; + panelRef.style.gridRowEnd = ``; + } else { + // if the current panel is being dragged, render it with a fixed position + size + panelRef.style.position = 'fixed'; + panelRef.style.left = `${draggingPosition.left}px`; + panelRef.style.top = `${draggingPosition.top}px`; + panelRef.style.width = `${draggingPosition.right - draggingPosition.left}px`; + panelRef.style.height = `${draggingPosition.bottom - draggingPosition.top}px`; + + // undo any "lock to grid" styles + panelRef.style.gridColumnStart = ``; + panelRef.style.gridRowStart = ``; + panelRef.style.gridColumnEnd = ``; + panelRef.style.gridRowEnd = ``; + } + } else { + // if the panel is not being dragged and/or resized, undo any fixed position styles + panelRef.style.position = ''; + panelRef.style.left = ``; + panelRef.style.top = ``; + panelRef.style.width = ``; + panelRef.style.height = ``; + + // and render the panel locked to the grid + panelRef.style.gridColumnStart = `${panel.column + 1}`; + panelRef.style.gridColumnEnd = `${panel.column + 1 + panel.width}`; + panelRef.style.gridRowStart = `${panel.row + 1}`; + panelRef.style.gridRowEnd = `${panel.row + 1 + panel.height}`; + } + }); + } + }); + + return () => { + resizeSubscription.unsubscribe(); + onLayoutChangeSubscription.unsubscribe(); + }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); From 6f44bf5b65843ce656c6b66e37403be91e19b96b Mon Sep 17 00:00:00 2001 From: Marta Bondyra <4283304+mbondyra@users.noreply.github.com> Date: Tue, 22 Oct 2024 22:04:07 +0200 Subject: [PATCH 17/22] [Lens] dont use legacy metric in the suggestions (#197101) ## Summary Fixes https://github.com/elastic/kibana/issues/197078 --- .../legacy_metric/metric_suggestions.test.ts | 4 ++-- .../legacy_metric/metric_suggestions.ts | 2 +- .../visualizations/metric/suggestions.test.ts | 12 ++++++------ .../public/visualizations/metric/suggestions.ts | 13 +++++++++---- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/x-pack/plugins/lens/public/visualizations/legacy_metric/metric_suggestions.test.ts b/x-pack/plugins/lens/public/visualizations/legacy_metric/metric_suggestions.test.ts index 794c8cedf5164..0de7fc26608d2 100644 --- a/x-pack/plugins/lens/public/visualizations/legacy_metric/metric_suggestions.test.ts +++ b/x-pack/plugins/lens/public/visualizations/legacy_metric/metric_suggestions.test.ts @@ -171,7 +171,7 @@ describe('metric_suggestions', () => { expect(rest).toHaveLength(0); expect(suggestion).toMatchInlineSnapshot(` Object { - "hide": false, + "hide": true, "previewIcon": [Function], "score": 0.1, "state": Object { @@ -207,7 +207,7 @@ describe('metric_suggestions', () => { expect(rest).toHaveLength(0); expect(suggestion).toMatchInlineSnapshot(` Object { - "hide": false, + "hide": true, "previewIcon": [Function], "score": 0.1, "state": Object { diff --git a/x-pack/plugins/lens/public/visualizations/legacy_metric/metric_suggestions.ts b/x-pack/plugins/lens/public/visualizations/legacy_metric/metric_suggestions.ts index 93e564cde841e..1b190cc0ce66f 100644 --- a/x-pack/plugins/lens/public/visualizations/legacy_metric/metric_suggestions.ts +++ b/x-pack/plugins/lens/public/visualizations/legacy_metric/metric_suggestions.ts @@ -52,7 +52,7 @@ function getSuggestion( return { title, - hide: datasourceId === 'textBased', + hide: true, score: 0.1, previewIcon: IconChartMetric, state: { diff --git a/x-pack/plugins/lens/public/visualizations/metric/suggestions.test.ts b/x-pack/plugins/lens/public/visualizations/metric/suggestions.test.ts index fff365e0e89dd..4f1c7b46d06be 100644 --- a/x-pack/plugins/lens/public/visualizations/metric/suggestions.test.ts +++ b/x-pack/plugins/lens/public/visualizations/metric/suggestions.test.ts @@ -159,7 +159,7 @@ describe('metric suggestions', () => { // should ignore bucketed column for initial drag }, title: 'Metric', - hide: true, + hide: false, previewIcon: IconChartMetric, score: 0.51, }, @@ -189,7 +189,7 @@ describe('metric suggestions', () => { breakdownByAccessor: bucketColumn.columnId, }, title: 'Metric', - hide: true, + hide: false, previewIcon: IconChartMetric, score: 0.51, }, @@ -221,7 +221,7 @@ describe('metric suggestions', () => { breakdownByAccessor: bucketColumn.columnId, }, title: 'Metric', - hide: true, + hide: false, previewIcon: IconChartMetric, score: 0.51, }, @@ -294,7 +294,7 @@ describe('metric suggestions', () => { breakdownByAccessor: bucketColumn.columnId, }, title: 'Metric', - hide: true, + hide: false, previewIcon: IconChartMetric, score: 0.52, }, @@ -326,7 +326,7 @@ describe('metric suggestions', () => { breakdownByAccessor: bucketColumn.columnId, }, title: 'Metric', - hide: true, + hide: false, previewIcon: IconChartMetric, score: 0.52, }, @@ -357,7 +357,7 @@ describe('metric suggestions', () => { breakdownByAccessor: bucketColumn.columnId, }, title: 'Metric', - hide: true, + hide: false, previewIcon: IconChartMetric, score: 0.52, }, diff --git a/x-pack/plugins/lens/public/visualizations/metric/suggestions.ts b/x-pack/plugins/lens/public/visualizations/metric/suggestions.ts index b405ea646d980..877d2b45d66af 100644 --- a/x-pack/plugins/lens/public/visualizations/metric/suggestions.ts +++ b/x-pack/plugins/lens/public/visualizations/metric/suggestions.ts @@ -30,6 +30,8 @@ export const getSuggestions: Visualization['getSuggest const bucketedColumns = table.columns.filter(({ operation }) => operation.isBucketed); + const hasInterval = bucketedColumns.some(({ operation }) => operation.scale === 'interval'); + const unsupportedColumns = table.columns.filter( ({ operation }) => !supportedDataTypes.has(operation.dataType) && !operation.isBucketed ); @@ -59,11 +61,10 @@ export const getSuggestions: Visualization['getSuggest layerId: table.layerId, layerType: LayerTypes.DATA, }, - title: metricLabel, + title: metricColumns[0]?.operation.label || metricLabel, previewIcon: IconChartMetric, score: 0.5, - // don't show suggestions since we're in tech preview - hide: true, + hide: hasInterval, }; const accessorMappings: Pick = @@ -72,7 +73,11 @@ export const getSuggestions: Visualization['getSuggest breakdownByAccessor: bucketedColumns[0]?.columnId, }; - baseSuggestion.score += 0.01 * Object.values(accessorMappings).filter(Boolean).length; + baseSuggestion.score = Number( + (baseSuggestion.score + 0.01 * Object.values(accessorMappings).filter(Boolean).length).toFixed( + 2 + ) + ); const suggestion = { ...baseSuggestion, From 5034558446d8350d0b5a81efc3100ee9cb7bda72 Mon Sep 17 00:00:00 2001 From: Rodney Norris Date: Tue, 22 Oct 2024 16:29:55 -0500 Subject: [PATCH 18/22] [Search][FTR] Fix / Improve tests (#197264) ## Summary Closes #196981 - Updated Search Index Details with data test suite to check for index documents first, this should help reduce flakey tests in MKI since we will ensure the documents are available before moving forward - Updated URL checks to use regex instead `.equal` with the mocked value. This should ensure the tests can pass in MKI and with integration testing - Removed the navigation check for the Maps page, this was causing intermittent failures due to a modal on the page. - Updated the getting started test suite to use `developer` role instead of `admin` ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [x] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed --- .../svl_search_elasticsearch_start_page.ts | 4 ++-- .../page_objects/svl_search_index_detail_page.ts | 4 ++-- .../test_suites/search/elasticsearch_start.ts | 3 --- .../test_suites/search/getting_started.ts | 2 +- .../functional/test_suites/search/navigation.ts | 15 --------------- .../test_suites/search/search_index_detail.ts | 9 +++------ 6 files changed, 8 insertions(+), 29 deletions(-) diff --git a/x-pack/test_serverless/functional/page_objects/svl_search_elasticsearch_start_page.ts b/x-pack/test_serverless/functional/page_objects/svl_search_elasticsearch_start_page.ts index 0e3bc4b82a298..1f4294d4c8f41 100644 --- a/x-pack/test_serverless/functional/page_objects/svl_search_elasticsearch_start_page.ts +++ b/x-pack/test_serverless/functional/page_objects/svl_search_elasticsearch_start_page.ts @@ -85,8 +85,8 @@ export function SvlSearchElasticsearchStartPageProvider({ getService }: FtrProvi }, async expectO11yTrialLink() { await testSubjects.existOrFail('startO11yTrialBtn'); - expect(await testSubjects.getAttribute('startO11yTrialBtn', 'href')).equal( - 'https://fake-cloud.elastic.co/projects/create/observability/start' + expect(await testSubjects.getAttribute('startO11yTrialBtn', 'href')).match( + /^https?\:\/\/.*\/projects\/create\/observability\/start/ ); expect(await testSubjects.getAttribute('startO11yTrialBtn', 'target')).equal('_blank'); }, diff --git a/x-pack/test_serverless/functional/page_objects/svl_search_index_detail_page.ts b/x-pack/test_serverless/functional/page_objects/svl_search_index_detail_page.ts index 656c75fb308bc..1b355138173d6 100644 --- a/x-pack/test_serverless/functional/page_objects/svl_search_index_detail_page.ts +++ b/x-pack/test_serverless/functional/page_objects/svl_search_index_detail_page.ts @@ -27,8 +27,8 @@ export function SvlSearchIndexDetailPageProvider({ getService }: FtrProviderCont }, async expectConnectionDetails() { await testSubjects.existOrFail('connectionDetailsEndpoint', { timeout: 2000 }); - expect(await (await testSubjects.find('connectionDetailsEndpoint')).getVisibleText()).to.be( - 'https://fakeprojectid.es.fake-domain.cld.elstc.co:443' + expect(await (await testSubjects.find('connectionDetailsEndpoint')).getVisibleText()).match( + /^https?\:\/\/.*(\:\d+)?/ ); }, async expectQuickStats() { diff --git a/x-pack/test_serverless/functional/test_suites/search/elasticsearch_start.ts b/x-pack/test_serverless/functional/test_suites/search/elasticsearch_start.ts index 7ff1505304761..129f769283b34 100644 --- a/x-pack/test_serverless/functional/test_suites/search/elasticsearch_start.ts +++ b/x-pack/test_serverless/functional/test_suites/search/elasticsearch_start.ts @@ -27,9 +27,6 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { }; describe('Elasticsearch Start [Onboarding Empty State]', function () { - // fails on MKI, see https://github.com/elastic/kibana/issues/196981 - this.tags(['failsOnMKI']); - describe('developer', function () { before(async () => { await pageObjects.svlCommonPage.loginWithRole('developer'); diff --git a/x-pack/test_serverless/functional/test_suites/search/getting_started.ts b/x-pack/test_serverless/functional/test_suites/search/getting_started.ts index dac9b2d7dae94..4a2c34d98eebc 100644 --- a/x-pack/test_serverless/functional/test_suites/search/getting_started.ts +++ b/x-pack/test_serverless/functional/test_suites/search/getting_started.ts @@ -15,7 +15,7 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { describe('getting started', function () { before(async () => { - await pageObjects.svlCommonPage.loginAsAdmin(); + await pageObjects.svlCommonPage.loginWithRole('developer'); }); it('has serverless side nav', async () => { diff --git a/x-pack/test_serverless/functional/test_suites/search/navigation.ts b/x-pack/test_serverless/functional/test_suites/search/navigation.ts index 88f327d6f1ac2..a6da7b1467e9a 100644 --- a/x-pack/test_serverless/functional/test_suites/search/navigation.ts +++ b/x-pack/test_serverless/functional/test_suites/search/navigation.ts @@ -130,21 +130,6 @@ export default function ({ getPageObject, getService }: FtrProviderContext) { await solutionNavigation.breadcrumbs.expectBreadcrumbExists({ deepLinkId: 'dashboards', }); - // check Other tools - // > Maps - await solutionNavigation.sidenav.clickLink({ - deepLinkId: 'maps', - }); - await solutionNavigation.sidenav.expectLinkActive({ - deepLinkId: 'maps', - }); - await solutionNavigation.breadcrumbs.expectBreadcrumbExists({ text: 'Other tools' }); - await solutionNavigation.breadcrumbs.expectBreadcrumbExists({ - text: 'Maps', - }); - await solutionNavigation.breadcrumbs.expectBreadcrumbExists({ - deepLinkId: 'maps', - }); // check Getting Started await solutionNavigation.sidenav.clickLink({ diff --git a/x-pack/test_serverless/functional/test_suites/search/search_index_detail.ts b/x-pack/test_serverless/functional/test_suites/search/search_index_detail.ts index e7831d1f91b3a..fae3abc3ad8d2 100644 --- a/x-pack/test_serverless/functional/test_suites/search/search_index_detail.ts +++ b/x-pack/test_serverless/functional/test_suites/search/search_index_detail.ts @@ -25,9 +25,6 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { const indexName = 'test-my-index'; describe('Search index detail page', function () { - // fails on MKI, see https://github.com/elastic/kibana/issues/196981 - this.tags(['failsOnMKI']); - before(async () => { await pageObjects.svlCommonPage.loginWithRole('developer'); await pageObjects.svlApiKeys.deleteAPIKeys(); @@ -120,6 +117,9 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { }); await svlSearchNavigation.navigateToIndexDetailPage(indexName); }); + it('should have index documents', async () => { + await pageObjects.svlSearchIndexDetailPage.expectHasIndexDocuments(); + }); it('menu action item should be replaced with playground', async () => { await pageObjects.svlSearchIndexDetailPage.expectActionItemReplacedWhenHasDocs(); }); @@ -127,9 +127,6 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { await pageObjects.svlSearchIndexDetailPage.clickMoreOptionsActionsButton(); await pageObjects.svlSearchIndexDetailPage.expectAPIReferenceDocLinkExistsInMoreOptions(); }); - it('should have index documents', async () => { - await pageObjects.svlSearchIndexDetailPage.expectHasIndexDocuments(); - }); it('should have one document in quick stats', async () => { await pageObjects.svlSearchIndexDetailPage.expectQuickStatsToHaveDocumentCount(1); }); From 3c3f748d8367774e364490753c37668eb2cd0672 Mon Sep 17 00:00:00 2001 From: "elastic-renovate-prod[bot]" <174716857+elastic-renovate-prod[bot]@users.noreply.github.com> Date: Tue, 22 Oct 2024 17:29:04 -0500 Subject: [PATCH 19/22] Update dependency @redocly/cli to ^1.25.6 (main) (#197146) Co-authored-by: elastic-renovate-prod[bot] <174716857+elastic-renovate-prod[bot]@users.noreply.github.com> --- package.json | 2 +- yarn.lock | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index f2eb15a482ae2..500f587e1166d 100644 --- a/package.json +++ b/package.json @@ -1496,7 +1496,7 @@ "@octokit/rest": "^17.11.2", "@parcel/watcher": "^2.1.0", "@playwright/test": "=1.46.0", - "@redocly/cli": "^1.25.5", + "@redocly/cli": "^1.25.6", "@statoscope/webpack-plugin": "^5.28.2", "@storybook/addon-a11y": "^6.5.16", "@storybook/addon-actions": "^6.5.16", diff --git a/yarn.lock b/yarn.lock index 67d502560380f..92309a0bd0a34 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8461,12 +8461,12 @@ require-from-string "^2.0.2" uri-js-replace "^1.0.1" -"@redocly/cli@^1.25.5": - version "1.25.5" - resolved "https://registry.yarnpkg.com/@redocly/cli/-/cli-1.25.5.tgz#258f6d23e8298814518ec4d24d023c1e21e3b081" - integrity sha512-sFh4A8wqwuig7mF/nYNVIyxSfKKZikWC+uVH6OB1IepYQXNsHFaLAU1VaNI9gS5mMvWmYx5SEuSCVB9LaNFBhw== +"@redocly/cli@^1.25.6": + version "1.25.8" + resolved "https://registry.yarnpkg.com/@redocly/cli/-/cli-1.25.8.tgz#fecd62d9ee1d564e6f0e1522f2c5648f514ce02b" + integrity sha512-oVFN3rpGFqupx57ZS0mF2B8grnk3i0xjTQrrMm1oftF3GEf7yTg5JzwnWi8KKRWuxin4qI7j+Id5AKgNQNmTKA== dependencies: - "@redocly/openapi-core" "1.25.5" + "@redocly/openapi-core" "1.25.8" abort-controller "^3.0.0" chokidar "^3.5.1" colorette "^1.2.0" @@ -8480,7 +8480,7 @@ pluralize "^8.0.0" react "^17.0.0 || ^18.2.0" react-dom "^17.0.0 || ^18.2.0" - redoc "~2.1.5" + redoc "~2.2.0" semver "^7.5.2" simple-websocket "^9.0.0" styled-components "^6.0.7" @@ -8491,10 +8491,10 @@ resolved "https://registry.yarnpkg.com/@redocly/config/-/config-0.12.1.tgz#7b905a17d710244550ef826542d0db164d5ace02" integrity sha512-RW3rSirfsPdr0uvATijRDU3f55SuZV3m7/ppdTDvGw4IB0cmeZRkFmqTrchxMqWP50Gfg1tpHnjdxUCNo0E2qg== -"@redocly/openapi-core@1.25.5", "@redocly/openapi-core@^1.4.0": - version "1.25.5" - resolved "https://registry.yarnpkg.com/@redocly/openapi-core/-/openapi-core-1.25.5.tgz#443b1488c8ef1ddcb8f407c3e7dd8cb7b388b427" - integrity sha512-BNgXjqesJu4L5f8F73c2hkkH5IdvjYCKYFgIl+m9oNgqGRIPBJjtiEGOx7jkQ6nElN4311z7Z4aTECtklaaHwg== +"@redocly/openapi-core@1.25.8", "@redocly/openapi-core@^1.4.0": + version "1.25.8" + resolved "https://registry.yarnpkg.com/@redocly/openapi-core/-/openapi-core-1.25.8.tgz#a3aff052b1d9d2db8ba86263ec994bbc85f6b8f1" + integrity sha512-eKKRqo2RYo7UIoDvIgcUB9ynhOjIWJnILXFz+VDevYeOBKd/CxvC0KbNRnuOrFqG3ip6363R/ONal2MyvuVrjg== dependencies: "@redocly/ajv" "^8.11.2" "@redocly/config" "^0.12.1" @@ -27328,10 +27328,10 @@ redent@^3.0.0: indent-string "^4.0.0" strip-indent "^3.0.0" -redoc@~2.1.5: - version "2.1.5" - resolved "https://registry.yarnpkg.com/redoc/-/redoc-2.1.5.tgz#421307b22036b244171095bfc7ea3cfd419563c8" - integrity sha512-POSbVg+7WLf+/5/c6GWLxL7+9t2D+1WlZdLN0a6qaCQc+ih3XYzteRBkXEN5kjrYrRNjdspfxTZkDLN5WV3Tzg== +redoc@~2.2.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/redoc/-/redoc-2.2.0.tgz#1043812696683f23e4b0fbe66fb50236fbe8b5bb" + integrity sha512-52rz/xJtpUBc3Y/GAkaX03czKhQXTxoU7WnkXNzRLuGwiGb/iEO4OgwcgQqtwHWrYNaZXTyqZ4MAVXpi/e1gAg== dependencies: "@cfaester/enzyme-adapter-react-18" "^0.8.0" "@redocly/openapi-core" "^1.4.0" From f7f122c891f7a3678faa3883727bf85bff9adfb9 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 23 Oct 2024 09:51:05 +1100 Subject: [PATCH 20/22] skip failing test suite (#197342) --- test/functional/apps/discover/group3/_lens_vis.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/functional/apps/discover/group3/_lens_vis.ts b/test/functional/apps/discover/group3/_lens_vis.ts index 321486a40238e..db526fe978610 100644 --- a/test/functional/apps/discover/group3/_lens_vis.ts +++ b/test/functional/apps/discover/group3/_lens_vis.ts @@ -110,7 +110,8 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { return seriesType; } - describe('discover lens vis', function () { + // Failing: See https://github.com/elastic/kibana/issues/197342 + describe.skip('discover lens vis', function () { before(async () => { await security.testUser.setRoles(['kibana_admin', 'test_logstash_reader']); await esArchiver.loadIfNeeded('test/functional/fixtures/es_archiver/logstash_functional'); From bec3d23d65cc2af89d18f3fb662054a27c28c37d Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 23 Oct 2024 09:51:30 +1100 Subject: [PATCH 21/22] skip failing test suite (#181889) --- x-pack/plugins/osquery/cypress/e2e/all/alerts_linked_apps.cy.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/plugins/osquery/cypress/e2e/all/alerts_linked_apps.cy.ts b/x-pack/plugins/osquery/cypress/e2e/all/alerts_linked_apps.cy.ts index 6ea1acfebabce..f7585d32a2bba 100644 --- a/x-pack/plugins/osquery/cypress/e2e/all/alerts_linked_apps.cy.ts +++ b/x-pack/plugins/osquery/cypress/e2e/all/alerts_linked_apps.cy.ts @@ -18,6 +18,7 @@ import { import { closeModalIfVisible, closeToastIfVisible } from '../../tasks/integrations'; import { RESULTS_TABLE, RESULTS_TABLE_BUTTON } from '../../screens/live_query'; +// Failing: See https://github.com/elastic/kibana/issues/181889 // Failing: See https://github.com/elastic/kibana/issues/181889 describe.skip( 'Alert Event Details', From 800e39224eed1335519d00e62e423af241495be9 Mon Sep 17 00:00:00 2001 From: Philippe Oberti Date: Tue, 22 Oct 2024 18:20:10 -0500 Subject: [PATCH 22/22] [Security Solution][Notes] - copy changes for note and timeline + move the unassociated note advanced setting under the Security Solution section (#197312) ## Summary This PR tackles 2 small tasks: - move the unassociated advanced settings introduced in [this PR](https://github.com/elastic/kibana/pull/194947) under the `Security Solution` category instead of `General` - make some copy changes on the notes functionality, mainly the following ([copy changes document](https://docs.google.com/document/d/10blyxRfkMIR8gk4cw6nFzajA-L63iUzQaxQXHauL8LM/edit#heading=h.mlyibn1i5q84)) - make sure we don't use a capital `N` for the word `note` - make sure that we use a capital `T` for the word `timeline` - change some of the no message and callout texts - prioritize using `attach` instead of `associate` All changes have been done with @nastasha-solomon. https://github.com/elastic/kibana/issues/193495 --- .../sections/investigations_translations.ts | 3 +- .../add_note_icon_item.test.tsx | 14 ++++----- .../header_actions/add_note_icon_item.tsx | 30 +++++++++++++++---- .../components/header_actions/translations.ts | 24 --------------- .../attach_to_active_timeline.test.tsx | 14 ++++----- .../components/attach_to_active_timeline.tsx | 12 ++++---- .../left/components/notes_details.tsx | 2 +- .../public/management/links.ts | 2 +- .../notes/components/open_flyout_button.tsx | 2 +- .../public/notes/components/search_row.tsx | 21 +++++++------ .../components/notes/save_timeline.test.tsx | 6 ++-- .../components/notes/save_timeline.tsx | 8 ++--- .../components/timeline/tabs/notes/index.tsx | 2 +- .../query_tab_unified_components.test.tsx | 4 +-- .../security_solution/server/ui_settings.ts | 1 + 15 files changed, 72 insertions(+), 73 deletions(-) diff --git a/x-pack/plugins/security_solution/public/app/solution_navigation/links/sections/investigations_translations.ts b/x-pack/plugins/security_solution/public/app/solution_navigation/links/sections/investigations_translations.ts index d70717783870a..931c3c20d4002 100644 --- a/x-pack/plugins/security_solution/public/app/solution_navigation/links/sections/investigations_translations.ts +++ b/x-pack/plugins/security_solution/public/app/solution_navigation/links/sections/investigations_translations.ts @@ -24,7 +24,8 @@ export const TIMELINE_DESCRIPTION = i18n.translate( export const NOTE_DESCRIPTION = i18n.translate( 'xpack.securitySolution.navLinks.investigations.note.title', { - defaultMessage: 'Oversee, revise and revisit the annotations within each document and timeline', + defaultMessage: + 'Oversee, revise, and revisit the notes attached to alerts, events and Timelines.', } ); diff --git a/x-pack/plugins/security_solution/public/common/components/header_actions/add_note_icon_item.test.tsx b/x-pack/plugins/security_solution/public/common/components/header_actions/add_note_icon_item.test.tsx index 98dfc83e9d3e8..b07aa7aedfcad 100644 --- a/x-pack/plugins/security_solution/public/common/components/header_actions/add_note_icon_item.test.tsx +++ b/x-pack/plugins/security_solution/public/common/components/header_actions/add_note_icon_item.test.tsx @@ -37,7 +37,7 @@ const renderTestComponent = (props: Partial = { timelineType: TimelineTypeEnum.default, eventId: 'event-1', - ariaLabel: 'Add Note', + ariaLabel: 'Add note', toggleShowNotes: toggleShowNotesMock, notesCount: 2, ...props, @@ -76,12 +76,12 @@ describe('AddEventNoteAction', () => { expect(NotesButtonMock).toHaveBeenCalledWith( expect.objectContaining({ - ariaLabel: 'Add Note', + ariaLabel: 'Add note', 'data-test-subj': 'add-note', isDisabled: false, timelineType: TimelineTypeEnum.default, toggleShowNotes: expect.any(Function), - toolTip: '2 Notes available. Click to view them & add more.', + toolTip: '2 notes available. Click to view them and add more.', eventId: 'event-1', notesCount: 2, }), @@ -98,12 +98,12 @@ describe('AddEventNoteAction', () => { expect(NotesButtonMock).toHaveBeenCalledWith( expect.objectContaining({ - ariaLabel: 'Add Note', + ariaLabel: 'Add note', 'data-test-subj': 'add-note', isDisabled: false, timelineType: TimelineTypeEnum.default, toggleShowNotes: expect.any(Function), - toolTip: '1 Note available. Click to view it & add more.', + toolTip: '1 note available. Click to view it and add more.', eventId: 'event-2', notesCount: 1, }), @@ -120,12 +120,12 @@ describe('AddEventNoteAction', () => { expect(NotesButtonMock).toHaveBeenCalledWith( expect.objectContaining({ - ariaLabel: 'Add Note', + ariaLabel: 'Add note', 'data-test-subj': 'add-note', isDisabled: false, timelineType: TimelineTypeEnum.default, toggleShowNotes: expect.any(Function), - toolTip: 'Add Note', + toolTip: 'Add note', eventId: 'event-3', notesCount: 0, }), diff --git a/x-pack/plugins/security_solution/public/common/components/header_actions/add_note_icon_item.tsx b/x-pack/plugins/security_solution/public/common/components/header_actions/add_note_icon_item.tsx index f9539b9062331..f931042863a62 100644 --- a/x-pack/plugins/security_solution/public/common/components/header_actions/add_note_icon_item.tsx +++ b/x-pack/plugins/security_solution/public/common/components/header_actions/add_note_icon_item.tsx @@ -6,12 +6,34 @@ */ import React, { useMemo } from 'react'; +import { i18n } from '@kbn/i18n'; import { NotesButton } from '../../../timelines/components/timeline/properties/helpers'; import { type TimelineType, TimelineTypeEnum } from '../../../../common/api/timeline'; import { useUserPrivileges } from '../user_privileges'; -import * as i18n from './translations'; import { ActionIconItem } from './action_icon_item'; +const NOTES_DISABLE_TOOLTIP = i18n.translate( + 'xpack.securitySolution.timeline.body.notes.disableEventTooltip', + { + defaultMessage: 'Notes cannot be added here while editing a template Timeline.', + } +); +const NOTES_ADD_TOOLTIP = i18n.translate( + 'xpack.securitySolution.timeline.body.notes.addNoteTooltip', + { + defaultMessage: 'Add note', + } +); +const NOTES_COUNT_TOOLTIP = ({ notesCount }: { notesCount: number }) => + i18n.translate( + 'xpack.securitySolution.timeline.body.notes.addNote.multipleNotesAvailableTooltip', + { + values: { notesCount }, + defaultMessage: + '{notesCount} {notesCount, plural, one {note} other {notes} } available. Click to view {notesCount, plural, one {it} other {them}} and add more.', + } + ); + interface AddEventNoteActionProps { ariaLabel?: string; timelineType: TimelineType; @@ -33,7 +55,7 @@ const AddEventNoteActionComponent: React.FC = ({ const { kibanaSecuritySolutionsPrivileges } = useUserPrivileges(); const NOTES_TOOLTIP = useMemo( - () => (notesCount > 0 ? i18n.NOTES_COUNT_TOOLTIP({ notesCount }) : i18n.NOTES_ADD_TOOLTIP), + () => (notesCount > 0 ? NOTES_COUNT_TOOLTIP({ notesCount }) : NOTES_ADD_TOOLTIP), [notesCount] ); @@ -45,9 +67,7 @@ const AddEventNoteActionComponent: React.FC = ({ isDisabled={kibanaSecuritySolutionsPrivileges.crud === false} timelineType={timelineType} toggleShowNotes={toggleShowNotes} - toolTip={ - timelineType === TimelineTypeEnum.template ? i18n.NOTES_DISABLE_TOOLTIP : NOTES_TOOLTIP - } + toolTip={timelineType === TimelineTypeEnum.template ? NOTES_DISABLE_TOOLTIP : NOTES_TOOLTIP} eventId={eventId} notesCount={notesCount} /> diff --git a/x-pack/plugins/security_solution/public/common/components/header_actions/translations.ts b/x-pack/plugins/security_solution/public/common/components/header_actions/translations.ts index 10832ccfac1e5..03ff7503643c9 100644 --- a/x-pack/plugins/security_solution/public/common/components/header_actions/translations.ts +++ b/x-pack/plugins/security_solution/public/common/components/header_actions/translations.ts @@ -14,30 +14,6 @@ export const OPEN_SESSION_VIEW = i18n.translate( } ); -export const NOTES_DISABLE_TOOLTIP = i18n.translate( - 'xpack.securitySolution.timeline.body.notes.disableEventTooltip', - { - defaultMessage: 'Notes may not be added here while editing a template timeline', - } -); - -export const NOTES_ADD_TOOLTIP = i18n.translate( - 'xpack.securitySolution.timeline.body.notes.addNoteTooltip', - { - defaultMessage: 'Add Note', - } -); - -export const NOTES_COUNT_TOOLTIP = ({ notesCount }: { notesCount: number }) => - i18n.translate( - 'xpack.securitySolution.timeline.body.notes.addNote.multipleNotesAvailableTooltip', - { - values: { notesCount }, - defaultMessage: - '{notesCount} {notesCount, plural, one {Note} other {Notes} } available. Click to view {notesCount, plural, one {it} other {them}} & add more.', - } - ); - export const SORT_FIELDS = i18n.translate('xpack.securitySolution.timeline.sortFieldsButton', { defaultMessage: 'Sort fields', }); diff --git a/x-pack/plugins/security_solution/public/flyout/document_details/left/components/attach_to_active_timeline.test.tsx b/x-pack/plugins/security_solution/public/flyout/document_details/left/components/attach_to_active_timeline.test.tsx index 610354f8af822..51f5641af0f9c 100644 --- a/x-pack/plugins/security_solution/public/flyout/document_details/left/components/attach_to_active_timeline.test.tsx +++ b/x-pack/plugins/security_solution/public/flyout/document_details/left/components/attach_to_active_timeline.test.tsx @@ -50,13 +50,13 @@ describe('AttachToActiveTimeline', () => { expect(getByTestId(SAVE_TIMELINE_BUTTON_TEST_ID)).toBeInTheDocument(); expect(getByTestId(SAVE_TIMELINE_BUTTON_TEST_ID)).toHaveStyle('background-color: #FEC514'); - expect(getByTestId(SAVE_TIMELINE_BUTTON_TEST_ID)).toHaveTextContent('Save timeline'); + expect(getByTestId(SAVE_TIMELINE_BUTTON_TEST_ID)).toHaveTextContent('Save current Timeline'); expect(queryByTestId(ATTACH_TO_TIMELINE_CHECKBOX_TEST_ID)).not.toBeInTheDocument(); expect(getByTestId(ATTACH_TO_TIMELINE_CALLOUT_TEST_ID)).toBeInTheDocument(); expect(getByTestId(ATTACH_TO_TIMELINE_CALLOUT_TEST_ID)).toHaveClass('euiCallOut--warning'); - expect(getByText('Attach to timeline')).toBeInTheDocument(); + expect(getByText('Attach to current Timeline')).toBeInTheDocument(); expect( - getByText('Before attaching a note to the timeline, you need to save the timeline first.') + getByText('You must save the current Timeline before attaching notes to it.') ).toBeInTheDocument(); }); @@ -76,7 +76,7 @@ describe('AttachToActiveTimeline', () => { }, }); - const { getByTestId, getByText, queryByTestId } = render( + const { getByTestId, getByText, getAllByText, queryByTestId } = render( { expect(getByTestId(ATTACH_TO_TIMELINE_CHECKBOX_TEST_ID)).toBeInTheDocument(); expect(getByTestId(ATTACH_TO_TIMELINE_CALLOUT_TEST_ID)).toBeInTheDocument(); expect(getByTestId(ATTACH_TO_TIMELINE_CALLOUT_TEST_ID)).toHaveClass('euiCallOut--primary'); - expect(getByText('Attach to timeline')).toBeInTheDocument(); - expect( - getByText('You can associate the newly created note to the active timeline.') - ).toBeInTheDocument(); + expect(getAllByText('Attach to current Timeline')).toHaveLength(2); + expect(getByText('Also attach this note to the current Timeline.')).toBeInTheDocument(); }); it('should call the callback when user click on the checkbox', () => { diff --git a/x-pack/plugins/security_solution/public/flyout/document_details/left/components/attach_to_active_timeline.tsx b/x-pack/plugins/security_solution/public/flyout/document_details/left/components/attach_to_active_timeline.tsx index 77b3404561275..c8cd360947881 100644 --- a/x-pack/plugins/security_solution/public/flyout/document_details/left/components/attach_to_active_timeline.tsx +++ b/x-pack/plugins/security_solution/public/flyout/document_details/left/components/attach_to_active_timeline.tsx @@ -26,31 +26,31 @@ const timelineCheckBoxId = 'xpack.securitySolution.flyout.notes.attachToTimeline export const ATTACH_TO_TIMELINE_CALLOUT_TITLE = i18n.translate( 'xpack.securitySolution.flyout.left.notes.attachToTimeline.calloutTitle', { - defaultMessage: 'Attach to timeline', + defaultMessage: 'Attach to current Timeline', } ); export const SAVED_TIMELINE_CALLOUT_CONTENT = i18n.translate( 'xpack.securitySolution.flyout.left.notes.attachToTimeline.calloutContent', { - defaultMessage: 'You can associate the newly created note to the active timeline.', + defaultMessage: 'Also attach this note to the current Timeline.', } ); export const UNSAVED_TIMELINE_CALLOUT_CONTENT = i18n.translate( 'xpack.securitySolution.flyout.left.notes.attachToTimeline.calloutContent', { - defaultMessage: 'Before attaching a note to the timeline, you need to save the timeline first.', + defaultMessage: 'You must save the current Timeline before attaching notes to it.', } ); export const ATTACH_TO_TIMELINE_CHECKBOX = i18n.translate( 'xpack.securitySolution.flyout.left.notes.attachToTimeline.checkboxLabel', { - defaultMessage: 'Attach to active timeline', + defaultMessage: 'Attach to current Timeline', } ); export const SAVE_TIMELINE_BUTTON = i18n.translate( - 'xpack.securitySolution.flyout.left.notes.savedTimelineButtonLabel', + 'xpack.securitySolution.flyout.left.notes.attachToTimeline.savedTimelineButtonLabel', { - defaultMessage: 'Save timeline', + defaultMessage: 'Save current Timeline', } ); diff --git a/x-pack/plugins/security_solution/public/flyout/document_details/left/components/notes_details.tsx b/x-pack/plugins/security_solution/public/flyout/document_details/left/components/notes_details.tsx index 5aca7ca389d11..8877e1759c475 100644 --- a/x-pack/plugins/security_solution/public/flyout/document_details/left/components/notes_details.tsx +++ b/x-pack/plugins/security_solution/public/flyout/document_details/left/components/notes_details.tsx @@ -44,7 +44,7 @@ export const FETCH_NOTES_ERROR = i18n.translate( ); export const NO_NOTES = (isAlert: boolean) => i18n.translate('xpack.securitySolution.flyout.left.notes.noNotesLabel', { - defaultMessage: 'No notes have been created for this {value}', + defaultMessage: 'No notes have been created for this {value}.', values: { value: isAlert ? 'alert' : 'event' }, }); diff --git a/x-pack/plugins/security_solution/public/management/links.ts b/x-pack/plugins/security_solution/public/management/links.ts index c83a7360910fa..27b5b62eac6f8 100644 --- a/x-pack/plugins/security_solution/public/management/links.ts +++ b/x-pack/plugins/security_solution/public/management/links.ts @@ -223,7 +223,7 @@ export const links: LinkItem = { title: NOTES, description: i18n.translate('xpack.securitySolution.appLinks.notesDescription', { defaultMessage: - 'Oversee, revise and revisit the annotations within each document and timeline.', + 'Oversee, revise, and revisit the notes attached to alerts, events and Timelines.', }), landingIcon: 'filebeatApp', path: NOTES_PATH, diff --git a/x-pack/plugins/security_solution/public/notes/components/open_flyout_button.tsx b/x-pack/plugins/security_solution/public/notes/components/open_flyout_button.tsx index 34ae9405fdf86..85e9e24c6f26e 100644 --- a/x-pack/plugins/security_solution/public/notes/components/open_flyout_button.tsx +++ b/x-pack/plugins/security_solution/public/notes/components/open_flyout_button.tsx @@ -20,7 +20,7 @@ import { DocumentDetailsRightPanelKey } from '../../flyout/document_details/shar export const OPEN_FLYOUT_BUTTON = i18n.translate( 'xpack.securitySolution.notes.openFlyoutButtonLabel', { - defaultMessage: 'Expand event details', + defaultMessage: 'Expand alert/event details', } ); diff --git a/x-pack/plugins/security_solution/public/notes/components/search_row.tsx b/x-pack/plugins/security_solution/public/notes/components/search_row.tsx index f2f90b3ba7e0d..3c4093f913acf 100644 --- a/x-pack/plugins/security_solution/public/notes/components/search_row.tsx +++ b/x-pack/plugins/security_solution/public/notes/components/search_row.tsx @@ -21,8 +21,8 @@ import { ASSOCIATED_NOT_SELECT_TEST_ID, SEARCH_BAR_TEST_ID } from './test_ids'; import { userFilterAssociatedNotes, userSearchedNotes } from '..'; import { AssociatedFilter } from '../../../common/notes/constants'; -const FILTER_SELECT = i18n.translate('xpack.securitySolution.notes.management.filterSelect', { - defaultMessage: 'Select filter', +const ATTACH_FILTER = i18n.translate('xpack.securitySolution.notes.management.attachFilter', { + defaultMessage: 'Attached to', }); const searchBox = { @@ -31,11 +31,14 @@ const searchBox = { 'data-test-subj': SEARCH_BAR_TEST_ID, }; const associatedNoteSelectOptions: EuiSelectOption[] = [ - { value: AssociatedFilter.all, text: 'All' }, - { value: AssociatedFilter.documentOnly, text: 'Attached to document only' }, - { value: AssociatedFilter.savedObjectOnly, text: 'Attached to timeline only' }, - { value: AssociatedFilter.documentAndSavedObject, text: 'Attached to document and timeline' }, - { value: AssociatedFilter.orphan, text: 'Orphan' }, + { value: AssociatedFilter.all, text: 'Anything or nothing' }, + { value: AssociatedFilter.documentOnly, text: 'Alerts or events only' }, + { value: AssociatedFilter.savedObjectOnly, text: 'Timelines only' }, + { + value: AssociatedFilter.documentAndSavedObject, + text: 'Alerts or events and Timelines only', + }, + { value: AssociatedFilter.orphan, text: 'Nothing' }, ]; export const SearchRow = React.memo(() => { @@ -69,8 +72,8 @@ export const SearchRow = React.memo(() => { id={associatedSelectId} options={associatedNoteSelectOptions} onChange={onAssociatedNoteSelectChange} - prepend={FILTER_SELECT} - aria-label={FILTER_SELECT} + prepend={ATTACH_FILTER} + aria-label={ATTACH_FILTER} data-test-subj={ASSOCIATED_NOT_SELECT_TEST_ID} /> diff --git a/x-pack/plugins/security_solution/public/timelines/components/notes/save_timeline.test.tsx b/x-pack/plugins/security_solution/public/timelines/components/notes/save_timeline.test.tsx index 36a45e993674f..8a024aec79840 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/notes/save_timeline.test.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/notes/save_timeline.test.tsx @@ -42,11 +42,11 @@ describe('SaveTimelineCallout', () => { expect(getByTestId(SAVE_TIMELINE_BUTTON_TEST_ID)).toBeInTheDocument(); expect(getByTestId(SAVE_TIMELINE_BUTTON_TEST_ID)).toHaveStyle('background-color: #BD271E'); - expect(getByTestId(SAVE_TIMELINE_BUTTON_TEST_ID)).toHaveTextContent('Save timeline'); + expect(getByTestId(SAVE_TIMELINE_BUTTON_TEST_ID)).toHaveTextContent('Save Timeline'); expect(getByTestId(SAVE_TIMELINE_CALLOUT_TEST_ID)).toBeInTheDocument(); - expect(getAllByText('Save timeline')).toHaveLength(2); + expect(getAllByText('Save Timeline')).toHaveLength(2); expect( - getByText('You need to save your timeline before creating notes for it.') + getByText('You must save this Timeline before attaching notes to it.') ).toBeInTheDocument(); }); }); diff --git a/x-pack/plugins/security_solution/public/timelines/components/notes/save_timeline.tsx b/x-pack/plugins/security_solution/public/timelines/components/notes/save_timeline.tsx index 0c0a56f2699d3..12efd2db689b7 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/notes/save_timeline.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/notes/save_timeline.tsx @@ -16,19 +16,19 @@ import { SaveTimelineButton } from '../modal/actions/save_timeline_button'; export const SAVE_TIMELINE_CALLOUT_TITLE = i18n.translate( 'xpack.securitySolution.timeline.notes.saveTimeline.calloutTitle', { - defaultMessage: 'Save timeline', + defaultMessage: 'Save Timeline', } ); export const SAVE_TIMELINE_CALLOUT_CONTENT = i18n.translate( 'xpack.securitySolution.timeline.notes.saveTimeline.calloutContent', { - defaultMessage: 'You need to save your timeline before creating notes for it.', + defaultMessage: 'You must save this Timeline before attaching notes to it.', } ); export const SAVE_TIMELINE_BUTTON = i18n.translate( - 'xpack.securitySolution.flyout.left.notes.savedTimelineButtonLabel', + 'xpack.securitySolution.flyout.left.notes.saveTimeline.buttonLabel', { - defaultMessage: 'Save timeline', + defaultMessage: 'Save Timeline', } ); diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/tabs/notes/index.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/tabs/notes/index.tsx index 737001125c990..aad74f15d4f62 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/tabs/notes/index.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/tabs/notes/index.tsx @@ -59,7 +59,7 @@ export const FETCH_NOTES_ERROR = i18n.translate( } ); export const NO_NOTES = i18n.translate('xpack.securitySolution.notes.noNotesLabel', { - defaultMessage: 'No notes have yet been created for this timeline', + defaultMessage: 'No notes have been created for this Timeline.', }); interface NotesTabContentProps { diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/tabs/query/query_tab_unified_components.test.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/tabs/query/query_tab_unified_components.test.tsx index 9d450f46f4b01..107c166183647 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/tabs/query/query_tab_unified_components.test.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/tabs/query/query_tab_unified_components.test.tsx @@ -918,7 +918,7 @@ describe('query tab with unified timeline', () => { await waitFor(() => { expect(screen.getByTestId('timeline-notes-tool-tip')).toBeInTheDocument(); expect(screen.getByTestId('timeline-notes-tool-tip')).toHaveTextContent( - '1 Note available. Click to view it & add more.' + '1 note available. Click to view it and add more.' ); }); }, @@ -975,7 +975,7 @@ describe('query tab with unified timeline', () => { await waitFor(() => { expect(screen.getByTestId('timeline-notes-tool-tip')).toBeVisible(); expect(screen.getByTestId('timeline-notes-tool-tip')).toHaveTextContent( - '1 Note available. Click to view it & add more.' + '1 note available. Click to view it and add more.' ); }); }, diff --git a/x-pack/plugins/security_solution/server/ui_settings.ts b/x-pack/plugins/security_solution/server/ui_settings.ts index 842b8bbeceff8..6aeb86750be7a 100644 --- a/x-pack/plugins/security_solution/server/ui_settings.ts +++ b/x-pack/plugins/security_solution/server/ui_settings.ts @@ -343,6 +343,7 @@ export const initUiSettings = ( max: 1000, defaultValue: DEFAULT_MAX_UNASSOCIATED_NOTES, }), + category: [APP_ID], requiresPageReload: false, }, [EXCLUDED_DATA_TIERS_FOR_RULE_EXECUTION]: {