PeerTube/client/src/app/+admin/follows/video-redundancies-list/video-redundancies-list.component.ts
Chocobozzz 3a4992633e
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:

 * Server can be faster at startup because imports() are async and we can
   easily lazy import big modules
 * Angular doesn't seem to support ES import (with .js extension), so we
   had to correctly organize peertube into a monorepo:
    * Use yarn workspace feature
    * Use typescript reference projects for dependencies
    * Shared projects have been moved into "packages", each one is now a
      node module (with a dedicated package.json/tsconfig.json)
    * server/tools have been moved into apps/ and is now a dedicated app
      bundled and published on NPM so users don't have to build peertube
      cli tools manually
    * server/tests have been moved into packages/ so we don't compile
      them every time we want to run the server
 * Use isolatedModule option:
   * Had to move from const enum to const
     (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
   * Had to explictely specify "type" imports when used in decorators
 * Prefer tsx (that uses esbuild under the hood) instead of ts-node to
   load typescript files (tests with mocha or scripts):
     * To reduce test complexity as esbuild doesn't support decorator
       metadata, we only test server files that do not import server
       models
     * We still build tests files into js files for a faster CI
 * Remove unmaintained peertube CLI import script
 * Removed some barrels to speed up execution (less imports)
2023-08-11 15:02:33 +02:00

196 lines
5.4 KiB
TypeScript

import { ChartData, ChartOptions, TooltipItem } from 'chart.js'
import { SortMeta } from 'primeng/api'
import { Component, OnInit } from '@angular/core'
import { ConfirmService, Notifier, RestPagination, RestTable, ServerService } from '@app/core'
import { BytesPipe, RedundancyService } from '@app/shared/shared-main'
import { VideoRedundanciesTarget, VideoRedundancy, VideosRedundancyStats } from '@peertube/peertube-models'
import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
@Component({
selector: 'my-video-redundancies-list',
templateUrl: './video-redundancies-list.component.html',
styleUrls: [ './video-redundancies-list.component.scss' ]
})
export class VideoRedundanciesListComponent extends RestTable implements OnInit {
private static LOCAL_STORAGE_DISPLAY_TYPE = 'video-redundancies-list-display-type'
videoRedundancies: VideoRedundancy[] = []
totalRecords = 0
sort: SortMeta = { field: 'name', order: 1 }
pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
displayType: VideoRedundanciesTarget = 'my-videos'
redundanciesGraphsData: { stats: VideosRedundancyStats, graphData: ChartData, options: ChartOptions }[] = []
noRedundancies = false
// Prevent layout shift for redundancy stats
dataLoaded = false
private bytesPipe: BytesPipe
constructor (
private notifier: Notifier,
private confirmService: ConfirmService,
private redundancyService: RedundancyService,
private serverService: ServerService
) {
super()
this.bytesPipe = new BytesPipe()
}
getIdentifier () {
return 'VideoRedundanciesListComponent'
}
ngOnInit () {
this.loadSelectLocalStorage()
this.initialize()
this.serverService.getServerStats()
.subscribe(res => {
const redundancies = res.videosRedundancy
if (redundancies.length === 0) this.noRedundancies = true
for (const r of redundancies) {
this.buildPieData(r)
}
})
}
isDisplayingRemoteVideos () {
return this.displayType === 'remote-videos'
}
getTotalSize (redundancy: VideoRedundancy) {
return redundancy.redundancies.files.reduce((a, b) => a + b.size, 0) +
redundancy.redundancies.streamingPlaylists.reduce((a, b) => a + b.size, 0)
}
onDisplayTypeChanged () {
this.pagination.start = 0
this.saveSelectLocalStorage()
this.reloadData()
}
getRedundancyStrategy (redundancy: VideoRedundancy) {
if (redundancy.redundancies.files.length !== 0) return redundancy.redundancies.files[0].strategy
if (redundancy.redundancies.streamingPlaylists.length !== 0) return redundancy.redundancies.streamingPlaylists[0].strategy
return ''
}
buildPieData (stats: VideosRedundancyStats) {
if (stats.totalSize === 0) return
const totalAvailable = stats.totalSize
? stats.totalSize - stats.totalUsed
: null
const labels = [ $localize`Used (${this.bytesToHuman(stats.totalUsed)})` ]
const data = [ stats.totalUsed ]
// Not in manual strategy
if (totalAvailable) {
labels.push(
$localize`Available (${this.bytesToHuman(totalAvailable)})`
)
data.push(totalAvailable)
}
this.redundanciesGraphsData.push({
stats,
graphData: {
labels,
datasets: [
{
data,
backgroundColor: [
'#FF6384',
'#36A2EB'
],
hoverBackgroundColor: [
'#FF6384',
'#36A2EB'
]
}
]
},
options: {
plugins: {
title: {
display: true,
text: stats.strategy
},
tooltip: {
callbacks: {
label: (tooltip: TooltipItem<any>) => {
return tooltip.label
}
}
}
}
}
})
}
async removeRedundancy (redundancy: VideoRedundancy) {
const message = $localize`Do you really want to remove this video redundancy?`
const res = await this.confirmService.confirm(message, $localize`Remove redundancy`)
if (res === false) return
this.redundancyService.removeVideoRedundancies(redundancy)
.subscribe({
next: () => {
this.notifier.success($localize`Video redundancies removed!`)
this.reloadData()
},
error: err => this.notifier.error(err.message)
})
}
protected reloadDataInternal () {
this.dataLoaded = false
const options = {
pagination: this.pagination,
sort: this.sort,
target: this.displayType
}
this.redundancyService.listVideoRedundancies(options)
.subscribe({
next: resultList => {
this.videoRedundancies = resultList.data
this.totalRecords = resultList.total
this.dataLoaded = true
},
error: err => this.notifier.error(err.message)
})
}
private loadSelectLocalStorage () {
const displayType = peertubeLocalStorage.getItem(VideoRedundanciesListComponent.LOCAL_STORAGE_DISPLAY_TYPE)
if (displayType) this.displayType = displayType as VideoRedundanciesTarget
}
private saveSelectLocalStorage () {
peertubeLocalStorage.setItem(VideoRedundanciesListComponent.LOCAL_STORAGE_DISPLAY_TYPE, this.displayType)
}
private bytesToHuman (bytes: number) {
return this.bytesPipe.transform(bytes, 1)
}
}