< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Health.HealthReportTextFormatter
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Health/HealthReportTextFormatter.cs
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
83%
Covered lines: 31
Uncovered lines: 6
Coverable lines: 37
Total lines: 79
Line coverage: 83.7%
Branch coverage
63%
Covered branches: 23
Total branches: 36
Branch coverage: 63.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 10/13/2025 - 16:52:37 Line coverage: 83.7% (31/37) Branch coverage: 63.8% (23/36) Total lines: 79 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e 10/13/2025 - 16:52:37 Line coverage: 83.7% (31/37) Branch coverage: 63.8% (23/36) Total lines: 79 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Format(...)88.88%181890.47%
RenderValue(...)50%211260%
Escape(...)100%11100%
FormatDuration(...)16.66%66100%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Health/HealthReportTextFormatter.cs

#LineLine coverage
 1using System.Text;
 2
 3namespace Kestrun.Health;
 4
 5/// <summary>
 6/// Produces a concise, human-readable text representation of a <see cref="HealthReport"/>.
 7/// Intended for terminals, logs, or lightweight probes where structured formats (JSON/YAML/XML)
 8/// are unnecessary.
 9/// </summary>
 10public static class HealthReportTextFormatter
 11{
 12    /// <summary>
 13    /// Formats a <see cref="HealthReport"/> into a concise plain text representation. Includes probe data when <paramre
 14    /// </summary>
 15    /// <param name="report">The health report to format.</param>
 16    /// <param name="includeData">When true (default) emits per-probe data key/value lines.</param>
 17    /// <returns>A multi-line string suitable for console or log output.</returns>
 18    public static string Format(HealthReport report, bool includeData = true)
 19    {
 120        ArgumentNullException.ThrowIfNull(report);
 121        var sb = new StringBuilder();
 122        _ = sb.AppendLine($"Status: {report.StatusText}");
 123        _ = sb.AppendLine($"GeneratedAt: {report.GeneratedAt:O}");
 124        var s = report.Summary;
 125        _ = sb.AppendLine($"Summary: total={s.Total} healthy={s.Healthy} degraded={s.Degraded} unhealthy={s.Unhealthy}")
 126        if (report.AppliedTags is { Count: > 0 })
 27        {
 028            _ = sb.AppendLine($"Tags: {string.Join(",", report.AppliedTags)}");
 29        }
 130        _ = sb.AppendLine("Probes:");
 631        foreach (var p in report.Probes)
 32        {
 233            _ = sb.Append("  - ");
 234            _ = sb.Append($"name={p.Name} status={p.StatusText} duration={FormatDuration(p.Duration)}");
 235            if (!string.IsNullOrWhiteSpace(p.Description))
 36            {
 237                _ = sb.Append($" desc=\"{Escape(p.Description!)}\"");
 38            }
 239            if (!string.IsNullOrWhiteSpace(p.Error))
 40            {
 041                _ = sb.Append($" error=\"{Escape(p.Error!)}\"");
 42            }
 243            _ = sb.AppendLine();
 244            if (includeData && p.Data is { Count: > 0 })
 45            {
 846                foreach (var kvp in p.Data)
 47                {
 248                    _ = sb.AppendLine($"      {kvp.Key}={RenderValue(kvp.Value)}");
 49                }
 50            }
 51        }
 152        return sb.ToString();
 53    }
 54
 55    private static string RenderValue(object? value)
 56    {
 257        return value is null
 258            ? "<null>"
 259            : value switch
 260            {
 061                string s => '"' + Escape(s) + '"',
 062                DateTime dt => dt.ToString("O"),
 063                DateTimeOffset dto => dto.ToString("O"),
 064                TimeSpan ts => ts.ToString(),
 265                _ => value.ToString() ?? string.Empty
 266            };
 67    }
 68
 269    private static string Escape(string input) => input.Replace("\"", "\\\"").Replace("\n", "\\n").Replace("\r", "\\r");
 70
 71    private static string FormatDuration(TimeSpan duration)
 72    {
 273        return duration.TotalMilliseconds < 1
 274            ? "<1ms"
 275            : duration.TotalMilliseconds < 1000
 276            ? ((int)duration.TotalMilliseconds) + "ms"
 277            : duration.TotalSeconds < 60 ? duration.TotalSeconds.ToString("0.###") + "s" : duration.ToString();
 78    }
 79}