< Summary

Information
Class: Chronicis.Api.Services.SystemHealthService
Assembly: Chronicis.Api
File(s): /home/runner/work/chronicis/chronicis/src/Chronicis.Api/Services/SystemHealthService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 51
Coverable lines: 51
Total lines: 90
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetSystemHealthAsync()100%210%
DetermineOverallStatus(...)0%2040%

File(s)

/home/runner/work/chronicis/chronicis/src/Chronicis.Api/Services/SystemHealthService.cs

#LineLine coverage
 1using Chronicis.Shared.DTOs;
 2
 3namespace Chronicis.Api.Services;
 4
 5public interface ISystemHealthService
 6{
 7    Task<SystemHealthStatusDto> GetSystemHealthAsync();
 8}
 9
 10public class SystemHealthService : ISystemHealthService
 11{
 12    private readonly DatabaseHealthCheckService _databaseHealth;
 13    private readonly AzureOpenAIHealthCheckService _azureOpenAIHealth;
 14    private readonly BlobStorageHealthCheckService _blobStorageHealth;
 15    private readonly Auth0HealthCheckService _auth0Health;
 16    private readonly Open5eHealthCheckService _open5eHealth;
 17    private readonly ILogger<SystemHealthService> _logger;
 18
 019    public SystemHealthService(
 020        DatabaseHealthCheckService databaseHealth,
 021        AzureOpenAIHealthCheckService azureOpenAIHealth,
 022        BlobStorageHealthCheckService blobStorageHealth,
 023        Auth0HealthCheckService auth0Health,
 024        Open5eHealthCheckService open5eHealth,
 025        ILogger<SystemHealthService> logger)
 26    {
 027        _databaseHealth = databaseHealth;
 028        _azureOpenAIHealth = azureOpenAIHealth;
 029        _blobStorageHealth = blobStorageHealth;
 030        _auth0Health = auth0Health;
 031        _open5eHealth = open5eHealth;
 032        _logger = logger;
 033    }
 34
 35    public async Task<SystemHealthStatusDto> GetSystemHealthAsync()
 36    {
 037        var timestamp = DateTime.UtcNow;
 038        _logger.LogInformation("Starting system health check");
 39
 40        // Run all health checks in parallel
 041        var healthCheckTasks = new[]
 042        {
 043            _databaseHealth.CheckHealthAsync("Database", ServiceKeys.Database),
 044            _azureOpenAIHealth.CheckHealthAsync("Azure OpenAI", ServiceKeys.AzureOpenAI),
 045            _blobStorageHealth.CheckHealthAsync("Document Storage", ServiceKeys.BlobStorage),
 046            _auth0Health.CheckHealthAsync("Auth0", ServiceKeys.Auth0),
 047            _open5eHealth.CheckHealthAsync("Open5e API", ServiceKeys.Open5e)
 048        };
 49
 50        // Add API self-check
 051        var apiHealthTask = Task.FromResult(new ServiceHealthDto
 052        {
 053            Name = "API",
 054            ServiceKey = ServiceKeys.Api,
 055            Status = HealthStatus.Healthy,
 056            Message = "API is responding",
 057            ResponseTimeMs = 0,
 058            CheckedAt = timestamp
 059        });
 60
 061        var allTasks = healthCheckTasks.Concat(new[] { apiHealthTask }).ToArray();
 062        var results = await Task.WhenAll(allTasks);
 63
 64        // Determine overall status
 065        var overallStatus = DetermineOverallStatus(results);
 66
 067        _logger.LogInformation("System health check completed. Overall status: {Status}", overallStatus);
 68
 069        return new SystemHealthStatusDto
 070        {
 071            Timestamp = timestamp,
 072            OverallStatus = overallStatus,
 073            Services = results.ToList()
 074        };
 075    }
 76
 77    private static string DetermineOverallStatus(ServiceHealthDto[] services)
 78    {
 079        var hasUnhealthy = services.Any(s => s.Status == HealthStatus.Unhealthy);
 080        var hasDegraded = services.Any(s => s.Status == HealthStatus.Degraded);
 81
 082        if (hasUnhealthy)
 083            return HealthStatus.Unhealthy;
 84
 085        if (hasDegraded)
 086            return HealthStatus.Degraded;
 87
 088        return HealthStatus.Healthy;
 89    }
 90}