From a8b092b3d28c44beff66c6d17ece06103291eaff Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Tue, 21 Jan 2020 18:48:44 +0200 Subject: Refactored JobRunsLogger to save job length, name, source and JobString. Fixed issue with machine emulator number of units. --- .../Tango.Emulations/Emulators/MachineEmulator.cs | 2 +- .../JobRuns/BasicJobRunsLogger.cs | 166 ++++++--------------- 2 files changed, 50 insertions(+), 118 deletions(-) (limited to 'Software/Visual_Studio') diff --git a/Software/Visual_Studio/Tango.Emulations/Emulators/MachineEmulator.cs b/Software/Visual_Studio/Tango.Emulations/Emulators/MachineEmulator.cs index 06a287693..0c06448e1 100644 --- a/Software/Visual_Studio/Tango.Emulations/Emulators/MachineEmulator.cs +++ b/Software/Visual_Studio/Tango.Emulations/Emulators/MachineEmulator.cs @@ -704,7 +704,7 @@ namespace Tango.Emulations.Emulators List calculatedStops = new List(); - for (int i = 0; i < job.NumberOfUnits; i++) + for (int i = 0; i < units; i++) { double progress = 0; double lastProgress = 0; diff --git a/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs b/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs index 78cc9a284..ce175e3d2 100644 --- a/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs +++ b/Software/Visual_Studio/Tango.Integration/JobRuns/BasicJobRunsLogger.cs @@ -76,31 +76,7 @@ namespace Tango.Integration.JobRuns return IsStarted && _job != null && JobDesignation.HasFlag(_job.Designation); } - #endregion - - #region Public Methods - - /// - /// Starts the logger. - /// - public void Start() - { - IsStarted = true; - } - - /// - /// Stops the logger. - /// - public void Stop() - { - IsStarted = false; - } - - #endregion - - #region Event Handlers - - private void Machine_PrintingFailed(object sender, PrintingFailedEventArgs e) + private void InsertJobRun(PrintingEventArgs e, JobRunStatus status, Exception exception) { if (ShouldLog()) { @@ -112,18 +88,27 @@ namespace Tango.Integration.JobRuns { using (var db = ObservablesContext.CreateDefault()) { - db.JobRuns.Add(new JobRun() + JobRun run = new JobRun(); + + run.StartDate = _start_date; + run.EndDate = DateTime.UtcNow; + run.JobName = _job.Name; + run.JobLength = _job.LengthIncludingNumberOfUnits; + run.JobSource = _job.Source; + run.JobGuid = _job.Guid; + run.MachineGuid = _job.MachineGuid; + run.JobRunStatus = status; + run.EndPosition = e.JobHandler.Status.Progress; + run.JobLength = e.JobHandler.Status.TotalProgress; + run.LiquidQuantities = e.LiquidQuantities; + run.JobString = _job.ToJobFileWhenLoaded().ToString(); + + if (exception != null) { - StartDate = _start_date, - EndDate = DateTime.UtcNow, - JobGuid = _job.Guid, - MachineGuid = _job.MachineGuid, - JobRunStatus = JobRunStatus.Failed, - EndPosition = e.JobHandler.Status.Progress, - FailedMessage = e.Exception.Message, - JobLength = e.JobHandler.Status.TotalProgress, - LiquidQuantities = e.LiquidQuantities, - }); + run.FailedMessage = exception.FlattenMessage(); + } + + db.JobRuns.Add(run); e.Job.LastRun = DateTime.UtcNow; _job.LastRun = DateTime.UtcNow; @@ -147,96 +132,43 @@ namespace Tango.Integration.JobRuns } } - private void Machine_PrintingAborted(object sender, PrintingEventArgs e) - { - if (ShouldLog()) - { - if (e.Job.Guid == _job.Guid) - { - Task.Factory.StartNew(() => - { - try - { - using (var db = ObservablesContext.CreateDefault()) - { - db.JobRuns.Add(new JobRun() - { - StartDate = _start_date, - EndDate = DateTime.UtcNow, - JobGuid = _job.Guid, - MachineGuid = _job.MachineGuid, - EndPosition = e.JobHandler.Status.Progress, - JobRunStatus = JobRunStatus.Aborted, - JobLength = e.JobHandler.Status.TotalProgress, - LiquidQuantities = e.LiquidQuantities, - }); - - e.Job.LastRun = DateTime.UtcNow; - _job.LastRun = DateTime.UtcNow; - - var job = db.Jobs.SingleOrDefault(x => x.Guid == _job.Guid); + #endregion - if (job != null) - { - job.LastRun = DateTime.UtcNow; - } + #region Public Methods - db.SaveChanges(); - } - } - catch (Exception ex) - { - LogManager.Log(ex, "Error logging the current job run to the database."); - } - }); - } - } + /// + /// Starts the logger. + /// + public void Start() + { + IsStarted = true; } - private void Machine_PrintingCompleted(object sender, PrintingEventArgs e) + /// + /// Stops the logger. + /// + public void Stop() { - if (ShouldLog()) - { - if (e.Job.Guid == _job.Guid) - { - Task.Factory.StartNew(() => - { - try - { - using (var db = ObservablesContext.CreateDefault()) - { - db.JobRuns.Add(new JobRun() - { - StartDate = _start_date, - EndDate = DateTime.UtcNow, - JobGuid = _job.Guid, - MachineGuid = _job.MachineGuid, - EndPosition = e.JobHandler.Status.Progress, - JobRunStatus = JobRunStatus.Completed, - JobLength = e.JobHandler.Status.TotalProgress, - LiquidQuantities = e.LiquidQuantities, - }); + IsStarted = false; + } - e.Job.LastRun = DateTime.UtcNow; - _job.LastRun = DateTime.UtcNow; + #endregion - var job = db.Jobs.SingleOrDefault(x => x.Guid == _job.Guid); + #region Event Handlers - if (job != null) - { - job.LastRun = DateTime.UtcNow; - } + private void Machine_PrintingFailed(object sender, PrintingFailedEventArgs e) + { + InsertJobRun(e, JobRunStatus.Failed, e.Exception); + } - db.SaveChanges(); - } - } - catch (Exception ex) - { - LogManager.Log(ex, "Error logging the current job run to the database."); - } - }); - } - } + private void Machine_PrintingAborted(object sender, PrintingEventArgs e) + { + InsertJobRun(e, JobRunStatus.Aborted, null); + } + + private void Machine_PrintingCompleted(object sender, PrintingEventArgs e) + { + InsertJobRun(e, JobRunStatus.Completed, null); } private void Machine_PrintingStarted(object sender, PrintingEventArgs e) -- cgit v1.3.1