Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,7 @@ public Task<IDictionary<string, IEnumerable<ThroughputData>>> GetEndpointThrough
{
var results = queueNames.ToDictionary(queueName => queueName, _ => Enumerable.Empty<ThroughputData>());

var requestedByNormalizedName = new Dictionary<string, string>();
foreach (var queueName in queueNames)
{
var normalizedName = Normalize(queueName);
if (!requestedByNormalizedName.ContainsKey(normalizedName))
{
requestedByNormalizedName[normalizedName] = queueName;
}
}

var normalizedNames = requestedByNormalizedName.Keys.ToList();
var normalizedNames = queueNames.Select(Normalize).Distinct().ToList();
var from = DateOnly.FromDateTime(timeProvider.GetUtcNow().UtcDateTime).AddMonths(-ThroughputReporting.ReportedMonths);

var rows = await context.LicensingEndpoints
Expand All @@ -134,15 +124,23 @@ public Task<IDictionary<string, IEnumerable<ThroughputData>>> GetEndpointThrough
throughput => new { throughput.NormalizedName, throughput.ThroughputSource },
(endpoint, throughput) => new
{
endpoint.NormalizedSanitizedName,
endpoint.NormalizedName,
endpoint.SanitizedName,
endpoint.ThroughputSource,
throughput.DateUtc,
throughput.MessageCount
})
.ToListAsync(token);

foreach (var endpointRows in rows.GroupBy(row => (row.NormalizedSanitizedName, row.ThroughputSource)))
foreach (var endpointRows in rows.GroupBy(row => (row.NormalizedName, row.ThroughputSource, row.SanitizedName)))
{
// The query ignores case, so it can return spellings nobody asked for. Each endpoint's throughput goes under its
// own spelling, because the collector deletes any endpoint whose spelling comes back empty.
if (!results.TryGetValue(endpointRows.Key.SanitizedName, out var queueThroughput))
{
continue;
}

var throughputData = new ThroughputData(endpointRows
.Where(row => !throughputMaxDate.HasValue || row.DateUtc < throughputMaxDate.Value)
.OrderBy(row => row.DateUtc)
Expand All @@ -151,8 +149,7 @@ public Task<IDictionary<string, IEnumerable<ThroughputData>>> GetEndpointThrough
ThroughputSource = endpointRows.Key.ThroughputSource
};

var queueName = requestedByNormalizedName[endpointRows.Key.NormalizedSanitizedName];
results[queueName] = results[queueName].Append(throughputData);
results[endpointRows.Key.SanitizedName] = queueThroughput.Append(throughputData);
}

return (IDictionary<string, IEnumerable<ThroughputData>>)results;
Expand Down
59 changes: 59 additions & 0 deletions src/ServiceControl.Persistence.Tests/Throughput/EndpointsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,65 @@ public async Task Should_update_endpoint_that_already_has_throughput_with_new_th
}
}

[Test]
public async Task Should_return_each_spelling_its_own_throughput_when_sanitized_names_differ_only_in_case()
{
var yesterday = DateOnly.FromDateTime(DateTime.UtcNow).AddDays(-1);
var broker = new Endpoint("Smoke.Billing", ThroughputSource.Broker) { SanitizedName = "Smoke.Billing" };
var monitoring = new Endpoint("smoke.billing", ThroughputSource.Monitoring) { SanitizedName = "smoke.billing" };
await LicensingDataStore.SaveEndpoint(broker);
await LicensingDataStore.SaveEndpoint(monitoring);
await LicensingDataStore.RecordEndpointThroughput(broker.Id.Name, ThroughputSource.Broker, yesterday, 100);
await LicensingDataStore.RecordEndpointThroughput(monitoring.Id.Name, ThroughputSource.Monitoring, yesterday, 30);

var throughput = await LicensingDataStore.GetEndpointThroughputByQueueName([broker.SanitizedName, monitoring.SanitizedName]);

using (Assert.EnterMultipleScope())
{
Assert.That(throughput[broker.SanitizedName].Select(data => (data.ThroughputSource, data[yesterday])),
Is.EqualTo(new[] { (ThroughputSource.Broker, 100L) }), broker.SanitizedName);
Assert.That(throughput[monitoring.SanitizedName].Select(data => (data.ThroughputSource, data[yesterday])),
Is.EqualTo(new[] { (ThroughputSource.Monitoring, 30L) }), monitoring.SanitizedName);
}
}

[Test]
public async Task Should_leave_out_a_spelling_that_was_not_requested()
{
var yesterday = DateOnly.FromDateTime(DateTime.UtcNow).AddDays(-1);
var broker = new Endpoint("Smoke.Billing", ThroughputSource.Broker) { SanitizedName = "Smoke.Billing" };
var monitoring = new Endpoint("smoke.billing", ThroughputSource.Monitoring) { SanitizedName = "smoke.billing" };
await LicensingDataStore.SaveEndpoint(broker);
await LicensingDataStore.SaveEndpoint(monitoring);
await LicensingDataStore.RecordEndpointThroughput(broker.Id.Name, ThroughputSource.Broker, yesterday, 100);
await LicensingDataStore.RecordEndpointThroughput(monitoring.Id.Name, ThroughputSource.Monitoring, yesterday, 30);

var throughput = await LicensingDataStore.GetEndpointThroughputByQueueName([broker.SanitizedName]);

using (Assert.EnterMultipleScope())
{
Assert.That(throughput.Keys, Is.EqualTo(new[] { broker.SanitizedName }));
Assert.That(throughput[broker.SanitizedName].Select(data => (data.ThroughputSource, data[yesterday])),
Is.EqualTo(new[] { (ThroughputSource.Broker, 100L) }));
}
}

[Test]
public async Task Should_return_one_throughput_entry_per_endpoint_when_endpoints_share_a_sanitized_name()
{
var yesterday = DateOnly.FromDateTime(DateTime.UtcNow).AddDays(-1);
var dotted = new Endpoint("Sales.Billing", ThroughputSource.Audit) { SanitizedName = "Sales-Billing" };
var dashed = new Endpoint("Sales-Billing", ThroughputSource.Audit) { SanitizedName = "Sales-Billing" };
await LicensingDataStore.SaveEndpoint(dotted);
await LicensingDataStore.SaveEndpoint(dashed);
await LicensingDataStore.RecordEndpointThroughput(dotted.Id.Name, ThroughputSource.Audit, yesterday, 10);
await LicensingDataStore.RecordEndpointThroughput(dashed.Id.Name, ThroughputSource.Audit, yesterday, 20);

var throughput = await LicensingDataStore.GetEndpointThroughputByQueueName(["Sales-Billing"]);

Assert.That(throughput["Sales-Billing"].Select(data => data[yesterday]), Is.EquivalentTo(new[] { 10L, 20L }));
}

[Test]
public async Task Should_retrieve_matching_endpoint_when_same_source()
{
Expand Down
45 changes: 45 additions & 0 deletions src/ServiceControl.Persistence.Tests/UsageSummaryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace ServiceControl.Persistence.Tests;

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging.Abstractions;
using NUnit.Framework;
using Particular.LicensingComponent;
using Particular.LicensingComponent.Contracts;
using Particular.LicensingComponent.MonitoringThroughput;

// Kept outside Throughput/ because the in-memory test project compiles that folder and does not reference ThroughputCollector.
class UsageSummaryTests : PersistenceTestBase
{
[Test]
public async Task Reading_the_summary_keeps_endpoints_whose_sanitized_names_differ_only_in_case()
{
var yesterday = DateOnly.FromDateTime(DateTime.UtcNow).AddDays(-1);
var broker = new Endpoint("Smoke.Billing", ThroughputSource.Broker) { SanitizedName = "Smoke.Billing" };
var monitoring = new Endpoint("smoke.billing", ThroughputSource.Monitoring) { SanitizedName = "smoke.billing" };
await LicensingDataStore.SaveEndpoint(broker);
await LicensingDataStore.SaveEndpoint(monitoring);
await LicensingDataStore.RecordEndpointThroughput(broker.Id.Name, ThroughputSource.Broker, yesterday, 100);
await LicensingDataStore.RecordEndpointThroughput(monitoring.Id.Name, ThroughputSource.Monitoring, yesterday, 30);

// The summary never queries audit instances. The learning transport has no broker query.
var collector = new ThroughputCollector(
NullLogger<ThroughputCollector>.Instance,
LicensingDataStore,
new ThroughputSettings("Particular.ServiceControl", "error", "Learning", "TestCustomer", "6.0.0"),
null,
new MonitoringService(LicensingDataStore),
[]);

var summary = await collector.GetThroughputSummary();
var remaining = await LicensingDataStore.GetAllEndpoints(true);

using (Assert.EnterMultipleScope())
{
Assert.That(summary.Select(endpoint => (endpoint.Name, endpoint.MaxDailyThroughput)),
Is.EquivalentTo(new[] { ("Smoke.Billing", 100L), ("smoke.billing", 30L) }));
Assert.That(remaining.Select(endpoint => endpoint.Id), Is.EquivalentTo(new[] { broker.Id, monitoring.Id }));
}
}
}
Loading