import { Plugin , CoreSetup , CoreStart , RequestHandlerContext , KibanaRequest , KibanaResponseFactory , IKibanaResponse , } from 'kibana/server' ; import { Subject } from 'rxjs' ; import { first } from 'rxjs/operators' ; import { schema } from '@kbn/config-schema' ; import { TaskManagerStartContract } from '../../../../../../../plugins/task_manager/server' ; export interface SampleTaskManagerFixtureStartDeps { taskManager : TaskManagerStartContract ; } const taskManagerQuery = ( ... filters ) => ( { bool : { filter : { bool : { must : filters , } , } , } , } ) ; const tasksForAlerting = { term : { 'task.scope' : 'alerting' , } , } ; const taskByIdQuery = ( id ) => ( { ids : { values : [ `task:${ id }` ] , } , } ) ; export class SampleTaskManagerFixturePlugin implements Plugin < void , void , { } , SampleTaskManagerFixtureStartDeps > { taskManagerStart$ : Subject < TaskManagerStartContract > = new Subject < TaskManagerStartContract > ( ) ; taskManagerStart : Promise < TaskManagerStartContract > = this . taskManagerStart$ . pipe ( first ( ) ) . toPromise ( ) ; public setup ( core ) { const router = core . http . createRouter ( ) ; router . get ( { path : '/api/alerting_tasks/{taskId}' , validate : { params : schema . object ( { taskId : schema . string ( ) , } ) , } , } , async ( context , req , res ) : Promise < IKibanaResponse < any > > => { try { const taskManager = await this . taskManagerStart ; return res . ok ( { body : await taskManager . fetch ( { query : taskManagerQuery ( tasksForAlerting , taskByIdQuery ( req . params . taskId ) ) , } ) , } ) ; } catch ( err ) { return res . badRequest ( { body : err } ) ; } } ) ; router . get ( { path : `/api/ensure_tasks_index_refreshed` , validate : { } , } , async function ( context , req , res ) : Promise < IKibanaResponse < any > > { await context . core . elasticsearch . client . asInternalUser . indices . refresh ( { index : '.kibana_task_manager' , } ) ; return res . ok ( { body : { } } ) ; } ) ; } public start ( core , { taskManager } ) { this . taskManagerStart$ . next ( taskManager ) ; this . taskManagerStart$ . complete ( ) ; } public stop ( ) { } }