]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/system/jobs/jobs.component.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / system / jobs / jobs.component.ts
1 import { SortMeta } from 'primeng/api'
2 import { Component, OnInit } from '@angular/core'
3 import { Notifier, RestPagination, RestTable } from '@app/core'
4 import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
5 import { escapeHTML } from '@shared/core-utils/renderer'
6 import { Job, JobState, JobType } from '@shared/models'
7 import { JobStateClient } from '../../../../types/job-state-client.type'
8 import { JobTypeClient } from '../../../../types/job-type-client.type'
9 import { JobService } from './job.service'
10
11 @Component({
12 selector: 'my-jobs',
13 templateUrl: './jobs.component.html',
14 styleUrls: [ './jobs.component.scss' ]
15 })
16 export class JobsComponent extends RestTable implements OnInit {
17 private static LOCAL_STORAGE_STATE = 'jobs-list-state'
18 private static LOCAL_STORAGE_TYPE = 'jobs-list-type'
19
20 jobState?: JobStateClient | 'all'
21 jobStates: JobStateClient[] = [ 'active', 'completed', 'failed', 'waiting', 'delayed' ]
22
23 jobType: JobTypeClient = 'all'
24 jobTypes: JobTypeClient[] = [
25 'all',
26
27 'activitypub-cleaner',
28 'activitypub-follow',
29 'activitypub-http-broadcast-parallel',
30 'activitypub-http-broadcast',
31 'activitypub-http-fetcher',
32 'activitypub-http-unicast',
33 'activitypub-refresher',
34 'actor-keys',
35 'after-video-channel-import',
36 'email',
37 'federate-video',
38 'manage-video-torrent',
39 'move-to-object-storage',
40 'notify',
41 'video-channel-import',
42 'video-file-import',
43 'video-import',
44 'video-live-ending',
45 'video-redundancy',
46 'video-studio-edition',
47 'video-transcoding',
48 'videos-views-stats'
49 ]
50
51 jobs: Job[] = []
52 totalRecords: number
53 sort: SortMeta = { field: 'createdAt', order: -1 }
54 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
55
56 constructor (
57 private notifier: Notifier,
58 private jobsService: JobService
59 ) {
60 super()
61 }
62
63 ngOnInit () {
64 this.loadJobStateAndType()
65 this.initialize()
66 }
67
68 getIdentifier () {
69 return 'JobsComponent'
70 }
71
72 getJobStateClass (state: JobStateClient) {
73 switch (state) {
74 case 'active':
75 return 'badge-blue'
76 case 'completed':
77 return 'badge-green'
78 case 'delayed':
79 return 'badge-brown'
80 case 'failed':
81 return 'badge-red'
82 case 'waiting':
83 return 'badge-yellow'
84 }
85 }
86
87 getColspan () {
88 if (this.jobState === 'all' && this.hasGlobalProgress()) return 7
89
90 if (this.jobState === 'all' || this.hasGlobalProgress()) return 6
91
92 return 5
93 }
94
95 onJobStateOrTypeChanged () {
96 this.pagination.start = 0
97
98 this.reloadData()
99 this.saveJobStateAndType()
100 }
101
102 hasGlobalProgress () {
103 return this.jobType === 'all' || this.jobType === 'video-transcoding'
104 }
105
106 hasProgress (job: Job) {
107 return job.type === 'video-transcoding'
108 }
109
110 getProgress (job: Job) {
111 if (job.state === 'active') return job.progress + '%'
112
113 return ''
114 }
115
116 refresh () {
117 this.jobs = []
118 this.totalRecords = 0
119
120 this.reloadData()
121 }
122
123 protected reloadDataInternal () {
124 let jobState = this.jobState as JobState
125 if (this.jobState === 'all') jobState = null
126
127 this.jobsService
128 .listJobs({
129 jobState,
130 jobType: this.jobType,
131 pagination: this.pagination,
132 sort: this.sort
133 })
134 .subscribe({
135 next: resultList => {
136 this.jobs = resultList.data
137 this.totalRecords = resultList.total
138 },
139
140 error: err => this.notifier.error(err.message)
141 })
142 }
143
144 private loadJobStateAndType () {
145 const state = peertubeLocalStorage.getItem(JobsComponent.LOCAL_STORAGE_STATE)
146
147 // FIXME: We use <ng-option> that doesn't escape HTML
148 // https://github.com/ng-select/ng-select/issues/1363
149 if (state) this.jobState = escapeHTML(state) as JobState
150
151 const type = peertubeLocalStorage.getItem(JobsComponent.LOCAL_STORAGE_TYPE)
152 if (type) this.jobType = type as JobType
153 }
154
155 private saveJobStateAndType () {
156 peertubeLocalStorage.setItem(JobsComponent.LOCAL_STORAGE_STATE, this.jobState)
157 peertubeLocalStorage.setItem(JobsComponent.LOCAL_STORAGE_TYPE, this.jobType)
158 }
159 }