1 import { BytesPipe } from 'ngx-pipes'
2 import { SortMeta } from 'primeng/api'
3 import { Component, OnInit } from '@angular/core'
4 import { ConfirmService, Notifier, RestPagination, RestTable, ServerService } from '@app/core'
5 import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
6 import { RedundancyService } from '@app/shared/shared-main'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { VideoRedundanciesTarget, VideoRedundancy } from '@shared/models'
9 import { VideosRedundancyStats } from '@shared/models/server'
12 selector: 'my-video-redundancies-list',
13 templateUrl: './video-redundancies-list.component.html',
14 styleUrls: [ '../follows.component.scss', './video-redundancies-list.component.scss' ]
16 export class VideoRedundanciesListComponent extends RestTable implements OnInit {
17 private static LOCAL_STORAGE_DISPLAY_TYPE = 'video-redundancies-list-display-type'
19 videoRedundancies: VideoRedundancy[] = []
22 sort: SortMeta = { field: 'name', order: 1 }
23 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
24 displayType: VideoRedundanciesTarget = 'my-videos'
26 redundanciesGraphsData: { stats: VideosRedundancyStats, graphData: object, options: object }[] = []
28 noRedundancies = false
30 private bytesPipe: BytesPipe
33 private notifier: Notifier,
34 private confirmService: ConfirmService,
35 private redundancyService: RedundancyService,
36 private serverService: ServerService,
41 this.bytesPipe = new BytesPipe()
45 return 'VideoRedundanciesListComponent'
49 this.loadSelectLocalStorage()
53 this.serverService.getServerStats()
55 const redundancies = res.videosRedundancy
57 if (redundancies.length === 0) this.noRedundancies = true
59 for (const r of redundancies) {
66 if (this.isDisplayingRemoteVideos()) return 5
71 isDisplayingRemoteVideos () {
72 return this.displayType === 'remote-videos'
75 getTotalSize (redundancy: VideoRedundancy) {
76 return redundancy.redundancies.files.reduce((a, b) => a + b.size, 0) +
77 redundancy.redundancies.streamingPlaylists.reduce((a, b) => a + b.size, 0)
80 onDisplayTypeChanged () {
81 this.pagination.start = 0
82 this.saveSelectLocalStorage()
87 getRedundancyStrategy (redundancy: VideoRedundancy) {
88 if (redundancy.redundancies.files.length !== 0) return redundancy.redundancies.files[0].strategy
89 if (redundancy.redundancies.streamingPlaylists.length !== 0) return redundancy.redundancies.streamingPlaylists[0].strategy
94 buildPieData (stats: VideosRedundancyStats) {
95 const totalSize = stats.totalSize
96 ? stats.totalSize - stats.totalUsed
99 if (totalSize === 0) return
101 this.redundanciesGraphsData.push({
104 labels: [ this.i18n('Used'), this.i18n('Available') ],
107 data: [ stats.totalUsed, totalSize ],
112 hoverBackgroundColor: [
127 label: (tooltipItem: any, data: any) => {
128 const dataset = data.datasets[tooltipItem.datasetIndex]
129 let label = data.labels[tooltipItem.index]
130 if (label) label += ': '
133 label += this.bytesPipe.transform(dataset.data[tooltipItem.index], 1)
142 async removeRedundancy (redundancy: VideoRedundancy) {
143 const message = this.i18n('Do you really want to remove this video redundancy?')
144 const res = await this.confirmService.confirm(message, this.i18n('Remove redundancy'))
145 if (res === false) return
147 this.redundancyService.removeVideoRedundancies(redundancy)
150 this.notifier.success(this.i18n('Video redundancies removed!'))
154 err => this.notifier.error(err.message)
159 protected loadData () {
161 pagination: this.pagination,
163 target: this.displayType
166 this.redundancyService.listVideoRedundancies(options)
169 this.videoRedundancies = resultList.data
170 this.totalRecords = resultList.total
173 err => this.notifier.error(err.message)
177 private loadSelectLocalStorage () {
178 const displayType = peertubeLocalStorage.getItem(VideoRedundanciesListComponent.LOCAL_STORAGE_DISPLAY_TYPE)
179 if (displayType) this.displayType = displayType as VideoRedundanciesTarget
182 private saveSelectLocalStorage () {
183 peertubeLocalStorage.setItem(VideoRedundanciesListComponent.LOCAL_STORAGE_DISPLAY_TYPE, this.displayType)