forked from ivanseidel/tournamenter-obr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyncModule.js
247 lines (201 loc) · 5.87 KB
/
SyncModule.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
var TAG = _TAG('SyncModule')
var request = require('request')
exports.default = {
url: null,
sync: false,
method: 'POST',
interval: 600,
}
/*
* Initializes deamon based on database configuration
*/
exports.init = function (app) {
// Generate 'obr-sync' config in database
// app.models.Config.destroy({id: 'obr-sync'}, console.log)
app.models.Config.findOrCreate({id: 'obr-sync'}, _.defaults({
id:'obr-sync',
}, exports.default), function (err, config){
if (err) {
return console.log('Falha ao criar configuração da `obr-sync`!');
}
console.log(TAG, 'init:', config.sync ? 'Ligado' : 'Desligado')
exports.updateDeamon(config)
})
app.models.Config.findOrCreate(_.defaults({
id:'obr-last-sync',
}, {}), function (err, config){
if (err) {
return console.log('Falha ao criar configuração da `obr-last-sync`!');
}
})
}
/*
* Handles the route /obr-sync
* Saves configs in the database, and update deamon to reflect new config
*/
exports.updateConfig = function (req, res) {
// Save to database
app.models.Config.findOne('obr-sync', function (err, model) {
var config = _.defaults(req.query, model, exports.default)
config = _.pick(config, _.keys(exports.default))
// Fix for boolean flags like 'sync'
var falses = ['false', '0', 'off', false]
config.sync = falses.includes(config.sync) ? false : true
// Convert time to number
config.interval = parseInt(config.interval)
// Update deamon
exports.updateDeamon(config)
// Persist do db if 'now' is not set
if ('now' in req.query) {
exports.sync(config, function (err, data) {
if (err) {
return res.status(500).send(err)
}
return res.send('now')
})
} else {
app.models.Config.update('obr-sync', config, function (err, model){
return res.send(_.pick(model[0], _.keys(exports.default)))
})
}
})
}
/*
* Controls the menu badge with the Sync status
*/
exports.statusMenu = {
path: '/obr-config',
name: '',
badge: 'Sync: Inicializando...'
}
exports.updateBadge = function () {
var badge = 'Sincronização OBR: '
if (!exports.config.sync) {
badge += '<span style="color: #DDD"> 😴️ Desligado</span>'
} else if (exports.error) {
badge += '<span style="color: #ff687d"> 👎 Falhou </span>'
} else {
badge += '<span style="color: #33EE30"> 👌 Ligado</span>'
}
exports.statusMenu.badge = badge
}
/*
* Returns the last successfull sync
*/
exports.getLastSync = function getLastSync(req, res) {
app.models.Config.findOne('obr-last-sync', function (err, config) {
if (err) {
return res.status(500).send(err)
}
var lastSync = config && config.value
res.send({timestamp: lastSync || null, })
})
}
/*
* Updates the timestamp of the last success on sync (config: obr-last-sync)
*/
exports.didSync = function didSync() {
app.models.Config.update('obr-last-sync', {
value: Date.now()
}, function (err, model) {
console.log(TAG, 'didSync', err ? err : 'OK')
})
}
exports.error = null
exports.config = null
exports.interval = null
/*
* Updates the deamon responsible for triggering sync
*/
exports.updateDeamon = function (config) {
exports.config = config
clearInterval(exports.interval)
exports.interval = null
exports.updateBadge()
if (!config.sync) {
return
}
// Limit interval period
var period = exports.config.interval * 1000 || 0
period = Math.max(period, 1 * 60 * 1000) // 1 minute minimum
period = Math.min(period, 30 * 60 * 1000) // 30 minutes maximum
// Update interval
exports.interval = setInterval(exports.sync, period)
// Call on change
exports.sync()
console.log(TAG, 'Deamon updated. ')
}
/*
* Fetches table data and sends as a post to the defined URL
*/
exports.sync = function (config, next) {
config = config || exports.config
// Check tournamenter is compatible...
if (! ('ConvertTableToMatrix' in app.helpers) )
return next('A versão do TournamenterApp precisa estar acima de 1.7.0')
// Sync
console.log(TAG, 'Sync data...')
// Get tables
app.controllers.Table._findAssociated(null, function (tables) {
var matrixTables = tables.map(ConvertTableToMatrix)
var data = {
tables: matrixTables,
blobs: tables,
}
request({
url: config.url,
json: data,
method: 'POST'
}, function (err, httpResponse, body) {
if (err) {
console.log(TAG, 'Failed to sync: ' + err)
exports.error = true; exports.updateBadge();
return next && next((err || '').toString() || body)
}
let statusCode = httpResponse.statusCode
if (statusCode >= 400) {
console.log(TAG, 'Failed to sync', statusCode)
exports.error = true; exports.updateBadge();
return next && next('Falha na sincronização. Retorno: ' + statusCode + ': ' + body)
}
// Update badge
exports.error = false; exports.updateBadge();
// Update timestamp
exports.didSync()
// Push to url as post
next && next(null, body)
})
}, {
isView: true,
})
}
/*
* Convert table to matrix
*/
function ConvertTableToMatrix(table) {
if (!table)
return null;
var scores = table.scores
var headers = table.headers
var scoreColumns = parseInt(table.columns)
var headerColumns = 3 // RANK, NAME and FINAL
// Generate rows
var rows = []
// Push Header
var header = _.flatten([headers.rank, headers.team, headers.scores, headers.final])
console.log(header)
rows.push(header)
// Push data
scores.forEach(function (line) {
var row = [line.rank, line.team ? line.team : null]
// Add scores (Makes sure the size is fixed)
_.times(scoreColumns, function (n) {
row.push(n in line.scores ? line.scores[n].value : '-')
})
// Add final score
row.push(line.final)
// Add to rows
rows.push(row)
})
return rows
}