-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a937a2
commit ff64031
Showing
3 changed files
with
94 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
x-pack/plugins/event_log/server/routes/get_events_summary_by_saved_object_ids.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* 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 { schema, TypeOf } from '@kbn/config-schema'; | ||
import type { | ||
KibanaRequest, | ||
IKibanaResponse, | ||
KibanaResponseFactory, | ||
Logger, | ||
} from 'src/core/server'; | ||
import type { EventLogRouter, EventLogRequestHandlerContext } from '../types'; | ||
|
||
import { BASE_EVENT_LOG_API_PATH } from '../../common'; | ||
|
||
const optionalDateFieldSchema = schema.maybe( | ||
schema.string({ | ||
validate(value) { | ||
if (isNaN(Date.parse(value))) { | ||
return 'Invalid Date'; | ||
} | ||
}, | ||
}) | ||
); | ||
|
||
const optionsSchema = schema.object({ | ||
start: optionalDateFieldSchema, | ||
end: optionalDateFieldSchema, | ||
}); | ||
|
||
const paramSchema = schema.object({ | ||
type: schema.string(), | ||
}); | ||
|
||
const bodySchema = schema.object({ | ||
ids: schema.arrayOf(schema.string(), { defaultValue: [] }), | ||
aggs: schema.recordOf(schema.string(), schema.any()), | ||
}); | ||
|
||
export const getEventsSummaryBySavedObjectIdsRoute = ( | ||
router: EventLogRouter, | ||
systemLogger: Logger | ||
) => { | ||
router.post( | ||
{ | ||
path: `${BASE_EVENT_LOG_API_PATH}/{type}/saved_object_summary`, | ||
validate: { | ||
params: paramSchema, | ||
query: optionsSchema, | ||
body: bodySchema, | ||
}, | ||
}, | ||
router.handleLegacyErrors(async function ( | ||
context: EventLogRequestHandlerContext, | ||
req: KibanaRequest< | ||
TypeOf<typeof paramSchema>, | ||
TypeOf<typeof optionsSchema> | undefined, | ||
TypeOf<typeof bodySchema> | ||
>, | ||
res: KibanaResponseFactory | ||
): Promise<IKibanaResponse> { | ||
if (!context.eventLog) { | ||
return res.badRequest({ body: 'RouteHandlerContext is not registered for eventLog' }); | ||
} | ||
const eventLogClient = context.eventLog.getEventLogClient(); | ||
const { | ||
params: { type }, | ||
body: { ids, aggs }, | ||
query, | ||
} = req; | ||
|
||
try { | ||
return res.ok({ | ||
body: await eventLogClient.getEventsSummaryBySavedObjectIds( | ||
type, | ||
ids, | ||
aggs, | ||
query?.start, | ||
query?.end | ||
), | ||
}); | ||
} catch (err) { | ||
const call = `getEventsSummaryBySavedObjectIdsRoute([${ids}], ${JSON.stringify(query)})`; | ||
systemLogger.debug(`error calling eventLog ${call}: ${err.message}`); | ||
return res.notFound(); | ||
} | ||
}) | ||
); | ||
}; |