mirror of
				https://github.com/TeaOSLab/EdgeAdmin.git
				synced 2025-11-04 13:10:26 +08:00 
			
		
		
		
	优化代码
This commit is contained in:
		@@ -46,11 +46,31 @@ func FormatCount(count int64) string {
 | 
			
		||||
	if count < 1000 {
 | 
			
		||||
		return types.String(count)
 | 
			
		||||
	}
 | 
			
		||||
	if count < 1000 * 1000 {
 | 
			
		||||
	if count < 1000*1000 {
 | 
			
		||||
		return fmt.Sprintf("%.1fK", float32(count)/1000)
 | 
			
		||||
	}
 | 
			
		||||
	if count < 1000 * 1000 * 1000{
 | 
			
		||||
	if count < 1000*1000*1000 {
 | 
			
		||||
		return fmt.Sprintf("%.1fM", float32(count)/1000/1000)
 | 
			
		||||
	}
 | 
			
		||||
	return fmt.Sprintf("%.1fB", float32(count)/1000/1000/1000)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func FormatFloat(f interface{}, decimal int) string {
 | 
			
		||||
	if f == nil {
 | 
			
		||||
		return ""
 | 
			
		||||
	}
 | 
			
		||||
	switch x := f.(type) {
 | 
			
		||||
	case float32, float64:
 | 
			
		||||
		var s = fmt.Sprintf("%."+types.String(decimal)+"f", x)
 | 
			
		||||
		return s
 | 
			
		||||
	case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
 | 
			
		||||
		return types.String(x)
 | 
			
		||||
	case string:
 | 
			
		||||
		return x
 | 
			
		||||
	}
 | 
			
		||||
	return ""
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func FormatFloat2(f interface{}) string {
 | 
			
		||||
	return FormatFloat(f, 2)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,26 +1,36 @@
 | 
			
		||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
 | 
			
		||||
 | 
			
		||||
package numberutils
 | 
			
		||||
package numberutils_test
 | 
			
		||||
 | 
			
		||||
import "testing"
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestFormatBytes(t *testing.T) {
 | 
			
		||||
	t.Log(FormatBytes(1))
 | 
			
		||||
	t.Log(FormatBytes(1000))
 | 
			
		||||
	t.Log(FormatBytes(1_000_000))
 | 
			
		||||
	t.Log(FormatBytes(1_000_000_000))
 | 
			
		||||
	t.Log(FormatBytes(1_000_000_000_000))
 | 
			
		||||
	t.Log(FormatBytes(1_000_000_000_000_000))
 | 
			
		||||
	t.Log(FormatBytes(1_000_000_000_000_000_000))
 | 
			
		||||
	t.Log(FormatBytes(9_000_000_000_000_000_000))
 | 
			
		||||
	t.Log(numberutils.FormatBytes(1))
 | 
			
		||||
	t.Log(numberutils.FormatBytes(1000))
 | 
			
		||||
	t.Log(numberutils.FormatBytes(1_000_000))
 | 
			
		||||
	t.Log(numberutils.FormatBytes(1_000_000_000))
 | 
			
		||||
	t.Log(numberutils.FormatBytes(1_000_000_000_000))
 | 
			
		||||
	t.Log(numberutils.FormatBytes(1_000_000_000_000_000))
 | 
			
		||||
	t.Log(numberutils.FormatBytes(1_000_000_000_000_000_000))
 | 
			
		||||
	t.Log(numberutils.FormatBytes(9_000_000_000_000_000_000))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestFormatCount(t *testing.T) {
 | 
			
		||||
	t.Log(FormatCount(1))
 | 
			
		||||
	t.Log(FormatCount(1000))
 | 
			
		||||
	t.Log(FormatCount(1500))
 | 
			
		||||
	t.Log(FormatCount(1_000_000))
 | 
			
		||||
	t.Log(FormatCount(1_500_000))
 | 
			
		||||
	t.Log(FormatCount(1_000_000_000))
 | 
			
		||||
	t.Log(FormatCount(1_500_000_000))
 | 
			
		||||
	t.Log(numberutils.FormatCount(1))
 | 
			
		||||
	t.Log(numberutils.FormatCount(1000))
 | 
			
		||||
	t.Log(numberutils.FormatCount(1500))
 | 
			
		||||
	t.Log(numberutils.FormatCount(1_000_000))
 | 
			
		||||
	t.Log(numberutils.FormatCount(1_500_000))
 | 
			
		||||
	t.Log(numberutils.FormatCount(1_000_000_000))
 | 
			
		||||
	t.Log(numberutils.FormatCount(1_500_000_000))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestFormatFloat(t *testing.T) {
 | 
			
		||||
	t.Log(numberutils.FormatFloat(1, 2))
 | 
			
		||||
	t.Log(numberutils.FormatFloat(100.23456, 2))
 | 
			
		||||
	t.Log(numberutils.FormatFloat(100.000023, 2))
 | 
			
		||||
	t.Log(numberutils.FormatFloat(100.012, 2))
 | 
			
		||||
}
 | 
			
		||||
@@ -300,9 +300,9 @@ func (this *DetailAction) RunGet(params struct {
 | 
			
		||||
			"buildVersion":         status.BuildVersion,
 | 
			
		||||
			"cpuPhysicalCount":     status.CPUPhysicalCount,
 | 
			
		||||
			"cpuLogicalCount":      status.CPULogicalCount,
 | 
			
		||||
			"load1m":               fmt.Sprintf("%.2f", status.Load1m),
 | 
			
		||||
			"load5m":               fmt.Sprintf("%.2f", status.Load5m),
 | 
			
		||||
			"load15m":              fmt.Sprintf("%.2f", status.Load15m),
 | 
			
		||||
			"load1m":               numberutils.FormatFloat2(status.Load1m),
 | 
			
		||||
			"load5m":               numberutils.FormatFloat2(status.Load5m),
 | 
			
		||||
			"load15m":              numberutils.FormatFloat2(status.Load15m),
 | 
			
		||||
			"cacheTotalDiskSize":   numberutils.FormatBytes(status.CacheTotalDiskSize),
 | 
			
		||||
			"cacheTotalMemorySize": numberutils.FormatBytes(status.CacheTotalMemorySize),
 | 
			
		||||
		},
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
@@ -210,7 +211,7 @@ func (this *NodesAction) RunGet(params struct {
 | 
			
		||||
				"memUsageText":    fmt.Sprintf("%.2f%%", status.MemoryUsage*100),
 | 
			
		||||
				"trafficInBytes":  status.TrafficInBytes,
 | 
			
		||||
				"trafficOutBytes": status.TrafficOutBytes,
 | 
			
		||||
				"load1m":          fmt.Sprintf("%.2f", status.Load1m),
 | 
			
		||||
				"load1m":          numberutils.FormatFloat2(status.Load1m),
 | 
			
		||||
			},
 | 
			
		||||
			"cluster": maps.Map{
 | 
			
		||||
				"id":   node.NodeCluster.Id,
 | 
			
		||||
 
 | 
			
		||||
@@ -4,8 +4,8 @@ package clusters
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
@@ -207,12 +207,12 @@ func (this *NodesAction) RunGet(params struct {
 | 
			
		||||
				"updatedAt":       status.UpdatedAt,
 | 
			
		||||
				"hostname":        status.Hostname,
 | 
			
		||||
				"cpuUsage":        status.CPUUsage,
 | 
			
		||||
				"cpuUsageText":    fmt.Sprintf("%.2f%%", status.CPUUsage*100),
 | 
			
		||||
				"cpuUsageText":    numberutils.FormatFloat2(status.CPUUsage * 100),
 | 
			
		||||
				"memUsage":        status.MemoryUsage,
 | 
			
		||||
				"memUsageText":    fmt.Sprintf("%.2f%%", status.MemoryUsage*100),
 | 
			
		||||
				"memUsageText":    numberutils.FormatFloat2(status.MemoryUsage * 100),
 | 
			
		||||
				"trafficInBytes":  status.TrafficInBytes,
 | 
			
		||||
				"trafficOutBytes": status.TrafficOutBytes,
 | 
			
		||||
				"load1m":          fmt.Sprintf("%.2f", status.Load1m),
 | 
			
		||||
				"load1m":          numberutils.FormatFloat2(status.Load1m),
 | 
			
		||||
			},
 | 
			
		||||
			"cluster": maps.Map{
 | 
			
		||||
				"id":   node.NodeCluster.Id,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user