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