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.CentralUI.Services;
///
/// Flattened, typed view of a canonical for the
/// Central UI audit pages. The canonical record is the seam type — the
/// query service decomposes it into this view (via ) so the
/// existing razor bindings (row.Channel, Event.Status, evt.RequestSummary,
/// …) keep working against typed properties rather than parsing DetailsJson inline.
///
///
/// This is presentation-only: it carries the same field surface the bespoke
/// Commons.Entities.Audit.AuditEvent exposed previously. ForwardState is always
/// null on the central read path (it is site-storage-only and not carried on canonical rows).
///
public sealed record AuditEventView
{
/// Idempotency key.
public Guid EventId { get; init; }
/// UTC timestamp when the audited action occurred.
public DateTime OccurredAtUtc { get; init; }
/// UTC ingest timestamp (central-set); null until ingest.
public DateTime? IngestedAtUtc { get; init; }
/// Trust-boundary channel.
public AuditChannel Channel { get; init; }
/// Specific event kind.
public AuditKind Kind { get; init; }
/// Per-operation correlation id.
public Guid? CorrelationId { get; init; }
/// Originating execution id.
public Guid? ExecutionId { get; init; }
/// Spawning execution id; null for top-level runs.
public Guid? ParentExecutionId { get; init; }
/// Site id where the action originated.
public string? SourceSiteId { get; init; }
/// Cluster node that emitted the event.
public string? SourceNode { get; init; }
/// Instance id where the action originated.
public string? SourceInstanceId { get; init; }
/// Script that initiated the action.
public string? SourceScript { get; init; }
/// Authenticated actor.
public string? Actor { get; init; }
/// Target of the action.
public string? Target { get; init; }
/// Lifecycle status.
public AuditStatus Status { get; init; }
/// HTTP status code where applicable.
public int? HttpStatus { get; init; }
/// Duration of the action in ms.
public int? DurationMs { get; init; }
/// Human-readable error summary.
public string? ErrorMessage { get; init; }
/// Verbose error detail.
public string? ErrorDetail { get; init; }
/// Truncated/redacted request summary.
public string? RequestSummary { get; init; }
/// Truncated/redacted response summary.
public string? ResponseSummary { get; init; }
/// True when summaries were truncated.
public bool PayloadTruncated { get; init; }
/// Free-form JSON extension.
public string? Extra { get; init; }
/// Site-local forwarding state; always null on the central read path.
public AuditForwardState? ForwardState { get; init; }
///
/// Decomposes a canonical into a flat view for the UI.
///
/// The canonical audit event to decompose.
/// A flat populated from the event's top-level and details fields.
public static AuditEventView From(AuditEvent evt)
{
var r = AuditRowProjection.Decompose(evt);
return new AuditEventView
{
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,
};
}
}