| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.Audit; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents a framework-neutral lifecycle event linked to a governed audit residue flow. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// Lifecycle events are append-only progress records. They allow acknowledgment, capability token, gateway, outbox, and |
| | | 10 | | /// </remarks> |
| | | 11 | | public sealed class AuditResidueLifecycleEvent |
| | | 12 | | { |
| | 1 | 13 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 1 | 14 | | new ReadOnlyDictionary<string, string>( |
| | 1 | 15 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 16 | | |
| | 103 | 17 | | private AuditResidueLifecycleEvent( |
| | 103 | 18 | | string eventId, |
| | 103 | 19 | | AuditResidueLifecycleStage stage, |
| | 103 | 20 | | DateTimeOffset occurredUtc, |
| | 103 | 21 | | string correlationId, |
| | 103 | 22 | | string? auditResidueId, |
| | 103 | 23 | | string? traceId, |
| | 103 | 24 | | string? operationName, |
| | 103 | 25 | | string? outcome, |
| | 103 | 26 | | IReadOnlyDictionary<string, string> metadata) |
| | | 27 | | { |
| | 103 | 28 | | ArgumentException.ThrowIfNullOrWhiteSpace(eventId); |
| | 103 | 29 | | ArgumentException.ThrowIfNullOrWhiteSpace(correlationId); |
| | | 30 | | |
| | 100 | 31 | | if (!Enum.IsDefined(stage)) |
| | | 32 | | { |
| | 1 | 33 | | throw new ArgumentOutOfRangeException(nameof(stage), stage, "Lifecycle stage must be a defined audit residue |
| | | 34 | | } |
| | | 35 | | |
| | 99 | 36 | | EventId = eventId.Trim(); |
| | 99 | 37 | | Stage = stage; |
| | 99 | 38 | | OccurredUtc = occurredUtc.ToUniversalTime(); |
| | 99 | 39 | | CorrelationId = correlationId.Trim(); |
| | 99 | 40 | | AuditResidueId = NormalizeOptional(auditResidueId); |
| | 99 | 41 | | TraceId = NormalizeOptional(traceId); |
| | 99 | 42 | | OperationName = NormalizeOptional(operationName); |
| | 99 | 43 | | Outcome = NormalizeOptional(outcome); |
| | 99 | 44 | | Metadata = metadata; |
| | 99 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets the stable identifier for this lifecycle event. |
| | | 49 | | /// </summary> |
| | 139 | 50 | | public string EventId { get; } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets the lifecycle stage represented by this event. |
| | | 54 | | /// </summary> |
| | 134 | 55 | | public AuditResidueLifecycleStage Stage { get; } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets the stable sequence value for this lifecycle stage. |
| | | 59 | | /// </summary> |
| | 52 | 60 | | public int StageSequence => (int)Stage; |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets the UTC timestamp when the lifecycle event occurred. |
| | | 64 | | /// </summary> |
| | 84 | 65 | | public DateTimeOffset OccurredUtc { get; } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets the correlation identifier that links this lifecycle event to the original decision context. |
| | | 69 | | /// </summary> |
| | 90 | 70 | | public string CorrelationId { get; } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Gets the related audit residue identifier when the original decision residue is available. |
| | | 74 | | /// </summary> |
| | 59 | 75 | | public string? AuditResidueId { get; } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets the trace identifier associated with the lifecycle event, when supplied by the host or original residue. |
| | | 79 | | /// </summary> |
| | 47 | 80 | | public string? TraceId { get; } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets the operation name associated with the lifecycle event, when supplied by the host or original residue. |
| | | 84 | | /// </summary> |
| | 45 | 85 | | public string? OperationName { get; } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Gets the decision, gateway, emission, or host-defined outcome associated with this lifecycle event, when supplie |
| | | 89 | | /// </summary> |
| | 45 | 90 | | public string? Outcome { get; } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Gets additional framework-neutral lifecycle metadata supplied by the host. |
| | | 94 | | /// </summary> |
| | 49 | 95 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Gets a value indicating whether this lifecycle event is linked to an audit residue identifier. |
| | | 99 | | /// </summary> |
| | 1 | 100 | | public bool HasAuditResidueId => AuditResidueId is not null; |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Gets a value indicating whether this lifecycle event contains metadata. |
| | | 104 | | /// </summary> |
| | 1 | 105 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Creates an audit residue lifecycle event. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <param name="stage">The lifecycle stage represented by this event.</param> |
| | | 111 | | /// <param name="correlationId">The correlation identifier linking the event to the original decision context.</para |
| | | 112 | | /// <param name="auditResidueId">Optional audit residue identifier when the original decision residue is available.< |
| | | 113 | | /// <param name="eventId">Optional lifecycle event identifier. When omitted, a new identifier is generated.</param> |
| | | 114 | | /// <param name="occurredUtc">Optional lifecycle timestamp. When omitted, the current UTC timestamp is used.</param> |
| | | 115 | | /// <param name="traceId">Optional trace identifier.</param> |
| | | 116 | | /// <param name="operationName">Optional operation name.</param> |
| | | 117 | | /// <param name="outcome">Optional lifecycle or host-defined outcome.</param> |
| | | 118 | | /// <param name="metadata">Optional host-provided lifecycle metadata.</param> |
| | | 119 | | /// <returns>An audit residue lifecycle event.</returns> |
| | | 120 | | public static AuditResidueLifecycleEvent Create( |
| | | 121 | | AuditResidueLifecycleStage stage, |
| | | 122 | | string correlationId, |
| | | 123 | | string? auditResidueId = null, |
| | | 124 | | string? eventId = null, |
| | | 125 | | DateTimeOffset? occurredUtc = null, |
| | | 126 | | string? traceId = null, |
| | | 127 | | string? operationName = null, |
| | | 128 | | string? outcome = null, |
| | | 129 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 130 | | { |
| | 101 | 131 | | return new AuditResidueLifecycleEvent( |
| | 101 | 132 | | NormalizeIdentifier(eventId), |
| | 101 | 133 | | stage, |
| | 101 | 134 | | occurredUtc ?? DateTimeOffset.UtcNow, |
| | 101 | 135 | | correlationId, |
| | 101 | 136 | | auditResidueId, |
| | 101 | 137 | | traceId, |
| | 101 | 138 | | operationName, |
| | 101 | 139 | | outcome, |
| | 101 | 140 | | NormalizeMetadata(metadata)); |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Creates an audit residue lifecycle event by copying correlation context from existing audit residue. |
| | | 145 | | /// </summary> |
| | | 146 | | /// <param name="stage">The lifecycle stage represented by this event.</param> |
| | | 147 | | /// <param name="residue">The original audit residue to correlate with the lifecycle event.</param> |
| | | 148 | | /// <param name="correlationId">Optional correlation identifier override. When omitted, the residue correlation iden |
| | | 149 | | /// <param name="auditResidueId">Optional audit residue identifier override. When omitted, the residue event identif |
| | | 150 | | /// <param name="eventId">Optional lifecycle event identifier. When omitted, a new identifier is generated.</param> |
| | | 151 | | /// <param name="occurredUtc">Optional lifecycle timestamp. When omitted, the current UTC timestamp is used.</param> |
| | | 152 | | /// <param name="outcome">Optional lifecycle or host-defined outcome. When omitted, the residue outcome is used.</pa |
| | | 153 | | /// <param name="metadata">Optional host-provided lifecycle metadata merged after residue metadata.</param> |
| | | 154 | | /// <returns>An audit residue lifecycle event.</returns> |
| | | 155 | | public static AuditResidueLifecycleEvent FromResidue( |
| | | 156 | | AuditResidueLifecycleStage stage, |
| | | 157 | | IAsiBackboneAuditResidue residue, |
| | | 158 | | string? correlationId = null, |
| | | 159 | | string? auditResidueId = null, |
| | | 160 | | string? eventId = null, |
| | | 161 | | DateTimeOffset? occurredUtc = null, |
| | | 162 | | string? outcome = null, |
| | | 163 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 164 | | { |
| | 3 | 165 | | ArgumentNullException.ThrowIfNull(residue); |
| | | 166 | | |
| | 3 | 167 | | string? effectiveCorrelationId = string.IsNullOrWhiteSpace(correlationId) |
| | 3 | 168 | | ? residue.CorrelationId |
| | 3 | 169 | | : correlationId; |
| | | 170 | | |
| | 3 | 171 | | return new AuditResidueLifecycleEvent( |
| | 3 | 172 | | NormalizeIdentifier(eventId), |
| | 3 | 173 | | stage, |
| | 3 | 174 | | occurredUtc ?? DateTimeOffset.UtcNow, |
| | 3 | 175 | | effectiveCorrelationId ?? throw new ArgumentException("A lifecycle event requires a correlation identifier f |
| | 3 | 176 | | string.IsNullOrWhiteSpace(auditResidueId) ? residue.EventId : auditResidueId, |
| | 3 | 177 | | residue.TraceId, |
| | 3 | 178 | | residue.OperationName, |
| | 3 | 179 | | string.IsNullOrWhiteSpace(outcome) ? residue.Outcome : outcome, |
| | 3 | 180 | | NormalizeMetadata(residue.Metadata, metadata)); |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | private static string NormalizeIdentifier(string? identifier) |
| | | 184 | | { |
| | 104 | 185 | | return string.IsNullOrWhiteSpace(identifier) |
| | 104 | 186 | | ? Guid.NewGuid().ToString("N") |
| | 104 | 187 | | : identifier.Trim(); |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | private static string? NormalizeOptional(string? value) |
| | | 191 | | { |
| | 396 | 192 | | return string.IsNullOrWhiteSpace(value) |
| | 396 | 193 | | ? null |
| | 396 | 194 | | : value.Trim(); |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 198 | | params IReadOnlyDictionary<string, string>?[] metadataSets) |
| | | 199 | | { |
| | 103 | 200 | | Dictionary<string, string> normalizedMetadata = new(StringComparer.Ordinal); |
| | | 201 | | |
| | 416 | 202 | | foreach (IReadOnlyDictionary<string, string>? metadata in metadataSets) |
| | | 203 | | { |
| | 105 | 204 | | if (metadata is null || metadata.Count == 0) |
| | | 205 | | { |
| | | 206 | | continue; |
| | | 207 | | } |
| | | 208 | | |
| | 484 | 209 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 210 | | { |
| | 155 | 211 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 212 | | { |
| | | 213 | | continue; |
| | | 214 | | } |
| | | 215 | | |
| | 154 | 216 | | normalizedMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 217 | | } |
| | | 218 | | } |
| | | 219 | | |
| | 103 | 220 | | return normalizedMetadata.Count == 0 |
| | 103 | 221 | | ? EmptyMetadata |
| | 103 | 222 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 223 | | } |
| | | 224 | | } |