diff --git a/service/src/main/inse/dashboard/service.py b/service/src/main/inse/dashboard/service.py index 2338b16f..ae9d8eac 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(normalized_id, aliases), + _top_eu_project_publication_authors(normalized_id, aliases, authors), ) patents = await _safe_top_authors( @@ -843,7 +843,16 @@ async def _top_patent_authors(institution_id: str) -> list[TopAuthor]: async def _top_eu_project_publication_authors( institution_id: str, organization_names: list[str], + institution_authors: list[dict[str, Any]], ) -> list[TopAuthor]: + institution_author_ids = { + author_id + for author in institution_authors + if (author_id := _normalize_author_id(author.get("id"))) + } + if not institution_author_ids: + return [] + project_page = await get_eu_organization_project_contributions( organization_names=organization_names, projects_filtering_properties=ProjectsFilteringProperties( @@ -905,6 +914,7 @@ async def _top_eu_project_publication_authors( works_response, doi_project_ids, institution_id, + institution_author_ids, ) @@ -941,6 +951,7 @@ def _openalex_work_authors_by_eu_projects( response: dict[str, Any], doi_project_ids: dict[str, set[str]], institution_id: str, + institution_author_ids: set[str], ) -> list[TopAuthor]: # Count unique CORDIS projects per author, not publications. Multiple works # (and therefore multiple DOIs) can belong to the same project ID. @@ -962,7 +973,11 @@ def _openalex_work_authors_by_eu_projects( author = authorship.get("author") or {} author_id = _normalize_author_id(author.get("id")) author_name = str(author.get("display_name") or "").strip() - if not author_id or not author_name: + if ( + not author_id + or author_id not in institution_author_ids + or not author_name + ): continue author_project_ids[author_id].update(project_ids) author_names.setdefault(author_id, author_name) diff --git a/service/src/main/inse/dashboard/test_service.py b/service/src/main/inse/dashboard/test_service.py index 543af0d7..063a3d48 100644 --- a/service/src/main/inse/dashboard/test_service.py +++ b/service/src/main/inse/dashboard/test_service.py @@ -356,7 +356,14 @@ def test_eu_author_count_groups_multiple_publications_by_project_id(): "display_name": "Marcus Example", }, "institutions": [{"id": ORG_INSTITUTION_ID}], - } + }, + { + "author": { + "id": "https://openalex.org/A999", + "display_name": "External Example", + }, + "institutions": [{"id": ORG_INSTITUTION_ID}], + }, ], } }, @@ -386,6 +393,7 @@ def test_eu_author_count_groups_multiple_publications_by_project_id(): works_response, doi_project_ids, ORG_INSTITUTION_ID, + {"https://openalex.org/A5007170803"}, ) assert [author.model_dump() for author in result] == [ @@ -397,7 +405,28 @@ def test_eu_author_count_groups_multiple_publications_by_project_id(): async def test_top_eu_project_authors_count_filtered_projects_by_stable_author_id(): async def search(**kwargs): if kwargs["index"] == ESIndex.OPENALEX_AUTHORS: - return {"hits": {"hits": []}} + return { + "hits": { + "hits": [ + { + "_source": { + "id": "https://openalex.org/A1", + "display_name": "Ada Example", + "works_count": 3, + "counts_by_year": [], + } + }, + { + "_source": { + "id": "https://openalex.org/A2", + "display_name": "Grace Example", + "works_count": 1, + "counts_by_year": [], + } + }, + ] + } + } if kwargs["index"] == ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD: assert kwargs["body"]["query"] == { "terms": {"projectID": ["P1", "P2"]}