|
| 1 | +/* |
| 2 | +Copyright © 2025 Keyfactor |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using System.Diagnostics; |
| 20 | +using System.Text; |
| 21 | +using System.Threading.Tasks; |
| 22 | +using Microsoft.Extensions.Logging; |
| 23 | + |
| 24 | +namespace Keyfactor.Extensions.CAPlugin.Idnomic; |
| 25 | + |
| 26 | +public enum FlowStepStatus { Success, Failed, Skipped, InProgress } |
| 27 | + |
| 28 | +public class FlowStep |
| 29 | +{ |
| 30 | + public string Name { get; set; } |
| 31 | + public FlowStepStatus Status { get; set; } |
| 32 | + public string Detail { get; set; } |
| 33 | + public long ElapsedMs { get; set; } |
| 34 | + public List<FlowStep> Children { get; } = new(); |
| 35 | +} |
| 36 | + |
| 37 | +public sealed class FlowLogger : IDisposable |
| 38 | +{ |
| 39 | + private readonly ILogger _logger; |
| 40 | + private readonly string _flowName; |
| 41 | + private readonly Stopwatch _totalTimer; |
| 42 | + private readonly List<FlowStep> _steps = new(); |
| 43 | + private FlowStep _currentParent; |
| 44 | + private bool _disposed; |
| 45 | + |
| 46 | + public FlowLogger(ILogger logger, string flowName) |
| 47 | + { |
| 48 | + _logger = logger; |
| 49 | + _flowName = flowName; |
| 50 | + _totalTimer = Stopwatch.StartNew(); |
| 51 | + _logger.LogTrace("===== FLOW START: {FlowName} =====", _flowName); |
| 52 | + } |
| 53 | + |
| 54 | + public void Step(string name, string detail = null) |
| 55 | + { |
| 56 | + var step = new FlowStep |
| 57 | + { |
| 58 | + Name = name, |
| 59 | + Status = FlowStepStatus.Success, |
| 60 | + Detail = detail |
| 61 | + }; |
| 62 | + AddStep(step); |
| 63 | + } |
| 64 | + |
| 65 | + public void Step(string name, Action action, string detail = null) |
| 66 | + { |
| 67 | + var sw = Stopwatch.StartNew(); |
| 68 | + var step = new FlowStep |
| 69 | + { |
| 70 | + Name = name, |
| 71 | + Status = FlowStepStatus.InProgress, |
| 72 | + Detail = detail |
| 73 | + }; |
| 74 | + AddStep(step); |
| 75 | + |
| 76 | + try |
| 77 | + { |
| 78 | + action(); |
| 79 | + sw.Stop(); |
| 80 | + step.ElapsedMs = sw.ElapsedMilliseconds; |
| 81 | + step.Status = FlowStepStatus.Success; |
| 82 | + } |
| 83 | + catch |
| 84 | + { |
| 85 | + sw.Stop(); |
| 86 | + step.ElapsedMs = sw.ElapsedMilliseconds; |
| 87 | + step.Status = FlowStepStatus.Failed; |
| 88 | + throw; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public async Task StepAsync(string name, Func<Task> action, string detail = null) |
| 93 | + { |
| 94 | + var sw = Stopwatch.StartNew(); |
| 95 | + var step = new FlowStep |
| 96 | + { |
| 97 | + Name = name, |
| 98 | + Status = FlowStepStatus.InProgress, |
| 99 | + Detail = detail |
| 100 | + }; |
| 101 | + AddStep(step); |
| 102 | + |
| 103 | + try |
| 104 | + { |
| 105 | + await action(); |
| 106 | + sw.Stop(); |
| 107 | + step.ElapsedMs = sw.ElapsedMilliseconds; |
| 108 | + step.Status = FlowStepStatus.Success; |
| 109 | + } |
| 110 | + catch |
| 111 | + { |
| 112 | + sw.Stop(); |
| 113 | + step.ElapsedMs = sw.ElapsedMilliseconds; |
| 114 | + step.Status = FlowStepStatus.Failed; |
| 115 | + throw; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public void Fail(string name, string reason = null) |
| 120 | + { |
| 121 | + var step = new FlowStep |
| 122 | + { |
| 123 | + Name = name, |
| 124 | + Status = FlowStepStatus.Failed, |
| 125 | + Detail = reason |
| 126 | + }; |
| 127 | + AddStep(step); |
| 128 | + } |
| 129 | + |
| 130 | + public void Skip(string name, string reason = null) |
| 131 | + { |
| 132 | + var step = new FlowStep |
| 133 | + { |
| 134 | + Name = name, |
| 135 | + Status = FlowStepStatus.Skipped, |
| 136 | + Detail = reason |
| 137 | + }; |
| 138 | + AddStep(step); |
| 139 | + } |
| 140 | + |
| 141 | + public void Branch(string name) |
| 142 | + { |
| 143 | + var step = new FlowStep |
| 144 | + { |
| 145 | + Name = name, |
| 146 | + Status = FlowStepStatus.InProgress |
| 147 | + }; |
| 148 | + AddStep(step); |
| 149 | + _currentParent = step; |
| 150 | + } |
| 151 | + |
| 152 | + public void EndBranch() |
| 153 | + { |
| 154 | + _currentParent = null; |
| 155 | + } |
| 156 | + |
| 157 | + private void AddStep(FlowStep step) |
| 158 | + { |
| 159 | + if (_currentParent != null) |
| 160 | + { |
| 161 | + _currentParent.Children.Add(step); |
| 162 | + } |
| 163 | + else |
| 164 | + { |
| 165 | + _steps.Add(step); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + public void Dispose() |
| 170 | + { |
| 171 | + if (_disposed) return; |
| 172 | + _disposed = true; |
| 173 | + |
| 174 | + _totalTimer.Stop(); |
| 175 | + RenderFlow(); |
| 176 | + } |
| 177 | + |
| 178 | + private void RenderFlow() |
| 179 | + { |
| 180 | + var sb = new StringBuilder(); |
| 181 | + var overallStatus = DetermineOverallStatus(); |
| 182 | + |
| 183 | + sb.AppendLine(); |
| 184 | + sb.AppendLine($" ===== FLOW: {_flowName} ({_totalTimer.ElapsedMilliseconds}ms total) ====="); |
| 185 | + sb.AppendLine(); |
| 186 | + |
| 187 | + for (int i = 0; i < _steps.Count; i++) |
| 188 | + { |
| 189 | + RenderStep(sb, _steps[i], " "); |
| 190 | + |
| 191 | + if (i < _steps.Count - 1) |
| 192 | + { |
| 193 | + sb.AppendLine(" |"); |
| 194 | + sb.AppendLine(" v"); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + sb.AppendLine(); |
| 199 | + sb.AppendLine($" ===== FLOW RESULT: {overallStatus} ====="); |
| 200 | + |
| 201 | + _logger.LogTrace(sb.ToString()); |
| 202 | + } |
| 203 | + |
| 204 | + private void RenderStep(StringBuilder sb, FlowStep step, string indent) |
| 205 | + { |
| 206 | + var icon = step.Status switch |
| 207 | + { |
| 208 | + FlowStepStatus.Success => "[OK]", |
| 209 | + FlowStepStatus.Failed => "[FAIL]", |
| 210 | + FlowStepStatus.Skipped => "[SKIP]", |
| 211 | + FlowStepStatus.InProgress => "[...]", |
| 212 | + _ => "[?]" |
| 213 | + }; |
| 214 | + |
| 215 | + var timing = step.ElapsedMs > 0 ? $" ({step.ElapsedMs}ms)" : ""; |
| 216 | + var detail = !string.IsNullOrEmpty(step.Detail) ? $" [{step.Detail}]" : ""; |
| 217 | + |
| 218 | + sb.AppendLine($"{indent}{icon} {step.Name}{timing}{detail}"); |
| 219 | + |
| 220 | + foreach (var child in step.Children) |
| 221 | + { |
| 222 | + RenderStep(sb, child, indent + " "); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + private string DetermineOverallStatus() |
| 227 | + { |
| 228 | + foreach (var step in _steps) |
| 229 | + { |
| 230 | + if (step.Status == FlowStepStatus.Failed || HasFailedChild(step)) |
| 231 | + return "FAILED"; |
| 232 | + } |
| 233 | + return "SUCCESS"; |
| 234 | + } |
| 235 | + |
| 236 | + private bool HasFailedChild(FlowStep step) |
| 237 | + { |
| 238 | + foreach (var child in step.Children) |
| 239 | + { |
| 240 | + if (child.Status == FlowStepStatus.Failed || HasFailedChild(child)) |
| 241 | + return true; |
| 242 | + } |
| 243 | + return false; |
| 244 | + } |
| 245 | +} |
0 commit comments