2017-05-15 16:22:03 -04:00
|
|
|
import { each, waterfall } from 'async'
|
|
|
|
import { map } from 'lodash'
|
2017-05-22 14:58:25 -04:00
|
|
|
import * as Sequelize from 'sequelize'
|
2016-02-07 05:23:23 -05:00
|
|
|
|
2017-06-16 03:45:46 -04:00
|
|
|
import { FRIEND_SCORE, PODS_SCORE } from '../../initializers'
|
|
|
|
import { logger, isHostValid } from '../../helpers'
|
2016-02-07 05:23:23 -05:00
|
|
|
|
2017-06-16 03:45:46 -04:00
|
|
|
import { addMethodsToModel } from '../utils'
|
2017-05-22 14:58:25 -04:00
|
|
|
import {
|
|
|
|
PodClass,
|
|
|
|
PodInstance,
|
|
|
|
PodAttributes,
|
|
|
|
|
|
|
|
PodMethods
|
|
|
|
} from './pod-interface'
|
|
|
|
|
|
|
|
let Pod: Sequelize.Model<PodInstance, PodAttributes>
|
|
|
|
let toFormatedJSON: PodMethods.ToFormatedJSON
|
|
|
|
let countAll: PodMethods.CountAll
|
|
|
|
let incrementScores: PodMethods.IncrementScores
|
|
|
|
let list: PodMethods.List
|
|
|
|
let listAllIds: PodMethods.ListAllIds
|
|
|
|
let listRandomPodIdsWithRequest: PodMethods.ListRandomPodIdsWithRequest
|
|
|
|
let listBadPods: PodMethods.ListBadPods
|
|
|
|
let load: PodMethods.Load
|
|
|
|
let loadByHost: PodMethods.LoadByHost
|
|
|
|
let removeAll: PodMethods.RemoveAll
|
|
|
|
let updatePodsScore: PodMethods.UpdatePodsScore
|
|
|
|
|
2017-06-11 11:35:32 -04:00
|
|
|
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
|
|
|
|
Pod = sequelize.define<PodInstance, PodAttributes>('Pod',
|
2016-12-11 15:50:51 -05:00
|
|
|
{
|
|
|
|
host: {
|
2016-12-28 09:49:23 -05:00
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
isHost: function (value) {
|
2017-05-15 16:22:03 -04:00
|
|
|
const res = isHostValid(value)
|
2016-12-28 09:49:23 -05:00
|
|
|
if (res === false) throw new Error('Host not valid.')
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 15:50:51 -05:00
|
|
|
},
|
|
|
|
publicKey: {
|
2016-12-28 09:49:23 -05:00
|
|
|
type: DataTypes.STRING(5000),
|
|
|
|
allowNull: false
|
2016-12-11 15:50:51 -05:00
|
|
|
},
|
|
|
|
score: {
|
|
|
|
type: DataTypes.INTEGER,
|
2017-05-15 16:22:03 -04:00
|
|
|
defaultValue: FRIEND_SCORE.BASE,
|
2016-12-28 09:49:23 -05:00
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
isInt: true,
|
2017-05-15 16:22:03 -04:00
|
|
|
max: FRIEND_SCORE.MAX
|
2016-12-28 09:49:23 -05:00
|
|
|
}
|
2017-02-16 13:19:56 -05:00
|
|
|
},
|
|
|
|
email: {
|
|
|
|
type: DataTypes.STRING(400),
|
2017-02-18 03:29:59 -05:00
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
isEmail: true
|
|
|
|
}
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2016-12-29 03:33:28 -05:00
|
|
|
indexes: [
|
|
|
|
{
|
2017-02-16 13:24:34 -05:00
|
|
|
fields: [ 'host' ],
|
|
|
|
unique: true
|
2016-12-29 03:33:28 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'score' ]
|
|
|
|
}
|
2017-05-22 14:58:25 -04:00
|
|
|
]
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
const classMethods = [
|
|
|
|
associate,
|
|
|
|
|
|
|
|
countAll,
|
|
|
|
incrementScores,
|
|
|
|
list,
|
|
|
|
listAllIds,
|
|
|
|
listRandomPodIdsWithRequest,
|
|
|
|
listBadPods,
|
|
|
|
load,
|
|
|
|
loadByHost,
|
|
|
|
updatePodsScore,
|
|
|
|
removeAll
|
|
|
|
]
|
|
|
|
const instanceMethods = [ toFormatedJSON ]
|
|
|
|
addMethodsToModel(Pod, classMethods, instanceMethods)
|
|
|
|
|
2016-12-11 15:50:51 -05:00
|
|
|
return Pod
|
2016-08-26 12:55:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------ METHODS ------------------------------
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
toFormatedJSON = function () {
|
2016-08-26 12:55:10 -04:00
|
|
|
const json = {
|
2016-12-11 15:50:51 -05:00
|
|
|
id: this.id,
|
2016-11-14 14:03:04 -05:00
|
|
|
host: this.host,
|
2017-02-16 13:19:56 -05:00
|
|
|
email: this.email,
|
2016-08-26 12:55:10 -04:00
|
|
|
score: this.score,
|
2016-12-11 15:50:51 -05:00
|
|
|
createdAt: this.createdAt
|
2016-08-26 12:55:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
2016-06-30 16:39:08 -04:00
|
|
|
// ------------------------------ Statics ------------------------------
|
|
|
|
|
2016-12-11 15:50:51 -05:00
|
|
|
function associate (models) {
|
2017-05-22 14:58:25 -04:00
|
|
|
Pod.belongsToMany(models.Request, {
|
2016-12-11 15:50:51 -05:00
|
|
|
foreignKey: 'podId',
|
|
|
|
through: models.RequestToPod,
|
2016-12-24 10:59:17 -05:00
|
|
|
onDelete: 'cascade'
|
2016-12-11 15:50:51 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
countAll = function (callback: PodMethods.CountAllCallback) {
|
2017-05-22 14:58:25 -04:00
|
|
|
return Pod.count().asCallback(callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-01-23 12:31:58 -05:00
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
incrementScores = function (ids: number[], value: number, callback?: PodMethods.IncrementScoresCallback) {
|
2017-05-15 16:22:03 -04:00
|
|
|
if (!callback) callback = function () { /* empty */ }
|
2016-12-11 15:50:51 -05:00
|
|
|
|
|
|
|
const update = {
|
2017-05-22 14:58:25 -04:00
|
|
|
score: Sequelize.literal('score +' + value)
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
|
2016-12-28 09:49:23 -05:00
|
|
|
const options = {
|
2016-12-11 15:50:51 -05:00
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
$in: ids
|
|
|
|
}
|
2016-12-28 09:49:23 -05:00
|
|
|
},
|
|
|
|
// In this case score is a literal and not an integer so we do not validate it
|
|
|
|
validate: false
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
return Pod.update(update, options).asCallback(callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-01-23 12:31:58 -05:00
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
list = function (callback: PodMethods.ListCallback) {
|
2017-05-22 14:58:25 -04:00
|
|
|
return Pod.findAll().asCallback(callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2015-06-09 11:41:40 -04:00
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
listAllIds = function (transaction: Sequelize.Transaction, callback: PodMethods.ListAllIdsCallback) {
|
2017-05-15 16:22:03 -04:00
|
|
|
const query: any = {
|
2016-12-11 15:50:51 -05:00
|
|
|
attributes: [ 'id' ]
|
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
if (transaction !== null) query.transaction = transaction
|
2017-01-06 17:24:47 -05:00
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
return Pod.findAll(query).asCallback(function (err: Error, pods) {
|
2016-06-28 14:10:32 -04:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
2016-12-11 15:50:51 -05:00
|
|
|
return callback(null, map(pods, 'id'))
|
2016-06-28 14:10:32 -04:00
|
|
|
})
|
2016-06-18 10:13:54 -04:00
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
listRandomPodIdsWithRequest = function (limit: number, tableWithPods: string, tableWithPodsJoins: string, callback: PodMethods.ListRandomPodIdsWithRequestCallback) {
|
2017-05-22 14:58:25 -04:00
|
|
|
Pod.count().asCallback(function (err, count) {
|
2017-01-10 16:24:42 -05:00
|
|
|
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-05-22 14:58:25 -04:00
|
|
|
Sequelize.literal(`SELECT DISTINCT "${tableWithPods}"."podId" FROM "${tableWithPods}" ${tableWithPodsJoins}`)
|
2017-01-10 16:24:42 -05:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
return Pod.findAll(query).asCallback(function (err, pods) {
|
2017-01-10 16:24:42 -05:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
return callback(null, map(pods, 'id'))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
listBadPods = function (callback: PodMethods.ListBadPodsCallback) {
|
2016-12-11 15:50:51 -05:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
score: { $lte: 0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
return Pod.findAll(query).asCallback(callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2015-06-09 11:41:40 -04:00
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
load = function (id: number, callback: PodMethods.LoadCallback) {
|
2017-05-22 14:58:25 -04:00
|
|
|
return Pod.findById(id).asCallback(callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
loadByHost = function (host: string, callback: PodMethods.LoadByHostCallback) {
|
2016-12-11 15:50:51 -05:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
host: host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
return Pod.findOne(query).asCallback(callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-01-31 05:23:52 -05:00
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
removeAll = function (callback: PodMethods.RemoveAllCallback) {
|
2017-05-22 14:58:25 -04:00
|
|
|
return Pod.destroy().asCallback(callback)
|
2016-06-30 16:39:08 -04:00
|
|
|
}
|
2017-02-21 15:35:59 -05:00
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
updatePodsScore = function (goodPods: number[], badPods: number[]) {
|
2017-02-21 15:35:59 -05:00
|
|
|
logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
|
|
|
|
|
|
|
|
if (goodPods.length !== 0) {
|
2017-05-22 14:58:25 -04:00
|
|
|
incrementScores(goodPods, PODS_SCORE.BONUS, function (err) {
|
2017-02-21 15:35:59 -05:00
|
|
|
if (err) logger.error('Cannot increment scores of good pods.', { error: err })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (badPods.length !== 0) {
|
2017-05-22 14:58:25 -04:00
|
|
|
incrementScores(badPods, PODS_SCORE.MALUS, function (err) {
|
2017-02-21 15:35:59 -05:00
|
|
|
if (err) logger.error('Cannot decrement scores of bad pods.', { error: err })
|
2017-05-22 14:58:25 -04:00
|
|
|
removeBadPods()
|
2017-02-21 15:35:59 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Remove pods with a score of 0 (too many requests where they were unreachable)
|
|
|
|
function removeBadPods () {
|
|
|
|
waterfall([
|
|
|
|
function findBadPods (callback) {
|
2017-05-22 14:58:25 -04:00
|
|
|
listBadPods(function (err, pods) {
|
2017-02-21 15:35:59 -05:00
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot find bad pods.', { error: err })
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, pods)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
function removeTheseBadPods (pods, callback) {
|
2017-05-15 16:22:03 -04:00
|
|
|
each(pods, function (pod: any, callbackEach) {
|
2017-02-21 15:35:59 -05:00
|
|
|
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.')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|