diff ServerMonitor/Objects/Checks/FileCheck.cs @ 4:3142e52cbe69

Lots more progress
author Brad Greco <brad@bgreco.net>
date Sun, 10 Feb 2019 20:51:26 -0500
parents 96f0b028176d
children 7127d5b5ac75
line wrap: on
line diff
--- a/ServerMonitor/Objects/Checks/FileCheck.cs	Fri Jan 11 22:34:18 2019 -0500
+++ b/ServerMonitor/Objects/Checks/FileCheck.cs	Sun Feb 10 20:51:26 2019 -0500
@@ -11,7 +11,7 @@
     [DisplayName("File check"), Description("Check file size or modified time"), DisplayWeight(12)]
     public class FileCheck : SshCheck
     {
-        public override string Command { get => string.Format(base.Command, Regex.Replace(File, "^~", "$HOME")); }
+        public override string Command => string.Format(base.Command, Regex.Replace(File, "^~", "$HOME"));
 
         public string File { get; set; }
 
@@ -19,12 +19,12 @@
 
         public bool FileSizeLessThan { get; set; }
 
-        public double FileSize { get; set; }
+        public int FileSize { get; set; }
 
         public double FileSizeInSelectedUnits
         {
-            get { return FileSize / Math.Pow(1024, (double)FileSizeUnits); }
-            set { FileSize = value * Math.Pow(1024, (double)FileSizeUnits); }
+            get => Math.Round(ConvertBytesToSelectedUnits(FileSize), 1);
+            set => FileSize = ConvertSelectedUnitsToBytes(value);
         }
 
         public SizeUnits FileSizeUnits { get; set; }
@@ -48,7 +48,7 @@
         protected override List<CheckResult> ProcessCommandResult(string output, int exitCode)
         {
             List<CheckResult> results = base.ProcessCommandResult(output, exitCode);
-            if (results.Any(r => r.CheckStatus != CheckStatus.Success))
+            if (results.Any(r => r.Failed))
                 return results;
 
             if (output.Split('\n').Length > 1)
@@ -62,7 +62,7 @@
                 {
                     if (int.TryParse(tokens[4], out int bytes))
                     {
-                        string message = string.Format("File size is {0} {1}", bytes / Math.Pow(1024, (double)FileSizeUnits), FileSizeUnits);
+                        string message = string.Format("File size is {0} {1}", Math.Round(ConvertBytesToSelectedUnits(bytes), 1), FileSizeUnits);
                         if ((bytes < FileSize && FileSizeLessThan) || (bytes > FileSize && !FileSizeLessThan))
                             results.Add(Pass(message));
                         else
@@ -110,5 +110,15 @@
                 message += "Date modified must be greater than 0." + Environment.NewLine;
             return message;
         }
+
+        private double ConvertBytesToSelectedUnits(int sizeInBytes)
+        {
+            return sizeInBytes / Math.Pow(1024, (double)FileSizeUnits);
+        }
+
+        private int ConvertSelectedUnitsToBytes(double sizeInSelectedUnits)
+        {
+            return (int)(sizeInSelectedUnits * Math.Pow(1024, (int)FileSizeUnits));
+        }
     }
 }
\ No newline at end of file