Skip to content

Commit

Permalink
better user experience in compute client (#52)
Browse files Browse the repository at this point in the history
- replaced wait with NoWait and wait as default
- automatically report progress without verbose
  • Loading branch information
fw2568 authored Oct 22, 2023
1 parent f5e89f2 commit d96b18b
Show file tree
Hide file tree
Showing 35 changed files with 464 additions and 104 deletions.
12 changes: 6 additions & 6 deletions src/Eryph.ComputeClient.Commands/Catlets/CatletCmdLet.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Eryph.ComputeClient.Models;
using Eryph.ComputeClient.Models;
using JetBrains.Annotations;

namespace Eryph.ComputeClient.Commands.Catlets
Expand All @@ -12,9 +11,9 @@ protected Catlet GetSingleCatlet(string id)
return Factory.CreateCatletsClient().Get(id);
}

protected void WaitForOperation(Operation operation, bool wait, bool alwaysWriteMachine, string knownMachineId = default)
protected void WaitForOperation(Operation operation, bool noWait, bool alwaysWriteMachine, string knownMachineId = default)
{
if (!wait)
if (noWait)
{
if (knownMachineId == default || !alwaysWriteMachine)
WriteObject(operation);
Expand All @@ -23,14 +22,15 @@ protected void WaitForOperation(Operation operation, bool wait, bool alwaysWrite
return;
}

WaitForOperation(operation, (op) => ResourceWriter(op, WriteCatlet));
return;

void WriteCatlet(ResourceType resourceType, string id)
{
if (resourceType == ResourceType.Catlet)
WriteObject(GetSingleCatlet(id));

}

WaitForOperation(operation, (op) => ResourceWriter(op, WriteCatlet));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ protected override void ProcessRecord()

private void WriteConfig(CatletConfiguration config)
{
var configString = config.ToString();
var catletConfig = CatletConfigDictionaryConverter.Convert(
ConfigModelJsonSerializer.DeserializeToDictionary(config.Configuration));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Management.Automation;
using System.Management.Automation;
using Eryph.ComputeClient.Models;
using JetBrains.Annotations;

Expand Down
10 changes: 5 additions & 5 deletions src/Eryph.ComputeClient.Commands/Catlets/NewCatletCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public class NewCatletCommand : CatletConfigCmdlet
public string Config { get; set; }

[Parameter]
public SwitchParameter Wait
public SwitchParameter NoWait
{
get => _wait;
set => _wait = value;
get => _noWait;
set => _noWait = value;
}

[Parameter]
Expand All @@ -48,7 +48,7 @@ public SwitchParameter Wait
[ValidateNotNullOrEmpty]
public string Name { get; set; }

private bool _wait;
private bool _noWait;
private StringBuilder _input = new StringBuilder();

protected override void ProcessRecord()
Expand Down Expand Up @@ -101,7 +101,7 @@ protected override void EndProcessing()
.Create(
new NewCatletRequest(Guid.NewGuid(),
JsonSerializer.SerializeToElement(config,
ConfigModelJsonSerializer.DefaultOptions))), _wait, true);
ConfigModelJsonSerializer.DefaultOptions))), _noWait, true);
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/Eryph.ComputeClient.Commands/Catlets/RemoveCatletCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Management.Automation;
using Eryph.ClientRuntime;
using Eryph.ComputeClient.Models;
using JetBrains.Annotations;
using Operation = Eryph.ComputeClient.Models.Operation;
Expand Down Expand Up @@ -44,14 +43,14 @@ public SwitchParameter PassThru
}

[Parameter]
public SwitchParameter Wait
public SwitchParameter NoWait
{
get => _wait;
set => _wait = value;
get => _nowait;
set => _nowait = value;
}

private bool _force;
private bool _wait;
private bool _nowait;
private bool _passThru;

private bool _yesToAll, _noToAll;
Expand Down Expand Up @@ -79,7 +78,7 @@ protected override void ProcessRecord()
continue;
}

WaitForOperation(Factory.CreateCatletsClient().Delete(id).Value, _wait, false, id);
WaitForOperation(Factory.CreateCatletsClient().Delete(id).Value, _nowait, false, id);

if (PassThru)
WriteObject(catlet);
Expand Down
10 changes: 5 additions & 5 deletions src/Eryph.ComputeClient.Commands/Catlets/StartCatletCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public SwitchParameter Force
}

[Parameter]
public SwitchParameter Wait
public SwitchParameter NoWait
{
get => _wait;
set => _wait = value;
get => _nowait;
set => _nowait = value;
}

private bool _force;
private bool _wait;
private bool _nowait;
private bool _yesToAll, _noToAll;


Expand All @@ -64,7 +64,7 @@ protected override void ProcessRecord()
continue;
}

WaitForOperation(Factory.CreateCatletsClient().Start(id), _wait, false, id);
WaitForOperation(Factory.CreateCatletsClient().Start(id), _nowait, false, id);

}

Expand Down
10 changes: 5 additions & 5 deletions src/Eryph.ComputeClient.Commands/Catlets/StopCatletCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public SwitchParameter Force


[Parameter]
public SwitchParameter Wait
public SwitchParameter NoWait
{
get => _wait;
set => _wait = value;
get => _nowait;
set => _nowait = value;
}

private bool _force;
private bool _wait;
private bool _nowait;
private bool _yesToAll, _noToAll;


Expand All @@ -65,7 +65,7 @@ protected override void ProcessRecord()
continue;
}

WaitForOperation(Factory.CreateCatletsClient().Stop(id).Value, _wait, false, id);
WaitForOperation(Factory.CreateCatletsClient().Stop(id).Value, _nowait, false, id);

}

Expand Down
10 changes: 5 additions & 5 deletions src/Eryph.ComputeClient.Commands/Catlets/UpdateCatletCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public class UpdateCatletCommand : CatletConfigCmdlet
public string Config { get; set; }

[Parameter]
public SwitchParameter Wait
public SwitchParameter NoWait
{
get => _wait;
set => _wait = value;
get => _nowait;
set => _nowait = value;
}

private bool _wait;
private bool _nowait;


protected override void ProcessRecord()
Expand All @@ -40,7 +40,7 @@ protected override void ProcessRecord()
var config = DeserializeConfigString(Config);
WaitForOperation(Factory.CreateCatletsClient().Update(new UpdateCatletRequest(Guid.NewGuid(),
JsonSerializer.SerializeToElement(config), id))
, _wait, true);
, _nowait, true);
}

}
Expand Down
112 changes: 111 additions & 1 deletion src/Eryph.ComputeClient.Commands/ComputeCmdLet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,122 @@ protected void WaitForOperation(Operation operation, Action<Operation> writerDel


var processedLogIds = new List<string>();
var activityCounter = -1;
var activityIds = new Dictionary<string, int> { { operation.Id, activityCounter++ } };

var completedActivities = new List<int>();

while (!Stopping)
{
Task.Delay(1000).GetAwaiter().GetResult();

var currentOperation = Factory.CreateOperationsClient()
.Get(operation.Id, timeStamp, expand: "logs").Value;
.Get(operation.Id, timeStamp, expand: "logs,tasks").Value;

foreach (var operationTask in currentOperation.Tasks)
{
if (!activityIds.ContainsKey(operationTask.Id))
activityIds.Add(operationTask.Id, activityCounter++);
}

var activities = new Dictionary<string,ProgressRecord>();
foreach (var operationTask in currentOperation.Tasks)
{
var displayName = operationTask.DisplayName;
if(string.IsNullOrWhiteSpace(displayName))
{
if (operationTask.ParentTask == operation.Id)
{
displayName = operationTask.Name;
if(displayName.EndsWith("Command"))
// ReSharper disable once ReplaceSubstringWithRangeIndexer
displayName = displayName.Substring(0, displayName.Length - 7);
}
}

if (!string.IsNullOrWhiteSpace(displayName))
{
var activityName = operationTask.ParentTask == operation.Id
? $"{displayName} (Operation: {operation.Id})"
: $"{displayName} (Task: {operationTask.Id})";

var progressRecord = new ProgressRecord(activityIds[operationTask.Id], activityName,
operationTask.Status.ToString())
{
PercentComplete = operationTask.Status.GetValueOrDefault() == OperationTaskStatus.Completed
? 100
: operationTask.Progress.GetValueOrDefault(),
ParentActivityId = !string.IsNullOrWhiteSpace(operationTask.ParentTask)
? activityIds[operationTask.ParentTask]
: 0,
RecordType = operationTask.Status.GetValueOrDefault() == OperationTaskStatus.Completed
? ProgressRecordType.Completed
: ProgressRecordType.Processing,
};

if (progressRecord.PercentComplete == 0)
progressRecord.PercentComplete = -1;


var lastTaskLogEntry = currentOperation.LogEntries.Where(x => x.TaskId == operationTask.Id)
.OrderByDescending(x => x.Timestamp)
.FirstOrDefault();

if (lastTaskLogEntry != null)
progressRecord.CurrentOperation = lastTaskLogEntry.Message;

activities.Add(operationTask.Id,progressRecord);
}
}

// second pass for tasks without display name - to show them as operation of parent task
foreach (var operationTask in currentOperation.Tasks.Where(x => x.ParentTask != operation.Id))
{
var displayName = operationTask.DisplayName;
if (!string.IsNullOrWhiteSpace(displayName))
continue;

var lastTaskLogEntry = currentOperation.LogEntries.Where(x => x.TaskId == operationTask.Id)
.OrderByDescending(x => x.Timestamp)
.FirstOrDefault();

if (lastTaskLogEntry == null) continue; // no log, so no need to update parent

var parentId = operationTask.ParentTask;
while (parentId!= null)
{
var parentTask = currentOperation.Tasks.FirstOrDefault(x => x.Id == parentId);
if(parentTask == null)
break;

parentId = parentTask.ParentTask;

if (!activities.TryGetValue(parentTask.Id, out var activity)) continue;
activity.CurrentOperation = lastTaskLogEntry.Message;
break;

}

}

foreach (var progressRecord in activities.Values.OrderBy(x=>x.ActivityId))
{
if(completedActivities.Contains(progressRecord.ActivityId))
continue;

//looking for running child - in that case keep parent status to running
var runningChild = activities.Any(x =>
x.Value.ParentActivityId == progressRecord.ActivityId &&
progressRecord.RecordType == ProgressRecordType.Processing);

if(runningChild)
progressRecord.RecordType = ProgressRecordType.Processing;

WriteProgress(progressRecord);

if (progressRecord.RecordType == ProgressRecordType.Completed)
completedActivities.Add(progressRecord.ActivityId);
}

foreach (var logEntry in currentOperation.LogEntries)
{
Expand Down
9 changes: 5 additions & 4 deletions src/Eryph.ComputeClient.Commands/Networks/NetworkCmdLet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ protected VirtualNetwork GetSingleNetwork(string id)
}


protected void WaitForOperation(Operation operation, bool wait, bool alwaysWriteNetwork, string knownNetworkId = default)
protected void WaitForOperation(Operation operation, bool noWait, bool alwaysWriteNetwork, string knownNetworkId = default)
{
if (!wait)
if (noWait)
{
if (knownNetworkId == default || !alwaysWriteNetwork)
WriteObject(operation);
Expand All @@ -23,14 +23,15 @@ protected void WaitForOperation(Operation operation, bool wait, bool alwaysWrite
return;
}

WaitForOperation(operation, (op) => ResourceWriter(op, Write));
return;

void Write(ResourceType resourceType, string id)
{
if (resourceType == ResourceType.VirtualNetwork)
WriteObject(GetSingleNetwork(id));

}

WaitForOperation(operation, (op) => ResourceWriter(op, Write));
}
}
}
10 changes: 5 additions & 5 deletions src/Eryph.ComputeClient.Commands/Networks/SetVNetworkCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ public class SetVNetworkCommand : NetworkConfigCmdlet
public string Config { get; set; }

[Parameter]
public SwitchParameter Wait
public SwitchParameter NoWait
{
get => _wait;
set => _wait = value;
get => _nowait;
set => _nowait = value;
}

[Parameter]
[ValidateNotNullOrEmpty]
public string ProjectName { get; set; }

private bool _wait;
private bool _nowait;
private StringBuilder _input = new StringBuilder();


Expand Down Expand Up @@ -80,7 +80,7 @@ protected override void EndProcessing()
.Create(
new UpdateProjectNetworksRequest(Guid.NewGuid(),
JsonSerializer.SerializeToElement(config,
ConfigModelJsonSerializer.DefaultOptions))), _wait, false);
ConfigModelJsonSerializer.DefaultOptions))), _nowait, false);
}
}

Expand Down
Loading

0 comments on commit d96b18b

Please sign in to comment.