Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: RX/TX display #21

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions internal/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 18 additions & 2 deletions ui/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
HashMapsData2Value marked this conversation as resolved.
Show resolved Hide resolved
}

return txString
}

// View handles the render cycle
func (m StatusViewModel) View() string {
if !m.IsVisible {
Expand All @@ -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)
Expand Down