using System.Text;
using System.Text.Json;
namespace ZB.MOM.WW.ScadaBridge.CLI.Commands;
///
/// Arguments for an audit backfill-source-node invocation.
///
public sealed class AuditBackfillSourceNodeArgs
{
///
/// Value written into SourceNode for NULL rows (default "unknown").
///
public string Sentinel { get; set; } = "unknown";
///
/// Only rows with OccurredAtUtc strictly before this UTC datetime are
/// eligible. Required — must be an ISO-8601 UTC datetime.
///
public string Before { get; set; } = string.Empty;
///
/// Maximum rows updated per batch (default 5000). Caps the per-transaction
/// log footprint; the loop repeats until no rows remain.
///
public int BatchSize { get; set; } = 5000;
}
///
/// Pure helpers for the audit backfill-source-node subcommand. Builds
/// the request body, POSTs to
/// /api/audit/backfill-source-node, and renders the result. Kept separate
/// from the command wiring so each piece is unit-testable without standing up the
/// command tree.
///
public static class AuditBackfillHelpers
{
private static readonly JsonSerializerOptions JsonWriteOptions = new()
{
WriteIndented = true,
};
///
/// Builds the JSON request body for POST /api/audit/backfill-source-node.
///
/// The backfill arguments.
/// A JSON string suitable for the request body.
public static string BuildRequestBody(AuditBackfillSourceNodeArgs args)
{
var obj = new
{
sentinel = args.Sentinel,
before = args.Before,
batchSize = args.BatchSize,
};
return JsonSerializer.Serialize(obj);
}
///
/// Executes the backfill: POSTs /api/audit/backfill-source-node and
/// prints the result. Returns the process exit code (0 = success,
/// 1 = error, 2 = authorization failure).
///
/// The management HTTP client.
/// The backfill arguments.
/// The output writer for results.
/// A task that resolves to the process exit code.
public static async Task RunBackfillAsync(
ManagementHttpClient client,
AuditBackfillSourceNodeArgs args,
TextWriter output)
{
var body = BuildRequestBody(args);
var response = await client.SendPostAsync(
"api/audit/backfill-source-node", body, TimeSpan.FromMinutes(10));
if (response.JsonData == null)
{
OutputFormatter.WriteError(
response.Error ?? "Backfill request failed.", response.ErrorCode ?? "ERROR");
return CommandHelpers.IsAuthorizationFailure(response) ? 2 : 1;
}
// Parse and display the result.
try
{
using var doc = JsonDocument.Parse(response.JsonData);
var root = doc.RootElement;
var rowsUpdated = root.TryGetProperty("rowsUpdated", out var r)
? r.GetInt64()
: 0L;
var sentinel = root.TryGetProperty("sentinel", out var s)
? s.GetString() ?? args.Sentinel
: args.Sentinel;
var before = root.TryGetProperty("before", out var b)
? b.GetString() ?? args.Before
: args.Before;
output.WriteLine($"SourceNode backfill complete.");
output.WriteLine($" rows updated : {rowsUpdated}");
output.WriteLine($" sentinel : {sentinel}");
output.WriteLine($" before : {before}");
}
catch (JsonException)
{
// Server returned success but non-JSON body — not expected; print raw.
output.WriteLine(response.JsonData);
}
output.Flush();
return 0;
}
}