From 28f7baa607b8e7ca11a432367292ece113b05132 Mon Sep 17 00:00:00 2001 From: Madison Caldwell Date: Tue, 4 Feb 2020 15:15:08 -0500 Subject: [PATCH] Address comments - Round 3 --- x-pack/plugins/endpoint/common/types.ts | 24 +++++++++++++++---- .../plugins/endpoint/server/routes/alerts.ts | 23 ++++++++++-------- .../services/endpoint/alert_query_builders.ts | 10 ++++---- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/x-pack/plugins/endpoint/common/types.ts b/x-pack/plugins/endpoint/common/types.ts index 1c8ba1f8cf7b8..0128cd3dd6df7 100644 --- a/x-pack/plugins/endpoint/common/types.ts +++ b/x-pack/plugins/endpoint/common/types.ts @@ -28,14 +28,30 @@ export class EndpointAppConstants { } export interface AlertResultList { - /* the alerts restricted by the page size */ + /** + * The alerts restricted by page size. + */ alerts: AlertData[]; - /* the total number of unique alerts in the index */ + + /** + * The total number of alerts on the page. + */ total: number; - /* the page size requested */ + + /** + * The size of the requested page. + */ request_page_size: number; - /* the page index requested */ + + /** + * The index of the requested page, starting at 0. + */ request_page_index: number; + + /** + * The offset of the requested page, starting at 0. + */ + result_from_index: number; } export interface EndpointResultList { diff --git a/x-pack/plugins/endpoint/server/routes/alerts.ts b/x-pack/plugins/endpoint/server/routes/alerts.ts index 2ed4cd90602e3..dcd594fac4dd0 100644 --- a/x-pack/plugins/endpoint/server/routes/alerts.ts +++ b/x-pack/plugins/endpoint/server/routes/alerts.ts @@ -7,7 +7,7 @@ import { IRouter } from 'kibana/server'; import { RequestHandler } from 'kibana/server'; import { SearchResponse } from 'elasticsearch'; -import { schema } from '@kbn/config-schema'; +import { schema, TypeOf } from '@kbn/config-schema'; import { getPagingProperties, @@ -19,8 +19,17 @@ import { EndpointAppContext } from '../types'; const ALERTS_ROUTE = '/api/endpoint/alerts'; +const reqSchema = schema.object({ + page_size: schema.number({ defaultValue: 10, min: 1, max: 10000 }), + page_index: schema.number({ defaultValue: 0, min: 0 }), +}); + export function registerAlertRoutes(router: IRouter, endpointAppContext: EndpointAppContext) { - const alertsHandler: RequestHandler = async (ctx, req, res) => { + const alertsHandler: RequestHandler> = async ( + ctx, + req, + res + ) => { try { const queryParams = await getPagingProperties(req, endpointAppContext); const reqBody = await kibanaRequestToAlertListQuery(queryParams, endpointAppContext); @@ -38,10 +47,7 @@ export function registerAlertRoutes(router: IRouter, endpointAppContext: Endpoin { path: ALERTS_ROUTE, validate: { - query: schema.object({ - page_size: schema.number({ defaultValue: 10, min: 1, max: 10000 }), - page_index: schema.number({ defaultValue: 0, min: 0 }), - }), + query: reqSchema, }, options: { authRequired: true }, }, @@ -52,10 +58,7 @@ export function registerAlertRoutes(router: IRouter, endpointAppContext: Endpoin { path: ALERTS_ROUTE, validate: { - body: schema.object({ - page_size: schema.number({ defaultValue: 10, min: 1, max: 10000 }), - page_index: schema.number({ defaultValue: 0, min: 0 }), - }), + body: reqSchema, }, options: { authRequired: true }, }, diff --git a/x-pack/plugins/endpoint/server/services/endpoint/alert_query_builders.ts b/x-pack/plugins/endpoint/server/services/endpoint/alert_query_builders.ts index 71dba0b544ddf..d02c3a76f0f8d 100644 --- a/x-pack/plugins/endpoint/server/services/endpoint/alert_query_builders.ts +++ b/x-pack/plugins/endpoint/server/services/endpoint/alert_query_builders.ts @@ -54,11 +54,9 @@ export const getPagingProperties = async ( pagingProperties.page_size = request.body?.page_size; } - const finalParams = { - pageSize: pagingProperties.page_size || config.alertResultListDefaultPageSize, - pageIndex: pagingProperties.page_index || config.alertResultListDefaultFirstPageIndex, - }; + const pageSize = pagingProperties.page_size || config.alertResultListDefaultPageSize; + const pageIndex = pagingProperties.page_index || config.alertResultListDefaultFirstPageIndex; + const fromIndex = pageIndex * pageSize; - finalParams.fromIndex = finalParams.pageIndex * finalParams.pageSize; - return finalParams; + return { pageSize, pageIndex, fromIndex }; };