lotus/cmd/lotus-townhall/townhall/src/App.js

88 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-10-10 11:07:00 +00:00
import React from 'react';
import './App.css';
function colForH(besth, height) {
const diff = besth - height
if(diff === 0) return '#6f6'
if(diff === 1) return '#df4'
if(diff < 4) return '#ff0'
if(diff < 10) return '#f60'
return '#f00'
}
2019-11-10 16:26:39 +00:00
function colLag(lag) {
if(lag < 100) return '#6f6'
if(lag < 400) return '#df4'
if(lag < 1000) return '#ff0'
if(lag < 4000) return '#f60'
return '#f00'
}
function lagCol(lag, good) {
return <span>
<span style={{color: colLag(lag)}}>{lag}</span>
<span style={{color: good ? '#f0f0f0' : '#f60'}}>ms</span>
</span>
}
2019-10-10 11:07:00 +00:00
class App extends React.Component {
constructor(props) {
super(props);
2019-10-11 06:11:57 +00:00
let ws = new WebSocket("ws://" + window.location.host + "/sub")
//let ws = new WebSocket("ws://127.0.0.1:2975/sub")
2019-10-10 11:07:00 +00:00
ws.onmessage = (ev) => {
console.log(ev)
let update = JSON.parse(ev.data)
2019-11-10 22:07:54 +00:00
update.Update.Weight = Number(update.Update.Weight)
2019-11-10 16:58:57 +00:00
let wdiff = update.Update.Weight - (this.state[update.From] || {Weight: update.Update.Weight}).Weight
wdiff = <span style={{color: wdiff < 0 ? '#f00' : '#f0f0f0'}}>{wdiff}</span>
2019-11-14 16:56:13 +00:00
let utDiff = update.Time - (this.state[update.From] || {utime: update.Time}).utime
utDiff = <span style={{color: utDiff < 0 ? '#f00' : '#f0f0f0'}}>{utDiff}ms</span>
2019-10-10 11:07:00 +00:00
this.setState( prev => ({
2019-11-14 16:56:13 +00:00
...prev, [update.From]: {...update.Update, utime: update.Time, wdiff: wdiff, utDiff: utDiff},
2019-10-10 11:07:00 +00:00
}))
}
2019-11-10 16:26:39 +00:00
ws.onclose = () => {
this.setState({disconnected: true})
}
2019-10-10 11:07:00 +00:00
this.state = {}
}
render() {
2019-11-10 16:26:39 +00:00
if(this.state.disconnected) {
return <span>Error: disconnected</span>
}
let besth = Object.keys(this.state).map(k => this.state[k]).reduce((p, n) => p > n.Height ? p : n.Height, -1)
let bestw = Object.keys(this.state).map(k => this.state[k]).reduce((p, n) => p > n.Weight ? p : n.Weight, -1)
2019-10-10 11:07:00 +00:00
2019-11-10 16:26:39 +00:00
return <table>
2019-11-10 16:58:57 +00:00
<tr><td>PeerID</td><td>Nickname</td><td>Lag</td><td>Weight(best, prev)</td><td>Height</td><td>Blocks</td></tr>
2019-11-10 16:26:39 +00:00
{Object.keys(this.state).map(k => [k, this.state[k]]).map(([k, v]) => {
let mnrs = v.Blocks.map(b => <td>&nbsp;m:{b.Miner}({lagCol(v.Time ? v.Time - (b.Timestamp*1000) : v.utime - (b.Timestamp*1000), v.Time)})</td>)
let l = [
<td>{k}</td>,
<td>{v.NodeName}</td>,
2019-11-14 16:56:13 +00:00
<td>{v.Time ? lagCol(v.utime - v.Time, true) : ""}(Δ{v.utDiff})</td>,
2019-11-10 16:58:57 +00:00
<td style={{color: bestw !== v.Weight ? '#f00' : '#afa'}}>{v.Weight}({bestw - v.Weight}, {v.wdiff})</td>,
2019-11-10 16:26:39 +00:00
<td style={{color: colForH(besth, v.Height)}}>{v.Height}({besth - v.Height})</td>,
...mnrs,
]
2019-10-12 14:05:41 +00:00
2019-10-11 02:45:45 +00:00
l = <tr>{l}</tr>
return l
2019-10-10 11:07:00 +00:00
})
2019-11-10 16:26:39 +00:00
}
</table>
2019-10-10 11:07:00 +00:00
}
}
export default App;