cs-codex-dist-tests/Framework/OverwatchTranscript/MomentReferenceBuilder.cs

152 lines
4.9 KiB
C#
Raw Normal View History

using Logging;
using Newtonsoft.Json;
2024-08-01 14:25:28 +00:00
namespace OverwatchTranscript
{
public class MomentReferenceBuilder
{
private const int MaxMomentsPerReference = 10000;
private readonly ILog log;
2024-08-01 14:25:28 +00:00
private readonly string workingDir;
public MomentReferenceBuilder(ILog log, string workingDir)
2024-08-01 14:25:28 +00:00
{
this.log = log;
2024-08-01 14:25:28 +00:00
this.workingDir = workingDir;
}
public OverwatchMomentReference[] Build(IFinalizedBucket[] finalizedBuckets)
2024-08-01 14:25:28 +00:00
{
var result = new List<OverwatchMomentReference>();
var currentBuilder = new Builder(log, workingDir);
2024-08-01 14:25:28 +00:00
var buckets = finalizedBuckets.ToList();
log.Debug($"Building references for {buckets.Count} buckets.");
while (buckets.Any())
2024-08-01 14:25:28 +00:00
{
buckets.RemoveAll(b => b.IsEmpty);
if (!buckets.Any()) break;
2024-08-01 14:25:28 +00:00
var earliestUtc = GetEarliestUtc(buckets);
if (earliestUtc == null) continue;
var tops = CollectAllTopsForUtc(earliestUtc.Value, buckets);
var moment = ConvertTopsToMoment(tops);
2024-08-01 14:25:28 +00:00
currentBuilder.Add(moment);
if (currentBuilder.NumberOfMoments == MaxMomentsPerReference)
{
result.Add(currentBuilder.Build());
currentBuilder = new Builder(log, workingDir);
2024-08-01 14:25:28 +00:00
}
}
2024-08-02 06:56:49 +00:00
if (currentBuilder.NumberOfMoments > 0)
{
result.Add(currentBuilder.Build());
}
2024-08-01 14:25:28 +00:00
return result.ToArray();
}
private OverwatchMoment ConvertTopsToMoment(List<BucketTop> tops)
2024-08-01 14:25:28 +00:00
{
var discintUtc = tops.Select(e => e.Utc).Distinct().ToArray();
2024-08-01 14:25:28 +00:00
if (discintUtc.Length != 1) throw new Exception("UTC mixing in moment construction.");
return new OverwatchMoment
{
Utc = tops[0].Utc,
Events = tops.SelectMany(e => e.Events).ToArray()
2024-08-01 14:25:28 +00:00
};
}
private List<BucketTop> CollectAllTopsForUtc(DateTime earliestUtc, List<IFinalizedBucket> buckets)
2024-08-01 14:25:28 +00:00
{
var result = new List<BucketTop>();
2024-08-01 14:25:28 +00:00
foreach (var bucket in buckets)
{
if (bucket.IsEmpty) continue;
var utc = bucket.SeeTopUtc();
if (utc == null) continue;
if (utc.Value == earliestUtc)
2024-08-01 14:25:28 +00:00
{
var top = bucket.TakeTop();
if (top == null) throw new Exception("top was null after top utc was not");
2024-08-01 14:25:28 +00:00
result.Add(top);
}
}
return result;
}
private DateTime? GetEarliestUtc(List<IFinalizedBucket> buckets)
2024-08-01 14:25:28 +00:00
{
var earliest = DateTime.MaxValue;
foreach (var bucket in buckets)
{
var utc = bucket.SeeTopUtc();
if (utc == null) return null;
if (utc.Value < earliest) earliest = utc.Value;
2024-08-01 14:25:28 +00:00
}
return earliest;
}
public class Builder
{
private readonly ILog log;
private readonly string workingDir;
2024-08-01 14:25:28 +00:00
private OverwatchMomentReference reference;
private readonly ActionQueue queue = new ActionQueue();
2024-08-01 14:25:28 +00:00
public Builder(ILog log, string workingDir)
2024-08-01 14:25:28 +00:00
{
reference = new OverwatchMomentReference
{
MomentsFile = Guid.NewGuid().ToString(),
2024-08-01 14:25:28 +00:00
EarliestUtc = DateTime.MaxValue,
LatestUtc = DateTime.MinValue,
NumberOfEvents = 0,
NumberOfMoments = 0,
};
this.log = log;
this.workingDir = workingDir;
queue.Start();
2024-08-01 14:25:28 +00:00
}
public int NumberOfMoments => reference.NumberOfMoments;
public void Add(OverwatchMoment moment)
{
if (moment.Utc < reference.EarliestUtc) reference.EarliestUtc = moment.Utc;
if (moment.Utc > reference.LatestUtc) reference.LatestUtc = moment.Utc;
reference.NumberOfMoments++;
reference.NumberOfEvents += moment.Events.Length;
var filePath = Path.Combine(workingDir, reference.MomentsFile);
queue.Add(() =>
{
File.AppendAllLines(filePath, new[]
{
Json.Serialize(moment)
});
});
2024-08-01 14:25:28 +00:00
}
public OverwatchMomentReference Build()
{
queue.StopAndJoin();
log.Debug($"Created reference with {reference.NumberOfMoments} moments and {reference.NumberOfEvents} events...");
2024-08-01 14:25:28 +00:00
var result = reference;
reference = null!;
return result;
}
}
}
}