PeerTube/server/models/request.js

195 lines
4.7 KiB
JavaScript
Raw Normal View History

'use strict'
2016-07-18 17:17:52 +02:00
const each = require('async/each')
const waterfall = require('async/waterfall')
2016-12-28 15:49:23 +01:00
const values = require('lodash/values')
2016-03-16 22:29:27 +01:00
const constants = require('../initializers/constants')
const logger = require('../helpers/logger')
// ---------------------------------------------------------------------------
2016-12-11 21:50:51 +01:00
module.exports = function (sequelize, DataTypes) {
const Request = sequelize.define('Request',
{
request: {
2016-12-28 15:49:23 +01:00
type: DataTypes.JSON,
allowNull: false
2016-12-11 21:50:51 +01:00
},
endpoint: {
2016-12-28 15:49:23 +01:00
type: DataTypes.ENUM(values(constants.REQUEST_ENDPOINTS)),
allowNull: false
2016-12-11 21:50:51 +01:00
}
},
2016-11-01 18:47:57 +01:00
{
2016-12-11 21:50:51 +01:00
classMethods: {
associate,
listWithLimitAndRandom,
2016-12-11 21:50:51 +01:00
countTotalRequests,
removeBadPods,
updatePodsScore,
removeAll,
removeWithEmptyTo
2016-12-11 21:50:51 +01:00
}
2016-11-01 18:47:57 +01:00
}
2016-12-11 21:50:51 +01:00
)
2016-12-11 21:50:51 +01:00
return Request
}
// ------------------------------ STATICS ------------------------------
2016-12-11 21:50:51 +01:00
function associate (models) {
this.belongsToMany(models.Pod, {
foreignKey: {
name: 'requestId',
allowNull: false
},
through: models.RequestToPod,
onDelete: 'CASCADE'
})
}
function countTotalRequests (callback) {
const query = {
include: [ this.sequelize.models.Pod ]
}
return this.count(query).asCallback(callback)
}
// Remove pods with a score of 0 (too many requests where they were unreachable)
function removeBadPods () {
2016-12-11 21:50:51 +01:00
const self = this
2016-07-18 17:17:52 +02:00
waterfall([
function findBadPods (callback) {
2016-12-11 21:50:51 +01:00
self.sequelize.models.Pod.listBadPods(function (err, pods) {
if (err) {
logger.error('Cannot find bad pods.', { error: err })
return callback(err)
}
2015-12-06 22:40:30 +01:00
return callback(null, pods)
})
},
2015-12-06 22:40:30 +01:00
2016-10-21 11:20:45 +02:00
function removeTheseBadPods (pods, callback) {
2016-07-18 17:17:52 +02:00
each(pods, function (pod, callbackEach) {
2016-12-11 21:50:51 +01:00
pod.destroy().asCallback(callbackEach)
}, function (err) {
2016-10-21 11:20:45 +02:00
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.')
}
})
}
function updatePodsScore (goodPods, badPods) {
2016-12-11 21:50:51 +01:00
const self = this
const Pod = this.sequelize.models.Pod
logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
2016-12-11 21:50:51 +01:00
if (goodPods.length !== 0) {
Pod.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) {
2016-12-28 15:49:23 +01:00
if (err) logger.error('Cannot increment scores of good pods.', { error: err })
2016-12-11 21:50:51 +01:00
})
}
2016-02-05 19:02:05 +01:00
2016-12-11 21:50:51 +01:00
if (badPods.length !== 0) {
Pod.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) {
2016-12-28 15:49:23 +01:00
if (err) logger.error('Cannot decrement scores of bad pods.', { error: err })
2016-12-11 21:50:51 +01:00
removeBadPods.call(self)
})
}
}
2017-01-10 22:24:42 +01:00
function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) {
const self = this
2017-01-10 22:24:42 +01:00
const Pod = this.sequelize.models.Pod
2017-01-10 22:24:42 +01:00
Pod.listRandomPodIdsWithRequest(limitPods, function (err, podIds) {
if (err) return callback(err)
2017-01-10 22:24:42 +01:00
// We don't have friends that have requests
if (podIds.length === 0) return callback(null, [])
2017-01-10 22:24:42 +01:00
// The the first x requests of these pods
// It is very important to sort by id ASC to keep the requests order!
2016-12-11 21:50:51 +01:00
const query = {
order: [
[ 'id', 'ASC' ]
],
2017-01-10 22:24:42 +01:00
include: [
{
model: self.sequelize.models.Pod,
where: {
id: {
$in: podIds
}
}
}
]
2016-12-11 21:50:51 +01:00
}
2017-01-10 22:24:42 +01:00
self.findAll(query).asCallback(function (err, requests) {
if (err) return callback(err)
const requestsGrouped = groupAndTruncateRequests(requests, limitRequestsPerPod)
return callback(err, requestsGrouped)
})
})
}
function removeAll (callback) {
2016-12-11 21:50:51 +01:00
// Delete all requests
2016-12-24 16:59:17 +01:00
this.truncate({ cascade: true }).asCallback(callback)
}
function removeWithEmptyTo (callback) {
if (!callback) callback = function () {}
2016-12-11 21:50:51 +01:00
const query = {
where: {
id: {
$notIn: [
this.sequelize.literal('SELECT "requestId" FROM "RequestToPods"')
]
}
}
}
this.destroy(query).asCallback(callback)
}
// ---------------------------------------------------------------------------
function groupAndTruncateRequests (requests, limitRequestsPerPod) {
const requestsGrouped = {}
requests.forEach(function (request) {
request.Pods.forEach(function (pod) {
if (!requestsGrouped[pod.id]) requestsGrouped[pod.id] = []
if (requestsGrouped[pod.id].length < limitRequestsPerPod) {
requestsGrouped[pod.id].push({
request,
pod
})
}
})
})
return requestsGrouped
}