< Summary

Information
Class: Elsa.AI.Persistence.EFCore.Stores.EFCoreAIAuditSink
Assembly: Elsa.AI.Persistence.EFCore
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.AI.Persistence.EFCore/Stores/EFCoreAIAuditSink.cs
Line coverage
96%
Covered lines: 28
Uncovered lines: 1
Coverable lines: 29
Total lines: 51
Line coverage: 96.5%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
RecordAsync()100%11100%
RecordManyAsync()75%4491.66%
ToRecord(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.AI.Persistence.EFCore/Stores/EFCoreAIAuditSink.cs

#LineLine coverage
 1using Elsa.AI.Abstractions.Contracts;
 2using Elsa.AI.Abstractions.Models;
 3using Elsa.AI.Persistence.EFCore.Entities;
 4using Elsa.AI.Persistence.EFCore.Helpers;
 5using Microsoft.Extensions.Logging;
 6
 7namespace Elsa.AI.Persistence.EFCore.Stores;
 8
 49public class EFCoreAIAuditSink(AIDbContext dbContext, ILogger<EFCoreAIAuditSink> logger) : IAIAuditEventHandler
 10{
 11    public async ValueTask RecordAsync(AIAuditEvent auditEvent, CancellationToken cancellationToken = default)
 12    {
 213        await RecordManyAsync([auditEvent], cancellationToken);
 214    }
 15
 16    public async ValueTask RecordManyAsync(IReadOnlyCollection<AIAuditEvent> auditEvents, CancellationToken cancellation
 17    {
 418        if (auditEvents.Count == 0)
 019            return;
 20
 21        try
 22        {
 1923            foreach (var auditEvent in auditEvents)
 624                dbContext.AuditRecords.Add(ToRecord(auditEvent));
 25
 326            await dbContext.SaveChangesAsync(cancellationToken);
 327            dbContext.ChangeTracker.Clear();
 328        }
 129        catch (Exception e) when (ExceptionFilters.IsNonFatal(e))
 30        {
 131            dbContext.ChangeTracker.Clear();
 132            logger.LogWarning(e, "Failed to persist {AuditEventCount} AI audit event(s)", auditEvents.Count);
 133        }
 434    }
 35
 36    private static AIAuditRecord ToRecord(AIAuditEvent auditEvent) =>
 637        new()
 638        {
 639            Id = auditEvent.Id,
 640            TenantId = auditEvent.TenantId,
 641            ActorId = auditEvent.ActorId,
 642            ConversationId = auditEvent.ConversationId,
 643            ProposalId = auditEvent.ProposalId,
 644            ToolInvocationId = auditEvent.ToolInvocationId,
 645            Type = auditEvent.Type,
 646            Timestamp = auditEvent.Timestamp,
 647            TraceId = auditEvent.TraceId,
 648            Summary = auditEvent.Summary,
 649            Data = auditEvent.Data.ToJsonString()
 650        };
 51}