From edd17c7ff89e0b908cbd62bdad7bd481dd79c325 Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Wed, 20 Dec 2017 00:41:55 +0100
Subject: [PATCH] Split CheckGraceLocation() into ExpiredDirs() and
 DirNameToAge().

Refers to #1
---
 AutoTx/AutoTx.cs | 76 ++++++++++++++++++++++++++++++++++--------------
 1 file changed, 54 insertions(+), 22 deletions(-)

diff --git a/AutoTx/AutoTx.cs b/AutoTx/AutoTx.cs
index 03a2e4c..20175a5 100644
--- a/AutoTx/AutoTx.cs
+++ b/AutoTx/AutoTx.cs
@@ -914,7 +914,7 @@ namespace AutoTx
         /// <summary>
         /// Recursively sum up size of all files under a given path.
         /// </summary>
-        /// <param name="path"></param>
+        /// <param name="path">Full path of the directory.</param>
         /// <returns>The total size in bytes.</returns>
         public static long GetDirectorySize(string path) {
             return new DirectoryInfo(path)
@@ -930,36 +930,68 @@ namespace AutoTx
         /// containing the age and size of those directories.
         /// </summary>
         public void CheckGraceLocation() {
-            var graceDir = new DirectoryInfo(Path.Combine(_managedPath, "DONE"));
+            var expired = ExpiredDirs(_config.GracePeriod);
             var report = "";
+            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);
+                }
+            }
+            writeLogDebug("Expired folders in grace location:\n" + report);
+        }
+
+        /// <summary>
+        /// Assemble a dictionary with information about expired directories.
+        /// </summary>
+        /// <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>
+        private Dictionary<string, List<Tuple<DirectoryInfo, long, int>>> ExpiredDirs(int thresh) {
+            var collection = new Dictionary<string, List<Tuple<DirectoryInfo, long, int>>>();
+            var graceDir = new DirectoryInfo(Path.Combine(_managedPath, "DONE"));
+            var now = DateTime.UtcNow;
             foreach (var userdir in graceDir.GetDirectories()) {
-                var expired = "";
+                var expired = new List<Tuple<DirectoryInfo, long, int>>();
                 foreach (var subdir in userdir.GetDirectories()) {
-                    DateTime timestamp;
+                    var age = DirNameToAge(subdir, now);
+                    if (age < thresh)
+                        continue;
+                    long size = -1;
                     try {
-                        timestamp = DateTime.ParseExact(subdir.Name,
-                            "yyyy-MM-dd__HH-mm-ss", CultureInfo.InvariantCulture);
+                        size = GetDirectorySize(subdir.FullName);
                     }
                     catch (Exception ex) {
-                        writeLogDebug("ERROR parsing timestamp from directory name '" +
-                            subdir.Name + "', skipping: " + ex.Message);
-                        continue;
+                        writeLog("ERROR getting directory size of " + subdir.FullName +
+                            " - " + ex.Message);
                     }
-                    var delta = DateTime.UtcNow - timestamp;
-                    if (delta.Days < _config.GracePeriod)
-                        continue;
-                    var size = GetDirectorySize(subdir.FullName) / MegaBytes;
-                    expired += "   - " + subdir + ": " + size + " MB (age: "
-                        + delta.Days + " days)\n";
+                    expired.Add(new Tuple<DirectoryInfo, long, int>(subdir, size, age));
                 }
-                if (string.IsNullOrWhiteSpace(expired))
-                    continue;
-                report += "\n - user '" + userdir + "':\n" + expired;
+                if (expired.Count > 0)
+                    collection.Add(userdir.Name, expired);
             }
-            if (string.IsNullOrWhiteSpace(report))
-                return;
-            writeLogDebug("Expired folders in grace location (" + graceDir.FullName + "):\n"
-                + report);
+            return collection;
+        }
+
+        /// <summary>
+        /// Convert the timestamp given by the NAME of a directory into the age in days.
+        /// </summary>
+        /// <param name="dir">The DirectoryInfo object to check for its name-age.</param>
+        /// <param name="baseTime">The DateTime object to compare to.</param>
+        /// <returns>The age in days, or -1 in case of an error.</returns>
+        private int DirNameToAge(DirectoryInfo dir, DateTime baseTime) {
+            DateTime dirTimestamp;
+            try {
+                dirTimestamp = DateTime.ParseExact(dir.Name, "yyyy-MM-dd__HH-mm-ss",
+                    CultureInfo.InvariantCulture);
+            }
+            catch (Exception ex) {
+                writeLogDebug("ERROR parsing timestamp from directory name '" +
+                              dir.Name + "', skipping: " + ex.Message);
+                return -1;
+            }
+            return (baseTime - dirTimestamp).Days;
         }
 
         #endregion
-- 
GitLab