Skip to content

Commit

Permalink
fix(nnr): fix nnr traefik show
Browse files Browse the repository at this point in the history
fix nnr traefik show

Signed-off-by: ysicing <i@ysicing.me>
  • Loading branch information
ysicing committed Oct 20, 2023
1 parent 29b9a50 commit e0a7a9f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cmd/nnr/nnr.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func listRules(f factory.Factory) *cobra.Command {
if all {
rulename = fmt.Sprintf("%s(%s)", index.Name, color.SGreen(index.Rid))
}
table.AddRow(rulename, sname, fmt.Sprintf("%v:%v", index.Host, index.Port), fmt.Sprintf("%v:%v", index.Remote, index.RPort), index.Type, util.Traffic(index.Traffic))
table.AddRow(rulename, sname, fmt.Sprintf("%v:%v", index.Host, index.Port), fmt.Sprintf("%v:%v", index.Remote, index.RPort), index.Type, util.Traffic(index.Traffic, 1000.0))
}
return output.EncodeTable(os.Stdout, table)
},
Expand Down Expand Up @@ -143,7 +143,7 @@ func listRemoteNode(f factory.Factory) *cobra.Command {
table := uitable.New()
table.AddRow("远程地址", "流量")
for _, index := range s {
table.AddRow(index.Remote, util.Traffic(index.Traffic))
table.AddRow(index.Remote, util.Traffic(index.Traffic, 1000.0))
}
return output.EncodeTable(os.Stdout, table)
},
Expand Down
16 changes: 10 additions & 6 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ import (
"math"
)

func Traffic(k int64) string {
t := math.Round(float64(k)/1024.0/1024.0*100) / 100
func Traffic(k int64, f ...float64) string {
value := 1024.0
if len(f) > 0 {
value = f[0]
}
t := math.Round(float64(k)/value/value*100) / 100
tunit := "MB"
if t >= 1024.0 {
t = t / 1024.0
if t >= value {
t = t / value
tunit = "GB"
}
if t >= 1024.0 {
t = t / 1024.0
if t >= value {
t = t / value
tunit = "TB"
}
return fmt.Sprintf("%.2f%v", t, tunit)
Expand Down

0 comments on commit e0a7a9f

Please sign in to comment.