# SPDX-FileCopyrightText: 2026 Dennis Fink # # SPDX-License-Identifier: BSD-3-Clause """A private, single-site overview of incoming and outgoing Webmentions.""" from datetime import UTC, datetime, timedelta from uuid import UUID import sqlalchemy as sa from flask import current_app, render_template from flask.typing import ResponseReturnValue from flask_login import login_required from ..extensions import db, huey from ..models.received import ReceivedWebmention, ReceivedWebmentionStatus from ..models.scan_state import ScanState from ..models.sources import SentWebmention, SentWebmentionStatus, SourceDocument from .blueprint import webmentions_bp def uuid7_lower_bound(at: datetime) -> UUID: """Get the smallest UUIDv7 with a creation time at or after ``at``. Received Webmentions store their creation time in their UUIDv7 primary key, not in a separate timestamp column. UUIDv7 orders by Unix milliseconds. """ milliseconds = int(at.timestamp() * 1000) return UUID(int=(milliseconds << 80) | (7 << 76)) @webmentions_bp.get("/") @login_required def index() -> ResponseReturnValue: """Show persisted site activity, delivery health, and worker queue depth.""" now = datetime.now(UTC) received_counts = dict( db.session.execute( sa.select(ReceivedWebmention.status, sa.func.count()).group_by( ReceivedWebmention.status ) ).all() ) sent_counts = dict( db.session.execute( sa.select(SentWebmention.status, sa.func.count()) .where(~SentWebmention.awaiting_processing) .group_by(SentWebmention.status) ).all() ) pending_sends = db.session.scalar( sa.select(sa.func.count()) .select_from(SentWebmention) .where(SentWebmention.awaiting_processing) ) # A claimed attempt also has a future deadline, but isn't necessarily a # retry. Only show failed attempts awaiting their next scheduled retry. waiting_retries = db.session.scalar( sa.select(sa.func.count()) .select_from(SentWebmention) .where( SentWebmention.awaiting_processing, SentWebmention.status == SentWebmentionStatus.FAILED, SentWebmention.next_retry_at > now, ) ) recent: dict[int, dict[str, int]] = {} for days in (7, 30): cutoff = now - timedelta(days=days) recent[days] = { "received": db.session.scalar( sa.select(sa.func.count()) .select_from(ReceivedWebmention) .where(ReceivedWebmention.uuid >= uuid7_lower_bound(cutoff)) ) or 0, "sent": db.session.scalar( sa.select(sa.func.count()) .select_from(SentWebmention) .where(SentWebmention.last_sent_at >= cutoff) ) or 0, } try: queue_size = huey.huey.pending_count() except Exception: current_app.logger.exception("Could not read Huey pending queue depth") queue_size = None scan = db.session.get(ScanState, 1) last_scan = scan.last_completed_at if scan is not None else None # SQLite drops timezone information when reading DateTime columns. if last_scan is not None and last_scan.tzinfo is None: last_scan = last_scan.replace(tzinfo=UTC) sources = db.session.scalar(sa.select(sa.func.count()).select_from(SourceDocument)) return render_template( "dashboard.html", title="Dashboard", received_counts={ status.value: received_counts.get(status, 0) for status in ReceivedWebmentionStatus }, received_total=sum(received_counts.values()), sent_counts={ status.value: sent_counts.get(status, 0) for status in SentWebmentionStatus }, sent_total=sum(sent_counts.values()) + (pending_sends or 0), not_attempted=sent_counts.get(None, 0), pending_sends=pending_sends or 0, waiting_retries=waiting_retries or 0, recent=recent, source_count=sources or 0, last_scan=last_scan, queue_size=queue_size, )