]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/pod/pod.ts
Begin activitypub
[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 score: {
43 type: DataTypes.INTEGER,
44 defaultValue: FRIEND_SCORE.BASE,
45 allowNull: false,
46 validate: {
47 isInt: true,
48 max: FRIEND_SCORE.MAX
49 }
50 }
51 },
52 {
53 indexes: [
54 {
55 fields: [ 'host' ],
56 unique: true
57 },
58 {
59 fields: [ 'score' ]
60 }
61 ]
62 }
63 )
64
65 const classMethods = [
66 associate,
67
68 countAll,
69 incrementScores,
70 list,
71 listForApi,
72 listAllIds,
73 listRandomPodIdsWithRequest,
74 listBadPods,
75 load,
76 loadByHost,
77 updatePodsScore,
78 removeAll
79 ]
80 const instanceMethods = [ toFormattedJSON ]
81 addMethodsToModel(Pod, classMethods, instanceMethods)
82
83 return Pod
84 }
85
86 // ------------------------------ METHODS ------------------------------
87
88 toFormattedJSON = function (this: PodInstance) {
89 const json = {
90 id: this.id,
91 host: this.host,
92 score: this.score as number,
93 createdAt: this.createdAt
94 }
95
96 return json
97 }
98
99 // ------------------------------ Statics ------------------------------
100
101 function associate (models) {
102 Pod.belongsToMany(models.Request, {
103 foreignKey: 'podId',
104 through: models.RequestToPod,
105 onDelete: 'cascade'
106 })
107 }
108
109 countAll = function () {
110 return Pod.count()
111 }
112
113 incrementScores = function (ids: number[], value: number) {
114 const update = {
115 score: Sequelize.literal('score +' + value)
116 }
117
118 const options = {
119 where: {
120 id: {
121 [Sequelize.Op.in]: ids
122 }
123 },
124 // In this case score is a literal and not an integer so we do not validate it
125 validate: false
126 }
127
128 return Pod.update(update, options)
129 }
130
131 list = function () {
132 return Pod.findAll()
133 }
134
135 listForApi = function (start: number, count: number, sort: string) {
136 const query = {
137 offset: start,
138 limit: count,
139 order: [ getSort(sort) ]
140 }
141
142 return Pod.findAndCountAll(query).then(({ rows, count }) => {
143 return {
144 data: rows,
145 total: count
146 }
147 })
148 }
149
150 listAllIds = function (transaction: Sequelize.Transaction) {
151 const query = {
152 attributes: [ 'id' ],
153 transaction
154 }
155
156 return Pod.findAll(query).then(pods => {
157 return map(pods, 'id')
158 })
159 }
160
161 listRandomPodIdsWithRequest = function (limit: number, tableWithPods: string, tableWithPodsJoins: string) {
162 return Pod.count().then(count => {
163 // Optimization...
164 if (count === 0) return []
165
166 let start = Math.floor(Math.random() * count) - limit
167 if (start < 0) start = 0
168
169 const subQuery = `(SELECT DISTINCT "${tableWithPods}"."podId" FROM "${tableWithPods}" ${tableWithPodsJoins})`
170 const query = {
171 attributes: [ 'id' ],
172 order: [
173 [ 'id', 'ASC' ]
174 ],
175 offset: start,
176 limit: limit,
177 where: {
178 id: {
179 [Sequelize.Op.in]: Sequelize.literal(subQuery)
180 }
181 }
182 }
183
184 return Pod.findAll(query).then(pods => {
185 return map(pods, 'id')
186 })
187 })
188 }
189
190 listBadPods = function () {
191 const query = {
192 where: {
193 score: {
194 [Sequelize.Op.lte]: 0
195 }
196 }
197 }
198
199 return Pod.findAll(query)
200 }
201
202 load = function (id: number) {
203 return Pod.findById(id)
204 }
205
206 loadByHost = function (host: string) {
207 const query = {
208 where: {
209 host: host
210 }
211 }
212
213 return Pod.findOne(query)
214 }
215
216 removeAll = function () {
217 return Pod.destroy()
218 }
219
220 updatePodsScore = function (goodPods: number[], badPods: number[]) {
221 logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
222
223 if (goodPods.length !== 0) {
224 incrementScores(goodPods, PODS_SCORE.BONUS).catch(err => {
225 logger.error('Cannot increment scores of good pods.', err)
226 })
227 }
228
229 if (badPods.length !== 0) {
230 incrementScores(badPods, PODS_SCORE.PENALTY)
231 .then(() => removeBadPods())
232 .catch(err => {
233 if (err) logger.error('Cannot decrement scores of bad pods.', err)
234 })
235 }
236 }
237
238 // ---------------------------------------------------------------------------
239
240 // Remove pods with a score of 0 (too many requests where they were unreachable)
241 async function removeBadPods () {
242 try {
243 const pods = await listBadPods()
244
245 const podsRemovePromises = pods.map(pod => pod.destroy())
246 await Promise.all(podsRemovePromises)
247
248 const numberOfPodsRemoved = pods.length
249
250 if (numberOfPodsRemoved) {
251 logger.info('Removed %d pods.', numberOfPodsRemoved)
252 } else {
253 logger.info('No need to remove bad pods.')
254 }
255 } catch (err) {
256 logger.error('Cannot remove bad pods.', err)
257 }
258 }