fix news bug
LocationBasedServices/smartguide/pipeline/head This commit looks good

This commit is contained in:
2026-08-04 14:41:26 +02:00
parent 1fc509ea13
commit bb791138cd
2 changed files with 82 additions and 3 deletions
+40 -3
View File
@@ -1214,7 +1214,11 @@ async def get_top_authors(
news = await _safe_top_authors(
"news",
_top_news_authors(authors),
_top_news_authors(
authors,
from_year=period[0],
to_year=period[1],
),
)
return TopAuthorStatistics(
@@ -2443,16 +2447,49 @@ def _normalize_author_id(value: Any) -> str | None:
return f"https://openalex.org/{short_id}"
async def _top_news_authors(authors: list[dict[str, Any]]) -> list[TopAuthor]:
async def _top_news_authors(
authors: list[dict[str, Any]],
from_year: int | None = None,
to_year: int | None = None,
) -> list[TopAuthor]:
if not authors:
return []
period = _dashboard_period(from_year, to_year)
author_names = [author["name"] for author in authors]
filters: list[dict[str, Any]] = [
{"terms": {"authors.keyword": author_names}}
]
if period != (None, None):
date_range: dict[str, int] = {}
if period[0] is not None:
date_range["gte"] = period[0]
if period[1] is not None:
date_range["lte"] = period[1]
news_ids = [
str(hit["_id"])
async for hit in elasticsearch.scan(
index=str(ESIndex.AKTUELLES),
query={"range": {"date_year": date_range}},
source=False,
page_size=1000,
cluster=ESCluster.INSE,
)
if hit.get("_id")
]
if not news_ids:
return []
filters.append({"terms": {"news_id.keyword": news_ids}})
response = await elasticsearch.search(
index="aktuell_postproc",
body={
"size": 0,
"query": {"terms": {"authors.keyword": author_names}},
"query": (
filters[0]
if len(filters) == 1
else {"bool": {"filter": filters}}
),
"aggs": {
"authors": {
"terms": {
@@ -12,6 +12,7 @@ from inse.dashboard.service import (
_openalex_work_authors_by_eu_projects,
_get_dfg_project_ids_from_projects_service,
_reconcile_project_status_counts,
_top_news_authors,
get_dashboard_bmbf_projects,
get_dashboard_dfg_projects,
get_dashboard_eu_projects,
@@ -50,6 +51,47 @@ def test_project_status_counts_match_authoritative_total():
assert _reconcile_project_status_counts(7, 4, 2, 3) == (2, 2, 3)
@pytest.mark.anyio
async def test_top_news_authors_applies_dashboard_period():
response = {
"aggregations": {
"authors": {
"buckets": [{"key": "Ada Example", "doc_count": 3}]
}
}
}
async def scan(**kwargs):
assert kwargs["index"] == str(ESIndex.AKTUELLES)
assert kwargs["query"] == {
"range": {"date_year": {"gte": 2020, "lte": 2023}}
}
yield {"_id": "N1"}
yield {"_id": "N2"}
with patch(
"inse.dashboard.service.elasticsearch.search",
new=AsyncMock(return_value=response),
) as mocked_search, patch(
"inse.dashboard.service.elasticsearch.scan",
new=scan,
):
result = await _top_news_authors(
[{"id": "https://openalex.org/A1", "name": "Ada Example"}],
from_year=2020,
to_year=2023,
)
assert result[0].count == 3
assert mocked_search.await_args.kwargs["body"]["query"] == {
"bool": {
"filter": [
{"terms": {"authors.keyword": ["Ada Example"]}},
{"terms": {"news_id.keyword": ["N1", "N2"]}},
]
}
}
@pytest.mark.anyio
async def test_dfg_topic_project_ids_use_paginated_projects_service():
pages = [