-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathLicensingDataStore.cs
More file actions
319 lines (249 loc) · 14.2 KB
/
LicensingDataStore.cs
File metadata and controls
319 lines (249 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#nullable enable
namespace ServiceControl.Persistence.RavenDB.Throughput;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Models;
using Particular.LicensingComponent.Persistence;
using Particular.LicensingComponent.Contracts;
using Raven.Client.Documents;
using Raven.Client.Documents.Linq;
using Raven.Client.Documents.Queries;
using Raven.Client.Documents.Session;
class LicensingDataStore(
IRavenDocumentStoreProvider storeProvider,
ThroughputDatabaseConfiguration databaseConfiguration) : ILicensingDataStore
{
internal const string ThroughputTimeSeriesName = "INC: throughput data";
const string AuditServiceMetadataDocumentId = "AuditServiceMetadata";
const string BrokerMetadataDocumentId = "BrokerMetadata";
const string ReportMasksDocumentId = "ReportMasks";
static readonly AuditServiceMetadata DefaultAuditServiceMetadata = new([], []);
static readonly BrokerMetadata DefaultBrokerMetadata = new(null, []);
static readonly ReportConfigurationDocument DefaultReportConfiguration = new();
public async Task<IEnumerable<Endpoint>> GetAllEndpoints(bool includePlatformEndpoints, CancellationToken cancellationToken)
{
var store = await storeProvider.GetDocumentStore(cancellationToken);
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
var baseQuery = session.Query<EndpointDocument>();
var query = includePlatformEndpoints
? baseQuery
: baseQuery.Where(document => !document.EndpointIndicators.Contains(EndpointIndicator.PlatformEndpoint.ToString()));
var documents = await query.ToListAsync(cancellationToken);
return documents.Select(document => document.ToEndpoint());
}
public async Task<Endpoint?> GetEndpoint(EndpointIdentifier id, CancellationToken cancellationToken)
{
var store = await storeProvider.GetDocumentStore(cancellationToken);
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
var documentId = id.GenerateDocumentId();
var document = await session.LoadAsync<EndpointDocument>(
documentId,
builder => builder.IncludeTimeSeries(ThroughputTimeSeriesName),
cancellationToken);
var endpoint = document?.ToEndpoint();
if (endpoint != null)
{
var timeSeries = await session
.IncrementalTimeSeriesFor(documentId, ThroughputTimeSeriesName)
.GetAsync(token: cancellationToken);
endpoint.LastCollectedDate = DateOnly.FromDateTime(timeSeries.LastOrDefault()?.Timestamp ?? DateTime.MinValue);
}
return endpoint;
}
public async Task<IEnumerable<(EndpointIdentifier, Endpoint?)>> GetEndpoints(IList<EndpointIdentifier> endpointIds, CancellationToken cancellationToken)
{
var documentIds = endpointIds.Select(id => id.GenerateDocumentId());
var store = await storeProvider.GetDocumentStore(cancellationToken);
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
var query = session.Query<EndpointDocument>()
.Where(document => document.Id.In(documentIds))
.Select(endpoint => new
{
EndpointDocument = endpoint,
LastCollectedDate = RavenQuery.TimeSeries(endpoint, ThroughputTimeSeriesName)
.FromLast(timePeriod => timePeriod.Days(1))
.ToList()
.Results.Last().Timestamp
});
var queryResults = await query.ToListAsync(cancellationToken);
Debug.Assert(session.Advanced.NumberOfRequests == 1, "Query is doing multiple round trips to RavenDB");
return endpointIds.GroupJoin(queryResults,
id => id,
result => result.EndpointDocument.EndpointId,
(id, resultsForId) =>
{
Endpoint? endpoint = null;
var result = resultsForId.SingleOrDefault();
if (result != null)
{
endpoint = result.EndpointDocument.ToEndpoint();
endpoint.LastCollectedDate = DateOnly.FromDateTime(result.LastCollectedDate);
}
return (id, endpoint);
});
}
public async Task SaveEndpoint(Endpoint endpoint, CancellationToken cancellationToken)
{
var document = endpoint.ToEndpointDocument();
var store = await storeProvider.GetDocumentStore(cancellationToken);
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
await session.StoreAsync(document, document.GenerateDocumentId(), cancellationToken);
await session.SaveChangesAsync(cancellationToken);
}
public async Task<IDictionary<string, IEnumerable<ThroughputData>>> GetEndpointThroughputByQueueName(IList<string> queueNames, CancellationToken cancellationToken)
{
var results = queueNames.ToDictionary(queueName => queueName, _ => new List<ThroughputData>() as IEnumerable<ThroughputData>);
var store = await storeProvider.GetDocumentStore(cancellationToken);
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
var from = DateTime.UtcNow.AddMonths(-14);
var query = session.Query<EndpointDocument>()
.Where(document => document.SanitizedName.In(queueNames))
.Include(builder => builder.IncludeTimeSeries(ThroughputTimeSeriesName, from));
var documents = await query.ToListAsync(cancellationToken);
foreach (var document in documents)
{
var timeSeries = await session
.IncrementalTimeSeriesFor(document.GenerateDocumentId(), ThroughputTimeSeriesName)
.GetAsync(from, token: cancellationToken);
if (results.TryGetValue(document.SanitizedName, out var throughputDatas) &&
throughputDatas is List<ThroughputData> throughputDataList)
{
var endpointDailyThroughputs = timeSeries.Select(entry => new EndpointDailyThroughput(DateOnly.FromDateTime(entry.Timestamp), (long)entry.Value));
var throughputData = new ThroughputData(endpointDailyThroughputs)
{
ThroughputSource = document.EndpointId.ThroughputSource
};
throughputDataList.Add(throughputData);
}
}
return results;
}
public async Task RecordEndpointThroughput(string endpointName, ThroughputSource throughputSource, IList<EndpointDailyThroughput> throughput, CancellationToken cancellationToken)
{
if (!throughput.Any())
{
return;
}
var id = new EndpointIdentifier(endpointName, throughputSource);
var store = await storeProvider.GetDocumentStore(cancellationToken);
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
var documentId = id.GenerateDocumentId();
var document = await session.LoadAsync<EndpointDocument>(documentId, cancellationToken) ??
throw new InvalidOperationException($"Endpoint {id.Name} from {id.ThroughputSource} does not exist ");
var timeSeries = session.IncrementalTimeSeriesFor(documentId, ThroughputTimeSeriesName);
foreach (var (date, messageCount) in throughput)
{
timeSeries.Increment(date.ToDateTime(TimeOnly.MinValue), messageCount);
}
await session.SaveChangesAsync(cancellationToken);
}
public async Task UpdateUserIndicatorOnEndpoints(List<UpdateUserIndicator> userIndicatorUpdates, CancellationToken cancellationToken)
{
var updates = userIndicatorUpdates.ToDictionary(u => u.Name, u => u.UserIndicator);
var store = await storeProvider.GetDocumentStore(cancellationToken);
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
var query = session.Query<EndpointDocument>()
.Where(document => document.SanitizedName.In(updates.Keys) || document.EndpointId.Name.In(updates.Keys));
var documents = await query.ToListAsync(cancellationToken);
// Collect sanitized names needing sibling propagation to avoid issuing a query per document in the loop below.
var sanitizedNameToUserIndicator = new Dictionary<string, string>();
foreach (var document in documents)
{
if (updates.TryGetValue(document.SanitizedName, out var newValueFromSanitizedName))
{
document.UserIndicator = newValueFromSanitizedName;
}
else if (updates.TryGetValue(document.EndpointId.Name, out var newValueFromEndpoint))
{
document.UserIndicator = newValueFromEndpoint;
sanitizedNameToUserIndicator[document.SanitizedName] = newValueFromEndpoint;
}
}
if (sanitizedNameToUserIndicator.Count > 0)
{
// One batched query for all sibling documents, instead of one query per document.
var sanitizedNames = sanitizedNameToUserIndicator.Keys.ToList();
var alreadyLoadedIds = documents.Select(d => d.Id).ToHashSet();
var siblingDocuments = await session.Query<EndpointDocument>()
.Where(d => d.SanitizedName.In(sanitizedNames))
.ToListAsync(cancellationToken);
foreach (var sibling in siblingDocuments.Where(d => !alreadyLoadedIds.Contains(d.Id)))
{
if (sanitizedNameToUserIndicator.TryGetValue(sibling.SanitizedName, out var indicator))
{
sibling.UserIndicator = indicator;
}
}
}
await session.SaveChangesAsync(cancellationToken);
}
public async Task<bool> IsThereThroughputForLastXDays(int days, CancellationToken cancellationToken)
{
var store = await storeProvider.GetDocumentStore(cancellationToken);
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
var result = await IsThereThroughputForLastXDaysInternal(session.Query<EndpointDocument>(), days, false, cancellationToken);
return result;
}
public async Task<bool> IsThereThroughputForLastXDaysForSource(int days, ThroughputSource throughputSource, bool includeToday, CancellationToken cancellationToken)
{
var store = await storeProvider.GetDocumentStore(cancellationToken);
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
var baseQuery = session.Query<EndpointDocument>()
.Where(endpoint => endpoint.EndpointId.ThroughputSource == throughputSource);
var result = await IsThereThroughputForLastXDaysInternal(baseQuery, days, includeToday, cancellationToken);
return result;
}
static async Task<bool> IsThereThroughputForLastXDaysInternal(IRavenQueryable<EndpointDocument> baseQuery, int days, bool includeToday, CancellationToken cancellationToken)
{
DateTime fromDate = DateTime.UtcNow.AddDays(-days).Date;
DateTime toDate = includeToday ? DateTime.UtcNow.Date : DateTime.UtcNow.AddDays(-1).Date;
var documents = await baseQuery
.Select(e => RavenQuery.TimeSeries(e, ThroughputTimeSeriesName, fromDate, toDate).ToList())
.ToListAsync(cancellationToken);
return documents.SelectMany(timeSeries => timeSeries.Results).Any();
}
public async Task<BrokerMetadata> GetBrokerMetadata(CancellationToken cancellationToken)
{
var store = await storeProvider.GetDocumentStore(cancellationToken);
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
return await session.LoadAsync<BrokerMetadata>(BrokerMetadataDocumentId, cancellationToken) ?? DefaultBrokerMetadata;
}
public async Task SaveBrokerMetadata(BrokerMetadata brokerMetadata, CancellationToken cancellationToken)
{
var store = await storeProvider.GetDocumentStore(cancellationToken);
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
await session.StoreAsync(brokerMetadata, BrokerMetadataDocumentId, cancellationToken);
await session.SaveChangesAsync(cancellationToken);
}
public async Task<AuditServiceMetadata> GetAuditServiceMetadata(CancellationToken cancellationToken)
{
var store = await storeProvider.GetDocumentStore(cancellationToken);
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
return await session.LoadAsync<AuditServiceMetadata>(AuditServiceMetadataDocumentId, cancellationToken) ?? DefaultAuditServiceMetadata;
}
public async Task SaveAuditServiceMetadata(AuditServiceMetadata auditServiceMetadata, CancellationToken cancellationToken)
{
var store = await storeProvider.GetDocumentStore(cancellationToken);
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
await session.StoreAsync(auditServiceMetadata, AuditServiceMetadataDocumentId, cancellationToken);
await session.SaveChangesAsync(cancellationToken);
}
public async Task<List<string>> GetReportMasks(CancellationToken cancellationToken)
{
var store = await storeProvider.GetDocumentStore(cancellationToken);
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
var config = await session.LoadAsync<ReportConfigurationDocument>(ReportMasksDocumentId, cancellationToken) ?? DefaultReportConfiguration;
return config.MaskedStrings;
}
public async Task SaveReportMasks(List<string> reportMasks, CancellationToken cancellationToken)
{
var store = await storeProvider.GetDocumentStore(cancellationToken);
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);
await session.StoreAsync(new ReportConfigurationDocument { MaskedStrings = reportMasks }, ReportMasksDocumentId, cancellationToken);
await session.SaveChangesAsync(cancellationToken);
}
}