62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
/**
|
|
* Health Metric Display Object
|
|
*
|
|
* Provides formatting and display logic for health metrics.
|
|
* This display object isolates UI-specific formatting from business logic.
|
|
*/
|
|
|
|
export class HealthMetricDisplay {
|
|
static formatUptime(uptime?: number): string {
|
|
if (uptime === undefined || uptime === null) return 'N/A';
|
|
if (uptime < 0) return 'N/A';
|
|
|
|
// Format as percentage with 2 decimal places
|
|
return `${uptime.toFixed(2)}%`;
|
|
}
|
|
|
|
static formatResponseTime(responseTime?: number): string {
|
|
if (responseTime === undefined || responseTime === null) return 'N/A';
|
|
if (responseTime < 0) return 'N/A';
|
|
|
|
// Format as milliseconds with appropriate units
|
|
if (responseTime < 1000) {
|
|
return `${responseTime.toFixed(0)}ms`;
|
|
} else if (responseTime < 60000) {
|
|
return `${(responseTime / 1000).toFixed(2)}s`;
|
|
} else {
|
|
return `${(responseTime / 60000).toFixed(2)}m`;
|
|
}
|
|
}
|
|
|
|
static formatErrorRate(errorRate?: number): string {
|
|
if (errorRate === undefined || errorRate === null) return 'N/A';
|
|
if (errorRate < 0) return 'N/A';
|
|
|
|
// Format as percentage with 2 decimal places
|
|
return `${errorRate.toFixed(2)}%`;
|
|
}
|
|
|
|
static formatTimestamp(timestamp: string): string {
|
|
const date = new Date(timestamp);
|
|
return date.toLocaleString('en-US', {
|
|
year: 'numeric',
|
|
month: 'short',
|
|
day: 'numeric',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
second: '2-digit',
|
|
});
|
|
}
|
|
|
|
static formatSuccessRate(checksPassed?: number, checksFailed?: number): string {
|
|
const passed = checksPassed || 0;
|
|
const failed = checksFailed || 0;
|
|
const total = passed + failed;
|
|
|
|
if (total === 0) return 'N/A';
|
|
|
|
const successRate = (passed / total) * 100;
|
|
return `${successRate.toFixed(1)}%`;
|
|
}
|
|
}
|