-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathDataStoreBuilder.cs
More file actions
171 lines (137 loc) · 6.02 KB
/
DataStoreBuilder.cs
File metadata and controls
171 lines (137 loc) · 6.02 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
namespace Particular.LicensingComponent.UnitTests.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Contracts;
using Persistence;
class DataStoreBuilder(ILicensingDataStore store)
{
static readonly Random rng = new();
readonly List<Endpoint> endpoints = [];
readonly Dictionary<EndpointIdentifier, List<ThroughputData>> endpointThroughput = [];
public DataStoreBuilder AddEndpoint(string name = null, IEnumerable<ThroughputSource> sources = null)
{
int index = endpoints.Count;
name ??= Guid.NewGuid().ToString("N");
sources ??= [ThroughputSource.Broker];
foreach (ThroughputSource source in sources)
{
endpoints.Add(CreateEndpoint(index, name, source));
}
return this;
}
public DataStoreBuilder ConfigureEndpoint(Action<Endpoint> configureEndpoint) =>
ConfigureEndpoint(null, configureEndpoint);
public DataStoreBuilder ConfigureEndpoint(ThroughputSource? source = null,
Action<Endpoint> configureEndpoint = null)
{
Func<Endpoint, bool> predicate = source is null
? endpoint => true
: endpoint => endpoint.Id.ThroughputSource == source.Value;
Endpoint endpoint = endpoints.LastOrDefault(predicate) ??
throw new InvalidOperationException(
$"Need to add an endpoint before calling {nameof(ConfigureEndpoint)}");
configureEndpoint?.Invoke(endpoint);
return this;
}
public DataStoreBuilder WithThroughput(ThroughputSource? source = null, DateOnly? startDate = null,
int? days = null, IList<long> data = null)
{
Endpoint endpoint = endpoints.LastOrDefault() ??
throw new InvalidOperationException(
$"Need to add an endpoint before calling {nameof(WithThroughput)}");
if (source is not null)
{
Endpoint endpointForSource =
endpoints.SingleOrDefault(e => e.Id.Name == endpoint.Id.Name && e.Id.ThroughputSource == source);
if (endpointForSource is null)
{
endpointForSource =
new Endpoint(endpoint.Id.Name, source.Value) { SanitizedName = endpoint.SanitizedName };
endpoints.Add(endpointForSource);
}
endpoint = endpointForSource;
}
source ??= endpoint.Id.ThroughputSource;
if (endpoints.SingleOrDefault(e => e.Id.Name == endpoint.Id.Name && e.Id.ThroughputSource == source) == null)
{
throw new InvalidOperationException(
$"Need to add endpoint {endpoint.Id.Name}:{source} before calling {nameof(WithThroughput)}");
}
var idForThroughput = new EndpointIdentifier(endpoint.Id.Name, source.Value);
ThroughputData throughput = CreateThroughput(source.Value, startDate, days, data);
if (endpointThroughput.TryGetValue(idForThroughput, out List<ThroughputData> throughputList))
{
throughputList.Add(throughput);
}
else
{
endpointThroughput.Add(idForThroughput, [throughput]);
}
return this;
}
public DataStoreBuilder WithThroughput(ThroughputData throughput)
{
Endpoint endpoint = endpoints.LastOrDefault() ??
throw new InvalidOperationException(
$"Need to add an endpoint before calling {nameof(WithThroughput)}");
var source = endpoint.Id.ThroughputSource;
if (endpoints.SingleOrDefault(e => e.Id.Name == endpoint.Id.Name && e.Id.ThroughputSource == source) == null)
{
throw new InvalidOperationException(
$"Need to add endpoint {endpoint.Id.Name}:{source} before calling {nameof(WithThroughput)}");
}
var idForThroughput = new EndpointIdentifier(endpoint.Id.Name, source);
if (endpointThroughput.TryGetValue(idForThroughput, out List<ThroughputData> throughputList))
{
throughputList.Add(throughput);
}
else
{
endpointThroughput.Add(idForThroughput, [throughput]);
}
return this;
}
public async Task Build()
{
foreach (Endpoint endpoint in endpoints)
{
await store.SaveEndpoint(endpoint, default);
}
;
foreach ((EndpointIdentifier endpointId, List<ThroughputData> throughputList) in endpointThroughput)
{
foreach (ThroughputData throughput in throughputList)
{
await store.RecordEndpointThroughput(endpointId.Name, throughput.ThroughputSource,
throughput.Select(entry => new EndpointDailyThroughput(entry.Key, entry.Value)).ToList(), default);
}
}
}
static Endpoint CreateEndpoint(int index, string name, ThroughputSource source)
{
string endpointName = name ?? $"Endpoint{index + 1}";
return new Endpoint(endpointName, source) { SanitizedName = endpointName };
}
protected static ThroughputData CreateThroughput(ThroughputSource source, DateOnly? startDate = null,
int? days = null, IList<long> data = null)
{
int numberOfThroughputEntries = days is not null && data is not null
? Math.Max(days.Value, data.Count)
: days ?? data?.Count ?? 1;
if (startDate == null)
{
startDate = DateOnly.FromDateTime(DateTime.UtcNow).AddDays(-numberOfThroughputEntries);
}
IEnumerable<EndpointDailyThroughput> throughput = Enumerable.Range(0, numberOfThroughputEntries)
.Select(i =>
{
long messageCount = data is not null && i < data.Count
? data[i]
: rng.Next(100);
return new EndpointDailyThroughput(startDate.Value.AddDays(i), messageCount);
});
return new ThroughputData(throughput) { ThroughputSource = source };
}
}