2
0
mirror of synced 2025-01-12 09:34:40 +00:00

129 lines
4.2 KiB
C#
Raw Normal View History

using CodexPlugin.OverwatchSupport.LineConverters;
using KubernetesWorkflow;
2024-07-26 09:14:46 +02:00
using OverwatchTranscript;
using Utils;
namespace CodexPlugin.OverwatchSupport
{
public class CodexLogConverter
{
private readonly ITranscriptWriter writer;
private readonly CodexTranscriptWriterConfig config;
2024-08-13 14:21:15 +02:00
private readonly IdentityMap identityMap;
2024-07-26 09:14:46 +02:00
public CodexLogConverter(ITranscriptWriter writer, CodexTranscriptWriterConfig 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, CodexTranscriptWriterConfig 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(CodexTranscriptWriterConfig 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);
}
});
}
2024-08-01 14:50:25 +02:00
private void AddEvent(DateTime utc, Action<OverwatchCodexEvent> action)
{
var e = new OverwatchCodexEvent
{
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 codexLine = CodexLogLine.Parse(line);
2024-08-08 10:24:16 +02:00
if (codexLine == null) throw new Exception("Unable to parse required line");
2024-08-08 10:24:16 +02:00
EnsureFullIds(codexLine);
converter.Process(codexLine, (action) =>
{
AddEvent(codexLine.TimestampUtc, action);
});
}
2024-08-08 10:24:16 +02:00
private void EnsureFullIds(CodexLogLine codexLine)
{
// 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 codexLine.Attributes)
{
if (pair.Value.Contains("*"))
{
codexLine.Attributes[pair.Key] = nameIdMap.ReplaceShortIds(pair.Value);
}
}
}
}
public interface ILineConverter
{
string Interest { get; }
void Process(CodexLogLine line, Action<Action<OverwatchCodexEvent>> addEvent);
}
2024-07-26 09:14:46 +02:00
}