import { transformIdParamSchema , TransformIdParamSchema } from '../../../common/api_schemas/common' ; import { AuditMessage } from '../../../common/types/messages' ; import { RouteDependencies } from '../../types' ; import { addBasePath } from '../index' ; import { wrapError , wrapEsError } from './error_utils' ; const ML_DF_NOTIFICATION_INDEX_PATTERN = '.transform-notifications-read' ; const SIZE = 500 ; interface BoolQuery { bool : { [ key ] : any ; } ; } export function registerTransformsAuditMessagesRoutes ( { router , license } ) { apiGroup api apiName apiDescription apiSchema router . get < TransformIdParamSchema , undefined , undefined > ( { path : addBasePath ( 'transforms/{transformId}/messages' ) , validate : { params : transformIdParamSchema } , } , license . guardApiRoute < TransformIdParamSchema , undefined , undefined > ( async ( ctx , req , res ) => { const { transformId } = req . params ; const query = { bool : { filter : [ { bool : { must_not : { term : { level : 'activity' , } , } , } , } , ] , } , } ; if ( transformId !== undefined ) { query . bool . filter . push ( { bool : { should : [ { term : { transform_id : '' , } , } , { term : { transform_id : transformId , } , } , ] , } , } ) ; } try { const { body : resp } = await ctx . core . elasticsearch . client . asCurrentUser . search < AuditMessage > ( { index : ML_DF_NOTIFICATION_INDEX_PATTERN , ignore_unavailable : true , size : SIZE , body : { sort : [ { timestamp : { order : 'desc' as const } } , { transform_id : { order : 'asc' as const } } , ] , query , } , } ) ; let messages = [ ] ; if ( ( typeof resp . hits . total === 'number' && resp . hits . total > 0 ) || ( typeof resp . hits . total === 'object' && resp . hits . total . value > 0 ) ) { messages = resp . hits . hits . map ( ( hit ) => hit . _source ! ) ; messages . reverse ( ) ; } return res . ok ( { body : messages } ) ; } catch ( e ) { return res . customError ( wrapError ( wrapEsError ( e ) ) ) ; } } ) ) ; }