import { IRawHealthStateCount } from '../Models/RawDataTypes' ; import { RoutesService } from '../services/routes.service' ; import { ValueResolver , ITextAndBadge } from '../Utils/ValueResolver' ; export interface IDashboardViewModel { displayTitle : string ; largeTile : boolean ; count : number ; viewPath : string ; dataPoints : IDashboardDataPointViewModel [ ] ; onClick ( ) : void ; getDataPointTooltip ( dp ) : string ; } export interface IDashboardDataPointViewModel { state : ITextAndBadge ; title : string ; count : number ; adjustedCount : number ; } export class DashboardViewModel implements IDashboardViewModel { public count = 0 ; public viewPath : string ; public static fromHealthStateCount ( title , titleInSingular , largeTile , healthStateCount , routes ? , viewPath ? ) { const dps = [ ] ; dps . push ( new DashboardDataPointViewModel ( 'Error' , healthStateCount . ErrorCount , ValueResolver . healthStatuses [ 3 ] ) ) ; dps . push ( new DashboardDataPointViewModel ( 'Warning' , healthStateCount . WarningCount , ValueResolver . healthStatuses [ 2 ] ) ) ; dps . push ( new DashboardDataPointViewModel ( 'Healthy' , healthStateCount . OkCount , ValueResolver . healthStatuses [ 1 ] ) ) ; const data = new DashboardViewModel ( title , titleInSingular , dps , largeTile , routes , viewPath ) ; return data ; } constructor ( private title , private titleInSingular , public dataPoints = [ ] , public largeTile , private routes ? , viewPath ? ) { this . viewPath = viewPath ; this . count = dataPoints . reduce ( ( sum , d ) => sum + d . count , 0 ) ; this . adjustCount ( ) ; } public get displayTitle ( ) : string { return this . count === 1 ? this . titleInSingular : this . title ; } public getDataPointTooltip ( dp ) { return `${ dp . title }: ${ ( ( dp . count / this . count ) * 100 ) . toFixed ( 1 ) }%` ; } public onClick ( ) { if ( this . viewPath && this . routes ) { this . routes . navigate ( ( ) => this . viewPath ) ; } } private adjustCount ( ) { this . dataPoints . forEach ( dp => { if ( dp . count === 0 ) { dp . adjustedCount = 0 ; } else { dp . adjustedCount = Math . max ( this . count * 0.02 , dp . count ) ; } } ) ; } } export class DashboardDataPointViewModel implements IDashboardDataPointViewModel { public adjustedCount = 0 ; constructor ( public title , public count , public state ) { } }