Update frontend consumer status phases

This commit is contained in:
shamoon 2023-07-25 10:21:34 -07:00 committed by Trenton H
parent d376f9e7a3
commit 0beb9f0b5f
4 changed files with 54 additions and 19 deletions

View File

@ -84,7 +84,7 @@ describe('UploadFileWidgetComponent', () => {
it('should change color by status phase', () => { it('should change color by status phase', () => {
const processingStatus = new FileStatus() const processingStatus = new FileStatus()
processingStatus.phase = FileStatusPhase.PROCESSING processingStatus.phase = FileStatusPhase.WORKING
expect(component.getStatusColor(processingStatus)).toEqual('primary') expect(component.getStatusColor(processingStatus)).toEqual('primary')
const failedStatus = new FileStatus() const failedStatus = new FileStatus()
failedStatus.phase = FileStatusPhase.FAILED failedStatus.phase = FileStatusPhase.FAILED
@ -134,7 +134,7 @@ function mockConsumerStatuses(consumerStatusService) {
switch (phase) { switch (phase) {
case FileStatusPhase.FAILED: case FileStatusPhase.FAILED:
return [new FileStatus()] return [new FileStatus()]
case FileStatusPhase.PROCESSING: case FileStatusPhase.WORKING:
return [new FileStatus(), new FileStatus()] return [new FileStatus(), new FileStatus()]
case FileStatusPhase.STARTED: case FileStatusPhase.STARTED:
return [new FileStatus(), new FileStatus(), new FileStatus()] return [new FileStatus(), new FileStatus(), new FileStatus()]

View File

@ -90,8 +90,9 @@ export class UploadFileWidgetComponent extends ComponentWithPermissions {
getStatusColor(status: FileStatus) { getStatusColor(status: FileStatus) {
switch (status.phase) { switch (status.phase) {
case FileStatusPhase.PROCESSING:
case FileStatusPhase.UPLOADING: case FileStatusPhase.UPLOADING:
case FileStatusPhase.STARTED:
case FileStatusPhase.WORKING:
return 'primary' return 'primary'
case FileStatusPhase.FAILED: case FileStatusPhase.FAILED:
return 'danger' return 'danger'

View File

@ -60,10 +60,10 @@ describe('ConsumerStatusService', () => {
current_progress: 50, current_progress: 50,
max_progress: 100, max_progress: 100,
document_id: 12, document_id: 12,
status: 'STARTING', status: 'WORKING',
}) })
expect(status.getProgress()).toBeCloseTo(0.6) // 0.8 * 50/100 expect(status.getProgress()).toBeCloseTo(0.6) // (0.8 * 50/100) + .2
expect(consumerStatusService.getConsumerStatusNotCompleted()).toEqual([ expect(consumerStatusService.getConsumerStatusNotCompleted()).toEqual([
status, status,
]) ])
@ -194,6 +194,7 @@ describe('ConsumerStatusService', () => {
expect(consumerStatusService.getConsumerStatusCompleted()).toHaveLength(1) expect(consumerStatusService.getConsumerStatusCompleted()).toHaveLength(1)
consumerStatusService.dismissCompleted() consumerStatusService.dismissCompleted()
expect(consumerStatusService.getConsumerStatusCompleted()).toHaveLength(0) expect(consumerStatusService.getConsumerStatusCompleted()).toHaveLength(0)
consumerStatusService.disconnect()
}) })
it('should support dismiss', () => { it('should support dismiss', () => {
@ -238,17 +239,40 @@ describe('ConsumerStatusService', () => {
}) })
it('should notify of document created on status message without upload', () => { it('should notify of document created on status message without upload', () => {
let detected = false
consumerStatusService.onDocumentDetected().subscribe((filestatus) => { consumerStatusService.onDocumentDetected().subscribe((filestatus) => {
expect(filestatus.phase).toEqual(FileStatusPhase.STARTED) expect(filestatus.phase).toEqual(FileStatusPhase.STARTED)
detected = true
}) })
consumerStatusService.connect()
server.send({
task_id: '1234',
filename: 'file.pdf',
current_progress: 0,
max_progress: 100,
message: 'new_file',
status: 'STARTED',
})
consumerStatusService.disconnect()
expect(detected).toBeTruthy()
})
it('should notify of document in progress without upload', () => {
consumerStatusService.connect()
server.send({ server.send({
task_id: '1234', task_id: '1234',
filename: 'file.pdf', filename: 'file.pdf',
current_progress: 50, current_progress: 50,
max_progress: 100, max_progress: 100,
document_id: 12, docuement_id: 12,
status: 'STARTING', status: 'WORKING',
}) })
consumerStatusService.disconnect()
expect(consumerStatusService.getConsumerStatusNotCompleted()).toHaveLength(
1
)
}) })
}) })

View File

@ -3,10 +3,11 @@ import { Subject } from 'rxjs'
import { environment } from 'src/environments/environment' import { environment } from 'src/environments/environment'
import { WebsocketConsumerStatusMessage } from '../data/websocket-consumer-status-message' import { WebsocketConsumerStatusMessage } from '../data/websocket-consumer-status-message'
// see ConsumerFilePhase in src/documents/consumer.py
export enum FileStatusPhase { export enum FileStatusPhase {
STARTED = 0, STARTED = 0,
UPLOADING = 1, UPLOADING = 1,
PROCESSING = 2, WORKING = 2,
SUCCESS = 3, SUCCESS = 3,
FAILED = 4, FAILED = 4,
} }
@ -49,7 +50,7 @@ export class FileStatus {
return 0.0 return 0.0
case FileStatusPhase.UPLOADING: case FileStatusPhase.UPLOADING:
return (this.currentPhaseProgress / this.currentPhaseMaxProgress) * 0.2 return (this.currentPhaseProgress / this.currentPhaseMaxProgress) * 0.2
case FileStatusPhase.PROCESSING: case FileStatusPhase.WORKING:
return ( return (
(this.currentPhaseProgress / this.currentPhaseMaxProgress) * 0.8 + 0.2 (this.currentPhaseProgress / this.currentPhaseMaxProgress) * 0.8 + 0.2
) )
@ -150,7 +151,7 @@ export class ConsumerStatusService {
let created = statusMessageGet.created let created = statusMessageGet.created
status.updateProgress( status.updateProgress(
FileStatusPhase.PROCESSING, FileStatusPhase.WORKING,
statusMessage.current_progress, statusMessage.current_progress,
statusMessage.max_progress statusMessage.max_progress
) )
@ -164,16 +165,25 @@ export class ConsumerStatusService {
} }
status.documentId = statusMessage.document_id status.documentId = statusMessage.document_id
if (created && statusMessage.status == 'STARTED') { if (statusMessage.status in FileStatusPhase) {
this.documentDetectedSubject.next(status) status.phase = FileStatusPhase[statusMessage.status]
} }
if (statusMessage.status == 'SUCCESS') {
status.phase = FileStatusPhase.SUCCESS switch (status.phase) {
case FileStatusPhase.STARTED:
if (created) this.documentDetectedSubject.next(status)
break
case FileStatusPhase.SUCCESS:
this.documentConsumptionFinishedSubject.next(status) this.documentConsumptionFinishedSubject.next(status)
} break
if (statusMessage.status == 'FAILED') {
status.phase = FileStatusPhase.FAILED case FileStatusPhase.FAILED:
this.documentConsumptionFailedSubject.next(status) this.documentConsumptionFailedSubject.next(status)
break
default:
break
} }
} }
} }