1 import { ChartData, ChartOptions, TooltipItem } from 'chart.js'
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 { BytesPipe, RedundancyService } from '@app/shared/shared-main'
6 import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
7 import { VideoRedundanciesTarget, VideoRedundancy } from '@shared/models'
8 import { VideosRedundancyStats } from '@shared/models/server'
11 selector: 'my-video-redundancies-list',
12 templateUrl: './video-redundancies-list.component.html',
13 styleUrls: [ './video-redundancies-list.component.scss' ]
15 export class VideoRedundanciesListComponent extends RestTable implements OnInit {
16 private static LOCAL_STORAGE_DISPLAY_TYPE = 'video-redundancies-list-display-type'
18 videoRedundancies: VideoRedundancy[] = []
21 sort: SortMeta = { field: 'name', order: 1 }
22 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
23 displayType: VideoRedundanciesTarget = 'my-videos'
25 redundanciesGraphsData: { stats: VideosRedundancyStats, graphData: ChartData, options: ChartOptions }[] = []
27 noRedundancies = false
29 // Prevent layout shift for redundancy stats
32 private bytesPipe: BytesPipe
35 private notifier: Notifier,
36 private confirmService: ConfirmService,
37 private redundancyService: RedundancyService,
38 private serverService: ServerService
42 this.bytesPipe = new BytesPipe()
46 return 'VideoRedundanciesListComponent'
50 this.loadSelectLocalStorage()
54 this.serverService.getServerStats()
56 const redundancies = res.videosRedundancy
58 if (redundancies.length === 0) this.noRedundancies = true
60 for (const r of redundancies) {
67 if (this.isDisplayingRemoteVideos()) return 5
72 isDisplayingRemoteVideos () {
73 return this.displayType === 'remote-videos'
76 getTotalSize (redundancy: VideoRedundancy) {
77 return redundancy.redundancies.files.reduce((a, b) => a + b.size, 0) +
78 redundancy.redundancies.streamingPlaylists.reduce((a, b) => a + b.size, 0)
81 onDisplayTypeChanged () {
82 this.pagination.start = 0
83 this.saveSelectLocalStorage()
88 getRedundancyStrategy (redundancy: VideoRedundancy) {
89 if (redundancy.redundancies.files.length !== 0) return redundancy.redundancies.files[0].strategy
90 if (redundancy.redundancies.streamingPlaylists.length !== 0) return redundancy.redundancies.streamingPlaylists[0].strategy
95 buildPieData (stats: VideosRedundancyStats) {
96 if (stats.totalSize === 0) return
98 const totalAvailable = stats.totalSize
99 ? stats.totalSize - stats.totalUsed
102 const labels = [ $localize`Used (${this.bytesToHuman(stats.totalUsed)})` ]
103 const data = [ stats.totalUsed ]
105 // Not in manual strategy
106 if (totalAvailable) {
108 $localize`Available (${this.bytesToHuman(totalAvailable)})`
111 data.push(totalAvailable)
114 this.redundanciesGraphsData.push({
125 hoverBackgroundColor: [
141 label: (tooltip: TooltipItem<any>) => {
151 async removeRedundancy (redundancy: VideoRedundancy) {
152 const message = $localize`Do you really want to remove this video redundancy?`
153 const res = await this.confirmService.confirm(message, $localize`Remove redundancy`)
154 if (res === false) return
156 this.redundancyService.removeVideoRedundancies(redundancy)
159 this.notifier.success($localize`Video redundancies removed!`)
163 error: err => this.notifier.error(err.message)
168 protected reloadDataInternal () {
169 this.dataLoaded = false
172 pagination: this.pagination,
174 target: this.displayType
177 this.redundancyService.listVideoRedundancies(options)
179 next: resultList => {
180 this.videoRedundancies = resultList.data
181 this.totalRecords = resultList.total
183 this.dataLoaded = true
186 error: err => this.notifier.error(err.message)
190 private loadSelectLocalStorage () {
191 const displayType = peertubeLocalStorage.getItem(VideoRedundanciesListComponent.LOCAL_STORAGE_DISPLAY_TYPE)
192 if (displayType) this.displayType = displayType as VideoRedundanciesTarget
195 private saveSelectLocalStorage () {
196 peertubeLocalStorage.setItem(VideoRedundanciesListComponent.LOCAL_STORAGE_DISPLAY_TYPE, this.displayType)
199 private bytesToHuman (bytes: number) {
200 return this.bytesPipe.transform(bytes, 1)