146 lines
4.2 KiB
C#
Raw Normal View History

2024-08-02 08:56:49 +02:00
using Logging;
using Newtonsoft.Json;
2024-08-01 14:50:25 +02:00
namespace OverwatchTranscript
{
2024-08-01 16:25:28 +02:00
public class EventBucket : IFinalizedBucket
2024-08-01 14:50:25 +02:00
{
private const int MaxCount = 10000;
private const int MaxBuffer = 100;
private readonly object _lock = new object();
private bool closed = false;
2024-08-02 08:56:49 +02:00
private readonly ILog log;
2024-08-01 14:50:25 +02:00
private readonly string bucketFile;
private readonly List<EventBucketEntry> buffer = new List<EventBucketEntry>();
private EventBucketEntry? topEntry;
2024-08-02 08:56:49 +02:00
public EventBucket(ILog log, string bucketFile)
2024-08-01 14:50:25 +02:00
{
2024-08-02 08:56:49 +02:00
this.log = log;
2024-08-01 14:50:25 +02:00
this.bucketFile = bucketFile;
if (File.Exists(bucketFile)) throw new Exception("Already exists");
2024-08-02 08:56:49 +02:00
log.Debug("Bucket open: " + bucketFile);
2024-08-01 14:50:25 +02:00
}
public int Count { get; private set; }
public bool IsFull { get; private set; }
public void Add(DateTime utc, object payload)
{
2024-08-01 16:25:28 +02:00
lock (_lock)
{
if (closed) throw new Exception("Already closed");
AddToBuffer(utc, payload);
2024-08-02 08:56:49 +02:00
BufferToFile(emptyBuffer: false);
2024-08-01 16:25:28 +02:00
}
2024-08-01 14:50:25 +02:00
}
2024-08-01 16:25:28 +02:00
public IFinalizedBucket FinalizeBucket()
2024-08-01 14:50:25 +02:00
{
lock (_lock)
{
2024-08-01 16:25:28 +02:00
closed = true;
2024-08-02 08:56:49 +02:00
BufferToFile(emptyBuffer: true);
2024-08-01 14:50:25 +02:00
SortFileByTimestamps();
}
2024-08-02 08:56:49 +02:00
log.Debug($"Finalized bucket with {Count} entries");
2024-08-01 16:25:28 +02:00
return this;
2024-08-01 14:50:25 +02:00
}
public EventBucketEntry? ViewTopEntry()
{
if (!closed) throw new Exception("Bucket not closed yet. FinalizeBucket first.");
return topEntry;
}
public void PopTopEntry()
{
var lines = File.ReadAllLines(bucketFile).ToList();
lines.RemoveAt(0);
File.WriteAllLines(bucketFile, lines);
if (lines.Any())
{
topEntry = JsonConvert.DeserializeObject<EventBucketEntry>(lines[0]);
}
else
{
topEntry = null;
}
}
2024-08-02 08:56:49 +02:00
public override string ToString()
{
return $"EventBucket: " + Count;
}
2024-08-01 14:50:25 +02:00
private void AddToBuffer(DateTime utc, object payload)
{
var typeName = payload.GetType().FullName;
2024-08-01 16:25:28 +02:00
if (string.IsNullOrEmpty(typeName)) throw new Exception("Empty typename for payload");
if (utc == default) throw new Exception("DateTimeUtc not set");
2024-08-01 14:50:25 +02:00
var entry = new EventBucketEntry
{
Utc = utc,
Event = new OverwatchEvent
{
Type = typeName,
Payload = JsonConvert.SerializeObject(payload)
}
};
Count++;
IsFull = Count > MaxCount;
buffer.Add(entry);
}
2024-08-02 08:56:49 +02:00
private void BufferToFile(bool emptyBuffer)
2024-08-01 16:25:28 +02:00
{
2024-08-02 08:56:49 +02:00
if (emptyBuffer || buffer.Count > MaxBuffer)
2024-08-01 16:25:28 +02:00
{
using var file = File.Open(bucketFile, FileMode.Append);
using var writer = new StreamWriter(file);
foreach (var entry in buffer)
{
writer.WriteLine(JsonConvert.SerializeObject(entry));
}
2024-08-02 08:56:49 +02:00
log.Debug($"Bucket wrote {buffer.Count} entries to file.");
2024-08-01 16:25:28 +02:00
buffer.Clear();
}
}
2024-08-01 14:50:25 +02:00
private void SortFileByTimestamps()
{
var lines = File.ReadAllLines(bucketFile);
var entries = lines.Select(JsonConvert.DeserializeObject<EventBucketEntry>)
.Cast<EventBucketEntry>()
.OrderBy(e => e.Utc)
.ToArray();
File.Delete(bucketFile);
File.WriteAllLines(bucketFile, entries.Select(JsonConvert.SerializeObject));
2024-08-02 08:56:49 +02:00
topEntry = entries.FirstOrDefault();
2024-08-01 14:50:25 +02:00
}
2024-08-01 16:25:28 +02:00
}
2024-08-01 14:50:25 +02:00
2024-08-01 16:25:28 +02:00
public interface IFinalizedBucket
{
int Count { get; }
bool IsFull { get; }
EventBucketEntry? ViewTopEntry();
void PopTopEntry();
2024-08-01 14:50:25 +02:00
}
[Serializable]
public class EventBucketEntry
{
public DateTime Utc { get; set; }
public OverwatchEvent Event { get; set; } = new();
}
}