Keep check-dashboard publication count after timeline merge

This commit is contained in:
2026-08-06 18:31:52 +02:00
parent 33192efa33
commit aed1f33935
2 changed files with 40 additions and 43 deletions
+24 -12
View File
@@ -266,24 +266,36 @@ async def get_dashboard_publications(
) -> DashboardStatisticValue:
normalized_id = _normalize_institution_id(institution_id)
period = _dashboard_period(from_year, to_year)
must = [
works_v2_service._institutions_nested_query([normalized_id]),
]
if publication_years := _dashboard_publication_years(period):
must.append({"terms": {"publication_year": publication_years}})
response = await elasticsearch.search(
if period == (None, None):
institution = await institutions_v2_service.get_institution_by_id_or_raise(
raw_oa_institution_id=_raw_institution_id(normalized_id),
source=["id", "works_count"],
)
return DashboardStatisticValue(
institution_id=normalized_id,
organization_name=organization_name,
value=int(institution.works_count or 0),
)
first_year = period[0] if period[0] is not None else 1900
last_year = period[1] if period[1] is not None else 2100
publication_years = list(range(first_year, last_year + 1))
value = await elasticsearch.count(
index=ESIndex.OPENALEX_WORKS_WILDCARD,
body={
"size": 0,
"track_total_hits": True,
"query": {"bool": {"must": must}},
query={
"bool": {
"filter": [
works_v2_service._institutions_nested_query([normalized_id]),
{"terms": {"publication_year": publication_years}},
]
}
},
cluster=ESCluster.NES,
)
return DashboardStatisticValue(
institution_id=normalized_id,
organization_name=organization_name,
value=_total(response),
value=value,
)
+16 -31
View File
@@ -41,6 +41,7 @@ from inse.projects.model import (
EuCordisProjectSummary,
EuCordisProjectType,
)
from inse.works.v2 import service as works_v2_service
ORG_INSTITUTION_ID = "https://openalex.org/I123"
@@ -409,16 +410,16 @@ async def test_dashboard_statistics_counts_organization_data_and_all_projects():
}
@pytest.mark.anyio
async def test_dashboard_publications_counts_matching_openalex_works():
async def test_dashboard_publications_uses_exact_es_count_for_period():
with patch(
"inse.dashboard.service.elasticsearch.search",
"inse.dashboard.service.institutions_v2_service.get_institution_by_id_or_raise",
new=AsyncMock(
side_effect=[
{"hits": {"total": {"value": 250}}},
{"hits": {"total": {"value": 22}}},
]
return_value=Institution(id=ORG_INSTITUTION_ID, works_count=250)
),
) as mocked_search:
) as mocked_get_institution, patch(
"inse.dashboard.service.elasticsearch.count",
new=AsyncMock(return_value=22),
) as mocked_count:
all_time = await get_dashboard_publications(
ORG_INSTITUTION_ID,
"Example Org",
@@ -432,31 +433,15 @@ async def test_dashboard_publications_counts_matching_openalex_works():
assert all_time.value == 250
assert selected_period.value == 22
all_time_body = mocked_search.await_args_list[0].kwargs["body"]
selected_period_body = mocked_search.await_args_list[1].kwargs["body"]
assert mocked_search.await_args_list[0].kwargs["index"] == (
ESIndex.OPENALEX_WORKS_WILDCARD
mocked_get_institution.assert_awaited_once_with(
raw_oa_institution_id="I123",
source=["id", "works_count"],
)
assert all_time_body["track_total_hits"] is True
assert all_time_body["query"]["bool"]["must"] == [
{
"nested": {
"path": "authorships",
"query": {
"nested": {
"path": "authorships.institutions",
"query": {
"terms": {
"authorships.institutions.id": [ORG_INSTITUTION_ID]
}
},
}
},
}
}
]
assert selected_period_body["query"]["bool"]["must"] == [
all_time_body["query"]["bool"]["must"][0],
mocked_count.assert_awaited_once()
count_call = mocked_count.await_args.kwargs
assert count_call["index"] == ESIndex.OPENALEX_WORKS_WILDCARD
assert count_call["query"]["bool"]["filter"] == [
works_v2_service._institutions_nested_query([ORG_INSTITUTION_ID]),
{"terms": {"publication_year": [2021, 2022]}},
]