refactor: make exporting consistent

This commit is contained in:
Gary Sharp
2025-02-06 19:14:36 +11:00
parent f946f3250c
commit 67f1c2a5d1
69 changed files with 908 additions and 921 deletions
@@ -1,9 +1,8 @@
using Disco.Models.Repository;
using Disco.Models.Services.Devices.Exporting;
using Disco.Models.Services.Devices.Importing;
using Disco.Services;
using Disco.Services.Authorization;
using Disco.Services.Devices.Exporting;
using Disco.Services.Devices;
using Disco.Services.Devices.Importing;
using Disco.Services.Exporting;
using Disco.Services.Interop;
@@ -685,7 +684,6 @@ namespace Disco.Web.Areas.API.Controllers
#endregion
#region Exporting
internal const string ExportSessionCacheKey = "DeviceExportContext_{0}";
[DiscoAuthorize(Claims.Device.Actions.Export)]
[HttpPost, ValidateAntiForgeryToken]
@@ -699,39 +697,29 @@ namespace Disco.Web.Areas.API.Controllers
Database.SaveChanges();
// Start Export
var exportContext = DeviceExportTask.ScheduleNow(Model.Options);
// Store Export Context in Web Cache
string key = string.Format(ExportSessionCacheKey, exportContext.TaskStatus.SessionId);
HttpRuntime.Cache.Insert(key, exportContext, null, DateTime.Now.AddMinutes(60), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
// Set Task Finished Url
var finishedActionResult = MVC.Device.Export(exportContext.TaskStatus.SessionId, null, null);
exportContext.TaskStatus.SetFinishedUrl(Url.Action(finishedActionResult));
var exportContext = new DeviceExportContext(Model.Options);
var taskContext = ExportTask.ScheduleNowCacheResult(exportContext, id => Url.Action(MVC.Device.Export(id, null, null)));
// Try waiting for completion
if (exportContext.TaskStatus.WaitUntilFinished(TimeSpan.FromSeconds(2)))
return RedirectToAction(finishedActionResult);
if (taskContext.TaskStatus.WaitUntilFinished(TimeSpan.FromSeconds(1)))
return RedirectToAction(MVC.Device.Export(taskContext.Id, null, null));
else
return RedirectToAction(MVC.Config.Logging.TaskStatus(exportContext.TaskStatus.SessionId));
return RedirectToAction(MVC.Config.Logging.TaskStatus(taskContext.TaskStatus.SessionId));
}
[DiscoAuthorize(Claims.Device.Actions.Export)]
public virtual ActionResult ExportRetrieve(string Id)
public virtual ActionResult ExportRetrieve(Guid id)
{
if (string.IsNullOrWhiteSpace(Id))
throw new ArgumentNullException("Id");
if (id == Guid.Empty)
throw new ArgumentNullException(nameof(id));
string key = string.Format(ExportSessionCacheKey, Id);
var context = HttpRuntime.Cache.Get(key) as ExportTaskContext<DeviceExportOptions>;
if (context == null)
throw new ArgumentException("The Id specified is invalid, or the export data expired (60 minutes)", "Id");
if (!ExportTask.TryFromCache(id, out var context))
throw new ArgumentException("The export id specified is invalid, or the export data expired (60 minutes)", nameof(id));
if (context.Result == null || context.Result.Result == null)
throw new ArgumentException("The export session is still running, or failed to complete successfully", "Id");
throw new ArgumentException("The export session is still running, or failed to complete successfully", nameof(id));
if (context.Result.RecordCount == 0)
throw new ArgumentException("No records were found to export", nameof(Id));
throw new ArgumentException("No records were found to export", nameof(id));
var fileStream = context.Result.Result;
@@ -404,7 +404,6 @@ namespace Disco.Web.Areas.API.Controllers
#endregion
#region Exporting
internal const string ExportSessionCacheKey = "DeviceFlagExportContext_{0}";
[DiscoAuthorize(Claims.Config.DeviceFlag.Export)]
public virtual ActionResult Export(ExportModel Model)
@@ -413,39 +412,29 @@ namespace Disco.Web.Areas.API.Controllers
throw new ArgumentNullException(nameof(Model));
// Start Export
var exportContext = DeviceFlagExportTask.ScheduleNow(Model.Options);
// Store Export Context in Web Cache
string key = string.Format(ExportSessionCacheKey, exportContext.TaskStatus.SessionId);
HttpRuntime.Cache.Insert(key, exportContext, null, DateTime.Now.AddMinutes(60), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
// Set Task Finished Url
var finishedActionResult = MVC.Config.DeviceFlag.Export(exportContext.TaskStatus.SessionId, null, null);
exportContext.TaskStatus.SetFinishedUrl(Url.Action(finishedActionResult));
var exportContext = new DeviceFlagExportContext(Model.Options);
var taskContext = ExportTask.ScheduleNowCacheResult(exportContext, id => Url.Action(MVC.Config.DeviceFlag.Export(id, null, null)));
// Try waiting for completion
if (exportContext.TaskStatus.WaitUntilFinished(TimeSpan.FromSeconds(2)))
return RedirectToAction(finishedActionResult);
if (taskContext.TaskStatus.WaitUntilFinished(TimeSpan.FromSeconds(1)))
return RedirectToAction(MVC.Config.DeviceFlag.Export(taskContext.Id, null, null));
else
return RedirectToAction(MVC.Config.Logging.TaskStatus(exportContext.TaskStatus.SessionId));
return RedirectToAction(MVC.Config.Logging.TaskStatus(taskContext.TaskStatus.SessionId));
}
[DiscoAuthorize(Claims.Config.DeviceFlag.Export)]
public virtual ActionResult ExportRetrieve(string Id)
public virtual ActionResult ExportRetrieve(Guid id)
{
if (string.IsNullOrWhiteSpace(Id))
throw new ArgumentNullException("Id");
if (id == Guid.Empty)
throw new ArgumentNullException(nameof(id));
string key = string.Format(ExportSessionCacheKey, Id);
var context = HttpRuntime.Cache.Get(key) as ExportTaskContext<DeviceFlagExportOptions>;
if (context == null)
throw new ArgumentException("The Id specified is invalid, or the export data expired (60 minutes)", nameof(Id));
if (!ExportTask.TryFromCache(id, out var context))
throw new ArgumentException("The export id specified is invalid, or the export data expired (60 minutes)", nameof(id));
if (context.Result == null || context.Result.Result == null)
throw new ArgumentException("The export session is still running, or failed to complete successfully", nameof(Id));
throw new ArgumentException("The export session is still running, or failed to complete successfully", nameof(id));
if (context.Result.RecordCount == 0)
throw new ArgumentException("No records were found to export", nameof(Id));
throw new ArgumentException("No records were found to export", nameof(id));
var fileStream = context.Result.Result;
@@ -1,13 +1,11 @@
using Disco.Models.Repository;
using Disco.Models.Services.Jobs;
using Disco.Models.Services.Jobs.Exporting;
using Disco.Models.Services.Jobs.JobLists;
using Disco.Services;
using Disco.Services.Authorization;
using Disco.Services.Exporting;
using Disco.Services.Interop;
using Disco.Services.Jobs;
using Disco.Services.Jobs.Exporting;
using Disco.Services.Jobs.JobLists;
using Disco.Services.Jobs.Statistics;
using Disco.Services.Users;
@@ -2168,7 +2166,6 @@ namespace Disco.Web.Areas.API.Controllers
}
#region Exporting
internal const string ExportSessionCacheKey = "JobExportContext_{0}";
[DiscoAuthorize(Claims.Job.Actions.Export)]
[HttpPost, ValidateAntiForgeryToken]
@@ -2182,34 +2179,24 @@ namespace Disco.Web.Areas.API.Controllers
Database.SaveChanges();
// Start Export
var exportContext = JobExportTask.ScheduleNow(model.Options);
// Store Export Context in Web Cache
string key = string.Format(ExportSessionCacheKey, exportContext.TaskStatus.SessionId);
HttpRuntime.Cache.Insert(key, exportContext, null, DateTime.Now.AddMinutes(60), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
// Set Task Finished Url
var finishedActionResult = MVC.Job.Export(exportContext.TaskStatus.SessionId);
exportContext.TaskStatus.SetFinishedUrl(Url.Action(finishedActionResult));
var exportContext = new JobExportContext(model.Options);
var taskContext = ExportTask.ScheduleNowCacheResult(exportContext, id => Url.Action(MVC.Job.Export(id)));
// Try waiting for completion
if (exportContext.TaskStatus.WaitUntilFinished(TimeSpan.FromSeconds(2)))
return RedirectToAction(finishedActionResult);
if (taskContext.TaskStatus.WaitUntilFinished(TimeSpan.FromSeconds(1)))
return RedirectToAction(MVC.Job.Export(taskContext.Id));
else
return RedirectToAction(MVC.Config.Logging.TaskStatus(exportContext.TaskStatus.SessionId));
return RedirectToAction(MVC.Config.Logging.TaskStatus(taskContext.TaskStatus.SessionId));
}
[DiscoAuthorize(Claims.Job.Actions.Export)]
public virtual ActionResult ExportRetrieve(string id)
public virtual ActionResult ExportRetrieve(Guid id)
{
if (string.IsNullOrWhiteSpace(id))
throw new ArgumentNullException("Id");
if (id == Guid.Empty)
throw new ArgumentNullException(nameof(id));
string key = string.Format(ExportSessionCacheKey, id);
var context = HttpRuntime.Cache.Get(key) as ExportTaskContext<JobExportOptions>;
if (context == null)
throw new ArgumentException("The Id specified is invalid, or the export data expired (60 minutes)", nameof(id));
if (!ExportTask.TryFromCache(id, out var context))
throw new ArgumentException("The export id specified is invalid, or the export data expired (60 minutes)", nameof(id));
if (context.Result == null || context.Result.Result == null)
throw new ArgumentException("The export session is still running, or failed to complete successfully", nameof(id));
@@ -1,4 +1,5 @@
using Disco.Models.Exporting;
using Disco.Models.Services.Logging;
using Disco.Services.Authorization;
using Disco.Services.Logging;
using Disco.Services.Tasks;
@@ -23,30 +24,40 @@ namespace Disco.Web.Areas.API.Controllers
[HttpPost, ValidateAntiForgeryToken, DiscoAuthorize(Claims.Config.Logging.Show)]
public virtual ActionResult RetrieveEvents(string Format, DateTime? Start = null, DateTime? End = null, int? ModuleId = null, List<int> EventTypeIds = null, int? Take = null)
{
var logRetriever = new ReadLogContext()
if (string.Equals(Format, "json", StringComparison.OrdinalIgnoreCase))
{
Start = Start,
End = End,
Module = ModuleId,
EventTypes = EventTypeIds,
Take = Take
};
var results = logRetriever.Query(Database);
var exportFormat = ExportFormat.Xlsx;
switch (Format.ToLower())
{
case "json":
return Json(results, JsonRequestBehavior.AllowGet);
case "csv":
exportFormat = ExportFormat.Csv;
break;
var logRetriever = new ReadLogContext()
{
Start = Start,
End = End,
Module = ModuleId,
EventTypes = EventTypeIds,
Take = Take,
};
var results = logRetriever.Query(Database);
return Json(results, JsonRequestBehavior.AllowGet);
}
else
{
var exportFormat = ExportFormat.Xlsx;
if (string.Equals(Format, "csv", StringComparison.OrdinalIgnoreCase))
exportFormat = ExportFormat.Csv;
var export = LogExport.GenerateExport(exportFormat, results);
var options = new LogExportOptions()
{
Format = exportFormat,
StartDate = Start,
EndDate = End,
ModuleId = ModuleId,
EventTypeIds = EventTypeIds,
Take = Take,
};
var exportContext = new LogExportContext(options);
return File(export.Result, export.MimeType, export.Filename);
var export = exportContext.Export(Database, ScheduledTaskMockStatus.Create("Log Export"));
return File(export.Result, export.MimeType, export.Filename);
}
}
public virtual ActionResult ScheduledTaskStatus(string id)
@@ -409,7 +409,6 @@ namespace Disco.Web.Areas.API.Controllers
#endregion
#region Exporting
internal const string ExportSessionCacheKey = "UserFlagExportContext_{0}";
[DiscoAuthorize(Claims.Config.UserFlag.Export)]
public virtual ActionResult Export(ExportModel Model)
@@ -418,39 +417,26 @@ namespace Disco.Web.Areas.API.Controllers
throw new ArgumentNullException(nameof(Model));
// Start Export
var exportContext = UserFlagExportTask.ScheduleNow(Model.Options);
// Store Export Context in Web Cache
string key = string.Format(ExportSessionCacheKey, exportContext.TaskStatus.SessionId);
HttpRuntime.Cache.Insert(key, exportContext, null, DateTime.Now.AddMinutes(60), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
// Set Task Finished Url
var finishedActionResult = MVC.Config.UserFlag.Export(exportContext.TaskStatus.SessionId, null, null);
exportContext.TaskStatus.SetFinishedUrl(Url.Action(finishedActionResult));
var exportContext = new UserFlagExportContext(Model.Options);
var taskContext = ExportTask.ScheduleNowCacheResult(exportContext, id => Url.Action(MVC.Config.UserFlag.Export(id, null, null)));
// Try waiting for completion
if (exportContext.TaskStatus.WaitUntilFinished(TimeSpan.FromSeconds(2)))
return RedirectToAction(finishedActionResult);
if (taskContext.TaskStatus.WaitUntilFinished(TimeSpan.FromSeconds(1)))
return RedirectToAction(MVC.Config.UserFlag.Export(taskContext.Id, null, null));
else
return RedirectToAction(MVC.Config.Logging.TaskStatus(exportContext.TaskStatus.SessionId));
return RedirectToAction(MVC.Config.Logging.TaskStatus(taskContext.TaskStatus.SessionId));
}
[DiscoAuthorize(Claims.Config.UserFlag.Export)]
public virtual ActionResult ExportRetrieve(string Id)
public virtual ActionResult ExportRetrieve(Guid id)
{
if (string.IsNullOrWhiteSpace(Id))
throw new ArgumentNullException("Id");
string key = string.Format(ExportSessionCacheKey, Id);
var context = HttpRuntime.Cache.Get(key) as ExportTaskContext<UserFlagExportOptions>;
if (context == null)
throw new ArgumentException("The Id specified is invalid, or the export data expired (60 minutes)", nameof(Id));
if (!ExportTask.TryFromCache(id, out var context))
throw new ArgumentException("The export id specified is invalid, or the export data expired (60 minutes)", nameof(id));
if (context.Result == null || context.Result.Result == null)
throw new ArgumentException("The export session is still running, or failed to complete successfully", nameof(Id));
throw new ArgumentException("The export session is still running, or failed to complete successfully", nameof(id));
if (context.Result.RecordCount == 0)
throw new ArgumentException("No records were found to export", nameof(Id));
throw new ArgumentException("No records were found to export", nameof(id));
var fileStream = context.Result.Result;