2 import { SortMeta } from 'primeng/api'
3 import { catchError, forkJoin, map } from 'rxjs'
4 import { HttpClient, HttpParams } from '@angular/common/http'
5 import { Injectable } from '@angular/core'
6 import { RestExtractor, RestPagination, RestService, ServerService } from '@app/core'
7 import { peertubeTranslate } from '@shared/core-utils'
8 import { ResultList } from '@shared/models/common'
9 import { Runner, RunnerJob, RunnerJobAdmin, RunnerRegistrationToken } from '@shared/models/runners'
10 import { environment } from '../../../../environments/environment'
12 export type RunnerJobFormatted = RunnerJob & {
14 privatePayload: string
18 export class RunnerService {
19 private static BASE_RUNNER_URL = environment.apiUrl + '/api/v1/runners'
22 private authHttp: HttpClient,
23 private server: ServerService,
24 private restService: RestService,
25 private restExtractor: RestExtractor
28 listRegistrationTokens (options: {
29 pagination: RestPagination
32 const { pagination, sort } = options
34 let params = new HttpParams()
35 params = this.restService.addRestGetParams(params, pagination, sort)
37 return this.authHttp.get<ResultList<RunnerRegistrationToken>>(RunnerService.BASE_RUNNER_URL + '/registration-tokens', { params })
38 .pipe(catchError(res => this.restExtractor.handleError(res)))
42 return this.authHttp.post(RunnerService.BASE_RUNNER_URL + '/registration-tokens/generate', {})
43 .pipe(catchError(res => this.restExtractor.handleError(res)))
46 removeToken (token: RunnerRegistrationToken) {
47 return this.authHttp.delete(RunnerService.BASE_RUNNER_URL + '/registration-tokens/' + token.id)
48 .pipe(catchError(res => this.restExtractor.handleError(res)))
51 // ---------------------------------------------------------------------------
53 listRunnerJobs (options: {
54 pagination: RestPagination
58 const { pagination, sort, search } = options
60 let params = new HttpParams()
61 params = this.restService.addRestGetParams(params, pagination, sort)
63 if (search) params = params.append('search', search)
66 this.authHttp.get<ResultList<RunnerJobAdmin>>(RunnerService.BASE_RUNNER_URL + '/jobs', { params }),
67 this.server.getServerLocale()
69 map(([ res, translations ]) => {
70 const newData = res.data.map(job => {
76 label: peertubeTranslate(job.state.label, translations)
78 payload: JSON.stringify(job.payload, null, 2),
79 privatePayload: JSON.stringify(job.privatePayload, null, 2)
80 } as RunnerJobFormatted
88 map(res => this.restExtractor.convertResultListDateToHuman(res, [ 'createdAt', 'startedAt', 'finishedAt' ], 'precise')),
89 catchError(res => this.restExtractor.handleError(res))
93 cancelJob (job: RunnerJob) {
94 return this.authHttp.post(RunnerService.BASE_RUNNER_URL + '/jobs/' + job.uuid + '/cancel', {})
95 .pipe(catchError(res => this.restExtractor.handleError(res)))
98 // ---------------------------------------------------------------------------
100 listRunners (options: {
101 pagination: RestPagination
104 const { pagination, sort } = options
106 let params = new HttpParams()
107 params = this.restService.addRestGetParams(params, pagination, sort)
109 return this.authHttp.get<ResultList<Runner>>(RunnerService.BASE_RUNNER_URL + '/', { params })
110 .pipe(catchError(res => this.restExtractor.handleError(res)))
113 deleteRunner (runner: Runner) {
114 return this.authHttp.delete(RunnerService.BASE_RUNNER_URL + '/' + runner.id)
115 .pipe(catchError(res => this.restExtractor.handleError(res)))