From 1d37a37192e68e169e991d15383e6562ae63aa02 Mon Sep 17 00:00:00 2001 From: Michael Feher Date: Mon, 4 Nov 2024 13:43:56 -0500 Subject: [PATCH 1/2] fix: RX/TX display --- internal/metrics.go | 3 +++ internal/state.go | 11 +++++++++-- ui/status.go | 20 ++++++++++++++++++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/internal/metrics.go b/internal/metrics.go index d2403094..b10a9f40 100644 --- a/internal/metrics.go +++ b/internal/metrics.go @@ -17,6 +17,9 @@ type MetricsModel struct { TPS float64 RX int TX int + LastTS time.Time + LastRX int + LastTX int } type MetricsResponse map[string]int diff --git a/internal/state.go b/internal/state.go index c20508c0..e14c07aa 100644 --- a/internal/state.go +++ b/internal/state.go @@ -91,8 +91,15 @@ func (s *StateModel) UpdateMetricsFromRPC(ctx context.Context, client *api.Clien } if err == nil { s.Metrics.Enabled = true - s.Metrics.TX = res["algod_network_sent_bytes_total"] - s.Metrics.RX = res["algod_network_received_bytes_total"] + now := time.Now() + diff := now.Sub(s.Metrics.LastTS) + + s.Metrics.TX = max(0, int(float64(res["algod_network_sent_bytes_total"]-s.Metrics.LastTX)/diff.Seconds())) + s.Metrics.RX = max(0, int(float64(res["algod_network_received_bytes_total"]-s.Metrics.LastRX)/diff.Seconds())) + + s.Metrics.LastTS = now + s.Metrics.LastTX = res["algod_network_sent_bytes_total"] + s.Metrics.LastRX = res["algod_network_received_bytes_total"] } } func (s *StateModel) UpdateAccounts(client *api.ClientWithResponses) { diff --git a/ui/status.go b/ui/status.go index 281288bf..fd9cce2a 100644 --- a/ui/status.go +++ b/ui/status.go @@ -6,6 +6,7 @@ import ( "github.com/algorandfoundation/hack-tui/ui/style" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" + "math" "strconv" "strings" "time" @@ -44,6 +45,21 @@ func (m StatusViewModel) HandleMessage(msg tea.Msg) (StatusViewModel, tea.Cmd) { return m, nil } +func getBitRate(bytes int) string { + txString := fmt.Sprintf("%d B/s ", bytes) + if bytes >= 1024 { + txString = fmt.Sprintf("%d KB/s ", bytes/(1<<10)) + } + if bytes >= int(math.Pow(1024, 2)) { + txString = fmt.Sprintf("%d MB/s ", bytes/(1<<20)) + } + if bytes >= int(math.Pow(1024, 3)) { + txString = fmt.Sprintf("%d GB/s ", bytes/(1<<20)) + } + + return txString +} + // View handles the render cycle func (m StatusViewModel) View() string { if !m.IsVisible { @@ -70,13 +86,13 @@ func (m StatusViewModel) View() string { row1 := lipgloss.JoinHorizontal(lipgloss.Left, beginning, middle, end) beginning = style.Blue.Render(" Round time: ") + fmt.Sprintf("%.2fs", float64(m.Data.Metrics.RoundTime)/float64(time.Second)) - end = fmt.Sprintf("%d KB/s ", m.Data.Metrics.TX/1024) + style.Green.Render("TX ") + end = getBitRate(m.Data.Metrics.TX) + style.Green.Render("TX ") middle = strings.Repeat(" ", max(0, size-(lipgloss.Width(beginning)+lipgloss.Width(end)+2))) row2 := lipgloss.JoinHorizontal(lipgloss.Left, beginning, middle, end) beginning = style.Blue.Render(" TPS: ") + fmt.Sprintf("%.2f", m.Data.Metrics.TPS) - end = fmt.Sprintf("%d KB/s ", m.Data.Metrics.RX/1024) + style.Green.Render("RX ") + end = getBitRate(m.Data.Metrics.RX) + style.Green.Render("RX ") middle = strings.Repeat(" ", max(0, size-(lipgloss.Width(beginning)+lipgloss.Width(end)+2))) row3 := lipgloss.JoinHorizontal(lipgloss.Left, beginning, middle, end) From a0abfe6a1bf9ad0a98ceb89140fd3e51ee971d74 Mon Sep 17 00:00:00 2001 From: Michael Feher Date: Tue, 5 Nov 2024 14:59:59 -0500 Subject: [PATCH 2/2] fix: bit rate display for GB --- ui/status.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/status.go b/ui/status.go index fd9cce2a..451a5758 100644 --- a/ui/status.go +++ b/ui/status.go @@ -54,7 +54,7 @@ func getBitRate(bytes int) string { txString = fmt.Sprintf("%d MB/s ", bytes/(1<<20)) } if bytes >= int(math.Pow(1024, 3)) { - txString = fmt.Sprintf("%d GB/s ", bytes/(1<<20)) + txString = fmt.Sprintf("%d GB/s ", bytes/(1<<30)) } return txString