import { isEqual } from 'lodash'; import React from 'react'; import type { IInstanceCounts } from '../domain'; import type { Placement } from '../presentation'; import { Tooltip } from '../presentation'; import './healthCounts.less'; export interface IHealthCountsProps { className?: string; container: IInstanceCounts; additionalLegendText?: string; legendPlacement?: Placement; } export interface IHealthCountsState { percentLabel: string; statusClass: string; total: number; } export class HealthCounts extends React.Component { public static defaultProps: Partial = { legendPlacement: 'top', container: {} as IInstanceCounts, }; constructor(props: IHealthCountsProps) { super(props); this.state = this.calculatePercent(props.container); } private calculatePercent(container: IInstanceCounts): IHealthCountsState { container = container || ({} as IInstanceCounts); const up = container.up || 0; const down = container.down || 0; const succeeded = container.succeeded || 0; const failed = container.failed || 0; const unknown = container.unknown || 0; const starting = container.starting || 0; const total = container.total || up + down + unknown + starting + succeeded + failed; const percent = total ? Math.floor(((up + succeeded) * 100) / total) : undefined; const percentLabel = percent === undefined ? 'n/a' : percent + '%'; const statusClass = percent === undefined ? 'disabled' : percent === 100 ? 'healthy' : percent < 100 && percent > 0 ? 'unhealthy' : percent === 0 ? 'dead' : 'disabled'; return { percentLabel, statusClass, total }; } public componentWillReceiveProps(nextProps: IHealthCountsProps): void { if (!isEqual(nextProps.container, this.props.container)) { this.setState(this.calculatePercent(nextProps.container)); } } public render(): React.ReactElement { const legend = (
Up
Down
In transition or no status reported
Out of Service
Terminated successfully
Terminated unsuccessfully
{this.props.additionalLegendText}
); const container = this.props.container; const percentLabel = this.state.percentLabel; let hasValue = false; const counts: Array> = []; if (container.up) { counts.push( {' '} {container.up} , ); hasValue = true; } if (container.down && container.down !== container.missingHealthCount) { if (hasValue) { counts.push( / ); } counts.push( {' '} {container.down} , ); hasValue = true; } if (container.unknown || container.starting) { if (hasValue) { counts.push( / ); } counts.push( {' '} {container.unknown + container.starting} , ); hasValue = true; } if (container.outOfService) { if (hasValue) { counts.push( / ); } counts.push( {' '} {container.outOfService} , ); hasValue = true; } if (container.succeeded) { if (hasValue) { counts.push( / ); } counts.push( {' '} {container.succeeded} , ); hasValue = true; } if (container.failed) { if (hasValue) { counts.push( / ); } counts.push( {' '} {container.failed} , ); } const className = this.props.className || ''; if (percentLabel !== 'n/a') { return ( {counts} {container.unknown !== this.state.total && ( {' '} : {percentLabel} )} ); } else if (container.outOfService) { return ( {container.outOfService} ); } else { return null; } } }