import { IResponseMessageHandler } from 'src/app/Common/ResponseMessageHandlers' ; import { ITextAndBadge , ValueResolver } from 'src/app/Utils/ValueResolver' ; import { DataService } from 'src/app/services/data.service' ; import { Observable , of , Subject } from 'rxjs' ; import { IHealthStateChunk , IClusterHealthChunkQueryDescription , IHealthStateFilter } from '../HealthChunkRawDataTypes' ; import { mergeMap , map } from 'rxjs/operators' ; import { ActionCollection } from '../ActionCollection' ; export interface IDataModel < T > { raw : T ; isInitialized : boolean ; isRefreshing : boolean ; parent : any ; uniqueId : string ; id : string ; name : string ; healthState : ITextAndBadge ; viewPath : string ; actions : ActionCollection ; refresh ( messageHandler ? ) : Observable < any > ; ensureInitialized ( forceRefresh ? , messageHandler ? ) : Observable < any > ; update ( raw ) : Observable < any > ; mergeHealthStateChunk ( healthChunk ) : Observable < any > ; addHealthStateFiltersForChildren ( clusterHealthChunkQueryDescription ) : IHealthStateFilter ; } export interface IDecorator { displayName ? : ( name : string ) => string ; displayValueInHtml ? : ( value : any ) => string ; } export interface IDecorators { showList ? : string [ ] ; hideList ? : string [ ] ; decorators ? : Record < string , IDecorator > ; } export class DataModelBase < T > implements IDataModel < T > { public isInitialized : boolean ; public actions : ActionCollection ; public raw : T ; public parent : any ; protected valueResolver : ValueResolver ; private refreshingPromise : Subject < any > ; public get isRefreshing ( ) : boolean { return ! ! this . refreshingPromise ; } public get uniqueId ( ) : string { return this . id ; } public get id ( ) : string { return this . rawAny . Id || this . name ; } public get name ( ) : string { return this . rawAny . Name || '' ; } public get viewPath ( ) : string { return '' ; } public get healthState ( ) : ITextAndBadge { if ( this . rawAny . HealthState ) { return this . valueResolver . resolveHealthStatus ( this . rawAny . HealthState ) ; } else if ( this . rawAny . AggregatedHealthState ) { return this . valueResolver . resolveHealthStatus ( this . rawAny . AggregatedHealthState ) ; } return ValueResolver . unknown ; } public constructor ( public data , raw ? , parent ? ) { this . raw = raw ; this . parent = parent ; this . valueResolver = new ValueResolver ( ) ; if ( this . data ) { this . actions = new ActionCollection ( this . data . telemetry ) ; } this . isInitialized = ! ! raw ; } public refresh ( messageHandler ? ) { if ( ! this . refreshingPromise ) { this . refreshingPromise = new Subject < any > ( ) ; this . retrieveNewData ( messageHandler ) . pipe ( mergeMap ( ( raw ) => { return this . update ( raw ) ; } ) ) . subscribe ( data => { this . refreshingPromise . next ( this ) ; this . refreshingPromise . complete ( ) ; this . refreshingPromise = null ; } , err => { this . refreshingPromise . error ( this ) ; this . refreshingPromise = null ; } ) ; } return this . refreshingPromise ? this . refreshingPromise . asObservable ( ) : of ( null ) ; } public update ( raw ) { if ( raw ) { this . isInitialized = true ; this . raw = raw ; return of ( this . updateInternal ( ) ) ; } return of ( true ) ; } public ensureInitialized ( forceRefresh ? , messageHandler ? ) { if ( ! this . isInitialized || forceRefresh ) { return this . refresh ( messageHandler ) . pipe ( map ( ( ) => this ) ) ; } return of ( this ) ; } public addHealthStateFiltersForChildren ( clusterHealthChunkQueryDescription ) { return null ; } public mergeHealthStateChunk ( healthChunk ) { if ( this . rawAny . HealthState ) { this . rawAny . HealthState = healthChunk . HealthState ; } else if ( this . rawAny . AggregatedHealthState ) { this . rawAny . AggregatedHealthState = healthChunk . HealthState ; } return this . refreshFromHealthChunkInternal ( healthChunk ) ; } protected get rawAny ( ) : any { return this . raw as any ; } protected retrieveNewData ( messageHandler ? ) { return of ( this . raw ) ; } protected updateInternal ( ) { return of ( true ) ; } protected refreshFromHealthChunkInternal ( healthChunk ) { return of ( true ) ; } }