Skip to content

Commit

Permalink
Address comments - Round 3
Browse files Browse the repository at this point in the history
  • Loading branch information
madirey committed Feb 4, 2020
1 parent 426502c commit 28f7baa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
24 changes: 20 additions & 4 deletions x-pack/plugins/endpoint/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 13 additions & 10 deletions x-pack/plugins/endpoint/server/routes/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<unknown, unknown, unknown> = async (ctx, req, res) => {
const alertsHandler: RequestHandler<unknown, TypeOf<typeof reqSchema>> = async (
ctx,
req,
res
) => {
try {
const queryParams = await getPagingProperties(req, endpointAppContext);
const reqBody = await kibanaRequestToAlertListQuery(queryParams, endpointAppContext);
Expand All @@ -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 },
},
Expand All @@ -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 },
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};

0 comments on commit 28f7baa

Please sign in to comment.