From f7e0dc87fd4d64547d63005b88b1a8507f80d1db Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli (W11-AI)" Date: Tue, 7 Apr 2026 11:41:31 +0200 Subject: [PATCH] prima rev x flushService --- MP.IOC/Services/MetricsFlushService.cs | 29 ++++++++++++-------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/MP.IOC/Services/MetricsFlushService.cs b/MP.IOC/Services/MetricsFlushService.cs index 417fbc18..b0196fad 100644 --- a/MP.IOC/Services/MetricsFlushService.cs +++ b/MP.IOC/Services/MetricsFlushService.cs @@ -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 } -} \ No newline at end of file +}