Merge branch 'dev' into multi-organisations-statics
LocationBasedServices/smartguide/pipeline/head This commit looks good

This commit is contained in:
2026-08-12 10:55:48 +02:00
2 changed files with 38 additions and 47 deletions
+12 -20
View File
@@ -180,35 +180,27 @@ async def get_dashboard_publications(
from_year: int | None = None,
to_year: int | None = None,
) -> DashboardStatisticValue:
institution_ids = institution_id if isinstance(institution_id, list) else [institution_id]
institution_ids = (
institution_id if isinstance(institution_id, list) else [institution_id]
)
normalized_ids = [_normalize_institution_id(value) for value in institution_ids]
period = _dashboard_period(from_year, to_year)
if len(normalized_ids) == 1 and period == (None, None):
institution = await institutions_v2_service.get_institution_by_id_or_raise(
raw_oa_institution_id=_raw_institution_id(normalized_ids[0]),
source=["id", "works_count"],
)
return DashboardStatisticValue(
institution_id=normalized_ids[0],
organization_name=organization_name,
value=int(institution.works_count or 0),
)
filters = [works_v2_service._institutions_nested_query(normalized_ids)]
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
filters.append(
{"terms": {"publication_year": list(range(first_year, last_year + 1))}}
)
value = await elasticsearch.count(
index=ESIndex.OPENALEX_WORKS_WILDCARD,
query={"bool": {"filter": filters}},
publication_years = list(range(first_year, last_year + 1))
works = await works_v2_service.get_works(
page=0,
size=1,
institution_ids=normalized_ids,
publication_years=publication_years,
)
return DashboardStatisticValue(
institution_id=_statistic_scope_id(institution_ids),
organization_name=organization_name,
value=value,
value=works.total,
)
+26 -27
View File
@@ -63,6 +63,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"
@@ -385,11 +386,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}}}
@@ -495,19 +496,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",
@@ -521,17 +519,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