From 9d9f65c5a3fdeef0fd3dd05ac43e671a865c1d0e Mon Sep 17 00:00:00 2001 From: benbierens Date: Mon, 29 Jul 2024 11:02:24 +0200 Subject: [PATCH] Fixes missing name and null events --- Framework/Core/CoreInterface.cs | 2 +- Framework/Core/DownloadedLog.cs | 7 +++++- .../Recipe/ContainerRecipe.cs | 1 + .../OverwatchSupport/CodexLogConverter.cs | 10 +++++--- .../OverwatchSupport/CodexTranscriptWriter.cs | 10 +++----- .../LineConverters/BootstrapLineConverter.cs | 25 ++++++++++++------- .../OverwatchSupport/ModelExtensions.cs | 23 +++++++++++++++-- Tools/OverwatchTranscript/TranscriptWriter.cs | 1 + 8 files changed, 56 insertions(+), 23 deletions(-) diff --git a/Framework/Core/CoreInterface.cs b/Framework/Core/CoreInterface.cs index 81483a8..e48b6de 100644 --- a/Framework/Core/CoreInterface.cs +++ b/Framework/Core/CoreInterface.cs @@ -34,7 +34,7 @@ namespace Core entryPoint.Tools.GetLog().Log(msg); var logHandler = new WriteToFileLogHandler(entryPoint.Tools.GetLog(), msg); workflow.DownloadContainerLog(container, logHandler, tailLines); - return new DownloadedLog(logHandler); + return new DownloadedLog(logHandler, container.Name); } public string ExecuteContainerCommand(IHasContainer containerSource, string command, params string[] args) diff --git a/Framework/Core/DownloadedLog.cs b/Framework/Core/DownloadedLog.cs index 075e9eb..381ef3d 100644 --- a/Framework/Core/DownloadedLog.cs +++ b/Framework/Core/DownloadedLog.cs @@ -5,6 +5,8 @@ namespace Core { public interface IDownloadedLog { + string ContainerName { get; } + void IterateLines(Action action, params string[] thatContain); string[] GetLinesContaining(string expectedString); string[] FindLinesThatContain(params string[] tags); @@ -16,10 +18,13 @@ namespace Core { private readonly LogFile logFile; - internal DownloadedLog(WriteToFileLogHandler logHandler) + internal DownloadedLog(WriteToFileLogHandler logHandler, string containerName) { logFile = logHandler.LogFile; + ContainerName = containerName; } + + public string ContainerName { get; } public void IterateLines(Action action, params string[] thatContain) { diff --git a/Framework/KubernetesWorkflow/Recipe/ContainerRecipe.cs b/Framework/KubernetesWorkflow/Recipe/ContainerRecipe.cs index b57b62f..b643f38 100644 --- a/Framework/KubernetesWorkflow/Recipe/ContainerRecipe.cs +++ b/Framework/KubernetesWorkflow/Recipe/ContainerRecipe.cs @@ -4,6 +4,7 @@ { public ContainerRecipe(DateTime recipeCreatedUtc, int number, string? nameOverride, string image, ContainerResources resources, SchedulingAffinity schedulingAffinity, CommandOverride commandOverride, bool setCriticalPriority, Port[] exposedPorts, Port[] internalPorts, EnvVar[] envVars, PodLabels podLabels, PodAnnotations podAnnotations, VolumeMount[] volumes, ContainerAdditionals additionals) { + RecipeCreatedUtc = recipeCreatedUtc; Number = number; NameOverride = nameOverride; Image = image; diff --git a/ProjectPlugins/CodexPlugin/OverwatchSupport/CodexLogConverter.cs b/ProjectPlugins/CodexPlugin/OverwatchSupport/CodexLogConverter.cs index d0ec611..0754d9d 100644 --- a/ProjectPlugins/CodexPlugin/OverwatchSupport/CodexLogConverter.cs +++ b/ProjectPlugins/CodexPlugin/OverwatchSupport/CodexLogConverter.cs @@ -19,7 +19,7 @@ namespace CodexPlugin.OverwatchSupport public void ProcessLog(IDownloadedLog log) { var peerId = DeterminPeerId(log); - var runner = new ConversionRunner(writer, peerId); + var runner = new ConversionRunner(writer, log.ContainerName, peerId); runner.Run(log); } @@ -51,6 +51,7 @@ namespace CodexPlugin.OverwatchSupport public class ConversionRunner { private readonly ITranscriptWriter writer; + private readonly string name; private readonly string peerId; private readonly ILineConverter[] converters = new ILineConverter[] { @@ -58,8 +59,9 @@ namespace CodexPlugin.OverwatchSupport new BootstrapLineConverter() }; - public ConversionRunner(ITranscriptWriter writer, string peerId) + public ConversionRunner(ITranscriptWriter writer, string name, string peerId) { + this.name = name; this.writer = writer; this.peerId = peerId; } @@ -79,10 +81,12 @@ namespace CodexPlugin.OverwatchSupport { var e = new OverwatchCodexEvent { + Name = name, PeerId = peerId, }; action(e); - writer.Add(utc, e); + + e.Write(utc, writer); } private void ProcessLine(string line, ILineConverter converter) diff --git a/ProjectPlugins/CodexPlugin/OverwatchSupport/CodexTranscriptWriter.cs b/ProjectPlugins/CodexPlugin/OverwatchSupport/CodexTranscriptWriter.cs index 86ca9c3..fa7f1e4 100644 --- a/ProjectPlugins/CodexPlugin/OverwatchSupport/CodexTranscriptWriter.cs +++ b/ProjectPlugins/CodexPlugin/OverwatchSupport/CodexTranscriptWriter.cs @@ -23,11 +23,6 @@ namespace CodexPlugin.OverwatchSupport writer.AddHeader(CodexHeaderKey, CreateCodexHeader()); writer.Write(outputFilepath); - - we need: - total number of events - min max utc, time range - total number of codex nodes } public ICodexNodeHooks CreateHooks(string nodeName) @@ -59,6 +54,7 @@ namespace CodexPlugin.OverwatchSupport { writer.Add(DateTime.UtcNow, new OverwatchCodexEvent { + Name = string.Empty, PeerId = string.Empty, ScenarioFinished = new ScenarioFinishedEvent { @@ -102,7 +98,6 @@ namespace CodexPlugin.OverwatchSupport { e.NodeStarting = new NodeStartingEvent { - Name = name, Image = image }; }); @@ -162,11 +157,12 @@ namespace CodexPlugin.OverwatchSupport { var e = new OverwatchCodexEvent { + Name = name, PeerId = peerId }; action(e); - writer.Add(utc, e); + e.Write(utc, writer); } } } diff --git a/ProjectPlugins/CodexPlugin/OverwatchSupport/LineConverters/BootstrapLineConverter.cs b/ProjectPlugins/CodexPlugin/OverwatchSupport/LineConverters/BootstrapLineConverter.cs index 27741d9..792b6e5 100644 --- a/ProjectPlugins/CodexPlugin/OverwatchSupport/LineConverters/BootstrapLineConverter.cs +++ b/ProjectPlugins/CodexPlugin/OverwatchSupport/LineConverters/BootstrapLineConverter.cs @@ -2,6 +2,8 @@ { public class BootstrapLineConverter : ILineConverter { + private const string peerIdTag = "peerId: "; + public string Interest => "Starting codex node"; public void Process(CodexLogLine line, Action> addEvent) @@ -48,17 +50,22 @@ // )" var config = line.Attributes["config"]; - var openIndex = config.IndexOf("peerId:") + 7; - var closeIndex = config.IndexOf(",", openIndex); - var bootPeerId = config.Substring(openIndex, closeIndex - openIndex); - - addEvent(e => + + while (config.Contains(peerIdTag)) { - e.BootstrapConfig = new BootstrapConfigEvent + var openIndex = config.IndexOf(peerIdTag) + peerIdTag.Length; + var closeIndex = config.IndexOf(",", openIndex); + var bootPeerId = config.Substring(openIndex, closeIndex - openIndex); + config = config.Substring(closeIndex); + + addEvent(e => { - BootstrapPeerId = bootPeerId - }; - }); + e.BootstrapConfig = new BootstrapConfigEvent + { + BootstrapPeerId = bootPeerId + }; + }); + } } } } diff --git a/ProjectPlugins/CodexPlugin/OverwatchSupport/ModelExtensions.cs b/ProjectPlugins/CodexPlugin/OverwatchSupport/ModelExtensions.cs index 2899202..16e396a 100644 --- a/ProjectPlugins/CodexPlugin/OverwatchSupport/ModelExtensions.cs +++ b/ProjectPlugins/CodexPlugin/OverwatchSupport/ModelExtensions.cs @@ -1,4 +1,6 @@ -namespace CodexPlugin.OverwatchSupport +using OverwatchTranscript; + +namespace CodexPlugin.OverwatchSupport { [Serializable] public class OverwatchCodexHeader @@ -9,6 +11,7 @@ [Serializable] public class OverwatchCodexEvent { + public string Name { get; set; } = string.Empty; public string PeerId { get; set; } = string.Empty; public ScenarioFinishedEvent? ScenarioFinished { get; set; } public NodeStartingEvent? NodeStarting { get; set; } @@ -18,6 +21,23 @@ public FileUploadedEvent? FileUploaded { get; set; } public FileDownloadedEvent? FileDownloaded { get; set; } public BlockReceivedEvent? BlockReceived { get; set; } + + public void Write(DateTime utc, ITranscriptWriter writer) + { + if (string.IsNullOrWhiteSpace(Name)) throw new Exception("Name required"); + if (AllNull()) throw new Exception("No event data was set"); + + writer.Add(utc, this); + } + + private bool AllNull() + { + var props = GetType() + .GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance) + .Where(p => p.PropertyType != typeof(string)).ToArray(); + + return props.All(p => p.GetValue(this) == null); + } } #region Scenario Generated Events @@ -32,7 +52,6 @@ [Serializable] public class NodeStartingEvent { - public string Name { get; set; } = string.Empty; public string Image { get; set; } = string.Empty; } diff --git a/Tools/OverwatchTranscript/TranscriptWriter.cs b/Tools/OverwatchTranscript/TranscriptWriter.cs index 74b5ac2..742a771 100644 --- a/Tools/OverwatchTranscript/TranscriptWriter.cs +++ b/Tools/OverwatchTranscript/TranscriptWriter.cs @@ -37,6 +37,7 @@ namespace OverwatchTranscript CheckClosed(); var typeName = payload.GetType().FullName; if (string.IsNullOrEmpty(typeName)) throw new Exception("Empty typename for payload"); + if (utc == default) throw new Exception("DateTimeUtc not set"); var newEvent = new OverwatchEvent {