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