Merge branch 'check-final-timeline' into check-dashboard
# Conflicts: # service/src/main/inse/dashboard/service.py # service/src/main/inse/dashboard/test_service.py
This commit is contained in:
@@ -31,6 +31,7 @@ from inse.projects.model import (
|
||||
ProjectsFilteringProperties,
|
||||
ProjectType,
|
||||
)
|
||||
from inse.works.v2 import service as works_v2_service
|
||||
|
||||
from .institute_helper import get_openalex_institution
|
||||
from .model import (
|
||||
@@ -265,54 +266,34 @@ async def get_dashboard_publications(
|
||||
) -> DashboardStatisticValue:
|
||||
normalized_id = _normalize_institution_id(institution_id)
|
||||
period = _dashboard_period(from_year, to_year)
|
||||
if period == (None, None):
|
||||
institution = await institutions_v2_service.get_institution_by_id_or_raise(
|
||||
raw_oa_institution_id=_raw_institution_id(normalized_id),
|
||||
source=["id", "works_count"],
|
||||
)
|
||||
return DashboardStatisticValue(
|
||||
institution_id=normalized_id,
|
||||
organization_name=organization_name,
|
||||
value=int(institution.works_count or 0),
|
||||
)
|
||||
|
||||
first_year = period[0] if period[0] is not None else 1900
|
||||
last_year = period[1] if period[1] is not None else 2100
|
||||
publication_years = list(range(first_year, last_year + 1))
|
||||
|
||||
value = await elasticsearch.count(
|
||||
must = [
|
||||
works_v2_service._institutions_nested_query([normalized_id]),
|
||||
]
|
||||
if publication_years := _dashboard_publication_years(period):
|
||||
must.append({"terms": {"publication_year": publication_years}})
|
||||
response = await elasticsearch.search(
|
||||
index=ESIndex.OPENALEX_WORKS_WILDCARD,
|
||||
query={
|
||||
"bool": {
|
||||
"filter": [
|
||||
{
|
||||
"nested": {
|
||||
"path": "authorships",
|
||||
"query": {
|
||||
"nested": {
|
||||
"path": "authorships.institutions",
|
||||
"query": {
|
||||
"term": {
|
||||
"authorships.institutions.id": normalized_id
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
{"terms": {"publication_year": publication_years}},
|
||||
]
|
||||
}
|
||||
body={
|
||||
"size": 0,
|
||||
"track_total_hits": True,
|
||||
"query": {"bool": {"must": must}},
|
||||
},
|
||||
cluster=ESCluster.NES,
|
||||
)
|
||||
|
||||
return DashboardStatisticValue(
|
||||
institution_id=normalized_id,
|
||||
organization_name=organization_name,
|
||||
value=value,
|
||||
value=_total(response),
|
||||
)
|
||||
|
||||
|
||||
def _dashboard_publication_years(period: DashboardPeriod) -> list[int] | None:
|
||||
from_year, to_year = period
|
||||
if from_year is None and to_year is None:
|
||||
return None
|
||||
return list(range(from_year or 1900, (to_year or 2100) + 1))
|
||||
|
||||
|
||||
async def get_dashboard_persons(
|
||||
institution_id: str,
|
||||
organization_name: str,
|
||||
@@ -1459,7 +1440,10 @@ async def get_top_authors(
|
||||
to_year = datetime.now().year
|
||||
period = _dashboard_period(from_year, to_year)
|
||||
authors = await _safe_institution_authors(normalized_id)
|
||||
works = _top_work_authors(authors, period)
|
||||
works = await _safe_top_authors(
|
||||
"works",
|
||||
_top_work_authors(normalized_id, period, authors),
|
||||
)
|
||||
projects = await _safe_top_authors(
|
||||
"projects",
|
||||
_top_project_authors(
|
||||
@@ -1875,51 +1859,81 @@ def _short_author_id(value: str) -> str:
|
||||
return value.replace("https://openalex.org/", "").strip().upper()
|
||||
|
||||
|
||||
def _top_work_authors(
|
||||
authors: list[dict[str, Any]],
|
||||
async def _top_work_authors(
|
||||
institution_id: str,
|
||||
period: DashboardPeriod,
|
||||
institution_authors: list[dict[str, Any]],
|
||||
) -> list[TopAuthor]:
|
||||
if period != (None, None):
|
||||
ranked_authors = sorted(
|
||||
(
|
||||
{
|
||||
**author,
|
||||
"selected_works_count": _works_count_in_period(author, period),
|
||||
}
|
||||
for author in authors
|
||||
),
|
||||
key=lambda author: author["selected_works_count"],
|
||||
reverse=True,
|
||||
)
|
||||
else:
|
||||
ranked_authors = [
|
||||
{**author, "selected_works_count": author["works_count"]}
|
||||
for author in authors
|
||||
]
|
||||
institution_author_ids = {
|
||||
_short_author_id(str(author["id"]))
|
||||
for author in institution_authors
|
||||
if author.get("id")
|
||||
}
|
||||
if not institution_author_ids:
|
||||
return []
|
||||
|
||||
must = [works_v2_service._institutions_nested_query([institution_id])]
|
||||
if publication_years := _dashboard_publication_years(period):
|
||||
must.append({"terms": {"publication_year": publication_years}})
|
||||
|
||||
author_counts: dict[str, int] = defaultdict(int)
|
||||
author_names: dict[str, str] = {}
|
||||
async for hit in elasticsearch.scan(
|
||||
index=str(ESIndex.OPENALEX_WORKS_WILDCARD),
|
||||
query={"bool": {"must": must}},
|
||||
source=[
|
||||
"id",
|
||||
"authorships.author.id",
|
||||
"authorships.author.display_name",
|
||||
"authorships.institutions.id",
|
||||
],
|
||||
page_size=1000,
|
||||
cluster=ESCluster.NES,
|
||||
):
|
||||
hit_author_ids: set[str] = set()
|
||||
for authorship in hit.get("_source", {}).get("authorships", []):
|
||||
if not any(
|
||||
_normalize_institution_id(institution.get("id")) == institution_id
|
||||
for institution in authorship.get("institutions") or []
|
||||
if institution.get("id")
|
||||
):
|
||||
continue
|
||||
author = authorship.get("author") or {}
|
||||
author_id = author.get("id")
|
||||
if not author_id:
|
||||
continue
|
||||
author_id = str(author_id)
|
||||
if _short_author_id(author_id) not in institution_author_ids:
|
||||
continue
|
||||
hit_author_ids.add(author_id)
|
||||
if author.get("display_name"):
|
||||
author_names.setdefault(author_id, str(author["display_name"]))
|
||||
for author_id in hit_author_ids:
|
||||
author_counts[author_id] += 1
|
||||
|
||||
ranked_authors = sorted(
|
||||
(
|
||||
{
|
||||
"id": author_id,
|
||||
"name": author_names.get(author_id, author_id),
|
||||
"count": count,
|
||||
}
|
||||
for author_id, count in author_counts.items()
|
||||
),
|
||||
key=lambda author: author["count"],
|
||||
reverse=True,
|
||||
)
|
||||
return [
|
||||
TopAuthor(
|
||||
id=_short_author_id(author["id"]),
|
||||
name=author["name"],
|
||||
count=author["selected_works_count"],
|
||||
count=author["count"],
|
||||
)
|
||||
for author in ranked_authors[:TOP_AUTHOR_LIMIT]
|
||||
if author["selected_works_count"] > 0
|
||||
if author["count"] > 0
|
||||
]
|
||||
|
||||
|
||||
def _works_count_in_period(author: dict[str, Any], period: DashboardPeriod) -> int:
|
||||
from_year, to_year = period
|
||||
return sum(
|
||||
int(item.get("works_count", 0) or 0)
|
||||
for item in author.get("counts_by_year", [])
|
||||
if (
|
||||
(from_year is None or int(item.get("year", 0) or 0) >= from_year)
|
||||
and (to_year is None or int(item.get("year", 0) or 0) <= to_year)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
async def _safe_institution_authors(institution_id: str) -> list[dict[str, Any]]:
|
||||
try:
|
||||
return await _institution_authors(institution_id)
|
||||
|
||||
@@ -408,18 +408,17 @@ async def test_dashboard_statistics_counts_organization_data_and_all_projects():
|
||||
"bmbf_projects": 5,
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_dashboard_publications_uses_exact_es_count_for_period():
|
||||
async def test_dashboard_publications_counts_matching_openalex_works():
|
||||
with patch(
|
||||
"inse.dashboard.service.institutions_v2_service.get_institution_by_id_or_raise",
|
||||
"inse.dashboard.service.elasticsearch.search",
|
||||
new=AsyncMock(
|
||||
return_value=Institution(id=ORG_INSTITUTION_ID, works_count=250)
|
||||
side_effect=[
|
||||
{"hits": {"total": {"value": 250}}},
|
||||
{"hits": {"total": {"value": 22}}},
|
||||
]
|
||||
),
|
||||
) as mocked_get_institution, patch(
|
||||
"inse.dashboard.service.elasticsearch.count",
|
||||
new=AsyncMock(return_value=22),
|
||||
) as mocked_count:
|
||||
) as mocked_search:
|
||||
all_time = await get_dashboard_publications(
|
||||
ORG_INSTITUTION_ID,
|
||||
"Example Org",
|
||||
@@ -433,19 +432,33 @@ async def test_dashboard_publications_uses_exact_es_count_for_period():
|
||||
|
||||
assert all_time.value == 250
|
||||
assert selected_period.value == 22
|
||||
mocked_get_institution.assert_awaited_once_with(
|
||||
raw_oa_institution_id="I123",
|
||||
source=["id", "works_count"],
|
||||
all_time_body = mocked_search.await_args_list[0].kwargs["body"]
|
||||
selected_period_body = mocked_search.await_args_list[1].kwargs["body"]
|
||||
assert mocked_search.await_args_list[0].kwargs["index"] == (
|
||||
ESIndex.OPENALEX_WORKS_WILDCARD
|
||||
)
|
||||
mocked_count.assert_awaited_once()
|
||||
count_call = mocked_count.await_args.kwargs
|
||||
assert count_call["index"] == ESIndex.OPENALEX_WORKS_WILDCARD
|
||||
filters = count_call["query"]["bool"]["filter"]
|
||||
assert filters[0]["nested"]["path"] == "authorships"
|
||||
assert filters[0]["nested"]["query"]["nested"]["query"] == {
|
||||
"term": {"authorships.institutions.id": ORG_INSTITUTION_ID}
|
||||
}
|
||||
assert filters[1] == {"terms": {"publication_year": [2021, 2022]}}
|
||||
assert all_time_body["track_total_hits"] is True
|
||||
assert all_time_body["query"]["bool"]["must"] == [
|
||||
{
|
||||
"nested": {
|
||||
"path": "authorships",
|
||||
"query": {
|
||||
"nested": {
|
||||
"path": "authorships.institutions",
|
||||
"query": {
|
||||
"terms": {
|
||||
"authorships.institutions.id": [ORG_INSTITUTION_ID]
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
]
|
||||
assert selected_period_body["query"]["bool"]["must"] == [
|
||||
all_time_body["query"]["bool"]["must"][0],
|
||||
{"terms": {"publication_year": [2021, 2022]}},
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
@@ -671,6 +684,48 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects():
|
||||
|
||||
return {"aggregations": {"people": {"buckets": []}}}
|
||||
|
||||
scan_calls = []
|
||||
|
||||
async def scan(**kwargs):
|
||||
scan_calls.append(kwargs)
|
||||
yield {
|
||||
"_id": "W1",
|
||||
"_source": {
|
||||
"id": "https://openalex.org/W1",
|
||||
"authorships": [
|
||||
{
|
||||
"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": "https://openalex.org/I999"}],
|
||||
},
|
||||
],
|
||||
},
|
||||
}
|
||||
yield {
|
||||
"_id": "W2",
|
||||
"_source": {
|
||||
"id": "https://openalex.org/W2",
|
||||
"authorships": [
|
||||
{
|
||||
"author": {
|
||||
"id": "https://openalex.org/A1",
|
||||
"display_name": "Ada Example",
|
||||
},
|
||||
"institutions": [{"id": ORG_INSTITUTION_ID}],
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
async def eu_projects(**kwargs):
|
||||
if kwargs.get("raw_oa_institution_id"):
|
||||
return EuCordisProjectSearchPage(
|
||||
@@ -694,6 +749,9 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects():
|
||||
"inse.dashboard.service.elasticsearch.search",
|
||||
new=AsyncMock(side_effect=search),
|
||||
) as mocked_search, patch(
|
||||
"inse.dashboard.service.elasticsearch.scan",
|
||||
new=scan,
|
||||
), patch(
|
||||
"inse.dashboard.service.InstitutionsSearchClient.get_institution_aliases_by_oa_institution_id",
|
||||
new=AsyncMock(return_value=["Example University"]),
|
||||
), patch(
|
||||
@@ -715,8 +773,9 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects():
|
||||
assert result.works[0].model_dump() == {
|
||||
"id": "A1",
|
||||
"name": "Ada Example",
|
||||
"count": 42,
|
||||
"count": 2,
|
||||
}
|
||||
assert len(result.works) == 1
|
||||
assert result.patents[0].model_dump() == {
|
||||
"id": "A1",
|
||||
"name": "Ada Example",
|
||||
@@ -756,18 +815,44 @@ async def test_top_authors_maps_works_patents_news_and_eu_projects():
|
||||
assert eu_body["_source"] == ["projectID", "qaProcessedDoi"]
|
||||
assert "aggs" not in eu_body
|
||||
|
||||
works_calls = [
|
||||
assert len(scan_calls) == 1
|
||||
assert scan_calls[0]["index"] == str(ESIndex.OPENALEX_WORKS_WILDCARD)
|
||||
assert scan_calls[0]["query"]["bool"]["must"] == [
|
||||
{
|
||||
"nested": {
|
||||
"path": "authorships",
|
||||
"query": {
|
||||
"nested": {
|
||||
"path": "authorships.institutions",
|
||||
"query": {
|
||||
"terms": {
|
||||
"authorships.institutions.id": [ORG_INSTITUTION_ID]
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
]
|
||||
assert scan_calls[0]["source"] == [
|
||||
"id",
|
||||
"authorships.author.id",
|
||||
"authorships.author.display_name",
|
||||
"authorships.institutions.id",
|
||||
]
|
||||
|
||||
eu_work_calls = [
|
||||
call
|
||||
for call in mocked_search.await_args_list
|
||||
if call.kwargs["index"] == "openalex-works"
|
||||
]
|
||||
assert len(works_calls) == 1
|
||||
works_body = works_calls[0].kwargs["body"]
|
||||
assert works_body["query"]["bool"]["filter"][0]["terms"]["doi"] == [
|
||||
assert len(eu_work_calls) == 1
|
||||
eu_works_body = eu_work_calls[0].kwargs["body"]
|
||||
assert eu_works_body["query"]["bool"]["filter"][0]["terms"]["doi"] == [
|
||||
"https://doi.org/10.1234/p1",
|
||||
"https://doi.org/10.1234/p2",
|
||||
]
|
||||
assert works_body["_source"] == [
|
||||
assert eu_works_body["_source"] == [
|
||||
"doi",
|
||||
"authorships.author.id",
|
||||
"authorships.author.display_name",
|
||||
@@ -1052,9 +1137,29 @@ async def test_top_authors_returns_partial_response_when_one_source_fails():
|
||||
|
||||
raise RuntimeError("projects unavailable")
|
||||
|
||||
async def scan(**kwargs):
|
||||
yield {
|
||||
"_id": "W1",
|
||||
"_source": {
|
||||
"id": "https://openalex.org/W1",
|
||||
"authorships": [
|
||||
{
|
||||
"author": {
|
||||
"id": "https://openalex.org/A1",
|
||||
"display_name": "Ada Example",
|
||||
},
|
||||
"institutions": [{"id": ORG_INSTITUTION_ID}],
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
with patch(
|
||||
"inse.dashboard.service.elasticsearch.search",
|
||||
new=AsyncMock(side_effect=search),
|
||||
), patch(
|
||||
"inse.dashboard.service.elasticsearch.scan",
|
||||
new=scan,
|
||||
), patch(
|
||||
"inse.dashboard.service.InstitutionsSearchClient.get_institution_aliases_by_oa_institution_id",
|
||||
new=AsyncMock(return_value=["Example University"]),
|
||||
@@ -1118,9 +1223,47 @@ async def test_top_authors_can_rank_works_by_last_five_years():
|
||||
}
|
||||
return {"aggregations": {"people": {"buckets": []}}}
|
||||
|
||||
scan_calls = []
|
||||
|
||||
async def scan(**kwargs):
|
||||
scan_calls.append(kwargs)
|
||||
for index in range(16):
|
||||
yield {
|
||||
"_id": f"W-grace-{index}",
|
||||
"_source": {
|
||||
"authorships": [
|
||||
{
|
||||
"author": {
|
||||
"id": "https://openalex.org/A2",
|
||||
"display_name": "Grace Example",
|
||||
},
|
||||
"institutions": [{"id": ORG_INSTITUTION_ID}],
|
||||
}
|
||||
]
|
||||
},
|
||||
}
|
||||
for index in range(2):
|
||||
yield {
|
||||
"_id": f"W-ada-{index}",
|
||||
"_source": {
|
||||
"authorships": [
|
||||
{
|
||||
"author": {
|
||||
"id": "https://openalex.org/A1",
|
||||
"display_name": "Ada Example",
|
||||
},
|
||||
"institutions": [{"id": ORG_INSTITUTION_ID}],
|
||||
}
|
||||
]
|
||||
},
|
||||
}
|
||||
|
||||
with patch(
|
||||
"inse.dashboard.service.elasticsearch.search",
|
||||
new=AsyncMock(side_effect=search),
|
||||
) as mocked_search, patch(
|
||||
"inse.dashboard.service.elasticsearch.scan",
|
||||
new=scan,
|
||||
), patch(
|
||||
"inse.dashboard.service.InstitutionsSearchClient.get_institution_aliases_by_oa_institution_id",
|
||||
new=AsyncMock(return_value=["Example University"]),
|
||||
@@ -1148,6 +1291,16 @@ async def test_top_authors_can_rank_works_by_last_five_years():
|
||||
"count": 2,
|
||||
}
|
||||
|
||||
work_scan_calls = [
|
||||
call
|
||||
for call in scan_calls
|
||||
if call["index"] == str(ESIndex.OPENALEX_WORKS_WILDCARD)
|
||||
]
|
||||
assert len(work_scan_calls) == 1
|
||||
assert work_scan_calls[0]["query"]["bool"]["must"][1] == {
|
||||
"terms": {"publication_year": list(range(current_year - 4, current_year + 1))}
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_topic_heatmap_maps_field_year_counts_and_shares():
|
||||
|
||||
@@ -118,7 +118,7 @@ export class DashboardComponent implements OnInit {
|
||||
readonly topicTreemapMinYear = 1900;
|
||||
readonly topicTreemapMaxYear = new Date().getFullYear();
|
||||
dashboardPeriodMode: DashboardPeriodMode = 'all_time';
|
||||
dashboardFromYear = this.topicTreemapMaxYear - 4;
|
||||
dashboardFromYear = this.topicTreemapMinYear;
|
||||
dashboardToYear = this.topicTreemapMaxYear;
|
||||
selectedTopicTreemapNode?: string;
|
||||
selectedDfgTopicTreemapNode?: string;
|
||||
@@ -480,14 +480,16 @@ export class DashboardComponent implements OnInit {
|
||||
this.dashboardPeriodMode = mode;
|
||||
}
|
||||
this.applyPresetPeriod();
|
||||
this.dashboardFromYear = this.parseDashboardYear(
|
||||
params.get('fromYear'),
|
||||
this.dashboardFromYear,
|
||||
);
|
||||
this.dashboardToYear = this.parseDashboardYear(
|
||||
params.get('toYear'),
|
||||
this.dashboardToYear,
|
||||
);
|
||||
if (this.dashboardPeriodMode !== 'all_time') {
|
||||
this.dashboardFromYear = this.parseDashboardYear(
|
||||
params.get('fromYear'),
|
||||
this.dashboardFromYear,
|
||||
);
|
||||
this.dashboardToYear = this.parseDashboardYear(
|
||||
params.get('toYear'),
|
||||
this.dashboardToYear,
|
||||
);
|
||||
}
|
||||
if (this.dashboardFromYear > this.dashboardToYear) {
|
||||
this.dashboardFromYear = this.dashboardToYear;
|
||||
}
|
||||
@@ -496,7 +498,10 @@ export class DashboardComponent implements OnInit {
|
||||
|
||||
private applyPresetPeriod(): void {
|
||||
const currentYear = this.topicTreemapMaxYear;
|
||||
if (this.dashboardPeriodMode === 'last_10_years') {
|
||||
if (this.dashboardPeriodMode === 'all_time') {
|
||||
this.dashboardFromYear = this.topicTreemapMinYear;
|
||||
this.dashboardToYear = currentYear;
|
||||
} else if (this.dashboardPeriodMode === 'last_10_years') {
|
||||
this.dashboardFromYear = currentYear - 9;
|
||||
this.dashboardToYear = currentYear;
|
||||
} else if (this.dashboardPeriodMode === 'last_5_years') {
|
||||
|
||||
Reference in New Issue
Block a user