mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-13 23:43:47 +00:00
code refactor
This commit is contained in:
parent
5039f4fc1e
commit
d50341053e
1 changed files with 78 additions and 22 deletions
|
|
@ -37,32 +37,88 @@ func getCoresCount(cp []cpu.InfoStat) int {
|
||||||
return cores
|
return cores
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MemoryDetails struct {
|
||||||
|
TotalMem float64 `json:"totalMem"`
|
||||||
|
FreeMem float64 `json:"freeMem"`
|
||||||
|
UsedMem float64 `json:"usedMem"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DiskDetails struct {
|
||||||
|
TotalDisk float64 `json:"totalDisk"`
|
||||||
|
FreeDisk float64 `json:"freeDisk"`
|
||||||
|
UsedDisk float64 `json:"usedDisk"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type BorFingerprint struct {
|
||||||
|
CoresCount int `json:"coresCount"`
|
||||||
|
OsName string `json:"osName"`
|
||||||
|
OsVer string `json:"osVer"`
|
||||||
|
DiskDetails *DiskDetails `json:"diskDetails"`
|
||||||
|
MemoryDetails *MemoryDetails `json:"memoryDetails"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatFingerprint(borFingerprint *BorFingerprint) string {
|
||||||
|
base := formatKV([]string{
|
||||||
|
fmt.Sprintf("Bor Version : %s", params.VersionWithMeta),
|
||||||
|
fmt.Sprintf("CPU : %d cores", borFingerprint.CoresCount),
|
||||||
|
fmt.Sprintf("OS : %s %s ", borFingerprint.OsName, borFingerprint.OsVer),
|
||||||
|
fmt.Sprintf("RAM :: total : %v GB, free : %v GB, used : %v GB", borFingerprint.MemoryDetails.TotalMem, borFingerprint.MemoryDetails.FreeMem, borFingerprint.MemoryDetails.UsedMem),
|
||||||
|
fmt.Sprintf("STORAGE :: total : %v GB, free : %v GB, used : %v GB", borFingerprint.DiskDetails.TotalDisk, borFingerprint.DiskDetails.FreeDisk, borFingerprint.DiskDetails.UsedDisk),
|
||||||
|
})
|
||||||
|
return base
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertBytesToGB(bytesValue uint64) float64 {
|
||||||
|
return math.Floor(float64(bytesValue)/(1024*1024*1024)*100) / 100
|
||||||
|
}
|
||||||
|
|
||||||
// Run implements the cli.Command interface
|
// Run implements the cli.Command interface
|
||||||
func (c *FingerprintCommand) Run(args []string) int {
|
func (c *FingerprintCommand) Run(args []string) int {
|
||||||
v, _ := mem.VirtualMemory()
|
|
||||||
h, _ := host.Info()
|
|
||||||
cp, _ := cpu.Info()
|
|
||||||
d, _ := disk.Usage("/")
|
|
||||||
|
|
||||||
osName := h.OS
|
v, err := mem.VirtualMemory()
|
||||||
osVer := h.Platform + " - " + h.PlatformVersion + " - " + h.KernelArch
|
if err != nil {
|
||||||
totalMem := math.Floor(float64(v.Total)/(1024*1024*1024)*100) / 100
|
c.UI.Error(err.Error())
|
||||||
availableMem := math.Floor(float64(v.Available)/(1024*1024*1024)*100) / 100
|
return 1
|
||||||
usedMem := math.Floor(float64(v.Used)/(1024*1024*1024)*100) / 100
|
}
|
||||||
totalDisk := math.Floor(float64(d.Total)/(1024*1024*1024)*100) / 100
|
|
||||||
availableDisk := math.Floor(float64(d.Free)/(1024*1024*1024)*100) / 100
|
|
||||||
usedDisk := math.Floor(float64(d.Used)/(1024*1024*1024)*100) / 100
|
|
||||||
|
|
||||||
borDetails := fmt.Sprintf("Bor Version : %s", params.VersionWithMeta)
|
h, err := host.Info()
|
||||||
cpuDetails := fmt.Sprintf("CPU : %d cores", getCoresCount(cp))
|
if err != nil {
|
||||||
osDetails := fmt.Sprintf("OS : %s %s ", osName, osVer)
|
c.UI.Error(err.Error())
|
||||||
memDetails := fmt.Sprintf("RAM :: total : %v GB, free : %v GB, used : %v GB", totalMem, availableMem, usedMem)
|
return 1
|
||||||
diskDetails := fmt.Sprintf("STORAGE :: total : %v GB, free : %v GB, used : %v GB", totalDisk, availableDisk, usedDisk)
|
}
|
||||||
|
|
||||||
c.UI.Output(borDetails)
|
cp, err := cpu.Info()
|
||||||
c.UI.Output(cpuDetails)
|
if err != nil {
|
||||||
c.UI.Output(osDetails)
|
c.UI.Error(err.Error())
|
||||||
c.UI.Output(memDetails)
|
return 1
|
||||||
c.UI.Output(diskDetails)
|
}
|
||||||
|
|
||||||
|
d, err := disk.Usage("/")
|
||||||
|
if err != nil {
|
||||||
|
c.UI.Error(err.Error())
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
diskDetails := &DiskDetails{
|
||||||
|
TotalDisk: convertBytesToGB(d.Total),
|
||||||
|
FreeDisk: convertBytesToGB(d.Free),
|
||||||
|
UsedDisk: convertBytesToGB(d.Used),
|
||||||
|
}
|
||||||
|
|
||||||
|
memoryDetails := &MemoryDetails{
|
||||||
|
TotalMem: convertBytesToGB(v.Total),
|
||||||
|
FreeMem: convertBytesToGB(v.Available),
|
||||||
|
UsedMem: convertBytesToGB(v.Used),
|
||||||
|
}
|
||||||
|
|
||||||
|
borFingerprint := &BorFingerprint{
|
||||||
|
CoresCount: getCoresCount(cp),
|
||||||
|
OsName: h.OS,
|
||||||
|
OsVer: h.Platform + " - " + h.PlatformVersion + " - " + h.KernelArch,
|
||||||
|
DiskDetails: diskDetails,
|
||||||
|
MemoryDetails: memoryDetails,
|
||||||
|
}
|
||||||
|
|
||||||
|
c.UI.Output(formatFingerprint(borFingerprint))
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue