2
0
mirror of synced 2025-02-20 20:18:11 +00:00

91 lines
3.2 KiB
C#
Raw Normal View History

2024-08-06 15:56:42 +02:00
using CodexPlugin.OverwatchSupport;
using OverwatchTranscript;
2024-08-07 11:18:40 +02:00
namespace TranscriptAnalysis.Receivers
2024-08-06 15:56:42 +02:00
{
2024-08-07 11:18:40 +02:00
public class DuplicateBlocksReceived : BaseReceiver<OverwatchCodexEvent>
2024-08-06 15:56:42 +02:00
{
2024-10-14 09:11:29 +02:00
public static List<int> Counts = new List<int>();
2024-10-17 09:37:15 +02:00
private long uploadSize;
2024-10-14 09:11:29 +02:00
2024-08-07 11:18:40 +02:00
public override string Name => "BlocksReceived";
2024-08-06 15:56:42 +02:00
2024-08-07 11:18:40 +02:00
public override void Receive(ActivateEvent<OverwatchCodexEvent> @event)
2024-08-06 15:56:42 +02:00
{
2024-08-07 11:18:40 +02:00
if (@event.Payload.BlockReceived != null)
{
Handle(@event.Payload, @event.Payload.BlockReceived);
}
2024-10-17 09:37:15 +02:00
if (@event.Payload.FileUploaded != null)
{
var uploadEvent = @event.Payload.FileUploaded;
uploadSize = uploadEvent.ByteSize;
}
2024-08-06 15:56:42 +02:00
}
2024-08-07 11:18:40 +02:00
public override void Finish()
2024-08-06 15:56:42 +02:00
{
Log("Number of BlockReceived events seen: " + seen);
2024-10-17 09:37:15 +02:00
var csv = CsvWriter.CreateNew();
2024-08-06 15:56:42 +02:00
var totalReceived = peerIdBlockAddrCount.Sum(a => a.Value.Sum(p => p.Value));
var maxRepeats = peerIdBlockAddrCount.Max(a => a.Value.Max(p => p.Value));
var occurances = new OccuranceMap();
2024-08-06 15:56:42 +02:00
foreach (var peerPair in peerIdBlockAddrCount)
{
foreach (var blockCountPair in peerPair.Value)
2024-08-06 15:56:42 +02:00
{
occurances.Add(blockCountPair.Value);
2024-08-07 11:18:40 +02:00
}
2024-08-06 15:56:42 +02:00
}
2024-10-14 09:11:29 +02:00
if (Counts.Any()) throw new Exception("Should be empty");
2024-08-06 15:56:42 +02:00
float t = totalReceived;
2024-10-17 09:37:15 +02:00
csv.GetColumn("numNodes", Header.Nodes.Length);
csv.GetColumn("filesize", uploadSize.ToString());
var receiveCountColumn = csv.GetColumn("receiveCount", 0.0f);
var occuranceColumn = csv.GetColumn("occurance", 0.0f);
occurances.PrintContinous((i, count) =>
2024-08-06 15:56:42 +02:00
{
float n = count;
2024-08-06 15:56:42 +02:00
float p = 100.0f * (n / t);
Log($"Block received {i} times = {count}x ({p}%)");
2024-10-14 09:11:29 +02:00
Counts.Add(count);
2024-10-17 09:37:15 +02:00
csv.AddRow(
new CsvCell(receiveCountColumn, i),
new CsvCell(occuranceColumn, count)
);
});
2024-10-14 09:11:29 +02:00
2024-10-17 09:37:15 +02:00
CsvWriter.Write(csv, SourceFilename + "_blockduplicates.csv");
2024-08-06 15:56:42 +02:00
}
private int seen = 0;
private readonly Dictionary<string, Dictionary<string, int>> peerIdBlockAddrCount = new Dictionary<string, Dictionary<string, int>>();
private void Handle(OverwatchCodexEvent payload, BlockReceivedEvent blockReceived)
{
2024-08-14 14:13:00 +02:00
var receiverPeerId = GetPeerId(payload.NodeIdentity);
2024-10-14 09:11:29 +02:00
if (receiverPeerId == null) return;
2024-08-06 15:56:42 +02:00
var blockAddress = blockReceived.BlockAddress;
seen++;
if (!peerIdBlockAddrCount.ContainsKey(receiverPeerId))
{
peerIdBlockAddrCount.Add(receiverPeerId, new Dictionary<string, int>());
}
var blockAddCount = peerIdBlockAddrCount[receiverPeerId];
if (!blockAddCount.ContainsKey(blockAddress))
{
blockAddCount.Add(blockAddress, 1);
}
else
{
blockAddCount[blockAddress]++;
}
}
}
}