diff options
author | Chocobozzz <me@florianbigard.com> | 2018-09-14 11:05:38 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-09-14 11:05:38 +0200 |
commit | 3f6b6a565dc98a658ec9d8f697252788c0faa46d (patch) | |
tree | 9549d3722b2d2d6501d85b4a56e6bfd3ea14fed3 /server/models | |
parent | 780daa7e91336116a5163156f298c9ad875ec1e3 (diff) | |
download | PeerTube-3f6b6a565dc98a658ec9d8f697252788c0faa46d.tar.gz PeerTube-3f6b6a565dc98a658ec9d8f697252788c0faa46d.tar.zst PeerTube-3f6b6a565dc98a658ec9d8f697252788c0faa46d.zip |
Add recently added redundancy strategy
Diffstat (limited to 'server/models')
-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 () { |