| | | 1 | | using Elsa.Common.Entities; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using Elsa.Dashboard.Abstractions.Contracts; |
| | | 4 | | using Elsa.Dashboard.Abstractions.Models; |
| | | 5 | | using Elsa.Workflows.Management; |
| | | 6 | | using Elsa.Workflows.Management.Entities; |
| | | 7 | | using Elsa.Workflows.Management.Enums; |
| | | 8 | | using Elsa.Workflows.Management.Filters; |
| | | 9 | | using Elsa.Workflows.Management.Models; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Workflows.Runtime.Dashboard; |
| | | 12 | | |
| | 3 | 13 | | public class WorkflowDashboardContributor( |
| | 3 | 14 | | IWorkflowInstanceStore workflowInstanceStore, |
| | 3 | 15 | | IWorkflowRuntimeAdminService runtimeAdminService) : IDashboardContributor |
| | | 16 | | { |
| | 0 | 17 | | public string Id => "workflows"; |
| | | 18 | | |
| | 0 | 19 | | public int Order => 100; |
| | | 20 | | |
| | | 21 | | public async ValueTask<DashboardOverviewContribution?> GetOverviewAsync(DashboardContext context) |
| | | 22 | | { |
| | 0 | 23 | | return new() |
| | 0 | 24 | | { |
| | 0 | 25 | | Runtime = GetRuntimeStatus(), |
| | 0 | 26 | | WorkflowInstances = await GetWorkflowMetricsAsync(context.Range, context.IncludeSystem, context.Cancellation |
| | 0 | 27 | | }; |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | public async ValueTask<IReadOnlyCollection<DashboardFinding>> GetFindingsAsync(DashboardContext context) |
| | | 31 | | { |
| | 0 | 32 | | var runtime = GetRuntimeStatus(); |
| | 0 | 33 | | var workflowMetrics = await GetWorkflowMetricsAsync(context.Range, context.IncludeSystem, context.CancellationTo |
| | 0 | 34 | | var findings = new List<DashboardFinding>(); |
| | | 35 | | |
| | 0 | 36 | | if (runtime.Status == DashboardRuntimeStatusKeys.Paused) |
| | 0 | 37 | | findings.Add(Finding("runtime-paused", DashboardFindingSeverity.Warning, "Runtime is paused", "Runtime", "ru |
| | 0 | 38 | | else if (runtime.Status == DashboardRuntimeStatusKeys.Draining) |
| | 0 | 39 | | findings.Add(Finding("runtime-draining", DashboardFindingSeverity.Warning, "Runtime is draining", "Runtime", |
| | | 40 | | |
| | 0 | 41 | | if (runtime.FailedIngressSourceCount > 0) |
| | 0 | 42 | | findings.Add(Finding("ingress-source-failures", DashboardFindingSeverity.Warning, $"{runtime.FailedIngressSo |
| | | 43 | | |
| | 0 | 44 | | if (workflowMetrics.Faulted > 0) |
| | 0 | 45 | | findings.Add(Finding("workflow-faults", DashboardFindingSeverity.Error, $"{workflowMetrics.Faulted} workflow |
| | | 46 | | |
| | 0 | 47 | | if (workflowMetrics.Interrupted > 0) |
| | 0 | 48 | | findings.Add(Finding("workflow-interrupted", DashboardFindingSeverity.Warning, $"{workflowMetrics.Interrupte |
| | | 49 | | |
| | 0 | 50 | | if (workflowMetrics.IncidentBearing > 0) |
| | 0 | 51 | | findings.Add(Finding("workflow-incidents", DashboardFindingSeverity.Error, $"{workflowMetrics.IncidentBearin |
| | | 52 | | |
| | 0 | 53 | | return findings; |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | public async ValueTask<DashboardTrendResponse?> GetWorkflowTrendsAsync(DashboardTrendContext context) |
| | | 57 | | { |
| | 0 | 58 | | var bucketSize = GetBucketSize(context.Granularity); |
| | 0 | 59 | | var buckets = new List<DashboardTrendBucket>(); |
| | | 60 | | |
| | 0 | 61 | | for (var bucketFrom = context.Range.From; bucketFrom < context.Range.To; bucketFrom = bucketFrom.Add(bucketSize) |
| | | 62 | | { |
| | 0 | 63 | | var bucketTo = Min(bucketFrom.Add(bucketSize), context.Range.To); |
| | 0 | 64 | | buckets.Add(new() |
| | 0 | 65 | | { |
| | 0 | 66 | | From = bucketFrom, |
| | 0 | 67 | | To = bucketTo, |
| | 0 | 68 | | CreatedOrStarted = await CountAsync(context.IncludeSystem, nameof(WorkflowInstance.CreatedAt), bucketFro |
| | 0 | 69 | | Finished = await CountAsync(context.IncludeSystem, nameof(WorkflowInstance.FinishedAt), bucketFrom, buck |
| | 0 | 70 | | Faulted = await CountAsync(context.IncludeSystem, nameof(WorkflowInstance.UpdatedAt), bucketFrom, bucket |
| | 0 | 71 | | Suspended = await CountAsync(context.IncludeSystem, nameof(WorkflowInstance.UpdatedAt), bucketFrom, buck |
| | 0 | 72 | | IncidentBearing = await CountAsync(context.IncludeSystem, nameof(WorkflowInstance.UpdatedAt), bucketFrom |
| | 0 | 73 | | }); |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | return new() |
| | 0 | 77 | | { |
| | 0 | 78 | | Buckets = buckets, |
| | 0 | 79 | | AppliedRange = context.Range.Key, |
| | 0 | 80 | | Granularity = context.Granularity, |
| | 0 | 81 | | From = context.Range.From, |
| | 0 | 82 | | To = context.Range.To |
| | 0 | 83 | | }; |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | public async ValueTask<DashboardRecentActivityResponse?> GetRecentActivityAsync(DashboardListContext context) |
| | | 87 | | { |
| | 0 | 88 | | var filter = CreateRangeFilter(context.IncludeSystem, nameof(WorkflowInstance.UpdatedAt), context.Range.From, co |
| | 0 | 89 | | var order = new WorkflowInstanceOrder<DateTimeOffset?> |
| | 0 | 90 | | { |
| | 0 | 91 | | KeySelector = x => x.UpdatedAt, |
| | 0 | 92 | | Direction = OrderDirection.Descending |
| | 0 | 93 | | }; |
| | 0 | 94 | | var page = await workflowInstanceStore.SummarizeManyAsync(filter, PageArgs.FromPage(0, context.Take), order, con |
| | | 95 | | |
| | 0 | 96 | | return new() |
| | 0 | 97 | | { |
| | 0 | 98 | | Items = page.Items.Select(MapRecentActivity).ToList(), |
| | 0 | 99 | | AppliedRange = context.Range.Key, |
| | 0 | 100 | | From = context.Range.From, |
| | 0 | 101 | | To = context.Range.To |
| | 0 | 102 | | }; |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | public async ValueTask<DashboardWorkflowHotspotsResponse?> GetWorkflowHotspotsAsync(DashboardHotspotsContext context |
| | | 106 | | { |
| | 0 | 107 | | var summaries = await workflowInstanceStore.SummarizeManyAsync(CreateRangeFilter(context.IncludeSystem, nameof(W |
| | 0 | 108 | | var hotspots = summaries |
| | 0 | 109 | | .GroupBy(x => x.DefinitionId) |
| | 0 | 110 | | .Select(x => CreateHotspot(x, context.Metric)) |
| | 0 | 111 | | .OrderByDescending(x => x.Value) |
| | 0 | 112 | | .ThenBy(x => x.WorkflowName) |
| | 0 | 113 | | .Take(context.Take) |
| | 0 | 114 | | .ToList(); |
| | | 115 | | |
| | 0 | 116 | | return new() |
| | 0 | 117 | | { |
| | 0 | 118 | | Items = hotspots, |
| | 0 | 119 | | AppliedRange = context.Range.Key, |
| | 0 | 120 | | Metric = context.Metric, |
| | 0 | 121 | | From = context.Range.From, |
| | 0 | 122 | | To = context.Range.To |
| | 0 | 123 | | }; |
| | 0 | 124 | | } |
| | | 125 | | |
| | | 126 | | private async Task<DashboardWorkflowInstanceMetrics> GetWorkflowMetricsAsync(DashboardRange range, bool includeSyste |
| | | 127 | | { |
| | 0 | 128 | | var completedSummaries = (await workflowInstanceStore.SummarizeManyAsync( |
| | 0 | 129 | | CreateRangeFilter(includeSystem, nameof(WorkflowInstance.FinishedAt), range.From, range.To, subStatus: Workf |
| | 0 | 130 | | cancellationToken)).ToList(); |
| | 0 | 131 | | var durations = completedSummaries |
| | 0 | 132 | | .Where(x => x.FinishedAt != null) |
| | 0 | 133 | | .Select(x => x.FinishedAt!.Value - x.CreatedAt) |
| | 0 | 134 | | .Where(x => x >= TimeSpan.Zero) |
| | 0 | 135 | | .ToList(); |
| | | 136 | | |
| | 0 | 137 | | return new() |
| | 0 | 138 | | { |
| | 0 | 139 | | Running = await CountAsync(includeSystem, status: WorkflowStatus.Running, cancellationToken: cancellationTok |
| | 0 | 140 | | Completed = completedSummaries.Count, |
| | 0 | 141 | | Faulted = await CountAsync(includeSystem, nameof(WorkflowInstance.UpdatedAt), range.From, range.To, cancella |
| | 0 | 142 | | Suspended = await CountAsync(includeSystem, subStatus: WorkflowSubStatus.Suspended, cancellationToken: cance |
| | 0 | 143 | | Interrupted = await CountAsync(includeSystem, nameof(WorkflowInstance.UpdatedAt), range.From, range.To, canc |
| | 0 | 144 | | IncidentBearing = await CountAsync(includeSystem, hasIncidents: true, cancellationToken: cancellationToken), |
| | 0 | 145 | | AverageDuration = durations.Count == 0 ? null : TimeSpan.FromTicks(Convert.ToInt64(durations.Average(x => x. |
| | 0 | 146 | | }; |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | private DashboardRuntimeStatus GetRuntimeStatus() |
| | | 150 | | { |
| | 0 | 151 | | var status = runtimeAdminService.GetStatus(); |
| | 0 | 152 | | var state = status.State; |
| | 0 | 153 | | var runtimeStatus = state.IsAcceptingNewWork |
| | 0 | 154 | | ? DashboardRuntimeStatusKeys.AcceptingWork |
| | 0 | 155 | | : state.DrainStartedAt != null |
| | 0 | 156 | | ? DashboardRuntimeStatusKeys.Draining |
| | 0 | 157 | | : DashboardRuntimeStatusKeys.Paused; |
| | 0 | 158 | | var failedSourceCount = status.Sources.Count(x => x.LastError != null); |
| | | 159 | | |
| | 0 | 160 | | return new() |
| | 0 | 161 | | { |
| | 0 | 162 | | Status = runtimeStatus, |
| | 0 | 163 | | IsAcceptingWork = state.IsAcceptingNewWork, |
| | 0 | 164 | | ActiveExecutionCycleCount = status.ActiveExecutionCycleCount, |
| | 0 | 165 | | IngressSourceCount = status.Sources.Count, |
| | 0 | 166 | | FailedIngressSourceCount = failedSourceCount, |
| | 0 | 167 | | PausedAt = state.PausedAt, |
| | 0 | 168 | | DrainStartedAt = state.DrainStartedAt, |
| | 0 | 169 | | Reason = state.Reason.ToString() |
| | 0 | 170 | | }; |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | private async Task<long> CountAsync( |
| | | 174 | | bool includeSystem, |
| | | 175 | | string? timestampColumn = null, |
| | | 176 | | DateTimeOffset? from = null, |
| | | 177 | | DateTimeOffset? to = null, |
| | | 178 | | CancellationToken cancellationToken = default, |
| | | 179 | | WorkflowStatus? status = null, |
| | | 180 | | WorkflowSubStatus? subStatus = null, |
| | | 181 | | bool? hasIncidents = null) |
| | | 182 | | { |
| | 0 | 183 | | return await workflowInstanceStore.CountAsync(CreateRangeFilter(includeSystem, timestampColumn, from, to, status |
| | 0 | 184 | | } |
| | | 185 | | |
| | | 186 | | private static WorkflowInstanceFilter CreateRangeFilter( |
| | | 187 | | bool includeSystem, |
| | | 188 | | string? timestampColumn, |
| | | 189 | | DateTimeOffset? from, |
| | | 190 | | DateTimeOffset? to, |
| | | 191 | | WorkflowStatus? status = null, |
| | | 192 | | WorkflowSubStatus? subStatus = null, |
| | | 193 | | bool? hasIncidents = null) |
| | | 194 | | { |
| | 0 | 195 | | var timestampFilters = new List<TimestampFilter>(); |
| | 0 | 196 | | if (timestampColumn != null && from != null) |
| | 0 | 197 | | timestampFilters.Add(new() { Column = timestampColumn, Operator = TimestampFilterOperator.GreaterThanOrEqual |
| | 0 | 198 | | if (timestampColumn != null && to != null) |
| | 0 | 199 | | timestampFilters.Add(new() { Column = timestampColumn, Operator = TimestampFilterOperator.LessThan, Timestam |
| | | 200 | | |
| | 0 | 201 | | return new() |
| | 0 | 202 | | { |
| | 0 | 203 | | IsSystem = includeSystem ? null : false, |
| | 0 | 204 | | WorkflowStatus = status, |
| | 0 | 205 | | WorkflowSubStatus = subStatus, |
| | 0 | 206 | | HasIncidents = hasIncidents, |
| | 0 | 207 | | TimestampFilters = timestampFilters.Count == 0 ? null : timestampFilters |
| | 0 | 208 | | }; |
| | | 209 | | } |
| | | 210 | | |
| | 0 | 211 | | private static DashboardRecentActivityItem MapRecentActivity(WorkflowInstanceSummary summary) => new() |
| | 0 | 212 | | { |
| | 0 | 213 | | InstanceId = summary.Id, |
| | 0 | 214 | | DefinitionId = summary.DefinitionId, |
| | 0 | 215 | | WorkflowName = summary.Name, |
| | 0 | 216 | | Status = summary.Status.ToString(), |
| | 0 | 217 | | SubStatus = summary.SubStatus.ToString(), |
| | 0 | 218 | | IncidentCount = summary.IncidentCount, |
| | 0 | 219 | | Duration = summary.FinishedAt == null ? null : summary.FinishedAt.Value - summary.CreatedAt, |
| | 0 | 220 | | CreatedAt = summary.CreatedAt, |
| | 0 | 221 | | UpdatedAt = summary.UpdatedAt, |
| | 0 | 222 | | FinishedAt = summary.FinishedAt |
| | 0 | 223 | | }; |
| | | 224 | | |
| | | 225 | | private static DashboardHotspot CreateHotspot(IGrouping<string, WorkflowInstanceSummary> group, string metric) |
| | | 226 | | { |
| | 0 | 227 | | var items = group.ToList(); |
| | 0 | 228 | | var durations = items |
| | 0 | 229 | | .Where(x => x.FinishedAt != null) |
| | 0 | 230 | | .Select(x => x.FinishedAt!.Value - x.CreatedAt) |
| | 0 | 231 | | .Where(x => x >= TimeSpan.Zero) |
| | 0 | 232 | | .ToList(); |
| | 0 | 233 | | var value = metric switch |
| | 0 | 234 | | { |
| | 0 | 235 | | DashboardHotspotMetric.Executions => items.Count, |
| | 0 | 236 | | DashboardHotspotMetric.Incidents => items.Sum(x => x.IncidentCount), |
| | 0 | 237 | | DashboardHotspotMetric.Duration => durations.Count == 0 ? 0 : Convert.ToInt64(durations.Average(x => x.Total |
| | 0 | 238 | | _ => items.LongCount(x => x.SubStatus == WorkflowSubStatus.Faulted) |
| | 0 | 239 | | }; |
| | | 240 | | |
| | 0 | 241 | | return new() |
| | 0 | 242 | | { |
| | 0 | 243 | | DefinitionId = group.Key, |
| | 0 | 244 | | WorkflowName = items.Select(x => x.Name).FirstOrDefault(x => !string.IsNullOrWhiteSpace(x)), |
| | 0 | 245 | | Value = value, |
| | 0 | 246 | | AverageDuration = durations.Count == 0 ? null : TimeSpan.FromTicks(Convert.ToInt64(durations.Average(x => x. |
| | 0 | 247 | | }; |
| | | 248 | | } |
| | | 249 | | |
| | | 250 | | private static TimeSpan GetBucketSize(string granularity) => |
| | 0 | 251 | | granularity.Equals(DashboardTrendGranularity.Minute, StringComparison.OrdinalIgnoreCase) |
| | 0 | 252 | | ? TimeSpan.FromMinutes(1) |
| | 0 | 253 | | : granularity.Equals(DashboardTrendGranularity.Day, StringComparison.OrdinalIgnoreCase) |
| | 0 | 254 | | ? TimeSpan.FromDays(1) |
| | 0 | 255 | | : TimeSpan.FromHours(1); |
| | | 256 | | |
| | 0 | 257 | | private static DashboardFinding Finding(string id, string severity, string message, string? targetKind, string? targ |
| | 0 | 258 | | { |
| | 0 | 259 | | Id = id, |
| | 0 | 260 | | Severity = severity, |
| | 0 | 261 | | Message = message, |
| | 0 | 262 | | TargetKind = targetKind, |
| | 0 | 263 | | Target = target, |
| | 0 | 264 | | Priority = priority |
| | 0 | 265 | | }; |
| | | 266 | | |
| | 0 | 267 | | private static DateTimeOffset Min(DateTimeOffset left, DateTimeOffset right) => left <= right ? left : right; |
| | | 268 | | } |