import expect from '@kbn/expect' ; import { indexTimestamp } from '../../plugins/reporting/server/lib/store/index_timestamp' ; import { services as xpackServices } from '../functional/services' ; import { services as apiIntegrationServices } from '../api_integration/services' ; import { FtrProviderContext } from './ftr_provider_context' ; interface PDFAppCounts { app : { [ appName ] : number ; } ; layout : { [ layoutType ] : number ; } ; } export interface ReportingUsageStats { available : boolean ; enabled : boolean ; total : number ; last_7_days : { total : number ; printable_pdf : PDFAppCounts ; [ jobType ] : any ; } ; printable_pdf : PDFAppCounts ; status : any ; [ jobType ] : any ; } interface UsageStats { reporting : ReportingUsageStats ; } function removeWhitespace ( str ) { return str . replace ( /\s/g , '' ) ; } export function ReportingAPIProvider ( { getService } ) { const log = getService ( 'log' ) ; const supertest = getService ( 'supertest' ) ; const esSupertest = getService ( 'esSupertest' ) ; const retry = getService ( 'retry' ) ; return { async waitForJobToFinish ( downloadReportPath ) { log . debug ( `Waiting for job to finish: ${ downloadReportPath }` ) ; const JOB_IS_PENDING_CODE = 503 ; const statusCode = await new Promise ( ( resolve ) => { const intervalId = setInterval ( async ( ) => { const response = ( await supertest . get ( downloadReportPath ) . responseType ( 'blob' ) . set ( 'kbn-xsrf' , 'xxx' ) ) as any ; if ( response . statusCode === 503 ) { log . debug ( `Report at path ${ downloadReportPath } is pending` ) ; } else if ( response . statusCode === 200 ) { log . debug ( `Report at path ${ downloadReportPath } is complete` ) ; } else { log . debug ( `Report at path ${ downloadReportPath } returned code ${ response . statusCode }` ) ; } if ( response . statusCode !== JOB_IS_PENDING_CODE ) { clearInterval ( intervalId ) ; resolve ( response . statusCode ) ; } } , 1500 ) ; } ) ; expect ( statusCode ) . to . be ( 200 ) ; } , async expectAllJobsToFinishSuccessfully ( jobPaths ) { await Promise . all ( jobPaths . map ( async ( path ) => { await this . waitForJobToFinish ( path ) ; } ) ) ; } , async postJob ( apiPath ) { log . debug ( `ReportingAPI.postJob(${ apiPath })` ) ; const { body } = await supertest . post ( removeWhitespace ( apiPath ) ) . set ( 'kbn-xsrf' , 'xxx' ) . expect ( 200 ) ; return body . path ; } , async postJobJSON ( apiPath , jobJSON = { } ) { log . debug ( `ReportingAPI.postJobJSON((${ apiPath }): ${ JSON . stringify ( jobJSON ) })` ) ; const { body } = await supertest . post ( apiPath ) . set ( 'kbn-xsrf' , 'xxx' ) . send ( jobJSON ) ; return body . path ; } , return { Promise < Function > } async coerceReportsIntoExistingIndex ( indexName ) { log . debug ( `ReportingAPI.coerceReportsIntoExistingIndex(${ indexName })` ) ; const timestampForIndex = indexTimestamp ( 'week' , '.' ) ; await esSupertest . post ( '/_aliases' ) . send ( { actions : [ { add : { index : indexName , alias : `.reporting-${ timestampForIndex }` } , } , ] , } ) . expect ( 200 ) ; return async ( ) => { await esSupertest . post ( '/_aliases' ) . send ( { actions : [ { remove : { index : indexName , alias : `.reporting-${ timestampForIndex }` } , } , ] , } ) . expect ( 200 ) ; } ; } , async deleteAllReports ( ) { log . debug ( 'ReportingAPI.deleteAllReports' ) ; await retry . tryForTime ( 5000 , async ( ) => { await esSupertest . post ( '/.reporting*/_delete_by_query' ) . send ( { query : { match_all : { } } } ) . expect ( 200 ) ; } ) ; } , expectRecentPdfAppStats ( stats , app , count ) { expect ( stats . reporting . last_7_days . printable_pdf . app [ app ] ) . to . be ( count ) ; } , expectAllTimePdfAppStats ( stats , app , count ) { expect ( stats . reporting . printable_pdf . app [ app ] ) . to . be ( count ) ; } , expectRecentPdfLayoutStats ( stats , layout , count ) { expect ( stats . reporting . last_7_days . printable_pdf . layout [ layout ] ) . to . be ( count ) ; } , expectAllTimePdfLayoutStats ( stats , layout , count ) { expect ( stats . reporting . printable_pdf . layout [ layout ] ) . to . be ( count ) ; } , expectRecentJobTypeTotalStats ( stats , jobType , count ) { expect ( stats . reporting . last_7_days [ jobType ] . total ) . to . be ( count ) ; } , expectAllTimeJobTypeTotalStats ( stats , jobType , count ) { expect ( stats . reporting [ jobType ] . total ) . to . be ( count ) ; } , getCompletedReportCount ( stats ) { return stats . reporting . status . completed ; } , expectCompletedReportCount ( stats , count ) { expect ( this . getCompletedReportCount ( stats ) ) . to . be ( count ) ; } , getRecentPdfAppStats ( stats , app ) { return stats . reporting . last_7_days . printable_pdf . app [ app ] ; } , getAllTimePdfAppStats ( stats , app ) { return stats . reporting . printable_pdf . app [ app ] ; } , getRecentPdfLayoutStats ( stats , layout ) { return stats . reporting . last_7_days . printable_pdf . layout [ layout ] ; } , getAllTimePdfLayoutStats ( stats , layout ) { return stats . reporting . printable_pdf . layout [ layout ] ; } , getRecentJobTypeTotalStats ( stats , jobType ) { return stats . reporting . last_7_days [ jobType ] . total ; } , getAllTimeJobTypeTotalStats ( stats , jobType ) { return stats . reporting [ jobType ] . total ; } , } ; } export const services = { ... xpackServices , supertestWithoutAuth : apiIntegrationServices . supertestWithoutAuth , usageAPI : apiIntegrationServices . usageAPI , reportingAPI : ReportingAPIProvider , } ;