aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/request-video-qadu.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-02-21 21:35:59 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-02-26 20:01:26 +0100
commit9e167724f7e933f41d9ea2e1c31772bf4c560a28 (patch)
tree093cb7c1b088f35aaf847f859a313a121c8cd233 /server/models/request-video-qadu.js
parent0150b17e51df3e9fad8a59133d828c68f8ba672b (diff)
downloadPeerTube-9e167724f7e933f41d9ea2e1c31772bf4c560a28.tar.gz
PeerTube-9e167724f7e933f41d9ea2e1c31772bf4c560a28.tar.zst
PeerTube-9e167724f7e933f41d9ea2e1c31772bf4c560a28.zip
Server: make a basic "quick and dirty update" for videos
This system will be useful to to update some int video attributes (likes, dislikes, views...) The classic system is not used because we need some optimization for scaling
Diffstat (limited to 'server/models/request-video-qadu.js')
-rw-r--r--server/models/request-video-qadu.js154
1 files changed, 154 insertions, 0 deletions
diff --git a/server/models/request-video-qadu.js b/server/models/request-video-qadu.js
new file mode 100644
index 000000000..7010fc992
--- /dev/null
+++ b/server/models/request-video-qadu.js
@@ -0,0 +1,154 @@
1'use strict'
2
3/*
4 Request Video for Quick And Dirty Updates like:
5 - views
6 - likes
7 - dislikes
8
9 We can't put it in the same system than basic requests for efficiency.
10 Moreover we don't want to slow down the basic requests with a lot of views/likes/dislikes requests.
11 So we put it an independant request scheduler.
12*/
13
14const values = require('lodash/values')
15
16const constants = require('../initializers/constants')
17
18// ---------------------------------------------------------------------------
19
20module.exports = function (sequelize, DataTypes) {
21 const RequestVideoQadu = sequelize.define('RequestVideoQadu',
22 {
23 type: {
24 type: DataTypes.ENUM(values(constants.REQUEST_VIDEO_QADU_TYPES)),
25 allowNull: false
26 }
27 },
28 {
29 timestamps: false,
30 indexes: [
31 {
32 fields: [ 'podId' ]
33 },
34 {
35 fields: [ 'videoId' ]
36 }
37 ],
38 classMethods: {
39 associate,
40
41 listWithLimitAndRandom,
42
43 countTotalRequests,
44 removeAll,
45 removeByRequestIdsAndPod
46 }
47 }
48 )
49
50 return RequestVideoQadu
51}
52
53// ------------------------------ STATICS ------------------------------
54
55function associate (models) {
56 this.belongsTo(models.Pod, {
57 foreignKey: {
58 name: 'podId',
59 allowNull: false
60 },
61 onDelete: 'CASCADE'
62 })
63
64 this.belongsTo(models.Video, {
65 foreignKey: {
66 name: 'videoId',
67 allowNull: false
68 },
69 onDelete: 'CASCADE'
70 })
71}
72
73function countTotalRequests (callback) {
74 const query = {
75 include: [ this.sequelize.models.Pod ]
76 }
77
78 return this.count(query).asCallback(callback)
79}
80
81function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) {
82 const self = this
83 const Pod = this.sequelize.models.Pod
84
85 Pod.listRandomPodIdsWithRequest(limitPods, 'RequestVideoQadus', function (err, podIds) {
86 if (err) return callback(err)
87
88 // We don't have friends that have requests
89 if (podIds.length === 0) return callback(null, [])
90
91 const query = {
92 include: [
93 {
94 model: self.sequelize.models.Pod,
95 where: {
96 id: {
97 $in: podIds
98 }
99 }
100 },
101 {
102 model: self.sequelize.models.Video
103 }
104 ]
105 }
106
107 self.findAll(query).asCallback(function (err, requests) {
108 if (err) return callback(err)
109
110 const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
111 return callback(err, requestsGrouped)
112 })
113 })
114}
115
116function removeByRequestIdsAndPod (ids, podId, callback) {
117 const query = {
118 where: {
119 id: {
120 $in: ids
121 },
122 podId
123 }
124 }
125
126 this.destroy(query).asCallback(callback)
127}
128
129function removeAll (callback) {
130 // Delete all requests
131 this.truncate({ cascade: true }).asCallback(callback)
132}
133
134// ---------------------------------------------------------------------------
135
136function groupAndTruncateRequests (requests, limitRequestsPerPod) {
137 const requestsGrouped = {}
138
139 requests.forEach(function (request) {
140 const pod = request.Pod
141
142 if (!requestsGrouped[pod.id]) requestsGrouped[pod.id] = []
143
144 if (requestsGrouped[pod.id].length < limitRequestsPerPod) {
145 requestsGrouped[pod.id].push({
146 request: request,
147 video: request.Video,
148 pod
149 })
150 }
151 })
152
153 return requestsGrouped
154}