import { FRIEND_SCORE, PODS_SCORE } from '../../initializers'
import { logger, isHostValid } from '../../helpers'
-import { addMethodsToModel } from '../utils'
+import { addMethodsToModel, getSort } from '../utils'
import {
PodInstance,
PodAttributes,
let countAll: PodMethods.CountAll
let incrementScores: PodMethods.IncrementScores
let list: PodMethods.List
+let listForApi: PodMethods.ListForApi
let listAllIds: PodMethods.ListAllIds
let listRandomPodIdsWithRequest: PodMethods.ListRandomPodIdsWithRequest
let listBadPods: PodMethods.ListBadPods
}
}
},
- publicKey: {
- type: DataTypes.STRING(5000),
- allowNull: false
- },
score: {
type: DataTypes.INTEGER,
defaultValue: FRIEND_SCORE.BASE,
isInt: true,
max: FRIEND_SCORE.MAX
}
- },
- email: {
- type: DataTypes.STRING(400),
- allowNull: false,
- validate: {
- isEmail: true
- }
}
},
{
)
const classMethods = [
- associate,
-
countAll,
incrementScores,
list,
+ listForApi,
listAllIds,
listRandomPodIdsWithRequest,
listBadPods,
const json = {
id: this.id,
host: this.host,
- email: this.email,
score: this.score as number,
createdAt: this.createdAt
}
// ------------------------------ Statics ------------------------------
-function associate (models) {
- Pod.belongsToMany(models.Request, {
- foreignKey: 'podId',
- through: models.RequestToPod,
- onDelete: 'cascade'
- })
-}
-
countAll = function () {
return Pod.count()
}
const options = {
where: {
id: {
- $in: ids
+ [Sequelize.Op.in]: ids
}
},
// In this case score is a literal and not an integer so we do not validate it
return Pod.findAll()
}
+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
+ }
+ })
+}
+
listAllIds = function (transaction: Sequelize.Transaction) {
const query = {
attributes: [ 'id' ],
let start = Math.floor(Math.random() * count) - limit
if (start < 0) start = 0
+ const subQuery = `(SELECT DISTINCT "${tableWithPods}"."podId" FROM "${tableWithPods}" ${tableWithPodsJoins})`
const query = {
attributes: [ 'id' ],
order: [
limit: limit,
where: {
id: {
- $in: Sequelize.literal(`(SELECT DISTINCT "${tableWithPods}"."podId" FROM "${tableWithPods}" ${tableWithPodsJoins})`)
+ [Sequelize.Op.in]: Sequelize.literal(subQuery)
}
}
}
listBadPods = function () {
const query = {
where: {
- score: { $lte: 0 }
+ score: {
+ [Sequelize.Op.lte]: 0
+ }
}
}
// ---------------------------------------------------------------------------
// Remove pods with a score of 0 (too many requests where they were unreachable)
-function removeBadPods () {
- return listBadPods()
- .then(pods => {
- const podsRemovePromises = pods.map(pod => pod.destroy())
- return Promise.all(podsRemovePromises).then(() => pods.length)
- })
- .then(numberOfPodsRemoved => {
- 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)
- })
+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)
+ }
}