aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/pod.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/pod.js')
-rw-r--r--server/models/pod.js37
1 files changed, 37 insertions, 0 deletions
diff --git a/server/models/pod.js b/server/models/pod.js
index 8e7dd1fd8..b3c6db8e8 100644
--- a/server/models/pod.js
+++ b/server/models/pod.js
@@ -50,6 +50,7 @@ module.exports = function (sequelize, DataTypes) {
50 incrementScores, 50 incrementScores,
51 list, 51 list,
52 listAllIds, 52 listAllIds,
53 listRandomPodIdsWithRequest,
53 listBadPods, 54 listBadPods,
54 load, 55 load,
55 loadByHost, 56 loadByHost,
@@ -134,6 +135,42 @@ function listAllIds (transaction, callback) {
134 }) 135 })
135} 136}
136 137
138function listRandomPodIdsWithRequest (limit, callback) {
139 const self = this
140
141 self.count().asCallback(function (err, count) {
142 if (err) return callback(err)
143
144 // Optimization...
145 if (count === 0) return callback(null, [])
146
147 let start = Math.floor(Math.random() * count) - limit
148 if (start < 0) start = 0
149
150 const query = {
151 attributes: [ 'id' ],
152 order: [
153 [ 'id', 'ASC' ]
154 ],
155 offset: start,
156 limit: limit,
157 where: {
158 id: {
159 $in: [
160 this.sequelize.literal('SELECT "podId" FROM "RequestToPods"')
161 ]
162 }
163 }
164 }
165
166 return this.findAll(query).asCallback(function (err, pods) {
167 if (err) return callback(err)
168
169 return callback(null, map(pods, 'id'))
170 })
171 })
172}
173
137function listBadPods (callback) { 174function listBadPods (callback) {
138 const query = { 175 const query = {
139 where: { 176 where: {