| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Workflows.Runtime; |
| | | 3 | | using Elsa.Workflows.Runtime.Filters; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Workflows.Api.Endpoints.ActivityExecutions.GetCallStack; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Gets the call stack (execution chain) for a given activity execution. |
| | | 10 | | /// Returns activity execution records from root to the specified activity, ordered by call stack depth. |
| | | 11 | | /// Supports pagination for deep call stacks. |
| | | 12 | | /// </summary> |
| | | 13 | | [PublicAPI] |
| | 3 | 14 | | internal class Endpoint(IActivityExecutionStore store) : ElsaEndpoint<Request, Response> |
| | | 15 | | { |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public override void Configure() |
| | | 18 | | { |
| | 3 | 19 | | Get("/activity-executions/{id}/call-stack"); |
| | 3 | 20 | | ConfigurePermissions("read:activity-execution"); |
| | 3 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 25 | | { |
| | 0 | 26 | | var id = Route<string>("id")!; |
| | 0 | 27 | | var includeCrossWorkflowChain = request.IncludeCrossWorkflowChain ?? true; |
| | | 28 | | |
| | | 29 | | // Apply defaulting and upper bound to the requested page size to prevent excessive queries. |
| | | 30 | | const int defaultTake = 100; |
| | | 31 | | const int maxTake = 1000; |
| | | 32 | | |
| | 0 | 33 | | var skip = request.Skip; |
| | 0 | 34 | | var take = request.Take ?? defaultTake; |
| | | 35 | | |
| | 0 | 36 | | if (take <= 0) |
| | 0 | 37 | | take = defaultTake; |
| | | 38 | | |
| | 0 | 39 | | if (take > maxTake) |
| | 0 | 40 | | take = maxTake; |
| | | 41 | | |
| | | 42 | | // First, check if the activity execution exists. |
| | 0 | 43 | | var idFilter = new ActivityExecutionRecordFilter |
| | 0 | 44 | | { |
| | 0 | 45 | | Id = id |
| | 0 | 46 | | }; |
| | 0 | 47 | | var activityExecution = await store.FindAsync(idFilter, cancellationToken); |
| | 0 | 48 | | if (activityExecution == null) |
| | | 49 | | { |
| | 0 | 50 | | await Send.NotFoundAsync(cancellationToken); |
| | 0 | 51 | | return; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | // Then, get the execution chain. An empty chain should result in 200 with an empty items array. |
| | 0 | 55 | | var result = await store.GetExecutionChainAsync(id, includeCrossWorkflowChain, skip, take, cancellationToken); |
| | 0 | 56 | | var response = new Response |
| | 0 | 57 | | { |
| | 0 | 58 | | ActivityExecutionId = id, |
| | 0 | 59 | | Items = result.Items.ToList(), |
| | 0 | 60 | | TotalCount = result.TotalCount |
| | 0 | 61 | | }; |
| | | 62 | | |
| | 0 | 63 | | await Send.OkAsync(response, cancellationToken); |
| | 0 | 64 | | } |
| | | 65 | | } |