diff options
| author | Dennis Fink | 2026-11-18 22:07:58 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-18 22:07:58 +0100 |
| commit | 45d3d3fde75173dfbf27e08aae8501842a9511dd (patch) | |
| tree | 96926d2ff0111e838620afdc73a66a8172bfa6c8 /tests | |
| parent | c724c87eb5e23581732a275f1dd808da72932286 (diff) | |
| download | webmentions-ssg-45d3d3fde75173dfbf27e08aae8501842a9511dd.tar.gz webmentions-ssg-45d3d3fde75173dfbf27e08aae8501842a9511dd.zip | |
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.
Diffstat (limited to '')
| -rw-r--r-- | tests/models/test_sources.py | 70 | ||||
| -rw-r--r-- | tests/views/test_sources.py | 31 |
2 files changed, 101 insertions, 0 deletions
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", diff --git a/tests/views/test_sources.py b/tests/views/test_sources.py index a41c18c..8f5672c 100644 --- a/tests/views/test_sources.py +++ b/tests/views/test_sources.py @@ -659,6 +659,37 @@ def test_resend_preserves_source_filter_context( send.assert_called_once_with(mention_id) +def test_sources_show_sql_statistics_with_zero_webmentions( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + make_source( + app, + "mixed-stats", + statuses=( + SentWebmentionStatus.SENT, + SentWebmentionStatus.SENT, + SentWebmentionStatus.FAILED, + SentWebmentionStatus.NO_ENDPOINT, + ), + ) + make_source(app, "empty-stats", statuses=()) + + response = client.get("/sources", query_string={"sort": "path", "order": "asc"}) + assert response.status_code == HTTPStatus.OK + rows = BeautifulSoup(response.get_data(as_text=True), "html.parser").select( + "tbody tr" + ) + assert len(rows) == 2 + assert "0 Total" in rows[0].get_text(" ", strip=True) + assert " sent" not in rows[0].get_text(" ", strip=True) + mixed = rows[1].get_text(" ", strip=True) + assert "4 Total" in mixed + assert "2 sent" in mixed + assert "1 failed" in mixed + assert "awaiting retry" not in mixed + + def test_sources_empty_filter_result_keeps_rescan_modal( app: Flask, client: FlaskClient ) -> None: |
