aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/views
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--webmentions_ssg/views/__init__.py3
-rw-r--r--webmentions_ssg/views/dashboard.py123
-rw-r--r--webmentions_ssg/views/session.py9
3 files changed, 125 insertions, 10 deletions
diff --git a/webmentions_ssg/views/__init__.py b/webmentions_ssg/views/__init__.py
index a4954b6..5abaa14 100644
--- a/webmentions_ssg/views/__init__.py
+++ b/webmentions_ssg/views/__init__.py
@@ -4,10 +4,11 @@
"""Collect and expose the routes for the blueprint."""
-from . import endpoint, moderation, received, session, sources, user
+from . import dashboard, endpoint, moderation, received, session, sources, user
from .blueprint import webmentions_bp
__all__ = [
+ "dashboard",
"endpoint",
"moderation",
"received",
diff --git a/webmentions_ssg/views/dashboard.py b/webmentions_ssg/views/dashboard.py
new file mode 100644
index 0000000..26b0134
--- /dev/null
+++ b/webmentions_ssg/views/dashboard.py
@@ -0,0 +1,123 @@
+# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
+#
+# 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,
+ )
diff --git a/webmentions_ssg/views/session.py b/webmentions_ssg/views/session.py
index 78d1ed6..99c0a1c 100644
--- a/webmentions_ssg/views/session.py
+++ b/webmentions_ssg/views/session.py
@@ -17,15 +17,6 @@ from ..models.user import User
from .blueprint import webmentions_bp
-@webmentions_bp.route("/")
-def index() -> ResponseReturnValue:
- """Redirect to the received Webmentions view.
-
- :returns: Redirect response to the received Webmentions view.
- """
- return redirect(url_for("webmentions.received_list"))
-
-
@webmentions_bp.route("/login", methods=["GET", "POST"])
@limiter.limit(
lambda: current_app.config["LOGIN_USERNAME_RATE_LIMIT"],