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

54 lines
2.0 KiB
TypeScript
Raw Normal View History

2020-06-23 14:10:17 +02:00
import { SortMeta } from 'primeng/api'
import { Observable } from 'rxjs'
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'
2020-06-23 14:10:17 +02:00
import { RestExtractor, RestPagination, RestService } from '@app/core'
import { Job, ResultList } from '@peertube/peertube-models'
2017-12-11 17:36:46 +01:00
import { environment } from '../../../../environments/environment'
import { JobStateClient } from '../../../../types/job-state-client.type'
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
) {}
2023-01-19 09:29:47 +01:00
listJobs (options: {
2021-08-17 14:42:53 +02:00
jobState?: JobStateClient
jobType: JobTypeClient
pagination: RestPagination
2020-12-13 19:27:25 +01:00
sort: SortMeta
}): Observable<ResultList<Job>> {
const { jobState, jobType, pagination, sort } = options
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)
2021-08-17 14:42:53 +02:00
return this.authHttp.get<ResultList<Job>>(JobService.BASE_JOB_URL + `/${jobState || ''}`, { params })
2018-05-15 11:55:51 +02:00
.pipe(
2022-08-10 10:26:20 +02:00
map(res => this.restExtractor.convertResultListDateToHuman(res, [ 'createdAt', 'processedOn', 'finishedOn' ], 'precise')),
2024-02-22 10:12:04 +01:00
map(res => this.restExtractor.applyToResultListData(res, this.prettyPrintData.bind(this))),
map(res => this.restExtractor.applyToResultListData(res, this.buildUniqId.bind(this))),
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
}