]>
Commit | Line | Data |
---|---|---|
b49f22d8 | 1 | import { sample } from 'lodash' |
6949a1a1 | 2 | import { FindOptions, literal, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize' |
c48e82b5 | 3 | import { |
c48e82b5 | 4 | AllowNull, |
25378bc8 | 5 | BeforeDestroy, |
c48e82b5 C |
6 | BelongsTo, |
7 | Column, | |
8 | CreatedAt, | |
9 | DataType, | |
10 | ForeignKey, | |
11 | Is, | |
12 | Model, | |
13 | Scopes, | |
c48e82b5 C |
14 | Table, |
15 | UpdatedAt | |
16 | } from 'sequelize-typescript' | |
b49f22d8 | 17 | import { getServerActor } from '@server/models/application/application' |
7448551f | 18 | import { MActor, MVideoForRedundancyAPI, MVideoRedundancy, MVideoRedundancyAP, MVideoRedundancyVideo } from '@server/types/models' |
b764380a C |
19 | import { VideoRedundanciesTarget } from '@shared/models/redundancy/video-redundancies-filters.model' |
20 | import { | |
21 | FileRedundancyInformation, | |
22 | StreamingPlaylistRedundancyInformation, | |
23 | VideoRedundancy | |
24 | } from '@shared/models/redundancy/video-redundancy.model' | |
b49f22d8 C |
25 | import { CacheFileObject, VideoPrivacy } from '../../../shared' |
26 | import { VideoRedundancyStrategy, VideoRedundancyStrategyWithManual } from '../../../shared/models/redundancy' | |
27 | import { isTestInstance } from '../../helpers/core-utils' | |
28 | import { isActivityPubUrlValid, isUrlValid } from '../../helpers/custom-validators/activitypub/misc' | |
29 | import { logger } from '../../helpers/logger' | |
30 | import { CONFIG } from '../../initializers/config' | |
31 | import { CONSTRAINTS_FIELDS, MIMETYPES } from '../../initializers/constants' | |
7d9ba5c0 | 32 | import { ActorModel } from '../actor/actor' |
b49f22d8 C |
33 | import { ServerModel } from '../server/server' |
34 | import { getSort, getVideoSort, parseAggregateResult, throwIfNotValid } from '../utils' | |
93544419 | 35 | import { ScheduleVideoUpdateModel } from '../video/schedule-video-update' |
b49f22d8 C |
36 | import { VideoModel } from '../video/video' |
37 | import { VideoChannelModel } from '../video/video-channel' | |
38 | import { VideoFileModel } from '../video/video-file' | |
39 | import { VideoStreamingPlaylistModel } from '../video/video-streaming-playlist' | |
c48e82b5 C |
40 | |
41 | export enum ScopeNames { | |
42 | WITH_VIDEO = 'WITH_VIDEO' | |
43 | } | |
44 | ||
3acc5084 | 45 | @Scopes(() => ({ |
a1587156 | 46 | [ScopeNames.WITH_VIDEO]: { |
c48e82b5 C |
47 | include: [ |
48 | { | |
3acc5084 | 49 | model: VideoFileModel, |
09209296 C |
50 | required: false, |
51 | include: [ | |
52 | { | |
3acc5084 | 53 | model: VideoModel, |
09209296 C |
54 | required: true |
55 | } | |
56 | ] | |
57 | }, | |
58 | { | |
3acc5084 | 59 | model: VideoStreamingPlaylistModel, |
09209296 | 60 | required: false, |
c48e82b5 C |
61 | include: [ |
62 | { | |
3acc5084 | 63 | model: VideoModel, |
c48e82b5 C |
64 | required: true |
65 | } | |
66 | ] | |
67 | } | |
3acc5084 | 68 | ] |
c48e82b5 | 69 | } |
3acc5084 | 70 | })) |
c48e82b5 C |
71 | |
72 | @Table({ | |
73 | tableName: 'videoRedundancy', | |
74 | indexes: [ | |
75 | { | |
76 | fields: [ 'videoFileId' ] | |
77 | }, | |
78 | { | |
79 | fields: [ 'actorId' ] | |
80 | }, | |
81 | { | |
82 | fields: [ 'url' ], | |
83 | unique: true | |
84 | } | |
85 | ] | |
86 | }) | |
b49f22d8 | 87 | export class VideoRedundancyModel extends Model { |
c48e82b5 C |
88 | |
89 | @CreatedAt | |
90 | createdAt: Date | |
91 | ||
92 | @UpdatedAt | |
93 | updatedAt: Date | |
94 | ||
b764380a | 95 | @AllowNull(true) |
c48e82b5 C |
96 | @Column |
97 | expiresOn: Date | |
98 | ||
99 | @AllowNull(false) | |
100 | @Is('VideoRedundancyFileUrl', value => throwIfNotValid(value, isUrlValid, 'fileUrl')) | |
101 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS_REDUNDANCY.URL.max)) | |
102 | fileUrl: string | |
103 | ||
104 | @AllowNull(false) | |
105 | @Is('VideoRedundancyUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) | |
106 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS_REDUNDANCY.URL.max)) | |
107 | url: string | |
108 | ||
109 | @AllowNull(true) | |
110 | @Column | |
111 | strategy: string // Only used by us | |
112 | ||
113 | @ForeignKey(() => VideoFileModel) | |
114 | @Column | |
115 | videoFileId: number | |
116 | ||
117 | @BelongsTo(() => VideoFileModel, { | |
118 | foreignKey: { | |
09209296 | 119 | allowNull: true |
c48e82b5 C |
120 | }, |
121 | onDelete: 'cascade' | |
122 | }) | |
123 | VideoFile: VideoFileModel | |
124 | ||
09209296 C |
125 | @ForeignKey(() => VideoStreamingPlaylistModel) |
126 | @Column | |
127 | videoStreamingPlaylistId: number | |
128 | ||
129 | @BelongsTo(() => VideoStreamingPlaylistModel, { | |
130 | foreignKey: { | |
131 | allowNull: true | |
132 | }, | |
133 | onDelete: 'cascade' | |
134 | }) | |
135 | VideoStreamingPlaylist: VideoStreamingPlaylistModel | |
136 | ||
c48e82b5 C |
137 | @ForeignKey(() => ActorModel) |
138 | @Column | |
139 | actorId: number | |
140 | ||
141 | @BelongsTo(() => ActorModel, { | |
142 | foreignKey: { | |
143 | allowNull: false | |
144 | }, | |
145 | onDelete: 'cascade' | |
146 | }) | |
147 | Actor: ActorModel | |
148 | ||
25378bc8 C |
149 | @BeforeDestroy |
150 | static async removeFile (instance: VideoRedundancyModel) { | |
8d1fa36a | 151 | if (!instance.isOwned()) return |
c48e82b5 | 152 | |
09209296 C |
153 | if (instance.videoFileId) { |
154 | const videoFile = await VideoFileModel.loadWithVideo(instance.videoFileId) | |
c48e82b5 | 155 | |
09209296 C |
156 | const logIdentifier = `${videoFile.Video.uuid}-${videoFile.resolution}` |
157 | logger.info('Removing duplicated video file %s.', logIdentifier) | |
25378bc8 | 158 | |
09209296 C |
159 | videoFile.Video.removeFile(videoFile, true) |
160 | .catch(err => logger.error('Cannot delete %s files.', logIdentifier, { err })) | |
161 | } | |
162 | ||
163 | if (instance.videoStreamingPlaylistId) { | |
164 | const videoStreamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(instance.videoStreamingPlaylistId) | |
165 | ||
166 | const videoUUID = videoStreamingPlaylist.Video.uuid | |
167 | logger.info('Removing duplicated video streaming playlist %s.', videoUUID) | |
168 | ||
ffc65cbd | 169 | videoStreamingPlaylist.Video.removeStreamingPlaylistFiles(videoStreamingPlaylist, true) |
a1587156 | 170 | .catch(err => logger.error('Cannot delete video streaming playlist files of %s.', videoUUID, { err })) |
09209296 | 171 | } |
d0ae9490 C |
172 | |
173 | return undefined | |
c48e82b5 C |
174 | } |
175 | ||
453e83ea | 176 | static async loadLocalByFileId (videoFileId: number): Promise<MVideoRedundancyVideo> { |
46f8d69b C |
177 | const actor = await getServerActor() |
178 | ||
c48e82b5 C |
179 | const query = { |
180 | where: { | |
46f8d69b | 181 | actorId: actor.id, |
c48e82b5 C |
182 | videoFileId |
183 | } | |
184 | } | |
185 | ||
186 | return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query) | |
187 | } | |
188 | ||
89613cb4 C |
189 | static async listLocalByVideoId (videoId: number): Promise<MVideoRedundancyVideo[]> { |
190 | const actor = await getServerActor() | |
191 | ||
192 | const queryStreamingPlaylist = { | |
193 | where: { | |
194 | actorId: actor.id | |
195 | }, | |
196 | include: [ | |
197 | { | |
198 | model: VideoStreamingPlaylistModel.unscoped(), | |
199 | required: true, | |
200 | include: [ | |
201 | { | |
202 | model: VideoModel.unscoped(), | |
203 | required: true, | |
204 | where: { | |
205 | id: videoId | |
206 | } | |
207 | } | |
208 | ] | |
209 | } | |
210 | ] | |
211 | } | |
212 | ||
213 | const queryFiles = { | |
214 | where: { | |
215 | actorId: actor.id | |
216 | }, | |
217 | include: [ | |
218 | { | |
219 | model: VideoFileModel, | |
220 | required: true, | |
221 | include: [ | |
222 | { | |
223 | model: VideoModel, | |
224 | required: true, | |
225 | where: { | |
226 | id: videoId | |
227 | } | |
228 | } | |
229 | ] | |
230 | } | |
231 | ] | |
232 | } | |
233 | ||
234 | return Promise.all([ | |
235 | VideoRedundancyModel.findAll(queryStreamingPlaylist), | |
236 | VideoRedundancyModel.findAll(queryFiles) | |
237 | ]).then(([ r1, r2 ]) => r1.concat(r2)) | |
238 | } | |
239 | ||
453e83ea | 240 | static async loadLocalByStreamingPlaylistId (videoStreamingPlaylistId: number): Promise<MVideoRedundancyVideo> { |
09209296 C |
241 | const actor = await getServerActor() |
242 | ||
243 | const query = { | |
244 | where: { | |
245 | actorId: actor.id, | |
246 | videoStreamingPlaylistId | |
247 | } | |
248 | } | |
249 | ||
250 | return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query) | |
251 | } | |
252 | ||
b49f22d8 | 253 | static loadByIdWithVideo (id: number, transaction?: Transaction): Promise<MVideoRedundancyVideo> { |
b764380a C |
254 | const query = { |
255 | where: { id }, | |
256 | transaction | |
257 | } | |
258 | ||
259 | return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query) | |
260 | } | |
261 | ||
b49f22d8 | 262 | static loadByUrl (url: string, transaction?: Transaction): Promise<MVideoRedundancy> { |
c48e82b5 C |
263 | const query = { |
264 | where: { | |
265 | url | |
e5565833 C |
266 | }, |
267 | transaction | |
c48e82b5 C |
268 | } |
269 | ||
270 | return VideoRedundancyModel.findOne(query) | |
271 | } | |
272 | ||
5ce1208a C |
273 | static async isLocalByVideoUUIDExists (uuid: string) { |
274 | const actor = await getServerActor() | |
275 | ||
276 | const query = { | |
277 | raw: true, | |
278 | attributes: [ 'id' ], | |
279 | where: { | |
280 | actorId: actor.id | |
281 | }, | |
282 | include: [ | |
283 | { | |
a1587156 | 284 | attributes: [], |
5ce1208a C |
285 | model: VideoFileModel, |
286 | required: true, | |
287 | include: [ | |
288 | { | |
a1587156 | 289 | attributes: [], |
5ce1208a C |
290 | model: VideoModel, |
291 | required: true, | |
292 | where: { | |
293 | uuid | |
294 | } | |
295 | } | |
296 | ] | |
297 | } | |
298 | ] | |
299 | } | |
300 | ||
301 | return VideoRedundancyModel.findOne(query) | |
a1587156 | 302 | .then(r => !!r) |
5ce1208a C |
303 | } |
304 | ||
b49f22d8 | 305 | static async getVideoSample (p: Promise<VideoModel[]>) { |
3f6b6a56 | 306 | const rows = await p |
9e3e3617 C |
307 | if (rows.length === 0) return undefined |
308 | ||
b36f41ca C |
309 | const ids = rows.map(r => r.id) |
310 | const id = sample(ids) | |
311 | ||
09209296 | 312 | return VideoModel.loadWithFiles(id, undefined, !isTestInstance()) |
b36f41ca C |
313 | } |
314 | ||
c48e82b5 | 315 | static async findMostViewToDuplicate (randomizedFactor: number) { |
7448551f C |
316 | const peertubeActor = await getServerActor() |
317 | ||
c48e82b5 C |
318 | // On VideoModel! |
319 | const query = { | |
b36f41ca | 320 | attributes: [ 'id', 'views' ], |
c48e82b5 | 321 | limit: randomizedFactor, |
b36f41ca | 322 | order: getVideoSort('-views'), |
4a08f669 | 323 | where: { |
9e2b2e76 | 324 | privacy: VideoPrivacy.PUBLIC, |
7448551f C |
325 | isLive: false, |
326 | ...this.buildVideoIdsForDuplication(peertubeActor) | |
4a08f669 | 327 | }, |
c48e82b5 | 328 | include: [ |
b36f41ca C |
329 | VideoRedundancyModel.buildServerRedundancyInclude() |
330 | ] | |
331 | } | |
332 | ||
3f6b6a56 | 333 | return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query)) |
b36f41ca C |
334 | } |
335 | ||
336 | static async findTrendingToDuplicate (randomizedFactor: number) { | |
7448551f C |
337 | const peertubeActor = await getServerActor() |
338 | ||
b36f41ca C |
339 | // On VideoModel! |
340 | const query = { | |
341 | attributes: [ 'id', 'views' ], | |
342 | subQuery: false, | |
b36f41ca C |
343 | group: 'VideoModel.id', |
344 | limit: randomizedFactor, | |
345 | order: getVideoSort('-trending'), | |
4a08f669 | 346 | where: { |
9e2b2e76 | 347 | privacy: VideoPrivacy.PUBLIC, |
7448551f C |
348 | isLive: false, |
349 | ...this.buildVideoIdsForDuplication(peertubeActor) | |
4a08f669 | 350 | }, |
b36f41ca | 351 | include: [ |
b36f41ca C |
352 | VideoRedundancyModel.buildServerRedundancyInclude(), |
353 | ||
354 | VideoModel.buildTrendingQuery(CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS) | |
c48e82b5 C |
355 | ] |
356 | } | |
357 | ||
3f6b6a56 C |
358 | return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query)) |
359 | } | |
360 | ||
361 | static async findRecentlyAddedToDuplicate (randomizedFactor: number, minViews: number) { | |
7448551f C |
362 | const peertubeActor = await getServerActor() |
363 | ||
3f6b6a56 C |
364 | // On VideoModel! |
365 | const query = { | |
366 | attributes: [ 'id', 'publishedAt' ], | |
3f6b6a56 C |
367 | limit: randomizedFactor, |
368 | order: getVideoSort('-publishedAt'), | |
369 | where: { | |
4a08f669 | 370 | privacy: VideoPrivacy.PUBLIC, |
9e2b2e76 | 371 | isLive: false, |
3f6b6a56 | 372 | views: { |
a1587156 | 373 | [Op.gte]: minViews |
7448551f C |
374 | }, |
375 | ...this.buildVideoIdsForDuplication(peertubeActor) | |
3f6b6a56 C |
376 | }, |
377 | include: [ | |
93544419 C |
378 | VideoRedundancyModel.buildServerRedundancyInclude(), |
379 | ||
380 | // Required by publishedAt sort | |
381 | { | |
382 | model: ScheduleVideoUpdateModel.unscoped(), | |
383 | required: false | |
384 | } | |
3f6b6a56 C |
385 | ] |
386 | } | |
c48e82b5 | 387 | |
3f6b6a56 | 388 | return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query)) |
c48e82b5 C |
389 | } |
390 | ||
453e83ea | 391 | static async loadOldestLocalExpired (strategy: VideoRedundancyStrategy, expiresAfterMs: number): Promise<MVideoRedundancyVideo> { |
e5565833 C |
392 | const expiredDate = new Date() |
393 | expiredDate.setMilliseconds(expiredDate.getMilliseconds() - expiresAfterMs) | |
394 | ||
395 | const actor = await getServerActor() | |
396 | ||
397 | const query = { | |
398 | where: { | |
399 | actorId: actor.id, | |
400 | strategy, | |
401 | createdAt: { | |
a1587156 | 402 | [Op.lt]: expiredDate |
e5565833 C |
403 | } |
404 | } | |
405 | } | |
406 | ||
407 | return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findOne(query) | |
408 | } | |
409 | ||
3f6b6a56 | 410 | static async getTotalDuplicated (strategy: VideoRedundancyStrategy) { |
c48e82b5 | 411 | const actor = await getServerActor() |
0b353d1d C |
412 | const redundancyInclude = { |
413 | attributes: [], | |
414 | model: VideoRedundancyModel, | |
415 | required: true, | |
416 | where: { | |
417 | actorId: actor.id, | |
418 | strategy | |
419 | } | |
420 | } | |
421 | ||
422 | const queryFiles: FindOptions = { | |
423 | include: [ redundancyInclude ] | |
424 | } | |
c48e82b5 | 425 | |
0b353d1d | 426 | const queryStreamingPlaylists: FindOptions = { |
3f6b6a56 C |
427 | include: [ |
428 | { | |
429 | attributes: [], | |
0b353d1d | 430 | model: VideoModel.unscoped(), |
3f6b6a56 | 431 | required: true, |
0b353d1d C |
432 | include: [ |
433 | { | |
9e3e3617 | 434 | required: true, |
0b353d1d C |
435 | attributes: [], |
436 | model: VideoStreamingPlaylistModel.unscoped(), | |
437 | include: [ | |
438 | redundancyInclude | |
439 | ] | |
440 | } | |
441 | ] | |
3f6b6a56 C |
442 | } |
443 | ] | |
c48e82b5 C |
444 | } |
445 | ||
0b353d1d C |
446 | return Promise.all([ |
447 | VideoFileModel.aggregate('size', 'SUM', queryFiles), | |
448 | VideoFileModel.aggregate('size', 'SUM', queryStreamingPlaylists) | |
449 | ]).then(([ r1, r2 ]) => { | |
450 | return parseAggregateResult(r1) + parseAggregateResult(r2) | |
451 | }) | |
c48e82b5 C |
452 | } |
453 | ||
e5565833 C |
454 | static async listLocalExpired () { |
455 | const actor = await getServerActor() | |
456 | ||
457 | const query = { | |
458 | where: { | |
459 | actorId: actor.id, | |
460 | expiresOn: { | |
a1587156 | 461 | [Op.lt]: new Date() |
e5565833 C |
462 | } |
463 | } | |
464 | } | |
465 | ||
466 | return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findAll(query) | |
467 | } | |
468 | ||
469 | static async listRemoteExpired () { | |
470 | const actor = await getServerActor() | |
471 | ||
c48e82b5 | 472 | const query = { |
c48e82b5 | 473 | where: { |
e5565833 | 474 | actorId: { |
3acc5084 | 475 | [Op.ne]: actor.id |
e5565833 | 476 | }, |
c48e82b5 | 477 | expiresOn: { |
a1587156 C |
478 | [Op.lt]: new Date(), |
479 | [Op.ne]: null | |
c48e82b5 C |
480 | } |
481 | } | |
482 | } | |
483 | ||
e5565833 | 484 | return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findAll(query) |
c48e82b5 C |
485 | } |
486 | ||
161b061d C |
487 | static async listLocalOfServer (serverId: number) { |
488 | const actor = await getServerActor() | |
09209296 C |
489 | const buildVideoInclude = () => ({ |
490 | model: VideoModel, | |
491 | required: true, | |
161b061d C |
492 | include: [ |
493 | { | |
09209296 C |
494 | attributes: [], |
495 | model: VideoChannelModel.unscoped(), | |
161b061d C |
496 | required: true, |
497 | include: [ | |
498 | { | |
09209296 C |
499 | attributes: [], |
500 | model: ActorModel.unscoped(), | |
161b061d | 501 | required: true, |
09209296 C |
502 | where: { |
503 | serverId | |
504 | } | |
161b061d C |
505 | } |
506 | ] | |
507 | } | |
508 | ] | |
09209296 C |
509 | }) |
510 | ||
511 | const query = { | |
512 | where: { | |
513 | actorId: actor.id | |
514 | }, | |
515 | include: [ | |
516 | { | |
517 | model: VideoFileModel, | |
518 | required: false, | |
519 | include: [ buildVideoInclude() ] | |
520 | }, | |
521 | { | |
522 | model: VideoStreamingPlaylistModel, | |
523 | required: false, | |
524 | include: [ buildVideoInclude() ] | |
525 | } | |
526 | ] | |
161b061d C |
527 | } |
528 | ||
529 | return VideoRedundancyModel.findAll(query) | |
530 | } | |
531 | ||
b764380a | 532 | static listForApi (options: { |
a1587156 C |
533 | start: number |
534 | count: number | |
535 | sort: string | |
536 | target: VideoRedundanciesTarget | |
b764380a C |
537 | strategy?: string |
538 | }) { | |
539 | const { start, count, sort, target, strategy } = options | |
a1587156 C |
540 | const redundancyWhere: WhereOptions = {} |
541 | const videosWhere: WhereOptions = {} | |
b764380a C |
542 | let redundancySqlSuffix = '' |
543 | ||
544 | if (target === 'my-videos') { | |
545 | Object.assign(videosWhere, { remote: false }) | |
546 | } else if (target === 'remote-videos') { | |
547 | Object.assign(videosWhere, { remote: true }) | |
548 | Object.assign(redundancyWhere, { strategy: { [Op.ne]: null } }) | |
549 | redundancySqlSuffix = ' AND "videoRedundancy"."strategy" IS NOT NULL' | |
550 | } | |
551 | ||
552 | if (strategy) { | |
553 | Object.assign(redundancyWhere, { strategy: strategy }) | |
554 | } | |
555 | ||
556 | const videoFilterWhere = { | |
557 | [Op.and]: [ | |
558 | { | |
a1587156 | 559 | [Op.or]: [ |
b764380a C |
560 | { |
561 | id: { | |
a1587156 | 562 | [Op.in]: literal( |
b764380a C |
563 | '(' + |
564 | 'SELECT "videoId" FROM "videoFile" ' + | |
565 | 'INNER JOIN "videoRedundancy" ON "videoRedundancy"."videoFileId" = "videoFile".id' + | |
566 | redundancySqlSuffix + | |
567 | ')' | |
568 | ) | |
569 | } | |
570 | }, | |
571 | { | |
572 | id: { | |
a1587156 | 573 | [Op.in]: literal( |
b764380a C |
574 | '(' + |
575 | 'select "videoId" FROM "videoStreamingPlaylist" ' + | |
576 | 'INNER JOIN "videoRedundancy" ON "videoRedundancy"."videoStreamingPlaylistId" = "videoStreamingPlaylist".id' + | |
577 | redundancySqlSuffix + | |
578 | ')' | |
579 | ) | |
580 | } | |
581 | } | |
582 | ] | |
583 | }, | |
584 | ||
585 | videosWhere | |
586 | ] | |
587 | } | |
588 | ||
589 | // /!\ On video model /!\ | |
590 | const findOptions = { | |
591 | offset: start, | |
592 | limit: count, | |
593 | order: getSort(sort), | |
594 | include: [ | |
595 | { | |
596 | required: false, | |
8319d6ae | 597 | model: VideoFileModel, |
b764380a C |
598 | include: [ |
599 | { | |
600 | model: VideoRedundancyModel.unscoped(), | |
601 | required: false, | |
602 | where: redundancyWhere | |
603 | } | |
604 | ] | |
605 | }, | |
606 | { | |
607 | required: false, | |
608 | model: VideoStreamingPlaylistModel.unscoped(), | |
609 | include: [ | |
610 | { | |
611 | model: VideoRedundancyModel.unscoped(), | |
612 | required: false, | |
613 | where: redundancyWhere | |
614 | }, | |
615 | { | |
8319d6ae | 616 | model: VideoFileModel, |
b764380a C |
617 | required: false |
618 | } | |
619 | ] | |
620 | } | |
621 | ], | |
622 | where: videoFilterWhere | |
623 | } | |
624 | ||
625 | // /!\ On video model /!\ | |
626 | const countOptions = { | |
627 | where: videoFilterWhere | |
628 | } | |
629 | ||
630 | return Promise.all([ | |
631 | VideoModel.findAll(findOptions), | |
632 | ||
633 | VideoModel.count(countOptions) | |
634 | ]).then(([ data, total ]) => ({ total, data })) | |
635 | } | |
636 | ||
637 | static async getStats (strategy: VideoRedundancyStrategyWithManual) { | |
4b5384f6 C |
638 | const actor = await getServerActor() |
639 | ||
7448551f C |
640 | const sql = `WITH "tmp" AS ` + |
641 | `(` + | |
642 | `SELECT "videoFile"."size" AS "videoFileSize", "videoStreamingFile"."size" AS "videoStreamingFileSize", ` + | |
643 | `"videoFile"."videoId" AS "videoFileVideoId", "videoStreamingPlaylist"."videoId" AS "videoStreamingVideoId"` + | |
644 | `FROM "videoRedundancy" AS "videoRedundancy" ` + | |
645 | `LEFT JOIN "videoFile" AS "videoFile" ON "videoRedundancy"."videoFileId" = "videoFile"."id" ` + | |
646 | `LEFT JOIN "videoStreamingPlaylist" ON "videoRedundancy"."videoStreamingPlaylistId" = "videoStreamingPlaylist"."id" ` + | |
647 | `LEFT JOIN "videoFile" AS "videoStreamingFile" ` + | |
648 | `ON "videoStreamingPlaylist"."id" = "videoStreamingFile"."videoStreamingPlaylistId" ` + | |
649 | `WHERE "videoRedundancy"."strategy" = :strategy AND "videoRedundancy"."actorId" = :actorId` + | |
650 | `), ` + | |
651 | `"videoIds" AS (` + | |
652 | `SELECT "videoFileVideoId" AS "videoId" FROM "tmp" ` + | |
653 | `UNION SELECT "videoStreamingVideoId" AS "videoId" FROM "tmp" ` + | |
654 | `) ` + | |
655 | `SELECT ` + | |
656 | `COALESCE(SUM("videoFileSize"), '0') + COALESCE(SUM("videoStreamingFileSize"), '0') AS "totalUsed", ` + | |
657 | `(SELECT COUNT("videoIds"."videoId") FROM "videoIds") AS "totalVideos", ` + | |
658 | `COUNT(*) AS "totalVideoFiles" ` + | |
659 | `FROM "tmp"` | |
660 | ||
661 | return VideoRedundancyModel.sequelize.query<any>(sql, { | |
662 | replacements: { strategy, actorId: actor.id }, | |
663 | type: QueryTypes.SELECT | |
664 | }).then(([ row ]) => ({ | |
665 | totalUsed: parseAggregateResult(row.totalUsed), | |
666 | totalVideos: row.totalVideos, | |
667 | totalVideoFiles: row.totalVideoFiles | |
668 | })) | |
4b5384f6 C |
669 | } |
670 | ||
b764380a | 671 | static toFormattedJSONStatic (video: MVideoForRedundancyAPI): VideoRedundancy { |
a1587156 C |
672 | const filesRedundancies: FileRedundancyInformation[] = [] |
673 | const streamingPlaylistsRedundancies: StreamingPlaylistRedundancyInformation[] = [] | |
b764380a C |
674 | |
675 | for (const file of video.VideoFiles) { | |
676 | for (const redundancy of file.RedundancyVideos) { | |
677 | filesRedundancies.push({ | |
678 | id: redundancy.id, | |
679 | fileUrl: redundancy.fileUrl, | |
680 | strategy: redundancy.strategy, | |
681 | createdAt: redundancy.createdAt, | |
682 | updatedAt: redundancy.updatedAt, | |
683 | expiresOn: redundancy.expiresOn, | |
684 | size: file.size | |
685 | }) | |
686 | } | |
687 | } | |
688 | ||
689 | for (const playlist of video.VideoStreamingPlaylists) { | |
690 | const size = playlist.VideoFiles.reduce((a, b) => a + b.size, 0) | |
691 | ||
692 | for (const redundancy of playlist.RedundancyVideos) { | |
693 | streamingPlaylistsRedundancies.push({ | |
694 | id: redundancy.id, | |
695 | fileUrl: redundancy.fileUrl, | |
696 | strategy: redundancy.strategy, | |
697 | createdAt: redundancy.createdAt, | |
698 | updatedAt: redundancy.updatedAt, | |
699 | expiresOn: redundancy.expiresOn, | |
700 | size | |
701 | }) | |
702 | } | |
703 | } | |
704 | ||
705 | return { | |
706 | id: video.id, | |
707 | name: video.name, | |
708 | url: video.url, | |
709 | uuid: video.uuid, | |
710 | ||
711 | redundancies: { | |
712 | files: filesRedundancies, | |
713 | streamingPlaylists: streamingPlaylistsRedundancies | |
714 | } | |
715 | } | |
716 | } | |
717 | ||
09209296 | 718 | getVideo () { |
2e1e4af0 | 719 | if (this.VideoFile?.Video) return this.VideoFile.Video |
09209296 | 720 | |
2e1e4af0 | 721 | if (this.VideoStreamingPlaylist?.Video) return this.VideoStreamingPlaylist.Video |
9cfeb3cf C |
722 | |
723 | return undefined | |
09209296 C |
724 | } |
725 | ||
8d1fa36a C |
726 | isOwned () { |
727 | return !!this.strategy | |
728 | } | |
729 | ||
b5fecbf4 | 730 | toActivityPubObject (this: MVideoRedundancyAP): CacheFileObject { |
09209296 C |
731 | if (this.VideoStreamingPlaylist) { |
732 | return { | |
733 | id: this.url, | |
734 | type: 'CacheFile' as 'CacheFile', | |
735 | object: this.VideoStreamingPlaylist.Video.url, | |
b764380a | 736 | expires: this.expiresOn ? this.expiresOn.toISOString() : null, |
09209296 C |
737 | url: { |
738 | type: 'Link', | |
09209296 C |
739 | mediaType: 'application/x-mpegURL', |
740 | href: this.fileUrl | |
741 | } | |
742 | } | |
743 | } | |
744 | ||
c48e82b5 C |
745 | return { |
746 | id: this.url, | |
747 | type: 'CacheFile' as 'CacheFile', | |
748 | object: this.VideoFile.Video.url, | |
b764380a | 749 | expires: this.expiresOn ? this.expiresOn.toISOString() : null, |
c48e82b5 C |
750 | url: { |
751 | type: 'Link', | |
a1587156 | 752 | mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[this.VideoFile.extname] as any, |
c48e82b5 C |
753 | href: this.fileUrl, |
754 | height: this.VideoFile.resolution, | |
755 | size: this.VideoFile.size, | |
756 | fps: this.VideoFile.fps | |
757 | } | |
758 | } | |
759 | } | |
760 | ||
b36f41ca | 761 | // Don't include video files we already duplicated |
7448551f | 762 | private static buildVideoIdsForDuplication (peertubeActor: MActor) { |
3acc5084 | 763 | const notIn = literal( |
c48e82b5 | 764 | '(' + |
7448551f C |
765 | `SELECT "videoFile"."videoId" AS "videoId" FROM "videoRedundancy" ` + |
766 | `INNER JOIN "videoFile" ON "videoFile"."id" = "videoRedundancy"."videoFileId" ` + | |
767 | `WHERE "videoRedundancy"."actorId" = ${peertubeActor.id} ` + | |
768 | `UNION ` + | |
769 | `SELECT "videoStreamingPlaylist"."videoId" AS "videoId" FROM "videoRedundancy" ` + | |
770 | `INNER JOIN "videoStreamingPlaylist" ON "videoStreamingPlaylist"."id" = "videoRedundancy"."videoStreamingPlaylistId" ` + | |
771 | `WHERE "videoRedundancy"."actorId" = ${peertubeActor.id} ` + | |
c48e82b5 C |
772 | ')' |
773 | ) | |
b36f41ca C |
774 | |
775 | return { | |
7448551f C |
776 | id: { |
777 | [Op.notIn]: notIn | |
b36f41ca C |
778 | } |
779 | } | |
780 | } | |
781 | ||
782 | private static buildServerRedundancyInclude () { | |
783 | return { | |
784 | attributes: [], | |
785 | model: VideoChannelModel.unscoped(), | |
786 | required: true, | |
787 | include: [ | |
788 | { | |
789 | attributes: [], | |
790 | model: ActorModel.unscoped(), | |
791 | required: true, | |
792 | include: [ | |
793 | { | |
794 | attributes: [], | |
795 | model: ServerModel.unscoped(), | |
796 | required: true, | |
797 | where: { | |
798 | redundancyAllowed: true | |
799 | } | |
800 | } | |
801 | ] | |
802 | } | |
803 | ] | |
804 | } | |
c48e82b5 C |
805 | } |
806 | } |