From ca86f769d73920a811c375004e699c027a11c141 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Tue, 24 Aug 2021 17:21:36 +0200 Subject: [PATCH 01/37] [ML] Fix tooltip text. --- .../app/correlations/failed_transactions_correlations.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx index 307dfc556672f..1e75bd2273848 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx @@ -146,7 +146,7 @@ export function FailedTransactionsCorrelations({ 'xpack.apm.correlations.failedTransactions.correlationsTable.failurePercentageDescription', { defaultMessage: - 'Percentage of time the term appear in failed transactions.', + 'Percentage of time the term appears in failed transactions.', } )} > @@ -179,7 +179,7 @@ export function FailedTransactionsCorrelations({ 'xpack.apm.correlations.failedTransactions.correlationsTable.successPercentageDescription', { defaultMessage: - 'Percentage of time the term appear in successful transactions.', + 'Percentage of time the term appears in successful transactions.', } )} > From b17a0b901c12b77a9fd85ced7cf3ec3d227aefc9 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Wed, 25 Aug 2021 15:33:14 +0200 Subject: [PATCH 02/37] Revert "[ML] Fix tooltip text." This reverts commit ca86f769d73920a811c375004e699c027a11c141. --- .../app/correlations/failed_transactions_correlations.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx index 1e75bd2273848..307dfc556672f 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx @@ -146,7 +146,7 @@ export function FailedTransactionsCorrelations({ 'xpack.apm.correlations.failedTransactions.correlationsTable.failurePercentageDescription', { defaultMessage: - 'Percentage of time the term appears in failed transactions.', + 'Percentage of time the term appear in failed transactions.', } )} > @@ -179,7 +179,7 @@ export function FailedTransactionsCorrelations({ 'xpack.apm.correlations.failedTransactions.correlationsTable.successPercentageDescription', { defaultMessage: - 'Percentage of time the term appears in successful transactions.', + 'Percentage of time the term appear in successful transactions.', } )} > From 7f39a6bf7d40bcc69260f7fab44ecd9b5438329c Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Mon, 30 Aug 2021 15:03:13 +0200 Subject: [PATCH 03/37] [ML] Code consolidation. --- .../apm/common/search_strategies/constants.ts | 11 ++ .../failure_correlations/constants.ts | 3 - ...ailed_transactions_correlations_fetcher.ts | 4 +- .../use_transaction_distribution_fetcher.ts | 3 +- ...ransaction_latency_correlations_fetcher.ts | 3 +- .../{correlations => }/constants.ts | 0 .../utils/current_time_as_string.test.ts | 24 ---- .../async_search_service.ts | 39 ++++-- .../failed_transactions_correlations/index.ts | 3 +- .../search_strategy.ts | 120 ------------------ .../{correlations => }/index.ts | 2 +- .../async_search_service.ts | 31 +++-- .../async_search_service_log.test.ts | 19 ++- .../async_search_service_log.ts | 4 +- .../async_search_service_state.test.ts | 0 .../async_search_service_state.ts | 2 +- .../index.ts} | 2 +- .../get_prioritized_field_value_pairs.test.ts | 0 .../get_prioritized_field_value_pairs.ts | 0 .../queries/get_query_with_params.test.ts | 2 +- .../queries/get_query_with_params.ts | 8 +- .../queries/get_request_base.test.ts | 2 +- .../queries/get_request_base.ts | 2 +- .../{correlations => }/queries/index.ts | 1 + .../queries/query_correlation.test.ts | 2 +- .../queries/query_correlation.ts | 4 +- .../queries/query_failure_correlation.ts | 13 +- .../queries/query_field_candidates.test.ts | 4 +- .../queries/query_field_candidates.ts | 2 +- .../queries/query_field_value_pairs.test.ts | 6 +- .../queries/query_field_value_pairs.ts | 6 +- .../queries/query_fractions.test.ts | 2 +- .../queries/query_fractions.ts | 4 +- .../queries/query_histogram.test.ts | 2 +- .../queries/query_histogram.ts | 4 +- .../queries/query_histogram_interval.test.ts | 2 +- .../queries/query_histogram_interval.ts | 4 +- .../query_histogram_range_steps.test.ts | 2 +- .../queries/query_histogram_range_steps.ts | 4 +- .../query_histograms_generator.test.ts | 6 +- .../queries/query_histograms_generator.ts | 6 +- .../queries/query_percentiles.test.ts | 2 +- .../queries/query_percentiles.ts | 4 +- .../queries/query_ranges.test.ts | 2 +- .../queries/query_ranges.ts | 4 +- .../register_search_strategies.ts | 41 ++++++ ...st.ts => search_strategy_provider.test.ts} | 31 +++-- ...trategy.ts => search_strategy_provider.ts} | 91 +++++++------ .../compute_expectations_and_ranges.test.ts | 0 .../utils/compute_expectations_and_ranges.ts | 0 .../utils/has_prefix_to_include.test.ts | 0 .../utils/has_prefix_to_include.ts | 0 .../{correlations => }/utils/index.ts | 1 - x-pack/plugins/apm/server/plugin.ts | 26 +--- .../tests/correlations/failed_transactions.ts | 2 +- .../tests/correlations/latency.ts | 2 +- 56 files changed, 251 insertions(+), 313 deletions(-) create mode 100644 x-pack/plugins/apm/common/search_strategies/constants.ts rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/constants.ts (100%) delete mode 100644 x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/current_time_as_string.test.ts delete mode 100644 x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/search_strategy.ts rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/index.ts (77%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => latency_correlations}/async_search_service.ts (95%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => latency_correlations}/async_search_service_log.test.ts (64%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => latency_correlations}/async_search_service_log.ts (92%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => latency_correlations}/async_search_service_state.test.ts (100%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => latency_correlations}/async_search_service_state.ts (98%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations/utils/current_time_as_string.ts => latency_correlations/index.ts} (78%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/get_prioritized_field_value_pairs.test.ts (100%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/get_prioritized_field_value_pairs.ts (100%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/get_query_with_params.test.ts (97%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/get_query_with_params.ts (83%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/get_request_base.test.ts (92%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/get_request_base.ts (82%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/index.ts (91%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_correlation.test.ts (97%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_correlation.ts (94%) rename x-pack/plugins/apm/server/lib/search_strategies/{failed_transactions_correlations => }/queries/query_failure_correlation.ts (87%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_field_candidates.test.ts (96%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_field_candidates.ts (96%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_field_value_pairs.test.ts (90%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_field_value_pairs.ts (91%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_fractions.test.ts (96%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_fractions.ts (90%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_histogram.test.ts (97%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_histogram.ts (91%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_histogram_interval.test.ts (97%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_histogram_interval.ts (89%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_histogram_range_steps.test.ts (97%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_histogram_range_steps.ts (91%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_histograms_generator.test.ts (93%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_histograms_generator.ts (90%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_percentiles.test.ts (97%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_percentiles.ts (92%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_ranges.test.ts (97%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/queries/query_ranges.ts (92%) create mode 100644 x-pack/plugins/apm/server/lib/search_strategies/register_search_strategies.ts rename x-pack/plugins/apm/server/lib/search_strategies/{correlations/search_strategy.test.ts => search_strategy_provider.test.ts} (89%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations/search_strategy.ts => search_strategy_provider.ts} (62%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/utils/compute_expectations_and_ranges.test.ts (100%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/utils/compute_expectations_and_ranges.ts (100%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/utils/has_prefix_to_include.test.ts (100%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/utils/has_prefix_to_include.ts (100%) rename x-pack/plugins/apm/server/lib/search_strategies/{correlations => }/utils/index.ts (86%) diff --git a/x-pack/plugins/apm/common/search_strategies/constants.ts b/x-pack/plugins/apm/common/search_strategies/constants.ts new file mode 100644 index 0000000000000..57f78a9ee6564 --- /dev/null +++ b/x-pack/plugins/apm/common/search_strategies/constants.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; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export const APM_SEARCH_STRATEGIES = { + APM_FAILED_TRANSACTIONS_CORRELATIONS: 'apmFailedTransactionsCorrelations', + APM_LATENCY_CORRELATIONS: 'apmLatencyCorrelations', +} as const; diff --git a/x-pack/plugins/apm/common/search_strategies/failure_correlations/constants.ts b/x-pack/plugins/apm/common/search_strategies/failure_correlations/constants.ts index a80918f0e399e..09e3e22a1d352 100644 --- a/x-pack/plugins/apm/common/search_strategies/failure_correlations/constants.ts +++ b/x-pack/plugins/apm/common/search_strategies/failure_correlations/constants.ts @@ -7,9 +7,6 @@ import { i18n } from '@kbn/i18n'; -export const FAILED_TRANSACTIONS_CORRELATION_SEARCH_STRATEGY = - 'apmFailedTransactionsCorrelationsSearchStrategy'; - export const FAILED_TRANSACTIONS_IMPACT_THRESHOLD = { HIGH: i18n.translate( 'xpack.apm.correlations.failedTransactions.highImpactText', diff --git a/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts b/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts index f12cee7ee0332..38406416d4d96 100644 --- a/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts +++ b/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts @@ -17,7 +17,7 @@ import type { SearchServiceParams } from '../../common/search_strategies/correla import { useKibana } from '../../../../../src/plugins/kibana_react/public'; import { ApmPluginStartDeps } from '../plugin'; import { FailedTransactionsCorrelationValue } from '../../common/search_strategies/failure_correlations/types'; -import { FAILED_TRANSACTIONS_CORRELATION_SEARCH_STRATEGY } from '../../common/search_strategies/failure_correlations/constants'; +import { APM_SEARCH_STRATEGIES } from '../../common/search_strategies/constants'; interface RawResponse { took: number; @@ -88,7 +88,7 @@ export const useFailedTransactionsCorrelationsFetcher = () => { // Submit the search request using the `data.search` service. searchSubscription$.current = data.search .search>(req, { - strategy: FAILED_TRANSACTIONS_CORRELATION_SEARCH_STRATEGY, + strategy: APM_SEARCH_STRATEGIES.APM_FAILED_TRANSACTIONS_CORRELATIONS, abortSignal: abortCtrl.current.signal, }) .subscribe({ diff --git a/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts b/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts index 2ff1b83ef1782..72e1baa126df7 100644 --- a/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts +++ b/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts @@ -18,6 +18,7 @@ import type { SearchServiceRawResponse, } from '../../common/search_strategies/correlations/types'; import { useKibana } from '../../../../../src/plugins/kibana_react/public'; +import { APM_SEARCH_STRATEGIES } from '../../common/search_strategies/constants'; import { ApmPluginStartDeps } from '../plugin'; interface TransactionDistributionFetcherState { @@ -107,7 +108,7 @@ export function useTransactionDistributionFetcher() { IKibanaSearchRequest, IKibanaSearchResponse >(req, { - strategy: 'apmCorrelationsSearchStrategy', + strategy: APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, abortSignal: abortCtrl.current.signal, }) .subscribe({ diff --git a/x-pack/plugins/apm/public/hooks/use_transaction_latency_correlations_fetcher.ts b/x-pack/plugins/apm/public/hooks/use_transaction_latency_correlations_fetcher.ts index 0b035c6af2354..ad5a78173c7e1 100644 --- a/x-pack/plugins/apm/public/hooks/use_transaction_latency_correlations_fetcher.ts +++ b/x-pack/plugins/apm/public/hooks/use_transaction_latency_correlations_fetcher.ts @@ -18,6 +18,7 @@ import type { SearchServiceRawResponse, } from '../../common/search_strategies/correlations/types'; import { useKibana } from '../../../../../src/plugins/kibana_react/public'; +import { APM_SEARCH_STRATEGIES } from '../../common/search_strategies/constants'; import { ApmPluginStartDeps } from '../plugin'; interface TransactionLatencyCorrelationsFetcherState { @@ -109,7 +110,7 @@ export const useTransactionLatencyCorrelationsFetcher = () => { IKibanaSearchRequest, IKibanaSearchResponse >(req, { - strategy: 'apmCorrelationsSearchStrategy', + strategy: APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, abortSignal: abortCtrl.current.signal, }) .subscribe({ diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/constants.ts b/x-pack/plugins/apm/server/lib/search_strategies/constants.ts similarity index 100% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/constants.ts rename to x-pack/plugins/apm/server/lib/search_strategies/constants.ts diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/current_time_as_string.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/current_time_as_string.test.ts deleted file mode 100644 index 2034c29b01d94..0000000000000 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/current_time_as_string.test.ts +++ /dev/null @@ -1,24 +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; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { currentTimeAsString } from './current_time_as_string'; - -describe('aggregation utils', () => { - describe('currentTimeAsString', () => { - it('returns the current time as a string', () => { - const mockDate = new Date(1392202800000); - // @ts-ignore ignore the mockImplementation callback error - const spy = jest.spyOn(global, 'Date').mockImplementation(() => mockDate); - - const timeString = currentTimeAsString(); - - expect(timeString).toEqual('2014-02-12T11:00:00.000Z'); - - spy.mockRestore(); - }); - }); -}); diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts index 89fcda926d547..e07db04665ef0 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts @@ -6,16 +6,22 @@ */ import type { ElasticsearchClient } from 'src/core/server'; + import { chunk } from 'lodash'; -import type { SearchServiceParams } from '../../../../common/search_strategies/correlations/types'; + +import { EVENT_OUTCOME } from '../../../../common/elasticsearch_fieldnames'; +import type { + SearchServiceParams, + SearchServiceFetchParams, +} from '../../../../common/search_strategies/correlations/types'; import type { ApmIndicesConfig } from '../../settings/apm_indices/get_apm_indices'; -import { asyncSearchServiceLogProvider } from '../correlations/async_search_service_log'; +import { asyncSearchServiceLogProvider } from '../latency_correlations/async_search_service_log'; import { asyncErrorCorrelationsSearchServiceStateProvider } from './async_search_service_state'; -import { fetchTransactionDurationFieldCandidates } from '../correlations/queries'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; -import { fetchFailedTransactionsCorrelationPValues } from './queries/query_failure_correlation'; +import { + fetchFailedTransactionsCorrelationPValues, + fetchTransactionDurationFieldCandidates, +} from '../queries'; import { ERROR_CORRELATION_THRESHOLD } from './constants'; -import { EVENT_OUTCOME } from '../../../../common/elasticsearch_fieldnames'; export const asyncErrorCorrelationSearchServiceProvider = ( esClient: ElasticsearchClient, @@ -115,19 +121,28 @@ export const asyncErrorCorrelationSearchServiceProvider = ( return () => { const { ccsWarning, error, isRunning, progress } = state.getState(); - return { - ccsWarning, - error, - log: getLogMessages(), - isRunning, + const meta = { loaded: Math.round(state.getOverallProgress() * 100), - started: progress.started, total: 100, + isRunning, + isPartial: isRunning, + }; + + const rawResponse = { + ccsWarning, + log: getLogMessages(), + took: Date.now() - progress.started, values: state.getValuesSortedByScore(), + }; + + return { cancel: () => { addLogMessage(`Service cancelled.`); state.setIsCancelled(true); }, + error, + meta, + rawResponse, }; }; }; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/index.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/index.ts index f7e24ac6e1335..d346bf32f419e 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/index.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/index.ts @@ -5,5 +5,4 @@ * 2.0. */ -export { apmFailedTransactionsCorrelationsSearchStrategyProvider } from './search_strategy'; -export { FAILED_TRANSACTIONS_CORRELATION_SEARCH_STRATEGY } from '../../../../common/search_strategies/failure_correlations/constants'; +export { asyncErrorCorrelationSearchServiceProvider } from './async_search_service'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/search_strategy.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/search_strategy.ts deleted file mode 100644 index 415f19e892741..0000000000000 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/search_strategy.ts +++ /dev/null @@ -1,120 +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; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import uuid from 'uuid'; -import { of } from 'rxjs'; - -import type { ISearchStrategy } from '../../../../../../../src/plugins/data/server'; -import { - IKibanaSearchRequest, - IKibanaSearchResponse, -} from '../../../../../../../src/plugins/data/common'; - -import type { SearchServiceParams } from '../../../../common/search_strategies/correlations/types'; -import type { ApmIndicesConfig } from '../../settings/apm_indices/get_apm_indices'; - -import { asyncErrorCorrelationSearchServiceProvider } from './async_search_service'; -import { FailedTransactionsCorrelationValue } from '../../../../common/search_strategies/failure_correlations/types'; - -export type PartialSearchRequest = IKibanaSearchRequest; -export type PartialSearchResponse = IKibanaSearchResponse<{ - values: FailedTransactionsCorrelationValue[]; -}>; - -export const apmFailedTransactionsCorrelationsSearchStrategyProvider = ( - getApmIndices: () => Promise, - includeFrozen: boolean -): ISearchStrategy => { - const asyncSearchServiceMap = new Map< - string, - ReturnType - >(); - - return { - search: (request, options, deps) => { - if (request.params === undefined) { - throw new Error('Invalid request parameters.'); - } - - // The function to fetch the current state of the async search service. - // This will be either an existing service for a follow up fetch or a new one for new requests. - let getAsyncSearchServiceState: ReturnType< - typeof asyncErrorCorrelationSearchServiceProvider - >; - - // If the request includes an ID, we require that the async search service already exists - // otherwise we throw an error. The client should never poll a service that's been cancelled or finished. - // This also avoids instantiating async search services when the service gets called with random IDs. - if (typeof request.id === 'string') { - const existingGetAsyncSearchServiceState = asyncSearchServiceMap.get( - request.id - ); - - if (typeof existingGetAsyncSearchServiceState === 'undefined') { - throw new Error( - `AsyncSearchService with ID '${request.id}' does not exist.` - ); - } - - getAsyncSearchServiceState = existingGetAsyncSearchServiceState; - } else { - getAsyncSearchServiceState = asyncErrorCorrelationSearchServiceProvider( - deps.esClient.asCurrentUser, - getApmIndices, - request.params, - includeFrozen - ); - } - - // Reuse the request's id or create a new one. - const id = request.id ?? uuid(); - - const { - ccsWarning, - error, - log, - isRunning, - loaded, - started, - total, - values, - } = getAsyncSearchServiceState(); - - if (error instanceof Error) { - asyncSearchServiceMap.delete(id); - throw error; - } else if (isRunning) { - asyncSearchServiceMap.set(id, getAsyncSearchServiceState); - } else { - asyncSearchServiceMap.delete(id); - } - - const took = Date.now() - started; - - return of({ - id, - loaded, - total, - isRunning, - isPartial: isRunning, - rawResponse: { - ccsWarning, - log, - took, - values, - }, - }); - }, - cancel: async (id, options, deps) => { - const getAsyncSearchServiceState = asyncSearchServiceMap.get(id); - if (getAsyncSearchServiceState !== undefined) { - getAsyncSearchServiceState().cancel(); - asyncSearchServiceMap.delete(id); - } - }, - }; -}; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/index.ts b/x-pack/plugins/apm/server/lib/search_strategies/index.ts similarity index 77% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/index.ts rename to x-pack/plugins/apm/server/lib/search_strategies/index.ts index 5ba7b4d7c957a..b4668138eefab 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/index.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/index.ts @@ -5,4 +5,4 @@ * 2.0. */ -export { apmCorrelationsSearchStrategyProvider } from './search_strategy'; +export { registerSearchStrategies } from './register_search_strategies'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/async_search_service.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts similarity index 95% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/async_search_service.ts rename to x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts index e9986bd9f0cf5..959a699a93c8a 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/async_search_service.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts @@ -9,8 +9,10 @@ import { range } from 'lodash'; import type { ElasticsearchClient } from 'src/core/server'; import type { SearchServiceParams, + SearchServiceRawResponse, SearchServiceFetchParams, } from '../../../../common/search_strategies/correlations/types'; + import type { ApmIndicesConfig } from '../../settings/apm_indices/get_apm_indices'; import { fetchTransactionDurationFieldCandidates, @@ -20,8 +22,8 @@ import { fetchTransactionDurationHistograms, fetchTransactionDurationHistogramRangeSteps, fetchTransactionDurationRanges, -} from './queries'; -import { computeExpectationsAndRanges } from './utils'; +} from '../queries'; +import { computeExpectationsAndRanges } from '../utils'; import { asyncSearchServiceLogProvider } from './async_search_service_log'; import { asyncSearchServiceStateProvider } from './async_search_service_state'; @@ -228,21 +230,32 @@ export const asyncSearchServiceProvider = ( progress, } = state.getState(); - return { - ccsWarning, - error, - log: getLogMessages(), - isRunning, + const took = Date.now() - progress.started; + + const meta = { loaded: Math.round(state.getOverallProgress() * 100), - overallHistogram, - started: progress.started, total: 100, + isRunning, + isPartial: isRunning, + }; + + const rawResponse: SearchServiceRawResponse = { + ccsWarning, + log: getLogMessages(), + took, values: state.getValuesSortedByCorrelation(), percentileThresholdValue, + overallHistogram, + }; + + return { cancel: () => { addLogMessage(`Service cancelled.`); state.setIsCancelled(true); }, + error, + meta, + rawResponse, }; }; }; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/async_search_service_log.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_log.test.ts similarity index 64% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/async_search_service_log.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_log.test.ts index bbeb8435e61bf..bfc28bc37ea78 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/async_search_service_log.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_log.test.ts @@ -5,9 +5,26 @@ * 2.0. */ -import { asyncSearchServiceLogProvider } from './async_search_service_log'; +import { + asyncSearchServiceLogProvider, + currentTimeAsString, +} from './async_search_service_log'; describe('async search service', () => { + describe('currentTimeAsString', () => { + it('returns the current time as a string', () => { + const mockDate = new Date(1392202800000); + // @ts-ignore ignore the mockImplementation callback error + const spy = jest.spyOn(global, 'Date').mockImplementation(() => mockDate); + + const timeString = currentTimeAsString(); + + expect(timeString).toEqual('2014-02-12T11:00:00.000Z'); + + spy.mockRestore(); + }); + }); + describe('asyncSearchServiceLogProvider', () => { it('adds and retrieves messages from the log', async () => { const { addLogMessage, getLogMessages } = asyncSearchServiceLogProvider(); diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/async_search_service_log.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_log.ts similarity index 92% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/async_search_service_log.ts rename to x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_log.ts index e69d2f55b6c56..058a902026c48 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/async_search_service_log.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_log.ts @@ -5,14 +5,14 @@ * 2.0. */ -import { currentTimeAsString } from './utils'; - interface LogMessage { timestamp: string; message: string; error?: string; } +export const currentTimeAsString = () => new Date().toISOString(); + export const asyncSearchServiceLogProvider = () => { const log: LogMessage[] = []; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/async_search_service_state.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.test.ts similarity index 100% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/async_search_service_state.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.test.ts diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/async_search_service_state.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts similarity index 98% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/async_search_service_state.ts rename to x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts index d0aac8987e070..32ac034eef22d 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/async_search_service_state.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts @@ -10,7 +10,7 @@ import type { SearchServiceValue, } from '../../../../common/search_strategies/correlations/types'; -import { HistogramItem } from './queries'; +import type { HistogramItem } from '../queries'; export const asyncSearchServiceStateProvider = () => { let ccsWarning = false; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/current_time_as_string.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/index.ts similarity index 78% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/current_time_as_string.ts rename to x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/index.ts index f454b8c8274f1..64c666d98efb7 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/current_time_as_string.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/index.ts @@ -5,4 +5,4 @@ * 2.0. */ -export const currentTimeAsString = () => new Date().toISOString(); +export { asyncSearchServiceProvider } from './async_search_service'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/get_prioritized_field_value_pairs.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_prioritized_field_value_pairs.test.ts similarity index 100% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/get_prioritized_field_value_pairs.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/get_prioritized_field_value_pairs.test.ts diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/get_prioritized_field_value_pairs.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_prioritized_field_value_pairs.ts similarity index 100% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/get_prioritized_field_value_pairs.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/get_prioritized_field_value_pairs.ts diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/get_query_with_params.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.test.ts similarity index 97% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/get_query_with_params.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.test.ts index 3be3438b2d18f..b8bce75320942 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/get_query_with_params.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { ENVIRONMENT_ALL } from '../../../../../common/environment_filter_values'; +import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; import { getQueryWithParams } from './get_query_with_params'; describe('correlations', () => { diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/get_query_with_params.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts similarity index 83% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/get_query_with_params.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts index 8bd9f3d4e582c..b4246ac191e83 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/get_query_with_params.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts @@ -10,10 +10,10 @@ import { getOrElse } from 'fp-ts/lib/Either'; import { pipe } from 'fp-ts/lib/pipeable'; import * as t from 'io-ts'; import { failure } from 'io-ts/lib/PathReporter'; -import type { SearchServiceFetchParams } from '../../../../../common/search_strategies/correlations/types'; -import { rangeRt } from '../../../../routes/default_api_types'; -import { getCorrelationsFilters } from '../../../correlations/get_filters'; -import { Setup, SetupTimeRange } from '../../../helpers/setup_request'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; +import { rangeRt } from '../../../routes/default_api_types'; +import { getCorrelationsFilters } from '../../correlations/get_filters'; +import { Setup, SetupTimeRange } from '../../helpers/setup_request'; export const getTermsQuery = ( fieldName: string | undefined, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/get_request_base.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.test.ts similarity index 92% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/get_request_base.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.test.ts index b95db6d2691f1..fd5f52207d4c5 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/get_request_base.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { ENVIRONMENT_ALL } from '../../../../../common/environment_filter_values'; +import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; import { getRequestBase } from './get_request_base'; describe('correlations', () => { diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/get_request_base.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.ts similarity index 82% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/get_request_base.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.ts index e2cdbab830e0d..d3547a0ea546d 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/get_request_base.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.ts @@ -5,7 +5,7 @@ * 2.0. */ -import type { SearchServiceFetchParams } from '../../../../../common/search_strategies/correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; export const getRequestBase = ({ index, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/index.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/index.ts similarity index 91% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/index.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/index.ts index c33b131d9cbd7..ce24ccaf320e5 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/index.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/index.ts @@ -5,6 +5,7 @@ * 2.0. */ +export { fetchFailedTransactionsCorrelationPValues } from './query_failure_correlation'; export { fetchTransactionDurationFieldCandidates } from './query_field_candidates'; export { fetchTransactionDurationFieldValuePairs } from './query_field_value_pairs'; export { fetchTransactionDurationFractions } from './query_fractions'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_correlation.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.test.ts similarity index 97% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_correlation.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.test.ts index 5245af6cdadcd..d3d14260df65c 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_correlation.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.test.ts @@ -8,7 +8,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import { ENVIRONMENT_ALL } from '../../../../../common/environment_filter_values'; +import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; import { fetchTransactionDurationCorrelation, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_correlation.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts similarity index 94% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_correlation.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts index 823abe936e223..fd17e2b559694 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_correlation.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts @@ -9,8 +9,8 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import { TRANSACTION_DURATION } from '../../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../../common/search_strategies/correlations/types'; +import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/queries/query_failure_correlation.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts similarity index 87% rename from x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/queries/query_failure_correlation.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts index 81fe6697d1fb1..7a2efd5ce4d27 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/queries/query_failure_correlation.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts @@ -6,14 +6,11 @@ */ import { estypes } from '@elastic/elasticsearch'; import { ElasticsearchClient } from 'kibana/server'; -import { SearchServiceFetchParams } from '../../../../../common/search_strategies/correlations/types'; -import { - getQueryWithParams, - getTermsQuery, -} from '../../correlations/queries/get_query_with_params'; -import { getRequestBase } from '../../correlations/queries/get_request_base'; -import { EVENT_OUTCOME } from '../../../../../common/elasticsearch_fieldnames'; -import { EventOutcome } from '../../../../../common/event_outcome'; +import { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; +import { EVENT_OUTCOME } from '../../../../common/elasticsearch_fieldnames'; +import { EventOutcome } from '../../../../common/event_outcome'; +import { getQueryWithParams, getTermsQuery } from './get_query_with_params'; +import { getRequestBase } from './get_request_base'; export const getFailureCorrelationRequest = ( params: SearchServiceFetchParams, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_field_candidates.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.test.ts similarity index 96% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_field_candidates.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.test.ts index 688af72e8f6d3..150348e2a7aa2 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_field_candidates.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.test.ts @@ -8,9 +8,9 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import { ENVIRONMENT_ALL } from '../../../../../common/environment_filter_values'; +import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; -import { hasPrefixToInclude } from '../utils/has_prefix_to_include'; +import { hasPrefixToInclude } from '../utils'; import { fetchTransactionDurationFieldCandidates, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_field_candidates.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts similarity index 96% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_field_candidates.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts index aeb67a4d6884b..e1762e888eae3 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_field_candidates.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts @@ -9,7 +9,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import type { SearchServiceFetchParams } from '../../../../../common/search_strategies/correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; import { FIELD_PREFIX_TO_EXCLUDE_AS_CANDIDATE, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_field_value_pairs.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.test.ts similarity index 90% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_field_value_pairs.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.test.ts index a20720944f19b..0d47302464727 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_field_value_pairs.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.test.ts @@ -8,10 +8,10 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import { ENVIRONMENT_ALL } from '../../../../../common/environment_filter_values'; +import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; -import { asyncSearchServiceLogProvider } from '../async_search_service_log'; -import { asyncSearchServiceStateProvider } from '../async_search_service_state'; +import { asyncSearchServiceLogProvider } from '../latency_correlations/async_search_service_log'; +import { asyncSearchServiceStateProvider } from '../latency_correlations/async_search_service_state'; import { fetchTransactionDurationFieldValuePairs, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_field_value_pairs.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts similarity index 91% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_field_value_pairs.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts index 33adff4af7a52..e8b3f6407e1af 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_field_value_pairs.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts @@ -9,10 +9,10 @@ import type { ElasticsearchClient } from 'src/core/server'; import type { estypes } from '@elastic/elasticsearch'; -import type { SearchServiceFetchParams } from '../../../../../common/search_strategies/correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; -import type { AsyncSearchServiceLog } from '../async_search_service_log'; -import type { AsyncSearchServiceState } from '../async_search_service_state'; +import type { AsyncSearchServiceLog } from '../latency_correlations/async_search_service_log'; +import type { AsyncSearchServiceState } from '../latency_correlations/async_search_service_state'; import { TERMS_SIZE } from '../constants'; import { getQueryWithParams } from './get_query_with_params'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_fractions.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.test.ts similarity index 96% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_fractions.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.test.ts index 73df48a0d8170..fdf383453e17f 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_fractions.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.test.ts @@ -8,7 +8,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import { ENVIRONMENT_ALL } from '../../../../../common/environment_filter_values'; +import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; import { fetchTransactionDurationFractions, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_fractions.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.ts similarity index 90% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_fractions.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.ts index 35e59054ad01f..64b5bd862ad6d 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_fractions.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.ts @@ -8,8 +8,8 @@ import { ElasticsearchClient } from 'kibana/server'; import { estypes } from '@elastic/elasticsearch'; -import { SearchServiceFetchParams } from '../../../../../common/search_strategies/correlations/types'; -import { TRANSACTION_DURATION } from '../../../../../common/elasticsearch_fieldnames'; +import { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; +import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.test.ts similarity index 97% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.test.ts index 9b2a4807d4863..e6faeb16247fb 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.test.ts @@ -8,7 +8,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import { ENVIRONMENT_ALL } from '../../../../../common/environment_filter_values'; +import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; import { fetchTransactionDurationHistogram, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts similarity index 91% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts index 18fc18af1472e..a5875c78072a2 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts @@ -9,12 +9,12 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import { TRANSACTION_DURATION } from '../../../../../common/elasticsearch_fieldnames'; +import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; import type { HistogramItem, ResponseHit, SearchServiceFetchParams, -} from '../../../../../common/search_strategies/correlations/types'; +} from '../../../../common/search_strategies/correlations/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram_interval.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.test.ts similarity index 97% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram_interval.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.test.ts index bb76769fe94b5..7b0d00d0d9b57 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram_interval.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.test.ts @@ -8,7 +8,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import { ENVIRONMENT_ALL } from '../../../../../common/environment_filter_values'; +import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; import { fetchTransactionDurationHistogramInterval, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram_interval.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.ts similarity index 89% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram_interval.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.ts index cc50c8d4d860a..af5f990f19bab 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram_interval.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.ts @@ -9,8 +9,8 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import { TRANSACTION_DURATION } from '../../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../../common/search_strategies/correlations/types'; +import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram_range_steps.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.test.ts similarity index 97% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram_range_steps.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.test.ts index 52cfe6168232d..88d4f1a57adeb 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram_range_steps.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.test.ts @@ -8,7 +8,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import { ENVIRONMENT_ALL } from '../../../../../common/environment_filter_values'; +import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; import { fetchTransactionDurationHistogramRangeSteps, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram_range_steps.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.ts similarity index 91% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram_range_steps.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.ts index 116b5d1645601..b9f591885afc7 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histogram_range_steps.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.ts @@ -11,8 +11,8 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import { TRANSACTION_DURATION } from '../../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../../common/search_strategies/correlations/types'; +import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histograms_generator.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.test.ts similarity index 93% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histograms_generator.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.test.ts index 22876684bec7e..a436920aa3c9d 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histograms_generator.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.test.ts @@ -8,10 +8,10 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import { ENVIRONMENT_ALL } from '../../../../../common/environment_filter_values'; +import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; -import { asyncSearchServiceLogProvider } from '../async_search_service_log'; -import { asyncSearchServiceStateProvider } from '../async_search_service_state'; +import { asyncSearchServiceLogProvider } from '../latency_correlations/async_search_service_log'; +import { asyncSearchServiceStateProvider } from '../latency_correlations/async_search_service_state'; import { fetchTransactionDurationHistograms } from './query_histograms_generator'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histograms_generator.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts similarity index 90% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histograms_generator.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts index c4869aac187c6..3c6b472b4ebd8 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_histograms_generator.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts @@ -9,10 +9,10 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import type { SearchServiceFetchParams } from '../../../../../common/search_strategies/correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; -import type { AsyncSearchServiceLog } from '../async_search_service_log'; -import type { AsyncSearchServiceState } from '../async_search_service_state'; +import type { AsyncSearchServiceLog } from '../latency_correlations/async_search_service_log'; +import type { AsyncSearchServiceState } from '../latency_correlations/async_search_service_state'; import { CORRELATION_THRESHOLD, KS_TEST_THRESHOLD } from '../constants'; import { getPrioritizedFieldValuePairs } from './get_prioritized_field_value_pairs'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_percentiles.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.test.ts similarity index 97% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_percentiles.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.test.ts index cab2e283935d6..1a5d518b7e47a 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_percentiles.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.test.ts @@ -8,7 +8,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import { ENVIRONMENT_ALL } from '../../../../../common/environment_filter_values'; +import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; import { fetchTransactionDurationPercentiles, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_percentiles.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts similarity index 92% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_percentiles.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts index bd230687314e6..c88b9f83fac55 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_percentiles.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts @@ -9,8 +9,8 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import { TRANSACTION_DURATION } from '../../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../../common/search_strategies/correlations/types'; +import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_ranges.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.test.ts similarity index 97% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_ranges.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.test.ts index 839d6a33cfe05..64b746b72534a 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_ranges.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.test.ts @@ -8,7 +8,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import { ENVIRONMENT_ALL } from '../../../../../common/environment_filter_values'; +import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; import { fetchTransactionDurationRanges, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_ranges.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts similarity index 92% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_ranges.ts rename to x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts index 6f662363d0c42..840ddf97421c2 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/queries/query_ranges.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts @@ -9,8 +9,8 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import { TRANSACTION_DURATION } from '../../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../../common/search_strategies/correlations/types'; +import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/register_search_strategies.ts b/x-pack/plugins/apm/server/lib/search_strategies/register_search_strategies.ts new file mode 100644 index 0000000000000..83ec719abf055 --- /dev/null +++ b/x-pack/plugins/apm/server/lib/search_strategies/register_search_strategies.ts @@ -0,0 +1,41 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { PluginSetup as DataPluginSetup } from 'src/plugins/data/server'; + +import { APM_SEARCH_STRATEGIES } from '../../../common/search_strategies/constants'; + +import type { ApmIndicesConfig } from '../settings/apm_indices/get_apm_indices'; + +import { searchStrategyProvider } from './search_strategy_provider'; + +import { asyncSearchServiceProvider } from './latency_correlations'; +import { asyncErrorCorrelationSearchServiceProvider } from './failed_transactions_correlations'; + +export const registerSearchStrategies = ( + registerSearchStrategy: DataPluginSetup['search']['registerSearchStrategy'], + getApmIndices: () => Promise, + includeFrozen: boolean +) => { + registerSearchStrategy( + APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, + searchStrategyProvider( + asyncSearchServiceProvider, + getApmIndices, + includeFrozen + ) + ); + + registerSearchStrategy( + APM_SEARCH_STRATEGIES.APM_FAILED_TRANSACTIONS_CORRELATIONS, + searchStrategyProvider( + asyncErrorCorrelationSearchServiceProvider, + getApmIndices, + includeFrozen + ) + ); +}; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/search_strategy.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts similarity index 89% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/search_strategy.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts index b5ab4a072be6c..9570a74271690 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/search_strategy.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts @@ -8,14 +8,16 @@ import type { estypes } from '@elastic/elasticsearch'; import { SearchStrategyDependencies } from 'src/plugins/data/server'; -import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; +import { ENVIRONMENT_ALL } from '../../../common/environment_filter_values'; -import type { ApmIndicesConfig } from '../../settings/apm_indices/get_apm_indices'; +import type { ApmIndicesConfig } from '../settings/apm_indices/get_apm_indices'; + +import { asyncSearchServiceProvider } from './latency_correlations'; import { - apmCorrelationsSearchStrategyProvider, + searchStrategyProvider, PartialSearchRequest, -} from './search_strategy'; +} from './search_strategy_provider'; // helper to trigger promises in the async search service const flushPromises = () => new Promise(setImmediate); @@ -106,7 +108,8 @@ const getApmIndicesMock = async () => describe('APM Correlations search strategy', () => { describe('strategy interface', () => { it('returns a custom search strategy with a `search` and `cancel` function', async () => { - const searchStrategy = await apmCorrelationsSearchStrategyProvider( + const searchStrategy = await searchStrategyProvider( + asyncSearchServiceProvider, getApmIndicesMock, false ); @@ -145,7 +148,8 @@ describe('APM Correlations search strategy', () => { describe('async functionality', () => { describe('when no params are provided', () => { it('throws an error', async () => { - const searchStrategy = await apmCorrelationsSearchStrategyProvider( + const searchStrategy = await searchStrategyProvider( + asyncSearchServiceProvider, mockGetApmIndicesMock, false ); @@ -160,7 +164,8 @@ describe('APM Correlations search strategy', () => { describe('when no ID is provided', () => { it('performs a client search with params', async () => { - const searchStrategy = await apmCorrelationsSearchStrategyProvider( + const searchStrategy = await searchStrategyProvider( + asyncSearchServiceProvider, mockGetApmIndicesMock, false ); @@ -206,7 +211,8 @@ describe('APM Correlations search strategy', () => { describe('when an ID with params is provided', () => { it('retrieves the current request', async () => { - const searchStrategy = await apmCorrelationsSearchStrategyProvider( + const searchStrategy = await searchStrategyProvider( + asyncSearchServiceProvider, mockGetApmIndicesMock, false ); @@ -232,7 +238,8 @@ describe('APM Correlations search strategy', () => { mockClientSearch .mockReset() .mockRejectedValueOnce(new Error('client error')); - const searchStrategy = await apmCorrelationsSearchStrategyProvider( + const searchStrategy = await searchStrategyProvider( + asyncSearchServiceProvider, mockGetApmIndicesMock, false ); @@ -250,7 +257,8 @@ describe('APM Correlations search strategy', () => { it('triggers the subscription only once', async () => { expect.assertions(2); - const searchStrategy = await apmCorrelationsSearchStrategyProvider( + const searchStrategy = await searchStrategyProvider( + asyncSearchServiceProvider, mockGetApmIndicesMock, false ); @@ -267,7 +275,8 @@ describe('APM Correlations search strategy', () => { describe('response', () => { it('sends an updated response on consecutive search calls', async () => { - const searchStrategy = await apmCorrelationsSearchStrategyProvider( + const searchStrategy = await searchStrategyProvider( + asyncSearchServiceProvider, mockGetApmIndicesMock, false ); diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/search_strategy.ts b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts similarity index 62% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/search_strategy.ts rename to x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts index 7f67147a75580..d6618ed168dbc 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/search_strategy.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts @@ -8,34 +8,58 @@ import uuid from 'uuid'; import { of } from 'rxjs'; -import type { ISearchStrategy } from '../../../../../../../src/plugins/data/server'; +import type { ElasticsearchClient } from 'src/core/server'; + +import type { ISearchStrategy } from '../../../../../../src/plugins/data/server'; import { IKibanaSearchRequest, IKibanaSearchResponse, -} from '../../../../../../../src/plugins/data/common'; +} from '../../../../../../src/plugins/data/common'; -import type { - SearchServiceParams, - SearchServiceRawResponse, - SearchServiceValue, -} from '../../../../common/search_strategies/correlations/types'; +import type { SearchServiceParams } from '../../../common/search_strategies/correlations/types'; +import type { ApmIndicesConfig } from '../settings/apm_indices/get_apm_indices'; -import type { ApmIndicesConfig } from '../../settings/apm_indices/get_apm_indices'; +export type PartialSearchRequest = IKibanaSearchRequest; -import { asyncSearchServiceProvider } from './async_search_service'; +interface RawResponseBase { + ccsWarning: boolean; + took: number; +} + +interface AsyncSearchServiceState { + cancel: () => void; + error: Error; + meta: { + loaded: number; + total: number; + isRunning: boolean; + isPartial: boolean; + }; + rawResponse: RawResponse; +} -export type PartialSearchRequest = IKibanaSearchRequest; -export type PartialSearchResponse = IKibanaSearchResponse<{ - values: SearchServiceValue[]; -}>; +type GetAsyncSearchServiceState< + RawResponse extends RawResponseBase +> = () => AsyncSearchServiceState; -export const apmCorrelationsSearchStrategyProvider = ( +type AsyncSearchServiceProvider = ( + esClient: ElasticsearchClient, getApmIndices: () => Promise, + searchServiceParams: SearchServiceParams, includeFrozen: boolean -): ISearchStrategy => { +) => GetAsyncSearchServiceState; + +export const searchStrategyProvider = ( + asyncSearchServiceProvider: AsyncSearchServiceProvider, + getApmIndices: () => Promise, + includeFrozen: boolean +): ISearchStrategy< + PartialSearchRequest, + IKibanaSearchResponse +> => { const asyncSearchServiceMap = new Map< string, - ReturnType + GetAsyncSearchServiceState >(); return { @@ -46,9 +70,7 @@ export const apmCorrelationsSearchStrategyProvider = ( // The function to fetch the current state of the async search service. // This will be either an existing service for a follow up fetch or a new one for new requests. - let getAsyncSearchServiceState: ReturnType< - typeof asyncSearchServiceProvider - >; + let getAsyncSearchServiceState: GetAsyncSearchServiceState; // If the request includes an ID, we require that the async search service already exists // otherwise we throw an error. The client should never poll a service that's been cancelled or finished. @@ -77,45 +99,20 @@ export const apmCorrelationsSearchStrategyProvider = ( // Reuse the request's id or create a new one. const id = request.id ?? uuid(); - const { - ccsWarning, - error, - log, - isRunning, - loaded, - started, - total, - values, - percentileThresholdValue, - overallHistogram, - } = getAsyncSearchServiceState(); + const { error, meta, rawResponse } = getAsyncSearchServiceState(); if (error instanceof Error) { asyncSearchServiceMap.delete(id); throw error; - } else if (isRunning) { + } else if (meta.isRunning) { asyncSearchServiceMap.set(id, getAsyncSearchServiceState); } else { asyncSearchServiceMap.delete(id); } - const took = Date.now() - started; - - const rawResponse: SearchServiceRawResponse = { - ccsWarning, - log, - took, - values, - percentileThresholdValue, - overallHistogram, - }; - return of({ id, - loaded, - total, - isRunning, - isPartial: isRunning, + ...meta, rawResponse, }); }, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/compute_expectations_and_ranges.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/utils/compute_expectations_and_ranges.test.ts similarity index 100% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/compute_expectations_and_ranges.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/utils/compute_expectations_and_ranges.test.ts diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/compute_expectations_and_ranges.ts b/x-pack/plugins/apm/server/lib/search_strategies/utils/compute_expectations_and_ranges.ts similarity index 100% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/compute_expectations_and_ranges.ts rename to x-pack/plugins/apm/server/lib/search_strategies/utils/compute_expectations_and_ranges.ts diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/has_prefix_to_include.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/utils/has_prefix_to_include.test.ts similarity index 100% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/has_prefix_to_include.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/utils/has_prefix_to_include.test.ts diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/has_prefix_to_include.ts b/x-pack/plugins/apm/server/lib/search_strategies/utils/has_prefix_to_include.ts similarity index 100% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/has_prefix_to_include.ts rename to x-pack/plugins/apm/server/lib/search_strategies/utils/has_prefix_to_include.ts diff --git a/x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/index.ts b/x-pack/plugins/apm/server/lib/search_strategies/utils/index.ts similarity index 86% rename from x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/index.ts rename to x-pack/plugins/apm/server/lib/search_strategies/utils/index.ts index 000fd57c718b7..727bc6cd787a0 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/correlations/utils/index.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/utils/index.ts @@ -6,5 +6,4 @@ */ export { computeExpectationsAndRanges } from './compute_expectations_and_ranges'; -export { currentTimeAsString } from './current_time_as_string'; export { hasPrefixToInclude } from './has_prefix_to_include'; diff --git a/x-pack/plugins/apm/server/plugin.ts b/x-pack/plugins/apm/server/plugin.ts index fadeae338cbdb..ec18c87c96f73 100644 --- a/x-pack/plugins/apm/server/plugin.ts +++ b/x-pack/plugins/apm/server/plugin.ts @@ -28,7 +28,7 @@ import { registerFleetPolicyCallbacks } from './lib/fleet/register_fleet_policy_ import { createApmTelemetry } from './lib/apm_telemetry'; import { createApmEventClient } from './lib/helpers/create_es_client/create_apm_event_client'; import { getInternalSavedObjectsClient } from './lib/helpers/get_internal_saved_objects_client'; -import { apmCorrelationsSearchStrategyProvider } from './lib/search_strategies/correlations'; +import { registerSearchStrategies } from './lib/search_strategies'; import { createApmAgentConfigurationIndex } from './lib/settings/agent_configuration/create_agent_config_index'; import { getApmIndices } from './lib/settings/apm_indices/get_apm_indices'; import { createApmCustomLinkIndex } from './lib/settings/custom_link/create_custom_link_index'; @@ -51,10 +51,6 @@ import { TRANSACTION_TYPE, } from '../common/elasticsearch_fieldnames'; import { tutorialProvider } from './tutorial'; -import { - apmFailedTransactionsCorrelationsSearchStrategyProvider, - FAILED_TRANSACTIONS_CORRELATION_SEARCH_STRATEGY, -} from './lib/search_strategies/failed_transactions_correlations'; export class APMPlugin implements @@ -223,22 +219,10 @@ export class APMPlugin .asScopedToClient(savedObjectsClient) .get(UI_SETTINGS.SEARCH_INCLUDE_FROZEN); - // Register APM latency correlations search strategy - plugins.data.search.registerSearchStrategy( - 'apmCorrelationsSearchStrategy', - apmCorrelationsSearchStrategyProvider( - boundGetApmIndices, - includeFrozen - ) - ); - - // Register APM failed transactions correlations search strategy - plugins.data.search.registerSearchStrategy( - FAILED_TRANSACTIONS_CORRELATION_SEARCH_STRATEGY, - apmFailedTransactionsCorrelationsSearchStrategyProvider( - boundGetApmIndices, - includeFrozen - ) + registerSearchStrategies( + plugins.data.search.registerSearchStrategy, + boundGetApmIndices, + includeFrozen ); })(); }); diff --git a/x-pack/test/apm_api_integration/tests/correlations/failed_transactions.ts b/x-pack/test/apm_api_integration/tests/correlations/failed_transactions.ts index 94c293cd1a19f..eb3473691838e 100644 --- a/x-pack/test/apm_api_integration/tests/correlations/failed_transactions.ts +++ b/x-pack/test/apm_api_integration/tests/correlations/failed_transactions.ts @@ -8,7 +8,7 @@ import expect from '@kbn/expect'; import { FtrProviderContext } from '../../common/ftr_provider_context'; import { registry } from '../../common/registry'; -import { PartialSearchRequest } from '../../../../plugins/apm/server/lib/search_strategies/correlations/search_strategy'; +import type { PartialSearchRequest } from '../../../../plugins/apm/server/lib/search_strategies/search_strategy_provider'; import { parseBfetchResponse } from '../../common/utils/parse_b_fetch'; export default function ApiTest({ getService }: FtrProviderContext) { diff --git a/x-pack/test/apm_api_integration/tests/correlations/latency.ts b/x-pack/test/apm_api_integration/tests/correlations/latency.ts index 32ca71694626f..0a85423911709 100644 --- a/x-pack/test/apm_api_integration/tests/correlations/latency.ts +++ b/x-pack/test/apm_api_integration/tests/correlations/latency.ts @@ -8,7 +8,7 @@ import expect from '@kbn/expect'; import { FtrProviderContext } from '../../common/ftr_provider_context'; import { registry } from '../../common/registry'; -import { PartialSearchRequest } from '../../../../plugins/apm/server/lib/search_strategies/correlations/search_strategy'; +import type { PartialSearchRequest } from '../../../../plugins/apm/server/lib/search_strategies/search_strategy_provider'; import { parseBfetchResponse } from '../../common/utils/parse_b_fetch'; export default function ApiTest({ getService }: FtrProviderContext) { From 547ae65e6d46ef81b79602e6e2d765b74c766dfa Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Mon, 30 Aug 2021 16:54:59 +0200 Subject: [PATCH 04/37] [ML] Fix naming inconsistencies. --- .../constants.ts | 0 .../types.ts | 0 .../types.ts | 0 .../app/correlations/correlations_table.tsx | 2 +- .../failed_transactions_correlations.tsx | 2 +- .../correlations/latency_correlations.test.tsx | 2 +- ...transactions_correlation_impact_label.test.ts | 2 +- ...iled_transactions_correlation_impact_label.ts | 4 ++-- .../distribution/index.test.tsx | 2 +- .../transaction_distribution_chart/index.tsx | 2 +- ...e_failed_transactions_correlations_fetcher.ts | 6 +++--- .../use_transaction_distribution_fetcher.ts | 2 +- ...e_transaction_latency_correlations_fetcher.ts | 2 +- .../async_search_service_log.test.ts | 0 .../async_search_service_log.ts | 0 .../server/lib/search_strategies/constants.ts | 2 ++ .../async_search_service.ts | 8 ++++---- .../async_search_service_state.ts | 2 +- .../constants.ts | 8 -------- .../failed_transactions_correlations/index.ts | 2 +- .../latency_correlations/async_search_service.ts | 9 ++++++--- .../async_search_service_state.ts | 2 +- .../latency_correlations/index.ts | 2 +- .../queries/get_query_with_params.ts | 2 +- .../queries/get_request_base.ts | 2 +- .../queries/query_correlation.ts | 2 +- .../queries/query_failure_correlation.ts | 2 +- .../queries/query_field_candidates.ts | 2 +- .../queries/query_field_value_pairs.test.ts | 2 +- .../queries/query_field_value_pairs.ts | 4 ++-- .../search_strategies/queries/query_fractions.ts | 2 +- .../search_strategies/queries/query_histogram.ts | 2 +- .../queries/query_histogram_interval.ts | 2 +- .../queries/query_histogram_range_steps.ts | 2 +- .../queries/query_histograms_generator.test.ts | 2 +- .../queries/query_histograms_generator.ts | 4 ++-- .../queries/query_percentiles.ts | 2 +- .../search_strategies/queries/query_ranges.ts | 2 +- .../register_search_strategies.ts | 8 ++++---- .../search_strategy_provider.test.ts | 16 ++++++++-------- .../search_strategy_provider.ts | 4 ++-- 41 files changed, 60 insertions(+), 63 deletions(-) rename x-pack/plugins/apm/common/search_strategies/{failure_correlations => failed_transactions_correlations}/constants.ts (100%) rename x-pack/plugins/apm/common/search_strategies/{failure_correlations => failed_transactions_correlations}/types.ts (100%) rename x-pack/plugins/apm/common/search_strategies/{correlations => latency_correlations}/types.ts (100%) rename x-pack/plugins/apm/server/lib/search_strategies/{latency_correlations => }/async_search_service_log.test.ts (100%) rename x-pack/plugins/apm/server/lib/search_strategies/{latency_correlations => }/async_search_service_log.ts (100%) delete mode 100644 x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/constants.ts diff --git a/x-pack/plugins/apm/common/search_strategies/failure_correlations/constants.ts b/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/constants.ts similarity index 100% rename from x-pack/plugins/apm/common/search_strategies/failure_correlations/constants.ts rename to x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/constants.ts diff --git a/x-pack/plugins/apm/common/search_strategies/failure_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts similarity index 100% rename from x-pack/plugins/apm/common/search_strategies/failure_correlations/types.ts rename to x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts diff --git a/x-pack/plugins/apm/common/search_strategies/correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts similarity index 100% rename from x-pack/plugins/apm/common/search_strategies/correlations/types.ts rename to x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts diff --git a/x-pack/plugins/apm/public/components/app/correlations/correlations_table.tsx b/x-pack/plugins/apm/public/components/app/correlations/correlations_table.tsx index f7e62b76a61c0..6fb239b9e3c3a 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/correlations_table.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/correlations_table.tsx @@ -14,7 +14,7 @@ import type { Criteria } from '@elastic/eui/src/components/basic_table/basic_tab import { FETCH_STATUS } from '../../../hooks/use_fetcher'; import { useUiTracker } from '../../../../../observability/public'; import { useTheme } from '../../../hooks/use_theme'; -import type { CorrelationsTerm } from '../../../../common/search_strategies/failure_correlations/types'; +import type { CorrelationsTerm } from '../../../../common/search_strategies/failed_transactions_correlations/types'; const PAGINATION_SIZE_OPTIONS = [5, 10, 20, 50]; diff --git a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx index 4fb7bf5d6fcfb..7b92dd3bc01b3 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx @@ -41,7 +41,7 @@ import { CorrelationsLog } from './correlations_log'; import { CorrelationsEmptyStatePrompt } from './empty_state_prompt'; import { CrossClusterSearchCompatibilityWarning } from './cross_cluster_search_warning'; import { CorrelationsProgressControls } from './progress_controls'; -import type { FailedTransactionsCorrelationValue } from '../../../../common/search_strategies/failure_correlations/types'; +import type { FailedTransactionsCorrelationValue } from '../../../../common/search_strategies/failed_transactions_correlations/types'; import { Summary } from '../../shared/Summary'; import { asPercent } from '../../../../common/utils/formatters'; import { useTimeRange } from '../../../hooks/use_time_range'; diff --git a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.test.tsx b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.test.tsx index b0da5b6d60d74..ea08e7ebd4926 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.test.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.test.tsx @@ -18,7 +18,7 @@ import { dataPluginMock } from 'src/plugins/data/public/mocks'; import type { IKibanaSearchResponse } from 'src/plugins/data/public'; import { EuiThemeProvider } from 'src/plugins/kibana_react/common'; import { createKibanaReactContext } from 'src/plugins/kibana_react/public'; -import type { SearchServiceRawResponse } from '../../../../common/search_strategies/correlations/types'; +import type { SearchServiceRawResponse } from '../../../../common/search_strategies/latency_correlations/types'; import { MockUrlParamsContextProvider } from '../../../context/url_params_context/mock_url_params_context_provider'; import { ApmPluginContextValue } from '../../../context/apm_plugin/apm_plugin_context'; import { diff --git a/x-pack/plugins/apm/public/components/app/correlations/utils/get_failed_transactions_correlation_impact_label.test.ts b/x-pack/plugins/apm/public/components/app/correlations/utils/get_failed_transactions_correlation_impact_label.test.ts index edb7c8c16e267..e4c08b42b2420 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/utils/get_failed_transactions_correlation_impact_label.test.ts +++ b/x-pack/plugins/apm/public/components/app/correlations/utils/get_failed_transactions_correlation_impact_label.test.ts @@ -6,7 +6,7 @@ */ import { getFailedTransactionsCorrelationImpactLabel } from './get_failed_transactions_correlation_impact_label'; -import { FAILED_TRANSACTIONS_IMPACT_THRESHOLD } from '../../../../../common/search_strategies/failure_correlations/constants'; +import { FAILED_TRANSACTIONS_IMPACT_THRESHOLD } from '../../../../../common/search_strategies/failed_transactions_correlations/constants'; const EXPECTED_RESULT = { HIGH: { diff --git a/x-pack/plugins/apm/public/components/app/correlations/utils/get_failed_transactions_correlation_impact_label.ts b/x-pack/plugins/apm/public/components/app/correlations/utils/get_failed_transactions_correlation_impact_label.ts index 5a806aba5371e..7a8bf28058cc0 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/utils/get_failed_transactions_correlation_impact_label.ts +++ b/x-pack/plugins/apm/public/components/app/correlations/utils/get_failed_transactions_correlation_impact_label.ts @@ -5,8 +5,8 @@ * 2.0. */ -import { FailureCorrelationImpactThreshold } from '../../../../../common/search_strategies/failure_correlations/types'; -import { FAILED_TRANSACTIONS_IMPACT_THRESHOLD } from '../../../../../common/search_strategies/failure_correlations/constants'; +import { FailureCorrelationImpactThreshold } from '../../../../../common/search_strategies/failed_transactions_correlations/types'; +import { FAILED_TRANSACTIONS_IMPACT_THRESHOLD } from '../../../../../common/search_strategies/failed_transactions_correlations/constants'; export function getFailedTransactionsCorrelationImpactLabel( pValue: number diff --git a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx index 5a9977b373c33..db9babb830581 100644 --- a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx +++ b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx @@ -16,7 +16,7 @@ import { dataPluginMock } from 'src/plugins/data/public/mocks'; import type { IKibanaSearchResponse } from 'src/plugins/data/public'; import { EuiThemeProvider } from 'src/plugins/kibana_react/common'; import { createKibanaReactContext } from 'src/plugins/kibana_react/public'; -import type { SearchServiceRawResponse } from '../../../../../common/search_strategies/correlations/types'; +import type { SearchServiceRawResponse } from '../../../../../common/search_strategies/latency_correlations/types'; import { MockUrlParamsContextProvider } from '../../../../context/url_params_context/mock_url_params_context_provider'; import { ApmPluginContextValue } from '../../../../context/apm_plugin/apm_plugin_context'; import { diff --git a/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx b/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx index a58a2887b1576..aaeb83ea97043 100644 --- a/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx +++ b/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx @@ -30,7 +30,7 @@ import { i18n } from '@kbn/i18n'; import { useChartTheme } from '../../../../../../observability/public'; import { getDurationFormatter } from '../../../../../common/utils/formatters'; -import { HistogramItem } from '../../../../../common/search_strategies/correlations/types'; +import type { HistogramItem } from '../../../../../common/search_strategies/latency_correlations/types'; import { FETCH_STATUS } from '../../../../hooks/use_fetcher'; import { useTheme } from '../../../../hooks/use_theme'; diff --git a/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts b/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts index 38406416d4d96..7ba7c8adbe702 100644 --- a/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts +++ b/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts @@ -13,10 +13,10 @@ import { isCompleteResponse, isErrorResponse, } from '../../../../../src/plugins/data/public'; -import type { SearchServiceParams } from '../../common/search_strategies/correlations/types'; +import type { SearchServiceParams } from '../../common/search_strategies/latency_correlations/types'; import { useKibana } from '../../../../../src/plugins/kibana_react/public'; -import { ApmPluginStartDeps } from '../plugin'; -import { FailedTransactionsCorrelationValue } from '../../common/search_strategies/failure_correlations/types'; +import type { ApmPluginStartDeps } from '../plugin'; +import type { FailedTransactionsCorrelationValue } from '../../common/search_strategies/failed_transactions_correlations/types'; import { APM_SEARCH_STRATEGIES } from '../../common/search_strategies/constants'; interface RawResponse { diff --git a/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts b/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts index 72e1baa126df7..cbbad93742b9f 100644 --- a/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts +++ b/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts @@ -16,7 +16,7 @@ import { import type { SearchServiceParams, SearchServiceRawResponse, -} from '../../common/search_strategies/correlations/types'; +} from '../../common/search_strategies/latency_correlations/types'; import { useKibana } from '../../../../../src/plugins/kibana_react/public'; import { APM_SEARCH_STRATEGIES } from '../../common/search_strategies/constants'; import { ApmPluginStartDeps } from '../plugin'; diff --git a/x-pack/plugins/apm/public/hooks/use_transaction_latency_correlations_fetcher.ts b/x-pack/plugins/apm/public/hooks/use_transaction_latency_correlations_fetcher.ts index ad5a78173c7e1..43ef683e266dd 100644 --- a/x-pack/plugins/apm/public/hooks/use_transaction_latency_correlations_fetcher.ts +++ b/x-pack/plugins/apm/public/hooks/use_transaction_latency_correlations_fetcher.ts @@ -16,7 +16,7 @@ import { import type { SearchServiceParams, SearchServiceRawResponse, -} from '../../common/search_strategies/correlations/types'; +} from '../../common/search_strategies/latency_correlations/types'; import { useKibana } from '../../../../../src/plugins/kibana_react/public'; import { APM_SEARCH_STRATEGIES } from '../../common/search_strategies/constants'; import { ApmPluginStartDeps } from '../plugin'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_log.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/async_search_service_log.test.ts similarity index 100% rename from x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_log.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/async_search_service_log.test.ts diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_log.ts b/x-pack/plugins/apm/server/lib/search_strategies/async_search_service_log.ts similarity index 100% rename from x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_log.ts rename to x-pack/plugins/apm/server/lib/search_strategies/async_search_service_log.ts diff --git a/x-pack/plugins/apm/server/lib/search_strategies/constants.ts b/x-pack/plugins/apm/server/lib/search_strategies/constants.ts index 6b96b6b9d2131..5500e336c3542 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/constants.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/constants.ts @@ -79,3 +79,5 @@ export const SIGNIFICANT_VALUE_DIGITS = 3; export const CORRELATION_THRESHOLD = 0.3; export const KS_TEST_THRESHOLD = 0.1; + +export const ERROR_CORRELATION_THRESHOLD = 0.02; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts index e07db04665ef0..3116ae587bd87 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts @@ -13,17 +13,17 @@ import { EVENT_OUTCOME } from '../../../../common/elasticsearch_fieldnames'; import type { SearchServiceParams, SearchServiceFetchParams, -} from '../../../../common/search_strategies/correlations/types'; +} from '../../../../common/search_strategies/latency_correlations/types'; import type { ApmIndicesConfig } from '../../settings/apm_indices/get_apm_indices'; -import { asyncSearchServiceLogProvider } from '../latency_correlations/async_search_service_log'; +import { asyncSearchServiceLogProvider } from '../async_search_service_log'; import { asyncErrorCorrelationsSearchServiceStateProvider } from './async_search_service_state'; import { fetchFailedTransactionsCorrelationPValues, fetchTransactionDurationFieldCandidates, } from '../queries'; -import { ERROR_CORRELATION_THRESHOLD } from './constants'; +import { ERROR_CORRELATION_THRESHOLD } from '../constants'; -export const asyncErrorCorrelationSearchServiceProvider = ( +export const failedTransactionsCorrelationsAsyncSearchServiceProvider = ( esClient: ElasticsearchClient, getApmIndices: () => Promise, searchServiceParams: SearchServiceParams, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service_state.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service_state.ts index fb0c6fea4879a..cbb90371da352 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service_state.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service_state.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { FailedTransactionsCorrelationValue } from '../../../../common/search_strategies/failure_correlations/types'; +import { FailedTransactionsCorrelationValue } from '../../../../common/search_strategies/failed_transactions_correlations/types'; interface Progress { started: number; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/constants.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/constants.ts deleted file mode 100644 index 711c5f736d774..0000000000000 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/constants.ts +++ /dev/null @@ -1,8 +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; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -export const ERROR_CORRELATION_THRESHOLD = 0.02; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/index.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/index.ts index d346bf32f419e..ade25ada80a62 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/index.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/index.ts @@ -5,4 +5,4 @@ * 2.0. */ -export { asyncErrorCorrelationSearchServiceProvider } from './async_search_service'; +export { failedTransactionsCorrelationsAsyncSearchServiceProvider } from './async_search_service'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts index 959a699a93c8a..a2566add7447f 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts @@ -11,9 +11,10 @@ import type { SearchServiceParams, SearchServiceRawResponse, SearchServiceFetchParams, -} from '../../../../common/search_strategies/correlations/types'; +} from '../../../../common/search_strategies/latency_correlations/types'; import type { ApmIndicesConfig } from '../../settings/apm_indices/get_apm_indices'; + import { fetchTransactionDurationFieldCandidates, fetchTransactionDurationFieldValuePairs, @@ -24,10 +25,12 @@ import { fetchTransactionDurationRanges, } from '../queries'; import { computeExpectationsAndRanges } from '../utils'; -import { asyncSearchServiceLogProvider } from './async_search_service_log'; +import { asyncSearchServiceLogProvider } from '../async_search_service_log'; +import type { AsyncSearchServiceProvider } from '../search_strategy_provider'; + import { asyncSearchServiceStateProvider } from './async_search_service_state'; -export const asyncSearchServiceProvider = ( +export const latencyCorrelationsAsyncSearchServiceProvider: AsyncSearchServiceProvider = ( esClient: ElasticsearchClient, getApmIndices: () => Promise, searchServiceParams: SearchServiceParams, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts index 32ac034eef22d..3bfe67b0e1e0d 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts @@ -8,7 +8,7 @@ import type { AsyncSearchProviderProgress, SearchServiceValue, -} from '../../../../common/search_strategies/correlations/types'; +} from '../../../../common/search_strategies/latency_correlations/types'; import type { HistogramItem } from '../queries'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/index.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/index.ts index 64c666d98efb7..0eea704edc3e0 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/index.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/index.ts @@ -5,4 +5,4 @@ * 2.0. */ -export { asyncSearchServiceProvider } from './async_search_service'; +export { latencyCorrelationsAsyncSearchServiceProvider } from './async_search_service'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts index b4246ac191e83..d4f9293522467 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts @@ -10,7 +10,7 @@ import { getOrElse } from 'fp-ts/lib/Either'; import { pipe } from 'fp-ts/lib/pipeable'; import * as t from 'io-ts'; import { failure } from 'io-ts/lib/PathReporter'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; import { rangeRt } from '../../../routes/default_api_types'; import { getCorrelationsFilters } from '../../correlations/get_filters'; import { Setup, SetupTimeRange } from '../../helpers/setup_request'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.ts index d3547a0ea546d..d4c4453376afb 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.ts @@ -5,7 +5,7 @@ * 2.0. */ -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; export const getRequestBase = ({ index, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts index fd17e2b559694..4e677b69c3440 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts @@ -10,7 +10,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts index 7a2efd5ce4d27..305574d589fee 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts @@ -6,7 +6,7 @@ */ import { estypes } from '@elastic/elasticsearch'; import { ElasticsearchClient } from 'kibana/server'; -import { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; +import { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; import { EVENT_OUTCOME } from '../../../../common/elasticsearch_fieldnames'; import { EventOutcome } from '../../../../common/event_outcome'; import { getQueryWithParams, getTermsQuery } from './get_query_with_params'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts index e1762e888eae3..2e83b85488b2e 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts @@ -9,7 +9,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; import { FIELD_PREFIX_TO_EXCLUDE_AS_CANDIDATE, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.test.ts index 0d47302464727..55cc23de764fe 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.test.ts @@ -10,7 +10,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; -import { asyncSearchServiceLogProvider } from '../latency_correlations/async_search_service_log'; +import { asyncSearchServiceLogProvider } from '../async_search_service_log'; import { asyncSearchServiceStateProvider } from '../latency_correlations/async_search_service_state'; import { diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts index e8b3f6407e1af..f1c7a8564f42a 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts @@ -9,9 +9,9 @@ import type { ElasticsearchClient } from 'src/core/server'; import type { estypes } from '@elastic/elasticsearch'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; -import type { AsyncSearchServiceLog } from '../latency_correlations/async_search_service_log'; +import type { AsyncSearchServiceLog } from '../async_search_service_log'; import type { AsyncSearchServiceState } from '../latency_correlations/async_search_service_state'; import { TERMS_SIZE } from '../constants'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.ts index 64b5bd862ad6d..59e9a1298f39e 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.ts @@ -8,7 +8,7 @@ import { ElasticsearchClient } from 'kibana/server'; import { estypes } from '@elastic/elasticsearch'; -import { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; +import { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; import { getQueryWithParams } from './get_query_with_params'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts index a5875c78072a2..3b7cb9fabc859 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts @@ -14,7 +14,7 @@ import type { HistogramItem, ResponseHit, SearchServiceFetchParams, -} from '../../../../common/search_strategies/correlations/types'; +} from '../../../../common/search_strategies/latency_correlations/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.ts index af5f990f19bab..9551f7aaab4ee 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.ts @@ -10,7 +10,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.ts index b9f591885afc7..f9645c7b184cd 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.ts @@ -12,7 +12,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.test.ts index a436920aa3c9d..1de8e83e46beb 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.test.ts @@ -10,7 +10,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; -import { asyncSearchServiceLogProvider } from '../latency_correlations/async_search_service_log'; +import { asyncSearchServiceLogProvider } from '../async_search_service_log'; import { asyncSearchServiceStateProvider } from '../latency_correlations/async_search_service_state'; import { fetchTransactionDurationHistograms } from './query_histograms_generator'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts index 3c6b472b4ebd8..3a96c78db90fd 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts @@ -9,9 +9,9 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; -import type { AsyncSearchServiceLog } from '../latency_correlations/async_search_service_log'; +import type { AsyncSearchServiceLog } from '../async_search_service_log'; import type { AsyncSearchServiceState } from '../latency_correlations/async_search_service_state'; import { CORRELATION_THRESHOLD, KS_TEST_THRESHOLD } from '../constants'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts index c88b9f83fac55..72577a380f4fd 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts @@ -10,7 +10,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts index 840ddf97421c2..7591143646b34 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts @@ -10,7 +10,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/register_search_strategies.ts b/x-pack/plugins/apm/server/lib/search_strategies/register_search_strategies.ts index 83ec719abf055..8a6dda0c4e0f5 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/register_search_strategies.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/register_search_strategies.ts @@ -13,8 +13,8 @@ import type { ApmIndicesConfig } from '../settings/apm_indices/get_apm_indices'; import { searchStrategyProvider } from './search_strategy_provider'; -import { asyncSearchServiceProvider } from './latency_correlations'; -import { asyncErrorCorrelationSearchServiceProvider } from './failed_transactions_correlations'; +import { latencyCorrelationsAsyncSearchServiceProvider } from './latency_correlations'; +import { failedTransactionsCorrelationsAsyncSearchServiceProvider } from './failed_transactions_correlations'; export const registerSearchStrategies = ( registerSearchStrategy: DataPluginSetup['search']['registerSearchStrategy'], @@ -24,7 +24,7 @@ export const registerSearchStrategies = ( registerSearchStrategy( APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, searchStrategyProvider( - asyncSearchServiceProvider, + latencyCorrelationsAsyncSearchServiceProvider, getApmIndices, includeFrozen ) @@ -33,7 +33,7 @@ export const registerSearchStrategies = ( registerSearchStrategy( APM_SEARCH_STRATEGIES.APM_FAILED_TRANSACTIONS_CORRELATIONS, searchStrategyProvider( - asyncErrorCorrelationSearchServiceProvider, + failedTransactionsCorrelationsAsyncSearchServiceProvider, getApmIndices, includeFrozen ) diff --git a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts index 9570a74271690..43bb77d70c4ba 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts @@ -12,7 +12,7 @@ import { ENVIRONMENT_ALL } from '../../../common/environment_filter_values'; import type { ApmIndicesConfig } from '../settings/apm_indices/get_apm_indices'; -import { asyncSearchServiceProvider } from './latency_correlations'; +import { latencyCorrelationsAsyncSearchServiceProvider } from './latency_correlations'; import { searchStrategyProvider, @@ -109,7 +109,7 @@ describe('APM Correlations search strategy', () => { describe('strategy interface', () => { it('returns a custom search strategy with a `search` and `cancel` function', async () => { const searchStrategy = await searchStrategyProvider( - asyncSearchServiceProvider, + latencyCorrelationsAsyncSearchServiceProvider, getApmIndicesMock, false ); @@ -149,7 +149,7 @@ describe('APM Correlations search strategy', () => { describe('when no params are provided', () => { it('throws an error', async () => { const searchStrategy = await searchStrategyProvider( - asyncSearchServiceProvider, + latencyCorrelationsAsyncSearchServiceProvider, mockGetApmIndicesMock, false ); @@ -165,7 +165,7 @@ describe('APM Correlations search strategy', () => { describe('when no ID is provided', () => { it('performs a client search with params', async () => { const searchStrategy = await searchStrategyProvider( - asyncSearchServiceProvider, + latencyCorrelationsAsyncSearchServiceProvider, mockGetApmIndicesMock, false ); @@ -212,7 +212,7 @@ describe('APM Correlations search strategy', () => { describe('when an ID with params is provided', () => { it('retrieves the current request', async () => { const searchStrategy = await searchStrategyProvider( - asyncSearchServiceProvider, + latencyCorrelationsAsyncSearchServiceProvider, mockGetApmIndicesMock, false ); @@ -239,7 +239,7 @@ describe('APM Correlations search strategy', () => { .mockReset() .mockRejectedValueOnce(new Error('client error')); const searchStrategy = await searchStrategyProvider( - asyncSearchServiceProvider, + latencyCorrelationsAsyncSearchServiceProvider, mockGetApmIndicesMock, false ); @@ -258,7 +258,7 @@ describe('APM Correlations search strategy', () => { it('triggers the subscription only once', async () => { expect.assertions(2); const searchStrategy = await searchStrategyProvider( - asyncSearchServiceProvider, + latencyCorrelationsAsyncSearchServiceProvider, mockGetApmIndicesMock, false ); @@ -276,7 +276,7 @@ describe('APM Correlations search strategy', () => { describe('response', () => { it('sends an updated response on consecutive search calls', async () => { const searchStrategy = await searchStrategyProvider( - asyncSearchServiceProvider, + latencyCorrelationsAsyncSearchServiceProvider, mockGetApmIndicesMock, false ); diff --git a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts index d6618ed168dbc..e80fe3f2f5d0a 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts @@ -16,7 +16,7 @@ import { IKibanaSearchResponse, } from '../../../../../../src/plugins/data/common'; -import type { SearchServiceParams } from '../../../common/search_strategies/correlations/types'; +import type { SearchServiceParams } from '../../../common/search_strategies/latency_correlations/types'; import type { ApmIndicesConfig } from '../settings/apm_indices/get_apm_indices'; export type PartialSearchRequest = IKibanaSearchRequest; @@ -42,7 +42,7 @@ type GetAsyncSearchServiceState< RawResponse extends RawResponseBase > = () => AsyncSearchServiceState; -type AsyncSearchServiceProvider = ( +export type AsyncSearchServiceProvider = ( esClient: ElasticsearchClient, getApmIndices: () => Promise, searchServiceParams: SearchServiceParams, From 64cc796eb261a78cea4dbbc5d49f8ec0bb7c026c Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Mon, 30 Aug 2021 17:10:11 +0200 Subject: [PATCH 05/37] [ML] Fix naming inconsistencies. --- .../failed_transactions_correlations/types.ts | 8 ++++++ .../latency_correlations/types.ts | 24 +++------------- .../apm/common/search_strategies/types.ts | 28 +++++++++++++++++++ .../latency_correlations.test.tsx | 4 +-- .../distribution/index.test.tsx | 4 +-- ...ailed_transactions_correlations_fetcher.ts | 2 +- .../use_transaction_distribution_fetcher.ts | 22 +++++++-------- ...ransaction_latency_correlations_fetcher.ts | 24 ++++++++-------- .../async_search_service.ts | 10 +++++-- .../async_search_service.ts | 8 +++--- .../queries/get_request_base.ts | 2 +- .../search_strategy_provider.ts | 8 ++---- 12 files changed, 82 insertions(+), 62 deletions(-) create mode 100644 x-pack/plugins/apm/common/search_strategies/types.ts diff --git a/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts index 2b0d2b5642e0c..0929a18a30d04 100644 --- a/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts @@ -5,6 +5,8 @@ * 2.0. */ +import { RawResponseBase } from '../types'; + import { FAILED_TRANSACTIONS_IMPACT_THRESHOLD } from './constants'; export interface FailedTransactionsCorrelationValue { @@ -26,3 +28,9 @@ export interface CorrelationsTerm { fieldName: string; fieldValue: string; } + +export interface FailedTransactionsCorrelationsAsyncSearchServiceRawResponse + extends RawResponseBase { + log: string[]; + values: FailedTransactionsCorrelationValue[]; +} diff --git a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts index 886c5fd6161d8..ffb9b10897896 100644 --- a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts @@ -5,6 +5,8 @@ * 2.0. */ +import { RawResponseBase } from '../types'; + export interface HistogramItem { key: number; doc_count: number; @@ -18,23 +20,6 @@ export interface ResponseHit { _source: ResponseHitSource; } -export interface SearchServiceParams { - environment: string; - kuery: string; - serviceName?: string; - transactionName?: string; - transactionType?: string; - start?: string; - end?: string; - percentileThreshold?: number; - analyzeCorrelations?: boolean; -} - -export interface SearchServiceFetchParams extends SearchServiceParams { - index: string; - includeFrozen?: boolean; -} - export interface SearchServiceValue { histogram: HistogramItem[]; value: string; @@ -53,11 +38,10 @@ export interface AsyncSearchProviderProgress { loadedHistograms: number; } -export interface SearchServiceRawResponse { - ccsWarning: boolean; +export interface LatencyCorrelationsAsyncSearchServiceRawResponse + extends RawResponseBase { log: string[]; overallHistogram?: HistogramItem[]; percentileThresholdValue?: number; - took: number; values: SearchServiceValue[]; } diff --git a/x-pack/plugins/apm/common/search_strategies/types.ts b/x-pack/plugins/apm/common/search_strategies/types.ts new file mode 100644 index 0000000000000..a5e2cb3c22048 --- /dev/null +++ b/x-pack/plugins/apm/common/search_strategies/types.ts @@ -0,0 +1,28 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export interface RawResponseBase { + ccsWarning: boolean; + took: number; +} + +export interface SearchServiceParams { + environment: string; + kuery: string; + serviceName?: string; + transactionName?: string; + transactionType?: string; + start?: string; + end?: string; + percentileThreshold?: number; + analyzeCorrelations?: boolean; +} + +export interface SearchServiceFetchParams extends SearchServiceParams { + index: string; + includeFrozen?: boolean; +} diff --git a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.test.tsx b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.test.tsx index ea08e7ebd4926..2729c62099259 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.test.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.test.tsx @@ -18,7 +18,7 @@ import { dataPluginMock } from 'src/plugins/data/public/mocks'; import type { IKibanaSearchResponse } from 'src/plugins/data/public'; import { EuiThemeProvider } from 'src/plugins/kibana_react/common'; import { createKibanaReactContext } from 'src/plugins/kibana_react/public'; -import type { SearchServiceRawResponse } from '../../../../common/search_strategies/latency_correlations/types'; +import type { LatencyCorrelationsAsyncSearchServiceRawResponse } from '../../../../common/search_strategies/latency_correlations/types'; import { MockUrlParamsContextProvider } from '../../../context/url_params_context/mock_url_params_context_provider'; import { ApmPluginContextValue } from '../../../context/apm_plugin/apm_plugin_context'; import { @@ -34,7 +34,7 @@ function Wrapper({ dataSearchResponse, }: { children?: ReactNode; - dataSearchResponse: IKibanaSearchResponse; + dataSearchResponse: IKibanaSearchResponse; }) { const mockDataSearch = jest.fn(() => of(dataSearchResponse)); diff --git a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx index db9babb830581..7fee1fc94f8d0 100644 --- a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx +++ b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx @@ -16,7 +16,7 @@ import { dataPluginMock } from 'src/plugins/data/public/mocks'; import type { IKibanaSearchResponse } from 'src/plugins/data/public'; import { EuiThemeProvider } from 'src/plugins/kibana_react/common'; import { createKibanaReactContext } from 'src/plugins/kibana_react/public'; -import type { SearchServiceRawResponse } from '../../../../../common/search_strategies/latency_correlations/types'; +import type { LatencyCorrelationsAsyncSearchServiceRawResponse } from '../../../../../common/search_strategies/latency_correlations/types'; import { MockUrlParamsContextProvider } from '../../../../context/url_params_context/mock_url_params_context_provider'; import { ApmPluginContextValue } from '../../../../context/apm_plugin/apm_plugin_context'; import { @@ -32,7 +32,7 @@ function Wrapper({ dataSearchResponse, }: { children?: ReactNode; - dataSearchResponse: IKibanaSearchResponse; + dataSearchResponse: IKibanaSearchResponse; }) { const mockDataSearch = jest.fn(() => of(dataSearchResponse)); diff --git a/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts b/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts index 7ba7c8adbe702..53f3975210684 100644 --- a/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts +++ b/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts @@ -13,7 +13,7 @@ import { isCompleteResponse, isErrorResponse, } from '../../../../../src/plugins/data/public'; -import type { SearchServiceParams } from '../../common/search_strategies/latency_correlations/types'; +import type { SearchServiceParams } from '../../common/search_strategies/types'; import { useKibana } from '../../../../../src/plugins/kibana_react/public'; import type { ApmPluginStartDeps } from '../plugin'; import type { FailedTransactionsCorrelationValue } from '../../common/search_strategies/failed_transactions_correlations/types'; diff --git a/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts b/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts index cbbad93742b9f..3f877a853ee26 100644 --- a/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts +++ b/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts @@ -13,10 +13,8 @@ import { isCompleteResponse, isErrorResponse, } from '../../../../../src/plugins/data/public'; -import type { - SearchServiceParams, - SearchServiceRawResponse, -} from '../../common/search_strategies/latency_correlations/types'; +import type { SearchServiceParams } from '../../common/search_strategies/types'; +import type { LatencyCorrelationsAsyncSearchServiceRawResponse } from '../../common/search_strategies/latency_correlations/types'; import { useKibana } from '../../../../../src/plugins/kibana_react/public'; import { APM_SEARCH_STRATEGIES } from '../../common/search_strategies/constants'; import { ApmPluginStartDeps } from '../plugin'; @@ -26,10 +24,10 @@ interface TransactionDistributionFetcherState { isComplete: boolean; isRunning: boolean; loaded: number; - ccsWarning: SearchServiceRawResponse['ccsWarning']; - log: SearchServiceRawResponse['log']; - transactionDistribution?: SearchServiceRawResponse['overallHistogram']; - percentileThresholdValue?: SearchServiceRawResponse['percentileThresholdValue']; + ccsWarning: LatencyCorrelationsAsyncSearchServiceRawResponse['ccsWarning']; + log: LatencyCorrelationsAsyncSearchServiceRawResponse['log']; + transactionDistribution?: LatencyCorrelationsAsyncSearchServiceRawResponse['overallHistogram']; + percentileThresholdValue?: LatencyCorrelationsAsyncSearchServiceRawResponse['percentileThresholdValue']; timeTook?: number; total: number; } @@ -55,7 +53,7 @@ export function useTransactionDistributionFetcher() { const searchSubscription$ = useRef(); function setResponse( - response: IKibanaSearchResponse + response: IKibanaSearchResponse ) { setFetchState((prevState) => ({ ...prevState, @@ -106,13 +104,15 @@ export function useTransactionDistributionFetcher() { searchSubscription$.current = data.search .search< IKibanaSearchRequest, - IKibanaSearchResponse + IKibanaSearchResponse >(req, { strategy: APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, abortSignal: abortCtrl.current.signal, }) .subscribe({ - next: (res: IKibanaSearchResponse) => { + next: ( + res: IKibanaSearchResponse + ) => { setResponse(res); if (isCompleteResponse(res)) { searchSubscription$.current?.unsubscribe(); diff --git a/x-pack/plugins/apm/public/hooks/use_transaction_latency_correlations_fetcher.ts b/x-pack/plugins/apm/public/hooks/use_transaction_latency_correlations_fetcher.ts index 43ef683e266dd..cf3c4b054c3fb 100644 --- a/x-pack/plugins/apm/public/hooks/use_transaction_latency_correlations_fetcher.ts +++ b/x-pack/plugins/apm/public/hooks/use_transaction_latency_correlations_fetcher.ts @@ -13,10 +13,8 @@ import { isCompleteResponse, isErrorResponse, } from '../../../../../src/plugins/data/public'; -import type { - SearchServiceParams, - SearchServiceRawResponse, -} from '../../common/search_strategies/latency_correlations/types'; +import type { SearchServiceParams } from '../../common/search_strategies/types'; +import type { LatencyCorrelationsAsyncSearchServiceRawResponse } from '../../common/search_strategies/latency_correlations/types'; import { useKibana } from '../../../../../src/plugins/kibana_react/public'; import { APM_SEARCH_STRATEGIES } from '../../common/search_strategies/constants'; import { ApmPluginStartDeps } from '../plugin'; @@ -26,11 +24,11 @@ interface TransactionLatencyCorrelationsFetcherState { isComplete: boolean; isRunning: boolean; loaded: number; - ccsWarning: SearchServiceRawResponse['ccsWarning']; - histograms: SearchServiceRawResponse['values']; - log: SearchServiceRawResponse['log']; - overallHistogram?: SearchServiceRawResponse['overallHistogram']; - percentileThresholdValue?: SearchServiceRawResponse['percentileThresholdValue']; + ccsWarning: LatencyCorrelationsAsyncSearchServiceRawResponse['ccsWarning']; + histograms: LatencyCorrelationsAsyncSearchServiceRawResponse['values']; + log: LatencyCorrelationsAsyncSearchServiceRawResponse['log']; + overallHistogram?: LatencyCorrelationsAsyncSearchServiceRawResponse['overallHistogram']; + percentileThresholdValue?: LatencyCorrelationsAsyncSearchServiceRawResponse['percentileThresholdValue']; timeTook?: number; total: number; } @@ -57,7 +55,7 @@ export const useTransactionLatencyCorrelationsFetcher = () => { const searchSubscription$ = useRef(); function setResponse( - response: IKibanaSearchResponse + response: IKibanaSearchResponse ) { setFetchState((prevState) => ({ ...prevState, @@ -108,13 +106,15 @@ export const useTransactionLatencyCorrelationsFetcher = () => { searchSubscription$.current = data.search .search< IKibanaSearchRequest, - IKibanaSearchResponse + IKibanaSearchResponse >(req, { strategy: APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, abortSignal: abortCtrl.current.signal, }) .subscribe({ - next: (res: IKibanaSearchResponse) => { + next: ( + res: IKibanaSearchResponse + ) => { setResponse(res); if (isCompleteResponse(res)) { searchSubscription$.current?.unsubscribe(); diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts index 3116ae587bd87..d1165881c37c2 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts @@ -13,17 +13,21 @@ import { EVENT_OUTCOME } from '../../../../common/elasticsearch_fieldnames'; import type { SearchServiceParams, SearchServiceFetchParams, -} from '../../../../common/search_strategies/latency_correlations/types'; +} from '../../../../common/search_strategies/types'; +import type { FailedTransactionsCorrelationsAsyncSearchServiceRawResponse } from '../../../../common/search_strategies/failed_transactions_correlations/types'; import type { ApmIndicesConfig } from '../../settings/apm_indices/get_apm_indices'; import { asyncSearchServiceLogProvider } from '../async_search_service_log'; -import { asyncErrorCorrelationsSearchServiceStateProvider } from './async_search_service_state'; import { fetchFailedTransactionsCorrelationPValues, fetchTransactionDurationFieldCandidates, } from '../queries'; +import type { AsyncSearchServiceProvider } from '../search_strategy_provider'; + +import { asyncErrorCorrelationsSearchServiceStateProvider } from './async_search_service_state'; + import { ERROR_CORRELATION_THRESHOLD } from '../constants'; -export const failedTransactionsCorrelationsAsyncSearchServiceProvider = ( +export const failedTransactionsCorrelationsAsyncSearchServiceProvider: AsyncSearchServiceProvider = ( esClient: ElasticsearchClient, getApmIndices: () => Promise, searchServiceParams: SearchServiceParams, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts index a2566add7447f..54ce5cfff8e72 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts @@ -9,9 +9,9 @@ import { range } from 'lodash'; import type { ElasticsearchClient } from 'src/core/server'; import type { SearchServiceParams, - SearchServiceRawResponse, SearchServiceFetchParams, -} from '../../../../common/search_strategies/latency_correlations/types'; +} from '../../../../common/search_strategies/types'; +import type { LatencyCorrelationsAsyncSearchServiceRawResponse } from '../../../../common/search_strategies/latency_correlations/types'; import type { ApmIndicesConfig } from '../../settings/apm_indices/get_apm_indices'; @@ -30,7 +30,7 @@ import type { AsyncSearchServiceProvider } from '../search_strategy_provider'; import { asyncSearchServiceStateProvider } from './async_search_service_state'; -export const latencyCorrelationsAsyncSearchServiceProvider: AsyncSearchServiceProvider = ( +export const latencyCorrelationsAsyncSearchServiceProvider: AsyncSearchServiceProvider = ( esClient: ElasticsearchClient, getApmIndices: () => Promise, searchServiceParams: SearchServiceParams, @@ -242,7 +242,7 @@ export const latencyCorrelationsAsyncSearchServiceProvider: AsyncSearchServicePr isPartial: isRunning, }; - const rawResponse: SearchServiceRawResponse = { + const rawResponse: LatencyCorrelationsAsyncSearchServiceRawResponse = { ccsWarning, log: getLogMessages(), took, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.ts index d4c4453376afb..cedda3ed633fe 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.ts @@ -5,7 +5,7 @@ * 2.0. */ -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; export const getRequestBase = ({ index, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts index e80fe3f2f5d0a..17d618e33e5c3 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts @@ -16,16 +16,12 @@ import { IKibanaSearchResponse, } from '../../../../../../src/plugins/data/common'; -import type { SearchServiceParams } from '../../../common/search_strategies/latency_correlations/types'; +import type { SearchServiceParams } from '../../../common/search_strategies/types'; +import type { RawResponseBase } from '../../../common/search_strategies/types'; import type { ApmIndicesConfig } from '../settings/apm_indices/get_apm_indices'; export type PartialSearchRequest = IKibanaSearchRequest; -interface RawResponseBase { - ccsWarning: boolean; - took: number; -} - interface AsyncSearchServiceState { cancel: () => void; error: Error; From ade9431c4a30f083dc883ce34aafc829aed3d6ab Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Mon, 30 Aug 2021 17:20:33 +0200 Subject: [PATCH 06/37] [ML] Fix naming inconsistencies. --- .../async_search_service.ts | 34 +++++++------- .../async_search_service_state.ts | 10 ++--- .../async_search_service.ts | 44 +++++++++---------- 3 files changed, 40 insertions(+), 48 deletions(-) diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts index d1165881c37c2..aa7861f11214a 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts @@ -23,7 +23,7 @@ import { } from '../queries'; import type { AsyncSearchServiceProvider } from '../search_strategy_provider'; -import { asyncErrorCorrelationsSearchServiceStateProvider } from './async_search_service_state'; +import { asyncFailedTransactionsCorrelationsSearchServiceStateProvider } from './async_search_service_state'; import { ERROR_CORRELATION_THRESHOLD } from '../constants'; @@ -35,7 +35,7 @@ export const failedTransactionsCorrelationsAsyncSearchServiceProvider: AsyncSear ) => { const { addLogMessage, getLogMessages } = asyncSearchServiceLogProvider(); - const state = asyncErrorCorrelationsSearchServiceStateProvider(); + const state = asyncFailedTransactionsCorrelationsSearchServiceStateProvider(); async function fetchErrorCorrelations() { try { @@ -97,7 +97,7 @@ export const failedTransactionsCorrelationsAsyncSearchServiceProvider: AsyncSear } finally { fieldCandidatesFetchedCount += batches[i].length; state.setProgress({ - loadedErrorCorrelations: + loadedFailedTransactionsCorrelations: fieldCandidatesFetchedCount / fieldCandidates.length, }); } @@ -125,28 +125,24 @@ export const failedTransactionsCorrelationsAsyncSearchServiceProvider: AsyncSear return () => { const { ccsWarning, error, isRunning, progress } = state.getState(); - const meta = { - loaded: Math.round(state.getOverallProgress() * 100), - total: 100, - isRunning, - isPartial: isRunning, - }; - - const rawResponse = { - ccsWarning, - log: getLogMessages(), - took: Date.now() - progress.started, - values: state.getValuesSortedByScore(), - }; - return { cancel: () => { addLogMessage(`Service cancelled.`); state.setIsCancelled(true); }, error, - meta, - rawResponse, + meta: { + loaded: Math.round(state.getOverallProgress() * 100), + total: 100, + isRunning, + isPartial: isRunning, + }, + rawResponse: { + ccsWarning, + log: getLogMessages(), + took: Date.now() - progress.started, + values: state.getValuesSortedByScore(), + }, }; }; }; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service_state.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service_state.ts index cbb90371da352..0e99b90e9f3c2 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service_state.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service_state.ts @@ -10,9 +10,9 @@ import { FailedTransactionsCorrelationValue } from '../../../../common/search_st interface Progress { started: number; loadedFieldCandidates: number; - loadedErrorCorrelations: number; + loadedFailedTransactionsCorrelations: number; } -export const asyncErrorCorrelationsSearchServiceStateProvider = () => { +export const asyncFailedTransactionsCorrelationsSearchServiceStateProvider = () => { let ccsWarning = false; function setCcsWarning(d: boolean) { ccsWarning = d; @@ -36,12 +36,12 @@ export const asyncErrorCorrelationsSearchServiceStateProvider = () => { let progress: Progress = { started: Date.now(), loadedFieldCandidates: 0, - loadedErrorCorrelations: 0, + loadedFailedTransactionsCorrelations: 0, }; function getOverallProgress() { return ( progress.loadedFieldCandidates * 0.025 + - progress.loadedErrorCorrelations * (1 - 0.025) + progress.loadedFailedTransactionsCorrelations * (1 - 0.025) ); } function setProgress(d: Partial>) { @@ -89,5 +89,5 @@ export const asyncErrorCorrelationsSearchServiceStateProvider = () => { }; export type AsyncSearchServiceState = ReturnType< - typeof asyncErrorCorrelationsSearchServiceStateProvider + typeof asyncFailedTransactionsCorrelationsSearchServiceStateProvider >; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts index 54ce5cfff8e72..c66238b3b317b 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts @@ -221,6 +221,11 @@ export const latencyCorrelationsAsyncSearchServiceProvider: AsyncSearchServicePr state.setIsRunning(false); } + function cancel() { + addLogMessage(`Service cancelled.`); + state.setIsCancelled(true); + } + fetchCorrelations(); return () => { @@ -233,32 +238,23 @@ export const latencyCorrelationsAsyncSearchServiceProvider: AsyncSearchServicePr progress, } = state.getState(); - const took = Date.now() - progress.started; - - const meta = { - loaded: Math.round(state.getOverallProgress() * 100), - total: 100, - isRunning, - isPartial: isRunning, - }; - - const rawResponse: LatencyCorrelationsAsyncSearchServiceRawResponse = { - ccsWarning, - log: getLogMessages(), - took, - values: state.getValuesSortedByCorrelation(), - percentileThresholdValue, - overallHistogram, - }; - return { - cancel: () => { - addLogMessage(`Service cancelled.`); - state.setIsCancelled(true); - }, + cancel, error, - meta, - rawResponse, + meta: { + loaded: Math.round(state.getOverallProgress() * 100), + total: 100, + isRunning, + isPartial: isRunning, + }, + rawResponse: { + ccsWarning, + log: getLogMessages(), + took: Date.now() - progress.started, + values: state.getValuesSortedByCorrelation(), + percentileThresholdValue, + overallHistogram, + }, }; }; }; From be265ee87a0f7725f233c5dc830bf8f8d6945d88 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Tue, 31 Aug 2021 12:39:32 +0200 Subject: [PATCH 07/37] [ML] Consolidate hooks. --- .../apm/common/search_strategies/constants.ts | 1 + .../latency_correlations/types.ts | 2 +- .../app/correlations/latency_correlations.tsx | 28 +-- .../distribution/index.tsx | 5 +- ...ailed_transactions_correlations_fetcher.ts | 2 - .../apm/public/hooks/use_search_strategy.ts | 126 +++++++++++++ .../use_transaction_distribution_fetcher.ts | 155 +++------------- ...ransaction_latency_correlations_fetcher.ts | 168 ------------------ 8 files changed, 175 insertions(+), 312 deletions(-) create mode 100644 x-pack/plugins/apm/public/hooks/use_search_strategy.ts delete mode 100644 x-pack/plugins/apm/public/hooks/use_transaction_latency_correlations_fetcher.ts diff --git a/x-pack/plugins/apm/common/search_strategies/constants.ts b/x-pack/plugins/apm/common/search_strategies/constants.ts index 57f78a9ee6564..45b24a28f7e04 100644 --- a/x-pack/plugins/apm/common/search_strategies/constants.ts +++ b/x-pack/plugins/apm/common/search_strategies/constants.ts @@ -9,3 +9,4 @@ export const APM_SEARCH_STRATEGIES = { APM_FAILED_TRANSACTIONS_CORRELATIONS: 'apmFailedTransactionsCorrelations', APM_LATENCY_CORRELATIONS: 'apmLatencyCorrelations', } as const; +export type ApmSearchStrategies = typeof APM_SEARCH_STRATEGIES[keyof typeof APM_SEARCH_STRATEGIES]; diff --git a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts index ffb9b10897896..889fa7a682350 100644 --- a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts @@ -43,5 +43,5 @@ export interface LatencyCorrelationsAsyncSearchServiceRawResponse log: string[]; overallHistogram?: HistogramItem[]; percentileThresholdValue?: number; - values: SearchServiceValue[]; + values?: SearchServiceValue[]; } diff --git a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx index ad8a56a3ac6f9..2ecfff27c1b8c 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx @@ -23,7 +23,7 @@ import { EuiTableSortingType } from '@elastic/eui/src/components/basic_table/tab import { useUrlParams } from '../../../context/url_params_context/use_url_params'; import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; import { FETCH_STATUS } from '../../../hooks/use_fetcher'; -import { useTransactionLatencyCorrelationsFetcher } from '../../../hooks/use_transaction_latency_correlations_fetcher'; +import { useTransactionDistributionFetcher } from '../../../hooks/use_transaction_distribution_fetcher'; import { TransactionDistributionChart } from '../../shared/charts/transaction_distribution_chart'; import { CorrelationsTable } from './correlations_table'; import { push } from '../../shared/Links/url_helpers'; @@ -75,14 +75,14 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { ccsWarning, log, error, - histograms, + values, percentileThresholdValue, isRunning, progress, startFetch, cancelFetch, overallHistogram, - } = useTransactionLatencyCorrelationsFetcher(); + } = useTransactionDistributionFetcher(); const startFetchHandler = useCallback(() => { startFetch({ @@ -94,6 +94,7 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { start, end, percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD, + analyzeCorrelations: true, }); }, [ startFetch, @@ -131,17 +132,22 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { ] = useState(null); const selectedHistogram = useMemo(() => { - let selected = histograms.length > 0 ? histograms[0] : undefined; - - if (histograms.length > 0 && selectedSignificantTerm !== null) { - selected = histograms.find( + let selected = + Array.isArray(values) && values.length > 0 ? values[0] : undefined; + + if ( + Array.isArray(values) && + values.length > 0 && + selectedSignificantTerm !== null + ) { + selected = values.find( (h) => h.field === selectedSignificantTerm.fieldName && h.value === selectedSignificantTerm.fieldValue ); } return selected; - }, [histograms, selectedSignificantTerm]); + }, [values, selectedSignificantTerm]); const history = useHistory(); const trackApmEvent = useUiTracker({ app: 'apm' }); @@ -269,11 +275,11 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { }, []); const { histogramTerms, sorting } = useMemo(() => { - if (!Array.isArray(histograms)) { + if (!Array.isArray(values)) { return { histogramTerms: [], sorting: undefined }; } const orderedTerms = orderBy( - histograms.map((d) => { + values.map((d) => { return { fieldName: d.field, fieldValue: d.value, @@ -295,7 +301,7 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { }, } as EuiTableSortingType, }; - }, [histograms, sortField, sortDirection]); + }, [values, sortField, sortDirection]); return (
diff --git a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx index 2da61bc0fc555..4b80295d9a6cb 100644 --- a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx +++ b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx @@ -110,7 +110,7 @@ export function TransactionDistribution({ percentileThresholdValue, startFetch, cancelFetch, - transactionDistribution, + overallHistogram, } = useTransactionDistributionFetcher(); const startFetchHandler = useCallback(() => { @@ -123,6 +123,7 @@ export function TransactionDistribution({ start, end, percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD, + analyzeCorrelations: false, }); }, [ startFetch, @@ -232,7 +233,7 @@ export function TransactionDistribution({ markerCurrentTransaction={markerCurrentTransaction} markerPercentile={DEFAULT_PERCENTILE_THRESHOLD} markerValue={percentileThresholdValue ?? 0} - overallHistogram={transactionDistribution} + overallHistogram={overallHistogram} onChartSelection={onTrackedChartSelection} onHasData={onTransactionDistributionHasData} selection={selection} diff --git a/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts b/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts index 53f3975210684..45598a6775d10 100644 --- a/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts +++ b/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts @@ -34,7 +34,6 @@ interface FailedTransactionsCorrelationsFetcherState { ccsWarning: RawResponse['ccsWarning']; values: RawResponse['values']; log: RawResponse['log']; - timeTook?: number; total: number; } @@ -68,7 +67,6 @@ export const useFailedTransactionsCorrelationsFetcher = () => { log: response.rawResponse?.log ?? [], loaded: response.loaded!, total: response.total!, - timeTook: response.rawResponse.took, })); } diff --git a/x-pack/plugins/apm/public/hooks/use_search_strategy.ts b/x-pack/plugins/apm/public/hooks/use_search_strategy.ts new file mode 100644 index 0000000000000..0a98b0208709a --- /dev/null +++ b/x-pack/plugins/apm/public/hooks/use_search_strategy.ts @@ -0,0 +1,126 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { useCallback, useRef, useState } from 'react'; +import type { Subscription } from 'rxjs'; +import { + IKibanaSearchRequest, + IKibanaSearchResponse, + isCompleteResponse, + isErrorResponse, +} from '../../../../../src/plugins/data/public'; +import type { SearchServiceParams } from '../../common/search_strategies/types'; +import type { RawResponseBase } from '../../common/search_strategies/types'; +import { useKibana } from '../../../../../src/plugins/kibana_react/public'; +import { ApmSearchStrategies } from '../../common/search_strategies/constants'; +import { ApmPluginStartDeps } from '../plugin'; + +interface SearchStrategyFetcherState { + error?: Error; + isComplete: boolean; + isRunning: boolean; + loaded: number; + total: number; +} + +export function useSearchStrategy( + searchStrategyName: ApmSearchStrategies +) { + const { + services: { data }, + } = useKibana(); + + const [rawResponse, setRawResponse] = useState({ + ccsWarning: false, + took: 0, + } as RawResponse); + + const [fetchState, setFetchState] = useState({ + isComplete: false, + isRunning: false, + loaded: 0, + total: 100, + }); + + const abortCtrl = useRef(new AbortController()); + const searchSubscription$ = useRef(); + + const startFetch = useCallback( + (params: SearchServiceParams) => { + setFetchState((prevState) => ({ + ...prevState, + error: undefined, + isComplete: false, + })); + searchSubscription$.current?.unsubscribe(); + abortCtrl.current.abort(); + abortCtrl.current = new AbortController(); + + const req = { params }; + + // Submit the search request using the `data.search` service. + searchSubscription$.current = data.search + .search>(req, { + strategy: searchStrategyName, + abortSignal: abortCtrl.current.signal, + }) + .subscribe({ + next: (res: IKibanaSearchResponse) => { + setRawResponse(res.rawResponse); + setFetchState((prevState) => ({ + ...prevState, + isRunning: res.isRunning || false, + loaded: res.loaded!, + total: res.total!, + })); + + if (isCompleteResponse(res)) { + searchSubscription$.current?.unsubscribe(); + setFetchState((prevState) => ({ + ...prevState, + isRunnning: false, + isComplete: true, + })); + } else if (isErrorResponse(res)) { + searchSubscription$.current?.unsubscribe(); + setFetchState((prevState) => ({ + ...prevState, + error: (res as unknown) as Error, + isRunning: false, + })); + } + }, + error: (error: Error) => { + setFetchState((prevState) => ({ + ...prevState, + error, + isRunning: false, + })); + }, + }); + }, + [data.search, searchStrategyName, setFetchState, setRawResponse] + ); + + const cancelFetch = useCallback(() => { + searchSubscription$.current?.unsubscribe(); + searchSubscription$.current = undefined; + abortCtrl.current.abort(); + setFetchState((prevState) => ({ + ...prevState, + isRunning: false, + })); + }, [setFetchState]); + + return { + fetchState, + rawResponse, + progress: fetchState.loaded / fetchState.total, + startFetch, + cancelFetch, + }; +} diff --git a/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts b/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts index 3f877a853ee26..5bd1bb2d57264 100644 --- a/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts +++ b/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts @@ -5,157 +5,56 @@ * 2.0. */ -import { useCallback, useRef, useState } from 'react'; -import type { Subscription } from 'rxjs'; -import { - IKibanaSearchRequest, - IKibanaSearchResponse, - isCompleteResponse, - isErrorResponse, -} from '../../../../../src/plugins/data/public'; -import type { SearchServiceParams } from '../../common/search_strategies/types'; +import { useEffect, useState } from 'react'; import type { LatencyCorrelationsAsyncSearchServiceRawResponse } from '../../common/search_strategies/latency_correlations/types'; -import { useKibana } from '../../../../../src/plugins/kibana_react/public'; import { APM_SEARCH_STRATEGIES } from '../../common/search_strategies/constants'; -import { ApmPluginStartDeps } from '../plugin'; -interface TransactionDistributionFetcherState { - error?: Error; - isComplete: boolean; - isRunning: boolean; - loaded: number; - ccsWarning: LatencyCorrelationsAsyncSearchServiceRawResponse['ccsWarning']; - log: LatencyCorrelationsAsyncSearchServiceRawResponse['log']; - transactionDistribution?: LatencyCorrelationsAsyncSearchServiceRawResponse['overallHistogram']; - percentileThresholdValue?: LatencyCorrelationsAsyncSearchServiceRawResponse['percentileThresholdValue']; - timeTook?: number; - total: number; -} +import { useSearchStrategy } from './use_search_strategy'; export function useTransactionDistributionFetcher() { - const { - services: { data }, - } = useKibana(); - const [ - fetchState, - setFetchState, - ] = useState({ - isComplete: false, - isRunning: false, - loaded: 0, + response, + setResponse, + ] = useState({ ccsWarning: false, log: [], - total: 100, + took: 0, }); - const abortCtrl = useRef(new AbortController()); - const searchSubscription$ = useRef(); + const { + fetchState, + rawResponse, + ...searchStrategy + } = useSearchStrategy( + APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS + ); - function setResponse( - response: IKibanaSearchResponse - ) { - setFetchState((prevState) => ({ + useEffect(() => { + setResponse((prevState) => ({ ...prevState, - isRunning: response.isRunning || false, - ccsWarning: response.rawResponse?.ccsWarning ?? false, - histograms: response.rawResponse?.values ?? [], - log: response.rawResponse?.log ?? [], - loaded: response.loaded!, - total: response.total!, - timeTook: response.rawResponse.took, + ccsWarning: rawResponse?.ccsWarning ?? false, + values: rawResponse?.values ?? [], + log: rawResponse?.log ?? [], // only set percentileThresholdValue and overallHistogram once it's repopulated on a refresh, // otherwise the consuming chart would flicker with an empty state on reload. - ...(response.rawResponse?.percentileThresholdValue !== undefined && - response.rawResponse?.overallHistogram !== undefined + ...(rawResponse?.percentileThresholdValue !== undefined && + rawResponse?.overallHistogram !== undefined ? { - transactionDistribution: response.rawResponse?.overallHistogram, - percentileThresholdValue: - response.rawResponse?.percentileThresholdValue, + overallHistogram: rawResponse?.overallHistogram, + percentileThresholdValue: rawResponse?.percentileThresholdValue, } : {}), // if loading is done but didn't return any data for the overall histogram, // set it to an empty array so the consuming chart component knows loading is done. - ...(!response.isRunning && - response.rawResponse?.overallHistogram === undefined - ? { transactionDistribution: [] } + ...(!fetchState.isRunning && rawResponse?.overallHistogram === undefined + ? { overallHistogram: [] } : {}), })); - } - - const startFetch = useCallback( - (params: Omit) => { - setFetchState((prevState) => ({ - ...prevState, - error: undefined, - isComplete: false, - })); - searchSubscription$.current?.unsubscribe(); - abortCtrl.current.abort(); - abortCtrl.current = new AbortController(); - - const searchServiceParams: SearchServiceParams = { - ...params, - analyzeCorrelations: false, - }; - const req = { params: searchServiceParams }; - - // Submit the search request using the `data.search` service. - searchSubscription$.current = data.search - .search< - IKibanaSearchRequest, - IKibanaSearchResponse - >(req, { - strategy: APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, - abortSignal: abortCtrl.current.signal, - }) - .subscribe({ - next: ( - res: IKibanaSearchResponse - ) => { - setResponse(res); - if (isCompleteResponse(res)) { - searchSubscription$.current?.unsubscribe(); - setFetchState((prevState) => ({ - ...prevState, - isRunnning: false, - isComplete: true, - })); - } else if (isErrorResponse(res)) { - searchSubscription$.current?.unsubscribe(); - setFetchState((prevState) => ({ - ...prevState, - error: (res as unknown) as Error, - isRunning: false, - })); - } - }, - error: (error: Error) => { - setFetchState((prevState) => ({ - ...prevState, - error, - isRunning: false, - })); - }, - }); - }, - [data.search, setFetchState] - ); - - const cancelFetch = useCallback(() => { - searchSubscription$.current?.unsubscribe(); - searchSubscription$.current = undefined; - abortCtrl.current.abort(); - setFetchState((prevState) => ({ - ...prevState, - isRunning: false, - })); - }, [setFetchState]); + }, [fetchState.isRunning, rawResponse]); return { ...fetchState, - progress: fetchState.loaded / fetchState.total, - startFetch, - cancelFetch, + ...searchStrategy, + ...response, }; } diff --git a/x-pack/plugins/apm/public/hooks/use_transaction_latency_correlations_fetcher.ts b/x-pack/plugins/apm/public/hooks/use_transaction_latency_correlations_fetcher.ts deleted file mode 100644 index cf3c4b054c3fb..0000000000000 --- a/x-pack/plugins/apm/public/hooks/use_transaction_latency_correlations_fetcher.ts +++ /dev/null @@ -1,168 +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; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { useCallback, useRef, useState } from 'react'; -import type { Subscription } from 'rxjs'; -import { - IKibanaSearchRequest, - IKibanaSearchResponse, - isCompleteResponse, - isErrorResponse, -} from '../../../../../src/plugins/data/public'; -import type { SearchServiceParams } from '../../common/search_strategies/types'; -import type { LatencyCorrelationsAsyncSearchServiceRawResponse } from '../../common/search_strategies/latency_correlations/types'; -import { useKibana } from '../../../../../src/plugins/kibana_react/public'; -import { APM_SEARCH_STRATEGIES } from '../../common/search_strategies/constants'; -import { ApmPluginStartDeps } from '../plugin'; - -interface TransactionLatencyCorrelationsFetcherState { - error?: Error; - isComplete: boolean; - isRunning: boolean; - loaded: number; - ccsWarning: LatencyCorrelationsAsyncSearchServiceRawResponse['ccsWarning']; - histograms: LatencyCorrelationsAsyncSearchServiceRawResponse['values']; - log: LatencyCorrelationsAsyncSearchServiceRawResponse['log']; - overallHistogram?: LatencyCorrelationsAsyncSearchServiceRawResponse['overallHistogram']; - percentileThresholdValue?: LatencyCorrelationsAsyncSearchServiceRawResponse['percentileThresholdValue']; - timeTook?: number; - total: number; -} - -export const useTransactionLatencyCorrelationsFetcher = () => { - const { - services: { data }, - } = useKibana(); - - const [ - fetchState, - setFetchState, - ] = useState({ - isComplete: false, - isRunning: false, - loaded: 0, - ccsWarning: false, - histograms: [], - log: [], - total: 100, - }); - - const abortCtrl = useRef(new AbortController()); - const searchSubscription$ = useRef(); - - function setResponse( - response: IKibanaSearchResponse - ) { - setFetchState((prevState) => ({ - ...prevState, - isRunning: response.isRunning || false, - ccsWarning: response.rawResponse?.ccsWarning ?? false, - histograms: response.rawResponse?.values ?? [], - log: response.rawResponse?.log ?? [], - loaded: response.loaded!, - total: response.total!, - timeTook: response.rawResponse.took, - // only set percentileThresholdValue and overallHistogram once it's repopulated on a refresh, - // otherwise the consuming chart would flicker with an empty state on reload. - ...(response.rawResponse?.percentileThresholdValue !== undefined && - response.rawResponse?.overallHistogram !== undefined - ? { - overallHistogram: response.rawResponse?.overallHistogram, - percentileThresholdValue: - response.rawResponse?.percentileThresholdValue, - } - : {}), - // if loading is done but didn't return any data for the overall histogram, - // set it to an empty array so the consuming chart component knows loading is done. - ...(!response.isRunning && - response.rawResponse?.overallHistogram === undefined - ? { overallHistogram: [] } - : {}), - })); - } - - const startFetch = useCallback( - (params: Omit) => { - setFetchState((prevState) => ({ - ...prevState, - error: undefined, - isComplete: false, - })); - searchSubscription$.current?.unsubscribe(); - abortCtrl.current.abort(); - abortCtrl.current = new AbortController(); - - const searchServiceParams: SearchServiceParams = { - ...params, - analyzeCorrelations: true, - }; - const req = { params: searchServiceParams }; - - // Submit the search request using the `data.search` service. - searchSubscription$.current = data.search - .search< - IKibanaSearchRequest, - IKibanaSearchResponse - >(req, { - strategy: APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, - abortSignal: abortCtrl.current.signal, - }) - .subscribe({ - next: ( - res: IKibanaSearchResponse - ) => { - setResponse(res); - if (isCompleteResponse(res)) { - searchSubscription$.current?.unsubscribe(); - setFetchState((prevState) => ({ - ...prevState, - isRunnning: false, - isComplete: true, - })); - } else if (isErrorResponse(res)) { - searchSubscription$.current?.unsubscribe(); - setFetchState((prevState) => ({ - ...prevState, - error: (res as unknown) as Error, - isRunning: false, - })); - } - }, - error: (error: Error) => { - setFetchState((prevState) => ({ - ...prevState, - error, - isRunning: false, - })); - }, - }); - }, - [data.search, setFetchState] - ); - - const cancelFetch = useCallback(() => { - searchSubscription$.current?.unsubscribe(); - searchSubscription$.current = undefined; - abortCtrl.current.abort(); - setFetchState((prevState) => ({ - ...prevState, - // If we didn't receive data for the overall histogram yet - // set it to an empty array to indicate loading stopped. - ...(prevState.overallHistogram === undefined - ? { overallHistogram: [] } - : {}), - isRunning: false, - })); - }, [setFetchState]); - - return { - ...fetchState, - progress: fetchState.loaded / fetchState.total, - startFetch, - cancelFetch, - }; -}; From 013369b776b587f297d5b615427516a03dae9061 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Tue, 31 Aug 2021 13:04:35 +0200 Subject: [PATCH 08/37] [ML] Consolidate hooks. --- .../failed_transactions_correlations.tsx | 27 +++- ...ailed_transactions_correlations_fetcher.ts | 139 ------------------ 2 files changed, 23 insertions(+), 143 deletions(-) delete mode 100644 x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts diff --git a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx index 7b92dd3bc01b3..50620bfd9acb2 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx @@ -35,14 +35,18 @@ import { FETCH_STATUS } from '../../../hooks/use_fetcher'; import { getFailedTransactionsCorrelationImpactLabel } from './utils/get_failed_transactions_correlation_impact_label'; import { createHref, push } from '../../shared/Links/url_helpers'; import { useUiTracker } from '../../../../../observability/public'; -import { useFailedTransactionsCorrelationsFetcher } from '../../../hooks/use_failed_transactions_correlations_fetcher'; +import { useSearchStrategy } from '../../../hooks/use_search_strategy'; import { useApmParams } from '../../../hooks/use_apm_params'; import { CorrelationsLog } from './correlations_log'; import { CorrelationsEmptyStatePrompt } from './empty_state_prompt'; import { CrossClusterSearchCompatibilityWarning } from './cross_cluster_search_warning'; import { CorrelationsProgressControls } from './progress_controls'; -import type { FailedTransactionsCorrelationValue } from '../../../../common/search_strategies/failed_transactions_correlations/types'; +import type { + FailedTransactionsCorrelationValue, + FailedTransactionsCorrelationsAsyncSearchServiceRawResponse, +} from '../../../../common/search_strategies/failed_transactions_correlations/types'; import { Summary } from '../../shared/Summary'; +import { APM_SEARCH_STRATEGIES } from '../../../../common/search_strategies/constants'; import { asPercent } from '../../../../common/utils/formatters'; import { useTimeRange } from '../../../hooks/use_time_range'; @@ -69,7 +73,22 @@ export function FailedTransactionsCorrelations({ const inspectEnabled = uiSettings.get(enableInspectEsQueries); - const result = useFailedTransactionsCorrelationsFetcher(); + const { + fetchState, + rawResponse, + ...searchStrategy + } = useSearchStrategy( + APM_SEARCH_STRATEGIES.APM_FAILED_TRANSACTIONS_CORRELATIONS + ); + + const result = useMemo( + () => ({ + ...fetchState, + ...searchStrategy, + ...rawResponse, + }), + [fetchState, searchStrategy, rawResponse] + ); const { ccsWarning, @@ -503,7 +522,7 @@ export function FailedTransactionsCorrelations({ )}
- {inspectEnabled && } + {inspectEnabled && } ); } diff --git a/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts b/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts deleted file mode 100644 index 45598a6775d10..0000000000000 --- a/x-pack/plugins/apm/public/hooks/use_failed_transactions_correlations_fetcher.ts +++ /dev/null @@ -1,139 +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; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { useCallback, useRef, useState } from 'react'; -import type { Subscription } from 'rxjs'; -import { - IKibanaSearchRequest, - IKibanaSearchResponse, - isCompleteResponse, - isErrorResponse, -} from '../../../../../src/plugins/data/public'; -import type { SearchServiceParams } from '../../common/search_strategies/types'; -import { useKibana } from '../../../../../src/plugins/kibana_react/public'; -import type { ApmPluginStartDeps } from '../plugin'; -import type { FailedTransactionsCorrelationValue } from '../../common/search_strategies/failed_transactions_correlations/types'; -import { APM_SEARCH_STRATEGIES } from '../../common/search_strategies/constants'; - -interface RawResponse { - took: number; - values: FailedTransactionsCorrelationValue[]; - log: string[]; - ccsWarning: boolean; -} - -interface FailedTransactionsCorrelationsFetcherState { - error?: Error; - isComplete: boolean; - isRunning: boolean; - loaded: number; - ccsWarning: RawResponse['ccsWarning']; - values: RawResponse['values']; - log: RawResponse['log']; - total: number; -} - -export const useFailedTransactionsCorrelationsFetcher = () => { - const { - services: { data }, - } = useKibana(); - - const [ - fetchState, - setFetchState, - ] = useState({ - isComplete: false, - isRunning: false, - loaded: 0, - ccsWarning: false, - values: [], - log: [], - total: 100, - }); - - const abortCtrl = useRef(new AbortController()); - const searchSubscription$ = useRef(); - - function setResponse(response: IKibanaSearchResponse) { - setFetchState((prevState) => ({ - ...prevState, - isRunning: response.isRunning || false, - ccsWarning: response.rawResponse?.ccsWarning ?? false, - values: response.rawResponse?.values ?? [], - log: response.rawResponse?.log ?? [], - loaded: response.loaded!, - total: response.total!, - })); - } - - const startFetch = useCallback( - (params: SearchServiceParams) => { - setFetchState((prevState) => ({ - ...prevState, - error: undefined, - isComplete: false, - })); - searchSubscription$.current?.unsubscribe(); - abortCtrl.current.abort(); - abortCtrl.current = new AbortController(); - - const req = { params }; - - // Submit the search request using the `data.search` service. - searchSubscription$.current = data.search - .search>(req, { - strategy: APM_SEARCH_STRATEGIES.APM_FAILED_TRANSACTIONS_CORRELATIONS, - abortSignal: abortCtrl.current.signal, - }) - .subscribe({ - next: (res: IKibanaSearchResponse) => { - setResponse(res); - if (isCompleteResponse(res)) { - searchSubscription$.current?.unsubscribe(); - setFetchState((prevState) => ({ - ...prevState, - isRunnning: false, - isComplete: true, - })); - } else if (isErrorResponse(res)) { - searchSubscription$.current?.unsubscribe(); - setFetchState((prevState) => ({ - ...prevState, - error: (res as unknown) as Error, - isRunning: false, - })); - } - }, - error: (error: Error) => { - setFetchState((prevState) => ({ - ...prevState, - error, - isRunning: false, - })); - }, - }); - }, - [data.search, setFetchState] - ); - - const cancelFetch = useCallback(() => { - searchSubscription$.current?.unsubscribe(); - searchSubscription$.current = undefined; - abortCtrl.current.abort(); - setFetchState((prevState) => ({ - ...prevState, - isRunning: false, - })); - }, [setFetchState]); - - return { - ...fetchState, - progress: fetchState.loaded / fetchState.total, - startFetch, - cancelFetch, - }; -}; From cb6c9de705be1761cc9b4e6535a8ba5445d96b8c Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Wed, 1 Sep 2021 10:00:35 +0200 Subject: [PATCH 09/37] [ML] Consolidate hooks. --- .../failed_transactions_correlations.tsx | 124 +++------- .../app/correlations/latency_correlations.tsx | 94 +++----- .../distribution/index.tsx | 60 +---- .../apm/public/hooks/use_search_strategy.ts | 213 +++++++++++------- .../use_transaction_distribution_fetcher.ts | 60 ----- .../queries/get_query_with_params.ts | 2 +- .../queries/query_correlation.ts | 2 +- .../queries/query_failure_correlation.ts | 2 +- .../queries/query_field_candidates.ts | 2 +- .../queries/query_field_value_pairs.ts | 2 +- .../queries/query_fractions.ts | 2 +- .../queries/query_histogram.ts | 2 +- .../queries/query_histogram_interval.ts | 2 +- .../queries/query_histogram_range_steps.ts | 2 +- .../queries/query_histograms_generator.ts | 2 +- .../queries/query_percentiles.ts | 2 +- .../search_strategies/queries/query_ranges.ts | 2 +- .../register_search_strategies.ts | 5 +- 18 files changed, 230 insertions(+), 350 deletions(-) delete mode 100644 x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts diff --git a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx index 50620bfd9acb2..aac685ddcbdd6 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx @@ -6,6 +6,9 @@ */ import React, { useCallback, useEffect, useMemo, useState } from 'react'; +import { useHistory } from 'react-router-dom'; +import { orderBy } from 'lodash'; + import { EuiBasicTableColumn, EuiFlexGroup, @@ -18,37 +21,38 @@ import { EuiBadge, EuiToolTip, } from '@elastic/eui'; -import { i18n } from '@kbn/i18n'; -import { useHistory } from 'react-router-dom'; -import { orderBy } from 'lodash'; import type { EuiTableSortingType } from '@elastic/eui/src/components/basic_table/table_types'; import type { Direction } from '@elastic/eui/src/services/sort/sort_direction'; -import { useUrlParams } from '../../../context/url_params_context/use_url_params'; + +import { i18n } from '@kbn/i18n'; +import { + enableInspectEsQueries, + useUiTracker, +} from '../../../../../observability/public'; + +import { asPercent } from '../../../../common/utils/formatters'; +import type { + FailedTransactionsCorrelationValue, + FailedTransactionsCorrelationsAsyncSearchServiceRawResponse, +} from '../../../../common/search_strategies/failed_transactions_correlations/types'; +import { APM_SEARCH_STRATEGIES } from '../../../../common/search_strategies/constants'; + import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; +import { FETCH_STATUS } from '../../../hooks/use_fetcher'; +import { useSearchStrategy } from '../../../hooks/use_search_strategy'; + +import { ImpactBar } from '../../shared/ImpactBar'; +import { createHref, push } from '../../shared/Links/url_helpers'; +import { Summary } from '../../shared/Summary'; + import { CorrelationsTable } from './correlations_table'; -import { enableInspectEsQueries } from '../../../../../observability/public'; -import { useApmServiceContext } from '../../../context/apm_service/use_apm_service_context'; import { FailedTransactionsCorrelationsHelpPopover } from './failed_transactions_correlations_help_popover'; -import { ImpactBar } from '../../shared/ImpactBar'; import { isErrorMessage } from './utils/is_error_message'; -import { FETCH_STATUS } from '../../../hooks/use_fetcher'; import { getFailedTransactionsCorrelationImpactLabel } from './utils/get_failed_transactions_correlation_impact_label'; -import { createHref, push } from '../../shared/Links/url_helpers'; -import { useUiTracker } from '../../../../../observability/public'; -import { useSearchStrategy } from '../../../hooks/use_search_strategy'; -import { useApmParams } from '../../../hooks/use_apm_params'; import { CorrelationsLog } from './correlations_log'; import { CorrelationsEmptyStatePrompt } from './empty_state_prompt'; import { CrossClusterSearchCompatibilityWarning } from './cross_cluster_search_warning'; import { CorrelationsProgressControls } from './progress_controls'; -import type { - FailedTransactionsCorrelationValue, - FailedTransactionsCorrelationsAsyncSearchServiceRawResponse, -} from '../../../../common/search_strategies/failed_transactions_correlations/types'; -import { Summary } from '../../shared/Summary'; -import { APM_SEARCH_STRATEGIES } from '../../../../common/search_strategies/constants'; -import { asPercent } from '../../../../common/utils/formatters'; -import { useTimeRange } from '../../../hooks/use_time_range'; export function FailedTransactionsCorrelations({ onFilter, @@ -60,71 +64,17 @@ export function FailedTransactionsCorrelations({ } = useApmPluginContext(); const trackApmEvent = useUiTracker({ app: 'apm' }); - const { serviceName, transactionType } = useApmServiceContext(); - - const { - query: { kuery, environment, rangeFrom, rangeTo }, - } = useApmParams('/services/:serviceName'); - - const { urlParams } = useUrlParams(); - const { transactionName } = urlParams; - - const { start, end } = useTimeRange({ rangeFrom, rangeTo }); - const inspectEnabled = uiSettings.get(enableInspectEsQueries); const { - fetchState, - rawResponse, - ...searchStrategy + state: { error, isRunning, loaded, total }, + data: { ccsWarning, log, values }, + startFetch, + cancelFetch, } = useSearchStrategy( APM_SEARCH_STRATEGIES.APM_FAILED_TRANSACTIONS_CORRELATIONS ); - - const result = useMemo( - () => ({ - ...fetchState, - ...searchStrategy, - ...rawResponse, - }), - [fetchState, searchStrategy, rawResponse] - ); - - const { - ccsWarning, - log, - error, - isRunning, - progress, - startFetch, - cancelFetch, - } = result; - - const startFetchHandler = useCallback(() => { - startFetch({ - environment, - kuery, - serviceName, - transactionName, - transactionType, - start, - end, - }); - }, [ - startFetch, - environment, - serviceName, - transactionName, - transactionType, - kuery, - start, - end, - ]); - - useEffect(() => { - startFetchHandler(); - return cancelFetch; - }, [cancelFetch, startFetchHandler]); + const progress = loaded / total; const [ selectedSignificantTerm, @@ -133,12 +83,8 @@ export function FailedTransactionsCorrelations({ const selectedTerm = useMemo(() => { if (selectedSignificantTerm) return selectedSignificantTerm; - return result?.values && - Array.isArray(result.values) && - result.values.length > 0 - ? result?.values[0] - : undefined; - }, [selectedSignificantTerm, result]); + return Array.isArray(values) && values.length > 0 ? values[0] : undefined; + }, [selectedSignificantTerm, values]); const history = useHistory(); @@ -387,11 +333,11 @@ export function FailedTransactionsCorrelations({ }, []); const { sorting, correlationTerms } = useMemo(() => { - if (!Array.isArray(result.values)) { + if (!Array.isArray(values)) { return { correlationTerms: [], sorting: undefined }; } const orderedTerms = orderBy( - result.values, + values, // The smaller the p value the higher the impact // So we want to sort by the normalized score here // which goes from 0 -> 1 @@ -407,7 +353,7 @@ export function FailedTransactionsCorrelations({ }, } as EuiTableSortingType, }; - }, [result?.values, sortField, sortDirection]); + }, [values, sortField, sortDirection]); return (
@@ -477,7 +423,7 @@ export function FailedTransactionsCorrelations({ diff --git a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx index 2ecfff27c1b8c..1745ac17b7736 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx @@ -7,6 +7,8 @@ import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { useHistory } from 'react-router-dom'; +import { orderBy } from 'lodash'; + import { EuiIcon, EuiBasicTableColumn, @@ -16,31 +18,34 @@ import { EuiTitle, EuiToolTip, } from '@elastic/eui'; -import { i18n } from '@kbn/i18n'; import { Direction } from '@elastic/eui/src/services/sort/sort_direction'; -import { orderBy } from 'lodash'; import { EuiTableSortingType } from '@elastic/eui/src/components/basic_table/table_types'; -import { useUrlParams } from '../../../context/url_params_context/use_url_params'; -import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; -import { FETCH_STATUS } from '../../../hooks/use_fetcher'; -import { useTransactionDistributionFetcher } from '../../../hooks/use_transaction_distribution_fetcher'; -import { TransactionDistributionChart } from '../../shared/charts/transaction_distribution_chart'; -import { CorrelationsTable } from './correlations_table'; -import { push } from '../../shared/Links/url_helpers'; + +import { i18n } from '@kbn/i18n'; + import { enableInspectEsQueries, useUiTracker, } from '../../../../../observability/public'; + import { asPreciseDecimal } from '../../../../common/utils/formatters'; -import { useApmServiceContext } from '../../../context/apm_service/use_apm_service_context'; +import type { LatencyCorrelationsAsyncSearchServiceRawResponse } from '../../../../common/search_strategies/latency_correlations/types'; +import { APM_SEARCH_STRATEGIES } from '../../../../common/search_strategies/constants'; + +import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; +import { FETCH_STATUS } from '../../../hooks/use_fetcher'; +import { useSearchStrategy } from '../../../hooks/use_search_strategy'; + +import { TransactionDistributionChart } from '../../shared/charts/transaction_distribution_chart'; +import { push } from '../../shared/Links/url_helpers'; + +import { CorrelationsTable } from './correlations_table'; import { LatencyCorrelationsHelpPopover } from './latency_correlations_help_popover'; -import { useApmParams } from '../../../hooks/use_apm_params'; import { isErrorMessage } from './utils/is_error_message'; import { CorrelationsLog } from './correlations_log'; import { CorrelationsEmptyStatePrompt } from './empty_state_prompt'; import { CrossClusterSearchCompatibilityWarning } from './cross_cluster_search_warning'; import { CorrelationsProgressControls } from './progress_controls'; -import { useTimeRange } from '../../../hooks/use_time_range'; const DEFAULT_PERCENTILE_THRESHOLD = 95; @@ -57,60 +62,27 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { core: { notifications, uiSettings }, } = useApmPluginContext(); - const { serviceName, transactionType } = useApmServiceContext(); - - const { - query: { kuery, environment, rangeFrom, rangeTo }, - } = useApmParams('/services/:serviceName/transactions/view'); - - const { urlParams } = useUrlParams(); - - const { transactionName } = urlParams; - - const { start, end } = useTimeRange({ rangeFrom, rangeTo }); - const displayLog = uiSettings.get(enableInspectEsQueries); const { - ccsWarning, - log, - error, - values, - percentileThresholdValue, - isRunning, - progress, + state: { error, isRunning, loaded, total }, + data: { + ccsWarning, + log, + values, + percentileThresholdValue, + overallHistogram, + }, startFetch, cancelFetch, - overallHistogram, - } = useTransactionDistributionFetcher(); - - const startFetchHandler = useCallback(() => { - startFetch({ - environment, - kuery, - serviceName, - transactionName, - transactionType, - start, - end, + } = useSearchStrategy( + APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, + { percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD, analyzeCorrelations: true, - }); - }, [ - startFetch, - environment, - serviceName, - transactionName, - transactionType, - kuery, - start, - end, - ]); - - useEffect(() => { - startFetchHandler(); - return cancelFetch; - }, [cancelFetch, startFetchHandler]); + } + ); + const progress = loaded / total; useEffect(() => { if (isErrorMessage(error)) { @@ -350,7 +322,7 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { @@ -386,7 +358,7 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { )}
- {displayLog && } + {displayLog && } ); } diff --git a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx index 4b80295d9a6cb..5cd936ca4c3c4 100644 --- a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx +++ b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx @@ -18,18 +18,16 @@ import { } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import { getDurationFormatter } from '../../../../../common/utils/formatters'; -import { useUrlParams } from '../../../../context/url_params_context/use_url_params'; +import type { LatencyCorrelationsAsyncSearchServiceRawResponse } from '../../../../../common/search_strategies/latency_correlations/types'; +import { APM_SEARCH_STRATEGIES } from '../../../../../common/search_strategies/constants'; import { useApmPluginContext } from '../../../../context/apm_plugin/use_apm_plugin_context'; -import { useTransactionDistributionFetcher } from '../../../../hooks/use_transaction_distribution_fetcher'; +import { useSearchStrategy } from '../../../../hooks/use_search_strategy'; import { OnHasData, TransactionDistributionChart, } from '../../../shared/charts/transaction_distribution_chart'; import { useUiTracker } from '../../../../../../observability/public'; -import { useApmServiceContext } from '../../../../context/apm_service/use_apm_service_context'; -import { useApmParams } from '../../../../hooks/use_apm_params'; import { isErrorMessage } from '../../correlations/utils/is_error_message'; -import { useTimeRange } from '../../../../hooks/use_time_range'; const DEFAULT_PERCENTILE_THRESHOLD = 95; // Enforce min height so it's consistent across all tabs on the same level @@ -69,18 +67,6 @@ export function TransactionDistribution({ core: { notifications }, } = useApmPluginContext(); - const { serviceName, transactionType } = useApmServiceContext(); - - const { - query: { kuery, environment, rangeFrom, rangeTo }, - } = useApmParams('/services/:serviceName/transactions/view'); - - const { start, end } = useTimeRange({ rangeFrom, rangeTo }); - - const { urlParams } = useUrlParams(); - - const { transactionName } = urlParams; - const [showSelection, setShowSelection] = useState(false); const onTransactionDistributionHasData: OnHasData = useCallback( @@ -106,40 +92,18 @@ export function TransactionDistribution({ ); const { - error, - percentileThresholdValue, - startFetch, - cancelFetch, - overallHistogram, - } = useTransactionDistributionFetcher(); - - const startFetchHandler = useCallback(() => { - startFetch({ - environment, - kuery, - serviceName, - transactionName, - transactionType, - start, - end, + state, + data, + } = useSearchStrategy( + APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, + { percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD, analyzeCorrelations: false, - }); - }, [ - startFetch, - environment, - serviceName, - transactionName, - transactionType, - kuery, - start, - end, - ]); + } + ); - useEffect(() => { - startFetchHandler(); - return cancelFetch; - }, [cancelFetch, startFetchHandler]); + const { error } = state; + const { percentileThresholdValue, overallHistogram } = data; useEffect(() => { if (isErrorMessage(error)) { diff --git a/x-pack/plugins/apm/public/hooks/use_search_strategy.ts b/x-pack/plugins/apm/public/hooks/use_search_strategy.ts index 0a98b0208709a..e3f26fd3d3d63 100644 --- a/x-pack/plugins/apm/public/hooks/use_search_strategy.ts +++ b/x-pack/plugins/apm/public/hooks/use_search_strategy.ts @@ -5,20 +5,28 @@ * 2.0. */ -import { useCallback, useRef, useState } from 'react'; +import { useCallback, useEffect, useReducer, useRef } from 'react'; import type { Subscription } from 'rxjs'; + import { IKibanaSearchRequest, IKibanaSearchResponse, isCompleteResponse, isErrorResponse, } from '../../../../../src/plugins/data/public'; +import { useKibana } from '../../../../../src/plugins/kibana_react/public'; + import type { SearchServiceParams } from '../../common/search_strategies/types'; import type { RawResponseBase } from '../../common/search_strategies/types'; -import { useKibana } from '../../../../../src/plugins/kibana_react/public'; import { ApmSearchStrategies } from '../../common/search_strategies/constants'; +import { useApmServiceContext } from '../context/apm_service/use_apm_service_context'; +import { useUrlParams } from '../context/url_params_context/use_url_params'; + import { ApmPluginStartDeps } from '../plugin'; +import { useApmParams } from './use_apm_params'; +import { useTimeRange } from './use_time_range'; + interface SearchStrategyFetcherState { error?: Error; isComplete: boolean; @@ -27,99 +35,150 @@ interface SearchStrategyFetcherState { total: number; } -export function useSearchStrategy( - searchStrategyName: ApmSearchStrategies -) { +const getInitialRawResponse = < + TRawResponse extends RawResponseBase +>(): TRawResponse => + ({ + ccsWarning: false, + took: 0, + } as TRawResponse); + +const getInitialFetchState = (): SearchStrategyFetcherState => ({ + isComplete: false, + isRunning: false, + loaded: 0, + total: 100, +}); + +const getReducer = () => (prev: T, update: Partial): T => ({ + ...prev, + ...update, +}); + +export function useSearchStrategy< + TRawResponse extends RawResponseBase, + TOptions extends {} = {} +>(searchStrategyName: ApmSearchStrategies, options: TOptions = {} as TOptions) { const { services: { data }, } = useKibana(); - const [rawResponse, setRawResponse] = useState({ - ccsWarning: false, - took: 0, - } as RawResponse); + const { serviceName, transactionType } = useApmServiceContext(); + const { + query: { kuery, environment, rangeFrom, rangeTo }, + } = useApmParams('/services/:serviceName/transactions/view'); + const { start, end } = useTimeRange({ rangeFrom, rangeTo }); + const { urlParams } = useUrlParams(); + const { transactionName } = urlParams; + + const [rawResponse, setRawResponse] = useReducer( + getReducer(), + getInitialRawResponse() + ); - const [fetchState, setFetchState] = useState({ - isComplete: false, - isRunning: false, - loaded: 0, - total: 100, - }); + const [fetchState, setFetchState] = useReducer( + getReducer(), + getInitialFetchState() + ); const abortCtrl = useRef(new AbortController()); const searchSubscription$ = useRef(); + // options get passed in as a Generic and we don't want to support + // an updated on options changes, they should be considered non-changing options + // to initialize the search service. + const optionsRef = useRef(options); + + const startFetch = useCallback(() => { + setFetchState({ + error: undefined, + isComplete: false, + }); + searchSubscription$.current?.unsubscribe(); + abortCtrl.current.abort(); + abortCtrl.current = new AbortController(); + + const req = { + params: { + environment, + serviceName, + transactionName, + transactionType, + kuery, + start, + end, + ...optionsRef.current, + }, + }; + + // Submit the search request using the `data.search` service. + searchSubscription$.current = data.search + .search< + IKibanaSearchRequest, + IKibanaSearchResponse + >(req, { + strategy: searchStrategyName, + abortSignal: abortCtrl.current.signal, + }) + .subscribe({ + next: (res: IKibanaSearchResponse) => { + setRawResponse(res.rawResponse); + setFetchState({ + isRunning: res.isRunning || false, + loaded: res.loaded!, + total: res.total!, + }); - const startFetch = useCallback( - (params: SearchServiceParams) => { - setFetchState((prevState) => ({ - ...prevState, - error: undefined, - isComplete: false, - })); - searchSubscription$.current?.unsubscribe(); - abortCtrl.current.abort(); - abortCtrl.current = new AbortController(); - - const req = { params }; - - // Submit the search request using the `data.search` service. - searchSubscription$.current = data.search - .search>(req, { - strategy: searchStrategyName, - abortSignal: abortCtrl.current.signal, - }) - .subscribe({ - next: (res: IKibanaSearchResponse) => { - setRawResponse(res.rawResponse); - setFetchState((prevState) => ({ - ...prevState, - isRunning: res.isRunning || false, - loaded: res.loaded!, - total: res.total!, - })); - - if (isCompleteResponse(res)) { - searchSubscription$.current?.unsubscribe(); - setFetchState((prevState) => ({ - ...prevState, - isRunnning: false, - isComplete: true, - })); - } else if (isErrorResponse(res)) { - searchSubscription$.current?.unsubscribe(); - setFetchState((prevState) => ({ - ...prevState, - error: (res as unknown) as Error, - isRunning: false, - })); - } - }, - error: (error: Error) => { - setFetchState((prevState) => ({ - ...prevState, - error, + if (isCompleteResponse(res)) { + searchSubscription$.current?.unsubscribe(); + setFetchState({ isRunning: false, - })); - }, - }); - }, - [data.search, searchStrategyName, setFetchState, setRawResponse] - ); + isComplete: true, + }); + } else if (isErrorResponse(res)) { + searchSubscription$.current?.unsubscribe(); + setFetchState({ + error: (res as unknown) as Error, + isRunning: false, + }); + } + }, + error: (error: Error) => { + setFetchState({ + error, + isRunning: false, + }); + }, + }); + }, [ + searchStrategyName, + data.search, + environment, + serviceName, + transactionName, + transactionType, + kuery, + start, + end, + ]); const cancelFetch = useCallback(() => { searchSubscription$.current?.unsubscribe(); searchSubscription$.current = undefined; abortCtrl.current.abort(); - setFetchState((prevState) => ({ - ...prevState, + setFetchState({ isRunning: false, - })); - }, [setFetchState]); + }); + }, []); + + // auto-update + useEffect(() => { + startFetch(); + return cancelFetch; + }, [startFetch, cancelFetch]); return { - fetchState, - rawResponse, - progress: fetchState.loaded / fetchState.total, + state: fetchState, + data: rawResponse, startFetch, cancelFetch, }; diff --git a/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts b/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts deleted file mode 100644 index 5bd1bb2d57264..0000000000000 --- a/x-pack/plugins/apm/public/hooks/use_transaction_distribution_fetcher.ts +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { useEffect, useState } from 'react'; -import type { LatencyCorrelationsAsyncSearchServiceRawResponse } from '../../common/search_strategies/latency_correlations/types'; -import { APM_SEARCH_STRATEGIES } from '../../common/search_strategies/constants'; - -import { useSearchStrategy } from './use_search_strategy'; - -export function useTransactionDistributionFetcher() { - const [ - response, - setResponse, - ] = useState({ - ccsWarning: false, - log: [], - took: 0, - }); - - const { - fetchState, - rawResponse, - ...searchStrategy - } = useSearchStrategy( - APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS - ); - - useEffect(() => { - setResponse((prevState) => ({ - ...prevState, - ccsWarning: rawResponse?.ccsWarning ?? false, - values: rawResponse?.values ?? [], - log: rawResponse?.log ?? [], - // only set percentileThresholdValue and overallHistogram once it's repopulated on a refresh, - // otherwise the consuming chart would flicker with an empty state on reload. - ...(rawResponse?.percentileThresholdValue !== undefined && - rawResponse?.overallHistogram !== undefined - ? { - overallHistogram: rawResponse?.overallHistogram, - percentileThresholdValue: rawResponse?.percentileThresholdValue, - } - : {}), - // if loading is done but didn't return any data for the overall histogram, - // set it to an empty array so the consuming chart component knows loading is done. - ...(!fetchState.isRunning && rawResponse?.overallHistogram === undefined - ? { overallHistogram: [] } - : {}), - })); - }, [fetchState.isRunning, rawResponse]); - - return { - ...fetchState, - ...searchStrategy, - ...response, - }; -} diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts index d4f9293522467..244f42097874c 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts @@ -10,7 +10,7 @@ import { getOrElse } from 'fp-ts/lib/Either'; import { pipe } from 'fp-ts/lib/pipeable'; import * as t from 'io-ts'; import { failure } from 'io-ts/lib/PathReporter'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; import { rangeRt } from '../../../routes/default_api_types'; import { getCorrelationsFilters } from '../../correlations/get_filters'; import { Setup, SetupTimeRange } from '../../helpers/setup_request'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts index 4e677b69c3440..02bb97f355a67 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts @@ -10,7 +10,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts index 305574d589fee..d654e69121885 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts @@ -6,7 +6,7 @@ */ import { estypes } from '@elastic/elasticsearch'; import { ElasticsearchClient } from 'kibana/server'; -import { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; +import { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; import { EVENT_OUTCOME } from '../../../../common/elasticsearch_fieldnames'; import { EventOutcome } from '../../../../common/event_outcome'; import { getQueryWithParams, getTermsQuery } from './get_query_with_params'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts index 2e83b85488b2e..a3e823eaf4f69 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts @@ -9,7 +9,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; import { FIELD_PREFIX_TO_EXCLUDE_AS_CANDIDATE, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts index f1c7a8564f42a..15a47063771b7 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts @@ -9,7 +9,7 @@ import type { ElasticsearchClient } from 'src/core/server'; import type { estypes } from '@elastic/elasticsearch'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; import type { AsyncSearchServiceLog } from '../async_search_service_log'; import type { AsyncSearchServiceState } from '../latency_correlations/async_search_service_state'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.ts index 59e9a1298f39e..3dd10c4529a22 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.ts @@ -8,7 +8,7 @@ import { ElasticsearchClient } from 'kibana/server'; import { estypes } from '@elastic/elasticsearch'; -import { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; +import { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; import { getQueryWithParams } from './get_query_with_params'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts index 3b7cb9fabc859..9329e4f2795fe 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts @@ -13,8 +13,8 @@ import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldname import type { HistogramItem, ResponseHit, - SearchServiceFetchParams, } from '../../../../common/search_strategies/latency_correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.ts index 9551f7aaab4ee..d1dbe578e2997 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.ts @@ -10,7 +10,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.ts index f9645c7b184cd..b7912a44b6caa 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.ts @@ -12,7 +12,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts index 3a96c78db90fd..dfaf7838cea00 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts @@ -9,7 +9,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; import type { AsyncSearchServiceLog } from '../async_search_service_log'; import type { AsyncSearchServiceState } from '../latency_correlations/async_search_service_state'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts index 72577a380f4fd..70cc36e22482f 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts @@ -10,7 +10,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts index 7591143646b34..2131dd2598f37 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts @@ -10,7 +10,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/latency_correlations/types'; +import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/register_search_strategies.ts b/x-pack/plugins/apm/server/lib/search_strategies/register_search_strategies.ts index 8a6dda0c4e0f5..33b764be2c625 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/register_search_strategies.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/register_search_strategies.ts @@ -11,10 +11,9 @@ import { APM_SEARCH_STRATEGIES } from '../../../common/search_strategies/constan import type { ApmIndicesConfig } from '../settings/apm_indices/get_apm_indices'; -import { searchStrategyProvider } from './search_strategy_provider'; - -import { latencyCorrelationsAsyncSearchServiceProvider } from './latency_correlations'; import { failedTransactionsCorrelationsAsyncSearchServiceProvider } from './failed_transactions_correlations'; +import { latencyCorrelationsAsyncSearchServiceProvider } from './latency_correlations'; +import { searchStrategyProvider } from './search_strategy_provider'; export const registerSearchStrategies = ( registerSearchStrategy: DataPluginSetup['search']['registerSearchStrategy'], From 52443478a6e155ef68bf2426b1132927fd228f3b Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Wed, 1 Sep 2021 11:55:52 +0200 Subject: [PATCH 10/37] [ML] Use function overloads. --- .../failed_transactions_correlations.tsx | 7 +-- .../app/correlations/latency_correlations.tsx | 12 ++--- .../distribution/index.tsx | 6 +-- .../apm/public/hooks/use_search_strategy.ts | 46 ++++++++++++++----- 4 files changed, 42 insertions(+), 29 deletions(-) diff --git a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx index aac685ddcbdd6..91eebc7fc5cda 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx @@ -31,10 +31,7 @@ import { } from '../../../../../observability/public'; import { asPercent } from '../../../../common/utils/formatters'; -import type { - FailedTransactionsCorrelationValue, - FailedTransactionsCorrelationsAsyncSearchServiceRawResponse, -} from '../../../../common/search_strategies/failed_transactions_correlations/types'; +import type { FailedTransactionsCorrelationValue } from '../../../../common/search_strategies/failed_transactions_correlations/types'; import { APM_SEARCH_STRATEGIES } from '../../../../common/search_strategies/constants'; import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; @@ -71,7 +68,7 @@ export function FailedTransactionsCorrelations({ data: { ccsWarning, log, values }, startFetch, cancelFetch, - } = useSearchStrategy( + } = useSearchStrategy( APM_SEARCH_STRATEGIES.APM_FAILED_TRANSACTIONS_CORRELATIONS ); const progress = loaded / total; diff --git a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx index 1745ac17b7736..776d4677e3f7c 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx @@ -29,7 +29,6 @@ import { } from '../../../../../observability/public'; import { asPreciseDecimal } from '../../../../common/utils/formatters'; -import type { LatencyCorrelationsAsyncSearchServiceRawResponse } from '../../../../common/search_strategies/latency_correlations/types'; import { APM_SEARCH_STRATEGIES } from '../../../../common/search_strategies/constants'; import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; @@ -75,13 +74,10 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { }, startFetch, cancelFetch, - } = useSearchStrategy( - APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, - { - percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD, - analyzeCorrelations: true, - } - ); + } = useSearchStrategy(APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, { + percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD, + analyzeCorrelations: true, + }); const progress = loaded / total; useEffect(() => { diff --git a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx index 5cd936ca4c3c4..ea696fcaabe62 100644 --- a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx +++ b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx @@ -18,7 +18,6 @@ import { } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import { getDurationFormatter } from '../../../../../common/utils/formatters'; -import type { LatencyCorrelationsAsyncSearchServiceRawResponse } from '../../../../../common/search_strategies/latency_correlations/types'; import { APM_SEARCH_STRATEGIES } from '../../../../../common/search_strategies/constants'; import { useApmPluginContext } from '../../../../context/apm_plugin/use_apm_plugin_context'; import { useSearchStrategy } from '../../../../hooks/use_search_strategy'; @@ -91,10 +90,7 @@ export function TransactionDistribution({ } ); - const { - state, - data, - } = useSearchStrategy( + const { state, data } = useSearchStrategy( APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, { percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD, diff --git a/x-pack/plugins/apm/public/hooks/use_search_strategy.ts b/x-pack/plugins/apm/public/hooks/use_search_strategy.ts index e3f26fd3d3d63..7d2e97384a1d3 100644 --- a/x-pack/plugins/apm/public/hooks/use_search_strategy.ts +++ b/x-pack/plugins/apm/public/hooks/use_search_strategy.ts @@ -18,7 +18,12 @@ import { useKibana } from '../../../../../src/plugins/kibana_react/public'; import type { SearchServiceParams } from '../../common/search_strategies/types'; import type { RawResponseBase } from '../../common/search_strategies/types'; -import { ApmSearchStrategies } from '../../common/search_strategies/constants'; +import type { LatencyCorrelationsAsyncSearchServiceRawResponse } from '../../common/search_strategies/latency_correlations/types'; +import type { FailedTransactionsCorrelationsAsyncSearchServiceRawResponse } from '../../common/search_strategies/failed_transactions_correlations/types'; +import { + ApmSearchStrategies, + APM_SEARCH_STRATEGIES, +} from '../../common/search_strategies/constants'; import { useApmServiceContext } from '../context/apm_service/use_apm_service_context'; import { useUrlParams } from '../context/url_params_context/use_url_params'; @@ -29,7 +34,6 @@ import { useTimeRange } from './use_time_range'; interface SearchStrategyFetcherState { error?: Error; - isComplete: boolean; isRunning: boolean; loaded: number; total: number; @@ -44,7 +48,6 @@ const getInitialRawResponse = < } as TRawResponse); const getInitialFetchState = (): SearchStrategyFetcherState => ({ - isComplete: false, isRunning: false, loaded: 0, total: 100, @@ -55,10 +58,32 @@ const getReducer = () => (prev: T, update: Partial): T => ({ ...update, }); +interface SearchStrategyReturnBase { + state: SearchStrategyFetcherState; + startFetch: () => void; + cancelFetch: () => void; +} + +export function useSearchStrategy( + searchStrategyName: typeof APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, + options: { + percentileThreshold: number; + analyzeCorrelations: boolean; + } +): { + data: LatencyCorrelationsAsyncSearchServiceRawResponse; +} & SearchStrategyReturnBase; + +export function useSearchStrategy( + searchStrategyName: typeof APM_SEARCH_STRATEGIES.APM_FAILED_TRANSACTIONS_CORRELATIONS +): { + data: FailedTransactionsCorrelationsAsyncSearchServiceRawResponse; +} & SearchStrategyReturnBase; + export function useSearchStrategy< TRawResponse extends RawResponseBase, - TOptions extends {} = {} ->(searchStrategyName: ApmSearchStrategies, options: TOptions = {} as TOptions) { + TOptions = unknown +>(searchStrategyName: ApmSearchStrategies, options?: TOptions): unknown { const { services: { data }, } = useKibana(); @@ -90,8 +115,8 @@ export function useSearchStrategy< const startFetch = useCallback(() => { setFetchState({ + ...getInitialFetchState(), error: undefined, - isComplete: false, }); searchSubscription$.current?.unsubscribe(); abortCtrl.current.abort(); @@ -106,14 +131,14 @@ export function useSearchStrategy< kuery, start, end, - ...optionsRef.current, + ...(optionsRef.current ? { ...optionsRef.current } : {}), }, }; // Submit the search request using the `data.search` service. searchSubscription$.current = data.search .search< - IKibanaSearchRequest, + IKibanaSearchRequest, IKibanaSearchResponse >(req, { strategy: searchStrategyName, @@ -124,15 +149,14 @@ export function useSearchStrategy< setRawResponse(res.rawResponse); setFetchState({ isRunning: res.isRunning || false, - loaded: res.loaded!, - total: res.total!, + loaded: res.loaded, + total: res.total, }); if (isCompleteResponse(res)) { searchSubscription$.current?.unsubscribe(); setFetchState({ isRunning: false, - isComplete: true, }); } else if (isErrorResponse(res)) { searchSubscription$.current?.unsubscribe(); From 9104784fe065adbb2afbf54044c0f12a1511af0a Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Wed, 1 Sep 2021 14:14:10 +0200 Subject: [PATCH 11/37] [ML] Fix naming inconsistencies. --- .../failed_transactions_correlations/types.ts | 8 ++-- .../latency_correlations/types.ts | 10 ++-- .../apm/common/search_strategies/types.ts | 9 ++-- .../latency_correlations.test.tsx | 4 +- ...d_transactions_correlation_impact_label.ts | 7 ++- .../distribution/index.test.tsx | 4 +- .../apm/public/hooks/use_search_strategy.ts | 12 ++--- .../async_search_service.ts | 17 ++++--- .../async_search_service.ts | 19 +++++--- .../queries/get_query_with_params.ts | 4 +- .../queries/get_request_base.ts | 4 +- .../queries/query_correlation.ts | 6 +-- .../queries/query_failure_correlation.ts | 6 +-- .../queries/query_field_candidates.ts | 6 +-- .../queries/query_field_value_pairs.ts | 8 ++-- .../queries/query_fractions.ts | 6 +-- .../queries/query_histogram.ts | 6 +-- .../queries/query_histogram_interval.ts | 6 +-- .../queries/query_histogram_range_steps.ts | 6 +-- .../queries/query_histograms_generator.ts | 4 +- .../queries/query_percentiles.ts | 6 +-- .../search_strategies/queries/query_ranges.ts | 6 +-- .../search_strategy_provider.test.ts | 15 ++++-- .../search_strategy_provider.ts | 47 +++++++++++-------- .../tests/correlations/failed_transactions.ts | 13 +++-- .../tests/correlations/latency.ts | 16 +++++-- 26 files changed, 148 insertions(+), 107 deletions(-) diff --git a/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts index 0929a18a30d04..0815f7e1c0d1a 100644 --- a/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { RawResponseBase } from '../types'; +import { RawResponseBase, SearchStrategyClientParams } from '../types'; import { FAILED_TRANSACTIONS_IMPACT_THRESHOLD } from './constants'; @@ -22,14 +22,16 @@ export interface FailedTransactionsCorrelationValue { successPercentage: number; } -export type FailureCorrelationImpactThreshold = typeof FAILED_TRANSACTIONS_IMPACT_THRESHOLD[keyof typeof FAILED_TRANSACTIONS_IMPACT_THRESHOLD]; +export type FailedTransactionsCorrelationsImpactThreshold = typeof FAILED_TRANSACTIONS_IMPACT_THRESHOLD[keyof typeof FAILED_TRANSACTIONS_IMPACT_THRESHOLD]; export interface CorrelationsTerm { fieldName: string; fieldValue: string; } -export interface FailedTransactionsCorrelationsAsyncSearchServiceRawResponse +export type FailedTransactionsCorrelationsParams = SearchStrategyClientParams; + +export interface FailedTransactionsCorrelationsRawResponse extends RawResponseBase { log: string[]; values: FailedTransactionsCorrelationValue[]; diff --git a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts index 889fa7a682350..d2673d7df74c5 100644 --- a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { RawResponseBase } from '../types'; +import { RawResponseBase, SearchStrategyClientParams } from '../types'; export interface HistogramItem { key: number; @@ -38,8 +38,12 @@ export interface AsyncSearchProviderProgress { loadedHistograms: number; } -export interface LatencyCorrelationsAsyncSearchServiceRawResponse - extends RawResponseBase { +export interface LatencyCorrelationsParams extends SearchStrategyClientParams { + percentileThreshold: number; + analyzeCorrelations: boolean; +} + +export interface LatencyCorrelationsRawResponse extends RawResponseBase { log: string[]; overallHistogram?: HistogramItem[]; percentileThresholdValue?: number; diff --git a/x-pack/plugins/apm/common/search_strategies/types.ts b/x-pack/plugins/apm/common/search_strategies/types.ts index a5e2cb3c22048..c111fe98c9769 100644 --- a/x-pack/plugins/apm/common/search_strategies/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/types.ts @@ -10,7 +10,7 @@ export interface RawResponseBase { took: number; } -export interface SearchServiceParams { +export interface SearchStrategyClientParams { environment: string; kuery: string; serviceName?: string; @@ -18,11 +18,12 @@ export interface SearchServiceParams { transactionType?: string; start?: string; end?: string; - percentileThreshold?: number; - analyzeCorrelations?: boolean; } -export interface SearchServiceFetchParams extends SearchServiceParams { +export interface SearchStrategyServerParams { index: string; includeFrozen?: boolean; } + +export type SearchStrategyParams = SearchStrategyClientParams & + SearchStrategyServerParams; diff --git a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.test.tsx b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.test.tsx index 2729c62099259..195d262b0175e 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.test.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.test.tsx @@ -18,7 +18,7 @@ import { dataPluginMock } from 'src/plugins/data/public/mocks'; import type { IKibanaSearchResponse } from 'src/plugins/data/public'; import { EuiThemeProvider } from 'src/plugins/kibana_react/common'; import { createKibanaReactContext } from 'src/plugins/kibana_react/public'; -import type { LatencyCorrelationsAsyncSearchServiceRawResponse } from '../../../../common/search_strategies/latency_correlations/types'; +import type { LatencyCorrelationsRawResponse } from '../../../../common/search_strategies/latency_correlations/types'; import { MockUrlParamsContextProvider } from '../../../context/url_params_context/mock_url_params_context_provider'; import { ApmPluginContextValue } from '../../../context/apm_plugin/apm_plugin_context'; import { @@ -34,7 +34,7 @@ function Wrapper({ dataSearchResponse, }: { children?: ReactNode; - dataSearchResponse: IKibanaSearchResponse; + dataSearchResponse: IKibanaSearchResponse; }) { const mockDataSearch = jest.fn(() => of(dataSearchResponse)); diff --git a/x-pack/plugins/apm/public/components/app/correlations/utils/get_failed_transactions_correlation_impact_label.ts b/x-pack/plugins/apm/public/components/app/correlations/utils/get_failed_transactions_correlation_impact_label.ts index 7a8bf28058cc0..936562cf76b2a 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/utils/get_failed_transactions_correlation_impact_label.ts +++ b/x-pack/plugins/apm/public/components/app/correlations/utils/get_failed_transactions_correlation_impact_label.ts @@ -5,12 +5,15 @@ * 2.0. */ -import { FailureCorrelationImpactThreshold } from '../../../../../common/search_strategies/failed_transactions_correlations/types'; +import { FailedTransactionsCorrelationsImpactThreshold } from '../../../../../common/search_strategies/failed_transactions_correlations/types'; import { FAILED_TRANSACTIONS_IMPACT_THRESHOLD } from '../../../../../common/search_strategies/failed_transactions_correlations/constants'; export function getFailedTransactionsCorrelationImpactLabel( pValue: number -): { impact: FailureCorrelationImpactThreshold; color: string } | null { +): { + impact: FailedTransactionsCorrelationsImpactThreshold; + color: string; +} | null { // The lower the p value, the higher the impact if (pValue >= 0 && pValue < 1e-6) return { diff --git a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx index 7fee1fc94f8d0..ac5e3bf304c02 100644 --- a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx +++ b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx @@ -16,7 +16,7 @@ import { dataPluginMock } from 'src/plugins/data/public/mocks'; import type { IKibanaSearchResponse } from 'src/plugins/data/public'; import { EuiThemeProvider } from 'src/plugins/kibana_react/common'; import { createKibanaReactContext } from 'src/plugins/kibana_react/public'; -import type { LatencyCorrelationsAsyncSearchServiceRawResponse } from '../../../../../common/search_strategies/latency_correlations/types'; +import type { LatencyCorrelationsRawResponse } from '../../../../../common/search_strategies/latency_correlations/types'; import { MockUrlParamsContextProvider } from '../../../../context/url_params_context/mock_url_params_context_provider'; import { ApmPluginContextValue } from '../../../../context/apm_plugin/apm_plugin_context'; import { @@ -32,7 +32,7 @@ function Wrapper({ dataSearchResponse, }: { children?: ReactNode; - dataSearchResponse: IKibanaSearchResponse; + dataSearchResponse: IKibanaSearchResponse; }) { const mockDataSearch = jest.fn(() => of(dataSearchResponse)); diff --git a/x-pack/plugins/apm/public/hooks/use_search_strategy.ts b/x-pack/plugins/apm/public/hooks/use_search_strategy.ts index 7d2e97384a1d3..d1532570ddd56 100644 --- a/x-pack/plugins/apm/public/hooks/use_search_strategy.ts +++ b/x-pack/plugins/apm/public/hooks/use_search_strategy.ts @@ -16,10 +16,10 @@ import { } from '../../../../../src/plugins/data/public'; import { useKibana } from '../../../../../src/plugins/kibana_react/public'; -import type { SearchServiceParams } from '../../common/search_strategies/types'; +import type { SearchStrategyClientParams } from '../../common/search_strategies/types'; import type { RawResponseBase } from '../../common/search_strategies/types'; -import type { LatencyCorrelationsAsyncSearchServiceRawResponse } from '../../common/search_strategies/latency_correlations/types'; -import type { FailedTransactionsCorrelationsAsyncSearchServiceRawResponse } from '../../common/search_strategies/failed_transactions_correlations/types'; +import type { LatencyCorrelationsRawResponse } from '../../common/search_strategies/latency_correlations/types'; +import type { FailedTransactionsCorrelationsRawResponse } from '../../common/search_strategies/failed_transactions_correlations/types'; import { ApmSearchStrategies, APM_SEARCH_STRATEGIES, @@ -71,13 +71,13 @@ export function useSearchStrategy( analyzeCorrelations: boolean; } ): { - data: LatencyCorrelationsAsyncSearchServiceRawResponse; + data: LatencyCorrelationsRawResponse; } & SearchStrategyReturnBase; export function useSearchStrategy( searchStrategyName: typeof APM_SEARCH_STRATEGIES.APM_FAILED_TRANSACTIONS_CORRELATIONS ): { - data: FailedTransactionsCorrelationsAsyncSearchServiceRawResponse; + data: FailedTransactionsCorrelationsRawResponse; } & SearchStrategyReturnBase; export function useSearchStrategy< @@ -138,7 +138,7 @@ export function useSearchStrategy< // Submit the search request using the `data.search` service. searchSubscription$.current = data.search .search< - IKibanaSearchRequest, + IKibanaSearchRequest, IKibanaSearchResponse >(req, { strategy: searchStrategyName, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts index aa7861f11214a..518e5dfd1d626 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts @@ -10,11 +10,11 @@ import type { ElasticsearchClient } from 'src/core/server'; import { chunk } from 'lodash'; import { EVENT_OUTCOME } from '../../../../common/elasticsearch_fieldnames'; +import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; import type { - SearchServiceParams, - SearchServiceFetchParams, -} from '../../../../common/search_strategies/types'; -import type { FailedTransactionsCorrelationsAsyncSearchServiceRawResponse } from '../../../../common/search_strategies/failed_transactions_correlations/types'; + FailedTransactionsCorrelationsParams, + FailedTransactionsCorrelationsRawResponse, +} from '../../../../common/search_strategies/failed_transactions_correlations/types'; import type { ApmIndicesConfig } from '../../settings/apm_indices/get_apm_indices'; import { asyncSearchServiceLogProvider } from '../async_search_service_log'; import { @@ -27,10 +27,13 @@ import { asyncFailedTransactionsCorrelationsSearchServiceStateProvider } from '. import { ERROR_CORRELATION_THRESHOLD } from '../constants'; -export const failedTransactionsCorrelationsAsyncSearchServiceProvider: AsyncSearchServiceProvider = ( +export const failedTransactionsCorrelationsAsyncSearchServiceProvider: AsyncSearchServiceProvider< + FailedTransactionsCorrelationsParams, + FailedTransactionsCorrelationsRawResponse +> = ( esClient: ElasticsearchClient, getApmIndices: () => Promise, - searchServiceParams: SearchServiceParams, + searchServiceParams: FailedTransactionsCorrelationsParams, includeFrozen: boolean ) => { const { addLogMessage, getLogMessages } = asyncSearchServiceLogProvider(); @@ -40,7 +43,7 @@ export const failedTransactionsCorrelationsAsyncSearchServiceProvider: AsyncSear async function fetchErrorCorrelations() { try { const indices = await getApmIndices(); - const params: SearchServiceFetchParams = { + const params: SearchStrategyParams = { ...searchServiceParams, index: indices['apm_oss.transactionIndices'], includeFrozen, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts index c66238b3b317b..fca73ed5f1bc3 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts @@ -7,11 +7,11 @@ import { range } from 'lodash'; import type { ElasticsearchClient } from 'src/core/server'; +import type { SearchStrategyServerParams } from '../../../../common/search_strategies/types'; import type { - SearchServiceParams, - SearchServiceFetchParams, -} from '../../../../common/search_strategies/types'; -import type { LatencyCorrelationsAsyncSearchServiceRawResponse } from '../../../../common/search_strategies/latency_correlations/types'; + LatencyCorrelationsParams, + LatencyCorrelationsRawResponse, +} from '../../../../common/search_strategies/latency_correlations/types'; import type { ApmIndicesConfig } from '../../settings/apm_indices/get_apm_indices'; @@ -30,10 +30,13 @@ import type { AsyncSearchServiceProvider } from '../search_strategy_provider'; import { asyncSearchServiceStateProvider } from './async_search_service_state'; -export const latencyCorrelationsAsyncSearchServiceProvider: AsyncSearchServiceProvider = ( +export const latencyCorrelationsAsyncSearchServiceProvider: AsyncSearchServiceProvider< + LatencyCorrelationsParams, + LatencyCorrelationsRawResponse +> = ( esClient: ElasticsearchClient, getApmIndices: () => Promise, - searchServiceParams: SearchServiceParams, + searchServiceParams: LatencyCorrelationsParams, includeFrozen: boolean ) => { const { addLogMessage, getLogMessages } = asyncSearchServiceLogProvider(); @@ -41,7 +44,9 @@ export const latencyCorrelationsAsyncSearchServiceProvider: AsyncSearchServicePr const state = asyncSearchServiceStateProvider(); async function fetchCorrelations() { - let params: SearchServiceFetchParams | undefined; + let params: + | (LatencyCorrelationsParams & SearchStrategyServerParams) + | undefined; try { const indices = await getApmIndices(); diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts index 244f42097874c..6220e1be098ff 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts @@ -10,7 +10,7 @@ import { getOrElse } from 'fp-ts/lib/Either'; import { pipe } from 'fp-ts/lib/pipeable'; import * as t from 'io-ts'; import { failure } from 'io-ts/lib/PathReporter'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; +import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; import { rangeRt } from '../../../routes/default_api_types'; import { getCorrelationsFilters } from '../../correlations/get_filters'; import { Setup, SetupTimeRange } from '../../helpers/setup_request'; @@ -23,7 +23,7 @@ export const getTermsQuery = ( }; interface QueryParams { - params: SearchServiceFetchParams; + params: SearchStrategyParams; fieldName?: string; fieldValue?: string; } diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.ts index cedda3ed633fe..fb1639b5d5f4a 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_request_base.ts @@ -5,12 +5,12 @@ * 2.0. */ -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; +import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; export const getRequestBase = ({ index, includeFrozen, -}: SearchServiceFetchParams) => ({ +}: SearchStrategyParams) => ({ index, // matches APM's event client settings ignore_throttled: includeFrozen === undefined ? true : !includeFrozen, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts index 02bb97f355a67..53a6d1694c667 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts @@ -10,7 +10,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; +import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; @@ -41,7 +41,7 @@ export interface BucketCorrelation { } export const getTransactionDurationCorrelationRequest = ( - params: SearchServiceFetchParams, + params: SearchStrategyParams, expectations: number[], ranges: estypes.AggregationsAggregationRange[], fractions: number[], @@ -96,7 +96,7 @@ export const getTransactionDurationCorrelationRequest = ( export const fetchTransactionDurationCorrelation = async ( esClient: ElasticsearchClient, - params: SearchServiceFetchParams, + params: SearchStrategyParams, expectations: number[], ranges: estypes.AggregationsAggregationRange[], fractions: number[], diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts index d654e69121885..192c298fa7721 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts @@ -6,14 +6,14 @@ */ import { estypes } from '@elastic/elasticsearch'; import { ElasticsearchClient } from 'kibana/server'; -import { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; +import { SearchStrategyParams } from '../../../../common/search_strategies/types'; import { EVENT_OUTCOME } from '../../../../common/elasticsearch_fieldnames'; import { EventOutcome } from '../../../../common/event_outcome'; import { getQueryWithParams, getTermsQuery } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; export const getFailureCorrelationRequest = ( - params: SearchServiceFetchParams, + params: SearchStrategyParams, fieldName: string ): estypes.SearchRequest => { const query = getQueryWithParams({ @@ -59,7 +59,7 @@ export const getFailureCorrelationRequest = ( export const fetchFailedTransactionsCorrelationPValues = async ( esClient: ElasticsearchClient, - params: SearchServiceFetchParams, + params: SearchStrategyParams, fieldName: string ) => { const resp = await esClient.search( diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts index a3e823eaf4f69..abcd5810fb5cb 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts @@ -9,7 +9,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; +import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; import { FIELD_PREFIX_TO_EXCLUDE_AS_CANDIDATE, @@ -33,7 +33,7 @@ export const shouldBeExcluded = (fieldName: string) => { }; export const getRandomDocsRequest = ( - params: SearchServiceFetchParams + params: SearchStrategyParams ): estypes.SearchRequest => ({ ...getRequestBase(params), body: { @@ -52,7 +52,7 @@ export const getRandomDocsRequest = ( export const fetchTransactionDurationFieldCandidates = async ( esClient: ElasticsearchClient, - params: SearchServiceFetchParams + params: SearchStrategyParams ): Promise<{ fieldCandidates: FieldName[] }> => { const { index } = params; // Get all fields with keyword mapping diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts index 15a47063771b7..71322d03ffb44 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts @@ -9,7 +9,7 @@ import type { ElasticsearchClient } from 'src/core/server'; import type { estypes } from '@elastic/elasticsearch'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; +import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; import type { AsyncSearchServiceLog } from '../async_search_service_log'; import type { AsyncSearchServiceState } from '../latency_correlations/async_search_service_state'; @@ -27,7 +27,7 @@ interface FieldValuePair { export type FieldValuePairs = FieldValuePair[]; export const getTermsAggRequest = ( - params: SearchServiceFetchParams, + params: SearchStrategyParams, fieldName: FieldName ): estypes.SearchRequest => ({ ...getRequestBase(params), @@ -47,7 +47,7 @@ export const getTermsAggRequest = ( const fetchTransactionDurationFieldTerms = async ( esClient: ElasticsearchClient, - params: SearchServiceFetchParams, + params: SearchStrategyParams, fieldName: string, addLogMessage: AsyncSearchServiceLog['addLogMessage'] ): Promise => { @@ -96,7 +96,7 @@ async function fetchInSequence( export const fetchTransactionDurationFieldValuePairs = async ( esClient: ElasticsearchClient, - params: SearchServiceFetchParams, + params: SearchStrategyParams, fieldCandidates: FieldName[], state: AsyncSearchServiceState, addLogMessage: AsyncSearchServiceLog['addLogMessage'] diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.ts index 3dd10c4529a22..25e5f62564b04 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_fractions.ts @@ -8,14 +8,14 @@ import { ElasticsearchClient } from 'kibana/server'; import { estypes } from '@elastic/elasticsearch'; -import { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; +import { SearchStrategyParams } from '../../../../common/search_strategies/types'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; export const getTransactionDurationRangesRequest = ( - params: SearchServiceFetchParams, + params: SearchStrategyParams, ranges: estypes.AggregationsAggregationRange[] ): estypes.SearchRequest => ({ ...getRequestBase(params), @@ -38,7 +38,7 @@ export const getTransactionDurationRangesRequest = ( */ export const fetchTransactionDurationFractions = async ( esClient: ElasticsearchClient, - params: SearchServiceFetchParams, + params: SearchStrategyParams, ranges: estypes.AggregationsAggregationRange[] ): Promise<{ fractions: number[]; totalDocCount: number }> => { const resp = await esClient.search( diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts index 9329e4f2795fe..56466728bee3c 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts @@ -14,13 +14,13 @@ import type { HistogramItem, ResponseHit, } from '../../../../common/search_strategies/latency_correlations/types'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; +import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; export const getTransactionDurationHistogramRequest = ( - params: SearchServiceFetchParams, + params: SearchStrategyParams, interval: number, fieldName?: string, fieldValue?: string @@ -39,7 +39,7 @@ export const getTransactionDurationHistogramRequest = ( export const fetchTransactionDurationHistogram = async ( esClient: ElasticsearchClient, - params: SearchServiceFetchParams, + params: SearchStrategyParams, interval: number, fieldName?: string, fieldValue?: string diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.ts index d1dbe578e2997..7a8752e45c6f2 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_interval.ts @@ -10,7 +10,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; +import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; @@ -18,7 +18,7 @@ import { getRequestBase } from './get_request_base'; const HISTOGRAM_INTERVALS = 1000; export const getHistogramIntervalRequest = ( - params: SearchServiceFetchParams + params: SearchStrategyParams ): estypes.SearchRequest => ({ ...getRequestBase(params), body: { @@ -33,7 +33,7 @@ export const getHistogramIntervalRequest = ( export const fetchTransactionDurationHistogramInterval = async ( esClient: ElasticsearchClient, - params: SearchServiceFetchParams + params: SearchStrategyParams ): Promise => { const resp = await esClient.search(getHistogramIntervalRequest(params)); diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.ts index b7912a44b6caa..31ab7392155bc 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram_range_steps.ts @@ -12,7 +12,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; +import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; @@ -26,7 +26,7 @@ const getHistogramRangeSteps = (min: number, max: number, steps: number) => { }; export const getHistogramIntervalRequest = ( - params: SearchServiceFetchParams + params: SearchStrategyParams ): estypes.SearchRequest => ({ ...getRequestBase(params), body: { @@ -41,7 +41,7 @@ export const getHistogramIntervalRequest = ( export const fetchTransactionDurationHistogramRangeSteps = async ( esClient: ElasticsearchClient, - params: SearchServiceFetchParams + params: SearchStrategyParams ): Promise => { const steps = 100; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts index dfaf7838cea00..682fbf6b32a75 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts @@ -9,7 +9,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; +import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; import type { AsyncSearchServiceLog } from '../async_search_service_log'; import type { AsyncSearchServiceState } from '../latency_correlations/async_search_service_state'; @@ -24,7 +24,7 @@ import type { FieldValuePairs } from './query_field_value_pairs'; export async function* fetchTransactionDurationHistograms( esClient: ElasticsearchClient, addLogMessage: AsyncSearchServiceLog['addLogMessage'], - params: SearchServiceFetchParams, + params: SearchStrategyParams, state: AsyncSearchServiceState, expectations: number[], ranges: estypes.AggregationsAggregationRange[], diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts index 70cc36e22482f..88efd70ebc304 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts @@ -10,7 +10,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; +import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; @@ -29,7 +29,7 @@ interface ResponseHit { } export const getTransactionDurationPercentilesRequest = ( - params: SearchServiceFetchParams, + params: SearchStrategyParams, percents?: number[], fieldName?: string, fieldValue?: string @@ -59,7 +59,7 @@ export const getTransactionDurationPercentilesRequest = ( export const fetchTransactionDurationPercentiles = async ( esClient: ElasticsearchClient, - params: SearchServiceFetchParams, + params: SearchStrategyParams, percents?: number[], fieldName?: string, fieldValue?: string diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts index 2131dd2598f37..2b767b58d4e9c 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts @@ -10,7 +10,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchServiceFetchParams } from '../../../../common/search_strategies/types'; +import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; @@ -28,7 +28,7 @@ interface ResponseHit { } export const getTransactionDurationRangesRequest = ( - params: SearchServiceFetchParams, + params: SearchStrategyParams, rangesSteps: number[], fieldName?: string, fieldValue?: string @@ -66,7 +66,7 @@ export const getTransactionDurationRangesRequest = ( export const fetchTransactionDurationRanges = async ( esClient: ElasticsearchClient, - params: SearchServiceFetchParams, + params: SearchStrategyParams, rangesSteps: number[], fieldName?: string, fieldValue?: string diff --git a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts index 43bb77d70c4ba..141c7b29bd877 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts @@ -8,16 +8,17 @@ import type { estypes } from '@elastic/elasticsearch'; import { SearchStrategyDependencies } from 'src/plugins/data/server'; + +import { IKibanaSearchRequest } from '../../../../../../src/plugins/data/common'; + import { ENVIRONMENT_ALL } from '../../../common/environment_filter_values'; +import type { LatencyCorrelationsParams } from '../../../common/search_strategies/latency_correlations/types'; import type { ApmIndicesConfig } from '../settings/apm_indices/get_apm_indices'; import { latencyCorrelationsAsyncSearchServiceProvider } from './latency_correlations'; -import { - searchStrategyProvider, - PartialSearchRequest, -} from './search_strategy_provider'; +import { searchStrategyProvider } from './search_strategy_provider'; // helper to trigger promises in the async search service const flushPromises = () => new Promise(setImmediate); @@ -123,7 +124,9 @@ describe('APM Correlations search strategy', () => { let mockClientSearch: jest.Mock; let mockGetApmIndicesMock: jest.Mock; let mockDeps: SearchStrategyDependencies; - let params: Required['params']; + let params: Required< + IKibanaSearchRequest + >['params']; beforeEach(() => { mockClientFieldCaps = jest.fn(clientFieldCapsMock); @@ -142,6 +145,8 @@ describe('APM Correlations search strategy', () => { end: '2021', environment: ENVIRONMENT_ALL.value, kuery: '', + percentileThreshold: 95, + analyzeCorrelations: true, }; }); diff --git a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts index 17d618e33e5c3..9ce58cd144dc8 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts @@ -16,13 +16,11 @@ import { IKibanaSearchResponse, } from '../../../../../../src/plugins/data/common'; -import type { SearchServiceParams } from '../../../common/search_strategies/types'; +import type { SearchStrategyClientParams } from '../../../common/search_strategies/types'; import type { RawResponseBase } from '../../../common/search_strategies/types'; import type { ApmIndicesConfig } from '../settings/apm_indices/get_apm_indices'; -export type PartialSearchRequest = IKibanaSearchRequest; - -interface AsyncSearchServiceState { +interface AsyncSearchServiceState { cancel: () => void; error: Error; meta: { @@ -31,31 +29,40 @@ interface AsyncSearchServiceState { isRunning: boolean; isPartial: boolean; }; - rawResponse: RawResponse; + rawResponse: TRawResponse; } type GetAsyncSearchServiceState< - RawResponse extends RawResponseBase -> = () => AsyncSearchServiceState; + TRawResponse extends RawResponseBase +> = () => AsyncSearchServiceState; -export type AsyncSearchServiceProvider = ( +export type AsyncSearchServiceProvider< + TSearchStrategyClientParams extends SearchStrategyClientParams, + TRawResponse extends RawResponseBase +> = ( esClient: ElasticsearchClient, getApmIndices: () => Promise, - searchServiceParams: SearchServiceParams, + searchServiceParams: TSearchStrategyClientParams, includeFrozen: boolean -) => GetAsyncSearchServiceState; - -export const searchStrategyProvider = ( - asyncSearchServiceProvider: AsyncSearchServiceProvider, +) => GetAsyncSearchServiceState; + +export function searchStrategyProvider< + TSearchStrategyClientParams extends SearchStrategyClientParams, + TRawResponse extends RawResponseBase +>( + asyncSearchServiceProvider: AsyncSearchServiceProvider< + TSearchStrategyClientParams, + TRawResponse + >, getApmIndices: () => Promise, includeFrozen: boolean ): ISearchStrategy< - PartialSearchRequest, - IKibanaSearchResponse -> => { + IKibanaSearchRequest, + IKibanaSearchResponse +> { const asyncSearchServiceMap = new Map< string, - GetAsyncSearchServiceState + GetAsyncSearchServiceState >(); return { @@ -66,7 +73,7 @@ export const searchStrategyProvider = ( // The function to fetch the current state of the async search service. // This will be either an existing service for a follow up fetch or a new one for new requests. - let getAsyncSearchServiceState: GetAsyncSearchServiceState; + let getAsyncSearchServiceState: GetAsyncSearchServiceState; // If the request includes an ID, we require that the async search service already exists // otherwise we throw an error. The client should never poll a service that's been cancelled or finished. @@ -87,7 +94,7 @@ export const searchStrategyProvider = ( getAsyncSearchServiceState = asyncSearchServiceProvider( deps.esClient.asCurrentUser, getApmIndices, - request.params, + request.params as TSearchStrategyClientParams, includeFrozen ); } @@ -120,4 +127,4 @@ export const searchStrategyProvider = ( } }, }; -}; +} diff --git a/x-pack/test/apm_api_integration/tests/correlations/failed_transactions.ts b/x-pack/test/apm_api_integration/tests/correlations/failed_transactions.ts index eb3473691838e..9f7e2dc63ad80 100644 --- a/x-pack/test/apm_api_integration/tests/correlations/failed_transactions.ts +++ b/x-pack/test/apm_api_integration/tests/correlations/failed_transactions.ts @@ -6,9 +6,14 @@ */ import expect from '@kbn/expect'; + +import { IKibanaSearchRequest } from '../../../../../src/plugins/data/common'; + +import type { FailedTransactionsCorrelationsParams } from '../../../../plugins/apm/common/search_strategies/failed_transactions_correlations/types'; +import { APM_SEARCH_STRATEGIES } from '../../../../plugins/apm/common/search_strategies/constants'; + import { FtrProviderContext } from '../../common/ftr_provider_context'; import { registry } from '../../common/registry'; -import type { PartialSearchRequest } from '../../../../plugins/apm/server/lib/search_strategies/search_strategy_provider'; import { parseBfetchResponse } from '../../common/utils/parse_b_fetch'; export default function ApiTest({ getService }: FtrProviderContext) { @@ -16,7 +21,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { const supertest = getService('supertest'); const getRequestBody = () => { - const partialSearchRequest: PartialSearchRequest = { + const request: IKibanaSearchRequest = { params: { environment: 'ENVIRONMENT_ALL', start: '2020', @@ -28,8 +33,8 @@ export default function ApiTest({ getService }: FtrProviderContext) { return { batch: [ { - request: partialSearchRequest, - options: { strategy: 'apmFailedTransactionsCorrelationsSearchStrategy' }, + request, + options: { strategy: APM_SEARCH_STRATEGIES.APM_FAILED_TRANSACTIONS_CORRELATIONS }, }, ], }; diff --git a/x-pack/test/apm_api_integration/tests/correlations/latency.ts b/x-pack/test/apm_api_integration/tests/correlations/latency.ts index 0a85423911709..262969fe60258 100644 --- a/x-pack/test/apm_api_integration/tests/correlations/latency.ts +++ b/x-pack/test/apm_api_integration/tests/correlations/latency.ts @@ -6,9 +6,14 @@ */ import expect from '@kbn/expect'; + +import { IKibanaSearchRequest } from '../../../../../src/plugins/data/common'; + +import type { LatencyCorrelationsParams } from '../../../../plugins/apm/common/search_strategies/latency_correlations/types'; +import { APM_SEARCH_STRATEGIES } from '../../../../plugins/apm/common/search_strategies/constants'; + import { FtrProviderContext } from '../../common/ftr_provider_context'; import { registry } from '../../common/registry'; -import type { PartialSearchRequest } from '../../../../plugins/apm/server/lib/search_strategies/search_strategy_provider'; import { parseBfetchResponse } from '../../common/utils/parse_b_fetch'; export default function ApiTest({ getService }: FtrProviderContext) { @@ -16,21 +21,22 @@ export default function ApiTest({ getService }: FtrProviderContext) { const supertest = getService('supertest'); const getRequestBody = () => { - const partialSearchRequest: PartialSearchRequest = { + const request: IKibanaSearchRequest = { params: { environment: 'ENVIRONMENT_ALL', start: '2020', end: '2021', - percentileThreshold: 95, kuery: '', + percentileThreshold: 95, + analyzeCorrelations: true, }, }; return { batch: [ { - request: partialSearchRequest, - options: { strategy: 'apmCorrelationsSearchStrategy' }, + request, + options: { strategy: APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS }, }, ], }; From 9a4b99d4b98199e93aa74e87a0c6e079aa5595c9 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Wed, 1 Sep 2021 15:43:33 +0200 Subject: [PATCH 12/37] [ML] Fix jest test. --- .../lib/search_strategies/search_strategy_provider.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts index 141c7b29bd877..1732208dc7296 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts @@ -188,6 +188,7 @@ describe('APM Correlations search strategy', () => { percentiles: { field: 'transaction.duration.us', hdr: { number_of_significant_value_digits: 3 }, + percents: [95], }, }, }, From afbcd8bb9ac63136524fd9315260c8f303db3817 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Wed, 1 Sep 2021 16:22:05 +0200 Subject: [PATCH 13/37] [ML] Fix chart loading behavior. --- .../app/correlations/latency_correlations.tsx | 28 +++++++++---------- .../distribution/index.tsx | 9 ++++-- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx index 776d4677e3f7c..89fc54a7909b6 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx @@ -63,21 +63,19 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { const displayLog = uiSettings.get(enableInspectEsQueries); - const { - state: { error, isRunning, loaded, total }, - data: { - ccsWarning, - log, - values, - percentileThresholdValue, - overallHistogram, - }, - startFetch, - cancelFetch, - } = useSearchStrategy(APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, { - percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD, - analyzeCorrelations: true, - }); + const { state, data, startFetch, cancelFetch } = useSearchStrategy( + APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, + { + percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD, + analyzeCorrelations: true, + } + ); + const { error, isRunning, loaded, total } = state; + const { ccsWarning, log, values, percentileThresholdValue } = data; + const overallHistogram = + data.overallHistogram === undefined && !isRunning + ? [] + : data.overallHistogram; const progress = loaded / total; useEffect(() => { diff --git a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx index ea696fcaabe62..41d75b0c4435c 100644 --- a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx +++ b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx @@ -97,9 +97,12 @@ export function TransactionDistribution({ analyzeCorrelations: false, } ); - - const { error } = state; - const { percentileThresholdValue, overallHistogram } = data; + const { error, isRunning } = state; + const { percentileThresholdValue } = data; + const overallHistogram = + data.overallHistogram === undefined && !isRunning + ? [] + : data.overallHistogram; useEffect(() => { if (isErrorMessage(error)) { From f1312834780b3f1c0b1daaa282eb584d96e9e643 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Wed, 1 Sep 2021 16:39:21 +0200 Subject: [PATCH 14/37] [ML] Rename values to latencyCorrelations. --- .../latency_correlations/types.ts | 4 +-- .../latency_correlations.test.tsx | 14 +++++++++-- .../app/correlations/latency_correlations.tsx | 25 ++++++++++++------- .../distribution/index.test.tsx | 14 +++++++++-- .../async_search_service.ts | 4 +-- .../async_search_service_state.ts | 16 ++++++------ 6 files changed, 52 insertions(+), 25 deletions(-) diff --git a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts index d2673d7df74c5..da2742308b28c 100644 --- a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts @@ -20,7 +20,7 @@ export interface ResponseHit { _source: ResponseHitSource; } -export interface SearchServiceValue { +export interface LatencyCorrelation { histogram: HistogramItem[]; value: string; field: string; @@ -47,5 +47,5 @@ export interface LatencyCorrelationsRawResponse extends RawResponseBase { log: string[]; overallHistogram?: HistogramItem[]; percentileThresholdValue?: number; - values?: SearchServiceValue[]; + latencyCorrelations?: LatencyCorrelation[]; } diff --git a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.test.tsx b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.test.tsx index 195d262b0175e..c1fb1beb1918e 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.test.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.test.tsx @@ -97,7 +97,12 @@ describe('correlations', () => { @@ -115,7 +120,12 @@ describe('correlations', () => { diff --git a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx index 89fc54a7909b6..0fda04d2f99e8 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx @@ -71,7 +71,12 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { } ); const { error, isRunning, loaded, total } = state; - const { ccsWarning, log, values, percentileThresholdValue } = data; + const { + ccsWarning, + log, + latencyCorrelations, + percentileThresholdValue, + } = data; const overallHistogram = data.overallHistogram === undefined && !isRunning ? [] @@ -99,21 +104,23 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { const selectedHistogram = useMemo(() => { let selected = - Array.isArray(values) && values.length > 0 ? values[0] : undefined; + Array.isArray(latencyCorrelations) && latencyCorrelations.length > 0 + ? latencyCorrelations[0] + : undefined; if ( - Array.isArray(values) && - values.length > 0 && + Array.isArray(latencyCorrelations) && + latencyCorrelations.length > 0 && selectedSignificantTerm !== null ) { - selected = values.find( + selected = latencyCorrelations.find( (h) => h.field === selectedSignificantTerm.fieldName && h.value === selectedSignificantTerm.fieldValue ); } return selected; - }, [values, selectedSignificantTerm]); + }, [latencyCorrelations, selectedSignificantTerm]); const history = useHistory(); const trackApmEvent = useUiTracker({ app: 'apm' }); @@ -241,11 +248,11 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { }, []); const { histogramTerms, sorting } = useMemo(() => { - if (!Array.isArray(values)) { + if (!Array.isArray(latencyCorrelations)) { return { histogramTerms: [], sorting: undefined }; } const orderedTerms = orderBy( - values.map((d) => { + latencyCorrelations.map((d) => { return { fieldName: d.field, fieldValue: d.value, @@ -267,7 +274,7 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { }, } as EuiTableSortingType, }; - }, [values, sortField, sortDirection]); + }, [latencyCorrelations, sortField, sortDirection]); return (
diff --git a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx index ac5e3bf304c02..1e13a0dbe316d 100644 --- a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx +++ b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx @@ -106,7 +106,12 @@ describe('transaction_details/distribution', () => { { { }; } - const values: SearchServiceValue[] = []; - function addValue(d: SearchServiceValue) { - values.push(d); + const latencyCorrelations: LatencyCorrelation[] = []; + function addValue(d: LatencyCorrelation) { + latencyCorrelations.push(d); } - function getValuesSortedByCorrelation() { - return values.sort((a, b) => b.correlation - a.correlation); + function getLatencyCorrelationsSortedByCorrelation() { + return latencyCorrelations.sort((a, b) => b.correlation - a.correlation); } function getState() { @@ -90,7 +90,7 @@ export const asyncSearchServiceStateProvider = () => { overallHistogram, percentileThresholdValue, progress, - values, + latencyCorrelations, }; } @@ -99,7 +99,7 @@ export const asyncSearchServiceStateProvider = () => { getIsCancelled, getOverallProgress, getState, - getValuesSortedByCorrelation, + getLatencyCorrelationsSortedByCorrelation, setCcsWarning, setError, setIsCancelled, From c9b304001803414f3f882ec8db3cc7ef82272d90 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Wed, 1 Sep 2021 16:59:54 +0200 Subject: [PATCH 15/37] [ML] Clean up types. --- .../latency_correlations/types.ts | 19 +++++-------------- .../apm/common/search_strategies/types.ts | 13 +++++++++++++ .../distribution/index.tsx | 3 +++ .../transaction_distribution_chart/index.tsx | 2 +- .../async_search_service_state.ts | 3 +-- .../lib/search_strategies/queries/index.ts | 2 +- .../queries/query_correlation.ts | 17 ++++------------- .../queries/query_histogram.ts | 4 ++-- .../queries/query_percentiles.ts | 17 ++++------------- .../search_strategies/queries/query_ranges.ts | 17 ++++------------- 10 files changed, 38 insertions(+), 59 deletions(-) diff --git a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts index da2742308b28c..acab8314a19fc 100644 --- a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts @@ -5,20 +5,11 @@ * 2.0. */ -import { RawResponseBase, SearchStrategyClientParams } from '../types'; - -export interface HistogramItem { - key: number; - doc_count: number; -} - -export interface ResponseHitSource { - [s: string]: unknown; -} - -export interface ResponseHit { - _source: ResponseHitSource; -} +import { + HistogramItem, + RawResponseBase, + SearchStrategyClientParams, +} from '../types'; export interface LatencyCorrelation { histogram: HistogramItem[]; diff --git a/x-pack/plugins/apm/common/search_strategies/types.ts b/x-pack/plugins/apm/common/search_strategies/types.ts index c111fe98c9769..564f33697072a 100644 --- a/x-pack/plugins/apm/common/search_strategies/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/types.ts @@ -5,6 +5,19 @@ * 2.0. */ +export interface HistogramItem { + key: number; + doc_count: number; +} + +export interface ResponseHitSource { + [s: string]: unknown; +} + +export interface ResponseHit { + _source: ResponseHitSource; +} + export interface RawResponseBase { ccsWarning: boolean; took: number; diff --git a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx index 41d75b0c4435c..ec926add9ebc5 100644 --- a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx +++ b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx @@ -99,6 +99,9 @@ export function TransactionDistribution({ ); const { error, isRunning } = state; const { percentileThresholdValue } = data; + // If loading stopped but we didn't receive any histogram data + // set it to an empty error so that the chart component finishes + // its loading state. const overallHistogram = data.overallHistogram === undefined && !isRunning ? [] diff --git a/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx b/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx index aaeb83ea97043..eb3c1506e3e2e 100644 --- a/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx +++ b/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx @@ -30,7 +30,7 @@ import { i18n } from '@kbn/i18n'; import { useChartTheme } from '../../../../../../observability/public'; import { getDurationFormatter } from '../../../../../common/utils/formatters'; -import type { HistogramItem } from '../../../../../common/search_strategies/latency_correlations/types'; +import type { HistogramItem } from '../../../../../common/search_strategies/types'; import { FETCH_STATUS } from '../../../../hooks/use_fetcher'; import { useTheme } from '../../../../hooks/use_theme'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts index 6d10db34d96e5..bbb9970ca02ed 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts @@ -5,13 +5,12 @@ * 2.0. */ +import type { HistogramItem } from '../../../../common/search_strategies/types'; import type { AsyncSearchProviderProgress, LatencyCorrelation, } from '../../../../common/search_strategies/latency_correlations/types'; -import type { HistogramItem } from '../queries'; - export const asyncSearchServiceStateProvider = () => { let ccsWarning = false; function setCcsWarning(d: boolean) { diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/index.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/index.ts index ce24ccaf320e5..e691b81e4adcf 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/index.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/index.ts @@ -13,4 +13,4 @@ export { fetchTransactionDurationPercentiles } from './query_percentiles'; export { fetchTransactionDurationCorrelation } from './query_correlation'; export { fetchTransactionDurationHistograms } from './query_histograms_generator'; export { fetchTransactionDurationHistogramRangeSteps } from './query_histogram_range_steps'; -export { fetchTransactionDurationRanges, HistogramItem } from './query_ranges'; +export { fetchTransactionDurationRanges } from './query_ranges'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts index 53a6d1694c667..b00023d417ac2 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts @@ -10,23 +10,14 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; +import type { + ResponseHit, + SearchStrategyParams, +} from '../../../../common/search_strategies/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; -export interface HistogramItem { - key: number; - doc_count: number; -} - -interface ResponseHitSource { - [s: string]: unknown; -} -interface ResponseHit { - _source: ResponseHitSource; -} - export interface BucketCorrelation { buckets_path: string; function: { diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts index 56466728bee3c..d9a2d0a6264d3 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts @@ -13,8 +13,8 @@ import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldname import type { HistogramItem, ResponseHit, -} from '../../../../common/search_strategies/latency_correlations/types'; -import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; + SearchStrategyParams, +} from '../../../../common/search_strategies/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts index 88efd70ebc304..a1d0c4c560df4 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts @@ -10,24 +10,15 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; +import type { + ResponseHit, + SearchStrategyParams, +} from '../../../../common/search_strategies/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; import { SIGNIFICANT_VALUE_DIGITS } from '../constants'; -export interface HistogramItem { - key: number; - doc_count: number; -} - -interface ResponseHitSource { - [s: string]: unknown; -} -interface ResponseHit { - _source: ResponseHitSource; -} - export const getTransactionDurationPercentilesRequest = ( params: SearchStrategyParams, percents?: number[], diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts index 2b767b58d4e9c..29f25bc277c1c 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts @@ -10,23 +10,14 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; -import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; +import type { + ResponseHit, + SearchStrategyParams, +} from '../../../../common/search_strategies/types'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; -export interface HistogramItem { - key: number; - doc_count: number; -} - -interface ResponseHitSource { - [s: string]: unknown; -} -interface ResponseHit { - _source: ResponseHitSource; -} - export const getTransactionDurationRangesRequest = ( params: SearchStrategyParams, rangesSteps: number[], From ecd1d75a68e055a0bda6660604bd65dfc0813619 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Wed, 1 Sep 2021 17:28:07 +0200 Subject: [PATCH 16/37] [ML] Add function overloads. --- .../async_search_service.ts | 19 ++++++++++++--- .../failed_transactions_correlations/index.ts | 6 ++++- .../async_search_service.ts | 18 +++++++++++++-- .../latency_correlations/index.ts | 6 ++++- .../search_strategy_provider.ts | 23 +++++++++++++++++++ 5 files changed, 65 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts index 518e5dfd1d626..7c4b87c4bbff8 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts @@ -5,9 +5,15 @@ * 2.0. */ +import { chunk } from 'lodash'; + import type { ElasticsearchClient } from 'src/core/server'; -import { chunk } from 'lodash'; +import type { ISearchStrategy } from '../../../../../../../src/plugins/data/server'; +import { + IKibanaSearchRequest, + IKibanaSearchResponse, +} from '../../../../../../../src/plugins/data/common'; import { EVENT_OUTCOME } from '../../../../common/elasticsearch_fieldnames'; import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; @@ -27,10 +33,17 @@ import { asyncFailedTransactionsCorrelationsSearchServiceStateProvider } from '. import { ERROR_CORRELATION_THRESHOLD } from '../constants'; -export const failedTransactionsCorrelationsAsyncSearchServiceProvider: AsyncSearchServiceProvider< +export type FailedTransactionsCorrelationsAsyncSearchServiceProvider = AsyncSearchServiceProvider< FailedTransactionsCorrelationsParams, FailedTransactionsCorrelationsRawResponse -> = ( +>; + +export type FailedTransactionsCorrelationsSearchStrategy = ISearchStrategy< + IKibanaSearchRequest, + IKibanaSearchResponse +>; + +export const failedTransactionsCorrelationsAsyncSearchServiceProvider: FailedTransactionsCorrelationsAsyncSearchServiceProvider = ( esClient: ElasticsearchClient, getApmIndices: () => Promise, searchServiceParams: FailedTransactionsCorrelationsParams, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/index.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/index.ts index ade25ada80a62..96244aaa6e937 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/index.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/index.ts @@ -5,4 +5,8 @@ * 2.0. */ -export { failedTransactionsCorrelationsAsyncSearchServiceProvider } from './async_search_service'; +export { + failedTransactionsCorrelationsAsyncSearchServiceProvider, + FailedTransactionsCorrelationsAsyncSearchServiceProvider, + FailedTransactionsCorrelationsSearchStrategy, +} from './async_search_service'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts index b3bc62cfd6546..65c3329df5de4 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts @@ -7,6 +7,13 @@ import { range } from 'lodash'; import type { ElasticsearchClient } from 'src/core/server'; + +import type { ISearchStrategy } from '../../../../../../../src/plugins/data/server'; +import { + IKibanaSearchRequest, + IKibanaSearchResponse, +} from '../../../../../../../src/plugins/data/common'; + import type { SearchStrategyServerParams } from '../../../../common/search_strategies/types'; import type { LatencyCorrelationsParams, @@ -30,10 +37,17 @@ import type { AsyncSearchServiceProvider } from '../search_strategy_provider'; import { asyncSearchServiceStateProvider } from './async_search_service_state'; -export const latencyCorrelationsAsyncSearchServiceProvider: AsyncSearchServiceProvider< +export type LatencyCorrelationsAsyncSearchServiceProvider = AsyncSearchServiceProvider< LatencyCorrelationsParams, LatencyCorrelationsRawResponse -> = ( +>; + +export type LatencyCorrelationsSearchStrategy = ISearchStrategy< + IKibanaSearchRequest, + IKibanaSearchResponse +>; + +export const latencyCorrelationsAsyncSearchServiceProvider: LatencyCorrelationsAsyncSearchServiceProvider = ( esClient: ElasticsearchClient, getApmIndices: () => Promise, searchServiceParams: LatencyCorrelationsParams, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/index.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/index.ts index 0eea704edc3e0..9d341e82f0422 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/index.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/index.ts @@ -5,4 +5,8 @@ * 2.0. */ -export { latencyCorrelationsAsyncSearchServiceProvider } from './async_search_service'; +export { + latencyCorrelationsAsyncSearchServiceProvider, + LatencyCorrelationsAsyncSearchServiceProvider, + LatencyCorrelationsSearchStrategy, +} from './async_search_service'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts index 9ce58cd144dc8..dfe936b60bec0 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts @@ -20,6 +20,15 @@ import type { SearchStrategyClientParams } from '../../../common/search_strategi import type { RawResponseBase } from '../../../common/search_strategies/types'; import type { ApmIndicesConfig } from '../settings/apm_indices/get_apm_indices'; +import type { + LatencyCorrelationsAsyncSearchServiceProvider, + LatencyCorrelationsSearchStrategy, +} from './latency_correlations'; +import type { + FailedTransactionsCorrelationsAsyncSearchServiceProvider, + FailedTransactionsCorrelationsSearchStrategy, +} from './failed_transactions_correlations'; + interface AsyncSearchServiceState { cancel: () => void; error: Error; @@ -46,6 +55,20 @@ export type AsyncSearchServiceProvider< includeFrozen: boolean ) => GetAsyncSearchServiceState; +// Failed Transactions Correlations function overload +export function searchStrategyProvider( + asyncSearchServiceProvider: FailedTransactionsCorrelationsAsyncSearchServiceProvider, + getApmIndices: () => Promise, + includeFrozen: boolean +): FailedTransactionsCorrelationsSearchStrategy; + +// Latency Correlations function overload +export function searchStrategyProvider( + asyncSearchServiceProvider: LatencyCorrelationsAsyncSearchServiceProvider, + getApmIndices: () => Promise, + includeFrozen: boolean +): LatencyCorrelationsSearchStrategy; + export function searchStrategyProvider< TSearchStrategyClientParams extends SearchStrategyClientParams, TRawResponse extends RawResponseBase From 130f49806fc3dfc510df50608d30cdf1bebbfe73 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Thu, 2 Sep 2021 00:06:18 +0200 Subject: [PATCH 17/37] [Ml] Update API integration tests. --- .../apm_api_integration/tests/correlations/latency.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/x-pack/test/apm_api_integration/tests/correlations/latency.ts b/x-pack/test/apm_api_integration/tests/correlations/latency.ts index 262969fe60258..57072aab26013 100644 --- a/x-pack/test/apm_api_integration/tests/correlations/latency.ts +++ b/x-pack/test/apm_api_integration/tests/correlations/latency.ts @@ -128,7 +128,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { expect(typeof finalRawResponse?.took).to.be('number'); expect(finalRawResponse?.percentileThresholdValue).to.be(undefined); expect(finalRawResponse?.overallHistogram).to.be(undefined); - expect(finalRawResponse?.values.length).to.be(0); + expect(finalRawResponse?.latencyCorrelations.length).to.be(0); expect(finalRawResponse?.log.map((d: string) => d.split(': ')[1])).to.eql([ 'Fetched 95th percentile value of undefined based on 0 documents.', 'Abort service since percentileThresholdValue could not be determined.', @@ -182,7 +182,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { const { rawResponse } = result; expect(typeof rawResponse?.took).to.be('number'); - expect(rawResponse?.values).to.eql([]); + expect(rawResponse?.latencyCorrelations).to.eql([]); // follow up request body including search strategy ID const reqBody = getRequestBody(); @@ -236,9 +236,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { expect(finalRawResponse?.percentileThresholdValue).to.be(1309695.875); expect(finalRawResponse?.overallHistogram.length).to.be(101); - expect(finalRawResponse?.values.length).to.eql( + expect(finalRawResponse?.latencyCorrelations.length).to.eql( 13, - `Expected 13 identified correlations, got ${finalRawResponse?.values.length}.` + `Expected 13 identified correlations, got ${finalRawResponse?.latencyCorrelations.length}.` ); expect(finalRawResponse?.log.map((d: string) => d.split(': ')[1])).to.eql([ 'Fetched 95th percentile value of 1309695.875 based on 1244 documents.', @@ -251,7 +251,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { 'Identified 13 significant correlations out of 379 field/value pairs.', ]); - const correlation = finalRawResponse?.values[0]; + const correlation = finalRawResponse?.latencyCorrelations[0]; expect(typeof correlation).to.be('object'); expect(correlation?.field).to.be('transaction.result'); expect(correlation?.value).to.be('success'); From 1236eb6ab3f762e24bd3c9669755453a8726953a Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Thu, 2 Sep 2021 09:54:28 +0200 Subject: [PATCH 18/37] [ML] Rename values to failedTransactionsCorrelations. --- .../failed_transactions_correlations/types.ts | 4 +- .../failed_transactions_correlations.tsx | 37 ++++++++++--------- .../async_search_service.ts | 6 +-- .../async_search_service_state.ts | 26 +++++++------ .../tests/correlations/failed_transactions.ts | 10 ++--- 5 files changed, 44 insertions(+), 39 deletions(-) diff --git a/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts index 0815f7e1c0d1a..8a2ec40afe9f6 100644 --- a/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts @@ -9,7 +9,7 @@ import { RawResponseBase, SearchStrategyClientParams } from '../types'; import { FAILED_TRANSACTIONS_IMPACT_THRESHOLD } from './constants'; -export interface FailedTransactionsCorrelationValue { +export interface FailedTransactionsCorrelation { key: string; doc_count: number; bg_count: number; @@ -34,5 +34,5 @@ export type FailedTransactionsCorrelationsParams = SearchStrategyClientParams; export interface FailedTransactionsCorrelationsRawResponse extends RawResponseBase { log: string[]; - values: FailedTransactionsCorrelationValue[]; + failedTransactionsCorrelations: FailedTransactionsCorrelation[]; } diff --git a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx index 91eebc7fc5cda..01a47ffe6326e 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx @@ -31,7 +31,7 @@ import { } from '../../../../../observability/public'; import { asPercent } from '../../../../common/utils/formatters'; -import type { FailedTransactionsCorrelationValue } from '../../../../common/search_strategies/failed_transactions_correlations/types'; +import type { FailedTransactionsCorrelation } from '../../../../common/search_strategies/failed_transactions_correlations/types'; import { APM_SEARCH_STRATEGIES } from '../../../../common/search_strategies/constants'; import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; @@ -65,7 +65,7 @@ export function FailedTransactionsCorrelations({ const { state: { error, isRunning, loaded, total }, - data: { ccsWarning, log, values }, + data: { ccsWarning, log, failedTransactionsCorrelations }, startFetch, cancelFetch, } = useSearchStrategy( @@ -76,20 +76,23 @@ export function FailedTransactionsCorrelations({ const [ selectedSignificantTerm, setSelectedSignificantTerm, - ] = useState(null); + ] = useState(null); const selectedTerm = useMemo(() => { if (selectedSignificantTerm) return selectedSignificantTerm; - return Array.isArray(values) && values.length > 0 ? values[0] : undefined; - }, [selectedSignificantTerm, values]); + return Array.isArray(failedTransactionsCorrelations) && + failedTransactionsCorrelations.length > 0 + ? failedTransactionsCorrelations[0] + : undefined; + }, [selectedSignificantTerm, failedTransactionsCorrelations]); const history = useHistory(); const failedTransactionsCorrelationsColumns: Array< - EuiBasicTableColumn + EuiBasicTableColumn > = useMemo(() => { const percentageColumns: Array< - EuiBasicTableColumn + EuiBasicTableColumn > = inspectEnabled ? [ { @@ -237,7 +240,7 @@ export function FailedTransactionsCorrelations({ ), icon: 'plusInCircle', type: 'icon', - onClick: (term: FailedTransactionsCorrelationValue) => { + onClick: (term: FailedTransactionsCorrelation) => { push(history, { query: { kuery: `${term.fieldName}:"${term.fieldValue}"`, @@ -258,7 +261,7 @@ export function FailedTransactionsCorrelations({ ), icon: 'minusInCircle', type: 'icon', - onClick: (term: FailedTransactionsCorrelationValue) => { + onClick: (term: FailedTransactionsCorrelation) => { push(history, { query: { kuery: `not ${term.fieldName}:"${term.fieldValue}"`, @@ -273,7 +276,7 @@ export function FailedTransactionsCorrelations({ 'xpack.apm.correlations.correlationsTable.actionsLabel', { defaultMessage: 'Filter' } ), - render: (_: unknown, term: FailedTransactionsCorrelationValue) => { + render: (_: unknown, term: FailedTransactionsCorrelation) => { return ( <> >; + ] as Array>; }, [history, onFilter, trackApmEvent, inspectEnabled]); useEffect(() => { @@ -318,7 +321,7 @@ export function FailedTransactionsCorrelations({ }, [error, notifications.toasts]); const [sortField, setSortField] = useState< - keyof FailedTransactionsCorrelationValue + keyof FailedTransactionsCorrelation >('normalizedScore'); const [sortDirection, setSortDirection] = useState('desc'); @@ -330,11 +333,11 @@ export function FailedTransactionsCorrelations({ }, []); const { sorting, correlationTerms } = useMemo(() => { - if (!Array.isArray(values)) { + if (!Array.isArray(failedTransactionsCorrelations)) { return { correlationTerms: [], sorting: undefined }; } const orderedTerms = orderBy( - values, + failedTransactionsCorrelations, // The smaller the p value the higher the impact // So we want to sort by the normalized score here // which goes from 0 -> 1 @@ -348,9 +351,9 @@ export function FailedTransactionsCorrelations({ field: sortField, direction: sortDirection, }, - } as EuiTableSortingType, + } as EuiTableSortingType, }; - }, [values, sortField, sortDirection]); + }, [failedTransactionsCorrelations, sortField, sortDirection]); return (
@@ -451,7 +454,7 @@ export function FailedTransactionsCorrelations({
{(isRunning || correlationTerms.length > 0) && ( - + columns={failedTransactionsCorrelationsColumns} significantTerms={correlationTerms} status={isRunning ? FETCH_STATUS.LOADING : FETCH_STATUS.SUCCESS} diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts index 7c4b87c4bbff8..507f61ba61744 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts @@ -89,7 +89,7 @@ export const failedTransactionsCorrelationsAsyncSearchServiceProvider: FailedTra results.forEach((result, idx) => { if (result.status === 'fulfilled') { - state.addValues( + state.addFailedTransactionsCorrelations( result.value.filter( (record) => record && @@ -129,7 +129,7 @@ export const failedTransactionsCorrelationsAsyncSearchServiceProvider: FailedTra addLogMessage( `Identified ${ - state.getState().values.length + state.getState().failedTransactionsCorrelations.length } significant correlations relating to failed transactions.` ); @@ -157,7 +157,7 @@ export const failedTransactionsCorrelationsAsyncSearchServiceProvider: FailedTra ccsWarning, log: getLogMessages(), took: Date.now() - progress.started, - values: state.getValuesSortedByScore(), + failedTransactionsCorrelations: state.getFailedTransactionsCorrelationsSortedByScore(), }, }; }; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service_state.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service_state.ts index 0e99b90e9f3c2..2a1d20eeb1cc2 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service_state.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service_state.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { FailedTransactionsCorrelationValue } from '../../../../common/search_strategies/failed_transactions_correlations/types'; +import { FailedTransactionsCorrelation } from '../../../../common/search_strategies/failed_transactions_correlations/types'; interface Progress { started: number; @@ -51,16 +51,18 @@ export const asyncFailedTransactionsCorrelationsSearchServiceStateProvider = () }; } - const values: FailedTransactionsCorrelationValue[] = []; - function addValue(d: FailedTransactionsCorrelationValue) { - values.push(d); + const failedTransactionsCorrelations: FailedTransactionsCorrelation[] = []; + function addFailedTransactionsCorrelation(d: FailedTransactionsCorrelation) { + failedTransactionsCorrelations.push(d); } - function addValues(d: FailedTransactionsCorrelationValue[]) { - values.push(...d); + function addFailedTransactionsCorrelations( + d: FailedTransactionsCorrelation[] + ) { + failedTransactionsCorrelations.push(...d); } - function getValuesSortedByScore() { - return values.sort((a, b) => b.score - a.score); + function getFailedTransactionsCorrelationsSortedByScore() { + return failedTransactionsCorrelations.sort((a, b) => b.score - a.score); } function getState() { @@ -70,16 +72,16 @@ export const asyncFailedTransactionsCorrelationsSearchServiceStateProvider = () isCancelled, isRunning, progress, - values, + failedTransactionsCorrelations, }; } return { - addValue, - addValues, + addFailedTransactionsCorrelation, + addFailedTransactionsCorrelations, getOverallProgress, getState, - getValuesSortedByScore, + getFailedTransactionsCorrelationsSortedByScore, setCcsWarning, setError, setIsCancelled, diff --git a/x-pack/test/apm_api_integration/tests/correlations/failed_transactions.ts b/x-pack/test/apm_api_integration/tests/correlations/failed_transactions.ts index 9f7e2dc63ad80..e5e48a087e83c 100644 --- a/x-pack/test/apm_api_integration/tests/correlations/failed_transactions.ts +++ b/x-pack/test/apm_api_integration/tests/correlations/failed_transactions.ts @@ -122,9 +122,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { expect(typeof finalRawResponse?.took).to.be('number'); - expect(finalRawResponse?.values.length).to.eql( + expect(finalRawResponse?.failedTransactionsCorrelations.length).to.eql( 0, - `Expected 0 identified correlations, got ${finalRawResponse?.values.length}.` + `Expected 0 identified correlations, got ${finalRawResponse?.failedTransactionsCorrelations.length}.` ); }); }); @@ -214,9 +214,9 @@ export default function ApiTest({ getService }: FtrProviderContext) { expect(finalRawResponse?.percentileThresholdValue).to.be(undefined); expect(finalRawResponse?.overallHistogram).to.be(undefined); - expect(finalRawResponse?.values.length).to.eql( + expect(finalRawResponse?.failedTransactionsCorrelations.length).to.eql( 43, - `Expected 43 identified correlations, got ${finalRawResponse?.values.length}.` + `Expected 43 identified correlations, got ${finalRawResponse?.failedTransactionsCorrelations.length}.` ); expect(finalRawResponse?.log.map((d: string) => d.split(': ')[1])).to.eql([ @@ -225,7 +225,7 @@ export default function ApiTest({ getService }: FtrProviderContext) { 'Identified 43 significant correlations relating to failed transactions.', ]); - const sortedCorrelations = finalRawResponse?.values.sort(); + const sortedCorrelations = finalRawResponse?.failedTransactionsCorrelations.sort(); const correlation = sortedCorrelations[0]; expect(typeof correlation).to.be('object'); From bfa89a1c99dd317329b1473309e9c9504e684f86 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Thu, 2 Sep 2021 12:49:24 +0200 Subject: [PATCH 19/37] [ML] Fix naming inconsistencies. --- .../apm/common/search_strategies/constants.ts | 2 + .../failed_transactions_correlations/types.ts | 7 ++ .../latency_correlations/types.ts | 14 ++-- .../failed_transactions_correlations.tsx | 10 +-- .../app/correlations/latency_correlations.tsx | 67 +++++++------------ .../distribution/index.tsx | 6 +- .../async_search_service.ts | 2 +- .../async_search_service_state.ts | 4 +- .../get_prioritized_field_value_pairs.test.ts | 26 +++---- .../get_prioritized_field_value_pairs.ts | 8 +-- .../queries/query_field_value_pairs.ts | 8 +-- .../query_histograms_generator.test.ts | 6 +- .../queries/query_histograms_generator.ts | 10 +-- 13 files changed, 86 insertions(+), 84 deletions(-) diff --git a/x-pack/plugins/apm/common/search_strategies/constants.ts b/x-pack/plugins/apm/common/search_strategies/constants.ts index 45b24a28f7e04..b1bd321e1c914 100644 --- a/x-pack/plugins/apm/common/search_strategies/constants.ts +++ b/x-pack/plugins/apm/common/search_strategies/constants.ts @@ -10,3 +10,5 @@ export const APM_SEARCH_STRATEGIES = { APM_LATENCY_CORRELATIONS: 'apmLatencyCorrelations', } as const; export type ApmSearchStrategies = typeof APM_SEARCH_STRATEGIES[keyof typeof APM_SEARCH_STRATEGIES]; + +export const DEFAULT_PERCENTILE_THRESHOLD = 95; diff --git a/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts index 8a2ec40afe9f6..faa537168bcef 100644 --- a/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts @@ -22,6 +22,13 @@ export interface FailedTransactionsCorrelation { successPercentage: number; } +// Basic type guard for array of FailedTransactionsCorrelation +export const isFailedTransactionsCorrelations = ( + arg: unknown +): arg is FailedTransactionsCorrelation[] => { + return Array.isArray(arg) && arg.length > 0; +}; + export type FailedTransactionsCorrelationsImpactThreshold = typeof FAILED_TRANSACTIONS_IMPACT_THRESHOLD[keyof typeof FAILED_TRANSACTIONS_IMPACT_THRESHOLD]; export interface CorrelationsTerm { diff --git a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts index acab8314a19fc..6309e0045ea3d 100644 --- a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts @@ -12,12 +12,18 @@ import { } from '../types'; export interface LatencyCorrelation { - histogram: HistogramItem[]; - value: string; - field: string; correlation: number; + fieldName: string; + fieldValue: string; + histogram: HistogramItem[]; ksTest: number; - duplicatedFields?: string[]; +} + +// Basic type guard for array of LatencyCorrelation +export function isLatencyCorrelations( + arg: unknown +): arg is LatencyCorrelation[] { + return Array.isArray(arg) && arg.length > 0; } export interface AsyncSearchProviderProgress { diff --git a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx index 01a47ffe6326e..dd6c07f10a345 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx @@ -31,7 +31,10 @@ import { } from '../../../../../observability/public'; import { asPercent } from '../../../../common/utils/formatters'; -import type { FailedTransactionsCorrelation } from '../../../../common/search_strategies/failed_transactions_correlations/types'; +import { + isFailedTransactionsCorrelations, + FailedTransactionsCorrelation, +} from '../../../../common/search_strategies/failed_transactions_correlations/types'; import { APM_SEARCH_STRATEGIES } from '../../../../common/search_strategies/constants'; import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; @@ -80,8 +83,7 @@ export function FailedTransactionsCorrelations({ const selectedTerm = useMemo(() => { if (selectedSignificantTerm) return selectedSignificantTerm; - return Array.isArray(failedTransactionsCorrelations) && - failedTransactionsCorrelations.length > 0 + return isFailedTransactionsCorrelations(failedTransactionsCorrelations) ? failedTransactionsCorrelations[0] : undefined; }, [selectedSignificantTerm, failedTransactionsCorrelations]); @@ -333,7 +335,7 @@ export function FailedTransactionsCorrelations({ }, []); const { sorting, correlationTerms } = useMemo(() => { - if (!Array.isArray(failedTransactionsCorrelations)) { + if (!isFailedTransactionsCorrelations(failedTransactionsCorrelations)) { return { correlationTerms: [], sorting: undefined }; } const orderedTerms = orderBy( diff --git a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx index 0fda04d2f99e8..6ae0cbbb1e019 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx @@ -29,7 +29,14 @@ import { } from '../../../../../observability/public'; import { asPreciseDecimal } from '../../../../common/utils/formatters'; -import { APM_SEARCH_STRATEGIES } from '../../../../common/search_strategies/constants'; +import { + APM_SEARCH_STRATEGIES, + DEFAULT_PERCENTILE_THRESHOLD, +} from '../../../../common/search_strategies/constants'; +import { + isLatencyCorrelations, + LatencyCorrelation, +} from '../../../../common/search_strategies/latency_correlations/types'; import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; import { FETCH_STATUS } from '../../../hooks/use_fetcher'; @@ -46,16 +53,6 @@ import { CorrelationsEmptyStatePrompt } from './empty_state_prompt'; import { CrossClusterSearchCompatibilityWarning } from './cross_cluster_search_warning'; import { CorrelationsProgressControls } from './progress_controls'; -const DEFAULT_PERCENTILE_THRESHOLD = 95; - -interface MlCorrelationsTerms { - correlation: number; - ksTest: number; - fieldName: string; - fieldValue: string; - duplicatedFields?: string[]; -} - export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { const { core: { notifications, uiSettings }, @@ -100,23 +97,21 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { const [ selectedSignificantTerm, setSelectedSignificantTerm, - ] = useState(null); + ] = useState(null); const selectedHistogram = useMemo(() => { - let selected = - Array.isArray(latencyCorrelations) && latencyCorrelations.length > 0 - ? latencyCorrelations[0] - : undefined; + let selected = isLatencyCorrelations(latencyCorrelations) + ? latencyCorrelations[0] + : undefined; if ( - Array.isArray(latencyCorrelations) && - latencyCorrelations.length > 0 && + isLatencyCorrelations(latencyCorrelations) && selectedSignificantTerm !== null ) { selected = latencyCorrelations.find( (h) => - h.field === selectedSignificantTerm.fieldName && - h.value === selectedSignificantTerm.fieldValue + h.fieldName === selectedSignificantTerm.fieldName && + h.fieldValue === selectedSignificantTerm.fieldValue ); } return selected; @@ -126,7 +121,7 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { const trackApmEvent = useUiTracker({ app: 'apm' }); const mlCorrelationColumns: Array< - EuiBasicTableColumn + EuiBasicTableColumn > = useMemo( () => [ { @@ -194,7 +189,7 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { ), icon: 'plusInCircle', type: 'icon', - onClick: (term: MlCorrelationsTerms) => { + onClick: (term: LatencyCorrelation) => { push(history, { query: { kuery: `${term.fieldName}:"${term.fieldValue}"`, @@ -215,7 +210,7 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { ), icon: 'minusInCircle', type: 'icon', - onClick: (term: MlCorrelationsTerms) => { + onClick: (term: LatencyCorrelation) => { push(history, { query: { kuery: `not ${term.fieldName}:"${term.fieldValue}"`, @@ -235,7 +230,7 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { [history, onFilter, trackApmEvent] ); - const [sortField, setSortField] = useState( + const [sortField, setSortField] = useState( 'correlation' ); const [sortDirection, setSortDirection] = useState('desc'); @@ -248,22 +243,10 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { }, []); const { histogramTerms, sorting } = useMemo(() => { - if (!Array.isArray(latencyCorrelations)) { + if (!isLatencyCorrelations(latencyCorrelations)) { return { histogramTerms: [], sorting: undefined }; } - const orderedTerms = orderBy( - latencyCorrelations.map((d) => { - return { - fieldName: d.field, - fieldValue: d.value, - ksTest: d.ksTest, - correlation: d.correlation, - duplicatedFields: d.duplicatedFields, - }; - }), - sortField, - sortDirection - ); + const orderedTerms = orderBy(latencyCorrelations, sortField, sortDirection); return { histogramTerms: orderedTerms, @@ -272,7 +255,7 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { field: sortField, direction: sortDirection, }, - } as EuiTableSortingType, + } as EuiTableSortingType, }; }, [latencyCorrelations, sortField, sortDirection]); @@ -338,7 +321,7 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) {
{(isRunning || histogramTerms.length > 0) && ( - + columns={mlCorrelationColumns} significantTerms={histogramTerms} status={isRunning ? FETCH_STATUS.LOADING : FETCH_STATUS.SUCCESS} @@ -346,8 +329,8 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { selectedTerm={ selectedHistogram !== undefined ? { - fieldName: selectedHistogram.field, - fieldValue: selectedHistogram.value, + fieldName: selectedHistogram.fieldName, + fieldValue: selectedHistogram.fieldValue, } : undefined } diff --git a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx index ec926add9ebc5..03f904fedd886 100644 --- a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx +++ b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx @@ -18,7 +18,10 @@ import { } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import { getDurationFormatter } from '../../../../../common/utils/formatters'; -import { APM_SEARCH_STRATEGIES } from '../../../../../common/search_strategies/constants'; +import { + APM_SEARCH_STRATEGIES, + DEFAULT_PERCENTILE_THRESHOLD, +} from '../../../../../common/search_strategies/constants'; import { useApmPluginContext } from '../../../../context/apm_plugin/use_apm_plugin_context'; import { useSearchStrategy } from '../../../../hooks/use_search_strategy'; import { @@ -28,7 +31,6 @@ import { import { useUiTracker } from '../../../../../../observability/public'; import { isErrorMessage } from '../../correlations/utils/is_error_message'; -const DEFAULT_PERCENTILE_THRESHOLD = 95; // Enforce min height so it's consistent across all tabs on the same level // to prevent "flickering" behavior const MIN_TAB_TITLE_HEIGHT = 56; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts index 65c3329df5de4..3e08d2b051a7a 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts @@ -214,7 +214,7 @@ export const latencyCorrelationsAsyncSearchServiceProvider: LatencyCorrelationsA fieldValuePairs )) { if (item !== undefined) { - state.addValue(item); + state.addLatencyCorrelation(item); } loadedHistograms++; state.setProgress({ diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts index bbb9970ca02ed..3cdd03869710a 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts @@ -72,7 +72,7 @@ export const asyncSearchServiceStateProvider = () => { } const latencyCorrelations: LatencyCorrelation[] = []; - function addValue(d: LatencyCorrelation) { + function addLatencyCorrelation(d: LatencyCorrelation) { latencyCorrelations.push(d); } @@ -94,7 +94,7 @@ export const asyncSearchServiceStateProvider = () => { } return { - addValue, + addLatencyCorrelation, getIsCancelled, getOverallProgress, getState, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_prioritized_field_value_pairs.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_prioritized_field_value_pairs.test.ts index dc11b4860a8b6..cb1500e70babc 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_prioritized_field_value_pairs.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_prioritized_field_value_pairs.test.ts @@ -11,13 +11,13 @@ describe('correlations', () => { describe('getPrioritizedFieldValuePairs', () => { it('returns fields without prioritization in the same order', () => { const fieldValuePairs = [ - { field: 'the-field-1', value: 'the-value-1' }, - { field: 'the-field-2', value: 'the-value-2' }, + { fieldName: 'the-field-1', fieldValue: 'the-value-1' }, + { fieldName: 'the-field-2', fieldValue: 'the-value-2' }, ]; const prioritziedFieldValuePairs = getPrioritizedFieldValuePairs( fieldValuePairs ); - expect(prioritziedFieldValuePairs.map((d) => d.field)).toEqual([ + expect(prioritziedFieldValuePairs.map((d) => d.fieldName)).toEqual([ 'the-field-1', 'the-field-2', ]); @@ -25,13 +25,13 @@ describe('correlations', () => { it('returns fields with already sorted prioritization in the same order', () => { const fieldValuePairs = [ - { field: 'service.version', value: 'the-value-1' }, - { field: 'the-field-2', value: 'the-value-2' }, + { fieldName: 'service.version', fieldValue: 'the-value-1' }, + { fieldName: 'the-field-2', fieldValue: 'the-value-2' }, ]; const prioritziedFieldValuePairs = getPrioritizedFieldValuePairs( fieldValuePairs ); - expect(prioritziedFieldValuePairs.map((d) => d.field)).toEqual([ + expect(prioritziedFieldValuePairs.map((d) => d.fieldName)).toEqual([ 'service.version', 'the-field-2', ]); @@ -39,13 +39,13 @@ describe('correlations', () => { it('returns fields with unsorted prioritization in the corrected order', () => { const fieldValuePairs = [ - { field: 'the-field-1', value: 'the-value-1' }, - { field: 'service.version', value: 'the-value-2' }, + { fieldName: 'the-field-1', fieldValue: 'the-value-1' }, + { fieldName: 'service.version', fieldValue: 'the-value-2' }, ]; const prioritziedFieldValuePairs = getPrioritizedFieldValuePairs( fieldValuePairs ); - expect(prioritziedFieldValuePairs.map((d) => d.field)).toEqual([ + expect(prioritziedFieldValuePairs.map((d) => d.fieldName)).toEqual([ 'service.version', 'the-field-1', ]); @@ -53,14 +53,14 @@ describe('correlations', () => { it('considers prefixes when sorting', () => { const fieldValuePairs = [ - { field: 'the-field-1', value: 'the-value-1' }, - { field: 'service.version', value: 'the-value-2' }, - { field: 'cloud.the-field-3', value: 'the-value-3' }, + { fieldName: 'the-field-1', fieldValue: 'the-value-1' }, + { fieldName: 'service.version', fieldValue: 'the-value-2' }, + { fieldName: 'cloud.the-field-3', fieldValue: 'the-value-3' }, ]; const prioritziedFieldValuePairs = getPrioritizedFieldValuePairs( fieldValuePairs ); - expect(prioritziedFieldValuePairs.map((d) => d.field)).toEqual([ + expect(prioritziedFieldValuePairs.map((d) => d.fieldName)).toEqual([ 'service.version', 'cloud.the-field-3', 'the-field-1', diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_prioritized_field_value_pairs.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_prioritized_field_value_pairs.ts index ddfd87c83f9f3..3e67f8936903f 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_prioritized_field_value_pairs.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_prioritized_field_value_pairs.ts @@ -16,11 +16,11 @@ export const getPrioritizedFieldValuePairs = ( const prioritizedFields = [...FIELDS_TO_ADD_AS_CANDIDATE]; return fieldValuePairs.sort((a, b) => { - const hasPrefixA = hasPrefixToInclude(a.field); - const hasPrefixB = hasPrefixToInclude(b.field); + const hasPrefixA = hasPrefixToInclude(a.fieldName); + const hasPrefixB = hasPrefixToInclude(b.fieldName); - const includesA = prioritizedFields.includes(a.field); - const includesB = prioritizedFields.includes(b.field); + const includesA = prioritizedFields.includes(a.fieldName); + const includesB = prioritizedFields.includes(b.fieldName); if ((includesA || hasPrefixA) && !includesB && !hasPrefixB) { return -1; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts index 71322d03ffb44..83cef659d4f72 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts @@ -21,8 +21,8 @@ import { getRequestBase } from './get_request_base'; export type FieldName = string; interface FieldValuePair { - field: FieldName; - value: string; + fieldName: FieldName; + fieldValue: string; } export type FieldValuePairs = FieldValuePair[]; @@ -67,8 +67,8 @@ const fetchTransactionDurationFieldTerms = async ( }>)?.buckets; if (buckets?.length >= 1) { return buckets.map((d) => ({ - field: fieldName, - value: d.key, + fieldName, + fieldValue: d.key, })); } } catch (e) { diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.test.ts index 1de8e83e46beb..4144dc17d043a 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.test.ts @@ -30,9 +30,9 @@ const totalDocCount = 1234; const histogramRangeSteps = [1, 2, 4, 5]; const fieldValuePairs = [ - { field: 'the-field-name-1', value: 'the-field-value-1' }, - { field: 'the-field-name-2', value: 'the-field-value-2' }, - { field: 'the-field-name-2', value: 'the-field-value-3' }, + { fieldName: 'the-field-name-1', fieldValue: 'the-field-value-1' }, + { fieldName: 'the-field-name-2', fieldValue: 'the-field-value-2' }, + { fieldName: 'the-field-name-2', fieldValue: 'the-field-value-3' }, ]; describe('query_histograms_generator', () => { diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts index 682fbf6b32a75..c3c876a4b0600 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts @@ -49,8 +49,8 @@ export async function* fetchTransactionDurationHistograms( ranges, fractions, totalDocCount, - item.field, - item.value + item.fieldName, + item.fieldValue ); if (state.getIsCancelled()) { @@ -68,8 +68,8 @@ export async function* fetchTransactionDurationHistograms( esClient, params, histogramRangeSteps, - item.field, - item.value + item.fieldName, + item.fieldValue ); yield { ...item, @@ -85,7 +85,7 @@ export async function* fetchTransactionDurationHistograms( // just add the error to the internal log and check if we'd want to set the // cross-cluster search compatibility warning to true. addLogMessage( - `Failed to fetch correlation/kstest for '${item.field}/${item.value}'`, + `Failed to fetch correlation/kstest for '${item.fieldName}/${item.fieldValue}'`, JSON.stringify(e) ); if (params?.index.includes(':')) { From 8724c98e8c0dc5582ad5fbc55eae49a200443b67 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Thu, 2 Sep 2021 12:57:52 +0200 Subject: [PATCH 20/37] [ML] Fix naming inconsistencies. --- .../failed_transactions_correlations/types.ts | 5 ---- .../apm/common/search_strategies/types.ts | 5 ++++ .../app/correlations/correlations_table.tsx | 6 ++--- .../get_prioritized_field_value_pairs.ts | 4 +-- .../queries/query_field_candidates.ts | 3 +-- .../queries/query_field_value_pairs.ts | 25 ++++++++----------- .../queries/query_histograms_generator.ts | 9 ++++--- 7 files changed, 26 insertions(+), 31 deletions(-) diff --git a/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts index faa537168bcef..08d27b86f5805 100644 --- a/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts @@ -31,11 +31,6 @@ export const isFailedTransactionsCorrelations = ( export type FailedTransactionsCorrelationsImpactThreshold = typeof FAILED_TRANSACTIONS_IMPACT_THRESHOLD[keyof typeof FAILED_TRANSACTIONS_IMPACT_THRESHOLD]; -export interface CorrelationsTerm { - fieldName: string; - fieldValue: string; -} - export type FailedTransactionsCorrelationsParams = SearchStrategyClientParams; export interface FailedTransactionsCorrelationsRawResponse diff --git a/x-pack/plugins/apm/common/search_strategies/types.ts b/x-pack/plugins/apm/common/search_strategies/types.ts index 564f33697072a..6963d068520dc 100644 --- a/x-pack/plugins/apm/common/search_strategies/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/types.ts @@ -5,6 +5,11 @@ * 2.0. */ +export interface FieldValuePair { + fieldName: string; + fieldValue: string; +} + export interface HistogramItem { key: number; doc_count: number; diff --git a/x-pack/plugins/apm/public/components/app/correlations/correlations_table.tsx b/x-pack/plugins/apm/public/components/app/correlations/correlations_table.tsx index 6fb239b9e3c3a..20d0bfcd58dec 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/correlations_table.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/correlations_table.tsx @@ -14,11 +14,11 @@ import type { Criteria } from '@elastic/eui/src/components/basic_table/basic_tab import { FETCH_STATUS } from '../../../hooks/use_fetcher'; import { useUiTracker } from '../../../../../observability/public'; import { useTheme } from '../../../hooks/use_theme'; -import type { CorrelationsTerm } from '../../../../common/search_strategies/failed_transactions_correlations/types'; +import type { FieldValuePair } from '../../../../common/search_strategies/types'; const PAGINATION_SIZE_OPTIONS = [5, 10, 20, 50]; -export type SelectedCorrelationTerm = Pick< +export type SelectedCorrelationTerm = Pick< T, 'fieldName' | 'fieldValue' >; @@ -35,7 +35,7 @@ interface Props { sorting?: EuiTableSortingType; } -export function CorrelationsTable({ +export function CorrelationsTable({ significantTerms, status, setSelectedSignificantTerm, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_prioritized_field_value_pairs.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_prioritized_field_value_pairs.ts index 3e67f8936903f..6338422b022da 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_prioritized_field_value_pairs.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_prioritized_field_value_pairs.ts @@ -8,10 +8,10 @@ import { FIELDS_TO_ADD_AS_CANDIDATE } from '../constants'; import { hasPrefixToInclude } from '../utils'; -import type { FieldValuePairs } from './query_field_value_pairs'; +import type { FieldValuePair } from '../../../../common/search_strategies/types'; export const getPrioritizedFieldValuePairs = ( - fieldValuePairs: FieldValuePairs + fieldValuePairs: FieldValuePair[] ) => { const prioritizedFields = [...FIELDS_TO_ADD_AS_CANDIDATE]; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts index abcd5810fb5cb..390243295c4f0 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_candidates.ts @@ -21,7 +21,6 @@ import { hasPrefixToInclude } from '../utils'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; -import type { FieldName } from './query_field_value_pairs'; export const shouldBeExcluded = (fieldName: string) => { return ( @@ -53,7 +52,7 @@ export const getRandomDocsRequest = ( export const fetchTransactionDurationFieldCandidates = async ( esClient: ElasticsearchClient, params: SearchStrategyParams -): Promise<{ fieldCandidates: FieldName[] }> => { +): Promise<{ fieldCandidates: string[] }> => { const { index } = params; // Get all fields with keyword mapping const respMapping = await esClient.fieldCaps({ diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts index 83cef659d4f72..3df7b1f46c567 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts @@ -9,7 +9,10 @@ import type { ElasticsearchClient } from 'src/core/server'; import type { estypes } from '@elastic/elasticsearch'; -import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; +import type { + FieldValuePair, + SearchStrategyParams, +} from '../../../../common/search_strategies/types'; import type { AsyncSearchServiceLog } from '../async_search_service_log'; import type { AsyncSearchServiceState } from '../latency_correlations/async_search_service_state'; @@ -18,17 +21,9 @@ import { TERMS_SIZE } from '../constants'; import { getQueryWithParams } from './get_query_with_params'; import { getRequestBase } from './get_request_base'; -export type FieldName = string; - -interface FieldValuePair { - fieldName: FieldName; - fieldValue: string; -} -export type FieldValuePairs = FieldValuePair[]; - export const getTermsAggRequest = ( params: SearchStrategyParams, - fieldName: FieldName + fieldName: string ): estypes.SearchRequest => ({ ...getRequestBase(params), body: { @@ -50,7 +45,7 @@ const fetchTransactionDurationFieldTerms = async ( params: SearchStrategyParams, fieldName: string, addLogMessage: AsyncSearchServiceLog['addLogMessage'] -): Promise => { +): Promise => { try { const resp = await esClient.search(getTermsAggRequest(params, fieldName)); @@ -82,8 +77,8 @@ const fetchTransactionDurationFieldTerms = async ( }; async function fetchInSequence( - fieldCandidates: FieldName[], - fn: (fieldCandidate: string) => Promise + fieldCandidates: string[], + fn: (fieldCandidate: string) => Promise ) { const results = []; @@ -97,10 +92,10 @@ async function fetchInSequence( export const fetchTransactionDurationFieldValuePairs = async ( esClient: ElasticsearchClient, params: SearchStrategyParams, - fieldCandidates: FieldName[], + fieldCandidates: string[], state: AsyncSearchServiceState, addLogMessage: AsyncSearchServiceLog['addLogMessage'] -): Promise => { +): Promise => { let fieldValuePairsProgress = 1; return await fetchInSequence( diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts index c3c876a4b0600..4dc59083186f2 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts @@ -9,7 +9,10 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; -import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; +import type { + FieldValuePair, + SearchStrategyParams, +} from '../../../../common/search_strategies/types'; import type { AsyncSearchServiceLog } from '../async_search_service_log'; import type { AsyncSearchServiceState } from '../latency_correlations/async_search_service_state'; @@ -19,8 +22,6 @@ import { getPrioritizedFieldValuePairs } from './get_prioritized_field_value_pai import { fetchTransactionDurationCorrelation } from './query_correlation'; import { fetchTransactionDurationRanges } from './query_ranges'; -import type { FieldValuePairs } from './query_field_value_pairs'; - export async function* fetchTransactionDurationHistograms( esClient: ElasticsearchClient, addLogMessage: AsyncSearchServiceLog['addLogMessage'], @@ -31,7 +32,7 @@ export async function* fetchTransactionDurationHistograms( fractions: number[], histogramRangeSteps: number[], totalDocCount: number, - fieldValuePairs: FieldValuePairs + fieldValuePairs: FieldValuePair[] ) { for (const item of getPrioritizedFieldValuePairs(fieldValuePairs)) { if (params === undefined || item === undefined || state.getIsCancelled()) { From aa4f9097d8bff22483244ae9620ae4b63c272090 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Thu, 2 Sep 2021 13:12:35 +0200 Subject: [PATCH 21/37] [ML] Fix naming inconsistencies. --- .../apm/public/hooks/use_search_strategy.ts | 31 +++++++++---------- .../search_strategy_provider.test.ts | 1 - 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/x-pack/plugins/apm/public/hooks/use_search_strategy.ts b/x-pack/plugins/apm/public/hooks/use_search_strategy.ts index d1532570ddd56..c4c73864cf42f 100644 --- a/x-pack/plugins/apm/public/hooks/use_search_strategy.ts +++ b/x-pack/plugins/apm/public/hooks/use_search_strategy.ts @@ -64,6 +64,7 @@ interface SearchStrategyReturnBase { cancelFetch: () => void; } +// Function overload for Latency Correlations export function useSearchStrategy( searchStrategyName: typeof APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, options: { @@ -74,6 +75,7 @@ export function useSearchStrategy( data: LatencyCorrelationsRawResponse; } & SearchStrategyReturnBase; +// Function overload for Failed Transactions Correlations export function useSearchStrategy( searchStrategyName: typeof APM_SEARCH_STRATEGIES.APM_FAILED_TRANSACTIONS_CORRELATIONS ): { @@ -108,21 +110,18 @@ export function useSearchStrategy< const abortCtrl = useRef(new AbortController()); const searchSubscription$ = useRef(); - // options get passed in as a Generic and we don't want to support - // an updated on options changes, they should be considered non-changing options - // to initialize the search service. const optionsRef = useRef(options); const startFetch = useCallback(() => { + searchSubscription$.current?.unsubscribe(); + abortCtrl.current.abort(); + abortCtrl.current = new AbortController(); setFetchState({ ...getInitialFetchState(), error: undefined, }); - searchSubscription$.current?.unsubscribe(); - abortCtrl.current.abort(); - abortCtrl.current = new AbortController(); - const req = { + const request = { params: { environment, serviceName, @@ -140,28 +139,28 @@ export function useSearchStrategy< .search< IKibanaSearchRequest, IKibanaSearchResponse - >(req, { + >(request, { strategy: searchStrategyName, abortSignal: abortCtrl.current.signal, }) .subscribe({ - next: (res: IKibanaSearchResponse) => { - setRawResponse(res.rawResponse); + next: (response: IKibanaSearchResponse) => { + setRawResponse(response.rawResponse); setFetchState({ - isRunning: res.isRunning || false, - loaded: res.loaded, - total: res.total, + isRunning: response.isRunning || false, + loaded: response.loaded, + total: response.total, }); - if (isCompleteResponse(res)) { + if (isCompleteResponse(response)) { searchSubscription$.current?.unsubscribe(); setFetchState({ isRunning: false, }); - } else if (isErrorResponse(res)) { + } else if (isErrorResponse(response)) { searchSubscription$.current?.unsubscribe(); setFetchState({ - error: (res as unknown) as Error, + error: (response as unknown) as Error, isRunning: false, }); } diff --git a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts index 1732208dc7296..2bbca7abd7c3a 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts @@ -17,7 +17,6 @@ import type { LatencyCorrelationsParams } from '../../../common/search_strategie import type { ApmIndicesConfig } from '../settings/apm_indices/get_apm_indices'; import { latencyCorrelationsAsyncSearchServiceProvider } from './latency_correlations'; - import { searchStrategyProvider } from './search_strategy_provider'; // helper to trigger promises in the async search service From ac873d309bde7e13c1783e72a81399ad8c98e972 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Thu, 2 Sep 2021 14:30:57 +0200 Subject: [PATCH 22/37] [ML] Fix jest test. --- .../queries/query_field_value_pairs.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.test.ts index 55cc23de764fe..17988b334e680 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.test.ts @@ -77,12 +77,12 @@ describe('query_field_value_pairs', () => { expect(progress.loadedFieldValuePairs).toBe(1); expect(resp).toEqual([ - { field: 'myFieldCandidate1', value: 'myValue1' }, - { field: 'myFieldCandidate1', value: 'myValue2' }, - { field: 'myFieldCandidate2', value: 'myValue1' }, - { field: 'myFieldCandidate2', value: 'myValue2' }, - { field: 'myFieldCandidate3', value: 'myValue1' }, - { field: 'myFieldCandidate3', value: 'myValue2' }, + { fieldName: 'myFieldCandidate1', fieldValue: 'myValue1' }, + { fieldName: 'myFieldCandidate1', fieldValue: 'myValue2' }, + { fieldName: 'myFieldCandidate2', fieldValue: 'myValue1' }, + { fieldName: 'myFieldCandidate2', fieldValue: 'myValue2' }, + { fieldName: 'myFieldCandidate3', fieldValue: 'myValue1' }, + { fieldName: 'myFieldCandidate3', fieldValue: 'myValue2' }, ]); expect(esClientSearchMock).toHaveBeenCalledTimes(3); expect(getLogMessages()).toEqual([]); From 55314be09961bcc09b3a4bc6125a64dd8b5d2544 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Thu, 2 Sep 2021 17:01:51 +0200 Subject: [PATCH 23/37] [ML] Fix API integration test --- x-pack/test/apm_api_integration/tests/correlations/latency.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/test/apm_api_integration/tests/correlations/latency.ts b/x-pack/test/apm_api_integration/tests/correlations/latency.ts index 57072aab26013..d7023aac457a3 100644 --- a/x-pack/test/apm_api_integration/tests/correlations/latency.ts +++ b/x-pack/test/apm_api_integration/tests/correlations/latency.ts @@ -253,8 +253,8 @@ export default function ApiTest({ getService }: FtrProviderContext) { const correlation = finalRawResponse?.latencyCorrelations[0]; expect(typeof correlation).to.be('object'); - expect(correlation?.field).to.be('transaction.result'); - expect(correlation?.value).to.be('success'); + expect(correlation?.fieldName).to.be('transaction.result'); + expect(correlation?.fieldValue).to.be('success'); expect(correlation?.correlation).to.be(0.6275246559191225); expect(correlation?.ksTest).to.be(4.806503252860024e-13); expect(correlation?.histogram.length).to.be(101); From 8f41990d98820144e3fae5d0e88db204f9d84192 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Fri, 3 Sep 2021 09:32:22 +0200 Subject: [PATCH 24/37] [ML] Clean up chart data. --- .../app/correlations/latency_correlations.tsx | 11 +-- .../utils/get_overall_histogram.test.ts | 56 +++++++++++++++ .../utils/get_overall_histogram.ts | 30 ++++++++ .../distribution/index.test.tsx | 8 +-- .../distribution/index.tsx | 70 ++++++++++++------- .../transaction_details/trace_samples_tab.tsx | 61 +++------------- .../transaction_distribution_chart/index.tsx | 41 ++--------- 7 files changed, 156 insertions(+), 121 deletions(-) create mode 100644 x-pack/plugins/apm/public/components/app/correlations/utils/get_overall_histogram.test.ts create mode 100644 x-pack/plugins/apm/public/components/app/correlations/utils/get_overall_histogram.ts diff --git a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx index 6ae0cbbb1e019..bcdee7a35dbcd 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx @@ -48,6 +48,7 @@ import { push } from '../../shared/Links/url_helpers'; import { CorrelationsTable } from './correlations_table'; import { LatencyCorrelationsHelpPopover } from './latency_correlations_help_popover'; import { isErrorMessage } from './utils/is_error_message'; +import { getOverallHistogram } from './utils/get_overall_histogram'; import { CorrelationsLog } from './correlations_log'; import { CorrelationsEmptyStatePrompt } from './empty_state_prompt'; import { CrossClusterSearchCompatibilityWarning } from './cross_cluster_search_warning'; @@ -74,11 +75,11 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { latencyCorrelations, percentileThresholdValue, } = data; - const overallHistogram = - data.overallHistogram === undefined && !isRunning - ? [] - : data.overallHistogram; const progress = loaded / total; + const { overallHistogram, hasData, status } = getOverallHistogram( + data, + isRunning + ); useEffect(() => { if (isErrorMessage(error)) { @@ -286,6 +287,8 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { markerValue={percentileThresholdValue ?? 0} {...selectedHistogram} overallHistogram={overallHistogram} + hasData={hasData} + status={status} /> diff --git a/x-pack/plugins/apm/public/components/app/correlations/utils/get_overall_histogram.test.ts b/x-pack/plugins/apm/public/components/app/correlations/utils/get_overall_histogram.test.ts new file mode 100644 index 0000000000000..c323b69594013 --- /dev/null +++ b/x-pack/plugins/apm/public/components/app/correlations/utils/get_overall_histogram.test.ts @@ -0,0 +1,56 @@ +/* + * 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 type { LatencyCorrelationsRawResponse } from '../../../../../common/search_strategies/latency_correlations/types'; + +import { getOverallHistogram } from './get_overall_histogram'; + +describe('getOverallHistogram', () => { + it('returns "loading" when undefined and running', () => { + const { overallHistogram, hasData, status } = getOverallHistogram( + {} as LatencyCorrelationsRawResponse, + true + ); + expect(overallHistogram).toStrictEqual(undefined); + expect(hasData).toBe(false); + expect(status).toBe('loading'); + }); + + it('returns "success" when undefined and not running', () => { + const { overallHistogram, hasData, status } = getOverallHistogram( + {} as LatencyCorrelationsRawResponse, + false + ); + expect(overallHistogram).toStrictEqual([]); + expect(hasData).toBe(false); + expect(status).toBe('success'); + }); + + it('returns "success" when not undefined and still running', () => { + const { overallHistogram, hasData, status } = getOverallHistogram( + { + overallHistogram: [{ key: 1, doc_count: 1234 }], + } as LatencyCorrelationsRawResponse, + true + ); + expect(overallHistogram).toStrictEqual([{ key: 1, doc_count: 1234 }]); + expect(hasData).toBe(true); + expect(status).toBe('success'); + }); + + it('returns "success" when not undefined and not running', () => { + const { overallHistogram, hasData, status } = getOverallHistogram( + { + overallHistogram: [{ key: 1, doc_count: 1234 }], + } as LatencyCorrelationsRawResponse, + false + ); + expect(overallHistogram).toStrictEqual([{ key: 1, doc_count: 1234 }]); + expect(hasData).toBe(true); + expect(status).toBe('success'); + }); +}); diff --git a/x-pack/plugins/apm/public/components/app/correlations/utils/get_overall_histogram.ts b/x-pack/plugins/apm/public/components/app/correlations/utils/get_overall_histogram.ts new file mode 100644 index 0000000000000..3a90eb4b89123 --- /dev/null +++ b/x-pack/plugins/apm/public/components/app/correlations/utils/get_overall_histogram.ts @@ -0,0 +1,30 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import type { LatencyCorrelationsRawResponse } from '../../../../../common/search_strategies/latency_correlations/types'; + +import { FETCH_STATUS } from '../../../../hooks/use_fetcher'; + +// `isRunning` refers to the search strategy as whole which might still be in the process +// of fetching more data such as correlation results. That's why we have to determine +// the `status` of the data for the latency chart separately. +export function getOverallHistogram( + data: LatencyCorrelationsRawResponse, + isRunning: boolean +) { + const overallHistogram = + data.overallHistogram === undefined && !isRunning + ? [] + : data.overallHistogram; + const hasData = + Array.isArray(overallHistogram) && overallHistogram.length > 0; + const status = Array.isArray(overallHistogram) + ? FETCH_STATUS.SUCCESS + : FETCH_STATUS.LOADING; + + return { overallHistogram, hasData, status }; +} diff --git a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx index 1e13a0dbe316d..9a38e0fcf6289 100644 --- a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx +++ b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.test.tsx @@ -101,7 +101,6 @@ describe('transaction_details/distribution', () => { describe('TransactionDistribution', () => { it('shows loading indicator when the service is running and returned no results yet', async () => { - const onHasData = jest.fn(); render( { ); @@ -125,12 +124,10 @@ describe('transaction_details/distribution', () => { await waitFor(() => { expect(screen.getByTestId('apmCorrelationsChart')).toBeInTheDocument(); expect(screen.getByTestId('loading')).toBeInTheDocument(); - expect(onHasData).toHaveBeenLastCalledWith(false); }); }); it("doesn't show loading indicator when the service isn't running", async () => { - const onHasData = jest.fn(); render( { ); @@ -154,7 +151,6 @@ describe('transaction_details/distribution', () => { await waitFor(() => { expect(screen.getByTestId('apmCorrelationsChart')).toBeInTheDocument(); expect(screen.queryByTestId('loading')).toBeNull(); // it doesn't exist - expect(onHasData).toHaveBeenLastCalledWith(false); }); }); }); diff --git a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx index 03f904fedd886..a35367c93cf73 100644 --- a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx +++ b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import React, { useCallback, useEffect, useState } from 'react'; +import React, { useEffect } from 'react'; import { BrushEndListener, XYBrushArea } from '@elastic/charts'; import { EuiBadge, @@ -24,12 +24,17 @@ import { } from '../../../../../common/search_strategies/constants'; import { useApmPluginContext } from '../../../../context/apm_plugin/use_apm_plugin_context'; import { useSearchStrategy } from '../../../../hooks/use_search_strategy'; -import { - OnHasData, - TransactionDistributionChart, -} from '../../../shared/charts/transaction_distribution_chart'; +import { useUrlParams } from '../../../../context/url_params_context/use_url_params'; +import { FETCH_STATUS } from '../../../../hooks/use_fetcher'; + +import { TransactionDistributionChart } from '../../../shared/charts/transaction_distribution_chart'; import { useUiTracker } from '../../../../../../observability/public'; import { isErrorMessage } from '../../correlations/utils/is_error_message'; +import { getOverallHistogram } from '../../correlations/utils/get_overall_histogram'; + +import type { TabContentProps } from '../types'; +import { useWaterfallFetcher } from '../use_waterfall_fetcher'; +import { WaterfallWithSummary } from '../waterfall_with_summary'; // Enforce min height so it's consistent across all tabs on the same level // to prevent "flickering" behavior @@ -50,33 +55,32 @@ export function getFormattedSelection(selection: Selection): string { } interface TransactionDistributionProps { - markerCurrentTransaction?: number; onChartSelection: BrushEndListener; onClearSelection: () => void; - onHasData: OnHasData; selection?: Selection; + traceSamples: TabContentProps['traceSamples']; } export function TransactionDistribution({ - markerCurrentTransaction, onChartSelection, onClearSelection, - onHasData, selection, + traceSamples, }: TransactionDistributionProps) { const { core: { notifications }, } = useApmPluginContext(); - const [showSelection, setShowSelection] = useState(false); + const { urlParams } = useUrlParams(); - const onTransactionDistributionHasData: OnHasData = useCallback( - (hasData) => { - setShowSelection(hasData); - onHasData(hasData); - }, - [onHasData] - ); + const { + waterfall, + exceedsMax, + status: waterfallStatus, + } = useWaterfallFetcher(); + + const markerCurrentTransaction = + waterfall.entryWaterfallTransaction?.doc.transaction.duration.us; const emptySelectionText = i18n.translate( 'xpack.apm.transactionDetails.emptySelectionText', @@ -101,13 +105,10 @@ export function TransactionDistribution({ ); const { error, isRunning } = state; const { percentileThresholdValue } = data; - // If loading stopped but we didn't receive any histogram data - // set it to an empty error so that the chart component finishes - // its loading state. - const overallHistogram = - data.overallHistogram === undefined && !isRunning - ? [] - : data.overallHistogram; + const { overallHistogram, hasData, status } = getOverallHistogram( + data, + isRunning + ); useEffect(() => { if (isErrorMessage(error)) { @@ -152,7 +153,7 @@ export function TransactionDistribution({ - {showSelection && !selection && ( + {hasData && !selection && ( )} - {showSelection && selection && ( + {hasData && selection && ( + + {hasData && ( + <> + + + + + )}
); } diff --git a/x-pack/plugins/apm/public/components/app/transaction_details/trace_samples_tab.tsx b/x-pack/plugins/apm/public/components/app/transaction_details/trace_samples_tab.tsx index ea02cfea5a941..ad629b7a2d132 100644 --- a/x-pack/plugins/apm/public/components/app/transaction_details/trace_samples_tab.tsx +++ b/x-pack/plugins/apm/public/components/app/transaction_details/trace_samples_tab.tsx @@ -5,19 +5,12 @@ * 2.0. */ -import React, { useState } from 'react'; - -import { EuiSpacer } from '@elastic/eui'; +import React from 'react'; import { i18n } from '@kbn/i18n'; -import { useUrlParams } from '../../../context/url_params_context/use_url_params'; -import { FETCH_STATUS } from '../../../hooks/use_fetcher'; - import { TransactionDistribution } from './distribution'; -import { useWaterfallFetcher } from './use_waterfall_fetcher'; import type { TabContentProps } from './types'; -import { WaterfallWithSummary } from './waterfall_with_summary'; function TraceSamplesTab({ selectSampleFromChartSelection, @@ -26,49 +19,17 @@ function TraceSamplesTab({ sampleRangeTo, traceSamples, }: TabContentProps) { - const { urlParams } = useUrlParams(); - - const { - waterfall, - exceedsMax, - status: waterfallStatus, - } = useWaterfallFetcher(); - - const [ - transactionDistributionHasData, - setTransactionDistributionHasData, - ] = useState(false); - return ( - <> - - - {transactionDistributionHasData && ( - <> - - - - - )} - + ); } diff --git a/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx b/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx index eb3c1506e3e2e..a57a26a7d48cc 100644 --- a/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx +++ b/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import React, { useEffect, useMemo } from 'react'; +import React, { useMemo } from 'react'; import { AnnotationDomainType, AreaSeries, @@ -35,26 +35,20 @@ import type { HistogramItem } from '../../../../../common/search_strategies/type import { FETCH_STATUS } from '../../../../hooks/use_fetcher'; import { useTheme } from '../../../../hooks/use_theme'; -import { ChartContainer, ChartContainerProps } from '../chart_container'; - -export type TransactionDistributionChartLoadingState = Pick< - ChartContainerProps, - 'hasData' | 'status' ->; - -export type OnHasData = (hasData: boolean) => void; +import { ChartContainer } from '../chart_container'; interface TransactionDistributionChartProps { field?: string; value?: string; + hasData: boolean; histogram?: HistogramItem[]; markerCurrentTransaction?: number; markerValue: number; markerPercentile: number; overallHistogram?: HistogramItem[]; onChartSelection?: BrushEndListener; - onHasData?: OnHasData; selection?: [number, number]; + status: FETCH_STATUS; } const getAnnotationsStyle = (color = 'gray'): LineAnnotationStyle => ({ @@ -105,14 +99,15 @@ const xAxisTickFormat: TickFormatter = (d) => export function TransactionDistributionChart({ field: fieldName, value: fieldValue, + hasData, histogram: originalHistogram, markerCurrentTransaction, markerValue, markerPercentile, overallHistogram, onChartSelection, - onHasData, selection, + status, }: TransactionDistributionChartProps) { const chartTheme = useChartTheme(); const euiTheme = useTheme(); @@ -163,34 +158,12 @@ export function TransactionDistributionChart({ ] : undefined; - const chartLoadingState: TransactionDistributionChartLoadingState = useMemo( - () => ({ - hasData: - Array.isArray(patchedOverallHistogram) && - patchedOverallHistogram.length > 0, - status: Array.isArray(patchedOverallHistogram) - ? FETCH_STATUS.SUCCESS - : FETCH_STATUS.LOADING, - }), - [patchedOverallHistogram] - ); - - useEffect(() => { - if (onHasData) { - onHasData(chartLoadingState.hasData); - } - }, [chartLoadingState, onHasData]); - return (
- + Date: Fri, 3 Sep 2021 13:05:04 +0200 Subject: [PATCH 25/37] [ML] Fix chart props. --- .../charts/transaction_distribution_chart/index.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx b/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx index a57a26a7d48cc..fe613da72d66c 100644 --- a/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx +++ b/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx @@ -38,8 +38,8 @@ import { useTheme } from '../../../../hooks/use_theme'; import { ChartContainer } from '../chart_container'; interface TransactionDistributionChartProps { - field?: string; - value?: string; + fieldName?: string; + fieldValue?: string; hasData: boolean; histogram?: HistogramItem[]; markerCurrentTransaction?: number; @@ -97,8 +97,8 @@ const xAxisTickFormat: TickFormatter = (d) => getDurationFormatter(d, 0.9999)(d).formatted; export function TransactionDistributionChart({ - field: fieldName, - value: fieldValue, + fieldName, + fieldValue, hasData, histogram: originalHistogram, markerCurrentTransaction, From 5f69244f40ded3f2488f48dc7b6e8bd3c954545d Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Mon, 6 Sep 2021 14:19:00 +0200 Subject: [PATCH 26/37] [ML] Tweak types for failed correlations. --- .../failed_transactions_correlations/types.ts | 34 +++++++++++++++---- .../latency_correlations/types.ts | 23 ++++++++++--- .../apm/common/search_strategies/types.ts | 5 ++- .../app/correlations/correlations_table.tsx | 11 ++---- .../failed_transactions_correlations.tsx | 4 +-- .../transaction_distribution_chart/index.tsx | 9 +++-- .../queries/query_failure_correlation.ts | 16 ++++----- 7 files changed, 68 insertions(+), 34 deletions(-) diff --git a/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts index 08d27b86f5805..02c76813eca83 100644 --- a/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts @@ -5,28 +5,48 @@ * 2.0. */ -import { RawResponseBase, SearchStrategyClientParams } from '../types'; +import { + FieldValuePair, + RawResponseBase, + SearchStrategyClientParams, +} from '../types'; import { FAILED_TRANSACTIONS_IMPACT_THRESHOLD } from './constants'; -export interface FailedTransactionsCorrelation { - key: string; +export interface FailedTransactionsCorrelation extends FieldValuePair { doc_count: number; bg_count: number; score: number; pValue: number | null; - fieldName: string; - fieldValue: string; normalizedScore: number; failurePercentage: number; successPercentage: number; } -// Basic type guard for array of FailedTransactionsCorrelation +// Type guard for populated array of FailedTransactionsCorrelation export const isFailedTransactionsCorrelations = ( arg: unknown ): arg is FailedTransactionsCorrelation[] => { - return Array.isArray(arg) && arg.length > 0; + return ( + Array.isArray(arg) && + arg.length > 0 && + arg.every( + (d) => + typeof d === 'object' && + d !== null && + Object.keys(d).length === 9 && + typeof d.doc_count === 'number' && + typeof d.bg_count === 'number' && + typeof d.score === 'number' && + (typeof d.pValue === 'number' || d.pValue === null) && + typeof d.fieldName === 'string' && + (typeof d.fieldValue === 'string' || + typeof d.fieldValue === 'number') && + typeof d.normalizedScore === 'number' && + typeof d.failurePercentage === 'number' && + typeof d.successPercentage === 'number' + ) + ); }; export type FailedTransactionsCorrelationsImpactThreshold = typeof FAILED_TRANSACTIONS_IMPACT_THRESHOLD[keyof typeof FAILED_TRANSACTIONS_IMPACT_THRESHOLD]; diff --git a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts index 6309e0045ea3d..7b297fa2b78e3 100644 --- a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts @@ -6,24 +6,37 @@ */ import { + FieldValuePair, HistogramItem, RawResponseBase, SearchStrategyClientParams, } from '../types'; -export interface LatencyCorrelation { +export interface LatencyCorrelation extends FieldValuePair { correlation: number; - fieldName: string; - fieldValue: string; histogram: HistogramItem[]; ksTest: number; } -// Basic type guard for array of LatencyCorrelation +// Type guard for populated array of LatencyCorrelation export function isLatencyCorrelations( arg: unknown ): arg is LatencyCorrelation[] { - return Array.isArray(arg) && arg.length > 0; + return ( + Array.isArray(arg) && + arg.length > 0 && + arg.every( + (d) => + typeof d === 'object' && + d !== null && + Object.keys(d).length === 5 && + typeof d.correlation === 'number' && + typeof d.fieldName === 'string' && + typeof d.fieldValue === 'string' && + Array.isArray(d.histogram) && + typeof d.ksTest === 'number' + ) + ); } export interface AsyncSearchProviderProgress { diff --git a/x-pack/plugins/apm/common/search_strategies/types.ts b/x-pack/plugins/apm/common/search_strategies/types.ts index 6963d068520dc..d7c6eab1f07c1 100644 --- a/x-pack/plugins/apm/common/search_strategies/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/types.ts @@ -7,7 +7,10 @@ export interface FieldValuePair { fieldName: string; - fieldValue: string; + // For dynamic fieldValues we only identify fields as `string`, + // but for example `http.response.status_code` which is part of + // of the list of predefined field candidates is of type long/number. + fieldValue: string | number; } export interface HistogramItem { diff --git a/x-pack/plugins/apm/public/components/app/correlations/correlations_table.tsx b/x-pack/plugins/apm/public/components/app/correlations/correlations_table.tsx index 20d0bfcd58dec..c700533ca4f45 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/correlations_table.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/correlations_table.tsx @@ -18,17 +18,12 @@ import type { FieldValuePair } from '../../../../common/search_strategies/types' const PAGINATION_SIZE_OPTIONS = [5, 10, 20, 50]; -export type SelectedCorrelationTerm = Pick< - T, - 'fieldName' | 'fieldValue' ->; - -interface Props { +interface CorrelationsTableProps { significantTerms?: T[]; status: FETCH_STATUS; percentageColumnName?: string; setSelectedSignificantTerm: (term: T | null) => void; - selectedTerm?: { fieldName: string; fieldValue: string }; + selectedTerm?: FieldValuePair; onFilter?: () => void; columns: Array>; onTableChange: (c: Criteria) => void; @@ -43,7 +38,7 @@ export function CorrelationsTable({ selectedTerm, onTableChange, sorting, -}: Props) { +}: CorrelationsTableProps) { const euiTheme = useTheme(); const trackApmEvent = useUiTracker({ app: 'apm' }); const trackSelectSignificantCorrelationTerm = useCallback( diff --git a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx index dd6c07f10a345..8def817969066 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx @@ -219,7 +219,7 @@ export function FailedTransactionsCorrelations({ sortable: true, }, { - field: 'key', + field: 'fieldValue', name: i18n.translate( 'xpack.apm.correlations.failedTransactions.correlationsTable.fieldValueLabel', { defaultMessage: 'Field value' } @@ -444,7 +444,7 @@ export function FailedTransactionsCorrelations({ - {`${selectedTerm.fieldName}: ${selectedTerm.key}`} + {`${selectedTerm.fieldName}: ${selectedTerm.fieldValue}`} , <>{`p-value: ${selectedTerm.pValue.toPrecision(3)}`}, ]} diff --git a/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx b/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx index fe613da72d66c..ee5ae0d4dc840 100644 --- a/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx +++ b/x-pack/plugins/apm/public/components/shared/charts/transaction_distribution_chart/index.tsx @@ -30,7 +30,10 @@ import { i18n } from '@kbn/i18n'; import { useChartTheme } from '../../../../../../observability/public'; import { getDurationFormatter } from '../../../../../common/utils/formatters'; -import type { HistogramItem } from '../../../../../common/search_strategies/types'; +import type { + FieldValuePair, + HistogramItem, +} from '../../../../../common/search_strategies/types'; import { FETCH_STATUS } from '../../../../hooks/use_fetcher'; import { useTheme } from '../../../../hooks/use_theme'; @@ -38,8 +41,8 @@ import { useTheme } from '../../../../hooks/use_theme'; import { ChartContainer } from '../chart_container'; interface TransactionDistributionChartProps { - fieldName?: string; - fieldValue?: string; + fieldName?: FieldValuePair['fieldName']; + fieldValue?: FieldValuePair['fieldValue']; hasData: boolean; histogram?: HistogramItem[]; markerCurrentTransaction?: number; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts index 192c298fa7721..bc8ab4be97c11 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_failure_correlation.ts @@ -74,26 +74,26 @@ export const fetchFailedTransactionsCorrelationPValues = async ( const overallResult = resp.body.aggregations .failure_p_value as estypes.AggregationsSignificantTermsAggregate<{ - key: string; + key: string | number; doc_count: number; bg_count: number; score: number; }>; const result = overallResult.buckets.map((bucket) => { - const score = bucket.score; - // Scale the score into a value from 0 - 1 // using a concave piecewise linear function in -log(p-value) const normalizedScore = - 0.5 * Math.min(Math.max((score - 3.912) / 2.995, 0), 1) + - 0.25 * Math.min(Math.max((score - 6.908) / 6.908, 0), 1) + - 0.25 * Math.min(Math.max((score - 13.816) / 101.314, 0), 1); + 0.5 * Math.min(Math.max((bucket.score - 3.912) / 2.995, 0), 1) + + 0.25 * Math.min(Math.max((bucket.score - 6.908) / 6.908, 0), 1) + + 0.25 * Math.min(Math.max((bucket.score - 13.816) / 101.314, 0), 1); return { - ...bucket, fieldName, fieldValue: bucket.key, - pValue: Math.exp(-score), + doc_count: bucket.doc_count, + bg_count: bucket.doc_count, + score: bucket.score, + pValue: Math.exp(-bucket.score), normalizedScore, // Percentage of time the term appears in failed transactions failurePercentage: bucket.doc_count / overallResult.doc_count, From e96aed98fef032200afb97897622079463c11969 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Mon, 6 Sep 2021 14:55:02 +0200 Subject: [PATCH 27/37] [ML] Improve FieldValuePair type usage. --- .../failed_transactions_correlations.tsx | 4 +++- .../app/correlations/latency_correlations.tsx | 4 +++- .../queries/get_query_with_params.ts | 13 ++++++++----- .../search_strategies/queries/query_correlation.ts | 9 +++++---- .../search_strategies/queries/query_histogram.ts | 9 +++++---- .../search_strategies/queries/query_percentiles.ts | 9 +++++---- .../lib/search_strategies/queries/query_ranges.ts | 9 +++++---- 7 files changed, 34 insertions(+), 23 deletions(-) diff --git a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx index 8def817969066..84bcadbca627a 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx @@ -36,6 +36,7 @@ import { FailedTransactionsCorrelation, } from '../../../../common/search_strategies/failed_transactions_correlations/types'; import { APM_SEARCH_STRATEGIES } from '../../../../common/search_strategies/constants'; +import { FieldValuePair } from '../../../../common/search_strategies/types'; import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; import { FETCH_STATUS } from '../../../hooks/use_fetcher'; @@ -224,7 +225,8 @@ export function FailedTransactionsCorrelations({ 'xpack.apm.correlations.failedTransactions.correlationsTable.fieldValueLabel', { defaultMessage: 'Field value' } ), - render: (fieldValue: string) => String(fieldValue).slice(0, 50), + render: (fieldValue: FieldValuePair['fieldValue']) => + String(fieldValue).slice(0, 50), sortable: true, }, ...percentageColumns, diff --git a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx index bcdee7a35dbcd..fa463bdc92e24 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx @@ -33,6 +33,7 @@ import { APM_SEARCH_STRATEGIES, DEFAULT_PERCENTILE_THRESHOLD, } from '../../../../common/search_strategies/constants'; +import { FieldValuePair } from '../../../../common/search_strategies/types'; import { isLatencyCorrelations, LatencyCorrelation, @@ -173,7 +174,8 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { 'xpack.apm.correlations.latencyCorrelations.correlationsTable.fieldValueLabel', { defaultMessage: 'Field value' } ), - render: (fieldValue: string) => String(fieldValue).slice(0, 50), + render: (fieldValue: FieldValuePair['fieldValue']) => + String(fieldValue).slice(0, 50), sortable: true, }, { diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts index 6220e1be098ff..445f432f2d5ad 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/get_query_with_params.ts @@ -10,22 +10,25 @@ import { getOrElse } from 'fp-ts/lib/Either'; import { pipe } from 'fp-ts/lib/pipeable'; import * as t from 'io-ts'; import { failure } from 'io-ts/lib/PathReporter'; -import type { SearchStrategyParams } from '../../../../common/search_strategies/types'; +import type { + FieldValuePair, + SearchStrategyParams, +} from '../../../../common/search_strategies/types'; import { rangeRt } from '../../../routes/default_api_types'; import { getCorrelationsFilters } from '../../correlations/get_filters'; import { Setup, SetupTimeRange } from '../../helpers/setup_request'; export const getTermsQuery = ( - fieldName: string | undefined, - fieldValue: string | undefined + fieldName: FieldValuePair['fieldName'] | undefined, + fieldValue: FieldValuePair['fieldValue'] | undefined ) => { return fieldName && fieldValue ? [{ term: { [fieldName]: fieldValue } }] : []; }; interface QueryParams { params: SearchStrategyParams; - fieldName?: string; - fieldValue?: string; + fieldName?: FieldValuePair['fieldName']; + fieldValue?: FieldValuePair['fieldValue']; } export const getQueryWithParams = ({ params, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts index b00023d417ac2..6e2981032d67d 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_correlation.ts @@ -11,6 +11,7 @@ import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; import type { + FieldValuePair, ResponseHit, SearchStrategyParams, } from '../../../../common/search_strategies/types'; @@ -37,8 +38,8 @@ export const getTransactionDurationCorrelationRequest = ( ranges: estypes.AggregationsAggregationRange[], fractions: number[], totalDocCount: number, - fieldName?: string, - fieldValue?: string + fieldName?: FieldValuePair['fieldName'], + fieldValue?: FieldValuePair['fieldValue'] ): estypes.SearchRequest => { const query = getQueryWithParams({ params, fieldName, fieldValue }); @@ -92,8 +93,8 @@ export const fetchTransactionDurationCorrelation = async ( ranges: estypes.AggregationsAggregationRange[], fractions: number[], totalDocCount: number, - fieldName?: string, - fieldValue?: string + fieldName?: FieldValuePair['fieldName'], + fieldValue?: FieldValuePair['fieldValue'] ): Promise<{ ranges: unknown[]; correlation: number | null; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts index d9a2d0a6264d3..1dac98d785f3c 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histogram.ts @@ -11,6 +11,7 @@ import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; import type { + FieldValuePair, HistogramItem, ResponseHit, SearchStrategyParams, @@ -22,8 +23,8 @@ import { getRequestBase } from './get_request_base'; export const getTransactionDurationHistogramRequest = ( params: SearchStrategyParams, interval: number, - fieldName?: string, - fieldValue?: string + fieldName?: FieldValuePair['fieldName'], + fieldValue?: FieldValuePair['fieldValue'] ): estypes.SearchRequest => ({ ...getRequestBase(params), body: { @@ -41,8 +42,8 @@ export const fetchTransactionDurationHistogram = async ( esClient: ElasticsearchClient, params: SearchStrategyParams, interval: number, - fieldName?: string, - fieldValue?: string + fieldName?: FieldValuePair['fieldName'], + fieldValue?: FieldValuePair['fieldValue'] ): Promise => { const resp = await esClient.search( getTransactionDurationHistogramRequest( diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts index a1d0c4c560df4..8d9e2ed88ba37 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_percentiles.ts @@ -11,6 +11,7 @@ import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; import type { + FieldValuePair, ResponseHit, SearchStrategyParams, } from '../../../../common/search_strategies/types'; @@ -22,8 +23,8 @@ import { SIGNIFICANT_VALUE_DIGITS } from '../constants'; export const getTransactionDurationPercentilesRequest = ( params: SearchStrategyParams, percents?: number[], - fieldName?: string, - fieldValue?: string + fieldName?: FieldValuePair['fieldName'], + fieldValue?: FieldValuePair['fieldValue'] ): estypes.SearchRequest => { const query = getQueryWithParams({ params, fieldName, fieldValue }); @@ -52,8 +53,8 @@ export const fetchTransactionDurationPercentiles = async ( esClient: ElasticsearchClient, params: SearchStrategyParams, percents?: number[], - fieldName?: string, - fieldValue?: string + fieldName?: FieldValuePair['fieldName'], + fieldValue?: FieldValuePair['fieldValue'] ): Promise<{ totalDocs: number; percentiles: Record }> => { const resp = await esClient.search( getTransactionDurationPercentilesRequest( diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts index 29f25bc277c1c..e15962f2979ba 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_ranges.ts @@ -11,6 +11,7 @@ import type { ElasticsearchClient } from 'src/core/server'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; import type { + FieldValuePair, ResponseHit, SearchStrategyParams, } from '../../../../common/search_strategies/types'; @@ -21,8 +22,8 @@ import { getRequestBase } from './get_request_base'; export const getTransactionDurationRangesRequest = ( params: SearchStrategyParams, rangesSteps: number[], - fieldName?: string, - fieldValue?: string + fieldName?: FieldValuePair['fieldName'], + fieldValue?: FieldValuePair['fieldValue'] ): estypes.SearchRequest => { const query = getQueryWithParams({ params, fieldName, fieldValue }); @@ -59,8 +60,8 @@ export const fetchTransactionDurationRanges = async ( esClient: ElasticsearchClient, params: SearchStrategyParams, rangesSteps: number[], - fieldName?: string, - fieldValue?: string + fieldName?: FieldValuePair['fieldName'], + fieldValue?: FieldValuePair['fieldValue'] ): Promise> => { const resp = await esClient.search( getTransactionDurationRangesRequest( From 1656d8dd432c36d980b9cf3064145cab809e9cdb Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Mon, 6 Sep 2021 15:26:45 +0200 Subject: [PATCH 28/37] [ML] Remove 'async' from variable names. --- .../latency_correlations/types.ts | 2 +- ...ansactions_correlations_search_service.ts} | 14 ++--- ...ions_correlations_search_service_state.ts} | 6 +- .../failed_transactions_correlations/index.ts | 6 +- .../latency_correlations/index.ts | 6 +- ...=> latency_correlations_search_service.ts} | 14 ++--- ...correlations_search_service_state.test.ts} | 10 ++-- ...ency_correlations_search_service_state.ts} | 12 ++-- .../queries/query_field_value_pairs.test.ts | 8 +-- .../queries/query_field_value_pairs.ts | 10 ++-- .../query_histograms_generator.test.ts | 12 ++-- .../queries/query_histograms_generator.ts | 8 +-- .../register_search_strategies.ts | 8 +-- ...log.test.ts => search_service_log.test.ts} | 10 ++-- ...h_service_log.ts => search_service_log.ts} | 6 +- .../search_strategy_provider.test.ts | 16 ++--- .../search_strategy_provider.ts | 60 +++++++++---------- 17 files changed, 102 insertions(+), 106 deletions(-) rename x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/{async_search_service.ts => failed_transactions_correlations_search_service.ts} (88%) rename x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/{async_search_service_state.ts => failed_transactions_correlations_search_service_state.ts} (90%) rename x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/{async_search_service.ts => latency_correlations_search_service.ts} (93%) rename x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/{async_search_service_state.test.ts => latency_correlations_search_service_state.test.ts} (86%) rename x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/{async_search_service_state.ts => latency_correlations_search_service_state.ts} (87%) rename x-pack/plugins/apm/server/lib/search_strategies/{async_search_service_log.test.ts => search_service_log.test.ts} (84%) rename x-pack/plugins/apm/server/lib/search_strategies/{async_search_service_log.ts => search_service_log.ts} (84%) diff --git a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts index 7b297fa2b78e3..9710fdcd5dc37 100644 --- a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts @@ -39,7 +39,7 @@ export function isLatencyCorrelations( ); } -export interface AsyncSearchProviderProgress { +export interface LatencyCorrelationSearchServiceProgress { started: number; loadedHistogramStepsize: number; loadedOverallHistogram: number; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/failed_transactions_correlations_search_service.ts similarity index 88% rename from x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts rename to x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/failed_transactions_correlations_search_service.ts index 507f61ba61744..12f7902b51488 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/failed_transactions_correlations_search_service.ts @@ -22,18 +22,18 @@ import type { FailedTransactionsCorrelationsRawResponse, } from '../../../../common/search_strategies/failed_transactions_correlations/types'; import type { ApmIndicesConfig } from '../../settings/apm_indices/get_apm_indices'; -import { asyncSearchServiceLogProvider } from '../async_search_service_log'; +import { searchServiceLogProvider } from '../search_service_log'; import { fetchFailedTransactionsCorrelationPValues, fetchTransactionDurationFieldCandidates, } from '../queries'; -import type { AsyncSearchServiceProvider } from '../search_strategy_provider'; +import type { SearchServiceProvider } from '../search_strategy_provider'; -import { asyncFailedTransactionsCorrelationsSearchServiceStateProvider } from './async_search_service_state'; +import { failedTransactionsCorrelationsSearchServiceStateProvider } from './failed_transactions_correlations_search_service_state'; import { ERROR_CORRELATION_THRESHOLD } from '../constants'; -export type FailedTransactionsCorrelationsAsyncSearchServiceProvider = AsyncSearchServiceProvider< +export type FailedTransactionsCorrelationsSearchServiceProvider = SearchServiceProvider< FailedTransactionsCorrelationsParams, FailedTransactionsCorrelationsRawResponse >; @@ -43,15 +43,15 @@ export type FailedTransactionsCorrelationsSearchStrategy = ISearchStrategy< IKibanaSearchResponse >; -export const failedTransactionsCorrelationsAsyncSearchServiceProvider: FailedTransactionsCorrelationsAsyncSearchServiceProvider = ( +export const failedTransactionsCorrelationsSearchServiceProvider: FailedTransactionsCorrelationsSearchServiceProvider = ( esClient: ElasticsearchClient, getApmIndices: () => Promise, searchServiceParams: FailedTransactionsCorrelationsParams, includeFrozen: boolean ) => { - const { addLogMessage, getLogMessages } = asyncSearchServiceLogProvider(); + const { addLogMessage, getLogMessages } = searchServiceLogProvider(); - const state = asyncFailedTransactionsCorrelationsSearchServiceStateProvider(); + const state = failedTransactionsCorrelationsSearchServiceStateProvider(); async function fetchErrorCorrelations() { try { diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service_state.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/failed_transactions_correlations_search_service_state.ts similarity index 90% rename from x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service_state.ts rename to x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/failed_transactions_correlations_search_service_state.ts index 2a1d20eeb1cc2..13cf752618537 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/async_search_service_state.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/failed_transactions_correlations_search_service_state.ts @@ -12,7 +12,7 @@ interface Progress { loadedFieldCandidates: number; loadedFailedTransactionsCorrelations: number; } -export const asyncFailedTransactionsCorrelationsSearchServiceStateProvider = () => { +export const failedTransactionsCorrelationsSearchServiceStateProvider = () => { let ccsWarning = false; function setCcsWarning(d: boolean) { ccsWarning = d; @@ -90,6 +90,6 @@ export const asyncFailedTransactionsCorrelationsSearchServiceStateProvider = () }; }; -export type AsyncSearchServiceState = ReturnType< - typeof asyncFailedTransactionsCorrelationsSearchServiceStateProvider +export type FailedTransactionsCorrelationsSearchServiceState = ReturnType< + typeof failedTransactionsCorrelationsSearchServiceStateProvider >; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/index.ts b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/index.ts index 96244aaa6e937..ec91165cb481b 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/index.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/failed_transactions_correlations/index.ts @@ -6,7 +6,7 @@ */ export { - failedTransactionsCorrelationsAsyncSearchServiceProvider, - FailedTransactionsCorrelationsAsyncSearchServiceProvider, + failedTransactionsCorrelationsSearchServiceProvider, + FailedTransactionsCorrelationsSearchServiceProvider, FailedTransactionsCorrelationsSearchStrategy, -} from './async_search_service'; +} from './failed_transactions_correlations_search_service'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/index.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/index.ts index 9d341e82f0422..073bb122896ff 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/index.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/index.ts @@ -6,7 +6,7 @@ */ export { - latencyCorrelationsAsyncSearchServiceProvider, - LatencyCorrelationsAsyncSearchServiceProvider, + latencyCorrelationsSearchServiceProvider, + LatencyCorrelationsSearchServiceProvider, LatencyCorrelationsSearchStrategy, -} from './async_search_service'; +} from './latency_correlations_search_service'; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service.ts similarity index 93% rename from x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts rename to x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service.ts index 3e08d2b051a7a..a5f5045642c47 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service.ts @@ -32,12 +32,12 @@ import { fetchTransactionDurationRanges, } from '../queries'; import { computeExpectationsAndRanges } from '../utils'; -import { asyncSearchServiceLogProvider } from '../async_search_service_log'; -import type { AsyncSearchServiceProvider } from '../search_strategy_provider'; +import { searchServiceLogProvider } from '../search_service_log'; +import type { SearchServiceProvider } from '../search_strategy_provider'; -import { asyncSearchServiceStateProvider } from './async_search_service_state'; +import { latencyCorrelationsSearchServiceStateProvider } from './latency_correlations_search_service_state'; -export type LatencyCorrelationsAsyncSearchServiceProvider = AsyncSearchServiceProvider< +export type LatencyCorrelationsSearchServiceProvider = SearchServiceProvider< LatencyCorrelationsParams, LatencyCorrelationsRawResponse >; @@ -47,15 +47,15 @@ export type LatencyCorrelationsSearchStrategy = ISearchStrategy< IKibanaSearchResponse >; -export const latencyCorrelationsAsyncSearchServiceProvider: LatencyCorrelationsAsyncSearchServiceProvider = ( +export const latencyCorrelationsSearchServiceProvider: LatencyCorrelationsSearchServiceProvider = ( esClient: ElasticsearchClient, getApmIndices: () => Promise, searchServiceParams: LatencyCorrelationsParams, includeFrozen: boolean ) => { - const { addLogMessage, getLogMessages } = asyncSearchServiceLogProvider(); + const { addLogMessage, getLogMessages } = searchServiceLogProvider(); - const state = asyncSearchServiceStateProvider(); + const state = latencyCorrelationsSearchServiceStateProvider(); async function fetchCorrelations() { let params: diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service_state.test.ts similarity index 86% rename from x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service_state.test.ts index cfa1bf2a5ad71..fa95378086c94 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service_state.test.ts @@ -5,12 +5,12 @@ * 2.0. */ -import { asyncSearchServiceStateProvider } from './async_search_service_state'; +import { latencyCorrelationsSearchServiceStateProvider } from './latency_correlations_search_service_state'; -describe('async search service', () => { - describe('asyncSearchServiceStateProvider', () => { +describe('search service', () => { + describe('latencyCorrelationsSearchServiceStateProvider', () => { it('initializes with default state', () => { - const state = asyncSearchServiceStateProvider(); + const state = latencyCorrelationsSearchServiceStateProvider(); const defaultState = state.getState(); const defaultProgress = state.getOverallProgress(); @@ -30,7 +30,7 @@ describe('async search service', () => { }); it('returns updated state', () => { - const state = asyncSearchServiceStateProvider(); + const state = latencyCorrelationsSearchServiceStateProvider(); state.setCcsWarning(true); state.setError(new Error('the-error-message')); diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service_state.ts similarity index 87% rename from x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts rename to x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service_state.ts index 3cdd03869710a..0b92ca3ef8636 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/async_search_service_state.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service_state.ts @@ -7,11 +7,11 @@ import type { HistogramItem } from '../../../../common/search_strategies/types'; import type { - AsyncSearchProviderProgress, + LatencyCorrelationSearchServiceProgress, LatencyCorrelation, } from '../../../../common/search_strategies/latency_correlations/types'; -export const asyncSearchServiceStateProvider = () => { +export const latencyCorrelationsSearchServiceStateProvider = () => { let ccsWarning = false; function setCcsWarning(d: boolean) { ccsWarning = d; @@ -45,7 +45,7 @@ export const asyncSearchServiceStateProvider = () => { percentileThresholdValue = d; } - let progress: AsyncSearchProviderProgress = { + let progress: LatencyCorrelationSearchServiceProgress = { started: Date.now(), loadedHistogramStepsize: 0, loadedOverallHistogram: 0, @@ -63,7 +63,7 @@ export const asyncSearchServiceStateProvider = () => { ); } function setProgress( - d: Partial> + d: Partial> ) { progress = { ...progress, @@ -109,6 +109,6 @@ export const asyncSearchServiceStateProvider = () => { }; }; -export type AsyncSearchServiceState = ReturnType< - typeof asyncSearchServiceStateProvider +export type LatencyCorrelationsSearchServiceState = ReturnType< + typeof latencyCorrelationsSearchServiceStateProvider >; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.test.ts index 17988b334e680..1fff8cde5bbb3 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.test.ts @@ -10,8 +10,8 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; -import { asyncSearchServiceLogProvider } from '../async_search_service_log'; -import { asyncSearchServiceStateProvider } from '../latency_correlations/async_search_service_state'; +import { searchServiceLogProvider } from '../search_service_log'; +import { latencyCorrelationsSearchServiceStateProvider } from '../latency_correlations/latency_correlations_search_service_state'; import { fetchTransactionDurationFieldValuePairs, @@ -62,8 +62,8 @@ describe('query_field_value_pairs', () => { search: esClientSearchMock, } as unknown) as ElasticsearchClient; - const { addLogMessage, getLogMessages } = asyncSearchServiceLogProvider(); - const state = asyncSearchServiceStateProvider(); + const { addLogMessage, getLogMessages } = searchServiceLogProvider(); + const state = latencyCorrelationsSearchServiceStateProvider(); const resp = await fetchTransactionDurationFieldValuePairs( esClientMock, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts index 3df7b1f46c567..aa7d9f341a345 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_field_value_pairs.ts @@ -14,8 +14,8 @@ import type { SearchStrategyParams, } from '../../../../common/search_strategies/types'; -import type { AsyncSearchServiceLog } from '../async_search_service_log'; -import type { AsyncSearchServiceState } from '../latency_correlations/async_search_service_state'; +import type { SearchServiceLog } from '../search_service_log'; +import type { LatencyCorrelationsSearchServiceState } from '../latency_correlations/latency_correlations_search_service_state'; import { TERMS_SIZE } from '../constants'; import { getQueryWithParams } from './get_query_with_params'; @@ -44,7 +44,7 @@ const fetchTransactionDurationFieldTerms = async ( esClient: ElasticsearchClient, params: SearchStrategyParams, fieldName: string, - addLogMessage: AsyncSearchServiceLog['addLogMessage'] + addLogMessage: SearchServiceLog['addLogMessage'] ): Promise => { try { const resp = await esClient.search(getTermsAggRequest(params, fieldName)); @@ -93,8 +93,8 @@ export const fetchTransactionDurationFieldValuePairs = async ( esClient: ElasticsearchClient, params: SearchStrategyParams, fieldCandidates: string[], - state: AsyncSearchServiceState, - addLogMessage: AsyncSearchServiceLog['addLogMessage'] + state: LatencyCorrelationsSearchServiceState, + addLogMessage: SearchServiceLog['addLogMessage'] ): Promise => { let fieldValuePairsProgress = 1; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.test.ts index 4144dc17d043a..c80b2533d7e32 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.test.ts @@ -10,8 +10,8 @@ import type { estypes } from '@elastic/elasticsearch'; import type { ElasticsearchClient } from 'src/core/server'; import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values'; -import { asyncSearchServiceLogProvider } from '../async_search_service_log'; -import { asyncSearchServiceStateProvider } from '../latency_correlations/async_search_service_state'; +import { searchServiceLogProvider } from '../search_service_log'; +import { latencyCorrelationsSearchServiceStateProvider } from '../latency_correlations/latency_correlations_search_service_state'; import { fetchTransactionDurationHistograms } from './query_histograms_generator'; @@ -50,8 +50,8 @@ describe('query_histograms_generator', () => { search: esClientSearchMock, } as unknown) as ElasticsearchClient; - const state = asyncSearchServiceStateProvider(); - const { addLogMessage, getLogMessages } = asyncSearchServiceLogProvider(); + const state = latencyCorrelationsSearchServiceStateProvider(); + const { addLogMessage, getLogMessages } = searchServiceLogProvider(); let loadedHistograms = 0; const items = []; @@ -104,8 +104,8 @@ describe('query_histograms_generator', () => { search: esClientSearchMock, } as unknown) as ElasticsearchClient; - const state = asyncSearchServiceStateProvider(); - const { addLogMessage, getLogMessages } = asyncSearchServiceLogProvider(); + const state = latencyCorrelationsSearchServiceStateProvider(); + const { addLogMessage, getLogMessages } = searchServiceLogProvider(); let loadedHistograms = 0; const items = []; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts index 4dc59083186f2..a07abd356db6d 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/queries/query_histograms_generator.ts @@ -14,8 +14,8 @@ import type { SearchStrategyParams, } from '../../../../common/search_strategies/types'; -import type { AsyncSearchServiceLog } from '../async_search_service_log'; -import type { AsyncSearchServiceState } from '../latency_correlations/async_search_service_state'; +import type { SearchServiceLog } from '../search_service_log'; +import type { LatencyCorrelationsSearchServiceState } from '../latency_correlations/latency_correlations_search_service_state'; import { CORRELATION_THRESHOLD, KS_TEST_THRESHOLD } from '../constants'; import { getPrioritizedFieldValuePairs } from './get_prioritized_field_value_pairs'; @@ -24,9 +24,9 @@ import { fetchTransactionDurationRanges } from './query_ranges'; export async function* fetchTransactionDurationHistograms( esClient: ElasticsearchClient, - addLogMessage: AsyncSearchServiceLog['addLogMessage'], + addLogMessage: SearchServiceLog['addLogMessage'], params: SearchStrategyParams, - state: AsyncSearchServiceState, + state: LatencyCorrelationsSearchServiceState, expectations: number[], ranges: estypes.AggregationsAggregationRange[], fractions: number[], diff --git a/x-pack/plugins/apm/server/lib/search_strategies/register_search_strategies.ts b/x-pack/plugins/apm/server/lib/search_strategies/register_search_strategies.ts index 33b764be2c625..713c5e390ca8b 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/register_search_strategies.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/register_search_strategies.ts @@ -11,8 +11,8 @@ import { APM_SEARCH_STRATEGIES } from '../../../common/search_strategies/constan import type { ApmIndicesConfig } from '../settings/apm_indices/get_apm_indices'; -import { failedTransactionsCorrelationsAsyncSearchServiceProvider } from './failed_transactions_correlations'; -import { latencyCorrelationsAsyncSearchServiceProvider } from './latency_correlations'; +import { failedTransactionsCorrelationsSearchServiceProvider } from './failed_transactions_correlations'; +import { latencyCorrelationsSearchServiceProvider } from './latency_correlations'; import { searchStrategyProvider } from './search_strategy_provider'; export const registerSearchStrategies = ( @@ -23,7 +23,7 @@ export const registerSearchStrategies = ( registerSearchStrategy( APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, searchStrategyProvider( - latencyCorrelationsAsyncSearchServiceProvider, + latencyCorrelationsSearchServiceProvider, getApmIndices, includeFrozen ) @@ -32,7 +32,7 @@ export const registerSearchStrategies = ( registerSearchStrategy( APM_SEARCH_STRATEGIES.APM_FAILED_TRANSACTIONS_CORRELATIONS, searchStrategyProvider( - failedTransactionsCorrelationsAsyncSearchServiceProvider, + failedTransactionsCorrelationsSearchServiceProvider, getApmIndices, includeFrozen ) diff --git a/x-pack/plugins/apm/server/lib/search_strategies/async_search_service_log.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/search_service_log.test.ts similarity index 84% rename from x-pack/plugins/apm/server/lib/search_strategies/async_search_service_log.test.ts rename to x-pack/plugins/apm/server/lib/search_strategies/search_service_log.test.ts index bfc28bc37ea78..c21b31bb4462a 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/async_search_service_log.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/search_service_log.test.ts @@ -6,11 +6,11 @@ */ import { - asyncSearchServiceLogProvider, + searchServiceLogProvider, currentTimeAsString, -} from './async_search_service_log'; +} from './search_service_log'; -describe('async search service', () => { +describe('search service', () => { describe('currentTimeAsString', () => { it('returns the current time as a string', () => { const mockDate = new Date(1392202800000); @@ -25,9 +25,9 @@ describe('async search service', () => { }); }); - describe('asyncSearchServiceLogProvider', () => { + describe('searchServiceLogProvider', () => { it('adds and retrieves messages from the log', async () => { - const { addLogMessage, getLogMessages } = asyncSearchServiceLogProvider(); + const { addLogMessage, getLogMessages } = searchServiceLogProvider(); const mockDate = new Date(1392202800000); // @ts-ignore ignore the mockImplementation callback error diff --git a/x-pack/plugins/apm/server/lib/search_strategies/async_search_service_log.ts b/x-pack/plugins/apm/server/lib/search_strategies/search_service_log.ts similarity index 84% rename from x-pack/plugins/apm/server/lib/search_strategies/async_search_service_log.ts rename to x-pack/plugins/apm/server/lib/search_strategies/search_service_log.ts index 058a902026c48..73a59021b01ed 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/async_search_service_log.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/search_service_log.ts @@ -13,7 +13,7 @@ interface LogMessage { export const currentTimeAsString = () => new Date().toISOString(); -export const asyncSearchServiceLogProvider = () => { +export const searchServiceLogProvider = () => { const log: LogMessage[] = []; function addLogMessage(message: string, error?: string) { @@ -31,6 +31,4 @@ export const asyncSearchServiceLogProvider = () => { return { addLogMessage, getLogMessages }; }; -export type AsyncSearchServiceLog = ReturnType< - typeof asyncSearchServiceLogProvider ->; +export type SearchServiceLog = ReturnType; diff --git a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts index 2bbca7abd7c3a..b45b95666326f 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.test.ts @@ -16,7 +16,7 @@ import type { LatencyCorrelationsParams } from '../../../common/search_strategie import type { ApmIndicesConfig } from '../settings/apm_indices/get_apm_indices'; -import { latencyCorrelationsAsyncSearchServiceProvider } from './latency_correlations'; +import { latencyCorrelationsSearchServiceProvider } from './latency_correlations'; import { searchStrategyProvider } from './search_strategy_provider'; // helper to trigger promises in the async search service @@ -109,7 +109,7 @@ describe('APM Correlations search strategy', () => { describe('strategy interface', () => { it('returns a custom search strategy with a `search` and `cancel` function', async () => { const searchStrategy = await searchStrategyProvider( - latencyCorrelationsAsyncSearchServiceProvider, + latencyCorrelationsSearchServiceProvider, getApmIndicesMock, false ); @@ -153,7 +153,7 @@ describe('APM Correlations search strategy', () => { describe('when no params are provided', () => { it('throws an error', async () => { const searchStrategy = await searchStrategyProvider( - latencyCorrelationsAsyncSearchServiceProvider, + latencyCorrelationsSearchServiceProvider, mockGetApmIndicesMock, false ); @@ -169,7 +169,7 @@ describe('APM Correlations search strategy', () => { describe('when no ID is provided', () => { it('performs a client search with params', async () => { const searchStrategy = await searchStrategyProvider( - latencyCorrelationsAsyncSearchServiceProvider, + latencyCorrelationsSearchServiceProvider, mockGetApmIndicesMock, false ); @@ -217,7 +217,7 @@ describe('APM Correlations search strategy', () => { describe('when an ID with params is provided', () => { it('retrieves the current request', async () => { const searchStrategy = await searchStrategyProvider( - latencyCorrelationsAsyncSearchServiceProvider, + latencyCorrelationsSearchServiceProvider, mockGetApmIndicesMock, false ); @@ -244,7 +244,7 @@ describe('APM Correlations search strategy', () => { .mockReset() .mockRejectedValueOnce(new Error('client error')); const searchStrategy = await searchStrategyProvider( - latencyCorrelationsAsyncSearchServiceProvider, + latencyCorrelationsSearchServiceProvider, mockGetApmIndicesMock, false ); @@ -263,7 +263,7 @@ describe('APM Correlations search strategy', () => { it('triggers the subscription only once', async () => { expect.assertions(2); const searchStrategy = await searchStrategyProvider( - latencyCorrelationsAsyncSearchServiceProvider, + latencyCorrelationsSearchServiceProvider, mockGetApmIndicesMock, false ); @@ -281,7 +281,7 @@ describe('APM Correlations search strategy', () => { describe('response', () => { it('sends an updated response on consecutive search calls', async () => { const searchStrategy = await searchStrategyProvider( - latencyCorrelationsAsyncSearchServiceProvider, + latencyCorrelationsSearchServiceProvider, mockGetApmIndicesMock, false ); diff --git a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts index dfe936b60bec0..c0376852b2505 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/search_strategy_provider.ts @@ -21,15 +21,15 @@ import type { RawResponseBase } from '../../../common/search_strategies/types'; import type { ApmIndicesConfig } from '../settings/apm_indices/get_apm_indices'; import type { - LatencyCorrelationsAsyncSearchServiceProvider, + LatencyCorrelationsSearchServiceProvider, LatencyCorrelationsSearchStrategy, } from './latency_correlations'; import type { - FailedTransactionsCorrelationsAsyncSearchServiceProvider, + FailedTransactionsCorrelationsSearchServiceProvider, FailedTransactionsCorrelationsSearchStrategy, } from './failed_transactions_correlations'; -interface AsyncSearchServiceState { +interface SearchServiceState { cancel: () => void; error: Error; meta: { @@ -41,11 +41,11 @@ interface AsyncSearchServiceState { rawResponse: TRawResponse; } -type GetAsyncSearchServiceState< +type GetSearchServiceState< TRawResponse extends RawResponseBase -> = () => AsyncSearchServiceState; +> = () => SearchServiceState; -export type AsyncSearchServiceProvider< +export type SearchServiceProvider< TSearchStrategyClientParams extends SearchStrategyClientParams, TRawResponse extends RawResponseBase > = ( @@ -53,18 +53,18 @@ export type AsyncSearchServiceProvider< getApmIndices: () => Promise, searchServiceParams: TSearchStrategyClientParams, includeFrozen: boolean -) => GetAsyncSearchServiceState; +) => GetSearchServiceState; // Failed Transactions Correlations function overload export function searchStrategyProvider( - asyncSearchServiceProvider: FailedTransactionsCorrelationsAsyncSearchServiceProvider, + searchServiceProvider: FailedTransactionsCorrelationsSearchServiceProvider, getApmIndices: () => Promise, includeFrozen: boolean ): FailedTransactionsCorrelationsSearchStrategy; // Latency Correlations function overload export function searchStrategyProvider( - asyncSearchServiceProvider: LatencyCorrelationsAsyncSearchServiceProvider, + searchServiceProvider: LatencyCorrelationsSearchServiceProvider, getApmIndices: () => Promise, includeFrozen: boolean ): LatencyCorrelationsSearchStrategy; @@ -73,7 +73,7 @@ export function searchStrategyProvider< TSearchStrategyClientParams extends SearchStrategyClientParams, TRawResponse extends RawResponseBase >( - asyncSearchServiceProvider: AsyncSearchServiceProvider< + searchServiceProvider: SearchServiceProvider< TSearchStrategyClientParams, TRawResponse >, @@ -83,9 +83,9 @@ export function searchStrategyProvider< IKibanaSearchRequest, IKibanaSearchResponse > { - const asyncSearchServiceMap = new Map< + const searchServiceMap = new Map< string, - GetAsyncSearchServiceState + GetSearchServiceState >(); return { @@ -94,27 +94,25 @@ export function searchStrategyProvider< throw new Error('Invalid request parameters.'); } - // The function to fetch the current state of the async search service. + // The function to fetch the current state of the search service. // This will be either an existing service for a follow up fetch or a new one for new requests. - let getAsyncSearchServiceState: GetAsyncSearchServiceState; + let getSearchServiceState: GetSearchServiceState; - // If the request includes an ID, we require that the async search service already exists + // If the request includes an ID, we require that the search service already exists // otherwise we throw an error. The client should never poll a service that's been cancelled or finished. - // This also avoids instantiating async search services when the service gets called with random IDs. + // This also avoids instantiating search services when the service gets called with random IDs. if (typeof request.id === 'string') { - const existingGetAsyncSearchServiceState = asyncSearchServiceMap.get( - request.id - ); + const existingGetSearchServiceState = searchServiceMap.get(request.id); - if (typeof existingGetAsyncSearchServiceState === 'undefined') { + if (typeof existingGetSearchServiceState === 'undefined') { throw new Error( - `AsyncSearchService with ID '${request.id}' does not exist.` + `SearchService with ID '${request.id}' does not exist.` ); } - getAsyncSearchServiceState = existingGetAsyncSearchServiceState; + getSearchServiceState = existingGetSearchServiceState; } else { - getAsyncSearchServiceState = asyncSearchServiceProvider( + getSearchServiceState = searchServiceProvider( deps.esClient.asCurrentUser, getApmIndices, request.params as TSearchStrategyClientParams, @@ -125,15 +123,15 @@ export function searchStrategyProvider< // Reuse the request's id or create a new one. const id = request.id ?? uuid(); - const { error, meta, rawResponse } = getAsyncSearchServiceState(); + const { error, meta, rawResponse } = getSearchServiceState(); if (error instanceof Error) { - asyncSearchServiceMap.delete(id); + searchServiceMap.delete(id); throw error; } else if (meta.isRunning) { - asyncSearchServiceMap.set(id, getAsyncSearchServiceState); + searchServiceMap.set(id, getSearchServiceState); } else { - asyncSearchServiceMap.delete(id); + searchServiceMap.delete(id); } return of({ @@ -143,10 +141,10 @@ export function searchStrategyProvider< }); }, cancel: async (id, options, deps) => { - const getAsyncSearchServiceState = asyncSearchServiceMap.get(id); - if (getAsyncSearchServiceState !== undefined) { - getAsyncSearchServiceState().cancel(); - asyncSearchServiceMap.delete(id); + const getSearchServiceState = searchServiceMap.get(id); + if (getSearchServiceState !== undefined) { + getSearchServiceState().cancel(); + searchServiceMap.delete(id); } }, }; From a0f9d4f929b6c66e658a1ca88d9e0906587e3182 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Mon, 6 Sep 2021 15:32:30 +0200 Subject: [PATCH 29/37] [ML] Fix typo. --- .../common/search_strategies/latency_correlations/types.ts | 2 +- .../latency_correlations_search_service.ts | 6 +++--- .../latency_correlations_search_service_state.test.ts | 4 ++-- .../latency_correlations_search_service_state.ts | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts index 9710fdcd5dc37..1333518b4c129 100644 --- a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts @@ -43,7 +43,7 @@ export interface LatencyCorrelationSearchServiceProgress { started: number; loadedHistogramStepsize: number; loadedOverallHistogram: number; - loadedFieldCanditates: number; + loadedFieldCandidates: number; loadedFieldValuePairs: number; loadedHistograms: number; } diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service.ts index a5f5045642c47..b623f6c73f896 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service.ts @@ -95,7 +95,7 @@ export const latencyCorrelationsSearchServiceProvider: LatencyCorrelationsSearch state.setProgress({ loadedHistogramStepsize: 1, loadedOverallHistogram: 1, - loadedFieldCanditates: 1, + loadedFieldCandidates: 1, loadedFieldValuePairs: 1, loadedHistograms: 1, }); @@ -139,7 +139,7 @@ export const latencyCorrelationsSearchServiceProvider: LatencyCorrelationsSearch state.setProgress({ loadedHistogramStepsize: 1, loadedOverallHistogram: 1, - loadedFieldCanditates: 1, + loadedFieldCandidates: 1, loadedFieldValuePairs: 1, loadedHistograms: 1, }); @@ -172,7 +172,7 @@ export const latencyCorrelationsSearchServiceProvider: LatencyCorrelationsSearch addLogMessage(`Identified ${fieldCandidates.length} fieldCandidates.`); - state.setProgress({ loadedFieldCanditates: 1 }); + state.setProgress({ loadedFieldCandidates: 1 }); const fieldValuePairs = await fetchTransactionDurationFieldValuePairs( esClient, diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service_state.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service_state.test.ts index fa95378086c94..ce9014004f4b0 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service_state.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service_state.test.ts @@ -19,7 +19,7 @@ describe('search service', () => { expect(defaultState.isCancelled).toBe(false); expect(defaultState.isRunning).toBe(true); expect(defaultState.overallHistogram).toBe(undefined); - expect(defaultState.progress.loadedFieldCanditates).toBe(0); + expect(defaultState.progress.loadedFieldCandidates).toBe(0); expect(defaultState.progress.loadedFieldValuePairs).toBe(0); expect(defaultState.progress.loadedHistogramStepsize).toBe(0); expect(defaultState.progress.loadedHistograms).toBe(0); @@ -49,7 +49,7 @@ describe('search service', () => { expect(updatedState.overallHistogram).toEqual([ { key: 1392202800000, doc_count: 1234 }, ]); - expect(updatedState.progress.loadedFieldCanditates).toBe(0); + expect(updatedState.progress.loadedFieldCandidates).toBe(0); expect(updatedState.progress.loadedFieldValuePairs).toBe(0); expect(updatedState.progress.loadedHistogramStepsize).toBe(0); expect(updatedState.progress.loadedHistograms).toBe(0.5); diff --git a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service_state.ts b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service_state.ts index 0b92ca3ef8636..53f357ed1135f 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service_state.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/latency_correlations/latency_correlations_search_service_state.ts @@ -49,7 +49,7 @@ export const latencyCorrelationsSearchServiceStateProvider = () => { started: Date.now(), loadedHistogramStepsize: 0, loadedOverallHistogram: 0, - loadedFieldCanditates: 0, + loadedFieldCandidates: 0, loadedFieldValuePairs: 0, loadedHistograms: 0, }; @@ -57,7 +57,7 @@ export const latencyCorrelationsSearchServiceStateProvider = () => { return ( progress.loadedHistogramStepsize * 0.025 + progress.loadedOverallHistogram * 0.025 + - progress.loadedFieldCanditates * 0.025 + + progress.loadedFieldCandidates * 0.025 + progress.loadedFieldValuePairs * 0.025 + progress.loadedHistograms * 0.9 ); From 1709e11ec842387d7e1550221f8e5fa5dbe49381 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Mon, 6 Sep 2021 15:43:36 +0200 Subject: [PATCH 30/37] [ML] Simplify mock. --- .../server/lib/search_strategies/search_service_log.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/apm/server/lib/search_strategies/search_service_log.test.ts b/x-pack/plugins/apm/server/lib/search_strategies/search_service_log.test.ts index c21b31bb4462a..5b887f15a584e 100644 --- a/x-pack/plugins/apm/server/lib/search_strategies/search_service_log.test.ts +++ b/x-pack/plugins/apm/server/lib/search_strategies/search_service_log.test.ts @@ -15,7 +15,7 @@ describe('search service', () => { it('returns the current time as a string', () => { const mockDate = new Date(1392202800000); // @ts-ignore ignore the mockImplementation callback error - const spy = jest.spyOn(global, 'Date').mockImplementation(() => mockDate); + const spy = jest.spyOn(global, 'Date').mockReturnValue(mockDate); const timeString = currentTimeAsString(); @@ -31,7 +31,7 @@ describe('search service', () => { const mockDate = new Date(1392202800000); // @ts-ignore ignore the mockImplementation callback error - const spy = jest.spyOn(global, 'Date').mockImplementation(() => mockDate); + const spy = jest.spyOn(global, 'Date').mockReturnValue(mockDate); addLogMessage('the first message'); addLogMessage('the second message'); From c52158b89a06c73704113c4faa2d579bc9aea49a Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Mon, 6 Sep 2021 19:39:35 +0200 Subject: [PATCH 31/37] [ML] Refactor code that used type guards. --- .../failed_transactions_correlations/types.ts | 26 ------ .../latency_correlations/types.ts | 21 ----- .../failed_transactions_correlations.tsx | 92 +++++++++---------- .../app/correlations/latency_correlations.tsx | 75 +++++++-------- .../distribution/index.tsx | 16 ++-- .../apm/public/hooks/use_search_strategy.ts | 20 ++-- 6 files changed, 101 insertions(+), 149 deletions(-) diff --git a/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts index 02c76813eca83..dca07e52107e7 100644 --- a/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/failed_transactions_correlations/types.ts @@ -23,32 +23,6 @@ export interface FailedTransactionsCorrelation extends FieldValuePair { successPercentage: number; } -// Type guard for populated array of FailedTransactionsCorrelation -export const isFailedTransactionsCorrelations = ( - arg: unknown -): arg is FailedTransactionsCorrelation[] => { - return ( - Array.isArray(arg) && - arg.length > 0 && - arg.every( - (d) => - typeof d === 'object' && - d !== null && - Object.keys(d).length === 9 && - typeof d.doc_count === 'number' && - typeof d.bg_count === 'number' && - typeof d.score === 'number' && - (typeof d.pValue === 'number' || d.pValue === null) && - typeof d.fieldName === 'string' && - (typeof d.fieldValue === 'string' || - typeof d.fieldValue === 'number') && - typeof d.normalizedScore === 'number' && - typeof d.failurePercentage === 'number' && - typeof d.successPercentage === 'number' - ) - ); -}; - export type FailedTransactionsCorrelationsImpactThreshold = typeof FAILED_TRANSACTIONS_IMPACT_THRESHOLD[keyof typeof FAILED_TRANSACTIONS_IMPACT_THRESHOLD]; export type FailedTransactionsCorrelationsParams = SearchStrategyClientParams; diff --git a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts index 1333518b4c129..29f419da350ef 100644 --- a/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts +++ b/x-pack/plugins/apm/common/search_strategies/latency_correlations/types.ts @@ -18,27 +18,6 @@ export interface LatencyCorrelation extends FieldValuePair { ksTest: number; } -// Type guard for populated array of LatencyCorrelation -export function isLatencyCorrelations( - arg: unknown -): arg is LatencyCorrelation[] { - return ( - Array.isArray(arg) && - arg.length > 0 && - arg.every( - (d) => - typeof d === 'object' && - d !== null && - Object.keys(d).length === 5 && - typeof d.correlation === 'number' && - typeof d.fieldName === 'string' && - typeof d.fieldValue === 'string' && - Array.isArray(d.histogram) && - typeof d.ksTest === 'number' - ) - ); -} - export interface LatencyCorrelationSearchServiceProgress { started: number; loadedHistogramStepsize: number; diff --git a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx index 84bcadbca627a..20885906425a8 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx @@ -31,10 +31,7 @@ import { } from '../../../../../observability/public'; import { asPercent } from '../../../../common/utils/formatters'; -import { - isFailedTransactionsCorrelations, - FailedTransactionsCorrelation, -} from '../../../../common/search_strategies/failed_transactions_correlations/types'; +import { FailedTransactionsCorrelation } from '../../../../common/search_strategies/failed_transactions_correlations/types'; import { APM_SEARCH_STRATEGIES } from '../../../../common/search_strategies/constants'; import { FieldValuePair } from '../../../../common/search_strategies/types'; @@ -67,15 +64,10 @@ export function FailedTransactionsCorrelations({ const inspectEnabled = uiSettings.get(enableInspectEsQueries); - const { - state: { error, isRunning, loaded, total }, - data: { ccsWarning, log, failedTransactionsCorrelations }, - startFetch, - cancelFetch, - } = useSearchStrategy( + const { progress, response, startFetch, cancelFetch } = useSearchStrategy( APM_SEARCH_STRATEGIES.APM_FAILED_TRANSACTIONS_CORRELATIONS ); - const progress = loaded / total; + const progressNormalized = progress.loaded / progress.total; const [ selectedSignificantTerm, @@ -84,10 +76,11 @@ export function FailedTransactionsCorrelations({ const selectedTerm = useMemo(() => { if (selectedSignificantTerm) return selectedSignificantTerm; - return isFailedTransactionsCorrelations(failedTransactionsCorrelations) - ? failedTransactionsCorrelations[0] + return Array.isArray(response.failedTransactionsCorrelations) && + response.failedTransactionsCorrelations.length > 0 + ? response.failedTransactionsCorrelations[0] : undefined; - }, [selectedSignificantTerm, failedTransactionsCorrelations]); + }, [selectedSignificantTerm, response.failedTransactionsCorrelations]); const history = useHistory(); @@ -310,7 +303,7 @@ export function FailedTransactionsCorrelations({ }, [history, onFilter, trackApmEvent, inspectEnabled]); useEffect(() => { - if (isErrorMessage(error)) { + if (isErrorMessage(progress.error)) { notifications.toasts.addDanger({ title: i18n.translate( 'xpack.apm.correlations.failedTransactions.errorTitle', @@ -319,10 +312,10 @@ export function FailedTransactionsCorrelations({ 'An error occurred performing correlations on failed transactions', } ), - text: error.toString(), + text: progress.error.toString(), }); } - }, [error, notifications.toasts]); + }, [progress.error, notifications.toasts]); const [sortField, setSortField] = useState< keyof FailedTransactionsCorrelation @@ -337,11 +330,8 @@ export function FailedTransactionsCorrelations({ }, []); const { sorting, correlationTerms } = useMemo(() => { - if (!isFailedTransactionsCorrelations(failedTransactionsCorrelations)) { - return { correlationTerms: [], sorting: undefined }; - } const orderedTerms = orderBy( - failedTransactionsCorrelations, + response.failedTransactionsCorrelations, // The smaller the p value the higher the impact // So we want to sort by the normalized score here // which goes from 0 -> 1 @@ -357,7 +347,17 @@ export function FailedTransactionsCorrelations({ }, } as EuiTableSortingType, }; - }, [failedTransactionsCorrelations, sortField, sortDirection]); + }, [response.failedTransactionsCorrelations, sortField, sortDirection]); + + const showCorrelationsTable = + progress.isRunning || correlationTerms.length > 0; + + const showCorrelationsEmptyStatePrompt = + correlationTerms.length < 1 && + (progressNormalized === 1 || !progress.isRunning); + + const showSummaryBadge = + inspectEnabled && (progress.isRunning || correlationTerms.length > 0); return (
@@ -425,54 +425,54 @@ export function FailedTransactionsCorrelations({ - {ccsWarning && ( + {response.ccsWarning && ( <> )} - {inspectEnabled && - selectedTerm?.pValue != null && - (isRunning || correlationTerms.length > 0) ? ( - <> - - - {`${selectedTerm.fieldName}: ${selectedTerm.fieldValue}`} - , - <>{`p-value: ${selectedTerm.pValue.toPrecision(3)}`}, - ]} - /> - - ) : null} + {showSummaryBadge && + selectedTerm !== undefined && + selectedTerm.pValue !== null && ( + <> + + + {`${selectedTerm.fieldName}: ${selectedTerm.fieldValue}`} + , + <>{`p-value: ${selectedTerm.pValue.toPrecision(3)}`}, + ]} + /> + + )}
- {(isRunning || correlationTerms.length > 0) && ( + {showCorrelationsTable && ( columns={failedTransactionsCorrelationsColumns} significantTerms={correlationTerms} - status={isRunning ? FETCH_STATUS.LOADING : FETCH_STATUS.SUCCESS} + status={ + progress.isRunning ? FETCH_STATUS.LOADING : FETCH_STATUS.SUCCESS + } setSelectedSignificantTerm={setSelectedSignificantTerm} selectedTerm={selectedTerm} onTableChange={onTableChange} sorting={sorting} /> )} - {correlationTerms.length < 1 && (progress === 1 || !isRunning) && ( - - )} + {showCorrelationsEmptyStatePrompt && }
- {inspectEnabled && } + {inspectEnabled && }
); } diff --git a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx index fa463bdc92e24..5a3bf068b4391 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx @@ -34,10 +34,7 @@ import { DEFAULT_PERCENTILE_THRESHOLD, } from '../../../../common/search_strategies/constants'; import { FieldValuePair } from '../../../../common/search_strategies/types'; -import { - isLatencyCorrelations, - LatencyCorrelation, -} from '../../../../common/search_strategies/latency_correlations/types'; +import { LatencyCorrelation } from '../../../../common/search_strategies/latency_correlations/types'; import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; import { FETCH_STATUS } from '../../../hooks/use_fetcher'; @@ -62,28 +59,21 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { const displayLog = uiSettings.get(enableInspectEsQueries); - const { state, data, startFetch, cancelFetch } = useSearchStrategy( + const { progress, response, startFetch, cancelFetch } = useSearchStrategy( APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, { percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD, analyzeCorrelations: true, } ); - const { error, isRunning, loaded, total } = state; - const { - ccsWarning, - log, - latencyCorrelations, - percentileThresholdValue, - } = data; - const progress = loaded / total; + const progressNormalized = progress.loaded / progress.total; const { overallHistogram, hasData, status } = getOverallHistogram( - data, - isRunning + response, + progress.isRunning ); useEffect(() => { - if (isErrorMessage(error)) { + if (isErrorMessage(progress.error)) { notifications.toasts.addDanger({ title: i18n.translate( 'xpack.apm.correlations.latencyCorrelations.errorTitle', @@ -91,10 +81,10 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { defaultMessage: 'An error occurred fetching correlations', } ), - text: error.toString(), + text: progress.error.toString(), }); } - }, [error, notifications.toasts]); + }, [progress.error, notifications.toasts]); const [ selectedSignificantTerm, @@ -102,22 +92,24 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { ] = useState(null); const selectedHistogram = useMemo(() => { - let selected = isLatencyCorrelations(latencyCorrelations) - ? latencyCorrelations[0] - : undefined; + let selected = + Array.isArray(response.latencyCorrelations) && + response.latencyCorrelations.length > 0 + ? response.latencyCorrelations[0] + : undefined; if ( - isLatencyCorrelations(latencyCorrelations) && + Array.isArray(response.latencyCorrelations) && selectedSignificantTerm !== null ) { - selected = latencyCorrelations.find( + selected = response.latencyCorrelations.find( (h) => h.fieldName === selectedSignificantTerm.fieldName && h.fieldValue === selectedSignificantTerm.fieldValue ); } return selected; - }, [latencyCorrelations, selectedSignificantTerm]); + }, [response.latencyCorrelations, selectedSignificantTerm]); const history = useHistory(); const trackApmEvent = useUiTracker({ app: 'apm' }); @@ -246,10 +238,14 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { }, []); const { histogramTerms, sorting } = useMemo(() => { - if (!isLatencyCorrelations(latencyCorrelations)) { + if (!response.latencyCorrelations) { return { histogramTerms: [], sorting: undefined }; } - const orderedTerms = orderBy(latencyCorrelations, sortField, sortDirection); + const orderedTerms = orderBy( + response.latencyCorrelations, + sortField, + sortDirection + ); return { histogramTerms: orderedTerms, @@ -260,7 +256,12 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { }, } as EuiTableSortingType, }; - }, [latencyCorrelations, sortField, sortDirection]); + }, [response.latencyCorrelations, sortField, sortDirection]); + + const showCorrelationsTable = progress.isRunning || histogramTerms.length > 0; + const showCorrelationsEmptyStatePrompt = + histogramTerms.length < 1 && + (progressNormalized === 1 || !progress.isRunning); return (
@@ -286,7 +287,7 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { void }) { - {ccsWarning && ( + {response.ccsWarning && ( <> @@ -325,11 +326,13 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) {
- {(isRunning || histogramTerms.length > 0) && ( + {showCorrelationsTable && ( columns={mlCorrelationColumns} significantTerms={histogramTerms} - status={isRunning ? FETCH_STATUS.LOADING : FETCH_STATUS.SUCCESS} + status={ + progress.isRunning ? FETCH_STATUS.LOADING : FETCH_STATUS.SUCCESS + } setSelectedSignificantTerm={setSelectedSignificantTerm} selectedTerm={ selectedHistogram !== undefined @@ -343,11 +346,9 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { sorting={sorting} /> )} - {histogramTerms.length < 1 && (progress === 1 || !isRunning) && ( - - )} + {showCorrelationsEmptyStatePrompt && }
- {displayLog && } + {displayLog && }
); } diff --git a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx index a35367c93cf73..acd8c5f4d57d3 100644 --- a/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx +++ b/x-pack/plugins/apm/public/components/app/transaction_details/distribution/index.tsx @@ -96,22 +96,20 @@ export function TransactionDistribution({ } ); - const { state, data } = useSearchStrategy( + const { progress, response } = useSearchStrategy( APM_SEARCH_STRATEGIES.APM_LATENCY_CORRELATIONS, { percentileThreshold: DEFAULT_PERCENTILE_THRESHOLD, analyzeCorrelations: false, } ); - const { error, isRunning } = state; - const { percentileThresholdValue } = data; const { overallHistogram, hasData, status } = getOverallHistogram( - data, - isRunning + response, + progress.isRunning ); useEffect(() => { - if (isErrorMessage(error)) { + if (isErrorMessage(progress.error)) { notifications.toasts.addDanger({ title: i18n.translate( 'xpack.apm.transactionDetails.distribution.errorTitle', @@ -119,10 +117,10 @@ export function TransactionDistribution({ defaultMessage: 'An error occurred fetching the distribution', } ), - text: error.toString(), + text: progress.error.toString(), }); } - }, [error, notifications.toasts]); + }, [progress.error, notifications.toasts]); const trackApmEvent = useUiTracker({ app: 'apm' }); @@ -201,7 +199,7 @@ export function TransactionDistribution({ ({ +const getInitialProgress = (): SearchStrategyProgress => ({ isRunning: false, loaded: 0, total: 100, @@ -59,7 +59,7 @@ const getReducer = () => (prev: T, update: Partial): T => ({ }); interface SearchStrategyReturnBase { - state: SearchStrategyFetcherState; + progress: SearchStrategyProgress; startFetch: () => void; cancelFetch: () => void; } @@ -72,14 +72,14 @@ export function useSearchStrategy( analyzeCorrelations: boolean; } ): { - data: LatencyCorrelationsRawResponse; + response: LatencyCorrelationsRawResponse; } & SearchStrategyReturnBase; // Function overload for Failed Transactions Correlations export function useSearchStrategy( searchStrategyName: typeof APM_SEARCH_STRATEGIES.APM_FAILED_TRANSACTIONS_CORRELATIONS ): { - data: FailedTransactionsCorrelationsRawResponse; + response: FailedTransactionsCorrelationsRawResponse; } & SearchStrategyReturnBase; export function useSearchStrategy< @@ -104,8 +104,8 @@ export function useSearchStrategy< ); const [fetchState, setFetchState] = useReducer( - getReducer(), - getInitialFetchState() + getReducer(), + getInitialProgress() ); const abortCtrl = useRef(new AbortController()); @@ -117,7 +117,7 @@ export function useSearchStrategy< abortCtrl.current.abort(); abortCtrl.current = new AbortController(); setFetchState({ - ...getInitialFetchState(), + ...getInitialProgress(), error: undefined, }); @@ -200,8 +200,8 @@ export function useSearchStrategy< }, [startFetch, cancelFetch]); return { - state: fetchState, - data: rawResponse, + progress: fetchState, + response: rawResponse, startFetch, cancelFetch, }; From 11e3f28acd3ec5ff8fdfbf59732f0c837ada7796 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Tue, 7 Sep 2021 10:45:57 +0200 Subject: [PATCH 32/37] [ML] Comment about feature availability. --- .../app/correlations/failed_transactions_correlations.tsx | 1 + .../public/components/app/correlations/latency_correlations.tsx | 1 + 2 files changed, 2 insertions(+) diff --git a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx index 20885906425a8..cca15f89ddb00 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx @@ -434,6 +434,7 @@ export function FailedTransactionsCorrelations({ {response.ccsWarning && ( <> + {/* Failed transactions correlations uses ES aggs that are available since 7.15 */} )} diff --git a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx index 5a3bf068b4391..ae180e52b4339 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx @@ -319,6 +319,7 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { {response.ccsWarning && ( <> + {/* Latency correlations uses ES aggs that are available since 7.14 */} )} From 3e7d6110e9618753660b653bbaa969f60ef54e5a Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Tue, 7 Sep 2021 11:28:23 +0200 Subject: [PATCH 33/37] [ML] Simplify check. --- .../failed_transactions_correlations.tsx | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx index cca15f89ddb00..3238a9cbef232 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx @@ -439,21 +439,19 @@ export function FailedTransactionsCorrelations({ )} - {showSummaryBadge && - selectedTerm !== undefined && - selectedTerm.pValue !== null && ( - <> - - - {`${selectedTerm.fieldName}: ${selectedTerm.fieldValue}`} - , - <>{`p-value: ${selectedTerm.pValue.toPrecision(3)}`}, - ]} - /> - - )} + {showSummaryBadge && selectedTerm?.pValue && ( + <> + + + {`${selectedTerm.fieldName}: ${selectedTerm.fieldValue}`} + , + <>{`p-value: ${selectedTerm.pValue.toPrecision(3)}`}, + ]} + /> + + )} From 6af23f394202095e81bd5955aa4ff3d5c7682ad9 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Tue, 7 Sep 2021 11:41:09 +0200 Subject: [PATCH 34/37] [ML] Simplify selectedHistogram. --- .../app/correlations/latency_correlations.tsx | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx index ae180e52b4339..01b05d3290e2d 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx @@ -91,25 +91,15 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { setSelectedSignificantTerm, ] = useState(null); - const selectedHistogram = useMemo(() => { - let selected = - Array.isArray(response.latencyCorrelations) && - response.latencyCorrelations.length > 0 - ? response.latencyCorrelations[0] - : undefined; - - if ( - Array.isArray(response.latencyCorrelations) && - selectedSignificantTerm !== null - ) { - selected = response.latencyCorrelations.find( + const selectedHistogram = useMemo( + () => + response.latencyCorrelations?.find( (h) => - h.fieldName === selectedSignificantTerm.fieldName && - h.fieldValue === selectedSignificantTerm.fieldValue - ); - } - return selected; - }, [response.latencyCorrelations, selectedSignificantTerm]); + h.fieldName === selectedSignificantTerm?.fieldName && + h.fieldValue === selectedSignificantTerm?.fieldValue + ) ?? response.latencyCorrelations?.[0], + [response.latencyCorrelations, selectedSignificantTerm] + ); const history = useHistory(); const trackApmEvent = useUiTracker({ app: 'apm' }); From 787d9d77cb603fabf994aada4c12ee15170d4bc1 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Tue, 7 Sep 2021 11:48:05 +0200 Subject: [PATCH 35/37] [ML] Improve column type safety. --- .../failed_transactions_correlations.tsx | 18 ++++++++---------- .../app/correlations/latency_correlations.tsx | 6 ++---- ...ed_transactions_correlation_impact_label.ts | 11 +++++++++-- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx index 3238a9cbef232..5486b1e290b42 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx @@ -33,7 +33,6 @@ import { import { asPercent } from '../../../../common/utils/formatters'; import { FailedTransactionsCorrelation } from '../../../../common/search_strategies/failed_transactions_correlations/types'; import { APM_SEARCH_STRATEGIES } from '../../../../common/search_strategies/constants'; -import { FieldValuePair } from '../../../../common/search_strategies/types'; import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; import { FETCH_STATUS } from '../../../hooks/use_fetcher'; @@ -120,7 +119,7 @@ export function FailedTransactionsCorrelations({ ), - render: (failurePercentage: number) => + render: (_, { failurePercentage }) => asPercent(failurePercentage, 1), sortable: true, }, @@ -154,7 +153,7 @@ export function FailedTransactionsCorrelations({ ), - render: (successPercentage: number) => + render: (_, { successPercentage }) => asPercent(successPercentage, 1), sortable: true, }, @@ -174,7 +173,7 @@ export function FailedTransactionsCorrelations({ )} ), - render: (normalizedScore: number) => { + render: (_, { normalizedScore }) => { return ( <> @@ -196,7 +195,7 @@ export function FailedTransactionsCorrelations({ )} ), - render: (pValue: number) => { + render: (_, { pValue }) => { const label = getFailedTransactionsCorrelationImpactLabel(pValue); return label ? ( {label.impact} @@ -218,8 +217,7 @@ export function FailedTransactionsCorrelations({ 'xpack.apm.correlations.failedTransactions.correlationsTable.fieldValueLabel', { defaultMessage: 'Field value' } ), - render: (fieldValue: FieldValuePair['fieldValue']) => - String(fieldValue).slice(0, 50), + render: (_, { fieldValue }) => String(fieldValue).slice(0, 50), sortable: true, }, ...percentageColumns, @@ -273,13 +271,13 @@ export function FailedTransactionsCorrelations({ 'xpack.apm.correlations.correlationsTable.actionsLabel', { defaultMessage: 'Filter' } ), - render: (_: unknown, term: FailedTransactionsCorrelation) => { + render: (_, { fieldName, fieldValue }) => { return ( <> @@ -289,7 +287,7 @@ export function FailedTransactionsCorrelations({ diff --git a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx index 01b05d3290e2d..6d3e56b31fff2 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx @@ -33,7 +33,6 @@ import { APM_SEARCH_STRATEGIES, DEFAULT_PERCENTILE_THRESHOLD, } from '../../../../common/search_strategies/constants'; -import { FieldValuePair } from '../../../../common/search_strategies/types'; import { LatencyCorrelation } from '../../../../common/search_strategies/latency_correlations/types'; import { useApmPluginContext } from '../../../context/apm_plugin/use_apm_plugin_context'; @@ -137,7 +136,7 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { ), - render: (correlation: number) => { + render: (_, { correlation }) => { return
{asPreciseDecimal(correlation, 2)}
; }, sortable: true, @@ -156,8 +155,7 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { 'xpack.apm.correlations.latencyCorrelations.correlationsTable.fieldValueLabel', { defaultMessage: 'Field value' } ), - render: (fieldValue: FieldValuePair['fieldValue']) => - String(fieldValue).slice(0, 50), + render: (_, { fieldValue }) => String(fieldValue).slice(0, 50), sortable: true, }, { diff --git a/x-pack/plugins/apm/public/components/app/correlations/utils/get_failed_transactions_correlation_impact_label.ts b/x-pack/plugins/apm/public/components/app/correlations/utils/get_failed_transactions_correlation_impact_label.ts index 936562cf76b2a..cbfaee88ff6f4 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/utils/get_failed_transactions_correlation_impact_label.ts +++ b/x-pack/plugins/apm/public/components/app/correlations/utils/get_failed_transactions_correlation_impact_label.ts @@ -5,15 +5,22 @@ * 2.0. */ -import { FailedTransactionsCorrelationsImpactThreshold } from '../../../../../common/search_strategies/failed_transactions_correlations/types'; +import { + FailedTransactionsCorrelation, + FailedTransactionsCorrelationsImpactThreshold, +} from '../../../../../common/search_strategies/failed_transactions_correlations/types'; import { FAILED_TRANSACTIONS_IMPACT_THRESHOLD } from '../../../../../common/search_strategies/failed_transactions_correlations/constants'; export function getFailedTransactionsCorrelationImpactLabel( - pValue: number + pValue: FailedTransactionsCorrelation['pValue'] ): { impact: FailedTransactionsCorrelationsImpactThreshold; color: string; } | null { + if (pValue === null) { + return null; + } + // The lower the p value, the higher the impact if (pValue >= 0 && pValue < 1e-6) return { From b66541336af9a60f5d760b6a94040c403d55db99 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Tue, 7 Sep 2021 14:27:05 +0200 Subject: [PATCH 36/37] [ML] Simplify selectedTerm. --- .../correlations/failed_transactions_correlations.tsx | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx index 5486b1e290b42..12b6d5d9e024c 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx @@ -73,13 +73,8 @@ export function FailedTransactionsCorrelations({ setSelectedSignificantTerm, ] = useState(null); - const selectedTerm = useMemo(() => { - if (selectedSignificantTerm) return selectedSignificantTerm; - return Array.isArray(response.failedTransactionsCorrelations) && - response.failedTransactionsCorrelations.length > 0 - ? response.failedTransactionsCorrelations[0] - : undefined; - }, [selectedSignificantTerm, response.failedTransactionsCorrelations]); + const selectedTerm = + selectedSignificantTerm ?? response.failedTransactionsCorrelations?.[0]; const history = useHistory(); From 6076fbf213ff467f6b888423a06b89daf0a9cf3d Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Tue, 7 Sep 2021 14:53:35 +0200 Subject: [PATCH 37/37] [ML] Simplify sorting. --- .../failed_transactions_correlations.tsx | 35 ++++++++---------- .../app/correlations/latency_correlations.tsx | 37 +++++-------------- 2 files changed, 25 insertions(+), 47 deletions(-) diff --git a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx index 12b6d5d9e024c..fc1d9a3324b24 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/failed_transactions_correlations.tsx @@ -322,25 +322,22 @@ export function FailedTransactionsCorrelations({ setSortDirection(currentSortDirection); }, []); - const { sorting, correlationTerms } = useMemo(() => { - const orderedTerms = orderBy( - response.failedTransactionsCorrelations, - // The smaller the p value the higher the impact - // So we want to sort by the normalized score here - // which goes from 0 -> 1 - sortField === 'pValue' ? 'normalizedScore' : sortField, - sortDirection - ); - return { - correlationTerms: orderedTerms, - sorting: { - sort: { - field: sortField, - direction: sortDirection, - }, - } as EuiTableSortingType, - }; - }, [response.failedTransactionsCorrelations, sortField, sortDirection]); + const sorting: EuiTableSortingType = { + sort: { field: sortField, direction: sortDirection }, + }; + + const correlationTerms = useMemo( + () => + orderBy( + response.failedTransactionsCorrelations, + // The smaller the p value the higher the impact + // So we want to sort by the normalized score here + // which goes from 0 -> 1 + sortField === 'pValue' ? 'normalizedScore' : sortField, + sortDirection + ), + [response.failedTransactionsCorrelations, sortField, sortDirection] + ); const showCorrelationsTable = progress.isRunning || correlationTerms.length > 0; diff --git a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx index 6d3e56b31fff2..1a769adb621df 100644 --- a/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx +++ b/x-pack/plugins/apm/public/components/app/correlations/latency_correlations.tsx @@ -225,26 +225,14 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { setSortDirection(currentSortDirection); }, []); - const { histogramTerms, sorting } = useMemo(() => { - if (!response.latencyCorrelations) { - return { histogramTerms: [], sorting: undefined }; - } - const orderedTerms = orderBy( - response.latencyCorrelations, - sortField, - sortDirection - ); - - return { - histogramTerms: orderedTerms, - sorting: { - sort: { - field: sortField, - direction: sortDirection, - }, - } as EuiTableSortingType, - }; - }, [response.latencyCorrelations, sortField, sortDirection]); + const sorting: EuiTableSortingType = { + sort: { field: sortField, direction: sortDirection }, + }; + + const histogramTerms = useMemo( + () => orderBy(response.latencyCorrelations ?? [], sortField, sortDirection), + [response.latencyCorrelations, sortField, sortDirection] + ); const showCorrelationsTable = progress.isRunning || histogramTerms.length > 0; const showCorrelationsEmptyStatePrompt = @@ -323,14 +311,7 @@ export function LatencyCorrelations({ onFilter }: { onFilter: () => void }) { progress.isRunning ? FETCH_STATUS.LOADING : FETCH_STATUS.SUCCESS } setSelectedSignificantTerm={setSelectedSignificantTerm} - selectedTerm={ - selectedHistogram !== undefined - ? { - fieldName: selectedHistogram.fieldName, - fieldValue: selectedHistogram.fieldValue, - } - : undefined - } + selectedTerm={selectedHistogram} onTableChange={onTableChange} sorting={sorting} />