2025-12-07 15:14:30 +00:00
|
|
|
package mining
|
|
|
|
|
|
|
|
|
|
import (
|
2025-12-31 01:55:24 +00:00
|
|
|
"context"
|
2025-12-07 15:14:30 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
2025-12-31 01:55:24 +00:00
|
|
|
"time"
|
2025-12-07 15:14:30 +00:00
|
|
|
)
|
|
|
|
|
|
2025-12-31 01:55:24 +00:00
|
|
|
// statsTimeout is the timeout for stats HTTP requests (shorter than general timeout)
|
|
|
|
|
const statsTimeout = 5 * time.Second
|
2025-12-07 15:14:30 +00:00
|
|
|
|
2025-12-31 01:55:24 +00:00
|
|
|
// GetStats retrieves the performance statistics from the running XMRig miner.
|
|
|
|
|
func (m *XMRigMiner) GetStats(ctx context.Context) (*PerformanceMetrics, error) {
|
|
|
|
|
// Read state under RLock, then release before HTTP call
|
|
|
|
|
m.mu.RLock()
|
2025-12-07 15:14:30 +00:00
|
|
|
if !m.Running {
|
2025-12-31 01:55:24 +00:00
|
|
|
m.mu.RUnlock()
|
2025-12-07 15:14:30 +00:00
|
|
|
return nil, errors.New("miner is not running")
|
|
|
|
|
}
|
|
|
|
|
if m.API == nil || m.API.ListenPort == 0 {
|
2025-12-31 01:55:24 +00:00
|
|
|
m.mu.RUnlock()
|
2025-12-07 15:14:30 +00:00
|
|
|
return nil, errors.New("miner API not configured or port is zero")
|
|
|
|
|
}
|
2025-12-31 01:55:24 +00:00
|
|
|
host := m.API.ListenHost
|
|
|
|
|
port := m.API.ListenPort
|
|
|
|
|
m.mu.RUnlock()
|
2025-12-07 15:14:30 +00:00
|
|
|
|
2025-12-31 01:55:24 +00:00
|
|
|
// Create request with context and timeout
|
|
|
|
|
reqCtx, cancel := context.WithTimeout(ctx, statsTimeout)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(reqCtx, "GET", fmt.Sprintf("http://%s:%d/2/summary", host, port), nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HTTP call outside the lock to avoid blocking other operations
|
|
|
|
|
resp, err := getHTTPClient().Do(req)
|
2025-12-07 15:14:30 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-31 01:55:24 +00:00
|
|
|
// Store the full summary in the miner struct (requires lock)
|
|
|
|
|
m.mu.Lock()
|
2025-12-11 15:22:58 +00:00
|
|
|
m.FullStats = &summary
|
2025-12-31 01:55:24 +00:00
|
|
|
m.mu.Unlock()
|
2025-12-11 15:22:58 +00:00
|
|
|
|
2025-12-07 15:14:30 +00:00
|
|
|
var hashrate int
|
|
|
|
|
if len(summary.Hashrate.Total) > 0 {
|
|
|
|
|
hashrate = int(summary.Hashrate.Total[0])
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-29 23:41:26 +00:00
|
|
|
// Calculate average difficulty per accepted share
|
|
|
|
|
var avgDifficulty int
|
|
|
|
|
if summary.Results.SharesGood > 0 {
|
|
|
|
|
avgDifficulty = summary.Results.HashesTotal / summary.Results.SharesGood
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-07 15:14:30 +00:00
|
|
|
return &PerformanceMetrics{
|
2025-12-29 23:41:26 +00:00
|
|
|
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,
|
2025-12-07 15:14:30 +00:00
|
|
|
}, nil
|
|
|
|
|
}
|