Skip to content

compute top page origins for each collection #2483

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions backend/btrixcloud/colls.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,8 @@ async def update_collection_counts_and_tags(self, collection_id: UUID):

unique_page_count = await self.page_ops.get_unique_page_count(crawl_ids)

top_page_hosts = await self.page_ops.get_top_page_hosts(crawl_ids)

await self.collections.find_one_and_update(
{"_id": collection_id},
{
Expand All @@ -715,6 +717,7 @@ async def update_collection_counts_and_tags(self, collection_id: UUID):
"totalSize": total_size,
"tags": sorted_tags,
"preloadResources": preload_resources,
"topPageHosts": top_page_hosts,
}
},
)
Expand Down
2 changes: 1 addition & 1 deletion backend/btrixcloud/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
) = PageOps = BackgroundJobOps = object


CURR_DB_VERSION = "0043"
CURR_DB_VERSION = "0044"


# ============================================================================
Expand Down
12 changes: 12 additions & 0 deletions backend/btrixcloud/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,14 @@ class PreloadResource(BaseModel):
crawlId: str


# ============================================================================
class HostCount(BaseModel):
"""Host Count"""

host: str
count: int


# ============================================================================
class Collection(BaseMongoModel):
Copy link
Member

Choose a reason for hiding this comment

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

Should we include topPageOrigins in the base Collection model too, since we're storing it there? Might be useful for e.g. org import/export to have it on the base model.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, perhaps can rename this to domains internally too? We can strip out the https:// with the regex too, but maybe useful to keep since RWP does treat them separately..

Copy link
Member

Choose a reason for hiding this comment

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

Yeah not a bad idea to rename internally, and I would keep the prefix I think

"""Org collection structure"""
Expand Down Expand Up @@ -1515,6 +1523,8 @@ class CollOut(BaseMongoModel):
pagesQueryUrl: str = ""
downloadUrl: Optional[str] = None

topPageHosts: List[HostCount] = []


# ============================================================================
class PublicCollOut(BaseMongoModel):
Expand Down Expand Up @@ -1550,6 +1560,8 @@ class PublicCollOut(BaseMongoModel):

allowPublicDownload: bool = True

topPageHosts: List[HostCount] = []


# ============================================================================
class UpdateColl(BaseModel):
Expand Down
29 changes: 29 additions & 0 deletions backend/btrixcloud/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,35 @@ async def get_unique_page_count(self, crawl_ids: List[str]) -> int:
res = await cursor.to_list(1)
return res[0].get("urls") if res else 0

async def get_top_page_hosts(
self, crawl_ids: List[str]
) -> List[dict[str, str | int]]:
"""Get count of top page hosts across all archived items"""
cursor = self.pages.aggregate(
[
{"$match": {"crawl_id": {"$in": crawl_ids}}},
{
"$addFields": {
"host": {
"$regexFind": {
"input": "$url",
"regex": "^https?://([^/]+)",
}
}
}
},
{
"$group": {
"_id": {"$first": "$host.captures"},
"count": {"$count": {}},
}
},
{"$sort": {"count": -1}},
]
)
res = await cursor.to_list(10)
return [{"host": x.get("_id"), "count": x.get("count")} for x in res]

async def set_archived_item_page_counts(self, crawl_id: str):
"""Store archived item page and unique page counts in crawl document"""
page_count = await self.pages.count_documents({"crawl_id": crawl_id})
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/layouts/collections/metadataColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ export function metadataColumn(collection?: Collection | PublicCollection) {
label: metadata.totalSize,
render: (col) => `${localize.bytes(col.totalSize)}`,
})}
${metadataItem({
label: metadata.topPageHosts,
render: (col) =>
html` <table>
${col.topPageHosts.map(
(x) => html`
<tr>
<td>${x.host}</td>
<td class="pl-4">${x.count}</td>
</tr>
`,
)}
</table>`,
})}
</btrix-desc-list>
`;
}
1 change: 1 addition & 0 deletions frontend/src/strings/collections/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export const metadata = {
uniquePageCount: msg("Unique Pages in Collection"),
pageCount: msg("Total Pages Crawled"),
totalSize: msg("Collection Size"),
topPageHosts: msg("Top Page Hostnames"),
};
6 changes: 6 additions & 0 deletions frontend/src/types/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export const publicCollectionSchema = z.object({
crawlCount: z.number(),
uniquePageCount: z.number(),
pageCount: z.number(),
topPageHosts: z.array(
z.object({
host: z.string(),
count: z.number(),
}),
),
totalSize: z.number(),
allowPublicDownload: z.boolean(),
homeUrl: z.string().url().nullable(),
Expand Down
Loading