| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using Elsa.Workflows.Management.Entities; |
| | | 3 | | using Elsa.Workflows.Management.Enums; |
| | | 4 | | using Elsa.Workflows.Management.Models; |
| | | 5 | | using System.Linq.Dynamic.Core; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Management.Filters; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// A filter for querying workflow instances. |
| | | 11 | | /// </summary> |
| | | 12 | | public class WorkflowInstanceFilter |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Filter workflow instances by ID. |
| | | 16 | | /// </summary> |
| | 360 | 17 | | public string? Id { get; set; } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Filter workflow instances by IDs. |
| | | 21 | | /// </summary> |
| | 161 | 22 | | public ICollection<string>? Ids { get; set; } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Filter workflow instances that match the specified search term. |
| | | 26 | | /// </summary> |
| | 141 | 27 | | public string? SearchTerm { get; set; } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Filter workflow instances that match the specified name. |
| | | 31 | | /// </summary> |
| | 141 | 32 | | public string? Name { get; set; } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Filter workflow instances by definition ID. |
| | | 36 | | /// </summary> |
| | 173 | 37 | | public string? DefinitionId { get; set; } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Filter workflow instances by definition version ID. |
| | | 41 | | /// </summary> |
| | 141 | 42 | | public string? DefinitionVersionId { get; set; } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Filter workflow instances by definition IDs. |
| | | 46 | | /// </summary> |
| | 153 | 47 | | public ICollection<string>? DefinitionIds { get; set; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Filter workflow instances by definition version IDs. |
| | | 51 | | /// </summary> |
| | 141 | 52 | | public ICollection<string>? DefinitionVersionIds { get; set; } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Filter workflow instances by version. |
| | | 56 | | /// </summary> |
| | 141 | 57 | | public int? Version { get; set; } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Filter workflow instances by their parent instance IDs. |
| | | 61 | | /// </summary> |
| | 141 | 62 | | public ICollection<string>? ParentWorkflowInstanceIds { get; set; } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Filter workflow instances by correlation ID. |
| | | 66 | | /// </summary> |
| | 147 | 67 | | public string? CorrelationId { get; set; } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Filter workflow instances by correlation IDs. |
| | | 71 | | /// </summary> |
| | 141 | 72 | | public ICollection<string>? CorrelationIds { get; set; } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Filter workflow instances by status. |
| | | 76 | | /// </summary> |
| | 155 | 77 | | public WorkflowStatus? WorkflowStatus { get; set; } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Filter workflow instances by a set of statuses. |
| | | 81 | | /// </summary> |
| | 141 | 82 | | public ICollection<WorkflowStatus>? WorkflowStatuses { get; set; } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Filter workflow instances by sub-status. |
| | | 86 | | /// </summary> |
| | 143 | 87 | | public WorkflowSubStatus? WorkflowSubStatus { get; set; } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Filter workflow instances by a set of sub-status. |
| | | 91 | | /// </summary> |
| | 141 | 92 | | public ICollection<WorkflowSubStatus>? WorkflowSubStatuses { get; set; } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Filter workflow instances by whether they are executing. |
| | | 96 | | /// </summary> |
| | 147 | 97 | | public bool? IsExecuting { get; set; } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Filter workflow instances by whether they have incidents. |
| | | 101 | | /// </summary> |
| | 141 | 102 | | public bool? HasIncidents { get; set; } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Filter on workflows that are system workflows. |
| | | 106 | | /// </summary> |
| | 141 | 107 | | public bool? IsSystem { get; set; } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Filter workflow instances that are older than the specified timestamp. |
| | | 111 | | /// </summary> |
| | 147 | 112 | | public DateTimeOffset? BeforeLastUpdated { get; set; } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Filter workflow instances by timestamp. |
| | | 116 | | /// </summary> |
| | 141 | 117 | | public ICollection<TimestampFilter>? TimestampFilters { get; set; } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Filter workflow instances by name. |
| | | 121 | | /// </summary> |
| | 141 | 122 | | public List<string>? Names { get; set; } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Applies the filter to the specified query. |
| | | 126 | | /// </summary> |
| | | 127 | | [RequiresUnreferencedCode("The method uses reflection to create an expression tree.")] |
| | | 128 | | public IQueryable<WorkflowInstance> Apply(IQueryable<WorkflowInstance> query) |
| | | 129 | | { |
| | 141 | 130 | | var filter = this; |
| | | 131 | | |
| | 254 | 132 | | if (!string.IsNullOrWhiteSpace(filter.Id)) query = query.Where(x => x.Id == filter.Id); |
| | 151 | 133 | | if (filter.Ids != null) query = query.Where(x => filter.Ids.Contains(x.Id)); |
| | 156 | 134 | | if (!string.IsNullOrWhiteSpace(filter.DefinitionId)) query = query.Where(x => x.DefinitionId == filter.Definitio |
| | 141 | 135 | | if (!string.IsNullOrWhiteSpace(filter.DefinitionVersionId)) query = query.Where(x => x.DefinitionVersionId == fi |
| | 142 | 136 | | if (filter.DefinitionIds != null) query = query.Where(x => filter.DefinitionIds.Contains(x.DefinitionId)); |
| | 141 | 137 | | if (filter.DefinitionVersionIds != null) query = query.Where(x => filter.DefinitionVersionIds.Contains(x.Definit |
| | 141 | 138 | | if (filter.Version != null) query = query.Where(x => x.Version == filter.Version); |
| | 141 | 139 | | if (filter.ParentWorkflowInstanceIds != null) query = query.Where(x => x.ParentWorkflowInstanceId != null && fil |
| | 144 | 140 | | if (!string.IsNullOrWhiteSpace(filter.CorrelationId)) query = query.Where(x => x.CorrelationId == filter.Correla |
| | 141 | 141 | | if (filter.CorrelationIds != null) query = query.Where(x => filter.CorrelationIds.Contains(x.CorrelationId!)); |
| | 141 | 142 | | if (filter.Names != null) query = query.Where(x => filter.Names.Contains(x.Name!)); |
| | 151 | 143 | | if (filter.WorkflowStatus != null) query = query.Where(x => x.Status == filter.WorkflowStatus); |
| | 142 | 144 | | if (filter.WorkflowSubStatus != null) query = query.Where(x => x.SubStatus == filter.WorkflowSubStatus); |
| | 141 | 145 | | if (filter.WorkflowStatuses != null) query = query.Where(x => filter.WorkflowStatuses.Contains(x.Status)); |
| | 141 | 146 | | if (filter.WorkflowSubStatuses != null) query = query.Where(x => filter.WorkflowSubStatuses.Contains(x.SubStatus |
| | 143 | 147 | | if (filter.IsExecuting != null) query = query.Where(x => x.IsExecuting == filter.IsExecuting); |
| | 141 | 148 | | if (filter.HasIncidents != null) query = filter.HasIncidents == true ? query.Where(x => x.IncidentCount > 0) : q |
| | 141 | 149 | | if (filter.IsSystem != null) query = query.Where(x => x.IsSystem == filter.IsSystem); |
| | 141 | 150 | | if (filter.Name != null) query = query.Where(x => x.Name!.ToLower().Contains(filter.Name.ToLower())); |
| | 143 | 151 | | if (filter.BeforeLastUpdated != null) query = query.Where(x => x.UpdatedAt < filter.BeforeLastUpdated); |
| | | 152 | | |
| | 141 | 153 | | if (TimestampFilters != null) |
| | | 154 | | { |
| | 0 | 155 | | foreach (var timestampFilter in TimestampFilters) |
| | | 156 | | { |
| | 0 | 157 | | var column = timestampFilter.Column; |
| | 0 | 158 | | var timestamp = timestampFilter.Timestamp; |
| | 0 | 159 | | var isZeroTime = timestamp.TimeOfDay == TimeSpan.Zero; |
| | 0 | 160 | | var startDay = new DateTimeOffset(timestamp.Date); |
| | 0 | 161 | | var endDay = startDay.AddDays(1); |
| | | 162 | | |
| | 0 | 163 | | query = timestampFilter.Operator switch |
| | 0 | 164 | | { |
| | 0 | 165 | | TimestampFilterOperator.Is when isZeroTime => query.Where($"{column} >= @0 && {column} < @1", startD |
| | 0 | 166 | | TimestampFilterOperator.Is => query.Where($"{column} == @0", timestamp), |
| | 0 | 167 | | TimestampFilterOperator.IsNot when isZeroTime => query.Where($"{column} < @0 || {column} >= @1", sta |
| | 0 | 168 | | TimestampFilterOperator.IsNot => query.Where($"{column} != @0", timestamp), |
| | 0 | 169 | | TimestampFilterOperator.GreaterThan when isZeroTime => query.Where($"{column} > @0", endDay), |
| | 0 | 170 | | TimestampFilterOperator.GreaterThan => query.Where($"{column} > @0", timestamp), |
| | 0 | 171 | | TimestampFilterOperator.GreaterThanOrEqual when isZeroTime => query.Where($"{column} >= @0", startDa |
| | 0 | 172 | | TimestampFilterOperator.GreaterThanOrEqual => query.Where($"{column} >= @0", timestamp), |
| | 0 | 173 | | TimestampFilterOperator.LessThan when isZeroTime => query.Where($"{column} < @0", startDay), |
| | 0 | 174 | | TimestampFilterOperator.LessThan => query.Where($"{column} < @0", timestamp), |
| | 0 | 175 | | TimestampFilterOperator.LessThanOrEqual when isZeroTime => query.Where($"{column} <= @0", endDay), |
| | 0 | 176 | | TimestampFilterOperator.LessThanOrEqual => query.Where($"{column} <= @0", timestamp), |
| | 0 | 177 | | _ => query |
| | 0 | 178 | | }; |
| | | 179 | | } |
| | | 180 | | } |
| | | 181 | | |
| | 141 | 182 | | var searchTerm = filter.SearchTerm; |
| | 141 | 183 | | if (!string.IsNullOrWhiteSpace(searchTerm)) |
| | | 184 | | { |
| | 0 | 185 | | query = |
| | 0 | 186 | | from instance in query |
| | 0 | 187 | | where instance.Name!.ToLower().Contains(searchTerm.ToLower()) |
| | 0 | 188 | | || instance.DefinitionVersionId.Contains(searchTerm) |
| | 0 | 189 | | || instance.DefinitionId.Contains(searchTerm) |
| | 0 | 190 | | || instance.Id.Contains(searchTerm) |
| | 0 | 191 | | || instance.CorrelationId!.Contains(searchTerm) |
| | 0 | 192 | | select instance; |
| | | 193 | | } |
| | | 194 | | |
| | 141 | 195 | | return query; |
| | | 196 | | } |
| | | 197 | | } |