130 lines
4.4 KiB
C#
Raw Permalink Normal View History

using LogosStorageClient;
using StoragePlugin.OverwatchSupport.LineConverters;
2025-01-16 10:15:02 +01:00
using Logging;
2024-07-26 09:14:46 +02:00
using OverwatchTranscript;
using Utils;
namespace StoragePlugin.OverwatchSupport
2024-07-26 09:14:46 +02:00
{
public class LogosStorageLogConverter
2024-07-26 09:14:46 +02:00
{
private readonly ITranscriptWriter writer;
private readonly LogosStorageTranscriptWriterConfig config;
2024-08-13 14:21:15 +02:00
private readonly IdentityMap identityMap;
2024-07-26 09:14:46 +02:00
public LogosStorageLogConverter(ITranscriptWriter writer, LogosStorageTranscriptWriterConfig config, IdentityMap identityMap)
2024-07-26 09:14:46 +02:00
{
this.writer = writer;
this.config = config;
2024-08-13 14:21:15 +02:00
this.identityMap = identityMap;
2024-07-26 09:14:46 +02:00
}
public void ProcessLog(IDownloadedLog log)
{
var name = DetermineName(log);
2024-08-13 14:21:15 +02:00
var identityIndex = identityMap.GetIndex(name);
var runner = new ConversionRunner(writer, config, identityMap, identityIndex);
2024-08-13 14:21:15 +02:00
runner.Run(log);
2024-07-26 09:14:46 +02:00
}
private string DetermineName(IDownloadedLog log)
{
// Expected string:
// Downloading container log for '<Downloader1>'
var nameLine = log.FindLinesThatContain("Downloading container log for").First();
return Str.Between(nameLine, "'", "'");
}
2024-07-26 09:14:46 +02:00
}
public class ConversionRunner
{
private readonly ITranscriptWriter writer;
2024-08-13 14:21:15 +02:00
private readonly IdentityMap nameIdMap;
private readonly int nodeIdentityIndex;
private readonly ILineConverter[] converters;
public ConversionRunner(ITranscriptWriter writer, LogosStorageTranscriptWriterConfig config, IdentityMap nameIdMap, int nodeIdentityIndex)
{
2024-08-13 14:21:15 +02:00
this.nodeIdentityIndex = nodeIdentityIndex;
this.writer = writer;
2024-08-08 10:24:16 +02:00
this.nameIdMap = nameIdMap;
converters = CreateConverters(config).ToArray();
}
private IEnumerable<ILineConverter> CreateConverters(LogosStorageTranscriptWriterConfig config)
{
if (config.IncludeBlockReceivedEvents)
{
yield return new BlockReceivedLineConverter();
}
yield return new BootstrapLineConverter();
yield return new DialSuccessfulLineConverter();
yield return new PeerDroppedLineConverter();
}
public void Run(IDownloadedLog log)
{
log.IterateLines(line =>
{
foreach (var converter in converters)
{
ProcessLine(line, converter);
}
});
}
private void AddEvent(DateTime utc, Action<OverwatchLogosStorageEvent> action)
{
var e = new OverwatchLogosStorageEvent
{
2024-08-13 14:21:15 +02:00
NodeIdentity = nodeIdentityIndex,
};
action(e);
2024-07-29 11:02:24 +02:00
e.Write(utc, writer);
}
private void ProcessLine(string line, ILineConverter converter)
{
if (!line.Contains(converter.Interest)) return;
var logosStorageLogLine = LogosStorageLogLine.Parse(line);
2024-08-08 10:24:16 +02:00
if (logosStorageLogLine == null) throw new Exception("Unable to parse required line");
EnsureFullIds(logosStorageLogLine);
converter.Process(logosStorageLogLine, (action) =>
{
AddEvent(logosStorageLogLine.TimestampUtc, action);
});
}
2024-08-08 10:24:16 +02:00
private void EnsureFullIds(LogosStorageLogLine logosStorageLogLine)
2024-08-08 10:24:16 +02:00
{
// The issue is: node IDs occure both in full and short version.
// Downstream tools will assume that a node ID string-equals its own ID.
// So we replace all shortened IDs we can find with their full ones.
// Usually, the shortID appears as the entire string of an attribute:
// "peerId=123*567890"
// But sometimes, it is part of a larger string:
// "thing=abc:123*567890,def"
foreach (var pair in logosStorageLogLine.Attributes)
2024-08-08 10:24:16 +02:00
{
if (pair.Value.Contains("*"))
{
logosStorageLogLine.Attributes[pair.Key] = nameIdMap.ReplaceShortIds(pair.Value);
2024-08-08 10:24:16 +02:00
}
}
}
}
public interface ILineConverter
{
string Interest { get; }
void Process(LogosStorageLogLine line, Action<Action<OverwatchLogosStorageEvent>> addEvent);
}
2024-07-26 09:14:46 +02:00
}