|
| 1 | +namespace Sigtran.NET.Core.Utilities; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Describes retained filesystem verification artifacts written to disk. |
| 5 | +/// </summary> |
| 6 | +public sealed class SigtranCommercialEvidenceFileSystemArtifactWriteResult |
| 7 | +{ |
| 8 | + /// <summary>Creates a retained filesystem verification artifact write result.</summary> |
| 9 | + /// <param name="verificationExecution">The filesystem verification execution.</param> |
| 10 | + /// <param name="outputDirectory">The output directory.</param> |
| 11 | + /// <param name="reportPath">The written verification report path.</param> |
| 12 | + /// <param name="observationManifestPath">The written observation manifest path.</param> |
| 13 | + /// <param name="writtenAtUtc">The UTC write time.</param> |
| 14 | + public SigtranCommercialEvidenceFileSystemArtifactWriteResult( |
| 15 | + SigtranCommercialEvidenceFileSystemVerificationExecution verificationExecution, |
| 16 | + string outputDirectory, |
| 17 | + string reportPath, |
| 18 | + string observationManifestPath, |
| 19 | + DateTimeOffset writtenAtUtc) |
| 20 | + { |
| 21 | + VerificationExecution = verificationExecution ?? throw new ArgumentNullException(nameof(verificationExecution)); |
| 22 | + OutputDirectory = string.IsNullOrWhiteSpace(outputDirectory) ? throw new ArgumentException("Output directory is required.", nameof(outputDirectory)) : outputDirectory; |
| 23 | + ReportPath = string.IsNullOrWhiteSpace(reportPath) ? throw new ArgumentException("Report path is required.", nameof(reportPath)) : reportPath; |
| 24 | + ObservationManifestPath = string.IsNullOrWhiteSpace(observationManifestPath) ? throw new ArgumentException("Observation manifest path is required.", nameof(observationManifestPath)) : observationManifestPath; |
| 25 | + WrittenAtUtc = writtenAtUtc.Offset == TimeSpan.Zero ? writtenAtUtc : writtenAtUtc.ToUniversalTime(); |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary>The filesystem verification execution.</summary> |
| 29 | + public SigtranCommercialEvidenceFileSystemVerificationExecution VerificationExecution { get; } |
| 30 | + |
| 31 | + /// <summary>The output directory.</summary> |
| 32 | + public string OutputDirectory { get; } |
| 33 | + |
| 34 | + /// <summary>The written verification report path.</summary> |
| 35 | + public string ReportPath { get; } |
| 36 | + |
| 37 | + /// <summary>The written observation manifest path.</summary> |
| 38 | + public string ObservationManifestPath { get; } |
| 39 | + |
| 40 | + /// <summary>The UTC write time.</summary> |
| 41 | + public DateTimeOffset WrittenAtUtc { get; } |
| 42 | + |
| 43 | + /// <summary>Whether all expected artifacts exist.</summary> |
| 44 | + public bool AllArtifactsExist => File.Exists(ReportPath) |
| 45 | + && File.Exists(ObservationManifestPath); |
| 46 | + |
| 47 | + /// <summary>Whether all expected artifacts have non-empty content.</summary> |
| 48 | + public bool AllArtifactsHaveContent => AllArtifactsExist |
| 49 | + && new FileInfo(ReportPath).Length > 0 |
| 50 | + && new FileInfo(ObservationManifestPath).Length > 0; |
| 51 | + |
| 52 | + /// <summary>Whether the write time is normalized to UTC.</summary> |
| 53 | + public bool UsesUtcWriteTime => WrittenAtUtc.Offset == TimeSpan.Zero; |
| 54 | + |
| 55 | + /// <summary>Whether retained filesystem verification artifacts are ready for ledger execution.</summary> |
| 56 | + public bool IsReady => VerificationExecution.IsReady |
| 57 | + && AllArtifactsExist |
| 58 | + && AllArtifactsHaveContent |
| 59 | + && UsesUtcWriteTime; |
| 60 | + |
| 61 | + /// <summary>Formats a compact artifact write summary.</summary> |
| 62 | + /// <returns>The artifact write summary.</returns> |
| 63 | + public string Describe() |
| 64 | + { |
| 65 | + return $"commercialEvidenceFileSystemArtifactsReady={IsReady} report={ReportPath} observations={ObservationManifestPath}"; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// <summary> |
| 70 | +/// Writes retained filesystem verification artifacts to disk. |
| 71 | +/// </summary> |
| 72 | +public static class SigtranCommercialEvidenceFileSystemArtifactWriters |
| 73 | +{ |
| 74 | + /// <summary>Writes the filesystem verification report and observation manifest.</summary> |
| 75 | + /// <param name="verificationExecution">The filesystem verification execution.</param> |
| 76 | + /// <param name="outputDirectory">The output directory.</param> |
| 77 | + /// <param name="writtenAtUtc">An optional UTC write time.</param> |
| 78 | + /// <returns>The retained filesystem verification artifact write result.</returns> |
| 79 | + public static SigtranCommercialEvidenceFileSystemArtifactWriteResult WriteVerificationArtifacts( |
| 80 | + SigtranCommercialEvidenceFileSystemVerificationExecution verificationExecution, |
| 81 | + string outputDirectory, |
| 82 | + DateTimeOffset? writtenAtUtc = null) |
| 83 | + { |
| 84 | + ArgumentNullException.ThrowIfNull(verificationExecution); |
| 85 | + if (string.IsNullOrWhiteSpace(outputDirectory)) |
| 86 | + { |
| 87 | + throw new ArgumentException("Output directory is required.", nameof(outputDirectory)); |
| 88 | + } |
| 89 | + |
| 90 | + Directory.CreateDirectory(outputDirectory); |
| 91 | + string reportPath = Path.Combine(outputDirectory, "filesystem-verification-report.md"); |
| 92 | + string observationManifestPath = Path.Combine(outputDirectory, "filesystem-observations.tsv"); |
| 93 | + File.WriteAllText(reportPath, RenderReport(verificationExecution)); |
| 94 | + File.WriteAllText(observationManifestPath, RenderObservations(verificationExecution.ManifestExecution.Observations)); |
| 95 | + |
| 96 | + return new( |
| 97 | + verificationExecution, |
| 98 | + outputDirectory, |
| 99 | + reportPath, |
| 100 | + observationManifestPath, |
| 101 | + writtenAtUtc ?? DateTimeOffset.UtcNow); |
| 102 | + } |
| 103 | + |
| 104 | + private static string RenderReport(SigtranCommercialEvidenceFileSystemVerificationExecution execution) |
| 105 | + { |
| 106 | + return string.Join( |
| 107 | + Environment.NewLine, |
| 108 | + [ |
| 109 | + "# Commercial Evidence Filesystem Verification Report", |
| 110 | + string.Empty, |
| 111 | + $"Intake: `{execution.ManifestExecution.Handoff.Report.Target.IntakeId}`", |
| 112 | + $"Observations: `{execution.ObservationCount}`", |
| 113 | + $"Filesystem evidence: `{execution.HasFilesystemEvidence}`", |
| 114 | + $"Verified: `{execution.Report.IsVerified}`", |
| 115 | + $"Missing files: `{execution.Report.MissingFileCount}`", |
| 116 | + $"Digest mismatches: `{execution.Report.DigestMismatchCount}`", |
| 117 | + $"Blockers: `{string.Join(",", execution.Report.Blockers)}`" |
| 118 | + ]); |
| 119 | + } |
| 120 | + |
| 121 | + private static string RenderObservations(IReadOnlyList<SigtranCommercialEvidenceFileSystemObservation> observations) |
| 122 | + { |
| 123 | + string[] lines = observations |
| 124 | + .Select(static observation => string.Join( |
| 125 | + '\t', |
| 126 | + observation.RetainedFile.Kind, |
| 127 | + observation.RetainedFile.RetainedPath, |
| 128 | + observation.FileSystemPath, |
| 129 | + observation.Exists, |
| 130 | + observation.RetainedFile.SizeBytes, |
| 131 | + observation.RetainedFile.ActualSha256, |
| 132 | + observation.RetainedFile.DigestMatches)) |
| 133 | + .Prepend("kind\tretainedPath\tfileSystemPath\texists\tsizeBytes\tactualSha256\tdigestMatches") |
| 134 | + .ToArray(); |
| 135 | + |
| 136 | + return string.Join(Environment.NewLine, lines); |
| 137 | + } |
| 138 | +} |
0 commit comments