]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/system/jobs/job.service.ts
4b4a8914fdd5543776351be93853e790ffc5624a
[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 getJobs (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 ? jobState : ''}`, { params })
36 .pipe(
37 map(res => {
38 return this.restExtractor.convertResultListDateToHuman(res, [ 'createdAt', 'processedOn', 'finishedOn' ])
39 }),
40 map(res => this.restExtractor.applyToResultListData(res, this.prettyPrintData)),
41 map(res => this.restExtractor.applyToResultListData(res, this.buildUniqId)),
42 catchError(err => this.restExtractor.handleError(err))
43 )
44 }
45
46 private prettyPrintData (obj: Job) {
47 const data = JSON.stringify(obj.data, null, 2)
48
49 return Object.assign(obj, { data })
50 }
51
52 private buildUniqId (obj: Job) {
53 return Object.assign(obj, { uniqId: `${obj.id}-${obj.type}` })
54 }
55 }