From b9413b21f25861a53c3fa67b1bf4362241275593 Mon Sep 17 00:00:00 2001 From: Jason Rhodes Date: Wed, 23 Oct 2019 13:37:15 -0400 Subject: [PATCH] WIP continues --- .../infra/server/routes/ip_to_hostname.ts | 8 +-- .../server/routes/metrics_explorer/index.ts | 58 ++++++++++--------- .../server/routes/metrics_explorer/types.ts | 4 +- 3 files changed, 36 insertions(+), 34 deletions(-) diff --git a/x-pack/legacy/plugins/infra/server/routes/ip_to_hostname.ts b/x-pack/legacy/plugins/infra/server/routes/ip_to_hostname.ts index 8261be2f2ee200..453449a67789f9 100644 --- a/x-pack/legacy/plugins/infra/server/routes/ip_to_hostname.ts +++ b/x-pack/legacy/plugins/infra/server/routes/ip_to_hostname.ts @@ -33,19 +33,19 @@ export const initIpToHostName = ({ framework }: InfraBackendLibs) => { body: ipToHostSchema, }, }, - async (context, req, response) => { + async (context, request, response) => { try { const params = { - index: req.body.index_pattern, + index: request.body.index_pattern, body: { size: 1, query: { - match: { 'host.ip': req.body.ip }, + match: { 'host.ip': request.body.ip }, }, _source: ['host.name'], }, }; - const { hits } = await callWithRequest(req, 'search', params); + const { hits } = await callWithRequest(request, 'search', params); if (hits.total.value === 0) { return response.notFound({ body: { message: 'Host with matching IP address not found.' }, diff --git a/x-pack/legacy/plugins/infra/server/routes/metrics_explorer/index.ts b/x-pack/legacy/plugins/infra/server/routes/metrics_explorer/index.ts index f3195365c2a84b..a7b379eef6d4a6 100644 --- a/x-pack/legacy/plugins/infra/server/routes/metrics_explorer/index.ts +++ b/x-pack/legacy/plugins/infra/server/routes/metrics_explorer/index.ts @@ -5,41 +5,45 @@ */ import { boomify } from 'boom'; +import { schema } from '@kbn/config-schema'; import { InfraBackendLibs } from '../../lib/infra_types'; import { getGroupings } from './lib/get_groupings'; import { populateSeriesWithTSVBData } from './lib/populate_series_with_tsvb_data'; import { metricsExplorerSchema } from './schema'; -import { MetricsExplorerResponse, MetricsExplorerWrappedRequest } from './types'; +import { MetricsExplorerResponse, MetricsExplorerRequestBody } from './types'; + +// NP_TODO: need to replace all of this with real types +const escapeHatch = schema.object({}, { allowUnknowns: true }); +type EscapeHatch = typeof escapeHatch; export const initMetricExplorerRoute = (libs: InfraBackendLibs) => { const { framework } = libs; const { callWithRequest } = framework; - // framework.registerRoute>({ - // method: 'post', - // path: '/api/infra/metrics_explorer', - // options: { - // validate: { - // payload: metricsExplorerSchema, - // }, - // }, - // handler: async req => { - // try { - // const search = (searchOptions: object) => - // callWithRequest<{}, Aggregation>(req, 'search', searchOptions); - // const options = req.payload; - // // First we get the groupings from a composite aggregation - // const response = await getGroupings(search, options); + framework.router.post( + { + path: '/api/infra/metrics_explorer', + validate: { + body: escapeHatch, + }, + }, + async (context, request, response) => { + try { + const search = (searchOptions: object) => + callWithRequest<{}, Aggregation>(request, 'search', searchOptions); + const options = request.payload; + // First we get the groupings from a composite aggregation + const groupings = await getGroupings(search, options); - // // Then we take the results and fill in the data from TSVB with the - // // user's custom metrics - // const seriesWithMetrics = await Promise.all( - // response.series.map(populateSeriesWithTSVBData(req, options, framework)) - // ); - // return { ...response, series: seriesWithMetrics }; - // } catch (error) { - // throw boomify(error); - // } - // }, - // }); + // Then we take the results and fill in the data from TSVB with the + // user's custom metrics + const seriesWithMetrics = await Promise.all( + groupings.series.map(populateSeriesWithTSVBData(request, options, framework)) + ); + response.ok({ body: { ...response, series: seriesWithMetrics } }); + } catch (error) { + throw boomify(error); + } + } + ); }; diff --git a/x-pack/legacy/plugins/infra/server/routes/metrics_explorer/types.ts b/x-pack/legacy/plugins/infra/server/routes/metrics_explorer/types.ts index b29c41fcbff184..2c2256a0e00268 100644 --- a/x-pack/legacy/plugins/infra/server/routes/metrics_explorer/types.ts +++ b/x-pack/legacy/plugins/infra/server/routes/metrics_explorer/types.ts @@ -27,7 +27,7 @@ export interface MetricsExplorerMetric { field?: string | undefined; } -export interface MetricsExplorerRequest { +export interface MetricsExplorerRequestBody { timerange: InfraTimerange; indexPattern: string; metrics: MetricsExplorerMetric[]; @@ -37,8 +37,6 @@ export interface MetricsExplorerRequest { filterQuery?: string; } -export type MetricsExplorerWrappedRequest = InfraWrappableRequest; - export interface MetricsExplorerPageInfo { total: number; afterKey?: string | null;