import type { ElasticsearchClient } from 'kibana/server' ; internal export const getOldestIdleActionTask = async ( client , taskManagerIndex ) : Promise < string > => { const oneDayAgo = `now-24h` ; const response = await client . search < { task : { runAt : string ; } ; } > ( { size : 1 , index : taskManagerIndex , body : { sort : [ { 'task.runAt' : { order : 'asc' } } ] , query : { bool : { filter : { bool : { must : [ { terms : { 'task.taskType' : [ 'actions:.email' , 'actions:.index' , 'actions:.pagerduty' , 'actions:.swimlane' , 'actions:.server-log' , 'actions:.slack' , 'actions:.webhook' , 'actions:.servicenow' , 'actions:.servicenow-sir' , 'actions:.jira' , 'actions:.resilient' , 'actions:.teams' , ] , } , } , { term : { type : 'task' } , } , { term : { 'task.status' : 'idle' } , } , ] , } , } , } , } , } , } , { ignore : [ 404 ] } ) ; if ( ( response . body as { error ? : { status : number ; } ; } ) . error ?. status === 404 ) { return oneDayAgo ; } else if ( response . body ?. hits ?. hits ?. length > 0 ) { const runAt = response . body . hits . hits [ 0 ] . _source ?. task ?. runAt ; return runAt ? `${ runAt }||-24h` : `0` ; } else { return oneDayAgo ; } } ;