From 1eab0c2491df90832ef348797881f866cca220eb Mon Sep 17 00:00:00 2001 From: jkrvivian <jkrvivian@gmail.com> Date: Wed, 7 Aug 2019 15:39:34 +0800 Subject: [PATCH] feat: Add node status on dashboard --- plugins/dashboard/tps.go | 67 +++++++++++++++++++++++++++++++ plugins/dashboard/tps_template.go | 40 ++++++++++++++---- 2 files changed, 99 insertions(+), 8 deletions(-) diff --git a/plugins/dashboard/tps.go b/plugins/dashboard/tps.go index ea287a74..a35d4721 100644 --- a/plugins/dashboard/tps.go +++ b/plugins/dashboard/tps.go @@ -2,16 +2,26 @@ package dashboard import ( "encoding/binary" + "fmt" "html/template" + "math" "net/http" "sync" + "strconv" + "time" "github.com/gorilla/websocket" + "github.com/iotaledger/goshimmer/packages/accountability" "github.com/iotaledger/goshimmer/packages/events" "github.com/iotaledger/goshimmer/plugins/metrics" + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/acceptedneighbors" + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/chosenneighbors" + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/knownpeers" + "github.com/iotaledger/goshimmer/plugins/autopeering/instances/neighborhood" ) var ( + start = time.Now() homeTempl, templ_err = template.New("dashboard").Parse(tpsTemplate) upgrader = websocket.Upgrader{ ReadBufferSize: 1024, @@ -19,6 +29,57 @@ var ( } ) +type Status struct { + Id string `json:"Id"` + Neighbor string `json:"Neighbor"` + KnownPeer string `json:"KnownPeer"` + Uptime string `json:"Uptime"` +} + +func GetStatus() *Status { + // Get Uptime + duration := time.Since(start) + padded := false + uptime := fmt.Sprintf("Uptime: ") + if int(duration.Seconds())/(60*60*24) > 0 { + days := int(duration.Hours()) / 24 + + numberLength := int(math.Log10(float64(days))) + 1 + padLength := 31 - numberLength + + uptime += fmt.Sprintf("%*v", padLength, "") + uptime += fmt.Sprintf("%02dd ", days) + } + + if int(duration.Seconds())/(60*60) > 0 { + if !padded { + uptime += fmt.Sprintf("%29v", "") + padded = true + } + uptime += fmt.Sprintf("%02dh ", int(duration.Hours())%24) + } + + if int(duration.Seconds())/60 > 0 { + if !padded { + uptime += fmt.Sprintf("%33v", "") + padded = true + } + uptime += fmt.Sprintf("%02dm ", int(duration.Minutes())%60) + } + + if !padded { + uptime += fmt.Sprintf("%37v", "") + } + uptime += fmt.Sprintf("%02ds ", int(duration.Seconds())%60) + + return &Status { + Id: accountability.OwnId().StringIdentifier, + Neighbor: "Neighbors: " + strconv.Itoa(chosenneighbors.INSTANCE.Peers.Len())+" chosen / "+strconv.Itoa(acceptedneighbors.INSTANCE.Peers.Len())+" accepted / "+strconv.Itoa(chosenneighbors.INSTANCE.Peers.Len()+acceptedneighbors.INSTANCE.Peers.Len())+" total", + KnownPeer: "Known Peers: "+ strconv.Itoa(knownpeers.INSTANCE.Peers.Len())+" total / "+strconv.Itoa(neighborhood.INSTANCE.Peers.Len())+" neighborhood", + Uptime: uptime, + } +} + // ServeWs websocket func ServeWs(w http.ResponseWriter, r *http.Request) { ws, err := upgrader.Upgrade(w, r, nil) @@ -38,6 +99,12 @@ func ServeWs(w http.ResponseWriter, r *http.Request) { if err := ws.WriteMessage(websocket.BinaryMessage, p); err != nil { return } + + // write node status message + status := GetStatus() + if err := ws.WriteJSON(status); err != nil { + return + } }() }) diff --git a/plugins/dashboard/tps_template.go b/plugins/dashboard/tps_template.go index f12df4b5..00cc9aa8 100644 --- a/plugins/dashboard/tps_template.go +++ b/plugins/dashboard/tps_template.go @@ -9,10 +9,16 @@ var tpsTemplate = ` <script src="https://code.highcharts.com/stock/highstock.js"></script> <script src="https://code.highcharts.com/stock/modules/exporting.js"></script> <script src="https://code.highcharts.com/stock/modules/export-data.js"></script> - <div id="container" style="height: 400px; min-width: 310px"></div> </head> <body> + <div id="node-status" style="min-width:310px"> + <h3 id="node-id"></h3> + <h3 id="node-neighbor"></h3> + <h3 id="node-knownpeer"></h3> + <h3 id="node-uptime"></h3> + </div> + <div id="container" style="height: 400px; min-width: 310px"></div> <script> Highcharts.createElement('link', { href: 'https://fonts.googleapis.com/css?family=Unica+One', @@ -306,14 +312,32 @@ var tpsTemplate = ` // console.log('Connection closed'); } conn.onmessage = evt => { - console.log('metric updated'); const data = evt.data; - const dv = new DataView(data); - // var value = dv.getUint32(4, true) << 32 | dv.getUint32(0, true); - const value = dv.getUint32(0, true); - chart.series[0].addPoint([time += 1000, value], true); //((counter += 1) % update_rate == 4)); - console.log(value); - console.log(dv); + if(isJSON(data)) { + console.log('status updated'); + var res = JSON.parse(data); + $("#node-id").html("Node ID: " + res.Id); + $("#node-neighbor").html(res.Neighbor); + $("#node-knownpeer").html(res.KnownPeer); + $("#node-uptime").html(res.Uptime); + } else { + console.log('metric updated'); + const dv = new DataView(data); + // var value = dv.getUint32(4, true) << 32 | dv.getUint32(0, true); + const value = dv.getUint32(0, true); + chart.series[0].addPoint([time += 1000, value], true); //((counter += 1) % update_rate == 4)); + console.log(value); + console.log(dv); + } + } + + function isJSON(str) { + try { + JSON.parse(str); + } catch(e) { + return false; + } + return true; } </script> </body> -- GitLab