diff options
Diffstat (limited to 'server/models/redundancy/video-redundancy.ts')
-rw-r--r-- | server/models/redundancy/video-redundancy.ts | 56 |
1 files changed, 26 insertions, 30 deletions
diff --git a/server/models/redundancy/video-redundancy.ts b/server/models/redundancy/video-redundancy.ts index cbeaa662b..eb2222256 100644 --- a/server/models/redundancy/video-redundancy.ts +++ b/server/models/redundancy/video-redundancy.ts | |||
@@ -13,7 +13,7 @@ import { | |||
13 | UpdatedAt | 13 | UpdatedAt |
14 | } from 'sequelize-typescript' | 14 | } from 'sequelize-typescript' |
15 | import { ActorModel } from '../activitypub/actor' | 15 | import { ActorModel } from '../activitypub/actor' |
16 | import { getVideoSort, throwIfNotValid } from '../utils' | 16 | import { getVideoSort, parseAggregateResult, throwIfNotValid } from '../utils' |
17 | import { isActivityPubUrlValid, isUrlValid } from '../../helpers/custom-validators/activitypub/misc' | 17 | import { isActivityPubUrlValid, isUrlValid } from '../../helpers/custom-validators/activitypub/misc' |
18 | import { CONSTRAINTS_FIELDS, MIMETYPES } from '../../initializers/constants' | 18 | import { CONSTRAINTS_FIELDS, MIMETYPES } from '../../initializers/constants' |
19 | import { VideoFileModel } from '../video/video-file' | 19 | import { VideoFileModel } from '../video/video-file' |
@@ -27,7 +27,7 @@ import { ServerModel } from '../server/server' | |||
27 | import { sample } from 'lodash' | 27 | import { sample } from 'lodash' |
28 | import { isTestInstance } from '../../helpers/core-utils' | 28 | import { isTestInstance } from '../../helpers/core-utils' |
29 | import * as Bluebird from 'bluebird' | 29 | import * as Bluebird from 'bluebird' |
30 | import * as Sequelize from 'sequelize' | 30 | import { col, FindOptions, fn, literal, Op, Transaction } from 'sequelize' |
31 | import { VideoStreamingPlaylistModel } from '../video/video-streaming-playlist' | 31 | import { VideoStreamingPlaylistModel } from '../video/video-streaming-playlist' |
32 | import { CONFIG } from '../../initializers/config' | 32 | import { CONFIG } from '../../initializers/config' |
33 | 33 | ||
@@ -35,32 +35,32 @@ export enum ScopeNames { | |||
35 | WITH_VIDEO = 'WITH_VIDEO' | 35 | WITH_VIDEO = 'WITH_VIDEO' |
36 | } | 36 | } |
37 | 37 | ||
38 | @Scopes({ | 38 | @Scopes(() => ({ |
39 | [ ScopeNames.WITH_VIDEO ]: { | 39 | [ ScopeNames.WITH_VIDEO ]: { |
40 | include: [ | 40 | include: [ |
41 | { | 41 | { |
42 | model: () => VideoFileModel, | 42 | model: VideoFileModel, |
43 | required: false, | 43 | required: false, |
44 | include: [ | 44 | include: [ |
45 | { | 45 | { |
46 | model: () => VideoModel, | 46 | model: VideoModel, |
47 | required: true | 47 | required: true |
48 | } | 48 | } |
49 | ] | 49 | ] |
50 | }, | 50 | }, |
51 | { | 51 | { |
52 | model: () => VideoStreamingPlaylistModel, | 52 | model: VideoStreamingPlaylistModel, |
53 | required: false, | 53 | required: false, |
54 | include: [ | 54 | include: [ |
55 | { | 55 | { |
56 | model: () => VideoModel, | 56 | model: VideoModel, |
57 | required: true | 57 | required: true |
58 | } | 58 | } |
59 | ] | 59 | ] |
60 | } | 60 | } |
61 | ] as any // FIXME: sequelize typings | 61 | ] |
62 | } | 62 | } |
63 | }) | 63 | })) |
64 | 64 | ||
65 | @Table({ | 65 | @Table({ |
66 | tableName: 'videoRedundancy', | 66 | tableName: 'videoRedundancy', |
@@ -192,7 +192,7 @@ export class VideoRedundancyModel extends Model<VideoRedundancyModel> { | |||
192 | return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query) | 192 | return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query) |
193 | } | 193 | } |
194 | 194 | ||
195 | static loadByUrl (url: string, transaction?: Sequelize.Transaction) { | 195 | static loadByUrl (url: string, transaction?: Transaction) { |
196 | const query = { | 196 | const query = { |
197 | where: { | 197 | where: { |
198 | url | 198 | url |
@@ -292,7 +292,7 @@ export class VideoRedundancyModel extends Model<VideoRedundancyModel> { | |||
292 | where: { | 292 | where: { |
293 | privacy: VideoPrivacy.PUBLIC, | 293 | privacy: VideoPrivacy.PUBLIC, |
294 | views: { | 294 | views: { |
295 | [ Sequelize.Op.gte ]: minViews | 295 | [ Op.gte ]: minViews |
296 | } | 296 | } |
297 | }, | 297 | }, |
298 | include: [ | 298 | include: [ |
@@ -315,7 +315,7 @@ export class VideoRedundancyModel extends Model<VideoRedundancyModel> { | |||
315 | actorId: actor.id, | 315 | actorId: actor.id, |
316 | strategy, | 316 | strategy, |
317 | createdAt: { | 317 | createdAt: { |
318 | [ Sequelize.Op.lt ]: expiredDate | 318 | [ Op.lt ]: expiredDate |
319 | } | 319 | } |
320 | } | 320 | } |
321 | } | 321 | } |
@@ -326,7 +326,7 @@ export class VideoRedundancyModel extends Model<VideoRedundancyModel> { | |||
326 | static async getTotalDuplicated (strategy: VideoRedundancyStrategy) { | 326 | static async getTotalDuplicated (strategy: VideoRedundancyStrategy) { |
327 | const actor = await getServerActor() | 327 | const actor = await getServerActor() |
328 | 328 | ||
329 | const options = { | 329 | const query: FindOptions = { |
330 | include: [ | 330 | include: [ |
331 | { | 331 | { |
332 | attributes: [], | 332 | attributes: [], |
@@ -340,12 +340,8 @@ export class VideoRedundancyModel extends Model<VideoRedundancyModel> { | |||
340 | ] | 340 | ] |
341 | } | 341 | } |
342 | 342 | ||
343 | return VideoFileModel.sum('size', options as any) // FIXME: typings | 343 | return VideoFileModel.aggregate('size', 'SUM', query) |
344 | .then(v => { | 344 | .then(result => parseAggregateResult(result)) |
345 | if (!v || isNaN(v)) return 0 | ||
346 | |||
347 | return v | ||
348 | }) | ||
349 | } | 345 | } |
350 | 346 | ||
351 | static async listLocalExpired () { | 347 | static async listLocalExpired () { |
@@ -355,7 +351,7 @@ export class VideoRedundancyModel extends Model<VideoRedundancyModel> { | |||
355 | where: { | 351 | where: { |
356 | actorId: actor.id, | 352 | actorId: actor.id, |
357 | expiresOn: { | 353 | expiresOn: { |
358 | [ Sequelize.Op.lt ]: new Date() | 354 | [ Op.lt ]: new Date() |
359 | } | 355 | } |
360 | } | 356 | } |
361 | } | 357 | } |
@@ -369,10 +365,10 @@ export class VideoRedundancyModel extends Model<VideoRedundancyModel> { | |||
369 | const query = { | 365 | const query = { |
370 | where: { | 366 | where: { |
371 | actorId: { | 367 | actorId: { |
372 | [Sequelize.Op.ne]: actor.id | 368 | [Op.ne]: actor.id |
373 | }, | 369 | }, |
374 | expiresOn: { | 370 | expiresOn: { |
375 | [ Sequelize.Op.lt ]: new Date() | 371 | [ Op.lt ]: new Date() |
376 | } | 372 | } |
377 | } | 373 | } |
378 | } | 374 | } |
@@ -428,12 +424,12 @@ export class VideoRedundancyModel extends Model<VideoRedundancyModel> { | |||
428 | static async getStats (strategy: VideoRedundancyStrategy) { | 424 | static async getStats (strategy: VideoRedundancyStrategy) { |
429 | const actor = await getServerActor() | 425 | const actor = await getServerActor() |
430 | 426 | ||
431 | const query = { | 427 | const query: FindOptions = { |
432 | raw: true, | 428 | raw: true, |
433 | attributes: [ | 429 | attributes: [ |
434 | [ Sequelize.fn('COALESCE', Sequelize.fn('SUM', Sequelize.col('VideoFile.size')), '0'), 'totalUsed' ], | 430 | [ fn('COALESCE', fn('SUM', col('VideoFile.size')), '0'), 'totalUsed' ], |
435 | [ Sequelize.fn('COUNT', Sequelize.fn('DISTINCT', Sequelize.col('videoId'))), 'totalVideos' ], | 431 | [ fn('COUNT', fn('DISTINCT', col('videoId'))), 'totalVideos' ], |
436 | [ Sequelize.fn('COUNT', Sequelize.col('videoFileId')), 'totalVideoFiles' ] | 432 | [ fn('COUNT', col('videoFileId')), 'totalVideoFiles' ] |
437 | ], | 433 | ], |
438 | where: { | 434 | where: { |
439 | strategy, | 435 | strategy, |
@@ -448,9 +444,9 @@ export class VideoRedundancyModel extends Model<VideoRedundancyModel> { | |||
448 | ] | 444 | ] |
449 | } | 445 | } |
450 | 446 | ||
451 | return VideoRedundancyModel.findOne(query as any) // FIXME: typings | 447 | return VideoRedundancyModel.findOne(query) |
452 | .then((r: any) => ({ | 448 | .then((r: any) => ({ |
453 | totalUsed: parseInt(r.totalUsed.toString(), 10), | 449 | totalUsed: parseAggregateResult(r.totalUsed), |
454 | totalVideos: r.totalVideos, | 450 | totalVideos: r.totalVideos, |
455 | totalVideoFiles: r.totalVideoFiles | 451 | totalVideoFiles: r.totalVideoFiles |
456 | })) | 452 | })) |
@@ -503,7 +499,7 @@ export class VideoRedundancyModel extends Model<VideoRedundancyModel> { | |||
503 | private static async buildVideoFileForDuplication () { | 499 | private static async buildVideoFileForDuplication () { |
504 | const actor = await getServerActor() | 500 | const actor = await getServerActor() |
505 | 501 | ||
506 | const notIn = Sequelize.literal( | 502 | const notIn = literal( |
507 | '(' + | 503 | '(' + |
508 | `SELECT "videoFileId" FROM "videoRedundancy" WHERE "actorId" = ${actor.id} AND "videoFileId" IS NOT NULL` + | 504 | `SELECT "videoFileId" FROM "videoRedundancy" WHERE "actorId" = ${actor.id} AND "videoFileId" IS NOT NULL` + |
509 | ')' | 505 | ')' |
@@ -515,7 +511,7 @@ export class VideoRedundancyModel extends Model<VideoRedundancyModel> { | |||
515 | required: true, | 511 | required: true, |
516 | where: { | 512 | where: { |
517 | id: { | 513 | id: { |
518 | [ Sequelize.Op.notIn ]: notIn | 514 | [ Op.notIn ]: notIn |
519 | } | 515 | } |
520 | } | 516 | } |
521 | } | 517 | } |