import { IScopedClusterClient } from 'kibana/server' ; import { ML_NOTIFICATION_INDEX_PATTERN } from '../../../common/constants/index_patterns' ; import { JobMessage } from '../../../common/types/audit_message' ; const SIZE = 50 ; interface Message { _index : string ; _type : string ; _id : string ; _score : null | number ; _source : JobMessage ; sort ? : any ; } interface BoolQuery { bool : { [ key ] : any ; } ; } export function analyticsAuditMessagesProvider ( { asInternalUser } ) { async function getAnalyticsAuditMessages ( analyticsId ) { const query = { bool : { filter : [ { bool : { must_not : { term : { level : 'activity' , } , } , must : { term : { job_type : 'data_frame_analytics' , } , } , } , } , ] , } , } ; if ( analyticsId !== undefined ) { query . bool . filter . push ( { bool : { should : [ { term : { job_id : '' , } , } , { term : { job_id : analyticsId , } , } , ] , } , } ) ; } const { body } = await asInternalUser . search ( { index : ML_NOTIFICATION_INDEX_PATTERN , ignore_unavailable : true , size : SIZE , body : { sort : [ { timestamp : { order : 'desc' } } , { job_id : { order : 'asc' } } ] , query , } , } ) ; let messages = [ ] ; if ( typeof body . hits . total !== 'number' && body . hits . total ?. value ) { messages = ( body . hits . hits as Message [ ] ) . map ( ( hit ) => hit . _source ) ; messages . reverse ( ) ; } return messages ; } return { getAnalyticsAuditMessages , } ; }