import { Controller, Get, Param, Logger } from "@nestjs/common"; import { IntegrationHealthService } from "./integration-health.service"; import { SelfHostedClientsService } from "./clients/self-hosted-clients.service"; @Controller("integrations/health") export class IntegrationHealthController { private readonly logger = new Logger(IntegrationHealthController.name); constructor( private readonly healthService: IntegrationHealthService, private readonly selfHosted: SelfHostedClientsService, ) {} @Get() async getHealth() { const results = await this.healthService.checkAll(); const healthyCount = results.filter((r) => r.status === "healthy").length; const degradedCount = results.filter((r) => r.status === "degraded").length; const unhealthyCount = results.filter((r) => r.status === "unhealthy").length; return { timestamp: new Date().toISOString(), summary: { total: results.length, healthy: healthyCount, degraded: degradedCount, unhealthy: unhealthyCount, }, integrations: results, }; } @Get("status") async getCachedHealth() { const results = this.healthService.getHealthResults(); const healthyCount = results.filter((r) => r.status === "healthy").length; return { timestamp: new Date().toISOString(), summary: { total: results.length, healthy: healthyCount, degraded: results.filter((r) => r.status === "degraded").length, unhealthy: results.filter((r) => r.status === "unhealthy").length, }, integrations: results, }; } @Get(":provider") async getProviderHealth(@Param("provider") provider: string) { return ( (await this.healthService.getIntegrationHealth(provider)) || { provider, status: "unknown", lastChecked: new Date().toISOString(), latencyMs: 0, error: "Provider not found in health cache", } ); } @Get("webhooks/recent") async getWebhookDeliveries() { return { deliveries: this.healthService.getWebhookDeliveries(100), totalRecorded: this.healthService.getWebhookDeliveries(10000).length, }; } @Get("metrics/client") async getClientMetrics() { return { provider: "self-hosted-clients", timestamp: new Date().toISOString(), metrics: this.selfHosted.getMetrics(), }; } @Get("metrics/webhook") async getWebhookMetrics() { const deliveries = this.healthService.getWebhookDeliveries(500); const providers = [...new Set(deliveries.map((d) => d.provider))]; const metrics = providers.map((provider) => { const providerDeliveries = deliveries.filter((d) => d.provider === provider); const total = providerDeliveries.length; const failed = providerDeliveries.filter((d) => d.status === "failed").length; const avgLatency = total > 0 ? providerDeliveries.reduce((s, d) => s + d.latencyMs, 0) / total : 0; return { provider, totalDeliveries: total, failedDeliveries: failed, successRate: total > 0 ? ((total - failed) / total) * 100 : 100, avgLatencyMs: Math.round(avgLatency), }; }); return { timestamp: new Date().toISOString(), metrics, }; } }