2016-02-07 04:23:23 -06:00
|
|
|
'use strict'
|
|
|
|
|
2017-02-21 14:35:59 -06:00
|
|
|
const each = require('async/each')
|
2016-06-28 13:10:32 -05:00
|
|
|
const map = require('lodash/map')
|
2017-02-21 14:35:59 -06:00
|
|
|
const waterfall = require('async/waterfall')
|
2016-02-07 04:23:23 -06:00
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const constants = require('../initializers/constants')
|
2017-02-21 14:35:59 -06:00
|
|
|
const logger = require('../helpers/logger')
|
2016-12-28 08:49:23 -06:00
|
|
|
const customPodsValidators = require('../helpers/custom-validators').pods
|
2016-02-07 04:23:23 -06:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
module.exports = function (sequelize, DataTypes) {
|
|
|
|
const Pod = sequelize.define('Pod',
|
|
|
|
{
|
|
|
|
host: {
|
2016-12-28 08:49:23 -06:00
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
isHost: function (value) {
|
|
|
|
const res = customPodsValidators.isHostValid(value)
|
|
|
|
if (res === false) throw new Error('Host not valid.')
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 14:50:51 -06:00
|
|
|
},
|
|
|
|
publicKey: {
|
2016-12-28 08:49:23 -06:00
|
|
|
type: DataTypes.STRING(5000),
|
|
|
|
allowNull: false
|
2016-12-11 14:50:51 -06:00
|
|
|
},
|
|
|
|
score: {
|
|
|
|
type: DataTypes.INTEGER,
|
2016-12-28 08:49:23 -06:00
|
|
|
defaultValue: constants.FRIEND_SCORE.BASE,
|
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
isInt: true,
|
|
|
|
max: constants.FRIEND_SCORE.MAX
|
|
|
|
}
|
2017-02-16 12:19:56 -06:00
|
|
|
},
|
|
|
|
email: {
|
|
|
|
type: DataTypes.STRING(400),
|
2017-02-18 02:29:59 -06:00
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
isEmail: true
|
|
|
|
}
|
2016-12-11 14:50:51 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2016-12-29 02:33:28 -06:00
|
|
|
indexes: [
|
|
|
|
{
|
2017-02-16 12:24:34 -06:00
|
|
|
fields: [ 'host' ],
|
|
|
|
unique: true
|
2016-12-29 02:33:28 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'score' ]
|
|
|
|
}
|
|
|
|
],
|
2016-12-11 14:50:51 -06:00
|
|
|
classMethods: {
|
|
|
|
associate,
|
|
|
|
|
|
|
|
countAll,
|
|
|
|
incrementScores,
|
|
|
|
list,
|
|
|
|
listAllIds,
|
2017-01-10 15:24:42 -06:00
|
|
|
listRandomPodIdsWithRequest,
|
2016-12-11 14:50:51 -06:00
|
|
|
listBadPods,
|
|
|
|
load,
|
|
|
|
loadByHost,
|
2017-02-21 14:35:59 -06:00
|
|
|
updatePodsScore,
|
2016-12-11 14:50:51 -06:00
|
|
|
removeAll
|
|
|
|
},
|
|
|
|
instanceMethods: {
|
|
|
|
toFormatedJSON
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return Pod
|
2016-08-26 11:55:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------ METHODS ------------------------------
|
|
|
|
|
|
|
|
function toFormatedJSON () {
|
|
|
|
const json = {
|
2016-12-11 14:50:51 -06:00
|
|
|
id: this.id,
|
2016-11-14 13:03:04 -06:00
|
|
|
host: this.host,
|
2017-02-16 12:19:56 -06:00
|
|
|
email: this.email,
|
2016-08-26 11:55:10 -05:00
|
|
|
score: this.score,
|
2016-12-11 14:50:51 -06:00
|
|
|
createdAt: this.createdAt
|
2016-08-26 11:55:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
// ------------------------------ Statics ------------------------------
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
function associate (models) {
|
|
|
|
this.belongsToMany(models.Request, {
|
|
|
|
foreignKey: 'podId',
|
|
|
|
through: models.RequestToPod,
|
2016-12-24 09:59:17 -06:00
|
|
|
onDelete: 'cascade'
|
2016-12-11 14:50:51 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
function countAll (callback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
return this.count().asCallback(callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-23 11:31:58 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function incrementScores (ids, value, callback) {
|
|
|
|
if (!callback) callback = function () {}
|
2016-12-11 14:50:51 -06:00
|
|
|
|
|
|
|
const update = {
|
|
|
|
score: this.sequelize.literal('score +' + value)
|
|
|
|
}
|
|
|
|
|
2016-12-28 08:49:23 -06:00
|
|
|
const options = {
|
2016-12-11 14:50:51 -06:00
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
$in: ids
|
|
|
|
}
|
2016-12-28 08:49:23 -06:00
|
|
|
},
|
|
|
|
// In this case score is a literal and not an integer so we do not validate it
|
|
|
|
validate: false
|
2016-12-11 14:50:51 -06:00
|
|
|
}
|
|
|
|
|
2016-12-28 08:49:23 -06:00
|
|
|
return this.update(update, options).asCallback(callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-23 11:31:58 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function list (callback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
return this.findAll().asCallback(callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2017-01-06 16:24:47 -06:00
|
|
|
function listAllIds (transaction, callback) {
|
|
|
|
if (!callback) {
|
|
|
|
callback = transaction
|
|
|
|
transaction = null
|
|
|
|
}
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
const query = {
|
|
|
|
attributes: [ 'id' ]
|
|
|
|
}
|
|
|
|
|
2017-01-06 16:24:47 -06:00
|
|
|
if (transaction) query.transaction = transaction
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
return this.findAll(query).asCallback(function (err, pods) {
|
2016-06-28 13:10:32 -05:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
return callback(null, map(pods, 'id'))
|
2016-06-28 13:10:32 -05:00
|
|
|
})
|
2016-06-18 09:13:54 -05:00
|
|
|
}
|
|
|
|
|
2017-02-26 11:57:33 -06:00
|
|
|
function listRandomPodIdsWithRequest (limit, tableWithPods, tableWithPodsJoins, callback) {
|
|
|
|
if (!callback) {
|
|
|
|
callback = tableWithPodsJoins
|
|
|
|
tableWithPodsJoins = ''
|
|
|
|
}
|
|
|
|
|
2017-01-10 15:24:42 -06:00
|
|
|
const self = this
|
|
|
|
|
|
|
|
self.count().asCallback(function (err, count) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
// Optimization...
|
|
|
|
if (count === 0) return callback(null, [])
|
|
|
|
|
|
|
|
let start = Math.floor(Math.random() * count) - limit
|
|
|
|
if (start < 0) start = 0
|
|
|
|
|
|
|
|
const query = {
|
|
|
|
attributes: [ 'id' ],
|
|
|
|
order: [
|
|
|
|
[ 'id', 'ASC' ]
|
|
|
|
],
|
|
|
|
offset: start,
|
|
|
|
limit: limit,
|
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
$in: [
|
2017-02-26 11:57:33 -06:00
|
|
|
this.sequelize.literal(`SELECT DISTINCT "${tableWithPods}"."podId" FROM "${tableWithPods}" ${tableWithPodsJoins}`)
|
2017-01-10 15:24:42 -06:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.findAll(query).asCallback(function (err, pods) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
return callback(null, map(pods, 'id'))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
function listBadPods (callback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
score: { $lte: 0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.findAll(query).asCallback(callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
function load (id, callback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
return this.findById(id).asCallback(callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-11-14 13:03:04 -06:00
|
|
|
function loadByHost (host, callback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
host: host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.findOne(query).asCallback(callback)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-06-30 15:39:08 -05:00
|
|
|
function removeAll (callback) {
|
2016-12-11 14:50:51 -06:00
|
|
|
return this.destroy().asCallback(callback)
|
2016-06-30 15:39:08 -05:00
|
|
|
}
|
2017-02-21 14:35:59 -06:00
|
|
|
|
|
|
|
function updatePodsScore (goodPods, badPods) {
|
|
|
|
const self = this
|
|
|
|
|
|
|
|
logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
|
|
|
|
|
|
|
|
if (goodPods.length !== 0) {
|
|
|
|
this.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) {
|
|
|
|
if (err) logger.error('Cannot increment scores of good pods.', { error: err })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (badPods.length !== 0) {
|
|
|
|
this.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) {
|
|
|
|
if (err) logger.error('Cannot decrement scores of bad pods.', { error: err })
|
|
|
|
removeBadPods.call(self)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Remove pods with a score of 0 (too many requests where they were unreachable)
|
|
|
|
function removeBadPods () {
|
|
|
|
const self = this
|
|
|
|
|
|
|
|
waterfall([
|
|
|
|
function findBadPods (callback) {
|
|
|
|
self.sequelize.models.Pod.listBadPods(function (err, pods) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot find bad pods.', { error: err })
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, pods)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
function removeTheseBadPods (pods, callback) {
|
|
|
|
each(pods, function (pod, callbackEach) {
|
|
|
|
pod.destroy().asCallback(callbackEach)
|
|
|
|
}, function (err) {
|
|
|
|
return callback(err, pods.length)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
], function (err, numberOfPodsRemoved) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot remove bad pods.', { error: err })
|
|
|
|
} else if (numberOfPodsRemoved) {
|
|
|
|
logger.info('Removed %d pods.', numberOfPodsRemoved)
|
|
|
|
} else {
|
|
|
|
logger.info('No need to remove bad pods.')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|