Add ability to list redundancies
[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 LOCAL_STORAGE_STATE = 'jobs-list-state'
20   private static 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     'video-redundancy'
39   ]
40
41   jobs: Job[] = []
42   totalRecords: number
43   rowsPerPage = 10
44   sort: SortMeta = { field: 'createdAt', order: -1 }
45   pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
46
47   constructor (
48     private notifier: Notifier,
49     private jobsService: JobService,
50     private i18n: I18n
51   ) {
52     super()
53   }
54
55   ngOnInit () {
56     this.loadJobStateAndType()
57     this.initialize()
58   }
59
60   onJobStateOrTypeChanged () {
61     this.pagination.start = 0
62
63     this.loadData()
64     this.saveJobStateAndType()
65   }
66
67   protected loadData () {
68     this.jobsService
69       .getJobs(this.jobState, this.jobType, this.pagination, this.sort)
70       .subscribe(
71         resultList => {
72           this.jobs = resultList.data
73           this.totalRecords = resultList.total
74         },
75
76         err => this.notifier.error(err.message)
77       )
78   }
79
80   private loadJobStateAndType () {
81     const state = peertubeLocalStorage.getItem(JobsComponent.LOCAL_STORAGE_STATE)
82     if (state) this.jobState = state as JobState
83
84     const type = peertubeLocalStorage.getItem(JobsComponent.LOCAL_STORAGE_TYPE)
85     if (type) this.jobType = type as JobType
86   }
87
88   private saveJobStateAndType () {
89     peertubeLocalStorage.setItem(JobsComponent.LOCAL_STORAGE_STATE, this.jobState)
90     peertubeLocalStorage.setItem(JobsComponent.LOCAL_STORAGE_TYPE, this.jobType)
91   }
92 }