From 15f55af4c458f0403de3389d09bdf2951c47764b Mon Sep 17 00:00:00 2001 From: Farajollahi Date: Tue, 11 Aug 2026 13:41:57 +0200 Subject: [PATCH] fix count --- service/src/main/inse/dashboard/service.py | 36 ++++--------- .../src/main/inse/dashboard/test_service.py | 53 +++++++++---------- 2 files changed, 37 insertions(+), 52 deletions(-) diff --git a/service/src/main/inse/dashboard/service.py b/service/src/main/inse/dashboard/service.py index a162da74..873c55f0 100644 --- a/service/src/main/inse/dashboard/service.py +++ b/service/src/main/inse/dashboard/service.py @@ -111,36 +111,22 @@ async def get_dashboard_publications( ) -> DashboardStatisticValue: normalized_id = _normalize_institution_id(institution_id) period = _dashboard_period(from_year, to_year) - 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), - ) + publication_years: list[int] = [] + if period != (None, None): + 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)) - 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, - query={ - "bool": { - "filter": [ - works_v2_service._institutions_nested_query([normalized_id]), - {"terms": {"publication_year": publication_years}}, - ] - } - }, + works = await works_v2_service.get_works( + page=0, + size=1, + institution_ids=[normalized_id], + publication_years=publication_years, ) return DashboardStatisticValue( institution_id=normalized_id, organization_name=organization_name, - value=value, + value=works.total, ) diff --git a/service/src/main/inse/dashboard/test_service.py b/service/src/main/inse/dashboard/test_service.py index 6d438e0f..4445262a 100644 --- a/service/src/main/inse/dashboard/test_service.py +++ b/service/src/main/inse/dashboard/test_service.py @@ -53,6 +53,7 @@ from inse.projects.model import ( EuCordisProjectType, ) from inse.works.v2 import service as works_v2_service +from inse.works.v2.model import WorkPageV2 ORG_INSTITUTION_ID = "https://openalex.org/I123" @@ -375,11 +376,11 @@ async def test_bund_topics_use_mapping_index_and_unique_hierarchy_counts(): @pytest.mark.anyio async def test_dashboard_statistics_counts_organization_data_and_all_projects(): - async def search(**kwargs): - index = kwargs["index"] + async def search(*args, **kwargs): + index = kwargs.get("index", args[0] if args else None) if index == config.settings.openalex_works_search_index: - return {"hits": {"total": {"value": 120}}} + return {"hits": {"total": {"value": 120}, "hits": []}} if index == ESIndex.OPENALEX_AUTHORS: return {"hits": {"total": {"value": 35}}} @@ -485,19 +486,16 @@ async def test_dashboard_statistics_counts_organization_data_and_all_projects(): @pytest.mark.anyio -async def test_dashboard_publications_uses_exact_es_count_for_period(): - with ( - patch( - "inse.dashboard.service.institutions_v2_service.get_institution_by_id_or_raise", - new=AsyncMock( - return_value=Institution(id=ORG_INSTITUTION_ID, works_count=250) - ), - ) as mocked_get_institution, - patch( - "inse.integrations.elasticsearch.count", - new=AsyncMock(return_value=22), - ) as mocked_count, - ): +async def test_dashboard_publications_uses_institution_publications_logic(): + with patch( + "inse.dashboard.service.works_v2_service.get_works", + new=AsyncMock( + side_effect=[ + WorkPageV2(elements=[], total=250, page=0, size=1), + WorkPageV2(elements=[], total=22, page=0, size=1), + ] + ), + ) as mocked_get_works: all_time = await get_dashboard_publications( ORG_INSTITUTION_ID, "Example Org", @@ -511,17 +509,18 @@ async def test_dashboard_publications_uses_exact_es_count_for_period(): assert all_time.value == 250 assert selected_period.value == 22 - mocked_get_institution.assert_awaited_once_with( - raw_oa_institution_id="I123", - source=["id", "works_count"], - ) - 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]}}, - ] + assert mocked_get_works.await_args_list[0].kwargs == { + "page": 0, + "size": 1, + "institution_ids": [ORG_INSTITUTION_ID], + "publication_years": [], + } + assert mocked_get_works.await_args_list[1].kwargs == { + "page": 0, + "size": 1, + "institution_ids": [ORG_INSTITUTION_ID], + "publication_years": [2021, 2022], + } @pytest.mark.anyio