aboutsummaryrefslogtreecommitdiff
path: root/hotline/stats.go
diff options
context:
space:
mode:
authorJeff Halter <868228+jhalter@users.noreply.github.com>2024-07-09 21:36:27 -0700
committerJeff Halter <868228+jhalter@users.noreply.github.com>2024-07-09 21:42:05 -0700
commitd9bc63a10d0978d9a5222cf7be74044e55f409b7 (patch)
tree1797c9593c279bf1334ed8285de8054a92a28dcb /hotline/stats.go
parent8fa166777cbcd92e871e937d9557f0f1a732c04d (diff)
Extensive refactor and clean up
Diffstat (limited to 'hotline/stats.go')
-rw-r--r--hotline/stats.go96
1 files changed, 86 insertions, 10 deletions
diff --git a/hotline/stats.go b/hotline/stats.go
index d1a3c5f..316a67d 100644
--- a/hotline/stats.go
+++ b/hotline/stats.go
@@ -5,16 +5,92 @@ import (
"time"
)
+// Stat counter keys
+const (
+ StatCurrentlyConnected = iota
+ StatDownloadsInProgress
+ StatUploadsInProgress
+ StatWaitingDownloads
+ StatConnectionPeak
+ StatConnectionCounter
+ StatDownloadCounter
+ StatUploadCounter
+)
+
+type Counter interface {
+ Increment(keys ...int)
+ Decrement(key int)
+ Set(key, val int)
+ Get(key int) int
+ Values() map[string]interface{}
+}
+
type Stats struct {
- CurrentlyConnected int
- DownloadsInProgress int
- UploadsInProgress int
- WaitingDownloads int
- ConnectionPeak int
- ConnectionCounter int
- DownloadCounter int
- UploadCounter int
- Since time.Time
+ stats map[int]int
+ since time.Time
+
+ mu sync.RWMutex
+}
+
+func NewStats() *Stats {
+ return &Stats{
+ since: time.Now(),
+ stats: map[int]int{
+ StatCurrentlyConnected: 0,
+ StatDownloadsInProgress: 0,
+ StatUploadsInProgress: 0,
+ StatWaitingDownloads: 0,
+ StatConnectionPeak: 0,
+ StatDownloadCounter: 0,
+ StatUploadCounter: 0,
+ StatConnectionCounter: 0,
+ },
+ }
+}
+
+func (s *Stats) Increment(keys ...int) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+
+ for _, key := range keys {
+ s.stats[key]++
+ }
+}
+
+func (s *Stats) Decrement(key int) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+
+ s.stats[key]--
+}
+
+func (s *Stats) Set(key, val int) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+
+ s.stats[key] = val
+}
+
+func (s *Stats) Get(key int) int {
+ s.mu.RLock()
+ defer s.mu.RUnlock()
+
+ return s.stats[key]
+}
+
+func (s *Stats) Values() map[string]interface{} {
+ s.mu.RLock()
+ defer s.mu.RUnlock()
- sync.Mutex
+ return map[string]interface{}{
+ "CurrentlyConnected": s.stats[StatCurrentlyConnected],
+ "DownloadsInProgress": s.stats[StatDownloadsInProgress],
+ "UploadsInProgress": s.stats[StatUploadsInProgress],
+ "WaitingDownloads": s.stats[StatWaitingDownloads],
+ "ConnectionPeak": s.stats[StatConnectionPeak],
+ "ConnectionCounter": s.stats[StatConnectionCounter],
+ "DownloadCounter": s.stats[StatDownloadCounter],
+ "UploadCounter": s.stats[StatUploadCounter],
+ "Since": s.since,
+ }
}