< Summary

Information
Class: Elsa.Alterations.Core.Services.WorkflowInstanceFinder
Assembly: Elsa.Alterations.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Alterations.Core/Services/WorkflowInstanceFinder.cs
Line coverage
1%
Covered lines: 1
Uncovered lines: 57
Coverable lines: 58
Total lines: 93
Line coverage: 1.7%
Branch coverage
0%
Covered branches: 0
Total branches: 44
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%
FindAsync()0%702260%
FilterRunningWorkflowInstancesAsync()100%210%
WorkflowFilterIsEmpty(...)0%506220%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Alterations.Core/Services/WorkflowInstanceFinder.cs

#LineLine coverage
 1using Elsa.Alterations.Core.Contracts;
 2using Elsa.Alterations.Core.Models;
 3using Elsa.Workflows;
 4using Elsa.Workflows.Management;
 5using Elsa.Workflows.Management.Filters;
 6using Elsa.Workflows.Runtime;
 7using Elsa.Workflows.Runtime.Filters;
 8
 9namespace Elsa.Alterations.Core.Services;
 10
 11/// <inheritdoc />
 112public class WorkflowInstanceFinder(IWorkflowInstanceStore workflowInstanceStore, IActivityExecutionStore activityExecut
 13{
 14    /// <inheritdoc />
 15    public async Task<IEnumerable<string>> FindAsync(AlterationWorkflowInstanceFilter filter, CancellationToken cancella
 16    {
 017        var workflowInstanceFilter = new WorkflowInstanceFilter
 018        {
 019            Ids = filter.WorkflowInstanceIds?.ToList(),
 020            DefinitionIds = filter.DefinitionIds,
 021            DefinitionVersionIds = filter.DefinitionVersionIds?.ToList(),
 022            CorrelationIds = filter.CorrelationIds?.ToList(),
 023            HasIncidents = filter.HasIncidents,
 024            IsSystem = filter.IsSystem,
 025            TimestampFilters = filter.TimestampFilters?.ToList(),
 026            WorkflowStatuses = filter.Statuses?.ToList(),
 027            WorkflowSubStatuses = filter.SubStatuses?.ToList(),
 028            Names = filter.Names?.ToList(),
 029            SearchTerm = filter.SearchTerm,
 030        };
 031        var activityExecutionFilters = filter.ActivityFilters?.Select(x => new ActivityExecutionRecordFilter
 032        {
 033            ActivityId = x.ActivityId,
 034            Id = x.ActivityInstanceId,
 035            ActivityNodeId = x.NodeId,
 036            Name = x.Name,
 037            Status = x.Status,
 038        }).ToList();
 39
 040        var emptyFilterSelectsAll = filter.EmptyFilterSelectsAll;
 041        var workflowInstanceFilterIsEmpty = WorkflowFilterIsEmpty(workflowInstanceFilter);
 42
 043        var workflowInstanceIds = workflowInstanceFilterIsEmpty && !emptyFilterSelectsAll
 044            ? Enumerable.Empty<string>().ToHashSet()
 045            : (await workflowInstanceStore.FindManyIdsAsync(workflowInstanceFilter, cancellationToken)).ToHashSet();
 46
 047        if (activityExecutionFilters == null)
 048            return workflowInstanceIds;
 49
 050        foreach (ActivityExecutionRecordFilter activityExecutionFilter in activityExecutionFilters.Where(x => !x.IsEmpty
 51        {
 052            var activityExecutionRecords = await activityExecutionStore.FindManySummariesAsync(activityExecutionFilter, 
 053            var matchingWorkflowInstanceIds = activityExecutionRecords.Select(x => x.WorkflowInstanceId).ToHashSet();
 54
 055            if (workflowInstanceFilterIsEmpty)
 056                workflowInstanceIds = matchingWorkflowInstanceIds;
 57            else
 058                workflowInstanceIds.IntersectWith(matchingWorkflowInstanceIds);
 59        }
 60
 61        // Alterations must apply only to running workflows.
 062        workflowInstanceIds = (await FilterRunningWorkflowInstancesAsync(workflowInstanceIds, cancellationToken)).ToHash
 63
 064        return workflowInstanceIds;
 065    }
 66
 67    private async Task<IEnumerable<string>> FilterRunningWorkflowInstancesAsync(IEnumerable<string> workflowInstanceIds,
 68    {
 069        var filter = new WorkflowInstanceFilter
 070        {
 071            Ids = workflowInstanceIds.ToList(),
 072            WorkflowStatus = WorkflowStatus.Running
 073        };
 74
 075        return await workflowInstanceStore.FindManyIdsAsync(filter, cancellationToken);
 076    }
 77
 78    private bool WorkflowFilterIsEmpty(WorkflowInstanceFilter filter)
 79    {
 080        return filter.Id == null &&
 081               filter.Ids == null &&
 082               filter.DefinitionId == null &&
 083               filter.DefinitionVersionId == null &&
 084               filter.DefinitionIds == null &&
 085               filter.DefinitionVersionIds == null &&
 086               filter.Version == null &&
 087               filter.CorrelationId == null &&
 088               filter.CorrelationIds == null &&
 089               filter.HasIncidents == null &&
 090               filter.TimestampFilters == null
 091               && string.IsNullOrWhiteSpace(filter.SearchTerm);
 92    }
 93}