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