aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/pod.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/pod.ts')
-rw-r--r--server/models/pod.ts127
1 files changed, 71 insertions, 56 deletions
diff --git a/server/models/pod.ts b/server/models/pod.ts
index 0e0262978..2df32e4a4 100644
--- a/server/models/pod.ts
+++ b/server/models/pod.ts
@@ -1,13 +1,34 @@
1import { each, waterfall } from 'async' 1import { each, waterfall } from 'async'
2import { map } from 'lodash' 2import { map } from 'lodash'
3import * as Sequelize from 'sequelize'
3 4
4import { FRIEND_SCORE, PODS_SCORE } from '../initializers' 5import { FRIEND_SCORE, PODS_SCORE } from '../initializers'
5import { logger, isHostValid } from '../helpers' 6import { logger, isHostValid } from '../helpers'
6 7
7// --------------------------------------------------------------------------- 8import { addMethodsToModel } from './utils'
8 9import {
9module.exports = function (sequelize, DataTypes) { 10 PodClass,
10 const Pod = sequelize.define('Pod', 11 PodInstance,
12 PodAttributes,
13
14 PodMethods
15} from './pod-interface'
16
17let Pod: Sequelize.Model<PodInstance, PodAttributes>
18let toFormatedJSON: PodMethods.ToFormatedJSON
19let countAll: PodMethods.CountAll
20let incrementScores: PodMethods.IncrementScores
21let list: PodMethods.List
22let listAllIds: PodMethods.ListAllIds
23let listRandomPodIdsWithRequest: PodMethods.ListRandomPodIdsWithRequest
24let listBadPods: PodMethods.ListBadPods
25let load: PodMethods.Load
26let loadByHost: PodMethods.LoadByHost
27let removeAll: PodMethods.RemoveAll
28let updatePodsScore: PodMethods.UpdatePodsScore
29
30export default function (sequelize, DataTypes) {
31 Pod = sequelize.define('Pod',
11 { 32 {
12 host: { 33 host: {
13 type: DataTypes.STRING, 34 type: DataTypes.STRING,
@@ -49,33 +70,33 @@ module.exports = function (sequelize, DataTypes) {
49 { 70 {
50 fields: [ 'score' ] 71 fields: [ 'score' ]
51 } 72 }
52 ], 73 ]
53 classMethods: {
54 associate,
55
56 countAll,
57 incrementScores,
58 list,
59 listAllIds,
60 listRandomPodIdsWithRequest,
61 listBadPods,
62 load,
63 loadByHost,
64 updatePodsScore,
65 removeAll
66 },
67 instanceMethods: {
68 toFormatedJSON
69 }
70 } 74 }
71 ) 75 )
72 76
77 const classMethods = [
78 associate,
79
80 countAll,
81 incrementScores,
82 list,
83 listAllIds,
84 listRandomPodIdsWithRequest,
85 listBadPods,
86 load,
87 loadByHost,
88 updatePodsScore,
89 removeAll
90 ]
91 const instanceMethods = [ toFormatedJSON ]
92 addMethodsToModel(Pod, classMethods, instanceMethods)
93
73 return Pod 94 return Pod
74} 95}
75 96
76// ------------------------------ METHODS ------------------------------ 97// ------------------------------ METHODS ------------------------------
77 98
78function toFormatedJSON () { 99toFormatedJSON = function () {
79 const json = { 100 const json = {
80 id: this.id, 101 id: this.id,
81 host: this.host, 102 host: this.host,
@@ -90,22 +111,22 @@ function toFormatedJSON () {
90// ------------------------------ Statics ------------------------------ 111// ------------------------------ Statics ------------------------------
91 112
92function associate (models) { 113function associate (models) {
93 this.belongsToMany(models.Request, { 114 Pod.belongsToMany(models.Request, {
94 foreignKey: 'podId', 115 foreignKey: 'podId',
95 through: models.RequestToPod, 116 through: models.RequestToPod,
96 onDelete: 'cascade' 117 onDelete: 'cascade'
97 }) 118 })
98} 119}
99 120
100function countAll (callback) { 121countAll = function (callback) {
101 return this.count().asCallback(callback) 122 return Pod.count().asCallback(callback)
102} 123}
103 124
104function incrementScores (ids, value, callback) { 125incrementScores = function (ids, value, callback) {
105 if (!callback) callback = function () { /* empty */ } 126 if (!callback) callback = function () { /* empty */ }
106 127
107 const update = { 128 const update = {
108 score: this.sequelize.literal('score +' + value) 129 score: Sequelize.literal('score +' + value)
109 } 130 }
110 131
111 const options = { 132 const options = {
@@ -118,14 +139,14 @@ function incrementScores (ids, value, callback) {
118 validate: false 139 validate: false
119 } 140 }
120 141
121 return this.update(update, options).asCallback(callback) 142 return Pod.update(update, options).asCallback(callback)
122} 143}
123 144
124function list (callback) { 145list = function (callback) {
125 return this.findAll().asCallback(callback) 146 return Pod.findAll().asCallback(callback)
126} 147}
127 148
128function listAllIds (transaction, callback) { 149listAllIds = function (transaction, callback) {
129 if (!callback) { 150 if (!callback) {
130 callback = transaction 151 callback = transaction
131 transaction = null 152 transaction = null
@@ -137,22 +158,20 @@ function listAllIds (transaction, callback) {
137 158
138 if (transaction) query.transaction = transaction 159 if (transaction) query.transaction = transaction
139 160
140 return this.findAll(query).asCallback(function (err, pods) { 161 return Pod.findAll(query).asCallback(function (err, pods) {
141 if (err) return callback(err) 162 if (err) return callback(err)
142 163
143 return callback(null, map(pods, 'id')) 164 return callback(null, map(pods, 'id'))
144 }) 165 })
145} 166}
146 167
147function listRandomPodIdsWithRequest (limit, tableWithPods, tableWithPodsJoins, callback) { 168listRandomPodIdsWithRequest = function (limit, tableWithPods, tableWithPodsJoins, callback) {
148 if (!callback) { 169 if (!callback) {
149 callback = tableWithPodsJoins 170 callback = tableWithPodsJoins
150 tableWithPodsJoins = '' 171 tableWithPodsJoins = ''
151 } 172 }
152 173
153 const self = this 174 Pod.count().asCallback(function (err, count) {
154
155 self.count().asCallback(function (err, count) {
156 if (err) return callback(err) 175 if (err) return callback(err)
157 176
158 // Optimization... 177 // Optimization...
@@ -171,13 +190,13 @@ function listRandomPodIdsWithRequest (limit, tableWithPods, tableWithPodsJoins,
171 where: { 190 where: {
172 id: { 191 id: {
173 $in: [ 192 $in: [
174 this.sequelize.literal(`SELECT DISTINCT "${tableWithPods}"."podId" FROM "${tableWithPods}" ${tableWithPodsJoins}`) 193 Sequelize.literal(`SELECT DISTINCT "${tableWithPods}"."podId" FROM "${tableWithPods}" ${tableWithPodsJoins}`)
175 ] 194 ]
176 } 195 }
177 } 196 }
178 } 197 }
179 198
180 return this.findAll(query).asCallback(function (err, pods) { 199 return Pod.findAll(query).asCallback(function (err, pods) {
181 if (err) return callback(err) 200 if (err) return callback(err)
182 201
183 return callback(null, map(pods, 'id')) 202 return callback(null, map(pods, 'id'))
@@ -185,49 +204,47 @@ function listRandomPodIdsWithRequest (limit, tableWithPods, tableWithPodsJoins,
185 }) 204 })
186} 205}
187 206
188function listBadPods (callback) { 207listBadPods = function (callback) {
189 const query = { 208 const query = {
190 where: { 209 where: {
191 score: { $lte: 0 } 210 score: { $lte: 0 }
192 } 211 }
193 } 212 }
194 213
195 return this.findAll(query).asCallback(callback) 214 return Pod.findAll(query).asCallback(callback)
196} 215}
197 216
198function load (id, callback) { 217load = function (id, callback) {
199 return this.findById(id).asCallback(callback) 218 return Pod.findById(id).asCallback(callback)
200} 219}
201 220
202function loadByHost (host, callback) { 221loadByHost = function (host, callback) {
203 const query = { 222 const query = {
204 where: { 223 where: {
205 host: host 224 host: host
206 } 225 }
207 } 226 }
208 227
209 return this.findOne(query).asCallback(callback) 228 return Pod.findOne(query).asCallback(callback)
210} 229}
211 230
212function removeAll (callback) { 231removeAll = function (callback) {
213 return this.destroy().asCallback(callback) 232 return Pod.destroy().asCallback(callback)
214} 233}
215 234
216function updatePodsScore (goodPods, badPods) { 235updatePodsScore = function (goodPods, badPods) {
217 const self = this
218
219 logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) 236 logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
220 237
221 if (goodPods.length !== 0) { 238 if (goodPods.length !== 0) {
222 this.incrementScores(goodPods, PODS_SCORE.BONUS, function (err) { 239 incrementScores(goodPods, PODS_SCORE.BONUS, function (err) {
223 if (err) logger.error('Cannot increment scores of good pods.', { error: err }) 240 if (err) logger.error('Cannot increment scores of good pods.', { error: err })
224 }) 241 })
225 } 242 }
226 243
227 if (badPods.length !== 0) { 244 if (badPods.length !== 0) {
228 this.incrementScores(badPods, PODS_SCORE.MALUS, function (err) { 245 incrementScores(badPods, PODS_SCORE.MALUS, function (err) {
229 if (err) logger.error('Cannot decrement scores of bad pods.', { error: err }) 246 if (err) logger.error('Cannot decrement scores of bad pods.', { error: err })
230 removeBadPods.call(self) 247 removeBadPods()
231 }) 248 })
232 } 249 }
233} 250}
@@ -236,11 +253,9 @@ function updatePodsScore (goodPods, badPods) {
236 253
237// Remove pods with a score of 0 (too many requests where they were unreachable) 254// Remove pods with a score of 0 (too many requests where they were unreachable)
238function removeBadPods () { 255function removeBadPods () {
239 const self = this
240
241 waterfall([ 256 waterfall([
242 function findBadPods (callback) { 257 function findBadPods (callback) {
243 self.sequelize.models.Pod.listBadPods(function (err, pods) { 258 listBadPods(function (err, pods) {
244 if (err) { 259 if (err) {
245 logger.error('Cannot find bad pods.', { error: err }) 260 logger.error('Cannot find bad pods.', { error: err })
246 return callback(err) 261 return callback(err)