import { i18n } from '@kbn/i18n' ; import * as Rx from 'rxjs' ; import { catchError , map } from 'rxjs/operators' ; import { NotificationsSetup , ThemeServiceStart } from 'src/core/public' ; import { JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY , JOB_STATUSES } from '../../common/constants' ; import { JobId , JobSummary , JobSummarySet } from '../../common/types' ; import { getFailureToast , getGeneralErrorToast , getSuccessToast , getWarningFormulasToast , getWarningMaxSizeToast , } from '../notifier' ; import { Job } from './job' ; import { ReportingAPIClient } from './reporting_api_client' ; function updateStored ( jobIds ) { sessionStorage . setItem ( JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY , JSON . stringify ( jobIds ) ) ; } function getReportStatus ( src ) { return { id : src . id , status : src . status , title : src . title , jobtype : src . jobtype , maxSizeReached : src . max_size_reached , csvContainsFormulas : src . csv_contains_formulas , } ; } export class ReportingNotifierStreamHandler { constructor ( private notifications , private apiClient , private theme ) { } public showNotifications ( { completed : completedJobs , failed : failedJobs , } ) { const showNotificationsAsync = async ( ) => { for ( const job of completedJobs ) { if ( job . csvContainsFormulas ) { this . notifications . toasts . addWarning ( getWarningFormulasToast ( job , this . apiClient . getManagementLink , this . apiClient . getDownloadLink , this . theme ) ) ; } else if ( job . maxSizeReached ) { this . notifications . toasts . addWarning ( getWarningMaxSizeToast ( job , this . apiClient . getManagementLink , this . apiClient . getDownloadLink , this . theme ) ) ; } else { this . notifications . toasts . addSuccess ( getSuccessToast ( job , this . apiClient . getManagementLink , this . apiClient . getDownloadLink , this . theme ) ) ; } } for ( const job of failedJobs ) { const errorText = await this . apiClient . getError ( job . id ) ; this . notifications . toasts . addDanger ( getFailureToast ( errorText , job , this . apiClient . getManagementLink , this . theme ) ) ; } return { completed : completedJobs , failed : failedJobs } ; } ; return Rx . from ( showNotificationsAsync ( ) ) ; } public findChangedStatusJobs ( storedJobs ) { return Rx . from ( this . apiClient . findForJobIds ( storedJobs ) ) . pipe ( map ( ( jobs ) => { const completedJobs = [ ] ; const failedJobs = [ ] ; const pending = [ ] ; for ( const job of jobs ) { const { id : jobId , status : jobStatus } = job ; if ( storedJobs . includes ( jobId ) ) { if ( jobStatus === JOB_STATUSES . COMPLETED || jobStatus === JOB_STATUSES . WARNINGS ) { completedJobs . push ( getReportStatus ( job ) ) ; } else if ( jobStatus === JOB_STATUSES . FAILED ) { failedJobs . push ( getReportStatus ( job ) ) ; } else { pending . push ( jobId ) ; } } } updateStored ( pending ) ; return { completed : completedJobs , failed : failedJobs } ; } ) , catchError ( ( err ) => { this . notifications . toasts . addDanger ( getGeneralErrorToast ( i18n . translate ( 'xpack.reporting.publicNotifier.httpErrorMessage' , { defaultMessage : 'Could not check Reporting job status!' , } ) , err , this . theme ) ) ; window . console . error ( err ) ; return Rx . of ( { completed : [ ] , failed : [ ] } ) ; } ) ) ; } }