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