aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/pod/pod.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-15 11:00:25 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:51 +0100
commit608624252466acf9f1d9ee1c1170bd4fe4d18d18 (patch)
tree47eab55bb5421b7fe88e0b2ac743a436fd9561cf /server/models/pod/pod.ts
parent51548b31815c6f96f314ae96588a9adca150519d (diff)
downloadPeerTube-608624252466acf9f1d9ee1c1170bd4fe4d18d18.tar.gz
PeerTube-608624252466acf9f1d9ee1c1170bd4fe4d18d18.tar.zst
PeerTube-608624252466acf9f1d9ee1c1170bd4fe4d18d18.zip
Rename Pod -> Server
Diffstat (limited to 'server/models/pod/pod.ts')
-rw-r--r--server/models/pod/pod.ts248
1 files changed, 0 insertions, 248 deletions
diff --git a/server/models/pod/pod.ts b/server/models/pod/pod.ts
deleted file mode 100644
index 6d270ad7f..000000000
--- a/server/models/pod/pod.ts
+++ /dev/null
@@ -1,248 +0,0 @@
1import { map } from 'lodash'
2import * as Sequelize from 'sequelize'
3
4import { FRIEND_SCORE, PODS_SCORE } from '../../initializers'
5import { logger, isHostValid } from '../../helpers'
6
7import { addMethodsToModel, getSort } from '../utils'
8import {
9 PodInstance,
10 PodAttributes,
11
12 PodMethods
13} from './pod-interface'
14
15let Pod: Sequelize.Model<PodInstance, PodAttributes>
16let toFormattedJSON: PodMethods.ToFormattedJSON
17let countAll: PodMethods.CountAll
18let incrementScores: PodMethods.IncrementScores
19let list: PodMethods.List
20let listForApi: PodMethods.ListForApi
21let listAllIds: PodMethods.ListAllIds
22let listRandomPodIdsWithRequest: PodMethods.ListRandomPodIdsWithRequest
23let listBadPods: PodMethods.ListBadPods
24let load: PodMethods.Load
25let loadByHost: PodMethods.LoadByHost
26let removeAll: PodMethods.RemoveAll
27let updatePodsScore: PodMethods.UpdatePodsScore
28
29export 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 countAll,
67 incrementScores,
68 list,
69 listForApi,
70 listAllIds,
71 listRandomPodIdsWithRequest,
72 listBadPods,
73 load,
74 loadByHost,
75 updatePodsScore,
76 removeAll
77 ]
78 const instanceMethods = [ toFormattedJSON ]
79 addMethodsToModel(Pod, classMethods, instanceMethods)
80
81 return Pod
82}
83
84// ------------------------------ METHODS ------------------------------
85
86toFormattedJSON = function (this: PodInstance) {
87 const json = {
88 id: this.id,
89 host: this.host,
90 score: this.score as number,
91 createdAt: this.createdAt
92 }
93
94 return json
95}
96
97// ------------------------------ Statics ------------------------------
98
99countAll = function () {
100 return Pod.count()
101}
102
103incrementScores = function (ids: number[], value: number) {
104 const update = {
105 score: Sequelize.literal('score +' + value)
106 }
107
108 const options = {
109 where: {
110 id: {
111 [Sequelize.Op.in]: ids
112 }
113 },
114 // In this case score is a literal and not an integer so we do not validate it
115 validate: false
116 }
117
118 return Pod.update(update, options)
119}
120
121list = function () {
122 return Pod.findAll()
123}
124
125listForApi = function (start: number, count: number, sort: string) {
126 const query = {
127 offset: start,
128 limit: count,
129 order: [ getSort(sort) ]
130 }
131
132 return Pod.findAndCountAll(query).then(({ rows, count }) => {
133 return {
134 data: rows,
135 total: count
136 }
137 })
138}
139
140listAllIds = function (transaction: Sequelize.Transaction) {
141 const query = {
142 attributes: [ 'id' ],
143 transaction
144 }
145
146 return Pod.findAll(query).then(pods => {
147 return map(pods, 'id')
148 })
149}
150
151listRandomPodIdsWithRequest = function (limit: number, tableWithPods: string, tableWithPodsJoins: string) {
152 return Pod.count().then(count => {
153 // Optimization...
154 if (count === 0) return []
155
156 let start = Math.floor(Math.random() * count) - limit
157 if (start < 0) start = 0
158
159 const subQuery = `(SELECT DISTINCT "${tableWithPods}"."podId" FROM "${tableWithPods}" ${tableWithPodsJoins})`
160 const query = {
161 attributes: [ 'id' ],
162 order: [
163 [ 'id', 'ASC' ]
164 ],
165 offset: start,
166 limit: limit,
167 where: {
168 id: {
169 [Sequelize.Op.in]: Sequelize.literal(subQuery)
170 }
171 }
172 }
173
174 return Pod.findAll(query).then(pods => {
175 return map(pods, 'id')
176 })
177 })
178}
179
180listBadPods = function () {
181 const query = {
182 where: {
183 score: {
184 [Sequelize.Op.lte]: 0
185 }
186 }
187 }
188
189 return Pod.findAll(query)
190}
191
192load = function (id: number) {
193 return Pod.findById(id)
194}
195
196loadByHost = function (host: string) {
197 const query = {
198 where: {
199 host: host
200 }
201 }
202
203 return Pod.findOne(query)
204}
205
206removeAll = function () {
207 return Pod.destroy()
208}
209
210updatePodsScore = function (goodPods: number[], badPods: number[]) {
211 logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length)
212
213 if (goodPods.length !== 0) {
214 incrementScores(goodPods, PODS_SCORE.BONUS).catch(err => {
215 logger.error('Cannot increment scores of good pods.', err)
216 })
217 }
218
219 if (badPods.length !== 0) {
220 incrementScores(badPods, PODS_SCORE.PENALTY)
221 .then(() => removeBadPods())
222 .catch(err => {
223 if (err) logger.error('Cannot decrement scores of bad pods.', err)
224 })
225 }
226}
227
228// ---------------------------------------------------------------------------
229
230// Remove pods with a score of 0 (too many requests where they were unreachable)
231async function removeBadPods () {
232 try {
233 const pods = await listBadPods()
234
235 const podsRemovePromises = pods.map(pod => pod.destroy())
236 await Promise.all(podsRemovePromises)
237
238 const numberOfPodsRemoved = pods.length
239
240 if (numberOfPodsRemoved) {
241 logger.info('Removed %d pods.', numberOfPodsRemoved)
242 } else {
243 logger.info('No need to remove bad pods.')
244 }
245 } catch (err) {
246 logger.error('Cannot remove bad pods.', err)
247 }
248}