import {Injectable} from '@angular/core'; import {HttpClient, HttpParams} from "@angular/common/http"; import {ConfigurationService} from "../env/configuration.service"; import {Observable} from "rxjs"; import {map, switchMap} from "rxjs/operators"; import {Job, JobExecution, JobSpec} from "../model/job"; import {FiltersService} from "./filters.service"; import {CustomHttpParamEncoder} from "../custom-http-param-encoder"; @Injectable({ providedIn: 'root' }) export class JobsService { endPoint: string = '/services/job/'; constructor( private http: HttpClient, private configService: ConfigurationService, private filtersService: FiltersService) { } public getJobRecordCount(epoch: number, queue: string, pattern: string, status: string): Observable<number> { return this.http.get<number>(this.configService.config.url + this.endPoint + 'byEpoch/' + epoch.toString() + '/byQueue/' + queue + '/pages?pattern=' + encodeURIComponent(pattern) + '&status=' + encodeURI(status), {observe: 'response'}).pipe( map(response => { return response.body; })); } public getJobSpecRecordCount(epoch: number, queue: string, pattern: string, status: string): Observable<number> { return this.http.get<number>(this.configService.config.url + this.endPoint + 'specs/byEpoch/' + epoch.toString() + '/byQueue/' + queue + '/pages?pattern=' + encodeURIComponent(pattern) + '&status=' + encodeURI(status), {observe: 'response'}).pipe( map(response => { return response.body; })); } public getJobSpecPage(epoch: number, queue: string, pageId: number, pattern: string, status: string): Observable<Array<JobSpec>> { let params = new HttpParams({encoder: new CustomHttpParamEncoder()}); if (!!pattern) { params = params.append('pattern', pattern); } if (!!status) { params = params.append('status', status); } const sortName = this.filtersService.getCurrentSetting('JOBSPEC_SORT'); const sortDir = this.filtersService.getCurrentSetting('SORT_DIRECTION'); params = params.append('columnNames', sortName); params = params.append('directions', sortDir); return this.http.get<Array<JobSpec>>(this.configService.config.url + this.endPoint + 'specs/byEpoch/' + epoch.toString() + '/byQueue/' + queue + '/pages/' + pageId.toString() + '/', {params: params, observe: 'response'}).pipe( map(response => { return response.body; })); } public getJobPage(epoch: number, queue: string, pageId: number, pattern: string, status: string): Observable<Array<Job>> { let params = new HttpParams({encoder: new CustomHttpParamEncoder()}); if (!!pattern) { params = params.append('pattern', pattern); } if (!!status) { params = params.append('status', status); } const sortName = this.filtersService.getCurrentSetting('JOB_SORT'); const sortDir = this.filtersService.getCurrentSetting('SORT_DIRECTION'); params = params.append('columnNames', sortName); params = params.append('directions', sortDir); return this.http.get<Array<Job>>(this.configService.config.url + this.endPoint + 'byEpoch/' + epoch.toString() + '/byQueue/' + queue + '/pages/' + pageId.toString() + '/', {params: params, observe: 'response'}).pipe( map(response => { return response.body; })); } public getJobSpec(id: number): Observable<JobSpec> { return this.http.get<JobSpec>(this.configService.config.url + this.endPoint + 'specs/' + id, {observe: 'response'}).pipe( map(response => { return response.body; })); } // unlike the above, this returns an object the same shape as the pages public getJobSpecById(id: number): Observable<JobSpec> { return this.http.get<JobSpec>(this.configService.config.url + this.endPoint + 'specs/byId/' + id, {observe: 'response'}).pipe( map(response => { return response.body; })); } public jobSpecToJob(spec: JobSpec): Job { const summary = new Job(); summary.jobspec_id = spec.id; summary.jobspec_name = spec.name; summary.jobspec_creation_date = spec.creationDate; summary.jobspec_status = spec.status; summary.jobspec_creation_date_formatted = new Date(spec.creationDate).toLocaleString("en-US"); summary.jobspec_sdm_id = spec.sdmId; for (let i = 0; i < spec.executions.length; i++) { const exec = spec.executions[i]; summary.job_id = exec.id; summary.job_name = exec.name; summary.job_starttime = Math.round(new Date(exec.startDate).getTime() / 1000); summary.job_endtime = Math.round(new Date(exec.endDate).getTime() / 1000); summary.job_status = exec.status; summary.job_arch_status = exec.archiveStatus; summary.job_starttime_formatted = exec.startDate; summary.job_endtime_formatted = exec.endDate; } return summary; } public getJobExecution(id: number): Observable<JobExecution> { return this.http.get<JobExecution>(this.configService.config.url + this.endPoint + 'jobs/' + id, {observe: 'response'}).pipe( map(response => { return response.body; })); } // unlike the above, this returns an object the same shape as the pages public getJobById(id: number): Observable<Job> { return this.http.get<Job>(this.configService.config.url + this.endPoint + 'byId/' + id, {observe: 'response'}).pipe( map(response => { return response.body; })); } public createJob(id: number, queue: string, version: number): Observable<any> { let command = {productId: id, inputProductVersions: [version]}; return this.http.post(this.configService.config.url + this.endPoint + 'createJob?id=' + id + '&queue=' + queue, command, {observe: "response"}).pipe( map(response => { return response.status; })); } public submitJob(id: number, queue: string): Observable<any> { return this.http.put(this.configService.config.url + this.endPoint + 'queues/' + queue + '/specs/' + id + '/submit', {}, {observe: "response"}).pipe( map(response => { return response; })); } public updateNotes(id: number, notes: string): Observable<any> { return this.http.put(this.configService.config.url + this.endPoint + 'jobs/' + id + '/notes', {notes: notes}, {observe: "response"}).pipe( map(response => { return notes; })); } public updateJobStatus(id: number, status: string) { return this.http.put(this.configService.config.url + this.endPoint + 'jobs/' + id + '/status?status=' + status, {}, {observe: "response"}).pipe( map(response => { return status; })); } // Returns a JSON encoded string of plane information public getPlanes(id: number): Observable<any> { return this.http.get(this.configService.config.url + this.endPoint + 'jobs/' + id + '/planes', {observe: 'response'}).pipe( map(response => { return response; })); } // Writes a string of plane names public writePlanes(id: number, planes: string): Observable<any> { return this.http.put(this.configService.config.url + this.endPoint + 'jobs/' + id + '/writePlanes', {planes}, {observe: 'response'}).pipe( map(response => { return response.status; })); } // Returns a response containing the execution's pipeline directory as a string public getPipelineDir(id: number): Observable<any> { return this.http.get(this.configService.config.url + this.endPoint + 'jobs/' + id + '/pipelinedir', {observe: 'response', responseType: 'text'}).pipe( map(response => { return response; })); } public performQA(id: number, status: string, queue: string) { return this.http.put(this.configService.config.url + this.endPoint + 'jobs/' + id + '/status?status=' + status + '&queue=' + queue, {}, {observe: "response"}).pipe( switchMap(response => { return this.http.put(this.configService.config.url + this.endPoint + 'jobs/' + id + '/ingest?status=' + status + '&queue=' + queue, {}, {observe: "response"}).pipe( map(response => { return response.status; })); })); } public deleteJobSpec(jobSpecId: number, queue: string): Observable<number> { return this.http.delete<string>(this.configService.config.url + this.endPoint + 'queues/' + queue + '/specs/' + jobSpecId, {observe: "response"}).pipe( map(response => { return response.status; })); } }