diff options
Diffstat (limited to 'server/models/redundancy')
-rw-r--r-- | server/models/redundancy/video-redundancy.ts | 53 |
1 files changed, 39 insertions, 14 deletions
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' | |||
27 | import { ServerModel } from '../server/server' | 27 | import { ServerModel } from '../server/server' |
28 | import { sample } from 'lodash' | 28 | import { sample } from 'lodash' |
29 | import { isTestInstance } from '../../helpers/core-utils' | 29 | import { isTestInstance } from '../../helpers/core-utils' |
30 | import * as Bluebird from 'bluebird' | ||
30 | 31 | ||
31 | export enum ScopeNames { | 32 | export enum ScopeNames { |
32 | WITH_VIDEO = 'WITH_VIDEO' | 33 | WITH_VIDEO = 'WITH_VIDEO' |
@@ -144,7 +145,8 @@ export class VideoRedundancyModel extends Model<VideoRedundancyModel> { | |||
144 | return VideoRedundancyModel.findOne(query) | 145 | return VideoRedundancyModel.findOne(query) |
145 | } | 146 | } |
146 | 147 | ||
147 | static getVideoSample (rows: { id: number }[]) { | 148 | static async getVideoSample (p: Bluebird<VideoModel[]>) { |
149 | const rows = await p | ||
148 | const ids = rows.map(r => r.id) | 150 | const ids = rows.map(r => r.id) |
149 | const id = sample(ids) | 151 | const id = sample(ids) |
150 | 152 | ||
@@ -164,9 +166,7 @@ export class VideoRedundancyModel extends Model<VideoRedundancyModel> { | |||
164 | ] | 166 | ] |
165 | } | 167 | } |
166 | 168 | ||
167 | const rows = await VideoModel.unscoped().findAll(query) | 169 | return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query)) |
168 | |||
169 | return VideoRedundancyModel.getVideoSample(rows as { id: number }[]) | ||
170 | } | 170 | } |
171 | 171 | ||
172 | static async findTrendingToDuplicate (randomizedFactor: number) { | 172 | static async findTrendingToDuplicate (randomizedFactor: number) { |
@@ -186,24 +186,49 @@ export class VideoRedundancyModel extends Model<VideoRedundancyModel> { | |||
186 | ] | 186 | ] |
187 | } | 187 | } |
188 | 188 | ||
189 | const rows = await VideoModel.unscoped().findAll(query) | 189 | return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query)) |
190 | } | ||
190 | 191 | ||
191 | return VideoRedundancyModel.getVideoSample(rows as { id: number }[]) | 192 | static async findRecentlyAddedToDuplicate (randomizedFactor: number, minViews: number) { |
193 | // On VideoModel! | ||
194 | const query = { | ||
195 | attributes: [ 'id', 'publishedAt' ], | ||
196 | // logging: !isTestInstance(), | ||
197 | limit: randomizedFactor, | ||
198 | order: getVideoSort('-publishedAt'), | ||
199 | where: { | ||
200 | views: { | ||
201 | [ Sequelize.Op.gte ]: minViews | ||
202 | } | ||
203 | }, | ||
204 | include: [ | ||
205 | await VideoRedundancyModel.buildVideoFileForDuplication(), | ||
206 | VideoRedundancyModel.buildServerRedundancyInclude() | ||
207 | ] | ||
208 | } | ||
209 | |||
210 | return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query)) | ||
192 | } | 211 | } |
193 | 212 | ||
194 | static async getVideoFiles (strategy: VideoRedundancyStrategy) { | 213 | static async getTotalDuplicated (strategy: VideoRedundancyStrategy) { |
195 | const actor = await getServerActor() | 214 | const actor = await getServerActor() |
196 | 215 | ||
197 | const queryVideoFiles = { | 216 | const options = { |
198 | logging: !isTestInstance(), | 217 | logging: !isTestInstance(), |
199 | where: { | 218 | include: [ |
200 | actorId: actor.id, | 219 | { |
201 | strategy | 220 | attributes: [], |
202 | } | 221 | model: VideoRedundancyModel, |
222 | required: true, | ||
223 | where: { | ||
224 | actorId: actor.id, | ||
225 | strategy | ||
226 | } | ||
227 | } | ||
228 | ] | ||
203 | } | 229 | } |
204 | 230 | ||
205 | return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO) | 231 | return VideoFileModel.sum('size', options) |
206 | .findAll(queryVideoFiles) | ||
207 | } | 232 | } |
208 | 233 | ||
209 | static listAllExpired () { | 234 | static listAllExpired () { |