Skip to content

Commit 63cb00f

Browse files
Shaun Mahonyclaude
andcommitted
Link match result badges to source databases; sort groups by count
Match results: - Make dbId and dbSource badges clickable links to the motif's page in its source database (dbUrl) when available Database selector: - Fetch per-group motif counts via Motif aggregation in /api/databases - Sort groups by motif count (descending), vertebrates always first - Show motif count in parentheses on each group pill - Show only first 20 groups by default; "Show N more..." reveals rest - Add search input for databases with many groups (e.g. CIS-BP species) Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
1 parent bd4f413 commit 63cb00f

3 files changed

Lines changed: 307 additions & 110 deletions

File tree

web/src/app/api/databases/route.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { NextResponse } from "next/server";
22
import { connectDB } from "@/lib/db/mongoose";
33
import { ReferenceDatabase } from "@/lib/db/models/ReferenceDatabase";
4+
import { Motif } from "@/lib/db/models/Motif";
45

56
export async function GET() {
67
try {
@@ -10,15 +11,36 @@ export async function GET() {
1011
.sort({ source: 1, name: 1 })
1112
.lean();
1213

13-
const result = databases.map((db) => ({
14-
slug: db.slug,
15-
name: db.name,
16-
source: db.source,
17-
version: db.version || null,
18-
motifCount: db.motifCount,
19-
taxonGroups: db.taxonGroups,
20-
lastSyncedAt: db.lastSyncedAt,
21-
}));
14+
// Get per-group motif counts in a single aggregation
15+
const dbIds = databases.map((db) => db._id);
16+
const groupCounts = await Motif.aggregate([
17+
{ $match: { databaseRef: { $in: dbIds } } },
18+
{ $group: { _id: { db: "$databaseRef", group: "$group" }, count: { $sum: 1 } } },
19+
]);
20+
21+
// Build a lookup: dbId -> { groupName -> count }
22+
const countsByDb = new Map<string, Record<string, number>>();
23+
for (const row of groupCounts) {
24+
const dbKey = String(row._id.db);
25+
if (!countsByDb.has(dbKey)) countsByDb.set(dbKey, {});
26+
countsByDb.get(dbKey)![row._id.group] = row.count;
27+
}
28+
29+
const result = databases.map((db) => {
30+
const dbKey = String(db._id);
31+
const groupCountMap = countsByDb.get(dbKey) || {};
32+
33+
return {
34+
slug: db.slug,
35+
name: db.name,
36+
source: db.source,
37+
version: db.version || null,
38+
motifCount: db.motifCount,
39+
taxonGroups: db.taxonGroups,
40+
groupCounts: groupCountMap,
41+
lastSyncedAt: db.lastSyncedAt,
42+
};
43+
});
2244

2345
return NextResponse.json({ databases: result });
2446
} catch (error) {

0 commit comments

Comments
 (0)