]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/system/jobs/job.service.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / system / jobs / job.service.ts
1 import { SortMeta } from 'primeng/api'
2 import { Observable } from 'rxjs'
3 import { catchError, map } from 'rxjs/operators'
4 import { HttpClient, HttpParams } from '@angular/common/http'
5 import { Injectable } from '@angular/core'
6 import { RestExtractor, RestPagination, RestService } from '@app/core'
7 import { Job, ResultList } from '@shared/models'
8 import { environment } from '../../../../environments/environment'
9 import { JobStateClient } from '../../../../types/job-state-client.type'
10 import { JobTypeClient } from '../../../../types/job-type-client.type'
11
12 @Injectable()
13 export class JobService {
14 private static BASE_JOB_URL = environment.apiUrl + '/api/v1/jobs'
15
16 constructor (
17 private authHttp: HttpClient,
18 private restService: RestService,
19 private restExtractor: RestExtractor
20 ) {}
21
22 listJobs (options: {
23 jobState?: JobStateClient
24 jobType: JobTypeClient
25 pagination: RestPagination
26 sort: SortMeta
27 }): Observable<ResultList<Job>> {
28 const { jobState, jobType, pagination, sort } = options
29
30 let params = new HttpParams()
31 params = this.restService.addRestGetParams(params, pagination, sort)
32
33 if (jobType !== 'all') params = params.append('jobType', jobType)
34
35 return this.authHttp.get<ResultList<Job>>(JobService.BASE_JOB_URL + `/${jobState || ''}`, { params })
36 .pipe(
37 map(res => this.restExtractor.convertResultListDateToHuman(res, [ 'createdAt', 'processedOn', 'finishedOn' ], 'precise')),
38 map(res => this.restExtractor.applyToResultListData(res, this.prettyPrintData)),
39 map(res => this.restExtractor.applyToResultListData(res, this.buildUniqId)),
40 catchError(err => this.restExtractor.handleError(err))
41 )
42 }
43
44 private prettyPrintData (obj: Job) {
45 const data = JSON.stringify(obj.data, null, 2)
46
47 return Object.assign(obj, { data })
48 }
49
50 private buildUniqId (obj: Job) {
51 return Object.assign(obj, { uniqId: `${obj.id}-${obj.type}` })
52 }
53 }