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']