diff --git a/x-pack/plugins/apm/server/lib/transactions/get_anomaly_data/index.ts b/x-pack/plugins/apm/server/lib/transactions/get_anomaly_data/index.ts index 9466a38e1bc0..27dd7c0f6970 100644 --- a/x-pack/plugins/apm/server/lib/transactions/get_anomaly_data/index.ts +++ b/x-pack/plugins/apm/server/lib/transactions/get_anomaly_data/index.ts @@ -12,29 +12,35 @@ import { anomalySeriesFetcher } from './fetcher'; import { getMlBucketSize } from './get_ml_bucket_size'; import { anomalySeriesTransform } from './transform'; import { getMLJobIds } from '../../service_map/get_service_anomalies'; +import { getLatencyTimeseries } from '../get_latency_charts'; +import { PromiseReturnType } from '../../../../../observability/typings/common'; export async function getAnomalySeries({ serviceName, transactionType, transactionName, - timeSeriesDates, + latencyTimeseries, setup, logger, }: { serviceName: string; transactionType: string | undefined; transactionName: string | undefined; - timeSeriesDates: number[]; + latencyTimeseries: PromiseReturnType< + typeof getLatencyTimeseries + >['latencyTimeseries']; setup: Setup & SetupTimeRange; logger: Logger; }) { - // don't fetch anomalies for transaction details page - if (transactionName) { - return; - } + const timeseriesDates = latencyTimeseries?.avg?.map(({ x }) => x); - // don't fetch anomalies without a type - if (!transactionType) { + /* + * don't fetch: + * - anomalies for transaction details page + * - anomalies without a type + * - timeseries is empty + */ + if (transactionName || !transactionType || !timeseriesDates?.length) { return; } @@ -94,7 +100,7 @@ export async function getAnomalySeries({ esResponse, mlBucketSize, bucketSize, - timeSeriesDates, + timeseriesDates, jobId ); } diff --git a/x-pack/plugins/apm/server/routes/transactions.ts b/x-pack/plugins/apm/server/routes/transactions.ts index 9666f7f62612..9b7a0981a4fe 100644 --- a/x-pack/plugins/apm/server/routes/transactions.ts +++ b/x-pack/plugins/apm/server/routes/transactions.ts @@ -154,13 +154,11 @@ export const transactionLatencyChatsRoute = createRoute({ overallAvgDuration, } = await getLatencyTimeseries(options); - const anomalyTimeseries = latencyTimeseries - ? await getAnomalySeries({ - ...options, - logger, - timeSeriesDates: latencyTimeseries.avg.map(({ x }) => x), - }) - : null; + const anomalyTimeseries = await getAnomalySeries({ + ...options, + logger, + latencyTimeseries, + }); return { latencyTimeseries, overallAvgDuration, anomalyTimeseries }; },