forked from isjeffcom/coronvirusFigureUK
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
424 lines (340 loc) · 9.02 KB
/
Copy pathdatabase.js
File metadata and controls
424 lines (340 loc) · 9.02 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/**
* Develop By Jeff Wu
* 2020.03
* isjeff.com
**/
/**
* DATABASES OPRATION
* why we need a database is for saving history, and for admin page.
* its not have to be this way, you can use other design like nosql
* as this part is quite easy to read, I will not explain how it works.
* as you can follow https://www.npmjs.com/package/ali-mysql-client for known how ali-mysql-client works.
* its writen in Chinese, however, if you know a little bit MySql, just read through the code would be enough.
**/
const DbClient = require("ali-mysql-client")
const conf = require('./conf')
const utils = require('./utils')
const { stripSlashes } = require('slashes')
const md5 = require('md5')
const db = new DbClient(conf.getDB())
// Get current number
async function current(){
const result = await db
.select("*")
.from("current")
.queryList()
if(result){
for(let i=0;i<result.length;i++){
result[i].area = stripSlashes(result[i].area)
}
return { status: true, data: result}
} else {
return { status: false, data: null, err: result }
}
}
// Get current number
async function shadow(){
const result = await db
.select("*")
.from("current_shadow")
.queryList()
if(result){
for(let i=0;i<result.length;i++){
result[i].area = stripSlashes(result[i].area)
}
return { status: true, data: result}
} else {
return { status: false, data: null, err: result }
}
}
async function locations(){
const result = await db
.select("*")
.from("geo")
.queryList()
return result ? { status: true, data: result} : { status: false, data: null, err: result }
}
async function addLocation(ready){
const save = await db
.insert("geo", ready)
.execute()
return save ? { status: true, data: null} : { status: false, data: null, err: save }
}
async function updateLocation(id, data){
const result = await db
.update("geo", data)
.where("id", id)
.execute()
return result ? { status: true, data: null} : { status: false, data: null, err: result }
}
// Get all histroy
async function history(){
const result = await db
.select("*")
.from("history")
.orderby("date asc")
.queryList()
if(result){
return { status: true, data: result}
} else {
return { status: false, data: null, err: result }
}
}
// Get all histroy (figures only)
async function historyFigures(){
const result = await db
.select([
"id",
"date",
"confirmed",
"death",
"cured",
"serious",
"negative",
"suspected",
"tested",
"test_done",
"england",
"scotland",
"wales",
"nireland",
"hospital",
"hospitalArea",
"icuArea"
])
.from("history")
.orderby("date asc")
.queryList()
if(result){
return { status: true, data: result}
} else {
return { status: false, data: null, err: result }
}
}
async function lastHistory(){
const result = await db
.select("*")
.from("history")
.orderby("date desc")
.queryRow()
if(result){
return { status: true, data: result}
} else {
return { status: false, data: null, err: result }
}
}
// Update, default to shadow waiting for approvement
async function update(sourceId, data){
const result = await db
.update("current_shadow", data)
.where("id", sourceId)
.execute()
return result ? { status: true, err: null } : { status: false, err: result }
}
// Get approve token
async function getApproveToken(){
const result = await db
.select("token")
.from("user")
.where('id', 1)
.queryRow()
return result ? { status: true, data: result } : { status: false, data:null, err: result }
}
// Verify pin for admin page
async function verifyPin(pin, token){
const result = await db
.select("*")
.from("user")
.where('token', token)
.queryRow()
if(result.psw == md5(pin)){
return { status: true, data: result.id }
} else {
return {status: false, data: null}
}
}
// Officially publish data
async function updateApprove(){
const result = await db
.select("*")
.from("current_shadow")
.queryList()
if(result){
result.forEach(async single => {
let sid = single.id
delete single.id;
const copy = await db
.update("current", single)
.where("id", sid)
.execute()
})
}
return { status: true, data: null, err: null}
}
async function getWales(){
const shadow = await db
.select("wales")
.from("current_shadow")
.queryRow()
return shadow
}
async function getnIreland(){
const shadow = await db
.select("nireland")
.from("current_shadow")
.queryRow()
return shadow
}
async function autoApprove(){
const shadow = await db
.select("*")
.from("current_shadow")
.queryList()
const current = await db
.select("*")
.from("current")
.queryList()
let confirmed1 = compare(shadow[0].confirmed, current[0].confirmed)
let death1 = compare(shadow[0].death, current[0].death)
let confirmed2 = compare(shadow[1].confirmed, current[1].confirmed)
let death2 = compare(shadow[1].death, current[1].death)
if(!shadow[0].area){
console.log("no area")
saveErr({source: "approve", reason: "no area data", detail: "none"})
return
}
if(confirmed1 && confirmed2 && death1 && death2 && noNull(shadow[0].area)){
updateApprove()
console.log("approved")
} else {
console.log("need approve")
}
return
}
function compare(num1, num2){
if(num1 == null || num1 == "" || isNaN(num1) || num1 == "null" || typeof num1 == null || typeof num1 == undefined || num1 == undefined){
return false
}
const notMuch = 20
if(num1 >= num2 && (num1 - num2) < notMuch){
return true
} else {
return false
}
}
function noNull (a) {
let d = JSON.parse(stripSlashes(a))
for(let i=0;i<d.length;i++){
if(d.location == "" || d.location == "null" || d.number == "" || d.number == "null" || !isNaN(d.number)){
return false
}
}
return true
}
async function saveHistory(){
const result = await db
.select("*")
.from("current")
.queryList()
const ready = {
date: utils.getTS(),
confirmed: result[0].confirmed,
death: result[0].death,
cured: result[1].cured,
serious: result[0].serious,
negative: result[0].negative,
suspected: result[0].suspected,
tested: result[0].tested,
test_done: result[0].test_done,
england: result[0].england,
scotland: result[0].scotland,
wales: result[0].wales,
nireland: result[0].nireland,
hospital: result[0].hospital,
hospitalArea: result[0].hospitalArea,
icuArea: result[0].icuArea,
area: result[0].area
}
const save = await db
.insert("history", ready)
.execute()
if(save){
return { status: true, data: null}
} else {
return { status: false, data: null, err: save}
}
}
async function addHistory(ready){
delete ready.id
delete ready.england
delete ready.scotland
delete ready.wales
delete ready.nireland
const save = await db
.insert("history", ready)
.execute()
}
async function getHospital(){
const res = await db
.select("hospital")
.from("current")
.queryRow()
return res
}
async function getHospitalArea(){
const res = await db
.select("hospitalArea")
.from("current")
.queryRow()
return res
}
async function updateHistory(ready, id){
const save = await db
.update("history", ready)
.where('id', id)
.execute()
}
async function updateHistoryByDate(ready, date){
const all = await db
.select('*')
.from('history')
.queryList()
for(let i=0;i<all.length;i++){
if(date == utils.tsToDate(all[i].date)){
const save = await db
.update("history", ready)
.where('date', all[i].date)
.execute()
}
}
}
async function saveErr(ready){
const save = await db
.insert("err", ready)
.execute()
return
}
module.exports = {
current: current,
shadow: shadow,
history: history,
historyFigures: historyFigures,
lastHistory: lastHistory,
update: update,
locations: locations,
addLocation: addLocation,
updateLocation: updateLocation,
updateApprove: updateApprove,
autoApprove: autoApprove,
saveHistory: saveHistory,
getApproveToken: getApproveToken,
verifyPin: verifyPin,
saveErr: saveErr,
addHistory: addHistory,
updateHistory:updateHistory,
updateHistoryByDate: updateHistoryByDate,
getHospital: getHospital,
getHospitalArea: getHospitalArea,
getWales: getWales,
getnIreland: getnIreland
}