< Summary

Information
Class: Elsa.Workflows.Management.Services.DefaultWorkflowReferenceQuery
Assembly: Elsa.Workflows.Management
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Services/DefaultWorkflowReferenceQuery.cs
Line coverage
100%
Covered lines: 33
Uncovered lines: 0
Coverable lines: 33
Total lines: 76
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ExecuteAsync()100%11100%
FindWorkflowsContainingUpdatedWorkflowDefinitionAsync()100%11100%
FindWorkflowActivityDefinitionActivityNodes()100%66100%
GetAllWorkflowGraphsAsync()100%44100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Services/DefaultWorkflowReferenceQuery.cs

#LineLine coverage
 1using Elsa.Common.Models;
 2using Elsa.Workflows.Management.Activities.WorkflowDefinitionActivity;
 3using Elsa.Workflows.Management.Filters;
 4using Elsa.Workflows.Models;
 5
 6namespace Elsa.Workflows.Management.Services;
 7
 8/// <summary>
 9/// Represents the default implementation of <see cref="IWorkflowReferenceQuery"/> that queries workflows
 10/// referencing a specific workflow definition.
 11/// </summary>
 12/// <remarks>
 13/// This class is designed to identify workflows that are dependent on a given workflow definition.
 14/// It leverages services such as <see cref="IWorkflowDefinitionService"/> and <see cref="IWorkflowDefinitionStore"/>
 15/// to locate, graph, and analyze workflows that include the specified workflow definition.
 16/// </remarks>
 32017public class DefaultWorkflowReferenceQuery(IWorkflowDefinitionService workflowDefinitionService, IWorkflowDefinitionStor
 18{
 19    public async Task<IEnumerable<string>> ExecuteAsync(string workflowDefinitionId, CancellationToken cancellationToken
 20    {
 321        var workflowGraphs = await FindWorkflowsContainingUpdatedWorkflowDefinitionAsync(workflowDefinitionId, cancellat
 422        return workflowGraphs.Select(x => x.Workflow.Identity.DefinitionId).Distinct();
 323    }
 24
 25    private async Task<IEnumerable<WorkflowGraph>> FindWorkflowsContainingUpdatedWorkflowDefinitionAsync(string updatedW
 26    {
 327        var allWorkflowGraphs = (await GetAllWorkflowGraphsAsync(cancellationToken)).ToList();
 328        var filteredWorkflowGraphs = allWorkflowGraphs
 24129            .Where(x => FindWorkflowActivityDefinitionActivityNodes(x.Root).Any(y => y.WorkflowDefinitionId == updatedWo
 330            .ToList();
 31
 332        return filteredWorkflowGraphs;
 333    }
 34
 35    private IEnumerable<WorkflowDefinitionActivity> FindWorkflowActivityDefinitionActivityNodes(ActivityNode parent)
 36    {
 277637        foreach (var child in parent.Children)
 38        {
 59539            if (child.Activity is WorkflowDefinitionActivity workflowDefinitionActivity)
 2140                yield return workflowDefinitionActivity;
 41            else
 42            {
 118943                foreach (var grandChild in FindWorkflowActivityDefinitionActivityNodes(child))
 2144                    yield return grandChild;
 45            }
 46        }
 79247    }
 48
 49    private async Task<IEnumerable<WorkflowGraph>> GetAllWorkflowGraphsAsync(CancellationToken cancellationToken)
 50    {
 351        var workflowDefinitionFilter = new WorkflowDefinitionFilter
 352        {
 353            VersionOptions = VersionOptions.LatestOrPublished,
 354            IsReadonly = false
 355        };
 356        var workflowDefinitionSummaries = await workflowDefinitionStore.FindSummariesAsync(workflowDefinitionFilter, can
 57
 58        // If there are workflow definition summaries with the same definition ID, only take the latest version.
 359        workflowDefinitionSummaries = workflowDefinitionSummaries
 22160            .GroupBy(x => x.DefinitionId)
 44161            .Select(x => x.OrderByDescending(y => y.Version).First())
 362            .ToList();
 63
 364        var workflowGraphs = new List<WorkflowGraph>();
 65
 44666        foreach (var workflowDefinitionSummary in workflowDefinitionSummaries)
 67        {
 22068            var workflowGraph = await workflowDefinitionService.FindWorkflowGraphAsync(workflowDefinitionSummary.Id, can
 69
 22070            if (workflowGraph != null)
 22071                workflowGraphs.Add(workflowGraph);
 72        }
 73
 374        return workflowGraphs;
 375    }
 76}