2
0
mirror of synced 2025-01-23 06:50:02 +00:00

setting up log converter

This commit is contained in:
benbierens 2024-07-26 09:14:46 +02:00
parent 927ccaa119
commit 410d62849a
No known key found for this signature in database
GPG Key ID: 877D2C2E09A22F3A
9 changed files with 124 additions and 18 deletions

View File

@ -8,6 +8,7 @@ namespace Core
void IterateLines(Action<string> action, params string[] thatContain);
string[] GetLinesContaining(string expectedString);
string[] FindLinesThatContain(params string[] tags);
string GetFilepath();
void DeleteFile();
}
@ -75,6 +76,11 @@ namespace Core
return result.ToArray();
}
public string GetFilepath()
{
return logFile.FullFilename;
}
public void DeleteFile()
{
File.Delete(logFile.FullFilename);

13
Framework/Utils/Str.cs Normal file
View File

@ -0,0 +1,13 @@
namespace Utils
{
public static class Str
{
public static string Between(string input, string open, string close)
{
var openIndex = input.IndexOf(open) + open.Length;
var closeIndex = input.LastIndexOf(close);
return input.Substring(openIndex, closeIndex - openIndex);
}
}
}

View File

@ -222,21 +222,12 @@ namespace CodexPlugin
var log = tools.GetLog();
log.AddStringReplace(peerId, nodeName);
log.AddStringReplace(ToShortIdString(peerId), nodeName);
log.AddStringReplace(CodexUtils.ToShortId(peerId), nodeName);
log.AddStringReplace(debugInfo.Table.LocalNode.NodeId, nodeName);
log.AddStringReplace(ToShortIdString(debugInfo.Table.LocalNode.NodeId), nodeName);
log.AddStringReplace(CodexUtils.ToShortId(debugInfo.Table.LocalNode.NodeId), nodeName);
Version = debugInfo.Version;
}
private string ToShortIdString(string id)
{
if (id.Length > 10)
{
return $"{id[..3]}*{id[^6..]}";
}
return id;
}
private string[] GetPeerMultiAddresses(CodexNode peer, DebugInfo peerInfo)
{
// The peer we want to connect is in a different pod.

View File

@ -0,0 +1,14 @@
namespace CodexPlugin
{
public static class CodexUtils
{
public static string ToShortId(string id)
{
if (id.Length > 10)
{
return $"{id[..3]}*{id[^6..]}";
}
return id;
}
}
}

View File

@ -0,0 +1,49 @@
using Core;
using OverwatchTranscript;
using Utils;
namespace CodexPlugin.OverwatchSupport
{
public class CodexLogConverter
{
private readonly ITranscriptWriter writer;
private readonly NameIdMap nameIdMap;
public CodexLogConverter(ITranscriptWriter writer, NameIdMap nameIdMap)
{
this.writer = writer;
this.nameIdMap = nameIdMap;
}
public void ProcessLog(IDownloadedLog log)
{
var peerId = DeterminPeerId(log);
}
private string DeterminPeerId(IDownloadedLog log)
{
// We have to use a look-up map to match the node name to its peerId,
// because the Codex logging never prints the peerId in full.
// After we find it, we confirm it be looking for the shortened version.
// Expected string:
// Downloading container log for '<Downloader1>'
var nameLine = log.FindLinesThatContain("Downloading container log for").Single();
var name = Str.Between(nameLine, "'<", ">'");
var peerId = nameIdMap.GetPeerId(name);
var shortPeerId = CodexUtils.ToShortId(peerId);
// Look for "Started codex node" line to confirm peerId.
var startedLine = log.FindLinesThatContain("Started codex node").Single();
var started = CodexLogLine.Parse(startedLine)!;
var foundId = started.Attributes["id"];
if (foundId != shortPeerId) throw new Exception("PeerId from name-lookup did not match PeerId found in codex-started log line.");
return peerId;
}
}
}

View File

@ -6,45 +6,59 @@ namespace CodexPlugin.OverwatchSupport
{
public class CodexTranscriptWriter : ICodexHooksProvider
{
private readonly ITranscriptWriter transcriptWriter;
private readonly ITranscriptWriter writer;
private readonly CodexLogConverter converter;
private readonly NameIdMap nameIdMap = new NameIdMap();
public CodexTranscriptWriter(ITranscriptWriter transcriptWriter)
{
this.transcriptWriter = transcriptWriter;
writer = transcriptWriter;
converter = new CodexLogConverter(writer, nameIdMap);
}
public void Finalize(string outputFilepath)
{
transcriptWriter.Write(outputFilepath);
writer.Write(outputFilepath);
}
public ICodexNodeHooks CreateHooks(string nodeName)
{
return new CodexNodeTranscriptWriter(transcriptWriter, nodeName);
return new CodexNodeTranscriptWriter(writer, nameIdMap, nodeName);
}
public void IncludeFile(string filepath)
{
writer.IncludeArtifact(filepath);
}
public void ProcessLogs(IDownloadedLog[] downloadedLogs)
{
// which logs to which nodes?
// nodeIDs, peerIDs needed.
foreach (var log in downloadedLogs)
{
writer.IncludeArtifact(log.GetFilepath());
converter.ProcessLog(log);
}
}
}
public class CodexNodeTranscriptWriter : ICodexNodeHooks
{
private readonly ITranscriptWriter writer;
private readonly NameIdMap nameIdMap;
private readonly string name;
private string peerId = string.Empty;
public CodexNodeTranscriptWriter(ITranscriptWriter writer, string name)
public CodexNodeTranscriptWriter(ITranscriptWriter writer, NameIdMap nameIdMap, string name)
{
this.writer = writer;
this.nameIdMap = nameIdMap;
this.name = name;
}
public void OnNodeStarted(string peerId, string image)
{
this.peerId = peerId;
nameIdMap.Add(name, peerId);
WriteCodexEvent(e =>
{
e.NodeStarted = new NodeStartedEvent

View File

@ -0,0 +1,17 @@
namespace CodexPlugin.OverwatchSupport
{
public class NameIdMap
{
private readonly Dictionary<string, string> map = new Dictionary<string, string>();
public void Add(string name, string peerId)
{
map.Add(name, peerId);
}
public string GetPeerId(string name)
{
return map[name];
}
}
}

View File

@ -58,6 +58,7 @@ namespace CodexTests
var file = lifecycle.Log.CreateSubfile("owts");
Stopwatch.Measure(lifecycle.Log, $"Transcript.Finalize: {file.FullFilename}", () =>
{
writer.IncludeFile(lifecycle.Log.LogFile.FullFilename);
writer.Finalize(file.FullFilename);
});
}

View File

@ -55,6 +55,7 @@ namespace OverwatchTranscript
{
CheckClosed();
if (!File.Exists(filePath)) throw new Exception("File not found: " + filePath);
if (!Directory.Exists(artifactsFolder)) Directory.CreateDirectory(artifactsFolder);
var name = Path.GetFileName(filePath);
File.Copy(filePath, Path.Combine(artifactsFolder, name), overwrite: false);
}