diff --git a/service/src/main/inse/dashboard/service.py b/service/src/main/inse/dashboard/service.py index 384e0755..39fbc8fb 100644 --- a/service/src/main/inse/dashboard/service.py +++ b/service/src/main/inse/dashboard/service.py @@ -346,7 +346,7 @@ async def get_top_authors( eu_projects = await _safe_top_authors( "eu_projects", - _top_eu_project_publication_authors(aliases), + _top_eu_project_publication_authors(normalized_id, aliases), ) patents = await _safe_top_authors( @@ -841,6 +841,7 @@ async def _top_patent_authors(institution_id: str) -> list[TopAuthor]: async def _top_eu_project_publication_authors( + institution_id: str, organization_names: list[str], ) -> list[TopAuthor]: project_page = await get_eu_organization_project_contributions( @@ -886,17 +887,25 @@ async def _top_eu_project_publication_authors( "doi", "authorships.author.id", "authorships.author.display_name", + "authorships.institutions.id", ], "query": { - "terms": { - "doi": sorted(doi_project_ids), + "bool": { + "filter": [ + {"terms": {"doi": sorted(doi_project_ids)}}, + _institution_works_query(institution_id), + ] } }, }, cluster=ESCluster.NES, ) - return _openalex_work_authors_by_eu_projects(works_response, doi_project_ids) + return _openalex_work_authors_by_eu_projects( + works_response, + doi_project_ids, + institution_id, + ) def _cordis_publication_doi_project_ids( @@ -932,6 +941,7 @@ def _normalize_doi(value: Any) -> str | None: def _openalex_work_authors_by_eu_projects( response: dict[str, Any], doi_project_ids: dict[str, set[str]], + institution_id: str, ) -> list[TopAuthor]: author_project_ids: dict[str, set[str]] = defaultdict(set) author_names: dict[str, str] = {} @@ -943,6 +953,11 @@ def _openalex_work_authors_by_eu_projects( continue for authorship in source.get("authorships") or []: + if not any( + institution.get("id") == institution_id + for institution in authorship.get("institutions") or [] + ): + continue author = authorship.get("author") or {} author_id = _normalize_author_id(author.get("id")) author_name = str(author.get("display_name") or "").strip() diff --git a/service/src/main/inse/dashboard/test_service.py b/service/src/main/inse/dashboard/test_service.py index 93302fc2..e0553d25 100644 --- a/service/src/main/inse/dashboard/test_service.py +++ b/service/src/main/inse/dashboard/test_service.py @@ -208,13 +208,15 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects(): "author": { "id": "https://openalex.org/A2", "display_name": "Grace Example", - } + }, + "institutions": [{"id": ORG_INSTITUTION_ID}], }, { "author": { "id": "https://openalex.org/A1", "display_name": "Ada Example", - } + }, + "institutions": [{"id": ORG_INSTITUTION_ID}], }, ], } @@ -227,7 +229,8 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects(): "author": { "id": "https://openalex.org/A2", "display_name": "Grace Example", - } + }, + "institutions": [{"id": ORG_INSTITUTION_ID}], } ], } @@ -322,14 +325,25 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects(): ] assert len(works_calls) == 1 works_body = works_calls[0].kwargs["body"] - assert works_body["query"]["terms"]["doi"] == [ + assert works_body["query"]["bool"]["filter"][0]["terms"]["doi"] == [ "https://doi.org/10.1234/p1", "https://doi.org/10.1234/p2", ] + assert works_body["query"]["bool"]["filter"][1] == { + "nested": { + "path": "authorships.institutions", + "query": { + "term": { + "authorships.institutions.id": ORG_INSTITUTION_ID, + } + }, + } + } assert works_body["_source"] == [ "doi", "authorships.author.id", "authorships.author.display_name", + "authorships.institutions.id", ] @@ -355,7 +369,7 @@ async def test_top_eu_project_authors_count_filtered_projects_by_stable_author_i } if kwargs["index"] == config.settings.openalex_works_search_index: - assert kwargs["body"]["query"]["terms"]["doi"] == [ + assert kwargs["body"]["query"]["bool"]["filter"][0]["terms"]["doi"] == [ "https://doi.org/10.1/p1-a", "https://doi.org/10.1/p1-b", "https://doi.org/10.1/p2", @@ -371,13 +385,24 @@ async def test_top_eu_project_authors_count_filtered_projects_by_stable_author_i "author": { "id": "https://openalex.org/A1", "display_name": "Ada Example", - } + }, + "institutions": [{"id": ORG_INSTITUTION_ID}], }, { "author": { "id": "https://openalex.org/A2", "display_name": "Grace Example", - } + }, + "institutions": [{"id": ORG_INSTITUTION_ID}], + }, + { + "author": { + "id": "https://openalex.org/A3", + "display_name": "External Author", + }, + "institutions": [ + {"id": "https://openalex.org/I999"} + ], }, ], } @@ -390,7 +415,8 @@ async def test_top_eu_project_authors_count_filtered_projects_by_stable_author_i "author": { "id": "https://openalex.org/A1", "display_name": "Ada Example", - } + }, + "institutions": [{"id": ORG_INSTITUTION_ID}], } ], } @@ -403,7 +429,8 @@ async def test_top_eu_project_authors_count_filtered_projects_by_stable_author_i "author": { "id": "https://openalex.org/A1", "display_name": "Ada Example", - } + }, + "institutions": [{"id": ORG_INSTITUTION_ID}], } ], } @@ -416,7 +443,8 @@ async def test_top_eu_project_authors_count_filtered_projects_by_stable_author_i "author": { "id": "https://openalex.org/A999", "display_name": "Outside Author", - } + }, + "institutions": [{"id": ORG_INSTITUTION_ID}], } ], }