revert period filters on dashboard
LocationBasedServices/smartguide/pipeline/head This commit looks good

This commit is contained in:
2026-08-06 11:32:05 +02:00
parent 727c3fde67
commit b21d8d3e88
6 changed files with 122 additions and 71 deletions
+34 -50
View File
@@ -185,37 +185,27 @@ def _project_boundary_year_filter(
from_year, to_year = period
filters: list[dict[str, Any]] = []
# A project overlaps the selected interval if it begins no later than the
# interval end and finishes no earlier than the interval start.
if to_year is not None:
if date_format == "year":
filters.append({"range": {start_field: {"lte": to_year}}})
else:
last_day = (
f"{to_year}-12-31" if date_format == "iso" else f"31.12.{to_year}"
)
filters.append({"range": {start_field: {"lte": last_day}}})
if from_year is not None:
if date_format == "year":
filters.append({"term": {start_field: from_year}})
filters.append({"range": {end_field: {"gte": from_year}}})
else:
first_day = (
f"{from_year}-01-01"
if date_format == "iso"
else f"01.01.{from_year}"
)
last_day = (
f"{from_year}-12-31"
if date_format == "iso"
else f"31.12.{from_year}"
)
filters.append(
{"range": {start_field: {"gte": first_day, "lte": last_day}}}
)
if to_year is not None:
if date_format == "year":
filters.append({"term": {end_field: to_year}})
else:
first_day = (
f"{to_year}-01-01" if date_format == "iso" else f"01.01.{to_year}"
)
last_day = (
f"{to_year}-12-31" if date_format == "iso" else f"31.12.{to_year}"
)
filters.append(
{"range": {end_field: {"gte": first_day, "lte": last_day}}}
)
filters.append({"range": {end_field: {"gte": first_day}}})
return {"bool": {"filter": filters}} if filters else None
@@ -396,8 +386,10 @@ async def get_dashboard_eu_projects(
topic_ids=None,
filter_topic_ids=None,
title=None,
funding_start_years=[str(period[0])] if period[0] is not None else None,
funding_end_years=[str(period[1])] if period[1] is not None else None,
funding_start_years=None,
funding_end_years=None,
active_from_year=period[0],
active_to_year=period[1],
countries=None,
framework_programmes=None,
funding_schemes=None,
@@ -565,8 +557,10 @@ async def get_dashboard_bmbf_projects(
funding_arts=None,
topic_ids=None,
filter_topic_ids=None,
funding_start_years=[str(period[0])] if period[0] is not None else None,
funding_end_years=[str(period[1])] if period[1] is not None else None,
funding_start_years=None,
funding_end_years=None,
active_from_year=period[0],
active_to_year=period[1],
countries=None,
page=0,
size=1,
@@ -675,8 +669,10 @@ async def get_dashboard_dfg_projects(
topic_ids=None,
filter_topic_ids=None,
title=None,
funding_start_years=[str(period[0])] if period[0] is not None else None,
funding_end_years=[str(period[1])] if period[1] is not None else None,
funding_start_years=None,
funding_end_years=None,
active_from_year=period[0],
active_to_year=period[1],
page=0,
size=1,
sort_by="id",
@@ -2323,16 +2319,10 @@ async def _get_eu_project_ids_from_projects_service(
topic_ids=None,
filter_topic_ids=None,
title=None,
funding_start_years=(
[str(year) for year in range(1900, period[1] + 1)]
if period[1] is not None
else None
),
funding_end_years=(
[str(year) for year in range(period[0], 2101)]
if period[0] is not None
else None
),
funding_start_years=None,
funding_end_years=None,
active_from_year=period[0],
active_to_year=period[1],
countries=None,
framework_programmes=None,
funding_schemes=None,
@@ -2438,7 +2428,7 @@ async def _person_eu_project_count(
from_year: int | None = None,
to_year: int | None = None,
) -> int:
"""Uses the same author-only EU project search as the person detail page."""
"""Returns the same filtered EU project total as the person projects tab."""
period = _dashboard_period(from_year, to_year)
projects = await projects_service.search_eu_cordis_projects(
page=0,
@@ -2454,16 +2444,10 @@ async def _person_eu_project_count(
topic_ids=None,
filter_topic_ids=None,
title=None,
funding_start_years=(
[str(year) for year in range(1900, period[1] + 1)]
if period[1] is not None
else None
),
funding_end_years=(
[str(year) for year in range(period[0], 2101)]
if period[0] is not None
else None
),
funding_start_years=None,
funding_end_years=None,
active_from_year=period[0],
active_to_year=period[1],
countries=None,
framework_programmes=None,
funding_schemes=None,
+23 -19
View File
@@ -51,22 +51,14 @@ def test_project_status_counts_match_authoritative_total():
assert _reconcile_project_status_counts(7, 4, 2, 3) == (2, 2, 3)
def test_project_status_period_uses_exact_boundary_years():
def test_project_status_period_uses_interval_overlap():
assert _project_boundary_year_filter(
"startDate", "endDate", (2022, 2026), "iso"
) == {
"bool": {
"filter": [
{
"range": {
"startDate": {"gte": "2022-01-01", "lte": "2022-12-31"}
}
},
{
"range": {
"endDate": {"gte": "2026-01-01", "lte": "2026-12-31"}
}
},
{"range": {"startDate": {"lte": "2026-12-31"}}},
{"range": {"endDate": {"gte": "2022-01-01"}}},
]
}
}
@@ -78,8 +70,8 @@ def test_project_status_period_uses_exact_boundary_years():
) == {
"bool": {
"filter": [
{"term": {"projects.funding_start": 2022}},
{"term": {"projects.funding_end": 2026}},
{"range": {"projects.funding_start": {"lte": 2026}}},
{"range": {"projects.funding_end": {"gte": 2022}}},
]
}
}
@@ -349,8 +341,10 @@ async def test_dashboard_dfg_projects_uses_projects_service_total():
mocked_get_projects.assert_awaited_once()
call = mocked_get_projects.await_args
assert call.kwargs["raw_oa_institution_id"] == "I123"
assert call.kwargs["funding_start_years"] == ["2020"]
assert call.kwargs["funding_end_years"] == ["2022"]
assert call.kwargs["funding_start_years"] is None
assert call.kwargs["funding_end_years"] is None
assert call.kwargs["active_from_year"] == 2020
assert call.kwargs["active_to_year"] == 2022
assert call.kwargs["skip_enrichment"] is True
assert call.kwargs["page"] == 0
assert call.kwargs["size"] == 1
@@ -375,8 +369,10 @@ async def test_dashboard_bmbf_projects_uses_projects_service_total():
mocked_get_projects.assert_awaited_once()
call = mocked_get_projects.await_args
assert call.kwargs["raw_oa_institution_id"] == "I123"
assert call.kwargs["funding_start_years"][-1] == "2020"
assert call.kwargs["funding_end_years"][0] == "2018"
assert call.kwargs["funding_start_years"] is None
assert call.kwargs["funding_end_years"] is None
assert call.kwargs["active_from_year"] == 2018
assert call.kwargs["active_to_year"] == 2020
assert call.kwargs["page"] == 0
assert call.kwargs["size"] == 1
@@ -402,8 +398,10 @@ async def test_dashboard_eu_projects_uses_projects_service_total():
mocked_get_projects.assert_awaited_once()
call = mocked_get_projects.await_args
assert call.kwargs["raw_oa_institution_id"] == "I123"
assert call.kwargs["funding_start_years"] == ["2019"]
assert call.kwargs["funding_end_years"] == ["2021"]
assert call.kwargs["funding_start_years"] is None
assert call.kwargs["funding_end_years"] is None
assert call.kwargs["active_from_year"] == 2019
assert call.kwargs["active_to_year"] == 2021
assert call.kwargs["project_types"] == list(EuCordisProjectType)
assert call.kwargs["page"] == 0
assert call.kwargs["size"] == 1
@@ -833,6 +831,10 @@ async def test_top_eu_project_authors_count_filtered_projects_by_stable_author_i
return {"aggregations": {"authors": {"buckets": []}}}
async def eu_projects(**kwargs):
assert kwargs["funding_start_years"] is None
assert kwargs["funding_end_years"] is None
assert kwargs["active_from_year"] == 2020
assert kwargs["active_to_year"] == 2022
if kwargs.get("raw_oa_institution_id"):
return EuCordisProjectSearchPage(
elements=[
@@ -866,6 +868,8 @@ async def test_top_eu_project_authors_count_filtered_projects_by_stable_author_i
result = await get_top_authors(
ORG_INSTITUTION_ID,
"Example Org",
from_year=2020,
to_year=2022,
)
assert [author.model_dump() for author in result.eu_projects] == [
@@ -741,6 +741,8 @@ async def _build_eu_cordis_search_query(
title: str | None = None,
funding_start_years: list[str] | None = None,
funding_end_years: list[str] | None = None,
active_from_year: int | None = None,
active_to_year: int | None = None,
countries: list[str] | None = None,
framework_programmes: list[str] | None = None,
funding_schemes: list[str] | None = None,
@@ -876,6 +878,18 @@ async def _build_eu_cordis_search_query(
if funding_end_years:
must_clauses.append(_eu_year_filters("endDate", funding_end_years))
# Period-overlap filter: a project was active in the selected interval when
# it started no later than the interval end and ended no earlier than the
# interval start. This is intentionally separate from exact-year filters.
if active_to_year is not None:
must_clauses.append(
{"range": {"startDate": {"lte": f"{active_to_year}-12-31"}}}
)
if active_from_year is not None:
must_clauses.append(
{"range": {"endDate": {"gte": f"{active_from_year}-01-01"}}}
)
# === Project core filters ===
if framework_programmes:
must_clauses.append({"terms": {"frameworkProgramme": framework_programmes}})
@@ -937,6 +951,8 @@ async def search_eu_cordis_projects(
title: str | None = None,
funding_start_years: list[str] | None = None,
funding_end_years: list[str] | None = None,
active_from_year: int | None = None,
active_to_year: int | None = None,
countries: list[str] | None = None,
framework_programmes: list[str] | None = None,
funding_schemes: list[str] | None = None,
@@ -965,6 +981,8 @@ async def search_eu_cordis_projects(
title=title,
funding_start_years=funding_start_years,
funding_end_years=funding_end_years,
active_from_year=active_from_year,
active_to_year=active_to_year,
countries=countries,
framework_programmes=framework_programmes,
funding_schemes=funding_schemes,
@@ -368,6 +368,8 @@ async def search_dfg_projects(
sort_direction: str = "asc",
fuzzy: bool = False,
skip_enrichment: bool = False,
active_from_year: int | None = None,
active_to_year: int | None = None,
) -> DfgProjectPage:
"""Searches for DFG projects based on the provided filtering and sorting criteria.
@@ -488,6 +490,11 @@ async def search_dfg_projects(
if funding_end_years:
must_clauses.append({"terms": {"funding_end": funding_end_years}})
if active_to_year is not None:
must_clauses.append({"range": {"funding_start": {"lte": str(active_to_year)}}})
if active_from_year is not None:
must_clauses.append({"range": {"funding_end": {"gte": str(active_from_year)}}})
if names:
name_must_clauses = []
for full_name in names:
@@ -858,6 +865,8 @@ async def search_bund_projects_with_filters(
sort_direction: str = "asc",
fuzzy: bool = False,
skip_topic_enrichment: bool = False,
active_from_year: int | None = None,
active_to_year: int | None = None,
) -> BundProjectPage:
"""
Main search function for BUND projects, analogous to the DFG search.
@@ -999,6 +1008,15 @@ async def search_bund_projects_with_filters(
if funding_end_years:
must_clauses.append(_bund_year_filters("funding_end", funding_end_years))
if active_to_year is not None:
must_clauses.append(
{"range": {"funding_start": {"lte": f"31.12.{active_to_year}"}}}
)
if active_from_year is not None:
must_clauses.append(
{"range": {"funding_end": {"gte": f"01.01.{active_from_year}"}}}
)
body["query"] = {"bool": {"must": must_clauses}} if must_clauses else {"match_all": {}}
# === Sorting ===
sort_by = sort_by.strip().lower()
+13 -1
View File
@@ -175,6 +175,8 @@ async def get_dfg_projects_by_author_id(
sort_direction: str = "asc",
fuzzy: bool = False,
skip_enrichment: bool = False,
active_from_year: int | None = None,
active_to_year: int | None = None,
) -> DfgProjectPage:
logger.debug(
"Fetching DFG projects for author_id=%s, author_filter_ids=%s, "
@@ -250,6 +252,8 @@ async def get_dfg_projects_by_author_id(
sort_direction=sort_direction,
fuzzy=fuzzy,
skip_enrichment=skip_enrichment,
active_from_year=active_from_year,
active_to_year=active_to_year,
)
async def get_institution_cordis_eu_projects_funding_over_time(
@@ -425,6 +429,8 @@ async def search_bund_projects_with_filters(
sort_by: str = "title",
sort_direction: str = "asc",
fuzzy: bool = False,
active_from_year: int | None = None,
active_to_year: int | None = None,
) -> BundProjectPage:
institution_aliases = []
if raw_oa_institution_id:
@@ -456,7 +462,9 @@ async def search_bund_projects_with_filters(
size=size,
sort_by=sort_by,
sort_direction=sort_direction,
fuzzy=fuzzy
fuzzy=fuzzy,
active_from_year=active_from_year,
active_to_year=active_to_year,
)
def _calculate_funding_over_time(
@@ -618,6 +626,8 @@ async def search_eu_cordis_projects(
title: str | None = None,
funding_start_years: list[str] | None = None,
funding_end_years: list[str] | None = None,
active_from_year: int | None = None,
active_to_year: int | None = None,
countries: list[str] | None = None,
framework_programmes: list[str] | None = None,
funding_schemes: list[str] | None = None,
@@ -679,6 +689,8 @@ async def search_eu_cordis_projects(
title=title,
funding_start_years=funding_start_years,
funding_end_years=funding_end_years,
active_from_year=active_from_year,
active_to_year=active_to_year,
countries=countries,
framework_programmes=framework_programmes,
funding_schemes=funding_schemes,
@@ -2,6 +2,21 @@ import pytest
from inse.projects import eu_cordis_projects_client as eu_cordis_search_client
@pytest.mark.anyio
async def test_active_period_filter_matches_projects_overlapping_interval():
query, _, _ = await eu_cordis_search_client._build_eu_cordis_search_query(
active_from_year=2022,
active_to_year=2026,
)
assert {"range": {"startDate": {"lte": "2026-12-31"}}} in query["bool"][
"must"
]
assert {"range": {"endDate": {"gte": "2022-01-01"}}} in query["bool"][
"must"
]
@pytest.mark.anyio
async def test_search_eu_cordis_projects_by_title():
"""Test searching EU CORDIS projects by title."""
@@ -362,4 +377,4 @@ async def test_search_eu_cordis_projects_with_fuzzy_topic_search():
# Note: This may return 0 results if the topic ID doesn't exist
# The important thing is that it doesn't crash
assert isinstance(res.total, int)
assert isinstance(res.elements, list)
assert isinstance(res.elements, list)