Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Sort filtered collection page URLs #2384

Merged
merged 4 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions backend/btrixcloud/colls.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ async def list_urls_in_collection(
page_size: int = DEFAULT_PAGE_SIZE,
page: int = 1,
) -> Tuple[List[PageUrlCount], int]:
"""List all URLs in collection sorted desc by snapshot count"""
"""List all URLs in collection sorted desc by snapshot count unless prefix is specified"""
# pylint: disable=duplicate-code, too-many-locals, too-many-branches, too-many-statements
# Zero-index page for query
page = page - 1
Expand All @@ -764,13 +764,15 @@ async def list_urls_in_collection(
crawl_ids = await self.get_collection_crawl_ids(coll_id)

match_query: dict[str, object] = {"oid": oid, "crawl_id": {"$in": crawl_ids}}
sort_query: dict[str, int] = {"count": -1, "_id": 1}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tw4l I did a quick pass on this to get it working, feel free to push any suggestions directly!


if url_prefix:
url_prefix = urllib.parse.unquote(url_prefix)
regex_pattern = f"^{re.escape(url_prefix)}"
match_query["url"] = {"$regex": regex_pattern, "$options": "i"}
sort_query = {"_id": 1}

aggregate = [{"$match": match_query}]
aggregate: List[Dict[str, Union[int, object]]] = [{"$match": match_query}]

aggregate.extend(
[
Expand All @@ -781,7 +783,7 @@ async def list_urls_in_collection(
"count": {"$sum": 1},
},
},
{"$sort": {"count": -1}},
{"$sort": sort_query},
{"$set": {"url": "$_id"}},
{
"$facet": {
Expand Down
74 changes: 41 additions & 33 deletions frontend/src/features/collections/select-collection-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,44 +357,52 @@ export class SelectCollectionPage extends BtrixElement {

private renderSearchResults() {
return this.searchResults.render({
pending: () => html`
pending: () =>
this.renderItems(
// Render previous value so that dropdown doesn't shift while typing
this.searchResults.value,
),
complete: this.renderItems,
});
}

private readonly renderItems = (
results: SelectCollectionPage["searchResults"]["value"],
) => {
if (!results) return;

const { items } = results;

if (!items.length) {
return html`
<sl-menu-item slot="menu-item" disabled>
<sl-spinner></sl-spinner>
${msg("No matching page found.")}
</sl-menu-item>
`,
complete: ({ items }) => {
if (!items.length) {
return html`
<sl-menu-item slot="menu-item" disabled>
${msg("No matching page found.")}
</sl-menu-item>
`;
}
`;
}

return html`
${items.map((item: Page) => {
return html`
${items.map((item: Page) => {
return html`
<sl-menu-item
slot="menu-item"
@click=${async () => {
if (this.input) {
this.input.value = item.url;
}

this.selectedPage = this.formatPage(item);

this.combobox?.hide();

this.selectedSnapshot = this.selectedPage.snapshots[0];
}}
>${item.url}
</sl-menu-item>
`;
})}
<sl-menu-item
slot="menu-item"
@click=${async () => {
if (this.input) {
this.input.value = item.url;
}

this.selectedPage = this.formatPage(item);

this.combobox?.hide();

this.selectedSnapshot = this.selectedPage.snapshots[0];
}}
>${item.url}
</sl-menu-item>
`;
},
});
}
})}
`;
};

private readonly onSearchInput = debounce(400)(() => {
const value = this.input?.value;
Expand Down
Loading