module Sfx { 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 ? ) : angular . IPromise < any > ; ensureInitialized ( forceRefresh ? , messageHandler ? ) : angular . IPromise < any > ; update ( raw ) : angular . IPromise < any > ; mergeHealthStateChunk ( healthChunk ) : angular . IPromise < any > ; addHealthStateFiltersForChildren ( clusterHealthChunkQueryDescription ) : IHealthStateFilter ; } export interface IDecorator { displayName ? : ( name : string ) => string ; displayValueInHtml ? : ( value : any ) => string ; } export interface IDecorators { showList ? : string [ ] ; hideList ? : string [ ] ; decorators ? : _ . Dictionary < 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 : angular . IPromise < 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 . data . $q ) ; } this . isInitialized = ! _ . isEmpty ( raw ) ; } public refresh ( messageHandler ? ) { if ( ! this . refreshingPromise ) { this . refreshingPromise = this . retrieveNewData ( messageHandler ) . then ( ( raw ) => { return this . update ( raw ) ; } ) . then ( ( ) => { return this ; } ) . finally ( ( ) => { this . refreshingPromise = null ; } ) ; } return this . refreshingPromise ; } public update ( raw ) { if ( ! _ . isEmpty ( raw ) ) { this . isInitialized = true ; this . raw = raw ; return this . data . $q . when ( this . updateInternal ( ) ) ; } return this . data . $q . when ( true ) ; } public ensureInitialized ( forceRefresh ? , messageHandler ? ) { if ( ! this . isInitialized || forceRefresh ) { return this . refresh ( messageHandler ) ; } return this . data . $q . when ( 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 < any > this . raw ; } protected retrieveNewData ( messageHandler ? ) { return this . data . $q . when ( this . raw ) ; } protected updateInternal ( ) { return this . data . $q . when ( true ) ; } protected refreshFromHealthChunkInternal ( healthChunk ) { return this . data . $q . when ( true ) ; } } export class HealthBase < T extends IRawHealth > extends DataModelBase < T > { public healthEvents : HealthEvent [ ] = [ ] ; public unhealthyEvaluations : HealthEvaluation [ ] = [ ] ; public constructor ( data , parent ? ) { super ( data , < T > { } , parent ) ; } public mergeHealthStateChunk ( healthChunk ) { this . raw . AggregatedHealthState = healthChunk . HealthState ; return this . data . $q . when ( true ) ; } protected updateInternal ( ) { this . parseCommonHealthProperties ( ) ; } protected parseCommonHealthProperties ( ) { return this . data . $q ( ( resolve , reject ) => { let healthEvents = _ . map ( this . raw . HealthEvents , rawHealthEvent => new HealthEvent ( this . data , < IRawHealthEvent > rawHealthEvent ) ) ; CollectionUtils . updateDataModelCollection ( this . healthEvents , healthEvents ) ; this . data . apps . ensureInitialized ( ) . then ( ( ) => { this . unhealthyEvaluations = Utils . getParsedHealthEvaluations ( this . raw . UnhealthyEvaluations , null , null , this . data ) ; resolve ( ) ; } ) ; } ) ; } } }