From 0873d25345dc21328a150dfc62fb3fd809597148 Mon Sep 17 00:00:00 2001 From: Khuder Khalti Date: Sun, 28 Jun 2026 13:58:23 +0200 Subject: [PATCH] feat(patents): add server-side pagination and source filter to org_patents endpoint --- .../inse/patents/org/elastic_search_client.py | 26 ++--- service/src/main/inse/patents/org/logic.py | 12 ++- service/src/main/inse/patents/org/model.py | 2 + service/src/main/inse/patents/org/service.py | 6 ++ webapp/src/app/api/api/patents.service.ts | 17 +++- .../src/app/api/model/orgPatentsResponse.ts | 2 + .../institution-patents.component.html | 4 +- .../institution-patents.component.ts | 99 +++++++++---------- 8 files changed, 95 insertions(+), 73 deletions(-) diff --git a/service/src/main/inse/patents/org/elastic_search_client.py b/service/src/main/inse/patents/org/elastic_search_client.py index 4266471b..c8ef2d8a 100644 --- a/service/src/main/inse/patents/org/elastic_search_client.py +++ b/service/src/main/inse/patents/org/elastic_search_client.py @@ -12,7 +12,7 @@ from inse import config logger = logging.getLogger(__name__) -DEFAULT_PAGE_SIZE = 500 +DEFAULT_PAGE_SIZE = 20 class OrgPatentsSearchClient: @@ -123,8 +123,10 @@ class OrgPatentsSearchClient: self, institution_id: str, match_type: MatchTypeFilter = "all", + page: int = 0, size: int = DEFAULT_PAGE_SIZE, - ) -> list[OrgPatentEntry]: + source: str | None = None, + ) -> tuple[list[OrgPatentEntry], int]: short_id = institution_id.replace("https://openalex.org/", "").upper() filters: list[dict[str, Any]] = [{"term": {"institution_id": short_id}}] @@ -135,10 +137,18 @@ class OrgPatentsSearchClient: elif match_type == "both": filters.append({"term": {"match_type": "both"}}) + if source in ("EPO", "USPTO"): + filters.append({"term": {"source": source}}) + payload = { + "from": page * size, "size": size, "track_total_hits": True, "query": {"bool": {"filter": filters}}, + "sort": [ + {"publication_year": {"order": "desc", "missing": "_last"}}, + {"patent_id": {"order": "asc"}}, + ], } async with httpx.AsyncClient( @@ -152,19 +162,13 @@ class OrgPatentsSearchClient: json=payload, ) if response.status_code == 404: - return [] + return [], 0 response.raise_for_status() data = response.json() + total = data.get("hits", {}).get("total", {}).get("value", 0) patents = [_hit_to_org_patent(hit) for hit in data.get("hits", {}).get("hits", [])] - patents.sort( - key=lambda patent: ( - int(patent.publication_year) if patent.publication_year and patent.publication_year.isdigit() else 0, - patent.patent_id, - ), - reverse=True, - ) - return patents + return patents, total def _hit_to_org_patent(hit: dict[str, Any]) -> OrgPatentEntry: diff --git a/service/src/main/inse/patents/org/logic.py b/service/src/main/inse/patents/org/logic.py index f242ed90..4e8f8308 100644 --- a/service/src/main/inse/patents/org/logic.py +++ b/service/src/main/inse/patents/org/logic.py @@ -383,6 +383,9 @@ async def index_org_patents_for_institution( async def get_org_patents_by_institution_id( institution_id: str, match_type: MatchTypeFilter = "all", + page: int = 0, + size: int = 20, + source: str | None = None, org_client: OrgPatentsSearchClient | None = None, institutions_client: InstitutionsSearchClient | None = None, ) -> OrgPatentsResponse: @@ -398,9 +401,12 @@ async def get_org_patents_by_institution_id( try: await org_patents_client.ensure_index() - patents = await org_patents_client.search_by_institution( + patents, total = await org_patents_client.search_by_institution( institution_id=short_id, match_type=match_type, + page=page, + size=size, + source=source, ) except MissingApiKeyError as exc: raise HTTPException( @@ -416,6 +422,8 @@ async def get_org_patents_by_institution_id( return OrgPatentsResponse( institution_id=short_id, institution_name=institution_name, - patent_count=len(patents), + patent_count=total, + page=page, + size=size, patents=patents, ) diff --git a/service/src/main/inse/patents/org/model.py b/service/src/main/inse/patents/org/model.py index e557aaf8..3de95d01 100644 --- a/service/src/main/inse/patents/org/model.py +++ b/service/src/main/inse/patents/org/model.py @@ -24,4 +24,6 @@ class OrgPatentsResponse(BaseModel): institution_id: str institution_name: str | None = None patent_count: int + page: int = 0 + size: int = 20 patents: list[OrgPatentEntry] diff --git a/service/src/main/inse/patents/org/service.py b/service/src/main/inse/patents/org/service.py index 46e7c708..df04d86b 100644 --- a/service/src/main/inse/patents/org/service.py +++ b/service/src/main/inse/patents/org/service.py @@ -30,9 +30,15 @@ async def get_org_patents_by_institution( MatchTypeFilter, Query(description="Filter patents by match type."), ] = "all", + page: int = 0, + size: int = 20, + source: str | None = None, ) -> OrgPatentsResponse: return await logic.get_org_patents_by_institution_id( institution_id=id, match_type=match_type, + page=page, + size=size, + source=source, institutions_client=client, ) diff --git a/webapp/src/app/api/api/patents.service.ts b/webapp/src/app/api/api/patents.service.ts index 8c8d1f41..ca79ad52 100644 --- a/webapp/src/app/api/api/patents.service.ts +++ b/webapp/src/app/api/api/patents.service.ts @@ -37,6 +37,9 @@ type GetPatentsByInstitutionOptions = { type GetOrgPatentsByInstitutionOptions = { matchType?: OrgPatentMatchTypeFilter; + page?: number; + size?: number; + source?: 'EPO' | 'USPTO'; httpHeaderAccept?: 'application/json'; context?: HttpContext; transferCache?: boolean; @@ -262,10 +265,16 @@ export class PatentsService extends BaseService { let localVarQueryParameters = new HttpParams(); if (options?.matchType) { - localVarQueryParameters = localVarQueryParameters.set( - 'match_type', - options.matchType, - ); + localVarQueryParameters = localVarQueryParameters.set('match_type', options.matchType); + } + if (options?.page !== undefined) { + localVarQueryParameters = localVarQueryParameters.set('page', String(options.page)); + } + if (options?.size !== undefined) { + localVarQueryParameters = localVarQueryParameters.set('size', String(options.size)); + } + if (options?.source) { + localVarQueryParameters = localVarQueryParameters.set('source', options.source); } let localVarHeaders = this.defaultHeaders; diff --git a/webapp/src/app/api/model/orgPatentsResponse.ts b/webapp/src/app/api/model/orgPatentsResponse.ts index 7cba08c5..eb884506 100644 --- a/webapp/src/app/api/model/orgPatentsResponse.ts +++ b/webapp/src/app/api/model/orgPatentsResponse.ts @@ -6,5 +6,7 @@ export interface OrgPatentsResponse { institution_id: string; institution_name?: string | null; patent_count: number; + page: number; + size: number; patents: Array; } diff --git a/webapp/src/app/modules/institution-patents/institution-patents.component.html b/webapp/src/app/modules/institution-patents/institution-patents.component.html index 0c13fff1..fe00f706 100644 --- a/webapp/src/app/modules/institution-patents/institution-patents.component.html +++ b/webapp/src/app/modules/institution-patents/institution-patents.component.html @@ -71,7 +71,7 @@ - @if (patent.matched_authors?.length) { + @if (patent.matched_authors.length) { @for (author of patent.matched_authors.slice(0, 2); track author.openalex_author_id) { - @if (selectedPatent.matched_authors?.length) { + @if (selectedPatent.matched_authors.length) {

Verknüpfte Personen

    diff --git a/webapp/src/app/modules/institution-patents/institution-patents.component.ts b/webapp/src/app/modules/institution-patents/institution-patents.component.ts index 56a2c251..8dd4be9e 100644 --- a/webapp/src/app/modules/institution-patents/institution-patents.component.ts +++ b/webapp/src/app/modules/institution-patents/institution-patents.component.ts @@ -37,7 +37,8 @@ export class InstitutionPatentsComponent implements OnInit { sortMode: SortMode = 'date'; selectedPage = 1; - readonly pageSize = 10; + readonly pageSize = 20; + totalPatents = 0; ngOnInit(): void { this.route.parent?.paramMap.subscribe((params) => { @@ -68,28 +69,20 @@ export class InstitutionPatentsComponent implements OnInit { this.activeFilter = filter; this.selectedPage = 1; this.selectedPatent = null; - this.applyFilter(); - this.updateQueryParams(); + this.reloadPatents(); } setMatchType(matchType: OrgPatentMatchTypeFilter): void { - if (this.activeMatchType === matchType) { - return; - } - + if (this.activeMatchType === matchType) return; this.activeMatchType = matchType; this.selectedPage = 1; this.selectedPatent = null; - - const institutionId = this.route.parent?.snapshot.paramMap.get('institutionId') ?? ''; - if (institutionId) { - this.loadPatents(institutionId); - } + this.reloadPatents(); } setSortMode(sortMode: SortMode): void { this.sortMode = sortMode; - this.applyFilter(); + this.filteredPatents = this.sortPatents(this.patents); this.updateQueryParams(); } @@ -103,6 +96,32 @@ export class InstitutionPatentsComponent implements OnInit { this.router.navigate(['/patent', this.selectedPatent.patent_id]); } + setPage(page: number): void { + this.selectedPage = Math.min(Math.max(page, 1), this.getPageCount()); + this.selectedPatent = null; + this.reloadPatents(); + } + + getPagedPatents(): OrgPatentEntry[] { + return this.filteredPatents; + } + + getPageCount(): number { + return Math.max(1, Math.ceil(this.totalPatents / this.pageSize)); + } + + hasPagination(): boolean { + return this.totalPatents > this.pageSize; + } + + private reloadPatents(): void { + const institutionId = this.institutionId ?? + this.route.parent?.snapshot.paramMap.get('institutionId') ?? ''; + if (institutionId) { + this.loadPatents(institutionId); + } + } + private loadPatents(institutionId: string): void { this.loading = true; this.error = false; @@ -110,12 +129,16 @@ export class InstitutionPatentsComponent implements OnInit { this.patentsApi .getOrgPatentsByInstitution(institutionId, 'body', false, { matchType: this.activeMatchType, + page: this.selectedPage - 1, + size: this.pageSize, + source: this.activeFilter !== 'all' ? this.activeFilter : undefined, }) .subscribe({ next: (result) => { this.patents = result.patents ?? []; + this.totalPatents = result.patent_count ?? 0; this.institutionName = result.institution_name ?? null; - this.applyFilter(); + this.filteredPatents = this.sortPatents(this.patents); this.loading = false; this.updateQueryParams(); }, @@ -126,23 +149,13 @@ export class InstitutionPatentsComponent implements OnInit { }); } - private applyFilter(): void { - let patents = this.patents; - - if (this.activeFilter !== 'all') { - patents = patents.filter((p) => p.source === this.activeFilter); - } - - this.filteredPatents = this.sortPatents(patents); - } - private sortPatents(patents: OrgPatentEntry[]): OrgPatentEntry[] { - return [...patents].sort((a, b) => { - if (this.sortMode === 'date') { - return (b.publication_date ?? '').localeCompare(a.publication_date ?? ''); - } - return (a.title ?? '').localeCompare(b.title ?? '', 'de', { sensitivity: 'base' }); - }); + if (this.sortMode === 'title') { + return [...patents].sort((a, b) => + (a.title ?? '').localeCompare(b.title ?? '', 'de', { sensitivity: 'base' }), + ); + } + return [...patents]; } private updateQueryParams(): void { @@ -152,6 +165,7 @@ export class InstitutionPatentsComponent implements OnInit { filter: this.activeFilter, sortBy: this.sortMode, matchType: this.activeMatchType, + page: this.selectedPage, }, queryParamsHandling: 'merge', replaceUrl: true, @@ -167,30 +181,7 @@ export class InstitutionPatentsComponent implements OnInit { } private isValidMatchType(value: string | null): value is OrgPatentMatchTypeFilter { - return ( - value === 'all' || - value === 'institution' || - value === 'name' || - value === 'both' - ); - } - - getPagedPatents(): OrgPatentEntry[] { - const start = (this.selectedPage - 1) * this.pageSize; - return this.filteredPatents.slice(start, start + this.pageSize); - } - - getPageCount(): number { - return Math.max(1, Math.ceil(this.filteredPatents.length / this.pageSize)); - } - - hasPagination(): boolean { - return this.getPageCount() > 1; - } - - setPage(page: number): void { - this.selectedPage = Math.min(Math.max(page, 1), this.getPageCount()); - this.updateQueryParams(); + return value === 'all' || value === 'institution' || value === 'name' || value === 'both'; } formatDate(date: string | null): string {