From: Jeff Halter Date: Mon, 4 Jul 2022 21:19:01 +0000 (-0700) Subject: Add basic stat counters X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/commitdiff_plain/00913df3521d967ac31ee22c23447b5aa711f2e6 Add basic stat counters --- diff --git a/cmd/mobius-hotline-server/main.go b/cmd/mobius-hotline-server/main.go index 63dc824..8407449 100644 --- a/cmd/mobius-hotline-server/main.go +++ b/cmd/mobius-hotline-server/main.go @@ -118,7 +118,7 @@ type statHandler struct { } 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) } diff --git a/hotline/server.go b/hotline/server.go index 06a0695..9cf93d0 100644 --- a/hotline/server.go +++ b/hotline/server.go @@ -57,7 +57,9 @@ type Server struct { NextGuestID *uint16 TrackerPassID [4]byte - Stats *Stats + + StatsMu sync.Mutex + Stats *Stats FS FileStore // Storage backend to use for File storage @@ -74,6 +76,16 @@ type Server struct { 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 @@ -219,7 +231,7 @@ func NewServer(configDir string, netPort int, logger *zap.SugaredLogger, FS File 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), @@ -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() { @@ -772,6 +787,8 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro } case FileDownload: s.Stats.DownloadCounter += 1 + s.Stats.DownloadsInProgress += 1 + defer func() { s.Stats.DownloadsInProgress -= 1 }() 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 + s.Stats.UploadsInProgress += 1 + defer func() { s.Stats.UploadsInProgress -= 1 }() 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) + 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 @@ -1046,6 +1070,9 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro } case FolderUpload: + s.Stats.UploadCounter += 1 + s.Stats.UploadsInProgress += 1 + defer func() { s.Stats.UploadsInProgress -= 1 }() rLogger.Infow( "Folder upload started", "dstPath", fullPath, diff --git a/hotline/stats.go b/hotline/stats.go index 4fa683a..bc06a69 100644 --- a/hotline/stats.go +++ b/hotline/stats.go @@ -8,10 +8,10 @@ type Stats struct { CurrentlyConnected int DownloadsInProgress int UploadsInProgress int + WaitingDownloads int ConnectionPeak int + ConnectionCounter int DownloadCounter int UploadCounter int - - LoginCount int `yaml:"login count"` - StartTime time.Time `yaml:"start time"` + Since time.Time }