diff --git a/service/src/main/inse/models/topics_by_time.py b/service/src/main/inse/models/topics_by_time.py index 4b495e5a..d4213640 100644 --- a/service/src/main/inse/models/topics_by_time.py +++ b/service/src/main/inse/models/topics_by_time.py @@ -6,5 +6,20 @@ from inse.models.topics_by_year import TopicsByYear class TopicsByTime(BaseModel): - elements: List[TopicsByYear] = Field(default_factory=list, description="Topics grouped by year.") + elements: List[TopicsByYear] = Field( + default_factory=list, + description="Topics grouped by year.", + ) total: int = Field(default=0, description="The number of years.") + is_limited: bool = Field( + default=False, + description="True if the timeline contains only a limited subset of the author's publications.", + ) + used_publications: int = Field( + default=0, + description="The number of publications used to build the timeline.", + ) + total_publications: int = Field( + default=0, + description="The total number of publications found for this author.", + ) \ No newline at end of file diff --git a/service/src/main/inse/works/logic.py b/service/src/main/inse/works/logic.py index 642f05d0..43a6135a 100644 --- a/service/src/main/inse/works/logic.py +++ b/service/src/main/inse/works/logic.py @@ -182,6 +182,69 @@ def _limit_works_by_complete_years( return selected_works +async def _get_limited_author_works_by_complete_years( + author_id: str, + target_publications: int, + client: OpenAlexWorksSearchClient | None = None, +) -> tuple[list[Work], int, bool]: + search_client = client or OpenAlexWorksSearchClient() + page = 1 + size = 100 + total = 0 + fetched_publications = 0 + selected_works: list[Work] = [] + cutoff_year: int | None = None + is_limited = False + + while True: + data = await _search_author_works_data( + search_client, + author_id, + page, + size, + ) + elements, total = _parse_full_work_search_response(data) + + if not elements: + break + + fetched_publications += len(elements) + should_stop = False + + for work in elements: + year = work.publication_year + + if year is None: + if cutoff_year is not None: + should_stop = True + break + + continue + + if cutoff_year is not None and year != cutoff_year: + is_limited = True + should_stop = True + break + + selected_works.append(work) + + if cutoff_year is None and len(selected_works) >= target_publications: + cutoff_year = year + + if should_stop or fetched_publications >= total: + break + + page += 1 + + return selected_works, total, is_limited + + +def _get_total_publication_count(meta: Meta | None, work_results: list[Work]) -> int: + if meta and meta.count is not None: + return meta.count + + return len(work_results) + async def get_topics_by_time_by_author( author_id: str, @@ -190,14 +253,20 @@ async def get_topics_by_time_by_author( 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( + if include_all: + works = await get_all_author_works(author_id, client=client) + work_results = works.results or [] + total_publications = _get_total_publication_count(works.meta, work_results) + is_limited = False + else: + ( work_results, + total_publications, + is_limited, + ) = await _get_limited_author_works_by_complete_years( + author_id, target_publications=target_publications, + client=client, ) years = {} @@ -245,44 +314,53 @@ async def get_topics_by_time_by_author( } years[year]["primary_topics"][primary_topic_name]["count"] += 1 - + elements = [] for year in sorted(years.keys()): topic_items = list(years[year]["topics"].values()) - topic_items.sort(key=lambda item: item["count"], reverse=True) + topic_items.sort(key=lambda item: (-item["count"], item["name"])) topics = [] for topic in topic_items[:max_topics_per_year]: - topics.append(TopicCount( - id=topic["id"], - name=topic["name"], - count=topic["count"], - )) + topics.append( + TopicCount( + id=topic["id"], + name=topic["name"], + count=topic["count"], + ) + ) primary_topic_items = list(years[year]["primary_topics"].values()) - primary_topic_items.sort(key=lambda item: item["count"], reverse=True) + primary_topic_items.sort(key=lambda item: (-item["count"], item["name"])) primary_topics = [] for topic in primary_topic_items[:max_topics_per_year]: - primary_topics.append(TopicCount( - id=topic["id"], - name=topic["name"], - count=topic["count"], - )) + primary_topics.append( + TopicCount( + id=topic["id"], + name=topic["name"], + count=topic["count"], + ) + ) - elements.append(TopicsByYear( - year=year, - publication_count=years[year]["publication_count"], - topics=topics, - primary_topics=primary_topics, - )) + elements.append( + TopicsByYear( + year=year, + publication_count=years[year]["publication_count"], + topics=topics, + primary_topics=primary_topics, + ) + ) return TopicsByTime( elements=elements, total=len(elements), + is_limited=is_limited, + used_publications=sum(element.publication_count for element in elements), + total_publications=total_publications, ) diff --git a/webapp/src/app/api/model/topics-by-time.ts b/webapp/src/app/api/model/topics-by-time.ts index d2ecf101..1972ade1 100644 --- a/webapp/src/app/api/model/topics-by-time.ts +++ b/webapp/src/app/api/model/topics-by-time.ts @@ -7,7 +7,7 @@ * https://openapi-generator.tech * Do not edit the class manually. */ -import { TopicsByYear } from './topics-by-year'; +import { TopicsByYear } from './topicsByYear'; export interface TopicsByTime { @@ -19,5 +19,16 @@ export interface TopicsByTime { * The number of years. */ total?: number; -} - + /** + * True if the timeline contains only a limited subset of the author's publications. + */ + is_limited?: boolean; + /** + * The number of publications used to build the timeline. + */ + used_publications?: number; + /** + * The total number of publications found for this author. + */ + total_publications?: number; +} \ No newline at end of file diff --git a/webapp/src/app/api/model/topicsByTime.ts b/webapp/src/app/api/model/topicsByTime.ts index 0dac04a7..5c50d9c1 100644 --- a/webapp/src/app/api/model/topicsByTime.ts +++ b/webapp/src/app/api/model/topicsByTime.ts @@ -7,7 +7,7 @@ * https://openapi-generator.tech * Do not edit the class manually. */ -import { TopicsByYear } from './topicsByYear'; +import { TopicsByYear } from './topics-by-year'; export interface TopicsByTime { @@ -19,5 +19,16 @@ export interface TopicsByTime { * The number of years. */ total?: number; -} - + /** + * True if the timeline contains only a limited subset of the author's publications. + */ + is_limited?: boolean; + /** + * The number of publications used to build the timeline. + */ + used_publications?: number; + /** + * The total number of publications found for this author. + */ + total_publications?: number; +} \ No newline at end of file diff --git a/webapp/src/app/modules/person-overview/person-overview.component.ts b/webapp/src/app/modules/person-overview/person-overview.component.ts index 36bc99fb..c94bd967 100644 --- a/webapp/src/app/modules/person-overview/person-overview.component.ts +++ b/webapp/src/app/modules/person-overview/person-overview.component.ts @@ -27,7 +27,7 @@ export class PersonOverviewComponent implements OnInit { private static readonly timelineStartYearQueryParam = 'timeline_start_year'; private static readonly timelineEndYearQueryParam = 'timeline_end_year'; private static readonly timelineTopicCountLimit = 4; - private static readonly initialTimelineTargetPublications = 300; + private static readonly initialTimelineTargetPublications = 250; private readonly route = inject(ActivatedRoute); private readonly router = inject(Router); @@ -185,8 +185,7 @@ export class PersonOverviewComponent implements OnInit { this.loadingTimeline = false; this.timelineLoadFailed = false; this.timelineCanLoadEarlierYears = - this.getTimelinePublicationCount(timelineElements) >= - PersonOverviewComponent.initialTimelineTargetPublications; + response.is_limited ?? this.isLikelyLimitedTimeline(timelineElements); }, error: (error) => { if (!this.isCurrentTimelineLoad(loadId, authorId)) { @@ -292,8 +291,11 @@ export class PersonOverviewComponent implements OnInit { return [...elementsByYear.values()].sort((left, right) => left.year - right.year); } - private getTimelinePublicationCount(elements: TopicTimelineElement[]): number { - return elements.reduce((total, element) => total + element.publication_count, 0); + private isLikelyLimitedTimeline(elements: TopicTimelineElement[]): boolean { + return ( + elements.reduce((total, element) => total + element.publication_count, 0) >= + PersonOverviewComponent.initialTimelineTargetPublications + ); } private isCurrentTimelineLoad(loadId: number, authorId: string): boolean {