import { Observable , combineLatest } from 'rxjs' ; import { startWith , map } from 'rxjs/operators' ; import { ServiceStatus , ServiceStatusLevels } from '../status' ; import { SavedObjectStatusMeta } from './types' ; import { OpenSearchDashboardsMigratorStatus } from './migrations/opensearch_dashboards' ; export const calculateStatus$ = ( rawMigratorStatus$ , opensearchStatus$ ) : Observable < ServiceStatus < SavedObjectStatusMeta > > => { const migratorStatus$ = rawMigratorStatus$ . pipe ( map ( ( migrationStatus ) => { if ( migrationStatus . status === 'waiting' ) { return { level : ServiceStatusLevels . unavailable , summary : `SavedObjects service is waiting to start migrations` , } ; } else if ( migrationStatus . status === 'running' ) { return { level : ServiceStatusLevels . unavailable , summary : `SavedObjects service is running migrations` , } ; } const statusCounts = { migrated : 0 , skipped : 0 } ; if ( migrationStatus . result ) { migrationStatus . result . forEach ( ( { status } ) => { statusCounts [ status ] = ( statusCounts [ status ] ?? 0 ) + 1 ; } ) ; } return { level : ServiceStatusLevels . available , summary : `SavedObjects service has completed migrations and is available` , meta : { migratedIndices : statusCounts , } , } ; } ) , startWith ( { level : ServiceStatusLevels . unavailable , summary : `SavedObjects service is waiting to start migrations` , } ) ) ; return combineLatest ( [ opensearchStatus$ , migratorStatus$ ] ) . pipe ( map ( ( [ openSearchStatus , migratorStatus ] ) => { if ( openSearchStatus . level >= ServiceStatusLevels . unavailable ) { return { level : ServiceStatusLevels . unavailable , summary : `SavedObjects service is not available without a healthy OpenSearch connection` , } ; } else if ( migratorStatus . level === ServiceStatusLevels . unavailable ) { return migratorStatus ; } else if ( openSearchStatus . level === ServiceStatusLevels . degraded ) { return { level : openSearchStatus . level , summary : `SavedObjects service is degraded due to OpenSearch: [${ openSearchStatus . summary }]` , } ; } else { return migratorStatus ; } } ) ) ; } ;