aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/server/server.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-23 17:36:15 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:53 +0100
commit39445ead45aaaea801ec09991b8dd2464f722e47 (patch)
tree9306c6b89115dabdd4a7c31a59784134a0e1c804 /server/models/server/server.ts
parent16b90975941b78d01d7202d441bf731a10048c16 (diff)
downloadPeerTube-39445ead45aaaea801ec09991b8dd2464f722e47.tar.gz
PeerTube-39445ead45aaaea801ec09991b8dd2464f722e47.tar.zst
PeerTube-39445ead45aaaea801ec09991b8dd2464f722e47.zip
Cleanup models
Diffstat (limited to 'server/models/server/server.ts')
-rw-r--r--server/models/server/server.ts181
1 files changed, 36 insertions, 145 deletions
diff --git a/server/models/server/server.ts b/server/models/server/server.ts
index 26fa87550..75cd5f929 100644
--- a/server/models/server/server.ts
+++ b/server/models/server/server.ts
@@ -1,29 +1,11 @@
1import { map } from 'lodash'
2import * as Sequelize from 'sequelize' 1import * as Sequelize from 'sequelize'
3 2import { isHostValid, logger } from '../../helpers'
4import { FRIEND_SCORE, SERVERS_SCORE } from '../../initializers' 3import { FRIEND_SCORE, SERVERS_SCORE } from '../../initializers'
5import { logger, isHostValid } from '../../helpers' 4import { addMethodsToModel } from '../utils'
6 5import { ServerAttributes, ServerInstance, ServerMethods } from './server-interface'
7import { addMethodsToModel, getSort } from '../utils'
8import {
9 ServerInstance,
10 ServerAttributes,
11
12 ServerMethods
13} from './server-interface'
14 6
15let Server: Sequelize.Model<ServerInstance, ServerAttributes> 7let Server: Sequelize.Model<ServerInstance, ServerAttributes>
16let countAll: ServerMethods.CountAll 8let updateServersScoreAndRemoveBadOnes: ServerMethods.UpdateServersScoreAndRemoveBadOnes
17let incrementScores: ServerMethods.IncrementScores
18let list: ServerMethods.List
19let listForApi: ServerMethods.ListForApi
20let listAllIds: ServerMethods.ListAllIds
21let listRandomServerIdsWithRequest: ServerMethods.ListRandomServerIdsWithRequest
22let listBadServers: ServerMethods.ListBadServers
23let load: ServerMethods.Load
24let loadByHost: ServerMethods.LoadByHost
25let removeAll: ServerMethods.RemoveAll
26let updateServersScore: ServerMethods.UpdateServersScore
27 9
28export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { 10export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
29 Server = sequelize.define<ServerInstance, ServerAttributes>('Server', 11 Server = sequelize.define<ServerInstance, ServerAttributes>('Server',
@@ -62,17 +44,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
62 ) 44 )
63 45
64 const classMethods = [ 46 const classMethods = [
65 countAll, 47 listBadServers
66 incrementScores,
67 list,
68 listForApi,
69 listAllIds,
70 listRandomServerIdsWithRequest,
71 listBadServers,
72 load,
73 loadByHost,
74 updateServersScore,
75 removeAll
76 ] 48 ]
77 addMethodsToModel(Server, classMethods) 49 addMethodsToModel(Server, classMethods)
78 50
@@ -81,118 +53,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
81 53
82// ------------------------------ Statics ------------------------------ 54// ------------------------------ Statics ------------------------------
83 55
84countAll = function () { 56updateServersScoreAndRemoveBadOnes = function (goodServers: number[], badServers: number[]) {
85 return Server.count()
86}
87
88incrementScores = function (ids: number[], value: number) {
89 const update = {
90 score: Sequelize.literal('score +' + value)
91 }
92
93 const options = {
94 where: {
95 id: {
96 [Sequelize.Op.in]: ids
97 }
98 },
99 // In this case score is a literal and not an integer so we do not validate it
100 validate: false
101 }
102
103 return Server.update(update, options)
104}
105
106list = function () {
107 return Server.findAll()
108}
109
110listForApi = function (start: number, count: number, sort: string) {
111 const query = {
112 offset: start,
113 limit: count,
114 order: [ getSort(sort) ]
115 }
116
117 return Server.findAndCountAll(query).then(({ rows, count }) => {
118 return {
119 data: rows,
120 total: count
121 }
122 })
123}
124
125listAllIds = function (transaction: Sequelize.Transaction) {
126 const query = {
127 attributes: [ 'id' ],
128 transaction
129 }
130
131 return Server.findAll(query).then(servers => {
132 return map(servers, 'id')
133 })
134}
135
136listRandomServerIdsWithRequest = function (limit: number, tableWithServers: string, tableWithServersJoins: string) {
137 return Server.count().then(count => {
138 // Optimization...
139 if (count === 0) return []
140
141 let start = Math.floor(Math.random() * count) - limit
142 if (start < 0) start = 0
143
144 const subQuery = `(SELECT DISTINCT "${tableWithServers}"."serverId" FROM "${tableWithServers}" ${tableWithServersJoins})`
145 const query = {
146 attributes: [ 'id' ],
147 order: [
148 [ 'id', 'ASC' ]
149 ],
150 offset: start,
151 limit: limit,
152 where: {
153 id: {
154 [Sequelize.Op.in]: Sequelize.literal(subQuery)
155 }
156 }
157 }
158
159 return Server.findAll(query).then(servers => {
160 return map(servers, 'id')
161 })
162 })
163}
164
165listBadServers = function () {
166 const query = {
167 where: {
168 score: {
169 [Sequelize.Op.lte]: 0
170 }
171 }
172 }
173
174 return Server.findAll(query)
175}
176
177load = function (id: number) {
178 return Server.findById(id)
179}
180
181loadByHost = function (host: string) {
182 const query = {
183 where: {
184 host: host
185 }
186 }
187
188 return Server.findOne(query)
189}
190
191removeAll = function () {
192 return Server.destroy()
193}
194
195updateServersScore = function (goodServers: number[], badServers: number[]) {
196 logger.info('Updating %d good servers and %d bad servers scores.', goodServers.length, badServers.length) 57 logger.info('Updating %d good servers and %d bad servers scores.', goodServers.length, badServers.length)
197 58
198 if (goodServers.length !== 0) { 59 if (goodServers.length !== 0) {
@@ -231,3 +92,33 @@ async function removeBadServers () {
231 logger.error('Cannot remove bad servers.', err) 92 logger.error('Cannot remove bad servers.', err)
232 } 93 }
233} 94}
95
96function incrementScores (ids: number[], value: number) {
97 const update = {
98 score: Sequelize.literal('score +' + value)
99 }
100
101 const options = {
102 where: {
103 id: {
104 [Sequelize.Op.in]: ids
105 }
106 },
107 // In this case score is a literal and not an integer so we do not validate it
108 validate: false
109 }
110
111 return Server.update(update, options)
112}
113
114function listBadServers () {
115 const query = {
116 where: {
117 score: {
118 [Sequelize.Op.lte]: 0
119 }
120 }
121 }
122
123 return Server.findAll(query)
124}