< Summary

Information
Class: AmbientServices.WindowScope
Assembly: AmbientServices
File(s): /home/runner/work/AmbientServices/AmbientServices/AmbientServices/Support/WindowScope.cs
Tag: 332_35464845198
Line coverage
100%
Covered lines: 39
Uncovered lines: 0
Coverable lines: 39
Total lines: 110
Line coverage: 100%
Branch coverage
100%
Covered branches: 30
Total branches: 30
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
WindowId(...)100%1212100%
UnitString(...)100%44100%
WindowSize(...)100%1414100%

File(s)

/home/runner/work/AmbientServices/AmbientServices/AmbientServices/Support/WindowScope.cs

#LineLine coverage
 1using System;
 2
 3namespace AmbientServices;
 4
 5/// <summary>
 6/// A static class that provides utility functions for managing a time-based sampling window.
 7/// </summary>
 8/// <remarks>
 9/// <pitch>Compact, human-readable labels for time windows: <see cref="WindowId"/> names which window a moment falls in 
 10/// <pledge>Window identifiers are derived from the invariant-culture universal sortable format of the given <see cref="
 11/// <plan>Pure string slicing of the fixed-position "u" format at per-resolution-bucket positions; durations pick their 
 12/// </remarks>
 13public static class WindowScope
 14{
 15    /// <summary>
 16    /// Gets a timestamp for the specified <see cref="DateTime"/>, with a resolution appropriate for the units near the 
 17    /// </summary>
 18    /// <param name="dateTime">The <see cref="DateTime"/> to get an window identifier for.</param>
 19    /// <param name="resolution">A <see cref="TimeSpan"/> indicating what type of resolution is needed.</param>
 20    /// <returns>A string containing a timespan that will be distinguishable from timestamps for other <see cref="DateTi
 21    public static string WindowId(DateTime dateTime, TimeSpan resolution)
 22    {
 223        string timestamp = dateTime.ToString("u", System.Globalization.CultureInfo.InvariantCulture).TrimEnd('Z').Replac
 224        if (resolution > TimeSpan.FromDays(365))
 25        {
 226            timestamp = timestamp.Substring(0, 7);
 27        }
 228        else if (resolution > TimeSpan.FromDays(30))
 29        {
 230            timestamp = timestamp.Substring(5, 5);
 31        }
 232        else if (resolution > TimeSpan.FromDays(1))
 33        {
 234            timestamp = timestamp.Substring(8, 5);
 35        }
 236        else if (resolution > TimeSpan.FromHours(1))
 37        {
 238            timestamp = timestamp.Substring(11, 5);
 39        }
 240        else if (resolution > TimeSpan.FromMinutes(1))
 41        {
 242            timestamp = timestamp.Substring(11, 8);
 43        }
 244        else if (resolution > TimeSpan.FromSeconds(1))
 45        {
 246            timestamp = string.Concat(timestamp.Substring(14), ".", dateTime.Millisecond.ToString(System.Globalization.C
 47        }
 48        else
 49        {
 250            timestamp = string.Concat(timestamp.Substring(17), ".", dateTime.Millisecond.ToString(System.Globalization.C
 51        }
 252        return timestamp;
 53    }
 54    private static string UnitString(string prefix, double count, string postfix)
 55    {
 256        double rounded = Math.Round(count, 1);
 257        int intPart = (int)rounded;
 258        int firstDecimal = (int)((rounded * 10) - (intPart * 10));
 259        if (intPart < 10 && firstDecimal != 0)
 60        {
 261            return prefix + intPart.ToString(System.Globalization.CultureInfo.InvariantCulture) + "." + firstDecimal.ToS
 62        }
 263        return prefix + Math.Round(count, 0).ToString(System.Globalization.CultureInfo.InvariantCulture) + postfix;
 64    }
 65    /// <summary>
 66    /// Gets a short string representing the specified timespan.
 67    /// </summary>
 68    /// <param name="duration">The <see cref="TimeSpan"/> whose string representation is to be generated</param>
 69    /// <returns>An easily human-readable string representing the time span with a postfix character indicating the unit
 70    public static string WindowSize(TimeSpan duration)
 71    {
 72        string sign;
 73        TimeSpan absTimeSpan;
 274        if (duration.Ticks < 0)
 75        {
 276            sign = "-";
 277            absTimeSpan = new TimeSpan(-duration.Ticks);
 78        }
 79        else
 80        {
 281            sign = "";
 282            absTimeSpan = new TimeSpan(duration.Ticks);
 83        }
 284        if (absTimeSpan.TotalDays > 1827)
 85        {
 286            return UnitString(sign, absTimeSpan.TotalDays / 365.25, "Y");
 87        }
 288        if (absTimeSpan.TotalDays > 160)
 89        {
 290            return UnitString(sign, absTimeSpan.TotalDays / 30.5, "M");
 91        }
 292        if (absTimeSpan.TotalDays > 5)
 93        {
 294            return UnitString(sign, absTimeSpan.TotalDays, "D");
 95        }
 296        if (absTimeSpan.TotalHours > 5)
 97        {
 298            return UnitString(sign, absTimeSpan.TotalHours, "h");
 99        }
 2100        if (absTimeSpan.TotalMinutes > 5)
 101        {
 2102            return UnitString(sign, absTimeSpan.TotalMinutes, "m");
 103        }
 2104        if (absTimeSpan.TotalSeconds > 5)
 105        {
 2106            return UnitString(sign, absTimeSpan.TotalSeconds, "s");
 107        }
 2108        return UnitString(sign, absTimeSpan.TotalMilliseconds, "ms");
 109    }
 110}