first commit
This commit is contained in:
57
internal/stats/stats.go
Normal file
57
internal/stats/stats.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package stats
|
||||
|
||||
import "sync"
|
||||
|
||||
type SpotStats struct {
|
||||
mu sync.RWMutex
|
||||
Received int64
|
||||
Processed int64
|
||||
Rejected int64
|
||||
}
|
||||
|
||||
var Global = &SpotStats{}
|
||||
|
||||
func (s *SpotStats) IncrReceived() {
|
||||
s.mu.Lock()
|
||||
s.Received++
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *SpotStats) IncrProcessed() {
|
||||
s.mu.Lock()
|
||||
s.Processed++
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *SpotStats) IncrRejected() {
|
||||
s.mu.Lock()
|
||||
s.Rejected++
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *SpotStats) Get() (received, processed, rejected int64) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.Received, s.Processed, s.Rejected
|
||||
}
|
||||
|
||||
func (s *SpotStats) SuccessRate() float64 {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
if s.Received == 0 {
|
||||
return 0.0
|
||||
}
|
||||
return float64(s.Processed) / float64(s.Received) * 100.0
|
||||
}
|
||||
|
||||
// Snapshot retourne un map JSON-friendly pour le frontend
|
||||
func (s *SpotStats) Snapshot() map[string]interface{} {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return map[string]interface{}{
|
||||
"received": s.Received,
|
||||
"processed": s.Processed,
|
||||
"rejected": s.Rejected,
|
||||
"successRate": s.SuccessRate(),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user