import { IRouter } from 'kibana/server' ; import { schema } from '@kbn/config-schema' ; import { ILicenseState } from '../lib' ; import { GetAlertSummaryParams } from '../rules_client' ; import { RewriteRequestCase , RewriteResponseCase , verifyAccessAndContext } from './lib' ; import { AlertingRequestHandlerContext , INTERNAL_BASE_ALERTING_API_PATH , AlertSummary , } from '../types' ; const paramSchema = schema . object ( { id : schema . string ( ) , } ) ; const querySchema = schema . object ( { date_start : schema . maybe ( schema . string ( ) ) , } ) ; const rewriteReq = ( { date_start : dateStart , ... rest } ) => ( { ... rest , dateStart , } ) ; const rewriteBodyRes = ( { ruleTypeId , muteAll , statusStartDate , statusEndDate , errorMessages , lastRun , executionDuration : { valuesWithTimestamp , ... executionDuration } , ... rest } ) => ( { ... rest , rule_type_id : ruleTypeId , mute_all : muteAll , status_start_date : statusStartDate , status_end_date : statusEndDate , error_messages : errorMessages , last_run : lastRun , execution_duration : { ... executionDuration , values_with_timestamp : valuesWithTimestamp , } , } ) ; export const getRuleAlertSummaryRoute = ( router , licenseState ) => { router . get ( { path : `${ INTERNAL_BASE_ALERTING_API_PATH }/rule/{id}/_alert_summary` , validate : { params : paramSchema , query : querySchema , } , } , router . handleLegacyErrors ( verifyAccessAndContext ( licenseState , async function ( context , req , res ) { const rulesClient = context . alerting . getRulesClient ( ) ; const { id } = req . params ; const summary = await rulesClient . getAlertSummary ( rewriteReq ( { id , ... req . query } ) ) ; return res . ok ( { body : rewriteBodyRes ( summary ) } ) ; } ) ) ) ; } ;