]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/system/jobs/jobs.component.ts
Channel sync (#5135)
[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 'video-channel-import'
43 ]
44
45 jobs: Job[] = []
46 totalRecords: number
47 sort: SortMeta = { field: 'createdAt', order: -1 }
48 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
49
50 constructor (
51 private notifier: Notifier,
52 private jobsService: JobService
53 ) {
54 super()
55 }
56
57 ngOnInit () {
58 this.loadJobStateAndType()
59 this.initialize()
60 }
61
62 getIdentifier () {
63 return 'JobsComponent'
64 }
65
66 getJobStateClass (state: JobStateClient) {
67 switch (state) {
68 case 'active':
69 return 'badge-blue'
70 case 'completed':
71 return 'badge-green'
72 case 'delayed':
73 return 'badge-brown'
74 case 'failed':
75 return 'badge-red'
76 case 'waiting':
77 return 'badge-yellow'
78 }
79 }
80
81 getColspan () {
82 if (this.jobState === 'all' && this.hasGlobalProgress()) return 7
83
84 if (this.jobState === 'all' || this.hasGlobalProgress()) return 6
85
86 return 5
87 }
88
89 onJobStateOrTypeChanged () {
90 this.pagination.start = 0
91
92 this.reloadData()
93 this.saveJobStateAndType()
94 }
95
96 hasGlobalProgress () {
97 return this.jobType === 'all' || this.jobType === 'video-transcoding'
98 }
99
100 hasProgress (job: Job) {
101 return job.type === 'video-transcoding'
102 }
103
104 getProgress (job: Job) {
105 if (job.state === 'active') return job.progress + '%'
106
107 return ''
108 }
109
110 refresh () {
111 this.jobs = []
112 this.totalRecords = 0
113
114 this.reloadData()
115 }
116
117 protected reloadData () {
118 let jobState = this.jobState as JobState
119 if (this.jobState === 'all') jobState = null
120
121 this.jobsService
122 .getJobs({
123 jobState,
124 jobType: this.jobType,
125 pagination: this.pagination,
126 sort: this.sort
127 })
128 .subscribe({
129 next: resultList => {
130 this.jobs = resultList.data
131 this.totalRecords = resultList.total
132 },
133
134 error: err => this.notifier.error(err.message)
135 })
136 }
137
138 private loadJobStateAndType () {
139 const state = peertubeLocalStorage.getItem(JobsComponent.LOCAL_STORAGE_STATE)
140 if (state) this.jobState = state as JobState
141
142 const type = peertubeLocalStorage.getItem(JobsComponent.LOCAL_STORAGE_TYPE)
143 if (type) this.jobType = type as JobType
144 }
145
146 private saveJobStateAndType () {
147 peertubeLocalStorage.setItem(JobsComponent.LOCAL_STORAGE_STATE, this.jobState)
148 peertubeLocalStorage.setItem(JobsComponent.LOCAL_STORAGE_TYPE, this.jobType)
149 }
150 }