1 import { SortMeta } from 'primeng/api'
2 import { Component, OnInit } from '@angular/core'
3 import { ConfirmService, Notifier, RestPagination, RestTable, ServerService } from '@app/core'
4 import { BytesPipe, RedundancyService } from '@app/shared/shared-main'
5 import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
6 import { VideoRedundanciesTarget, VideoRedundancy } from '@shared/models'
7 import { VideosRedundancyStats } from '@shared/models/server'
10 selector: 'my-video-redundancies-list',
11 templateUrl: './video-redundancies-list.component.html',
12 styleUrls: [ '../follows.component.scss', './video-redundancies-list.component.scss' ]
14 export class VideoRedundanciesListComponent extends RestTable implements OnInit {
15 private static LOCAL_STORAGE_DISPLAY_TYPE = 'video-redundancies-list-display-type'
17 videoRedundancies: VideoRedundancy[] = []
20 sort: SortMeta = { field: 'name', order: 1 }
21 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
22 displayType: VideoRedundanciesTarget = 'my-videos'
24 redundanciesGraphsData: { stats: VideosRedundancyStats, graphData: object, options: object }[] = []
26 noRedundancies = false
28 private bytesPipe: BytesPipe
31 private notifier: Notifier,
32 private confirmService: ConfirmService,
33 private redundancyService: RedundancyService,
34 private serverService: ServerService
38 this.bytesPipe = new BytesPipe()
42 return 'VideoRedundanciesListComponent'
46 this.loadSelectLocalStorage()
50 this.serverService.getServerStats()
52 const redundancies = res.videosRedundancy
54 if (redundancies.length === 0) this.noRedundancies = true
56 for (const r of redundancies) {
63 if (this.isDisplayingRemoteVideos()) return 5
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: [ $localize`Used`, $localize`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 = $localize`Do you really want to remove this video redundancy?`
141 const res = await this.confirmService.confirm(message, $localize`Remove redundancy`)
142 if (res === false) return
144 this.redundancyService.removeVideoRedundancies(redundancy)
147 this.notifier.success($localize`Video redundancies removed!`)
151 err => this.notifier.error(err.message)
156 protected reloadData () {
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)