Update career stage backend thresholds
LocationBasedServices/smartguide/pipeline/head This commit looks good

This commit is contained in:
spsaefan
2026-06-19 14:46:42 +02:00
parent 5def7ac5f1
commit 123db12ac4
+13 -128
View File
@@ -188,17 +188,8 @@ def _fetch_works_and_topics(author_id: str) -> tuple[list, list]:
return works, topics
def _get_openalex_author_url(author_id: str) -> str:
author_id = author_id.strip()
if author_id.startswith("https://openalex.org/"):
return author_id
return f"https://openalex.org/{author_id}"
def _get_publication_years_for_author(author_id: str) -> list[int]:
author_url = _get_openalex_author_url(author_id)
author_url = f"https://openalex.org/{author_id.strip()}"
body = {
"size": 0,
@@ -246,7 +237,6 @@ def _get_publication_years_for_author(author_id: str) -> list[int]:
return publication_years
def _classify_career_stage(active_years: int) -> str:
if active_years <= 0:
return "unknown"
@@ -254,10 +244,10 @@ def _classify_career_stage(active_years: int) -> str:
if active_years <= 4:
return "Early Researcher"
if active_years <= 10:
if active_years <= 9:
return "Emerging Researcher"
if active_years <= 20:
if active_years <= 14:
return "Established Researcher"
return "Senior Researcher"
@@ -307,108 +297,11 @@ def _compute_career_metrics(publication_years: list[int]) -> CareerMetrics:
)
def _get_publication_years_for_authors(author_ids: list[str]) -> dict[str, list[int]]:
author_ids = list(dict.fromkeys(author_id for author_id in author_ids if author_id))
if not author_ids:
return {}
author_url_by_id = {
author_id: _get_openalex_author_url(author_id) for author_id in author_ids
}
body = {
"size": 0,
"query": {
"nested": {
"path": "authorships",
"query": {
"terms": {
"authorships.author.id": list(author_url_by_id.values())
},
},
},
},
"aggs": {
"authors": {
"filters": {
"filters": {
author_id: {
"nested": {
"path": "authorships",
"query": {
"term": {
"authorships.author.id": author_url
},
},
},
}
for author_id, author_url in author_url_by_id.items()
},
},
"aggs": {
"publication_years": {
"terms": {
"field": "publication_year",
"size": 1000,
},
},
},
},
},
}
try:
response = elasticsearch.search(
index=config.settings.openalex_works_search_index,
body=body,
)
except httpx.TimeoutException:
logger.warning(
"Batch career metrics Elasticsearch request timed out. "
"Returning empty career metrics for this search page."
)
return {author_id: [] for author_id in author_ids}
publication_years_by_author = {author_id: [] for author_id in author_ids}
author_buckets = (
response.get("aggregations", {})
.get("authors", {})
.get("buckets", {})
)
for author_id, author_bucket in author_buckets.items():
year_buckets = (
author_bucket.get("publication_years", {})
.get("buckets", [])
)
for year_bucket in year_buckets:
year = year_bucket.get("key")
count = year_bucket.get("doc_count", 0)
if isinstance(year, int):
publication_years_by_author[author_id].extend([year] * count)
return publication_years_by_author
def get_career_metrics_for_author(author_id: str) -> CareerMetrics:
publication_years = _get_publication_years_for_author(author_id)
return _compute_career_metrics(publication_years)
def get_career_metrics_for_authors(author_ids: list[str]) -> dict[str, CareerMetrics]:
publication_years_by_author = _get_publication_years_for_authors(author_ids)
return {
author_id: _compute_career_metrics(
publication_years_by_author.get(author_id, [])
)
for author_id in author_ids
}
def _build_papers(works: list) -> list:
papers = []
@@ -793,25 +686,17 @@ def advanced_search_persons(
raw_data = util.get_persons_data(hit)
custom_author = _enrich_author_data(raw_data)
if custom_author:
results.append(custom_author)
if include_career_metrics and results:
author_ids = [custom_author.id for custom_author in results if custom_author.id]
start_time = time.time()
career_metrics_by_author = get_career_metrics_for_authors(author_ids)
elapsed_time = time.time() - start_time
logger.info(
f"Batch career metrics calculation for {len(author_ids)} authors took "
f"{elapsed_time:.2f} seconds"
)
for custom_author in results:
if custom_author.id:
custom_author.career_metrics = career_metrics_by_author.get(
custom_author.id, CareerMetrics()
if include_career_metrics:
start_time = time.time()
custom_author.career_metrics = get_career_metrics_for_author(
custom_author.id
)
elapsed_time = time.time() - start_time
logger.info(
f"Career metrics calculation for author {custom_author.id} took "
f"{elapsed_time:.2f} seconds"
)
results.append(custom_author)
return CustomAuthorsListResponse(
results=results, meta=Meta(count=total_count, page=page, per_page=page_size)