Files
gridpilot.gg/apps/website/lib/builders/view-data/HealthViewDataBuilder.ts
Marc Mintel 09632d004d
Some checks failed
CI / lint-typecheck (pull_request) Failing after 12s
CI / tests (pull_request) Has been skipped
CI / contract-tests (pull_request) Has been skipped
CI / e2e-tests (pull_request) Has been skipped
CI / comment-pr (pull_request) Has been skipped
CI / commit-types (pull_request) Has been skipped
code quality
2026-01-26 22:16:33 +01:00

111 lines
4.5 KiB
TypeScript

import type { ViewDataBuilder } from '@/lib/contracts/builders/ViewDataBuilder';
import { HealthAlertFormatter } from '@/lib/formatters/HealthAlertFormatter';
import { HealthComponentFormatter } from '@/lib/formatters/HealthComponentFormatter';
import { HealthMetricFormatter } from '@/lib/formatters/HealthMetricFormatter';
import { HealthStatusFormatter } from '@/lib/formatters/HealthStatusFormatter';
interface HealthDTO {
status: 'ok' | 'degraded' | 'error' | 'unknown';
timestamp?: string;
uptime?: number;
responseTime?: number;
errorRate?: number;
lastCheck?: string;
checksPassed?: number;
checksFailed?: number;
components?: Array<{
name: string;
status: 'ok' | 'degraded' | 'error' | 'unknown';
lastCheck?: string;
responseTime?: number;
errorRate?: number;
}>;
alerts?: Array<{
id: string;
type: 'critical' | 'warning' | 'info';
title: string;
message: string;
timestamp: string;
}>;
}
import type { HealthAlert, HealthComponent, HealthMetrics, HealthStatus, HealthViewData } from '@/lib/view-data/HealthViewData';
export type { HealthDTO };
export class HealthViewDataBuilder {
public static build(apiDto: HealthDTO): HealthViewData {
const now = new Date();
const lastUpdated = apiDto.timestamp || now.toISOString();
// Build overall status
const overallStatus: HealthStatus = {
status: apiDto.status,
timestamp: lastUpdated,
formattedTimestamp: HealthStatusFormatter.formatTimestamp(lastUpdated),
relativeTime: HealthStatusFormatter.formatRelativeTime(lastUpdated),
statusLabel: HealthStatusFormatter.formatStatusLabel(apiDto.status),
statusColor: HealthStatusFormatter.formatStatusColor(apiDto.status),
statusIcon: HealthStatusFormatter.formatStatusIcon(apiDto.status),
};
// Build metrics
const metrics: HealthMetrics = {
uptime: HealthMetricFormatter.formatUptime(apiDto.uptime),
responseTime: HealthMetricFormatter.formatResponseTime(apiDto.responseTime),
errorRate: HealthMetricFormatter.formatErrorRate(apiDto.errorRate),
lastCheck: apiDto.lastCheck || lastUpdated,
formattedLastCheck: HealthMetricFormatter.formatTimestamp(apiDto.lastCheck || lastUpdated),
checksPassed: apiDto.checksPassed || 0,
checksFailed: apiDto.checksFailed || 0,
totalChecks: (apiDto.checksPassed || 0) + (apiDto.checksFailed || 0),
successRate: HealthMetricFormatter.formatSuccessRate(apiDto.checksPassed, apiDto.checksFailed),
};
// Build components
const components: HealthComponent[] = (apiDto.components || []).map((component: { name: string; status: 'ok' | 'degraded' | 'error' | 'unknown'; lastCheck?: string; responseTime?: number; errorRate?: number; }) => ({
name: component.name,
status: component.status,
statusLabel: HealthComponentFormatter.formatStatusLabel(component.status),
statusColor: HealthComponentFormatter.formatStatusColor(component.status),
statusIcon: HealthComponentFormatter.formatStatusIcon(component.status),
lastCheck: component.lastCheck || lastUpdated,
formattedLastCheck: HealthComponentFormatter.formatTimestamp(component.lastCheck || lastUpdated),
responseTime: HealthMetricFormatter.formatResponseTime(component.responseTime),
errorRate: HealthMetricFormatter.formatErrorRate(component.errorRate),
}));
// Build alerts
const alerts: HealthAlert[] = (apiDto.alerts || []).map((alert: { id: string; type: 'critical' | 'warning' | 'info'; title: string; message: string; timestamp: string; }) => ({
id: alert.id,
type: alert.type,
title: alert.title,
message: alert.message,
timestamp: alert.timestamp,
formattedTimestamp: HealthAlertFormatter.formatTimestamp(alert.timestamp),
relativeTime: HealthAlertFormatter.formatRelativeTime(alert.timestamp),
severity: HealthAlertFormatter.formatSeverity(alert.type),
severityColor: HealthAlertFormatter.formatSeverityColor(alert.type),
}));
// Calculate derived fields
const hasAlerts = alerts.length > 0;
const hasDegradedComponents = components.some((c) => c.status === 'degraded');
const hasErrorComponents = components.some((c) => c.status === 'error');
return {
overallStatus,
metrics,
components,
alerts,
hasAlerts,
hasDegradedComponents,
hasErrorComponents,
lastUpdated,
formattedLastUpdated: HealthStatusFormatter.formatTimestamp(lastUpdated),
};
}
}
HealthViewDataBuilder satisfies ViewDataBuilder<HealthDTO, HealthViewData>;