Files
gridpilot.gg/apps/website/lib/display-objects/HealthMetricDisplay.ts
Marc Mintel fb1221701d
Some checks failed
Contract Testing / contract-tests (push) Failing after 6m7s
Contract Testing / contract-snapshot (push) Failing after 4m46s
add tests
2026-01-22 11:52:42 +01:00

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)}%`;
}
}