Compare commits

...
Author SHA1 Message Date
spsaefan 770f2b31e1 Add timeline API options
LocationBasedServices/smartguide/pipeline/head This commit looks good
LocationBasedServices/smartguide/pipeline/pr-dev Build started...
2026-06-08 01:25:03 +02:00
spsaefan 3325590735 Limit timeline publications 2026-06-08 01:24:49 +02:00
Louis f903e417ad Fix timeline topic year selection
LocationBasedServices/smartguide/pipeline/head This commit looks good
2026-06-07 21:34:26 +02:00
4 changed files with 70 additions and 16 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,
)
@@ -117,7 +117,7 @@
type="button"
class="main-topic-button"
[disabled]="!getMainTopicItem(item)?.id"
(click)="selectTopic(getMainTopicItem(item)!)"
(click)="selectTopic(getMainTopicItem(item), [item.year])"
>
{{ getMainTopic(item) }}
</button>
@@ -136,7 +136,7 @@
class="topic-chip"
[ngClass]="getColorClass(index)"
[disabled]="!topic.id"
(click)="selectTopic(topic)"
(click)="selectTopic(topic, [item.year])"
>
<span class="topic-name">{{ topic.name }}</span>
<span class="topic-count">{{ topic.count }}</span>
@@ -172,7 +172,7 @@
type="button"
class="main-topic-button"
[disabled]="!phase.primaryTopic.id"
(click)="selectTopic(phase.primaryTopic)"
(click)="selectTopic(phase.primaryTopic, getPhaseYears(phase))"
>
{{ phase.primaryTopicName }}
</button>
@@ -192,7 +192,7 @@
class="topic-chip"
[ngClass]="getColorClass(index)"
[disabled]="!topic.id"
(click)="selectTopic(topic)"
(click)="selectTopic(topic, getPhaseYears(phase))"
>
<span class="topic-name">{{ topic.name }}</span>
<span class="topic-count">{{ topic.count }}</span>
@@ -240,7 +240,7 @@
"
class="mini-topic-chip"
[disabled]="!topic.id"
(click)="selectTopic(topic)"
(click)="selectTopic(topic, [year.year])"
>
<span>{{ topic.name }}</span>
<strong>{{ topic.count }}</strong>
@@ -273,4 +273,4 @@
Keine Timeline-Daten vorhanden.
</div>
</ng-template>
</section>
</section>
@@ -176,14 +176,14 @@ export class ResearchTimelineComponent implements OnChanges {
this.emitYearRangeChange();
}
selectTopic(topic: TopicTimelineTopic): void {
if (!topic.id) {
selectTopic(topic: TopicTimelineTopic | undefined, years: number[]): void {
if (!topic?.id || years.length === 0) {
return;
}
this.topicSelect.emit({
topic,
years: this.createSelectedYears(),
years: this.normalizeSelectedYears(years),
});
}
@@ -222,6 +222,10 @@ export class ResearchTimelineComponent implements OnChanges {
return `${phase.startYear}–${phase.endYear}`;
}
getPhaseYears(phase: ResearchTimelinePhase): number[] {
return phase.years.map((year) => year.year);
}
togglePhase(phase: ResearchTimelinePhase): void {
if (this.expandedPhaseIds.has(phase.id)) {
this.expandedPhaseIds.delete(phase.id);
@@ -405,11 +409,10 @@ export class ResearchTimelineComponent implements OnChanges {
.slice(0, this.displayedPhaseTopicLimit);
}
private createSelectedYears(): number[] {
return Array.from(
{ length: this.selectedEndYear - this.selectedStartYear + 1 },
(_, index) => this.selectedStartYear + index,
);
private normalizeSelectedYears(years: number[]): number[] {
return Array.from(new Set(years))
.filter((year) => Number.isFinite(year))
.sort((left, right) => left - right);
}
private getDominantPrimaryTopicCandidates(item: TopicTimelineElement): TopicTimelineTopic[] {
@@ -483,4 +486,4 @@ export class ResearchTimelineComponent implements OnChanges {
endYear: this.selectedEndYear,
});
}
}
}