Files
smartguide/webapp/src/app/modules/dashboard/dashboard.component.ts
T
spsaefan c2a2a31fb9
LocationBasedServices/smartguide/pipeline/head This commit looks good
LocationBasedServices/smartguide/pipeline/pr-dev Build started...
Merge dev into dfg PG authors
2026-07-25 23:53:49 +02:00

465 lines
13 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 { 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,
DashboardStatisticValue,
DashboardStatistics,
DashboardStatisticsService,
DfgProjectTopics,
EuProjectStatusStatistics,
TopAuthor,
TopAuthorStatistics,
TopAuthorsWorksPeriod,
} 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;
};
@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<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';
topicTreemap?: TopicTreemapData;
topicTreemapLoading = false;
topicTreemapError = false;
dfgProjectTopics?: DfgProjectTopics;
dfgTopicsLoading = false;
dfgTopicsError = false;
euProjectStatuses?: EuProjectStatusStatistics;
euProjectStatusesLoading = false;
euProjectStatusesError = false;
bmbfProjectStatuses?: BmbfProjectStatusStatistics;
bmbfProjectStatusesLoading = false;
bmbfProjectStatusesError = false;
readonly topicTreemapMinYear = 1900;
readonly topicTreemapMaxYear = new Date().getFullYear();
topicTreemapFromYear = this.topicTreemapMaxYear - 4;
topicTreemapToYear = 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.restoreTopicTreemapState();
this.loadStatistics();
this.loadTopAuthors();
this.loadTopicTreemap();
this.loadDfgProjectTopics();
this.loadEuProjectStatuses();
this.loadBmbfProjectStatuses();
}
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(): 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;
}
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;
}
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<string, string | number> | 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(),
);
}
onTopicTreemapRangeChange(): void {
this.topicTreemapLoading = true;
this.topicTreemapError = false;
this.topicTreemapRequestId += 1;
this.updateTopicTreemapUrl();
if (this.topicTreemapReloadQueued) return;
this.topicTreemapReloadQueued = true;
setTimeout(() => {
this.topicTreemapReloadQueued = false;
this.loadTopicTreemap();
});
}
onTopicTreemapNodeChange(nodeId: string | undefined): void {
this.selectedTopicTreemapNode = nodeId;
this.updateTopicTreemapUrl();
}
onTopicTreemapTopicSelect(topic: TopicTreemapTopicSelect): void {
this.dialog.open<TopicWordCloudDialogComponent, TopicWordCloudDialogData>(
TopicWordCloudDialogComponent,
{
width: '85vw',
maxWidth: '92vw',
data: topic,
},
);
}
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 loadEuProjectStatuses(): void {
this.euProjectStatusesLoading = true;
this.euProjectStatusesError = false;
this.statisticsService
.getEuProjectStatuses()
.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()
.pipe(finalize(() => (this.bmbfProjectStatusesLoading = false)))
.subscribe({
next: (statuses) => {
this.bmbfProjectStatuses = statuses;
},
error: () => {
this.bmbfProjectStatusesError = true;
},
});
}
private loadTopicTreemap(): void {
if (this.topicTreemapFromYear > this.topicTreemapToYear) {
this.topicTreemapLoading = false;
this.topicTreemapError = true;
return;
}
const requestId = ++this.topicTreemapRequestId;
this.topicTreemapLoading = true;
this.topicTreemapError = false;
this.statisticsService
.getTopicTreemap(this.topicTreemapFromYear, this.topicTreemapToYear)
.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;
}
},
});
}
}