From 6a99b5018d83b6220a96da85b9591cc6b8cfdf45 Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Fri, 19 Jan 2018 15:05:43 +0100
Subject: [PATCH] Use bytes for all sizes, introduce BytesToString for
 pretty-printing.

Fixes #24
---
 ATXCommon/Conv.cs                        | 16 +++++++++++++++-
 ATXCommon/FsUtils.cs                     |  8 ++++----
 ATXCommon/Serializables/ServiceConfig.cs |  4 +++-
 ATXCommon/SystemChecks.cs                | 10 +++++-----
 AutoTx/AutoTx.cs                         |  6 +++---
 AutoTx/RoboCommand.cs                    |  4 ++--
 6 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/ATXCommon/Conv.cs b/ATXCommon/Conv.cs
index 004a38c..b0516aa 100644
--- a/ATXCommon/Conv.cs
+++ b/ATXCommon/Conv.cs
@@ -3,6 +3,20 @@
     public static class Conv
     {
         public const int MegaBytes = 1024 * 1024;
-        public const int GigaBytes = 1024 * 1024 * 1024;
+
+        /// <summary>
+        /// Convert bytes into a human-readable string with the appropriate suffix (up to TB).
+        /// </summary>
+        /// <param name="numBytes">The number of bytes.</param>
+        /// <returns>A formatted string with the size showing one decimal.</returns>
+        public static string BytesToString(long numBytes) {
+            string[] suffixes = {"Bytes", "KB", "MB", "GB", "TB"};
+            var order = 0;
+            while (numBytes >= 1024 && order < suffixes.Length - 1) {
+                order++;
+                numBytes /= 1024;
+            }
+            return string.Format("{0:0.#} {1}", numBytes, suffixes[order]);
+        }
     }
 }
diff --git a/ATXCommon/FsUtils.cs b/ATXCommon/FsUtils.cs
index e153419..67e15c6 100644
--- a/ATXCommon/FsUtils.cs
+++ b/ATXCommon/FsUtils.cs
@@ -51,9 +51,9 @@ namespace ATXCommon
         /// <param name="thresh">The number of days used as expiration threshold.</param>
         /// <returns>A dictionary having usernames as keys (of those users that actually do have
         /// expired directories), where the values are lists of tuples with the DirInfo objects,
-        /// size and age (in days) of the expired directories.</returns>
-        public static Dictionary<string, List<Tuple<DirectoryInfo, long, int>>> ExpiredDirs(
-            DirectoryInfo baseDir,int thresh) {
+        /// size (in bytes) and age (in days) of the expired directories.</returns>
+        public static Dictionary<string, List<Tuple<DirectoryInfo, long, int>>>
+            ExpiredDirs(DirectoryInfo baseDir,int thresh) {
 
             var collection = new Dictionary<string, List<Tuple<DirectoryInfo, long, int>>>();
             var now = DateTime.Now;
@@ -65,7 +65,7 @@ namespace ATXCommon
                         continue;
                     long size = -1;
                     try {
-                        size = GetDirectorySize(subdir.FullName) / Conv.MegaBytes;
+                        size = GetDirectorySize(subdir.FullName);
                     }
                     catch (Exception ex) {
                         Log.Error("ERROR getting directory size of [{0}]: {1}",
diff --git a/ATXCommon/Serializables/ServiceConfig.cs b/ATXCommon/Serializables/ServiceConfig.cs
index 1e8a8f6..7e967dc 100644
--- a/ATXCommon/Serializables/ServiceConfig.cs
+++ b/ATXCommon/Serializables/ServiceConfig.cs
@@ -283,7 +283,9 @@ namespace ATXCommon.Serializables
             }
             foreach (var driveToCheck in SpaceMonitoring) {
                 msg += "Drive to check free space: " + driveToCheck.DriveName +
-                       " (threshold: " + driveToCheck.SpaceThreshold + ")" + "\n";
+                       " (threshold: " +
+                       Conv.BytesToString(driveToCheck.SpaceThreshold * Conv.MegaBytes) +
+                       ")" + "\n";
             }
             if (string.IsNullOrEmpty(SmtpHost)) {
                 msg += "SmtpHost: ====== Not configured, disabling email! ======" + "\n";
diff --git a/ATXCommon/SystemChecks.cs b/ATXCommon/SystemChecks.cs
index 29abc61..b3832f5 100644
--- a/ATXCommon/SystemChecks.cs
+++ b/ATXCommon/SystemChecks.cs
@@ -53,14 +53,14 @@ namespace ATXCommon
         }
 
         /// <summary>
-        /// Get the free space of a drive in megabytes.
+        /// Get the free space of a drive in bytes.
         /// </summary>
         /// /// <param name="drive">The drive name, e.g. "c:".</param>
-        /// <returns>Free space of a drive in megabytes, zero if an error occured.</returns>
+        /// <returns>Free space of a drive in bytes, zero if an error occured.</returns>
         public static long GetFreeDriveSpace(string drive) {
             try {
                 var dInfo = new DriveInfo(drive);
-                return dInfo.TotalFreeSpace / Conv.MegaBytes;
+                return dInfo.TotalFreeSpace;
             }
             catch (Exception ex) {
                 Log.Warn("Error in GetFreeDriveSpace({0}): {1}", drive, ex.Message);
@@ -80,8 +80,8 @@ namespace ATXCommon
                     continue;
 
                 msg += "Drive '" + driveToCheck.DriveName +
-                       "' - free space: " + freeSpace +
-                       "  (threshold: " + driveToCheck.SpaceThreshold + ")\n";
+                       "' - free space: " + Conv.BytesToString(freeSpace) +
+                       "  (threshold: " + Conv.BytesToString(driveToCheck.SpaceThreshold) + ")\n";
             }
             return msg;
         }
diff --git a/AutoTx/AutoTx.cs b/AutoTx/AutoTx.cs
index 1920273..2c6b400 100644
--- a/AutoTx/AutoTx.cs
+++ b/AutoTx/AutoTx.cs
@@ -289,7 +289,7 @@ namespace AutoTx
                    "Free system memory: " + SystemChecks.GetFreeMemory() + " MB" + "\n";
             foreach (var driveToCheck in _config.SpaceMonitoring) {
                 msg += "Free space on drive '" + driveToCheck.DriveName + "': " +
-                       SystemChecks.GetFreeDriveSpace(driveToCheck.DriveName) + "\n";
+                       Conv.BytesToString(SystemChecks.GetFreeDriveSpace(driveToCheck.DriveName)) + "\n";
             }
 
 
@@ -920,8 +920,8 @@ namespace AutoTx
             foreach (var userdir in expired.Keys) {
                 report += "\n - user '" + userdir + "'\n";
                 foreach (var subdir in expired[userdir]) {
-                    report += string.Format("   - {0} [age: {2} days, size: {1} MB]\n",
-                        subdir.Item1, subdir.Item2, subdir.Item3);
+                    report += string.Format("   - {0} [age: {2} days, size: {1}]\n",
+                        subdir.Item1, Conv.BytesToString(subdir.Item2), subdir.Item3);
                 }
             }
             if (string.IsNullOrEmpty(report))
diff --git a/AutoTx/RoboCommand.cs b/AutoTx/RoboCommand.cs
index e36b403..443282c 100644
--- a/AutoTx/RoboCommand.cs
+++ b/AutoTx/RoboCommand.cs
@@ -72,8 +72,8 @@ namespace AutoTx
                 _roboCommand.RetryOptions.RetryCount = 0;
                 _roboCommand.RetryOptions.RetryWaitTime = 2;
                 _roboCommand.Start();
-                Log.Info("Transfer started, total size: {0} MB",
-                    _status.CurrentTransferSize / Conv.MegaBytes);
+                Log.Info("Transfer started, total size: {0}",
+                    Conv.BytesToString(_status.CurrentTransferSize));
             }
             catch (ManagementException ex) {
                 Log.Error("Error in StartTransfer(): {0}", ex.Message);
-- 
GitLab