From 742a2a2d5144ab4f4eaa78e6ccf6e3909a51ff56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20St=C3=BCrmer?= Date: Thu, 28 Apr 2022 17:26:33 +0200 Subject: [PATCH] [Stack Monitoring] Convert elasticsearch routes to TypeScript (#130969) --- .../common/http_api/elasticsearch/index.ts | 15 ++++ .../elasticsearch/post_elasticsearch_ccr.ts | 30 ++++++++ .../post_elasticsearch_ccr_shard.ts | 32 ++++++++ .../post_elasticsearch_index_detail.ts | 32 ++++++++ .../post_elasticsearch_indices.ts | 41 +++++++++++ .../post_elasticsearch_ml_jobs.ts | 30 ++++++++ .../post_elasticsearch_node_detail.ts | 33 +++++++++ .../elasticsearch/post_elasticsearch_nodes.ts | 33 +++++++++ .../post_elasticsearch_overview.ts | 30 ++++++++ .../common/http_api/shared/index.ts | 6 +- .../common/http_api/shared/literal_value.ts | 20 +++++ .../common/http_api/shared/pagination.ts | 13 ++++ .../http_api/shared/query_string_boolean.ts | 27 +++++++ .../common/http_api/shared/sorting.ts | 18 +++++ .../common/http_api/shared/time_range.ts | 25 ++++++- .../nodes/get_nodes/get_paginated_nodes.ts | 24 +++--- .../nodes/get_nodes/sort_nodes.ts | 5 +- .../elasticsearch/shards/get_shard_stats.ts | 31 ++++---- .../server/lib/logs/detect_reason.ts | 10 ++- .../monitoring/server/lib/logs/get_logs.ts | 18 +---- x-pack/plugins/monitoring/server/plugin.ts | 2 +- .../server/routes/api/v1/apm/instance.ts | 7 +- .../server/routes/api/v1/apm/instances.ts | 7 +- .../server/routes/api/v1/apm/overview.ts | 7 +- .../server/routes/api/v1/beats/beat_detail.ts | 7 +- .../server/routes/api/v1/beats/beats.ts | 7 +- .../server/routes/api/v1/beats/overview.ts | 7 +- .../server/routes/api/v1/elasticsearch/ccr.ts | 53 +++++++------- .../routes/api/v1/elasticsearch/ccr_shard.ts | 60 +++++++-------- .../v1/elasticsearch/{index.js => index.ts} | 0 .../{index_detail.js => index_detail.ts} | 47 ++++++------ .../elasticsearch/{indices.js => indices.ts} | 46 ++++++------ ...x_detail.js => metric_set_index_detail.ts} | 7 +- ...de_detail.js => metric_set_node_detail.ts} | 7 +- ...set_overview.js => metric_set_overview.ts} | 4 +- .../elasticsearch/{ml_jobs.js => ml_jobs.ts} | 37 +++++----- .../{node_detail.js => node_detail.ts} | 73 ++++++++++--------- .../v1/elasticsearch/{nodes.js => nodes.ts} | 64 ++++++++-------- .../{overview.js => overview.ts} | 47 ++++++------ x-pack/plugins/monitoring/server/types.ts | 29 ++++---- 40 files changed, 696 insertions(+), 295 deletions(-) create mode 100644 x-pack/plugins/monitoring/common/http_api/elasticsearch/index.ts create mode 100644 x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_ccr.ts create mode 100644 x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_ccr_shard.ts create mode 100644 x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_index_detail.ts create mode 100644 x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_indices.ts create mode 100644 x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_ml_jobs.ts create mode 100644 x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_node_detail.ts create mode 100644 x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_nodes.ts create mode 100644 x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_overview.ts create mode 100644 x-pack/plugins/monitoring/common/http_api/shared/literal_value.ts create mode 100644 x-pack/plugins/monitoring/common/http_api/shared/pagination.ts create mode 100644 x-pack/plugins/monitoring/common/http_api/shared/query_string_boolean.ts create mode 100644 x-pack/plugins/monitoring/common/http_api/shared/sorting.ts rename x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/{index.js => index.ts} (100%) rename x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/{index_detail.js => index_detail.ts} (81%) rename x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/{indices.js => indices.ts} (59%) rename x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/{metric_set_index_detail.js => metric_set_index_detail.ts} (92%) rename x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/{metric_set_node_detail.js => metric_set_node_detail.ts} (93%) rename x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/{metric_set_overview.js => metric_set_overview.ts} (79%) rename x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/{ml_jobs.js => ml_jobs.ts} (62%) rename x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/{node_detail.js => node_detail.ts} (71%) rename x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/{nodes.js => nodes.ts} (66%) rename x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/{overview.js => overview.ts} (72%) diff --git a/x-pack/plugins/monitoring/common/http_api/elasticsearch/index.ts b/x-pack/plugins/monitoring/common/http_api/elasticsearch/index.ts new file mode 100644 index 0000000000000..2fa6180cc34a5 --- /dev/null +++ b/x-pack/plugins/monitoring/common/http_api/elasticsearch/index.ts @@ -0,0 +1,15 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export * from './post_elasticsearch_ccr'; +export * from './post_elasticsearch_ccr_shard'; +export * from './post_elasticsearch_index_detail'; +export * from './post_elasticsearch_indices'; +export * from './post_elasticsearch_ml_jobs'; +export * from './post_elasticsearch_node_detail'; +export * from './post_elasticsearch_nodes'; +export * from './post_elasticsearch_overview'; diff --git a/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_ccr.ts b/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_ccr.ts new file mode 100644 index 0000000000000..db74be0955c06 --- /dev/null +++ b/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_ccr.ts @@ -0,0 +1,30 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import * as rt from 'io-ts'; +import { clusterUuidRT, ccsRT, timeRangeRT } from '../shared'; + +export const postElasticsearchCcrRequestParamsRT = rt.type({ + clusterUuid: clusterUuidRT, +}); + +export const postElasticsearchCcrRequestPayloadRT = rt.intersection([ + rt.partial({ + ccs: ccsRT, + }), + rt.type({ + timeRange: timeRangeRT, + }), +]); + +export type PostElasticsearchCcrRequestPayload = rt.TypeOf< + typeof postElasticsearchCcrRequestPayloadRT +>; + +export const postElasticsearchCcrResponsePayloadRT = rt.type({ + // TODO: add payload entries +}); diff --git a/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_ccr_shard.ts b/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_ccr_shard.ts new file mode 100644 index 0000000000000..64f75658420f7 --- /dev/null +++ b/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_ccr_shard.ts @@ -0,0 +1,32 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import * as rt from 'io-ts'; +import { clusterUuidRT, ccsRT, timeRangeRT } from '../shared'; + +export const postElasticsearchCcrShardRequestParamsRT = rt.type({ + clusterUuid: clusterUuidRT, + index: rt.string, + shardId: rt.string, +}); + +export const postElasticsearchCcrShardRequestPayloadRT = rt.intersection([ + rt.partial({ + ccs: ccsRT, + }), + rt.type({ + timeRange: timeRangeRT, + }), +]); + +export type PostElasticsearchCcrShardRequestPayload = rt.TypeOf< + typeof postElasticsearchCcrShardRequestPayloadRT +>; + +export const postElasticsearchCcrShardResponsePayloadRT = rt.type({ + // TODO: add payload entries +}); diff --git a/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_index_detail.ts b/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_index_detail.ts new file mode 100644 index 0000000000000..b9bb491c0c387 --- /dev/null +++ b/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_index_detail.ts @@ -0,0 +1,32 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import * as rt from 'io-ts'; +import { ccsRT, clusterUuidRT, createLiteralValueFromUndefinedRT, timeRangeRT } from '../shared'; + +export const postElasticsearchIndexDetailRequestParamsRT = rt.type({ + clusterUuid: clusterUuidRT, + id: rt.string, +}); + +export const postElasticsearchIndexDetailRequestPayloadRT = rt.intersection([ + rt.partial({ + ccs: ccsRT, + }), + rt.type({ + is_advanced: rt.union([rt.boolean, createLiteralValueFromUndefinedRT(false)]), + timeRange: timeRangeRT, + }), +]); + +export type PostElasticsearchIndexDetailRequestPayload = rt.TypeOf< + typeof postElasticsearchIndexDetailRequestPayloadRT +>; + +export const postElasticsearchIndexDetailResponsePayloadRT = rt.type({ + // TODO: add payload entries +}); diff --git a/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_indices.ts b/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_indices.ts new file mode 100644 index 0000000000000..57a4b953f15dc --- /dev/null +++ b/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_indices.ts @@ -0,0 +1,41 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import * as rt from 'io-ts'; +import { + booleanFromStringRT, + ccsRT, + clusterUuidRT, + createLiteralValueFromUndefinedRT, + timeRangeRT, +} from '../shared'; + +export const postElasticsearchIndicesRequestParamsRT = rt.type({ + clusterUuid: clusterUuidRT, +}); + +export const postElasticsearchIndicesRequestPayloadRT = rt.intersection([ + rt.partial({ + ccs: ccsRT, + }), + rt.type({ + is_advanced: rt.union([rt.boolean, createLiteralValueFromUndefinedRT(false)]), + timeRange: timeRangeRT, + }), +]); + +export type PostElasticsearchIndicesRequestPayload = rt.TypeOf< + typeof postElasticsearchIndicesRequestPayloadRT +>; + +export const postElasticsearchIndicesRequestQueryRT = rt.type({ + show_system_indices: booleanFromStringRT, +}); + +export const postElasticsearchIndicesResponsePayloadRT = rt.type({ + // TODO: add payload entries +}); diff --git a/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_ml_jobs.ts b/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_ml_jobs.ts new file mode 100644 index 0000000000000..3cbe83fa5d27a --- /dev/null +++ b/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_ml_jobs.ts @@ -0,0 +1,30 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import * as rt from 'io-ts'; +import { clusterUuidRT, ccsRT, timeRangeRT } from '../shared'; + +export const postElasticsearchMlJobsRequestParamsRT = rt.type({ + clusterUuid: clusterUuidRT, +}); + +export const postElasticsearchMlJobsRequestPayloadRT = rt.intersection([ + rt.partial({ + ccs: ccsRT, + }), + rt.type({ + timeRange: timeRangeRT, + }), +]); + +export type PostElasticsearchMlJobsRequestPayload = rt.TypeOf< + typeof postElasticsearchMlJobsRequestPayloadRT +>; + +export const postElasticsearchMlJobsResponsePayloadRT = rt.type({ + // TODO: add payload entries +}); diff --git a/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_node_detail.ts b/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_node_detail.ts new file mode 100644 index 0000000000000..b29d9fb7b8daf --- /dev/null +++ b/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_node_detail.ts @@ -0,0 +1,33 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import * as rt from 'io-ts'; +import { ccsRT, clusterUuidRT, createLiteralValueFromUndefinedRT, timeRangeRT } from '../shared'; + +export const postElasticsearchNodeDetailRequestParamsRT = rt.type({ + clusterUuid: clusterUuidRT, + nodeUuid: rt.string, +}); + +export const postElasticsearchNodeDetailRequestPayloadRT = rt.intersection([ + rt.partial({ + ccs: ccsRT, + showSystemIndices: rt.boolean, // show/hide system indices in shard allocation table + }), + rt.type({ + is_advanced: rt.union([rt.boolean, createLiteralValueFromUndefinedRT(false)]), + timeRange: timeRangeRT, + }), +]); + +export type PostElasticsearchNodeDetailRequestPayload = rt.TypeOf< + typeof postElasticsearchNodeDetailRequestPayloadRT +>; + +export const postElasticsearchNodeDetailResponsePayloadRT = rt.type({ + // TODO: add payload entries +}); diff --git a/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_nodes.ts b/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_nodes.ts new file mode 100644 index 0000000000000..bba6525d9a2c8 --- /dev/null +++ b/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_nodes.ts @@ -0,0 +1,33 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import * as rt from 'io-ts'; +import { clusterUuidRT, ccsRT, timeRangeRT, paginationRT, sortingRT } from '../shared'; + +export const postElasticsearchNodesRequestParamsRT = rt.type({ + clusterUuid: clusterUuidRT, +}); + +export const postElasticsearchNodesRequestPayloadRT = rt.intersection([ + rt.partial({ + ccs: ccsRT, + queryText: rt.string, + sort: sortingRT, + }), + rt.type({ + timeRange: timeRangeRT, + pagination: paginationRT, + }), +]); + +export type PostElasticsearchNodesRequestPayload = rt.TypeOf< + typeof postElasticsearchNodesRequestPayloadRT +>; + +export const postElasticsearchNodesResponsePayloadRT = rt.type({ + // TODO: add payload entries +}); diff --git a/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_overview.ts b/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_overview.ts new file mode 100644 index 0000000000000..da82f7cfedbf0 --- /dev/null +++ b/x-pack/plugins/monitoring/common/http_api/elasticsearch/post_elasticsearch_overview.ts @@ -0,0 +1,30 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import * as rt from 'io-ts'; +import { clusterUuidRT, ccsRT, timeRangeRT } from '../shared'; + +export const postElasticsearchOverviewRequestParamsRT = rt.type({ + clusterUuid: clusterUuidRT, +}); + +export const postElasticsearchOverviewRequestPayloadRT = rt.intersection([ + rt.partial({ + ccs: ccsRT, + }), + rt.type({ + timeRange: timeRangeRT, + }), +]); + +export type PostElasticsearchOverviewRequestPayload = rt.TypeOf< + typeof postElasticsearchOverviewRequestPayloadRT +>; + +export const postElasticsearchOverviewResponsePayloadRT = rt.type({ + // TODO: add payload entries +}); diff --git a/x-pack/plugins/monitoring/common/http_api/shared/index.ts b/x-pack/plugins/monitoring/common/http_api/shared/index.ts index db7d3dce6c463..4e47c7239e39f 100644 --- a/x-pack/plugins/monitoring/common/http_api/shared/index.ts +++ b/x-pack/plugins/monitoring/common/http_api/shared/index.ts @@ -5,6 +5,10 @@ * 2.0. */ -export * from './cluster'; export * from './ccs'; +export * from './cluster'; +export * from './literal_value'; +export * from './pagination'; +export * from './query_string_boolean'; +export * from './sorting'; export * from './time_range'; diff --git a/x-pack/plugins/monitoring/common/http_api/shared/literal_value.ts b/x-pack/plugins/monitoring/common/http_api/shared/literal_value.ts new file mode 100644 index 0000000000000..55719d6b31b12 --- /dev/null +++ b/x-pack/plugins/monitoring/common/http_api/shared/literal_value.ts @@ -0,0 +1,20 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import * as rt from 'io-ts'; + +export const createLiteralValueFromUndefinedRT = ( + literalValue: LiteralValue +) => + rt.undefined.pipe( + new rt.Type( + 'BooleanFromString', + rt.literal(literalValue).is, + (_value, _context) => rt.success(literalValue), + () => undefined + ) + ); diff --git a/x-pack/plugins/monitoring/common/http_api/shared/pagination.ts b/x-pack/plugins/monitoring/common/http_api/shared/pagination.ts new file mode 100644 index 0000000000000..50f1f77b0361c --- /dev/null +++ b/x-pack/plugins/monitoring/common/http_api/shared/pagination.ts @@ -0,0 +1,13 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import * as rt from 'io-ts'; + +export const paginationRT = rt.type({ + index: rt.number, + size: rt.number, +}); diff --git a/x-pack/plugins/monitoring/common/http_api/shared/query_string_boolean.ts b/x-pack/plugins/monitoring/common/http_api/shared/query_string_boolean.ts new file mode 100644 index 0000000000000..b9b6fcae3780c --- /dev/null +++ b/x-pack/plugins/monitoring/common/http_api/shared/query_string_boolean.ts @@ -0,0 +1,27 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { chain } from 'fp-ts/lib/Either'; +import { pipe } from 'fp-ts/lib/pipeable'; +import * as rt from 'io-ts'; + +export const booleanFromStringRT = new rt.Type( + 'BooleanFromString', + rt.boolean.is, + (value, context) => + pipe( + rt.string.validate(value, context), + chain((stringValue) => + stringValue === 'true' + ? rt.success(true) + : stringValue === 'false' + ? rt.success(false) + : rt.failure(value, context) + ) + ), + String +); diff --git a/x-pack/plugins/monitoring/common/http_api/shared/sorting.ts b/x-pack/plugins/monitoring/common/http_api/shared/sorting.ts new file mode 100644 index 0000000000000..c52c46a41376c --- /dev/null +++ b/x-pack/plugins/monitoring/common/http_api/shared/sorting.ts @@ -0,0 +1,18 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import * as rt from 'io-ts'; + +const sortingDirectionRT = rt.keyof({ + asc: null, + desc: null, +}); + +export const sortingRT = rt.partial({ + field: rt.string, + direction: sortingDirectionRT, +}); diff --git a/x-pack/plugins/monitoring/common/http_api/shared/time_range.ts b/x-pack/plugins/monitoring/common/http_api/shared/time_range.ts index edeb56d1e2ea1..4021e8d52f4cd 100644 --- a/x-pack/plugins/monitoring/common/http_api/shared/time_range.ts +++ b/x-pack/plugins/monitoring/common/http_api/shared/time_range.ts @@ -6,8 +6,29 @@ */ import * as rt from 'io-ts'; +import moment from 'moment'; +import { pipe } from 'fp-ts/lib/pipeable'; +import { chain } from 'fp-ts/lib/Either'; + +export const timestampFromStringRT = new rt.Type( + 'timestampFromStringRT', + (input): input is number => typeof input === 'number', + (input, context) => + pipe( + rt.string.validate(input, context), + chain((stringInput) => { + const momentValue = moment.utc(stringInput); + return momentValue.isValid() + ? rt.success(momentValue.valueOf()) + : rt.failure(stringInput, context); + }) + ), + (output) => new Date(output).toISOString() +); export const timeRangeRT = rt.type({ - min: rt.string, - max: rt.string, + min: timestampFromStringRT, + max: timestampFromStringRT, }); + +export type TimeRange = rt.TypeOf; diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_paginated_nodes.ts b/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_paginated_nodes.ts index 541320e8499f5..bf999d67a4d88 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_paginated_nodes.ts +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/get_paginated_nodes.ts @@ -36,6 +36,9 @@ import { ElasticsearchModifiedSource } from '../../../../../common/types/es'; interface Node { name: string; uuid: string; +} + +interface NodeWithStatus extends Node { isOnline: boolean; shardCount: number; } @@ -52,7 +55,7 @@ export async function getPaginatedNodes( nodesShardCount, }: { clusterStats: { - cluster_state?: { nodes: Record }; + cluster_state?: { nodes?: Record }; elasticsearch?: ElasticsearchModifiedSource['elasticsearch']; }; nodesShardCount: { nodes: Record }; @@ -67,10 +70,11 @@ export async function getPaginatedNodes( clusterStats?.cluster_state?.nodes ?? clusterStats?.elasticsearch?.cluster?.stats?.state?.nodes ?? {}; - for (const node of nodes) { - node.isOnline = !isUndefined(clusterStateNodes && clusterStateNodes[node.uuid]); - node.shardCount = nodesShardCount?.nodes[node.uuid]?.shardCount ?? 0; - } + const nodesWithStatus: NodeWithStatus[] = nodes.map((node) => ({ + ...node, + isOnline: !isUndefined(clusterStateNodes && clusterStateNodes[node.uuid]), + shardCount: nodesShardCount?.nodes[node.uuid]?.shardCount ?? 0, + })); // `metricSet` defines a list of metrics that are sortable in the UI // but we don't need to fetch all the data for these metrics to perform @@ -80,13 +84,13 @@ export async function getPaginatedNodes( const filters = [ { terms: { - 'source_node.name': nodes.map((node) => node.name), + 'source_node.name': nodesWithStatus.map((node) => node.name), }, }, ]; const groupBy = { field: `source_node.uuid`, - include: nodes.map((node) => node.uuid), + include: nodesWithStatus.map((node) => node.uuid), size, }; const metricSeriesData = await getMetrics( @@ -94,7 +98,7 @@ export async function getPaginatedNodes( 'elasticsearch', metricSet, filters, - { nodes }, + { nodes: nodesWithStatus }, 4, groupBy ); @@ -106,7 +110,7 @@ export async function getPaginatedNodes( const metricList = metricSeriesData[metricName]; for (const metricItem of metricList[0]) { - const node = nodes.find((n) => n.uuid === metricItem.groupedBy); + const node = nodesWithStatus.find((n) => n.uuid === metricItem.groupedBy); if (!node) { continue; } @@ -124,7 +128,7 @@ export async function getPaginatedNodes( // Manually apply pagination/sorting/filtering concerns // Filtering - const filteredNodes = filter(nodes, queryText, ['name']); // We only support filtering by name right now + const filteredNodes = filter(nodesWithStatus, queryText, ['name']); // We only support filtering by name right now // Sorting const sortedNodes = sortNodes(filteredNodes, sort); diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/sort_nodes.ts b/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/sort_nodes.ts index 33d29f2a05998..1fbd0f0130e49 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/sort_nodes.ts +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/nodes/get_nodes/sort_nodes.ts @@ -9,7 +9,10 @@ import { orderBy } from 'lodash'; type Node = Record; -export function sortNodes(nodes: Node[], sort?: { field: string; direction: 'asc' | 'desc' }) { +export function sortNodes( + nodes: T[], + sort?: { field: string; direction: 'asc' | 'desc' } +) { if (!sort || !sort.field) { return nodes; } diff --git a/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_shard_stats.ts b/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_shard_stats.ts index 9c2c5cf45235a..df60ce3ffecef 100644 --- a/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_shard_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/elasticsearch/shards/get_shard_stats.ts @@ -6,22 +6,15 @@ */ import { get } from 'lodash'; -// @ts-ignore -import { checkParam } from '../../error_missing_required'; -// @ts-ignore +import { ElasticsearchModifiedSource, ElasticsearchResponse } from '../../../../common/types/es'; +import { Globals } from '../../../static_globals'; +import { LegacyRequest } from '../../../types'; +import { getNewIndexPatterns } from '../../cluster/get_index_patterns'; import { createQuery } from '../../create_query'; -// @ts-ignore import { ElasticsearchMetric } from '../../metrics'; -// @ts-ignore -import { normalizeIndexShards, normalizeNodeShards } from './normalize_shard_objects'; -// @ts-ignore -import { getShardAggs } from './get_shard_stat_aggs'; -// @ts-ignore import { calculateIndicesTotals } from './calculate_shard_stat_indices_totals'; -import { LegacyRequest } from '../../../types'; -import { ElasticsearchResponse, ElasticsearchModifiedSource } from '../../../../common/types/es'; -import { getNewIndexPatterns } from '../../cluster/get_index_patterns'; -import { Globals } from '../../../static_globals'; +import { getShardAggs } from './get_shard_stat_aggs'; +import { normalizeIndexShards, normalizeNodeShards } from './normalize_shard_objects'; export function handleResponse( resp: ElasticsearchResponse, @@ -58,7 +51,17 @@ export function handleResponse( export function getShardStats( req: LegacyRequest, cluster: ElasticsearchModifiedSource, - { includeNodes = false, includeIndices = false, indexName = null, nodeUuid = null } = {} + { + includeNodes = false, + includeIndices = false, + indexName = null, + nodeUuid = null, + }: { + includeNodes?: boolean; + includeIndices?: boolean; + indexName?: string | null; + nodeUuid?: string | null; + } = {} ) { const dataset = 'shard'; // data_stream.dataset const type = 'shards'; // legacy diff --git a/x-pack/plugins/monitoring/server/lib/logs/detect_reason.ts b/x-pack/plugins/monitoring/server/lib/logs/detect_reason.ts index 216d29d841a86..e9b906af677e8 100644 --- a/x-pack/plugins/monitoring/server/lib/logs/detect_reason.ts +++ b/x-pack/plugins/monitoring/server/lib/logs/detect_reason.ts @@ -8,7 +8,7 @@ import { LegacyRequest } from '../../types'; import { createTimeFilter } from '../create_query'; -interface Opts { +export interface FilebeatIndexCheckOpts { start: number; end: number; clusterUuid?: string; @@ -19,7 +19,7 @@ interface Opts { async function doesFilebeatIndexExist( req: LegacyRequest, filebeatIndexPattern: string, - { start, end, clusterUuid, nodeUuid, indexUuid }: Opts + { start, end, clusterUuid, nodeUuid, indexUuid }: FilebeatIndexCheckOpts ) { const metric = { timestampField: '@timestamp' }; const filter = [createTimeFilter({ start, end, metric })]; @@ -142,6 +142,10 @@ async function doesFilebeatIndexExist( }; } -export async function detectReason(req: LegacyRequest, filebeatIndexPattern: string, opts: Opts) { +export async function detectReason( + req: LegacyRequest, + filebeatIndexPattern: string, + opts: FilebeatIndexCheckOpts +) { return await doesFilebeatIndexExist(req, filebeatIndexPattern, opts); } diff --git a/x-pack/plugins/monitoring/server/lib/logs/get_logs.ts b/x-pack/plugins/monitoring/server/lib/logs/get_logs.ts index 9e9f67831ba95..c243414c3649e 100644 --- a/x-pack/plugins/monitoring/server/lib/logs/get_logs.ts +++ b/x-pack/plugins/monitoring/server/lib/logs/get_logs.ts @@ -6,17 +6,11 @@ */ import moment from 'moment'; -// @ts-ignore import { checkParam } from '../error_missing_required'; -// @ts-ignore import { createTimeFilter } from '../create_query'; -// @ts-ignore -import { detectReason } from './detect_reason'; -// @ts-ignore +import { detectReason, FilebeatIndexCheckOpts } from './detect_reason'; import { formatUTCTimestampForTimezone } from '../format_timezone'; -// @ts-ignore import { getTimezone } from '../get_timezone'; -// @ts-ignore import { detectReasonFromException } from './detect_reason_from_exception'; import { LegacyRequest } from '../../types'; import { FilebeatResponse } from '../../../common/types/filebeat'; @@ -36,7 +30,7 @@ async function handleResponse( response: FilebeatResponse, req: LegacyRequest, filebeatIndexPattern: string, - opts: { clusterUuid: string; nodeUuid: string; indexUuid: string; start: number; end: number } + opts: FilebeatIndexCheckOpts ) { const result: { enabled: boolean; logs: Log[]; reason?: any } = { enabled: false, @@ -73,13 +67,7 @@ export async function getLogs( config: MonitoringConfig, req: LegacyRequest, filebeatIndexPattern: string, - { - clusterUuid, - nodeUuid, - indexUuid, - start, - end, - }: { clusterUuid: string; nodeUuid: string; indexUuid: string; start: number; end: number } + { clusterUuid, nodeUuid, indexUuid, start, end }: FilebeatIndexCheckOpts ) { checkParam(filebeatIndexPattern, 'filebeatIndexPattern in logs/getLogs'); diff --git a/x-pack/plugins/monitoring/server/plugin.ts b/x-pack/plugins/monitoring/server/plugin.ts index 433a3358558da..c81fb90e614cb 100644 --- a/x-pack/plugins/monitoring/server/plugin.ts +++ b/x-pack/plugins/monitoring/server/plugin.ts @@ -328,7 +328,7 @@ export class MonitoringPlugin const plugins = (await getCoreServices())[1]; const coreContext = await context.core; const actionContext = await context.actions; - const legacyRequest: LegacyRequest = { + const legacyRequest: LegacyRequest = { ...req, logger: this.log, getLogger: this.getLogger, diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/apm/instance.ts b/x-pack/plugins/monitoring/server/routes/api/v1/apm/instance.ts index 69b226ef3eaed..7410a91293fb9 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/apm/instance.ts +++ b/x-pack/plugins/monitoring/server/routes/api/v1/apm/instance.ts @@ -19,12 +19,15 @@ import { MonitoringCore } from '../../../../types'; import { metricSet } from './metric_set_instance'; export function apmInstanceRoute(server: MonitoringCore) { + const validateParams = createValidationFunction(postApmInstanceRequestParamsRT); + const validateBody = createValidationFunction(postApmInstanceRequestPayloadRT); + server.route({ method: 'post', path: '/api/monitoring/v1/clusters/{clusterUuid}/apm/{apmUuid}', validate: { - params: createValidationFunction(postApmInstanceRequestParamsRT), - body: createValidationFunction(postApmInstanceRequestPayloadRT), + params: validateParams, + body: validateBody, }, async handler(req) { const apmUuid = req.params.apmUuid; diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/apm/instances.ts b/x-pack/plugins/monitoring/server/routes/api/v1/apm/instances.ts index 960a8dc3627b0..6225bd4b553b1 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/apm/instances.ts +++ b/x-pack/plugins/monitoring/server/routes/api/v1/apm/instances.ts @@ -17,12 +17,15 @@ import { handleError } from '../../../../lib/errors'; import { MonitoringCore } from '../../../../types'; export function apmInstancesRoute(server: MonitoringCore) { + const validateParams = createValidationFunction(postApmInstancesRequestParamsRT); + const validateBody = createValidationFunction(postApmInstancesRequestPayloadRT); + server.route({ method: 'post', path: '/api/monitoring/v1/clusters/{clusterUuid}/apm/instances', validate: { - params: createValidationFunction(postApmInstancesRequestParamsRT), - body: createValidationFunction(postApmInstancesRequestPayloadRT), + params: validateParams, + body: validateBody, }, async handler(req) { const config = server.config; diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/apm/overview.ts b/x-pack/plugins/monitoring/server/routes/api/v1/apm/overview.ts index 532b5fa4dc4c8..5e23b0138a72f 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/apm/overview.ts +++ b/x-pack/plugins/monitoring/server/routes/api/v1/apm/overview.ts @@ -17,12 +17,15 @@ import { metricSet } from './metric_set_overview'; import { getApmClusterStatus } from './_get_apm_cluster_status'; export function apmOverviewRoute(server: MonitoringCore) { + const validateParams = createValidationFunction(postApmOverviewRequestParamsRT); + const validateBody = createValidationFunction(postApmOverviewRequestPayloadRT); + server.route({ method: 'post', path: '/api/monitoring/v1/clusters/{clusterUuid}/apm', validate: { - params: createValidationFunction(postApmOverviewRequestParamsRT), - body: createValidationFunction(postApmOverviewRequestPayloadRT), + params: validateParams, + body: validateBody, }, async handler(req) { const config = server.config; diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/beats/beat_detail.ts b/x-pack/plugins/monitoring/server/routes/api/v1/beats/beat_detail.ts index 18f6de905dbfe..26477b3611360 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/beats/beat_detail.ts +++ b/x-pack/plugins/monitoring/server/routes/api/v1/beats/beat_detail.ts @@ -20,12 +20,15 @@ import { MonitoringCore } from '../../../../types'; import { metricSet } from './metric_set_detail'; export function beatsDetailRoute(server: MonitoringCore) { + const validateParams = createValidationFunction(postBeatDetailRequestParamsRT); + const validateBody = createValidationFunction(postBeatDetailRequestPayloadRT); + server.route({ method: 'post', path: '/api/monitoring/v1/clusters/{clusterUuid}/beats/beat/{beatUuid}', validate: { - params: createValidationFunction(postBeatDetailRequestParamsRT), - body: createValidationFunction(postBeatDetailRequestPayloadRT), + params: validateParams, + body: validateBody, }, async handler(req) { const clusterUuid = req.params.clusterUuid; diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/beats/beats.ts b/x-pack/plugins/monitoring/server/routes/api/v1/beats/beats.ts index 8bf73bb115f74..385950d23f836 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/beats/beats.ts +++ b/x-pack/plugins/monitoring/server/routes/api/v1/beats/beats.ts @@ -18,12 +18,15 @@ import { handleError } from '../../../../lib/errors'; import { MonitoringCore } from '../../../../types'; export function beatsListingRoute(server: MonitoringCore) { + const validateParams = createValidationFunction(postBeatsListingRequestParamsRT); + const validateBody = createValidationFunction(postBeatsListingRequestPayloadRT); + server.route({ method: 'post', path: '/api/monitoring/v1/clusters/{clusterUuid}/beats/beats', validate: { - params: createValidationFunction(postBeatsListingRequestParamsRT), - body: createValidationFunction(postBeatsListingRequestPayloadRT), + params: validateParams, + body: validateBody, }, async handler(req) { const config = server.config; diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/beats/overview.ts b/x-pack/plugins/monitoring/server/routes/api/v1/beats/overview.ts index 6497c5568a1a8..f36d6cd0b1aeb 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/beats/overview.ts +++ b/x-pack/plugins/monitoring/server/routes/api/v1/beats/overview.ts @@ -20,12 +20,15 @@ import { MonitoringCore } from '../../../../types'; import { metricSet } from './metric_set_overview'; export function beatsOverviewRoute(server: MonitoringCore) { + const validateParams = createValidationFunction(postBeatsOverviewRequestParamsRT); + const validateBody = createValidationFunction(postBeatsOverviewRequestPayloadRT); + server.route({ method: 'post', path: '/api/monitoring/v1/clusters/{clusterUuid}/beats', validate: { - params: createValidationFunction(postBeatsOverviewRequestParamsRT), - body: createValidationFunction(postBeatsOverviewRequestPayloadRT), + params: validateParams, + body: validateBody, }, async handler(req) { const config = server.config; diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ccr.ts b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ccr.ts index 3dd4feb3db805..726ed7b2500d1 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ccr.ts +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ccr.ts @@ -5,21 +5,24 @@ * 2.0. */ -import { schema } from '@kbn/config-schema'; -import moment from 'moment'; import { get, groupBy } from 'lodash'; -// @ts-ignore -import { handleError } from '../../../../lib/errors/handle_error'; -// @ts-ignore import { prefixIndexPatternWithCcs } from '../../../../../common/ccs_utils'; import { INDEX_PATTERN_ELASTICSEARCH } from '../../../../../common/constants'; import { - ElasticsearchResponse, + postElasticsearchCcrRequestParamsRT, + postElasticsearchCcrRequestPayloadRT, + postElasticsearchCcrResponsePayloadRT, +} from '../../../../../common/http_api/elasticsearch'; +import { TimeRange } from '../../../../../common/http_api/shared'; +import { ElasticsearchLegacySource, ElasticsearchMetricbeatSource, + ElasticsearchResponse, } from '../../../../../common/types/es'; -import { LegacyRequest } from '../../../../types'; import { MonitoringConfig } from '../../../../config'; +import { createValidationFunction } from '../../../../lib/create_route_validation_function'; +import { handleError } from '../../../../lib/errors/handle_error'; +import { LegacyRequest, MonitoringCore } from '../../../../types'; function getBucketScript(max: string, min: string) { return { @@ -33,9 +36,12 @@ function getBucketScript(max: string, min: string) { }; } -function buildRequest(req: LegacyRequest, config: MonitoringConfig, esIndexPattern: string) { - const min = moment.utc(req.payload.timeRange.min).valueOf(); - const max = moment.utc(req.payload.timeRange.max).valueOf(); +function buildRequest( + req: LegacyRequest, + config: MonitoringConfig, + esIndexPattern: string +) { + const { min, max } = req.payload.timeRange; const maxBucketSize = config.ui.max_bucket_size; const aggs = { ops_synced_max: { @@ -195,25 +201,18 @@ function buildRequest(req: LegacyRequest, config: MonitoringConfig, esIndexPatte }; } -export function ccrRoute(server: { route: (p: any) => void; config: MonitoringConfig }) { +export function ccrRoute(server: MonitoringCore) { + const validateParams = createValidationFunction(postElasticsearchCcrRequestParamsRT); + const validateBody = createValidationFunction(postElasticsearchCcrRequestPayloadRT); + server.route({ - method: 'POST', + method: 'post', path: '/api/monitoring/v1/clusters/{clusterUuid}/elasticsearch/ccr', - config: { - validate: { - params: schema.object({ - clusterUuid: schema.string(), - }), - body: schema.object({ - ccs: schema.maybe(schema.string()), - timeRange: schema.object({ - min: schema.string(), - max: schema.string(), - }), - }), - }, + validate: { + params: validateParams, + body: validateBody, }, - async handler(req: LegacyRequest) { + async handler(req) { const config = server.config; const ccs = req.payload.ccs; const esIndexPattern = prefixIndexPatternWithCcs(config, INDEX_PATTERN_ELASTICSEARCH, ccs); @@ -322,7 +321,7 @@ export function ccrRoute(server: { route: (p: any) => void; config: MonitoringCo return accum; }, []); - return { data }; + return postElasticsearchCcrResponsePayloadRT.encode({ data }); } catch (err) { return handleError(err, req); } diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ccr_shard.ts b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ccr_shard.ts index 44a1eb1807595..797b8addd02cf 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ccr_shard.ts +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ccr_shard.ts @@ -5,18 +5,19 @@ * 2.0. */ -import moment from 'moment'; -import { schema } from '@kbn/config-schema'; -// @ts-ignore -import { handleError } from '../../../../lib/errors/handle_error'; -// @ts-ignore -import { prefixIndexPatternWithCcs } from '../../../../../common/ccs_utils'; -// @ts-ignore -import { getMetrics } from '../../../../lib/details/get_metrics'; +import { + postElasticsearchCcrShardRequestParamsRT, + postElasticsearchCcrShardRequestPayloadRT, + postElasticsearchCcrShardResponsePayloadRT, +} from '../../../../../common/http_api/elasticsearch'; +import { TimeRange } from '../../../../../common/http_api/shared'; import { ElasticsearchResponse } from '../../../../../common/types/es'; -import { LegacyRequest } from '../../../../types'; import { getNewIndexPatterns } from '../../../../lib/cluster/get_index_patterns'; +import { createValidationFunction } from '../../../../lib/create_route_validation_function'; +import { getMetrics } from '../../../../lib/details/get_metrics'; +import { handleError } from '../../../../lib/errors/handle_error'; import { Globals } from '../../../../static_globals'; +import { LegacyRequest, MonitoringCore } from '../../../../types'; function getFormattedLeaderIndex(leaderIndex: string) { let leader = leaderIndex; @@ -27,10 +28,12 @@ function getFormattedLeaderIndex(leaderIndex: string) { return leader; } -async function getCcrStat(req: LegacyRequest, esIndexPattern: string, filters: unknown[]) { - const min = moment.utc(req.payload.timeRange.min).valueOf(); - const max = moment.utc(req.payload.timeRange.max).valueOf(); - +async function getCcrStat( + req: LegacyRequest, + esIndexPattern: string, + filters: unknown[] +) { + const { min, max } = req.payload.timeRange; const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('monitoring'); const params = { @@ -78,27 +81,18 @@ async function getCcrStat(req: LegacyRequest, esIndexPattern: string, filters: u return await callWithRequest(req, 'search', params); } -export function ccrShardRoute(server: { route: (p: any) => void; config: () => {} }) { +export function ccrShardRoute(server: MonitoringCore) { + const validateParams = createValidationFunction(postElasticsearchCcrShardRequestParamsRT); + const validateBody = createValidationFunction(postElasticsearchCcrShardRequestPayloadRT); + server.route({ - method: 'POST', + method: 'post', path: '/api/monitoring/v1/clusters/{clusterUuid}/elasticsearch/ccr/{index}/shard/{shardId}', - config: { - validate: { - params: schema.object({ - clusterUuid: schema.string(), - index: schema.string(), - shardId: schema.string(), - }), - body: schema.object({ - ccs: schema.maybe(schema.string()), - timeRange: schema.object({ - min: schema.string(), - max: schema.string(), - }), - }), - }, + validate: { + params: validateParams, + body: validateBody, }, - async handler(req: LegacyRequest) { + async handler(req) { const index = req.params.index; const shardId = req.params.shardId; const moduleType = 'elasticsearch'; @@ -171,7 +165,7 @@ export function ccrShardRoute(server: { route: (p: any) => void; config: () => { const leaderIndex = mbStat ? mbStat?.leader?.index : legacyStat?.leader_index; - return { + return postElasticsearchCcrShardResponsePayloadRT.encode({ metrics, stat: mbStat ?? legacyStat, formattedLeader: getFormattedLeaderIndex(leaderIndex ?? ''), @@ -179,7 +173,7 @@ export function ccrShardRoute(server: { route: (p: any) => void; config: () => { ccrResponse.hits?.hits[0]?._source['@timestamp'] ?? ccrResponse.hits?.hits[0]?._source.timestamp, oldestStat: oldestMBStat ?? oldestLegacyStat, - }; + }); } catch (err) { return handleError(err, req); } diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/index.js b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/index.ts similarity index 100% rename from x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/index.js rename to x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/index.ts diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/index_detail.js b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/index_detail.ts similarity index 81% rename from x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/index_detail.js rename to x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/index_detail.ts index b4f317c9a435d..1d31064f71ebb 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/index_detail.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/index_detail.ts @@ -6,38 +6,35 @@ */ import { get } from 'lodash'; -import { schema } from '@kbn/config-schema'; +import { prefixIndexPatternWithCcs } from '../../../../../common/ccs_utils'; +import { CCS_REMOTE_PATTERN } from '../../../../../common/constants'; +import { + postElasticsearchIndexDetailRequestParamsRT, + postElasticsearchIndexDetailRequestPayloadRT, + postElasticsearchIndexDetailResponsePayloadRT, +} from '../../../../../common/http_api/elasticsearch'; import { getClusterStats } from '../../../../lib/cluster/get_cluster_stats'; -import { getIndexSummary } from '../../../../lib/elasticsearch/indices'; +import { createValidationFunction } from '../../../../lib/create_route_validation_function'; import { getMetrics } from '../../../../lib/details/get_metrics'; +import { getIndexSummary } from '../../../../lib/elasticsearch/indices'; import { getShardAllocation, getShardStats } from '../../../../lib/elasticsearch/shards'; import { handleError } from '../../../../lib/errors/handle_error'; -import { prefixIndexPatternWithCcs } from '../../../../../common/ccs_utils'; -import { metricSet } from './metric_set_index_detail'; import { getLogs } from '../../../../lib/logs/get_logs'; -import { CCS_REMOTE_PATTERN } from '../../../../../common/constants'; +import { MonitoringCore } from '../../../../types'; +import { metricSets } from './metric_set_index_detail'; -const { advanced: metricSetAdvanced, overview: metricSetOverview } = metricSet; +const { advanced: metricSetAdvanced, overview: metricSetOverview } = metricSets; + +export function esIndexRoute(server: MonitoringCore) { + const validateParams = createValidationFunction(postElasticsearchIndexDetailRequestParamsRT); + const validateBody = createValidationFunction(postElasticsearchIndexDetailRequestPayloadRT); -export function esIndexRoute(server) { server.route({ - method: 'POST', + method: 'post', path: '/api/monitoring/v1/clusters/{clusterUuid}/elasticsearch/indices/{id}', - config: { - validate: { - params: schema.object({ - clusterUuid: schema.string(), - id: schema.string(), - }), - body: schema.object({ - ccs: schema.maybe(schema.string()), - timeRange: schema.object({ - min: schema.string(), - max: schema.string(), - }), - is_advanced: schema.boolean(), - }), - }, + validate: { + params: validateParams, + body: validateBody, }, handler: async (req) => { try { @@ -111,12 +108,12 @@ export function esIndexRoute(server) { }; } - return { + return postElasticsearchIndexDetailResponsePayloadRT.encode({ indexSummary, metrics, logs, ...shardAllocation, - }; + }); } catch (err) { throw handleError(err, req); } diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/indices.js b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/indices.ts similarity index 59% rename from x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/indices.js rename to x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/indices.ts index de41137791108..00956410d8c4d 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/indices.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/indices.ts @@ -5,48 +5,46 @@ * 2.0. */ -import { schema } from '@kbn/config-schema'; +import { + postElasticsearchIndicesRequestParamsRT, + postElasticsearchIndicesRequestPayloadRT, + postElasticsearchIndicesRequestQueryRT, + postElasticsearchIndicesResponsePayloadRT, +} from '../../../../../common/http_api/elasticsearch'; import { getClusterStats } from '../../../../lib/cluster/get_cluster_stats'; import { getClusterStatus } from '../../../../lib/cluster/get_cluster_status'; +import { createValidationFunction } from '../../../../lib/create_route_validation_function'; import { getIndices } from '../../../../lib/elasticsearch/indices'; -import { handleError } from '../../../../lib/errors/handle_error'; import { getIndicesUnassignedShardStats } from '../../../../lib/elasticsearch/shards/get_indices_unassigned_shard_stats'; +import { handleError } from '../../../../lib/errors/handle_error'; +import { MonitoringCore } from '../../../../types'; + +export function esIndicesRoute(server: MonitoringCore) { + const validateParams = createValidationFunction(postElasticsearchIndicesRequestParamsRT); + const validateQuery = createValidationFunction(postElasticsearchIndicesRequestQueryRT); + const validateBody = createValidationFunction(postElasticsearchIndicesRequestPayloadRT); -export function esIndicesRoute(server) { server.route({ - method: 'POST', + method: 'post', path: '/api/monitoring/v1/clusters/{clusterUuid}/elasticsearch/indices', - config: { - validate: { - params: schema.object({ - clusterUuid: schema.string(), - }), - query: schema.object({ - show_system_indices: schema.boolean(), - }), - body: schema.object({ - ccs: schema.maybe(schema.string()), - timeRange: schema.object({ - min: schema.string(), - max: schema.string(), - }), - }), - }, + validate: { + params: validateParams, + query: validateQuery, + body: validateBody, }, async handler(req) { const { clusterUuid } = req.params; const { show_system_indices: showSystemIndices } = req.query; - const { ccs } = req.payload; try { - const clusterStats = await getClusterStats(req, clusterUuid, ccs); + const clusterStats = await getClusterStats(req, clusterUuid); const indicesUnassignedShardStats = await getIndicesUnassignedShardStats(req, clusterStats); const indices = await getIndices(req, showSystemIndices, indicesUnassignedShardStats); - return { + return postElasticsearchIndicesResponsePayloadRT.encode({ clusterStatus: getClusterStatus(clusterStats, indicesUnassignedShardStats), indices, - }; + }); } catch (err) { throw handleError(err, req); } diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_index_detail.js b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_index_detail.ts similarity index 92% rename from x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_index_detail.js rename to x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_index_detail.ts index fce09eac4918f..36e3cef797967 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_index_detail.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_index_detail.ts @@ -5,7 +5,12 @@ * 2.0. */ -export const metricSet = { +import { MetricDescriptor } from '../../../../lib/details/get_metrics'; + +export const metricSets: { + advanced: MetricDescriptor[]; + overview: MetricDescriptor[]; +} = { advanced: [ { keys: ['index_mem_fixed_bit_set', 'index_mem_versions'], diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_node_detail.js b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_node_detail.ts similarity index 93% rename from x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_node_detail.js rename to x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_node_detail.ts index 5303dc452f850..9de51af5ecbaa 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_node_detail.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_node_detail.ts @@ -5,7 +5,12 @@ * 2.0. */ -export const metricSets = { +import { MetricDescriptor } from '../../../../lib/details/get_metrics'; + +export const metricSets: { + advanced: MetricDescriptor[]; + overview: MetricDescriptor[]; +} = { advanced: [ { keys: ['node_jvm_mem_max_in_bytes', 'node_jvm_mem_used_in_bytes'], diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_overview.js b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_overview.ts similarity index 79% rename from x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_overview.js rename to x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_overview.ts index 317486bce4adf..9ea00bf8a503a 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_overview.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/metric_set_overview.ts @@ -5,7 +5,9 @@ * 2.0. */ -export const metricSet = [ +import { MetricDescriptor } from '../../../../lib/details/get_metrics'; + +export const metricSet: MetricDescriptor[] = [ 'cluster_search_request_rate', 'cluster_query_latency', { diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ml_jobs.js b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ml_jobs.ts similarity index 62% rename from x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ml_jobs.js rename to x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ml_jobs.ts index f2f0698bae18c..f51ca41ee4e9f 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ml_jobs.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/ml_jobs.ts @@ -5,30 +5,29 @@ * 2.0. */ -import { schema } from '@kbn/config-schema'; +import { + postElasticsearchMlJobsRequestParamsRT, + postElasticsearchMlJobsRequestPayloadRT, + postElasticsearchMlJobsResponsePayloadRT, +} from '../../../../../common/http_api/elasticsearch'; import { getClusterStats } from '../../../../lib/cluster/get_cluster_stats'; import { getClusterStatus } from '../../../../lib/cluster/get_cluster_status'; +import { createValidationFunction } from '../../../../lib/create_route_validation_function'; import { getMlJobs } from '../../../../lib/elasticsearch/get_ml_jobs'; -import { handleError } from '../../../../lib/errors/handle_error'; import { getIndicesUnassignedShardStats } from '../../../../lib/elasticsearch/shards/get_indices_unassigned_shard_stats'; +import { handleError } from '../../../../lib/errors/handle_error'; +import { MonitoringCore } from '../../../../types'; + +export function mlJobRoute(server: MonitoringCore) { + const validateParams = createValidationFunction(postElasticsearchMlJobsRequestParamsRT); + const validateBody = createValidationFunction(postElasticsearchMlJobsRequestPayloadRT); -export function mlJobRoute(server) { server.route({ - method: 'POST', + method: 'post', path: '/api/monitoring/v1/clusters/{clusterUuid}/elasticsearch/ml_jobs', - config: { - validate: { - params: schema.object({ - clusterUuid: schema.string(), - }), - body: schema.object({ - ccs: schema.maybe(schema.string()), - timeRange: schema.object({ - min: schema.string(), - max: schema.string(), - }), - }), - }, + validate: { + params: validateParams, + body: validateBody, }, async handler(req) { const clusterUuid = req.params.clusterUuid; @@ -37,10 +36,10 @@ export function mlJobRoute(server) { const clusterStats = await getClusterStats(req, clusterUuid); const indicesUnassignedShardStats = await getIndicesUnassignedShardStats(req, clusterStats); const rows = await getMlJobs(req); - return { + return postElasticsearchMlJobsResponsePayloadRT.encode({ clusterStatus: getClusterStatus(clusterStats, indicesUnassignedShardStats), rows, - }; + }); } catch (err) { throw handleError(err, req); } diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/node_detail.js b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/node_detail.ts similarity index 71% rename from x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/node_detail.js rename to x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/node_detail.ts index 1504bb076101e..85ba1d1a2bb8e 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/node_detail.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/node_detail.ts @@ -6,44 +6,43 @@ */ import { get } from 'lodash'; -import { schema } from '@kbn/config-schema'; +import { prefixIndexPatternWithCcs } from '../../../../../common/ccs_utils'; +import { CCS_REMOTE_PATTERN } from '../../../../../common/constants'; +import { + postElasticsearchNodeDetailRequestParamsRT, + postElasticsearchNodeDetailRequestPayloadRT, + postElasticsearchNodeDetailResponsePayloadRT, +} from '../../../../../common/http_api/elasticsearch'; import { getClusterStats } from '../../../../lib/cluster/get_cluster_stats'; +import { createValidationFunction } from '../../../../lib/create_route_validation_function'; +import { + getMetrics, + MetricDescriptor, + NamedMetricDescriptor, +} from '../../../../lib/details/get_metrics'; import { getNodeSummary } from '../../../../lib/elasticsearch/nodes'; -import { getShardStats, getShardAllocation } from '../../../../lib/elasticsearch/shards'; -import { getMetrics } from '../../../../lib/details/get_metrics'; +import { getShardAllocation, getShardStats } from '../../../../lib/elasticsearch/shards'; import { handleError } from '../../../../lib/errors/handle_error'; -import { prefixIndexPatternWithCcs } from '../../../../../common/ccs_utils'; -import { metricSets } from './metric_set_node_detail'; import { getLogs } from '../../../../lib/logs/get_logs'; -import { CCS_REMOTE_PATTERN } from '../../../../../common/constants'; +import { MonitoringCore } from '../../../../types'; +import { metricSets } from './metric_set_node_detail'; const { advanced: metricSetAdvanced, overview: metricSetOverview } = metricSets; -export function esNodeRoute(server) { +export function esNodeRoute(server: MonitoringCore) { + const validateParams = createValidationFunction(postElasticsearchNodeDetailRequestParamsRT); + const validateBody = createValidationFunction(postElasticsearchNodeDetailRequestPayloadRT); + server.route({ - method: 'POST', + method: 'post', path: '/api/monitoring/v1/clusters/{clusterUuid}/elasticsearch/nodes/{nodeUuid}', - config: { - validate: { - params: schema.object({ - clusterUuid: schema.string(), - nodeUuid: schema.string(), - }), - body: schema.object({ - ccs: schema.maybe(schema.string()), - showSystemIndices: schema.boolean({ defaultValue: false }), // show/hide system indices in shard allocation table - timeRange: schema.object({ - min: schema.string(), - max: schema.string(), - }), - is_advanced: schema.boolean(), - }), - }, + validate: { + params: validateParams, + body: validateBody, }, async handler(req) { const config = server.config; - const ccs = req.payload.ccs; - const showSystemIndices = req.payload.showSystemIndices; + const showSystemIndices = req.payload.showSystemIndices ?? false; const clusterUuid = req.params.clusterUuid; const nodeUuid = req.params.nodeUuid; const start = req.payload.timeRange.min; @@ -55,23 +54,27 @@ export function esNodeRoute(server) { ); const isAdvanced = req.payload.is_advanced; - let metricSet; + let metricSet: MetricDescriptor[]; if (isAdvanced) { metricSet = metricSetAdvanced; } else { metricSet = metricSetOverview; // set the cgroup option if needed const showCgroupMetricsElasticsearch = config.ui.container.elasticsearch.enabled; - const metricCpu = metricSet.find((m) => m.name === 'node_cpu_metric'); - if (showCgroupMetricsElasticsearch) { - metricCpu.keys = ['node_cgroup_quota_as_cpu_utilization']; - } else { - metricCpu.keys = ['node_cpu_utilization']; + const metricCpu = metricSet.find( + (m): m is NamedMetricDescriptor => typeof m === 'object' && m.name === 'node_cpu_metric' + ); + if (metricCpu) { + if (showCgroupMetricsElasticsearch) { + metricCpu.keys = ['node_cgroup_quota_as_cpu_utilization']; + } else { + metricCpu.keys = ['node_cpu_utilization']; + } } } try { - const cluster = await getClusterStats(req, clusterUuid, ccs); + const cluster = await getClusterStats(req, clusterUuid); const clusterState = get( cluster, @@ -132,12 +135,12 @@ export function esNodeRoute(server) { }); } - return { + return postElasticsearchNodeDetailResponsePayloadRT.encode({ nodeSummary, metrics, logs, ...shardAllocation, - }; + }); } catch (err) { throw handleError(err, req); } diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/nodes.js b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/nodes.ts similarity index 66% rename from x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/nodes.js rename to x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/nodes.ts index fa0329f957f54..f291af318bf9b 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/nodes.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/nodes.ts @@ -5,45 +5,39 @@ * 2.0. */ -import { schema } from '@kbn/config-schema'; +import { + postElasticsearchNodesRequestParamsRT, + postElasticsearchNodesRequestPayloadRT, + postElasticsearchNodesResponsePayloadRT, +} from '../../../../../common/http_api/elasticsearch'; import { getClusterStats } from '../../../../lib/cluster/get_cluster_stats'; import { getClusterStatus } from '../../../../lib/cluster/get_cluster_status'; +import { createValidationFunction } from '../../../../lib/create_route_validation_function'; import { getNodes } from '../../../../lib/elasticsearch/nodes'; -import { getNodesShardCount } from '../../../../lib/elasticsearch/shards/get_nodes_shard_count'; -import { handleError } from '../../../../lib/errors/handle_error'; import { getPaginatedNodes } from '../../../../lib/elasticsearch/nodes/get_nodes/get_paginated_nodes'; import { LISTING_METRICS_NAMES } from '../../../../lib/elasticsearch/nodes/get_nodes/nodes_listing_metrics'; import { getIndicesUnassignedShardStats } from '../../../../lib/elasticsearch/shards/get_indices_unassigned_shard_stats'; +import { getNodesShardCount } from '../../../../lib/elasticsearch/shards/get_nodes_shard_count'; +import { handleError } from '../../../../lib/errors/handle_error'; +import { MonitoringCore } from '../../../../types'; + +export function esNodesRoute(server: MonitoringCore) { + const validateParams = createValidationFunction(postElasticsearchNodesRequestParamsRT); + const validateBody = createValidationFunction(postElasticsearchNodesRequestPayloadRT); -export function esNodesRoute(server) { server.route({ - method: 'POST', + method: 'post', path: '/api/monitoring/v1/clusters/{clusterUuid}/elasticsearch/nodes', - config: { - validate: { - params: schema.object({ - clusterUuid: schema.string(), - }), - body: schema.object({ - ccs: schema.maybe(schema.string()), - timeRange: schema.object({ - min: schema.string(), - max: schema.string(), - }), - pagination: schema.object({ - index: schema.number(), - size: schema.number(), - }), - sort: schema.object({ - field: schema.string({ defaultValue: '' }), - direction: schema.string({ defaultValue: '' }), - }), - queryText: schema.string({ defaultValue: '' }), - }), - }, + validate: { + params: validateParams, + body: validateBody, }, async handler(req) { - const { ccs, pagination, sort, queryText } = req.payload; + const { + pagination, + sort: { field = '', direction = 'asc' } = {}, + queryText = '', + } = req.payload; const clusterUuid = req.params.clusterUuid; try { @@ -58,17 +52,23 @@ export function esNodesRoute(server) { { clusterUuid }, metricSet, pagination, - sort, + { + field, + direction, + }, queryText, { clusterStats, nodesShardCount, - }, - ccs + } ); const nodes = await getNodes(req, pageOfNodes, clusterStats, nodesShardCount); - return { clusterStatus, nodes, totalNodeCount }; + return postElasticsearchNodesResponsePayloadRT.encode({ + clusterStatus, + nodes, + totalNodeCount, + }); } catch (err) { throw handleError(err, req); } diff --git a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/overview.js b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/overview.ts similarity index 72% rename from x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/overview.js rename to x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/overview.ts index 35066bb33784d..52410dd09f8df 100644 --- a/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/overview.js +++ b/x-pack/plugins/monitoring/server/routes/api/v1/elasticsearch/overview.ts @@ -5,35 +5,34 @@ * 2.0. */ -import { schema } from '@kbn/config-schema'; +import { prefixIndexPatternWithCcs } from '../../../../../common/ccs_utils'; +import { CCS_REMOTE_PATTERN } from '../../../../../common/constants'; +import { + postElasticsearchOverviewRequestParamsRT, + postElasticsearchOverviewRequestPayloadRT, + postElasticsearchOverviewResponsePayloadRT, +} from '../../../../../common/http_api/elasticsearch'; import { getClusterStats } from '../../../../lib/cluster/get_cluster_stats'; import { getClusterStatus } from '../../../../lib/cluster/get_cluster_status'; -import { getLastRecovery } from '../../../../lib/elasticsearch/get_last_recovery'; +import { createValidationFunction } from '../../../../lib/create_route_validation_function'; import { getMetrics } from '../../../../lib/details/get_metrics'; +import { getLastRecovery } from '../../../../lib/elasticsearch/get_last_recovery'; +import { getIndicesUnassignedShardStats } from '../../../../lib/elasticsearch/shards/get_indices_unassigned_shard_stats'; import { handleError } from '../../../../lib/errors/handle_error'; -import { prefixIndexPatternWithCcs } from '../../../../../common/ccs_utils'; -import { metricSet } from './metric_set_overview'; import { getLogs } from '../../../../lib/logs'; -import { getIndicesUnassignedShardStats } from '../../../../lib/elasticsearch/shards/get_indices_unassigned_shard_stats'; -import { CCS_REMOTE_PATTERN } from '../../../../../common/constants'; +import { MonitoringCore } from '../../../../types'; +import { metricSet } from './metric_set_overview'; + +export function esOverviewRoute(server: MonitoringCore) { + const validateParams = createValidationFunction(postElasticsearchOverviewRequestParamsRT); + const validateBody = createValidationFunction(postElasticsearchOverviewRequestPayloadRT); -export function esOverviewRoute(server) { server.route({ - method: 'POST', + method: 'post', path: '/api/monitoring/v1/clusters/{clusterUuid}/elasticsearch', - config: { - validate: { - params: schema.object({ - clusterUuid: schema.string(), - }), - body: schema.object({ - ccs: schema.maybe(schema.string()), - timeRange: schema.object({ - min: schema.string(), - max: schema.string(), - }), - }), - }, + validate: { + params: validateParams, + body: validateBody, }, async handler(req) { const config = server.config; @@ -43,9 +42,7 @@ export function esOverviewRoute(server) { config.ui.logs.index, CCS_REMOTE_PATTERN ); - - const start = req.payload.timeRange.min; - const end = req.payload.timeRange.max; + const { min: start, max: end } = req.payload.timeRange; try { const [clusterStats, metrics, shardActivity, logs] = await Promise.all([ @@ -63,7 +60,7 @@ export function esOverviewRoute(server) { logs, shardActivity, }; - return result; + return postElasticsearchOverviewResponsePayloadRT.encode(result); } catch (err) { throw handleError(err, req); } diff --git a/x-pack/plugins/monitoring/server/types.ts b/x-pack/plugins/monitoring/server/types.ts index bcd40fe38e415..5977484518146 100644 --- a/x-pack/plugins/monitoring/server/types.ts +++ b/x-pack/plugins/monitoring/server/types.ts @@ -80,16 +80,22 @@ export interface RouteDependencies { logger: Logger; } -export type MonitoringRouteConfig = { - method: RouteMethod; -} & RouteConfig & { - handler: (request: LegacyRequest) => any; - }; +type LegacyHandler = (req: LegacyRequest) => Promise; + +export type MonitoringRouteConfig = RouteConfig< + Params, + Query, + Body, + Method +> & { + method: Method; + handler: LegacyHandler; +}; export interface MonitoringCore { config: MonitoringConfig; log: Logger; - route: ( + route: ( options: MonitoringRouteConfig ) => void; } @@ -112,15 +118,12 @@ export interface MonitoringPluginSetup { getKibanaStats: IBulkUploader['getKibanaStats']; } -export interface LegacyRequest { +export interface LegacyRequest { logger: Logger; getLogger: (...scopes: string[]) => Logger; - payload: { - [key: string]: any; - }; - params: { - [key: string]: string; - }; + payload: Body; + params: Params; + query: Query; getKibanaStatsCollector: () => any; getUiSettingsService: () => any; getActionTypeRegistry: () => any;