]>
Commit | Line | Data |
---|---|---|
65fcc311 C |
1 | import { each, waterfall } from 'async' |
2 | import { map } from 'lodash' | |
e02643f3 | 3 | import * as Sequelize from 'sequelize' |
9f10b292 | 4 | |
65fcc311 C |
5 | import { FRIEND_SCORE, PODS_SCORE } from '../initializers' |
6 | import { logger, isHostValid } from '../helpers' | |
9f10b292 | 7 | |
e02643f3 C |
8 | import { addMethodsToModel } from './utils' |
9 | import { | |
10 | PodClass, | |
11 | PodInstance, | |
12 | PodAttributes, | |
13 | ||
14 | PodMethods | |
15 | } from './pod-interface' | |
16 | ||
17 | let Pod: Sequelize.Model<PodInstance, PodAttributes> | |
18 | let toFormatedJSON: PodMethods.ToFormatedJSON | |
19 | let countAll: PodMethods.CountAll | |
20 | let incrementScores: PodMethods.IncrementScores | |
21 | let list: PodMethods.List | |
22 | let listAllIds: PodMethods.ListAllIds | |
23 | let listRandomPodIdsWithRequest: PodMethods.ListRandomPodIdsWithRequest | |
24 | let listBadPods: PodMethods.ListBadPods | |
25 | let load: PodMethods.Load | |
26 | let loadByHost: PodMethods.LoadByHost | |
27 | let removeAll: PodMethods.RemoveAll | |
28 | let updatePodsScore: PodMethods.UpdatePodsScore | |
29 | ||
127944aa C |
30 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
31 | Pod = sequelize.define<PodInstance, PodAttributes>('Pod', | |
feb4bdfd C |
32 | { |
33 | host: { | |
67bf9b96 C |
34 | type: DataTypes.STRING, |
35 | allowNull: false, | |
36 | validate: { | |
37 | isHost: function (value) { | |
65fcc311 | 38 | const res = isHostValid(value) |
67bf9b96 C |
39 | if (res === false) throw new Error('Host not valid.') |
40 | } | |
41 | } | |
feb4bdfd C |
42 | }, |
43 | publicKey: { | |
67bf9b96 C |
44 | type: DataTypes.STRING(5000), |
45 | allowNull: false | |
feb4bdfd C |
46 | }, |
47 | score: { | |
48 | type: DataTypes.INTEGER, | |
65fcc311 | 49 | defaultValue: FRIEND_SCORE.BASE, |
67bf9b96 C |
50 | allowNull: false, |
51 | validate: { | |
52 | isInt: true, | |
65fcc311 | 53 | max: FRIEND_SCORE.MAX |
67bf9b96 | 54 | } |
4793c343 C |
55 | }, |
56 | email: { | |
57 | type: DataTypes.STRING(400), | |
ad4a8a1c C |
58 | allowNull: false, |
59 | validate: { | |
60 | isEmail: true | |
61 | } | |
feb4bdfd | 62 | } |
feb4bdfd C |
63 | }, |
64 | { | |
319d072e C |
65 | indexes: [ |
66 | { | |
5d67f289 C |
67 | fields: [ 'host' ], |
68 | unique: true | |
319d072e C |
69 | }, |
70 | { | |
71 | fields: [ 'score' ] | |
72 | } | |
e02643f3 | 73 | ] |
feb4bdfd C |
74 | } |
75 | ) | |
76 | ||
e02643f3 C |
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 | ||
feb4bdfd | 94 | return Pod |
53572423 C |
95 | } |
96 | ||
53572423 C |
97 | // ------------------------------ METHODS ------------------------------ |
98 | ||
e02643f3 | 99 | toFormatedJSON = function () { |
53572423 | 100 | const json = { |
feb4bdfd | 101 | id: this.id, |
49abbbbe | 102 | host: this.host, |
4793c343 | 103 | email: this.email, |
53572423 | 104 | score: this.score, |
feb4bdfd | 105 | createdAt: this.createdAt |
53572423 C |
106 | } |
107 | ||
108 | return json | |
109 | } | |
110 | ||
a3ee6fa2 C |
111 | // ------------------------------ Statics ------------------------------ |
112 | ||
feb4bdfd | 113 | function associate (models) { |
e02643f3 | 114 | Pod.belongsToMany(models.Request, { |
feb4bdfd C |
115 | foreignKey: 'podId', |
116 | through: models.RequestToPod, | |
7920c273 | 117 | onDelete: 'cascade' |
feb4bdfd C |
118 | }) |
119 | } | |
120 | ||
69818c93 | 121 | countAll = function (callback: PodMethods.CountAllCallback) { |
e02643f3 | 122 | return Pod.count().asCallback(callback) |
9f10b292 | 123 | } |
45239549 | 124 | |
69818c93 | 125 | incrementScores = function (ids: number[], value: number, callback?: PodMethods.IncrementScoresCallback) { |
65fcc311 | 126 | if (!callback) callback = function () { /* empty */ } |
feb4bdfd C |
127 | |
128 | const update = { | |
e02643f3 | 129 | score: Sequelize.literal('score +' + value) |
feb4bdfd C |
130 | } |
131 | ||
67bf9b96 | 132 | const options = { |
feb4bdfd C |
133 | where: { |
134 | id: { | |
135 | $in: ids | |
136 | } | |
67bf9b96 C |
137 | }, |
138 | // In this case score is a literal and not an integer so we do not validate it | |
139 | validate: false | |
feb4bdfd C |
140 | } |
141 | ||
e02643f3 | 142 | return Pod.update(update, options).asCallback(callback) |
9f10b292 | 143 | } |
45239549 | 144 | |
69818c93 | 145 | list = function (callback: PodMethods.ListCallback) { |
e02643f3 | 146 | return Pod.findAll().asCallback(callback) |
9f10b292 | 147 | } |
8c308c2b | 148 | |
69818c93 | 149 | listAllIds = function (transaction: Sequelize.Transaction, callback: PodMethods.ListAllIdsCallback) { |
65fcc311 | 150 | const query: any = { |
feb4bdfd C |
151 | attributes: [ 'id' ] |
152 | } | |
153 | ||
69818c93 | 154 | if (transaction !== null) query.transaction = transaction |
ed04d94f | 155 | |
69818c93 | 156 | return Pod.findAll(query).asCallback(function (err: Error, pods) { |
00057e85 C |
157 | if (err) return callback(err) |
158 | ||
feb4bdfd | 159 | return callback(null, map(pods, 'id')) |
00057e85 | 160 | }) |
528a9efa C |
161 | } |
162 | ||
69818c93 | 163 | listRandomPodIdsWithRequest = function (limit: number, tableWithPods: string, tableWithPodsJoins: string, callback: PodMethods.ListRandomPodIdsWithRequestCallback) { |
e02643f3 | 164 | Pod.count().asCallback(function (err, count) { |
bd14d16a C |
165 | if (err) return callback(err) |
166 | ||
167 | // Optimization... | |
168 | if (count === 0) return callback(null, []) | |
169 | ||
170 | let start = Math.floor(Math.random() * count) - limit | |
171 | if (start < 0) start = 0 | |
172 | ||
173 | const query = { | |
174 | attributes: [ 'id' ], | |
175 | order: [ | |
176 | [ 'id', 'ASC' ] | |
177 | ], | |
178 | offset: start, | |
179 | limit: limit, | |
180 | where: { | |
181 | id: { | |
182 | $in: [ | |
e02643f3 | 183 | Sequelize.literal(`SELECT DISTINCT "${tableWithPods}"."podId" FROM "${tableWithPods}" ${tableWithPodsJoins}`) |
bd14d16a C |
184 | ] |
185 | } | |
186 | } | |
187 | } | |
188 | ||
e02643f3 | 189 | return Pod.findAll(query).asCallback(function (err, pods) { |
bd14d16a C |
190 | if (err) return callback(err) |
191 | ||
192 | return callback(null, map(pods, 'id')) | |
193 | }) | |
194 | }) | |
195 | } | |
196 | ||
69818c93 | 197 | listBadPods = function (callback: PodMethods.ListBadPodsCallback) { |
feb4bdfd C |
198 | const query = { |
199 | where: { | |
200 | score: { $lte: 0 } | |
201 | } | |
202 | } | |
203 | ||
e02643f3 | 204 | return Pod.findAll(query).asCallback(callback) |
9f10b292 | 205 | } |
8c308c2b | 206 | |
69818c93 | 207 | load = function (id: number, callback: PodMethods.LoadCallback) { |
e02643f3 | 208 | return Pod.findById(id).asCallback(callback) |
9f10b292 | 209 | } |
c45f7f84 | 210 | |
69818c93 | 211 | loadByHost = function (host: string, callback: PodMethods.LoadByHostCallback) { |
feb4bdfd C |
212 | const query = { |
213 | where: { | |
214 | host: host | |
215 | } | |
216 | } | |
217 | ||
e02643f3 | 218 | return Pod.findOne(query).asCallback(callback) |
9f10b292 | 219 | } |
c45f7f84 | 220 | |
69818c93 | 221 | removeAll = function (callback: PodMethods.RemoveAllCallback) { |
e02643f3 | 222 | return Pod.destroy().asCallback(callback) |
a3ee6fa2 | 223 | } |
9e167724 | 224 | |
69818c93 | 225 | updatePodsScore = function (goodPods: number[], badPods: number[]) { |
9e167724 C |
226 | logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) |
227 | ||
228 | if (goodPods.length !== 0) { | |
e02643f3 | 229 | incrementScores(goodPods, PODS_SCORE.BONUS, function (err) { |
9e167724 C |
230 | if (err) logger.error('Cannot increment scores of good pods.', { error: err }) |
231 | }) | |
232 | } | |
233 | ||
234 | if (badPods.length !== 0) { | |
e02643f3 | 235 | incrementScores(badPods, PODS_SCORE.MALUS, function (err) { |
9e167724 | 236 | if (err) logger.error('Cannot decrement scores of bad pods.', { error: err }) |
e02643f3 | 237 | removeBadPods() |
9e167724 C |
238 | }) |
239 | } | |
240 | } | |
241 | ||
242 | // --------------------------------------------------------------------------- | |
243 | ||
244 | // Remove pods with a score of 0 (too many requests where they were unreachable) | |
245 | function removeBadPods () { | |
9e167724 C |
246 | waterfall([ |
247 | function findBadPods (callback) { | |
e02643f3 | 248 | listBadPods(function (err, pods) { |
9e167724 C |
249 | if (err) { |
250 | logger.error('Cannot find bad pods.', { error: err }) | |
251 | return callback(err) | |
252 | } | |
253 | ||
254 | return callback(null, pods) | |
255 | }) | |
256 | }, | |
257 | ||
258 | function removeTheseBadPods (pods, callback) { | |
65fcc311 | 259 | each(pods, function (pod: any, callbackEach) { |
9e167724 C |
260 | pod.destroy().asCallback(callbackEach) |
261 | }, function (err) { | |
262 | return callback(err, pods.length) | |
263 | }) | |
264 | } | |
265 | ], function (err, numberOfPodsRemoved) { | |
266 | if (err) { | |
267 | logger.error('Cannot remove bad pods.', { error: err }) | |
268 | } else if (numberOfPodsRemoved) { | |
269 | logger.info('Removed %d pods.', numberOfPodsRemoved) | |
270 | } else { | |
271 | logger.info('No need to remove bad pods.') | |
272 | } | |
273 | }) | |
274 | } |