From 5b25967a25096c754b30fb83c4fda3c21f842336 Mon Sep 17 00:00:00 2001 From: Timo Date: Mon, 7 Dec 2020 18:56:44 +0200 Subject: [PATCH] smart auto reanalyse --- .../screens/file/model/file-status.wrapper.ts | 6 +++++ .../red-ui/src/app/state/app-state.service.ts | 27 ++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/apps/red-ui/src/app/screens/file/model/file-status.wrapper.ts b/apps/red-ui/src/app/screens/file/model/file-status.wrapper.ts index 474633623..6ced083a9 100644 --- a/apps/red-ui/src/app/screens/file/model/file-status.wrapper.ts +++ b/apps/red-ui/src/app/screens/file/model/file-status.wrapper.ts @@ -130,4 +130,10 @@ export class FileStatusWrapper { get canApprove() { return this.status === 'UNDER_REVIEW' || this.status === 'UNDER_APPROVAL'; } + + get newestDate() { + const uploadedDate = new Date(this.fileStatus.lastUploaded); + const updatedDate = new Date(this.fileStatus.lastUpdated); + return updatedDate && updatedDate.getTime() > uploadedDate.getTime() ? updatedDate : uploadedDate; + } } diff --git a/apps/red-ui/src/app/state/app-state.service.ts b/apps/red-ui/src/app/state/app-state.service.ts index 87c2a4018..f4c355113 100644 --- a/apps/red-ui/src/app/state/app-state.service.ts +++ b/apps/red-ui/src/app/state/app-state.service.ts @@ -184,13 +184,38 @@ export class AppStateService { } private _automaticallyReanalyseErrorFiles(project: ProjectWrapper) { + const now = new Date().getTime(); project.files.forEach((file) => { if (file.status === 'ERROR') { - this._reanalysisControllerService.reanalyzeFile(project.projectId, file.fileId).subscribe(() => {}); + console.log('Auto-Reanalyse file because of error'); + this._reanalyseFileInCurrentSessionIfNeeded(project, file); + } + if (file.status === 'PROCESSING') { + const delta = now - file.newestDate.getTime(); + if (delta > 1000 * 60 * 5) { + console.log('Auto-Reanalyse file because of timeout'); + this._reanalyseFileInCurrentSessionIfNeeded(project, file); + } } }); } + private _reanalyseFileInCurrentSessionIfNeeded(project: ProjectWrapper, file: FileStatusWrapper) { + const result = sessionStorage.getItem('FILE_' + file.fileId); + if (!result) { + this._reanalysisControllerService.reanalyzeFile(project.projectId, file.fileId).subscribe(() => {}); + sessionStorage.setItem('FILE_' + file.fileId, `${new Date().getTime()}`); + } else { + const now = new Date().getTime(); + const lastAttempt = new Date(result).getTime(); + const delta = now - lastAttempt; + if (delta > 1000 * 60 * 5) { + this._reanalysisControllerService.reanalyzeFile(project.projectId, file.fileId).subscribe(() => {}); + sessionStorage.setItem('FILE_' + file.fileId, `${new Date().getTime()}`); + } + } + } + async getFiles(project?: ProjectWrapper) { if (!project) { project = this.activeProject;