3 const each
= require('async/each')
4 const map
= require('lodash/map')
5 const waterfall
= require('async/waterfall')
7 const constants
= require('../initializers/constants')
8 const logger
= require('../helpers/logger')
9 const customPodsValidators
= require('../helpers/custom-validators').pods
11 // ---------------------------------------------------------------------------
13 module
.exports = function (sequelize
, DataTypes
) {
14 const Pod
= sequelize
.define('Pod',
17 type: DataTypes
.STRING
,
20 isHost: function (value
) {
21 const res
= customPodsValidators
.isHostValid(value
)
22 if (res
=== false) throw new Error('Host not valid.')
27 type: DataTypes
.STRING(5000),
31 type: DataTypes
.INTEGER
,
32 defaultValue: constants
.FRIEND_SCORE
.BASE
,
36 max: constants
.FRIEND_SCORE
.MAX
40 type: DataTypes
.STRING(400),
64 listRandomPodIdsWithRequest
,
80 // ------------------------------ METHODS ------------------------------
82 function toFormatedJSON () {
88 createdAt: this.createdAt
94 // ------------------------------ Statics ------------------------------
96 function associate (models
) {
97 this.belongsToMany(models
.Request
, {
99 through: models
.RequestToPod
,
104 function countAll (callback
) {
105 return this.count().asCallback(callback
)
108 function incrementScores (ids
, value
, callback
) {
109 if (!callback
) callback = function () {}
112 score: this.sequelize
.literal('score +' + value
)
121 // In this case score is a literal and not an integer so we do not validate it
125 return this.update(update
, options
).asCallback(callback
)
128 function list (callback
) {
129 return this.findAll().asCallback(callback
)
132 function listAllIds (transaction
, callback
) {
134 callback
= transaction
142 if (transaction
) query
.transaction
= transaction
144 return this.findAll(query
).asCallback(function (err
, pods
) {
145 if (err
) return callback(err
)
147 return callback(null, map(pods
, 'id'))
151 function listRandomPodIdsWithRequest (limit
, tableWithPods
, tableWithPodsJoins
, callback
) {
153 callback
= tableWithPodsJoins
154 tableWithPodsJoins
= ''
159 self
.count().asCallback(function (err
, count
) {
160 if (err
) return callback(err
)
163 if (count
=== 0) return callback(null, [])
165 let start
= Math
.floor(Math
.random() * count
) - limit
166 if (start
< 0) start
= 0
169 attributes: [ 'id' ],
178 this.sequelize
.literal(`SELECT DISTINCT "${tableWithPods}"."podId" FROM "${tableWithPods}" ${tableWithPodsJoins}`)
184 return this.findAll(query
).asCallback(function (err
, pods
) {
185 if (err
) return callback(err
)
187 return callback(null, map(pods
, 'id'))
192 function listBadPods (callback
) {
199 return this.findAll(query
).asCallback(callback
)
202 function load (id
, callback
) {
203 return this.findById(id
).asCallback(callback
)
206 function loadByHost (host
, callback
) {
213 return this.findOne(query
).asCallback(callback
)
216 function removeAll (callback
) {
217 return this.destroy().asCallback(callback
)
220 function updatePodsScore (goodPods
, badPods
) {
223 logger
.info('Updating %d good pods and %d bad pods scores.', goodPods
.length
, badPods
.length
)
225 if (goodPods
.length
!== 0) {
226 this.incrementScores(goodPods
, constants
.PODS_SCORE
.BONUS
, function (err
) {
227 if (err
) logger
.error('Cannot increment scores of good pods.', { error: err
})
231 if (badPods
.length
!== 0) {
232 this.incrementScores(badPods
, constants
.PODS_SCORE
.MALUS
, function (err
) {
233 if (err
) logger
.error('Cannot decrement scores of bad pods.', { error: err
})
234 removeBadPods
.call(self
)
239 // ---------------------------------------------------------------------------
241 // Remove pods with a score of 0 (too many requests where they were unreachable)
242 function removeBadPods () {
246 function findBadPods (callback
) {
247 self
.sequelize
.models
.Pod
.listBadPods(function (err
, pods
) {
249 logger
.error('Cannot find bad pods.', { error: err
})
253 return callback(null, pods
)
257 function removeTheseBadPods (pods
, callback
) {
258 each(pods
, function (pod
, callbackEach
) {
259 pod
.destroy().asCallback(callbackEach
)
261 return callback(err
, pods
.length
)
264 ], function (err
, numberOfPodsRemoved
) {
266 logger
.error('Cannot remove bad pods.', { error: err
})
267 } else if (numberOfPodsRemoved
) {
268 logger
.info('Removed %d pods.', numberOfPodsRemoved
)
270 logger
.info('No need to remove bad pods.')