]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/redundancy/video-redundancy.ts
a61c3578c97311e30a553485fbef2a1f4e4c6550
[github/Chocobozzz/PeerTube.git] / server / models / redundancy / video-redundancy.ts
1 import { sample } from 'lodash'
2 import { FindOptions, literal, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize'
3 import {
4 AllowNull,
5 BeforeDestroy,
6 BelongsTo,
7 Column,
8 CreatedAt,
9 DataType,
10 ForeignKey,
11 Is,
12 Model,
13 Scopes,
14 Table,
15 UpdatedAt
16 } from 'sequelize-typescript'
17 import { getServerActor } from '@server/models/application/application'
18 import { MActor, MVideoForRedundancyAPI, MVideoRedundancy, MVideoRedundancyAP, MVideoRedundancyVideo } from '@server/types/models'
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'
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'
32 import { ActorModel } from '../activitypub/actor'
33 import { ServerModel } from '../server/server'
34 import { getSort, getVideoSort, parseAggregateResult, throwIfNotValid } from '../utils'
35 import { ScheduleVideoUpdateModel } from '../video/schedule-video-update'
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'
40
41 export enum ScopeNames {
42 WITH_VIDEO = 'WITH_VIDEO'
43 }
44
45 @Scopes(() => ({
46 [ScopeNames.WITH_VIDEO]: {
47 include: [
48 {
49 model: VideoFileModel,
50 required: false,
51 include: [
52 {
53 model: VideoModel,
54 required: true
55 }
56 ]
57 },
58 {
59 model: VideoStreamingPlaylistModel,
60 required: false,
61 include: [
62 {
63 model: VideoModel,
64 required: true
65 }
66 ]
67 }
68 ]
69 }
70 }))
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 })
87 export class VideoRedundancyModel extends Model {
88
89 @CreatedAt
90 createdAt: Date
91
92 @UpdatedAt
93 updatedAt: Date
94
95 @AllowNull(true)
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: {
119 allowNull: true
120 },
121 onDelete: 'cascade'
122 })
123 VideoFile: VideoFileModel
124
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
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
149 @BeforeDestroy
150 static async removeFile (instance: VideoRedundancyModel) {
151 if (!instance.isOwned()) return
152
153 if (instance.videoFileId) {
154 const videoFile = await VideoFileModel.loadWithVideo(instance.videoFileId)
155
156 const logIdentifier = `${videoFile.Video.uuid}-${videoFile.resolution}`
157 logger.info('Removing duplicated video file %s.', logIdentifier)
158
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
169 videoStreamingPlaylist.Video.removeStreamingPlaylistFiles(videoStreamingPlaylist, true)
170 .catch(err => logger.error('Cannot delete video streaming playlist files of %s.', videoUUID, { err }))
171 }
172
173 return undefined
174 }
175
176 static async loadLocalByFileId (videoFileId: number): Promise<MVideoRedundancyVideo> {
177 const actor = await getServerActor()
178
179 const query = {
180 where: {
181 actorId: actor.id,
182 videoFileId
183 }
184 }
185
186 return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query)
187 }
188
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
240 static async loadLocalByStreamingPlaylistId (videoStreamingPlaylistId: number): Promise<MVideoRedundancyVideo> {
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
253 static loadByIdWithVideo (id: number, transaction?: Transaction): Promise<MVideoRedundancyVideo> {
254 const query = {
255 where: { id },
256 transaction
257 }
258
259 return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query)
260 }
261
262 static loadByUrl (url: string, transaction?: Transaction): Promise<MVideoRedundancy> {
263 const query = {
264 where: {
265 url
266 },
267 transaction
268 }
269
270 return VideoRedundancyModel.findOne(query)
271 }
272
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 {
284 attributes: [],
285 model: VideoFileModel,
286 required: true,
287 include: [
288 {
289 attributes: [],
290 model: VideoModel,
291 required: true,
292 where: {
293 uuid
294 }
295 }
296 ]
297 }
298 ]
299 }
300
301 return VideoRedundancyModel.findOne(query)
302 .then(r => !!r)
303 }
304
305 static async getVideoSample (p: Promise<VideoModel[]>) {
306 const rows = await p
307 if (rows.length === 0) return undefined
308
309 const ids = rows.map(r => r.id)
310 const id = sample(ids)
311
312 return VideoModel.loadWithFiles(id, undefined, !isTestInstance())
313 }
314
315 static async findMostViewToDuplicate (randomizedFactor: number) {
316 const peertubeActor = await getServerActor()
317
318 // On VideoModel!
319 const query = {
320 attributes: [ 'id', 'views' ],
321 limit: randomizedFactor,
322 order: getVideoSort('-views'),
323 where: {
324 privacy: VideoPrivacy.PUBLIC,
325 isLive: false,
326 ...this.buildVideoIdsForDuplication(peertubeActor)
327 },
328 include: [
329 VideoRedundancyModel.buildServerRedundancyInclude()
330 ]
331 }
332
333 return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query))
334 }
335
336 static async findTrendingToDuplicate (randomizedFactor: number) {
337 const peertubeActor = await getServerActor()
338
339 // On VideoModel!
340 const query = {
341 attributes: [ 'id', 'views' ],
342 subQuery: false,
343 group: 'VideoModel.id',
344 limit: randomizedFactor,
345 order: getVideoSort('-trending'),
346 where: {
347 privacy: VideoPrivacy.PUBLIC,
348 isLive: false,
349 ...this.buildVideoIdsForDuplication(peertubeActor)
350 },
351 include: [
352 VideoRedundancyModel.buildServerRedundancyInclude(),
353
354 VideoModel.buildTrendingQuery(CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS)
355 ]
356 }
357
358 return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query))
359 }
360
361 static async findRecentlyAddedToDuplicate (randomizedFactor: number, minViews: number) {
362 const peertubeActor = await getServerActor()
363
364 // On VideoModel!
365 const query = {
366 attributes: [ 'id', 'publishedAt' ],
367 limit: randomizedFactor,
368 order: getVideoSort('-publishedAt'),
369 where: {
370 privacy: VideoPrivacy.PUBLIC,
371 isLive: false,
372 views: {
373 [Op.gte]: minViews
374 },
375 ...this.buildVideoIdsForDuplication(peertubeActor)
376 },
377 include: [
378 VideoRedundancyModel.buildServerRedundancyInclude(),
379
380 // Required by publishedAt sort
381 {
382 model: ScheduleVideoUpdateModel.unscoped(),
383 required: false
384 }
385 ]
386 }
387
388 return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query))
389 }
390
391 static async loadOldestLocalExpired (strategy: VideoRedundancyStrategy, expiresAfterMs: number): Promise<MVideoRedundancyVideo> {
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: {
402 [Op.lt]: expiredDate
403 }
404 }
405 }
406
407 return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findOne(query)
408 }
409
410 static async listLocalExpired () {
411 const actor = await getServerActor()
412
413 const query = {
414 where: {
415 actorId: actor.id,
416 expiresOn: {
417 [Op.lt]: new Date()
418 }
419 }
420 }
421
422 return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findAll(query)
423 }
424
425 static async listRemoteExpired () {
426 const actor = await getServerActor()
427
428 const query = {
429 where: {
430 actorId: {
431 [Op.ne]: actor.id
432 },
433 expiresOn: {
434 [Op.lt]: new Date(),
435 [Op.ne]: null
436 }
437 }
438 }
439
440 return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findAll(query)
441 }
442
443 static async listLocalOfServer (serverId: number) {
444 const actor = await getServerActor()
445 const buildVideoInclude = () => ({
446 model: VideoModel,
447 required: true,
448 include: [
449 {
450 attributes: [],
451 model: VideoChannelModel.unscoped(),
452 required: true,
453 include: [
454 {
455 attributes: [],
456 model: ActorModel.unscoped(),
457 required: true,
458 where: {
459 serverId
460 }
461 }
462 ]
463 }
464 ]
465 })
466
467 const query = {
468 where: {
469 actorId: actor.id
470 },
471 include: [
472 {
473 model: VideoFileModel,
474 required: false,
475 include: [ buildVideoInclude() ]
476 },
477 {
478 model: VideoStreamingPlaylistModel,
479 required: false,
480 include: [ buildVideoInclude() ]
481 }
482 ]
483 }
484
485 return VideoRedundancyModel.findAll(query)
486 }
487
488 static listForApi (options: {
489 start: number
490 count: number
491 sort: string
492 target: VideoRedundanciesTarget
493 strategy?: string
494 }) {
495 const { start, count, sort, target, strategy } = options
496 const redundancyWhere: WhereOptions = {}
497 const videosWhere: WhereOptions = {}
498 let redundancySqlSuffix = ''
499
500 if (target === 'my-videos') {
501 Object.assign(videosWhere, { remote: false })
502 } else if (target === 'remote-videos') {
503 Object.assign(videosWhere, { remote: true })
504 Object.assign(redundancyWhere, { strategy: { [Op.ne]: null } })
505 redundancySqlSuffix = ' AND "videoRedundancy"."strategy" IS NOT NULL'
506 }
507
508 if (strategy) {
509 Object.assign(redundancyWhere, { strategy: strategy })
510 }
511
512 const videoFilterWhere = {
513 [Op.and]: [
514 {
515 [Op.or]: [
516 {
517 id: {
518 [Op.in]: literal(
519 '(' +
520 'SELECT "videoId" FROM "videoFile" ' +
521 'INNER JOIN "videoRedundancy" ON "videoRedundancy"."videoFileId" = "videoFile".id' +
522 redundancySqlSuffix +
523 ')'
524 )
525 }
526 },
527 {
528 id: {
529 [Op.in]: literal(
530 '(' +
531 'select "videoId" FROM "videoStreamingPlaylist" ' +
532 'INNER JOIN "videoRedundancy" ON "videoRedundancy"."videoStreamingPlaylistId" = "videoStreamingPlaylist".id' +
533 redundancySqlSuffix +
534 ')'
535 )
536 }
537 }
538 ]
539 },
540
541 videosWhere
542 ]
543 }
544
545 // /!\ On video model /!\
546 const findOptions = {
547 offset: start,
548 limit: count,
549 order: getSort(sort),
550 include: [
551 {
552 required: false,
553 model: VideoFileModel,
554 include: [
555 {
556 model: VideoRedundancyModel.unscoped(),
557 required: false,
558 where: redundancyWhere
559 }
560 ]
561 },
562 {
563 required: false,
564 model: VideoStreamingPlaylistModel.unscoped(),
565 include: [
566 {
567 model: VideoRedundancyModel.unscoped(),
568 required: false,
569 where: redundancyWhere
570 },
571 {
572 model: VideoFileModel,
573 required: false
574 }
575 ]
576 }
577 ],
578 where: videoFilterWhere
579 }
580
581 // /!\ On video model /!\
582 const countOptions = {
583 where: videoFilterWhere
584 }
585
586 return Promise.all([
587 VideoModel.findAll(findOptions),
588
589 VideoModel.count(countOptions)
590 ]).then(([ data, total ]) => ({ total, data }))
591 }
592
593 static async getStats (strategy: VideoRedundancyStrategyWithManual) {
594 const actor = await getServerActor()
595
596 const sql = `WITH "tmp" AS ` +
597 `(` +
598 `SELECT "videoFile"."size" AS "videoFileSize", "videoStreamingFile"."size" AS "videoStreamingFileSize", ` +
599 `"videoFile"."videoId" AS "videoFileVideoId", "videoStreamingPlaylist"."videoId" AS "videoStreamingVideoId"` +
600 `FROM "videoRedundancy" AS "videoRedundancy" ` +
601 `LEFT JOIN "videoFile" AS "videoFile" ON "videoRedundancy"."videoFileId" = "videoFile"."id" ` +
602 `LEFT JOIN "videoStreamingPlaylist" ON "videoRedundancy"."videoStreamingPlaylistId" = "videoStreamingPlaylist"."id" ` +
603 `LEFT JOIN "videoFile" AS "videoStreamingFile" ` +
604 `ON "videoStreamingPlaylist"."id" = "videoStreamingFile"."videoStreamingPlaylistId" ` +
605 `WHERE "videoRedundancy"."strategy" = :strategy AND "videoRedundancy"."actorId" = :actorId` +
606 `), ` +
607 `"videoIds" AS (` +
608 `SELECT "videoFileVideoId" AS "videoId" FROM "tmp" ` +
609 `UNION SELECT "videoStreamingVideoId" AS "videoId" FROM "tmp" ` +
610 `) ` +
611 `SELECT ` +
612 `COALESCE(SUM("videoFileSize"), '0') + COALESCE(SUM("videoStreamingFileSize"), '0') AS "totalUsed", ` +
613 `(SELECT COUNT("videoIds"."videoId") FROM "videoIds") AS "totalVideos", ` +
614 `COUNT(*) AS "totalVideoFiles" ` +
615 `FROM "tmp"`
616
617 return VideoRedundancyModel.sequelize.query<any>(sql, {
618 replacements: { strategy, actorId: actor.id },
619 type: QueryTypes.SELECT
620 }).then(([ row ]) => ({
621 totalUsed: parseAggregateResult(row.totalUsed),
622 totalVideos: row.totalVideos,
623 totalVideoFiles: row.totalVideoFiles
624 }))
625 }
626
627 static toFormattedJSONStatic (video: MVideoForRedundancyAPI): VideoRedundancy {
628 const filesRedundancies: FileRedundancyInformation[] = []
629 const streamingPlaylistsRedundancies: StreamingPlaylistRedundancyInformation[] = []
630
631 for (const file of video.VideoFiles) {
632 for (const redundancy of file.RedundancyVideos) {
633 filesRedundancies.push({
634 id: redundancy.id,
635 fileUrl: redundancy.fileUrl,
636 strategy: redundancy.strategy,
637 createdAt: redundancy.createdAt,
638 updatedAt: redundancy.updatedAt,
639 expiresOn: redundancy.expiresOn,
640 size: file.size
641 })
642 }
643 }
644
645 for (const playlist of video.VideoStreamingPlaylists) {
646 const size = playlist.VideoFiles.reduce((a, b) => a + b.size, 0)
647
648 for (const redundancy of playlist.RedundancyVideos) {
649 streamingPlaylistsRedundancies.push({
650 id: redundancy.id,
651 fileUrl: redundancy.fileUrl,
652 strategy: redundancy.strategy,
653 createdAt: redundancy.createdAt,
654 updatedAt: redundancy.updatedAt,
655 expiresOn: redundancy.expiresOn,
656 size
657 })
658 }
659 }
660
661 return {
662 id: video.id,
663 name: video.name,
664 url: video.url,
665 uuid: video.uuid,
666
667 redundancies: {
668 files: filesRedundancies,
669 streamingPlaylists: streamingPlaylistsRedundancies
670 }
671 }
672 }
673
674 getVideo () {
675 if (this.VideoFile?.Video) return this.VideoFile.Video
676
677 if (this.VideoStreamingPlaylist?.Video) return this.VideoStreamingPlaylist.Video
678
679 return undefined
680 }
681
682 isOwned () {
683 return !!this.strategy
684 }
685
686 toActivityPubObject (this: MVideoRedundancyAP): CacheFileObject {
687 if (this.VideoStreamingPlaylist) {
688 return {
689 id: this.url,
690 type: 'CacheFile' as 'CacheFile',
691 object: this.VideoStreamingPlaylist.Video.url,
692 expires: this.expiresOn ? this.expiresOn.toISOString() : null,
693 url: {
694 type: 'Link',
695 mediaType: 'application/x-mpegURL',
696 href: this.fileUrl
697 }
698 }
699 }
700
701 return {
702 id: this.url,
703 type: 'CacheFile' as 'CacheFile',
704 object: this.VideoFile.Video.url,
705 expires: this.expiresOn ? this.expiresOn.toISOString() : null,
706 url: {
707 type: 'Link',
708 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[this.VideoFile.extname] as any,
709 href: this.fileUrl,
710 height: this.VideoFile.resolution,
711 size: this.VideoFile.size,
712 fps: this.VideoFile.fps
713 }
714 }
715 }
716
717 // Don't include video files we already duplicated
718 private static buildVideoIdsForDuplication (peertubeActor: MActor) {
719 const notIn = literal(
720 '(' +
721 `SELECT "videoFile"."videoId" AS "videoId" FROM "videoRedundancy" ` +
722 `INNER JOIN "videoFile" ON "videoFile"."id" = "videoRedundancy"."videoFileId" ` +
723 `WHERE "videoRedundancy"."actorId" = ${peertubeActor.id} ` +
724 `UNION ` +
725 `SELECT "videoStreamingPlaylist"."videoId" AS "videoId" FROM "videoRedundancy" ` +
726 `INNER JOIN "videoStreamingPlaylist" ON "videoStreamingPlaylist"."id" = "videoRedundancy"."videoStreamingPlaylistId" ` +
727 `WHERE "videoRedundancy"."actorId" = ${peertubeActor.id} ` +
728 ')'
729 )
730
731 return {
732 id: {
733 [Op.notIn]: notIn
734 }
735 }
736 }
737
738 private static buildServerRedundancyInclude () {
739 return {
740 attributes: [],
741 model: VideoChannelModel.unscoped(),
742 required: true,
743 include: [
744 {
745 attributes: [],
746 model: ActorModel.unscoped(),
747 required: true,
748 include: [
749 {
750 attributes: [],
751 model: ServerModel.unscoped(),
752 required: true,
753 where: {
754 redundancyAllowed: true
755 }
756 }
757 ]
758 }
759 ]
760 }
761 }
762 }