ts-85-limit-timeline-complete-years #116

Merged
erfanian merged 2 commits from ts-85-limit-timeline-complete-years into dev 2026-06-08 10:16:26 +02:00
2 changed files with 52 additions and 1 deletions
+39 -1
View File
@@ -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
+13
View File
@@ -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,
)