2023-07-25 13:12:27 +00:00
|
|
|
using Elsa.Common.Entities;
|
|
|
|
|
using Elsa.Workflows.Runtime.Contracts;
|
|
|
|
|
using Elsa.Workflows.Runtime.Filters;
|
|
|
|
|
using Elsa.Workflows.Runtime.Models;
|
|
|
|
|
using Elsa.Workflows.Runtime.OrderDefinitions;
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Workflows.Runtime.Services;
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2023-10-10 10:34:32 +00:00
|
|
|
public class ActivityExecutionStatsService : IActivityExecutionStatsService
|
2023-07-25 13:12:27 +00:00
|
|
|
{
|
|
|
|
|
private readonly IActivityExecutionStore _store;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-10-10 10:34:32 +00:00
|
|
|
/// Initializes a new instance of the <see cref="ActivityExecutionStatsService"/> class.
|
2023-07-25 13:12:27 +00:00
|
|
|
/// </summary>
|
2023-10-10 10:34:32 +00:00
|
|
|
public ActivityExecutionStatsService(IActivityExecutionStore store)
|
2023-07-25 13:12:27 +00:00
|
|
|
{
|
|
|
|
|
_store = store;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2023-10-25 20:46:06 +00:00
|
|
|
public async Task<IEnumerable<ActivityExecutionStats>> GetStatsAsync(string workflowInstanceId, IEnumerable<string> activityNodeIds, CancellationToken cancellationToken = default)
|
2023-07-25 13:12:27 +00:00
|
|
|
{
|
|
|
|
|
var filter = new ActivityExecutionRecordFilter
|
|
|
|
|
{
|
|
|
|
|
WorkflowInstanceId = workflowInstanceId,
|
2023-10-25 20:46:06 +00:00
|
|
|
ActivityNodeIds = activityNodeIds.ToList()
|
2023-07-25 13:12:27 +00:00
|
|
|
};
|
|
|
|
|
var order = new ActivityExecutionRecordOrder<DateTimeOffset>(x => x.StartedAt, OrderDirection.Ascending);
|
|
|
|
|
var records = (await _store.FindManyAsync(filter, order, cancellationToken)).ToList();
|
2023-10-25 20:46:06 +00:00
|
|
|
var groupedRecords = records.GroupBy(x => x.ActivityNodeId).ToList();
|
2023-07-25 13:12:27 +00:00
|
|
|
var stats = groupedRecords.Select(grouping => new ActivityExecutionStats
|
|
|
|
|
{
|
2023-10-25 20:46:06 +00:00
|
|
|
ActivityNodeId = grouping.Key,
|
|
|
|
|
ActivityId = grouping.First().ActivityId,
|
2023-07-25 13:12:27 +00:00
|
|
|
StartedCount = grouping.Count(),
|
|
|
|
|
CompletedCount = grouping.Count(x => x.CompletedAt != null),
|
|
|
|
|
UncompletedCount = grouping.Count(x => x.CompletedAt == null),
|
2023-08-21 20:45:56 +00:00
|
|
|
IsBlocked = grouping.Any(x => x.HasBookmarks),
|
|
|
|
|
IsFaulted = grouping.Any(x => x.Status == ActivityStatus.Faulted)
|
2023-07-25 13:12:27 +00:00
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
|
|
return stats;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2023-10-25 20:46:06 +00:00
|
|
|
public async Task<ActivityExecutionStats> GetStatsAsync(string workflowInstanceId, string activityNodeId, CancellationToken cancellationToken = default)
|
2023-07-25 13:12:27 +00:00
|
|
|
{
|
2023-10-25 20:46:06 +00:00
|
|
|
var stats = (await GetStatsAsync(workflowInstanceId, new[] { activityNodeId }, cancellationToken)).FirstOrDefault();
|
2023-07-25 13:12:27 +00:00
|
|
|
|
|
|
|
|
return stats ?? new ActivityExecutionStats
|
|
|
|
|
{
|
2023-10-25 20:46:06 +00:00
|
|
|
ActivityNodeId = activityNodeId
|
2023-07-25 13:12:27 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|