Skip to content

Commit

Permalink
type es agg response
Browse files Browse the repository at this point in the history
  • Loading branch information
neptunian committed Jan 2, 2024
1 parent d66dce4 commit d7fd655
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import {
} from '@kbn/io-ts-utils';
import * as rt from 'io-ts';

const AgentNameRT = rt.union([rt.string, rt.null]);

export const serviceAssetRT = rt.type({
'service.name': rt.string,
'agent.name': rt.union([rt.string, rt.undefined]),
'agent.name': AgentNameRT,
});

export type ServiceAsset = rt.TypeOf<typeof serviceAssetRT>;
Expand Down Expand Up @@ -45,6 +47,32 @@ export const GetServicesRequestQueryRT = rt.intersection([
}),
]);

export interface GetServicesResponse {
services: ServiceAsset[];
export interface ServicesAPIRequest {
filters: ServicesFilter;
from: string;
to?: string;
}

export const ServicesAPIQueryAggregationRT = rt.type({
services: rt.type({
buckets: rt.array(
rt.type({
key: rt.string,
latestAgent: rt.type({
top: rt.array(
rt.type({
sort: rt.array(rt.string),
metrics: rt.type({
'agent.name': AgentNameRT,
}),
})
),
}),
})
),
}),
});

export type ServicesAPIQueryAggregationAggregation = rt.TypeOf<
typeof ServicesAPIQueryAggregationRT
>;
1 change: 1 addition & 0 deletions x-pack/plugins/infra/common/http_api/host_details/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*/

export * from './process_list';
export * from './get_infra_services';
8 changes: 0 additions & 8 deletions x-pack/plugins/infra/common/http_api/services/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@
*/

import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types';
import { estypes } from '@elastic/elasticsearch';
import { ServiceAsset, GetServicesResponse } from '../../../../common/http_api/services';
import { GetServicesOptions } from './types';
import { APMDataAccessConfig } from '@kbn/apm-data-access-plugin/server';
import { ESSearchClient } from '../metrics/types';
import {
ServiceAsset,
ServicesAPIRequest,
ServicesAPIQueryAggregationAggregation,
} from '../../../common/http_api/host_details';

export const getServices = async (options: GetServicesOptions): Promise<GetServicesResponse> => {
const { transaction, error, metric } = options.apmIndices;
export const getServices = async (
client: ESSearchClient,
apmIndices: APMDataAccessConfig['indices'],
options: ServicesAPIRequest
) => {
const { transaction, error, metric } = apmIndices;
const filters: QueryDslQueryContainer[] = [];
filters.push({
bool: {
Expand All @@ -23,8 +31,7 @@ export const getServices = async (options: GetServicesOptions): Promise<GetServi
},
});

const dsl: estypes.SearchRequest = {
index: [transaction, error, metric],
const body = {
size: 0,
_source: false,
query: {
Expand Down Expand Up @@ -62,12 +69,16 @@ export const getServices = async (options: GetServicesOptions): Promise<GetServi
},
},
};
const esResponse = await options.searchClient.search(dsl);

const { buckets = [] } = (esResponse.aggregations?.services || {}) as any;
const services = buckets.reduce((acc: ServiceAsset[], bucket: any) => {
const result = await client<{}, ServicesAPIQueryAggregationAggregation>({
body,
index: [transaction, error, metric],
});

const { buckets: servicesListBuckets } = result.aggregations!.services;
const services = servicesListBuckets.reduce((acc: ServiceAsset[], bucket) => {
const serviceName = bucket.key;
const agentName = bucket.latestAgent.top[0]?.metrics['agent.name'];
const agentName = bucket.latestAgent.top[0].metrics['agent.name'];

if (!serviceName) {
return acc;
Expand Down
12 changes: 5 additions & 7 deletions x-pack/plugins/infra/server/routes/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import { createRouteValidationFunction } from '@kbn/io-ts-utils';
import {
GetServicesRequestQueryRT,
GetServicesRequestQuery,
} from '../../../common/http_api/services';
} from '../../../common/http_api/host_details';
import { InfraBackendLibs } from '../../lib/infra_types';
import { getServices } from './lib/get_services';
import { getServices } from '../../lib/host_details/get_services';
import { validateStringAssetFilters } from './lib/utils';
import { createSearchClient } from '../../lib/create_search_client';

export const initServicesRoute = (libs: InfraBackendLibs) => {
const validate = createRouteValidationFunction(GetServicesRequestQueryRT);
Expand Down Expand Up @@ -45,16 +46,13 @@ export const initServicesRoute = (libs: InfraBackendLibs) => {
if (!filters) {
throw Boom.badRequest('Invalid filters');
}
const coreContext = await requestContext.core;
const searchClient = coreContext.elasticsearch.client.asCurrentUser;
const client = createSearchClient(requestContext, framework, request);
const soClient = savedObjects.getScopedClient(request);
const apmIndices = await libs.getApmIndices(soClient);
const services = await getServices({
const services = await getServices(client, apmIndices, {
from,
to,
filters,
searchClient,
apmIndices,
});
return response.ok({
body: services,
Expand Down
18 changes: 0 additions & 18 deletions x-pack/plugins/infra/server/routes/services/lib/types.ts

This file was deleted.

2 changes: 1 addition & 1 deletion x-pack/plugins/infra/server/routes/services/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import { RouteValidationError, RouteValidationResultFactory } from '@kbn/core/server';
import { isLeft } from 'fp-ts/Either';
import { servicesFiltersRT, ServicesFilter } from '../../../../common/http_api/services';
import { servicesFiltersRT, ServicesFilter } from '../../../../common/http_api/host_details';

type ValidateStringAssetFiltersReturn = [{ error: RouteValidationError }] | [null, ServicesFilter];

Expand Down

0 comments on commit d7fd655

Please sign in to comment.