Skip to content

Commit

Permalink
Merge pull request #21 from algorandfoundation/fix/metrics-bytes-calc…
Browse files Browse the repository at this point in the history
…ulation

fix: RX/TX display
  • Loading branch information
PhearZero authored Nov 5, 2024
2 parents 49c0ee6 + b73a4f6 commit 616af98
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
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<<30))
}

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

0 comments on commit 616af98

Please sign in to comment.