|
| 1 | +using ModularityKit.Mutator.Abstractions.Audit; |
| 2 | +using ModularityKit.Mutator.Abstractions.Changes; |
| 3 | +using ModularityKit.Mutator.Abstractions.Context; |
| 4 | +using ModularityKit.Mutator.Abstractions.Engine; |
| 5 | +using ModularityKit.Mutator.Abstractions.Effects; |
| 6 | +using ModularityKit.Mutator.Abstractions.History; |
| 7 | +using ModularityKit.Mutator.Abstractions.Policies; |
| 8 | +using ModularityKit.Mutator.Abstractions.Results; |
| 9 | + |
| 10 | +namespace ModularityKit.Mutator.Runtime.Diagnostics; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Factory for creating audit and history entries for mutations. |
| 14 | +/// </summary> |
| 15 | +internal static class MutationAuditEntryFactory |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Creates a successful mutation audit entry. |
| 19 | + /// </summary> |
| 20 | + /// <typeparam name="TState">The state type handled by the mutation.</typeparam> |
| 21 | + /// <param name="mutation">The mutation that was executed.</param> |
| 22 | + /// <param name="result">The result of the mutation execution.</param> |
| 23 | + /// <param name="policyDecision">The policy decision applied to the mutation.</param> |
| 24 | + /// <param name="executionId">The unique identifier of the execution.</param> |
| 25 | + /// <param name="duration">The execution duration.</param> |
| 26 | + /// <returns>A configured <see cref="MutationAuditEntry"/> representing success.</returns> |
| 27 | + public static MutationAuditEntry CreateSuccess<TState>( |
| 28 | + IMutation<TState> mutation, |
| 29 | + MutationResult<TState> result, |
| 30 | + PolicyDecision policyDecision, |
| 31 | + string executionId, |
| 32 | + TimeSpan duration) |
| 33 | + { |
| 34 | + return Create( |
| 35 | + mutation, |
| 36 | + executionId, |
| 37 | + duration, |
| 38 | + isSuccess: true, |
| 39 | + changes: result.Changes, |
| 40 | + policyDecisions: result.PolicyDecisions.Count > 0 ? result.PolicyDecisions : [policyDecision], |
| 41 | + sideEffects: result.SideEffects, |
| 42 | + sourceIpAddress: mutation.Context.SourceIpAddress, |
| 43 | + userAgent: mutation.Context.UserAgent); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Creates a failed mutation audit entry. |
| 48 | + /// </summary> |
| 49 | + /// <typeparam name="TState">The state type handled by the mutation.</typeparam> |
| 50 | + /// <param name="mutation">The mutation that was executed.</param> |
| 51 | + /// <param name="result">The result of the mutation execution.</param> |
| 52 | + /// <param name="executionId">The unique identifier of the execution.</param> |
| 53 | + /// <param name="duration">The execution duration.</param> |
| 54 | + /// <returns>A configured <see cref="MutationAuditEntry"/> representing failure.</returns> |
| 55 | + public static MutationAuditEntry CreateFailure<TState>( |
| 56 | + IMutation<TState> mutation, |
| 57 | + MutationResult<TState> result, |
| 58 | + string executionId, |
| 59 | + TimeSpan duration) |
| 60 | + { |
| 61 | + return Create( |
| 62 | + mutation, |
| 63 | + executionId, |
| 64 | + duration, |
| 65 | + isSuccess: false, |
| 66 | + changes: result.Changes, |
| 67 | + errorMessage: string.Join("; ", result.ValidationResult.Errors.Select(e => e.Message)), |
| 68 | + policyDecisions: result.PolicyDecisions, |
| 69 | + sideEffects: result.SideEffects); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Creates a failed mutation audit entry due to an exception. |
| 74 | + /// </summary> |
| 75 | + /// <typeparam name="TState">The state type handled by the mutation.</typeparam> |
| 76 | + /// <param name="mutation">The mutation that was executed.</param> |
| 77 | + /// <param name="exception">The exception that occurred.</param> |
| 78 | + /// <param name="executionId">The unique identifier of the execution.</param> |
| 79 | + /// <param name="duration">The execution duration.</param> |
| 80 | + /// <returns>A configured <see cref="MutationAuditEntry"/> representing an exception failure.</returns> |
| 81 | + public static MutationAuditEntry CreateException<TState>( |
| 82 | + IMutation<TState> mutation, |
| 83 | + Exception exception, |
| 84 | + string executionId, |
| 85 | + TimeSpan duration) |
| 86 | + { |
| 87 | + return Create( |
| 88 | + mutation, |
| 89 | + executionId, |
| 90 | + duration, |
| 91 | + isSuccess: false, |
| 92 | + errorMessage: exception.Message); |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Creates a mutation history entry for persistence. |
| 97 | + /// </summary> |
| 98 | + /// <typeparam name="TState">The state type handled by the mutation.</typeparam> |
| 99 | + /// <param name="mutation">The mutation that was executed.</param> |
| 100 | + /// <param name="result">The result of the mutation execution.</param> |
| 101 | + /// <param name="executionId">The unique identifier of the execution.</param> |
| 102 | + /// <param name="stateId">The identifier of the target state.</param> |
| 103 | + /// <param name="duration">The execution duration.</param> |
| 104 | + /// <returns>A configured <see cref="MutationHistoryEntry"/>.</returns> |
| 105 | + public static MutationHistoryEntry CreateHistoryEntry<TState>( |
| 106 | + IMutation<TState> mutation, |
| 107 | + MutationResult<TState> result, |
| 108 | + string executionId, |
| 109 | + string stateId, |
| 110 | + TimeSpan duration) |
| 111 | + { |
| 112 | + return new MutationHistoryEntry |
| 113 | + { |
| 114 | + ExecutionId = executionId, |
| 115 | + StateId = stateId, |
| 116 | + Intent = mutation.Intent, |
| 117 | + Context = mutation.Context, |
| 118 | + Changes = result.Changes, |
| 119 | + SideEffects = result.SideEffects.ToList(), |
| 120 | + Timestamp = mutation.Context.Timestamp, |
| 121 | + ExecutionTime = duration |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Resolves the state identifier from the mutation context. |
| 127 | + /// </summary> |
| 128 | + /// <param name="context">The mutation context.</param> |
| 129 | + /// <returns>The resolved state ID or correlation ID.</returns> |
| 130 | + public static string? ResolveStateId(MutationContext context) => |
| 131 | + context.StateId ?? context.CorrelationId; |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Helper method to create a mutation audit entry. |
| 135 | + /// </summary> |
| 136 | + private static MutationAuditEntry Create<TState>( |
| 137 | + IMutation<TState> mutation, |
| 138 | + string executionId, |
| 139 | + TimeSpan duration, |
| 140 | + bool isSuccess, |
| 141 | + ChangeSet? changes = null, |
| 142 | + string? errorMessage = null, |
| 143 | + IReadOnlyList<PolicyDecision>? policyDecisions = null, |
| 144 | + IReadOnlyList<SideEffect>? sideEffects = null, |
| 145 | + string? sourceIpAddress = null, |
| 146 | + string? userAgent = null) |
| 147 | + { |
| 148 | + return new MutationAuditEntry |
| 149 | + { |
| 150 | + ExecutionId = executionId, |
| 151 | + StateId = ResolveStateId(mutation.Context), |
| 152 | + StateType = typeof(TState).Name, |
| 153 | + MutationIntent = mutation.Intent, |
| 154 | + Context = mutation.Context, |
| 155 | + Changes = changes ?? ChangeSet.Empty, |
| 156 | + IsSuccess = isSuccess, |
| 157 | + ErrorMessage = errorMessage, |
| 158 | + PolicyDecisions = policyDecisions ?? [], |
| 159 | + SideEffects = sideEffects ?? [], |
| 160 | + Timestamp = mutation.Context.Timestamp, |
| 161 | + Duration = duration, |
| 162 | + SourceIpAddress = sourceIpAddress, |
| 163 | + UserAgent = userAgent |
| 164 | + }; |
| 165 | + } |
| 166 | +} |
0 commit comments