< Summary

Information
Class: Elsa.Workflows.Api.RealTime.Handlers.BroadcastWorkflowProgress
Assembly: Elsa.Workflows.Api
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/RealTime/Handlers/BroadcastWorkflowProgress.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 36
Coverable lines: 36
Total lines: 92
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
HandleAsync()100%210%
HandleAsync()100%210%
HandleAsync()100%210%
HandleAsync()100%210%
HandleAsync()100%210%
BroadcastActivityExecutionLogUpdatedAsync()100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/RealTime/Handlers/BroadcastWorkflowProgress.cs

#LineLine coverage
 1using Elsa.Mediator.Contracts;
 2using Elsa.Workflows.Api.RealTime.Contracts;
 3using Elsa.Workflows.Api.RealTime.Hubs;
 4using Elsa.Workflows.Api.RealTime.Messages;
 5using Elsa.Workflows.Management.Notifications;
 6using Elsa.Workflows.Runtime;
 7using Elsa.Workflows.Runtime.Notifications;
 8using Microsoft.AspNetCore.SignalR;
 9
 10namespace Elsa.Workflows.Api.RealTime.Handlers;
 11
 12/// <summary>
 13/// Broadcasts workflow progress to clients.
 14/// </summary>
 15public class BroadcastWorkflowProgress :
 16    INotificationHandler<ActivityExecutionLogUpdated>,
 17    INotificationHandler<ActivityExecutionRecordDeleted>,
 18    INotificationHandler<ActivityExecutionRecordUpdated>,
 19    INotificationHandler<WorkflowExecutionLogUpdated>,
 20    INotificationHandler<WorkflowInstanceSaved>
 21{
 22    private readonly IHubContext<WorkflowInstanceHub, IWorkflowInstanceClient> _hubContext;
 23    private readonly IActivityExecutionStatsService _activityExecutionStatsService;
 24
 25    /// <summary>
 26    /// Initializes a new instance of the <see cref="BroadcastWorkflowProgress"/> class.
 27    /// </summary>
 028    public BroadcastWorkflowProgress(IHubContext<WorkflowInstanceHub, IWorkflowInstanceClient> hubContext, IActivityExec
 29    {
 030        _hubContext = hubContext;
 031        _activityExecutionStatsService = activityExecutionStatsService;
 032    }
 33
 34    /// <inheritdoc />
 35    public async Task HandleAsync(ActivityExecutionLogUpdated notification, CancellationToken cancellationToken)
 36    {
 037        var workflowInstanceId = notification.WorkflowExecutionContext.Id;
 038        var activityIds = notification.Records.Select(x => x.ActivityNodeId).Distinct().ToList();
 039        var stats = (await _activityExecutionStatsService.GetStatsAsync(workflowInstanceId, activityIds, cancellationTok
 040        var clients = _hubContext.Clients.Group(workflowInstanceId);
 041        var message = new ActivityExecutionLogUpdatedMessage(stats);
 42
 043        await clients.ActivityExecutionLogUpdatedAsync(message, cancellationToken);
 044    }
 45
 46    /// <inheritdoc />
 47    public async Task HandleAsync(ActivityExecutionRecordDeleted notification, CancellationToken cancellationToken)
 48    {
 049        var record = notification.Record;
 050        var workflowInstanceId = record.WorkflowInstanceId;
 051        var activityId = record.ActivityId;
 052        await BroadcastActivityExecutionLogUpdatedAsync(workflowInstanceId, activityId, cancellationToken);
 053    }
 54
 55    /// <inheritdoc />
 56    public async Task HandleAsync(ActivityExecutionRecordUpdated notification, CancellationToken cancellationToken)
 57    {
 058        var record = notification.Record;
 059        var workflowInstanceId = record.WorkflowInstanceId;
 060        var activityId = record.ActivityId;
 061        await BroadcastActivityExecutionLogUpdatedAsync(workflowInstanceId, activityId, cancellationToken);
 062    }
 63
 64    /// <inheritdoc />
 65    public async Task HandleAsync(WorkflowExecutionLogUpdated notification, CancellationToken cancellationToken)
 66    {
 067        var workflowInstanceId = notification.WorkflowExecutionContext.Id;
 068        var clients = _hubContext.Clients.Group(workflowInstanceId);
 069        var message = new WorkflowExecutionLogUpdatedMessage();
 70
 071        await clients.WorkflowExecutionLogUpdatedAsync(message, cancellationToken);
 072    }
 73
 74    /// <inheritdoc />
 75    public async Task HandleAsync(WorkflowInstanceSaved notification, CancellationToken cancellationToken)
 76    {
 077        var workflowInstanceId = notification.WorkflowInstance.Id;
 078        var clients = _hubContext.Clients.Group(workflowInstanceId);
 079        var message = new WorkflowInstanceUpdatedMessage(workflowInstanceId);
 80
 081        await clients.WorkflowInstanceUpdatedAsync(message, cancellationToken);
 082    }
 83
 84    private async Task BroadcastActivityExecutionLogUpdatedAsync(string workflowInstanceId, string activityId, Cancellat
 85    {
 086        var stats = await _activityExecutionStatsService.GetStatsAsync(workflowInstanceId, activityId, cancellationToken
 087        var clients = _hubContext.Clients.Group(workflowInstanceId);
 088        var message = new ActivityExecutionLogUpdatedMessage(new[] { stats });
 89
 090        await clients.ActivityExecutionLogUpdatedAsync(message, cancellationToken);
 091    }
 92}