ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
Show per-app request rate, errors and latency
Juan Denisd2fd3b38b0d4
diff --git a/backend/app/api/bandwidth.py b/backend/app/api/bandwidth.pyindex 8f39598b..e1179298 100644--- a/backend/app/api/bandwidth.py+++ b/backend/app/api/bandwidth.py@@ -11,6 +11,7 @@ from app.error_reporting import unexpected_responsefrom ..middleware.rbac import admin_required, viewer_requiredfrom ..models import Applicationfrom ..services.bandwidth_service import BandwidthService+from ..services.request_metrics_service import RequestMetricsServicefrom ..services.resource_grant_service import ResourceGrantServicebandwidth_bp = Blueprint('bandwidth', __name__)@@ -51,6 +52,17 @@ def get_app_bandwidth(app_id):return unexpected_response(exc)+@bandwidth_bp.route('/apps/<int:app_id>/requests', methods=['GET'])+@viewer_required+def get_app_request_metrics(app_id):+ """Request rate, status classes, latency percentiles and cache hit ratio+ from the timed access log (plan 86 §A2). ``period``: 1h, 24h, 7d or 30d."""+ # Unknown/foreign app → NotFoundError (404); bad period → ValidationError+ # (400); both through the global handler.+ return jsonify(RequestMetricsService.series_for_user(+ get_current_user(), app_id, period=request.args.get('period', '24h')))++@bandwidth_bp.route('/aggregate', methods=['POST'])@admin_requireddef run_aggregate():diff --git a/backend/app/jobs/builtin_handlers.py b/backend/app/jobs/builtin_handlers.pyindex 5cf909e4..588435f1 100644--- a/backend/app/jobs/builtin_handlers.py+++ b/backend/app/jobs/builtin_handlers.py@@ -397,6 +397,15 @@ def run_disk_alert():return {'level': level} if level else None+def run_request_metrics():+ """Roll new access-log lines into per-app request metrics (plan 86 §A2).++ A builtin tick, so its once-a-minute successes get the 24 h tick retention+ rather than the full job retention."""+ from app.services.request_metrics_service import RequestMetricsService+ return RequestMetricsService.sample()++def run_security_feed_check():"""Daily security-advisory feed check.@@ -579,6 +588,7 @@ _BUILTINS = [('builtin.job_retention', run_job_retention, 'job-retention', 21600, 1500),('builtin.telemetry_retention', run_telemetry_retention, 'telemetry-retention', 21600, 1800),('builtin.disk_alert', run_disk_alert, 'disk-alert', 900, 240),+ ('builtin.request_metrics', run_request_metrics, 'request-metrics', 60, 90),# Fleet alert thresholds. Configurable since the Fleet page shipped, and# never evaluated until this row existed.('builtin.fleet_thresholds', run_fleet_threshold_checks, 'fleet-thresholds', FLEET_THRESHOLD_INTERVAL, 45),diff --git a/backend/app/models/__init__.py b/backend/app/models/__init__.pyindex 48584216..a9cfa9f6 100644--- a/backend/app/models/__init__.py+++ b/backend/app/models/__init__.py@@ -82,6 +82,7 @@ from app.models.login_link import LoginLinkfrom app.models.managed_database_user import ManagedDatabaseUserfrom app.models.site_import import SiteImportfrom app.models.site_bandwidth import SiteBandwidthDaily+from app.models.app_request_metric import AppRequestMetricfrom app.models.cron_run import CronRunfrom app.models.fleet_doctor_result import FleetDoctorResultfrom app.models.server_survey import ServerSurvey@@ -138,6 +139,7 @@ __all__ = ['SiteImport','CronRun','SiteBandwidthDaily',+ 'AppRequestMetric','FleetDoctorResult','ServerSurvey','DnsCutoverSnapshot',diff --git a/backend/app/models/app_request_metric.py b/backend/app/models/app_request_metric.pynew file mode 100644index 00000000..dd80be87--- /dev/null+++ b/backend/app/models/app_request_metric.py@@ -0,0 +1,66 @@+"""Per-app request rollups from the timed nginx access log (plan 86 §A2).++One row per (app, level, bucket). ``level`` is ``minute`` or ``hour``; the+sampler writes both on ingest, so downsampling is just a coarser bucket, not a+separate aggregation pass. Latency is a fixed-bucket histogram+(``RequestMetricsService.LATENCY_BUCKETS_MS``) rather than stored percentiles:+histograms merge exactly, so a minute read across two sampler ticks — and the+hour built from sixty minutes — still yields correct p50/p95/p99 to bucket+resolution. Pruned on every sampler tick (minute rows after 48h, hour rows+after 30 days).+"""+import json+from datetime import datetime++from app import db+++class AppRequestMetric(db.Model):+ __tablename__ = 'app_request_metrics'+ __table_args__ = (+ db.UniqueConstraint('app_id', 'level', 'bucket',+ name='uq_app_request_metrics_app_level_bucket'),+ db.Index('ix_app_request_metrics_level_bucket', 'level', 'bucket'),+ )++ id = db.Column(db.Integer, primary_key=True)+ app_id = db.Column(+ db.Integer,+ db.ForeignKey('applications.id', ondelete='CASCADE'),+ nullable=False,+ index=True,+ )+ level = db.Column(db.String(8), nullable=False)+ bucket = db.Column(db.DateTime, nullable=False)++ requests = db.Column(db.Integer, nullable=False, default=0)+ status_2xx = db.Column(db.Integer, nullable=False, default=0)+ status_3xx = db.Column(db.Integer, nullable=False, default=0)+ status_4xx = db.Column(db.Integer, nullable=False, default=0)+ status_5xx = db.Column(db.Integer, nullable=False, default=0)+ bytes_sent = db.Column(db.BigInteger, nullable=False, default=0)++ # Requests that carried a $request_time (timed-format lines only).+ timed_requests = db.Column(db.Integer, nullable=False, default=0)+ latency_sum_ms = db.Column(db.Float, nullable=False, default=0)+ latency_hist_json = db.Column(db.Text)++ cache_hit = db.Column(db.Integer, nullable=False, default=0)+ cache_miss = db.Column(db.Integer, nullable=False, default=0)+ cache_bypass = db.Column(db.Integer, nullable=False, default=0)+ cache_stale = db.Column(db.Integer, nullable=False, default=0)++ updated_at = db.Column(db.DateTime, default=datetime.utcnow,+ onupdate=datetime.utcnow)++ def get_hist(self):+ try:+ return [int(n) for n in json.loads(self.latency_hist_json or '[]')]+ except (TypeError, ValueError):+ return []++ def set_hist(self, hist):+ self.latency_hist_json = json.dumps([int(n) for n in hist])++ def __repr__(self):+ return f'<AppRequestMetric app={self.app_id} {self.level} {self.bucket}>'diff --git a/backend/app/services/request_metrics_service.py b/backend/app/services/request_metrics_service.pynew file mode 100644index 00000000..7ee5ca03--- /dev/null+++ b/backend/app/services/request_metrics_service.py@@ -0,0 +1,446 @@+"""Per-app request rate, errors and latency from nginx access logs (plan 86 §A2).++A one-minute builtin tick (``builtin.request_metrics``) tails each live app's+``/var/log/nginx/<app>.access.log`` from a stored byte offset. It never+re-parses a whole day, and it rolls the new lines up into+:class:`AppRequestMetric` rows at two levels:++ * ``minute`` rows, kept ``MINUTE_RETENTION_HOURS``+ * ``hour`` rows, kept ``HOUR_RETENTION_DAYS``++Both levels are written on ingest, and old rows are pruned on the same tick,+so the table's size is bounded from day one. Tables like this one filled the+disk before (plan 85).++Lines in the timed format (``NginxService.TIMED_LOG_FORMAT``) contribute+latency and cache status. Plain combined lines, from vhosts written before the+format existed, still count toward requests, status classes and bytes.++Cursor handling:+ * offsets live in one SystemSettings JSON row, keyed by path, with the+ file's inode;+ * on rotation (the inode changed), the tail of ``<log>.1`` is finished first+ when it is still the file we were reading, then the new file is read from 0;+ * on truncation (``copytruncate``) reading restarts from 0;+ * one tick reads at most ``MAX_READ_BYTES`` per file. A larger backlog skips+ ahead to the newest data, because a first run on a busy box must not+ stall the scheduler;+ * only complete lines are consumed. A half-written last line waits for the+ next tick.+"""+import logging+import math+import os+import re+from datetime import datetime, timedelta, timezone++from app import db++logger = logging.getLogger(__name__)++CURSOR_SETTING_KEY = 'request_metrics_cursors'++MINUTE_RETENTION_HOURS = 48+HOUR_RETENTION_DAYS = 30+MAX_READ_BYTES = 16 * 1024 * 1024++# Upper bounds (ms) of the latency histogram buckets; one extra overflow+# bucket follows the last bound. Percentiles are reported as the upper bound+# of the bucket the rank falls in.+LATENCY_BUCKETS_MS = (1, 2, 5, 10, 25, 50, 100, 250, 500,+ 1000, 2500, 5000, 10000, 30000)+HIST_LEN = len(LATENCY_BUCKETS_MS) + 1++_LINE_RE = re.compile(+ r'^\S+ \S+ \S+ \[(?P<time>[^\]]+)\] '+ r'"[^"]*" (?P<status>\d{3}) (?P<bytes>\d+|-)'+)+_TIMED_TAIL_RE = re.compile(+ r' rt=(?P<rt>[\d.]+|-) urt="[^"]*" cs=(?P<cs>\S+) h=\S+\s*$')++_MONTHS = {m: i for i, m in enumerate(+ ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',+ 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'), start=1)}++# $upstream_cache_status → the four counters kept per row.+_CACHE_CLASS = {+ 'HIT': 'cache_hit', 'REVALIDATED': 'cache_hit',+ 'MISS': 'cache_miss', 'EXPIRED': 'cache_miss',+ 'BYPASS': 'cache_bypass',+ 'STALE': 'cache_stale', 'UPDATING': 'cache_stale',+}++_COUNTERS = ('requests', 'status_2xx', 'status_3xx', 'status_4xx',+ 'status_5xx', 'bytes_sent', 'timed_requests',+ 'cache_hit', 'cache_miss', 'cache_bypass', 'cache_stale')++PERIODS = {+ '1h': ('minute', timedelta(hours=1)),+ '24h': ('minute', timedelta(hours=24)),+ '7d': ('hour', timedelta(days=7)),+ '30d': ('hour', timedelta(days=30)),+}+++def _parse_time(raw):+ """``'03/Jul/2026:10:00:07 +0200'`` → naive UTC datetime, or None."""+ try:+ day, mon, rest = raw.split('/', 2)+ year, hh, mm, ss_tz = rest.split(':', 3)+ ss, tz = ss_tz.split(' ')+ sign = -1 if tz[0] == '-' else 1+ offset = timedelta(hours=int(tz[1:3]), minutes=int(tz[3:5])) * sign+ local = datetime(int(year), _MONTHS[mon], int(day),+ int(hh), int(mm), int(ss))+ return local - offset+ except (ValueError, KeyError, IndexError):+ return None+++def parse_line(line):+ """One access-log line → dict, or None when it isn't a combined line.++ Keys: ``time`` (naive UTC), ``status``, ``bytes``, ``rt_ms`` (None for+ plain combined lines or ``-``), ``cache`` (upper-cased status or None).+ """+ match = _LINE_RE.match(line)+ if not match:+ return None+ when = _parse_time(match.group('time'))+ if when is None:+ return None+ raw_bytes = match.group('bytes')+ out = {+ 'time': when,+ 'status': int(match.group('status')),+ 'bytes': int(raw_bytes) if raw_bytes.isdigit() else 0,+ 'rt_ms': None,+ 'cache': None,+ }+ tail = _TIMED_TAIL_RE.search(line)+ if tail:+ rt = tail.group('rt')+ if rt != '-':+ out['rt_ms'] = float(rt) * 1000.0+ cs = tail.group('cs').upper()+ out['cache'] = cs if cs != '-' else None+ return out+++def hist_index(ms):+ for i, bound in enumerate(LATENCY_BUCKETS_MS):+ if ms <= bound:+ return i+ return len(LATENCY_BUCKETS_MS)+++def percentile(hist, p):+ """Upper bucket bound holding the ``p`` quantile (0 < p <= 1), or None.++ A rank in the overflow bucket reports the last bound — "at least this".+ """+ total = sum(hist)+ if total <= 0:+ return None+ rank = max(1, math.ceil(p * total))+ seen = 0+ for i, count in enumerate(hist):+ seen += count+ if seen >= rank:+ return LATENCY_BUCKETS_MS[min(i, len(LATENCY_BUCKETS_MS) - 1)]+ return LATENCY_BUCKETS_MS[-1]+++def _floor(when, level):+ if level == 'hour':+ return when.replace(minute=0, second=0, microsecond=0)+ return when.replace(second=0, microsecond=0)+++def _empty_acc():+ acc = {k: 0 for k in _COUNTERS}+ acc['latency_sum_ms'] = 0.0+ acc['hist'] = [0] * HIST_LEN+ return acc+++def _add_line(acc, rec):+ acc['requests'] += 1+ cls = rec['status'] // 100+ if 2 <= cls <= 5:+ acc[f'status_{cls}xx'] += 1+ acc['bytes_sent'] += rec['bytes']+ if rec['rt_ms'] is not None:+ acc['timed_requests'] += 1+ acc['latency_sum_ms'] += rec['rt_ms']+ acc['hist'][hist_index(rec['rt_ms'])] += 1+ counter = _CACHE_CLASS.get(rec['cache'] or '')+ if counter:+ acc[counter] += 1+++class RequestMetricsService:++ # ------------------------------------------------------------------ #+ # Reading new lines+ # ------------------------------------------------------------------ #++ @staticmethod+ def _read_from(path, offset):+ """Complete lines of ``path`` after ``offset``.++ Returns ``(lines, new_offset, skipped_bytes)``. At most+ ``MAX_READ_BYTES`` are read; a bigger backlog jumps ahead to the+ newest data and drops the partial line it lands in.+ """+ try:+ size = os.path.getsize(path)+ skipped = 0+ start = offset+ if size - start > MAX_READ_BYTES:+ skipped = size - MAX_READ_BYTES - start+ start = size - MAX_READ_BYTES+ with open(path, 'rb') as fh:+ fh.seek(start)+ data = fh.read(size - start)+ except OSError:+ return [], offset, 0+ if skipped:+ nl = data.find(b'\n')+ if nl < 0:+ return [], start, skipped+ skipped += nl + 1+ start += nl + 1+ data = data[nl + 1:]+ end = data.rfind(b'\n')+ if end < 0:+ return [], start, skipped+ chunk = data[:end + 1]+ lines = chunk.decode('utf-8', errors='replace').splitlines()+ return lines, start + len(chunk), skipped++ @classmethod+ def read_new_lines(cls, path, cursor):+ """New lines of one log since ``cursor`` (``{'ino', 'off'}`` or None).++ Returns ``(lines, new_cursor, skipped_bytes)``. ``new_cursor`` is None+ when the log does not exist.+ """+ try:+ st = os.stat(path)+ except OSError:+ return [], None, 0+ lines = []+ skipped = 0+ offset = 0+ if cursor:+ if cursor.get('ino') == st.st_ino:+ offset = int(cursor.get('off') or 0)+ if st.st_size < offset: # copytruncate+ offset = 0+ else:+ # Rotated: finish the old file if it is still at <log>.1.+ rotated = path + '.1'+ try:+ if os.stat(rotated).st_ino == cursor.get('ino'):+ old, _off, skip = cls._read_from(+ rotated, int(cursor.get('off') or 0))+ lines.extend(old)+ skipped += skip+ except OSError:+ pass+ new, new_off, skip = cls._read_from(path, offset)+ lines.extend(new)+ skipped += skip+ return lines, {'ino': st.st_ino, 'off': new_off}, skipped++ # ------------------------------------------------------------------ #+ # Ingest+ # ------------------------------------------------------------------ #++ @classmethod+ def rollup(cls, lines):+ """Lines → ``{(level, bucket): acc}`` plus an unparsable count."""+ out = {}+ bad = 0+ for line in lines:+ line = line.strip()+ if not line:+ continue+ rec = parse_line(line)+ if rec is None:+ bad += 1+ continue+ for level in ('minute', 'hour'):+ key = (level, _floor(rec['time'], level))+ acc = out.get(key)+ if acc is None:+ acc = out[key] = _empty_acc()+ _add_line(acc, rec)+ return out, bad++ @classmethod+ def _merge_into_db(cls, app_id, rolled):+ from app.models.app_request_metric import AppRequestMetric++ if not rolled:+ return 0+ buckets = {b for (_l, b) in rolled}+ existing = {+ (row.level, row.bucket): row+ for row in AppRequestMetric.query.filter(+ AppRequestMetric.app_id == app_id,+ AppRequestMetric.bucket.in_(buckets)).all()+ }+ for (level, bucket), acc in rolled.items():+ row = existing.get((level, bucket))+ if row is None:+ row = AppRequestMetric(app_id=app_id, level=level, bucket=bucket)+ for key in _COUNTERS:+ setattr(row, key, 0)+ row.latency_sum_ms = 0.0+ db.session.add(row)+ for key in _COUNTERS:+ setattr(row, key, int(getattr(row, key) or 0) + acc[key])+ row.latency_sum_ms = float(row.latency_sum_ms or 0) + acc['latency_sum_ms']+ hist = row.get_hist()+ if len(hist) != HIST_LEN:+ hist = [0] * HIST_LEN+ row.set_hist([a + b for a, b in zip(hist, acc['hist'])])+ return len(rolled)++ @classmethod+ def prune(cls, now=None):+ from app.models.app_request_metric import AppRequestMetric++ now = now or datetime.utcnow()+ pruned = AppRequestMetric.query.filter(+ AppRequestMetric.level == 'minute',+ AppRequestMetric.bucket < now - timedelta(hours=MINUTE_RETENTION_HOURS),+ ).delete(synchronize_session=False)+ pruned += AppRequestMetric.query.filter(+ AppRequestMetric.level == 'hour',+ AppRequestMetric.bucket < now - timedelta(days=HOUR_RETENTION_DAYS),+ ).delete(synchronize_session=False)+ return int(pruned or 0)++ @classmethod+ def sample(cls, log_dir=None, now=None):+ """One tick: read every live app's new log lines, roll up, prune."""+ from app.models.application import Application+ from app.models.system_settings import SystemSettings+ from app.services.nginx_service import NginxService++ log_dir = log_dir or NginxService.LOG_DIR+ cursors = SystemSettings.get(CURSOR_SETTING_KEY) or {}+ if not isinstance(cursors, dict):+ cursors = {}+ new_cursors = {}+ stats = {'apps': 0, 'lines': 0, 'unparsed': 0, 'rows': 0,+ 'skipped_bytes': 0}++ for app_row in Application.query_active().all():+ path = os.path.join(log_dir, f'{app_row.name}.access.log')+ lines, cursor, skipped = cls.read_new_lines(path, cursors.get(path))+ if cursor is None:+ continue+ new_cursors[path] = cursor+ stats['apps'] += 1+ stats['skipped_bytes'] += skipped+ if not lines:+ continue+ rolled, bad = cls.rollup(lines)+ stats['lines'] += len(lines)+ stats['unparsed'] += bad+ stats['rows'] += cls._merge_into_db(app_row.id, rolled)++ stats['pruned'] = cls.prune(now)+ # Only live apps' cursors are kept, so a deleted app's entry goes too.+ if new_cursors != cursors:+ SystemSettings.set(CURSOR_SETTING_KEY, new_cursors, value_type='json',+ description='Request-metrics log offsets (plan 86 §A2)')+ db.session.commit()+ if stats['skipped_bytes']:+ logger.info('request metrics skipped %d backlog bytes',+ stats['skipped_bytes'])+ return stats++ # ------------------------------------------------------------------ #+ # Read side+ # ------------------------------------------------------------------ #++ @classmethod+ def series_for_user(cls, user, app_id, period='24h'):+ """:meth:`series` scoped to apps ``user`` can see. A missing or foreign+ app is the same 404, so the route can't be used to probe app ids."""+ from app.exceptions import NotFoundError+ from app.models.application import Application+ from app.services.resource_grant_service import ResourceGrantService++ app_row = Application.query_active().filter_by(id=app_id).first()+ if app_row is None or not ResourceGrantService.can_access_app(user, app_row):+ raise NotFoundError('Not found')+ return cls.series(app_id, period=period)++ @classmethod+ def series(cls, app_id, period='24h', now=None):+ """Zero-filled series plus window totals for one app.++ Hit ratio counts STALE/UPDATING as served from cache and leaves BYPASS+ out: a bypassed request was never cacheable.+ """+ from app.models.app_request_metric import AppRequestMetric++ if period not in PERIODS:+ from app.exceptions import ValidationError+ raise ValidationError(f'period must be one of {", ".join(PERIODS)}')+ level, span = PERIODS[period]+ now = now or datetime.utcnow()+ step = timedelta(hours=1) if level == 'hour' else timedelta(minutes=1)+ end = _floor(now, level)+ start = end - span + step++ rows = {+ row.bucket: row+ for row in AppRequestMetric.query.filter(+ AppRequestMetric.app_id == int(app_id),+ AppRequestMetric.level == level,+ AppRequestMetric.bucket >= start,+ AppRequestMetric.bucket <= end).all()+ }++ totals = _empty_acc()+ points = []+ at = start+ while at <= end:+ row = rows.get(at)+ hist = row.get_hist() if row else []+ if len(hist) != HIST_LEN:+ hist = [0] * HIST_LEN+ point = {'t': at.replace(tzinfo=timezone.utc).isoformat()}+ for key in ('requests', 'status_4xx', 'status_5xx'):+ point[key] = int(getattr(row, key) or 0) if row else 0+ point['p95_ms'] = percentile(hist, 0.95)+ points.append(point)+ if row:+ for key in _COUNTERS:+ totals[key] += int(getattr(row, key) or 0)+ totals['latency_sum_ms'] += float(row.latency_sum_ms or 0)+ totals['hist'] = [a + b for a, b in zip(totals['hist'], hist)]+ at += step++ cached = totals['cache_hit'] + totals['cache_stale']+ cacheable = cached + totals['cache_miss']+ timed = totals['timed_requests']+ summary = {k: totals[k] for k in _COUNTERS}+ summary.update({+ 'error_rate': (totals['status_5xx'] / totals['requests']+ if totals['requests'] else None),+ 'avg_ms': totals['latency_sum_ms'] / timed if timed else None,+ 'p50_ms': percentile(totals['hist'], 0.50),+ 'p95_ms': percentile(totals['hist'], 0.95),+ 'p99_ms': percentile(totals['hist'], 0.99),+ 'cache_hit_ratio': cached / cacheable if cacheable else None,+ })+ return {'app_id': int(app_id), 'period': period, 'level': level,+ 'points': points, 'summary': summary}diff --git a/backend/migrations/versions/100_app_request_metrics.py b/backend/migrations/versions/100_app_request_metrics.pynew file mode 100644index 00000000..823880de--- /dev/null+++ b/backend/migrations/versions/100_app_request_metrics.py@@ -0,0 +1,48 @@+"""Per-app request rollups from the timed nginx access log (plan 86 §A2)."""+from alembic import op+import sqlalchemy as sa++revision = '100_app_request_metrics'+down_revision = '099_ai_management'+branch_labels = None+depends_on = None+++def upgrade():+ inspector = sa.inspect(op.get_bind())+ if 'app_request_metrics' in inspector.get_table_names():+ return+ op.create_table(+ 'app_request_metrics',+ sa.Column('id', sa.Integer, primary_key=True),+ sa.Column('app_id', sa.Integer,+ sa.ForeignKey('applications.id', ondelete='CASCADE'),+ nullable=False),+ sa.Column('level', sa.String(8), nullable=False),+ sa.Column('bucket', sa.DateTime, nullable=False),+ sa.Column('requests', sa.Integer, nullable=False, server_default='0'),+ sa.Column('status_2xx', sa.Integer, nullable=False, server_default='0'),+ sa.Column('status_3xx', sa.Integer, nullable=False, server_default='0'),+ sa.Column('status_4xx', sa.Integer, nullable=False, server_default='0'),+ sa.Column('status_5xx', sa.Integer, nullable=False, server_default='0'),+ sa.Column('bytes_sent', sa.BigInteger, nullable=False, server_default='0'),+ sa.Column('timed_requests', sa.Integer, nullable=False, server_default='0'),+ sa.Column('latency_sum_ms', sa.Float, nullable=False, server_default='0'),+ sa.Column('latency_hist_json', sa.Text),+ sa.Column('cache_hit', sa.Integer, nullable=False, server_default='0'),+ sa.Column('cache_miss', sa.Integer, nullable=False, server_default='0'),+ sa.Column('cache_bypass', sa.Integer, nullable=False, server_default='0'),+ sa.Column('cache_stale', sa.Integer, nullable=False, server_default='0'),+ sa.Column('updated_at', sa.DateTime),+ sa.UniqueConstraint('app_id', 'level', 'bucket',+ name='uq_app_request_metrics_app_level_bucket'),+ )+ op.create_index('ix_app_request_metrics_app_id', 'app_request_metrics', ['app_id'])+ op.create_index('ix_app_request_metrics_level_bucket', 'app_request_metrics',+ ['level', 'bucket'])+++def downgrade():+ op.drop_index('ix_app_request_metrics_level_bucket', table_name='app_request_metrics')+ op.drop_index('ix_app_request_metrics_app_id', table_name='app_request_metrics')+ op.drop_table('app_request_metrics')diff --git a/backend/tests/test_jobs.py b/backend/tests/test_jobs.pyindex ce41ebe1..2869ffbe 100644--- a/backend/tests/test_jobs.py+++ b/backend/tests/test_jobs.py@@ -191,18 +191,21 @@ class TestBuiltins:# Fleet alert thresholds. The evaluation existed and had no caller, so# thresholds set on the Fleet page were never checked against anything.assert 'builtin.fleet_thresholds' in kinds- assert len([k for k in kinds if k.startswith('builtin.')]) == 18+ # Per-app request rollups (plan 86 §A2): a builtin so its once-a-minute+ # successes get the 24 h tick retention.+ assert 'builtin.request_metrics' in kinds+ assert len([k for k in kinds if k.startswith('builtin.')]) == 19builtin_handlers.seed_builtin_schedules()- # 17 builtin.* schedules (incl. restore-point/job/telemetry retention,+ # 19 builtin.* schedules (incl. the request-metrics tick, restore-point/job/telemetry retention,# the monitor sweep, security-feed check, recycle-bin retention and the# fleet threshold check) + login-link/SSO reapers + drift/FIM/bandwidth# sweeps + the host doctor sweep AND the fleet doctor sweep (plan 26)# + the setup-health nag (plan 22).- assert ScheduledJob.query.count() == 26+ assert ScheduledJob.query.count() == 27# Seeding twice doesn't duplicate.builtin_handlers.seed_builtin_schedules()- assert ScheduledJob.query.count() == 26+ assert ScheduledJob.query.count() == 27class TestApi:diff --git a/backend/tests/test_request_metrics.py b/backend/tests/test_request_metrics.pynew file mode 100644index 00000000..32a557bd--- /dev/null+++ b/backend/tests/test_request_metrics.py@@ -0,0 +1,274 @@+"""Per-app request metrics from the timed access log (plan 86 §A2).++A fixture log gives exact counts and percentiles; the byte-offset cursor never+counts a line twice across ticks, rotation or truncation; and old rows are+pruned on the same tick that writes new ones (this table class has filled the+disk before — plan 85).+"""+import os+from datetime import datetime, timedelta++import pytest++from app import db+from app.models.app_request_metric import AppRequestMetric+from app.services import request_metrics_service as rms+from app.services.request_metrics_service import RequestMetricsService, parse_line+from tests.factories import make_application++NOW = datetime(2026, 7, 3, 10, 5, 0)+++def _line(minute=0, second=0, status=200, nbytes=100, rt=None, cs='-',+ tz='+0000', hour=10):+ base = (f'203.0.113.9 - - [03/Jul/2026:{hour:02d}:{minute:02d}:{second:02d} {tz}] '+ f'"GET / HTTP/1.1" {status} {nbytes} "-" "curl/8"')+ if rt is None:+ return base+ return base + f' rt={rt} urt="{rt}" cs={cs} h=shop.example.com'+++def _make_app(name='shop'):+ return make_application(db, name=name)+++def _rows(app_id, level):+ return (AppRequestMetric.query+ .filter_by(app_id=app_id, level=level)+ .order_by(AppRequestMetric.bucket).all())+++def _write(path, lines, mode='a'):+ with open(path, mode, newline='\n') as fh:+ fh.write(''.join(line + '\n' for line in lines))+++# ── parsing ──────────────────────────────────────────────────────────────────++class TestParseLine:+ def test_timed_line(self):+ rec = parse_line(_line(rt='0.250', cs='HIT', nbytes=512, status=404))+ assert rec['status'] == 404+ assert rec['bytes'] == 512+ assert rec['rt_ms'] == pytest.approx(250.0)+ assert rec['cache'] == 'HIT'+ assert rec['time'] == datetime(2026, 7, 3, 10, 0, 0)++ def test_plain_combined_line_counts_without_latency(self):+ rec = parse_line(_line())+ assert rec['status'] == 200 and rec['rt_ms'] is None and rec['cache'] is None++ def test_time_is_normalised_to_utc(self):+ rec = parse_line(_line(hour=12, tz='+0200', rt='0.001'))+ assert rec['time'] == datetime(2026, 7, 3, 10, 0, 0)++ def test_uncached_request_has_no_cache_status(self):+ assert parse_line(_line(rt='0.010', cs='-'))['cache'] is None++ def test_garbage_is_rejected(self):+ assert parse_line('not an access log line') is None+++class TestPercentile:+ def test_exact_bucket_bounds(self):+ # 90 fast (<=10ms) + 9 at 200ms + 1 at 3s+ hist = [0] * rms.HIST_LEN+ hist[rms.hist_index(8)] = 90+ hist[rms.hist_index(200)] = 9+ hist[rms.hist_index(3000)] = 1+ assert rms.percentile(hist, 0.50) == 10+ assert rms.percentile(hist, 0.95) == 250+ assert rms.percentile(hist, 0.99) == 250+ assert rms.percentile(hist, 1.0) == 5000++ def test_overflow_reports_the_last_bound(self):+ hist = [0] * rms.HIST_LEN+ hist[rms.hist_index(90_000)] = 1+ assert rms.percentile(hist, 0.5) == rms.LATENCY_BUCKETS_MS[-1]++ def test_empty_histogram(self):+ assert rms.percentile([0] * rms.HIST_LEN, 0.95) is None+++# ── rollup + ingest ──────────────────────────────────────────────────────────++FIXTURE = [+ _line(0, 1, 200, 100, '0.008', 'HIT'),+ _line(0, 2, 200, 100, '0.009', 'HIT'),+ _line(0, 3, 200, 100, '0.020', 'MISS'),+ _line(0, 4, 304, 0, '0.001', 'REVALIDATED'),+ _line(0, 5, 404, 50, '0.004', 'BYPASS'),+ _line(0, 6, 502, 10, '0.300', 'STALE'),+ _line(1, 0, 200, 100, '1.200', '-'),+ _line(1, 1, 500, 10, '0.050', 'EXPIRED'),+ _line(1, 2, 200, 100), # legacy combined line+ 'garbage',+]+++class TestSample:+ def test_fixture_log_gives_exact_counts_and_percentiles(self, app, tmp_path):+ site = _make_app()+ _write(tmp_path / 'shop.access.log', FIXTURE, 'w')++ stats = RequestMetricsService.sample(log_dir=str(tmp_path), now=NOW)++ assert stats['lines'] == 10 and stats['unparsed'] == 1+ m0, m1 = _rows(site.id, 'minute')+ assert m0.bucket == datetime(2026, 7, 3, 10, 0)+ assert (m0.requests, m0.status_2xx, m0.status_3xx, m0.status_4xx,+ m0.status_5xx) == (6, 3, 1, 1, 1)+ assert (m0.cache_hit, m0.cache_miss, m0.cache_bypass,+ m0.cache_stale) == (3, 1, 1, 1)+ assert m0.bytes_sent == 360+ assert (m1.requests, m1.timed_requests, m1.status_5xx,+ m1.cache_miss) == (3, 2, 1, 1)++ (hour,) = _rows(site.id, 'hour')+ assert hour.bucket == datetime(2026, 7, 3, 10, 0)+ assert hour.requests == 9 and hour.timed_requests == 8++ summary = RequestMetricsService.series(site.id, '1h', now=NOW)['summary']+ assert summary['requests'] == 9+ # timed latencies (ms): 1, 4, 8, 9, 20, 50, 300, 1200+ assert summary['p50_ms'] == 10+ assert summary['p95_ms'] == 2500+ assert summary['avg_ms'] == pytest.approx(1592 / 8)+ # (HIT+REVALIDATED 3 + STALE 1) / (4 + MISS/EXPIRED 2)+ assert summary['cache_hit_ratio'] == pytest.approx(4 / 6)+ assert summary['error_rate'] == pytest.approx(2 / 9)++ def test_a_second_tick_reads_only_new_lines(self, app, tmp_path):+ site = _make_app()+ log = tmp_path / 'shop.access.log'+ _write(log, [_line(0, 1, rt='0.010')], 'w')+ RequestMetricsService.sample(log_dir=str(tmp_path), now=NOW)+ RequestMetricsService.sample(log_dir=str(tmp_path), now=NOW)+ _write(log, [_line(0, 30, rt='0.010')])+ RequestMetricsService.sample(log_dir=str(tmp_path), now=NOW)++ (m0,) = _rows(site.id, 'minute')+ assert m0.requests == 2+ assert sum(m0.get_hist()) == 2++ def test_a_half_written_line_waits_for_the_next_tick(self, app, tmp_path):+ site = _make_app()+ log = tmp_path / 'shop.access.log'+ full = _line(0, 1, rt='0.010')+ with open(log, 'w', newline='\n') as fh:+ fh.write(full + '\n' + full[:20])+ RequestMetricsService.sample(log_dir=str(tmp_path), now=NOW)+ assert _rows(site.id, 'minute')[0].requests == 1++ with open(log, 'a', newline='\n') as fh:+ fh.write(full[20:] + '\n')+ RequestMetricsService.sample(log_dir=str(tmp_path), now=NOW)+ assert _rows(site.id, 'minute')[0].requests == 2++ def test_rotation_finishes_the_old_file_then_reads_the_new_one(self, app, tmp_path):+ site = _make_app()+ log = tmp_path / 'shop.access.log'+ _write(log, [_line(0, 1)], 'w')+ RequestMetricsService.sample(log_dir=str(tmp_path), now=NOW)+ _write(log, [_line(0, 2)]) # written before rotation+ os.replace(log, str(log) + '.1')+ _write(log, [_line(0, 3), _line(0, 4)], 'w')++ RequestMetricsService.sample(log_dir=str(tmp_path), now=NOW)+ assert _rows(site.id, 'minute')[0].requests == 4++ def test_truncation_restarts_from_zero(self, app, tmp_path):+ site = _make_app()+ log = tmp_path / 'shop.access.log'+ _write(log, [_line(0, 1), _line(0, 2), _line(0, 3)], 'w')+ RequestMetricsService.sample(log_dir=str(tmp_path), now=NOW)+ with open(log, 'r+') as fh: # copytruncate+ fh.truncate(0)+ _write(log, [_line(0, 4)])+ RequestMetricsService.sample(log_dir=str(tmp_path), now=NOW)+ assert _rows(site.id, 'minute')[0].requests == 4++ def test_a_backlog_bigger_than_the_cap_skips_to_the_newest_lines(+ self, app, tmp_path, monkeypatch):+ site = _make_app()+ line = _line(0, 1)+ monkeypatch.setattr(rms, 'MAX_READ_BYTES', len(line) * 3)+ _write(tmp_path / 'shop.access.log', [line] * 10, 'w')++ stats = RequestMetricsService.sample(log_dir=str(tmp_path), now=NOW)++ assert stats['skipped_bytes'] > 0+ # The cap window holds 3 lines; the first is partial and dropped.+ assert _rows(site.id, 'minute')[0].requests == 2++ def test_an_app_without_a_log_is_a_clean_no_op(self, app, tmp_path):+ _make_app()+ stats = RequestMetricsService.sample(log_dir=str(tmp_path), now=NOW)+ assert stats['apps'] == 0 and stats['rows'] == 0+++# ── retention ────────────────────────────────────────────────────────────────++def test_old_rows_are_pruned_on_the_same_tick(app, tmp_path):+ site = _make_app()+ old_minute = NOW - timedelta(hours=rms.MINUTE_RETENTION_HOURS, minutes=1)+ fresh_minute = NOW - timedelta(hours=1)+ old_hour = NOW - timedelta(days=rms.HOUR_RETENTION_DAYS, hours=1)+ fresh_hour = NOW - timedelta(days=rms.HOUR_RETENTION_DAYS - 1)+ for level, bucket in (('minute', old_minute), ('minute', fresh_minute),+ ('hour', old_hour), ('hour', fresh_hour),+ ('hour', old_minute)): # 48h-old hour row stays+ db.session.add(AppRequestMetric(app_id=site.id, level=level,+ bucket=bucket, requests=1))+ db.session.commit()++ stats = RequestMetricsService.sample(log_dir=str(tmp_path), now=NOW)++ assert stats['pruned'] == 2+ assert [r.bucket for r in _rows(site.id, 'minute')] == [fresh_minute]+ assert sorted(r.bucket for r in _rows(site.id, 'hour')) == [fresh_hour, old_minute]+++def test_the_sampler_is_a_builtin_tick():+ """Once a minute: it must get the 24 h tick retention, not 14-30 days."""+ from app.jobs.builtin_handlers import _BUILTINS+ (row,) = [b for b in _BUILTINS if b[2] == 'request-metrics']+ assert row[0].startswith('builtin.') and row[3] == 60+++# ── read side + API ──────────────────────────────────────────────────────────++def test_series_is_zero_filled(app, tmp_path):+ site = _make_app()+ _write(tmp_path / 'shop.access.log', FIXTURE, 'w')+ RequestMetricsService.sample(log_dir=str(tmp_path), now=NOW)++ data = RequestMetricsService.series(site.id, '1h', now=NOW)+ assert len(data['points']) == 60+ assert data['points'][-1]['t'].startswith('2026-07-03T10:05')+ by_t = {p['t'][:16]: p for p in data['points']}+ assert by_t['2026-07-03T10:00']['requests'] == 6+ assert by_t['2026-07-03T10:02']['requests'] == 0++ assert len(RequestMetricsService.series(site.id, '7d', now=NOW)['points']) == 168+++def test_series_rejects_an_unknown_period(app):+ with pytest.raises(ValueError):+ RequestMetricsService.series(1, '5y')+++def test_api(app, client, auth_headers):+ site = _make_app()+ ok = client.get(f'/api/v1/bandwidth/apps/{site.id}/requests?period=1h',+ headers=auth_headers)+ assert ok.status_code == 200+ assert ok.get_json()['summary']['requests'] == 0++ bad = client.get(f'/api/v1/bandwidth/apps/{site.id}/requests?period=5y',+ headers=auth_headers)+ assert bad.status_code == 400++ missing = client.get('/api/v1/bandwidth/apps/999999/requests',+ headers=auth_headers)+ assert missing.status_code == 404diff --git a/docs/API_SURFACE.md b/docs/API_SURFACE.mdindex 54a7a512..ca18f3c3 100644--- a/docs/API_SURFACE.md+++ b/docs/API_SURFACE.md@@ -210,6 +210,7 @@ Regenerate (backend/):- `GET /backups/target-types`- `GET /bandwidth/apps`- `GET /bandwidth/apps/{app_id}`+- `GET /bandwidth/apps/{app_id}/requests`- `GET /builds/apps/{app_id}/build-config`- `GET /builds/apps/{app_id}/build-logs`- `GET /builds/apps/{app_id}/build-logs/{timestamp}`diff --git a/frontend/src/components/service-detail/OverviewTab.jsx b/frontend/src/components/service-detail/OverviewTab.jsxindex cd353e73..961055de 100644--- a/frontend/src/components/service-detail/OverviewTab.jsx+++ b/frontend/src/components/service-detail/OverviewTab.jsx@@ -8,6 +8,7 @@ import { getDeployStatus, formatRelativeTime, formatDuration } from '../../utilsimport { formatBytes } from '../../utils/formatBytes';import BandwidthSparkline from '../BandwidthSparkline';import ScheduledTasksCard from '../ScheduledTasksCard';+import RequestMetricsCard from './RequestMetricsCard';import { KpiBand, MetricCard, Pill, Gauge, EnvTag, statusKind } from '@/components/ds';import { usePolling } from '@/hooks/usePolling';import { useTranslation } from 'react-i18next';@@ -326,6 +327,9 @@ const OverviewTab = ({ app, deployConfig }) => {</div>)}+ {/* Traffic (timed access log; hidden until requests exist) */}+ <RequestMetricsCard appId={app.id} />+{/* Recent Deployments */}<div className="overview-tab__card overview-tab__card--full"><div className="overview-tab__card-header-row">diff --git a/frontend/src/components/service-detail/RequestMetricsCard.jsx b/frontend/src/components/service-detail/RequestMetricsCard.jsxnew file mode 100644index 00000000..b1f0cf25--- /dev/null+++ b/frontend/src/components/service-detail/RequestMetricsCard.jsx@@ -0,0 +1,106 @@+import { useEffect, useState } from 'react';+import { useTranslation } from 'react-i18next';+import api from '../../services/api';+import { Sparkline } from '@/components/ds';+import { formatCompactNumber, formatNumber, formatPercent } from '@/utils/intl';++// Minute points folded into this many bins, so a 24h window draws 48 points.+const BINS = 48;++function fold(points, pick, combine) {+ if (!points.length) return [];+ const size = Math.max(1, Math.ceil(points.length / BINS));+ const out = [];+ for (let i = 0; i < points.length; i += size) {+ out.push(combine(points.slice(i, i + size).map(pick)));+ }+ return out;+}++const sum = (xs) => xs.reduce((a, b) => a + b, 0);+const max = (xs) => xs.reduce((a, b) => Math.max(a, b ?? 0), 0);++function formatMs(ms) {+ if (ms === null || ms === undefined) return '-';+ return ms >= 1000+ ? `${formatNumber(ms / 1000, { maximumFractionDigits: 1 })} s`+ : `${formatNumber(ms)} ms`;+}++// Requests / errors / latency / cache from the timed nginx log (plan 86 §A2).+// Hidden until the app has served a request in the window.+export default function RequestMetricsCard({ appId }) {+ const { t } = useTranslation();+ const [data, setData] = useState(null);++ useEffect(() => {+ let cancelled = false;+ api.getAppRequestMetrics(appId, '24h')+ .then((res) => { if (!cancelled) setData(res); })+ .catch(() => {});+ return () => { cancelled = true; };+ }, [appId]);++ const summary = data?.summary;+ if (!summary?.requests) return null;+ const points = data.points || [];++ const stats = [+ {+ key: 'requests',+ label: t('app.requestMetrics.requests', 'Requests'),+ value: formatCompactNumber(summary.requests),+ series: fold(points, (p) => p.requests, sum),+ },+ {+ key: 'errors',+ label: t('app.requestMetrics.errorRate', '5xx rate'),+ value: formatPercent((summary.error_rate ?? 0) * 100, { decimals: 1 }),+ series: fold(points, (p) => p.status_5xx, sum),+ tone: 'error',+ },+ {+ key: 'latency',+ label: t('app.requestMetrics.p95', 'p95 latency'),+ value: formatMs(summary.p95_ms),+ series: fold(points, (p) => p.p95_ms, max),+ },+ {+ key: 'cache',+ label: t('app.requestMetrics.cacheHit', 'Cache hit ratio'),+ value: summary.cache_hit_ratio === null+ ? t('app.requestMetrics.notCached', 'Not cached')+ : formatPercent(summary.cache_hit_ratio * 100),+ },+ ];++ return (+ <div className="overview-tab__card overview-tab__card--full request-metrics">+ <div className="overview-tab__card-header-row">+ <h3 className="overview-tab__card-title">+ {t('app.requestMetrics.title', 'Traffic')}+ </h3>+ <span className="request-metrics__window">+ {t('app.requestMetrics.last24h', 'Last 24 hours')}+ </span>+ </div>+ <div className="request-metrics__grid">+ {stats.map((stat) => (+ <div key={stat.key} className="request-metrics__stat">+ <span className="request-metrics__label">{stat.label}</span>+ <span className="request-metrics__value">{stat.value}</span>+ {stat.series?.some((v) => v > 0) && (+ <Sparkline+ data={stat.series}+ width={160}+ height={28}+ color={stat.tone === 'error' ? 'var(--red)' : undefined}+ className="request-metrics__spark"+ />+ )}+ </div>+ ))}+ </div>+ </div>+ );+}diff --git a/frontend/src/i18n/locales/en.json b/frontend/src/i18n/locales/en.jsonindex 805d26a9..b40a8a21 100644--- a/frontend/src/i18n/locales/en.json+++ b/frontend/src/i18n/locales/en.json@@ -4221,6 +4221,15 @@"repositoryUrl": "Repository URL","trackThisServiceInVersionControl": "Track this service in version control — push to deploy."},+ "requestMetrics": {+ "cacheHit": "Cache hit ratio",+ "errorRate": "5xx rate",+ "last24h": "Last 24 hours",+ "notCached": "Not cached",+ "p95": "p95 latency",+ "requests": "Requests",+ "title": "Traffic"+ },"requiresDocker": {"curlFsslHttpsGetDockerCom": "curl -fsSL https://get.docker.com | sh","dockerIsnTAvailable": "Docker isn't available",diff --git a/frontend/src/i18n/locales/es.json b/frontend/src/i18n/locales/es.jsonindex b9851fd9..4d919097 100644--- a/frontend/src/i18n/locales/es.json+++ b/frontend/src/i18n/locales/es.json@@ -6689,6 +6689,15 @@"telemetryRetentionHint": "Mensajes de cola, eventos del sistema y registros de uso de la API.","title": "Almacenamiento","usedOfTotal": "{{used}} usados de {{total}}"+ },+ "requestMetrics": {+ "cacheHit": "Tasa de aciertos de caché",+ "errorRate": "Tasa de 5xx",+ "last24h": "Últimas 24 horas",+ "notCached": "Sin caché",+ "p95": "Latencia p95",+ "requests": "Solicitudes",+ "title": "Tráfico"}},"auth": {diff --git a/frontend/src/services/api/bandwidth.js b/frontend/src/services/api/bandwidth.jsindex 0e07c6ca..40e24345 100644--- a/frontend/src/services/api/bandwidth.js+++ b/frontend/src/services/api/bandwidth.js@@ -15,3 +15,9 @@ export async function runBandwidthAggregate(day = null) {body: JSON.stringify(day ? { day } : {}),});}++// Per-app request rate, status classes, latency percentiles and cache hit+// ratio from the timed access log. period: '1h' | '24h' | '7d' | '30d'.+export async function getAppRequestMetrics(appId, period = '24h') {+ return this.request(`/bandwidth/apps/${appId}/requests?period=${encodeURIComponent(period)}`);+}diff --git a/frontend/src/styles/pages/_bandwidth.scss b/frontend/src/styles/pages/_bandwidth.scssindex a9518107..b0c1d2fa 100644--- a/frontend/src/styles/pages/_bandwidth.scss+++ b/frontend/src/styles/pages/_bandwidth.scss@@ -48,3 +48,40 @@color: var(--text-faint);}}++// Service detail Overview: Traffic card (requests / 5xx / p95 / cache hit+// from the timed access log). Component: service-detail/RequestMetricsCard.+.request-metrics {+ &__window {+ font-size: 12px;+ color: var(--text-dim);+ }++ &__grid {+ display: grid;+ grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));+ gap: 16px;+ }++ &__stat {+ display: flex;+ flex-direction: column;+ gap: 4px;+ min-width: 0;+ }++ &__label {+ font-size: 11.5px;+ color: var(--text-dim);+ }++ &__value {+ font-family: var(--mono);+ font-size: 18px;+ color: var(--text);+ }++ &__spark {+ max-width: 100%;+ }+}