]> git.r.bdr.sh - rbdr/mobius/commitdiff
Add basic stat counters
authorJeff Halter <redacted>
Mon, 4 Jul 2022 21:19:01 +0000 (14:19 -0700)
committerJeff Halter <redacted>
Mon, 4 Jul 2022 21:19:01 +0000 (14:19 -0700)
cmd/mobius-hotline-server/main.go
hotline/server.go
hotline/stats.go

index 63dc824a22591eb3a8e7858aff3a9cddf6b86403..8407449ab826a3dfc0e0fe051b3c37249b81d2b6 100644 (file)
@@ -118,7 +118,7 @@ type statHandler struct {
 }
 
 func (sh *statHandler) RenderStats(w http.ResponseWriter, _ *http.Request) {
 }
 
 func (sh *statHandler) RenderStats(w http.ResponseWriter, _ *http.Request) {
-       u, err := json.Marshal(sh.hlServer.Stats)
+       u, err := json.Marshal(sh.hlServer.CurrentStats())
        if err != nil {
                panic(err)
        }
        if err != nil {
                panic(err)
        }
index 06a0695771d5e8fdfac7b717f366b240a82f54d7..9cf93d0edead8eb6ba75eee603ac4c34b053d349 100644 (file)
@@ -57,7 +57,9 @@ type Server struct {
 
        NextGuestID   *uint16
        TrackerPassID [4]byte
 
        NextGuestID   *uint16
        TrackerPassID [4]byte
-       Stats         *Stats
+
+       StatsMu sync.Mutex
+       Stats   *Stats
 
        FS FileStore // Storage backend to use for File storage
 
 
        FS FileStore // Storage backend to use for File storage
 
@@ -74,6 +76,16 @@ type Server struct {
        banList   map[string]*time.Time
 }
 
        banList   map[string]*time.Time
 }
 
+func (s *Server) CurrentStats() Stats {
+       s.StatsMu.Lock()
+       defer s.StatsMu.Unlock()
+
+       stats := s.Stats
+       stats.CurrentlyConnected = len(s.Clients)
+
+       return *stats
+}
+
 type PrivateChat struct {
        Subject    string
        ClientConn map[uint16]*ClientConn
 type PrivateChat struct {
        Subject    string
        ClientConn map[uint16]*ClientConn
@@ -219,7 +231,7 @@ func NewServer(configDir string, netPort int, logger *zap.SugaredLogger, FS File
                Logger:        logger,
                NextGuestID:   new(uint16),
                outbox:        make(chan Transaction),
                Logger:        logger,
                NextGuestID:   new(uint16),
                outbox:        make(chan Transaction),
-               Stats:         &Stats{StartTime: time.Now()},
+               Stats:         &Stats{Since: time.Now()},
                ThreadedNews:  &ThreadedNews{},
                FS:            FS,
                banList:       make(map[string]*time.Time),
                ThreadedNews:  &ThreadedNews{},
                FS:            FS,
                banList:       make(map[string]*time.Time),
@@ -679,7 +691,10 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser
                }
        }
 
                }
        }
 
-       c.Server.Stats.LoginCount += 1
+       c.Server.Stats.ConnectionCounter += 1
+       if len(s.Clients) > c.Server.Stats.ConnectionPeak {
+               c.Server.Stats.ConnectionPeak = len(s.Clients)
+       }
 
        // Scan for new transactions and handle them as they come in.
        for scanner.Scan() {
 
        // Scan for new transactions and handle them as they come in.
        for scanner.Scan() {
@@ -772,6 +787,8 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro
                }
        case FileDownload:
                s.Stats.DownloadCounter += 1
                }
        case FileDownload:
                s.Stats.DownloadCounter += 1
+               s.Stats.DownloadsInProgress += 1
+               defer func() { s.Stats.DownloadsInProgress -= 1 }()
 
                var dataOffset int64
                if fileTransfer.fileResumeData != nil {
 
                var dataOffset int64
                if fileTransfer.fileResumeData != nil {
@@ -826,6 +843,8 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro
 
        case FileUpload:
                s.Stats.UploadCounter += 1
 
        case FileUpload:
                s.Stats.UploadCounter += 1
+               s.Stats.UploadsInProgress += 1
+               defer func() { s.Stats.UploadsInProgress -= 1 }()
 
                var file *os.File
 
 
                var file *os.File
 
@@ -882,7 +901,12 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro
                }
 
                rLogger.Infow("File upload complete", "dstFile", fullPath)
                }
 
                rLogger.Infow("File upload complete", "dstFile", fullPath)
+
        case FolderDownload:
        case FolderDownload:
+               s.Stats.DownloadCounter += 1
+               s.Stats.DownloadsInProgress += 1
+               defer func() { s.Stats.DownloadsInProgress -= 1 }()
+
                // Folder Download flow:
                // 1. Get filePath from the transfer
                // 2. Iterate over files
                // Folder Download flow:
                // 1. Get filePath from the transfer
                // 2. Iterate over files
@@ -1046,6 +1070,9 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro
                }
 
        case FolderUpload:
                }
 
        case FolderUpload:
+               s.Stats.UploadCounter += 1
+               s.Stats.UploadsInProgress += 1
+               defer func() { s.Stats.UploadsInProgress -= 1 }()
                rLogger.Infow(
                        "Folder upload started",
                        "dstPath", fullPath,
                rLogger.Infow(
                        "Folder upload started",
                        "dstPath", fullPath,
index 4fa683ad0161c51447c9076d393cf6c06c97c040..bc06a6915776b6ed36bc86917fe256066046a71e 100644 (file)
@@ -8,10 +8,10 @@ type Stats struct {
        CurrentlyConnected  int
        DownloadsInProgress int
        UploadsInProgress   int
        CurrentlyConnected  int
        DownloadsInProgress int
        UploadsInProgress   int
+       WaitingDownloads    int
        ConnectionPeak      int
        ConnectionPeak      int
+       ConnectionCounter   int
        DownloadCounter     int
        UploadCounter       int
        DownloadCounter     int
        UploadCounter       int
-
-       LoginCount int       `yaml:"login count"`
-       StartTime  time.Time `yaml:"start time"`
+       Since               time.Time
 }
 }