2018-05-15 11:55:51 +02:00
|
|
|
import { catchError, map } from 'rxjs/operators'
|
2017-11-30 10:51:13 +01:00
|
|
|
import { HttpClient, HttpParams } from '@angular/common/http'
|
|
|
|
import { Injectable } from '@angular/core'
|
2019-07-25 10:44:21 +02:00
|
|
|
import { SortMeta } from 'primeng/api'
|
2018-05-15 11:55:51 +02:00
|
|
|
import { Observable } from 'rxjs'
|
2017-11-30 10:51:13 +01:00
|
|
|
import { ResultList } from '../../../../../../shared'
|
2018-01-25 15:05:18 +01:00
|
|
|
import { JobState } from '../../../../../../shared/models'
|
2018-02-28 18:04:46 +01:00
|
|
|
import { Job } from '../../../../../../shared/models/server/job.model'
|
2017-12-11 17:36:46 +01:00
|
|
|
import { environment } from '../../../../environments/environment'
|
2017-11-30 10:51:13 +01:00
|
|
|
import { RestExtractor, RestPagination, RestService } from '../../../shared'
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class JobService {
|
2017-12-11 17:36:46 +01:00
|
|
|
private static BASE_JOB_URL = environment.apiUrl + '/api/v1/jobs'
|
2017-11-30 10:51:13 +01:00
|
|
|
|
|
|
|
constructor (
|
|
|
|
private authHttp: HttpClient,
|
|
|
|
private restService: RestService,
|
|
|
|
private restExtractor: RestExtractor
|
|
|
|
) {}
|
|
|
|
|
2018-01-25 15:05:18 +01:00
|
|
|
getJobs (state: JobState, pagination: RestPagination, sort: SortMeta): Observable<ResultList<Job>> {
|
2017-11-30 10:51:13 +01:00
|
|
|
let params = new HttpParams()
|
|
|
|
params = this.restService.addRestGetParams(params, pagination, sort)
|
|
|
|
|
2018-01-25 15:05:18 +01:00
|
|
|
return this.authHttp.get<ResultList<Job>>(JobService.BASE_JOB_URL + '/' + state, { params })
|
2018-05-15 11:55:51 +02:00
|
|
|
.pipe(
|
2018-07-10 17:02:20 +02:00
|
|
|
map(res => {
|
|
|
|
return this.restExtractor.convertResultListDateToHuman(res, [ 'createdAt', 'processedOn', 'finishedOn' ])
|
|
|
|
}),
|
2018-05-15 11:55:51 +02:00
|
|
|
map(res => this.restExtractor.applyToResultListData(res, this.prettyPrintData)),
|
2018-07-10 17:02:20 +02:00
|
|
|
map(res => this.restExtractor.applyToResultListData(res, this.buildUniqId)),
|
2018-05-15 11:55:51 +02:00
|
|
|
catchError(err => this.restExtractor.handleError(err))
|
|
|
|
)
|
2017-11-30 10:51:13 +01:00
|
|
|
}
|
2017-12-08 14:34:17 +01:00
|
|
|
|
|
|
|
private prettyPrintData (obj: Job) {
|
2018-01-25 15:05:18 +01:00
|
|
|
const data = JSON.stringify(obj.data, null, 2)
|
2017-12-08 14:34:17 +01:00
|
|
|
|
2018-01-25 15:05:18 +01:00
|
|
|
return Object.assign(obj, { data })
|
2017-12-08 14:34:17 +01:00
|
|
|
}
|
2018-07-10 17:02:20 +02:00
|
|
|
|
|
|
|
private buildUniqId (obj: Job) {
|
|
|
|
return Object.assign(obj, { uniqId: `${obj.id}-${obj.type}` })
|
|
|
|
}
|
2017-11-30 10:51:13 +01:00
|
|
|
}
|