File size: 3,307 Bytes
6abfd9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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,
    };
  }
}