From f2d6d4888a04cbd6b3195e7469d78142256cc52f Mon Sep 17 00:00:00 2001 From: perrich Date: Wed, 8 May 2024 18:05:36 +0200 Subject: [PATCH] fix issue with multiple aggregated counter for monitoring --- Hangfire.MemoryStorage.nuspec | 4 +-- .../MemoryStorageMonitoringApiTests.cs | 29 +++++++++++++++++++ .../Hangfire.MemoryStorage.csproj | 8 ++--- .../MemoryStorageMonitoringApi.cs | 8 +++-- 4 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 src/Hangfire.MemoryStorage.UnitTest/MemoryStorageMonitoringApiTests.cs diff --git a/Hangfire.MemoryStorage.nuspec b/Hangfire.MemoryStorage.nuspec index 48bf89d..ef834d4 100644 --- a/Hangfire.MemoryStorage.nuspec +++ b/Hangfire.MemoryStorage.nuspec @@ -2,11 +2,11 @@ Hangfire.MemoryStorage - 1.8.0.0 + 1.8.1.0 Hangfire.MemoryStorage PERRICHOT Florian PERRICHOT Florian - APACHE LICENSE, VERSION 2.0 + https://www.apache.org/licenses/LICENSE-2.0.html https://github.com/perrich/Hangfire.MemoryStorage false A memory storage for Hangfire diff --git a/src/Hangfire.MemoryStorage.UnitTest/MemoryStorageMonitoringApiTests.cs b/src/Hangfire.MemoryStorage.UnitTest/MemoryStorageMonitoringApiTests.cs new file mode 100644 index 0000000..39ca48f --- /dev/null +++ b/src/Hangfire.MemoryStorage.UnitTest/MemoryStorageMonitoringApiTests.cs @@ -0,0 +1,29 @@ + +using Hangfire.MemoryStorage.Database; +using Hangfire.MemoryStorage.Dto; +using Hangfire.MemoryStorage; + +namespace Hangfire.MemoryStorage.UnitTest; + +public class MemoryStorageMonitoringApiTests +{ + private Data data; + private MemoryStorageMonitoringApi monitoringApi; + + public MemoryStorageMonitoringApiTests() + { + data = new Data(); + monitoringApi = new MemoryStorageMonitoringApi(data); + } + + [Fact] + public void MonitoringShouldReturnSumOfCounterIfTwoAggregatedCounterDtoHaveSameKey() + { + var key = string.Format("stats:succeeded:{0}", DateTime.UtcNow.AddHours(-2).ToString("yyyy-MM-dd-HH")); + data.Create(new AggregatedCounterDto { Id = 1, Key = key, Value = 5 }); + data.Create(new AggregatedCounterDto { Id = 2, Key = key, Value = 2 }); // Creation in CountersAggregator can produce two object in concurrent environment + + var stats = monitoringApi.HourlySucceededJobs().ToArray(); + Assert.Equal(7, stats[2].Value); + } +} \ No newline at end of file diff --git a/src/Hangfire.MemoryStorage/Hangfire.MemoryStorage.csproj b/src/Hangfire.MemoryStorage/Hangfire.MemoryStorage.csproj index 7958a0c..1982d53 100644 --- a/src/Hangfire.MemoryStorage/Hangfire.MemoryStorage.csproj +++ b/src/Hangfire.MemoryStorage/Hangfire.MemoryStorage.csproj @@ -2,9 +2,9 @@ A memory storage for Hangfire - Copyright 2015-2023 + Copyright 2015-2024 Hangfire.MemoryStorage - 1.8.0 + 1.8.1 PERRICHOT Florian net45;netstandard1.3 Hangfire.MemoryStorage @@ -14,8 +14,8 @@ git https://github.com/perrich/Hangfire.MemoryStorage 1.6.0 - 1.8.0 - 1.8.0.0 + 1.8.1 + 1.8.1.0 diff --git a/src/Hangfire.MemoryStorage/MemoryStorageMonitoringApi.cs b/src/Hangfire.MemoryStorage/MemoryStorageMonitoringApi.cs index 0ddb0e6..a531c36 100644 --- a/src/Hangfire.MemoryStorage/MemoryStorageMonitoringApi.cs +++ b/src/Hangfire.MemoryStorage/MemoryStorageMonitoringApi.cs @@ -285,9 +285,11 @@ private Dictionary GetTimelineStats(List dates, var counters = _data.GetEnumeration(); var keyMap = dates.ToDictionary(formatorAction, x => x); - var valuesMap = (from c in counters - where keyMap.Keys.Contains(c.Key) - select c).ToDictionary(o => o.Key, o => o.Value); + var valuesMap = counters + .Where(c => keyMap.Keys.Contains(c.Key)) + .GroupBy(c => c.Key) + .Select(g => new { g.Key, Sum = g.Sum(c => c.Value) }) + .ToDictionary(o => o.Key, o => o.Sum); foreach (var key in keyMap.Keys.Where(key => !valuesMap.ContainsKey(key))) {