From 375bf108ccf45b203b34dd81e0b4c68c1d316af2 Mon Sep 17 00:00:00 2001 From: Matthew Grotke Date: Mon, 1 Jun 2026 23:29:06 -0400 Subject: [PATCH] Development --- docker/routlin-dash/app/view_page.py | 51 ++++++++++------------------ 1 file changed, 17 insertions(+), 34 deletions(-) diff --git a/docker/routlin-dash/app/view_page.py b/docker/routlin-dash/app/view_page.py index 696c9fa..c772388 100644 --- a/docker/routlin-dash/app/view_page.py +++ b/docker/routlin-dash/app/view_page.py @@ -208,9 +208,9 @@ def live_dhcp_leases(): if obtained_ts is None: last_active = '-' elif obtained_ts <= now: - last_active = relative_time(obtained_ts) + last_active = relative_time(obtained_ts, now, short=True) + ' ago' elif renews_ts and renews_ts > now: - last_active = 'ETA ' + relative_time_future(renews_ts)[3:] + last_active = 'ETA ' + relative_time(renews_ts, now, short=True) else: last_active = 'ETA soon' mac_norm = parts[1].lower() @@ -231,7 +231,7 @@ def live_dhcp_leases(): 'vendor': _get_vendor(parts[1]), 'vlan_name': vlan_name, 'last_active': last_active, - 'renews': relative_time_future(renews_ts) if renews_ts else relative_time_future(expiry), + 'renews': 'in ' + relative_time(renews_ts or expiry, now), }) except Exception: pass @@ -257,41 +257,24 @@ def fmt_timestamp(ts): except Exception: return '-' -def relative_time(ts): +def relative_time(ts1, ts2, short=False): try: - diff = int(datetime.now(tz=timezone.utc).timestamp()) - int(ts) + diff = abs(int(ts1) - int(ts2)) if diff < 60: - n = max(0, diff) - return f'{n} second{"s" if n != 1 else ""} ago' + return f'{diff}s' if short else f'{diff} second{"s" if diff != 1 else ""}' m = diff // 60 if m < 60: - return f'{m} minute{"s" if m != 1 else ""} ago' - h = m // 60 - if h < 24: - return f'{h} hour{"s" if h != 1 else ""} ago' - d = h // 24 - if d < 365: - return f'{d} day{"s" if d != 1 else ""} ago' - y = d // 365 - return f'{y} year{"s" if y != 1 else ""} ago' - except Exception: - return '' - -def relative_time_future(ts): - try: - diff = int(ts) - int(datetime.now(tz=timezone.utc).timestamp()) - if diff <= 0: - return 'expired' - if diff < 60: - return f'in {diff} second{"s" if diff != 1 else ""}' - m = diff // 60 - if m < 60: - return f'in {m} minute{"s" if m != 1 else ""}' + return f'{m}m' if short else f'{m} minute{"s" if m != 1 else ""}' h, rem_m = divmod(m, 60) if h < 24: - return f'in {h}h {rem_m}m' if rem_m else f'in {h} hour{"s" if h != 1 else ""}' + if short: + return f'{h}h {rem_m}m' if rem_m else f'{h}h' + return f'{h}h {rem_m}m' if rem_m else f'{h} hour{"s" if h != 1 else ""}' d = h // 24 - return f'in {d} day{"s" if d != 1 else ""}' + if d < 365: + return f'{d}d' if short else f'{d} day{"s" if d != 1 else ""}' + y = d // 365 + return f'{y}y' if short else f'{y} year{"s" if y != 1 else ""}' except Exception: return '' @@ -561,7 +544,7 @@ def _blocklist_stats_html(cfg): entries = sum(1 for _ in f) mtime = int(os.path.getmtime(bl_path)) size_str = fmt_bytes(os.path.getsize(bl_path)) - last_refreshed = f'{datetime.fromtimestamp(mtime).strftime("%Y-%m-%d %H:%M")} ({relative_time(mtime)})' + last_refreshed = f'{datetime.fromtimestamp(mtime).strftime("%Y-%m-%d %H:%M")} ({relative_time(mtime, datetime.now(tz=timezone.utc).timestamp())} ago)' except Exception: entries, size_str, last_refreshed = '-', '-', 'Never' rows += ( @@ -696,7 +679,7 @@ def _public_ip_info(ddns_cfg): all_hosts.extend(p.get('hostnames', p.get('subdomains', []))) domains_sub = ', '.join(all_hosts) ip, mtime = _read_cached_ip() - last_obtained = f'Obtained: {relative_time(mtime)}' if mtime else '' + last_obtained = f'Obtained: {relative_time(mtime, datetime.now(tz=timezone.utc).timestamp())} ago' if mtime else '' if ip: return ip, domains_sub, '-', last_obtained return 'Offline', domains_sub, '-', '' @@ -704,7 +687,7 @@ def _public_ip_info(ddns_cfg): def _ddns_last_checked(): try: mtime = os.path.getmtime(f'{CONFIGS_DIR}/.ddns-last-service') - return f'Last checked: {relative_time(mtime)}' + return f'Last checked: {relative_time(mtime, datetime.now(tz=timezone.utc).timestamp())} ago' except OSError: return 'Last checked: ---'