Development

This commit is contained in:
Matthew Grotke 2026-06-01 23:29:06 -04:00
parent 1dce33d4eb
commit 375bf108cc

View file

@ -208,9 +208,9 @@ def live_dhcp_leases():
if obtained_ts is None: if obtained_ts is None:
last_active = '-' last_active = '-'
elif obtained_ts <= now: 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: 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: else:
last_active = 'ETA soon' last_active = 'ETA soon'
mac_norm = parts[1].lower() mac_norm = parts[1].lower()
@ -231,7 +231,7 @@ def live_dhcp_leases():
'vendor': _get_vendor(parts[1]), 'vendor': _get_vendor(parts[1]),
'vlan_name': vlan_name, 'vlan_name': vlan_name,
'last_active': last_active, '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: except Exception:
pass pass
@ -257,41 +257,24 @@ def fmt_timestamp(ts):
except Exception: except Exception:
return '-' return '-'
def relative_time(ts): def relative_time(ts1, ts2, short=False):
try: try:
diff = int(datetime.now(tz=timezone.utc).timestamp()) - int(ts) diff = abs(int(ts1) - int(ts2))
if diff < 60: if diff < 60:
n = max(0, diff) return f'{diff}s' if short else f'{diff} second{"s" if diff != 1 else ""}'
return f'{n} second{"s" if n != 1 else ""} ago'
m = diff // 60 m = diff // 60
if m < 60: if m < 60:
return f'{m} minute{"s" if m != 1 else ""} ago' return f'{m}m' if short else f'{m} minute{"s" if m != 1 else ""}'
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 ""}'
h, rem_m = divmod(m, 60) h, rem_m = divmod(m, 60)
if h < 24: 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 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: except Exception:
return '' return ''
@ -561,7 +544,7 @@ def _blocklist_stats_html(cfg):
entries = sum(1 for _ in f) entries = sum(1 for _ in f)
mtime = int(os.path.getmtime(bl_path)) mtime = int(os.path.getmtime(bl_path))
size_str = fmt_bytes(os.path.getsize(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: except Exception:
entries, size_str, last_refreshed = '-', '-', 'Never' entries, size_str, last_refreshed = '-', '-', 'Never'
rows += ( rows += (
@ -696,7 +679,7 @@ def _public_ip_info(ddns_cfg):
all_hosts.extend(p.get('hostnames', p.get('subdomains', []))) all_hosts.extend(p.get('hostnames', p.get('subdomains', [])))
domains_sub = ', '.join(all_hosts) domains_sub = ', '.join(all_hosts)
ip, mtime = _read_cached_ip() 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: if ip:
return ip, domains_sub, '-', last_obtained return ip, domains_sub, '-', last_obtained
return 'Offline', domains_sub, '-', '' return 'Offline', domains_sub, '-', ''
@ -704,7 +687,7 @@ def _public_ip_info(ddns_cfg):
def _ddns_last_checked(): def _ddns_last_checked():
try: try:
mtime = os.path.getmtime(f'{CONFIGS_DIR}/.ddns-last-service') 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: except OSError:
return 'Last checked: ---' return 'Last checked: ---'