// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Microsoft.Extensions.Diagnostics.HealthChecks
{
///
/// Represents the results of multiple health checks.
///
public class CompositeHealthCheckResult
{
///
/// A containing the results from each health check.
///
///
/// The keys in this dictionary map to the name of the health check, the values are the
/// returned when was called for that health check.
///
public IReadOnlyDictionary Results { get; }
///
/// Gets a representing the aggregate status of all the health checks.
///
///
/// This value is determined by taking the "worst" result of all the results. So if any result is ,
/// this value is . If no result is but any result is
/// , this value is , etc.
///
public HealthCheckStatus Status { get; }
///
/// Create a new from the specified results.
///
/// A containing the results from each health check.
public CompositeHealthCheckResult(IReadOnlyDictionary results)
{
Results = results;
Status = CalculateAggregateStatus(results.Values);
}
private HealthCheckStatus CalculateAggregateStatus(IEnumerable results)
{
// This is basically a Min() check, but we know the possible range, so we don't need to walk the whole list
var currentValue = HealthCheckStatus.Healthy;
foreach (var result in results)
{
if (currentValue > result.Status)
{
currentValue = result.Status;
}
if (currentValue == HealthCheckStatus.Failed)
{
// Game over, man! Game over!
// (We hit the worst possible status, so there's no need to keep iterating)
return currentValue;
}
}
return currentValue;
}
}
}