| | | 1 | | using Elsa.Common.Entities; |
| | | 2 | | using Elsa.Workflows.Runtime.Filters; |
| | | 3 | | using Elsa.Workflows.Runtime.OrderDefinitions; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Runtime; |
| | | 6 | | |
| | | 7 | | /// <inheritdoc /> |
| | | 8 | | public class ActivityExecutionStatsService : IActivityExecutionStatsService |
| | | 9 | | { |
| | | 10 | | private readonly IActivityExecutionStore _store; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Initializes a new instance of the <see cref="ActivityExecutionStatsService"/> class. |
| | | 14 | | /// </summary> |
| | 1 | 15 | | public ActivityExecutionStatsService(IActivityExecutionStore store) |
| | | 16 | | { |
| | 1 | 17 | | _store = store; |
| | 1 | 18 | | } |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | public async Task<IEnumerable<ActivityExecutionStats>> GetStatsAsync(string workflowInstanceId, IEnumerable<string> |
| | | 22 | | { |
| | 0 | 23 | | var filter = new ActivityExecutionRecordFilter |
| | 0 | 24 | | { |
| | 0 | 25 | | WorkflowInstanceId = workflowInstanceId, |
| | 0 | 26 | | ActivityNodeIds = activityNodeIds?.ToList() |
| | 0 | 27 | | }; |
| | 0 | 28 | | var order = new ActivityExecutionRecordOrder<DateTimeOffset>(x => x.StartedAt, OrderDirection.Ascending); |
| | 0 | 29 | | var records = (await _store.FindManySummariesAsync(filter, order, cancellationToken)).ToList(); |
| | 0 | 30 | | var groupedRecords = records.GroupBy(x => x.ActivityNodeId).ToList(); |
| | 0 | 31 | | var stats = groupedRecords.Select(grouping => |
| | 0 | 32 | | { |
| | 0 | 33 | | var first = grouping.First(); |
| | 0 | 34 | | var last = grouping.Last(); |
| | 0 | 35 | | return new ActivityExecutionStats |
| | 0 | 36 | | { |
| | 0 | 37 | | ActivityNodeId = grouping.Key, |
| | 0 | 38 | | ActivityId = first.ActivityId, |
| | 0 | 39 | | StartedCount = grouping.Count(), |
| | 0 | 40 | | CompletedCount = grouping.Count(x => x.CompletedAt != null), |
| | 0 | 41 | | UncompletedCount = grouping.Count(x => x.CompletedAt == null), |
| | 0 | 42 | | IsBlocked = grouping.Any(x => x.HasBookmarks), |
| | 0 | 43 | | IsFaulted = grouping.Any(x => x.Status == ActivityStatus.Faulted), |
| | 0 | 44 | | AggregateFaultCount = last.AggregateFaultCount, |
| | 0 | 45 | | Metadata = last.Metadata |
| | 0 | 46 | | }; |
| | 0 | 47 | | }).ToList(); |
| | | 48 | | |
| | 0 | 49 | | return stats; |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc /> |
| | | 53 | | public async Task<ActivityExecutionStats> GetStatsAsync(string workflowInstanceId, string activityNodeId, Cancellati |
| | | 54 | | { |
| | 0 | 55 | | var stats = (await GetStatsAsync(workflowInstanceId, [activityNodeId], cancellationToken)).FirstOrDefault(); |
| | | 56 | | |
| | 0 | 57 | | return stats ?? new ActivityExecutionStats |
| | 0 | 58 | | { |
| | 0 | 59 | | ActivityNodeId = activityNodeId |
| | 0 | 60 | | }; |
| | 0 | 61 | | } |
| | | 62 | | } |