Skip to content

Commit

Permalink
Migrate all work from #50730 to NP
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerry350 committed Dec 5, 2019
1 parent 7271327 commit 6373386
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import * as rt from 'io-ts';
import { InventoryMetricRT, ItemTypeRT } from '../inventory_models/types';
import { InfraWrappableRequest } from '../../server/lib/adapters/framework';
import { InfraTimerangeInputRT } from './snapshot_api';

const NodeDetailsDataPointRT = rt.intersection([
Expand Down Expand Up @@ -53,6 +52,4 @@ export const NodeDetailsRequestRT = rt.intersection([
// export type NodeDetailsRequest = InfraWrappableRequest<NodesArgs & SourceArgs>;

export type NodeDetailsRequest = rt.TypeOf<typeof NodeDetailsRequestRT>;
export type NodeDetailsWrappedRequest = InfraWrappableRequest<NodeDetailsRequest>;

export type NodeDetailsMetricDataResponse = rt.TypeOf<typeof NodeDetailsMetricDataResponseRT>;
2 changes: 0 additions & 2 deletions x-pack/legacy/plugins/infra/common/http_api/snapshot_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import * as rt from 'io-ts';
import { InfraWrappableRequest } from '../../server/lib/adapters/framework';
import { SnapshotMetricTypeRT, ItemTypeRT } from '../inventory_models/types';

export const SnapshotNodePathRT = rt.intersection([
Expand Down Expand Up @@ -64,6 +63,5 @@ export const SnapshotRequestRT = rt.intersection([
]);

export type SnapshotRequest = rt.TypeOf<typeof SnapshotRequestRT>;
export type SnapshotWrappedRequest = InfraWrappableRequest<SnapshotRequest>;
export type SnapshotNode = rt.TypeOf<typeof SnapshotNodeRT>;
export type SnapshotNodeResponse = rt.TypeOf<typeof SnapshotNodeResponseRT>;
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class KibanaMetricsAdapter implements InfraMetricsAdapter {

const validNode = await checkValidNode(search, indexPattern, nodeField, options.nodeIds.nodeId);
if (!validNode) {
throw Boom.notFound(
throw new Error(
i18n.translate('xpack.infra.kibanaMetrics.nodeDoesNotExistErrorMessage', {
defaultMessage: '{nodeId} does not exist.',
values: {
Expand Down Expand Up @@ -98,7 +98,7 @@ export class KibanaMetricsAdapter implements InfraMetricsAdapter {
) {
const createTSVBModel = get(metrics, ['tsvb', metricId]) as TSVBMetricModelCreator | undefined;
if (!createTSVBModel) {
throw Boom.badRequest(
throw new Error(
i18n.translate('xpack.infra.metrics.missingTSVBModelError', {
defaultMessage: 'The TSVB model for {metricId} does not exist for {nodeType}',
values: {
Expand Down Expand Up @@ -136,7 +136,7 @@ export class KibanaMetricsAdapter implements InfraMetricsAdapter {
}

if (model.id_type === 'cloud' && !options.nodeIds.cloudId) {
throw Boom.badRequest(
throw new Error(
i18n.translate('xpack.infra.kibanaMetrics.cloudIdMissingErrorMessage', {
defaultMessage:
'Model for {metricId} requires a cloudId, but none was given for {nodeId}.',
Expand Down
50 changes: 30 additions & 20 deletions x-pack/legacy/plugins/infra/server/routes/node_details/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import Boom from 'boom';
import { boomify } from 'boom';
import { schema } from '@kbn/config-schema';
import { pipe } from 'fp-ts/lib/pipeable';
import { fold } from 'fp-ts/lib/Either';
import { identity } from 'fp-ts/lib/function';
Expand All @@ -13,27 +13,34 @@ import { UsageCollector } from '../../usage/usage_collector';
import { InfraMetricsRequestOptions } from '../../lib/adapters/metrics';
import { InfraNodeType, InfraMetric } from '../../graphql/types';
import {
NodeDetailsWrappedRequest,
NodeDetailsRequestRT,
NodeDetailsMetricDataResponse,
NodeDetailsMetricDataResponseRT,
} from '../../../common/http_api/node_details_api';
import { throwErrors } from '../../../common/runtime_types';

const escapeHatch = schema.object({}, { allowUnknowns: true });

export const initNodeDetailsRoute = (libs: InfraBackendLibs) => {
const { framework } = libs;

framework.registerRoute<NodeDetailsWrappedRequest, Promise<NodeDetailsMetricDataResponse>>({
method: 'POST',
path: '/api/metrics/node_details',
handler: async req => {
const { nodeId, cloudId, nodeType, metrics, timerange, sourceId } = pipe(
NodeDetailsRequestRT.decode(req.payload),
fold(throwErrors(Boom.badRequest), identity)
);
framework.registerRoute(
{
method: 'post',
path: '/api/metrics/node_details',
validate: {
body: escapeHatch,
},
},
async (requestContext, request, response) => {
try {
const source = await libs.sources.getSourceConfiguration(req, sourceId);
const { nodeId, cloudId, nodeType, metrics, timerange, sourceId } = pipe(
NodeDetailsRequestRT.decode(request.body),
fold(throwErrors(Boom.badRequest), identity)
);
const source = await libs.sources.getSourceConfiguration(requestContext, sourceId);

UsageCollector.countNode(nodeType);

const options: InfraMetricsRequestOptions = {
nodeIds: {
nodeId,
Expand All @@ -44,13 +51,16 @@ export const initNodeDetailsRoute = (libs: InfraBackendLibs) => {
metrics: metrics as InfraMetric[],
timerange,
};

return {
metrics: await libs.metrics.getMetrics(req, options),
};
} catch (e) {
throw boomify(e);
return response.ok({
body: NodeDetailsMetricDataResponseRT.encode({
metrics: await libs.metrics.getMetrics(requestContext, options, request),
}),
});
} catch (error) {
return response.internalError({
body: error.message,
});
}
},
});
}
);
};
68 changes: 41 additions & 27 deletions x-pack/legacy/plugins/infra/server/routes/snapshot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import Boom from 'boom';
import { schema } from '@kbn/config-schema';
import { pipe } from 'fp-ts/lib/pipeable';
import { fold } from 'fp-ts/lib/Either';
import { identity } from 'fp-ts/lib/function';
Expand All @@ -12,37 +13,50 @@ import { InfraSnapshotRequestOptions } from '../../lib/snapshot';
import { UsageCollector } from '../../usage/usage_collector';
import { parseFilterQuery } from '../../utils/serialized_query';
import { InfraNodeType, InfraSnapshotMetricInput } from '../../../public/graphql/types';
import {
SnapshotRequestRT,
SnapshotWrappedRequest,
SnapshotNodeResponse,
} from '../../../common/http_api/snapshot_api';
import { SnapshotRequestRT, SnapshotNodeResponseRT } from '../../../common/http_api/snapshot_api';
import { throwErrors } from '../../../common/runtime_types';

const escapeHatch = schema.object({}, { allowUnknowns: true });

export const initSnapshotRoute = (libs: InfraBackendLibs) => {
const { framework } = libs;

framework.registerRoute<SnapshotWrappedRequest, Promise<SnapshotNodeResponse>>({
method: 'POST',
path: '/api/metrics/snapshot',
handler: async req => {
const { filterQuery, nodeType, groupBy, sourceId, metric, timerange } = pipe(
SnapshotRequestRT.decode(req.payload),
fold(throwErrors(Boom.badRequest), identity)
);
const source = await libs.sources.getSourceConfiguration(req, sourceId);
UsageCollector.countNode(nodeType);
const options: InfraSnapshotRequestOptions = {
filterQuery: parseFilterQuery(filterQuery),
// TODO: Use common infra metric and replace graphql type
nodeType: nodeType as InfraNodeType,
groupBy,
sourceConfiguration: source.configuration,
// TODO: Use common infra metric and replace graphql type
metric: metric as InfraSnapshotMetricInput,
timerange,
};
return { nodes: await libs.snapshot.getNodes(req, options) };
framework.registerRoute(
{
method: 'post',
path: '/api/metrics/snapshot',
validate: {
body: escapeHatch,
},
},
});
async (requestContext, request, response) => {
try {
const { filterQuery, nodeType, groupBy, sourceId, metric, timerange } = pipe(
SnapshotRequestRT.decode(request.body),
fold(throwErrors(Boom.badRequest), identity)
);
const source = await libs.sources.getSourceConfiguration(requestContext, sourceId);
UsageCollector.countNode(nodeType);
const options: InfraSnapshotRequestOptions = {
filterQuery: parseFilterQuery(filterQuery),
// TODO: Use common infra metric and replace graphql type
nodeType: nodeType as InfraNodeType,
groupBy,
sourceConfiguration: source.configuration,
// TODO: Use common infra metric and replace graphql type
metric: metric as InfraSnapshotMetricInput,
timerange,
};
return response.ok({
body: SnapshotNodeResponseRT.encode({
nodes: await libs.snapshot.getNodes(requestContext, options),
}),
});
} catch (error) {
return response.internalError({
body: error.message,
});
}
}
);
};

0 comments on commit 6373386

Please sign in to comment.