Add global dashboard period state

This commit is contained in:
spsaefan
2026-07-27 11:57:04 +02:00
parent 9d128e18b6
commit 401b82c226
@@ -19,6 +19,7 @@ import {
} from './topic-wordcloud-dialog/topic-wordcloud-dialog.component';
import {
BmbfProjectStatusStatistics,
DashboardPeriodBounds,
DashboardStatisticValue,
DashboardStatistics,
DashboardStatisticsService,
@@ -26,7 +27,6 @@ import {
EuProjectStatusStatistics,
TopAuthor,
TopAuthorStatistics,
TopAuthorsWorksPeriod,
} from './dashboard-statistics.service';
import {
activeInstitutionPersonListQueryParams,
@@ -45,6 +45,11 @@ type TopAuthorDataset = {
entries: TopAuthor[];
stripAuthorTitle?: boolean;
};
type DashboardPeriodMode =
| 'all_time'
| 'last_10_years'
| 'last_5_years'
| 'custom';
@Component({
selector: 'dashboard',
imports: [
@@ -84,7 +89,6 @@ export class DashboardComponent implements OnInit {
topAuthors?: TopAuthorStatistics;
topAuthorsLoading = false;
topAuthorsError = false;
topAuthorsWorksPeriod: TopAuthorsWorksPeriod = 'all_time';
topicTreemap?: TopicTreemapData;
topicTreemapLoading = false;
topicTreemapError = false;
@@ -99,8 +103,9 @@ export class DashboardComponent implements OnInit {
bmbfProjectStatusesError = false;
readonly topicTreemapMinYear = 1900;
readonly topicTreemapMaxYear = new Date().getFullYear();
topicTreemapFromYear = this.topicTreemapMaxYear - 4;
topicTreemapToYear = this.topicTreemapMaxYear;
dashboardPeriodMode: DashboardPeriodMode = 'all_time';
dashboardFromYear = this.topicTreemapMaxYear - 4;
dashboardToYear = this.topicTreemapMaxYear;
selectedTopicTreemapNode?: string;
private topicTreemapReloadQueued = false;
private topicTreemapRequestId = 0;
@@ -113,7 +118,7 @@ export class DashboardComponent implements OnInit {
) {}
ngOnInit() {
this.restoreTopicTreemapState();
this.restoreDashboardPeriodState();
this.loadStatistics();
this.loadTopAuthors();
this.loadTopicTreemap();
@@ -124,23 +129,24 @@ export class DashboardComponent implements OnInit {
private loadStatistics(): void {
this.statisticsError = false;
const period = this.selectedPeriodBounds();
this.loadStatistic(
'publications',
this.statisticsService.getPublicationCount(),
this.statisticsService.getPublicationCount(period),
);
this.loadStatistic('persons', this.statisticsService.getPersonCount());
this.loadStatistic('patents', this.statisticsService.getPatentCount());
this.loadStatistic('patents', this.statisticsService.getPatentCount(period));
this.loadStatistic(
'eu_projects',
this.statisticsService.getEuProjectCount(),
this.statisticsService.getEuProjectCount(period),
);
this.loadStatistic(
'bmbf_projects',
this.statisticsService.getBmbfProjectCount(),
this.statisticsService.getBmbfProjectCount(period),
);
this.loadStatistic(
'dfg_projects',
this.statisticsService.getDfgProjectCount(),
this.statisticsService.getDfgProjectCount(period),
);
}
@@ -199,10 +205,12 @@ export class DashboardComponent implements OnInit {
return (this.dfgProjectTopics?.topics.length ?? 0) > 0;
}
setTopAuthorsWorksPeriod(period: TopAuthorsWorksPeriod): void {
if (period === this.topAuthorsWorksPeriod) return;
this.topAuthorsWorksPeriod = period;
this.loadTopAuthors();
setDashboardPeriodMode(mode: DashboardPeriodMode): void {
if (mode === this.dashboardPeriodMode) return;
this.dashboardPeriodMode = mode;
this.applyPresetPeriod();
this.updateDashboardPeriodUrl();
this.reloadPeriodData();
}
authorBarWidth(entries: TopAuthor[], count: number): number {
@@ -253,22 +261,23 @@ export class DashboardComponent implements OnInit {
);
}
onTopicTreemapRangeChange(): void {
onDashboardPeriodRangeChange(): void {
this.dashboardPeriodMode = 'custom';
this.topicTreemapLoading = true;
this.topicTreemapError = false;
this.topicTreemapRequestId += 1;
this.updateTopicTreemapUrl();
this.updateDashboardPeriodUrl();
if (this.topicTreemapReloadQueued) return;
this.topicTreemapReloadQueued = true;
setTimeout(() => {
this.topicTreemapReloadQueued = false;
this.loadTopicTreemap();
this.reloadPeriodData();
});
}
onTopicTreemapNodeChange(nodeId: string | undefined): void {
this.selectedTopicTreemapNode = nodeId;
this.updateTopicTreemapUrl();
this.updateDashboardPeriodUrl();
}
onTopicTreemapTopicSelect(topic: TopicTreemapTopicSelect): void {
@@ -282,28 +291,51 @@ export class DashboardComponent implements OnInit {
);
}
private restoreTopicTreemapState(): void {
private restoreDashboardPeriodState(): void {
const params = this.route.snapshot.queryParamMap;
this.topicTreemapFromYear = this.parseTreemapYear(
params.get('treemapFrom'),
this.topicTreemapFromYear,
const mode = params.get('period');
if (
mode === 'all_time' ||
mode === 'last_10_years' ||
mode === 'last_5_years' ||
mode === 'custom'
) {
this.dashboardPeriodMode = mode;
}
this.applyPresetPeriod();
this.dashboardFromYear = this.parseDashboardYear(
params.get('fromYear'),
this.dashboardFromYear,
);
this.topicTreemapToYear = this.parseTreemapYear(
params.get('treemapTo'),
this.topicTreemapToYear,
this.dashboardToYear = this.parseDashboardYear(
params.get('toYear'),
this.dashboardToYear,
);
if (this.topicTreemapFromYear > this.topicTreemapToYear) {
this.topicTreemapFromYear = this.topicTreemapToYear;
if (this.dashboardFromYear > this.dashboardToYear) {
this.dashboardFromYear = this.dashboardToYear;
}
this.selectedTopicTreemapNode = params.get('treemapNode') ?? undefined;
}
private updateTopicTreemapUrl(): void {
private applyPresetPeriod(): void {
const currentYear = this.topicTreemapMaxYear;
if (this.dashboardPeriodMode === 'last_10_years') {
this.dashboardFromYear = currentYear - 9;
this.dashboardToYear = currentYear;
} else if (this.dashboardPeriodMode === 'last_5_years') {
this.dashboardFromYear = currentYear - 4;
this.dashboardToYear = currentYear;
}
}
private updateDashboardPeriodUrl(): void {
const period = this.selectedPeriodBounds();
void this.router.navigate([], {
relativeTo: this.route,
queryParams: {
treemapFrom: this.topicTreemapFromYear,
treemapTo: this.topicTreemapToYear,
period: this.dashboardPeriodMode,
fromYear: period.fromYear ?? null,
toYear: period.toYear ?? null,
treemapNode: this.selectedTopicTreemapNode ?? null,
},
queryParamsHandling: 'merge',
@@ -311,7 +343,7 @@ export class DashboardComponent implements OnInit {
});
}
private parseTreemapYear(value: string | null, fallback: number): number {
private parseDashboardYear(value: string | null, fallback: number): number {
const year = Number(value);
return Number.isInteger(year) &&
year >= this.topicTreemapMinYear &&
@@ -320,6 +352,24 @@ export class DashboardComponent implements OnInit {
: fallback;
}
private selectedPeriodBounds(): DashboardPeriodBounds {
return this.dashboardPeriodMode === 'all_time'
? {}
: {
fromYear: this.dashboardFromYear,
toYear: this.dashboardToYear,
};
}
private reloadPeriodData(): void {
this.loadStatistics();
this.loadTopAuthors();
this.loadTopicTreemap();
this.loadDfgProjectTopics();
this.loadEuProjectStatuses();
this.loadBmbfProjectStatuses();
}
private loadStatistic(
key: DashboardStatisticKey,
request: Observable<DashboardStatisticValue>,
@@ -369,7 +419,7 @@ export class DashboardComponent implements OnInit {
this.topAuthorsLoading = true;
this.topAuthorsError = false;
this.statisticsService
.getTopAuthors(this.topAuthorsWorksPeriod)
.getTopAuthors(this.selectedPeriodBounds())
.pipe(finalize(() => (this.topAuthorsLoading = false)))
.subscribe({
next: (statistics) => {
@@ -385,7 +435,7 @@ export class DashboardComponent implements OnInit {
this.dfgTopicsLoading = true;
this.dfgTopicsError = false;
this.statisticsService
.getDfgProjectTopics()
.getDfgProjectTopics(this.selectedPeriodBounds())
.pipe(finalize(() => (this.dfgTopicsLoading = false)))
.subscribe({
next: (topics) => {
@@ -401,7 +451,7 @@ export class DashboardComponent implements OnInit {
this.euProjectStatusesLoading = true;
this.euProjectStatusesError = false;
this.statisticsService
.getEuProjectStatuses()
.getEuProjectStatuses(this.selectedPeriodBounds())
.pipe(finalize(() => (this.euProjectStatusesLoading = false)))
.subscribe({
next: (statuses) => {
@@ -417,7 +467,7 @@ export class DashboardComponent implements OnInit {
this.bmbfProjectStatusesLoading = true;
this.bmbfProjectStatusesError = false;
this.statisticsService
.getBmbfProjectStatuses()
.getBmbfProjectStatuses(this.selectedPeriodBounds())
.pipe(finalize(() => (this.bmbfProjectStatusesLoading = false)))
.subscribe({
next: (statuses) => {
@@ -430,17 +480,18 @@ export class DashboardComponent implements OnInit {
}
private loadTopicTreemap(): void {
if (this.topicTreemapFromYear > this.topicTreemapToYear) {
if (this.dashboardFromYear > this.dashboardToYear) {
this.topicTreemapLoading = false;
this.topicTreemapError = true;
return;
}
const requestId = ++this.topicTreemapRequestId;
const period = this.selectedPeriodBounds();
this.topicTreemapLoading = true;
this.topicTreemapError = false;
this.statisticsService
.getTopicTreemap(this.topicTreemapFromYear, this.topicTreemapToYear)
.getTopicTreemap(period.fromYear, period.toYear)
.pipe(
finalize(() => {
if (requestId === this.topicTreemapRequestId) {