Skip to content

Commit

Permalink
V12/historyupdate (#553)
Browse files Browse the repository at this point in the history
* #552 moved history folder

* #552 added change and item counts

* #552 fixed blank on first load

* #552 removed history value from dashboard and added build version

* catch and log history exceptions
  • Loading branch information
henryjump authored Oct 6, 2023
1 parent 20451f6 commit fa879dd
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 31 deletions.
23 changes: 14 additions & 9 deletions uSync.History/Controllers/uSyncHistoryController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Infrastructure.Migrations.Expressions.Delete;
using Umbraco.Cms.Web.BackOffice.Controllers;
using Umbraco.Cms.Web.Common.Attributes;
Expand All @@ -11,21 +12,20 @@ namespace uSync.History.Controllers
[PluginController("uSync")]
public class uSyncHistoryController : UmbracoAuthorizedApiController
{
private readonly uSyncConfigService _configService;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly SyncFileService _syncFileService;

public uSyncHistoryController(uSyncConfigService configService, SyncFileService syncFileService)
public uSyncHistoryController(SyncFileService syncFileService, IHostingEnvironment hostingEnvironment)
{
_configService = configService;
_syncFileService = syncFileService;
_hostingEnvironment = hostingEnvironment;
}

public bool GetApi() => true;

public IEnumerable<HistoryInfo> GetHistory()
{
var rootFolder = _syncFileService.GetAbsPath(_configService.GetRootFolder());
var historyFolder = Path.GetFullPath(Path.Combine(rootFolder, "..", "history"));
string historyFolder = GetHistoryFolder();
var files = _syncFileService.GetFiles(historyFolder, "*.json")
.Select(x => x.Substring(historyFolder.Length + 1));

Expand All @@ -38,11 +38,17 @@ public IEnumerable<HistoryInfo> GetHistory()
return list.OrderByDescending(x => x.Date);
}

private string GetHistoryFolder()
{
var rootFolder = _syncFileService.GetAbsPath(_hostingEnvironment.LocalTempPath);
var historyFolder = Path.GetFullPath(Path.Combine(rootFolder, "uSync", "history"));
return historyFolder;
}

public bool ClearHistory()
{
// 1. get history folder
var rootFolder = _syncFileService.GetAbsPath(_configService.GetRootFolder());
var historyFolder = Path.GetFullPath(Path.Combine(rootFolder, "..", "history"));
string historyFolder = GetHistoryFolder();
// 2. get history files
var files = _syncFileService.GetFiles(historyFolder, "*.json");
// 3. delet this
Expand All @@ -56,8 +62,7 @@ public bool ClearHistory()

public HistoryInfo LoadHistory(string filePath)
{
var rootFolder = _syncFileService.GetAbsPath(_configService.GetRootFolder());
var historyFolder = Path.GetFullPath(Path.Combine(rootFolder, "..", "history"));
string historyFolder = GetHistoryFolder();
var fullPath = Path.Combine(historyFolder, filePath);
string contents = _syncFileService.LoadContent(fullPath);

Expand Down
4 changes: 4 additions & 0 deletions uSync.History/HistoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ public class HistoryInfo
public string Method { get; set; }

public string FilePath { get; set; }

public int Changes { get; set; }

public int Total { get; set; }
}
}
4 changes: 2 additions & 2 deletions uSync.History/uSyncHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace uSync.History
{
public class uSyncHistory : ISyncAddOn
{
public string Name => "uSync History";
public string Name => "_uSync History";

public string Version => "1.0";
public string Version => typeof(uSyncHistory).Assembly.GetName().Version.ToString(3);

public string Icon => "icon-calendar-alt";

Expand Down
53 changes: 35 additions & 18 deletions uSync.History/uSyncHistoryNotificationHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Newtonsoft.Json;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Security;
using uSync.BackOffice;
using uSync.BackOffice.Configuration;
Expand All @@ -11,15 +13,21 @@ internal class uSyncHistoryNotificationHandler
: INotificationHandler<uSyncImportCompletedNotification>,
INotificationHandler<uSyncExportCompletedNotification>
{
private readonly uSyncConfigService _configService;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly SyncFileService _syncFileService;
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
private readonly ILogger<uSyncHistoryNotificationHandler> _logger;

public uSyncHistoryNotificationHandler(uSyncConfigService configService, SyncFileService syncFileService, IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
public uSyncHistoryNotificationHandler(
SyncFileService syncFileService,
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
IHostingEnvironment hostingEnvironment,
ILogger<uSyncHistoryNotificationHandler> logger)
{
_configService = configService;
_syncFileService = syncFileService;
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
_hostingEnvironment = hostingEnvironment;
_logger = logger;
}

public void Handle(uSyncImportCompletedNotification notification)
Expand All @@ -30,7 +38,7 @@ public void Handle(uSyncImportCompletedNotification notification)

if (changeActions.Any())
{
SaveActions(changeActions, "Import");
SaveActions(changeActions, "Import", notification.Actions.Count());
}
}

Expand All @@ -42,28 +50,37 @@ public void Handle(uSyncExportCompletedNotification notification)

if (changeActions.Any())
{
SaveActions(changeActions, "Export");
SaveActions(changeActions, "Export", notification.Actions.Count());
}
}

private void SaveActions(IEnumerable<uSyncAction> actions, string method)
private void SaveActions(IEnumerable<uSyncAction> actions, string method, int total)
{
var historyInfo = new HistoryInfo
try
{
Actions = actions,
Date = DateTime.Now,
Username = _backOfficeSecurityAccessor?.BackOfficeSecurity?.CurrentUser?.Username ?? "Background Process",
Method = method
};
var historyInfo = new HistoryInfo
{
Actions = actions,
Date = DateTime.Now,
Username = _backOfficeSecurityAccessor?.BackOfficeSecurity?.CurrentUser?.Username ?? "Background Process",
Method = method,
Total = total,
Changes = actions.CountChanges()
};

var historyJson = JsonConvert.SerializeObject(historyInfo, Formatting.Indented);
var historyJson = JsonConvert.SerializeObject(historyInfo, Formatting.Indented);

var rootFolder = _syncFileService.GetAbsPath(_configService.GetRootFolder());
var historyFile = Path.Combine(rootFolder, "..", "history", DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".json");
var rootFolder = _syncFileService.GetAbsPath(_hostingEnvironment.LocalTempPath);
var historyFile = Path.Combine(rootFolder, "uSync", "history", DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".json");

_syncFileService.CreateFoldersForFile(historyFile);
_syncFileService.CreateFoldersForFile(historyFile);

_syncFileService.SaveFile(historyFile, historyJson);
_syncFileService.SaveFile(historyFile, historyJson);
}
catch(Exception ex)
{
_logger.LogWarning(ex, "Failed to save history.");
}
}
}
}
6 changes: 4 additions & 2 deletions uSync.History/wwwroot/uSyncHistory/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
</div>
<div class="umb-table-cell">
<localize key="usync_{{item.method.toLowerCase()}}"/>

</div>
<div class="umb-table-cell">
{{item.changes}}/{{item.total}}
</div>
<div class="umb-table-cell umb-table__name">
<div>
Expand All @@ -38,7 +40,7 @@
</div>
</umb-box-content>
</umb-box>
<div class="usync-fresh-view" ng-if="vm.history.length==0">
<div class="usync-fresh-view" ng-if="vm.history==null||vm.history.length==0">
<umb-empty-state>

<div class="usync-group-box-title">
Expand Down

0 comments on commit fa879dd

Please sign in to comment.