import { Component, OnInit } from '@angular/core'; import { DecimalPipe } from '@angular/common'; import { MatIconModule } from '@angular/material/icon'; import { Observable, finalize } from 'rxjs'; import { RouterLink } from '@angular/router'; import { TopicHeatmapComponent } from './topic-heatmap/topic-heatmap.component'; import { DashboardStatisticValue, DashboardStatistics, DashboardStatisticsService, TopAuthor, TopAuthorStatistics, TopAuthorsWorksPeriod, TopicHeatmap, } from './dashboard-statistics.service'; type DashboardStatisticKey = | 'publications' | 'persons' | 'eu_projects' | 'bmbf_projects' | 'dfg_projects'; type TopicTreemapPeriod = | 'last_5_years' | 'last_10_years' | 'all_time' | 'custom'; @Component({ selector: 'dashboard', imports: [DecimalPipe, MatIconModule, RouterLink, TopicHeatmapComponent], 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, eu_projects: false, bmbf_projects: false, dfg_projects: false, }; statisticErrors: Record = { publications: false, persons: false, eu_projects: false, bmbf_projects: false, dfg_projects: false, }; private pendingStatisticRequests = 0; topAuthors?: TopAuthorStatistics; topAuthorsLoading = false; topAuthorsError = false; topAuthorsWorksPeriod: TopAuthorsWorksPeriod = 'all_time'; topicHeatmap?: TopicHeatmap; topicHeatmapLoading = false; topicHeatmapError = false; topicTreemapPeriod: TopicTreemapPeriod = 'last_5_years'; topicTreemapFromYear = new Date().getFullYear() - 4; topicTreemapToYear = new Date().getFullYear(); constructor(private statisticsService: DashboardStatisticsService) {} ngOnInit() { this.loadStatistics(); this.loadTopAuthors(); this.loadTopicHeatmap(); } private loadStatistics(): void { this.statisticsError = false; this.loadStatistic( 'publications', this.statisticsService.getPublicationCount(), ); this.loadStatistic('persons', this.statisticsService.getPersonCount()); this.loadStatistic( 'eu_projects', this.statisticsService.getEuProjectCount(), ); this.loadStatistic( 'bmbf_projects', this.statisticsService.getBmbfProjectCount(), ); this.loadStatistic( 'dfg_projects', this.statisticsService.getDfgProjectCount(), ); } isStatisticLoading(key: DashboardStatisticKey): boolean { return this.statisticLoading[key]; } topAuthorDatasets(): Array<{ title: string; icon: string; entries: TopAuthor[]; }> { return [ { title: 'Publikationen', icon: 'menu_book', entries: this.topAuthors?.works ?? [], }, { title: 'Patente', icon: 'description', entries: this.topAuthors?.patents ?? [], }, { title: 'News', icon: 'article', entries: this.topAuthors?.news ?? [] }, ]; } visibleTopAuthorDatasets(): Array<{ title: string; icon: string; entries: TopAuthor[]; }> { 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, ); } hasTopicHeatmapData(): boolean { return (this.topicHeatmap?.fields ?? []).some( (field) => field.total > 0 || field.values.some((value) => value.count > 0), ); } setTopAuthorsWorksPeriod(period: TopAuthorsWorksPeriod): void { if (period === this.topAuthorsWorksPeriod) return; this.topAuthorsWorksPeriod = period; this.loadTopAuthors(); } authorBarWidth(entries: TopAuthor[], count: number): number { const maximum = Math.max(...entries.map((entry) => entry.count), 1); return (count / maximum) * 100; } 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/', '') ?? '' ); } setTopicTreemapPeriod(period: TopicTreemapPeriod): void { if (period === this.topicTreemapPeriod && period !== 'custom') return; const currentYear = new Date().getFullYear(); this.topicTreemapPeriod = period; if (period === 'last_5_years') { this.topicTreemapFromYear = currentYear - 4; this.topicTreemapToYear = currentYear; } else if (period === 'last_10_years') { this.topicTreemapFromYear = currentYear - 9; this.topicTreemapToYear = currentYear; } else if (period === 'all_time') { this.topicTreemapFromYear = 1900; this.topicTreemapToYear = currentYear; } this.loadTopicHeatmap(); } onTopicTreemapYearChange(field: 'from' | 'to', value: string): void { const year = Number(value); if (!Number.isInteger(year)) return; if (field === 'from') { this.topicTreemapFromYear = year; } else { this.topicTreemapToYear = year; } this.topicTreemapPeriod = 'custom'; this.loadTopicHeatmap(); } 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.topAuthorsWorksPeriod) .pipe(finalize(() => (this.topAuthorsLoading = false))) .subscribe({ next: (statistics) => { this.topAuthors = statistics; }, error: () => { this.topAuthorsError = true; }, }); } private loadTopicHeatmap(): void { if (this.topicTreemapFromYear > this.topicTreemapToYear) { this.topicHeatmapError = true; return; } this.topicHeatmapLoading = true; this.topicHeatmapError = false; this.statisticsService .getTopicHeatmap(this.topicTreemapFromYear, this.topicTreemapToYear) .pipe(finalize(() => (this.topicHeatmapLoading = false))) .subscribe({ next: (heatmap) => { this.topicHeatmap = heatmap; }, error: () => { this.topicHeatmapError = true; }, }); } }