X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fpod%2Fpod.ts;h=6619726afcf397f1f64e1f6d7b43c14e89d38096;hb=f5028693a896a3076dd286ac0030e3d8f78f5ebf;hp=e1088977f14bb503a9102a8cca5574b0a84c6bac;hpb=075f16caac5236cb04c98ae7b3a989766d764bb3;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/pod/pod.ts b/server/models/pod/pod.ts index e1088977f..6619726af 100644 --- a/server/models/pod/pod.ts +++ b/server/models/pod/pod.ts @@ -4,7 +4,7 @@ import * as Sequelize from 'sequelize' import { FRIEND_SCORE, PODS_SCORE } from '../../initializers' import { logger, isHostValid } from '../../helpers' -import { addMethodsToModel } from '../utils' +import { addMethodsToModel, getSort } from '../utils' import { PodInstance, PodAttributes, @@ -13,10 +13,11 @@ import { } from './pod-interface' let Pod: Sequelize.Model -let toFormatedJSON: PodMethods.ToFormatedJSON +let toFormattedJSON: PodMethods.ToFormattedJSON 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 @@ -78,6 +79,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da countAll, incrementScores, list, + listForApi, listAllIds, listRandomPodIdsWithRequest, listBadPods, @@ -86,7 +88,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da updatePodsScore, removeAll ] - const instanceMethods = [ toFormatedJSON ] + const instanceMethods = [ toFormattedJSON ] addMethodsToModel(Pod, classMethods, instanceMethods) return Pod @@ -94,7 +96,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da // ------------------------------ METHODS ------------------------------ -toFormatedJSON = function (this: PodInstance) { +toFormattedJSON = function (this: PodInstance) { const json = { id: this.id, host: this.host, @@ -142,8 +144,23 @@ list = function () { 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: Sequelize.FindOptions = { + const query = { attributes: [ 'id' ], transaction } @@ -170,9 +187,7 @@ listRandomPodIdsWithRequest = function (limit: number, tableWithPods: string, ta limit: limit, where: { id: { - $in: [ - Sequelize.literal(`SELECT DISTINCT "${tableWithPods}"."podId" FROM "${tableWithPods}" ${tableWithPodsJoins}`) - ] + $in: Sequelize.literal(`(SELECT DISTINCT "${tableWithPods}"."podId" FROM "${tableWithPods}" ${tableWithPodsJoins})`) } } } @@ -221,7 +236,7 @@ updatePodsScore = function (goodPods: number[], badPods: number[]) { } if (badPods.length !== 0) { - incrementScores(badPods, PODS_SCORE.MALUS) + incrementScores(badPods, PODS_SCORE.PENALTY) .then(() => removeBadPods()) .catch(err => { if (err) logger.error('Cannot decrement scores of bad pods.', err) @@ -232,20 +247,21 @@ updatePodsScore = function (goodPods: number[], badPods: number[]) { // --------------------------------------------------------------------------- // 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) + } }