using System.Globalization;
using System.Text.Json;
namespace ZB.MOM.WW.ScadaBridge.CLI.Commands;
///
/// Helpers for the audit archive list / audit archive verify subcommands
/// (AuditLog Parquet cold archive, ScadaBridge#42). Both send a management command
/// (ListAuditArchives, VerifyAuditArchive); the archives live on the central
/// nodes' archive directory, not on the operator's machine.
///
///
/// verify exit codes: 0 verified, when the
/// data file disagrees with its manifest, 1 on a request error, 2 on an
/// authorization failure.
///
public static class AuditArchiveHelpers
{
/// Process exit code when an archive failed verification.
public const int NotVerifiedExitCode = 3;
///
/// Client deadline for verify: the server hashes and reads a whole month under
/// the long-running management Ask (5 min); the client waits a little longer.
///
public static readonly TimeSpan VerifyRequestTimeout = TimeSpan.FromMinutes(6);
/// Renders a ListAuditArchives result.
/// The success body (an AuditArchiveListResult, camelCase).
/// Resolved output format.
/// Writer for the report.
/// Always 0.
public static int RenderList(string json, string format, TextWriter output)
{
if (!string.Equals(format, "table", StringComparison.OrdinalIgnoreCase))
{
output.WriteLine(json);
output.Flush();
return 0;
}
using var doc = JsonDocument.Parse(json);
var root = doc.RootElement;
var enabled = root.TryGetProperty("enabled", out var e) && e.ValueKind == JsonValueKind.True;
output.WriteLine($"Archiving: {(enabled ? "enabled" : "disabled")}");
if (!root.TryGetProperty("archives", out var archives) || archives.GetArrayLength() == 0)
{
output.WriteLine("No archives.");
output.Flush();
return 0;
}
output.WriteLine($"{"NAME",-42} {"PARTITION",-9} {"STATUS",-8} {"ROWS",12} {"BYTES",14}");
foreach (var a in archives.EnumerateArray())
{
output.WriteLine(string.Create(CultureInfo.InvariantCulture,
$"{Str(a, "name"),-42} {Str(a, "partition"),-9} {Str(a, "status"),-8} {Num(a, "rowCount"),12} {Num(a, "dataBytes"),14}"));
}
output.Flush();
return 0;
}
/// Renders a VerifyAuditArchive result and maps it to the exit code.
/// The success body (an AuditArchiveVerificationResult, camelCase).
/// Resolved output format.
/// Writer for the report.
/// 0 when verified, otherwise.
public static int RenderVerify(string json, string format, TextWriter output)
{
using var doc = JsonDocument.Parse(json);
var root = doc.RootElement;
var verified = root.TryGetProperty("verified", out var v) && v.ValueKind == JsonValueKind.True;
if (!string.Equals(format, "table", StringComparison.OrdinalIgnoreCase))
{
output.WriteLine(json);
output.Flush();
return verified ? 0 : NotVerifiedExitCode;
}
output.WriteLine($"Archive : {Str(root, "name")}");
output.WriteLine($"Partition : {Str(root, "partition")}");
output.WriteLine($"Manifest : {Str(root, "manifestStatus")}");
output.WriteLine($"Verified : {(verified ? "yes" : "NO")}");
output.WriteLine($"Files intact: {(root.TryGetProperty("filesIntact", out var f) && f.ValueKind == JsonValueKind.True ? "yes" : "NO")}");
if (Str(root, "fileProblem") is { Length: > 0 } problem)
{
output.WriteLine($" problem : {problem}");
}
output.WriteLine($"Rows read : {Num(root, "rowsRead")}");
output.Flush();
return verified ? 0 : NotVerifiedExitCode;
}
///
/// Renders a CleanupAuditArchives result (ScadaBridge#109). Always exit 0 — kept
/// stale archives are reported, not an error.
///
/// The success body (an AuditArchiveCleanupResult, camelCase).
/// Resolved output format.
/// Writer for the report.
/// 0.
public static int RenderCleanup(string json, string format, TextWriter output)
{
if (!string.Equals(format, "table", StringComparison.OrdinalIgnoreCase))
{
output.WriteLine(json);
output.Flush();
return 0;
}
using var doc = JsonDocument.Parse(json);
var root = doc.RootElement;
var dryRun = root.TryGetProperty("dryRun", out var d) && d.ValueKind == JsonValueKind.True;
var retention = Num(root, "retentionDays");
output.WriteLine($"{(dryRun ? "Dry run — nothing deleted. " : "")}Archive retention: {(retention == "0" ? "never delete" : retention + " days")}");
var items = root.TryGetProperty("items", out var i) && i.ValueKind == JsonValueKind.Array ? i.EnumerateArray().ToList() : [];
if (items.Count == 0)
{
output.WriteLine("Nothing to clean up.");
}
foreach (var item in items)
{
output.WriteLine($"{Str(item, "action"),-18} {Str(item, "name")} {Str(item, "detail")}");
}
output.Flush();
return 0;
}
private static string Str(JsonElement e, string name, string missing = "")
=> e.TryGetProperty(name, out var v) && v.ValueKind == JsonValueKind.String ? v.GetString() ?? missing : missing;
private static string Num(JsonElement e, string name, string missing = "0")
=> e.TryGetProperty(name, out var v) && v.ValueKind == JsonValueKind.Number ? v.GetInt64().ToString(CultureInfo.InvariantCulture) : missing;
}