feat(patents): add server-side pagination and source filter to org_patents endpoint
LocationBasedServices/smartguide/pipeline/pr-dev This commit looks good
LocationBasedServices/smartguide/pipeline/pr-dev This commit looks good
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -6,5 +6,7 @@ export interface OrgPatentsResponse {
|
||||
institution_id: string;
|
||||
institution_name?: string | null;
|
||||
patent_count: number;
|
||||
page: number;
|
||||
size: number;
|
||||
patents: Array<OrgPatentEntry>;
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@
|
||||
</span>
|
||||
</td>
|
||||
<td class="cell-authors">
|
||||
@if (patent.matched_authors?.length) {
|
||||
@if (patent.matched_authors.length) {
|
||||
@for (author of patent.matched_authors.slice(0, 2); track author.openalex_author_id) {
|
||||
<a
|
||||
class="author-link"
|
||||
@@ -130,7 +130,7 @@
|
||||
}
|
||||
</div>
|
||||
|
||||
@if (selectedPatent.matched_authors?.length) {
|
||||
@if (selectedPatent.matched_authors.length) {
|
||||
<div class="detail-section">
|
||||
<h4>Verknüpfte Personen</h4>
|
||||
<ul class="tag-list tag-list--compact">
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user