Show correct query timings, closes #2446

This commit is contained in:
Simon Willison 2026-09-16 21:23:54 -07:00
commit 9cdf95ac2c
2 changed files with 20 additions and 1 deletions

View file

@ -1,6 +1,7 @@
import asyncio
import itertools
import json
import time
import urllib
import urllib.parse
from dataclasses import dataclass, field
@ -1801,6 +1802,7 @@ async def table_view_traced(datasette, request):
context_for_html_hack = True
default_labels = True
start = time.perf_counter()
view_data = await table_view_data(
datasette,
request,
@ -1811,6 +1813,7 @@ async def table_view_traced(datasette, request):
)
if isinstance(view_data, Response):
return view_data
query_ms = (time.perf_counter() - start) * 1000
data, rows, columns, _expanded_columns, sql, next_url = view_data
# Handle formats from plugins
@ -1957,7 +1960,7 @@ async def table_view_traced(datasette, request):
resource=DatabaseResource(database=resolved.db.name),
actor=request.actor,
),
query_ms=1.2,
query_ms=query_ms,
select_templates=[
f"{'*' if template_name == template.name else ''}{template_name}"
for template_name in templates

View file

@ -1,5 +1,6 @@
import pathlib
import urllib.parse
from types import SimpleNamespace
import pytest
from bs4 import BeautifulSoup as Soup
@ -72,6 +73,21 @@ DEFAULT_EXPRESSION_OPTIONS = [
]
@pytest.mark.asyncio
@pytest.mark.parametrize("table", ("simple_primary_key", "simple_view"))
@pytest.mark.parametrize("duration", (0.125, 0.25))
async def test_table_footer_query_ms(ds_client, monkeypatch, table, duration):
times = iter((10.0, 10.0 + duration))
# Only mock the table view's clock, leaving SQL time limits unaffected.
monkeypatch.setattr(
"datasette.views.table.time", SimpleNamespace(perf_counter=lambda: next(times))
)
response = await ds_client.get(f"/fixtures/{table}")
assert response.status_code == 200
footer = Soup(response.text, "html.parser").find("footer")
assert f"Queries took {duration * 1000}ms" in footer.get_text()
@pytest.mark.asyncio
@pytest.mark.parametrize(
"path,expected_definition_sql",