Skip to content

Commit cdba30c

Browse files
committed
updated for use uptime from minataur
1 parent 2f8212e commit cdba30c

File tree

10 files changed

+122
-87
lines changed

10 files changed

+122
-87
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ package-lock.json
2121
*.http
2222
*.key
2323
*.crt
24+
.parcel-cache

babel.config.json

-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
{
22
"presets": [
3-
[
4-
"@babel/preset-env",
5-
{
6-
"targets": {
7-
"esmodules": true
8-
}
9-
}
10-
]
113
],
124
"plugins": [
13-
"@babel/plugin-proposal-class-properties"
145
]
156
}

client/index.html

+1-3
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,6 @@
437437
</div>
438438
</section>
439439

440-
<script src="vendor/metro4/js/metro.js"></script>
441-
<script src="vendor/chart/chart.js"></script>
442-
<script src="js/app.js"></script>
440+
<script type="module" src="js/app.js"></script>
443441
</body>
444442
</html>

client/js/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'regenerator-runtime/runtime' // this required for Parcel
1+
//import 'regenerator-runtime/runtime' // this required for Parcel
22

33
import "../vendor/metro4/css/metro-all.css"
44
import "../css/index.less"

client/package.json

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
{
22
"name": "@olton/mina-monitor-client",
3-
"version": "2.1.2",
3+
"version": "2.2.0",
44
"license": "MIT",
55
"scripts": {
6-
"client": "shx rm -rf output/* && parcel serve --target browser --out-dir output index.html -p 2222",
7-
"start": "shx rm -rf output/* && parcel serve --target browser --out-dir output index.html -p 2222 --open"
6+
"client": "shx rm -rf output/* && npm run copy && parcel serve --dist-dir output index.html -p 2222",
7+
"start": "shx rm -rf output/* && npm run copy && parcel serve --dist-dir output index.html -p 2222 --open",
8+
"copy": "copyfiles -f config.json output"
89
},
910
"devDependencies": {
1011
"less": "^4.1.1",
11-
"parcel-bundler": "^1.12.5",
12-
"parcel-plugin-static-files-copy": "^2.6.0",
12+
"parcel": "latest",
13+
"@parcel/transformer-less": "^2.5.0",
14+
"copyfiles": "^2.4.1",
1315
"shx": "^0.3.3"
1416
},
15-
"staticFiles": {
16-
"staticPath": [
17-
"config.json"
18-
]
19-
},
2017
"browserslist": [
2118
"last 1 Chrome versions",
2219
"last 1 Firefox versions",

package.json

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
{
22
"name": "@olton/mina-monitor",
3-
"version": "2.1.2",
3+
"version": "2.2.0",
44
"license": "MIT",
5-
"main": "server/index.js",
65
"scripts": {
7-
"serve": "shx rm -rf output/* && parcel serve --open --target browser --out-dir output client/index.html -p 2222",
8-
"prebuild": "shx rm -rf dist/*",
9-
"build": "parcel build --out-dir dist --public-url . --no-source-maps client/index.html",
10-
"start": "npm run serve",
6+
"start": "shx rm -rf client/output/* && npm run copy && parcel serve --open --dist-dir client/output client/index.html -p 2222",
7+
"serve": "parcel serve --open --dist-dir client/output client/index.html -p 2222",
8+
"prebuild": "shx rm -rf client/dist/* && npm run copy",
9+
"clear": "shx rm -rf client/dist/* client/output/* ",
10+
"build": "parcel build --dist-dir client/dist --public-url . --no-source-maps client/index.html",
1111
"client": "npm run serve",
1212
"server": "nodemon server/index.js",
13-
"test": "jest"
13+
"copy": "copyfiles -f client/config.json client/output"
1414
},
1515
"devDependencies": {
16+
"@babel/core": "^7.17.10",
17+
"@parcel/transformer-less": "^2.5.0",
18+
"copyfiles": "^2.4.1",
1619
"less": "^4.1.1",
17-
"parcel-bundler": "^1.12.5",
18-
"parcel-plugin-static-files-copy": "^2.6.0",
20+
"parcel": "latest",
1921
"shx": "^0.3.3"
2022
},
2123
"dependencies": {
@@ -24,11 +26,6 @@
2426
"systeminformation": "^5.7.7",
2527
"ws": "^5.2.3"
2628
},
27-
"staticFiles": {
28-
"staticPath": [
29-
"client/config.json"
30-
]
31-
},
3229
"browserslist": [
3330
"last 1 Chrome versions",
3431
"last 1 Firefox versions",

server/helpers/logs.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
const {timestamp} = require("./timestamp")
22

33
const print = (m) => console.log(m)
4-
const logging = (m, error = false) => console[error ? "error" : "log"](timestamp()+" "+m)
4+
const logging = (m, error = false, ...rest) => console[error ? "error" : "log"](timestamp()+" "+m, ...rest)
5+
const log = (m, ...rest) => console["log"](timestamp()+" "+m, ...rest)
6+
const error = (m, ...rest) => console["error"](timestamp()+" "+m, ...rest)
57

68
module.exports = {
79
print,
8-
logging
10+
logging,
11+
error,
12+
log
913
}

server/index.js

+72-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const {processWinningBlocks, processBlockchainSummary, processBlockchainLatestBl
1313
const {processAlerter} = require("./modules/alerter")
1414
const {processBalance} = require("./modules/balance")
1515
const {processHello} = require("./modules/hello")
16-
const {processUptime} = require("./modules/uptime")
16+
const {processUptime, processUptime2} = require("./modules/uptime")
1717
const {processDelegations} = require("./modules/delegations")
1818
const {processPriceInfo, processPriceSend} = require("./modules/price")
1919
const {processSnarkWorkerController} = require("./modules/snark-worker")
@@ -23,12 +23,13 @@ const {SYNC_STATE_UNKNOWN} = require("./helpers/consts")
2323
const {processConsensus} = require("./modules/consensus")
2424
const {processBlockchain} = require("./modules/blockchain")
2525
const {processBlockSpeed} = require("./modules/speed")
26-
const {logging} = require("./helpers/logs")
26+
const {logging, log, error} = require("./helpers/logs")
2727
const {welcomeHtml} = require("./helpers/welcome")
2828
const {processCompare} = require("./modules/comparer")
2929
const packageJson = require("./package.json")
3030
const {processConfigWatcher} = require("./helpers/watcher");
3131
const {processGetMinaVersion} = require("./helpers/shell");
32+
const {parseTime} = require("./helpers/parsers");
3233

3334
const version = packageJson.version
3435
const configPathLinux = "/etc/mina-monitor/config.json"
@@ -144,6 +145,75 @@ if (useHttps) {
144145

145146
const wss = new WebSocket.Server({ server })
146147

148+
const {host: minataurHost = 'minataur.net:443', secure: minataurSecure = true} = config.minataur
149+
150+
const isOpen = (ws) => ws && ws.readyState === ws.OPEN
151+
152+
const request = (ws, channel, data) => {
153+
if (isOpen(ws)) {
154+
ws.send(JSON.stringify({
155+
channel,
156+
data
157+
}))
158+
}
159+
}
160+
161+
const connectToMinataur = () => {
162+
const ws = new WebSocket(`${minataurSecure ? 'wss' : 'ws'}://${minataurHost}`)
163+
164+
ws.onerror = e => {
165+
error('Socket from Minataur encountered error: ', e.message, 'Closing socket');
166+
ws.close();
167+
}
168+
169+
ws.onclose = e => {
170+
log('Socket from Minataur is closed. Reconnect will be attempted in 1 second.', e.reason);
171+
setTimeout(connectToMinataur, 1000)
172+
}
173+
174+
ws.onopen = e => {
175+
log('Connected to Minataur!')
176+
}
177+
178+
ws.onmessage = msg => {
179+
const message = JSON.parse(msg.data)
180+
if (!message.channel) return
181+
const {channel, data} = message
182+
183+
let uptimeUpdateInterval = parseTime(config.uptimeUpdateInterval || '5m')
184+
185+
if (uptimeUpdateInterval < parseTime("5m")) {
186+
uptimeUpdateInterval = parseTime("5m")
187+
}
188+
189+
const requestUptime = (addr = config.publicKeyDelegators || config.publicKey) => {
190+
request(ws, "address_uptime_full", addr)
191+
}
192+
193+
switch (channel){
194+
case 'welcome': {
195+
log("Welcome to Minataur!")
196+
requestUptime()
197+
break
198+
}
199+
case 'address_uptime_full': {
200+
try {
201+
processUptime(data)
202+
}
203+
catch (e) {
204+
error(e.message)
205+
}
206+
finally {
207+
setTimeout(requestUptime, uptimeUpdateInterval)
208+
}
209+
break;
210+
}
211+
}
212+
}
213+
}
214+
215+
connectToMinataur()
216+
147217
server.listen(+SERVER_PORT, SERVER_HOST, () => {
148218
logging(`Mina Monitor Server running on ${useHttps ? 'https' : 'http'}://${SERVER_HOST}:${SERVER_PORT}`)
149219
})
@@ -198,7 +268,6 @@ setImmediate( processCpuTemp )
198268
setImmediate( processNetStat )
199269
setImmediate( processNetConn )
200270
setImmediate( processPriceInfo )
201-
setImmediate( processUptime )
202271
setImmediate( processDelegations )
203272
setImmediate( processJournal )
204273
setImmediate( processHello )

server/modules/uptime.js

+22-44
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,36 @@
1-
const fetch = require("node-fetch")
21
const {sendMessage} = require("../helpers/messangers");
3-
const {parseTime} = require("../helpers/parsers");
4-
5-
const getUptime = async (key) => {
6-
if (!key) return null
7-
8-
const link = `https://minastake.com/uptime/uptime2.php?publicKey=${key}`
9-
let data
10-
11-
try {
12-
data = await fetch(link)
13-
return data.ok ? data.json() : null
14-
} catch (e) {
15-
return null
16-
}
17-
}
18-
19-
const processUptime = async () => {
20-
const minInterval = parseTime('5m')
21-
let {uptimeUpdateInterval = minInterval} = config
22-
23-
if (uptimeUpdateInterval < minInterval) {
24-
uptimeUpdateInterval = minInterval
25-
}
2+
const {error} = require("../helpers/logs");
263

4+
const processUptime = (data) => {
275
try {
28-
const {publicKeyDelegators} = globalThis.config
29-
const uptime = await getUptime(publicKeyDelegators)
6+
const {uptime, line} = data
307

318
if (uptime && uptime.score) {
32-
let message = '', scoreChanged = false, positionChanged = false, rateChanged = false
33-
let {position, score, rate, group, positions} = uptime
9+
let message = '', scoreChanged = false, positionChanged = false, rateChanged = false, rangeChanged = false
10+
let {position, score, rate, range} = uptime
3411

35-
positions = positions.map( v => +v ).sort( (a, b) => a - b)
3612

3713
if (!cache.uptime) {
38-
message = `Your current uptime score is \`${uptime.score}\` with rate \`${uptime.rate}%\`, and at the \`${uptime.position}\` place in range ${positions[0]}...${positions[positions.length - 1]}.`
14+
message = `Your current uptime score is \`${uptime.score}\` with rate \`${uptime.rate}%\`, and at the \`${uptime.position}\` place in range ${range.min}...${range.max}.`
3915
} else {
40-
let {position: cachedPosition, score: cachedScore, rate: cachedRate, group: cachedGroup, positions: cachedPositions} = cache.uptime
16+
let {position: cachedPosition, score: cachedScore, rate: cachedRate, range: cachedRange} = cache.uptime.uptime
4117

4218
scoreChanged = +(cachedScore) !== +(score)
4319
rateChanged = +(cachedRate) !== +(rate)
44-
positionChanged = positions[0] !== cachedPositions[0] || positions[positions.length - 1] !== cachedPositions[cachedPositions.length - 1]
20+
positionChanged = +(cachedPosition) !== +(position)
21+
rangeChanged = (+(cachedRange.min) !== +(range.min)) || (+(cachedRange.max) !== +(range.max))
22+
23+
if (scoreChanged || rateChanged || positionChanged || rangeChanged) {
24+
let newValueMessage = `New value \`${score}\` with rate \`${rate}%\`, and at the \`${position}\` place in range ${range.min}..${range.max}.`
4525

46-
if (scoreChanged || rateChanged || positionChanged) {
47-
let newValueMessage = `New value \`${score}\` with rate \`${rate}%\`, and at the \`${position}\` place in range ${positions[0]}...${positions[positions.length - 1]}.`
4826
if (scoreChanged) {
49-
message = `Your uptime score changed ${score > cachedScore ? 'UP' : 'DOWN'}!`
27+
message = `Your uptime score changed ${+score > +cachedScore ? 'UP' : 'DOWN'}!`
5028
} else if (rateChanged) {
51-
message = `Your uptime rate changed ${rate > cachedRate ? 'UP' : 'DOWN'}!`
29+
message = `Your uptime rate changed ${+rate > +cachedRate ? 'UP' : 'DOWN'}!`
30+
} else if (positionChanged) {
31+
message = `Your uptime position changed ${+position > +cachedPosition ? 'UP' : 'DOWN'}!`
32+
} else if (rangeChanged) {
33+
message = `Your uptime range changed from ${cachedRange.min}..${cachedRange.max} to ${range.min}..${range.max}`
5234
} else {
5335
message = `Your uptime position changed!`
5436
}
@@ -59,18 +41,14 @@ const processUptime = async () => {
5941

6042
if (message) sendMessage("UPTIME", message)
6143

62-
globalThis.cache.uptime = {
63-
...uptime,
64-
positions
65-
}
44+
globalThis.cache.uptime = data
6645
}
46+
6747
} catch (e) {
68-
console.error(e)
48+
error(e.message)
6949
}
70-
71-
setTimeout(processUptime, uptimeUpdateInterval)
7250
}
7351

7452
module.exports = {
75-
processUptime
53+
processUptime,
7654
}

server/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mina_monitor_server",
3-
"version": "2.1.2",
3+
"version": "2.2.0",
44
"license": "MIT",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)