| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using Elsa.Common.Services; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Workflows.Management.Entities; |
| | | 6 | | using Elsa.Workflows.Management.Filters; |
| | | 7 | | using Elsa.Workflows.Management.Models; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Management.Stores; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// A non-persistent memory store for saving and loading <see cref="WorkflowInstance"/> entities. |
| | | 13 | | /// </summary> |
| | | 14 | | public class MemoryWorkflowInstanceStore : IWorkflowInstanceStore |
| | | 15 | | { |
| | | 16 | | private readonly MemoryStore<WorkflowInstance> _store; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Constructor. |
| | | 20 | | /// </summary> |
| | 0 | 21 | | public MemoryWorkflowInstanceStore(MemoryStore<WorkflowInstance> store) |
| | | 22 | | { |
| | 0 | 23 | | _store = store; |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public ValueTask<WorkflowInstance?> FindAsync(WorkflowInstanceFilter filter, CancellationToken cancellationToken = d |
| | | 28 | | { |
| | 0 | 29 | | var entity = _store.Query(query => Filter(query, filter)).FirstOrDefault(); |
| | 0 | 30 | | return ValueTask.FromResult(entity); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | public ValueTask<Page<WorkflowInstance>> FindManyAsync(WorkflowInstanceFilter filter, PageArgs pageArgs, Cancellatio |
| | | 35 | | { |
| | 0 | 36 | | var count = _store.Query(query => Filter(query, filter)).LongCount(); |
| | 0 | 37 | | var entities = _store.Query(query => Filter(query, filter).Paginate(pageArgs)).ToList(); |
| | 0 | 38 | | var page = Page.Of(entities, count); |
| | 0 | 39 | | return ValueTask.FromResult(page); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public ValueTask<Page<WorkflowInstance>> FindManyAsync<TOrderBy>(WorkflowInstanceFilter filter, PageArgs pageArgs, W |
| | | 44 | | { |
| | 0 | 45 | | var count = _store.Query(query => Filter(query, filter)).LongCount(); |
| | 0 | 46 | | var entities = _store.Query(query => Filter(query, filter).OrderBy(order).Paginate(pageArgs)).ToList(); |
| | 0 | 47 | | var page = Page.Of(entities, count); |
| | 0 | 48 | | return ValueTask.FromResult(page); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | | 52 | | public ValueTask<IEnumerable<WorkflowInstance>> FindManyAsync(WorkflowInstanceFilter filter, CancellationToken cance |
| | | 53 | | { |
| | 0 | 54 | | var entities = _store.Query(query => Filter(query, filter)).ToList().AsEnumerable(); |
| | 0 | 55 | | return ValueTask.FromResult(entities); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <inheritdoc /> |
| | | 59 | | public ValueTask<IEnumerable<WorkflowInstance>> FindManyAsync<TOrderBy>(WorkflowInstanceFilter filter, WorkflowInsta |
| | | 60 | | { |
| | 0 | 61 | | var entities = _store.Query(query => Filter(query, filter).OrderBy(order)).ToList().AsEnumerable(); |
| | 0 | 62 | | return ValueTask.FromResult(entities); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <inheritdoc /> |
| | | 66 | | public ValueTask<long> CountAsync(WorkflowInstanceFilter filter, CancellationToken cancellationToken = default) |
| | | 67 | | { |
| | 0 | 68 | | var count = _store.Query(query => Filter(query, filter)).LongCount(); |
| | 0 | 69 | | return new(count); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <inheritdoc /> |
| | | 73 | | public async ValueTask<Page<WorkflowInstanceSummary>> SummarizeManyAsync(WorkflowInstanceFilter filter, PageArgs pag |
| | | 74 | | { |
| | 0 | 75 | | var page = await FindManyAsync(filter, pageArgs, cancellationToken); |
| | 0 | 76 | | return new(page.Items.Select(WorkflowInstanceSummary.FromInstance).ToList(), page.TotalCount); |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <inheritdoc /> |
| | | 80 | | public async ValueTask<Page<WorkflowInstanceSummary>> SummarizeManyAsync<TOrderBy>(WorkflowInstanceFilter filter, Pa |
| | | 81 | | { |
| | 0 | 82 | | var page = await FindManyAsync(filter, pageArgs, order, cancellationToken); |
| | 0 | 83 | | return new(page.Items.Select(WorkflowInstanceSummary.FromInstance).ToList(), page.TotalCount); |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <inheritdoc /> |
| | | 87 | | public async ValueTask<IEnumerable<WorkflowInstanceSummary>> SummarizeManyAsync(WorkflowInstanceFilter filter, Cance |
| | | 88 | | { |
| | 0 | 89 | | var entities = await FindManyAsync(filter, cancellationToken); |
| | 0 | 90 | | return entities.Select(WorkflowInstanceSummary.FromInstance); |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <inheritdoc /> |
| | | 94 | | public async ValueTask<IEnumerable<WorkflowInstanceSummary>> SummarizeManyAsync<TOrderBy>(WorkflowInstanceFilter fil |
| | | 95 | | { |
| | 0 | 96 | | var entities = await FindManyAsync(filter, order, cancellationToken); |
| | 0 | 97 | | return entities.Select(WorkflowInstanceSummary.FromInstance); |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <inheritdoc /> |
| | | 101 | | public ValueTask<IEnumerable<string>> FindManyIdsAsync(WorkflowInstanceFilter filter, CancellationToken cancellation |
| | | 102 | | { |
| | 0 | 103 | | var entities = _store.Query(query => Filter(query, filter)).Select(x => x.Id).ToList().AsEnumerable(); |
| | 0 | 104 | | return ValueTask.FromResult(entities); |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <inheritdoc /> |
| | | 108 | | public async ValueTask<Page<string>> FindManyIdsAsync(WorkflowInstanceFilter filter, PageArgs pageArgs, Cancellation |
| | | 109 | | { |
| | 0 | 110 | | var page = await FindManyAsync(filter, pageArgs, cancellationToken); |
| | 0 | 111 | | var ids = page.Items.Select(x => x.Id).ToList(); |
| | 0 | 112 | | return new(ids, page.TotalCount); |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <inheritdoc /> |
| | | 116 | | public async ValueTask<Page<string>> FindManyIdsAsync<TOrderBy>(WorkflowInstanceFilter filter, PageArgs pageArgs, Wo |
| | | 117 | | { |
| | 0 | 118 | | var page = await FindManyAsync(filter, pageArgs, order, cancellationToken); |
| | 0 | 119 | | var ids = page.Items.Select(x => x.Id).ToList(); |
| | 0 | 120 | | return new(ids, page.TotalCount); |
| | 0 | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <inheritdoc /> |
| | | 124 | | public ValueTask SaveAsync(WorkflowInstance instance, CancellationToken cancellationToken = default) |
| | | 125 | | { |
| | 0 | 126 | | _store.Save(instance, x => x.Id); |
| | 0 | 127 | | return ValueTask.CompletedTask; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | public ValueTask AddAsync(WorkflowInstance instance, CancellationToken cancellationToken = default) |
| | | 131 | | { |
| | 0 | 132 | | _store.Add(instance, GetId); |
| | 0 | 133 | | return ValueTask.CompletedTask; |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | public ValueTask UpdateAsync(WorkflowInstance instance, CancellationToken cancellationToken = default) |
| | | 137 | | { |
| | 0 | 138 | | _store.Update(instance, GetId); |
| | 0 | 139 | | return ValueTask.CompletedTask; |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <inheritdoc /> |
| | | 143 | | public ValueTask SaveManyAsync(IEnumerable<WorkflowInstance> instances, CancellationToken cancellationToken = defaul |
| | | 144 | | { |
| | 0 | 145 | | _store.SaveMany(instances, GetId); |
| | 0 | 146 | | return ValueTask.CompletedTask; |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <inheritdoc /> |
| | | 150 | | public ValueTask<long> DeleteAsync(WorkflowInstanceFilter filter, CancellationToken cancellationToken = default) |
| | | 151 | | { |
| | 0 | 152 | | var query = Filter(_store.List().AsQueryable(), filter); |
| | 0 | 153 | | var count = _store.DeleteMany(query, x => x.Id); |
| | 0 | 154 | | return ValueTask.FromResult(count); |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <inheritdoc /> |
| | | 158 | | public async Task UpdateUpdatedTimestampAsync(string workflowInstanceId, DateTimeOffset value, CancellationToken can |
| | | 159 | | { |
| | 0 | 160 | | var workflowInstance = await FindAsync(new() |
| | 0 | 161 | | { |
| | 0 | 162 | | Id = workflowInstanceId |
| | 0 | 163 | | }, cancellationToken); |
| | | 164 | | |
| | 0 | 165 | | if (workflowInstance == null) |
| | 0 | 166 | | throw new InvalidOperationException($"Workflow instance with ID '{workflowInstanceId}' does not exist."); |
| | | 167 | | |
| | 0 | 168 | | workflowInstance.UpdatedAt = value; |
| | 0 | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | private static string GetId(WorkflowInstance workflowInstance) => workflowInstance.Id; |
| | | 172 | | |
| | | 173 | | [RequiresUnreferencedCode("Calls Elsa.Workflows.Management.Filters.WorkflowInstanceFilter.Apply(IQueryable<WorkflowI |
| | 0 | 174 | | private static IQueryable<WorkflowInstance> Filter(IQueryable<WorkflowInstance> query, WorkflowInstanceFilter filter |
| | | 175 | | } |