]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/system/runners/runner.service.ts
Add % to runner job percentage
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / system / runners / runner.service.ts
1
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'
11
12 export type RunnerJobFormatted = RunnerJob & {
13 payload: string
14 privatePayload: string
15 }
16
17 @Injectable()
18 export class RunnerService {
19 private static BASE_RUNNER_URL = environment.apiUrl + '/api/v1/runners'
20
21 constructor (
22 private authHttp: HttpClient,
23 private server: ServerService,
24 private restService: RestService,
25 private restExtractor: RestExtractor
26 ) {}
27
28 listRegistrationTokens (options: {
29 pagination: RestPagination
30 sort: SortMeta
31 }) {
32 const { pagination, sort } = options
33
34 let params = new HttpParams()
35 params = this.restService.addRestGetParams(params, pagination, sort)
36
37 return this.authHttp.get<ResultList<RunnerRegistrationToken>>(RunnerService.BASE_RUNNER_URL + '/registration-tokens', { params })
38 .pipe(catchError(res => this.restExtractor.handleError(res)))
39 }
40
41 generateToken () {
42 return this.authHttp.post(RunnerService.BASE_RUNNER_URL + '/registration-tokens/generate', {})
43 .pipe(catchError(res => this.restExtractor.handleError(res)))
44 }
45
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)))
49 }
50
51 // ---------------------------------------------------------------------------
52
53 listRunnerJobs (options: {
54 pagination: RestPagination
55 sort: SortMeta
56 search?: string
57 }) {
58 const { pagination, sort, search } = options
59
60 let params = new HttpParams()
61 params = this.restService.addRestGetParams(params, pagination, sort)
62
63 if (search) params = params.append('search', search)
64
65 return forkJoin([
66 this.authHttp.get<ResultList<RunnerJobAdmin>>(RunnerService.BASE_RUNNER_URL + '/jobs', { params }),
67 this.server.getServerLocale()
68 ]).pipe(
69 map(([ res, translations ]) => {
70 const newData = res.data.map(job => {
71 return {
72 ...job,
73
74 state: {
75 id: job.state.id,
76 label: peertubeTranslate(job.state.label, translations)
77 },
78 payload: JSON.stringify(job.payload, null, 2),
79 privatePayload: JSON.stringify(job.privatePayload, null, 2)
80 } as RunnerJobFormatted
81 })
82
83 return {
84 total: res.total,
85 data: newData
86 }
87 }),
88 map(res => this.restExtractor.convertResultListDateToHuman(res, [ 'createdAt', 'startedAt', 'finishedAt' ], 'precise')),
89 catchError(res => this.restExtractor.handleError(res))
90 )
91 }
92
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)))
96 }
97
98 // ---------------------------------------------------------------------------
99
100 listRunners (options: {
101 pagination: RestPagination
102 sort: SortMeta
103 }) {
104 const { pagination, sort } = options
105
106 let params = new HttpParams()
107 params = this.restService.addRestGetParams(params, pagination, sort)
108
109 return this.authHttp.get<ResultList<Runner>>(RunnerService.BASE_RUNNER_URL + '/', { params })
110 .pipe(catchError(res => this.restExtractor.handleError(res)))
111 }
112
113 deleteRunner (runner: Runner) {
114 return this.authHttp.delete(RunnerService.BASE_RUNNER_URL + '/' + runner.id)
115 .pipe(catchError(res => this.restExtractor.handleError(res)))
116 }
117 }