mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-07 23:30:26 +08:00
优化代码
This commit is contained in:
@@ -24,37 +24,19 @@ func Pow1024(n int) int64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func FormatBytes(bytes int64) string {
|
func FormatBytes(bytes int64) string {
|
||||||
if bytes < 1024 {
|
if bytes < Pow1024(1) {
|
||||||
return FormatInt64(bytes) + "B"
|
return FormatInt64(bytes) + "B"
|
||||||
} else if bytes < 1024*1024 {
|
} else if bytes < Pow1024(2) {
|
||||||
return fmt.Sprintf("%.2fKB", float64(bytes)/1024)
|
return fmt.Sprintf("%.2fKB", float64(bytes)/float64(Pow1024(1)))
|
||||||
} else if bytes < 1024*1024*1024 {
|
} else if bytes < Pow1024(3) {
|
||||||
return fmt.Sprintf("%.2fMB", float64(bytes)/1024/1024)
|
return fmt.Sprintf("%.2fMB", float64(bytes)/float64(Pow1024(2)))
|
||||||
} else if bytes < 1024*1024*1024*1024 {
|
} else if bytes < Pow1024(4) {
|
||||||
return fmt.Sprintf("%.2fGB", float64(bytes)/1024/1024/1024)
|
return fmt.Sprintf("%.2fGB", float64(bytes)/float64(Pow1024(3)))
|
||||||
} else if bytes < 1024*1024*1024*1024*1024 {
|
} else if bytes < Pow1024(5) {
|
||||||
return fmt.Sprintf("%.2fTB", float64(bytes)/1024/1024/1024/1024)
|
return fmt.Sprintf("%.2fTB", float64(bytes)/float64(Pow1024(4)))
|
||||||
} else if bytes < 1024*1024*1024*1024*1024*1024 {
|
} else if bytes < Pow1024(6) {
|
||||||
return fmt.Sprintf("%.2fPB", float64(bytes)/1024/1024/1024/1024/1024)
|
return fmt.Sprintf("%.2fPB", float64(bytes)/float64(Pow1024(5)))
|
||||||
} else {
|
} else {
|
||||||
return fmt.Sprintf("%.2fEB", float64(bytes)/1024/1024/1024/1024/1024/1024)
|
return fmt.Sprintf("%.2fEB", float64(bytes)/float64(Pow1024(6)))
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func FormatBits(bits int64) string {
|
|
||||||
if bits < 1000 {
|
|
||||||
return FormatInt64(bits) + "B"
|
|
||||||
} else if bits < 1000*1000 {
|
|
||||||
return fmt.Sprintf("%.2fKB", float64(bits)/1000)
|
|
||||||
} else if bits < 1000*1000*1000 {
|
|
||||||
return fmt.Sprintf("%.2fMB", float64(bits)/1000/1000)
|
|
||||||
} else if bits < 1000*1000*1000*1000 {
|
|
||||||
return fmt.Sprintf("%.2fGB", float64(bits)/1000/1000/1000)
|
|
||||||
} else if bits < 1000*1000*1000*1000*1000 {
|
|
||||||
return fmt.Sprintf("%.2fTB", float64(bits)/1000/1000/1000/1000)
|
|
||||||
} else if bits < 1000*1000*1000*1000*1000*1000 {
|
|
||||||
return fmt.Sprintf("%.2fPB", float64(bits)/1000/1000/1000/1000/1000)
|
|
||||||
} else {
|
|
||||||
return fmt.Sprintf("%.2fEB", float64(bits)/1000/1000/1000/1000/1000/1000)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
internal/utils/numberutils/utils_test.go
Normal file
16
internal/utils/numberutils/utils_test.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
|
||||||
|
package numberutils
|
||||||
|
|
||||||
|
import "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))
|
||||||
|
}
|
||||||
@@ -467,7 +467,7 @@ func (this *CreateAction) RunPost(params struct {
|
|||||||
AccessLogJSON: []byte(`{
|
AccessLogJSON: []byte(`{
|
||||||
"isPrior": false,
|
"isPrior": false,
|
||||||
"isOn": true,
|
"isOn": true,
|
||||||
"fields": [],
|
"fields": [6, 7],
|
||||||
"status1": true,
|
"status1": true,
|
||||||
"status2": true,
|
"status2": true,
|
||||||
"status3": true,
|
"status3": true,
|
||||||
|
|||||||
@@ -191,8 +191,8 @@ func (this *userMustAuth) modules(actionPtr actions.ActionWrapper, adminId int64
|
|||||||
var countNodeLogs = countNodeLogsResp.Count
|
var countNodeLogs = countNodeLogsResp.Count
|
||||||
if countNodeLogs > 0 {
|
if countNodeLogs > 0 {
|
||||||
countUnreadNodeLogs = countNodeLogs
|
countUnreadNodeLogs = countNodeLogs
|
||||||
if countUnreadNodeLogs >= 1000 {
|
if countUnreadNodeLogs >= 100 {
|
||||||
countUnreadNodeLogs = 999
|
countUnreadNodeLogs = 99
|
||||||
}
|
}
|
||||||
nodeLogsType = "unread"
|
nodeLogsType = "unread"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user