From 33255907350b4b2f86f63f9ab8065f29e60f850e Mon Sep 17 00:00:00 2001 From: spsaefan <117728614+spsaefan@users.noreply.github.com> Date: Mon, 8 Jun 2026 01:24:49 +0200 Subject: [PATCH 1/2] Limit timeline publications --- service/src/main/inse/works/logic.py | 40 +++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/service/src/main/inse/works/logic.py b/service/src/main/inse/works/logic.py index d2196a8e..27c15dac 100644 --- a/service/src/main/inse/works/logic.py +++ b/service/src/main/inse/works/logic.py @@ -147,16 +147,54 @@ async def get_work( return elements[0] +def _limit_works_by_complete_years( + work_results: list[Work], + target_publications: int, +) -> list[Work]: + works_by_year: dict[int, list[Work]] = {} + + for work in work_results: + if work.publication_year is None: + continue + + year = work.publication_year + + if year not in works_by_year: + works_by_year[year] = [] + + works_by_year[year].append(work) + + selected_works: list[Work] = [] + + for year in sorted(works_by_year.keys(), reverse=True): + selected_works.extend(works_by_year[year]) + + if len(selected_works) >= target_publications: + break + + return selected_works + + async def get_topics_by_time_by_author( author_id: str, max_topics_per_year: int = 3, + target_publications: int = 300, + include_all: bool = False, client: OpenAlexWorksSearchClient | None = None, ) -> TopicsByTime: works = await get_all_author_works(author_id, client=client) + work_results = works.results or [] + + if not include_all: + work_results = _limit_works_by_complete_years( + work_results, + target_publications=target_publications, + ) + years = {} - for work in works.results or []: + for work in work_results: if work.publication_year is None: continue -- 2.56.1 From 770f2b31e1bf5cb2cbdb20a8b6b275a11e2dafd8 Mon Sep 17 00:00:00 2001 From: spsaefan <117728614+spsaefan@users.noreply.github.com> Date: Mon, 8 Jun 2026 01:25:03 +0200 Subject: [PATCH 2/2] Add timeline API options --- service/src/main/inse/works/service.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/service/src/main/inse/works/service.py b/service/src/main/inse/works/service.py index 12ed15f3..8f6c20b6 100644 --- a/service/src/main/inse/works/service.py +++ b/service/src/main/inse/works/service.py @@ -101,11 +101,24 @@ async def get_primary_topic_counts( async def get_topics_by_time_by_author( author_id: Annotated[str, Path(description="The author id, for example A5023888391.")], max_topics_per_year: Annotated[int, Query(description="The maximum number of topics per year.", ge=1, le=20)] = 3, + target_publications: Annotated[ + int, + Query( + description="Target number of publications used for the timeline. Complete years are always included.", + ge=1, + ), + ] = 300, + include_all: Annotated[ + bool, + Query(description="If true, all publications are used for the timeline."), + ] = False, principal: security.Principal = Depends(security.roles_allowed(["SUPER_ADMIN", "UNI_ADMIN"])), ): return await logic.get_topics_by_time_by_author( author_id, max_topics_per_year=max_topics_per_year, + target_publications=target_publications, + include_all=include_all, ) -- 2.56.1