Add invw.org
[kiosk.git] / health_renderer.py
index 55e4cf937d25aca755fa6264cc561bd06fc7baf2..01b0b6db1cb96da9bcbb89a99eebbf66963afa1b 100644 (file)
-import constants
-import file_writer
-import os
-import renderer
-import time
-
-class periodic_health_renderer(renderer.debuggable_abstaining_renderer):
-    def __init__(self, name_to_timeout_dict):
-        super(periodic_health_renderer, self).__init__(name_to_timeout_dict, False)
-
-    def debug_prefix(self):
-        return "health"
+#!/usr/bin/env python3
 
-    def periodic_render(self, key):
-        f = file_writer.file_writer('periodic-health_6_300.html')
-        timestamps = '/timestamps/'
-        days = constants.seconds_per_day
-        hours = constants.seconds_per_hour
-        mins = constants.seconds_per_minute
-        limits = {
-            timestamps + 'last_rsnapshot_hourly'          : hours * 24,
-            timestamps + 'last_rsnapshot_daily'           : days * 3,
-            timestamps + 'last_rsnapshot_weekly'          : days * 14,
-            timestamps + 'last_rsnapshot_monthly'         : days * 70,
+import logging
+import subprocess
+from typing import Dict
 
-            timestamps + 'last_zfssnapshot_hourly'        : hours * 5,
-            timestamps + 'last_zfssnapshot_daily'         : hours * 36,
-            timestamps + 'last_zfssnapshot_weekly'        : days * 9,
-            timestamps + 'last_zfssnapshot_monthly'       : days * 70,
-            timestamps + 'last_zfssnapshot_cleanup'       : hours * 24,
-
-            timestamps + 'last_disk_selftest_short'       : days * 14,
-            timestamps + 'last_disk_selftest_long'        : days * 31,
-            timestamps + 'last_zfs_scrub'                 : days * 9,
-            timestamps + 'last_zfs_scrub_backup'          : days * 9,
+import file_writer
+import renderer
 
-            timestamps + 'last_zfsxfer_backup.house'      : hours * 36,
-            timestamps + 'last_zfsxfer_ski.dyn.guru.org'  : days * 7,
-            timestamps + 'last_photos_sync'               : hours * 8,
-            timestamps + 'last_disk_selftest_backup_short': days * 14,
-            timestamps + 'last_disk_selftest_backup_long' : days * 31,
 
-            timestamps + 'last_healthy_wifi'              : mins * 10,
-            timestamps + 'last_healthy_network'           : mins * 10,
-            timestamps + 'last_scott_sync'                : days * 2,
-        }
-        self.write_header(f)
+logger = logging.getLogger(__name__)
 
-        now = time.time()
-        n = 0
-        for x in sorted(limits):
-            ts = os.stat(x).st_mtime
-            age = now - ts
-            self.debug_print("%s -- age is %ds, limit is %ds" % (x, age, limits[x]))
-            if age < limits[x]:
-                f.write('<TD BGCOLOR="#007010" HEIGHT=100 WIDTH=33% STYLE="text-size:60%; vertical-align: middle;">\n')
-            else:
-                f.write('<TD BGCOLOR="#990000" HEIGHT=100 WIDTH=33% CLASS="invalid" STYLE="text-size:60%; vertical-align:middle;">\n')
-            f.write("  <CENTER><FONT SIZE=-2>\n")
 
-            name = x.replace(timestamps, "")
-            name = name.replace("last_", "")
-            name = name.replace("_", "&nbsp;")
-            days = divmod(age, constants.seconds_per_day)
-            hours = divmod(days[1], constants.seconds_per_hour)
-            minutes = divmod(hours[1], constants.seconds_per_minute)
+class periodic_health_renderer(renderer.abstaining_renderer):
+    def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None:
+        super().__init__(name_to_timeout_dict)
 
-            self.debug_print("%s is %d days %02d:%02d old." % (
-                name, days[0], hours[0], minutes[0]))
-            f.write("%s<BR>\n<B>%d</b> days <B>%02d</B>:<B>%02d</B> old.\n" % (
-                name, days[0], hours[0], minutes[0]))
-            f.write("</FONT></CENTER>\n</TD>\n\n")
-            n += 1
-            if n % 3 == 0:
-                f.write("</TR>\n<TR>\n<!-- ------------------- -->\n")
-        self.write_footer(f)
-        f.close()
+    def periodic_render(self, key: str) -> bool:
+        with file_writer.file_writer("periodic-health_6_300.html") as f:
+            command = "/home/pi/bin/cronhealth.py --kiosk_mode"
+            p = subprocess.Popen(command, shell=True, bufsize=0, stdout=subprocess.PIPE)
+            for line in iter(p.stdout.readline, b""):
+                f.write(line.decode("utf-8"))
+            p.stdout.close()
         return True
 
-    def write_header(self, f):
-        f.write("""
-<HTML>
-<HEAD>
-<STYLE>
-@-webkit-keyframes invalid {
-  from { background-color: #ff6400; }
-  to { background-color: #ff0000; }
-  padding-right: 25px;
-  padding-left: 25px;
-}
-@-moz-keyframes invalid {
-  from { background-color: #ff6400; }
-  to { background-color: #ff0000; }
-  padding-right: 25px;
-  padding-left: 25px;
-}
-@-o-keyframes invalid {
-  from { background-color: #ff6400; }
-  to { background-color: #ff0000; }
-  padding-right: 25px;
-  padding-left: 25px;
-}
-@keyframes invalid {
-  from { background-color: #ff6400; }
-  to { background-color: #ff0000; }
-  padding-right: 25px;
-  padding-left: 25px;
-}
-.invalid {
-  -webkit-animation: invalid 1s infinite; /* Safari 4+ */
-  -moz-animation:    invalid 1s infinite; /* Fx 5+ */
-  -o-animation:      invalid 1s infinite; /* Opera 12+ */
-  animation:         invalid 1s infinite; /* IE 10+ */
-}
-</STYLE>
-<meta http-equiv="cache-control" content="max-age=0" />
-<meta http-equiv="cache-control" content="no-cache" />
-<meta http-equiv="expires" content="0" />
-<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
-<meta http-equiv="pragma" content="no-cache" />
-</HEAD>
-<BODY>
-<H1>Periodic Cronjob Health Report</H1>
-<HR>
-<CENTER>
-<TABLE BORDER=0 WIDTH=99% style="font-size:16pt">
-<TR>
-""")
-
-    def write_footer(self, f):
-        f.write("""
-</TR>
-</TABLE>
-</BODY>
-</HTML>""")
 
-#test = periodic_health_renderer({"Test", 123})
+# test = periodic_health_renderer({"Test", 123})
+# test.periodic_render("Test")