PeerTube/client/src/app/+admin/system/jobs/job.service.ts

50 lines
2.0 KiB
TypeScript
Raw Normal View History

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'
2019-12-04 14:49:59 +01:00
import { JobType, ResultList } from '../../../../../../shared'
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'
2019-12-04 14:49:59 +01:00
import { JobTypeClient } from '../../../../types/job-type-client.type'
2017-11-30 10:51:13 +01:00
@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
) {}
2019-12-04 14:49:59 +01:00
getJobs (state: JobState, jobType: JobTypeClient, 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)
2019-12-04 14:49:59 +01:00
if (jobType !== 'all') params = params.append('jobType', jobType)
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) {
const data = JSON.stringify(obj.data, null, 2)
2017-12-08 14:34:17 +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
}