From 493cd196b7e621c3779ef8036c3404b98bfd23f9 Mon Sep 17 00:00:00 2001 From: jiuker <2818723467@qq.com> Date: Wed, 19 Jul 2023 23:34:35 +0800 Subject: [PATCH] fix: remove count flag and show the byteRate for support top net (#4639) --- cmd/support-top-net.go | 6 ------ cmd/top-net-spinner.go | 31 +++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/cmd/support-top-net.go b/cmd/support-top-net.go index b87e7461..982baf0d 100644 --- a/cmd/support-top-net.go +++ b/cmd/support-top-net.go @@ -29,11 +29,6 @@ import ( ) var supportTopNetFlags = []cli.Flag{ - cli.IntFlag{ - Name: "count, c", - Usage: "show top count interfaces", - Value: 10, - }, cli.IntFlag{ Name: "interval", Usage: "interval between requests in seconds", @@ -100,7 +95,6 @@ func mainSupportTopNet(ctx *cli.Context) error { Type: madmin.MetricNet, Interval: time.Duration(ctx.Int("interval")) * time.Second, ByHost: true, - N: ctx.Int("count"), Hosts: hosts, } diff --git a/cmd/top-net-spinner.go b/cmd/top-net-spinner.go index 0d33a6fa..4710b77f 100644 --- a/cmd/top-net-spinner.go +++ b/cmd/top-net-spinner.go @@ -19,8 +19,10 @@ package cmd import ( "fmt" + "math" "sort" "strings" + "time" "github.com/charmbracelet/bubbles/spinner" tea "github.com/charmbracelet/bubbletea" @@ -28,6 +30,7 @@ import ( "github.com/dustin/go-humanize" "github.com/minio/madmin-go/v3" "github.com/olekukonko/tablewriter" + "github.com/prometheus/procfs" ) type topNetUI struct { @@ -36,6 +39,7 @@ type topNetUI struct { sortAsc bool + prevTopMap map[string]topNetResult currTopMap map[string]topNetResult } @@ -64,6 +68,7 @@ func (m *topNetUI) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } return m, nil case topNetResult: + m.prevTopMap[msg.endPoint] = m.currTopMap[msg.endPoint] m.currTopMap[msg.endPoint] = msg if msg.final { m.quitting = true @@ -80,6 +85,13 @@ func (m *topNetUI) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } +func (m *topNetUI) calculationRate(prev, curr uint64, dur time.Duration) uint64 { + if curr < prev { + return uint64(float64(math.MaxUint64-prev+curr) / dur.Seconds()) + } + return uint64(float64(curr-prev) / dur.Seconds()) +} + func (m *topNetUI) View() string { var s strings.Builder // Set table header @@ -99,8 +111,22 @@ func (m *topNetUI) View() string { data := make([]topNetResult, 0, len(m.currTopMap)) - for _, metric := range m.currTopMap { - data = append(data, metric) + for endPoint, curr := range m.currTopMap { + if prev, ok := m.prevTopMap[endPoint]; ok { + data = append(data, topNetResult{ + final: curr.final, + endPoint: curr.endPoint, + error: curr.error, + stats: madmin.NetMetrics{ + CollectedAt: curr.stats.CollectedAt, + InterfaceName: curr.stats.InterfaceName, + NetStats: procfs.NetDevLine{ + RxBytes: m.calculationRate(prev.stats.NetStats.RxBytes, curr.stats.NetStats.RxBytes, curr.stats.CollectedAt.Sub(prev.stats.CollectedAt)), + TxBytes: m.calculationRate(prev.stats.NetStats.TxBytes, curr.stats.NetStats.TxBytes, curr.stats.CollectedAt.Sub(prev.stats.CollectedAt)), + }, + }, + }) + } } sort.Slice(data, func(i, j int) bool { @@ -143,5 +169,6 @@ func initTopNetUI() *topNetUI { return &topNetUI{ spinner: s, currTopMap: make(map[string]topNetResult), + prevTopMap: make(map[string]topNetResult), } }