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