| | | 1 | | using System.Text.Json; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | using Elsa.Workflows.Runtime; |
| | | 6 | | using Elsa.Workflows.Runtime.Entities; |
| | | 7 | | using Elsa.Workflows.Runtime.Extensions; |
| | | 8 | | using Elsa.Workflows.Runtime.Filters; |
| | | 9 | | using Elsa.Workflows.Runtime.OrderDefinitions; |
| | | 10 | | using Open.Linq.AsyncExtensions; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Persistence.EFCore.Modules.Runtime; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// An EF Core implementation of <see cref="IWorkflowExecutionLogStore"/>. |
| | | 16 | | /// </summary> |
| | 321 | 17 | | public class EFCoreWorkflowExecutionLogStore(EntityStore<RuntimeElsaDbContext, WorkflowExecutionLogRecord> store, ISafeS |
| | | 18 | | { |
| | | 19 | | /// <inheritdoc /> |
| | 0 | 20 | | public async Task AddAsync(WorkflowExecutionLogRecord record, CancellationToken cancellationToken = default) => awai |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public async Task AddManyAsync(IEnumerable<WorkflowExecutionLogRecord> records, CancellationToken cancellationToken |
| | | 24 | | { |
| | 323 | 25 | | await store.AddManyAsync(records, OnSaveAsync, cancellationToken); |
| | 323 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public async Task SaveAsync(WorkflowExecutionLogRecord record, CancellationToken cancellationToken = default) |
| | | 30 | | { |
| | 0 | 31 | | await store.SaveAsync(record, OnSaveAsync, cancellationToken); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | | 35 | | public async Task SaveManyAsync(IEnumerable<WorkflowExecutionLogRecord> records, CancellationToken cancellationToken |
| | | 36 | | { |
| | 0 | 37 | | await store.SaveManyAsync(records, OnSaveAsync, cancellationToken); |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | public async Task<WorkflowExecutionLogRecord?> FindAsync(WorkflowExecutionLogRecordFilter filter, CancellationToken |
| | | 42 | | { |
| | 0 | 43 | | return await store.QueryAsync(queryable => Filter(queryable, filter), OnLoadAsync, cancellationToken).FirstOrDef |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | public async Task<WorkflowExecutionLogRecord?> FindAsync<TOrderBy>(WorkflowExecutionLogRecordFilter filter, Workflow |
| | | 48 | | { |
| | 0 | 49 | | return await store.QueryAsync(queryable => Filter(queryable, filter).OrderBy(order), OnLoadAsync, cancellationTo |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc /> |
| | | 53 | | public async Task<Page<WorkflowExecutionLogRecord>> FindManyAsync(WorkflowExecutionLogRecordFilter filter, PageArgs |
| | | 54 | | { |
| | 16 | 55 | | var count = await store.QueryAsync(queryable => Filter(queryable, filter), cancellationToken).LongCount(); |
| | 16 | 56 | | var results = await store.QueryAsync(queryable => Filter(queryable, filter).OrderBy(x => x.Timestamp).Paginate(p |
| | 8 | 57 | | return new(results, count); |
| | 8 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | public async Task<Page<WorkflowExecutionLogRecord>> FindManyAsync<TOrderBy>(WorkflowExecutionLogRecordFilter filter, |
| | | 62 | | { |
| | 0 | 63 | | var count = await store.QueryAsync(queryable => Filter(queryable, filter), cancellationToken).LongCount(); |
| | 0 | 64 | | var results = await store.QueryAsync(queryable => Filter(queryable, filter).OrderBy(order).Paginate(pageArgs), O |
| | 0 | 65 | | return new(results, count); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc /> |
| | | 69 | | public async Task<long> DeleteManyAsync(WorkflowExecutionLogRecordFilter filter, CancellationToken cancellationToken |
| | | 70 | | { |
| | 26 | 71 | | return await store.DeleteWhereAsync(queryable => Filter(queryable, filter), cancellationToken); |
| | 13 | 72 | | } |
| | | 73 | | |
| | | 74 | | private ValueTask OnSaveAsync(RuntimeElsaDbContext dbContext, WorkflowExecutionLogRecord entity, CancellationToken c |
| | | 75 | | { |
| | 4769 | 76 | | entity = entity.SanitizeLogMessage(); |
| | 4769 | 77 | | dbContext.Entry(entity).Property("SerializedPayload").CurrentValue = ShouldSerializePayload(entity) ? safeSerial |
| | 4769 | 78 | | return ValueTask.CompletedTask; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | private async ValueTask OnLoadAsync(RuntimeElsaDbContext dbContext, WorkflowExecutionLogRecord? entity, Cancellation |
| | | 82 | | { |
| | 32 | 83 | | if (entity is null) |
| | 0 | 84 | | return; |
| | | 85 | | |
| | 32 | 86 | | entity.Payload = await LoadPayload(dbContext, entity); |
| | 32 | 87 | | } |
| | | 88 | | |
| | | 89 | | private ValueTask<object?> LoadPayload(RuntimeElsaDbContext dbContext, WorkflowExecutionLogRecord entity) |
| | | 90 | | { |
| | 32 | 91 | | var json = dbContext.Entry(entity).Property<string>("SerializedPayload").CurrentValue; |
| | 32 | 92 | | return new(!string.IsNullOrEmpty(json) ? JsonSerializer.Deserialize<object>(json) : null); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | private bool ShouldSerializePayload(WorkflowExecutionLogRecord source) |
| | | 96 | | { |
| | 4769 | 97 | | return source.Payload switch |
| | 4769 | 98 | | { |
| | 4764 | 99 | | null => false, |
| | 0 | 100 | | IDictionary<string, object> dictionary => dictionary.Count > 0, |
| | 5 | 101 | | _ => true |
| | 4769 | 102 | | }; |
| | | 103 | | } |
| | | 104 | | |
| | 29 | 105 | | private static IQueryable<WorkflowExecutionLogRecord> Filter(IQueryable<WorkflowExecutionLogRecord> queryable, Workf |
| | | 106 | | } |