Skip to content

Commit 797d9f8

Browse files
authored
Allow multiple data sources of the same type for throughput processing (#5155) (#5184)
* Prevent throughput report from breaking if there is more than one source of the same type * Add tests * Fixed the expected value in the new test
1 parent 4a64141 commit 797d9f8

File tree

3 files changed

+54
-26
lines changed

3 files changed

+54
-26
lines changed

src/Particular.LicensingComponent.UnitTests/Infrastructure/DataStoreBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public DataStoreBuilder WithThroughput(ThroughputSource? source = null, DateOnly
6969
}
7070

7171
source ??= endpoint.Id.ThroughputSource;
72-
if (endpoints.SingleOrDefault(e => e.Id.Name == endpoint.Id.Name && e.Id.ThroughputSource == source) == null)
72+
if (endpoints.FirstOrDefault(e => e.Id.Name == endpoint.Id.Name && e.Id.ThroughputSource == source) == null)
7373
{
7474
throw new InvalidOperationException(
7575
$"Need to add endpoint {endpoint.Id.Name}:{source} before calling {nameof(WithThroughput)}");

src/Particular.LicensingComponent.UnitTests/ThroughputCollector/ThroughputCollector_Report_Throughput_Tests.cs

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class ThroughputCollector_Report_Throughput_Tests : ThroughputCollectorTestFixtu
1515
{
1616
public override Task Setup()
1717
{
18-
1918
SetExtraDependencies = d => { };
2019
return base.Setup();
2120
}
@@ -159,15 +158,15 @@ public async Task Should_return_correct_throughput_in_report_when_multiple_sourc
159158
// Arrange
160159
await DataStore.CreateBuilder()
161160
.AddEndpoint("Endpoint1", sources: [ThroughputSource.Broker, ThroughputSource.Monitoring])
162-
.WithThroughput(ThroughputSource.Broker, data: [50, 55])
163-
.WithThroughput(ThroughputSource.Monitoring, data: [60, 65])
161+
.WithThroughput(ThroughputSource.Broker, data: [50, 55])
162+
.WithThroughput(ThroughputSource.Monitoring, data: [60, 65])
164163
.AddEndpoint("Endpoint2", sources: [ThroughputSource.Broker, ThroughputSource.Audit])
165-
.WithThroughput(ThroughputSource.Broker, data: [60, 65])
166-
.WithThroughput(ThroughputSource.Audit, data: [61, 64])
164+
.WithThroughput(ThroughputSource.Broker, data: [60, 65])
165+
.WithThroughput(ThroughputSource.Audit, data: [61, 64])
167166
.AddEndpoint("Endpoint3", sources: [ThroughputSource.Broker, ThroughputSource.Monitoring, ThroughputSource.Audit])
168-
.WithThroughput(ThroughputSource.Broker, data: [50, 57])
169-
.WithThroughput(ThroughputSource.Monitoring, data: [40, 45])
170-
.WithThroughput(ThroughputSource.Audit, data: [42, 47])
167+
.WithThroughput(ThroughputSource.Broker, data: [50, 57])
168+
.WithThroughput(ThroughputSource.Monitoring, data: [40, 45])
169+
.WithThroughput(ThroughputSource.Audit, data: [42, 47])
171170
.Build();
172171

173172
// Act
@@ -218,10 +217,10 @@ public async Task Should_return_correct_throughput_in_report_when_data_from_mult
218217
// Arrange
219218
await DataStore.CreateBuilder()
220219
.AddEndpoint("Endpoint1", sources: [ThroughputSource.Broker])
221-
.WithThroughput(data: [50, 75])
220+
.WithThroughput(data: [50, 75])
222221
.AddEndpoint("Endpoint1_", sources: [ThroughputSource.Audit])
223222
.ConfigureEndpoint(endpoint => endpoint.SanitizedName = "Endpoint1")
224-
.WithThroughput(data: [60, 65])
223+
.WithThroughput(data: [60, 65])
225224
.Build();
226225

227226
// Act
@@ -244,27 +243,55 @@ await DataStore.CreateBuilder()
244243
});
245244
}
246245

246+
[TestCase(ThroughputSource.Audit)]
247+
[TestCase(ThroughputSource.Broker)]
248+
[TestCase(ThroughputSource.Monitoring)]
249+
public async Task Should_return_correct_throughput_in_report_when_data_from_multiple_sources_of_same_type_exist(ThroughputSource source)
250+
{
251+
// Arrange
252+
await DataStore.CreateBuilder()
253+
.AddEndpoint("Endpoint1", sources: [source])
254+
.WithThroughput(data: [50, 75])
255+
.AddEndpoint("Endpoint1_", sources: [source])
256+
.ConfigureEndpoint(endpoint => endpoint.SanitizedName = "Endpoint1")
257+
.WithThroughput(data: [60, 65])
258+
.Build();
259+
260+
// Act
261+
var report = await ThroughputCollector.GenerateThroughputReport("", null, default);
262+
263+
// Assert
264+
Assert.That(report, Is.Not.Null);
265+
Assert.That(report.ReportData.Queues.Count, Is.EqualTo(1));
266+
267+
Assert.Multiple(() =>
268+
{
269+
Assert.That(report.ReportData.TotalThroughput, Is.EqualTo(75), $"Incorrect TotalThroughput recorded");
270+
Assert.That(report.ReportData.TotalQueues, Is.EqualTo(1), $"Incorrect TotalQueues recorded");
271+
});
272+
}
273+
247274
[Test]
248275
public async Task Should_generate_correct_report()
249276
{
250277
// Arrange
251278
await DataStore.CreateBuilder()
252279
.AddEndpoint("Endpoint1", sources: [ThroughputSource.Broker, ThroughputSource.Monitoring])
253-
.WithThroughput(ThroughputSource.Broker, data: [50, 55], startDate: DateOnly.FromDateTime(new DateTime(2024, 4, 24)))
254-
.WithThroughput(ThroughputSource.Monitoring, data: [60, 65], startDate: DateOnly.FromDateTime(new DateTime(2024, 4, 24)))
255-
.ConfigureEndpoint(endpoint => endpoint.EndpointIndicators = [EndpointIndicator.KnownEndpoint.ToString()])
280+
.WithThroughput(ThroughputSource.Broker, data: [50, 55], startDate: DateOnly.FromDateTime(new DateTime(2024, 4, 24)))
281+
.WithThroughput(ThroughputSource.Monitoring, data: [60, 65], startDate: DateOnly.FromDateTime(new DateTime(2024, 4, 24)))
282+
.ConfigureEndpoint(endpoint => endpoint.EndpointIndicators = [EndpointIndicator.KnownEndpoint.ToString()])
256283
.AddEndpoint("Endpoint2", sources: [ThroughputSource.Broker, ThroughputSource.Audit])
257-
.WithThroughput(ThroughputSource.Broker, data: [60, 65], startDate: DateOnly.FromDateTime(new DateTime(2024, 4, 24)))
258-
.WithThroughput(ThroughputSource.Audit, data: [61, 64], startDate: DateOnly.FromDateTime(new DateTime(2024, 4, 24)))
259-
.ConfigureEndpoint(endpoint => endpoint.EndpointIndicators = [EndpointIndicator.KnownEndpoint.ToString()])
284+
.WithThroughput(ThroughputSource.Broker, data: [60, 65], startDate: DateOnly.FromDateTime(new DateTime(2024, 4, 24)))
285+
.WithThroughput(ThroughputSource.Audit, data: [61, 64], startDate: DateOnly.FromDateTime(new DateTime(2024, 4, 24)))
286+
.ConfigureEndpoint(endpoint => endpoint.EndpointIndicators = [EndpointIndicator.KnownEndpoint.ToString()])
260287
.AddEndpoint("Endpoint3", sources: [ThroughputSource.Broker, ThroughputSource.Monitoring, ThroughputSource.Audit])
261-
.WithThroughput(ThroughputSource.Broker, data: [50, 57], startDate: DateOnly.FromDateTime(new DateTime(2024, 4, 24)))
262-
.WithThroughput(ThroughputSource.Monitoring, data: [40, 45], startDate: DateOnly.FromDateTime(new DateTime(2024, 4, 24)))
263-
.WithThroughput(ThroughputSource.Audit, data: [42, 47], startDate: DateOnly.FromDateTime(new DateTime(2024, 4, 24)))
264-
.ConfigureEndpoint(endpoint => endpoint.EndpointIndicators = [EndpointIndicator.KnownEndpoint.ToString()])
288+
.WithThroughput(ThroughputSource.Broker, data: [50, 57], startDate: DateOnly.FromDateTime(new DateTime(2024, 4, 24)))
289+
.WithThroughput(ThroughputSource.Monitoring, data: [40, 45], startDate: DateOnly.FromDateTime(new DateTime(2024, 4, 24)))
290+
.WithThroughput(ThroughputSource.Audit, data: [42, 47], startDate: DateOnly.FromDateTime(new DateTime(2024, 4, 24)))
291+
.ConfigureEndpoint(endpoint => endpoint.EndpointIndicators = [EndpointIndicator.KnownEndpoint.ToString()])
265292
.AddEndpoint("Endpoint4", sources: [ThroughputSource.Broker])
266-
.ConfigureEndpoint(endpoint => endpoint.UserIndicator = UserIndicator.PlannedToDecommission.ToString())
267-
.WithThroughput(ThroughputSource.Broker, data: [42, 47], startDate: DateOnly.FromDateTime(new DateTime(2024, 4, 24)))
293+
.ConfigureEndpoint(endpoint => endpoint.UserIndicator = UserIndicator.PlannedToDecommission.ToString())
294+
.WithThroughput(ThroughputSource.Broker, data: [42, 47], startDate: DateOnly.FromDateTime(new DateTime(2024, 4, 24)))
268295
.AddEndpoint("Endpoint5", sources: [ThroughputSource.Broker])
269296
.ConfigureEndpoint(endpoint => endpoint.UserIndicator = UserIndicator.NotNServiceBusEndpoint.ToString())
270297
.WithThroughput(ThroughputSource.Broker, data: [15, 4], startDate: DateOnly.FromDateTime(new DateTime(2024, 4, 24)))

src/Particular.LicensingComponent/ThroughputDataExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
static class ThroughputDataExtensions
66
{
77
public static IEnumerable<EndpointDailyThroughput> FromSource(this List<ThroughputData> throughputs, ThroughputSource source) => throughputs
8-
.SingleOrDefault(td => td.ThroughputSource == source)?
9-
.Select(kvp => new EndpointDailyThroughput(kvp.Key, kvp.Value)) ?? [];
8+
.Where(td => td.ThroughputSource == source)
9+
.SelectMany(td => td)
10+
.Select(kvp => new EndpointDailyThroughput(kvp.Key, kvp.Value));
1011

1112
public static long Sum(this List<ThroughputData> throughputs) => throughputs.SelectMany(t => t).Sum(kvp => kvp.Value);
1213

@@ -40,4 +41,4 @@ public static long MaxMonthlyThroughput(this List<ThroughputData> throughputs)
4041

4142
public static bool HasDataFromSource(this IDictionary<string, IEnumerable<ThroughputData>> throughputPerQueue, ThroughputSource source) =>
4243
throughputPerQueue.Any(queueName => queueName.Value.Any(data => data.ThroughputSource == source && data.Count > 0));
43-
}
44+
}

0 commit comments

Comments
 (0)