]>
Commit | Line | Data |
---|---|---|
b764380a | 1 | import { SortMeta } from 'primeng/api' |
67ed6552 C |
2 | import { Component, OnInit } from '@angular/core' |
3 | import { ConfirmService, Notifier, RestPagination, RestTable, ServerService } from '@app/core' | |
94676e63 | 4 | import { BytesPipe, RedundancyService } from '@app/shared/shared-main' |
94676e63 | 5 | import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' |
b764380a | 6 | import { VideoRedundanciesTarget, VideoRedundancy } from '@shared/models' |
b764380a | 7 | import { VideosRedundancyStats } from '@shared/models/server' |
b764380a C |
8 | |
9 | @Component({ | |
10 | selector: 'my-video-redundancies-list', | |
11 | templateUrl: './video-redundancies-list.component.html', | |
d3840613 | 12 | styleUrls: [ '../follows.component.scss', './video-redundancies-list.component.scss' ] |
b764380a C |
13 | }) |
14 | export class VideoRedundanciesListComponent extends RestTable implements OnInit { | |
15 | private static LOCAL_STORAGE_DISPLAY_TYPE = 'video-redundancies-list-display-type' | |
16 | ||
17 | videoRedundancies: VideoRedundancy[] = [] | |
18 | totalRecords = 0 | |
b764380a C |
19 | |
20 | sort: SortMeta = { field: 'name', order: 1 } | |
21 | pagination: RestPagination = { count: this.rowsPerPage, start: 0 } | |
22 | displayType: VideoRedundanciesTarget = 'my-videos' | |
23 | ||
24 | redundanciesGraphsData: { stats: VideosRedundancyStats, graphData: object, options: object }[] = [] | |
25 | ||
26 | noRedundancies = false | |
27 | ||
28 | private bytesPipe: BytesPipe | |
29 | ||
30 | constructor ( | |
31 | private notifier: Notifier, | |
32 | private confirmService: ConfirmService, | |
33 | private redundancyService: RedundancyService, | |
66357162 C |
34 | private serverService: ServerService |
35 | ) { | |
b764380a C |
36 | super() |
37 | ||
38 | this.bytesPipe = new BytesPipe() | |
39 | } | |
40 | ||
8e11a1b3 C |
41 | getIdentifier () { |
42 | return 'VideoRedundanciesListComponent' | |
43 | } | |
44 | ||
b764380a C |
45 | ngOnInit () { |
46 | this.loadSelectLocalStorage() | |
47 | ||
48 | this.initialize() | |
49 | ||
50 | this.serverService.getServerStats() | |
51 | .subscribe(res => { | |
52 | const redundancies = res.videosRedundancy | |
53 | ||
54 | if (redundancies.length === 0) this.noRedundancies = true | |
55 | ||
56 | for (const r of redundancies) { | |
57 | this.buildPieData(r) | |
58 | } | |
59 | }) | |
60 | } | |
61 | ||
b1f3b635 | 62 | getColspan () { |
2bc9bd08 | 63 | if (this.isDisplayingRemoteVideos()) return 5 |
b1f3b635 | 64 | |
2bc9bd08 | 65 | return 4 |
b1f3b635 C |
66 | } |
67 | ||
b764380a C |
68 | isDisplayingRemoteVideos () { |
69 | return this.displayType === 'remote-videos' | |
70 | } | |
71 | ||
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) | |
75 | } | |
76 | ||
77 | onDisplayTypeChanged () { | |
78 | this.pagination.start = 0 | |
79 | this.saveSelectLocalStorage() | |
80 | ||
81 | this.loadData() | |
82 | } | |
83 | ||
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 | |
87 | ||
88 | return '' | |
89 | } | |
90 | ||
91 | buildPieData (stats: VideosRedundancyStats) { | |
92 | const totalSize = stats.totalSize | |
93 | ? stats.totalSize - stats.totalUsed | |
94 | : stats.totalUsed | |
95 | ||
96 | if (totalSize === 0) return | |
97 | ||
98 | this.redundanciesGraphsData.push({ | |
99 | stats, | |
100 | graphData: { | |
66357162 | 101 | labels: [ $localize`Used`, $localize`Available` ], |
b764380a C |
102 | datasets: [ |
103 | { | |
104 | data: [ stats.totalUsed, totalSize ], | |
105 | backgroundColor: [ | |
106 | '#FF6384', | |
107 | '#36A2EB' | |
108 | ], | |
109 | hoverBackgroundColor: [ | |
110 | '#FF6384', | |
111 | '#36A2EB' | |
112 | ] | |
113 | } | |
114 | ] | |
115 | }, | |
116 | options: { | |
117 | title: { | |
118 | display: true, | |
119 | text: stats.strategy | |
120 | }, | |
121 | ||
122 | tooltips: { | |
123 | callbacks: { | |
124 | label: (tooltipItem: any, data: any) => { | |
125 | const dataset = data.datasets[tooltipItem.datasetIndex] | |
126 | let label = data.labels[tooltipItem.index] | |
127 | if (label) label += ': ' | |
128 | else label = '' | |
129 | ||
130 | label += this.bytesPipe.transform(dataset.data[tooltipItem.index], 1) | |
131 | return label | |
132 | } | |
133 | } | |
134 | } | |
135 | } | |
136 | }) | |
137 | } | |
138 | ||
139 | async removeRedundancy (redundancy: VideoRedundancy) { | |
66357162 C |
140 | const message = $localize`Do you really want to remove this video redundancy?` |
141 | const res = await this.confirmService.confirm(message, $localize`Remove redundancy`) | |
b764380a C |
142 | if (res === false) return |
143 | ||
144 | this.redundancyService.removeVideoRedundancies(redundancy) | |
145 | .subscribe( | |
146 | () => { | |
66357162 | 147 | this.notifier.success($localize`Video redundancies removed!`) |
b764380a C |
148 | this.loadData() |
149 | }, | |
150 | ||
151 | err => this.notifier.error(err.message) | |
152 | ) | |
153 | ||
154 | } | |
155 | ||
156 | protected loadData () { | |
157 | const options = { | |
158 | pagination: this.pagination, | |
159 | sort: this.sort, | |
160 | target: this.displayType | |
161 | } | |
162 | ||
163 | this.redundancyService.listVideoRedundancies(options) | |
164 | .subscribe( | |
165 | resultList => { | |
166 | this.videoRedundancies = resultList.data | |
167 | this.totalRecords = resultList.total | |
168 | }, | |
169 | ||
170 | err => this.notifier.error(err.message) | |
171 | ) | |
172 | } | |
173 | ||
174 | private loadSelectLocalStorage () { | |
175 | const displayType = peertubeLocalStorage.getItem(VideoRedundanciesListComponent.LOCAL_STORAGE_DISPLAY_TYPE) | |
176 | if (displayType) this.displayType = displayType as VideoRedundanciesTarget | |
177 | } | |
178 | ||
179 | private saveSelectLocalStorage () { | |
180 | peertubeLocalStorage.setItem(VideoRedundanciesListComponent.LOCAL_STORAGE_DISPLAY_TYPE, this.displayType) | |
181 | } | |
182 | } |