]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
9e167724 | 3 | const each = require('async/each') |
00057e85 | 4 | const map = require('lodash/map') |
9e167724 | 5 | const waterfall = require('async/waterfall') |
9f10b292 | 6 | |
f0f5567b | 7 | const constants = require('../initializers/constants') |
9e167724 | 8 | const logger = require('../helpers/logger') |
67bf9b96 | 9 | const customPodsValidators = require('../helpers/custom-validators').pods |
9f10b292 C |
10 | |
11 | // --------------------------------------------------------------------------- | |
12 | ||
feb4bdfd C |
13 | module.exports = function (sequelize, DataTypes) { |
14 | const Pod = sequelize.define('Pod', | |
15 | { | |
16 | host: { | |
67bf9b96 C |
17 | type: DataTypes.STRING, |
18 | allowNull: false, | |
19 | validate: { | |
20 | isHost: function (value) { | |
21 | const res = customPodsValidators.isHostValid(value) | |
22 | if (res === false) throw new Error('Host not valid.') | |
23 | } | |
24 | } | |
feb4bdfd C |
25 | }, |
26 | publicKey: { | |
67bf9b96 C |
27 | type: DataTypes.STRING(5000), |
28 | allowNull: false | |
feb4bdfd C |
29 | }, |
30 | score: { | |
31 | type: DataTypes.INTEGER, | |
67bf9b96 C |
32 | defaultValue: constants.FRIEND_SCORE.BASE, |
33 | allowNull: false, | |
34 | validate: { | |
35 | isInt: true, | |
36 | max: constants.FRIEND_SCORE.MAX | |
37 | } | |
4793c343 C |
38 | }, |
39 | email: { | |
40 | type: DataTypes.STRING(400), | |
ad4a8a1c C |
41 | allowNull: false, |
42 | validate: { | |
43 | isEmail: true | |
44 | } | |
feb4bdfd | 45 | } |
feb4bdfd C |
46 | }, |
47 | { | |
319d072e C |
48 | indexes: [ |
49 | { | |
5d67f289 C |
50 | fields: [ 'host' ], |
51 | unique: true | |
319d072e C |
52 | }, |
53 | { | |
54 | fields: [ 'score' ] | |
55 | } | |
56 | ], | |
feb4bdfd C |
57 | classMethods: { |
58 | associate, | |
59 | ||
60 | countAll, | |
61 | incrementScores, | |
62 | list, | |
63 | listAllIds, | |
bd14d16a | 64 | listRandomPodIdsWithRequest, |
feb4bdfd C |
65 | listBadPods, |
66 | load, | |
67 | loadByHost, | |
9e167724 | 68 | updatePodsScore, |
feb4bdfd C |
69 | removeAll |
70 | }, | |
71 | instanceMethods: { | |
72 | toFormatedJSON | |
73 | } | |
74 | } | |
75 | ) | |
76 | ||
77 | return Pod | |
53572423 C |
78 | } |
79 | ||
53572423 C |
80 | // ------------------------------ METHODS ------------------------------ |
81 | ||
82 | function toFormatedJSON () { | |
83 | const json = { | |
feb4bdfd | 84 | id: this.id, |
49abbbbe | 85 | host: this.host, |
4793c343 | 86 | email: this.email, |
53572423 | 87 | score: this.score, |
feb4bdfd | 88 | createdAt: this.createdAt |
53572423 C |
89 | } |
90 | ||
91 | return json | |
92 | } | |
93 | ||
a3ee6fa2 C |
94 | // ------------------------------ Statics ------------------------------ |
95 | ||
feb4bdfd C |
96 | function associate (models) { |
97 | this.belongsToMany(models.Request, { | |
98 | foreignKey: 'podId', | |
99 | through: models.RequestToPod, | |
7920c273 | 100 | onDelete: 'cascade' |
feb4bdfd C |
101 | }) |
102 | } | |
103 | ||
a3ee6fa2 | 104 | function countAll (callback) { |
feb4bdfd | 105 | return this.count().asCallback(callback) |
9f10b292 | 106 | } |
45239549 | 107 | |
9f10b292 C |
108 | function incrementScores (ids, value, callback) { |
109 | if (!callback) callback = function () {} | |
feb4bdfd C |
110 | |
111 | const update = { | |
112 | score: this.sequelize.literal('score +' + value) | |
113 | } | |
114 | ||
67bf9b96 | 115 | const options = { |
feb4bdfd C |
116 | where: { |
117 | id: { | |
118 | $in: ids | |
119 | } | |
67bf9b96 C |
120 | }, |
121 | // In this case score is a literal and not an integer so we do not validate it | |
122 | validate: false | |
feb4bdfd C |
123 | } |
124 | ||
67bf9b96 | 125 | return this.update(update, options).asCallback(callback) |
9f10b292 | 126 | } |
45239549 | 127 | |
9f10b292 | 128 | function list (callback) { |
feb4bdfd | 129 | return this.findAll().asCallback(callback) |
9f10b292 | 130 | } |
8c308c2b | 131 | |
ed04d94f C |
132 | function listAllIds (transaction, callback) { |
133 | if (!callback) { | |
134 | callback = transaction | |
135 | transaction = null | |
136 | } | |
137 | ||
feb4bdfd C |
138 | const query = { |
139 | attributes: [ 'id' ] | |
140 | } | |
141 | ||
ed04d94f C |
142 | if (transaction) query.transaction = transaction |
143 | ||
feb4bdfd | 144 | return this.findAll(query).asCallback(function (err, pods) { |
00057e85 C |
145 | if (err) return callback(err) |
146 | ||
feb4bdfd | 147 | return callback(null, map(pods, 'id')) |
00057e85 | 148 | }) |
528a9efa C |
149 | } |
150 | ||
9e167724 | 151 | function listRandomPodIdsWithRequest (limit, tableRequestPod, callback) { |
bd14d16a C |
152 | const self = this |
153 | ||
154 | self.count().asCallback(function (err, count) { | |
155 | if (err) return callback(err) | |
156 | ||
157 | // Optimization... | |
158 | if (count === 0) return callback(null, []) | |
159 | ||
160 | let start = Math.floor(Math.random() * count) - limit | |
161 | if (start < 0) start = 0 | |
162 | ||
163 | const query = { | |
164 | attributes: [ 'id' ], | |
165 | order: [ | |
166 | [ 'id', 'ASC' ] | |
167 | ], | |
168 | offset: start, | |
169 | limit: limit, | |
170 | where: { | |
171 | id: { | |
172 | $in: [ | |
9e167724 | 173 | this.sequelize.literal('SELECT "podId" FROM "' + tableRequestPod + '"') |
bd14d16a C |
174 | ] |
175 | } | |
176 | } | |
177 | } | |
178 | ||
179 | return this.findAll(query).asCallback(function (err, pods) { | |
180 | if (err) return callback(err) | |
181 | ||
182 | return callback(null, map(pods, 'id')) | |
183 | }) | |
184 | }) | |
185 | } | |
186 | ||
a3ee6fa2 | 187 | function listBadPods (callback) { |
feb4bdfd C |
188 | const query = { |
189 | where: { | |
190 | score: { $lte: 0 } | |
191 | } | |
192 | } | |
193 | ||
194 | return this.findAll(query).asCallback(callback) | |
9f10b292 | 195 | } |
8c308c2b | 196 | |
a3ee6fa2 | 197 | function load (id, callback) { |
feb4bdfd | 198 | return this.findById(id).asCallback(callback) |
9f10b292 | 199 | } |
c45f7f84 | 200 | |
49abbbbe | 201 | function loadByHost (host, callback) { |
feb4bdfd C |
202 | const query = { |
203 | where: { | |
204 | host: host | |
205 | } | |
206 | } | |
207 | ||
208 | return this.findOne(query).asCallback(callback) | |
9f10b292 | 209 | } |
c45f7f84 | 210 | |
a3ee6fa2 | 211 | function removeAll (callback) { |
feb4bdfd | 212 | return this.destroy().asCallback(callback) |
a3ee6fa2 | 213 | } |
9e167724 C |
214 | |
215 | function updatePodsScore (goodPods, badPods) { | |
216 | const self = this | |
217 | ||
218 | logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) | |
219 | ||
220 | if (goodPods.length !== 0) { | |
221 | this.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) { | |
222 | if (err) logger.error('Cannot increment scores of good pods.', { error: err }) | |
223 | }) | |
224 | } | |
225 | ||
226 | if (badPods.length !== 0) { | |
227 | this.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) { | |
228 | if (err) logger.error('Cannot decrement scores of bad pods.', { error: err }) | |
229 | removeBadPods.call(self) | |
230 | }) | |
231 | } | |
232 | } | |
233 | ||
234 | // --------------------------------------------------------------------------- | |
235 | ||
236 | // Remove pods with a score of 0 (too many requests where they were unreachable) | |
237 | function removeBadPods () { | |
238 | const self = this | |
239 | ||
240 | waterfall([ | |
241 | function findBadPods (callback) { | |
242 | self.sequelize.models.Pod.listBadPods(function (err, pods) { | |
243 | if (err) { | |
244 | logger.error('Cannot find bad pods.', { error: err }) | |
245 | return callback(err) | |
246 | } | |
247 | ||
248 | return callback(null, pods) | |
249 | }) | |
250 | }, | |
251 | ||
252 | function removeTheseBadPods (pods, callback) { | |
253 | each(pods, function (pod, callbackEach) { | |
254 | pod.destroy().asCallback(callbackEach) | |
255 | }, function (err) { | |
256 | return callback(err, pods.length) | |
257 | }) | |
258 | } | |
259 | ], function (err, numberOfPodsRemoved) { | |
260 | if (err) { | |
261 | logger.error('Cannot remove bad pods.', { error: err }) | |
262 | } else if (numberOfPodsRemoved) { | |
263 | logger.info('Removed %d pods.', numberOfPodsRemoved) | |
264 | } else { | |
265 | logger.info('No need to remove bad pods.') | |
266 | } | |
267 | }) | |
268 | } |