From 3a4d0abfd126ffa3f94df490fa41e103614c46a7 Mon Sep 17 00:00:00 2001 From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch> Date: Fri, 2 Feb 2018 17:21:10 +0100 Subject: [PATCH] Calculate and report overall transfer progress. Having the total progress is much more useful than for the individual files, especially for transfers with many files. Closes #26 --- ATxCommon/Serializables/ServiceStatus.cs | 28 +++++++++++++++++++ ATxService/AutoTx.cs | 4 ++- ATxService/RoboCommand.cs | 34 +++++++++++++++++++----- 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/ATxCommon/Serializables/ServiceStatus.cs b/ATxCommon/Serializables/ServiceStatus.cs index 3d5f9c1..66e8b8c 100644 --- a/ATxCommon/Serializables/ServiceStatus.cs +++ b/ATxCommon/Serializables/ServiceStatus.cs @@ -29,6 +29,8 @@ namespace ATxCommon.Serializables private bool _cleanShutdown; private long _currentTransferSize; + private long _transferredBytesCompleted; + private long _transferredBytesCurrentFile; #region constructor, serializer and deserializer @@ -40,6 +42,8 @@ namespace ATxCommon.Serializables _currentTransferSrc = ""; _currentTargetTmp = ""; _transferInProgress = false; + _transferredBytesCompleted = 0; + _transferredBytesCurrentFile = 0; } public void Serialize() { @@ -226,6 +230,30 @@ namespace ATxCommon.Serializables } } + /// <summary> + /// Total size of files of the running transfer that are already fully transferred. + /// </summary> + public long TransferredBytesCompleted { + get { return _transferredBytesCompleted; } + set { + _transferredBytesCompleted = value; + Log.Trace("TransferredBytesCompleted was updated ({0}).", value); + Serialize(); + } + } + + /// <summary> + /// Total size of files of the running transfer that are already fully transferred. + /// </summary> + public long TransferredBytesCurrentFile { + get { return _transferredBytesCurrentFile; } + set { + _transferredBytesCurrentFile = value; + Log.Trace("TransferredBytesCurrentFile was updated ({0}).", value); + Serialize(); + } + } + #endregion getter / setter methods diff --git a/ATxService/AutoTx.cs b/ATxService/AutoTx.cs index 098f4fd..975fe7a 100644 --- a/ATxService/AutoTx.cs +++ b/ATxService/AutoTx.cs @@ -31,7 +31,9 @@ namespace ATxService private readonly List<string> _transferredFiles = new List<string>(); - private int _txProgress; + private RoboCommand _roboCommand; + private long _txCurFileSize; + private int _txCurFileProgress; private DateTime _lastUserDirCheck = DateTime.MinValue; diff --git a/ATxService/RoboCommand.cs b/ATxService/RoboCommand.cs index ecd8a92..3c60000 100644 --- a/ATxService/RoboCommand.cs +++ b/ATxService/RoboCommand.cs @@ -8,8 +8,6 @@ namespace ATxService { public partial class AutoTx { - private RoboCommand _roboCommand; - /// <summary> /// Start transferring data from a given source directory to the destination /// location that is stored in CurrentTargetTmp. Requires CopyState to be in @@ -75,7 +73,7 @@ namespace ATxService _roboCommand.Start(); Log.Info("Transfer started, total size: {0}", Conv.BytesToString(_status.CurrentTransferSize)); - Log.Debug("RoboCopy command options: {0}", _roboCommand.CommandOptions); + Log.Trace("RoboCopy command options: {0}", _roboCommand.CommandOptions); } catch (ManagementException ex) { Log.Error("Error in StartTransfer(): {0}", ex.Message); @@ -132,6 +130,16 @@ namespace ATxService if (processed.FileClass.ToLower().Equals("new file")) { _transferredFiles.Add(string.Format("{0} ({1})", processed.Name, Conv.BytesToString(processed.Size))); + // reset the value for the amount of bytes transferred for the current file: + _status.TransferredBytesCurrentFile = 0; + // now add _txCurFileSize (containing either the size of the previously + // transferred file or 0 in case this is the first file of a new transfer) to + // the total amount of already transferred bytes: + _status.TransferredBytesCompleted += _txCurFileSize; + // finally we can now update the _txCurFileSize variable: + _txCurFileSize = processed.Size; + Log.Trace("++++ new file transfer started: size [{0}], already transferred [{1}] ++++", + _txCurFileSize, _status.TransferredBytesCompleted); } } catch (Exception ex) { @@ -149,24 +157,36 @@ namespace ATxService _roboCommand.Stop(); Log.Debug("Transfer stopped"); _transferState = TxState.Stopped; + _status.TransferredBytesCompleted = 0; + _status.TransferredBytesCurrentFile = 0; + _txCurFileSize = 0; + _txCurFileProgress = 0; _roboCommand.Dispose(); _roboCommand = new RoboCommand(); _status.TransferInProgress = false; } /// <summary> - /// RoboSharp OnCopyProgressChanged callback handler. + /// RoboSharp OnCopyProgressChanged callback handler, triggered every time RoboCopy reports + /// a change in the copy progress. /// Print a log message if the progress has changed for more than 20%. /// </summary> private void RsProgressChanged(object sender, CopyProgressEventArgs e) { // e.CurrentFileProgress has the current progress in percent // report progress in steps of 20: var progress = ((int) e.CurrentFileProgress / 20) * 20; - if (progress == _txProgress) + if (progress == _txCurFileProgress) return; - _txProgress = progress; - Log.Debug("Transfer progress {0}%", progress); + _txCurFileProgress = progress; + _status.TransferredBytesCurrentFile = (long) (_txCurFileSize * e.CurrentFileProgress / 100); + var overallProgress = ((double)(_status.TransferredBytesCompleted + _status.TransferredBytesCurrentFile) / + _status.CurrentTransferSize) * 100; + Log.Info("Current transfer at {0:0}%", overallProgress); + Log.Trace("Tx progress: complete [{0}] - current [{1}] - combined {2:0}%", + _status.TransferredBytesCompleted, _status.TransferredBytesCurrentFile, + overallProgress); + Log.Trace("Current file transfer progress {0}%", progress); } #endregion -- GitLab