|
|
@ -6,13 +6,17 @@ |
|
|
// ==========================================================================
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
|
using System.Collections.Concurrent; |
|
|
using System.Collections.Concurrent; |
|
|
|
|
|
using System.Globalization; |
|
|
using Microsoft.Extensions.Logging; |
|
|
using Microsoft.Extensions.Logging; |
|
|
using Squidex.Infrastructure.Timers; |
|
|
using Squidex.Infrastructure.Timers; |
|
|
|
|
|
using Squidex.Infrastructure.Validation; |
|
|
|
|
|
|
|
|
namespace Squidex.Infrastructure.UsageTracking; |
|
|
namespace Squidex.Infrastructure.UsageTracking; |
|
|
|
|
|
|
|
|
public sealed class BackgroundUsageTracker : DisposableObjectBase, IUsageTracker |
|
|
public sealed class BackgroundUsageTracker : DisposableObjectBase, IUsageTracker |
|
|
{ |
|
|
{ |
|
|
|
|
|
// The result contains one entry per day and category, therefore the range must be limited.
|
|
|
|
|
|
public const int MaxQueryDays = 3660; |
|
|
private const int Intervall = 60 * 1000; |
|
|
private const int Intervall = 60 * 1000; |
|
|
private readonly IUsageRepository usageRepository; |
|
|
private readonly IUsageRepository usageRepository; |
|
|
private readonly ILogger<BackgroundUsageTracker> log; |
|
|
private readonly ILogger<BackgroundUsageTracker> log; |
|
|
@ -128,6 +132,7 @@ public sealed class BackgroundUsageTracker : DisposableObjectBase, IUsageTracker |
|
|
Guard.NotNullOrEmpty(key); |
|
|
Guard.NotNullOrEmpty(key); |
|
|
|
|
|
|
|
|
ThrowIfDisposed(); |
|
|
ThrowIfDisposed(); |
|
|
|
|
|
ValidateRange(fromDate, toDate); |
|
|
|
|
|
|
|
|
var result = new Dictionary<string, List<(DateOnly Date, Counters Counters)>>(); |
|
|
var result = new Dictionary<string, List<(DateOnly Date, Counters Counters)>>(); |
|
|
|
|
|
|
|
|
@ -150,11 +155,17 @@ public sealed class BackgroundUsageTracker : DisposableObjectBase, IUsageTracker |
|
|
{ |
|
|
{ |
|
|
var enriched = new List<(DateOnly Date, Counters Counters)>(); |
|
|
var enriched = new List<(DateOnly Date, Counters Counters)>(); |
|
|
|
|
|
|
|
|
for (var date = fromDate; date <= toDate; date = date.AddDays(1)) |
|
|
// Use the first entry per date, if the store returns duplicates.
|
|
|
|
|
|
var countersByDate = new Dictionary<DateOnly, Counters>(); |
|
|
|
|
|
|
|
|
|
|
|
foreach (var usage in value) |
|
|
{ |
|
|
{ |
|
|
var counters = value.Find(x => x.Date == date)?.Counters; |
|
|
countersByDate.TryAdd(usage.Date, usage.Counters); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
enriched.Add((date, counters ?? [])); |
|
|
for (var date = fromDate; date <= toDate; date = date.AddDays(1)) |
|
|
|
|
|
{ |
|
|
|
|
|
enriched.Add((date, countersByDate.GetValueOrDefault(date) ?? [])); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
result[category] = enriched; |
|
|
result[category] = enriched; |
|
|
@ -200,4 +211,23 @@ public sealed class BackgroundUsageTracker : DisposableObjectBase, IUsageTracker |
|
|
{ |
|
|
{ |
|
|
return !string.IsNullOrWhiteSpace(category) ? category.Trim() : FallbackCategory; |
|
|
return !string.IsNullOrWhiteSpace(category) ? category.Trim() : FallbackCategory; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static void ValidateRange(DateOnly fromDate, DateOnly toDate) |
|
|
|
|
|
{ |
|
|
|
|
|
if (toDate < fromDate) |
|
|
|
|
|
{ |
|
|
|
|
|
throw new ValidationException(Not.GreaterEqualsThan("ToDate", "FromDate")); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (toDate.DayNumber - fromDate.DayNumber >= MaxQueryDays) |
|
|
|
|
|
{ |
|
|
|
|
|
throw new ValidationException(Not.LessEqualsThan("Days", MaxQueryDays.ToString(CultureInfo.InvariantCulture))); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// The result is created day by day and would overflow at the end.
|
|
|
|
|
|
if (toDate == DateOnly.MaxValue) |
|
|
|
|
|
{ |
|
|
|
|
|
throw new ValidationException(Not.LessThan("ToDate", DateOnly.MaxValue.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture))); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|