From 093801cea69263b85a985c80a94e9197b30c31a1 Mon Sep 17 00:00:00 2001 From: Oliver Gupte Date: Wed, 8 Jul 2020 23:38:16 -0700 Subject: [PATCH 1/4] Closes #70440 by adding a setup link to anomaly detection setting in the home header --- .../apm/public/components/app/Home/index.tsx | 4 + .../Links/apm/AnomalyDetectionSetupLink.tsx | 79 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 x-pack/plugins/apm/public/components/shared/Links/apm/AnomalyDetectionSetupLink.tsx diff --git a/x-pack/plugins/apm/public/components/app/Home/index.tsx b/x-pack/plugins/apm/public/components/app/Home/index.tsx index f612ac0d383ef..bcc834fef6a6a 100644 --- a/x-pack/plugins/apm/public/components/app/Home/index.tsx +++ b/x-pack/plugins/apm/public/components/app/Home/index.tsx @@ -20,6 +20,7 @@ import { EuiTabLink } from '../../shared/EuiTabLink'; import { ServiceMapLink } from '../../shared/Links/apm/ServiceMapLink'; import { ServiceOverviewLink } from '../../shared/Links/apm/ServiceOverviewLink'; import { SettingsLink } from '../../shared/Links/apm/SettingsLink'; +import { AnomalyDetectionSetupLink } from '../../shared/Links/apm/AnomalyDetectionSetupLink'; import { TraceOverviewLink } from '../../shared/Links/apm/TraceOverviewLink'; import { SetupInstructionsLink } from '../../shared/Links/SetupInstructionsLink'; import { ServiceMap } from '../ServiceMap'; @@ -118,6 +119,9 @@ export function Home({ tab }: Props) { + + + diff --git a/x-pack/plugins/apm/public/components/shared/Links/apm/AnomalyDetectionSetupLink.tsx b/x-pack/plugins/apm/public/components/shared/Links/apm/AnomalyDetectionSetupLink.tsx new file mode 100644 index 0000000000000..171a7c24d7661 --- /dev/null +++ b/x-pack/plugins/apm/public/components/shared/Links/apm/AnomalyDetectionSetupLink.tsx @@ -0,0 +1,79 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +import React from 'react'; +import { EuiButtonEmpty, EuiToolTip, EuiIcon } from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; +import { APMLink } from './APMLink'; +import { ENVIRONMENT_NOT_DEFINED } from '../../../../../common/environment_filter_values'; +import { useUrlParams } from '../../../../hooks/useUrlParams'; +import { useFetcher, FETCH_STATUS } from '../../../../hooks/useFetcher'; + +export function AnomalyDetectionSetupLink() { + const { uiFilters, urlParams } = useUrlParams(); + // check both uiFilters and urlParams for selected environment + const environment = uiFilters.environment || urlParams.environment; + + const { data = [], status } = useFetcher( + (callApmApi) => + callApmApi({ pathname: `/api/apm/settings/anomaly-detection` }), + [], + { preservePreviousData: false } + ); + const isFetchSuccess = status === FETCH_STATUS.SUCCESS; + + const hasJobs = data.length > 0; + const hasJobForEnv = environment + ? data.some(({ environment: env }) => environment === env) + : true; + + const showAlert = isFetchSuccess && (!hasJobs || !hasJobForEnv); + const toolTipText = environment + ? getNotEnabledForEnvironmentText(environment) + : NOT_ENABLED_TEXT; + + return ( + + + {ANOMALY_DETECTION_LINK_LABEL} + + {showAlert && ( + + + + )} + + ); +} + +function getEnvironmentLabel(environment: string) { + if (environment === ENVIRONMENT_NOT_DEFINED) { + return i18n.translate('xpack.apm.filter.environment.notDefinedLabel', { + defaultMessage: 'Not defined', + }); + } + return environment; +} + +function getNotEnabledForEnvironmentText(environment: string) { + return i18n.translate( + 'xpack.apm.anomalyDetectionSetup.notEnabledForEnvironmentText', + { + defaultMessage: `Anomaly detection is not yet enabled for the "{currentEnvironment}" environment. Click to continue setup.`, + values: { currentEnvironment: getEnvironmentLabel(environment) }, + } + ); +} + +const ANOMALY_DETECTION_LINK_LABEL = i18n.translate( + 'xpack.apm.anomalyDetectionSetup.linkLabel', + { defaultMessage: `Anomaly detection` } +); +const NOT_ENABLED_TEXT = i18n.translate( + 'xpack.apm.anomalyDetectionSetup.notEnabledText', + { + defaultMessage: `Anomaly detection is not yet enabled. Click to continue setup.`, + } +); From b2bc73be44523659442d01271916ed8815e5cb19 Mon Sep 17 00:00:00 2001 From: Oliver Gupte Date: Fri, 10 Jul 2020 00:50:16 -0700 Subject: [PATCH 2/4] PR feedback and type error fix --- .../apm/common/environment_filter_values.ts | 11 ++++++++ .../anomaly_detection/add_environments.tsx | 11 ++------ .../Settings/anomaly_detection/jobs_list.tsx | 11 ++------ .../Links/apm/AnomalyDetectionSetupLink.tsx | 26 +++++-------------- .../create_anomaly_detection_jobs.ts | 17 ++---------- .../get_service_map_service_node_info.ts | 16 +++--------- 6 files changed, 28 insertions(+), 64 deletions(-) diff --git a/x-pack/plugins/apm/common/environment_filter_values.ts b/x-pack/plugins/apm/common/environment_filter_values.ts index 239378d0ea94a..38b6f480ca3d3 100644 --- a/x-pack/plugins/apm/common/environment_filter_values.ts +++ b/x-pack/plugins/apm/common/environment_filter_values.ts @@ -4,5 +4,16 @@ * you may not use this file except in compliance with the Elastic License. */ +import { i18n } from '@kbn/i18n'; + export const ENVIRONMENT_ALL = 'ENVIRONMENT_ALL'; export const ENVIRONMENT_NOT_DEFINED = 'ENVIRONMENT_NOT_DEFINED'; + +export function getEnvironmentLabel(environment: string) { + if (environment === ENVIRONMENT_NOT_DEFINED) { + return i18n.translate('xpack.apm.filter.environment.notDefinedLabel', { + defaultMessage: 'Not defined', + }); + } + return environment; +} diff --git a/x-pack/plugins/apm/public/components/app/Settings/anomaly_detection/add_environments.tsx b/x-pack/plugins/apm/public/components/app/Settings/anomaly_detection/add_environments.tsx index 2da3c12563104..98b4ae2f4b63f 100644 --- a/x-pack/plugins/apm/public/components/app/Settings/anomaly_detection/add_environments.tsx +++ b/x-pack/plugins/apm/public/components/app/Settings/anomaly_detection/add_environments.tsx @@ -22,7 +22,7 @@ import { i18n } from '@kbn/i18n'; import { useFetcher, FETCH_STATUS } from '../../../../hooks/useFetcher'; import { useApmPluginContext } from '../../../../hooks/useApmPluginContext'; import { createJobs } from './create_jobs'; -import { ENVIRONMENT_NOT_DEFINED } from '../../../../../common/environment_filter_values'; +import { getEnvironmentLabel } from '../../../../../common/environment_filter_values'; interface Props { currentEnvironments: string[]; @@ -45,7 +45,7 @@ export const AddEnvironments = ({ ); const environmentOptions = data.map((env) => ({ - label: env === ENVIRONMENT_NOT_DEFINED ? NOT_DEFINED_OPTION_LABEL : env, + label: getEnvironmentLabel(env), value: env, disabled: currentEnvironments.includes(env), })); @@ -155,10 +155,3 @@ export const AddEnvironments = ({ ); }; - -const NOT_DEFINED_OPTION_LABEL = i18n.translate( - 'xpack.apm.filter.environment.notDefinedLabel', - { - defaultMessage: 'Not defined', - } -); diff --git a/x-pack/plugins/apm/public/components/app/Settings/anomaly_detection/jobs_list.tsx b/x-pack/plugins/apm/public/components/app/Settings/anomaly_detection/jobs_list.tsx index 674b4492c2c9c..a76f7c8b7580f 100644 --- a/x-pack/plugins/apm/public/components/app/Settings/anomaly_detection/jobs_list.tsx +++ b/x-pack/plugins/apm/public/components/app/Settings/anomaly_detection/jobs_list.tsx @@ -22,7 +22,7 @@ import { LoadingStatePrompt } from '../../../shared/LoadingStatePrompt'; import { AnomalyDetectionJobByEnv } from '../../../../../typings/anomaly_detection'; import { MLJobLink } from '../../../shared/Links/MachineLearningLinks/MLJobLink'; import { MLLink } from '../../../shared/Links/MachineLearningLinks/MLLink'; -import { ENVIRONMENT_NOT_DEFINED } from '../../../../../common/environment_filter_values'; +import { getEnvironmentLabel } from '../../../../../common/environment_filter_values'; import { LegacyJobsCallout } from './legacy_jobs_callout'; const columns: Array> = [ @@ -32,14 +32,7 @@ const columns: Array> = [ 'xpack.apm.settings.anomalyDetection.jobList.environmentColumnLabel', { defaultMessage: 'Environment' } ), - render: (environment: string) => { - if (environment === ENVIRONMENT_NOT_DEFINED) { - return i18n.translate('xpack.apm.filter.environment.notDefinedLabel', { - defaultMessage: 'Not defined', - }); - } - return environment; - }, + render: getEnvironmentLabel, }, { field: 'job_id', diff --git a/x-pack/plugins/apm/public/components/shared/Links/apm/AnomalyDetectionSetupLink.tsx b/x-pack/plugins/apm/public/components/shared/Links/apm/AnomalyDetectionSetupLink.tsx index 171a7c24d7661..67d4eeff64e70 100644 --- a/x-pack/plugins/apm/public/components/shared/Links/apm/AnomalyDetectionSetupLink.tsx +++ b/x-pack/plugins/apm/public/components/shared/Links/apm/AnomalyDetectionSetupLink.tsx @@ -7,16 +7,16 @@ import React from 'react'; import { EuiButtonEmpty, EuiToolTip, EuiIcon } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import { APMLink } from './APMLink'; -import { ENVIRONMENT_NOT_DEFINED } from '../../../../../common/environment_filter_values'; +import { getEnvironmentLabel } from '../../../../../common/environment_filter_values'; import { useUrlParams } from '../../../../hooks/useUrlParams'; import { useFetcher, FETCH_STATUS } from '../../../../hooks/useFetcher'; export function AnomalyDetectionSetupLink() { - const { uiFilters, urlParams } = useUrlParams(); + const { uiFilters } = useUrlParams(); // check both uiFilters and urlParams for selected environment - const environment = uiFilters.environment || urlParams.environment; + const environment = uiFilters.environment; - const { data = [], status } = useFetcher( + const { data = { jobs: [], hasLegacyJobs: false }, status } = useFetcher( (callApmApi) => callApmApi({ pathname: `/api/apm/settings/anomaly-detection` }), [], @@ -24,12 +24,9 @@ export function AnomalyDetectionSetupLink() { ); const isFetchSuccess = status === FETCH_STATUS.SUCCESS; - const hasJobs = data.length > 0; - const hasJobForEnv = environment - ? data.some(({ environment: env }) => environment === env) - : true; - - const showAlert = isFetchSuccess && (!hasJobs || !hasJobForEnv); + // Show alert if there are no jobs OR if no job matches the current environment + const showAlert = + isFetchSuccess && !data.jobs.some((job) => environment === job.environment); const toolTipText = environment ? getNotEnabledForEnvironmentText(environment) : NOT_ENABLED_TEXT; @@ -48,15 +45,6 @@ export function AnomalyDetectionSetupLink() { ); } -function getEnvironmentLabel(environment: string) { - if (environment === ENVIRONMENT_NOT_DEFINED) { - return i18n.translate('xpack.apm.filter.environment.notDefinedLabel', { - defaultMessage: 'Not defined', - }); - } - return environment; -} - function getNotEnabledForEnvironmentText(environment: string) { return i18n.translate( 'xpack.apm.anomalyDetectionSetup.notEnabledForEnvironmentText', diff --git a/x-pack/plugins/apm/server/lib/anomaly_detection/create_anomaly_detection_jobs.ts b/x-pack/plugins/apm/server/lib/anomaly_detection/create_anomaly_detection_jobs.ts index e723393a24013..6bc3a9cdf4c1a 100644 --- a/x-pack/plugins/apm/server/lib/anomaly_detection/create_anomaly_detection_jobs.ts +++ b/x-pack/plugins/apm/server/lib/anomaly_detection/create_anomaly_detection_jobs.ts @@ -10,12 +10,11 @@ import { snakeCase } from 'lodash'; import { PromiseReturnType } from '../../../../observability/typings/common'; import { Setup } from '../helpers/setup_request'; import { - SERVICE_ENVIRONMENT, TRANSACTION_DURATION, PROCESSOR_EVENT, } from '../../../common/elasticsearch_fieldnames'; -import { ENVIRONMENT_NOT_DEFINED } from '../../../common/environment_filter_values'; import { APM_ML_JOB_GROUP, ML_MODULE_ID_APM_TRANSACTION } from './constants'; +import { getEnvironmentUiFilterES } from '../helpers/convert_ui_filters/get_environment_ui_filter_es'; export type CreateAnomalyDetectionJobsAPIResponse = PromiseReturnType< typeof createAnomalyDetectionJobs @@ -89,9 +88,7 @@ async function createAnomalyDetectionJob({ filter: [ { term: { [PROCESSOR_EVENT]: 'transaction' } }, { exists: { field: TRANSACTION_DURATION } }, - environment === ENVIRONMENT_NOT_DEFINED - ? ENVIRONMENT_NOT_DEFINED_FILTER - : { term: { [SERVICE_ENVIRONMENT]: environment } }, + ...[getEnvironmentUiFilterES(environment)], ], }, }, @@ -109,13 +106,3 @@ async function createAnomalyDetectionJob({ ], }); } - -const ENVIRONMENT_NOT_DEFINED_FILTER = { - bool: { - must_not: { - exists: { - field: SERVICE_ENVIRONMENT, - }, - }, - }, -}; diff --git a/x-pack/plugins/apm/server/lib/service_map/get_service_map_service_node_info.ts b/x-pack/plugins/apm/server/lib/service_map/get_service_map_service_node_info.ts index be92bfe5a0099..95c5448de2b5f 100644 --- a/x-pack/plugins/apm/server/lib/service_map/get_service_map_service_node_info.ts +++ b/x-pack/plugins/apm/server/lib/service_map/get_service_map_service_node_info.ts @@ -9,7 +9,6 @@ import { ESFilter } from '../../../typings/elasticsearch'; import { rangeFilter } from '../../../common/utils/range_filter'; import { PROCESSOR_EVENT, - SERVICE_ENVIRONMENT, SERVICE_NAME, TRANSACTION_DURATION, TRANSACTION_TYPE, @@ -22,7 +21,7 @@ import { TRANSACTION_REQUEST, TRANSACTION_PAGE_LOAD, } from '../../../common/transaction_types'; -import { ENVIRONMENT_NOT_DEFINED } from '../../../common/environment_filter_values'; +import { getEnvironmentUiFilterES } from '../helpers/convert_ui_filters/get_environment_ui_filter_es'; interface Options { setup: Setup & SetupTimeRange; @@ -43,21 +42,14 @@ export async function getServiceMapServiceNodeInfo({ }: Options & { serviceName: string; environment?: string }) { const { start, end } = setup; - const environmentNotDefinedFilter = { - bool: { must_not: [{ exists: { field: SERVICE_ENVIRONMENT } }] }, - }; - const filter: ESFilter[] = [ { range: rangeFilter(start, end) }, { term: { [SERVICE_NAME]: serviceName } }, ]; - if (environment) { - filter.push( - environment === ENVIRONMENT_NOT_DEFINED - ? environmentNotDefinedFilter - : { term: { [SERVICE_ENVIRONMENT]: environment } } - ); + const environmentFilter = getEnvironmentUiFilterES(environment); + if (environmentFilter) { + filter.push(environmentFilter); } const minutes = Math.abs((end - start) / (1000 * 60)); From f150299f7bcca7a292b662805028fc955ce85418 Mon Sep 17 00:00:00 2001 From: Oliver Gupte Date: Fri, 10 Jul 2020 22:58:02 -0700 Subject: [PATCH 3/4] Code cleanup and PR feedback --- .../Links/apm/AnomalyDetectionSetupLink.tsx | 20 ++--- .../get_service_map_service_node_info.ts | 77 +++---------------- 2 files changed, 20 insertions(+), 77 deletions(-) diff --git a/x-pack/plugins/apm/public/components/shared/Links/apm/AnomalyDetectionSetupLink.tsx b/x-pack/plugins/apm/public/components/shared/Links/apm/AnomalyDetectionSetupLink.tsx index 67d4eeff64e70..88d15239b8fba 100644 --- a/x-pack/plugins/apm/public/components/shared/Links/apm/AnomalyDetectionSetupLink.tsx +++ b/x-pack/plugins/apm/public/components/shared/Links/apm/AnomalyDetectionSetupLink.tsx @@ -13,7 +13,6 @@ import { useFetcher, FETCH_STATUS } from '../../../../hooks/useFetcher'; export function AnomalyDetectionSetupLink() { const { uiFilters } = useUrlParams(); - // check both uiFilters and urlParams for selected environment const environment = uiFilters.environment; const { data = { jobs: [], hasLegacyJobs: false }, status } = useFetcher( @@ -27,9 +26,6 @@ export function AnomalyDetectionSetupLink() { // Show alert if there are no jobs OR if no job matches the current environment const showAlert = isFetchSuccess && !data.jobs.some((job) => environment === job.environment); - const toolTipText = environment - ? getNotEnabledForEnvironmentText(environment) - : NOT_ENABLED_TEXT; return ( @@ -37,7 +33,7 @@ export function AnomalyDetectionSetupLink() { {ANOMALY_DETECTION_LINK_LABEL} {showAlert && ( - + )} @@ -45,7 +41,13 @@ export function AnomalyDetectionSetupLink() { ); } -function getNotEnabledForEnvironmentText(environment: string) { +function getTooltipText(environment?: string) { + if (!environment) { + return i18n.translate('xpack.apm.anomalyDetectionSetup.notEnabledText', { + defaultMessage: `Anomaly detection is not yet enabled. Click to continue setup.`, + }); + } + return i18n.translate( 'xpack.apm.anomalyDetectionSetup.notEnabledForEnvironmentText', { @@ -59,9 +61,3 @@ const ANOMALY_DETECTION_LINK_LABEL = i18n.translate( 'xpack.apm.anomalyDetectionSetup.linkLabel', { defaultMessage: `Anomaly detection` } ); -const NOT_ENABLED_TEXT = i18n.translate( - 'xpack.apm.anomalyDetectionSetup.notEnabledText', - { - defaultMessage: `Anomaly detection is not yet enabled. Click to continue setup.`, - } -); diff --git a/x-pack/plugins/apm/server/lib/service_map/get_service_map_service_node_info.ts b/x-pack/plugins/apm/server/lib/service_map/get_service_map_service_node_info.ts index 95c5448de2b5f..85bfc08b46ca5 100644 --- a/x-pack/plugins/apm/server/lib/service_map/get_service_map_service_node_info.ts +++ b/x-pack/plugins/apm/server/lib/service_map/get_service_map_service_node_info.ts @@ -53,12 +53,7 @@ export async function getServiceMapServiceNodeInfo({ } const minutes = Math.abs((end - start) / (1000 * 60)); - - const taskParams = { - setup, - minutes, - filter, - }; + const taskParams = { setup, minutes, filter }; const [ errorMetrics, @@ -89,11 +84,7 @@ async function getErrorMetrics({ setup, minutes, filter }: TaskParameters) { size: 0, query: { bool: { - filter: filter.concat({ - term: { - [PROCESSOR_EVENT]: 'error', - }, - }), + filter: filter.concat({ term: { [PROCESSOR_EVENT]: 'error' } }), }, }, track_total_hits: true, @@ -126,11 +117,7 @@ async function getTransactionStats({ bool: { filter: [ ...filter, - { - term: { - [PROCESSOR_EVENT]: 'transaction', - }, - }, + { term: { [PROCESSOR_EVENT]: 'transaction' } }, { terms: { [TRANSACTION_TYPE]: [ @@ -143,13 +130,7 @@ async function getTransactionStats({ }, }, track_total_hits: true, - aggs: { - duration: { - avg: { - field: TRANSACTION_DURATION, - }, - }, - }, + aggs: { duration: { avg: { field: TRANSACTION_DURATION } } }, }, }; const response = await client.search(params); @@ -173,32 +154,16 @@ async function getCpuMetrics({ query: { bool: { filter: filter.concat([ - { - term: { - [PROCESSOR_EVENT]: 'metric', - }, - }, - { - exists: { - field: METRIC_SYSTEM_CPU_PERCENT, - }, - }, + { term: { [PROCESSOR_EVENT]: 'metric' } }, + { exists: { field: METRIC_SYSTEM_CPU_PERCENT } }, ]), }, }, - aggs: { - avgCpuUsage: { - avg: { - field: METRIC_SYSTEM_CPU_PERCENT, - }, - }, - }, + aggs: { avgCpuUsage: { avg: { field: METRIC_SYSTEM_CPU_PERCENT } } }, }, }); - return { - avgCpuUsage: response.aggregations?.avgCpuUsage.value ?? null, - }; + return { avgCpuUsage: response.aggregations?.avgCpuUsage.value ?? null }; } async function getMemoryMetrics({ @@ -212,31 +177,13 @@ async function getMemoryMetrics({ query: { bool: { filter: filter.concat([ - { - term: { - [PROCESSOR_EVENT]: 'metric', - }, - }, - { - exists: { - field: METRIC_SYSTEM_FREE_MEMORY, - }, - }, - { - exists: { - field: METRIC_SYSTEM_TOTAL_MEMORY, - }, - }, + { term: { [PROCESSOR_EVENT]: 'metric' } }, + { exists: { field: METRIC_SYSTEM_FREE_MEMORY } }, + { exists: { field: METRIC_SYSTEM_TOTAL_MEMORY } }, ]), }, }, - aggs: { - avgMemoryUsage: { - avg: { - script: percentMemoryUsedScript, - }, - }, - }, + aggs: { avgMemoryUsage: { avg: { script: percentMemoryUsedScript } } }, }, }); From 4bf17eefcead1809e6ce987ed10be83b3eb6d169 Mon Sep 17 00:00:00 2001 From: Oliver Gupte Date: Fri, 10 Jul 2020 23:18:36 -0700 Subject: [PATCH 4/4] Modified getEnvironmentUiFilterES return type from `ESFilter | undefined` to `ESFilter[]` for ease of use. --- .../create_anomaly_detection_jobs.ts | 2 +- .../get_environment_ui_filter_es.test.ts | 17 ++++++++--------- .../get_environment_ui_filter_es.ts | 15 ++++----------- .../convert_ui_filters/get_ui_filters_es.ts | 13 +++++-------- .../get_service_map_service_node_info.ts | 6 +----- .../get_derived_service_annotations.ts | 7 +------ .../annotations/get_stored_annotations.ts | 4 +--- 7 files changed, 21 insertions(+), 43 deletions(-) diff --git a/x-pack/plugins/apm/server/lib/anomaly_detection/create_anomaly_detection_jobs.ts b/x-pack/plugins/apm/server/lib/anomaly_detection/create_anomaly_detection_jobs.ts index 6bc3a9cdf4c1a..c387c5152b1c5 100644 --- a/x-pack/plugins/apm/server/lib/anomaly_detection/create_anomaly_detection_jobs.ts +++ b/x-pack/plugins/apm/server/lib/anomaly_detection/create_anomaly_detection_jobs.ts @@ -88,7 +88,7 @@ async function createAnomalyDetectionJob({ filter: [ { term: { [PROCESSOR_EVENT]: 'transaction' } }, { exists: { field: TRANSACTION_DURATION } }, - ...[getEnvironmentUiFilterES(environment)], + ...getEnvironmentUiFilterES(environment), ], }, }, diff --git a/x-pack/plugins/apm/server/lib/helpers/convert_ui_filters/__test__/get_environment_ui_filter_es.test.ts b/x-pack/plugins/apm/server/lib/helpers/convert_ui_filters/__test__/get_environment_ui_filter_es.test.ts index 0f0a11a868d6d..800f809727eb6 100644 --- a/x-pack/plugins/apm/server/lib/helpers/convert_ui_filters/__test__/get_environment_ui_filter_es.test.ts +++ b/x-pack/plugins/apm/server/lib/helpers/convert_ui_filters/__test__/get_environment_ui_filter_es.test.ts @@ -7,24 +7,23 @@ import { getEnvironmentUiFilterES } from '../get_environment_ui_filter_es'; import { ENVIRONMENT_NOT_DEFINED } from '../../../../../common/environment_filter_values'; import { SERVICE_ENVIRONMENT } from '../../../../../common/elasticsearch_fieldnames'; -import { ESFilter } from '../../../../../typings/elasticsearch'; describe('getEnvironmentUiFilterES', () => { - it('should return undefined, when environment is undefined', () => { + it('should return empty array, when environment is undefined', () => { const uiFilterES = getEnvironmentUiFilterES(); - expect(uiFilterES).toBeUndefined(); + expect(uiFilterES).toHaveLength(0); }); it('should create a filter for a service environment', () => { - const uiFilterES = getEnvironmentUiFilterES('test') as ESFilter; - expect(uiFilterES).toHaveProperty(['term', SERVICE_ENVIRONMENT], 'test'); + const uiFilterES = getEnvironmentUiFilterES('test'); + expect(uiFilterES).toHaveLength(1); + expect(uiFilterES[0]).toHaveProperty(['term', SERVICE_ENVIRONMENT], 'test'); }); it('should create a filter for missing service environments', () => { - const uiFilterES = getEnvironmentUiFilterES( - ENVIRONMENT_NOT_DEFINED - ) as ESFilter; - expect(uiFilterES).toHaveProperty( + const uiFilterES = getEnvironmentUiFilterES(ENVIRONMENT_NOT_DEFINED); + expect(uiFilterES).toHaveLength(1); + expect(uiFilterES[0]).toHaveProperty( ['bool', 'must_not', 'exists', 'field'], SERVICE_ENVIRONMENT ); diff --git a/x-pack/plugins/apm/server/lib/helpers/convert_ui_filters/get_environment_ui_filter_es.ts b/x-pack/plugins/apm/server/lib/helpers/convert_ui_filters/get_environment_ui_filter_es.ts index 63d222a7fcb6e..87bc8dc968373 100644 --- a/x-pack/plugins/apm/server/lib/helpers/convert_ui_filters/get_environment_ui_filter_es.ts +++ b/x-pack/plugins/apm/server/lib/helpers/convert_ui_filters/get_environment_ui_filter_es.ts @@ -8,19 +8,12 @@ import { ESFilter } from '../../../../typings/elasticsearch'; import { ENVIRONMENT_NOT_DEFINED } from '../../../../common/environment_filter_values'; import { SERVICE_ENVIRONMENT } from '../../../../common/elasticsearch_fieldnames'; -export function getEnvironmentUiFilterES( - environment?: string -): ESFilter | undefined { +export function getEnvironmentUiFilterES(environment?: string): ESFilter[] { if (!environment) { - return undefined; + return []; } - if (environment === ENVIRONMENT_NOT_DEFINED) { - return { - bool: { must_not: { exists: { field: SERVICE_ENVIRONMENT } } }, - }; + return [{ bool: { must_not: { exists: { field: SERVICE_ENVIRONMENT } } } }]; } - return { - term: { [SERVICE_ENVIRONMENT]: environment }, - }; + return [{ term: { [SERVICE_ENVIRONMENT]: environment } }]; } diff --git a/x-pack/plugins/apm/server/lib/helpers/convert_ui_filters/get_ui_filters_es.ts b/x-pack/plugins/apm/server/lib/helpers/convert_ui_filters/get_ui_filters_es.ts index b34d5535d58cc..c1405b44f2a8a 100644 --- a/x-pack/plugins/apm/server/lib/helpers/convert_ui_filters/get_ui_filters_es.ts +++ b/x-pack/plugins/apm/server/lib/helpers/convert_ui_filters/get_ui_filters_es.ts @@ -27,22 +27,19 @@ export function getUiFiltersES(uiFilters: UIFilters) { }; }) as ESFilter[]; - // remove undefined items from list const esFilters = [ - getKueryUiFilterES(uiFilters.kuery), - getEnvironmentUiFilterES(uiFilters.environment), - ] - .filter((filter) => !!filter) - .concat(mappedFilters) as ESFilter[]; + ...getKueryUiFilterES(uiFilters.kuery), + ...getEnvironmentUiFilterES(uiFilters.environment), + ].concat(mappedFilters) as ESFilter[]; return esFilters; } function getKueryUiFilterES(kuery?: string) { if (!kuery) { - return; + return []; } const ast = esKuery.fromKueryExpression(kuery); - return esKuery.toElasticsearchQuery(ast) as ESFilter; + return [esKuery.toElasticsearchQuery(ast) as ESFilter]; } diff --git a/x-pack/plugins/apm/server/lib/service_map/get_service_map_service_node_info.ts b/x-pack/plugins/apm/server/lib/service_map/get_service_map_service_node_info.ts index 85bfc08b46ca5..dd5d19b620c51 100644 --- a/x-pack/plugins/apm/server/lib/service_map/get_service_map_service_node_info.ts +++ b/x-pack/plugins/apm/server/lib/service_map/get_service_map_service_node_info.ts @@ -45,13 +45,9 @@ export async function getServiceMapServiceNodeInfo({ const filter: ESFilter[] = [ { range: rangeFilter(start, end) }, { term: { [SERVICE_NAME]: serviceName } }, + ...getEnvironmentUiFilterES(environment), ]; - const environmentFilter = getEnvironmentUiFilterES(environment); - if (environmentFilter) { - filter.push(environmentFilter); - } - const minutes = Math.abs((end - start) / (1000 * 60)); const taskParams = { setup, minutes, filter }; diff --git a/x-pack/plugins/apm/server/lib/services/annotations/get_derived_service_annotations.ts b/x-pack/plugins/apm/server/lib/services/annotations/get_derived_service_annotations.ts index 6da5d195cf194..6a8aaf8dca8a6 100644 --- a/x-pack/plugins/apm/server/lib/services/annotations/get_derived_service_annotations.ts +++ b/x-pack/plugins/apm/server/lib/services/annotations/get_derived_service_annotations.ts @@ -29,14 +29,9 @@ export async function getDerivedServiceAnnotations({ const filter: ESFilter[] = [ { term: { [PROCESSOR_EVENT]: 'transaction' } }, { term: { [SERVICE_NAME]: serviceName } }, + ...getEnvironmentUiFilterES(environment), ]; - const environmentFilter = getEnvironmentUiFilterES(environment); - - if (environmentFilter) { - filter.push(environmentFilter); - } - const versions = ( await client.search({ diff --git a/x-pack/plugins/apm/server/lib/services/annotations/get_stored_annotations.ts b/x-pack/plugins/apm/server/lib/services/annotations/get_stored_annotations.ts index 75aeb27ea2122..6e3ae0181ddee 100644 --- a/x-pack/plugins/apm/server/lib/services/annotations/get_stored_annotations.ts +++ b/x-pack/plugins/apm/server/lib/services/annotations/get_stored_annotations.ts @@ -29,8 +29,6 @@ export async function getStoredAnnotations({ logger: Logger; }): Promise { try { - const environmentFilter = getEnvironmentUiFilterES(environment); - const response: ESSearchResponse = (await apiCaller( 'search', { @@ -51,7 +49,7 @@ export async function getStoredAnnotations({ { term: { 'annotation.type': 'deployment' } }, { term: { tags: 'apm' } }, { term: { [SERVICE_NAME]: serviceName } }, - ...(environmentFilter ? [environmentFilter] : []), + ...getEnvironmentUiFilterES(environment), ], }, },