diff options
Diffstat (limited to 'server/models/request.js')
-rw-r--r-- | server/models/request.js | 63 |
1 files changed, 2 insertions, 61 deletions
diff --git a/server/models/request.js b/server/models/request.js index ca616d130..de73501fc 100644 --- a/server/models/request.js +++ b/server/models/request.js | |||
@@ -1,11 +1,8 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const each = require('async/each') | ||
4 | const waterfall = require('async/waterfall') | ||
5 | const values = require('lodash/values') | 3 | const values = require('lodash/values') |
6 | 4 | ||
7 | const constants = require('../initializers/constants') | 5 | const constants = require('../initializers/constants') |
8 | const logger = require('../helpers/logger') | ||
9 | 6 | ||
10 | // --------------------------------------------------------------------------- | 7 | // --------------------------------------------------------------------------- |
11 | 8 | ||
@@ -28,8 +25,6 @@ module.exports = function (sequelize, DataTypes) { | |||
28 | listWithLimitAndRandom, | 25 | listWithLimitAndRandom, |
29 | 26 | ||
30 | countTotalRequests, | 27 | countTotalRequests, |
31 | removeBadPods, | ||
32 | updatePodsScore, | ||
33 | removeAll, | 28 | removeAll, |
34 | removeWithEmptyTo | 29 | removeWithEmptyTo |
35 | } | 30 | } |
@@ -60,71 +55,17 @@ function countTotalRequests (callback) { | |||
60 | return this.count(query).asCallback(callback) | 55 | return this.count(query).asCallback(callback) |
61 | } | 56 | } |
62 | 57 | ||
63 | // Remove pods with a score of 0 (too many requests where they were unreachable) | ||
64 | function removeBadPods () { | ||
65 | const self = this | ||
66 | |||
67 | waterfall([ | ||
68 | function findBadPods (callback) { | ||
69 | self.sequelize.models.Pod.listBadPods(function (err, pods) { | ||
70 | if (err) { | ||
71 | logger.error('Cannot find bad pods.', { error: err }) | ||
72 | return callback(err) | ||
73 | } | ||
74 | |||
75 | return callback(null, pods) | ||
76 | }) | ||
77 | }, | ||
78 | |||
79 | function removeTheseBadPods (pods, callback) { | ||
80 | each(pods, function (pod, callbackEach) { | ||
81 | pod.destroy().asCallback(callbackEach) | ||
82 | }, function (err) { | ||
83 | return callback(err, pods.length) | ||
84 | }) | ||
85 | } | ||
86 | ], function (err, numberOfPodsRemoved) { | ||
87 | if (err) { | ||
88 | logger.error('Cannot remove bad pods.', { error: err }) | ||
89 | } else if (numberOfPodsRemoved) { | ||
90 | logger.info('Removed %d pods.', numberOfPodsRemoved) | ||
91 | } else { | ||
92 | logger.info('No need to remove bad pods.') | ||
93 | } | ||
94 | }) | ||
95 | } | ||
96 | |||
97 | function updatePodsScore (goodPods, badPods) { | ||
98 | const self = this | ||
99 | const Pod = this.sequelize.models.Pod | ||
100 | |||
101 | logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) | ||
102 | |||
103 | if (goodPods.length !== 0) { | ||
104 | Pod.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) { | ||
105 | if (err) logger.error('Cannot increment scores of good pods.', { error: err }) | ||
106 | }) | ||
107 | } | ||
108 | |||
109 | if (badPods.length !== 0) { | ||
110 | Pod.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) { | ||
111 | if (err) logger.error('Cannot decrement scores of bad pods.', { error: err }) | ||
112 | removeBadPods.call(self) | ||
113 | }) | ||
114 | } | ||
115 | } | ||
116 | |||
117 | function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) { | 58 | function listWithLimitAndRandom (limitPods, limitRequestsPerPod, callback) { |
118 | const self = this | 59 | const self = this |
119 | const Pod = this.sequelize.models.Pod | 60 | const Pod = this.sequelize.models.Pod |
120 | 61 | ||
121 | Pod.listRandomPodIdsWithRequest(limitPods, function (err, podIds) { | 62 | Pod.listRandomPodIdsWithRequest(limitPods, 'RequestToPods', function (err, podIds) { |
122 | if (err) return callback(err) | 63 | if (err) return callback(err) |
123 | 64 | ||
124 | // We don't have friends that have requests | 65 | // We don't have friends that have requests |
125 | if (podIds.length === 0) return callback(null, []) | 66 | if (podIds.length === 0) return callback(null, []) |
126 | 67 | ||
127 | // The the first x requests of these pods | 68 | // The first x requests of these pods |
128 | // It is very important to sort by id ASC to keep the requests order! | 69 | // It is very important to sort by id ASC to keep the requests order! |
129 | const query = { | 70 | const query = { |
130 | order: [ | 71 | order: [ |