< Summary

Information
Class: Elsa.Workflows.IdentityGraphService
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Services/IdentityGraphService.cs
Line coverage
100%
Covered lines: 51
Uncovered lines: 0
Coverable lines: 51
Total lines: 123
Line coverage: 100%
Branch coverage
100%
Covered branches: 30
Total branches: 30
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%
AssignIdentitiesAsync()100%11100%
AssignIdentitiesAsync()100%11100%
AssignIdentitiesAsync(...)100%11100%
AssignIdentitiesAsync()100%44100%
AssignInputOutputsAsync()100%1616100%
AssignVariables(...)100%44100%
CreateId(...)100%44100%
GetNextIndexFor(...)100%22100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Services/IdentityGraphService.cs

#LineLine coverage
 1using Elsa.Extensions;
 2using Elsa.Workflows.Activities;
 3using Elsa.Workflows.Models;
 4using Humanizer;
 5using Microsoft.Extensions.Logging;
 6
 7namespace Elsa.Workflows;
 8
 9/// <inheritdoc />
 85110public class IdentityGraphService(IActivityVisitor activityVisitor, IActivityRegistryLookupService activityRegistryLooku
 11{
 12    /// <inheritdoc />
 13    public async Task AssignIdentitiesAsync(Workflow workflow, CancellationToken cancellationToken = default)
 14    {
 64415        await AssignIdentitiesAsync((IActivity)workflow, cancellationToken);
 64416    }
 17
 18    /// <inheritdoc />
 19    public async Task AssignIdentitiesAsync(IActivity root, CancellationToken cancellationToken = default)
 20    {
 64421        var graph = await activityVisitor.VisitAsync(root, cancellationToken);
 64422        await AssignIdentitiesAsync(graph);
 64423    }
 24
 25    /// <inheritdoc />
 64426    public Task AssignIdentitiesAsync(ActivityNode root) => AssignIdentitiesAsync(root.Flatten().ToList());
 27
 28    /// <inheritdoc />
 29    public async Task AssignIdentitiesAsync(ICollection<ActivityNode> flattenedList)
 30    {
 365831        var identityCounters = new Dictionary<string, int>();
 32
 3887033        foreach (var node in flattenedList)
 34        {
 1577735            node.Activity.Id = CreateId(node, identityCounters, flattenedList);
 1577736            node.Activity.NodeId = node.NodeId;
 1577737            await AssignInputOutputsAsync(node.Activity);
 38
 1577739            if (node.Activity is IVariableContainer variableContainer)
 882440                AssignVariables(variableContainer);
 1577741        }
 365842    }
 43
 44    /// <inheritdoc />
 45    public async Task AssignInputOutputsAsync(IActivity activity)
 46    {
 1577747        var activityDescriptor = await activityRegistryLookup.FindAsync(activity.Type, activity.Version);
 48
 1577749        if (activityDescriptor == null!)
 50        {
 64251            logger.LogWarning("Activity descriptor not found for activity type {ActivityType}. Skipping identity assignm
 64252            return;
 53        }
 54
 1513555        var inputDictionary = activityDescriptor.GetWrappedInputProperties(activity);
 56
 5982057        foreach (var (inputName, input) in inputDictionary)
 58        {
 1477559            var blockReference = input?.MemoryBlockReference();
 60
 1477561            if (blockReference == null!)
 62                continue;
 63
 1006964            if (string.IsNullOrEmpty(blockReference.Id))
 616365                blockReference.Id = $"{activity.Id}:input-{inputName.Humanize().Kebaberize()}";
 66        }
 67
 1513568        var outputs = activity.GetOutputs();
 69
 3485470        foreach (var output in outputs)
 71        {
 229272            var blockReference = output.Value.MemoryBlockReference();
 73
 229274            if (blockReference == null!)
 75                continue;
 76
 229277            if (string.IsNullOrEmpty(blockReference.Id))
 108078                blockReference.Id = $"{activity.Id}:output-{output.Name.Humanize().Kebaberize()}";
 79        }
 1577780    }
 81
 82    /// <inheritdoc />
 83    public void AssignVariables(IVariableContainer activity)
 84    {
 882485        var variables = activity.Variables;
 882486        var seed = 0;
 87
 1874488        foreach (var variable in variables)
 54889            variable.Id = variable.Id != null! ? variable.Id : $"{activity.Id}:variable-{++seed}";
 882490    }
 91
 92    private string CreateId(ActivityNode activityNode, IDictionary<string, int> identityCounters, ICollection<ActivityNo
 93    {
 1577794        if (!string.IsNullOrWhiteSpace(activityNode.Activity.Id))
 915395            return activityNode.Activity.Id;
 96
 97        while (true)
 98        {
 734099            var fullTypeName = activityNode.Activity.Type;
 7340100            var shortTypeName = fullTypeName.Split('.').Last();
 7340101            var index = GetNextIndexFor(shortTypeName, identityCounters);
 7340102            var name = $"{shortTypeName}{index + 1}";
 103
 43064104            if (allNodes.All(x => x.Activity.Id != name))
 6624105                return name;
 106        }
 107    }
 108
 109    private int GetNextIndexFor(string activityType, IDictionary<string, int> identityCounters)
 110    {
 7340111        if (!identityCounters.TryGetValue(activityType, out var index))
 112        {
 5961113            identityCounters[activityType] = index;
 114        }
 115        else
 116        {
 1379117            index = identityCounters[activityType] + 1;
 1379118            identityCounters[activityType] = index;
 119        }
 120
 7340121        return index;
 122    }
 123}