Implement live progress tracking for bulk metadata refresh (remaining books, status, cancel support)

This commit is contained in:
aditya.chandel
2025-08-10 00:28:20 -06:00
committed by Aditya Chandel
parent 2c078716bf
commit b1508493cf
23 changed files with 586 additions and 112 deletions
+8 -1
View File
@@ -3,7 +3,7 @@ import {RxStompService} from './shared/websocket/rx-stomp.service';
import {Message} from '@stomp/stompjs';
import {BookService} from './book/service/book.service';
import {NotificationEventService} from './shared/websocket/notification-event.service';
import {parseLogNotification} from './shared/websocket/model/log-notification.model';
import {parseLogNotification, parseTaskMessage, TaskMessage} from './shared/websocket/model/log-notification.model';
import {ConfirmDialog} from 'primeng/confirmdialog';
import {Toast} from 'primeng/toast';
import {RouterOutlet} from '@angular/router';
@@ -12,6 +12,7 @@ import {AppConfigService} from './core/service/app-config.service';
import {MetadataBatchProgressNotification} from './core/model/metadata-batch-progress.model';
import {MetadataProgressService} from './core/service/metadata-progress-service';
import {BookdropFileService, BookdropFileNotification} from './bookdrop/bookdrop-file.service';
import {TaskEventService} from './shared/websocket/task-event.service';
@Component({
selector: 'app-root',
@@ -29,6 +30,7 @@ export class AppComponent implements OnInit {
private notificationEventService = inject(NotificationEventService);
private metadataProgressService = inject(MetadataProgressService);
private bookdropFileService = inject(BookdropFileService);
private taskEventService = inject(TaskEventService);
private appConfigService = inject(AppConfigService);
ngOnInit(): void {
@@ -64,6 +66,11 @@ export class AppComponent implements OnInit {
this.notificationEventService.handleNewNotification(logNotification);
});
this.rxStompService.watch('/topic/task').subscribe((message: Message) => {
const taskMessage: TaskMessage = parseTaskMessage(message.body);
this.taskEventService.handleTaskMessage(taskMessage);
});
this.rxStompService.watch('/topic/bookdrop-file').subscribe((message: Message) => {
const notification = JSON.parse(message.body) as BookdropFileNotification;
this.bookdropFileService.handleIncomingFile(notification);
@@ -1,23 +1,24 @@
<div class="staging-border p-4 mt-6 rounded">
<div class="staging-border p-4 rounded">
<div class="flex justify-between items-center">
<div class="flex flex-col justify-center">
<p class="text-sm font-medium text-gray-200">Pending Bookdrop Files</p>
<p class="text font-bold text-zinc-300">Pending Bookdrop Files</p>
<p class="text-lg font-bold text-primary">{{ pendingCount }}</p>
@if (lastUpdatedAt) {
<p class="text-xs text-gray-400 mt-1">
<p class="text-xs text-zinc-400 mt-1">
Last updated: {{ lastUpdatedAt | date: 'short' }}
</p>
}
</div>
<div class="flex items-center">
<button
type="button"
class="p-button p-button-sm p-button-outlined p-button-info"
<p-button
size="small"
outlined
severity="info"
(click)="openReviewDialog()">
Review
</button>
</p-button>
</div>
</div>
</div>
@@ -4,6 +4,7 @@ import {takeUntil} from 'rxjs/operators';
import {BookdropFileNotification, BookdropFileService} from '../bookdrop-file.service';
import {DatePipe} from '@angular/common';
import {Router} from '@angular/router';
import {Button} from 'primeng/button';
@Component({
selector: 'app-bookdrop-files-widget-component',
@@ -11,7 +12,8 @@ import {Router} from '@angular/router';
templateUrl: './bookdrop-files-widget.component.html',
styleUrl: './bookdrop-files-widget.component.scss',
imports: [
DatePipe
DatePipe,
Button
]
})
export class BookdropFilesWidgetComponent implements OnInit, OnDestroy {
@@ -1,4 +1,4 @@
<div class="flex flex-col px-4 py-6 live-border">
<p class="text-sm text-gray-400">{{ latestNotification.timestamp }}</p>
<p>{{ latestNotification.message }}</p>
<div class="flex flex-col p-4 space-y-2 live-border">
<p class="text-xs text-zinc-400">{{ latestNotification.timestamp }}</p>
<p class="font-normal text-zinc-200">{{ latestNotification.message }}</p>
</div>
@@ -1,6 +1,6 @@
import {Component, inject} from '@angular/core';
import {LogNotification} from '../../../shared/websocket/model/log-notification.model';
import {NotificationEventService} from '../../../shared/websocket/notification-event.service';
import {LogNotification} from '../../../shared/websocket/model/log-notification.model';
@Component({
selector: 'app-live-notification-box',
@@ -0,0 +1,43 @@
<div class="flex flex-col p-4 live-border">
@if (tasks$ | async; as tasks) {
@if (tasks.length === 0) {
<div class="text-center text-zinc-400">
<p class="text-sm">No active tasks</p>
</div>
} @else {
@for (task of tasks; track task.taskId; let i = $index) {
@if (i > 0) {
<div class="border-t border-zinc-700 my-4"></div>
}
<div class="flex flex-col space-y-1.5">
<p class="text-xs text-zinc-400">{{ task.timestamp }}</p>
<div class="flex flex-col space-y-0.5">
<div class="flex justify-between items-center">
<div class="flex items-center space-x-1.5">
<span class="px-1.5 py-0.5 text-xs rounded-xl font-bold" [class]="getStatusClasses(task.status)">
{{ getStatusText(task.status) }}
</span>
@if (task.title) {
<h4 class="font-bold text-zinc-200">Task: {{ task.title }}</h4>
}
</div>
<div class="flex items-center space-x-1">
@if (task.status === TaskStatus.IN_PROGRESS && task.cancellable) {
<p-button
(onClick)="cancelTask(task.taskId)" icon="pi pi-ban" severity="danger" size="small" rounded text>
</p-button>
}
<p-button
(onClick)="removeTask(task.taskId)" icon="pi pi-times" severity="secondary" size="small" rounded text>
</p-button>
</div>
</div>
<p class="font-normal text-zinc-300">{{ task.message }}</p>
</div>
</div>
}
}
}
</div>
@@ -0,0 +1,6 @@
.live-border {
background: var(--card-background);
border: 1px solid var(--primary-color);
border-radius: 0.5rem;
}
@@ -0,0 +1,84 @@
import {Component, inject} from '@angular/core';
import {CommonModule} from '@angular/common';
import {TaskMessage, TaskStatus} from '../../../shared/websocket/model/log-notification.model';
import {TaskEventService} from '../../../shared/websocket/task-event.service';
import {TaskService} from '../../../shared/services/task.service';
import {Observable, take} from 'rxjs';
import {Button} from 'primeng/button';
import {MessageService} from 'primeng/api';
@Component({
selector: 'app-live-task-event-box',
standalone: true,
imports: [CommonModule, Button],
templateUrl: './live-task-event-box.component.html',
styleUrls: ['./live-task-event-box.component.scss'],
host: {
class: 'config-panel'
}
})
export class LiveTaskEventBoxComponent {
tasks$: Observable<TaskMessage[]>;
TaskStatus = TaskStatus;
private taskEventService = inject(TaskEventService);
private taskService = inject(TaskService);
private messageService = inject(MessageService);
constructor() {
this.tasks$ = this.taskEventService.tasks$;
}
cancelTask(taskId: string): void {
this.taskService.cancelTask(taskId).pipe(take(1)).subscribe({
next: (response) => {
this.messageService.add({
severity: 'success',
summary: 'Success',
detail: `Task cancellation scheduled`
});
},
error: (error) => {
this.messageService.add({
severity: 'error',
summary: 'Cancellation failed',
detail: error.error?.error || 'Failed to cancel task'
});
}
});
}
removeTask(taskId: string): void {
this.taskEventService.removeTask(taskId);
}
getStatusClasses(status: TaskStatus): string {
switch (status) {
case TaskStatus.IN_PROGRESS:
return 'bg-blue-700 text-zinc-100';
case TaskStatus.COMPLETED:
return 'bg-green-700 text-zinc-100';
case TaskStatus.FAILED:
return 'bg-red-700 text-zinc-100';
case TaskStatus.CANCELLED:
return 'bg-gray-700 text-zinc-100';
default:
return 'bg-zinc-700 text-zinc-100';
}
}
getStatusText(status: TaskStatus): string {
switch (status) {
case TaskStatus.IN_PROGRESS:
return 'Running';
case TaskStatus.COMPLETED:
return 'Completed';
case TaskStatus.FAILED:
return 'Failed';
case TaskStatus.CANCELLED:
return 'Cancelled';
default:
return 'Unknown';
}
}
}
@@ -1,5 +1,8 @@
<div class="metadata-progress-box flex flex-col w-[25rem] max-h-[60vh] overflow-y-auto">
<div class="metadata-progress-box flex gap-4 flex-col w-[25rem] max-h-[60vh] overflow-y-auto">
<app-live-notification-box/>
@if (hasActiveTasks$ | async) {
<app-live-task-event-box/>
}
@if (hasMetadataTasks$ | async) {
<app-metadata-progress-widget/>
}
@@ -1,7 +1,9 @@
import {Component, inject} from '@angular/core';
import {LiveNotificationBoxComponent} from '../live-notification-box/live-notification-box.component';
import {LiveTaskEventBoxComponent} from '../live-task-event-box/live-task-event-box.component';
import {MetadataProgressWidgetComponent} from '../metadata-progress-widget-component/metadata-progress-widget-component';
import {MetadataProgressService} from '../../service/metadata-progress-service';
import {TaskEventService} from '../../../shared/websocket/task-event.service';
import {map} from 'rxjs/operators';
import {AsyncPipe} from '@angular/common';
import {BookdropFilesWidgetComponent} from '../../../bookdrop/bookdrop-files-widget-component/bookdrop-files-widget.component';
@@ -11,6 +13,7 @@ import {BookdropFileService} from '../../../bookdrop/bookdrop-file.service';
selector: 'app-unified-notification-popover-component',
imports: [
LiveNotificationBoxComponent,
LiveTaskEventBoxComponent,
MetadataProgressWidgetComponent,
AsyncPipe,
BookdropFilesWidgetComponent
@@ -22,10 +25,15 @@ import {BookdropFileService} from '../../../bookdrop/bookdrop-file.service';
export class UnifiedNotificationBoxComponent {
metadataProgressService = inject(MetadataProgressService);
bookdropFileService = inject(BookdropFileService);
taskEventService = inject(TaskEventService);
hasMetadataTasks$ = this.metadataProgressService.activeTasks$.pipe(
map(tasks => Object.keys(tasks).length > 0)
);
hasPendingBookdropFiles$ = this.bookdropFileService.hasPendingFiles$;
hasActiveTasks$ = this.taskEventService.tasks$.pipe(
map(tasks => tasks.length > 0)
);
}
@@ -23,6 +23,7 @@ import {MetadataBatchProgressNotification} from '../../../core/model/metadata-ba
import {UnifiedNotificationBoxComponent} from '../../../core/component/unified-notification-popover-component/unified-notification-popover-component';
import {BookdropFileService} from '../../../bookdrop/bookdrop-file.service';
import {DialogLauncherService} from '../../../dialog-launcher.service';
import {TaskEventService} from '../../../shared/websocket/task-event.service';
@Component({
selector: 'app-topbar',
@@ -77,10 +78,12 @@ export class AppTopBarComponent implements OnDestroy {
protected userService: UserService,
private metadataProgressService: MetadataProgressService,
private bookdropFileService: BookdropFileService,
private dialogLauncher: DialogLauncherService
private dialogLauncher: DialogLauncherService,
private taskEventService: TaskEventService
) {
this.subscribeToMetadataProgress();
this.subscribeToNotifications();
this.subscribeToTaskEvents();
this.metadataProgressService.activeTasks$
.pipe(takeUntil(this.destroy$))
@@ -153,6 +156,16 @@ export class AppTopBarComponent implements OnDestroy {
});
}
private subscribeToTaskEvents() {
this.taskEventService.tasks$
.pipe(takeUntil(this.destroy$))
.subscribe((tasks) => {
if (tasks.length > 0) {
this.triggerPulseEffect();
}
});
}
private triggerPulseEffect() {
this.showPulse = true;
clearTimeout(this.eventTimer);
@@ -0,0 +1,17 @@
import {Injectable, inject} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {API_CONFIG} from '../../config/api-config';
@Injectable({
providedIn: 'root'
})
export class TaskService {
private http = inject(HttpClient);
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/tasks`;
cancelTask(taskId: string): Observable<{ message: string }> {
return this.http.delete<{ message: string }>(`${this.url}/${taskId}`);
}
}
@@ -1,3 +1,10 @@
export enum TaskStatus {
IN_PROGRESS = 'IN_PROGRESS',
CANCELLED = 'CANCELLED',
COMPLETED = 'COMPLETED',
FAILED = 'FAILED'
}
export interface LogNotification {
timestamp?: string;
message: string;
@@ -5,10 +12,25 @@ export interface LogNotification {
export function parseLogNotification(messageBody: string): LogNotification {
const raw = JSON.parse(messageBody);
const localTime = new Date(raw.timestamp).toLocaleTimeString();
return {
timestamp: localTime,
timestamp: raw.timestamp ? new Date(raw.timestamp).toLocaleTimeString() : undefined,
message: raw.message,
};
}
export interface TaskMessage {
taskId: string;
timestamp: string;
title?: string;
message: string;
cancellable: boolean;
status: TaskStatus;
}
export function parseTaskMessage(messageBody: string): TaskMessage {
const raw = JSON.parse(messageBody) as TaskMessage;
return {
...raw,
timestamp: new Date(raw.timestamp).toLocaleTimeString()
};
}
@@ -0,0 +1,42 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { TaskMessage } from './model/log-notification.model';
@Injectable({
providedIn: 'root'
})
export class TaskEventService {
private tasksSubject = new BehaviorSubject<Map<string, TaskMessage>>(new Map());
tasks$: Observable<TaskMessage[]> = this.tasksSubject.asObservable().pipe(
map(taskMap => Array.from(taskMap.values()))
);
handleTaskMessage(taskMessage: TaskMessage): void {
const currentTasks = new Map(this.tasksSubject.value);
currentTasks.set(taskMessage.taskId, taskMessage);
this.tasksSubject.next(currentTasks);
}
removeTask(taskId: string): void {
const currentTasks = new Map(this.tasksSubject.value);
currentTasks.delete(taskId);
this.tasksSubject.next(currentTasks);
}
getTask(taskId: string): TaskMessage | undefined {
return this.tasksSubject.value.get(taskId);
}
getTaskById$(taskId: string): Observable<TaskMessage | undefined> {
return this.tasksSubject.asObservable().pipe(
map(taskMap => taskMap.get(taskId))
);
}
clearAllTasks(): void {
this.tasksSubject.next(new Map());
}
}