mirror of
				https://github.com/TeaOSLab/EdgeAdmin.git
				synced 2025-11-04 13:10:26 +08:00 
			
		
		
		
	节点列表负载数据如果是0,则显示0.00
This commit is contained in:
		@@ -75,7 +75,7 @@ func FormatCount(count int64) string {
 | 
			
		||||
	return fmt.Sprintf("%.1fB", float32(count)/1000/1000/1000)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func FormatFloat(f interface{}, decimal int) string {
 | 
			
		||||
func FormatFloat(f any, decimal int) string {
 | 
			
		||||
	if f == nil {
 | 
			
		||||
		return ""
 | 
			
		||||
	}
 | 
			
		||||
@@ -101,10 +101,29 @@ func FormatFloat(f interface{}, decimal int) string {
 | 
			
		||||
	return ""
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func FormatFloat2(f interface{}) string {
 | 
			
		||||
func FormatFloat2(f any) string {
 | 
			
		||||
	return FormatFloat(f, 2)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PadFloatZero 为浮点型数字字符串填充足够的0
 | 
			
		||||
func PadFloatZero(s string, countZero int) string {
 | 
			
		||||
	if countZero <= 0 {
 | 
			
		||||
		return s
 | 
			
		||||
	}
 | 
			
		||||
	if len(s) == 0 {
 | 
			
		||||
		s = "0"
 | 
			
		||||
	}
 | 
			
		||||
	var index = strings.Index(s, ".")
 | 
			
		||||
	if index < 0 {
 | 
			
		||||
		return s + "." + strings.Repeat("0", countZero)
 | 
			
		||||
	}
 | 
			
		||||
	var decimalLen = len(s) - 1 - index
 | 
			
		||||
	if decimalLen < countZero {
 | 
			
		||||
		return s + strings.Repeat("0", countZero-decimalLen)
 | 
			
		||||
	}
 | 
			
		||||
	return s
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var decimalReg = regexp.MustCompile(`^(\d+\.\d+)([a-zA-Z]+)?$`)
 | 
			
		||||
 | 
			
		||||
// TrimZeroSuffix 去除小数数字尾部多余的0
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ package numberutils_test
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils"
 | 
			
		||||
	"github.com/iwind/TeaGo/assert"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -49,6 +50,24 @@ func TestFormatFloat(t *testing.T) {
 | 
			
		||||
	t.Log(numberutils.FormatFloat(-221745.12, 2))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestFormatFloat2(t *testing.T) {
 | 
			
		||||
	t.Log(numberutils.FormatFloat2(0))
 | 
			
		||||
	t.Log(numberutils.FormatFloat2(0.0))
 | 
			
		||||
	t.Log(numberutils.FormatFloat2(1.23456))
 | 
			
		||||
	t.Log(numberutils.FormatFloat2(1.0))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestPadFloatZero(t *testing.T) {
 | 
			
		||||
	var a = assert.NewAssertion(t)
 | 
			
		||||
	a.IsTrue(numberutils.PadFloatZero("1", 0) == "1")
 | 
			
		||||
	a.IsTrue(numberutils.PadFloatZero("1", 2) == "1.00")
 | 
			
		||||
	a.IsTrue(numberutils.PadFloatZero("1.1", 2) == "1.10")
 | 
			
		||||
	a.IsTrue(numberutils.PadFloatZero("1.12", 2) == "1.12")
 | 
			
		||||
	a.IsTrue(numberutils.PadFloatZero("1.123", 2) == "1.123")
 | 
			
		||||
	a.IsTrue(numberutils.PadFloatZero("10000.123", 2) == "10000.123")
 | 
			
		||||
	a.IsTrue(numberutils.PadFloatZero("", 2) == "0.00")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestTrimZeroSuffix(t *testing.T) {
 | 
			
		||||
	for _, s := range []string{
 | 
			
		||||
		"1",
 | 
			
		||||
 
 | 
			
		||||
@@ -225,7 +225,7 @@ func (this *NodesAction) RunGet(params struct {
 | 
			
		||||
				"memUsageText":     fmt.Sprintf("%.2f%%", status.MemoryUsage*100),
 | 
			
		||||
				"trafficInBytes":   status.TrafficInBytes,
 | 
			
		||||
				"trafficOutBytes":  status.TrafficOutBytes,
 | 
			
		||||
				"load1m":           numberutils.FormatFloat2(status.Load1m),
 | 
			
		||||
				"load1m":           numberutils.PadFloatZero(numberutils.FormatFloat2(status.Load1m), 2),
 | 
			
		||||
				"countConnections": status.ConnectionCount,
 | 
			
		||||
			},
 | 
			
		||||
			"cluster": maps.Map{
 | 
			
		||||
 
 | 
			
		||||
@@ -227,7 +227,7 @@ func (this *NodesAction) RunGet(params struct {
 | 
			
		||||
				"memUsageText":     numberutils.FormatFloat2(status.MemoryUsage * 100),
 | 
			
		||||
				"trafficInBytes":   status.TrafficInBytes,
 | 
			
		||||
				"trafficOutBytes":  status.TrafficOutBytes,
 | 
			
		||||
				"load1m":           numberutils.FormatFloat2(status.Load1m),
 | 
			
		||||
				"load1m":           numberutils.PadFloatZero(numberutils.FormatFloat2(status.Load1m), 2),
 | 
			
		||||
				"countConnections": status.ConnectionCount,
 | 
			
		||||
			},
 | 
			
		||||
			"cluster": maps.Map{
 | 
			
		||||
 
 | 
			
		||||
@@ -139,7 +139,7 @@
 | 
			
		||||
            <span v-else class="disabled">-</span>
 | 
			
		||||
        </td>
 | 
			
		||||
        <td class="center" v-if="windowWidth < miniWidth || windowWidth > columnWidth5">
 | 
			
		||||
            <span v-if="node.status.isActive && node.status.load1m > 0">{{node.status.load1m}}</span>
 | 
			
		||||
            <span v-if="node.status.isActive">{{node.status.load1m}}</span>
 | 
			
		||||
            <span v-else class="disabled">-</span>
 | 
			
		||||
        </td>
 | 
			
		||||
		<td class="center">
 | 
			
		||||
 
 | 
			
		||||
@@ -134,7 +134,7 @@
 | 
			
		||||
            <span v-else class="disabled">-</span>
 | 
			
		||||
        </td>
 | 
			
		||||
        <td class="center" v-if="windowWidth < miniWidth || windowWidth > columnWidth5">
 | 
			
		||||
            <span v-if="node.status.isActive && node.status.load1m > 0">{{node.status.load1m}}</span>
 | 
			
		||||
            <span v-if="node.status.isActive">{{node.status.load1m}}</span>
 | 
			
		||||
            <span v-else class="disabled">-</span>
 | 
			
		||||
        </td>
 | 
			
		||||
        <td class="center">
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user