Files
smartguide/webapp/src/app/modules/dashboard/dashboard.component.ts
T

788 lines
23 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { DecimalPipe } from '@angular/common';
import { MatIconModule } from '@angular/material/icon';
import { MatTooltipModule } from '@angular/material/tooltip';
import { Observable, finalize } from 'rxjs';
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
import { MatDialog } from '@angular/material/dialog';
import { ComponentTooltipDirective } from '../../directives/component-tooltip.directive';
import { AppAuthorTooltipComponent } from '../../shared/ui/app-author-tooltip/app-author-tooltip.component';
import {
ACCOUNT_BALANCE_ICON,
ACCOUNT_TREE_ICON,
ARTICLE_ICON,
DESCRIPTION_ICON,
GROUPS_ICON,
MENU_BOOK_ICON,
PATENT_ICON,
PUBLIC_ICON,
SCIENCE_ICON,
STACKED_BAR_CHART_ICON,
} from '../../shared/ui/icons';
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,
BundProjectTopics,
DashboardPeriodBounds,
DashboardStatisticValue,
DashboardStatistics,
DashboardStatisticsService,
DfgProjectStatusStatistics,
DfgProjectTopic,
DfgProjectTopics,
EuProjectStatusStatistics,
EuProjectTopics,
TopAuthor,
TopAuthorStatistics,
} from './dashboard-statistics.service';
import {
institutionPersonListQueryParamsForPeriod,
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,
MatTooltipModule,
ComponentTooltipDirective,
RouterLink,
LoadingComponent,
EuProjectStatusChartComponent,
TopicTreemapComponent,
YearRangeSliderComponent,
],
templateUrl: './dashboard.component.html',
styleUrl: './dashboard.component.scss',
})
export class DashboardComponent implements OnInit {
readonly AuthorTooltipComponent = AppAuthorTooltipComponent;
readonly patentIcon = PATENT_ICON;
readonly MENU_BOOK_ICON = MENU_BOOK_ICON;
readonly GROUPS_ICON = GROUPS_ICON;
readonly PUBLIC_ICON = PUBLIC_ICON;
readonly ACCOUNT_BALANCE_ICON = ACCOUNT_BALANCE_ICON;
readonly SCIENCE_ICON = SCIENCE_ICON;
readonly STACKED_BAR_CHART_ICON = STACKED_BAR_CHART_ICON;
readonly ACCOUNT_TREE_ICON = ACCOUNT_TREE_ICON;
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;
topicTreemap?: TopicTreemapData;
topicTreemapLoading = false;
topicTreemapError = false;
dfgProjectTopics?: DfgProjectTopics;
dfgTopicTreemap?: TopicTreemapData;
dfgTopicsLoading = false;
dfgTopicsError = false;
euProjectTopics?: EuProjectTopics;
euTopicTreemap?: TopicTreemapData;
euTopicsLoading = false;
euTopicsError = false;
bundProjectTopics?: BundProjectTopics;
bundTopicTreemap?: TopicTreemapData;
bundTopicsLoading = false;
bundTopicsError = 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.topicTreemapMinYear;
dashboardToYear = this.topicTreemapMaxYear;
selectedTopicTreemapNode?: string;
selectedDfgTopicTreemapNode?: string;
selectedEuTopicTreemapNode?: string;
selectedBundTopicTreemapNode?: 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.loadEuProjectTopics();
this.loadBundProjectTopics();
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(period),
);
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_ICON,
entries: this.topAuthors?.works ?? [],
},
{
title: 'EU-Projekte',
icon: PUBLIC_ICON,
entries: this.topAuthors?.eu_projects ?? [],
},
{
title: 'Patente',
icon: DESCRIPTION_ICON,
entries: this.topAuthors?.patents ?? [],
},
{
title: 'DFG Projekte',
icon: ACCOUNT_TREE_ICON,
entries: this.topAuthors?.projects ?? [],
stripAuthorTitle: true,
},
{
title: 'News',
icon: ARTICLE_ICON,
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;
}
private buildDfgTopicTreemapData(
projectTopics: DfgProjectTopics,
): TopicTreemapData {
const fields = new Map<
string,
Map<string, { topics: DfgProjectTopic[] }>
>();
for (const topic of projectTopics.topics) {
const fieldName = topic.field?.trim() || 'Ohne Field';
const subfieldName = topic.subfield?.trim() || 'Ohne Subfield';
const subfields =
fields.get(fieldName) ??
new Map<string, { topics: DfgProjectTopic[] }>();
const subfield = subfields.get(subfieldName) ?? { topics: [] };
subfield.topics.push(topic);
subfields.set(subfieldName, subfield);
fields.set(fieldName, subfields);
}
return {
institution_id: projectTopics.institution_id,
organization_name: projectTopics.organization_name,
years: [],
fields: [...fields.entries()].map(([fieldName, subfields]) => {
const mappedSubfields = [...subfields.entries()].map(
([subfieldName, group]) => ({
id: `${fieldName}::${subfieldName}`,
name: subfieldName,
total:
group.topics[0]?.subfield_project_count ??
group.topics.reduce((sum, topic) => sum + topic.project_count, 0),
values: [],
topics: group.topics.map((topic) => ({
id: topic.id,
name: topic.name,
total: topic.project_count,
values: [],
})),
}),
);
return {
id: fieldName,
name: fieldName,
total:
[...subfields.values()][0]?.topics[0]?.field_project_count ??
mappedSubfields.reduce((sum, subfield) => sum + subfield.total, 0),
values: [],
subfields: mappedSubfields,
};
}),
};
}
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<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 {
const period = this.selectedPeriodBounds();
return institutionPersonListQueryParamsForPeriod(
this.institutionRouteId(),
this.organizationName(),
period.fromYear,
period.toYear,
);
}
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();
}
onDfgTopicTreemapNodeChange(nodeId: string | undefined): void {
this.selectedDfgTopicTreemapNode = nodeId;
this.updateDashboardPeriodUrl();
}
onDfgTopicTreemapTopicSelect(topic: TopicTreemapTopicSelect): void {
const dfgTopic = this.dfgProjectTopics?.topics.find(
(entry) => entry.id === topic.topicId,
);
if (!dfgTopic) return;
this.dialog.open<TopicWordCloudDialogComponent, TopicWordCloudDialogData>(
TopicWordCloudDialogComponent,
{
width: '85vw',
maxWidth: '92vw',
data: {
...topic,
source: 'dfg',
words: dfgTopic.keywords.map((keyword) => ({
id: keyword.name,
name: keyword.name,
count: keyword.project_count,
})),
},
},
);
}
onEuTopicTreemapNodeChange(nodeId: string | undefined): void {
this.selectedEuTopicTreemapNode = nodeId;
this.updateDashboardPeriodUrl();
}
onEuTopicTreemapTopicSelect(topic: TopicTreemapTopicSelect): void {
const euTopic = this.euProjectTopics?.topics.find(
(entry) => entry.id === topic.topicId,
);
if (!euTopic) return;
this.dialog.open<TopicWordCloudDialogComponent, TopicWordCloudDialogData>(
TopicWordCloudDialogComponent,
{
width: '85vw',
maxWidth: '92vw',
data: {
...topic,
source: 'eu',
words: euTopic.keywords.map((keyword) => ({
id: keyword.name,
name: keyword.name,
count: keyword.project_count,
})),
},
},
);
}
onBundTopicTreemapNodeChange(nodeId: string | undefined): void {
this.selectedBundTopicTreemapNode = nodeId;
this.updateDashboardPeriodUrl();
}
onBundTopicTreemapTopicSelect(topic: TopicTreemapTopicSelect): void {
const bundTopic = this.bundProjectTopics?.topics.find(
(entry) => entry.id === topic.topicId,
);
if (!bundTopic) return;
this.dialog.open<TopicWordCloudDialogComponent, TopicWordCloudDialogData>(
TopicWordCloudDialogComponent,
{
width: '85vw',
maxWidth: '92vw',
data: {
...topic,
source: 'bund',
words: bundTopic.keywords.map((keyword) => ({
id: keyword.name,
name: keyword.name,
count: keyword.project_count,
})),
},
},
);
}
onTopicTreemapTopicSelect(topic: TopicTreemapTopicSelect): void {
const period = this.selectedPeriodBounds();
this.dialog.open<TopicWordCloudDialogComponent, TopicWordCloudDialogData>(
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();
if (this.dashboardPeriodMode !== 'all_time') {
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;
this.selectedDfgTopicTreemapNode =
params.get('dfgTreemapNode') ?? undefined;
this.selectedEuTopicTreemapNode = params.get('euTreemapNode') ?? undefined;
this.selectedBundTopicTreemapNode =
params.get('bundTreemapNode') ?? undefined;
}
private applyPresetPeriod(): void {
const currentYear = this.topicTreemapMaxYear;
if (this.dashboardPeriodMode === 'all_time') {
this.dashboardFromYear = this.topicTreemapMinYear;
this.dashboardToYear = currentYear;
} else 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,
dfgTreemapNode: this.selectedDfgTopicTreemapNode ?? null,
euTreemapNode: this.selectedEuTopicTreemapNode ?? null,
bundTreemapNode: this.selectedBundTopicTreemapNode ?? 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.loadEuProjectTopics();
this.loadBundProjectTopics();
this.loadEuProjectStatuses();
this.loadBmbfProjectStatuses();
this.loadDfgProjectStatuses();
}
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.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;
this.dfgTopicTreemap = this.buildDfgTopicTreemapData(topics);
},
error: () => {
this.dfgTopicsError = true;
},
});
}
private loadEuProjectTopics(): void {
this.euTopicsLoading = true;
this.euTopicsError = false;
this.statisticsService
.getEuProjectTopics(this.selectedPeriodBounds())
.pipe(finalize(() => (this.euTopicsLoading = false)))
.subscribe({
next: (topics) => {
this.euProjectTopics = topics;
this.euTopicTreemap = this.buildDfgTopicTreemapData(topics);
},
error: () => {
this.euTopicsError = true;
},
});
}
private loadBundProjectTopics(): void {
this.bundTopicsLoading = true;
this.bundTopicsError = false;
this.statisticsService
.getBundProjectTopics(this.selectedPeriodBounds())
.pipe(finalize(() => (this.bundTopicsLoading = false)))
.subscribe({
next: (topics) => {
this.bundProjectTopics = topics;
this.bundTopicTreemap = this.buildDfgTopicTreemapData(topics);
},
error: () => {
this.bundTopicsError = 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;
}
},
});
}
}