-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
78 lines (70 loc) · 2.06 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package service
import (
"cess-cacher/base/cache"
"cess-cacher/config"
resp "cess-cacher/server/response"
"cess-cacher/utils"
"github.com/pkg/errors"
)
type MinerStats struct {
GeoLocation string `json:"geoLocation"`
BytePrice uint64 `json:"bytePrice"`
MinerStatus string `json:"status"`
NetStats cache.NetStats `json:"netStats"`
MemoryStats cache.MemoryStats `json:"memStats"`
CPUStats cache.CPUStats `json:"cpuStats"`
DiskStats cache.DiskStats `json:"diskStats"`
CacheStat cache.Stat `json:"cacheStat"`
}
type FileStat struct {
Cached bool `json:"cached"`
Price uint64 `json:"price"`
Size uint64 `json:"size"`
}
func QueryMinerStats() (MinerStats, resp.Error) {
var (
mstat MinerStats
err error
)
mstat.MinerStatus = "active"
mstat.NetStats = cache.GetNetInfo()
mstat.CacheStat = cache.GetCacheHandle().GetCacheStats()
mstat.BytePrice = config.GetConfig().BytePrice
mstat.MemoryStats, err = cache.GetMemoryStats()
if err != nil {
return mstat, resp.NewError(500, errors.Wrap(err, "query miner stats error"))
}
mstat.CPUStats, err = cache.GetCPUStats()
if err != nil {
return mstat, resp.NewError(500, errors.Wrap(err, "query miner stats error"))
}
mstat.DiskStats = cache.GetCacheDiskStats()
extIp, err := utils.GetExternalIp()
if err != nil {
return mstat, resp.NewError(500, errors.Wrap(err, "query miner stats error"))
}
country, city, err := utils.ParseCountryFromIp(extIp)
if err != nil {
return mstat, resp.NewError(500, errors.Wrap(err, "query miner stats error"))
}
mstat.GeoLocation = country + "," + city
return mstat, nil
}
func QueryCachedFiles() []string {
return cache.GetCacheHandle().GetHashList()
}
func QueryFileInfo(hash string) FileStat {
var stat FileStat
info, ok := cache.GetCacheHandle().QueryFile(hash)
if !ok {
//query info from chain
return stat
}
stat.Cached = true
stat.Price = uint64(info.Size) * config.GetConfig().BytePrice
stat.Size = uint64(info.Size)
return stat
}
func QueryBytePrice() uint64 {
return config.GetConfig().BytePrice
}