2017-05-15 15:22:03 -05:00
|
|
|
import { map } from 'lodash'
|
2017-05-22 13:58:25 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
2016-02-07 04:23:23 -06:00
|
|
|
|
2017-06-16 02:45:46 -05:00
|
|
|
import { FRIEND_SCORE, PODS_SCORE } from '../../initializers'
|
|
|
|
import { logger, isHostValid } from '../../helpers'
|
2016-02-07 04:23:23 -06:00
|
|
|
|
2017-10-19 02:43:01 -05:00
|
|
|
import { addMethodsToModel, getSort } from '../utils'
|
2017-05-22 13:58:25 -05:00
|
|
|
import {
|
|
|
|
PodInstance,
|
|
|
|
PodAttributes,
|
|
|
|
|
|
|
|
PodMethods
|
|
|
|
} from './pod-interface'
|
|
|
|
|
|
|
|
let Pod: Sequelize.Model<PodInstance, PodAttributes>
|
2017-08-25 04:45:31 -05:00
|
|
|
let toFormattedJSON: PodMethods.ToFormattedJSON
|
2017-05-22 13:58:25 -05:00
|
|
|
let countAll: PodMethods.CountAll
|
|
|
|
let incrementScores: PodMethods.IncrementScores
|
|
|
|
let list: PodMethods.List
|
2017-10-19 02:43:01 -05:00
|
|
|
let listForApi: PodMethods.ListForApi
|
2017-05-22 13:58:25 -05:00
|
|
|
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 10:35:32 -05:00
|
|
|
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
|
|
|
|
Pod = sequelize.define<PodInstance, PodAttributes>('Pod',
|
2016-12-11 14:50:51 -06:00
|
|
|
{
|
|
|
|
host: {
|
2016-12-28 08:49:23 -06:00
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
2017-07-11 10:04:57 -05:00
|
|
|
isHost: value => {
|
2017-05-15 15:22:03 -05:00
|
|
|
const res = isHostValid(value)
|
2016-12-28 08:49:23 -06:00
|
|
|
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,
|
2017-05-15 15:22:03 -05:00
|
|
|
defaultValue: FRIEND_SCORE.BASE,
|
2016-12-28 08:49:23 -06:00
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
isInt: true,
|
2017-05-15 15:22:03 -05:00
|
|
|
max: FRIEND_SCORE.MAX
|
2016-12-28 08:49:23 -06:00
|
|
|
}
|
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' ]
|
|
|
|
}
|
2017-05-22 13:58:25 -05:00
|
|
|
]
|
2016-12-11 14:50:51 -06:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
const classMethods = [
|
|
|
|
associate,
|
|
|
|
|
|
|
|
countAll,
|
|
|
|
incrementScores,
|
|
|
|
list,
|
2017-10-19 02:43:01 -05:00
|
|
|
listForApi,
|
2017-05-22 13:58:25 -05:00
|
|
|
listAllIds,
|
|
|
|
listRandomPodIdsWithRequest,
|
|
|
|
listBadPods,
|
|
|
|
load,
|
|
|
|
loadByHost,
|
|
|
|
updatePodsScore,
|
|
|
|
removeAll
|
|
|
|
]
|
2017-08-25 04:45:31 -05:00
|
|
|
const instanceMethods = [ toFormattedJSON ]
|
2017-05-22 13:58:25 -05:00
|
|
|
addMethodsToModel(Pod, classMethods, instanceMethods)
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
return Pod
|
2016-08-26 11:55:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------ METHODS ------------------------------
|
|
|
|
|
2017-08-25 04:45:31 -05:00
|
|
|
toFormattedJSON = function (this: PodInstance) {
|
2016-08-26 11:55:10 -05:00
|
|
|
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,
|
2017-06-16 02:54:59 -05:00
|
|
|
score: this.score as number,
|
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) {
|
2017-05-22 13:58:25 -05:00
|
|
|
Pod.belongsToMany(models.Request, {
|
2016-12-11 14:50:51 -06:00
|
|
|
foreignKey: 'podId',
|
|
|
|
through: models.RequestToPod,
|
2016-12-24 09:59:17 -06:00
|
|
|
onDelete: 'cascade'
|
2016-12-11 14:50:51 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
countAll = function () {
|
|
|
|
return Pod.count()
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-23 11:31:58 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
incrementScores = function (ids: number[], value: number) {
|
2016-12-11 14:50:51 -06:00
|
|
|
const update = {
|
2017-05-22 13:58:25 -05:00
|
|
|
score: Sequelize.literal('score +' + value)
|
2016-12-11 14:50:51 -06:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return Pod.update(update, options)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-23 11:31:58 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
list = function () {
|
|
|
|
return Pod.findAll()
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2017-10-19 02:43:01 -05:00
|
|
|
listForApi = function (start: number, count: number, sort: string) {
|
|
|
|
const query = {
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
|
|
|
order: [ getSort(sort) ]
|
|
|
|
}
|
|
|
|
|
|
|
|
return Pod.findAndCountAll(query).then(({ rows, count }) => {
|
|
|
|
return {
|
|
|
|
data: rows,
|
|
|
|
total: count
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
listAllIds = function (transaction: Sequelize.Transaction) {
|
2017-08-25 11:36:49 -05:00
|
|
|
const query = {
|
2017-07-05 06:26:25 -05:00
|
|
|
attributes: [ 'id' ],
|
|
|
|
transaction
|
2016-12-11 14:50:51 -06:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return Pod.findAll(query).then(pods => {
|
|
|
|
return map(pods, 'id')
|
2016-06-28 13:10:32 -05:00
|
|
|
})
|
2016-06-18 09:13:54 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
listRandomPodIdsWithRequest = function (limit: number, tableWithPods: string, tableWithPodsJoins: string) {
|
|
|
|
return Pod.count().then(count => {
|
2017-01-10 15:24:42 -06:00
|
|
|
// Optimization...
|
2017-07-05 06:26:25 -05:00
|
|
|
if (count === 0) return []
|
2017-01-10 15:24:42 -06:00
|
|
|
|
|
|
|
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: {
|
2017-08-25 11:36:49 -05:00
|
|
|
$in: Sequelize.literal(`(SELECT DISTINCT "${tableWithPods}"."podId" FROM "${tableWithPods}" ${tableWithPodsJoins})`)
|
2017-01-10 15:24:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return Pod.findAll(query).then(pods => {
|
|
|
|
return map(pods, 'id')
|
2017-01-10 15:24:42 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
listBadPods = function () {
|
2016-12-11 14:50:51 -06:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
score: { $lte: 0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return Pod.findAll(query)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
load = function (id: number) {
|
|
|
|
return Pod.findById(id)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
loadByHost = function (host: string) {
|
2016-12-11 14:50:51 -06:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
host: host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return Pod.findOne(query)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
removeAll = function () {
|
|
|
|
return Pod.destroy()
|
2016-06-30 15:39:08 -05:00
|
|
|
}
|
2017-02-21 14:35:59 -06:00
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
updatePodsScore = function (goodPods: number[], badPods: number[]) {
|
2017-02-21 14:35:59 -06:00
|
|
|
logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
|
|
|
|
|
|
|
|
if (goodPods.length !== 0) {
|
2017-07-05 06:26:25 -05:00
|
|
|
incrementScores(goodPods, PODS_SCORE.BONUS).catch(err => {
|
2017-07-07 11:26:12 -05:00
|
|
|
logger.error('Cannot increment scores of good pods.', err)
|
2017-02-21 14:35:59 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (badPods.length !== 0) {
|
2017-10-02 05:20:26 -05:00
|
|
|
incrementScores(badPods, PODS_SCORE.PENALTY)
|
2017-07-05 06:26:25 -05:00
|
|
|
.then(() => removeBadPods())
|
|
|
|
.catch(err => {
|
2017-07-07 11:26:12 -05:00
|
|
|
if (err) logger.error('Cannot decrement scores of bad pods.', err)
|
2017-07-05 06:26:25 -05:00
|
|
|
})
|
2017-02-21 14:35:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Remove pods with a score of 0 (too many requests where they were unreachable)
|
2017-10-25 09:03:33 -05:00
|
|
|
async function removeBadPods () {
|
|
|
|
try {
|
|
|
|
const pods = await listBadPods()
|
|
|
|
|
|
|
|
const podsRemovePromises = pods.map(pod => pod.destroy())
|
|
|
|
await Promise.all(podsRemovePromises)
|
|
|
|
|
|
|
|
const numberOfPodsRemoved = pods.length
|
|
|
|
|
|
|
|
if (numberOfPodsRemoved) {
|
|
|
|
logger.info('Removed %d pods.', numberOfPodsRemoved)
|
|
|
|
} else {
|
|
|
|
logger.info('No need to remove bad pods.')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot remove bad pods.', err)
|
|
|
|
}
|
2017-02-21 14:35:59 -06:00
|
|
|
}
|