5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-10 07:30:27 +00:00

Offload formatting to yggdrasilctl and keep precision in JSON

This commit is contained in:
Neil Alexander 2018-05-21 15:21:23 +01:00
parent 59688dcab3
commit 9567446f50
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
2 changed files with 20 additions and 12 deletions

View File

@ -434,17 +434,12 @@ func (a *admin) getData_getPeers() []admin_nodeInfo {
for _, port := range ps { for _, port := range ps {
p := ports[port] p := ports[port]
addr := *address_addrForNodeID(getNodeID(&p.box)) addr := *address_addrForNodeID(getNodeID(&p.box))
duration := time.Since(p.firstSeen)
info := admin_nodeInfo{ info := admin_nodeInfo{
{"ip", net.IP(addr[:]).String()}, {"ip", net.IP(addr[:]).String()},
{"port", port}, {"port", port},
{"uptime", fmt.Sprintf("%02d:%02d:%02d", {"uptime", int(time.Since(p.firstSeen).Seconds())},
int(duration.Hours()), {"bytes_sent", atomic.LoadUint64(&p.bytesSent)},
int(duration.Minutes())%60, {"bytes_recvd", atomic.LoadUint64(&p.bytesRecvd)},
int(duration.Seconds())%60,
)},
{"bytes_sent", fmt.Sprintf("%d", atomic.LoadUint64(&p.bytesSent))},
{"bytes_recvd", fmt.Sprintf("%d", atomic.LoadUint64(&p.bytesRecvd))},
} }
peerInfos = append(peerInfos, info) peerInfos = append(peerInfos, info)
} }
@ -486,7 +481,7 @@ func (a *admin) getData_getDHT() []admin_nodeInfo {
{"coords", fmt.Sprint(v.coords)}, {"coords", fmt.Sprint(v.coords)},
{"bucket", i}, {"bucket", i},
{"peer_only", isPeer}, {"peer_only", isPeer},
{"last_seen", fmt.Sprintf("%ds", int(now.Sub(v.recv).Seconds()))}, {"last_seen", int(now.Sub(v.recv).Seconds())},
} }
infos = append(infos, info) infos = append(infos, info)
} }
@ -509,8 +504,8 @@ func (a *admin) getData_getSessions() []admin_nodeInfo {
{"coords", fmt.Sprint(sinfo.coords)}, {"coords", fmt.Sprint(sinfo.coords)},
{"mtu", sinfo.getMTU()}, {"mtu", sinfo.getMTU()},
{"was_mtu_fixed", sinfo.wasMTUFixed}, {"was_mtu_fixed", sinfo.wasMTUFixed},
{"bytes_sent", fmt.Sprintf("%d", sinfo.bytesSent)}, {"bytes_sent", sinfo.bytesSent},
{"bytes_recvd", fmt.Sprintf("%d", sinfo.bytesRecvd)}, {"bytes_recvd", sinfo.bytesRecvd},
} }
infos = append(infos, info) infos = append(infos, info)
} }

View File

@ -127,7 +127,20 @@ func main() {
for slk, slv := range tlv.(map[string]interface{}) { for slk, slv := range tlv.(map[string]interface{}) {
fmt.Printf("%-"+fmt.Sprint(maxWidths["key"])+"s ", slk) fmt.Printf("%-"+fmt.Sprint(maxWidths["key"])+"s ", slk)
for _, k := range keyOrder { for _, k := range keyOrder {
fmt.Printf("%-"+fmt.Sprint(maxWidths[k])+"s ", fmt.Sprint(slv.(map[string]interface{})[k])) preformatted := slv.(map[string]interface{})[k]
var formatted string
switch k {
case "bytes_sent", "bytes_recvd":
formatted = fmt.Sprintf("%d", uint(preformatted.(float64)))
case "uptime", "last_seen":
seconds := uint(preformatted.(float64)) % 60
minutes := uint(preformatted.(float64)/60) % 60
hours := uint(preformatted.(float64) / 60 / 60)
formatted = fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
default:
formatted = fmt.Sprint(preformatted)
}
fmt.Printf("%-"+fmt.Sprint(maxWidths[k])+"s ", formatted)
} }
fmt.Println() fmt.Println()
} }