feat(audit)!: ScadaBridge C3 — swap to canonical ZB.MOM.WW.Audit.AuditEvent across seams/emitters/DTO/redactor wiring; transitional 24-col storage shim (Task 2.5)
This commit is contained in:
@@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Audit;
|
||||
using ZB.MOM.WW.Audit;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
|
||||
@@ -127,22 +127,26 @@ public static class AuditEndpoints
|
||||
var paging = ParsePaging(context.Request.Query);
|
||||
|
||||
var repo = context.RequestServices.GetRequiredService<IAuditLogRepository>();
|
||||
var events = await repo.QueryAsync(filter, paging, context.RequestAborted);
|
||||
var canonical = await repo.QueryAsync(filter, paging, context.RequestAborted);
|
||||
|
||||
// The cursor for the next page is the last row of this page — but only
|
||||
// when the page came back FULL. A short page means there is no next
|
||||
// page, so nextCursor is null and the CLI stops paging.
|
||||
object? nextCursor = null;
|
||||
if (events.Count == paging.PageSize && events.Count > 0)
|
||||
if (canonical.Count == paging.PageSize && canonical.Count > 0)
|
||||
{
|
||||
var last = events[^1];
|
||||
var last = canonical[^1];
|
||||
nextCursor = new
|
||||
{
|
||||
afterOccurredAtUtc = last.OccurredAtUtc,
|
||||
// C3: canonical OccurredAtUtc is a DateTimeOffset; the cursor key is UTC.
|
||||
afterOccurredAtUtc = last.OccurredAtUtc.UtcDateTime,
|
||||
afterEventId = last.EventId,
|
||||
};
|
||||
}
|
||||
|
||||
// C3 (Task 2.5): decompose canonical rows into the flat AuditExportRow so the
|
||||
// CLI's JSON shape (24-field) is unchanged.
|
||||
var events = canonical.Select(AuditExportRow.From).ToList();
|
||||
var payload = new { events, nextCursor };
|
||||
// EnvelopeJsonOptions keeps an explicit null nextCursor on the wire so
|
||||
// the CLI can always read the key. AuditEvent rows render with their
|
||||
@@ -248,7 +252,7 @@ public static class AuditEndpoints
|
||||
{
|
||||
foreach (var evt in page)
|
||||
{
|
||||
await writer.WriteLineAsync(FormatCsvRow(evt));
|
||||
await writer.WriteLineAsync(FormatCsvRow(AuditExportRow.From(evt)));
|
||||
}
|
||||
await writer.FlushAsync(ct);
|
||||
await output.FlushAsync(ct);
|
||||
@@ -275,7 +279,7 @@ public static class AuditEndpoints
|
||||
{
|
||||
foreach (var evt in page)
|
||||
{
|
||||
await writer.WriteLineAsync(JsonSerializer.Serialize(evt, JsonOptions));
|
||||
await writer.WriteLineAsync(JsonSerializer.Serialize(AuditExportRow.From(evt), JsonOptions));
|
||||
}
|
||||
await writer.FlushAsync(ct);
|
||||
await output.FlushAsync(ct);
|
||||
@@ -309,7 +313,8 @@ public static class AuditEndpoints
|
||||
}
|
||||
|
||||
var last = page[^1];
|
||||
cursor = new AuditLogPaging(ExportPageSize, last.OccurredAtUtc, last.EventId);
|
||||
// C3: canonical OccurredAtUtc is a DateTimeOffset; the keyset cursor column is UTC.
|
||||
cursor = new AuditLogPaging(ExportPageSize, last.OccurredAtUtc.UtcDateTime, last.EventId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -571,7 +576,7 @@ public static class AuditEndpoints
|
||||
/// Formats a single <see cref="AuditEvent"/> as an RFC 4180 CSV row matching <see cref="CsvHeader"/>.
|
||||
/// </summary>
|
||||
/// <param name="evt">The audit event to format.</param>
|
||||
public static string FormatCsvRow(AuditEvent evt)
|
||||
public static string FormatCsvRow(AuditExportRow evt)
|
||||
{
|
||||
var sb = new StringBuilder(256);
|
||||
AppendField(sb, evt.EventId.ToString(), first: true);
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
using ZB.MOM.WW.Audit;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.ManagementService;
|
||||
|
||||
/// <summary>
|
||||
/// Flat, wire-shape view of a canonical <see cref="ZB.MOM.WW.Audit.AuditEvent"/> for the
|
||||
/// management CLI's <c>/api/audit/query</c> + <c>/api/audit/export</c> endpoints. C3
|
||||
/// (Task 2.5) made the canonical record the repository seam type; this DTO preserves the
|
||||
/// existing 24-field JSON/CSV shape the CLI consumes by decomposing the canonical row
|
||||
/// (via <see cref="AuditRowProjection"/>) at the endpoint boundary.
|
||||
/// </summary>
|
||||
public sealed record AuditExportRow
|
||||
{
|
||||
/// <summary>Idempotency key.</summary>
|
||||
public Guid EventId { get; init; }
|
||||
/// <summary>UTC timestamp when the audited action occurred.</summary>
|
||||
public DateTime OccurredAtUtc { get; init; }
|
||||
/// <summary>UTC ingest timestamp; null until ingest.</summary>
|
||||
public DateTime? IngestedAtUtc { get; init; }
|
||||
/// <summary>Trust-boundary channel.</summary>
|
||||
public AuditChannel Channel { get; init; }
|
||||
/// <summary>Specific event kind.</summary>
|
||||
public AuditKind Kind { get; init; }
|
||||
/// <summary>Per-operation correlation id.</summary>
|
||||
public Guid? CorrelationId { get; init; }
|
||||
/// <summary>Originating execution id.</summary>
|
||||
public Guid? ExecutionId { get; init; }
|
||||
/// <summary>Spawning execution id; null for top-level runs.</summary>
|
||||
public Guid? ParentExecutionId { get; init; }
|
||||
/// <summary>Site id where the action originated.</summary>
|
||||
public string? SourceSiteId { get; init; }
|
||||
/// <summary>Cluster node that emitted the event.</summary>
|
||||
public string? SourceNode { get; init; }
|
||||
/// <summary>Instance id where the action originated.</summary>
|
||||
public string? SourceInstanceId { get; init; }
|
||||
/// <summary>Script that initiated the action.</summary>
|
||||
public string? SourceScript { get; init; }
|
||||
/// <summary>Authenticated actor.</summary>
|
||||
public string? Actor { get; init; }
|
||||
/// <summary>Target of the action.</summary>
|
||||
public string? Target { get; init; }
|
||||
/// <summary>Lifecycle status.</summary>
|
||||
public AuditStatus Status { get; init; }
|
||||
/// <summary>HTTP status code where applicable.</summary>
|
||||
public int? HttpStatus { get; init; }
|
||||
/// <summary>Duration of the action in ms.</summary>
|
||||
public int? DurationMs { get; init; }
|
||||
/// <summary>Human-readable error summary.</summary>
|
||||
public string? ErrorMessage { get; init; }
|
||||
/// <summary>Verbose error detail.</summary>
|
||||
public string? ErrorDetail { get; init; }
|
||||
/// <summary>Truncated/redacted request summary.</summary>
|
||||
public string? RequestSummary { get; init; }
|
||||
/// <summary>Truncated/redacted response summary.</summary>
|
||||
public string? ResponseSummary { get; init; }
|
||||
/// <summary>True when summaries were truncated.</summary>
|
||||
public bool PayloadTruncated { get; init; }
|
||||
/// <summary>Free-form JSON extension.</summary>
|
||||
public string? Extra { get; init; }
|
||||
/// <summary>Site-local forwarding state; always null on the central read path.</summary>
|
||||
public AuditForwardState? ForwardState { get; init; }
|
||||
|
||||
/// <summary>Decomposes a canonical <see cref="AuditEvent"/> into this flat export shape.</summary>
|
||||
public static AuditExportRow From(AuditEvent evt)
|
||||
{
|
||||
var r = AuditRowProjection.Decompose(evt);
|
||||
return new AuditExportRow
|
||||
{
|
||||
EventId = r.EventId,
|
||||
OccurredAtUtc = r.OccurredAtUtc,
|
||||
IngestedAtUtc = r.IngestedAtUtc,
|
||||
Channel = r.Channel,
|
||||
Kind = r.Kind,
|
||||
CorrelationId = r.CorrelationId,
|
||||
ExecutionId = r.ExecutionId,
|
||||
ParentExecutionId = r.ParentExecutionId,
|
||||
SourceSiteId = r.SourceSiteId,
|
||||
SourceNode = r.SourceNode,
|
||||
SourceInstanceId = r.SourceInstanceId,
|
||||
SourceScript = r.SourceScript,
|
||||
Actor = r.Actor,
|
||||
Target = r.Target,
|
||||
Status = r.Status,
|
||||
HttpStatus = r.HttpStatus,
|
||||
DurationMs = r.DurationMs,
|
||||
ErrorMessage = r.ErrorMessage,
|
||||
ErrorDetail = r.ErrorDetail,
|
||||
RequestSummary = r.RequestSummary,
|
||||
ResponseSummary = r.ResponseSummary,
|
||||
PayloadTruncated = r.PayloadTruncated,
|
||||
Extra = r.Extra,
|
||||
ForwardState = null,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user