You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
4.4 KiB
166 lines
4.4 KiB
8 years ago
|
package stratum
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"net/http"
|
||
|
"sync/atomic"
|
||
|
"time"
|
||
|
|
||
7 years ago
|
"github.com/sammy007/monero-stratum/rpc"
|
||
|
"github.com/sammy007/monero-stratum/util"
|
||
8 years ago
|
)
|
||
|
|
||
|
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(),
|
||
|
}
|
||
|
|
||
8 years ago
|
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())
|
||
8 years ago
|
stats["luck"] = s.getLuckStats()
|
||
8 years ago
|
stats["blocks"] = s.getBlocksStats()
|
||
8 years ago
|
|
||
|
if t := s.currentBlockTemplate(); t != nil {
|
||
8 years ago
|
stats["height"] = t.height
|
||
|
stats["diff"] = t.difficulty
|
||
8 years ago
|
roundShares := atomic.LoadInt64(&s.roundShares)
|
||
8 years ago
|
stats["variance"] = float64(roundShares) / float64(t.difficulty)
|
||
|
stats["prevHash"] = t.prevHash[0:8]
|
||
8 years ago
|
stats["template"] = true
|
||
|
}
|
||
|
json.NewEncoder(w).Encode(stats)
|
||
|
}
|
||
|
|
||
8 years ago
|
func convertUpstream(u *rpc.RPCClient) map[string]interface{} {
|
||
|
upstream := map[string]interface{}{
|
||
|
"name": u.Name,
|
||
|
"url": u.Url.String(),
|
||
|
"sick": u.Sick(),
|
||
|
"accepts": atomic.LoadUint64(&u.Accepts),
|
||
|
"rejects": atomic.LoadUint64(&u.Rejects),
|
||
|
"lastSubmissionAt": atomic.LoadInt64(&u.LastSubmissionAt),
|
||
|
"failsCount": atomic.LoadUint64(&u.FailsCount),
|
||
|
}
|
||
|
return upstream
|
||
|
}
|
||
|
|
||
8 years ago
|
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.LoadUint64(&m.Val.validShares)
|
||
8 years ago
|
stats["staleShares"] = atomic.LoadUint64(&m.Val.staleShares)
|
||
8 years ago
|
stats["invalidShares"] = atomic.LoadUint64(&m.Val.invalidShares)
|
||
|
stats["accepts"] = atomic.LoadUint64(&m.Val.accepts)
|
||
|
stats["rejects"] = atomic.LoadUint64(&m.Val.rejects)
|
||
|
if !s.config.Frontend.HideIP {
|
||
8 years ago
|
stats["ip"] = m.Val.ip
|
||
8 years ago
|
}
|
||
|
|
||
|
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-int64(s.luckWindow) {
|
||
|
blocksCount++
|
||
8 years ago
|
variance += v.variance
|
||
8 years ago
|
}
|
||
|
if k >= now-int64(s.luckLargeWindow) {
|
||
|
totalBlocksCount++
|
||
8 years ago
|
totalVariance += v.variance
|
||
8 years ago
|
} 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
|
||
|
}
|
||
8 years ago
|
|
||
|
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
|
||
|
}
|