Development

This commit is contained in:
Matthew Grotke 2026-06-09 21:40:14 -04:00
parent 0983e14de4
commit e4629fd164
4 changed files with 65 additions and 6 deletions

View file

@ -55,6 +55,27 @@ def upstreamdns_save():
return redirect(f'/{_PAGE}')
VALID_PERIODS = {0, 1, 7, 30, 60, 90, 365}
@bp.route('/action/dnsserver/metrics_period_save', methods=['POST'])
@auth.require_level('administrator')
def metrics_period_save():
try:
period = int(request.form.get('metrics_period', '0'))
except ValueError:
period = 0
if period not in VALID_PERIODS:
flash('Invalid period value.', 'error')
return redirect(f'/{_PAGE}')
if not config_utils.verify_config_hash(request.form.get('config_hash', '')):
flash('Configuration was modified by another session. Please refresh and try again.', 'error')
return redirect(f'/{_PAGE}')
cfg = config_utils.load_config()
cfg.setdefault('upstream_dns', {})['metrics_period'] = period
config_utils.save_config(cfg)
return redirect(f'/{_PAGE}')
@bp.route('/action/dnsserver/dnsforwarding_save', methods=['POST'])
@auth.require_level('administrator')
def dnsforwarding_save():

View file

@ -104,6 +104,10 @@
"type": "card",
"label": "DNS Statistics",
"items": [
{
"type": "raw_html",
"html": "%DNS_PERIOD_SELECTOR%"
},
{
"type": "grid",
"rows": [

View file

@ -1,7 +1,34 @@
import json
import config_utils
import factory
from pages.overview.view import load_dns_metrics, _dns_providers_table
_PERIOD_OPTIONS = [
(1, '1 Day'),
(7, '7 Days'),
(30, '30 Days'),
(60, '60 Days'),
(90, '90 Days'),
(365, '365 Days'),
(0, 'All Time'),
]
def _period_selector_html(current_period):
opts = ''.join(
f'<option value="{v}"{" selected" if v == current_period else ""}>{label}</option>'
for v, label in _PERIOD_OPTIONS
)
return (
'<form method="post" action="/action/dnsserver/metrics_period_save"'
' style="display:flex;align-items:center;gap:0.75rem;margin-bottom:1rem">'
'<label class="form-label" style="margin:0;white-space:nowrap">Period</label>'
f'<select name="metrics_period" class="form-input" style="width:auto"'
f' onchange="this.form.submit()">{opts}</select>'
f'<input type="hidden" name="config_hash" value="{factory.e(config_utils.config_hash())}"/>'
'</form>'
)
def collect_tokens(cfg):
tokens = config_utils.collect_layout_tokens(cfg)
@ -11,7 +38,10 @@ def collect_tokens(cfg):
tokens['DNS_CACHE_SIZE'] = str(dns.get('cache_size', '-'))
tokens['DNS_UPSTREAM_SERVERS_JSON'] = json.dumps(servers)
dns_stats = load_dns_metrics()
period = int(dns.get('metrics_period', 0))
dns_stats = load_dns_metrics(period=period)
tokens['DNS_PERIOD_SELECTOR'] = _period_selector_html(period)
tokens['DNS_METRICS_SINCE'] = dns_stats['since']
tokens['DNS_METRICS_UPDATED'] = dns_stats['updated']
tokens['DNS_STAT_QUERIES'] = dns_stats['queries']

View file

@ -63,7 +63,7 @@ def _dns_providers_table(servers):
)
def load_dns_metrics():
def load_dns_metrics(period=0):
import sqlite3
empty = {
'queries': '-', 'hits': '-', 'hit_rate': '-', 'forwarded': '-',
@ -71,16 +71,20 @@ def load_dns_metrics():
'updated': '-', 'since': '-', 'servers': [],
}
try:
where = (
f"WHERE date >= date('now','localtime','-{period - 1} days')"
if period and period > 0 else ''
)
con = sqlite3.connect(METRICS_DB, timeout=5)
con.execute('PRAGMA journal_mode=WAL')
row = con.execute('''
row = con.execute(f'''
SELECT
MIN(date), MAX(date),
SUM(queries_forwarded), SUM(queries_answered_locally),
SUM(queries_authoritative), SUM(cache_reused), MAX(tcp_hwm)
FROM daily_totals
FROM daily_totals {where}
''').fetchone()
srv_rows = con.execute('''
srv_rows = con.execute(f'''
SELECT
ds.address,
SUM(ds.queries_sent),
@ -90,7 +94,7 @@ def load_dns_metrics():
(SELECT avg_latency_ms FROM daily_servers d2
WHERE d2.address = ds.address AND d2.avg_latency_ms > 0
ORDER BY d2.date DESC LIMIT 1)
FROM daily_servers ds
FROM daily_servers ds {where}
GROUP BY ds.address
ORDER BY SUM(ds.queries_sent) DESC
''').fetchall()