Skip to content

Commit

Permalink
fix issue with multiple aggregated counter for monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
perrich committed May 8, 2024
1 parent 25b5c2e commit f2d6d48
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Hangfire.MemoryStorage.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>Hangfire.MemoryStorage</id>
<version>1.8.0.0</version>
<version>1.8.1.0</version>
<title>Hangfire.MemoryStorage</title>
<authors>PERRICHOT Florian</authors>
<owners>PERRICHOT Florian</owners>
<license type="expression">APACHE LICENSE, VERSION 2.0</license>
<licenseUrl>https://www.apache.org/licenses/LICENSE-2.0.html</licenseUrl>
<projectUrl>https://github.com/perrich/Hangfire.MemoryStorage</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A memory storage for Hangfire</description>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
8 changes: 4 additions & 4 deletions src/Hangfire.MemoryStorage/Hangfire.MemoryStorage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<Description>A memory storage for Hangfire</Description>
<Copyright>Copyright 2015-2023</Copyright>
<Copyright>Copyright 2015-2024</Copyright>
<AssemblyTitle>Hangfire.MemoryStorage</AssemblyTitle>
<VersionPrefix>1.8.0</VersionPrefix>
<VersionPrefix>1.8.1</VersionPrefix>
<Authors>PERRICHOT Florian</Authors>
<TargetFrameworks>net45;netstandard1.3</TargetFrameworks>
<AssemblyName>Hangfire.MemoryStorage</AssemblyName>
Expand All @@ -14,8 +14,8 @@
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/perrich/Hangfire.MemoryStorage</RepositoryUrl>
<NetStandardImplicitPackageVersion>1.6.0</NetStandardImplicitPackageVersion>
<Version>1.8.0</Version>
<AssemblyVersion>1.8.0.0</AssemblyVersion>
<Version>1.8.1</Version>
<AssemblyVersion>1.8.1.0</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 5 additions & 3 deletions src/Hangfire.MemoryStorage/MemoryStorageMonitoringApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,11 @@ private Dictionary<DateTime, long> GetTimelineStats(List<DateTime> dates,
var counters = _data.GetEnumeration<AggregatedCounterDto>();
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)))
{
Expand Down

0 comments on commit f2d6d48

Please sign in to comment.