Implement interactive metadata review for batch updates, with progress tracking and a unified notification system

This commit is contained in:
aditya.chandel
2025-07-13 23:32:08 -06:00
committed by Aditya Chandel
parent 6576f3351a
commit 116f0c950e
95 changed files with 2557 additions and 781 deletions
@@ -1,5 +1,4 @@
<div class="flex flex-col min-w-20">
<p>{{latestEvent.timestamp}}</p>
<p>{{latestEvent.message}}</p>
<div class="flex flex-col px-4 py-6 live-border">
<p>{{ latestNotification.timestamp }}</p>
<p>{{ latestNotification.message }}</p>
</div>
@@ -1 +1,5 @@
.live-border {
background: var(--card-background);
border: 1px solid var(--primary-color);
border-radius: 0.5rem;
}
@@ -1,25 +1,24 @@
import {Component, inject, Input} from '@angular/core';
import {Component, inject} from '@angular/core';
import {LogNotification} from '../../../shared/websocket/model/log-notification.model';
import {EventService} from '../../../shared/websocket/event.service';
import {NotificationEventService} from '../../../shared/websocket/notification-event.service';
@Component({
selector: 'app-live-notification-box',
standalone: true,
templateUrl: './live-notification-box.component.html',
styleUrl: './live-notification-box.component.scss',
styleUrls: ['./live-notification-box.component.scss'],
host: {
class: 'config-panel'
},
})
export class LiveNotificationBoxComponent {
latestNotification: LogNotification = {message: 'No recent notifications...'};
latestEvent: LogNotification = {message: 'No recent notifications...'};
private eventService = inject(EventService);
private notificationService = inject(NotificationEventService);
constructor() {
this.eventService.latestEvent$.subscribe(event => {
this.latestEvent = event;
this.notificationService.latestNotification$.subscribe(notification => {
this.latestNotification = notification;
});
}
}
@@ -0,0 +1,60 @@
<div class="task-border mt-4">
@if (Object.keys(activeTasks).length > 0) {
@for (task of activeTasks | keyvalue; track task; let idx = $index) {
<div class="task-card p-4">
<div class="flex items-center justify-between mb-2">
<p class="text-sm font-semibold text-[var(--primary-color)]">Metadata Fetch Task</p>
<p-tag
[value]="getStatusLabel(task.value.status)"
[severity]="getTagSeverity(task.value.status)"
class="text-xs"
></p-tag>
</div>
<div class="task-message text-gray-200">
{{ task.value.message }}
</div>
<div class="task-progress-info text-sm text-gray-300">
Book <strong>{{ task.value.status === 'COMPLETED' || task.value.completed >= task.value.total ? task.value.total : task.value.completed + 1 }}</strong>
of {{ task.value.total }}
</div>
<p-progressBar
[style]="{ height: '15px' }"
[value]="getProgressPercent(task.value)"
[showValue]="true">
</p-progressBar>
@if (task.value.status === 'COMPLETED' || task.value.status === 'ERROR') {
<div class="action-buttons mt-4 flex justify-end gap-2">
<p-button
label="Review"
severity="info"
icon="pi pi-search"
size="small"
outlined
(click)="reviewTask(task.key)">
</p-button>
<p-button
label="Discard"
icon="pi pi-times"
severity="danger"
size="small"
pTooltip="Deletes the fetched metadata"
tooltipPosition="top"
outlined
(click)="clearTask(task.key)">
</p-button>
</div>
}
</div>
@if (idx < Object.keys(activeTasks).length - 1) {
<p-divider></p-divider>
}
}
}
</div>
@@ -0,0 +1,28 @@
.task-border {
background: var(--card-background);
border: 1px solid var(--primary-color);
border-radius: 0.5rem;
}
.task-header {
display: flex;
justify-content: flex-end;
margin-bottom: 0.5rem;
}
.task-message {
font-size: 1rem;
color: var(--text-color);
margin-bottom: 0.5rem;
}
.task-progress-info {
font-size: 0.875rem;
color: var(--text-secondary-color);
margin-bottom: 0.5rem;
}
.review-button {
text-align: right;
margin-top: 0.75rem;
}
@@ -0,0 +1,140 @@
import {Component, inject, OnDestroy, OnInit} from '@angular/core';
import {Subject} from 'rxjs';
import {takeUntil} from 'rxjs/operators';
import {KeyValuePipe} from '@angular/common';
import {ProgressBarModule} from 'primeng/progressbar';
import {ButtonModule} from 'primeng/button';
import {Divider} from 'primeng/divider';
import {Tooltip} from 'primeng/tooltip';
import {DialogService} from 'primeng/dynamicdialog';
import {
MetadataBatchProgressNotification,
MetadataBatchStatus,
MetadataBatchStatusLabels
} from '../../model/metadata-batch-progress.model';
import {MetadataProgressService} from '../../service/metadata-progress-service';
import {MetadataReviewDialogComponent} from '../../../metadata/metadata-review-dialog-component/metadata-review-dialog-component';
import {MetadataTaskService} from '../../../book/service/metadata-task';
import {Tag} from 'primeng/tag';
@Component({
selector: 'app-metadata-progress-widget',
templateUrl: './metadata-progress-widget-component.html',
styleUrls: ['./metadata-progress-widget-component.scss'],
standalone: true,
imports: [KeyValuePipe, ProgressBarModule, ButtonModule, Divider, Tooltip, Tag]
})
export class MetadataProgressWidgetComponent implements OnInit, OnDestroy {
activeTasks: { [taskId: string]: MetadataBatchProgressNotification } = {};
private destroy$ = new Subject<void>();
private dialogService = inject(DialogService);
private metadataProgressService = inject(MetadataProgressService);
private metadataTaskService = inject(MetadataTaskService);
private lastUpdateMap = new Map<string, number>();
private timeoutHandles = new Map<string, any>();
private readonly TASK_STALL_TIMEOUT_MS = 60 * 1000; // 1 minute
ngOnInit(): void {
this.metadataProgressService.activeTasks$
.pipe(takeUntil(this.destroy$))
.subscribe(tasks => {
this.activeTasks = tasks;
this.checkForStalledTasks(tasks);
});
}
private checkForStalledTasks(tasks: { [taskId: string]: MetadataBatchProgressNotification }): void {
const now = Date.now();
for (const taskId of this.timeoutHandles.keys()) {
if (!tasks[taskId]) {
clearTimeout(this.timeoutHandles.get(taskId));
this.timeoutHandles.delete(taskId);
this.lastUpdateMap.delete(taskId);
}
}
for (const [taskId, task] of Object.entries(tasks)) {
this.lastUpdateMap.set(taskId, now);
if (this.timeoutHandles.has(taskId)) {
clearTimeout(this.timeoutHandles.get(taskId));
}
this.timeoutHandles.set(
taskId,
setTimeout(() => {
this.markTaskStalled(taskId);
}, this.TASK_STALL_TIMEOUT_MS)
);
}
}
private markTaskStalled(taskId: string): void {
const task = this.activeTasks[taskId];
if (!task) return;
if (task.status !== MetadataBatchStatus.COMPLETED && task.status !== 'ERROR') {
this.activeTasks[taskId] = {
...task,
status: MetadataBatchStatus.ERROR,
message: 'Task stalled or backend unavailable'
};
this.activeTasks = {...this.activeTasks};
}
}
getProgressPercent(task: MetadataBatchProgressNotification): number {
return task.total > 0 ? Math.round((task.completed / task.total) * 100) : 0;
}
clearTask(taskId: string): void {
this.metadataTaskService.deleteTask(taskId).subscribe(() => {
this.metadataProgressService.clearTask(taskId);
clearTimeout(this.timeoutHandles.get(taskId));
this.timeoutHandles.delete(taskId);
this.lastUpdateMap.delete(taskId);
});
}
reviewTask(taskId: string): void {
this.dialogService.open(MetadataReviewDialogComponent, {
showHeader: false,
width: '90vw',
height: '90vh',
data: {taskId},
closable: false,
modal: true
});
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
for (const timeout of this.timeoutHandles.values()) {
clearTimeout(timeout);
}
this.timeoutHandles.clear();
this.lastUpdateMap.clear();
}
getTagSeverity(status: 'IN_PROGRESS' | 'COMPLETED' | 'ERROR'): 'info' | 'success' | 'danger' {
switch (status) {
case 'COMPLETED':
return 'success';
case 'ERROR':
return 'danger';
case 'IN_PROGRESS':
default:
return 'info';
}
}
getStatusLabel(status: MetadataBatchStatus): string {
return MetadataBatchStatusLabels[status] ?? status;
}
protected readonly Object = Object;
}
@@ -0,0 +1,7 @@
<div class="metadata-progress-box flex flex-col w-[25rem] max-h-[60vh] overflow-y-auto">
<app-live-notification-box/>
@if (hasMetadataTasks$ | async) {
<app-metadata-progress-widget/>
}
</div>
@@ -0,0 +1,9 @@
.metadata-progress-box {
overflow-y: auto;
border-radius: 0.5rem;
}
::ng-deep .p-divider-horizontal {
margin: 0 !important;
padding: 0 !important;
}
@@ -0,0 +1,25 @@
import {Component, inject} from '@angular/core';
import {LiveNotificationBoxComponent} from '../live-notification-box/live-notification-box.component';
import {MetadataProgressWidgetComponent} from '../metadata-progress-widget-component/metadata-progress-widget-component';
import {MetadataProgressService} from '../../service/metadata-progress-service';
import {map} from 'rxjs/operators';
import {AsyncPipe} from '@angular/common';
@Component({
selector: 'app-unified-notification-popover-component',
imports: [
LiveNotificationBoxComponent,
MetadataProgressWidgetComponent,
AsyncPipe
],
templateUrl: './unified-notification-popover-component.html',
standalone: true,
styleUrl: './unified-notification-popover-component.scss'
})
export class UnifiedNotificationBoxComponent {
metadataProgressService = inject(MetadataProgressService);
hasMetadataTasks$ = this.metadataProgressService.activeTasks$.pipe(
map(tasks => Object.keys(tasks).length > 0)
);
}
@@ -1,4 +1,4 @@
import {MetadataRefreshOptions} from '../../book/metadata/model/request/metadata-refresh-options.model';
import {MetadataRefreshOptions} from '../../metadata/model/request/metadata-refresh-options.model';
export interface MetadataMatchWeights {
title: number;
@@ -0,0 +1,19 @@
export enum MetadataBatchStatus {
IN_PROGRESS = 'IN_PROGRESS',
COMPLETED = 'COMPLETED',
ERROR = 'ERROR',
}
export const MetadataBatchStatusLabels: Record<MetadataBatchStatus, string> = {
[MetadataBatchStatus.IN_PROGRESS]: 'In Progress',
[MetadataBatchStatus.COMPLETED]: 'Completed',
[MetadataBatchStatus.ERROR]: 'Error',
};
export interface MetadataBatchProgressNotification {
taskId: string;
completed: number;
total: number;
message: string;
status: MetadataBatchStatus;
}
@@ -0,0 +1,87 @@
import {inject, Injectable, OnDestroy} from '@angular/core';
import {BehaviorSubject, Subject, Subscription} from 'rxjs';
import {MetadataBatchProgressNotification} from '../model/metadata-batch-progress.model';
import {MetadataTaskService} from '../../book/service/metadata-task';
import {UserService} from '../../settings/user-management/user.service';
import {filter, take} from 'rxjs/operators';
@Injectable({providedIn: 'root'})
export class MetadataProgressService implements OnDestroy {
private progressMap = new Map<string, BehaviorSubject<MetadataBatchProgressNotification>>();
private progressUpdatesSubject = new Subject<MetadataBatchProgressNotification>();
progressUpdates$ = this.progressUpdatesSubject.asObservable();
private activeTasksSubject = new BehaviorSubject<{ [taskId: string]: MetadataBatchProgressNotification }>({});
activeTasks$ = this.activeTasksSubject.asObservable();
private metadataTaskService = inject(MetadataTaskService);
private userService = inject(UserService);
private subscriptions = new Subscription();
constructor() {
const sub = this.userService.userState$
.pipe(
filter(user => !!user),
take(1)
)
.subscribe(user => {
if (!this.hasMetadataPermissions(user)) {
return;
}
const activeTasksSub = this.metadataTaskService.getActiveTasks().subscribe({
next: (tasks) => this.initializeActiveTasks(tasks),
error: (err) => console.warn('Failed to fetch active metadata tasks:', err)
});
this.subscriptions.add(activeTasksSub);
});
this.subscriptions.add(sub);
}
handleIncomingProgress(progress: MetadataBatchProgressNotification): void {
const {taskId} = progress;
if (!this.progressMap.has(taskId)) {
this.progressMap.set(taskId, new BehaviorSubject(progress));
} else {
this.progressMap.get(taskId)!.next(progress);
}
this.progressUpdatesSubject.next(progress);
this.activeTasksSubject.next(this.getActiveTasks());
}
clearTask(taskId: string): void {
this.progressMap.delete(taskId);
this.activeTasksSubject.next(this.getActiveTasks());
}
getActiveTasks(): { [taskId: string]: MetadataBatchProgressNotification } {
const result: { [taskId: string]: MetadataBatchProgressNotification } = {};
this.progressMap.forEach((subject, taskId) => {
result[taskId] = subject.getValue();
});
return result;
}
private hasMetadataPermissions(user: any): boolean {
return user?.permissions?.admin || user?.permissions?.canEditMetadata;
}
private initializeActiveTasks(tasks: MetadataBatchProgressNotification[]): void {
for (const task of tasks) {
this.progressMap.set(task.taskId, new BehaviorSubject(task));
this.progressUpdatesSubject.next(task);
}
this.activeTasksSubject.next(this.getActiveTasks());
}
ngOnDestroy(): void {
this.subscriptions.unsubscribe();
this.progressMap.forEach(subject => subject.complete());
this.progressUpdatesSubject.complete();
this.activeTasksSubject.complete();
}
}
@@ -18,7 +18,7 @@
<form class="flex flex-col gap-4" [formGroup]="setupForm" (ngSubmit)="onSubmit()">
<div class="flex flex-col gap-2">
<label for="username" class="text-xl font-medium">Username</label>
<label for="username" class="text-lg font-medium">Username</label>
<input
pInputText
id="username"
@@ -36,7 +36,7 @@
</div>
<div class="flex flex-col gap-2">
<label for="name" class="text-xl font-medium">Name</label>
<label for="name" class="text-lg font-medium">Name</label>
<input
pInputText
id="name"
@@ -54,7 +54,7 @@
</div>
<div class="flex flex-col gap-2">
<label for="email" class="text-xl font-medium">Email</label>
<label for="email" class="text-lg font-medium">Email</label>
<input
pInputText
id="email"
@@ -72,7 +72,7 @@
</div>
<div class="flex flex-col gap-2">
<label for="password" class="text-xl font-medium">Password</label>
<label for="password" class="text-lg font-medium">Password</label>
<input
pInputText
id="password"