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: [ '../follows.component.scss', './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 private bytesPipe: BytesPipe
32 private notifier: Notifier,
33 private confirmService: ConfirmService,
34 private redundancyService: RedundancyService,
35 private serverService: ServerService
39 this.bytesPipe = new BytesPipe()
43 return 'VideoRedundanciesListComponent'
47 this.loadSelectLocalStorage()
51 this.serverService.getServerStats()
53 const redundancies = res.videosRedundancy
55 if (redundancies.length === 0) this.noRedundancies = true
57 for (const r of redundancies) {
64 if (this.isDisplayingRemoteVideos()) return 5
69 isDisplayingRemoteVideos () {
70 return this.displayType === 'remote-videos'
73 getTotalSize (redundancy: VideoRedundancy) {
74 return redundancy.redundancies.files.reduce((a, b) => a + b.size, 0) +
75 redundancy.redundancies.streamingPlaylists.reduce((a, b) => a + b.size, 0)
78 onDisplayTypeChanged () {
79 this.pagination.start = 0
80 this.saveSelectLocalStorage()
85 getRedundancyStrategy (redundancy: VideoRedundancy) {
86 if (redundancy.redundancies.files.length !== 0) return redundancy.redundancies.files[0].strategy
87 if (redundancy.redundancies.streamingPlaylists.length !== 0) return redundancy.redundancies.streamingPlaylists[0].strategy
92 buildPieData (stats: VideosRedundancyStats) {
93 const totalSize = stats.totalSize
94 ? stats.totalSize - stats.totalUsed
97 if (totalSize === 0) return
99 this.redundanciesGraphsData.push({
102 labels: [ $localize`Used`, $localize`Available` ],
105 data: [ stats.totalUsed, totalSize ],
110 hoverBackgroundColor: [
126 label: (tooltip: TooltipItem<any>) => {
127 let label = tooltip.label || ''
128 if (label) label += ': '
130 label += this.bytesPipe.transform(tooltip.raw as number, 1)
141 async removeRedundancy (redundancy: VideoRedundancy) {
142 const message = $localize`Do you really want to remove this video redundancy?`
143 const res = await this.confirmService.confirm(message, $localize`Remove redundancy`)
144 if (res === false) return
146 this.redundancyService.removeVideoRedundancies(redundancy)
149 this.notifier.success($localize`Video redundancies removed!`)
153 error: err => this.notifier.error(err.message)
158 protected reloadData () {
160 pagination: this.pagination,
162 target: this.displayType
165 this.redundancyService.listVideoRedundancies(options)
167 next: resultList => {
168 this.videoRedundancies = resultList.data
169 this.totalRecords = resultList.total
172 error: err => this.notifier.error(err.message)
176 private loadSelectLocalStorage () {
177 const displayType = peertubeLocalStorage.getItem(VideoRedundanciesListComponent.LOCAL_STORAGE_DISPLAY_TYPE)
178 if (displayType) this.displayType = displayType as VideoRedundanciesTarget
181 private saveSelectLocalStorage () {
182 peertubeLocalStorage.setItem(VideoRedundanciesListComponent.LOCAL_STORAGE_DISPLAY_TYPE, this.displayType)