Refactored to use process log bitstream

This commit is contained in:
Marie Verdonck
2020-11-12 17:35:10 +01:00
parent e4a1d717a2
commit df5570c7b0
9 changed files with 140 additions and 166 deletions

View File

@@ -7,7 +7,6 @@ import { EffectsModule } from '@ngrx/effects';
import { Action, StoreConfig, StoreModule } from '@ngrx/store';
import { MyDSpaceGuard } from '../+my-dspace-page/my-dspace.guard';
import { ProcessOutput } from '../process-page/processes/process-output.model';
import { isNotEmpty } from '../shared/empty.util';
import { FormBuilderService } from '../shared/form/builder/form-builder.service';
@@ -72,7 +71,6 @@ import { LookupRelationService } from './data/lookup-relation.service';
import { MappedCollectionsReponseParsingService } from './data/mapped-collections-reponse-parsing.service';
import { MyDSpaceResponseParsingService } from './data/mydspace-response-parsing.service';
import { ObjectUpdatesService } from './data/object-updates/object-updates.service';
import { ProcessOutputDataService } from './data/process-output-data.service';
import { RelationshipTypeService } from './data/relationship-type.service';
import { RelationshipService } from './data/relationship.service';
import { ResourcePolicyService } from './resource-policy/resource-policy.service';
@@ -291,7 +289,6 @@ const PROVIDERS = [
ItemTypeDataService,
WorkflowActionDataService,
ProcessDataService,
ProcessOutputDataService,
ScriptDataService,
ProcessFilesResponseParsingService,
FeatureDataService,
@@ -365,7 +362,6 @@ export const models =
ExternalSourceEntry,
Script,
Process,
ProcessOutput,
Version,
VersionHistory,
WorkflowAction,

View File

@@ -1,72 +0,0 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import { ProcessOutput } from '../../process-page/processes/process-output.model';
import { NotificationsService } from '../../shared/notifications/notifications.service';
import { FollowLinkConfig } from '../../shared/utils/follow-link-config.model';
import { dataService } from '../cache/builders/build-decorators';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { ObjectCacheService } from '../cache/object-cache.service';
import { CoreState } from '../core.reducers';
import { HALEndpointService } from '../shared/hal-endpoint.service';
import { PROCESS_OUTPUT_TYPE } from '../shared/process-output.resource-type';
import { DataService } from './data.service';
import { DefaultChangeAnalyzer } from './default-change-analyzer.service';
import { RemoteData } from './remote-data';
import { RequestService } from './request.service';
/* tslint:disable:max-classes-per-file */
/**
* A private DataService implementation to delegate specific methods to.
*/
class DataServiceImpl extends DataService<ProcessOutput> {
protected linkPath = 'processes';
constructor(
protected requestService: RequestService,
protected rdbService: RemoteDataBuildService,
protected store: Store<CoreState>,
protected objectCache: ObjectCacheService,
protected halService: HALEndpointService,
protected notificationsService: NotificationsService,
protected http: HttpClient,
protected comparator: DefaultChangeAnalyzer<ProcessOutput>) {
super();
}
}
/**
* A service to retrieve output from processes from the REST API.
*/
@Injectable()
@dataService(PROCESS_OUTPUT_TYPE)
export class ProcessOutputDataService {
/**
* A private DataService instance to delegate specific methods to.
*/
private dataService: DataServiceImpl;
constructor(
protected requestService: RequestService,
protected rdbService: RemoteDataBuildService,
protected store: Store<CoreState>,
protected objectCache: ObjectCacheService,
protected halService: HALEndpointService,
protected notificationsService: NotificationsService,
protected http: HttpClient,
protected comparator: DefaultChangeAnalyzer<ProcessOutput>) {
this.dataService = new DataServiceImpl(requestService, rdbService, null, objectCache, halService, notificationsService, http, comparator);
}
/**
* Returns an observable of {@link RemoteData} of a {@link ProcessOutput}, based on an href, with a list of {@link FollowLinkConfig},
* to automatically resolve {@link HALLink}s of the {@link ProcessOutput}
* @param href The url of {@link ProcessOutput} we want to retrieve
* @param linksToFollow List of {@link FollowLinkConfig} that indicate which {@link HALLink}s should be automatically resolved
*/
findByHref(href: string, ...linksToFollow: Array<FollowLinkConfig<ProcessOutput>>): Observable<RemoteData<ProcessOutput>> {
return this.dataService.findByHref(href, ...linksToFollow);
}
}
/* tslint:enable:max-classes-per-file */

View File

@@ -17,7 +17,7 @@
<ds-process-detail-field *ngIf="files && files?.length > 0" id="process-files" [title]="'process.detail.output-files'">
<ds-file-download-link *ngFor="let file of files; let last=last;" [href]="file?._links?.content?.href" [download]="getFileName(file)">
<span>{{getFileName(file)}}</span>
<span>({{(file?.sizeBytes) | dsFileSize }})</span>
<span>({{(file?.sizeBytes) | dsFileSize }})</span>
</ds-file-download-link>
</ds-process-detail-field>
</div>
@@ -34,14 +34,15 @@
<div>{{ process.processStatus }}</div>
</ds-process-detail-field>
<ds-process-detail-field id="process-output" [title]="'process.detail.output'">
<button *ngIf="!showOutputLogs" id="showOutputButton" class="btn btn-light" (click)="showProcessOutputLogs()">
<ds-process-detail-field *ngIf="isProcessFinished(process)" id="process-output" [title]="'process.detail.output'">
<button *ngIf="!showOutputLogs && process?._links?.output?.href != undefined" id="showOutputButton" class="btn btn-light" (click)="showProcessOutputLogs()">
{{ 'process.detail.logs.button' | translate }}
</button>
<ds-loading *ngIf="retrievingOutputLogs$ | async" class="ds-loading" message="{{ 'process.detail.logs.loading' | translate }}"></ds-loading>
<pre class="font-weight-bold text-secondary bg-light p-3"
*ngIf="showOutputLogs && (outputLogs$ | async)?.length > 0">{{ (outputLogs$ | async)?.join('\n') }}</pre>
<p id="no-output-logs-message" *ngIf="showOutputLogs && !(outputLogs$ | async) || (outputLogs$ | async)?.length == 0">
*ngIf="showOutputLogs && (outputLogs$ | async)?.length > 0">{{ (outputLogs$ | async) }}</pre>
<p id="no-output-logs-message" *ngIf="(!(retrievingOutputLogs$ | async) && showOutputLogs)
&& !(outputLogs$ | async) || (outputLogs$ | async)?.length == 0 || !process._links.output">
{{ 'process.detail.logs.none' | translate }}
</p>
</ds-process-detail-field>

View File

@@ -1,11 +1,22 @@
import { ProcessOutputDataService } from '../../core/data/process-output-data.service';
import { ProcessOutput } from '../processes/process-output.model';
import { HttpClient } from '@angular/common/http';
import { AuthService } from '../../core/auth/auth.service';
import { BitstreamDataService } from '../../core/data/bitstream-data.service';
import { AuthServiceMock } from '../../shared/mocks/auth.service.mock';
import { ProcessDetailComponent } from './process-detail.component';
import { async, ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import {
async,
ComponentFixture,
discardPeriodicTasks,
fakeAsync,
flush,
flushMicrotasks,
TestBed,
tick
} from '@angular/core/testing';
import { VarDirective } from '../../shared/utils/var.directive';
import { TranslateModule } from '@ngx-translate/core';
import { RouterTestingModule } from '@angular/router/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { ProcessDetailFieldComponent } from './process-detail-field/process-detail-field.component';
import { Process } from '../processes/process.model';
import { ActivatedRoute } from '@angular/router';
@@ -23,8 +34,9 @@ describe('ProcessDetailComponent', () => {
let fixture: ComponentFixture<ProcessDetailComponent>;
let processService: ProcessDataService;
let processOutputService: ProcessOutputDataService;
let nameService: DSONameService;
let bitstreamDataService: BitstreamDataService;
let httpClient: HttpClient;
let process: Process;
let fileName: string;
@@ -33,13 +45,11 @@ describe('ProcessDetailComponent', () => {
let processOutput;
function init() {
processOutput = Object.assign(new ProcessOutput(), {
logs: ['Process started', 'Process completed']
}
);
processOutput = 'Process Started'
process = Object.assign(new Process(), {
processId: 1,
scriptName: 'script-name',
processStatus: 'COMPLETED',
parameters: [
{
name: '-f',
@@ -76,15 +86,24 @@ describe('ProcessDetailComponent', () => {
}
})
];
const logBitstream = Object.assign(new Bitstream(), {
id: 'output.log',
_links: {
content: { href: 'log-selflink' }
}
});
processService = jasmine.createSpyObj('processService', {
getFiles: createSuccessfulRemoteDataObject$(createPaginatedList(files))
});
processOutputService = jasmine.createSpyObj('processOutputService', {
findByHref: createSuccessfulRemoteDataObject$(processOutput)
bitstreamDataService = jasmine.createSpyObj('bitstreamDataService', {
findByHref: createSuccessfulRemoteDataObject$(logBitstream)
});
nameService = jasmine.createSpyObj('nameService', {
getName: fileName
});
httpClient = jasmine.createSpyObj('httpClient', {
get: observableOf(processOutput)
});
}
beforeEach(async(() => {
@@ -98,10 +117,12 @@ describe('ProcessDetailComponent', () => {
useValue: { data: observableOf({ process: createSuccessfulRemoteDataObject(process) }) }
},
{ provide: ProcessDataService, useValue: processService },
{ provide: ProcessOutputDataService, useValue: processOutputService },
{ provide: DSONameService, useValue: nameService }
{ provide: BitstreamDataService, useValue: bitstreamDataService },
{ provide: DSONameService, useValue: nameService },
{ provide: AuthService, useValue: new AuthServiceMock() },
{ provide: HttpClient, useValue: httpClient },
],
schemas: [NO_ERRORS_SCHEMA]
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
}));
@@ -109,6 +130,14 @@ describe('ProcessDetailComponent', () => {
fixture = TestBed.createComponent(ProcessDetailComponent);
component = fixture.componentInstance;
});
afterEach(fakeAsync(() => {
TestBed.resetTestingModule();
fixture.destroy();
flush();
flushMicrotasks();
discardPeriodicTasks();
component = null;
}));
it('should display the script\'s name', () => {
fixture.detectChanges();
@@ -134,6 +163,7 @@ describe('ProcessDetailComponent', () => {
beforeEach(fakeAsync(() => {
spyOn(component, 'showProcessOutputLogs').and.callThrough();
fixture.detectChanges();
const showOutputButton = fixture.debugElement.query(By.css('#showOutputButton'));
showOutputButton.triggerEventHandler('click', {
preventDefault: () => {/**/
@@ -147,20 +177,16 @@ describe('ProcessDetailComponent', () => {
it('should display the process\'s output logs', () => {
fixture.detectChanges();
const outputProcess = fixture.debugElement.query(By.css('#process-output pre'));
expect(outputProcess.nativeElement.textContent).toContain('Process started');
expect(outputProcess.nativeElement.textContent).toContain(processOutput);
});
});
describe('if press show output logs and process has no output logs (yet)', () => {
describe('if press show output logs and process has no output logs', () => {
beforeEach(fakeAsync(() => {
jasmine.getEnv().allowRespy(true);
const emptyProcessOutput = Object.assign(new ProcessOutput(), {
logs: []
});
spyOn(processOutputService, 'findByHref').and.returnValue(createSuccessfulRemoteDataObject$(emptyProcessOutput));
spyOn(httpClient, 'get').and.returnValue(observableOf(null));
fixture = TestBed.createComponent(ProcessDetailComponent);
component = fixture.componentInstance;
fixture.detectChanges();
spyOn(component, 'showProcessOutputLogs').and.callThrough();
fixture.detectChanges();
const showOutputButton = fixture.debugElement.query(By.css('#showOutputButton'));

View File

@@ -1,19 +1,23 @@
import { HttpClient } from '@angular/common/http';
import { Component, NgZone, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';
import { Observable } from 'rxjs/internal/Observable';
import { ProcessOutputDataService } from '../../core/data/process-output-data.service';
import { RemoteData } from '../../core/data/remote-data';
import { DSpaceObject } from '../../core/shared/dspace-object.model';
import { ProcessOutput } from '../processes/process-output.model';
import { Process } from '../processes/process.model';
import { finalize, map, switchMap, take } from 'rxjs/operators';
import { getFirstSucceededRemoteDataPayload, redirectOn404Or401 } from '../../core/shared/operators';
import { AlertType } from '../../shared/alert/aletr-type';
import { ProcessDataService } from '../../core/data/processes/process-data.service';
import { PaginatedList } from '../../core/data/paginated-list';
import { Bitstream } from '../../core/shared/bitstream.model';
import { finalize, map, mergeMap, switchMap, take, tap } from 'rxjs/operators';
import { AuthService } from '../../core/auth/auth.service';
import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
import { BitstreamDataService } from '../../core/data/bitstream-data.service';
import { PaginatedList } from '../../core/data/paginated-list';
import { ProcessDataService } from '../../core/data/processes/process-data.service';
import { RemoteData } from '../../core/data/remote-data';
import { Bitstream } from '../../core/shared/bitstream.model';
import { DSpaceObject } from '../../core/shared/dspace-object.model';
import { getFirstSucceededRemoteDataPayload, redirectOn404Or401 } from '../../core/shared/operators';
import { URLCombiner } from '../../core/url-combiner/url-combiner';
import { AlertType } from '../../shared/alert/aletr-type';
import { hasValue } from '../../shared/empty.util';
import { ProcessStatus } from '../processes/process-status.model';
import { Process } from '../processes/process.model';
@Component({
selector: 'ds-process-detail',
@@ -40,26 +44,33 @@ export class ProcessDetailComponent implements OnInit {
*/
filesRD$: Observable<RemoteData<PaginatedList<Bitstream>>>;
/**
* File link that contain the output logs with auth token
*/
outputLogFileUrl$: Observable<string>;
/**
* The Process's Output logs
*/
outputLogs$: Observable<string[]>;
outputLogs$: Observable<string>;
/**
* Boolean on whether or not to show the output logs
*/
showOutputLogs = false;
showOutputLogs;
/**
* When it's retrieving the output logs from backend, to show loading component
*/
retrievingOutputLogs$ = new BehaviorSubject<boolean>(false);
retrievingOutputLogs$: BehaviorSubject<boolean>;
constructor(protected route: ActivatedRoute,
protected router: Router,
protected processService: ProcessDataService,
protected processOutputService: ProcessOutputDataService,
protected bitstreamDataService: BitstreamDataService,
protected nameService: DSONameService,
private zone: NgZone) {
private zone: NgZone,
protected authService: AuthService,
protected http: HttpClient) {
}
/**
@@ -67,8 +78,12 @@ export class ProcessDetailComponent implements OnInit {
* Display a 404 if the process doesn't exist
*/
ngOnInit(): void {
this.showOutputLogs = false;
this.retrievingOutputLogs$ = new BehaviorSubject<boolean>(false);
this.processRD$ = this.route.data.pipe(
map((data) => data.process as RemoteData<Process>),
map((data) => {
return data.process as RemoteData<Process>
}),
redirectOn404Or401(this.router)
);
@@ -93,20 +108,58 @@ export class ProcessDetailComponent implements OnInit {
showProcessOutputLogs() {
this.retrievingOutputLogs$.next(true);
this.zone.runOutsideAngular(() => {
const processOutputRD$: Observable<RemoteData<ProcessOutput>> = this.processRD$.pipe(
const processOutputRD$: Observable<RemoteData<Bitstream>> = this.processRD$.pipe(
getFirstSucceededRemoteDataPayload(),
switchMap((process: Process) => this.processOutputService.findByHref(process._links.output.href))
switchMap((process: Process) => {
return this.bitstreamDataService.findByHref(process._links.output.href);
})
);
this.outputLogs$ = processOutputRD$.pipe(
getFirstSucceededRemoteDataPayload(),
map((processOutput: ProcessOutput) => {
this.outputLogFileUrl$ = processOutputRD$.pipe(
tap((processOutputFileRD: RemoteData<Bitstream>) => {
if (processOutputFileRD.statusCode === 204) {
this.zone.run(() => this.retrievingOutputLogs$.next(false));
this.showOutputLogs = true;
return processOutput.logs;
}
}),
finalize(() => this.zone.run(() => this.retrievingOutputLogs$.next(false))),
getFirstSucceededRemoteDataPayload(),
mergeMap((processOutput: Bitstream) => {
const url = processOutput._links.content.href;
return this.authService.getShortlivedToken().pipe(take(1),
map((token: string) => {
return hasValue(token) ? new URLCombiner(url, `?authentication-token=${token}`).toString() : url;
}));
})
)
});
this.outputLogs$ = this.outputLogFileUrl$.pipe(take(1),
mergeMap((url: string) => {
return this.getTextFile(url);
}),
finalize(() => this.zone.run(() => this.retrievingOutputLogs$.next(false))),
);
this.outputLogs$.pipe(take(1)).subscribe();
}
getTextFile(filename: string): Observable<string> {
// The Observable returned by get() is of type Observable<string>
// because a text response was specified.
// There's no need to pass a <string> type parameter to get().
return this.http.get(filename, { responseType: 'text' })
.pipe(
finalize(() => {
this.showOutputLogs = true;
}),
);
}
/**
* Whether or not the given process has Completed or Failed status
* @param process Process to check if completed or failed
*/
isProcessFinished(process: Process): boolean {
return (hasValue(process) && hasValue(process.processStatus) &&
(process.processStatus.toString() === ProcessStatus[ProcessStatus.COMPLETED].toString()
|| process.processStatus.toString() === ProcessStatus[ProcessStatus.FAILED].toString()));
}
}

View File

@@ -1,36 +0,0 @@
import { PROCESS_OUTPUT_TYPE } from '../../core/shared/process-output.resource-type';
import { CacheableObject } from '../../core/cache/object-cache.reducer';
import { HALLink } from '../../core/shared/hal-link.model';
import { autoserialize, deserialize } from 'cerialize';
import { excludeFromEquals } from '../../core/utilities/equals.decorators';
import { ResourceType } from '../../core/shared/resource-type';
import { typedObject } from '../../core/cache/builders/build-decorators';
/**
* Object representing a process output object
*/
@typedObject
export class ProcessOutput implements CacheableObject {
static type = PROCESS_OUTPUT_TYPE;
/**
* The object type
*/
@excludeFromEquals
@autoserialize
type: ResourceType;
/**
* The output strings for this ProcessOutput
*/
@autoserialize
logs: string[];
/**
* The {@link HALLink}s for this ProcessOutput
*/
@deserialize
_links: {
self: HALLink,
};
}

View File

@@ -1,5 +1,5 @@
import { Bitstream } from '../../core/shared/bitstream.model';
import { PROCESS_OUTPUT_TYPE } from '../../core/shared/process-output.resource-type';
import { ProcessOutput } from './process-output.model';
import { ProcessStatus } from './process-status.model';
import { ProcessParameter } from './process-parameter.model';
import { CacheableObject } from '../../core/cache/object-cache.reducer';
@@ -93,5 +93,5 @@ export class Process implements CacheableObject {
* Will be undefined unless the output {@link HALLink} has been resolved.
*/
@link(PROCESS_OUTPUT_TYPE)
output?: Observable<RemoteData<ProcessOutput>>;
output?: Observable<RemoteData<Bitstream>>;
}

View File

@@ -1,4 +1,6 @@
/* tslint:disable:no-empty */
import { Observable, of as observableOf } from 'rxjs';
export class AuthServiceMock {
public checksAuthenticationToken() {
return
@@ -6,4 +8,8 @@ export class AuthServiceMock {
public buildAuthHeader() {
return 'auth-header';
}
public getShortlivedToken(): Observable<string> {
return observableOf('token');
}
}

View File

@@ -2270,7 +2270,7 @@
"process.detail.logs.loading": "Retrieving",
"process.detail.logs.none": "This process has no output yet",
"process.detail.logs.none": "This process has no output",
"process.detail.output-files" : "Output Files",