-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathCreateWorkflowInstanceDialog.razor
More file actions
271 lines (248 loc) · 10.3 KB
/
Copy pathCreateWorkflowInstanceDialog.razor
File metadata and controls
271 lines (248 loc) · 10.3 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
@*
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.
*@
@namespace Synapse.Dashboard.Components
@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
@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="Raw">
<Content>
<div class="pt-3">
<MonacoEditor OnTextChanged="OnTextChanged" ModelName="@modelName" Document="input" />
</div>
</Content>
</Tab>
</Tabs>
<Accordion class="py-3">
<AccordionItem Title="Advanced Settings">
<Content>
<div>
@if (Operators != null && Operators.Count() > 0)
{
<label for="operator" class="fw-bolder mb-2">Run on:</label>
<select id="operator" class="form-select" @onchange="(e) => SetOperator(e.Value?.ToString())">
<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="mt-4">
<DictionaryEditor Title="Labels"
KeyPlaceholder="Enter label key"
ValuePlaceholder="Enter label value"
Entries="parameters.Labels"
OnAddEntry="(kvp) => AddLabel(kvp.Key, kvp.Value)"
OnRemoveEntry="RemoveLabel" />
</div>
<div class="mt-4">
<DictionaryEditor Title="Annotations"
KeyPlaceholder="Enter annotation key"
ValuePlaceholder="Enter annotation value"
Entries="parameters.Annotations"
OnAddEntry="(kvp) => AddAnnotation(kvp.Key, kvp.Value)"
OnRemoveEntry="RemoveAnnotation" />
</div>
</Content>
</AccordionItem>
</Accordion>
<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 modelName = string.Empty;
EquatableDictionary<string, object>? input;
CreateWorkflowInstanceParameters parameters = new();
string? operatorName;
[Parameter] public WorkflowDefinition? WorkflowDefinition { get; set; }
[Parameter] public IEnumerable<Operator> Operators { get; set; } = [];
[Parameter] public EquatableDictionary<string, object>? Input { get; set; }
[Parameter] public EquatableDictionary<string, string>? Labels { get; set; }
[Parameter] public EquatableDictionary<string, string>? Annotations { get; set; }
[Parameter] public EventCallback<CreateWorkflowInstanceParameters> OnCreate { get; set; }
[Parameter] public EventCallback<ProblemDetails> OnProblem { get; set; }
void OnValueChanged(object? value)
{
parameters.Input = value == null ? string.Empty : JsonSerializer.SerializeToText(value);
}
void OnTextChanged(string value)
{
parameters.Input = value;
}
protected override async Task OnParametersSetAsync()
{
await base.OnParametersSetAsync();
if (input != Input) input = Input;
if (Labels != null && Labels.Count > 0) SetLabels(Labels);
else SetLabels(null);
if (Annotations != null && Annotations.Count > 0) SetAnnotations(Annotations);
else SetAnnotations(null);
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 SetLabels(EquatableDictionary<string, string>? labels)
{
parameters.Labels = labels != null ? [.. labels] : [];
if (labels != null) operatorName = labels.TryGetValue(SynapseDefaults.Resources.Labels.Operator, out var label) ? label : null;
}
protected void AddLabel(string key, string value)
{
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value))
{
return;
}
if (parameters.Labels.ContainsKey(key))
{
parameters.Labels.Remove(key);
}
parameters.Labels.Add(key, value);
}
protected void RemoveLabel(string key)
{
if (string.IsNullOrWhiteSpace(key))
{
return;
}
if (parameters.Labels.ContainsKey(key))
{
parameters.Labels.Remove(key);
}
}
protected void SetOperator(string? operatorName)
{
if (string.IsNullOrEmpty(operatorName))
RemoveLabel(SynapseDefaults.Resources.Labels.Operator);
else
AddLabel(SynapseDefaults.Resources.Labels.Operator, operatorName);
}
protected void SetAnnotations(EquatableDictionary<string, string>? annotations)
{
parameters.Annotations = annotations != null ? [.. annotations] : [];
}
protected void AddAnnotation(string key, string value)
{
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value))
{
return;
}
if (parameters.Annotations.ContainsKey(key))
{
parameters.Annotations.Remove(key);
}
parameters.Annotations.Add(key, value);
}
protected void RemoveAnnotation(string key)
{
if (string.IsNullOrWhiteSpace(key))
{
return;
}
if (parameters.Annotations.ContainsKey(key))
{
parameters.Annotations.Remove(key);
}
}
async Task OnStartAsync()
{
if (schema != null)
{
var node = string.IsNullOrWhiteSpace(parameters.Input) ? null : JsonSerializer.Deserialize<JsonNode>(parameters.Input);
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(parameters);
}
}
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"),
};
}
}
}