fix dfg projects analyse #210

Merged
farajollahi merged 1 commits from dfg-projects-analyse into dev 2026-07-12 20:20:08 +02:00
6 changed files with 92 additions and 29 deletions
+1
View File
@@ -10,6 +10,7 @@ dependencies = [
"fastapi~=0.115.0",
"httpx~=0.27.0",
"openai~=2.41.0",
"pypdf==6.14.2",
"pydantic~=2.7.1",
"pydantic-settings~=2.2.1",
"pyjwt~=2.8.0",
+12 -14
View File
@@ -599,7 +599,7 @@ async def _top_project_authors(
return []
counts: dict[str, int] = {}
names: dict[str, str] = {}
for bucket in await _top_bund_project_author_buckets(aliases):
for bucket in await _top_dfg_project_author_buckets(aliases):
key = str(bucket.get("key", "")).strip()
if key:
normalized_key = key.casefold()
@@ -635,20 +635,20 @@ async def _top_project_authors(
]
async def _top_bund_project_author_buckets(
async def _top_dfg_project_author_buckets(
aliases: list[str],
) -> list[dict[str, Any]]:
response = await elasticsearch.search(
index=ESIndex.PROJECTS_BUND_V1,
index=ESIndex.PROJECTS_DFG_ALL,
body={
"size": 0,
"query": _alias_match_query("Zuwendungsempfänger", aliases),
"query": _dfg_institution_query(aliases),
"aggs": {
"people": {
"terms": {
"script": {
"lang": "painless",
"source": _bund_persons_script(),
"source": _dfg_involved_persons_script(),
},
"size": 10000,
}
@@ -662,17 +662,15 @@ async def _top_bund_project_author_buckets(
)
def _bund_persons_script() -> str:
def _dfg_involved_persons_script() -> str:
return (
"def names = new HashSet(); "
"if (params['_source'].containsKey('Arb') && params['_source']['Arb'] != null) { "
" for (entry in params['_source']['Arb'].entrySet()) { "
" def value = entry.getValue(); "
" if (value instanceof String && value.length() > 0) { "
" if (value.contains(';')) { "
" for (name in value.splitOnToken(';')) { names.add(name.trim()); } "
" } else { names.add(value.trim()); } "
" } "
"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);"
+63 -13
View File
@@ -98,7 +98,7 @@ async def test_dashboard_statistics_counts_organization_data_and_all_projects():
@pytest.mark.anyio
async def test_top_authors_maps_works_and_bund_project_aggregations():
async def test_top_authors_maps_works_and_dfg_project_aggregations():
async def search(**kwargs):
if kwargs["index"] == ESIndex.OPENALEX_AUTHORS:
return {
@@ -131,7 +131,7 @@ async def test_top_authors_maps_works_and_bund_project_aggregations():
}
}
}
if kwargs["index"] == ESIndex.PROJECTS_BUND_V1:
if kwargs["index"] == ESIndex.PROJECTS_DFG_ALL:
return {
"aggregations": {
"people": {
@@ -184,21 +184,21 @@ async def test_top_authors_maps_works_and_bund_project_aggregations():
in {ESIndex.PROJECTS_DFG_ALL, ESIndex.PROJECTS_BUND_V1}
]
assert len(project_calls) == 1
assert project_calls[0].kwargs["index"] == ESIndex.PROJECTS_BUND_V1
assert project_calls[0].kwargs["body"]["query"]["bool"]["should"][0]["match"] == {
"Zuwendungsempfänger": {
"query": "Example Org",
"operator": "and",
}
}
assert project_calls[0].kwargs["index"] == ESIndex.PROJECTS_DFG_ALL
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"]
@pytest.mark.anyio
async def test_top_authors_keeps_bund_project_authors_without_openalex_match():
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.PROJECTS_BUND_V1:
if kwargs["index"] == ESIndex.PROJECTS_DFG_ALL:
return {
"aggregations": {
"people": {
@@ -228,11 +228,11 @@ async def test_top_authors_keeps_bund_project_authors_without_openalex_match():
@pytest.mark.anyio
async def test_top_authors_returns_top_ten_bund_project_people_by_project_count():
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.PROJECTS_BUND_V1:
if kwargs["index"] == ESIndex.PROJECTS_DFG_ALL:
return {
"aggregations": {
"people": {
@@ -262,6 +262,56 @@ async def test_top_authors_returns_top_ten_bund_project_people_by_project_count(
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.PROJECTS_DFG_ALL:
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.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):
+11
View File
@@ -799,6 +799,15 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/2b/4f/e04a8067c7c96c364cef7ef73906504e2f40d690811c021e1a1901473a19/PyJWT-2.8.0-py3-none-any.whl", hash = "sha256:59127c392cc44c2da5bb3192169a91f429924e17aff6534d70fdc02ab3e04320", size = 22591, upload-time = "2023-07-18T20:02:21.561Z" },
]
[[package]]
name = "pypdf"
version = "6.14.2"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/03/72/7dfd5ff1c9c37de97a731701f51af091325f123d9d4270361c9c69e4431f/pypdf-6.14.2.tar.gz", hash = "sha256:7873f502fe4385e79539b21d872392dc0c4e3714327c15881cbc7fbfd1f95b25", size = 6491182, upload-time = "2026-06-23T14:18:30.859Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/49/e6/136aa8993a2ae7214e0b0ef2edaa0d2e08d1d4e4982635b08a835ff31ec8/pypdf-6.14.2-py3-none-any.whl", hash = "sha256:3f07891af76dc002657e04993ab9b4de81de29f9013b9761d0b7968bff12e946", size = 349514, upload-time = "2026-06-23T14:18:28.867Z" },
]
[[package]]
name = "pytest"
version = "8.1.2"
@@ -1137,6 +1146,7 @@ dependencies = [
{ name = "pydantic" },
{ name = "pydantic-settings" },
{ name = "pyjwt" },
{ name = "pypdf" },
{ name = "pytest" },
{ name = "python-multipart" },
{ name = "pyyaml" },
@@ -1158,6 +1168,7 @@ requires-dist = [
{ name = "pydantic", specifier = "~=2.7.1" },
{ name = "pydantic-settings", specifier = "~=2.2.1" },
{ name = "pyjwt", specifier = "~=2.8.0" },
{ name = "pypdf", specifier = "==6.14.2" },
{ name = "pytest", specifier = "~=8.1.0" },
{ name = "python-multipart", specifier = "~=0.0.19" },
{ name = "pyyaml", specifier = "~=6.0.1" },
@@ -174,7 +174,10 @@
<header class="top-authors-header">
<div>
<h2>Top 10 Personen aller Zeiten</h2>
<p>Nach Anzahl der Works und Projekte Ihrer Organisation.</p>
<p>
Nach Anzahl der Works und DFG-Projekte Ihrer Organisation. Bei
DFG-Projekten werden beteiligte Personen gezählt.
</p>
</div>
<div class="top-authors-period-toggle" aria-label="Works-Zeitraum">
<button
@@ -106,7 +106,7 @@ export class DashboardComponent implements OnInit {
entries: this.topAuthors?.works ?? [],
},
{
title: 'Projekte',
title: 'DFG-Projekte',
icon: 'work',
entries: this.topAuthors?.projects ?? [],
},