Skip to content

Commit dd100b9

Browse files
authored
Licence metadata management (#5998)
* storage and application of licencedetails amendment data to support management of license metadata in ServicePulse
1 parent 620563c commit dd100b9

11 files changed

Lines changed: 389 additions & 8 deletions

File tree

‎src/Particular.LicensingComponent.Contracts/LicensedEndpointDetails.cs‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,51 @@ public class LicensedEndpointDetails
66
public LicensedEndpoint[] Endpoints { get; set; } = [];
77
public QueueIdentity[] InfrastructureQueues { get; set; } = [];
88
public QueueIdentity[] ExcludedQueues { get; set; } = [];
9+
public QueueIdentity[] UnassignedQueues { get; set; } = [];
910
public string? ServiceEndDate { get; set; }
1011
public Product[] Products { get; set; } = [];
1112
public bool ValidId { get; set; }
1213
}
1314

15+
public class LicensedEndpointDetailsAmendment
16+
{
17+
public LicensedEndpointAmendment[] Endpoints { get; set; } = [];
18+
public string[] RemovedEndpointIds { get; set; } = [];
19+
public QueueIdentity[] InfrastructureQueues { get; set; } = [];
20+
public QueueIdentity[] ExcludedQueues { get; set; } = [];
21+
public QueueIdentity[] UnassignedQueues { get; set; } = [];
22+
public QueueIdentity[] UnmappedQueues { get; set; } = [];
23+
24+
}
25+
1426
public class Product
1527
{
1628
public string? ProductCode { get; set; }
1729
public int? MonthlyThroughput { get; set; }
1830
}
1931

20-
public class QueueIdentity
32+
public record QueueIdentity
2133
{
2234
public string? NameHash { get; set; }
2335
public string? ScopeHash { get; set; }
36+
public string? OriginalLocation { get; set; }
2437
}
2538

26-
public class LicensedEndpoint
39+
public record LicensedEndpoint
2740
{
41+
public string? ClientId { get; set; }
2842
public string? Name { get; set; }
2943
public int? Classification { get; set; }
3044
public string? EndpointSize { get; set; }
45+
3146
public QueueIdentity[] Queues { get; set; } = [];
3247
}
48+
49+
public class LicensedEndpointAmendment
50+
{
51+
public string? ClientId { get; set; }
52+
public string? Name { get; set; }
53+
public int? Classification { get; set; }
54+
public QueueIdentity[] AmendedQueues { get; set; } = [];
55+
}
3356
}

‎src/Particular.LicensingComponent.Persistence.InMemory/InMemoryLicensingDataStore.cs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class InMemoryLicensingDataStore : ILicensingDataStore
1616
AuditServiceMetadata auditServiceMetadata = new([], []);
1717
List<string> reportMasks = [];
1818
LicensedEndpointDetails? endpointDetails = null;
19+
LicensedEndpointDetailsAmendment? endpointDetailsAmendment = null;
1920

2021
public Task<IEnumerable<Endpoint>> GetAllEndpoints(bool includePlatformEndpoints, CancellationToken cancellationToken = default)
2122
{
@@ -175,11 +176,19 @@ public Task SaveReportMasks(List<string> reportMasks, CancellationToken cancella
175176
return Task.CompletedTask;
176177
}
177178

178-
public Task<LicensedEndpointDetails?> GetLicensedEndpointDetails(CancellationToken cancellationToken = default) => Task.FromResult(endpointDetails);
179+
public Task<LicensedEndpointDetails?> GetLicensedEndpointDetails(CancellationToken cancellationToken = default) => Task.FromResult(endpointDetails?.Amend(endpointDetailsAmendment));
179180

180181
public Task SaveLicensedEndpointDetails(LicensedEndpointDetails result, CancellationToken cancellationToken = default)
181182
{
182183
endpointDetails = result;
184+
endpointDetailsAmendment = new LicensedEndpointDetailsAmendment();
185+
return Task.CompletedTask;
186+
}
187+
188+
public Task SaveLicensedEndpointDetailsAmendment(LicensedEndpointDetailsAmendment result, CancellationToken cancellationToken = default)
189+
{
190+
result.UnmappedQueues = [.. endpointDetailsAmendment?.UnmappedQueues ?? [], .. result.UnmappedQueues];
191+
endpointDetailsAmendment = result;
183192
return Task.CompletedTask;
184193
}
185194

‎src/Particular.LicensingComponent.Persistence/ILicensingDataStore.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ Task RecordEndpointThroughput(string endpointName, ThroughputSource throughputSo
4141

4242
Task<LicensedEndpointDetails?> GetLicensedEndpointDetails(CancellationToken cancellationToken = default);
4343
Task SaveLicensedEndpointDetails(LicensedEndpointDetails result, CancellationToken cancellationToken = default);
44+
Task SaveLicensedEndpointDetailsAmendment(LicensedEndpointDetailsAmendment result, CancellationToken cancellationToken = default);
4445
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
namespace Particular.LicensingComponent.Persistence
2+
{
3+
using System;
4+
using Particular.LicensingComponent.Contracts;
5+
6+
public static class LicenseMetadata
7+
{
8+
extension(LicensedEndpointDetails baseDetails)
9+
{
10+
public LicensedEndpointDetails Amend(LicensedEndpointDetailsAmendment? amendments)
11+
{
12+
if (amendments is null)
13+
{
14+
return baseDetails;
15+
}
16+
17+
//Note: baseDetails.UnassignedQueues will always be empty before amendments
18+
baseDetails.UnassignedQueues = amendments.UnassignedQueues;
19+
baseDetails.ExcludedQueues = [.. baseDetails.ExcludedQueues.Except(
20+
[
21+
.. amendments.UnassignedQueues,
22+
.. amendments.UnmappedQueues,
23+
.. amendments.InfrastructureQueues,
24+
.. amendments.Endpoints.SelectMany(queue => queue.AmendedQueues)]),
25+
.. amendments.ExcludedQueues
26+
];
27+
baseDetails.InfrastructureQueues = [.. baseDetails.InfrastructureQueues.Except(
28+
[
29+
.. amendments.UnassignedQueues,
30+
.. amendments.UnmappedQueues,
31+
.. amendments.ExcludedQueues,
32+
.. amendments.Endpoints.SelectMany(queue => queue.AmendedQueues)]),
33+
.. amendments.InfrastructureQueues
34+
];
35+
baseDetails.Endpoints = [
36+
.. baseDetails.Endpoints.Where(endpoint => !amendments.RemovedEndpointIds.Contains(endpoint.ClientId))
37+
.Select(endpoint =>
38+
{
39+
var amendment = amendments.Endpoints.FirstOrDefault(amendment => amendment.ClientId == endpoint.ClientId);
40+
return endpoint with
41+
{
42+
Name = amendment?.Name ?? endpoint.Name,
43+
Classification = amendment?.Classification ?? endpoint.Classification,
44+
Queues = [.. endpoint.Queues.Except(
45+
[
46+
.. amendments.UnassignedQueues,
47+
.. amendments.UnmappedQueues,
48+
.. amendments.ExcludedQueues,
49+
.. amendments.InfrastructureQueues,
50+
.. amendments.Endpoints.SelectMany(queue => queue.AmendedQueues)]),
51+
.. amendment?.AmendedQueues ?? []
52+
]
53+
};
54+
}),
55+
.. amendments.Endpoints.Where(endpoint => !baseDetails.Endpoints.Any(baseEndpoint => baseEndpoint.ClientId == endpoint.ClientId))
56+
.Select(endpoint => new LicensedEndpoint
57+
{
58+
ClientId = endpoint.ClientId,
59+
Name = endpoint.Name,
60+
Classification = endpoint.Classification,
61+
Queues = endpoint.AmendedQueues
62+
})];
63+
64+
return baseDetails;
65+
}
66+
}
67+
}
68+
}

‎src/Particular.LicensingComponent.UnitTests/MonitoringService_Tests.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ public Task<LicensedEndpointDetails> GetLicensedEndpointDetails(CancellationToke
264264
public Task SaveLicensedEndpointDetails(LicensedEndpointDetails result, CancellationToken cancellationToken = default) =>
265265
throw new NotSupportedException();
266266
public Task RemoveEndpoints(EndpointIdentifier[] endpointIds, CancellationToken cancellationToken = default) => throw new NotImplementedException();
267+
public Task SaveLicensedEndpointDetailsAmendment(LicensedEndpointDetailsAmendment result, CancellationToken cancellationToken = default) => throw new NotImplementedException();
267268
}
268269

269270
class BrokerThroughputQuery_WithSanitization : IBrokerThroughputQuery

‎src/ServiceControl.Persistence.EFCore/Implementation/LicensingDataStore.cs‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ namespace ServiceControl.Persistence.EFCore.Implementation;
44
using Microsoft.Extensions.DependencyInjection;
55
using Particular.LicensingComponent.Contracts;
66
using Particular.LicensingComponent.Persistence;
7-
using ServiceControl.Persistence.EFCore.DbContexts;
87
using ServiceControl.Persistence.EFCore.Entities;
98
using ServiceControl.Persistence.EFCore.Infrastructure;
109

@@ -310,10 +309,35 @@ public Task SaveReportMasks(List<string> reportMasks, CancellationToken cancella
310309
ExecuteWithDbContext((context, token) => context.StoreSetting(SettingKeys.ReportMasks, reportMasks, token), cancellationToken);
311310

312311
public Task<LicensedEndpointDetails?> GetLicensedEndpointDetails(CancellationToken cancellationToken = default) =>
313-
ExecuteWithDbContext((context, token) => context.GetSetting<LicensedEndpointDetails>(SettingKeys.LicensedEndpointDetails, token), cancellationToken);
312+
ExecuteWithDbContext(async (context, token) =>
313+
{
314+
var baseDetails = await context.GetSetting<LicensedEndpointDetails>(SettingKeys.LicensedEndpointDetails, token);
315+
if (baseDetails is null)
316+
{
317+
return null;
318+
}
319+
var amendments = await context.GetSetting<LicensedEndpointDetailsAmendment>(SettingKeys.LicensedEndpointDetailsAmendment, token);
320+
return baseDetails.Amend(amendments);
321+
}, cancellationToken);
314322

315323
public Task SaveLicensedEndpointDetails(LicensedEndpointDetails result, CancellationToken cancellationToken = default) =>
316-
ExecuteWithDbContext((context, token) => context.StoreSetting(SettingKeys.LicensedEndpointDetails, result, token), cancellationToken);
324+
ExecuteWithDbContext(async (context, token) =>
325+
{
326+
await context.StoreSetting(SettingKeys.LicensedEndpointDetails, result, token);
327+
await context.StoreSetting(SettingKeys.LicensedEndpointDetailsAmendment, new LicensedEndpointDetailsAmendment(), token);
328+
}, cancellationToken);
329+
330+
public Task SaveLicensedEndpointDetailsAmendment(LicensedEndpointDetailsAmendment result, CancellationToken cancellationToken = default) =>
331+
ExecuteWithDbContext(async (context, token) =>
332+
{
333+
var existing = await context.GetSetting<LicensedEndpointDetailsAmendment>(SettingKeys.LicensedEndpointDetailsAmendment, token);
334+
if (existing is not null)
335+
{
336+
// these do not round-trip
337+
result.UnmappedQueues = [.. existing.UnmappedQueues, .. result.UnmappedQueues];
338+
}
339+
await context.StoreSetting(SettingKeys.LicensedEndpointDetailsAmendment, result, token);
340+
}, cancellationToken);
317341

318342
static Endpoint ToEndpoint(LicensingEndpointEntity row) => ToEndpoint(row, lastCollectedDate: null);
319343

‎src/ServiceControl.Persistence.EFCore/Infrastructure/SettingKeys.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ static class SettingKeys
88
public const string AuditServiceMetadata = "AuditServiceMetadata";
99
public const string ReportMasks = "ReportMasks";
1010
public const string LicensedEndpointDetails = "LicensedEndpointDetails";
11+
public const string LicensedEndpointDetailsAmendment = "LicensedEndpointDetailsAmendment";
1112
public const string NotificationEmails = "NotificationEmails";
1213
}

‎src/ServiceControl.Persistence.RavenDB/Throughput/LicensingDataStore.cs‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class LicensingDataStore(
2525
const string BrokerMetadataDocumentId = "BrokerMetadata";
2626
const string ReportMasksDocumentId = "ReportMasks";
2727
const string LicencedEndpointDetailsDocumentId = "LicensedEndpointDetails";
28+
const string LicencedEndpointDetailsAmendmentDocumentId = "LicensedEndpointDetailsAmendment";
2829

2930
static readonly AuditServiceMetadata DefaultAuditServiceMetadata = new([], []);
3031
static readonly ReportConfigurationDocument DefaultReportConfiguration = new();
@@ -161,7 +162,8 @@ public async Task<IDictionary<string, IEnumerable<ThroughputData>>> GetEndpointT
161162
{
162163
var endpointDailyThroughputs = timeSeries
163164
.Where(entry => !throughputMaxDate.HasValue || entry.Timestamp < maxDateTime)
164-
.Select(entry => new EndpointDailyThroughput(DateOnly.FromDateTime(entry.Timestamp), (long)entry.Value));
165+
.Select(entry => new EndpointDailyThroughput(DateOnly.FromDateTime(entry.Timestamp), (long)entry.Value))
166+
.ToList();
165167
var throughputData = new ThroughputData(endpointDailyThroughputs)
166168
{
167169
ThroughputSource = document.EndpointId.ThroughputSource
@@ -341,7 +343,13 @@ public async Task SaveReportMasks(List<string> reportMasks, CancellationToken ca
341343
var store = await storeProvider.GetDocumentStore(cancellationToken);
342344
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
343345

344-
return await session.LoadAsync<LicensedEndpointDetails>(LicencedEndpointDetailsDocumentId, cancellationToken);
346+
var baseDetails = await session.LoadAsync<LicensedEndpointDetails>(LicencedEndpointDetailsDocumentId, cancellationToken);
347+
if (baseDetails is null)
348+
{
349+
return null;
350+
}
351+
var amendments = await session.LoadAsync<LicensedEndpointDetailsAmendment>(LicencedEndpointDetailsAmendmentDocumentId, cancellationToken);
352+
return baseDetails.Amend(amendments);
345353
}
346354

347355
public async Task SaveLicensedEndpointDetails(LicensedEndpointDetails result, CancellationToken cancellationToken = default)
@@ -350,6 +358,24 @@ public async Task SaveLicensedEndpointDetails(LicensedEndpointDetails result, Ca
350358
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
351359

352360
await session.StoreAsync(result, LicencedEndpointDetailsDocumentId, cancellationToken);
361+
await session.StoreAsync(new LicensedEndpointDetailsAmendment(), LicencedEndpointDetailsAmendmentDocumentId, cancellationToken);
362+
await session.SaveChangesAsync(cancellationToken);
363+
}
364+
365+
public async Task SaveLicensedEndpointDetailsAmendment(LicensedEndpointDetailsAmendment result, CancellationToken cancellationToken = default)
366+
{
367+
var store = await storeProvider.GetDocumentStore(cancellationToken);
368+
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
369+
370+
var existing = await session.LoadAsync<LicensedEndpointDetailsAmendment>(LicencedEndpointDetailsAmendmentDocumentId, cancellationToken);
371+
if (existing is not null)
372+
{
373+
// these do not round-trip
374+
result.UnmappedQueues = [.. existing.UnmappedQueues, .. result.UnmappedQueues];
375+
session.Advanced.Evict(existing);
376+
}
377+
378+
await session.StoreAsync(result, LicencedEndpointDetailsAmendmentDocumentId, cancellationToken);
353379
await session.SaveChangesAsync(cancellationToken);
354380
}
355381
}

0 commit comments

Comments
 (0)