dotnet-cqrs/Svrnty.CQRS.Events.Abstractions/Models/HealthCheckResult.cs

96 lines
2.6 KiB
C#

using System;
using Svrnty.CQRS.Events.Abstractions.Models;
using System.Collections.Generic;
namespace Svrnty.CQRS.Events.Abstractions.Models;
/// <summary>
/// Status of a health check.
/// </summary>
public enum HealthStatus
{
/// <summary>
/// The component is healthy.
/// </summary>
Healthy = 0,
/// <summary>
/// The component is degraded but still functional.
/// </summary>
Degraded = 1,
/// <summary>
/// The component is unhealthy.
/// </summary>
Unhealthy = 2
}
/// <summary>
/// Result of a health check operation.
/// </summary>
public sealed record HealthCheckResult
{
/// <summary>
/// Overall health status.
/// </summary>
public required HealthStatus Status { get; init; }
/// <summary>
/// Optional description of the health status.
/// </summary>
public string? Description { get; init; }
/// <summary>
/// Exception that occurred during the health check, if any.
/// </summary>
public Exception? Exception { get; init; }
/// <summary>
/// Additional data about the health check.
/// </summary>
public IReadOnlyDictionary<string, object>? Data { get; init; }
/// <summary>
/// Time taken to perform the health check.
/// </summary>
public TimeSpan Duration { get; init; }
/// <summary>
/// Creates a healthy result.
/// </summary>
public static HealthCheckResult Healthy(string? description = null, IReadOnlyDictionary<string, object>? data = null, TimeSpan duration = default)
=> new()
{
Status = HealthStatus.Healthy,
Description = description,
Data = data,
Duration = duration
};
/// <summary>
/// Creates a degraded result.
/// </summary>
public static HealthCheckResult Degraded(string? description = null, Exception? exception = null, IReadOnlyDictionary<string, object>? data = null, TimeSpan duration = default)
=> new()
{
Status = HealthStatus.Degraded,
Description = description,
Exception = exception,
Data = data,
Duration = duration
};
/// <summary>
/// Creates an unhealthy result.
/// </summary>
public static HealthCheckResult Unhealthy(string? description = null, Exception? exception = null, IReadOnlyDictionary<string, object>? data = null, TimeSpan duration = default)
=> new()
{
Status = HealthStatus.Unhealthy,
Description = description,
Exception = exception,
Data = data,
Duration = duration
};
}