Skip to content

Commit

Permalink
Code and types cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ogupte committed Jun 8, 2020
1 parent 08b94cf commit bd1ca55
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
41 changes: 24 additions & 17 deletions x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
import { intersection } from 'lodash';
import { leftJoin } from '../../../common/utils/left_join';
import { rangeFilter } from '../helpers/range_filter';
import { Job as AnomalyDetectionJob } from '../../../../ml/server';
import { PromiseReturnType } from '../../../typings/common';
import { IEnvOptions } from './get_service_map';
Expand Down Expand Up @@ -57,7 +56,6 @@ export async function getServiceAnomalies(
(apmJobCategory) => apmJobCategory !== undefined
) as ApmMlJobCategory[];
const apmJobIds = apmMlJobs.map((job) => job.job_id);
const rangeQuery = { range: rangeFilter(start, end, 'timestamp') };
const params = {
body: {
size: 0,
Expand All @@ -70,7 +68,11 @@ export async function getServiceAnomalies(
job_id: apmJobIds,
},
},
rangeQuery,
{
range: {
timestamp: { gte: start, lte: end, format: 'epoch_millis' },
},
},
],
},
},
Expand All @@ -91,12 +93,23 @@ export async function getServiceAnomalies(
},
};

const response = await ml.mlSystem.mlAnomalySearch(params);
const anomalyScores: Array<{
jobId: string;
anomalyScore: number;
timestamp: number;
}> = response.aggregations.jobs.buckets.map((jobBucket: any) => {
const response = (await ml.mlSystem.mlAnomalySearch(params)) as {
aggregations: {
jobs: {
buckets: Array<{
key: string;
top_score_hits: {
hits: {
hits: Array<{
_source: { anomaly_score: number; timestamp: number };
}>;
};
};
}>;
};
};
};
const anomalyScores = response.aggregations.jobs.buckets.map((jobBucket) => {
const jobId = jobBucket.key;
const bucketSource = jobBucket.top_score_hits.hits.hits?.[0]?._source;
return {
Expand Down Expand Up @@ -140,19 +153,13 @@ export async function getServiceAnomalies(
}
);

const anomalyModelValues = filterNonNullable(
const anomalyModelValues = (
await Promise.all(anomalyModelValuePromises)
);
).filter(<T>(value: T | undefined): value is T => value !== undefined);

return leftJoin(
apmMlJobCategories,
'jobId',
leftJoin(anomalyScores, 'jobId', anomalyModelValues)
);
}

function filterNonNullable<T>(array: T[]) {
return array.filter(
(element) => element !== undefined || element !== null
) as Array<NonNullable<T>>;
}
9 changes: 4 additions & 5 deletions x-pack/plugins/apm/server/lib/service_map/get_service_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,10 @@ export type ServicesResponse = PromiseReturnType<typeof getServicesData>;
export type ServiceMapAPIResponse = PromiseReturnType<typeof getServiceMap>;

export async function getServiceMap(options: IEnvOptions) {
const [connectionData, servicesData]: [
// explicit types to avoid TS "excessively deep" error
ConnectionsResponse,
ServicesResponse
] = await Promise.all([getConnectionData(options), getServicesData(options)]);
const [connectionData, servicesData] = await Promise.all([
getConnectionData(options),
getServicesData(options),
]);

// Derive all related service names from connection and service data
const allNodes = getAllNodes(servicesData, connectionData.connections);
Expand Down

0 comments on commit bd1ca55

Please sign in to comment.