refactor(dotnet): normalize decision learning records
This commit is contained in:
@@ -25,6 +25,12 @@ public sealed class DecisionLearningService
|
||||
object? trace = null,
|
||||
object? provenance = null)
|
||||
{
|
||||
decisionKey = RequireValue(decisionKey, nameof(decisionKey));
|
||||
instrumentId = RequireValue(instrumentId, nameof(instrumentId));
|
||||
action = RequireValue(action, nameof(action));
|
||||
gate = RequireValue(gate, nameof(gate));
|
||||
sourceVersion = RequireValue(sourceVersion, nameof(sourceVersion));
|
||||
|
||||
var decisionId = await _store.AppendDecisionAsync(new DecisionEventRecord(
|
||||
decisionKey,
|
||||
decidedAt,
|
||||
@@ -33,29 +39,30 @@ public sealed class DecisionLearningService
|
||||
gate,
|
||||
score,
|
||||
sourceVersion,
|
||||
JsonSerializer.Serialize(trace ?? new { }),
|
||||
JsonSerializer.Serialize(provenance ?? new { })));
|
||||
SerializeJson(trace),
|
||||
SerializeJson(provenance)));
|
||||
|
||||
foreach (var factor in factors)
|
||||
foreach (var factor in factors ?? throw new ArgumentNullException(nameof(factors)))
|
||||
{
|
||||
var normalizedFactor = NormalizeFactor(factor);
|
||||
var observationId = await _store.AppendSourceObservationAsync(new SourceObservationRecord(
|
||||
factor.ObservedAt,
|
||||
normalizedFactor.ObservedAt,
|
||||
instrumentId,
|
||||
factor.SourceName,
|
||||
normalizedFactor.SourceName,
|
||||
sourceVersion,
|
||||
factor.PayloadJson,
|
||||
factor.ProvenanceJson));
|
||||
normalizedFactor.PayloadJson,
|
||||
normalizedFactor.ProvenanceJson));
|
||||
var factorObservationId = await _store.AppendFactorObservationAsync(new FactorObservationRecord(
|
||||
observationId,
|
||||
factor.FactorObservationId,
|
||||
factor.FactorId,
|
||||
factor.FactorVersion,
|
||||
factor.ObservedAt,
|
||||
factor.NumericValue,
|
||||
factor.TextValue,
|
||||
factor.Gate,
|
||||
factor.ProvenanceJson));
|
||||
await _store.AppendDecisionFactorEvidenceAsync(decisionId, factorObservationId, factor.Role);
|
||||
normalizedFactor.FactorObservationId,
|
||||
normalizedFactor.FactorId,
|
||||
normalizedFactor.FactorVersion,
|
||||
normalizedFactor.ObservedAt,
|
||||
normalizedFactor.NumericValue,
|
||||
normalizedFactor.TextValue,
|
||||
normalizedFactor.Gate,
|
||||
normalizedFactor.ProvenanceJson));
|
||||
await _store.AppendDecisionFactorEvidenceAsync(decisionId, factorObservationId, normalizedFactor.Role);
|
||||
}
|
||||
|
||||
return decisionId;
|
||||
@@ -83,7 +90,36 @@ public sealed class DecisionLearningService
|
||||
excessReturn,
|
||||
outcomeClass,
|
||||
evaluationGate,
|
||||
JsonSerializer.Serialize(provenance ?? new { })));
|
||||
SerializeJson(provenance)));
|
||||
}
|
||||
|
||||
private static string SerializeJson(object? value)
|
||||
=> JsonSerializer.Serialize(value ?? new { });
|
||||
|
||||
private static string RequireValue(string value, string parameterName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
throw new ArgumentException("Value is required.", parameterName);
|
||||
}
|
||||
|
||||
return value.Trim();
|
||||
}
|
||||
|
||||
private static FactorEvidenceInput NormalizeFactor(FactorEvidenceInput factor)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(factor);
|
||||
|
||||
return factor with
|
||||
{
|
||||
FactorId = RequireValue(factor.FactorId, nameof(factor.FactorId)),
|
||||
FactorVersion = RequireValue(factor.FactorVersion, nameof(factor.FactorVersion)),
|
||||
SourceName = RequireValue(factor.SourceName, nameof(factor.SourceName)),
|
||||
PayloadJson = RequireValue(factor.PayloadJson, nameof(factor.PayloadJson)),
|
||||
ProvenanceJson = RequireValue(factor.ProvenanceJson, nameof(factor.ProvenanceJson)),
|
||||
Gate = RequireValue(factor.Gate, nameof(factor.Gate)),
|
||||
Role = RequireValue(factor.Role, nameof(factor.Role))
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user