1 import { each, waterfall } from 'async'
2 import { map } from 'lodash'
4 import { FRIEND_SCORE, PODS_SCORE } from '../initializers'
5 import { logger, isHostValid } from '../helpers'
7 // ---------------------------------------------------------------------------
9 module.exports = function (sequelize, DataTypes) {
10 const Pod = sequelize.define('Pod',
13 type: DataTypes.STRING,
16 isHost: function (value) {
17 const res = isHostValid(value)
18 if (res === false) throw new Error('Host not valid.')
23 type: DataTypes.STRING(5000),
27 type: DataTypes.INTEGER,
28 defaultValue: FRIEND_SCORE.BASE,
36 type: DataTypes.STRING(400),
60 listRandomPodIdsWithRequest,
76 // ------------------------------ METHODS ------------------------------
78 function toFormatedJSON () {
84 createdAt: this.createdAt
90 // ------------------------------ Statics ------------------------------
92 function associate (models) {
93 this.belongsToMany(models.Request, {
95 through: models.RequestToPod,
100 function countAll (callback) {
101 return this.count().asCallback(callback)
104 function incrementScores (ids, value, callback) {
105 if (!callback) callback = function () { /* empty */ }
108 score: this.sequelize.literal('score +' + value)
117 // In this case score is a literal and not an integer so we do not validate it
121 return this.update(update, options).asCallback(callback)
124 function list (callback) {
125 return this.findAll().asCallback(callback)
128 function listAllIds (transaction, callback) {
130 callback = transaction
138 if (transaction) query.transaction = transaction
140 return this.findAll(query).asCallback(function (err, pods) {
141 if (err) return callback(err)
143 return callback(null, map(pods, 'id'))
147 function listRandomPodIdsWithRequest (limit, tableWithPods, tableWithPodsJoins, callback) {
149 callback = tableWithPodsJoins
150 tableWithPodsJoins = ''
155 self.count().asCallback(function (err, count) {
156 if (err) return callback(err)
159 if (count === 0) return callback(null, [])
161 let start = Math.floor(Math.random() * count) - limit
162 if (start < 0) start = 0
165 attributes: [ 'id' ],
174 this.sequelize.literal(`SELECT DISTINCT "${tableWithPods}"."podId" FROM "${tableWithPods}" ${tableWithPodsJoins}`)
180 return this.findAll(query).asCallback(function (err, pods) {
181 if (err) return callback(err)
183 return callback(null, map(pods, 'id'))
188 function listBadPods (callback) {
195 return this.findAll(query).asCallback(callback)
198 function load (id, callback) {
199 return this.findById(id).asCallback(callback)
202 function loadByHost (host, callback) {
209 return this.findOne(query).asCallback(callback)
212 function removeAll (callback) {
213 return this.destroy().asCallback(callback)
216 function updatePodsScore (goodPods, badPods) {
219 logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
221 if (goodPods.length !== 0) {
222 this.incrementScores(goodPods, PODS_SCORE.BONUS, function (err) {
223 if (err) logger.error('Cannot increment scores of good pods.', { error: err })
227 if (badPods.length !== 0) {
228 this.incrementScores(badPods, PODS_SCORE.MALUS, function (err) {
229 if (err) logger.error('Cannot decrement scores of bad pods.', { error: err })
230 removeBadPods.call(self)
235 // ---------------------------------------------------------------------------
237 // Remove pods with a score of 0 (too many requests where they were unreachable)
238 function removeBadPods () {
242 function findBadPods (callback) {
243 self.sequelize.models.Pod.listBadPods(function (err, pods) {
245 logger.error('Cannot find bad pods.', { error: err })
249 return callback(null, pods)
253 function removeTheseBadPods (pods, callback) {
254 each(pods, function (pod: any, callbackEach) {
255 pod.destroy().asCallback(callbackEach)
257 return callback(err, pods.length)
260 ], function (err, numberOfPodsRemoved) {
262 logger.error('Cannot remove bad pods.', { error: err })
263 } else if (numberOfPodsRemoved) {
264 logger.info('Removed %d pods.', numberOfPodsRemoved)
266 logger.info('No need to remove bad pods.')