From 410bcdec4ba5f21783476045769bd1d1aa33264f Mon Sep 17 00:00:00 2001 From: spsaefan <117728614+spsaefan@users.noreply.github.com> Date: Fri, 17 Jul 2026 13:32:59 +0200 Subject: [PATCH 01/10] Use OpenAlex IDs for EU authors --- service/src/main/inse/dashboard/service.py | 163 ++++++-- .../src/main/inse/dashboard/test_service.py | 353 ++++++++++++++++-- 2 files changed, 440 insertions(+), 76 deletions(-) diff --git a/service/src/main/inse/dashboard/service.py b/service/src/main/inse/dashboard/service.py index 39bf6f91..dde3ba29 100644 --- a/service/src/main/inse/dashboard/service.py +++ b/service/src/main/inse/dashboard/service.py @@ -1,5 +1,6 @@ import asyncio import logging +from collections import defaultdict from datetime import datetime from typing import Any, Literal @@ -26,6 +27,9 @@ from .model import ( TopicWordCloud, TopicWordCloudWord, ) +from inse.projects.projects_search_client import ( + get_eu_organization_contributions, +) TOP_AUTHOR_LIMIT = 10 TOPIC_HEATMAP_LIMIT = 25 @@ -333,18 +337,27 @@ async def get_top_authors( normalized_id = _normalize_institution_id(institution_id) authors = await _safe_institution_authors(normalized_id) works = _top_work_authors(authors, works_period) + + aliases = await _institution_aliases( + normalized_id, + organization_name, + ) + eu_projects = await _safe_top_authors( "eu_projects", - _top_eu_project_publication_authors(), + _top_eu_project_publication_authors(aliases), ) + patents = await _safe_top_authors( "patents", _top_patent_authors(normalized_id), ) + news = await _safe_top_authors( "news", _top_news_authors(authors), ) + return TopAuthorStatistics( institution_id=normalized_id, works=works, @@ -826,52 +839,132 @@ async def _top_patent_authors(institution_id: str) -> list[TopAuthor]: ] -async def _top_eu_project_publication_authors() -> list[TopAuthor]: - response = await elasticsearch.search( - index=ESIndex.CORDIS_EU_FP7_PUBLICATIONS, +async def _top_eu_project_publication_authors( + organization_names: list[str], +) -> list[TopAuthor]: + contributions = await get_eu_organization_contributions( + organization_names=organization_names, + ) + + project_ids = list(contributions.keys()) + + if not project_ids: + return [] + + publication_response = await elasticsearch.search( + index=ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD, body={ - "size": 0, - "aggs": { - "authors": { - "terms": { - "script": { - "lang": "painless", - "source": _cordis_publication_authors_script(), - }, - "size": TOP_AUTHOR_LIMIT, - "order": {"_count": "desc"}, - } + "size": 10000, + "_source": ["projectID", "doi", "qaProcessedDoi"], + "query": { + "terms": { + "projectID": project_ids, } }, }, cluster=ESCluster.INSE, ) + + doi_project_ids = _cordis_publication_doi_project_ids( + publication_response, + set(project_ids), + ) + if not doi_project_ids: + return [] + + works_response = await elasticsearch.search( + index=config.settings.openalex_works_search_index, + body={ + "size": 10000, + "_source": [ + "doi", + "authorships.author.id", + "authorships.author.display_name", + ], + "query": { + "terms": { + "doi": sorted(doi_project_ids), + } + }, + }, + cluster=ESCluster.NES, + ) + + return _openalex_work_authors_by_eu_projects(works_response, doi_project_ids) + + +def _cordis_publication_doi_project_ids( + response: dict[str, Any], + allowed_project_ids: set[str], +) -> dict[str, set[str]]: + doi_project_ids: dict[str, set[str]] = defaultdict(set) + for hit in response.get("hits", {}).get("hits", []): + source = hit.get("_source", {}) + project_id = str(source.get("projectID") or "").strip() + if not project_id or project_id not in allowed_project_ids: + continue + for field in ("doi", "qaProcessedDoi"): + doi = _normalize_doi(source.get(field)) + if doi: + doi_project_ids[doi].add(project_id) + return dict(doi_project_ids) + + +def _normalize_doi(value: Any) -> str | None: + if not isinstance(value, str) or not value.strip(): + return None + doi = value.strip().lower() + if doi.startswith("https://doi.org/"): + return doi + if doi.startswith("http://doi.org/"): + return "https://" + doi.removeprefix("http://") + if doi.startswith("doi.org/"): + return f"https://{doi}" + return f"https://doi.org/{doi}" + + +def _openalex_work_authors_by_eu_projects( + response: dict[str, Any], + doi_project_ids: dict[str, set[str]], +) -> list[TopAuthor]: + author_project_ids: dict[str, set[str]] = defaultdict(set) + author_names: dict[str, str] = {} + + for hit in response.get("hits", {}).get("hits", []): + source = hit.get("_source", {}) + project_ids = doi_project_ids.get(_normalize_doi(source.get("doi")) or "") + if not project_ids: + continue + + for authorship in source.get("authorships") or []: + 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: + continue + author_project_ids[author_id].update(project_ids) + author_names.setdefault(author_id, author_name) + + ranked_authors = sorted( + author_project_ids.items(), + key=lambda item: (-len(item[1]), author_names.get(item[0], ""), item[0]), + ) + return [ TopAuthor( - name=str(bucket["key"]), - count=int(bucket.get("doc_count", 0)), + id=_short_author_id(author_id), + name=author_names[author_id], + count=len(project_ids), ) - for bucket in ( - response.get("aggregations", {}).get("authors", {}).get("buckets", []) - ) - if str(bucket.get("key", "")).strip() + for author_id, project_ids in ranked_authors[:TOP_AUTHOR_LIMIT] ] -def _cordis_publication_authors_script() -> str: - return ( - "def names = new HashSet(); " - "def source = params['_source']; " - "def value = source.containsKey('authors') ? source['authors'] " - " : (source.containsKey('Authors') ? source['Authors'] : null); " - "if (value instanceof String && value.length() > 0) { " - " for (name in value.splitOnToken(';')) { " - " def trimmed = name.trim(); " - " if (trimmed.length() > 0) { names.add(trimmed); } " - " } " - "} " - "return new ArrayList(names);" - ) +def _normalize_author_id(value: Any) -> str | None: + if not isinstance(value, str) or not value.strip(): + return None + short_id = value.replace("https://openalex.org/", "").strip().upper() + return f"https://openalex.org/{short_id}" async def _top_news_authors(authors: list[dict[str, Any]]) -> list[TopAuthor]: diff --git a/service/src/main/inse/dashboard/test_service.py b/service/src/main/inse/dashboard/test_service.py index 12c74d02..0a03a574 100644 --- a/service/src/main/inse/dashboard/test_service.py +++ b/service/src/main/inse/dashboard/test_service.py @@ -28,14 +28,19 @@ def anyio_backend(): async def test_dashboard_statistics_counts_organization_data_and_all_projects(): async def search(**kwargs): index = kwargs["index"] + if index == config.settings.openalex_works_search_index: return {"hits": {"total": {"value": 120}}} + if index == ESIndex.OPENALEX_AUTHORS: return {"hits": {"total": {"value": 35}}} + if index == ESIndex.CORDIS_EU_ORGANIZATIONS_WILDCARD: return {"aggregations": {"projects": {"value": 10}}} + if index == ESIndex.PROJECTS_BUND_V1: return {"hits": {"total": {"value": 5}}} + return {"hits": {"total": {"value": 3}}} with patch( @@ -45,7 +50,10 @@ async def test_dashboard_statistics_counts_organization_data_and_all_projects(): "inse.dashboard.service.InstitutionsSearchClient.get_institution_aliases_by_oa_institution_id", new=AsyncMock(return_value=["Example University", "Example Uni"]), ): - result = await get_dashboard_statistics(ORG_INSTITUTION_ID, "Example Org") + result = await get_dashboard_statistics( + ORG_INSTITUTION_ID, + "Example Org", + ) assert result.model_dump() == { "institution_id": ORG_INSTITUTION_ID, @@ -57,21 +65,38 @@ async def test_dashboard_statistics_counts_organization_data_and_all_projects(): "eu_projects": 10, "bmbf_projects": 5, } - calls = mocked_search.await_args_list - assert calls[0].kwargs["index"] == config.settings.openalex_works_search_index - assert calls[0].kwargs["query"]["nested"]["query"]["term"] == { + + publication_call = next( + call + for call in mocked_search.await_args_list + if call.kwargs["index"] == config.settings.openalex_works_search_index + ) + assert publication_call.kwargs["query"]["nested"]["query"]["term"] == { "authorships.institutions.id": ORG_INSTITUTION_ID } - assert calls[1].kwargs["query"]["nested"]["query"]["term"] == { - "affiliations.institution.id": ORG_INSTITUTION_ID + + person_call = next( + call + for call in mocked_search.await_args_list + if call.kwargs["index"] == ESIndex.OPENALEX_AUTHORS + ) + person_query = person_call.kwargs["query"] + recent_affiliation = person_query["bool"]["should"][0]["bool"]["filter"][1] + + assert recent_affiliation["nested"]["path"] == "affiliations" + assert recent_affiliation["nested"]["query"]["bool"]["filter"][0] == { + "match_phrase": { + "affiliations.institution.id": ORG_INSTITUTION_ID + } } - assert all(call.kwargs["cluster"] == ESCluster.INSE for call in calls[2:]) - assert [call.kwargs["index"] for call in calls[2:]] == [ - ESIndex.CORDIS_EU_ORGANIZATIONS_WILDCARD, - ESIndex.PROJECTS_BUND_V1, - ESIndex.PROJECTS_DFG_ALL, - ] - assert calls[2].kwargs["body"]["query"] == { + + eu_call = next( + call + for call in mocked_search.await_args_list + if call.kwargs["index"] == ESIndex.CORDIS_EU_ORGANIZATIONS_WILDCARD + ) + assert eu_call.kwargs["cluster"] == ESCluster.INSE + assert eu_call.kwargs["body"]["query"] == { "terms": { "name.keyword": [ "EXAMPLE ORG", @@ -80,15 +105,29 @@ async def test_dashboard_statistics_counts_organization_data_and_all_projects(): ] } } - assert calls[3].kwargs["query"]["bool"]["minimum_should_match"] == 1 - assert calls[3].kwargs["query"]["bool"]["should"][0]["match"] == { + + bund_call = next( + call + for call in mocked_search.await_args_list + if call.kwargs["index"] == ESIndex.PROJECTS_BUND_V1 + ) + assert bund_call.kwargs["cluster"] == ESCluster.INSE + assert bund_call.kwargs["query"]["bool"]["minimum_should_match"] == 1 + assert bund_call.kwargs["query"]["bool"]["should"][0]["match"] == { "Zuwendungsempfänger": { "query": "Example Org", "operator": "and", } } - assert calls[4].kwargs["query"]["bool"]["minimum_should_match"] == 1 - assert calls[4].kwargs["query"]["bool"]["should"][0]["bool"]["should"][0][ + + dfg_call = next( + call + for call in mocked_search.await_args_list + if call.kwargs["index"] == ESIndex.PROJECTS_DFG_ALL + ) + assert dfg_call.kwargs["cluster"] == ESCluster.INSE + assert dfg_call.kwargs["query"]["bool"]["minimum_should_match"] == 1 + assert dfg_call.kwargs["query"]["bool"]["should"][0]["bool"]["should"][0][ "match" ] == { "Antragstellende Institution": { @@ -99,7 +138,7 @@ async def test_dashboard_statistics_counts_organization_data_and_all_projects(): @pytest.mark.anyio -async def test_top_authors_maps_works_patents_and_news_without_project_queries(): +async def test_top_authors_maps_works_patents_news_and_eu_projects(): async def search(**kwargs): if kwargs["index"] == ESIndex.OPENALEX_AUTHORS: return { @@ -124,31 +163,95 @@ async def test_top_authors_maps_works_patents_and_news_without_project_queries() ] } } + if kwargs["index"] == "aktuell_postproc": - return { - "aggregations": { - "authors": { - "buckets": [{"key": "Ada Example", "doc_count": 6}] - } - } - } - if kwargs["index"] == ESIndex.CORDIS_EU_FP7_PUBLICATIONS: return { "aggregations": { "authors": { "buckets": [ - {"key": "Grace Example", "doc_count": 8}, - {"key": "Ada Example", "doc_count": 5}, + {"key": "Ada Example", "doc_count": 6} ] } } } + + if kwargs["index"] == ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD: + return { + "hits": { + "hits": [ + { + "_source": { + "projectID": "P1", + "doi": "10.1234/p1", + } + }, + { + "_source": { + "projectID": "P2", + "qaProcessedDoi": "https://doi.org/10.1234/p2", + } + }, + ] + } + } + + if kwargs["index"] == config.settings.openalex_works_search_index: + return { + "hits": { + "hits": [ + { + "_source": { + "doi": "https://doi.org/10.1234/p1", + "authorships": [ + { + "author": { + "id": "https://openalex.org/A2", + "display_name": "Grace Example", + } + }, + { + "author": { + "id": "https://openalex.org/A1", + "display_name": "Ada Example", + } + }, + ], + } + }, + { + "_source": { + "doi": "https://doi.org/10.1234/p2", + "authorships": [ + { + "author": { + "id": "https://openalex.org/A2", + "display_name": "Grace Example", + } + } + ], + } + }, + ] + } + } + return {"aggregations": {"people": {"buckets": []}}} with patch( "inse.dashboard.service.elasticsearch.search", new=AsyncMock(side_effect=search), ) as mocked_search, patch( + "inse.dashboard.service.InstitutionsSearchClient.get_institution_aliases_by_oa_institution_id", + new=AsyncMock(return_value=["Example University"]), + ), patch( + "inse.dashboard.service.get_eu_organization_contributions", + new=AsyncMock( + return_value={ + "P1": object(), + "P2": object(), + } + ), + ), patch( "inse.dashboard.service.OrgPatentsSearchClient.top_authors_by_institution", new=AsyncMock( return_value=[ @@ -156,7 +259,10 @@ async def test_top_authors_maps_works_patents_and_news_without_project_queries() ] ), ): - result = await get_top_authors(ORG_INSTITUTION_ID, "Example Org") + result = await get_top_authors( + ORG_INSTITUTION_ID, + "Example Org", + ) assert result.works[0].model_dump() == { "id": "A1", @@ -169,30 +275,180 @@ async def test_top_authors_maps_works_patents_and_news_without_project_queries() "count": 4, } assert [author.model_dump() for author in result.eu_projects] == [ - {"id": None, "name": "Grace Example", "count": 8}, - {"id": None, "name": "Ada Example", "count": 5}, + {"id": "A2", "name": "Grace Example", "count": 2}, + {"id": "A1", "name": "Ada Example", "count": 1}, ] assert result.news[0].model_dump() == { "id": "A1", "name": "Ada Example", "count": 6, } + project_calls = [ call for call in mocked_search.await_args_list if call.kwargs["index"] - in {ESIndex.PROJECTS_DFG_ALL, ESIndex.PROJECTS_BUND_V1} + in { + ESIndex.PROJECTS_DFG_ALL, + ESIndex.PROJECTS_BUND_V1, + } ] assert project_calls == [] + eu_calls = [ call for call in mocked_search.await_args_list - if call.kwargs["index"] == ESIndex.CORDIS_EU_FP7_PUBLICATIONS + if call.kwargs["index"] == ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD ] assert len(eu_calls) == 1 - terms = eu_calls[0].kwargs["body"]["aggs"]["authors"]["terms"] - assert terms["size"] == 10 - assert "splitOnToken(';')" in terms["script"]["source"] + + eu_body = eu_calls[0].kwargs["body"] + assert set(eu_body["query"]["terms"]["projectID"]) == { + "P1", + "P2", + } + assert eu_body["_source"] == ["projectID", "doi", "qaProcessedDoi"] + assert "aggs" not in eu_body + + works_calls = [ + call + for call in mocked_search.await_args_list + if call.kwargs["index"] == config.settings.openalex_works_search_index + ] + assert len(works_calls) == 1 + works_body = works_calls[0].kwargs["body"] + assert works_body["query"]["terms"]["doi"] == [ + "https://doi.org/10.1234/p1", + "https://doi.org/10.1234/p2", + ] + assert works_body["_source"] == [ + "doi", + "authorships.author.id", + "authorships.author.display_name", + ] + + +@pytest.mark.anyio +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": []}} + + if kwargs["index"] == ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD: + assert kwargs["body"]["query"] == { + "terms": {"projectID": ["P1", "P2"]} + } + return { + "hits": { + "hits": [ + {"_source": {"projectID": "P1", "doi": "10.1/P1-A"}}, + {"_source": {"projectID": "P1", "doi": "10.1/P1-B"}}, + {"_source": {"projectID": "P2", "doi": "10.1/P2"}}, + {"_source": {"projectID": "OUTSIDE", "doi": "10.1/OUT"}}, + ] + } + } + + if kwargs["index"] == config.settings.openalex_works_search_index: + assert kwargs["body"]["query"]["terms"]["doi"] == [ + "https://doi.org/10.1/p1-a", + "https://doi.org/10.1/p1-b", + "https://doi.org/10.1/p2", + ] + return { + "hits": { + "hits": [ + { + "_source": { + "doi": "https://doi.org/10.1/p1-a", + "authorships": [ + { + "author": { + "id": "https://openalex.org/A1", + "display_name": "Ada Example", + } + }, + { + "author": { + "id": "https://openalex.org/A2", + "display_name": "Grace Example", + } + }, + ], + } + }, + { + "_source": { + "doi": "https://doi.org/10.1/p1-b", + "authorships": [ + { + "author": { + "id": "https://openalex.org/A1", + "display_name": "Ada Example", + } + } + ], + } + }, + { + "_source": { + "doi": "https://doi.org/10.1/p2", + "authorships": [ + { + "author": { + "id": "https://openalex.org/A1", + "display_name": "Ada Example", + } + } + ], + } + }, + { + "_source": { + "doi": "https://doi.org/10.1/not-requested", + "authorships": [ + { + "author": { + "id": "https://openalex.org/A999", + "display_name": "Outside Author", + } + } + ], + } + }, + ] + } + } + + return {"aggregations": {"authors": {"buckets": []}}} + + with patch( + "inse.dashboard.service.elasticsearch.search", + new=AsyncMock(side_effect=search), + ), patch( + "inse.dashboard.service.InstitutionsSearchClient.get_institution_aliases_by_oa_institution_id", + new=AsyncMock(return_value=["Example University"]), + ), patch( + "inse.dashboard.service.get_eu_organization_contributions", + new=AsyncMock( + return_value={ + "P1": object(), + "P2": object(), + } + ), + ), patch( + "inse.dashboard.service.OrgPatentsSearchClient.top_authors_by_institution", + new=AsyncMock(return_value=[]), + ): + result = await get_top_authors( + ORG_INSTITUTION_ID, + "Example Org", + ) + + assert [author.model_dump() for author in result.eu_projects] == [ + {"id": "A1", "name": "Ada Example", "count": 2}, + {"id": "A2", "name": "Grace Example", "count": 1}, + ] @pytest.mark.anyio @@ -213,16 +469,26 @@ async def test_top_authors_returns_partial_response_when_one_source_fails(): ] } } + raise RuntimeError("projects unavailable") with patch( "inse.dashboard.service.elasticsearch.search", new=AsyncMock(side_effect=search), + ), patch( + "inse.dashboard.service.InstitutionsSearchClient.get_institution_aliases_by_oa_institution_id", + new=AsyncMock(return_value=["Example University"]), + ), patch( + "inse.dashboard.service.get_eu_organization_contributions", + new=AsyncMock(return_value={"P1": object()}), ), patch( "inse.dashboard.service.OrgPatentsSearchClient.top_authors_by_institution", new=AsyncMock(return_value=[]), ): - result = await get_top_authors(ORG_INSTITUTION_ID, "Example Org") + result = await get_top_authors( + ORG_INSTITUTION_ID, + "Example Org", + ) assert result.works[0].name == "Ada Example" assert result.eu_projects == [] @@ -272,6 +538,9 @@ async def test_top_authors_can_rank_works_by_last_five_years(): ), patch( "inse.dashboard.service.InstitutionsSearchClient.get_institution_aliases_by_oa_institution_id", new=AsyncMock(return_value=["Example University"]), + ), patch( + "inse.dashboard.service.get_eu_organization_contributions", + new=AsyncMock(return_value={}), ), patch( "inse.dashboard.service.OrgPatentsSearchClient.top_authors_by_institution", new=AsyncMock(return_value=[]), @@ -432,13 +701,15 @@ async def test_topic_heatmap_maps_field_year_counts_and_shares(): == "topics.field.id" ) assert ( - body["aggs"]["topics_nested"]["aggs"]["fields"]["aggs"] - ["subfields"]["terms"]["field"] + body["aggs"]["topics_nested"]["aggs"]["fields"]["aggs"]["subfields"]["terms"][ + "field" + ] == "topics.subfield.id" ) assert ( - body["aggs"]["topics_nested"]["aggs"]["fields"]["aggs"] - ["subfields"]["aggs"]["topics"]["terms"]["field"] + body["aggs"]["topics_nested"]["aggs"]["fields"]["aggs"]["subfields"][ + "aggs" + ]["topics"]["terms"]["field"] == "topics.id" ) subfield_aggs = body["aggs"]["topics_nested"]["aggs"]["fields"]["aggs"] -- 2.56.1 From d3266ee50e972db051bf898f52ef7b105887783c Mon Sep 17 00:00:00 2001 From: spsaefan <117728614+spsaefan@users.noreply.github.com> Date: Mon, 20 Jul 2026 20:37:40 +0200 Subject: [PATCH 02/10] Use EU project index for top authors --- service/src/main/inse/dashboard/service.py | 12 ++++-- .../src/main/inse/dashboard/test_service.py | 41 ++++++++++++------- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/service/src/main/inse/dashboard/service.py b/service/src/main/inse/dashboard/service.py index dde3ba29..384e0755 100644 --- a/service/src/main/inse/dashboard/service.py +++ b/service/src/main/inse/dashboard/service.py @@ -27,8 +27,9 @@ from .model import ( TopicWordCloud, TopicWordCloudWord, ) +from inse.projects.model import ProjectType, ProjectsFilteringProperties from inse.projects.projects_search_client import ( - get_eu_organization_contributions, + get_eu_organization_project_contributions, ) TOP_AUTHOR_LIMIT = 10 @@ -842,11 +843,16 @@ async def _top_patent_authors(institution_id: str) -> list[TopAuthor]: async def _top_eu_project_publication_authors( organization_names: list[str], ) -> list[TopAuthor]: - contributions = await get_eu_organization_contributions( + project_page = await get_eu_organization_project_contributions( organization_names=organization_names, + projects_filtering_properties=ProjectsFilteringProperties( + project_type=ProjectType.EU, + page=0, + size=10000, + ), ) - project_ids = list(contributions.keys()) + project_ids = [project.project_id for project in project_page.elements] if not project_ids: return [] diff --git a/service/src/main/inse/dashboard/test_service.py b/service/src/main/inse/dashboard/test_service.py index 0a03a574..93302fc2 100644 --- a/service/src/main/inse/dashboard/test_service.py +++ b/service/src/main/inse/dashboard/test_service.py @@ -1,5 +1,6 @@ from unittest.mock import AsyncMock, patch from datetime import datetime +from types import SimpleNamespace import pytest from fastapi import HTTPException @@ -244,12 +245,15 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects(): "inse.dashboard.service.InstitutionsSearchClient.get_institution_aliases_by_oa_institution_id", new=AsyncMock(return_value=["Example University"]), ), patch( - "inse.dashboard.service.get_eu_organization_contributions", + "inse.dashboard.service.get_eu_organization_project_contributions", new=AsyncMock( - return_value={ - "P1": object(), - "P2": object(), - } + return_value=SimpleNamespace( + elements=[ + SimpleNamespace(project_id="P1"), + SimpleNamespace(project_id="P2"), + SimpleNamespace(project_id="P3"), + ], + ) ), ), patch( "inse.dashboard.service.OrgPatentsSearchClient.top_authors_by_institution", @@ -306,6 +310,7 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects(): assert set(eu_body["query"]["terms"]["projectID"]) == { "P1", "P2", + "P3", } assert eu_body["_source"] == ["projectID", "doi", "qaProcessedDoi"] assert "aggs" not in eu_body @@ -429,12 +434,14 @@ async def test_top_eu_project_authors_count_filtered_projects_by_stable_author_i "inse.dashboard.service.InstitutionsSearchClient.get_institution_aliases_by_oa_institution_id", new=AsyncMock(return_value=["Example University"]), ), patch( - "inse.dashboard.service.get_eu_organization_contributions", + "inse.dashboard.service.get_eu_organization_project_contributions", new=AsyncMock( - return_value={ - "P1": object(), - "P2": object(), - } + return_value=SimpleNamespace( + elements=[ + SimpleNamespace(project_id="P1"), + SimpleNamespace(project_id="P2"), + ], + ) ), ), patch( "inse.dashboard.service.OrgPatentsSearchClient.top_authors_by_institution", @@ -479,8 +486,14 @@ async def test_top_authors_returns_partial_response_when_one_source_fails(): "inse.dashboard.service.InstitutionsSearchClient.get_institution_aliases_by_oa_institution_id", new=AsyncMock(return_value=["Example University"]), ), patch( - "inse.dashboard.service.get_eu_organization_contributions", - new=AsyncMock(return_value={"P1": object()}), + "inse.dashboard.service.get_eu_organization_project_contributions", + new=AsyncMock( + return_value=SimpleNamespace( + elements=[ + SimpleNamespace(project_id="P1"), + ], + ) + ), ), patch( "inse.dashboard.service.OrgPatentsSearchClient.top_authors_by_institution", new=AsyncMock(return_value=[]), @@ -539,8 +552,8 @@ async def test_top_authors_can_rank_works_by_last_five_years(): "inse.dashboard.service.InstitutionsSearchClient.get_institution_aliases_by_oa_institution_id", new=AsyncMock(return_value=["Example University"]), ), patch( - "inse.dashboard.service.get_eu_organization_contributions", - new=AsyncMock(return_value={}), + "inse.dashboard.service.get_eu_organization_project_contributions", + new=AsyncMock(return_value=SimpleNamespace(elements=[])), ), patch( "inse.dashboard.service.OrgPatentsSearchClient.top_authors_by_institution", new=AsyncMock(return_value=[]), -- 2.56.1 From f4e0888ce25987a4d64b2aeb7d2f5d072d09dc47 Mon Sep 17 00:00:00 2001 From: Farajollahi Date: Mon, 20 Jul 2026 22:50:31 +0200 Subject: [PATCH 03/10] fix organsaition id --- service/src/main/inse/dashboard/service.py | 23 +++++++-- .../src/main/inse/dashboard/test_service.py | 48 +++++++++++++++---- 2 files changed, 57 insertions(+), 14 deletions(-) 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}], } ], } -- 2.56.1 From 18c9785325a67c6dbb13f22e7084ebcfa918ca79 Mon Sep 17 00:00:00 2001 From: Farajollahi Date: Mon, 20 Jul 2026 23:59:08 +0200 Subject: [PATCH 04/10] change index --- service/src/main/inse/dashboard/service.py | 2 +- .../src/main/inse/dashboard/test_service.py | 48 ++++--------------- 2 files changed, 11 insertions(+), 39 deletions(-) diff --git a/service/src/main/inse/dashboard/service.py b/service/src/main/inse/dashboard/service.py index 39fbc8fb..dca250cc 100644 --- a/service/src/main/inse/dashboard/service.py +++ b/service/src/main/inse/dashboard/service.py @@ -859,7 +859,7 @@ async def _top_eu_project_publication_authors( return [] publication_response = await elasticsearch.search( - index=ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD, + index=ESIndex.CORDIS_EU_FP7_PUBLICATIONS, body={ "size": 10000, "_source": ["projectID", "doi", "qaProcessedDoi"], diff --git a/service/src/main/inse/dashboard/test_service.py b/service/src/main/inse/dashboard/test_service.py index e0553d25..93302fc2 100644 --- a/service/src/main/inse/dashboard/test_service.py +++ b/service/src/main/inse/dashboard/test_service.py @@ -208,15 +208,13 @@ 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}], + } }, ], } @@ -229,8 +227,7 @@ 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}], + } } ], } @@ -325,25 +322,14 @@ 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"]["bool"]["filter"][0]["terms"]["doi"] == [ + assert works_body["query"]["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", ] @@ -369,7 +355,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"]["bool"]["filter"][0]["terms"]["doi"] == [ + assert kwargs["body"]["query"]["terms"]["doi"] == [ "https://doi.org/10.1/p1-a", "https://doi.org/10.1/p1-b", "https://doi.org/10.1/p2", @@ -385,24 +371,13 @@ 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"} - ], + } }, ], } @@ -415,8 +390,7 @@ 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}], + } } ], } @@ -429,8 +403,7 @@ 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}], + } } ], } @@ -443,8 +416,7 @@ 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}], + } } ], } -- 2.56.1 From 167913aa85734f14da52673b5fd4358d224e540f Mon Sep 17 00:00:00 2001 From: Farajollahi Date: Tue, 21 Jul 2026 00:48:28 +0200 Subject: [PATCH 05/10] Align EU project author counts with person details --- service/src/main/inse/dashboard/service.py | 2 +- .../src/main/inse/dashboard/test_service.py | 26 +++-- .../inse/projects/projects_search_client.py | 38 ++++-- .../projects/test_projects_search_client.py | 108 ++++++++++++++++++ service/src/main/inse/works/logic.py | 2 +- 5 files changed, 157 insertions(+), 19 deletions(-) create mode 100644 service/src/main/inse/projects/test_projects_search_client.py diff --git a/service/src/main/inse/dashboard/service.py b/service/src/main/inse/dashboard/service.py index dca250cc..39fbc8fb 100644 --- a/service/src/main/inse/dashboard/service.py +++ b/service/src/main/inse/dashboard/service.py @@ -859,7 +859,7 @@ async def _top_eu_project_publication_authors( return [] publication_response = await elasticsearch.search( - index=ESIndex.CORDIS_EU_FP7_PUBLICATIONS, + index=ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD, body={ "size": 10000, "_source": ["projectID", "doi", "qaProcessedDoi"], diff --git a/service/src/main/inse/dashboard/test_service.py b/service/src/main/inse/dashboard/test_service.py index 93302fc2..54644a34 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,7 +325,7 @@ 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", ] @@ -330,6 +333,7 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects(): "doi", "authorships.author.id", "authorships.author.display_name", + "authorships.institutions.id", ] @@ -355,7 +359,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 +375,15 @@ 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}], }, ], } @@ -390,7 +396,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 +410,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}], } ], } diff --git a/service/src/main/inse/projects/projects_search_client.py b/service/src/main/inse/projects/projects_search_client.py index 22acb5bd..45b8e61b 100644 --- a/service/src/main/inse/projects/projects_search_client.py +++ b/service/src/main/inse/projects/projects_search_client.py @@ -487,12 +487,17 @@ def _get_raw_doi(doi: str) -> str: Returns: str: The raw DOI string. """ - if doi.startswith("https://doi.org/"): - return doi[len("https://doi.org/") :] - elif doi.startswith("http://dx.doi.org/"): - return doi[len("http://dx.doi.org/") :] - else: - return doi + normalized = doi.strip().lower() + for prefix in ( + "https://doi.org/", + "http://doi.org/", + "https://dx.doi.org/", + "http://dx.doi.org/", + "doi.org/", + ): + if normalized.startswith(prefix): + return normalized.removeprefix(prefix) + return normalized async def get_cordis_eu_publications_by_dois(dois: List[str]): @@ -504,12 +509,29 @@ async def get_cordis_eu_publications_by_dois(dois: List[str]): Returns: _type_: The retrieved publications. """ - raw_dois = [_get_raw_doi(doi) for doi in dois] - query = {"bool": {"filter": [{"terms": {"qaProcessedDoi": raw_dois}}]}} + raw_dois = sorted({_get_raw_doi(doi) for doi in dois if doi.strip()}) + canonical_dois = [f"https://doi.org/{doi}" for doi in raw_dois] + doi_values = sorted(set(raw_dois + canonical_dois)) + query = { + "bool": { + "filter": [ + { + "bool": { + "should": [ + {"terms": {"doi": doi_values}}, + {"terms": {"qaProcessedDoi": doi_values}}, + ], + "minimum_should_match": 1, + } + } + ] + } + } response = await elasticsearch.search( query=query, index=ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD, + size=10000, cluster=ESCluster.INSE, ) diff --git a/service/src/main/inse/projects/test_projects_search_client.py b/service/src/main/inse/projects/test_projects_search_client.py new file mode 100644 index 00000000..ef0ba980 --- /dev/null +++ b/service/src/main/inse/projects/test_projects_search_client.py @@ -0,0 +1,108 @@ +from unittest.mock import AsyncMock, patch + +import pytest + +from inse import config +from inse.model import ESCluster, ESIndex +from inse.projects.projects_search_client import ( + get_cordis_eu_project_ids_by_dois, +) +from inse.works.logic import get_publication_dois_of_author + + +@pytest.fixture +def anyio_backend(): + return "asyncio" + + +@pytest.mark.anyio +async def test_project_ids_match_raw_and_processed_cordis_dois(): + response = { + "hits": { + "hits": [ + {"_source": {"projectID": "P1", "doi": "10.1/first"}}, + { + "_source": { + "projectID": "P2", + "qaProcessedDoi": "10.1/second", + } + }, + {"_source": {"projectID": "P2", "doi": "10.1/second"}}, + ] + } + } + + with patch( + "inse.projects.projects_search_client.elasticsearch.search", + new=AsyncMock(return_value=response), + ) as search: + project_ids = await get_cordis_eu_project_ids_by_dois( + ["https://doi.org/10.1/FIRST", "doi.org/10.1/SECOND"] + ) + + assert set(project_ids) == {"P1", "P2"} + search.assert_awaited_once_with( + query={ + "bool": { + "filter": [ + { + "bool": { + "should": [ + { + "terms": { + "doi": [ + "10.1/first", + "10.1/second", + "https://doi.org/10.1/first", + "https://doi.org/10.1/second", + ] + } + }, + { + "terms": { + "qaProcessedDoi": [ + "10.1/first", + "10.1/second", + "https://doi.org/10.1/first", + "https://doi.org/10.1/second", + ] + } + }, + ], + "minimum_should_match": 1, + } + } + ] + } + }, + index=ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD, + size=10000, + cluster=ESCluster.INSE, + ) + + +@pytest.mark.anyio +async def test_author_dois_use_the_same_configured_works_index_as_dashboard(): + with patch( + "inse.works.logic.elasticsearch.search", + new=AsyncMock( + return_value={ + "hits": { + "hits": [ + {"_source": {"doi": "https://doi.org/10.1/example"}} + ] + } + } + ), + ) as search: + dois = await get_publication_dois_of_author("a123") + + assert dois == ["https://doi.org/10.1/example"] + assert search.await_args.kwargs["index"] == ( + config.settings.openalex_works_search_index + ) + assert search.await_args.kwargs["query"]["bool"]["must"][0]["nested"][ + "query" + ]["term"] == { + "authorships.author.id": "https://openalex.org/A123" + } diff --git a/service/src/main/inse/works/logic.py b/service/src/main/inse/works/logic.py index ec4f16af..641b230c 100644 --- a/service/src/main/inse/works/logic.py +++ b/service/src/main/inse/works/logic.py @@ -1015,7 +1015,7 @@ async def get_publication_dois_of_author( try: response = await elasticsearch.search( - index="openalex-works", + index=config.settings.openalex_works_search_index, source=["doi"], size=10000, query=query, -- 2.56.1 From de6339586f42353f65fd0633a226bcb9cebdc258 Mon Sep 17 00:00:00 2001 From: Farajollahi Date: Tue, 21 Jul 2026 00:59:34 +0200 Subject: [PATCH 06/10] Remove DFG top authors from dashboard --- service/src/main/inse/dashboard/model.py | 1 - service/src/main/inse/dashboard/service.py | 92 ------------ .../src/main/inse/dashboard/test_service.py | 142 +----------------- .../src/app/api/model/topAuthorStatistics.ts | 1 - .../dashboard/dashboard-statistics.service.ts | 1 - .../modules/dashboard/dashboard.component.ts | 5 - 6 files changed, 1 insertion(+), 241 deletions(-) diff --git a/service/src/main/inse/dashboard/model.py b/service/src/main/inse/dashboard/model.py index 272125ba..426c1402 100644 --- a/service/src/main/inse/dashboard/model.py +++ b/service/src/main/inse/dashboard/model.py @@ -28,7 +28,6 @@ class TopAuthorStatistics(BaseModel): institution_id: str works: list[TopAuthor] eu_projects: list[TopAuthor] - projects: list[TopAuthor] patents: list[TopAuthor] news: list[TopAuthor] diff --git a/service/src/main/inse/dashboard/service.py b/service/src/main/inse/dashboard/service.py index 8d6da399..6eed3b80 100644 --- a/service/src/main/inse/dashboard/service.py +++ b/service/src/main/inse/dashboard/service.py @@ -349,11 +349,6 @@ async def get_top_authors( _top_eu_project_publication_authors(normalized_id, aliases), ) - projects = await _safe_top_authors( - "projects", - _top_project_authors(authors, aliases), - ) - patents = await _safe_top_authors( "patents", _top_patent_authors(normalized_id), @@ -368,7 +363,6 @@ async def get_top_authors( institution_id=normalized_id, works=works, eu_projects=eu_projects, - projects=projects, patents=patents, news=news, ) @@ -830,92 +824,6 @@ async def _institution_authors(institution_id: str) -> list[dict[str, Any]]: ] -async def _top_project_authors( - authors: list[dict[str, Any]], - aliases: list[str], -) -> list[TopAuthor]: - if not aliases: - return [] - counts: dict[str, int] = {} - names: dict[str, str] = {} - for bucket in await _top_dfg_project_author_buckets(aliases): - key = str(bucket.get("key", "")).strip() - if key: - normalized_key = key.casefold() - names[normalized_key] = key - counts[normalized_key] = counts.get(normalized_key, 0) + int( - bucket.get("doc_count", 0) - ) - - institution_authors = {author["name"].casefold(): author for author in authors} - ranked = sorted( - ( - (name, count) - for name, count in counts.items() - if count > 0 - ), - key=lambda item: (-item[1], names[item[0]].casefold()), - ) - return [ - TopAuthor( - id=( - _short_author_id(institution_authors[name]["id"]) - if name in institution_authors - else None - ), - name=( - institution_authors[name]["name"] - if name in institution_authors - else names[name] - ), - count=count, - ) - for name, count in ranked[:TOP_AUTHOR_LIMIT] - ] - - -async def _top_dfg_project_author_buckets( - aliases: list[str], -) -> list[dict[str, Any]]: - response = await elasticsearch.search( - index=ESIndex.DFG_PROJECTS, - body={ - "size": 0, - "query": _dfg_institution_query(aliases), - "aggs": { - "people": { - "terms": { - "script": { - "lang": "painless", - "source": _dfg_involved_persons_script(), - }, - "size": 10000, - } - } - }, - }, - cluster=ESCluster.INSE, - ) - return ( - response.get("aggregations", {}).get("people", {}).get("buckets", []) - ) - - -def _dfg_involved_persons_script() -> str: - return ( - "def names = new HashSet(); " - "def value = params['_source'].containsKey('Beteiligte Personen') " - " ? params['_source']['Beteiligte Personen'] : null; " - "if (value instanceof String && value.length() > 0) { " - " for (name in value.splitOnToken(';')) { " - " def trimmed = name.trim(); " - " if (trimmed.length() > 0) { names.add(trimmed); } " - " } " - "} " - "return new ArrayList(names);" - ) - - async def _top_patent_authors(institution_id: str) -> list[TopAuthor]: client = OrgPatentsSearchClient() authors = await client.top_authors_by_institution( diff --git a/service/src/main/inse/dashboard/test_service.py b/service/src/main/inse/dashboard/test_service.py index 855b1b0a..6f6977a9 100644 --- a/service/src/main/inse/dashboard/test_service.py +++ b/service/src/main/inse/dashboard/test_service.py @@ -292,11 +292,6 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects(): {"id": "A2", "name": "Grace Example", "count": 2}, {"id": "A1", "name": "Ada Example", "count": 1}, ] - assert result.projects[0].model_dump() == { - "id": "A2", - "name": "Grace Example", - "count": 3, - } assert result.news[0].model_dump() == { "id": "A1", "name": "Ada Example", @@ -309,14 +304,7 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects(): if call.kwargs["index"] in {ESIndex.DFG_PROJECTS, ESIndex.PROJECTS_BUND_V1} ] - assert len(project_calls) == 1 - assert project_calls[0].kwargs["index"] == ESIndex.DFG_PROJECTS - assert project_calls[0].kwargs["body"]["query"]["bool"][ - "minimum_should_match" - ] == 1 - assert "Beteiligte Personen" in project_calls[0].kwargs["body"]["aggs"][ - "people" - ]["terms"]["script"]["source"] + assert project_calls == [] eu_calls = [ call @@ -492,134 +480,6 @@ async def test_top_eu_project_authors_count_filtered_projects_by_stable_author_i ] -@pytest.mark.anyio -async def test_top_authors_keeps_dfg_project_authors_without_openalex_match(): - async def search(**kwargs): - if kwargs["index"] == ESIndex.OPENALEX_AUTHORS: - return {"hits": {"hits": []}} - if kwargs["index"] == ESIndex.DFG_PROJECTS: - return { - "aggregations": { - "people": { - "buckets": [{"key": "Project Person", "doc_count": 9}] - } - } - } - return {"aggregations": {"people": {"buckets": []}}} - - with patch( - "inse.dashboard.service.elasticsearch.search", - new=AsyncMock(side_effect=search), - ), patch( - "inse.dashboard.service.InstitutionsSearchClient.get_institution_aliases_by_oa_institution_id", - new=AsyncMock(return_value=["Example University"]), - ), patch( - "inse.dashboard.service.get_eu_organization_project_contributions", - new=AsyncMock(return_value=SimpleNamespace(elements=[])), - ), patch( - "inse.dashboard.service.OrgPatentsSearchClient.top_authors_by_institution", - new=AsyncMock(return_value=[]), - ): - result = await get_top_authors(ORG_INSTITUTION_ID, "Example Org") - - assert result.projects[0].model_dump() == { - "id": None, - "name": "Project Person", - "count": 9, - } - - -@pytest.mark.anyio -async def test_top_authors_returns_top_ten_dfg_project_people_by_project_count(): - async def search(**kwargs): - if kwargs["index"] == ESIndex.OPENALEX_AUTHORS: - return {"hits": {"hits": []}} - if kwargs["index"] == ESIndex.DFG_PROJECTS: - return { - "aggregations": { - "people": { - "buckets": [ - {"key": f"Project Person {count}", "doc_count": count} - for count in range(1, 12) - ] - } - } - } - return {"aggregations": {"people": {"buckets": []}}} - - with patch( - "inse.dashboard.service.elasticsearch.search", - new=AsyncMock(side_effect=search), - ), patch( - "inse.dashboard.service.InstitutionsSearchClient.get_institution_aliases_by_oa_institution_id", - new=AsyncMock(return_value=["Example University"]), - ), patch( - "inse.dashboard.service.get_eu_organization_project_contributions", - new=AsyncMock(return_value=SimpleNamespace(elements=[])), - ), patch( - "inse.dashboard.service.OrgPatentsSearchClient.top_authors_by_institution", - new=AsyncMock(return_value=[]), - ): - result = await get_top_authors(ORG_INSTITUTION_ID, "Example Org") - - assert [author.count for author in result.projects] == list(range(11, 1, -1)) - assert result.projects[0].name == "Project Person 11" - assert len(result.projects) == 10 - - -@pytest.mark.anyio -async def test_top_authors_combines_dfg_involved_people_case_insensitively(): - async def search(**kwargs): - if kwargs["index"] == ESIndex.OPENALEX_AUTHORS: - return { - "hits": { - "hits": [ - { - "_source": { - "id": "https://openalex.org/A1", - "display_name": "Ada Example", - "works_count": 1, - "counts_by_year": [], - } - } - ] - } - } - if kwargs["index"] == ESIndex.DFG_PROJECTS: - return { - "aggregations": { - "people": { - "buckets": [ - {"key": "Ada Example", "doc_count": 2}, - {"key": "ada example", "doc_count": 3}, - {"key": "DFG Person", "doc_count": 4}, - ] - } - } - } - return {"aggregations": {"authors": {"buckets": []}}} - - with patch( - "inse.dashboard.service.elasticsearch.search", - new=AsyncMock(side_effect=search), - ), patch( - "inse.dashboard.service.InstitutionsSearchClient.get_institution_aliases_by_oa_institution_id", - new=AsyncMock(return_value=["Example University"]), - ), patch( - "inse.dashboard.service.get_eu_organization_project_contributions", - new=AsyncMock(return_value=SimpleNamespace(elements=[])), - ), patch( - "inse.dashboard.service.OrgPatentsSearchClient.top_authors_by_institution", - new=AsyncMock(return_value=[]), - ): - result = await get_top_authors(ORG_INSTITUTION_ID, "Example Org") - - assert [author.model_dump() for author in result.projects] == [ - {"id": "A1", "name": "Ada Example", "count": 5}, - {"id": None, "name": "DFG Person", "count": 4}, - ] - - @pytest.mark.anyio async def test_top_authors_returns_partial_response_when_one_source_fails(): async def search(**kwargs): diff --git a/webapp/src/app/api/model/topAuthorStatistics.ts b/webapp/src/app/api/model/topAuthorStatistics.ts index 5437d1be..3d06243b 100644 --- a/webapp/src/app/api/model/topAuthorStatistics.ts +++ b/webapp/src/app/api/model/topAuthorStatistics.ts @@ -13,7 +13,6 @@ export interface TopAuthorStatistics { institution_id: string; works: Array; eu_projects: Array; - projects: Array; patents: Array; news: Array; } diff --git a/webapp/src/app/modules/dashboard/dashboard-statistics.service.ts b/webapp/src/app/modules/dashboard/dashboard-statistics.service.ts index df6588ad..aa23dd73 100644 --- a/webapp/src/app/modules/dashboard/dashboard-statistics.service.ts +++ b/webapp/src/app/modules/dashboard/dashboard-statistics.service.ts @@ -33,7 +33,6 @@ export interface TopAuthorStatistics { institution_id: string; works: TopAuthor[]; eu_projects: TopAuthor[]; - projects: TopAuthor[]; patents: TopAuthor[]; news: TopAuthor[]; } diff --git a/webapp/src/app/modules/dashboard/dashboard.component.ts b/webapp/src/app/modules/dashboard/dashboard.component.ts index 96c29d51..b91ee6df 100644 --- a/webapp/src/app/modules/dashboard/dashboard.component.ts +++ b/webapp/src/app/modules/dashboard/dashboard.component.ts @@ -110,11 +110,6 @@ export class DashboardComponent implements OnInit { icon: 'public', entries: this.topAuthors?.eu_projects ?? [], }, - { - title: 'DFG-Projekte', - icon: 'account_balance', - entries: this.topAuthors?.projects ?? [], - }, { title: 'Patente', icon: 'description', -- 2.56.1 From 9ac618ebeca3067af31b8964d8f32a231f263fbe Mon Sep 17 00:00:00 2001 From: Farajollahi Date: Tue, 21 Jul 2026 01:05:55 +0200 Subject: [PATCH 07/10] Revert "Align EU project author counts with person details" This reverts commit 167913aa85734f14da52673b5fd4358d224e540f. --- service/src/main/inse/dashboard/service.py | 2 +- .../src/main/inse/dashboard/test_service.py | 26 ++--- .../inse/projects/projects_search_client.py | 38 ++---- .../projects/test_projects_search_client.py | 108 ------------------ service/src/main/inse/works/logic.py | 2 +- 5 files changed, 19 insertions(+), 157 deletions(-) delete mode 100644 service/src/main/inse/projects/test_projects_search_client.py diff --git a/service/src/main/inse/dashboard/service.py b/service/src/main/inse/dashboard/service.py index 6eed3b80..70e77b6c 100644 --- a/service/src/main/inse/dashboard/service.py +++ b/service/src/main/inse/dashboard/service.py @@ -859,7 +859,7 @@ async def _top_eu_project_publication_authors( return [] publication_response = await elasticsearch.search( - index=ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD, + index=ESIndex.CORDIS_EU_FP7_PUBLICATIONS, body={ "size": 10000, "_source": ["projectID", "doi", "qaProcessedDoi"], diff --git a/service/src/main/inse/dashboard/test_service.py b/service/src/main/inse/dashboard/test_service.py index 6f6977a9..6ccb4937 100644 --- a/service/src/main/inse/dashboard/test_service.py +++ b/service/src/main/inse/dashboard/test_service.py @@ -215,15 +215,13 @@ 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}], + } }, ], } @@ -236,8 +234,7 @@ 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}], + } } ], } @@ -329,7 +326,7 @@ 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"]["bool"]["filter"][0]["terms"]["doi"] == [ + assert works_body["query"]["terms"]["doi"] == [ "https://doi.org/10.1234/p1", "https://doi.org/10.1234/p2", ] @@ -337,7 +334,6 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects(): "doi", "authorships.author.id", "authorships.author.display_name", - "authorships.institutions.id", ] @@ -362,7 +358,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"]["bool"]["filter"][0]["terms"]["doi"] == [ + assert kwargs["body"]["query"]["terms"]["doi"] == [ "https://doi.org/10.1/p1-a", "https://doi.org/10.1/p1-b", "https://doi.org/10.1/p2", @@ -378,15 +374,13 @@ 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}], + } }, ], } @@ -399,8 +393,7 @@ 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}], + } } ], } @@ -413,8 +406,7 @@ 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}], + } } ], } diff --git a/service/src/main/inse/projects/projects_search_client.py b/service/src/main/inse/projects/projects_search_client.py index 4b998ef3..3a0a7557 100644 --- a/service/src/main/inse/projects/projects_search_client.py +++ b/service/src/main/inse/projects/projects_search_client.py @@ -488,17 +488,12 @@ def _get_raw_doi(doi: str) -> str: Returns: str: The raw DOI string. """ - normalized = doi.strip().lower() - for prefix in ( - "https://doi.org/", - "http://doi.org/", - "https://dx.doi.org/", - "http://dx.doi.org/", - "doi.org/", - ): - if normalized.startswith(prefix): - return normalized.removeprefix(prefix) - return normalized + if doi.startswith("https://doi.org/"): + return doi[len("https://doi.org/") :] + elif doi.startswith("http://dx.doi.org/"): + return doi[len("http://dx.doi.org/") :] + else: + return doi async def get_cordis_eu_publications_by_dois(dois: List[str]): @@ -510,29 +505,12 @@ async def get_cordis_eu_publications_by_dois(dois: List[str]): Returns: _type_: The retrieved publications. """ - raw_dois = sorted({_get_raw_doi(doi) for doi in dois if doi.strip()}) - canonical_dois = [f"https://doi.org/{doi}" for doi in raw_dois] - doi_values = sorted(set(raw_dois + canonical_dois)) - query = { - "bool": { - "filter": [ - { - "bool": { - "should": [ - {"terms": {"doi": doi_values}}, - {"terms": {"qaProcessedDoi": doi_values}}, - ], - "minimum_should_match": 1, - } - } - ] - } - } + raw_dois = [_get_raw_doi(doi) for doi in dois] + query = {"bool": {"filter": [{"terms": {"qaProcessedDoi": raw_dois}}]}} response = await elasticsearch.search( query=query, index=ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD, - size=10000, cluster=ESCluster.INSE, ) diff --git a/service/src/main/inse/projects/test_projects_search_client.py b/service/src/main/inse/projects/test_projects_search_client.py deleted file mode 100644 index ef0ba980..00000000 --- a/service/src/main/inse/projects/test_projects_search_client.py +++ /dev/null @@ -1,108 +0,0 @@ -from unittest.mock import AsyncMock, patch - -import pytest - -from inse import config -from inse.model import ESCluster, ESIndex -from inse.projects.projects_search_client import ( - get_cordis_eu_project_ids_by_dois, -) -from inse.works.logic import get_publication_dois_of_author - - -@pytest.fixture -def anyio_backend(): - return "asyncio" - - -@pytest.mark.anyio -async def test_project_ids_match_raw_and_processed_cordis_dois(): - response = { - "hits": { - "hits": [ - {"_source": {"projectID": "P1", "doi": "10.1/first"}}, - { - "_source": { - "projectID": "P2", - "qaProcessedDoi": "10.1/second", - } - }, - {"_source": {"projectID": "P2", "doi": "10.1/second"}}, - ] - } - } - - with patch( - "inse.projects.projects_search_client.elasticsearch.search", - new=AsyncMock(return_value=response), - ) as search: - project_ids = await get_cordis_eu_project_ids_by_dois( - ["https://doi.org/10.1/FIRST", "doi.org/10.1/SECOND"] - ) - - assert set(project_ids) == {"P1", "P2"} - search.assert_awaited_once_with( - query={ - "bool": { - "filter": [ - { - "bool": { - "should": [ - { - "terms": { - "doi": [ - "10.1/first", - "10.1/second", - "https://doi.org/10.1/first", - "https://doi.org/10.1/second", - ] - } - }, - { - "terms": { - "qaProcessedDoi": [ - "10.1/first", - "10.1/second", - "https://doi.org/10.1/first", - "https://doi.org/10.1/second", - ] - } - }, - ], - "minimum_should_match": 1, - } - } - ] - } - }, - index=ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD, - size=10000, - cluster=ESCluster.INSE, - ) - - -@pytest.mark.anyio -async def test_author_dois_use_the_same_configured_works_index_as_dashboard(): - with patch( - "inse.works.logic.elasticsearch.search", - new=AsyncMock( - return_value={ - "hits": { - "hits": [ - {"_source": {"doi": "https://doi.org/10.1/example"}} - ] - } - } - ), - ) as search: - dois = await get_publication_dois_of_author("a123") - - assert dois == ["https://doi.org/10.1/example"] - assert search.await_args.kwargs["index"] == ( - config.settings.openalex_works_search_index - ) - assert search.await_args.kwargs["query"]["bool"]["must"][0]["nested"][ - "query" - ]["term"] == { - "authorships.author.id": "https://openalex.org/A123" - } diff --git a/service/src/main/inse/works/logic.py b/service/src/main/inse/works/logic.py index 641b230c..ec4f16af 100644 --- a/service/src/main/inse/works/logic.py +++ b/service/src/main/inse/works/logic.py @@ -1015,7 +1015,7 @@ async def get_publication_dois_of_author( try: response = await elasticsearch.search( - index=config.settings.openalex_works_search_index, + index="openalex-works", source=["doi"], size=10000, query=query, -- 2.56.1 From a7ced052dd3ab2cf60175c4148fe71f04b713c2b Mon Sep 17 00:00:00 2001 From: Farajollahi Date: Tue, 21 Jul 2026 09:20:07 +0200 Subject: [PATCH 08/10] Verify EU author counts by unique project --- service/src/main/inse/dashboard/service.py | 2 + .../src/main/inse/dashboard/test_service.py | 84 ++++++++++++++++--- 2 files changed, 74 insertions(+), 12 deletions(-) diff --git a/service/src/main/inse/dashboard/service.py b/service/src/main/inse/dashboard/service.py index 70e77b6c..1035012e 100644 --- a/service/src/main/inse/dashboard/service.py +++ b/service/src/main/inse/dashboard/service.py @@ -943,6 +943,8 @@ def _openalex_work_authors_by_eu_projects( doi_project_ids: dict[str, set[str]], institution_id: str, ) -> list[TopAuthor]: + # Count unique CORDIS projects per author, not publications. Multiple works + # (and therefore multiple DOIs) can belong to the same project ID. author_project_ids: dict[str, set[str]] = defaultdict(set) author_names: dict[str, str] = {} diff --git a/service/src/main/inse/dashboard/test_service.py b/service/src/main/inse/dashboard/test_service.py index 6ccb4937..72ad4774 100644 --- a/service/src/main/inse/dashboard/test_service.py +++ b/service/src/main/inse/dashboard/test_service.py @@ -9,6 +9,7 @@ from sqlmodel import Session, SQLModel, create_engine from inse import config from inse.auth.model import AccessToken, Organization, UserRole from inse.dashboard.service import ( + _openalex_work_authors_by_eu_projects, get_dashboard_organization, get_dashboard_statistics, get_top_authors, @@ -174,7 +175,7 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects(): } } } - if kwargs["index"] == ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD: + if kwargs["index"] == ESIndex.CORDIS_EU_FP7_PUBLICATIONS: return { "hits": { "hits": [ @@ -215,13 +216,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}], }, ], } @@ -234,7 +237,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}], } ], } @@ -306,7 +310,7 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects(): eu_calls = [ call for call in mocked_search.await_args_list - if call.kwargs["index"] == ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD + if call.kwargs["index"] == ESIndex.CORDIS_EU_FP7_PUBLICATIONS ] assert len(eu_calls) == 1 @@ -326,7 +330,7 @@ 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", ] @@ -334,6 +338,58 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects(): "doi", "authorships.author.id", "authorships.author.display_name", + "authorships.institutions.id", + ] + + +def test_eu_author_count_groups_multiple_publications_by_project_id(): + works_response = { + "hits": { + "hits": [ + { + "_source": { + "doi": "https://doi.org/10.1/marcus-a", + "authorships": [ + { + "author": { + "id": "https://openalex.org/A5007170803", + "display_name": "Marcus Example", + }, + "institutions": [{"id": ORG_INSTITUTION_ID}], + } + ], + } + }, + { + "_source": { + "doi": "https://doi.org/10.1/marcus-b", + "authorships": [ + { + "author": { + "id": "https://openalex.org/A5007170803", + "display_name": "Marcus Example", + }, + "institutions": [{"id": ORG_INSTITUTION_ID}], + } + ], + } + }, + ] + } + } + doi_project_ids = { + "https://doi.org/10.1/marcus-a": {"P1"}, + "https://doi.org/10.1/marcus-b": {"P1"}, + } + + result = _openalex_work_authors_by_eu_projects( + works_response, + doi_project_ids, + ORG_INSTITUTION_ID, + ) + + assert [author.model_dump() for author in result] == [ + {"id": "A5007170803", "name": "Marcus Example", "count": 1} ] @@ -342,7 +398,7 @@ async def test_top_eu_project_authors_count_filtered_projects_by_stable_author_i async def search(**kwargs): if kwargs["index"] == ESIndex.OPENALEX_AUTHORS: return {"hits": {"hits": []}} - if kwargs["index"] == ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD: + if kwargs["index"] == ESIndex.CORDIS_EU_FP7_PUBLICATIONS: assert kwargs["body"]["query"] == { "terms": {"projectID": ["P1", "P2"]} } @@ -358,7 +414,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", @@ -374,13 +430,15 @@ 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}], }, ], } @@ -393,7 +451,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}], } ], } @@ -406,7 +465,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}], } ], } -- 2.56.1 From d4880b417b47028a9614722319c951e3f4342ba6 Mon Sep 17 00:00:00 2001 From: Farajollahi Date: Tue, 21 Jul 2026 09:33:18 +0200 Subject: [PATCH 09/10] Align dashboard EU authors with person details --- service/src/main/inse/dashboard/service.py | 13 +++--- .../src/main/inse/dashboard/test_service.py | 44 ++++++++++++++----- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/service/src/main/inse/dashboard/service.py b/service/src/main/inse/dashboard/service.py index 1035012e..2338b16f 100644 --- a/service/src/main/inse/dashboard/service.py +++ b/service/src/main/inse/dashboard/service.py @@ -859,10 +859,10 @@ async def _top_eu_project_publication_authors( return [] publication_response = await elasticsearch.search( - index=ESIndex.CORDIS_EU_FP7_PUBLICATIONS, + index=ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD, body={ "size": 10000, - "_source": ["projectID", "doi", "qaProcessedDoi"], + "_source": ["projectID", "qaProcessedDoi"], "query": { "terms": { "projectID": project_ids, @@ -880,7 +880,7 @@ async def _top_eu_project_publication_authors( return [] works_response = await elasticsearch.search( - index=config.settings.openalex_works_search_index, + index="openalex-works", body={ "size": 10000, "_source": [ @@ -918,10 +918,9 @@ def _cordis_publication_doi_project_ids( project_id = str(source.get("projectID") or "").strip() if not project_id or project_id not in allowed_project_ids: continue - for field in ("doi", "qaProcessedDoi"): - doi = _normalize_doi(source.get(field)) - if doi: - doi_project_ids[doi].add(project_id) + doi = _normalize_doi(source.get("qaProcessedDoi")) + if doi: + doi_project_ids[doi].add(project_id) return dict(doi_project_ids) diff --git a/service/src/main/inse/dashboard/test_service.py b/service/src/main/inse/dashboard/test_service.py index 72ad4774..543af0d7 100644 --- a/service/src/main/inse/dashboard/test_service.py +++ b/service/src/main/inse/dashboard/test_service.py @@ -175,14 +175,14 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects(): } } } - if kwargs["index"] == ESIndex.CORDIS_EU_FP7_PUBLICATIONS: + if kwargs["index"] == ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD: return { "hits": { "hits": [ { "_source": { "projectID": "P1", - "doi": "10.1234/p1", + "qaProcessedDoi": "10.1234/p1", } }, { @@ -204,7 +204,7 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects(): } } - if kwargs["index"] == config.settings.openalex_works_search_index: + if kwargs["index"] == "openalex-works": return { "hits": { "hits": [ @@ -310,7 +310,7 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects(): eu_calls = [ call for call in mocked_search.await_args_list - if call.kwargs["index"] == ESIndex.CORDIS_EU_FP7_PUBLICATIONS + if call.kwargs["index"] == ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD ] assert len(eu_calls) == 1 @@ -320,13 +320,13 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects(): "P2", "P3", } - assert eu_body["_source"] == ["projectID", "doi", "qaProcessedDoi"] + assert eu_body["_source"] == ["projectID", "qaProcessedDoi"] assert "aggs" not in eu_body works_calls = [ call for call in mocked_search.await_args_list - if call.kwargs["index"] == config.settings.openalex_works_search_index + if call.kwargs["index"] == "openalex-works" ] assert len(works_calls) == 1 works_body = works_calls[0].kwargs["body"] @@ -398,22 +398,42 @@ async def test_top_eu_project_authors_count_filtered_projects_by_stable_author_i async def search(**kwargs): if kwargs["index"] == ESIndex.OPENALEX_AUTHORS: return {"hits": {"hits": []}} - if kwargs["index"] == ESIndex.CORDIS_EU_FP7_PUBLICATIONS: + if kwargs["index"] == ESIndex.CORDIS_EU_PUBLICATIONS_WILDCARD: assert kwargs["body"]["query"] == { "terms": {"projectID": ["P1", "P2"]} } return { "hits": { "hits": [ - {"_source": {"projectID": "P1", "doi": "10.1/P1-A"}}, - {"_source": {"projectID": "P1", "doi": "10.1/P1-B"}}, - {"_source": {"projectID": "P2", "doi": "10.1/P2"}}, - {"_source": {"projectID": "OUTSIDE", "doi": "10.1/OUT"}}, + { + "_source": { + "projectID": "P1", + "qaProcessedDoi": "10.1/P1-A", + } + }, + { + "_source": { + "projectID": "P1", + "qaProcessedDoi": "10.1/P1-B", + } + }, + { + "_source": { + "projectID": "P2", + "qaProcessedDoi": "10.1/P2", + } + }, + { + "_source": { + "projectID": "OUTSIDE", + "qaProcessedDoi": "10.1/OUT", + } + }, ] } } - if kwargs["index"] == config.settings.openalex_works_search_index: + if kwargs["index"] == "openalex-works": assert kwargs["body"]["query"]["bool"]["filter"][0]["terms"]["doi"] == [ "https://doi.org/10.1/p1-a", "https://doi.org/10.1/p1-b", -- 2.56.1 From 0dd20232246c2a93cf5a374663ffd3ab19a940dc Mon Sep 17 00:00:00 2001 From: Farajollahi Date: Tue, 21 Jul 2026 09:41:27 +0200 Subject: [PATCH 10/10] Restrict EU top authors to user institution --- service/src/main/inse/dashboard/service.py | 19 +++++++++-- .../src/main/inse/dashboard/test_service.py | 33 +++++++++++++++++-- 2 files changed, 48 insertions(+), 4 deletions(-) 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"]} -- 2.56.1