Skip to content

Improved workflow creation vs update process in the Dashboard #469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
namespace Synapse.Dashboard.Pages.Functions.Create;

/// <summary>
/// The <see cref="State{TState}"/> of the workflow editor
/// The <see cref="State{TState}"/> of the function editor
/// </summary>
[Feature]
public record CreateFunctionViewState
Expand All @@ -40,7 +40,12 @@ public record CreateFunctionViewState
public SemVersion Version { get; set; } = new SemVersion(1, 0, 0);

/// <summary>
/// Gets/sets the definition of the workflow to create
/// Gets/sets a boolean determining if the function is new or if it's an update
/// </summary>
public bool IsNew { get; set; } = true;

/// <summary>
/// Gets/sets the definition of the function to create
/// </summary>
public TaskDefinition? Function { get; set; } = null;

Expand Down
82 changes: 52 additions & 30 deletions src/dashboard/Synapse.Dashboard/Pages/Functions/Create/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Semver;
using ServerlessWorkflow.Sdk.Models;
using Synapse.Api.Client.Services;
using Synapse.Dashboard.Pages.Workflows.Create;
using Synapse.Resources;

namespace Synapse.Dashboard.Pages.Functions.Create;
Expand Down Expand Up @@ -189,7 +190,6 @@ MonacoInterop monacoInterop
#endregion

#region Setters

/// <summary>
/// Sets the state's <see cref="CreateFunctionViewState.Name"/>
/// </summary>
Expand All @@ -215,6 +215,18 @@ public void SetChosenName(string? name)
});
}

/// <summary>
/// Sets the state's <see cref="CreateFunctionViewState.IsNew"/>
/// </summary>
/// <param name="isNew">The new <see cref="CreateFunctionViewState.IsNew"/> value</param>
public void SetIsNew(bool isNew)
{
this.Reduce(state => state with
{
IsNew = isNew
});
}

/// <summary>
/// Sets the state's <see cref="CreateFunctionViewState" /> <see cref="ProblemDetails"/>'s related data
/// </summary>
Expand Down Expand Up @@ -393,6 +405,7 @@ public async Task SaveCustomFunctionAsync()
this.YamlSerializer.Deserialize<TaskDefinition>(functionText)!;
var name = this.Get(state => state.Name) ?? this.Get(state => state.ChosenName);
var version = this.Get(state => state.Version).ToString();
var isNew = this.Get(state => state.IsNew);
if (string.IsNullOrEmpty(name))
{
this.Reduce(state => state with
Expand All @@ -407,41 +420,41 @@ public async Task SaveCustomFunctionAsync()
Saving = true
});
CustomFunction? resource = null;
try
{
resource = await this.ApiClient.CustomFunctions.GetAsync(name);
}
catch
if (isNew)
{
// Assume 404, might need actual handling
}
if (resource == null)
{
resource = await this.ApiClient.CustomFunctions.CreateAsync(new()

try
{
Metadata = new()
resource = await this.ApiClient.CustomFunctions.CreateAsync(new()
{
Name = name
},
Spec = new()
{
Versions = [new(version, function)]
}
});
}
else
{
var updatedResource = resource.Clone()!;
updatedResource.Spec.Versions.Add(new(version, function));
var jsonPatch = JsonPatch.FromDiff(this.JsonSerializer.SerializeToElement(resource)!.Value, this.JsonSerializer.SerializeToElement(updatedResource)!.Value);
var patch = this.JsonSerializer.Deserialize<Json.Patch.JsonPatch>(jsonPatch.RootElement);
if (patch != null)
Metadata = new()
{
Name = name
},
Spec = new()
{
Versions = [new(version, function)]
}
});
this.NavigationManager.NavigateTo($"/functions/{name}");
return;
}
catch (ProblemDetailsException ex) when (ex.Problem.Title == "Conflict" && ex.Problem.Detail != null && ex.Problem.Detail.EndsWith("already exists"))
{
var resourcePatch = new Patch(PatchType.JsonPatch, jsonPatch);
await this.ApiClient.ManageCluster<CustomFunction>().PatchAsync(name, resourcePatch, null, this.CancellationTokenSource.Token);
// the function exists, try to update it instead
}
}
this.NavigationManager.NavigateTo($"/functions/{name}");
resource = await this.ApiClient.CustomFunctions.GetAsync(name);
var updatedResource = resource.Clone()!;
updatedResource.Spec.Versions.Add(new(version, function));
var jsonPatch = JsonPatch.FromDiff(this.JsonSerializer.SerializeToElement(resource)!.Value, this.JsonSerializer.SerializeToElement(updatedResource)!.Value);
var patch = this.JsonSerializer.Deserialize<Json.Patch.JsonPatch>(jsonPatch.RootElement);
if (patch != null)
{
var resourcePatch = new Patch(PatchType.JsonPatch, jsonPatch);
await this.ApiClient.ManageCluster<CustomFunction>().PatchAsync(name, resourcePatch, null, this.CancellationTokenSource.Token);
}

}
catch (ProblemDetailsException ex)
{
Expand Down Expand Up @@ -477,6 +490,15 @@ public async Task SaveCustomFunctionAsync()
catch (Exception ex)
{
this.Logger.LogError("Unable to save function definition: {exception}", ex.ToString());
this.Reduce(state => state with
{
ProblemTitle = "Error",
ProblemDetail = "An error occurred while saving the function.",
ProblemErrors = new Dictionary<string, string[]>()
{
{"Message", [ex.ToString()] }
}
});
}
finally
{
Expand Down
15 changes: 10 additions & 5 deletions src/dashboard/Synapse.Dashboard/Pages/Functions/Create/View.razor
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ else
<PreferredLanguageSelector PreferredLanguageChange="Store.ToggleTextBasedEditorLanguageAsync" />
</div>
<StandaloneCodeEditor @ref="Store.TextEditor"
ConstructionOptions="Store.StandaloneEditorConstructionOptions"
OnDidInit="Store.OnTextBasedEditorInitAsync"
OnDidChangeModelContent="Store.OnDidChangeModelContent"
CssClass="h-100" />
ConstructionOptions="Store.StandaloneEditorConstructionOptions"
OnDidInit="Store.OnTextBasedEditorInitAsync"
OnDidChangeModelContent="Store.OnDidChangeModelContent"
CssClass="h-100" />
@if (problemDetails != null)
{
<div class="problems px-3">
Expand Down Expand Up @@ -104,6 +104,7 @@ else
await base.OnInitializedAsync();
BreadcrumbManager.Use(Breadcrumbs.Functions);
BreadcrumbManager.Add(new("New", "/functions/new"));
Store.SetIsNew(true);
Store.Name.Subscribe(value => OnStateChanged(_ => name = value), token: CancellationTokenSource.Token);
Store.ChosenName.Subscribe(value => OnStateChanged(_ => chosenName = value), token: CancellationTokenSource.Token);
Store.Version.Subscribe(value => OnStateChanged(_ => version = value?.ToString()), token: CancellationTokenSource.Token);
Expand All @@ -115,7 +116,11 @@ else
/// <inheritdoc/>
protected override void OnParametersSet()
{
if (Name != name) Store.SetName(Name);
if (Name != name)
{
Store.SetName(Name);
Store.SetIsNew(false);
}
}

protected void SetName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public record CreateWorkflowViewState
/// </summary>
public string? DslVersion { get; set; }

/// <summary>
/// Gets/sets a boolean determining if the workflow is new or if it's an update
/// </summary>
public bool IsNew { get; set; } = true;

/// <summary>
/// Gets/sets the definition of the workflow to create
/// </summary>
Expand Down
99 changes: 59 additions & 40 deletions src/dashboard/Synapse.Dashboard/Pages/Workflows/Create/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// limitations under the License.

using JsonCons.Utilities;
using Neuroglia.Collections;
using Neuroglia.Data;
using Semver;
using ServerlessWorkflow.Sdk.Models;
Expand Down Expand Up @@ -102,7 +101,7 @@ IWorkflowDefinitionValidator workflowDefinitionValidator
protected MonacoInterop MonacoInterop { get; } = monacoInterop;

/// <summary>
/// Gets the service to to validate workflow defintions
/// Gets the service to to validate workflow definitions
/// </summary>
protected IWorkflowDefinitionValidator WorkflowDefinitionValidator { get; } = workflowDefinitionValidator;

Expand Down Expand Up @@ -218,6 +217,18 @@ public void SetName(string? name)
});
}

/// <summary>
/// Sets the state's <see cref="CreateWorkflowViewState.IsNew"/>
/// </summary>
/// <param name="isNew">The new <see cref="CreateWorkflowViewState.IsNew"/> value</param>
public void SetIsNew(bool isNew)
{
this.Reduce(state => state with
{
IsNew = isNew
});
}

/// <summary>
/// Sets the state's <see cref="CreateWorkflowViewState" /> <see cref="ProblemDetails"/>'s related data
/// </summary>
Expand Down Expand Up @@ -415,57 +426,56 @@ public async Task SaveWorkflowDefinitionAsync()
var @namespace = workflowDefinition!.Document.Namespace;
var name = workflowDefinition.Document.Name;
var version = workflowDefinition.Document.Version;
var isNew = this.Get(state => state.IsNew);
this.Reduce(s => s with
{
Saving = true
});
Workflow? workflow = null;
try
if (isNew)
{
workflow = await this.Api.Workflows.GetAsync(name, @namespace);
}
catch
{
// Assume 404, might need actual handling
}
if (workflow == null)
{
workflow = await this.Api.Workflows.CreateAsync(new()
{
Metadata = new()
try {
workflow = await this.Api.Workflows.CreateAsync(new()
{
Namespace = workflowDefinition!.Document.Namespace,
Name = workflowDefinition.Document.Name
},
Spec = new()
{
Versions = [workflowDefinition]
}
});
}
else
{
var updatedResource = workflow.Clone()!;
var documentVersion = SemVersion.Parse(version, SemVersionStyles.Strict)!;
var latestVersion = SemVersion.Parse(updatedResource.Spec.Versions.GetLatest().Document.Version, SemVersionStyles.Strict)!;
if (updatedResource.Spec.Versions.Any(v => SemVersion.Parse(v.Document.Version, SemVersionStyles.Strict).CompareSortOrderTo(documentVersion) >= 0))
{
this.Reduce(state => state with
{
ProblemTitle = "Invalid version",
ProblemDetail = $"The specified version '{documentVersion}' must be strictly superior to the latest version '{latestVersion}'."
Metadata = new()
{
Namespace = workflowDefinition!.Document.Namespace,
Name = workflowDefinition.Document.Name
},
Spec = new()
{
Versions = [workflowDefinition]
}
});
this.NavigationManager.NavigateTo($"/workflows/details/{@namespace}/{name}/{version}");
return;
}
updatedResource.Spec.Versions.Add(workflowDefinition!);
var jsonPatch = JsonPatch.FromDiff(this.JsonSerializer.SerializeToElement(workflow)!.Value, this.JsonSerializer.SerializeToElement(updatedResource)!.Value);
var patch = this.JsonSerializer.Deserialize<Json.Patch.JsonPatch>(jsonPatch.RootElement);
if (patch != null)
catch (ProblemDetailsException ex) when (ex.Problem.Title == "Conflict" && ex.Problem.Detail != null && ex.Problem.Detail.EndsWith("already exists"))
{
var resourcePatch = new Patch(PatchType.JsonPatch, jsonPatch);
await this.Api.ManageNamespaced<Workflow>().PatchAsync(name, @namespace, resourcePatch, null, this.CancellationTokenSource.Token);
// the workflow exists, try to update it instead
}
}
workflow = await this.Api.Workflows.GetAsync(name, @namespace);
var updatedResource = workflow.Clone()!;
var documentVersion = SemVersion.Parse(version, SemVersionStyles.Strict)!;
var latestVersion = SemVersion.Parse(updatedResource.Spec.Versions.GetLatest().Document.Version, SemVersionStyles.Strict)!;
if (updatedResource.Spec.Versions.Any(v => SemVersion.Parse(v.Document.Version, SemVersionStyles.Strict).CompareSortOrderTo(documentVersion) >= 0))
{
this.Reduce(state => state with
{
ProblemTitle = "Invalid version",
ProblemDetail = $"The specified version '{documentVersion}' must be strictly superior to the latest version '{latestVersion}'."
});
return;
}
updatedResource.Spec.Versions.Add(workflowDefinition!);
var jsonPatch = JsonPatch.FromDiff(this.JsonSerializer.SerializeToElement(workflow)!.Value, this.JsonSerializer.SerializeToElement(updatedResource)!.Value);
var patch = this.JsonSerializer.Deserialize<Json.Patch.JsonPatch>(jsonPatch.RootElement);
if (patch != null)
{
var resourcePatch = new Patch(PatchType.JsonPatch, jsonPatch);
await this.Api.ManageNamespaced<Workflow>().PatchAsync(name, @namespace, resourcePatch, null, this.CancellationTokenSource.Token);
}
this.NavigationManager.NavigateTo($"/workflows/details/{@namespace}/{name}/{version}");
}
catch (ProblemDetailsException ex)
Expand Down Expand Up @@ -502,6 +512,15 @@ public async Task SaveWorkflowDefinitionAsync()
catch (Exception ex)
{
this.Logger.LogError("Unable to save workflow definition: {exception}", ex.ToString());
this.Reduce(state => state with
{
ProblemTitle = "Error",
ProblemDetail = "An error occurred while saving the workflow.",
ProblemErrors = new Dictionary<string, string[]>()
{
{"Message", [ex.ToString()] }
}
});
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ else
<PreferredLanguageSelector PreferredLanguageChange="Store.ToggleTextBasedEditorLanguageAsync" />
</div>
<StandaloneCodeEditor @ref="Store.TextEditor"
ConstructionOptions="Store.StandaloneEditorConstructionOptions"
OnDidInit="Store.OnTextBasedEditorInitAsync"
OnDidChangeModelContent="Store.OnDidChangeModelContent"
CssClass="h-100" />
ConstructionOptions="Store.StandaloneEditorConstructionOptions"
OnDidInit="Store.OnTextBasedEditorInitAsync"
OnDidChangeModelContent="Store.OnDidChangeModelContent"
CssClass="h-100" />
@if (problemDetails != null)
{
<div class="problems px-3">
Expand Down Expand Up @@ -93,6 +93,7 @@ else
await base.OnInitializedAsync();
BreadcrumbManager.Use(Breadcrumbs.Workflows);
BreadcrumbManager.Add(new($"New", $"/workflows/new"));
Store.SetIsNew(true);
Store.Namespace.Subscribe(value => OnStateChanged(_ => ns = value), token: CancellationTokenSource.Token);
Store.Name.Subscribe(value => OnStateChanged(_ => name = value), token: CancellationTokenSource.Token);
Store.Loading.Subscribe(value => OnStateChanged(_ => loading = value), token: CancellationTokenSource.Token);
Expand All @@ -110,6 +111,7 @@ else
if (Name != name)
{
Store.SetName(Name);
Store.SetIsNew(false);
}
}

Expand Down
Loading