|
| 1 | +// ------------------------------------------------------------------------ |
| 2 | +// Copyright 2026 The Dapr Authors |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | +// ------------------------------------------------------------------------ |
| 13 | + |
| 14 | +namespace Dapr.Workflow; |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Linq; |
| 19 | + |
| 20 | +/// <summary> |
| 21 | +/// Contains the workflow history that was propagated from ancestor workflow instances. |
| 22 | +/// Each entry corresponds to a single ancestor's history. |
| 23 | +/// </summary> |
| 24 | +/// <remarks> |
| 25 | +/// A workflow receives propagated history when it is scheduled with a |
| 26 | +/// <see cref="HistoryPropagationScope"/> other than <see cref="HistoryPropagationScope.None"/>. |
| 27 | +/// Use <see cref="WorkflowContext.GetPropagatedHistory"/> to retrieve the propagated history |
| 28 | +/// inside a workflow implementation. |
| 29 | +/// </remarks> |
| 30 | +public sealed class PropagatedHistory |
| 31 | +{ |
| 32 | + private readonly IReadOnlyList<PropagatedHistoryEntry> _entries; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Initializes a new instance of <see cref="PropagatedHistory"/> with the given entries. |
| 36 | + /// </summary> |
| 37 | + /// <param name="entries">The propagated history entries from ancestor workflows.</param> |
| 38 | + public PropagatedHistory(IReadOnlyList<PropagatedHistoryEntry> entries) |
| 39 | + { |
| 40 | + _entries = entries ?? throw new ArgumentNullException(nameof(entries)); |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Gets the ordered list of propagated history entries. |
| 45 | + /// The first entry corresponds to the immediate parent workflow; subsequent entries |
| 46 | + /// correspond to progressively older ancestors when <see cref="HistoryPropagationScope.Lineage"/> is used. |
| 47 | + /// </summary> |
| 48 | + public IReadOnlyList<PropagatedHistoryEntry> Entries => _entries; |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Returns a new <see cref="PropagatedHistory"/> containing only entries from the specified App ID. |
| 52 | + /// </summary> |
| 53 | + /// <param name="appId">The Dapr App ID to filter by.</param> |
| 54 | + /// <returns>A filtered <see cref="PropagatedHistory"/> instance.</returns> |
| 55 | + public PropagatedHistory FilterByAppId(string appId) |
| 56 | + { |
| 57 | + ArgumentException.ThrowIfNullOrWhiteSpace(appId); |
| 58 | + return new PropagatedHistory( |
| 59 | + _entries.Where(e => string.Equals(e.AppId, appId, StringComparison.OrdinalIgnoreCase)).ToList()); |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Returns a new <see cref="PropagatedHistory"/> containing only the entry with the specified instance ID. |
| 64 | + /// </summary> |
| 65 | + /// <param name="instanceId">The workflow instance ID to filter by.</param> |
| 66 | + /// <returns>A filtered <see cref="PropagatedHistory"/> instance.</returns> |
| 67 | + public PropagatedHistory FilterByInstanceId(string instanceId) |
| 68 | + { |
| 69 | + ArgumentException.ThrowIfNullOrWhiteSpace(instanceId); |
| 70 | + return new PropagatedHistory( |
| 71 | + _entries.Where(e => string.Equals(e.InstanceId, instanceId, StringComparison.Ordinal)).ToList()); |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Returns a new <see cref="PropagatedHistory"/> containing only entries for the specified workflow name. |
| 76 | + /// </summary> |
| 77 | + /// <param name="workflowName">The workflow name to filter by.</param> |
| 78 | + /// <returns>A filtered <see cref="PropagatedHistory"/> instance.</returns> |
| 79 | + public PropagatedHistory FilterByWorkflowName(string workflowName) |
| 80 | + { |
| 81 | + ArgumentException.ThrowIfNullOrWhiteSpace(workflowName); |
| 82 | + return new PropagatedHistory( |
| 83 | + _entries.Where(e => string.Equals(e.WorkflowName, workflowName, StringComparison.Ordinal)).ToList()); |
| 84 | + } |
| 85 | +} |
0 commit comments