This commit is contained in:
Samuele Locatelli
2026-04-07 11:41:43 +02:00
+13 -16
View File
@@ -1,4 +1,4 @@
using NLog;
using NLog;
using StackExchange.Redis;
using System.Globalization;
@@ -43,9 +43,11 @@ namespace MP.IOC.Services
var stat = kv.Value;
var count = Interlocked.Read(ref stat.Count);
var totalMs = stat.TotalDuration.TotalMilliseconds;
var maxMs = stat.MaxDuration.TotalMilliseconds;
var minMs = (stat.MinDuration == TimeSpan.MaxValue) ? 0 : stat.MinDuration.TotalMilliseconds;
Log.Info("Method {method} Count {count} TotalDurationMs {totalMs} Destinations {dests}",
method, count, totalMs, string.Join(",", stat.Destinations.Select(x => $"{x.Key}:{x.Value}")));
Log.Info("Method {method} Count {count} TotalDurationMs {totalMs} MaxDurationMs {maxMs} MinDurationMs {minMs} Destinations {dests}",
method, count, totalMs, maxMs, minMs, string.Join(",", stat.Destinations.Select(x => $"{x.Key}:{x.Value}")));
if (_db == null) continue;
@@ -61,9 +63,13 @@ namespace MP.IOC.Services
// Increment hash fields (count and totalMs)
var taskHourCount = batch.HashIncrementAsync(hourKey, "count", count);
var taskHourTotal = batch.HashIncrementAsync(hourKey, "totalMs", totalMs);
var taskHourMax = batch.HashSetAsync(hourKey, "maxMs", maxMs.ToString());
var taskHourMin = batch.HashSetAsync(hourKey, "minMs", minMs.ToString());
var taskDayCount = batch.HashIncrementAsync(dayKey, "count", count);
var taskDayTotal = batch.HashIncrementAsync(dayKey, "totalMs", totalMs);
var taskDayMax = batch.HashSetAsync(dayKey, "maxMs", maxMs.ToString());
var taskDayMin = batch.HashSetAsync(dayKey, "minMs", minMs.ToString());
// Add to sorted set indices with score = epoch seconds of bucket start
var hourScore = ToEpochSeconds(hourStart);
@@ -72,22 +78,13 @@ namespace MP.IOC.Services
var taskZAddHour = batch.SortedSetAddAsync(hoursIndex, hourKey, hourScore);
var taskZAddDay = batch.SortedSetAddAsync(daysIndex, dayKey, dayScore);
//// Optionally set TTL on bucket hashes (e.g., keep daily buckets 400 days)
//var taskExpireHour = batch.KeyExpireAsync(hourKey, TimeSpan.FromDays(11)); // keep ~11 days for hourly
//var taskExpireDay = batch.KeyExpireAsync(dayKey, TimeSpan.FromDays(400)); // keep ~400 days for daily
// Execute batch
batch.Execute();
// Await tasks to ensure completion
await Task.WhenAll(taskHourCount, taskHourTotal, taskDayCount, taskDayTotal, taskZAddHour, taskZAddDay);
//await Task.WhenAll(taskHourCount, taskHourTotal, taskDayCount, taskDayTotal,
// taskZAddHour, taskZAddDay, taskExpireHour, taskExpireDay);
//// Trim indices to keep only last N entries (use ZREMRANGEBYRANK)
//// Note: trimming is separate calls (not in batch) to ensure ordering; can be batched too.
//await _db.SortedSetRemoveRangeByRankAsync(hoursIndex, 0, -MaxHourlyBuckets - 1); // keep last MaxHourlyBuckets
//await _db.SortedSetRemoveRangeByRankAsync(daysIndex, 0, -MaxDailyBuckets - 1);
await Task.WhenAll(taskHourCount, taskHourTotal, taskHourMax, taskHourMin,
taskDayCount, taskDayTotal, taskDayMax, taskDayMin,
taskZAddHour, taskZAddDay);
}
_stats.Clear();
@@ -144,4 +141,4 @@ namespace MP.IOC.Services
#endregion Private Methods
}
}
}