|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Text; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using GeneralUpdate.Tools.Models; |
| 6 | + |
| 7 | +namespace GeneralUpdate.Tools.Services; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Generates simulation_report.md after a simulation run. |
| 11 | +/// </summary> |
| 12 | +public class ReportGeneratorService |
| 13 | +{ |
| 14 | + public async Task<string> GenerateAsync( |
| 15 | + SimulateConfigModel config, |
| 16 | + SimulationResult result, |
| 17 | + string outputDir) |
| 18 | + { |
| 19 | + var sb = new StringBuilder(); |
| 20 | + |
| 21 | + sb.AppendLine("# Update Simulation Report"); |
| 22 | + sb.AppendLine(); |
| 23 | + sb.AppendLine("## Configuration"); |
| 24 | + sb.AppendLine(); |
| 25 | + sb.AppendLine("| Field | Value |"); |
| 26 | + sb.AppendLine("|-------|-------|"); |
| 27 | + sb.AppendLine($"| Patch | {EscapeMd(config.PatchFilePath)} |"); |
| 28 | + sb.AppendLine($"| App Directory | {EscapeMd(config.AppDirectory)} |"); |
| 29 | + sb.AppendLine($"| Platform | {config.Platform} |"); |
| 30 | + sb.AppendLine($"| AppType | {config.AppType} |"); |
| 31 | + sb.AppendLine($"| Version | {config.CurrentVersion} → {config.TargetVersion} |"); |
| 32 | + sb.AppendLine($"| Server Port | {config.ServerPort} |"); |
| 33 | + sb.AppendLine($"| Simulation Time | {DateTime.Now:yyyy-MM-dd HH:mm:ss} |"); |
| 34 | + sb.AppendLine(); |
| 35 | + |
| 36 | + sb.AppendLine("## Result"); |
| 37 | + sb.AppendLine(); |
| 38 | + sb.AppendLine($"**{(result.Success ? "✅ PASS" : "❌ FAIL")}** — {result.Elapsed.TotalSeconds:F1}s"); |
| 39 | + if (!string.IsNullOrEmpty(result.ErrorMessage)) |
| 40 | + sb.AppendLine($"\nError: `{result.ErrorMessage}`"); |
| 41 | + sb.AppendLine(); |
| 42 | + |
| 43 | + if (result.Notes.Count > 0) |
| 44 | + { |
| 45 | + sb.AppendLine("## Notes"); |
| 46 | + sb.AppendLine(); |
| 47 | + foreach (var note in result.Notes) |
| 48 | + sb.AppendLine($"- {note}"); |
| 49 | + sb.AppendLine(); |
| 50 | + } |
| 51 | + |
| 52 | + sb.AppendLine("## Timeline"); |
| 53 | + sb.AppendLine(); |
| 54 | + sb.AppendLine("```"); |
| 55 | + sb.Append(result.FullLog); |
| 56 | + sb.AppendLine("```"); |
| 57 | + |
| 58 | + var reportPath = Path.Combine(outputDir, "simulation_report.md"); |
| 59 | + await File.WriteAllTextAsync(reportPath, sb.ToString(), Encoding.UTF8); |
| 60 | + return reportPath; |
| 61 | + } |
| 62 | + |
| 63 | + private static string EscapeMd(string s) => s.Replace(@"\", @"\\").Replace("|", "\\|"); |
| 64 | +} |
0 commit comments