Skip to content

Commit

Permalink
Rename filtered_table_rows_count to count, refs #782
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Dec 31, 2022
1 parent a2dca62 commit 5bbe2bc
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
6 changes: 3 additions & 3 deletions datasette/templates/table.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "base.html" %}

{% block title %}{{ database }}: {{ table }}: {% if filtered_table_rows_count or filtered_table_rows_count == 0 %}{{ "{:,}".format(filtered_table_rows_count) }} row{% if filtered_table_rows_count == 1 %}{% else %}s{% endif %}{% endif %}{% if human_description_en %} {{ human_description_en }}{% endif %}{% endblock %}
{% block title %}{{ database }}: {{ table }}: {% if count or count == 0 %}{{ "{:,}".format(count) }} row{% if count == 1 %}{% else %}s{% endif %}{% endif %}{% if human_description_en %} {{ human_description_en }}{% endif %}{% endblock %}

{% block extra_head %}
{{- super() -}}
Expand Down Expand Up @@ -55,8 +55,8 @@ <h1>{{ metadata.title or table }}{% if is_view %} (view){% endif %}{% if private
</dl>
{% endif %}

{% if filtered_table_rows_count or human_description_en %}
<h3>{% if filtered_table_rows_count or filtered_table_rows_count == 0 %}{{ "{:,}".format(filtered_table_rows_count) }} row{% if filtered_table_rows_count == 1 %}{% else %}s{% endif %}{% endif %}
{% if count or human_description_en %}
<h3>{% if count or count == 0 %}{{ "{:,}".format(count) }} row{% if count == 1 %}{% else %}s{% endif %}{% endif %}
{% if human_description_en %}{{ human_description_en }}{% endif %}
</h3>
{% endif %}
Expand Down
12 changes: 6 additions & 6 deletions datasette/views/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,25 +539,25 @@ async def _gather_sequential(*args):
results = await db.execute(sql, params, truncate=True, **extra_args)

# Calculate the total count for this query
filtered_table_rows_count = None
count = None
if (
not db.is_mutable
and self.ds.inspect_data
and count_sql == f"select count(*) from {table_name} "
):
# We can use a previously cached table row count
try:
filtered_table_rows_count = self.ds.inspect_data[database_name][
count = self.ds.inspect_data[database_name][
"tables"
][table_name]["count"]
except KeyError:
pass

# Otherwise run a select count(*) ...
if count_sql and filtered_table_rows_count is None and not nocount:
if count_sql and count is None and not nocount:
try:
count_rows = list(await db.execute(count_sql, from_sql_params))
filtered_table_rows_count = count_rows[0][0]
count = count_rows[0][0]
except QueryInterrupted:
pass

Expand All @@ -584,7 +584,7 @@ async def _gather_sequential(*args):
params=params,
table=table_name,
metadata=table_metadata,
row_count=filtered_table_rows_count,
row_count=count,
)
)

Expand Down Expand Up @@ -853,7 +853,7 @@ async def table_actions():
"human_description_en": human_description_en,
"rows": rows[:page_size],
"truncated": results.truncated,
"filtered_table_rows_count": filtered_table_rows_count,
"count": count,
"expanded_columns": expanded_columns,
"expandable_columns": expandable_columns,
"columns": columns,
Expand Down
1 change: 1 addition & 0 deletions docs/json_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ looks like this::
"value": "Pinus radiata :: Monterey Pine"
}
],
"count": 195002,
"truncated": false,
"next": "100",
"next_url": "http://127.0.0.1:8001/sf-trees-02c8ef1/qSpecies.json?_next=100",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ def test_common_prefix_database_names(app_client_conflicting_database_names):

def test_inspect_file_used_for_count(app_client_immutable_and_inspect_file):
response = app_client_immutable_and_inspect_file.get("/fixtures/sortable.json")
assert response.json["filtered_table_rows_count"] == 100
assert response.json["count"] == 100


@pytest.mark.asyncio
Expand Down
6 changes: 3 additions & 3 deletions tests/test_table_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ async def test_page_size_zero(ds_client):
response = await ds_client.get("/fixtures/no_primary_key.json?_size=0")
assert response.status_code == 200
assert [] == response.json()["rows"]
assert 201 == response.json()["filtered_table_rows_count"]
assert 201 == response.json()["count"]
assert None is response.json()["next"]
assert None is response.json()["next_url"]

Expand Down Expand Up @@ -346,7 +346,7 @@ async def test_sortable_and_filtered(ds_client):
== response.json()["human_description_en"]
)
expected = [row for row in generate_sortable_rows(201) if "d" in row["content"]]
assert len(expected) == response.json()["filtered_table_rows_count"]
assert len(expected) == response.json()["count"]
expected.sort(key=lambda row: -row["sortable"])
assert [r["content"] for r in expected] == [r["content"] for r in fetched]

Expand Down Expand Up @@ -997,7 +997,7 @@ async def test_nocount(ds_client, nocount, expected_count):
if nocount:
path += "?_nocount=1"
response = await ds_client.get(path)
assert response.json()["filtered_table_rows_count"] == expected_count
assert response.json()["count"] == expected_count


def test_nocount_nofacet_if_shape_is_object(app_client_with_trace):
Expand Down

0 comments on commit 5bbe2bc

Please sign in to comment.