From 45d3d3fde75173dfbf27e08aae8501842a9511dd Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Wed, 18 Nov 2026 22:07:58 +0100 Subject: perf(sender): aggregate source statistics in SQL Move sent Webmention counts from template iteration into a page-scoped SQL aggregate so source listings no longer load every related delivery row. Include zero-target sources and expose sent, no-endpoint, failed, and retrying counts directly to the template. --- tests/models/test_sources.py | 70 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'tests/models') diff --git a/tests/models/test_sources.py b/tests/models/test_sources.py index 10af3d6..100572f 100644 --- a/tests/models/test_sources.py +++ b/tests/models/test_sources.py @@ -98,6 +98,76 @@ def test_sent_webmention_pending_sql_matches_python(app: Flask) -> None: assert result.rowcount == 3 +def test_source_sent_webmention_statistics_sql(app: Flask) -> None: + """SQL counts every delivery state and includes sources with no targets.""" + now = datetime.now(UTC) + with app.app_context(): + source = SourceDocument( + path="stats/index.html", + url="https://site.example/stats/", + content_hash="0" * 32, + last_seen_at=now, + revised_at=now, + ) + empty = SourceDocument( + path="empty/index.html", + url="https://site.example/empty/", + content_hash="0" * 32, + last_seen_at=now, + revised_at=now, + ) + for index, (status, processed, attempts, retry_at) in enumerate( + [ + (SentWebmentionStatus.SENT, 1, 0, None), + (SentWebmentionStatus.SENT, 1, 0, None), + (SentWebmentionStatus.FAILED, None, 2, now), + (SentWebmentionStatus.NO_ENDPOINT, 1, 0, None), + (None, None, 1, None), + ] + ): + source.sent_webmentions.append( + SentWebmention( + target=f"https://target.example/{index}", + desired_revision=1, + processed_revision=processed, + status=status, + attempt_count=attempts, + next_retry_at=retry_at, + ) + ) + db.session.add_all([source, empty]) + db.session.commit() + + results = { + row.source_uuid: row + for row in db.session.execute( + SourceDocument.sent_webmention_statistics([source.uuid, empty.uuid]) + ) + } + assert set(results) == {source.uuid, empty.uuid} + assert ( + results[source.uuid].total, + results[source.uuid].sent, + results[source.uuid].failed, + results[source.uuid].retrying, + ) == (5, 2, 1, 1) + assert ( + results[empty.uuid].total, + results[empty.uuid].sent, + results[empty.uuid].failed, + results[empty.uuid].retrying, + ) == (0, 0, 0, 0) + + +def test_source_sent_webmention_statistics_is_scoped_to_page(app: Flask) -> None: + """An empty UUID selection must not aggregate unrelated sources.""" + with app.app_context(): + assert ( + list(db.session.execute(SourceDocument.sent_webmention_statistics([]))) + == [] + ) + + def test_source_repr() -> None: source = SourceDocument( path="example/index.html", -- cgit v1.3.1