diff --git a/Makefile b/Makefile index 4ae902d..5eef6db 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,9 @@ clean: format: tidy go fmt ./... +golines: + golines -w --ignore-generated --chain-split-dots --max-len=80 --reformat-tags . + test: tidy go test -v -race ./... diff --git a/main.go b/main.go index 6706ca4..f7ac94f 100644 --- a/main.go +++ b/main.go @@ -227,7 +227,9 @@ func main() { resourceTextView.SetText(resourceText).SetTitle("Resources").SetBorder(true) connectionText = getConnectionText(ctx) - connectionTextView.SetText(connectionText).SetTitle("Connections").SetBorder(true) + connectionTextView.SetText(connectionText). + SetTitle("Connections"). + SetBorder(true) coreText = getCoreText(ctx) coreTextView.SetText(coreText).SetTitle("Core").SetBorder(true) @@ -236,7 +238,9 @@ func main() { chainTextView.SetText(chainText).SetTitle("Chain").SetBorder(true) blockText = getBlockText(ctx) - blockTextView.SetText(blockText).SetTitle("Block Propagation").SetBorder(true) + blockTextView.SetText(blockText). + SetTitle("Block Propagation"). + SetBorder(true) peerText = getPeerText(ctx) peerTextView.SetText(peerText).SetTitle("Peers").SetBorder(true) @@ -255,6 +259,7 @@ func main() { 1, 1, false). + // Row 2 is our main text section, and its own flex AddItem(layout. AddItem(leftSide.SetDirection(tview.FlexRow). @@ -345,7 +350,11 @@ func main() { coreTextView.Clear() coreTextView.SetText(coreText) } - tmpText = fmt.Sprintf("%s\n%s", getEpochText(ctx), getChainText(ctx)) + tmpText = fmt.Sprintf( + "%s\n%s", + getEpochText(ctx), + getChainText(ctx), + ) if tmpText != "" && tmpText != chainText { chainText = tmpText chainTextView.Clear() @@ -423,7 +432,11 @@ func main() { coreTextView.Clear() coreTextView.SetText(coreText) } - tmpText = fmt.Sprintf("%s\n%s", getEpochText(ctx), getChainText(ctx)) + tmpText = fmt.Sprintf( + "%s\n%s", + getEpochText(ctx), + getChainText(ctx), + ) if tmpText != "" && tmpText != chainText { chainText = tmpText chainTextView.Clear() @@ -797,29 +810,49 @@ func getBlockText(ctx context.Context) string { delay := fmt.Sprintf("%.2f", promMetrics.BlockDelay) // Row 1 - sb.WriteString(fmt.Sprintf(" [green]Last Delay : [white]%s[blue]%-"+strconv.Itoa(10-len(delay))+"s", - delay, - "s", - )) - sb.WriteString(fmt.Sprintf(" [green]Served : [white]%-"+strconv.Itoa(10)+"s", - strconv.FormatUint(promMetrics.BlocksServed, 10), - )) - sb.WriteString(fmt.Sprintf(" [green]Late (>5s) : [white]%-"+strconv.Itoa(10)+"s\n", - strconv.FormatUint(promMetrics.BlocksLate, 10), - )) + sb.WriteString( + fmt.Sprintf( + " [green]Last Delay : [white]%s[blue]%-"+strconv.Itoa( + 10-len(delay), + )+"s", + delay, + "s", + ), + ) + sb.WriteString( + fmt.Sprintf(" [green]Served : [white]%-"+strconv.Itoa(10)+"s", + strconv.FormatUint(promMetrics.BlocksServed, 10), + ), + ) + sb.WriteString( + fmt.Sprintf(" [green]Late (>5s) : [white]%-"+strconv.Itoa(10)+"s\n", + strconv.FormatUint(promMetrics.BlocksLate, 10), + ), + ) // Row 2 - sb.WriteString(fmt.Sprintf(" [green]Within 1s : [white]%s%-"+strconv.Itoa(10-len(blk1s))+"s", - blk1s, - "%", - )) - sb.WriteString(fmt.Sprintf(" [green]Within 3s : [white]%s%-"+strconv.Itoa(10-len(blk3s))+"s", - blk3s, - "%", - )) - sb.WriteString(fmt.Sprintf(" [green]Within 5s : [white]%s%-"+strconv.Itoa(10-len(blk5s))+"s\n", - blk5s, - "%", - )) + sb.WriteString( + fmt.Sprintf( + " [green]Within 1s : [white]%s%-"+strconv.Itoa(10-len(blk1s))+"s", + blk1s, + "%", + ), + ) + sb.WriteString( + fmt.Sprintf( + " [green]Within 3s : [white]%s%-"+strconv.Itoa(10-len(blk3s))+"s", + blk3s, + "%", + ), + ) + sb.WriteString( + fmt.Sprintf( + " [green]Within 5s : [white]%s%-"+strconv.Itoa( + 10-len(blk5s), + )+"s\n", + blk5s, + "%", + ), + ) failCount = 0 return fmt.Sprint(sb.String()) @@ -835,14 +868,23 @@ func getNodeText(ctx context.Context) string { } nodeVersion, nodeRevision, _ := getNodeVersion() var sb strings.Builder - sb.WriteString(fmt.Sprintf(" [green]Name : [white]%s\n", cfg.App.NodeName)) + sb.WriteString( + fmt.Sprintf(" [green]Name : [white]%s\n", cfg.App.NodeName), + ) sb.WriteString(fmt.Sprintf(" [green]Role : [white]%s\n", role)) sb.WriteString(fmt.Sprintf(" [green]Network : [white]%s\n", network)) - sb.WriteString(fmt.Sprintf(" [green]Version : [white]%s\n", - fmt.Sprintf("[white]%s[blue] [[white]%s[blue]]", nodeVersion, nodeRevision), + sb.WriteString(fmt.Sprintf( + " [green]Version : [white]%s\n", + fmt.Sprintf( + "[white]%s[blue] [[white]%s[blue]]", + nodeVersion, + nodeRevision, + ), )) if publicIP != nil { - sb.WriteString(fmt.Sprintf(" [green]Public IP : [white]%s\n", publicIP)) + sb.WriteString( + fmt.Sprintf(" [green]Public IP : [white]%s\n", publicIP), + ) } else { sb.WriteString(fmt.Sprintln()) } @@ -960,7 +1002,12 @@ func getPeerText(ctx context.Context) string { // Divider sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", width-1))) - sb.WriteString(fmt.Sprintf(" [green]Total / Undetermined : [white]%d[white] / ", peerCount)) + sb.WriteString( + fmt.Sprintf( + " [green]Total / Undetermined : [white]%d[white] / ", + peerCount, + ), + ) if peerStats.CNT0 == 0 { sb.WriteString("[blue]0[white]") } else { @@ -968,7 +1015,12 @@ func getPeerText(ctx context.Context) string { } // TODO: figure out spacing here if peerStats.RTTAVG >= 200 { - sb.WriteString(fmt.Sprintf(" Average RTT : [fuchsia]%d[white] ms\n", peerStats.RTTAVG)) + sb.WriteString( + fmt.Sprintf( + " Average RTT : [fuchsia]%d[white] ms\n", + peerStats.RTTAVG, + ), + ) } else if peerStats.RTTAVG >= 100 { sb.WriteString(fmt.Sprintf(" Average RTT : [red]%d[white] ms\n", peerStats.RTTAVG)) } else if peerStats.RTTAVG >= 50 { @@ -982,7 +1034,9 @@ func getPeerText(ctx context.Context) string { // Divider sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", width-1))) - sb.WriteString(fmt.Sprintf(" [green]# %24s I/O RTT Geolocation\n", "REMOTE PEER")) + sb.WriteString( + fmt.Sprintf(" [green]# %24s I/O RTT Geolocation\n", "REMOTE PEER"), + ) // peerLocationWidth := width - 41 for peerNbr, peer := range peerStats.RTTresultsSlice { peerNbr++ @@ -1065,15 +1119,42 @@ func getResourceText(ctx context.Context) string { } memRss := fmt.Sprintf("%.1f", float64(rss)/float64(1073741824)) - memLive := fmt.Sprintf("%.1f", float64(promMetrics.MemLive)/float64(1073741824)) - memHeap := fmt.Sprintf("%.1f", float64(promMetrics.MemHeap)/float64(1073741824)) - - sb.WriteString(fmt.Sprintf(" [green]CPU (sys) : [white]%s%%\n", fmt.Sprintf("%.2f", cpuPercent))) - sb.WriteString(fmt.Sprintf(" [green]Mem (Live) : [white]%s[blue]G\n", memLive)) - sb.WriteString(fmt.Sprintf(" [green]Mem (RSS) : [white]%s[blue]G\n", memRss)) - sb.WriteString(fmt.Sprintf(" [green]Mem (Heap) : [white]%s[blue]G\n", memHeap)) - sb.WriteString(fmt.Sprintf(" [green]GC Minor : [white]%s\n", strconv.FormatUint(promMetrics.GcMinor, 10))) - sb.WriteString(fmt.Sprintf(" [green]GC Major : [white]%s\n", strconv.FormatUint(promMetrics.GcMajor, 10))) + memLive := fmt.Sprintf( + "%.1f", + float64(promMetrics.MemLive)/float64(1073741824), + ) + memHeap := fmt.Sprintf( + "%.1f", + float64(promMetrics.MemHeap)/float64(1073741824), + ) + + sb.WriteString( + fmt.Sprintf( + " [green]CPU (sys) : [white]%s%%\n", + fmt.Sprintf("%.2f", cpuPercent), + ), + ) + sb.WriteString( + fmt.Sprintf(" [green]Mem (Live) : [white]%s[blue]G\n", memLive), + ) + sb.WriteString( + fmt.Sprintf(" [green]Mem (RSS) : [white]%s[blue]G\n", memRss), + ) + sb.WriteString( + fmt.Sprintf(" [green]Mem (Heap) : [white]%s[blue]G\n", memHeap), + ) + sb.WriteString( + fmt.Sprintf( + " [green]GC Minor : [white]%s\n", + strconv.FormatUint(promMetrics.GcMinor, 10), + ), + ) + sb.WriteString( + fmt.Sprintf( + " [green]GC Major : [white]%s\n", + strconv.FormatUint(promMetrics.GcMajor, 10), + ), + ) return fmt.Sprint(sb.String()) } diff --git a/node.go b/node.go index f7ee91e..83383ca 100644 --- a/node.go +++ b/node.go @@ -57,7 +57,8 @@ func getP2P(ctx context.Context, processMetrics *process.Process) bool { } cmd, err := processMetrics.CmdlineWithContext(ctx) if err == nil { - if !strings.Contains(cmd, "p2p") && strings.Contains(cmd, "--config") { + if !strings.Contains(cmd, "p2p") && + strings.Contains(cmd, "--config") { cmdArray := strings.Split(cmd, " ") for p, arg := range cmdArray { if arg == "--config" { diff --git a/peers.go b/peers.go index f7e1034..4be34b6 100644 --- a/peers.go +++ b/peers.go @@ -35,7 +35,8 @@ var scrollPeers bool = false func filterPeers(ctx context.Context) error { var peers []string - if len(peerStats.RTTresultsSlice) != 0 && len(peerStats.RTTresultsSlice) == len(peersFiltered) { + if len(peerStats.RTTresultsSlice) != 0 && + len(peerStats.RTTresultsSlice) == len(peersFiltered) { return nil } if processMetrics == nil { @@ -44,7 +45,11 @@ func filterPeers(ctx context.Context) error { cfg := config.GetConfig() // Get process in/out connections - connections, err := netutil.ConnectionsPidWithContext(ctx, "tcp", processMetrics.Pid) + connections, err := netutil.ConnectionsPidWithContext( + ctx, + "tcp", + processMetrics.Pid, + ) if err != nil { return err } @@ -219,7 +224,10 @@ func pingPeers(ctx context.Context) error { UpdatedAt: time.Now(), } peerStats.RTTresultsMap[peerIP] = peer - peerStats.RTTresultsSlice = append(peerStats.RTTresultsSlice, peer) + peerStats.RTTresultsSlice = append( + peerStats.RTTresultsSlice, + peer, + ) sort.Sort(peerStats.RTTresultsSlice) }() wg.Wait() @@ -227,16 +235,33 @@ func pingPeers(ctx context.Context) error { peerCNTreachable := peerCount - peerStats.CNT0 if peerCNTreachable > 0 { peerStats.RTTAVG = peerStats.RTTSUM / peerCNTreachable - peerStats.PCT1 = float32(peerStats.CNT1) / float32(peerCNTreachable) * 100 + peerStats.PCT1 = float32( + peerStats.CNT1, + ) / float32( + peerCNTreachable, + ) * 100 peerStats.PCT1items = int(peerStats.PCT1) * granularitySmall / 100 - peerStats.PCT2 = float32(peerStats.CNT2) / float32(peerCNTreachable) * 100 + peerStats.PCT2 = float32( + peerStats.CNT2, + ) / float32( + peerCNTreachable, + ) * 100 peerStats.PCT2items = int(peerStats.PCT2) * granularitySmall / 100 - peerStats.PCT3 = float32(peerStats.CNT3) / float32(peerCNTreachable) * 100 + peerStats.PCT3 = float32( + peerStats.CNT3, + ) / float32( + peerCNTreachable, + ) * 100 peerStats.PCT3items = int(peerStats.PCT3) * granularitySmall / 100 - peerStats.PCT4 = float32(peerStats.CNT4) / float32(peerCNTreachable) * 100 + peerStats.PCT4 = float32( + peerStats.CNT4, + ) / float32( + peerCNTreachable, + ) * 100 peerStats.PCT4items = int(peerStats.PCT4) * granularitySmall / 100 } - if len(peerStats.RTTresultsSlice) != 0 && len(peerStats.RTTresultsSlice) >= peerCount { + if len(peerStats.RTTresultsSlice) != 0 && + len(peerStats.RTTresultsSlice) >= peerCount { checkPeers = false scrollPeers = true } diff --git a/utils.go b/utils.go index 312ff45..d568699 100644 --- a/utils.go +++ b/utils.go @@ -92,5 +92,9 @@ func getGeoIP(ctx context.Context, address string) string { return fmt.Sprintf("%v", record.Country.IsoCode) } } - return fmt.Sprintf("%v, %v", record.City.Names["en"], record.Country.IsoCode) + return fmt.Sprintf( + "%v, %v", + record.City.Names["en"], + record.Country.IsoCode, + ) }