Explorar el Código

fix(snmp): show 2.5 Gbps interfaces with their decimal, not truncated (#1284)

One decimal for non-whole Gbps. #1280
Nacho hace 2 meses
padre
commit
7ced19321f

+ 6 - 0
server-go/internal/snmp/ifTable.go

@@ -43,6 +43,12 @@ func (p PortStats) SpeedString() string {
 		return ""
 	}
 	if bps >= 1_000_000_000 {
+		// #1280: un decimal cuando no es múltiplo exacto de 1 Gbps. La
+		// división entera truncaba un 2.5 GE a "2 Gbps" (ifSpeed reporta
+		// 2500000000 y el dígito se perdía aquí).
+		if bps%1_000_000_000 != 0 {
+			return fmt.Sprintf("%.1f Gbps", float64(bps)/1_000_000_000)
+		}
 		return fmt.Sprintf("%d Gbps", bps/1_000_000_000)
 	}
 	if bps >= 1_000_000 {

+ 24 - 0
server-go/internal/snmp/ifTable_test.go

@@ -0,0 +1,24 @@
+package snmp
+
+import "testing"
+
+// #1280: un 2.5 GE reporta ifSpeed=2500000000; la división entera lo
+// truncaba a "2 Gbps". Los múltiplos exactos siguen sin decimales.
+func TestSpeedStringQuarterGigMultiples(t *testing.T) {
+	cases := []struct {
+		bps  uint64
+		want string
+	}{
+		{1_000_000_000, "1 Gbps"},
+		{2_500_000_000, "2.5 Gbps"},
+		{5_000_000_000, "5 Gbps"},
+		{10_000_000_000, "10 Gbps"},
+		{1_000_000, "1 Mbps"},
+	}
+	for _, c := range cases {
+		p := PortStats{OperUp: true, SpeedBps: c.bps}
+		if got := p.SpeedString(); got != c.want {
+			t.Errorf("SpeedString(%d) = %q, want %q", c.bps, got, c.want)
+		}
+	}
+}

+ 1 - 1
server-go/internal/snmp/snmp_test.go

@@ -85,7 +85,7 @@ func TestPortStatsSpeedString(t *testing.T) {
 		{"1G highspeed", PortStats{OperUp: true, HighSpeedMbps: 1000}, "1 Gbps"},
 		{"10G highspeed", PortStats{OperUp: true, HighSpeedMbps: 10000}, "10 Gbps"},
 		{"100M ifSpeed", PortStats{OperUp: true, SpeedBps: 100_000_000}, "100 Mbps"},
-		{"2.5G highspeed", PortStats{OperUp: true, HighSpeedMbps: 2500}, "2 Gbps"},
+		{"2.5G highspeed", PortStats{OperUp: true, HighSpeedMbps: 2500}, "2.5 Gbps"}, // #1280: ya no trunca
 		{"zero speed", PortStats{OperUp: true}, ""},
 	}
 	for _, tt := range tests {