/
api.go
166 lines (149 loc) · 4.45 KB
/
api.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package stratum
import (
"encoding/json"
"net/http"
"sync/atomic"
"time"
"github.com/PowPool/xmrpool/rpc"
"github.com/PowPool/xmrpool/util"
)
func (s *StratumServer) StatsIndex(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
hashrate, hashrate24h, totalOnline, miners := s.collectMinersStats()
stats := map[string]interface{}{
"miners": miners,
"hashrate": hashrate,
"hashrate24h": hashrate24h,
"totalMiners": len(miners),
"totalOnline": totalOnline,
"timedOut": len(miners) - totalOnline,
"now": util.MakeTimestamp(),
}
var upstreams []interface{}
current := atomic.LoadInt32(&s.upstream)
for i, u := range s.upstreams {
upstream := convertUpstream(u)
upstream["current"] = current == int32(i)
upstreams = append(upstreams, upstream)
}
stats["upstreams"] = upstreams
stats["current"] = convertUpstream(s.rpc())
stats["luck"] = s.getLuckStats()
stats["blocks"] = s.getBlocksStats()
if t := s.currentBlockTemplate(); t != nil {
stats["height"] = t.height
stats["diff"] = t.diffInt64
roundShares := atomic.LoadInt64(&s.roundShares)
stats["variance"] = float64(roundShares) / float64(t.diffInt64)
stats["prevHash"] = t.prevHash[0:8]
stats["template"] = true
}
_ = json.NewEncoder(w).Encode(stats)
}
func convertUpstream(u *rpc.RPCClient) map[string]interface{} {
upstream := map[string]interface{}{
"name": u.Name,
"url": u.Url.String(),
"sick": u.Sick(),
"accepts": atomic.LoadInt64(&u.Accepts),
"rejects": atomic.LoadInt64(&u.Rejects),
"lastSubmissionAt": atomic.LoadInt64(&u.LastSubmissionAt),
"failsCount": atomic.LoadInt64(&u.FailsCount),
//"info": u.Info(),
}
return upstream
}
func (s *StratumServer) collectMinersStats() (float64, float64, int, []interface{}) {
now := util.MakeTimestamp()
var result []interface{}
totalhashrate := float64(0)
totalhashrate24h := float64(0)
totalOnline := 0
window24h := 24 * time.Hour
for m := range s.miners.Iter() {
stats := make(map[string]interface{})
lastBeat := m.Val.getLastBeat()
hashrate := m.Val.hashrate(s.estimationWindow)
hashrate24h := m.Val.hashrate(window24h)
totalhashrate += hashrate
totalhashrate24h += hashrate24h
stats["name"] = m.Key
stats["hashrate"] = hashrate
stats["hashrate24h"] = hashrate24h
stats["lastBeat"] = lastBeat
stats["validShares"] = atomic.LoadInt64(&m.Val.validShares)
stats["staleShares"] = atomic.LoadInt64(&m.Val.staleShares)
stats["invalidShares"] = atomic.LoadInt64(&m.Val.invalidShares)
stats["accepts"] = atomic.LoadInt64(&m.Val.accepts)
stats["rejects"] = atomic.LoadInt64(&m.Val.rejects)
if !s.config.Frontend.HideIP {
stats["ip"] = m.Val.ip
}
if now-lastBeat > (int64(s.timeout/2) / 1000000) {
stats["warning"] = true
}
if now-lastBeat > (int64(s.timeout) / 1000000) {
stats["timeout"] = true
} else {
totalOnline++
}
result = append(result, stats)
}
return totalhashrate, totalhashrate24h, totalOnline, result
}
func (s *StratumServer) getLuckStats() map[string]interface{} {
now := util.MakeTimestamp()
var variance float64
var totalVariance float64
var blocksCount int
var totalBlocksCount int
s.blocksMu.Lock()
defer s.blocksMu.Unlock()
for k, v := range s.blockStats {
if k >= now-s.luckWindow {
blocksCount++
variance += v.variance
}
if k >= now-s.luckLargeWindow {
totalBlocksCount++
totalVariance += v.variance
} else {
delete(s.blockStats, k)
}
}
if blocksCount != 0 {
variance = variance / float64(blocksCount)
}
if totalBlocksCount != 0 {
totalVariance = totalVariance / float64(totalBlocksCount)
}
result := make(map[string]interface{})
result["variance"] = variance
result["blocksCount"] = blocksCount
result["window"] = s.config.LuckWindow
result["totalVariance"] = totalVariance
result["totalBlocksCount"] = totalBlocksCount
result["largeWindow"] = s.config.LargeLuckWindow
return result
}
func (s *StratumServer) getBlocksStats() []interface{} {
now := util.MakeTimestamp()
var result []interface{}
s.blocksMu.Lock()
defer s.blocksMu.Unlock()
for k, v := range s.blockStats {
if k >= now-int64(s.luckLargeWindow) {
block := map[string]interface{}{
"height": v.height,
"hash": v.hash,
"variance": v.variance,
"timestamp": k,
}
result = append(result, block)
} else {
delete(s.blockStats, k)
}
}
return result
}