2024-08-02 08:44:15 +00:00
|
|
|
|
using Logging;
|
|
|
|
|
using Newtonsoft.Json;
|
2024-08-01 14:25:28 +00:00
|
|
|
|
|
|
|
|
|
namespace OverwatchTranscript
|
|
|
|
|
{
|
|
|
|
|
public class MomentReferenceBuilder
|
|
|
|
|
{
|
2024-08-02 15:52:44 +00:00
|
|
|
|
private const int MaxMomentsPerReference = 10000;
|
2024-08-02 08:44:15 +00:00
|
|
|
|
private readonly ILog log;
|
2024-08-01 14:25:28 +00:00
|
|
|
|
private readonly string workingDir;
|
|
|
|
|
|
2024-08-02 08:44:15 +00:00
|
|
|
|
public MomentReferenceBuilder(ILog log, string workingDir)
|
2024-08-01 14:25:28 +00:00
|
|
|
|
{
|
2024-08-02 08:44:15 +00:00
|
|
|
|
this.log = log;
|
2024-08-01 14:25:28 +00:00
|
|
|
|
this.workingDir = workingDir;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-02 12:25:38 +00:00
|
|
|
|
public OverwatchMomentReference[] Build(IFinalizedBucket[] finalizedBuckets)
|
2024-08-01 14:25:28 +00:00
|
|
|
|
{
|
|
|
|
|
var result = new List<OverwatchMomentReference>();
|
2024-08-02 08:44:15 +00:00
|
|
|
|
var currentBuilder = new Builder(log, workingDir);
|
2024-08-01 14:25:28 +00:00
|
|
|
|
|
2024-08-02 12:25:38 +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
|
|
|
|
{
|
2024-08-02 12:25:38 +00:00
|
|
|
|
buckets.RemoveAll(b => b.IsEmpty);
|
|
|
|
|
if (!buckets.Any()) break;
|
|
|
|
|
|
2024-08-01 14:25:28 +00:00
|
|
|
|
var earliestUtc = GetEarliestUtc(buckets);
|
2024-08-02 15:52:44 +00:00
|
|
|
|
if (earliestUtc == null) continue;
|
|
|
|
|
|
2024-08-02 12:25:38 +00:00
|
|
|
|
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());
|
2024-08-02 08:44:15 +00:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-02 12:25:38 +00:00
|
|
|
|
private OverwatchMoment ConvertTopsToMoment(List<BucketTop> tops)
|
2024-08-01 14:25:28 +00:00
|
|
|
|
{
|
2024-08-02 12:25:38 +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
|
|
|
|
|
{
|
2024-08-02 12:25:38 +00:00
|
|
|
|
Utc = tops[0].Utc,
|
|
|
|
|
Events = tops.SelectMany(e => e.Events).ToArray()
|
2024-08-01 14:25:28 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-02 12:25:38 +00:00
|
|
|
|
private List<BucketTop> CollectAllTopsForUtc(DateTime earliestUtc, List<IFinalizedBucket> buckets)
|
2024-08-01 14:25:28 +00:00
|
|
|
|
{
|
2024-08-02 12:25:38 +00:00
|
|
|
|
var result = new List<BucketTop>();
|
2024-08-01 14:25:28 +00:00
|
|
|
|
|
|
|
|
|
foreach (var bucket in buckets)
|
|
|
|
|
{
|
2024-08-02 12:25:38 +00:00
|
|
|
|
if (bucket.IsEmpty) continue;
|
|
|
|
|
|
|
|
|
|
var utc = bucket.SeeTopUtc();
|
|
|
|
|
if (utc == null) continue;
|
|
|
|
|
|
|
|
|
|
if (utc.Value == earliestUtc)
|
2024-08-01 14:25:28 +00:00
|
|
|
|
{
|
2024-08-02 12:25:38 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-02 12:25:38 +00:00
|
|
|
|
private DateTime? GetEarliestUtc(List<IFinalizedBucket> buckets)
|
2024-08-01 14:25:28 +00:00
|
|
|
|
{
|
|
|
|
|
var earliest = DateTime.MaxValue;
|
|
|
|
|
foreach (var bucket in buckets)
|
|
|
|
|
{
|
2024-08-02 12:25:38 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
2024-08-02 08:44:15 +00:00
|
|
|
|
private readonly ILog log;
|
2024-08-05 12:16:44 +00:00
|
|
|
|
private readonly string workingDir;
|
2024-08-01 14:25:28 +00:00
|
|
|
|
private OverwatchMomentReference reference;
|
2024-08-02 12:25:38 +00:00
|
|
|
|
private readonly ActionQueue queue = new ActionQueue();
|
2024-08-01 14:25:28 +00:00
|
|
|
|
|
2024-08-02 08:44:15 +00:00
|
|
|
|
public Builder(ILog log, string workingDir)
|
2024-08-01 14:25:28 +00:00
|
|
|
|
{
|
|
|
|
|
reference = new OverwatchMomentReference
|
|
|
|
|
{
|
2024-08-05 12:16:44 +00:00
|
|
|
|
MomentsFile = Guid.NewGuid().ToString(),
|
2024-08-01 14:25:28 +00:00
|
|
|
|
EarliestUtc = DateTime.MaxValue,
|
|
|
|
|
LatestUtc = DateTime.MinValue,
|
|
|
|
|
NumberOfEvents = 0,
|
|
|
|
|
NumberOfMoments = 0,
|
|
|
|
|
};
|
2024-08-02 08:44:15 +00:00
|
|
|
|
this.log = log;
|
2024-08-05 12:16:44 +00:00
|
|
|
|
this.workingDir = workingDir;
|
2024-08-02 12:25:38 +00:00
|
|
|
|
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;
|
2024-08-02 12:25:38 +00:00
|
|
|
|
|
2024-08-05 12:16:44 +00:00
|
|
|
|
var filePath = Path.Combine(workingDir, reference.MomentsFile);
|
|
|
|
|
|
2024-08-02 12:25:38 +00:00
|
|
|
|
queue.Add(() =>
|
|
|
|
|
{
|
2024-08-05 12:16:44 +00:00
|
|
|
|
File.AppendAllLines(filePath, new[]
|
2024-08-02 12:25:38 +00:00
|
|
|
|
{
|
2024-08-21 07:51:22 +00:00
|
|
|
|
Json.Serialize(moment)
|
2024-08-02 12:25:38 +00:00
|
|
|
|
});
|
|
|
|
|
});
|
2024-08-01 14:25:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OverwatchMomentReference Build()
|
|
|
|
|
{
|
2024-08-02 12:25:38 +00:00
|
|
|
|
queue.StopAndJoin();
|
|
|
|
|
|
2024-08-02 08:44:15 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|