< Summary

Information
Class: Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Stores.RelationalStructuredLogStore
Assembly: Elsa.Diagnostics.StructuredLogs.Persistence.Relational
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Diagnostics.StructuredLogs.Persistence.Relational/Stores/RelationalStructuredLogStore.cs
Line coverage
6%
Covered lines: 5
Uncovered lines: 77
Coverable lines: 82
Total lines: 130
Line coverage: 6%
Branch coverage
0%
Covered branches: 0
Total branches: 40
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
WriteAsync()0%620%
WriteManyAsync()0%7280%
QueryAsync()0%110100%
ListSourcesAsync()0%110100%
InsertAsync()0%620%
CreateCommand(...)0%7280%
CreateParameters(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Diagnostics.StructuredLogs.Persistence.Relational/Stores/RelationalStructuredLogStore.cs

#LineLine coverage
 1using System.Data;
 2using System.Data.Common;
 3using Elsa.Diagnostics.StructuredLogs.Contracts;
 4using Elsa.Diagnostics.StructuredLogs.Models;
 5using Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Contracts;
 6using Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Models;
 7using Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Services;
 8
 9namespace Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Stores;
 10
 111public class RelationalStructuredLogStore(
 112    IRelationalStructuredLogConnectionFactory connectionFactory,
 113    IRelationalStructuredLogDialect dialect,
 114    RelationalStructuredLogSqlBuilder sqlBuilder,
 115    RelationalStructuredLogMapper mapper) : IStructuredLogStore
 16{
 17    public async ValueTask WriteAsync(StructuredLogEvent logEvent, CancellationToken cancellationToken = default)
 18    {
 019        await using var connection = await connectionFactory.OpenConnectionAsync(cancellationToken);
 020        await InsertAsync(connection, mapper.Map(logEvent), null, cancellationToken);
 021    }
 22
 23    public async ValueTask WriteManyAsync(IReadOnlyCollection<StructuredLogEvent> logEvents, CancellationToken cancellat
 24    {
 025        if (logEvents.Count == 0)
 026            return;
 27
 028        await using var connection = await connectionFactory.OpenConnectionAsync(cancellationToken);
 029        await using var transaction = await connection.BeginTransactionAsync(cancellationToken);
 30
 031        foreach (var logEvent in logEvents)
 032            await InsertAsync(connection, mapper.Map(logEvent), transaction, cancellationToken);
 33
 034        await transaction.CommitAsync(cancellationToken);
 035    }
 36
 37    public async ValueTask<RecentStructuredLogsResult> QueryAsync(StructuredLogFilter filter, CancellationToken cancella
 38    {
 039        var query = sqlBuilder.BuildQuery(filter);
 040        await using var connection = await connectionFactory.OpenConnectionAsync(cancellationToken);
 041        await using var command = CreateCommand(connection, query.Sql, query.Parameters);
 042        await using var reader = await command.ExecuteReaderAsync(cancellationToken);
 043        var items = new List<StructuredLogEvent>();
 44
 045        while (await reader.ReadAsync(cancellationToken))
 046            items.Add(mapper.Map(reader));
 47
 048        items.Reverse();
 049        return new(items, 0);
 050    }
 51
 52    public async ValueTask<IReadOnlyCollection<StructuredLogSource>> ListSourcesAsync(CancellationToken cancellationToke
 53    {
 054        await using var connection = await connectionFactory.OpenConnectionAsync(cancellationToken);
 055        await using var command = CreateCommand(connection, sqlBuilder.BuildListSources());
 056        await using var reader = await command.ExecuteReaderAsync(cancellationToken);
 057        var sources = new List<StructuredLogSource>();
 58
 059        while (await reader.ReadAsync(cancellationToken))
 60        {
 061            var sourceId = reader.GetString(reader.GetOrdinal("SourceId"));
 062            var lastSeen = RelationalStructuredLogMapper.ParseTimestamp(reader.GetString(reader.GetOrdinal("LastSeen")))
 063            sources.Add(new()
 064            {
 065                Id = sourceId,
 066                DisplayName = sourceId,
 067                MachineName = sourceId,
 068                ProcessId = 0,
 069                LastSeen = lastSeen,
 070                Status = StructuredLogSourceStatus.Connected
 071            });
 72        }
 73
 074        return sources;
 075    }
 76
 77    private async ValueTask InsertAsync(DbConnection connection, RelationalStructuredLogRecord record, DbTransaction? tr
 78    {
 079        await using var command = CreateCommand(connection, sqlBuilder.BuildInsert(), CreateParameters(record));
 080        command.Transaction = transaction;
 081        await command.ExecuteNonQueryAsync(cancellationToken);
 082    }
 83
 84    private DbCommand CreateCommand(DbConnection connection, string sql, IReadOnlyDictionary<string, object?>? parameter
 85    {
 086        var command = connection.CreateCommand();
 087        command.CommandText = sql;
 088        command.CommandType = CommandType.Text;
 89
 090        if (parameters == null)
 091            return command;
 92
 093        foreach (var (name, value) in parameters)
 94        {
 095            var parameter = command.CreateParameter();
 096            parameter.ParameterName = name.StartsWith(dialect.ParameterPrefix, StringComparison.Ordinal) ? name : $"{dia
 097            parameter.Value = value ?? DBNull.Value;
 098            command.Parameters.Add(parameter);
 99        }
 100
 0101        return command;
 102    }
 103
 104    private static IReadOnlyDictionary<string, object?> CreateParameters(RelationalStructuredLogRecord record)
 105    {
 0106        return new Dictionary<string, object?>
 0107        {
 0108            ["Id"] = record.Id,
 0109            ["Sequence"] = record.Sequence,
 0110            ["Timestamp"] = record.Timestamp,
 0111            ["ReceivedAt"] = record.ReceivedAt,
 0112            ["Level"] = (int)record.Level,
 0113            ["Category"] = record.Category,
 0114            ["EventId"] = record.EventId,
 0115            ["EventName"] = record.EventName,
 0116            ["Message"] = record.Message,
 0117            ["MessageTemplate"] = record.MessageTemplate,
 0118            ["ExceptionJson"] = record.ExceptionJson,
 0119            ["ScopesJson"] = record.ScopesJson,
 0120            ["PropertiesJson"] = record.PropertiesJson,
 0121            ["TraceId"] = record.TraceId,
 0122            ["SpanId"] = record.SpanId,
 0123            ["CorrelationId"] = record.CorrelationId,
 0124            ["TenantId"] = record.TenantId,
 0125            ["WorkflowDefinitionId"] = record.WorkflowDefinitionId,
 0126            ["WorkflowInstanceId"] = record.WorkflowInstanceId,
 0127            ["SourceId"] = record.SourceId
 0128        };
 129    }
 130}