X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=health_renderer.py;h=3c4c30c02ce2a0b01e633f6bd759ead3285e57ce;hb=18913d6fecc43aeecfd5b617030e9cd3e840ef08;hp=74819a52327dccf0b73302188d043e95086466c9;hpb=5e241dc47e497c547463cecc07946ea6882835a7;p=kiosk.git diff --git a/health_renderer.py b/health_renderer.py index 74819a5..3c4c30c 100644 --- a/health_renderer.py +++ b/health_renderer.py @@ -1,98 +1,99 @@ +#!/usr/bin/env python3 + +import os +import time +from typing import Dict, List + import constants import file_writer -import os import renderer -import time +import utils class periodic_health_renderer(renderer.debuggable_abstaining_renderer): - def __init__(self, name_to_timeout_dict): + def __init__(self, name_to_timeout_dict: Dict[str, int]) -> None: super(periodic_health_renderer, self).__init__(name_to_timeout_dict, False) - def debug_prefix(self): + def debug_prefix(self) -> str: return "health" - 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 - minutes = mins - limits = { - timestamps + "last_http_probe_wannabe_house": mins * 10, - timestamps + "last_http_probe_meerkat_cabin": mins * 10, - timestamps + "last_http_probe_dns_house": mins * 10, - timestamps + "last_http_probe_rpi_cabin": mins * 10, - timestamps + "last_http_probe_rpi_house": mins * 10, - timestamps + "last_http_probe_therm_house": mins * 10, - timestamps + "last_rsnapshot_hourly": hours * 24, - timestamps + "last_rsnapshot_daily": days * 3, - timestamps + "last_rsnapshot_weekly": days * 14, - timestamps + "last_rsnapshot_monthly": days * 70, - 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_zfs_scrub": days * 9, - timestamps + "last_backup_zfs_scrub": days * 9, - timestamps + "last_cabin_zfs_scrub": days * 9, - 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_short": days * 14, - timestamps + "last_disk_selftest_long": days * 31, - timestamps + "last_backup_disk_selftest_short": days * 14, - 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_healthy_wifi": mins * 10, - timestamps + "last_healthy_network": mins * 10, - timestamps + "last_scott_sync": days * 2, - } - self.write_header(f) + def periodic_render(self, key: str) -> bool: + with file_writer.file_writer("periodic-health_6_300.html") as f: + timestamps = "/timestamps/" + days = constants.seconds_per_day + hours = constants.seconds_per_hour + mins = constants.seconds_per_minute + minutes = mins + limits = { + timestamps + "last_http_probe_wannabe_house": mins * 10, + timestamps + "last_http_probe_meerkat_cabin": mins * 10, + timestamps + "last_http_probe_dns_house": mins * 10, + timestamps + "last_http_probe_rpi_cabin": mins * 10, + timestamps + "last_http_probe_rpi_house": mins * 10, + timestamps + "last_http_probe_therm_house": mins * 10, + timestamps + "last_rsnapshot_hourly": hours * 24, + timestamps + "last_rsnapshot_daily": days * 3, + timestamps + "last_rsnapshot_weekly": days * 14, + timestamps + "last_rsnapshot_monthly": days * 70, + 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_zfs_scrub": days * 9, + timestamps + "last_backup_zfs_scrub": days * 9, + timestamps + "last_cabin_zfs_scrub": days * 9, + 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_short": days * 14, + timestamps + "last_disk_selftest_long": days * 31, + timestamps + "last_backup_disk_selftest_short": days * 14, + 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 * 20, + timestamps + "last_healthy_wifi": mins * 10, + timestamps + "last_healthy_network": mins * 10, + timestamps + "last_scott_sync": days * 2, + } + self.write_header(f) - 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( - '\n' - ) - else: - f.write( - '\n' - ) - f.write("
\n") + now = time.time() + n = 0 + for filepath, limit_sec in sorted(limits.items()): + ts = os.stat(filepath).st_mtime + age = now - ts + self.debug_print(f"{filepath} -- age: {age}, limit {limit_sec}") + if age < limits[filepath]: + # OK + f.write( + '\n' + ) + txt_color="#ffffff" + else: + # BAD! + f.write( + '\n' + ) + txt_color="#000000" + f.write(f"
\n") - name = x.replace(timestamps, "") - name = name.replace("last_", "") - name = name.replace("_", " ") - days = divmod(age, constants.seconds_per_day) - hours = divmod(days[1], constants.seconds_per_hour) - minutes = divmod(hours[1], constants.seconds_per_minute) + name = filepath.replace(timestamps, "") + name = name.replace("last_", "") + name = name.replace("_", " ") + duration = utils.describe_duration_briefly(int(age)) - self.debug_print( - "%s is %d days %02d:%02d old." % (name, days[0], hours[0], minutes[0]) - ) - f.write( - "%s
\n%d days %02d:%02d old.\n" - % (name, days[0], hours[0], minutes[0]) - ) - f.write("
\n\n\n") - n += 1 - if n % 3 == 0: - f.write("\n\n\n") - self.write_footer(f) - f.close() + self.debug_print(f"{name} is {duration} old.") + f.write(f"{name}
\n{duration} old.\n") + f.write("
\n\n\n") + n += 1 + if n % 3 == 0: + f.write("\n\n\n") + self.write_footer(f) return True - def write_header(self, f): + def write_header(self, f: file_writer.file_writer) -> None: f.write( """ @@ -144,7 +145,7 @@ class periodic_health_renderer(renderer.debuggable_abstaining_renderer): """ ) - def write_footer(self, f): + def write_footer(self, f: file_writer.file_writer) -> None: f.write( """ @@ -154,5 +155,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")