aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/views/dashboard.py
blob: 26b0134171df68dd7cfdee29d88a287e174edc7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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,
    )