< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
DetermineOverallStatus(...)100%44100%

File(s)

/home/runner/work/chronicis/chronicis/src/Chronicis.Api/Services/HealthChecks/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 sealed 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 ILogger<SystemHealthService> _logger;
 17
 18    public SystemHealthService(
 19        DatabaseHealthCheckService databaseHealth,
 20        AzureOpenAIHealthCheckService azureOpenAIHealth,
 21        BlobStorageHealthCheckService blobStorageHealth,
 22        Auth0HealthCheckService auth0Health,
 23        ILogger<SystemHealthService> logger)
 24    {
 125        _databaseHealth = databaseHealth;
 126        _azureOpenAIHealth = azureOpenAIHealth;
 127        _blobStorageHealth = blobStorageHealth;
 128        _auth0Health = auth0Health;
 129        _logger = logger;
 130    }
 31
 32    public async Task<SystemHealthStatusDto> GetSystemHealthAsync()
 33    {
 34        var timestamp = DateTime.UtcNow;
 35        _logger.LogTraceSanitized("Starting system health check");
 36
 37        // Run all health checks in parallel
 38        var healthCheckTasks = new[]
 39        {
 40            _databaseHealth.CheckHealthAsync("Database", ServiceKeys.Database),
 41            _azureOpenAIHealth.CheckHealthAsync("Azure OpenAI", ServiceKeys.AzureOpenAI),
 42            _blobStorageHealth.CheckHealthAsync("Document Storage", ServiceKeys.BlobStorage),
 43            _auth0Health.CheckHealthAsync("Auth0", ServiceKeys.Auth0)
 44        };
 45
 46        // Add API self-check
 47        var apiHealthTask = Task.FromResult(new ServiceHealthDto
 48        {
 49            Name = "API",
 50            ServiceKey = ServiceKeys.Api,
 51            Status = HealthStatus.Healthy,
 52            Message = "API is responding",
 53            ResponseTimeMs = 0,
 54            CheckedAt = timestamp
 55        });
 56
 57        var allTasks = healthCheckTasks.Concat(new[] { apiHealthTask }).ToArray();
 58        var results = await Task.WhenAll(allTasks);
 59
 60        // Determine overall status
 61        var overallStatus = DetermineOverallStatus(results);
 62
 63        _logger.LogTraceSanitized("System health check completed. Overall status: {Status}", overallStatus);
 64
 65        return new SystemHealthStatusDto
 66        {
 67            Timestamp = timestamp,
 68            OverallStatus = overallStatus,
 69            Services = results.ToList()
 70        };
 71    }
 72
 73    private static string DetermineOverallStatus(ServiceHealthDto[] services)
 74    {
 375        var hasUnhealthy = services.Any(s => s.Status == HealthStatus.Unhealthy);
 376        var hasDegraded = services.Any(s => s.Status == HealthStatus.Degraded);
 77
 378        if (hasUnhealthy)
 179            return HealthStatus.Unhealthy;
 80
 281        if (hasDegraded)
 182            return HealthStatus.Degraded;
 83
 184        return HealthStatus.Healthy;
 85    }
 86}