Mining/pkg/mining/xmrig_stats.go
snider b9f9143336 feat: Add avg difficulty per share statistic to dashboard
- Add AvgDifficulty and DiffCurrent fields to PerformanceMetrics
- Calculate avg difficulty as HashesTotal/SharesGood in XMRig stats
- Add difficulty data to TT-Miner stats using pool difficulty
- Display "Avg Diff" stat in stats panel with k/M/G/T formatting
- Add WriteStdin to MockMiner for test interface compliance

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 23:41:26 +00:00

60 lines
1.5 KiB
Go

package mining
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
// GetStats retrieves the performance statistics from the running XMRig miner.
func (m *XMRigMiner) GetStats() (*PerformanceMetrics, error) {
m.mu.RLock()
defer m.mu.RUnlock()
if !m.Running {
return nil, errors.New("miner is not running")
}
if m.API == nil || m.API.ListenPort == 0 {
return nil, errors.New("miner API not configured or port is zero")
}
resp, err := httpClient.Get(fmt.Sprintf("http://%s:%d/2/summary", m.API.ListenHost, m.API.ListenPort))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to get stats: unexpected status code %d", resp.StatusCode)
}
var summary XMRigSummary
if err := json.NewDecoder(resp.Body).Decode(&summary); err != nil {
return nil, err
}
// Store the full summary in the miner struct
m.FullStats = &summary
var hashrate int
if len(summary.Hashrate.Total) > 0 {
hashrate = int(summary.Hashrate.Total[0])
}
// Calculate average difficulty per accepted share
var avgDifficulty int
if summary.Results.SharesGood > 0 {
avgDifficulty = summary.Results.HashesTotal / summary.Results.SharesGood
}
return &PerformanceMetrics{
Hashrate: hashrate,
Shares: summary.Results.SharesGood,
Rejected: summary.Results.SharesTotal - summary.Results.SharesGood,
Uptime: summary.Uptime,
Algorithm: summary.Algo,
AvgDifficulty: avgDifficulty,
DiffCurrent: summary.Results.DiffCurrent,
}, nil
}