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) {
66 isDisplayingRemoteVideos () {
67 return this.displayType === 'remote-videos'
70 getTotalSize (redundancy: VideoRedundancy) {
71 return redundancy.redundancies.files.reduce((a, b) => a + b.size, 0) +
72 redundancy.redundancies.streamingPlaylists.reduce((a, b) => a + b.size, 0)
75 onDisplayTypeChanged () {
76 this.pagination.start = 0
77 this.saveSelectLocalStorage()
82 getRedundancyStrategy (redundancy: VideoRedundancy) {
83 if (redundancy.redundancies.files.length !== 0) return redundancy.redundancies.files[0].strategy
84 if (redundancy.redundancies.streamingPlaylists.length !== 0) return redundancy.redundancies.streamingPlaylists[0].strategy
89 buildPieData (stats: VideosRedundancyStats) {
90 if (stats.totalSize === 0) return
92 const totalAvailable = stats.totalSize
93 ? stats.totalSize - stats.totalUsed
96 const labels = [ $localize`Used (${this.bytesToHuman(stats.totalUsed)})` ]
97 const data = [ stats.totalUsed ]
99 // Not in manual strategy
100 if (totalAvailable) {
102 $localize`Available (${this.bytesToHuman(totalAvailable)})`
105 data.push(totalAvailable)
108 this.redundanciesGraphsData.push({
119 hoverBackgroundColor: [
135 label: (tooltip: TooltipItem<any>) => {
145 async removeRedundancy (redundancy: VideoRedundancy) {
146 const message = $localize`Do you really want to remove this video redundancy?`
147 const res = await this.confirmService.confirm(message, $localize`Remove redundancy`)
148 if (res === false) return
150 this.redundancyService.removeVideoRedundancies(redundancy)
153 this.notifier.success($localize`Video redundancies removed!`)
157 error: err => this.notifier.error(err.message)
162 protected reloadDataInternal () {
163 this.dataLoaded = false
166 pagination: this.pagination,
168 target: this.displayType
171 this.redundancyService.listVideoRedundancies(options)
173 next: resultList => {
174 this.videoRedundancies = resultList.data
175 this.totalRecords = resultList.total
177 this.dataLoaded = true
180 error: err => this.notifier.error(err.message)
184 private loadSelectLocalStorage () {
185 const displayType = peertubeLocalStorage.getItem(VideoRedundanciesListComponent.LOCAL_STORAGE_DISPLAY_TYPE)
186 if (displayType) this.displayType = displayType as VideoRedundanciesTarget
189 private saveSelectLocalStorage () {
190 peertubeLocalStorage.setItem(VideoRedundanciesListComponent.LOCAL_STORAGE_DISPLAY_TYPE, this.displayType)
193 private bytesToHuman (bytes: number) {
194 return this.bytesPipe.transform(bytes, 1)