Files
smartguide/webapp/src/app/modules/dashboard/dashboard.component.ts
T
farajollahi 908e28c731
LocationBasedServices/smartguide/pipeline/head This commit looks good
implement dfg topics donutchart
2026-07-21 19:04:28 +02:00

362 lines
10 KiB
TypeScript

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 { TopicHeatmapComponent } from './topic-heatmap/topic-heatmap.component';
import { DfgTopicDonutComponent } from './dfg-topic-donut/dfg-topic-donut.component';
import { YearRangeSliderComponent } from '../../shared/ui/year-range-slider/year-range-slider.component';
import { LoadingComponent } from '../../shared/ui/loading/loading.component';
import {
DashboardStatisticValue,
DashboardStatistics,
DashboardStatisticsService,
DfgProjectTopics,
TopAuthor,
TopAuthorStatistics,
TopAuthorsWorksPeriod,
TopicHeatmap,
} from './dashboard-statistics.service';
type DashboardStatisticKey =
| 'publications'
| 'persons'
| 'patents'
| 'eu_projects'
| 'bmbf_projects'
| 'dfg_projects';
@Component({
selector: 'dashboard',
imports: [
DecimalPipe,
MatIconModule,
RouterLink,
LoadingComponent,
DfgTopicDonutComponent,
TopicHeatmapComponent,
YearRangeSliderComponent,
],
templateUrl: './dashboard.component.html',
styleUrl: './dashboard.component.scss',
})
export class DashboardComponent implements OnInit {
statistics?: Partial<DashboardStatistics>;
statisticsLoading = false;
statisticsError = false;
statisticLoading: Record<DashboardStatisticKey, boolean> = {
publications: false,
persons: false,
patents: false,
eu_projects: false,
bmbf_projects: false,
dfg_projects: false,
};
statisticErrors: Record<DashboardStatisticKey, boolean> = {
publications: false,
persons: false,
patents: 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;
dfgProjectTopics?: DfgProjectTopics;
dfgTopicsLoading = false;
dfgTopicsError = false;
readonly topicTreemapMinYear = 1900;
readonly topicTreemapMaxYear = new Date().getFullYear();
topicTreemapFromYear = this.topicTreemapMaxYear - 4;
topicTreemapToYear = this.topicTreemapMaxYear;
selectedTopicTreemapNode?: string;
private topicTreemapReloadQueued = false;
private topicHeatmapRequestId = 0;
constructor(
private statisticsService: DashboardStatisticsService,
private route: ActivatedRoute,
private router: Router,
) {}
ngOnInit() {
this.restoreTopicTreemapState();
this.loadStatistics();
this.loadTopAuthors();
this.loadTopicHeatmap();
this.loadDfgProjectTopics();
}
private loadStatistics(): void {
this.statisticsError = false;
this.loadStatistic(
'publications',
this.statisticsService.getPublicationCount(),
);
this.loadStatistic('persons', this.statisticsService.getPersonCount());
this.loadStatistic('patents', this.statisticsService.getPatentCount());
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: 'EU-Projekte',
icon: 'public',
entries: this.topAuthors?.eu_projects ?? [],
},
{
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),
);
}
hasDfgTopicData(): boolean {
return (this.dfgProjectTopics?.topics.length ?? 0) > 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/', '') ??
''
);
}
onTopicTreemapRangeChange(): void {
this.topicHeatmapLoading = true;
this.topicHeatmapError = false;
this.topicHeatmapRequestId += 1;
this.updateTopicTreemapUrl();
if (this.topicTreemapReloadQueued) return;
this.topicTreemapReloadQueued = true;
setTimeout(() => {
this.topicTreemapReloadQueued = false;
this.loadTopicHeatmap();
});
}
onTopicTreemapNodeChange(nodeId: string | undefined): void {
this.selectedTopicTreemapNode = nodeId;
this.updateTopicTreemapUrl();
}
private restoreTopicTreemapState(): void {
const params = this.route.snapshot.queryParamMap;
this.topicTreemapFromYear = this.parseTreemapYear(
params.get('treemapFrom'),
this.topicTreemapFromYear,
);
this.topicTreemapToYear = this.parseTreemapYear(
params.get('treemapTo'),
this.topicTreemapToYear,
);
if (this.topicTreemapFromYear > this.topicTreemapToYear) {
this.topicTreemapFromYear = this.topicTreemapToYear;
}
this.selectedTopicTreemapNode = params.get('treemapNode') ?? undefined;
}
private updateTopicTreemapUrl(): void {
void this.router.navigate([], {
relativeTo: this.route,
queryParams: {
treemapFrom: this.topicTreemapFromYear,
treemapTo: this.topicTreemapToYear,
treemapNode: this.selectedTopicTreemapNode ?? null,
},
queryParamsHandling: 'merge',
replaceUrl: true,
});
}
private parseTreemapYear(value: string | null, fallback: number): number {
const year = Number(value);
return Number.isInteger(year) &&
year >= this.topicTreemapMinYear &&
year <= this.topicTreemapMaxYear
? year
: fallback;
}
private loadStatistic(
key: DashboardStatisticKey,
request: Observable<DashboardStatisticValue>,
): 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 loadDfgProjectTopics(): void {
this.dfgTopicsLoading = true;
this.dfgTopicsError = false;
this.statisticsService
.getDfgProjectTopics()
.pipe(finalize(() => (this.dfgTopicsLoading = false)))
.subscribe({
next: (topics) => {
this.dfgProjectTopics = topics;
},
error: () => {
this.dfgTopicsError = true;
},
});
}
private loadTopicHeatmap(): void {
if (this.topicTreemapFromYear > this.topicTreemapToYear) {
this.topicHeatmapLoading = false;
this.topicHeatmapError = true;
return;
}
const requestId = ++this.topicHeatmapRequestId;
this.topicHeatmapLoading = true;
this.topicHeatmapError = false;
this.statisticsService
.getTopicHeatmap(this.topicTreemapFromYear, this.topicTreemapToYear)
.pipe(
finalize(() => {
if (requestId === this.topicHeatmapRequestId) {
this.topicHeatmapLoading = false;
}
}),
)
.subscribe({
next: (heatmap) => {
if (requestId === this.topicHeatmapRequestId) {
this.topicHeatmap = heatmap;
}
},
error: () => {
if (requestId === this.topicHeatmapRequestId) {
this.topicHeatmapError = true;
}
},
});
}
}