-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from strem-app/feature/trigger-on-flow-start-o…
…r-finish Have added triggers for flow starting/ending
- Loading branch information
Showing
9 changed files
with
191 additions
and
6 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/Strem.Flows.Default/Components/Triggers/Utility/OnFlowFinishedTriggerComponent.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
@using Strem.Flows.Services.Stores | ||
@inherits Strem.Flows.Components.Triggers.CustomTriggerComponent<Strem.Flows.Default.Flows.Triggers.Utility.OnFlowFinishedTriggerData> | ||
|
||
@inject IFlowStore FlowStore; | ||
|
||
<div class="field"> | ||
<label class="label">Flow</label> | ||
<div class="control"> | ||
<DataSelectInput @bind-Value="Data.FlowId" Data="FlowStore.Data" GetName="x => x.Name" GetValue="x => x.Id" MapValue="x => Guid.Parse(x.ToString())"></DataSelectInput> | ||
</div> | ||
</div> | ||
<div class="field"> | ||
<div class="control"> | ||
<CheckBox @bind-Value="Data.OnlyOnSuccessfulExecution" Label="Only When Finished Successfully?"></CheckBox> | ||
</div> | ||
</div> | ||
|
||
@code { | ||
public override string Title => GetTitle(); | ||
|
||
public string GetTitle() | ||
{ | ||
var targetedFlow = FlowStore.Get(Data.FlowId); | ||
var successfulSuffix = Data.OnlyOnSuccessfulExecution ? "Successfully" : ""; | ||
return $"On <strong>{targetedFlow?.Name ?? "Flow"}</strong> Finished {successfulSuffix}"; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Strem.Flows.Default/Components/Triggers/Utility/OnFlowStartedTriggerComponent.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
@using Strem.Flows.Services.Stores | ||
@inherits Strem.Flows.Components.Triggers.CustomTriggerComponent<Strem.Flows.Default.Flows.Triggers.Utility.OnFlowStartedTriggerData> | ||
|
||
@inject IFlowStore FlowStore; | ||
|
||
<div class="field"> | ||
<label class="label">Flow</label> | ||
<div class="control"> | ||
<DataSelectInput @bind-Value="Data.FlowId" Data="FlowStore.Data" GetName="x => x.Name" GetValue="x => x.Id" MapValue="x => Guid.Parse(x.ToString())"></DataSelectInput> | ||
</div> | ||
</div> | ||
|
||
@code { | ||
public override string Title => GetTitle(); | ||
|
||
public string GetTitle() | ||
{ | ||
var targetedFlow = FlowStore.Get(Data.FlowId); | ||
return $"On <strong>{targetedFlow?.Name ?? "Flow"}</strong> Starting"; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Strem.Flows.Default/Flows/Triggers/Utility/OnFlowFinishedTrigger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Reactive.Linq; | ||
using Microsoft.Extensions.Logging; | ||
using Strem.Core.Events.Bus; | ||
using Strem.Core.Extensions; | ||
using Strem.Core.State; | ||
using Strem.Core.Variables; | ||
using Strem.Flows.Data.Triggers; | ||
using Strem.Flows.Events; | ||
using Strem.Flows.Processors; | ||
using Strem.Flows.Services.Stores; | ||
using Strem.Flows.Types; | ||
|
||
namespace Strem.Flows.Default.Flows.Triggers.Utility; | ||
|
||
public class OnFlowFinishedTrigger : FlowTrigger<OnFlowFinishedTriggerData> | ||
{ | ||
public override string Code => OnFlowFinishedTriggerData.TriggerCode; | ||
public override string Version => OnFlowFinishedTriggerData.TriggerVersion; | ||
|
||
public static VariableEntry FlowNameVariableEntry = new("flow.name"); | ||
|
||
public override string Name => "On Flow Finished"; | ||
public override string Category => "Utility"; | ||
public override string Description => "Triggers whenever the matching flow has finished executing"; | ||
public override VariableDescriptor[] VariableOutputs { get; } = new[] { FlowNameVariableEntry.ToDescriptor() }; | ||
|
||
public IFlowStore FlowStore { get; } | ||
|
||
public OnFlowFinishedTrigger(ILogger<FlowTrigger<OnFlowFinishedTriggerData>> logger, IFlowStringProcessor flowStringProcessor, IAppState appState, IEventBus eventBus, IFlowStore flowStore) : base(logger, flowStringProcessor, appState, eventBus) | ||
{ | ||
FlowStore = flowStore; | ||
} | ||
|
||
public override bool CanExecute() => true; | ||
|
||
public override async Task<IObservable<IVariables>> Execute(OnFlowFinishedTriggerData data) | ||
{ | ||
return EventBus.Receive<FlowFinishedEvent>() | ||
.Where(x => x.FlowId == data.FlowId) | ||
.Where(x => MatchesExecutionState(x, data)) | ||
.Select(x => | ||
{ | ||
var newVariables = new Core.Variables.Variables(); | ||
var flow = FlowStore.Get(x.FlowId); | ||
newVariables.Set(FlowNameVariableEntry, flow!.Name); | ||
return newVariables; | ||
}); | ||
} | ||
|
||
private bool MatchesExecutionState(FlowFinishedEvent eventData, OnFlowFinishedTriggerData flowData) | ||
{ | ||
if (flowData.OnlyOnSuccessfulExecution) | ||
{ return eventData.ExecutionResultType is ExecutionResultType.Success or ExecutionResultType.FailedButContinue; } | ||
|
||
return true; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Strem.Flows.Default/Flows/Triggers/Utility/OnFlowFinishedTriggerData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using Strem.Flows.Data.Triggers; | ||
|
||
namespace Strem.Flows.Default.Flows.Triggers.Utility; | ||
|
||
public class OnFlowFinishedTriggerData : IFlowTriggerData | ||
{ | ||
public static readonly string TriggerCode = "on-flow-finished"; | ||
public static readonly string TriggerVersion = "1.0.0"; | ||
|
||
public Guid Id { get; set; } = Guid.NewGuid(); | ||
public string Code => TriggerCode; | ||
public string Version { get; set; } = TriggerVersion; | ||
|
||
[Required] | ||
public Guid FlowId { get; set; } | ||
|
||
public bool OnlyOnSuccessfulExecution { get; set; } = true; | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Strem.Flows.Default/Flows/Triggers/Utility/OnFlowStartedTrigger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Reactive.Linq; | ||
using Microsoft.Extensions.Logging; | ||
using Strem.Core.Events.Bus; | ||
using Strem.Core.Extensions; | ||
using Strem.Core.State; | ||
using Strem.Core.Variables; | ||
using Strem.Flows.Data.Triggers; | ||
using Strem.Flows.Events; | ||
using Strem.Flows.Processors; | ||
using Strem.Flows.Services.Stores; | ||
|
||
namespace Strem.Flows.Default.Flows.Triggers.Utility; | ||
|
||
public class OnFlowStartedTrigger : FlowTrigger<OnFlowStartedTriggerData> | ||
{ | ||
public override string Code => OnFlowStartedTriggerData.TriggerCode; | ||
public override string Version => OnFlowStartedTriggerData.TriggerVersion; | ||
|
||
public static VariableEntry FlowNameVariableEntry = new("flow.name"); | ||
|
||
public override string Name => "On Flow Started"; | ||
public override string Category => "Utility"; | ||
public override string Description => "Triggers when the matching flow starts executing"; | ||
public override VariableDescriptor[] VariableOutputs { get; } = new[] { FlowNameVariableEntry.ToDescriptor() }; | ||
|
||
public IFlowStore FlowStore { get; } | ||
|
||
public OnFlowStartedTrigger(ILogger<FlowTrigger<OnFlowStartedTriggerData>> logger, IFlowStringProcessor flowStringProcessor, IAppState appState, IEventBus eventBus, IFlowStore flowStore) : base(logger, flowStringProcessor, appState, eventBus) | ||
{ | ||
FlowStore = flowStore; | ||
} | ||
|
||
public override bool CanExecute() => true; | ||
|
||
public override async Task<IObservable<IVariables>> Execute(OnFlowStartedTriggerData data) | ||
{ | ||
return EventBus.Receive<FlowStartedEvent>() | ||
.Where(x => x.FlowId == data.FlowId) | ||
.Select(x => | ||
{ | ||
var newVariables = new Core.Variables.Variables(); | ||
var flow = FlowStore.Get(x.FlowId); | ||
newVariables.Set(FlowNameVariableEntry, flow!.Name); | ||
return newVariables; | ||
}); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Strem.Flows.Default/Flows/Triggers/Utility/OnFlowStartedTriggerData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using Strem.Flows.Data.Triggers; | ||
|
||
namespace Strem.Flows.Default.Flows.Triggers.Utility; | ||
|
||
public class OnFlowStartedTriggerData : IFlowTriggerData | ||
{ | ||
public static readonly string TriggerCode = "on-flow-started"; | ||
public static readonly string TriggerVersion = "1.0.0"; | ||
|
||
public Guid Id { get; set; } = Guid.NewGuid(); | ||
public string Code => TriggerCode; | ||
public string Version { get; set; } = TriggerVersion; | ||
|
||
[Required] | ||
public Guid FlowId { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
using Strem.Flows.Events.Base; | ||
using Strem.Flows.Types; | ||
|
||
namespace Strem.Flows.Events; | ||
|
||
public record FlowFinishedEvent(Guid FlowId) : FlowEvent(FlowId); | ||
public record FlowFinishedEvent(Guid FlowId, ExecutionResultType ExecutionResultType) : FlowEvent(FlowId); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters