| | | 1 | | using Elsa.Alterations.Core.Contracts; |
| | | 2 | | using Elsa.Alterations.Core.Models; |
| | | 3 | | using Elsa.Workflows; |
| | | 4 | | using Elsa.Workflows.Management; |
| | | 5 | | using Elsa.Workflows.Management.Filters; |
| | | 6 | | using Elsa.Workflows.Runtime; |
| | | 7 | | using Elsa.Workflows.Runtime.Filters; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Alterations.Core.Services; |
| | | 10 | | |
| | | 11 | | /// <inheritdoc /> |
| | 1 | 12 | | public class WorkflowInstanceFinder(IWorkflowInstanceStore workflowInstanceStore, IActivityExecutionStore activityExecut |
| | | 13 | | { |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public async Task<IEnumerable<string>> FindAsync(AlterationWorkflowInstanceFilter filter, CancellationToken cancella |
| | | 16 | | { |
| | 0 | 17 | | var workflowInstanceFilter = new WorkflowInstanceFilter |
| | 0 | 18 | | { |
| | 0 | 19 | | Ids = filter.WorkflowInstanceIds?.ToList(), |
| | 0 | 20 | | DefinitionIds = filter.DefinitionIds, |
| | 0 | 21 | | DefinitionVersionIds = filter.DefinitionVersionIds?.ToList(), |
| | 0 | 22 | | CorrelationIds = filter.CorrelationIds?.ToList(), |
| | 0 | 23 | | HasIncidents = filter.HasIncidents, |
| | 0 | 24 | | IsSystem = filter.IsSystem, |
| | 0 | 25 | | TimestampFilters = filter.TimestampFilters?.ToList(), |
| | 0 | 26 | | WorkflowStatuses = filter.Statuses?.ToList(), |
| | 0 | 27 | | WorkflowSubStatuses = filter.SubStatuses?.ToList(), |
| | 0 | 28 | | Names = filter.Names?.ToList(), |
| | 0 | 29 | | SearchTerm = filter.SearchTerm, |
| | 0 | 30 | | }; |
| | 0 | 31 | | var activityExecutionFilters = filter.ActivityFilters?.Select(x => new ActivityExecutionRecordFilter |
| | 0 | 32 | | { |
| | 0 | 33 | | ActivityId = x.ActivityId, |
| | 0 | 34 | | Id = x.ActivityInstanceId, |
| | 0 | 35 | | ActivityNodeId = x.NodeId, |
| | 0 | 36 | | Name = x.Name, |
| | 0 | 37 | | Status = x.Status, |
| | 0 | 38 | | }).ToList(); |
| | | 39 | | |
| | 0 | 40 | | var emptyFilterSelectsAll = filter.EmptyFilterSelectsAll; |
| | 0 | 41 | | var workflowInstanceFilterIsEmpty = WorkflowFilterIsEmpty(workflowInstanceFilter); |
| | | 42 | | |
| | 0 | 43 | | var workflowInstanceIds = workflowInstanceFilterIsEmpty && !emptyFilterSelectsAll |
| | 0 | 44 | | ? Enumerable.Empty<string>().ToHashSet() |
| | 0 | 45 | | : (await workflowInstanceStore.FindManyIdsAsync(workflowInstanceFilter, cancellationToken)).ToHashSet(); |
| | | 46 | | |
| | 0 | 47 | | if (activityExecutionFilters == null) |
| | 0 | 48 | | return workflowInstanceIds; |
| | | 49 | | |
| | 0 | 50 | | foreach (ActivityExecutionRecordFilter activityExecutionFilter in activityExecutionFilters.Where(x => !x.IsEmpty |
| | | 51 | | { |
| | 0 | 52 | | var activityExecutionRecords = await activityExecutionStore.FindManySummariesAsync(activityExecutionFilter, |
| | 0 | 53 | | var matchingWorkflowInstanceIds = activityExecutionRecords.Select(x => x.WorkflowInstanceId).ToHashSet(); |
| | | 54 | | |
| | 0 | 55 | | if (workflowInstanceFilterIsEmpty) |
| | 0 | 56 | | workflowInstanceIds = matchingWorkflowInstanceIds; |
| | | 57 | | else |
| | 0 | 58 | | workflowInstanceIds.IntersectWith(matchingWorkflowInstanceIds); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | // Alterations must apply only to running workflows. |
| | 0 | 62 | | workflowInstanceIds = (await FilterRunningWorkflowInstancesAsync(workflowInstanceIds, cancellationToken)).ToHash |
| | | 63 | | |
| | 0 | 64 | | return workflowInstanceIds; |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | private async Task<IEnumerable<string>> FilterRunningWorkflowInstancesAsync(IEnumerable<string> workflowInstanceIds, |
| | | 68 | | { |
| | 0 | 69 | | var filter = new WorkflowInstanceFilter |
| | 0 | 70 | | { |
| | 0 | 71 | | Ids = workflowInstanceIds.ToList(), |
| | 0 | 72 | | WorkflowStatus = WorkflowStatus.Running |
| | 0 | 73 | | }; |
| | | 74 | | |
| | 0 | 75 | | return await workflowInstanceStore.FindManyIdsAsync(filter, cancellationToken); |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | private bool WorkflowFilterIsEmpty(WorkflowInstanceFilter filter) |
| | | 79 | | { |
| | 0 | 80 | | return filter.Id == null && |
| | 0 | 81 | | filter.Ids == null && |
| | 0 | 82 | | filter.DefinitionId == null && |
| | 0 | 83 | | filter.DefinitionVersionId == null && |
| | 0 | 84 | | filter.DefinitionIds == null && |
| | 0 | 85 | | filter.DefinitionVersionIds == null && |
| | 0 | 86 | | filter.Version == null && |
| | 0 | 87 | | filter.CorrelationId == null && |
| | 0 | 88 | | filter.CorrelationIds == null && |
| | 0 | 89 | | filter.HasIncidents == null && |
| | 0 | 90 | | filter.TimestampFilters == null |
| | 0 | 91 | | && string.IsNullOrWhiteSpace(filter.SearchTerm); |
| | | 92 | | } |
| | | 93 | | } |