Development

This commit is contained in:
Matthew Grotke 2026-06-09 21:49:29 -04:00
parent e4629fd164
commit 0806656d68
2 changed files with 12 additions and 11 deletions

View file

@ -8,29 +8,28 @@ import mod_dns_queries
from pages.ddns.view import public_ip_info from pages.ddns.view import public_ip_info
from pages.dhcpleases.view import live_dhcp_leases from pages.dhcpleases.view import live_dhcp_leases
METRICS_DB = f'{config_utils.CONFIGS_DIR}/.dns-metrics2' METRICS_DB = f'{config_utils.CONFIGS_DIR}/.dns-metrics'
def _fmt_since(since_str): def _fmt_since(since_str):
try: try:
dt = datetime.strptime(since_str, '%Y-%m-%d %H:%M:%S') dt = datetime.strptime(since_str, '%Y-%m-%d')
now = datetime.now() now = datetime.now()
rel = config_utils.relative_time(int(dt.timestamp()), int(now.timestamp())) rel = config_utils.relative_time(int(dt.timestamp()), int(now.timestamp()))
if dt.date() == now.date(): if dt.date() == now.date():
return f'Today at {dt.strftime("%H:%M")} ({rel} ago)' return 'Today'
return f'{dt.strftime("%Y-%m-%d")} ({rel} ago)' return f'{dt.strftime("%Y-%m-%d")} ({rel} ago)'
except Exception: except Exception:
return since_str return since_str
def _fmt_updated(updated_str): def _fmt_updated(updated_ts):
try: try:
dt = datetime.strptime(updated_str, '%Y-%m-%d %H:%M:%S')
now = datetime.now() now = datetime.now()
rel = config_utils.relative_time(int(dt.timestamp()), int(now.timestamp())) rel = config_utils.relative_time(int(updated_ts), int(now.timestamp()))
return f'{rel} ago' return f'{rel} ago'
except Exception: except Exception:
return updated_str return '-'
def _dns_providers_table(servers): def _dns_providers_table(servers):
@ -79,7 +78,7 @@ def load_dns_metrics(period=0):
con.execute('PRAGMA journal_mode=WAL') con.execute('PRAGMA journal_mode=WAL')
row = con.execute(f''' row = con.execute(f'''
SELECT SELECT
MIN(date), MAX(date), MIN(date), MAX(last_updated),
SUM(queries_forwarded), SUM(queries_answered_locally), SUM(queries_forwarded), SUM(queries_answered_locally),
SUM(queries_authoritative), SUM(cache_reused), MAX(tcp_hwm) SUM(queries_authoritative), SUM(cache_reused), MAX(tcp_hwm)
FROM daily_totals {where} FROM daily_totals {where}

View file

@ -20,7 +20,7 @@ from datetime import date
import mod_shared as shared import mod_shared as shared
import mod_validation as validation import mod_validation as validation
DB_FILE = shared.SCRIPT_DIR / ".dns-metrics2" DB_FILE = shared.SCRIPT_DIR / ".dns-metrics"
# =================================================================== # ===================================================================
@ -33,6 +33,7 @@ def open_db():
con.executescript(''' con.executescript('''
CREATE TABLE IF NOT EXISTS daily_totals ( CREATE TABLE IF NOT EXISTS daily_totals (
date TEXT PRIMARY KEY, date TEXT PRIMARY KEY,
last_updated INTEGER,
queries_forwarded INTEGER NOT NULL DEFAULT 0, queries_forwarded INTEGER NOT NULL DEFAULT 0,
queries_answered_locally INTEGER NOT NULL DEFAULT 0, queries_answered_locally INTEGER NOT NULL DEFAULT 0,
queries_authoritative INTEGER NOT NULL DEFAULT 0, queries_authoritative INTEGER NOT NULL DEFAULT 0,
@ -162,12 +163,13 @@ def update_metrics_db(new_metrics):
con.execute(''' con.execute('''
INSERT INTO daily_totals( INSERT INTO daily_totals(
date, date, last_updated,
queries_forwarded, queries_answered_locally, queries_authoritative, queries_forwarded, queries_answered_locally, queries_authoritative,
cache_reused, tcp_hwm, tcp_max_allowed, pool_memory_max, cache_reused, tcp_hwm, tcp_max_allowed, pool_memory_max,
dnssec_subqueries_hwm, dnssec_crypto_hwm, dnssec_sig_fails_hwm dnssec_subqueries_hwm, dnssec_crypto_hwm, dnssec_sig_fails_hwm
) VALUES (?,?,?,?,?,?,?,?,?,?,?) ) VALUES (?,strftime('%s','now'),?,?,?,?,?,?,?,?,?,?)
ON CONFLICT(date) DO UPDATE SET ON CONFLICT(date) DO UPDATE SET
last_updated = strftime('%s','now'),
queries_forwarded = queries_forwarded + excluded.queries_forwarded, queries_forwarded = queries_forwarded + excluded.queries_forwarded,
queries_answered_locally = queries_answered_locally + excluded.queries_answered_locally, queries_answered_locally = queries_answered_locally + excluded.queries_answered_locally,
queries_authoritative = queries_authoritative + excluded.queries_authoritative, queries_authoritative = queries_authoritative + excluded.queries_authoritative,