Somewhat large overhaul to move the kiosk towards using normal python
[kiosk.git] / health_renderer.py
index 774e0babc893967a83b9562f2f7e60e53de2d803..5416af2fb1bebb847af55f8522b9edfb27232624 100644 (file)
@@ -1,8 +1,9 @@
 #!/usr/bin/env python3
 
+import logging
 import os
 import time
-from typing import Dict, List
+from typing import Dict
 
 import constants
 import file_writer
@@ -10,12 +11,12 @@ import renderer
 import utils
 
 
-class periodic_health_renderer(renderer.debuggable_abstaining_renderer):
-    def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None:
-        super(periodic_health_renderer, self).__init__(name_to_timeout_dict, False)
+logger = logging.getLogger(__file__)
+
 
-    def debug_prefix(self) -> str:
-        return "health"
+class periodic_health_renderer(renderer.abstaining_renderer):
+    def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None:
+        super().__init__(name_to_timeout_dict)
 
     def periodic_render(self, key: str) -> bool:
         with file_writer.file_writer("periodic-health_6_300.html") as f:
@@ -52,7 +53,7 @@ class periodic_health_renderer(renderer.debuggable_abstaining_renderer):
                 timestamps + "last_backup_disk_selftest_long": days * 31,
                 timestamps + "last_cabin_disk_selftest_short": days * 14,
                 timestamps + "last_cabin_disk_selftest_long": days * 31,
-                timestamps + "last_cabin_rpi_ping": mins * 10,
+                timestamps + "last_cabin_rpi_ping": mins * 20,
                 timestamps + "last_healthy_wifi": mins * 10,
                 timestamps + "last_healthy_network": mins * 10,
                 timestamps + "last_scott_sync": days * 2,
@@ -61,27 +62,31 @@ class periodic_health_renderer(renderer.debuggable_abstaining_renderer):
 
             now = time.time()
             n = 0
-            for x in sorted(limits):
-                ts = os.stat(x).st_mtime
+            for filepath, limit_sec in sorted(limits.items()):
+                ts = os.stat(filepath).st_mtime
                 age = now - ts
-                self.debug_print("%s -- age is %ds, limit is %ds" % (x, age, limits[x]))
-                if age < limits[x]:
+                logger.debug(f"{filepath} -- age: {age}, limit {limit_sec}")
+                if age < limits[filepath]:
+                    # OK
                     f.write(
-                        '<TD BGCOLOR="#007010" HEIGHT=100 WIDTH=33% STYLE="text-size:60%; vertical-align: middle;">\n'
+                        '<TD BGCOLOR="#007010" HEIGHT=100 WIDTH=33% STYLE="text-size:70%; vertical-align: middle;">\n'
                     )
+                    txt_color="#ffffff"
                 else:
+                    # BAD!
                     f.write(
-                        '<TD BGCOLOR="#990000" HEIGHT=100 WIDTH=33% CLASS="invalid" STYLE="text-size:60%; vertical-align:middle;">\n'
+                        '<TD BGCOLOR="#990000" HEIGHT=100 WIDTH=33% CLASS="invalid" STYLE="text-size:70%; vertical-align:middle;">\n'
                     )
-                f.write("  <CENTER><FONT SIZE=-2>\n")
+                    txt_color="#000000"
+                f.write(f"  <CENTER><FONT SIZE=-1 COLOR={txt_color}>\n")
 
-                name = x.replace(timestamps, "")
+                name = filepath.replace(timestamps, "")
                 name = name.replace("last_", "")
                 name = name.replace("_", "&nbsp;")
-                ts = utils.describe_duration_briefly(age)
+                duration = utils.describe_duration_briefly(int(age))
 
-                self.debug_print(f"{name} is {ts} old.")
-                f.write(f"{name}<BR>\n<B>{ts}</B> old.\n")
+                logger.debug(f"{name} is {duration} old.")
+                f.write(f"{name}<BR>\n<B>{duration}</B> old.\n")
                 f.write("</FONT></CENTER>\n</TD>\n\n")
                 n += 1
                 if n % 3 == 0:
@@ -151,5 +156,5 @@ class periodic_health_renderer(renderer.debuggable_abstaining_renderer):
         )
 
 
-# test = periodic_health_renderer({"Test", 123})
-# test.periodic_render("Test")
+#test = periodic_health_renderer({"Test", 123})
+#test.periodic_render("Test")