]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/pod/pod.ts
Remove sequelize deprecated operators
[github/Chocobozzz/PeerTube.git] / server / models / pod / pod.ts
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 [Sequelize.Op.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 subQuery = `(SELECT DISTINCT "${tableWithPods}"."podId" FROM "${tableWithPods}" ${tableWithPodsJoins})`
182 const query = {
183 attributes: [ 'id' ],
184 order: [
185 [ 'id', 'ASC' ]
186 ],
187 offset: start,
188 limit: limit,
189 where: {
190 id: {
191 [Sequelize.Op.in]: Sequelize.literal(subQuery)
192 }
193 }
194 }
195
196 return Pod.findAll(query).then(pods => {
197 return map(pods, 'id')
198 })
199 })
200 }
201
202 listBadPods = function () {
203 const query = {
204 where: {
205 score: {
206 [Sequelize.Op.lte]: 0
207 }
208 }
209 }
210
211 return Pod.findAll(query)
212 }
213
214 load = function (id: number) {
215 return Pod.findById(id)
216 }
217
218 loadByHost = function (host: string) {
219 const query = {
220 where: {
221 host: host
222 }
223 }
224
225 return Pod.findOne(query)
226 }
227
228 removeAll = function () {
229 return Pod.destroy()
230 }
231
232 updatePodsScore = function (goodPods: number[], badPods: number[]) {
233 logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
234
235 if (goodPods.length !== 0) {
236 incrementScores(goodPods, PODS_SCORE.BONUS).catch(err => {
237 logger.error('Cannot increment scores of good pods.', err)
238 })
239 }
240
241 if (badPods.length !== 0) {
242 incrementScores(badPods, PODS_SCORE.PENALTY)
243 .then(() => removeBadPods())
244 .catch(err => {
245 if (err) logger.error('Cannot decrement scores of bad pods.', err)
246 })
247 }
248 }
249
250 // ---------------------------------------------------------------------------
251
252 // Remove pods with a score of 0 (too many requests where they were unreachable)
253 async function removeBadPods () {
254 try {
255 const pods = await listBadPods()
256
257 const podsRemovePromises = pods.map(pod => pod.destroy())
258 await Promise.all(podsRemovePromises)
259
260 const numberOfPodsRemoved = pods.length
261
262 if (numberOfPodsRemoved) {
263 logger.info('Removed %d pods.', numberOfPodsRemoved)
264 } else {
265 logger.info('No need to remove bad pods.')
266 }
267 } catch (err) {
268 logger.error('Cannot remove bad pods.', err)
269 }
270 }