Implement live progress tracking for bulk metadata refresh (remaining books, status, cancel support)
This commit is contained in:
committed by
Aditya Chandel
parent
2c078716bf
commit
b1508493cf
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user