Skip to content

Commit 534e50c

Browse files
authored
Merge pull request #21 from algorandfoundation/fix/metrics-bytes-calculation
fix: RX/TX display
2 parents 3036470 + a0abfe6 commit 534e50c

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

internal/metrics.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ type MetricsModel struct {
1717
TPS float64
1818
RX int
1919
TX int
20+
LastTS time.Time
21+
LastRX int
22+
LastTX int
2023
}
2124

2225
type MetricsResponse map[string]int

internal/state.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,15 @@ func (s *StateModel) UpdateMetricsFromRPC(ctx context.Context, client *api.Clien
9191
}
9292
if err == nil {
9393
s.Metrics.Enabled = true
94-
s.Metrics.TX = res["algod_network_sent_bytes_total"]
95-
s.Metrics.RX = res["algod_network_received_bytes_total"]
94+
now := time.Now()
95+
diff := now.Sub(s.Metrics.LastTS)
96+
97+
s.Metrics.TX = max(0, int(float64(res["algod_network_sent_bytes_total"]-s.Metrics.LastTX)/diff.Seconds()))
98+
s.Metrics.RX = max(0, int(float64(res["algod_network_received_bytes_total"]-s.Metrics.LastRX)/diff.Seconds()))
99+
100+
s.Metrics.LastTS = now
101+
s.Metrics.LastTX = res["algod_network_sent_bytes_total"]
102+
s.Metrics.LastRX = res["algod_network_received_bytes_total"]
96103
}
97104
}
98105
func (s *StateModel) UpdateAccounts(client *api.ClientWithResponses) {

ui/status.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/algorandfoundation/hack-tui/ui/style"
77
tea "github.com/charmbracelet/bubbletea"
88
"github.com/charmbracelet/lipgloss"
9+
"math"
910
"strconv"
1011
"strings"
1112
"time"
@@ -44,6 +45,21 @@ func (m StatusViewModel) HandleMessage(msg tea.Msg) (StatusViewModel, tea.Cmd) {
4445
return m, nil
4546
}
4647

48+
func getBitRate(bytes int) string {
49+
txString := fmt.Sprintf("%d B/s ", bytes)
50+
if bytes >= 1024 {
51+
txString = fmt.Sprintf("%d KB/s ", bytes/(1<<10))
52+
}
53+
if bytes >= int(math.Pow(1024, 2)) {
54+
txString = fmt.Sprintf("%d MB/s ", bytes/(1<<20))
55+
}
56+
if bytes >= int(math.Pow(1024, 3)) {
57+
txString = fmt.Sprintf("%d GB/s ", bytes/(1<<30))
58+
}
59+
60+
return txString
61+
}
62+
4763
// View handles the render cycle
4864
func (m StatusViewModel) View() string {
4965
if !m.IsVisible {
@@ -70,13 +86,13 @@ func (m StatusViewModel) View() string {
7086
row1 := lipgloss.JoinHorizontal(lipgloss.Left, beginning, middle, end)
7187

7288
beginning = style.Blue.Render(" Round time: ") + fmt.Sprintf("%.2fs", float64(m.Data.Metrics.RoundTime)/float64(time.Second))
73-
end = fmt.Sprintf("%d KB/s ", m.Data.Metrics.TX/1024) + style.Green.Render("TX ")
89+
end = getBitRate(m.Data.Metrics.TX) + style.Green.Render("TX ")
7490
middle = strings.Repeat(" ", max(0, size-(lipgloss.Width(beginning)+lipgloss.Width(end)+2)))
7591

7692
row2 := lipgloss.JoinHorizontal(lipgloss.Left, beginning, middle, end)
7793

7894
beginning = style.Blue.Render(" TPS: ") + fmt.Sprintf("%.2f", m.Data.Metrics.TPS)
79-
end = fmt.Sprintf("%d KB/s ", m.Data.Metrics.RX/1024) + style.Green.Render("RX ")
95+
end = getBitRate(m.Data.Metrics.RX) + style.Green.Render("RX ")
8096
middle = strings.Repeat(" ", max(0, size-(lipgloss.Width(beginning)+lipgloss.Width(end)+2)))
8197

8298
row3 := lipgloss.JoinHorizontal(lipgloss.Left, beginning, middle, end)

0 commit comments

Comments
 (0)