20c8ea71a0da906af1c99a68e583265e214b959e
[oweals/peertube.git] / client / src / app / +admin / system / jobs / jobs.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
3 import { Notifier } from '@app/core'
4 import { SortMeta } from 'primeng/api'
5 import { Job, JobType } from '../../../../../../shared/index'
6 import { JobState } from '../../../../../../shared/models'
7 import { RestPagination, RestTable } from '../../../shared'
8 import { JobService } from './job.service'
9 import { I18n } from '@ngx-translate/i18n-polyfill'
10 import { JobStateClient } from '../../../../types/job-state-client.type'
11 import { JobTypeClient } from '../../../../types/job-type-client.type'
12
13 @Component({
14   selector: 'my-jobs',
15   templateUrl: './jobs.component.html',
16   styleUrls: [ './jobs.component.scss' ]
17 })
18 export class JobsComponent extends RestTable implements OnInit {
19   private static JOB_STATE_LOCAL_STORAGE_STATE = 'jobs-list-state'
20   private static JOB_STATE_LOCAL_STORAGE_TYPE = 'jobs-list-type'
21
22   jobState: JobStateClient = 'waiting'
23   jobStates: JobStateClient[] = [ 'active', 'completed', 'failed', 'waiting', 'delayed' ]
24
25   jobType: JobTypeClient = 'all'
26   jobTypes: JobTypeClient[] = [
27     'all',
28     'activitypub-follow',
29     'activitypub-http-broadcast',
30     'activitypub-http-fetcher',
31     'activitypub-http-unicast',
32     'email',
33     'video-transcoding',
34     'video-file-import',
35     'video-import',
36     'videos-views',
37     'activitypub-refresher'
38   ]
39
40   jobs: Job[] = []
41   totalRecords: number
42   rowsPerPage = 10
43   sort: SortMeta = { field: 'createdAt', order: -1 }
44   pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
45
46   constructor (
47     private notifier: Notifier,
48     private jobsService: JobService,
49     private i18n: I18n
50   ) {
51     super()
52   }
53
54   ngOnInit () {
55     this.loadJobStateAndType()
56     this.initialize()
57   }
58
59   onJobStateOrTypeChanged () {
60     this.pagination.start = 0
61
62     this.loadData()
63     this.saveJobStateAndType()
64   }
65
66   protected loadData () {
67     this.jobsService
68       .getJobs(this.jobState, this.jobType, this.pagination, this.sort)
69       .subscribe(
70         resultList => {
71           this.jobs = resultList.data
72           this.totalRecords = resultList.total
73         },
74
75         err => this.notifier.error(err.message)
76       )
77   }
78
79   private loadJobStateAndType () {
80     const state = peertubeLocalStorage.getItem(JobsComponent.JOB_STATE_LOCAL_STORAGE_STATE)
81     if (state) this.jobState = state as JobState
82
83     const type = peertubeLocalStorage.getItem(JobsComponent.JOB_STATE_LOCAL_STORAGE_TYPE)
84     if (type) this.jobType = type as JobType
85   }
86
87   private saveJobStateAndType () {
88     peertubeLocalStorage.setItem(JobsComponent.JOB_STATE_LOCAL_STORAGE_STATE, this.jobState)
89     peertubeLocalStorage.setItem(JobsComponent.JOB_STATE_LOCAL_STORAGE_TYPE, this.jobType)
90   }
91 }