Sometimes sort tables by number of relationships, closes #460

This commit is contained in:
Simon Willison 2019-05-15 20:23:33 -07:00
commit faf33515b2
4 changed files with 36 additions and 4 deletions

View file

@ -30,6 +30,7 @@ from .utils import (
detect_spatialite,
escape_css_string,
escape_sqlite,
get_all_foreign_keys,
get_outbound_foreign_keys,
get_plugins,
module_from_path,
@ -261,6 +262,11 @@ class ConnectedDatabase:
)
return [r[0] for r in results.rows]
async def get_all_foreign_keys(self):
return await self.ds.execute_against_connection_in_thread(
self.name, get_all_foreign_keys
)
def __repr__(self):
tags = []
if self.is_mutable:

View file

@ -29,9 +29,7 @@ class DatabaseView(BaseView):
table_counts = await db.table_counts(5)
views = await db.view_names()
hidden_table_names = set(await db.hidden_table_names())
all_foreign_keys = await self.ds.execute_against_connection_in_thread(
database, get_all_foreign_keys
)
all_foreign_keys = await db.get_all_foreign_keys()
metadata = (self.ds.metadata("databases") or {}).get(database, {})
self.ds.update_with_inherited_metadata(metadata)

View file

@ -54,14 +54,27 @@ class IndexView(RenderMixin):
"fts_table": await self.ds.execute_against_connection_in_thread(
name, lambda conn: detect_fts(conn, table)
),
"num_relationships_for_sorting": 0,
}
if request.args.get("_sort") == "relationships" or not table_counts:
# We will be sorting by number of relationships, so populate that field
all_foreign_keys = await db.get_all_foreign_keys()
for table, foreign_keys in all_foreign_keys.items():
count = len(foreign_keys["incoming"] + foreign_keys["outgoing"])
tables[table]["num_relationships_for_sorting"] = count
hidden_tables = [t for t in tables.values() if t["hidden"]]
visible_tables = [t for t in tables.values() if not t["hidden"]]
tables_and_views_truncated = list(
sorted(
(t for t in tables.values() if t not in hidden_tables),
key=lambda t: t["count"] or 0,
key=lambda t: (
t["num_relationships_for_sorting"],
t["count"] or 0,
t["name"],
),
reverse=True,
)[:TRUNCATE_AT]
)

View file

@ -34,6 +34,21 @@ def test_homepage(app_client):
assert d["views_count"] == 4
def test_homepage_sort_by_relationships(app_client):
response = app_client.get("/.json?_sort=relationships")
assert response.status == 200
tables = [
t["name"] for t in response.json["fixtures"]["tables_and_views_truncated"]
]
assert [
"simple_primary_key",
"complex_foreign_keys",
"searchable_tags",
"foreign_key_references",
"facetable",
] == tables
def test_database_page(app_client):
response = app_client.get("/fixtures.json")
data = response.json