fix/slow_author_interesting #126
@@ -5,6 +5,7 @@ Business logic for publication works.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from time import time
|
||||
from typing import List
|
||||
|
||||
import httpx
|
||||
@@ -14,6 +15,7 @@ from starlette.status import HTTP_502_BAD_GATEWAY
|
||||
from starlette.status import HTTP_404_NOT_FOUND
|
||||
from starlette.status import HTTP_503_SERVICE_UNAVAILABLE
|
||||
|
||||
from inse import config
|
||||
from inse.integrations import elasticsearch
|
||||
from inse.models.work import Work
|
||||
from inse.models.work_page import WorkPage
|
||||
@@ -100,34 +102,76 @@ async def get_primary_topic_counts(
|
||||
author_id: str,
|
||||
client: OpenAlexWorksSearchClient | None = None,
|
||||
) -> List[TopicCount]:
|
||||
works = await get_all_author_works(author_id, client=client)
|
||||
topic_counts = _get_primary_topic_counts_from_es(author_id)
|
||||
|
||||
counts = {}
|
||||
topic_ids = [item["id"] for item in topic_counts if item["id"]]
|
||||
details = _get_topic_details(topic_ids)
|
||||
|
||||
for work in works.results or []:
|
||||
primary_topic = work.primary_topic
|
||||
if not primary_topic or not primary_topic.display_name:
|
||||
result = []
|
||||
|
||||
for item in topic_counts:
|
||||
info = details.get(item["id"], {})
|
||||
name = info.get("display_name")
|
||||
if not name:
|
||||
continue
|
||||
|
||||
name = primary_topic.display_name
|
||||
if name not in counts:
|
||||
counts[name] = {"id": primary_topic.id, "name": name, "count": 0}
|
||||
counts[name]["count"] += 1
|
||||
|
||||
items = sorted(counts.values(), key=lambda item: item["count"], reverse=True)
|
||||
|
||||
topic_ids = [item["id"] for item in items if item["id"]]
|
||||
descriptions = _get_topic_descriptions(topic_ids)
|
||||
|
||||
return [
|
||||
TopicCount(
|
||||
id=item["id"],
|
||||
name=item["name"],
|
||||
count=item["count"],
|
||||
description=descriptions.get(item["id"]),
|
||||
result.append(
|
||||
TopicCount(
|
||||
id=item["id"],
|
||||
name=name,
|
||||
count=item["count"],
|
||||
description=info.get("description"),
|
||||
)
|
||||
)
|
||||
for item in items
|
||||
]
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def _get_primary_topic_counts_from_es(author_id: str) -> list[dict]:
|
||||
|
||||
indexed_author_id = f"https://openalex.org/{author_id.strip()}"
|
||||
|
||||
body = {
|
||||
"size": 0,
|
||||
"query": {
|
||||
"nested": {
|
||||
"path": "authorships",
|
||||
"query": {
|
||||
"bool": {
|
||||
"filter": [
|
||||
{"term": {"authorships.author.id": indexed_author_id}},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"aggs": {
|
||||
"primary_topics": {
|
||||
"terms": {"field": "primary_topic.id", "size": 1000},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
try:
|
||||
response = elasticsearch.search(
|
||||
index=config.settings.openalex_works_search_index,
|
||||
body=body,
|
||||
)
|
||||
except httpx.HTTPError as exc:
|
||||
logger.exception("OpenAlex primary topic aggregation request failed.")
|
||||
raise HTTPException(
|
||||
status_code=HTTP_502_BAD_GATEWAY,
|
||||
detail="OpenAlex works search request failed.",
|
||||
) from exc
|
||||
|
||||
aggregations = response.get("aggregations", {})
|
||||
buckets = aggregations.get("primary_topics", {}).get("buckets", [])
|
||||
|
||||
counts = []
|
||||
for bucket in buckets:
|
||||
counts.append({"id": bucket["key"], "count": bucket["doc_count"]})
|
||||
|
||||
return counts
|
||||
|
||||
|
||||
async def get_work(
|
||||
@@ -156,7 +200,7 @@ async def get_work(
|
||||
return elements[0]
|
||||
|
||||
|
||||
def _get_topic_descriptions(topic_ids: List[str]) -> dict[str, str]:
|
||||
def _get_topic_details(topic_ids: List[str]) -> dict[str, dict]:
|
||||
if not topic_ids:
|
||||
return {}
|
||||
|
||||
@@ -164,22 +208,26 @@ def _get_topic_descriptions(topic_ids: List[str]) -> dict[str, str]:
|
||||
response = elasticsearch.search(
|
||||
index="openalex-topics",
|
||||
query={"terms": {"id": topic_ids}},
|
||||
source=["id", "description"],
|
||||
source=["id", "display_name", "description"],
|
||||
size=len(topic_ids),
|
||||
)
|
||||
except httpx.HTTPError:
|
||||
logger.exception("Could not load topic descriptions from Elasticsearch.")
|
||||
logger.exception("Could not load topic details from Elasticsearch.")
|
||||
return {}
|
||||
|
||||
descriptions = {}
|
||||
details = {}
|
||||
for hit in response.get("hits", {}).get("hits", []):
|
||||
source = hit.get("_source", {})
|
||||
topic_id = source.get("id")
|
||||
description = source.get("description")
|
||||
if topic_id and description:
|
||||
descriptions[topic_id] = description
|
||||
if not topic_id:
|
||||
continue
|
||||
|
||||
return descriptions
|
||||
details[topic_id] = {
|
||||
"display_name": source.get("display_name"),
|
||||
"description": source.get("description"),
|
||||
}
|
||||
|
||||
return details
|
||||
|
||||
|
||||
def _limit_works_by_complete_years(
|
||||
|
||||
@@ -2,7 +2,10 @@
|
||||
<div class="interest-header">
|
||||
<h3>Interessant für</h3>
|
||||
@if (tags().length > 0) {
|
||||
<span class="interest-footnote">* Anzahl Publikationen zum Thema</span>
|
||||
<span class="interest-footnote">
|
||||
<mat-icon class="footnote-icon">article</mat-icon>
|
||||
Anzahl Publikationen zum Thema
|
||||
</span>
|
||||
}
|
||||
</div>
|
||||
<div class="interest-tags">
|
||||
@@ -16,7 +19,10 @@
|
||||
[disabled]="!tag.id"
|
||||
(click)="onTagClick(tag)">
|
||||
<span class="topic-name">{{ tag.label }}</span>
|
||||
<span class="topic-count">{{ tag.count }}*</span>
|
||||
<span class="topic-count">
|
||||
{{ tag.count }}
|
||||
<mat-icon class="count-icon">article</mat-icon>
|
||||
</span>
|
||||
</button>
|
||||
}
|
||||
} @else {
|
||||
|
||||
@@ -73,6 +73,7 @@
|
||||
flex: 0 0 auto;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 2px;
|
||||
min-width: 18px;
|
||||
height: 18px;
|
||||
padding: 0 5px;
|
||||
@@ -83,6 +84,13 @@
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.topic-count .count-icon {
|
||||
width: 13px;
|
||||
height: 13px;
|
||||
font-size: 13px;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.purple {
|
||||
--topic-border: #d8cafe;
|
||||
--topic-bg: #f6f0ff;
|
||||
@@ -114,10 +122,20 @@
|
||||
}
|
||||
|
||||
.interest-footnote {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
color: var(--sg-text-muted);
|
||||
font-size: 11px;
|
||||
}
|
||||
|
||||
.interest-footnote .footnote-icon {
|
||||
width: 14px;
|
||||
height: 14px;
|
||||
font-size: 14px;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.interest-tags .interest-empty {
|
||||
padding: 9px 16px;
|
||||
border: 1px solid #e2e8f0;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Component, input, computed, output } from '@angular/core';
|
||||
import { MatTooltipModule } from '@angular/material/tooltip';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { CommonModule } from '@angular/common';
|
||||
|
||||
import { TopicCount } from '../../api';
|
||||
@@ -16,7 +17,7 @@ const colors = ['purple', 'blue', 'green', 'orange', 'pink'];
|
||||
|
||||
@Component({
|
||||
selector: 'app-person-interesting',
|
||||
imports: [CommonModule, MatTooltipModule],
|
||||
imports: [CommonModule, MatTooltipModule, MatIconModule],
|
||||
templateUrl: './person-interesting.component.html',
|
||||
styleUrl: './person-interesting.component.scss',
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user