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