From 3f6b6a565dc98a658ec9d8f697252788c0faa46d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 14 Sep 2018 11:05:38 +0200 Subject: Add recently added redundancy strategy --- server/models/redundancy/video-redundancy.ts | 53 ++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 14 deletions(-) (limited to 'server/models') diff --git a/server/models/redundancy/video-redundancy.ts b/server/models/redundancy/video-redundancy.ts index b13ade0f4..b7454c617 100644 --- a/server/models/redundancy/video-redundancy.ts +++ b/server/models/redundancy/video-redundancy.ts @@ -27,6 +27,7 @@ import { VideoChannelModel } from '../video/video-channel' import { ServerModel } from '../server/server' import { sample } from 'lodash' import { isTestInstance } from '../../helpers/core-utils' +import * as Bluebird from 'bluebird' export enum ScopeNames { WITH_VIDEO = 'WITH_VIDEO' @@ -144,7 +145,8 @@ export class VideoRedundancyModel extends Model { return VideoRedundancyModel.findOne(query) } - static getVideoSample (rows: { id: number }[]) { + static async getVideoSample (p: Bluebird) { + const rows = await p const ids = rows.map(r => r.id) const id = sample(ids) @@ -164,9 +166,7 @@ export class VideoRedundancyModel extends Model { ] } - const rows = await VideoModel.unscoped().findAll(query) - - return VideoRedundancyModel.getVideoSample(rows as { id: number }[]) + return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query)) } static async findTrendingToDuplicate (randomizedFactor: number) { @@ -186,24 +186,49 @@ export class VideoRedundancyModel extends Model { ] } - const rows = await VideoModel.unscoped().findAll(query) + return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query)) + } - return VideoRedundancyModel.getVideoSample(rows as { id: number }[]) + static async findRecentlyAddedToDuplicate (randomizedFactor: number, minViews: number) { + // On VideoModel! + const query = { + attributes: [ 'id', 'publishedAt' ], + // logging: !isTestInstance(), + limit: randomizedFactor, + order: getVideoSort('-publishedAt'), + where: { + views: { + [ Sequelize.Op.gte ]: minViews + } + }, + include: [ + await VideoRedundancyModel.buildVideoFileForDuplication(), + VideoRedundancyModel.buildServerRedundancyInclude() + ] + } + + return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query)) } - static async getVideoFiles (strategy: VideoRedundancyStrategy) { + static async getTotalDuplicated (strategy: VideoRedundancyStrategy) { const actor = await getServerActor() - const queryVideoFiles = { + const options = { logging: !isTestInstance(), - where: { - actorId: actor.id, - strategy - } + include: [ + { + attributes: [], + model: VideoRedundancyModel, + required: true, + where: { + actorId: actor.id, + strategy + } + } + ] } - return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO) - .findAll(queryVideoFiles) + return VideoFileModel.sum('size', options) } static listAllExpired () { -- cgit v1.2.3