Skip to content

Commit

Permalink
Update messages from within the Handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJump committed Feb 25, 2019
1 parent 74b7bb3 commit 06580fc
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 72 deletions.
8 changes: 7 additions & 1 deletion uSync8.BackOffice/App_Plugins/uSync8/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ <h3 class="bold"><localize key="usync_name">uSync 8</localize>
</div>
</div>
<div class="flex justify-center">
<h4 class="usync-action-message">{{vm.status.Message}}</h4>
<div class="text-center">
<h4 class="usync-action-message">{{vm.status.Message}}</h4>
<small>{{vm.update.Message}}</small>
<div class="progress" style="height: 3px;">
<div class="bar" role="progressbar" style="width: {{vm.calcPercentage(vm.update)}}%;" aria-valuenow="{{vm.calcPercentage(vm.update)}}" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</div>
</div>
<div class="progress" style="height: 1px;">
<div class="bar" role="progressbar" style="width: {{vm.calcPercentage(vm.status)}}%;" aria-valuenow="{{vm.calcPercentage(vm.status)}}" aria-valuemin="0" aria-valuemax="100"></div>
Expand Down
16 changes: 13 additions & 3 deletions uSync8.BackOffice/App_Plugins/uSync8/uSyncDashboardController.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
}

function calcPercentage(status) {
return (100 * status.Processed) / status.TotalSteps;
return (100 * status.Count) / status.Total;
}

function toggle(item) {
Expand All @@ -191,6 +191,10 @@
vm.status = data;
});

vm.hub.on('update', function (update) {
vm.update = update;
});

vm.hub.start();
});

Expand Down Expand Up @@ -226,10 +230,16 @@
vm.showAll = false;

vm.status = {
Processed: 0,
TotalSteps: 1,
Count: 0,
Total: 1,
Message: "Initializing",
Handlers: []
};

vm.update = {
Message: "",
Count: 0,
Total: 1
};

switch (mode) {
Expand Down
16 changes: 13 additions & 3 deletions uSync8.BackOffice/Controllers/uSyncDashboardApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public IEnumerable<uSyncAction> Report(uSyncOptions options)
var hubClient = new HubClientService(options.clientId);
var summaryClient = new SummaryHandler(hubClient);

return uSyncService.Report(settings.RootFolder, summaryClient.PostSummary);
return uSyncService.Report(settings.RootFolder, summaryClient.PostSummary, summaryClient.PostUdate);
}

[HttpPost]
Expand All @@ -101,7 +101,7 @@ public IEnumerable<uSyncAction> Export(uSyncOptions options)
var hubClient = new HubClientService(options.clientId);
var summaryClient = new SummaryHandler(hubClient);

return uSyncService.Export(settings.RootFolder, summaryClient.PostSummary);
return uSyncService.Export(settings.RootFolder, summaryClient.PostSummary, summaryClient.PostUdate);
}

[HttpPut]
Expand All @@ -110,7 +110,7 @@ public IEnumerable<uSyncAction> Import(uSyncOptions options)
var hubClient = new HubClientService(options.clientId);
var summaryClient = new SummaryHandler(hubClient);

return uSyncService.Import(settings.RootFolder, options.force, summaryClient.PostSummary);
return uSyncService.Import(settings.RootFolder, options.force, summaryClient.PostSummary, summaryClient.PostUdate);
}

[HttpPost]
Expand All @@ -136,6 +136,16 @@ public void PostSummary(SyncProgressSummary summary)
hubClient.SendMessage(summary);
}

public void PostUdate(string message, int count, int total)
{
hubClient.SendUpdate(new
{
Message = message,
Count = count,
Total = total
});
}

}

public class uSyncOptions
Expand Down
18 changes: 18 additions & 0 deletions uSync8.BackOffice/Hubs/HubClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,23 @@ public void SendMessage<TObject>(TObject item)
hubContext.Clients.All.Add(item);
}
}

public void SendUpdate(Object message)
{
if (hubContext != null)
{
if (!string.IsNullOrWhiteSpace(clientId))
{
var client = hubContext.Clients.Client(clientId);
if (client != null)
{
client.Update(message);
return;
}
}

hubContext.Clients.All.Update(message);
}
}
}
}
6 changes: 3 additions & 3 deletions uSync8.BackOffice/Models/SyncProgressModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace uSync8.BackOffice
{
public class SyncProgressSummary
{
public int Processed { get; set; }
public int TotalSteps { get; set; }
public int Count { get; set; }
public int Total { get; set; }
public string Message { get; set; }
public List<SyncHandlerSummary> Handlers { get; set; }

Expand All @@ -16,7 +16,7 @@ public SyncProgressSummary(
string message,
int totalSteps)
{
this.TotalSteps = totalSteps;
this.Total = totalSteps;
this.Message = message;

this.Handlers = handlers.Select(x => new SyncHandlerSummary()
Expand Down
20 changes: 10 additions & 10 deletions uSync8.BackOffice/Services/uSyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private void BackOfficeConfig_Reloaded(uSyncSettings settings)

public delegate void SyncEventCallback(SyncProgressSummary summary);

public IEnumerable<uSyncAction> Report(string folder, SyncEventCallback callback = null)
public IEnumerable<uSyncAction> Report(string folder, SyncEventCallback callback = null, SyncUpdateCallback update = null)
{
var actions = new List<uSyncAction>();

Expand All @@ -51,13 +51,13 @@ public IEnumerable<uSyncAction> Report(string folder, SyncEventCallback callback
var handler = configuredHandler.Handler;
var handlerSettings = configuredHandler.Settings;

summary.Processed++;
summary.Count++;

summary.UpdateHandler(handler.Name, HandlerStatus.Processing, $"Reporting {handler.Name}");

callback?.Invoke(summary);

actions.AddRange(handler.Report($"{folder}/{handler.DefaultFolder}", handlerSettings));
actions.AddRange(handler.Report($"{folder}/{handler.DefaultFolder}", handlerSettings, update));

summary.UpdateHandler(handler.Name, HandlerStatus.Complete);
}
Expand All @@ -70,7 +70,7 @@ public IEnumerable<uSyncAction> Report(string folder, SyncEventCallback callback

private static object _importLock = new object();

public IEnumerable<uSyncAction> Import(string folder, bool force, SyncEventCallback callback = null)
public IEnumerable<uSyncAction> Import(string folder, bool force, SyncEventCallback callback = null, SyncUpdateCallback update = null)
{
lock (_importLock)
{
Expand All @@ -95,22 +95,22 @@ public IEnumerable<uSyncAction> Import(string folder, bool force, SyncEventCallb
var handler = configuredHandler.Handler;
var handlerSettings = configuredHandler.Settings;

summary.Processed++;
summary.Count++;

summary.UpdateHandler(
handler.Name, HandlerStatus.Processing, $"Importing {handler.Name}");

callback?.Invoke(summary);

actions.AddRange(handler.ImportAll($"{folder}/{handler.DefaultFolder}", handlerSettings, force));
actions.AddRange(handler.ImportAll($"{folder}/{handler.DefaultFolder}", handlerSettings, force, update));

summary.UpdateHandler(handler.Name, HandlerStatus.Complete);
}


// postImport things (mainly cleaning up folders)

summary.Processed++;
summary.Count++;
summary.UpdateHandler("Post Import", HandlerStatus.Pending, "Post Import Actions");

callback?.Invoke(summary);
Expand Down Expand Up @@ -153,7 +153,7 @@ public IEnumerable<uSyncAction> Import(string folder, bool force, SyncEventCallb
}
}

public IEnumerable<uSyncAction> Export(string folder, SyncEventCallback callback = null)
public IEnumerable<uSyncAction> Export(string folder, SyncEventCallback callback = null, SyncUpdateCallback update = null)
{
var actions = new List<uSyncAction>();

Expand All @@ -164,14 +164,14 @@ public IEnumerable<uSyncAction> Export(string folder, SyncEventCallback callback
foreach (var configuredHandler in configuredHandlers)
{
var handler = configuredHandler.Handler;
summary.Processed++;
summary.Count++;

summary.UpdateHandler(
handler.Name, HandlerStatus.Processing, $"Exporting {handler.Name}");

callback?.Invoke(summary);

actions.AddRange(handler.ExportAll($"{folder}/{handler.DefaultFolder}", configuredHandler.Settings));
actions.AddRange(handler.ExportAll($"{folder}/{handler.DefaultFolder}", configuredHandler.Settings, update));

summary.UpdateHandler(handler.Name, HandlerStatus.Complete);
}
Expand Down
2 changes: 1 addition & 1 deletion uSync8.BackOffice/SyncHandlers/Handlers/DataTypeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public override IEnumerable<uSyncAction> ProcessPostImport(string folder, IEnume
var attempt = Import(action.FileName, config);
if (attempt.Success)
{
ImportSecondPass(action.FileName, attempt.Item, config);
ImportSecondPass(action.FileName, attempt.Item, config, null);
}
}

Expand Down
10 changes: 8 additions & 2 deletions uSync8.BackOffice/SyncHandlers/Handlers/MacroHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,19 @@ public MacroHandler(IEntityService entityService,
/// <summary>
/// overrider the default export, because macros, don't exist as an object type???
/// </summary>
public override IEnumerable<uSyncAction> ExportAll(int parent, string folder, HandlerSettings config = null)
public override IEnumerable<uSyncAction> ExportAll(int parent, string folder, HandlerSettings config, SyncUpdateCallback callback)
{
// we clean the folder out on an export all.
syncFileService.CleanFolder(folder);

var actions = new List<uSyncAction>();

var items = macroService.GetAll();
var items = macroService.GetAll().ToList();
int count = 0;
foreach(var item in items)
{
count++;
callback?.Invoke(item.Name, count, items.Count);
actions.Add(Export(item, folder, config));
}

Expand Down
8 changes: 5 additions & 3 deletions uSync8.BackOffice/SyncHandlers/ISyncHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace uSync8.BackOffice.SyncHandlers
{
public delegate void SyncUpdateCallback(string message, int count, int total);

public interface ISyncHandler
{
string Alias { get; }
Expand All @@ -23,9 +25,9 @@ public interface ISyncHandler

void Initialize(HandlerSettings settings);

IEnumerable<uSyncAction> ExportAll(string folder, HandlerSettings settings);
IEnumerable<uSyncAction> ImportAll(string folder, HandlerSettings settings, bool force);
IEnumerable<uSyncAction> Report(string folder, HandlerSettings settings);
IEnumerable<uSyncAction> ExportAll(string folder, HandlerSettings settings, SyncUpdateCallback callback);
IEnumerable<uSyncAction> ImportAll(string folder, HandlerSettings settings, bool force, SyncUpdateCallback callback);
IEnumerable<uSyncAction> Report(string folder, HandlerSettings settings, SyncUpdateCallback callback);

// uSyncAction Import(string file, bool force);

Expand Down
Loading

0 comments on commit 06580fc

Please sign in to comment.