From b1b3211e9fc8daf7e45b58e319ea3f70e4603bde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Louv-Jansen?= Date: Sun, 28 Jun 2020 13:44:15 +0200 Subject: [PATCH] Example of straighening projections out --- .../apm/common/projections/services.ts | 22 +++++----- .../server/lib/service_map/get_service_map.ts | 19 ++++----- .../get_services/get_services_items.ts | 42 ++++++++++++------- 3 files changed, 45 insertions(+), 38 deletions(-) diff --git a/x-pack/plugins/apm/common/projections/services.ts b/x-pack/plugins/apm/common/projections/services.ts index 80a3471e9c30d..96f04f3494e20 100644 --- a/x-pack/plugins/apm/common/projections/services.ts +++ b/x-pack/plugins/apm/common/projections/services.ts @@ -4,23 +4,23 @@ * you may not use this file except in compliance with the Elastic License. */ -import { - Setup, - SetupUIFilters, - SetupTimeRange, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../server/lib/helpers/setup_request'; import { SERVICE_NAME, PROCESSOR_EVENT } from '../elasticsearch_fieldnames'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths import { rangeFilter } from '../utils/range_filter'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths +import { ApmIndicesConfig } from '../../server/lib/settings/apm_indices/get_apm_indices'; +import { ESFilter } from '../../typings/elasticsearch'; export function getServicesProjection({ - setup, + start, + end, + uiFiltersES, + indices, }: { - setup: Setup & SetupTimeRange & SetupUIFilters; + start: number; + end: number; + uiFiltersES: ESFilter[]; + indices: ApmIndicesConfig; }) { - const { start, end, uiFiltersES, indices } = setup; - return { index: [ indices['apm_oss.metricsIndices'], diff --git a/x-pack/plugins/apm/server/lib/service_map/get_service_map.ts b/x-pack/plugins/apm/server/lib/service_map/get_service_map.ts index 4d488cd1a5509..4a2c491fd83ef 100644 --- a/x-pack/plugins/apm/server/lib/service_map/get_service_map.ts +++ b/x-pack/plugins/apm/server/lib/service_map/get_service_map.ts @@ -10,7 +10,6 @@ import { SERVICE_NAME, } from '../../../common/elasticsearch_fieldnames'; import { getServicesProjection } from '../../../common/projections/services'; -import { mergeProjection } from '../../../common/projections/util/merge_projection'; import { PromiseReturnType } from '../../../typings/common'; import { Setup, SetupTimeRange } from '../helpers/setup_request'; import { transformServiceMapResponses } from './transform_service_map_responses'; @@ -76,21 +75,17 @@ async function getServicesData(options: IEnvOptions) { setup: { ...setup, uiFiltersES: [] }, }); - const { filter } = projection.body.query.bool; + const serviceNameFilter = options.serviceName + ? [{ term: { [SERVICE_NAME]: options.serviceName } }] + : []; - const params = mergeProjection(projection, { + const params = { body: { + index: projection.index, size: 0, query: { bool: { - ...projection.body.query.bool, - filter: options.serviceName - ? filter.concat({ - term: { - [SERVICE_NAME]: options.serviceName, - }, - }) - : filter, + filter: [...projection.body.query.bool.filter, ...serviceNameFilter], }, }, aggs: { @@ -109,7 +104,7 @@ async function getServicesData(options: IEnvOptions) { }, }, }, - }); + }; const { client } = setup; diff --git a/x-pack/plugins/apm/server/lib/services/get_services/get_services_items.ts b/x-pack/plugins/apm/server/lib/services/get_services/get_services_items.ts index acf052affabdb..ea338417713f6 100644 --- a/x-pack/plugins/apm/server/lib/services/get_services/get_services_items.ts +++ b/x-pack/plugins/apm/server/lib/services/get_services/get_services_items.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { mergeProjection } from '../../../../common/projections/util/merge_projection'; +import { ESFilter } from '../../../../typings/elasticsearch'; import { PROCESSOR_EVENT, AGENT_NAME, @@ -12,28 +12,40 @@ import { TRANSACTION_DURATION, } from '../../../../common/elasticsearch_fieldnames'; import { PromiseReturnType } from '../../../../typings/common'; -import { - Setup, - SetupTimeRange, - SetupUIFilters, -} from '../../helpers/setup_request'; import { getServicesProjection } from '../../../../common/projections/services'; +import { ApmIndicesConfig } from '../../settings/apm_indices/get_apm_indices'; +import { ESClient } from '../../helpers/es_client'; export type ServiceListAPIResponse = PromiseReturnType; -export async function getServicesItems( - setup: Setup & SetupTimeRange & SetupUIFilters -) { - const { start, end, client } = setup; - - const projection = getServicesProjection({ setup }); +export async function getServicesItems({ + start, + end, + uiFiltersES, + indices, + client, +}: { + start: number; + end: number; + uiFiltersES: ESFilter[]; + indices: ApmIndicesConfig; + client: ESClient; +}) { + const projection = getServicesProjection({ + start, + end, + uiFiltersES, + indices, + }); - const params = mergeProjection(projection, { + const params = { body: { + index: projection.index, size: 0, + query: projection.body.query, aggs: { services: { terms: { - ...projection.body.aggs.services.terms, + field: projection.body.aggs.services.terms.field, size: 500, }, aggs: { @@ -53,7 +65,7 @@ export async function getServicesItems( }, }, }, - }); + }; const resp = await client.search(params); const aggs = resp.aggregations;