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