| | | 1 | | using Elsa.Common; |
| | | 2 | | using Elsa.Dashboard.Api.Models; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Dashboard.Api.Services; |
| | | 5 | | |
| | 19 | 6 | | public class DashboardRangeResolver(ISystemClock clock) |
| | | 7 | | { |
| | | 8 | | public DashboardRange Resolve(string? range) |
| | | 9 | | { |
| | 13 | 10 | | var key = Normalize(range); |
| | 13 | 11 | | var duration = key switch |
| | 13 | 12 | | { |
| | 1 | 13 | | DashboardRangeKeys.OneHour => TimeSpan.FromHours(1), |
| | 1 | 14 | | DashboardRangeKeys.SevenDays => TimeSpan.FromDays(7), |
| | 11 | 15 | | _ => TimeSpan.FromHours(24) |
| | 13 | 16 | | }; |
| | 13 | 17 | | var to = clock.UtcNow; |
| | 13 | 18 | | return new(key, to.Subtract(duration), to); |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | public string ResolveGranularity(string? granularity, string range) => |
| | 5 | 22 | | string.IsNullOrWhiteSpace(granularity) |
| | 5 | 23 | | ? range switch |
| | 5 | 24 | | { |
| | 1 | 25 | | DashboardRangeKeys.OneHour => DashboardTrendGranularity.Minute, |
| | 1 | 26 | | DashboardRangeKeys.SevenDays => DashboardTrendGranularity.Day, |
| | 1 | 27 | | _ => DashboardTrendGranularity.Hour |
| | 5 | 28 | | } |
| | 5 | 29 | | : granularity; |
| | | 30 | | |
| | | 31 | | public TimeSpan GetBucketSize(string granularity) => |
| | 1 | 32 | | granularity.Equals(DashboardTrendGranularity.Minute, StringComparison.OrdinalIgnoreCase) |
| | 1 | 33 | | ? TimeSpan.FromMinutes(5) |
| | 1 | 34 | | : granularity.Equals(DashboardTrendGranularity.Day, StringComparison.OrdinalIgnoreCase) |
| | 1 | 35 | | ? TimeSpan.FromDays(1) |
| | 1 | 36 | | : TimeSpan.FromHours(1); |
| | | 37 | | |
| | | 38 | | private static string Normalize(string? range) => |
| | 13 | 39 | | range?.Trim().ToLowerInvariant() switch |
| | 13 | 40 | | { |
| | 1 | 41 | | DashboardRangeKeys.OneHour => DashboardRangeKeys.OneHour, |
| | 1 | 42 | | DashboardRangeKeys.SevenDays => DashboardRangeKeys.SevenDays, |
| | 11 | 43 | | _ => DashboardRangeKeys.TwentyFourHours |
| | 13 | 44 | | }; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public record DashboardRange(string Key, DateTimeOffset From, DateTimeOffset To); |