import { Component, OnInit } from '@angular/core'; import { DecimalPipe } from '@angular/common'; import { MatIconModule } from '@angular/material/icon'; import { Observable, finalize } from 'rxjs'; import { ActivatedRoute, Router, RouterLink } from '@angular/router'; import { MatDialog } from '@angular/material/dialog'; import { DfgTopicDonutComponent } from './dfg-topic-donut/dfg-topic-donut.component'; import { EuProjectStatusChartComponent } from './eu-project-status-chart/eu-project-status-chart.component'; import { YearRangeSliderComponent } from '../../shared/ui/year-range-slider/year-range-slider.component'; import { LoadingComponent } from '../../shared/ui/loading/loading.component'; import { TopicTreemapComponent } from '../../shared/ui/topic-treemap/topic-treemap.component'; import { TopicTreemapData, TopicTreemapTopicSelect, } from '../../shared/ui/topic-treemap/topic-treemap.models'; import { TopicWordCloudDialogComponent, TopicWordCloudDialogData, } from './topic-wordcloud-dialog/topic-wordcloud-dialog.component'; import { BmbfProjectStatusStatistics, DashboardPeriodBounds, DashboardStatisticValue, DashboardStatistics, DashboardStatisticsService, DfgProjectStatusStatistics, DfgProjectTopics, EuProjectStatusStatistics, TopAuthor, TopAuthorStatistics, } from './dashboard-statistics.service'; import { activeInstitutionPersonListQueryParams, PersonListQueryParams, } from '../../shared/person-list-query-params'; type DashboardStatisticKey = | 'publications' | 'persons' | 'patents' | 'eu_projects' | 'bmbf_projects' | 'dfg_projects'; type TopAuthorDataset = { title: string; icon: string; entries: TopAuthor[]; stripAuthorTitle?: boolean; }; type DashboardPeriodMode = | 'all_time' | 'last_10_years' | 'last_5_years' | 'custom'; @Component({ selector: 'dashboard', imports: [ DecimalPipe, MatIconModule, RouterLink, LoadingComponent, DfgTopicDonutComponent, EuProjectStatusChartComponent, TopicTreemapComponent, YearRangeSliderComponent, ], templateUrl: './dashboard.component.html', styleUrl: './dashboard.component.scss', }) export class DashboardComponent implements OnInit { statistics?: Partial; statisticsLoading = false; statisticsError = false; statisticLoading: Record = { publications: false, persons: false, patents: false, eu_projects: false, bmbf_projects: false, dfg_projects: false, }; statisticErrors: Record = { publications: false, persons: false, patents: false, eu_projects: false, bmbf_projects: false, dfg_projects: false, }; private pendingStatisticRequests = 0; topAuthors?: TopAuthorStatistics; topAuthorsLoading = false; topAuthorsError = false; topicTreemap?: TopicTreemapData; topicTreemapLoading = false; topicTreemapError = false; dfgProjectTopics?: DfgProjectTopics; dfgTopicsLoading = false; dfgTopicsError = false; euProjectStatuses?: EuProjectStatusStatistics; euProjectStatusesLoading = false; euProjectStatusesError = false; bmbfProjectStatuses?: BmbfProjectStatusStatistics; bmbfProjectStatusesLoading = false; bmbfProjectStatusesError = false; dfgProjectStatuses?: DfgProjectStatusStatistics; dfgProjectStatusesLoading = false; dfgProjectStatusesError = false; readonly topicTreemapMinYear = 1900; readonly topicTreemapMaxYear = new Date().getFullYear(); dashboardPeriodMode: DashboardPeriodMode = 'all_time'; dashboardFromYear = this.topicTreemapMaxYear - 4; dashboardToYear = this.topicTreemapMaxYear; selectedTopicTreemapNode?: string; private topicTreemapReloadQueued = false; private topicTreemapRequestId = 0; constructor( private statisticsService: DashboardStatisticsService, private route: ActivatedRoute, private router: Router, private dialog: MatDialog, ) {} ngOnInit() { this.restoreDashboardPeriodState(); this.loadStatistics(); this.loadTopAuthors(); this.loadTopicTreemap(); this.loadDfgProjectTopics(); this.loadEuProjectStatuses(); this.loadBmbfProjectStatuses(); this.loadDfgProjectStatuses(); } private loadStatistics(): void { this.statisticsError = false; const period = this.selectedPeriodBounds(); this.loadStatistic( 'publications', this.statisticsService.getPublicationCount(period), ); this.loadStatistic('persons', this.statisticsService.getPersonCount()); this.loadStatistic('patents', this.statisticsService.getPatentCount(period)); this.loadStatistic( 'eu_projects', this.statisticsService.getEuProjectCount(period), ); this.loadStatistic( 'bmbf_projects', this.statisticsService.getBmbfProjectCount(period), ); this.loadStatistic( 'dfg_projects', this.statisticsService.getDfgProjectCount(period), ); } isStatisticLoading(key: DashboardStatisticKey): boolean { return this.statisticLoading[key]; } topAuthorDatasets(): TopAuthorDataset[] { return [ { title: 'Publikationen', icon: 'menu_book', entries: this.topAuthors?.works ?? [], }, { title: 'EU-Projekte', icon: 'public', entries: this.topAuthors?.eu_projects ?? [], }, { title: 'Patente', icon: 'description', entries: this.topAuthors?.patents ?? [], }, { title: 'DFG Projekte', icon: 'account_tree', entries: this.topAuthors?.projects ?? [], stripAuthorTitle: true, }, { title: 'News', icon: 'article', entries: this.topAuthors?.news ?? [] }, ]; } visibleTopAuthorDatasets(): TopAuthorDataset[] { const datasets = this.topAuthorDatasets(); return this.topAuthorsLoading ? datasets : datasets.filter((dataset) => dataset.entries.length > 0); } hasTopAuthorData(): boolean { return this.topAuthorDatasets().some( (dataset) => dataset.entries.length > 0, ); } hasTopicTreemapData(): boolean { return (this.topicTreemap?.fields ?? []).some( (field) => field.total > 0 || field.values.some((value) => value.count > 0), ); } hasDfgTopicData(): boolean { return (this.dfgProjectTopics?.topics.length ?? 0) > 0; } setDashboardPeriodMode(mode: DashboardPeriodMode): void { if (mode === this.dashboardPeriodMode) return; this.dashboardPeriodMode = mode; this.applyPresetPeriod(); this.updateDashboardPeriodUrl(); this.reloadPeriodData(); } authorBarWidth(entries: TopAuthor[], count: number): number { const maximum = Math.max(...entries.map((entry) => entry.count), 1); return (count / maximum) * 100; } authorDisplayName(dataset: TopAuthorDataset, entry: TopAuthor): string { if (!dataset.stripAuthorTitle) return entry.name; return ( entry.name .replace( /^(?:(?:(?:apl\.\s*)?prof(?:essor)?\.?|priv\.-doz\.?|pd|dr\.?)\s+)+/i, '', ) .trim() || entry.name ); } authorLink(_dataset: TopAuthorDataset, entry: TopAuthor): string[] { return ['/person', entry.id!, 'overview']; } authorQueryParams( _dataset: TopAuthorDataset, _entry: TopAuthor, ): Record | null { return null; } organizationName(): string { return this.statistics?.organization_name ?? 'Ihre Organisation'; } institutionRouteId(): string { return ( this.statistics?.institution_id?.replace('https://openalex.org/', '') ?? this.topAuthors?.institution_id?.replace('https://openalex.org/', '') ?? '' ); } personListQueryParams(): PersonListQueryParams { return activeInstitutionPersonListQueryParams( this.institutionRouteId(), this.organizationName(), ); } onDashboardPeriodRangeChange(): void { this.dashboardPeriodMode = 'custom'; this.topicTreemapLoading = true; this.topicTreemapError = false; this.topicTreemapRequestId += 1; this.updateDashboardPeriodUrl(); if (this.topicTreemapReloadQueued) return; this.topicTreemapReloadQueued = true; setTimeout(() => { this.topicTreemapReloadQueued = false; this.reloadPeriodData(); }); } onTopicTreemapNodeChange(nodeId: string | undefined): void { this.selectedTopicTreemapNode = nodeId; this.updateDashboardPeriodUrl(); } onTopicTreemapTopicSelect(topic: TopicTreemapTopicSelect): void { const period = this.selectedPeriodBounds(); this.dialog.open( TopicWordCloudDialogComponent, { width: '85vw', maxWidth: '92vw', data: { ...topic, fromYear: period.fromYear, toYear: period.toYear, }, }, ); } private restoreDashboardPeriodState(): void { const params = this.route.snapshot.queryParamMap; 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.dashboardToYear = this.parseDashboardYear( params.get('toYear'), this.dashboardToYear, ); if (this.dashboardFromYear > this.dashboardToYear) { this.dashboardFromYear = this.dashboardToYear; } this.selectedTopicTreemapNode = params.get('treemapNode') ?? undefined; } 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: { period: this.dashboardPeriodMode, fromYear: period.fromYear ?? null, toYear: period.toYear ?? null, treemapNode: this.selectedTopicTreemapNode ?? null, }, queryParamsHandling: 'merge', replaceUrl: true, }); } private parseDashboardYear(value: string | null, fallback: number): number { const year = Number(value); return Number.isInteger(year) && year >= this.topicTreemapMinYear && year <= this.topicTreemapMaxYear ? year : 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(); this.loadDfgProjectStatuses(); } private loadStatistic( key: DashboardStatisticKey, request: Observable, ): void { this.statisticLoading[key] = true; this.statisticErrors[key] = false; this.pendingStatisticRequests += 1; this.statisticsLoading = true; request .pipe( finalize(() => { this.statisticLoading[key] = false; this.pendingStatisticRequests -= 1; this.statisticsLoading = this.pendingStatisticRequests > 0; }), ) .subscribe({ next: (statistic) => { this.applyStatisticValue(key, statistic); }, error: () => { this.statisticErrors[key] = true; this.statisticsError = true; }, }); } private applyStatisticValue( key: DashboardStatisticKey, statistic: DashboardStatisticValue, ): void { const nextStatistics = { ...(this.statistics ?? {}), institution_id: statistic.institution_id, organization_name: statistic.organization_name, [key]: statistic.value, }; nextStatistics.projects = (nextStatistics.eu_projects ?? 0) + (nextStatistics.bmbf_projects ?? 0) + (nextStatistics.dfg_projects ?? 0); this.statistics = nextStatistics; } private loadTopAuthors(): void { this.topAuthorsLoading = true; this.topAuthorsError = false; this.statisticsService .getTopAuthors(this.selectedPeriodBounds()) .pipe(finalize(() => (this.topAuthorsLoading = false))) .subscribe({ next: (statistics) => { this.topAuthors = statistics; }, error: () => { this.topAuthorsError = true; }, }); } private loadDfgProjectTopics(): void { this.dfgTopicsLoading = true; this.dfgTopicsError = false; this.statisticsService .getDfgProjectTopics(this.selectedPeriodBounds()) .pipe(finalize(() => (this.dfgTopicsLoading = false))) .subscribe({ next: (topics) => { this.dfgProjectTopics = topics; }, error: () => { this.dfgTopicsError = true; }, }); } private loadEuProjectStatuses(): void { this.euProjectStatusesLoading = true; this.euProjectStatusesError = false; this.statisticsService .getEuProjectStatuses(this.selectedPeriodBounds()) .pipe(finalize(() => (this.euProjectStatusesLoading = false))) .subscribe({ next: (statuses) => { this.euProjectStatuses = statuses; }, error: () => { this.euProjectStatusesError = true; }, }); } private loadBmbfProjectStatuses(): void { this.bmbfProjectStatusesLoading = true; this.bmbfProjectStatusesError = false; this.statisticsService .getBmbfProjectStatuses(this.selectedPeriodBounds()) .pipe(finalize(() => (this.bmbfProjectStatusesLoading = false))) .subscribe({ next: (statuses) => { this.bmbfProjectStatuses = statuses; }, error: () => { this.bmbfProjectStatusesError = true; }, }); } private loadDfgProjectStatuses(): void { this.dfgProjectStatusesLoading = true; this.dfgProjectStatusesError = false; this.statisticsService .getDfgProjectStatuses(this.selectedPeriodBounds()) .pipe(finalize(() => (this.dfgProjectStatusesLoading = false))) .subscribe({ next: (statuses) => { this.dfgProjectStatuses = statuses; }, error: () => { this.dfgProjectStatusesError = true; }, }); } private loadTopicTreemap(): void { 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(period.fromYear, period.toYear) .pipe( finalize(() => { if (requestId === this.topicTreemapRequestId) { this.topicTreemapLoading = false; } }), ) .subscribe({ next: (treemap) => { if (requestId === this.topicTreemapRequestId) { this.topicTreemap = treemap; } }, error: () => { if (requestId === this.topicTreemapRequestId) { this.topicTreemapError = true; } }, }); } }