-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathCreateWorkflowInstanceDialog.razor
More file actions
183 lines (166 loc) · 7.08 KB
/
CreateWorkflowInstanceDialog.razor
File metadata and controls
183 lines (166 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
@*
Copyright © 2024-Present The Synapse Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*@
@using Json.Schema
@using Neuroglia.Data.Infrastructure.ResourceOriented.Properties
@using ServerlessWorkflow.Sdk
@using ServerlessWorkflow.Sdk.Models
@using System.Xml.Schema
@using Synapse.Core.Infrastructure.Services
@using System.Text.Json.Nodes
@namespace Synapse.Dashboard.Components
@inject MonacoInterop MonacoInterop
@inject IExternalResourceProvider ExternalResourceProvider
@inject IJsonSerializer JsonSerializer
@inject IXmlSerializer XmlSerializer
<Tabs>
@if(schema != null)
{
<Tab Title="Form" Active="true">
<Content>
<div class="pt-3">
<DynamicForm Schema="schema" OnValueChanged="value => OnValueChanged(value)" />
</div>
</Content>
</Tab>
}
<Tab Title="Text">
<Content>
<div class="pt-3">
<MonacoEditor OnTextChanged="OnTextChanged" ModelName="@modelName" Document="input" />
</div>
</Content>
</Tab>
</Tabs>
<div class="py-3">
@if (Operators != null && Operators.Count() > 0)
{
<select class="form-select m-2" @onchange="OnSelectOperatorChanged">
<option value="">Any Operator</option>
@foreach (var op in Operators)
{
var name = op.GetName() + "." + op.GetNamespace();
<option value="@name" selected="@(name == operatorName)">@name</option>
}
</select>
}
</div>
<div class="text-center">
<Button Outline="true" Color="ButtonColor.Primary" class="m-auto mt-3 w-100" @onclick="async _ => await OnStartAsync()">
<Icon Name="IconName.Play" />
Start
</Button>
</div>
@code {
WorkflowDefinition? workflowDefinition;
JsonSchema? schema;
string payload = string.Empty;
string modelName = string.Empty;
EquatableDictionary<string, object>? input;
string? operatorName;
[Parameter] public WorkflowDefinition? WorkflowDefinition { get; set; }
[Parameter] public EquatableList<Operator> Operators { get; set; } = [];
[Parameter] public string? OperatorName { get; set; } = null;
[Parameter] public EquatableDictionary<string, object>? Input { get; set; }
[Parameter] public EventCallback<CreateWorkflowInstanceParameters> OnCreate { get; set; }
[Parameter] public EventCallback<ProblemDetails> OnProblem { get; set; }
void OnValueChanged(object? value)
{
payload = value == null ? string.Empty : JsonSerializer.SerializeToText(value);
}
void OnTextChanged(string value)
{
payload = value;
}
protected override async Task OnParametersSetAsync()
{
await base.OnParametersSetAsync();
if (input != Input) input = Input;
if (operatorName != OperatorName) operatorName = OperatorName;
if (workflowDefinition != WorkflowDefinition)
{
workflowDefinition = WorkflowDefinition;
await LoadSchemaAsync();
}
if (WorkflowDefinition?.Input?.Schema?.Document != null)
{
modelName = WorkflowDefinition.Document.Name + "-" + WorkflowDefinition.Document.Version;
await MonacoInterop.AddValidationSchemaAsync(JsonSerializer.SerializeToText(WorkflowDefinition.Input.Schema.Document), $"https://synapse.io/schemas/{modelName}.json", $"{modelName}*").ConfigureAwait(false);
}
}
protected void OnSelectOperatorChanged(ChangeEventArgs e)
{
operatorName = e.Value?.ToString();
}
async Task OnStartAsync()
{
if (schema != null)
{
var node = string.IsNullOrWhiteSpace(payload) ? null : JsonSerializer.Deserialize<JsonNode>(payload);
var evaluationOptions = new EvaluationOptions()
{
OutputFormat = OutputFormat.List
};
var evaluationResult = schema.Evaluate(node, evaluationOptions);
if (!evaluationResult.IsValid)
{
var errors = evaluationResult.Details.Where(d => d.Errors != null).SelectMany(d => d.Errors!).GroupBy(e => e.Key).Select(e => new KeyValuePair<string, string[]>(e.Key, e.Select(e => e.Value).ToArray())).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
var problemDetails = new ProblemDetails(ErrorTypes.Invalid, ProblemTitles.ValidationFailed, ErrorStatus.Validation, "Workflow instance input validation failed", errors: errors);
if (OnProblem.HasDelegate) await OnProblem.InvokeAsync(problemDetails);
return;
}
}
if (OnCreate.HasDelegate)
{
await OnCreate.InvokeAsync(new CreateWorkflowInstanceParameters() {
Input = payload,
Operator = operatorName
});
}
}
async Task LoadSchemaAsync()
{
var schemaDefinition = WorkflowDefinition?.Input?.Schema;
if (schemaDefinition == null) return;
if (schemaDefinition.Resource == null)
{
switch (schemaDefinition.Format)
{
case SchemaFormat.Avro:
schema = Avro.Schema.Parse(JsonSerializer.SerializeToText(schemaDefinition.Document)).ToJsonSchema();
break;
case SchemaFormat.Json:
schema = JsonSchema.FromText(JsonSerializer.SerializeToText(schemaDefinition.Document));
break;
case SchemaFormat.Xml:
var xml = XmlSerializer.SerializeToText(schemaDefinition.Document);
var stringReader = new StringReader(xml);
schema = XmlSchema.Read(stringReader, null)!.ToJsonSchema();
break;
default:
throw new NotSupportedException($"The specified schema format '{schemaDefinition.Format}' is not supported");
}
}
else
{
using var stream = await ExternalResourceProvider.ReadAsync(schemaDefinition.Resource).ConfigureAwait(false);
using var streamReader = new StreamReader(stream);
schema = schemaDefinition.Format switch
{
SchemaFormat.Avro => Avro.Schema.Parse(await streamReader.ReadToEndAsync()).ToJsonSchema(),
SchemaFormat.Json => await JsonSchema.FromStream(stream).ConfigureAwait(false),
SchemaFormat.Xml => XmlSchema.Read(stream, null)!.ToJsonSchema(),
_ => throw new NotSupportedException($"The specified schema format '{schemaDefinition.Format}' is not supported"),
};
}
}
}