-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathindex.js
105 lines (95 loc) · 3.15 KB
/
index.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
require('dotenv').config()
const express = require('express')
const fs = require('fs')
const { isV1Bridge } = require('./utils/serverUtils')
const app = express()
const MONITOR_TX_NUMBER_THRESHOLD = Number(process.env.MONITOR_TX_NUMBER_THRESHOLD) || 100
console.log('MONITOR_TX_NUMBER_THRESHOLD = ' + MONITOR_TX_NUMBER_THRESHOLD)
async function readFile(path) {
try {
const content = await fs.readFileSync(path)
const json = JSON.parse(content)
const timeDiff = Math.floor(Date.now() / 1000) - json.lastChecked
return Object.assign({}, json, { timeDiff })
} catch (e) {
console.error(e)
return {
error: 'please check your worker'
}
}
}
async function initV1routes(app) {
const exposeV1Routes = await isV1Bridge()
if (exposeV1Routes) {
app.get('/stuckTransfers', async (req, res, next) => {
try {
const results = await readFile('./responses/stuckTransfers.json')
results.ok = results.total.length === 0
res.json(results)
} catch (e) {
next(e)
}
})
}
}
app.get('/', async (req, res, next) => {
try {
const results = await readFile('./responses/getBalances.json')
res.json(results)
} catch (e) {
// this will eventually be handled by your error handling middleware
next(e)
}
})
app.get('/validators', async (req, res, next) => {
try {
const results = await readFile('./responses/validators.json')
results.homeOk = true
results.foreignOk = true
for (const hv in results.home.validators) {
if (results.home.validators[hv].leftTx < MONITOR_TX_NUMBER_THRESHOLD) {
results.homeOk = false
break
}
}
for (const hv in results.foreign.validators) {
if (results.foreign.validators[hv].leftTx < MONITOR_TX_NUMBER_THRESHOLD) {
results.foreignOk = false
break
}
}
results.ok = results.homeOk && results.foreignOk
res.json(results)
} catch (e) {
// this will eventually be handled by your error handling middleware
next(e)
}
})
// responses/eventsStats.json
app.get('/eventsStats', async (req, res, next) => {
try {
const results = await readFile('./responses/eventsStats.json')
results.ok =
(results.onlyInHomeDeposits || results.home.deliveredMsgNotProcessedInForeign).length === 0 &&
(results.onlyInForeignDeposits || results.home.processedMsgNotDeliveredInForeign).length === 0 &&
(results.onlyInHomeWithdrawals || results.foreign.deliveredMsgNotProcessedInHome).length === 0 &&
(results.onlyInForeignWithdrawals || results.foreign.processedMsgNotDeliveredInHome).length === 0
res.json(results)
} catch (e) {
// this will eventually be handled by your error handling middleware
next(e)
}
})
app.get('/alerts', async (req, res, next) => {
try {
const results = await readFile('./responses/alerts.json')
results.ok = !results.executeAffirmations.mostRecentTxHash && !results.executeSignatures.mostRecentTxHash
res.json(results)
} catch (e) {
next(e)
}
})
initV1routes(app)
const port = process.env.MONITOR_PORT || 3003
app.set('port', port)
app.listen(port, () => console.log(`Monitoring app listening on port ${port}!`))