1 import { Component, OnInit } from '@angular/core'
2 import { Notifier, ServerService } from '@app/core'
3 import { SortMeta } from 'primeng/api'
4 import { ConfirmService } from '../../../core/confirm/confirm.service'
5 import { RestPagination, RestTable } from '../../../shared'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { VideoRedundanciesTarget, VideoRedundancy } from '@shared/models'
8 import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
9 import { VideosRedundancyStats } from '@shared/models/server'
10 import { BytesPipe } from 'ngx-pipes'
11 import { RedundancyService } from '@app/shared/video/redundancy.service'
14 selector: 'my-video-redundancies-list',
15 templateUrl: './video-redundancies-list.component.html',
16 styleUrls: [ './video-redundancies-list.component.scss' ]
18 export class VideoRedundanciesListComponent extends RestTable implements OnInit {
19 private static LOCAL_STORAGE_DISPLAY_TYPE = 'video-redundancies-list-display-type'
21 videoRedundancies: VideoRedundancy[] = []
25 sort: SortMeta = { field: 'name', order: 1 }
26 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
27 displayType: VideoRedundanciesTarget = 'my-videos'
29 redundanciesGraphsData: { stats: VideosRedundancyStats, graphData: object, options: object }[] = []
31 noRedundancies = false
33 private bytesPipe: BytesPipe
36 private notifier: Notifier,
37 private confirmService: ConfirmService,
38 private redundancyService: RedundancyService,
39 private serverService: ServerService,
44 this.bytesPipe = new BytesPipe()
48 return 'VideoRedundanciesListComponent'
52 this.loadSelectLocalStorage()
56 this.serverService.getServerStats()
58 const redundancies = res.videosRedundancy
60 if (redundancies.length === 0) this.noRedundancies = true
62 for (const r of redundancies) {
68 isDisplayingRemoteVideos () {
69 return this.displayType === 'remote-videos'
72 getTotalSize (redundancy: VideoRedundancy) {
73 return redundancy.redundancies.files.reduce((a, b) => a + b.size, 0) +
74 redundancy.redundancies.streamingPlaylists.reduce((a, b) => a + b.size, 0)
77 onDisplayTypeChanged () {
78 this.pagination.start = 0
79 this.saveSelectLocalStorage()
84 getRedundancyStrategy (redundancy: VideoRedundancy) {
85 if (redundancy.redundancies.files.length !== 0) return redundancy.redundancies.files[0].strategy
86 if (redundancy.redundancies.streamingPlaylists.length !== 0) return redundancy.redundancies.streamingPlaylists[0].strategy
91 buildPieData (stats: VideosRedundancyStats) {
92 const totalSize = stats.totalSize
93 ? stats.totalSize - stats.totalUsed
96 if (totalSize === 0) return
98 this.redundanciesGraphsData.push({
101 labels: [ this.i18n('Used'), this.i18n('Available') ],
104 data: [ stats.totalUsed, totalSize ],
109 hoverBackgroundColor: [
124 label: (tooltipItem: any, data: any) => {
125 const dataset = data.datasets[tooltipItem.datasetIndex]
126 let label = data.labels[tooltipItem.index]
127 if (label) label += ': '
130 label += this.bytesPipe.transform(dataset.data[tooltipItem.index], 1)
139 async removeRedundancy (redundancy: VideoRedundancy) {
140 const message = this.i18n('Do you really want to remove this video redundancy?')
141 const res = await this.confirmService.confirm(message, this.i18n('Remove redundancy'))
142 if (res === false) return
144 this.redundancyService.removeVideoRedundancies(redundancy)
147 this.notifier.success(this.i18n('Video redundancies removed!'))
151 err => this.notifier.error(err.message)
156 protected loadData () {
158 pagination: this.pagination,
160 target: this.displayType
163 this.redundancyService.listVideoRedundancies(options)
166 this.videoRedundancies = resultList.data
167 this.totalRecords = resultList.total
170 err => this.notifier.error(err.message)
174 private loadSelectLocalStorage () {
175 const displayType = peertubeLocalStorage.getItem(VideoRedundanciesListComponent.LOCAL_STORAGE_DISPLAY_TYPE)
176 if (displayType) this.displayType = displayType as VideoRedundanciesTarget
179 private saveSelectLocalStorage () {
180 peertubeLocalStorage.setItem(VideoRedundanciesListComponent.LOCAL_STORAGE_DISPLAY_TYPE, this.displayType)