Files
syncwarden/internal/monitor/speed.go
Axel Meyer 34a1a94502
Some checks failed
Release / build (push) Failing after 19s
Implement SyncWarden v0.1.0
Full Syncthing tray wrapper with:
- System tray with 5 icon states (idle/syncing/paused/error/disconnected)
- Syncthing REST API client with auto-discovered API key
- Long-polling event listener for real-time status
- Transfer rate monitoring, folder tracking, recent files, conflict counting
- Full context menu with folders, recent files, settings toggles
- Embedded admin panel binary (webview, requires CGO)
- OS notifications via beeep (per-event configurable)
- Syncthing process management with auto-restart
- Cross-platform installer with autostart
- CI pipeline for Linux (.deb + .tar.gz) and Windows (.zip)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 21:16:28 +01:00

53 lines
1.0 KiB
Go

package monitor
import (
"sync"
"time"
)
// SpeedTracker calculates transfer rates by diffing byte counters.
type SpeedTracker struct {
mu sync.Mutex
lastIn int64
lastOut int64
lastTime time.Time
downRate float64
upRate float64
}
// NewSpeedTracker creates a new speed tracker.
func NewSpeedTracker() *SpeedTracker {
return &SpeedTracker{}
}
// Update records new byte counters and calculates rates.
func (s *SpeedTracker) Update(inBytes, outBytes int64) {
s.mu.Lock()
defer s.mu.Unlock()
now := time.Now()
if !s.lastTime.IsZero() {
dt := now.Sub(s.lastTime).Seconds()
if dt > 0 {
s.downRate = float64(inBytes-s.lastIn) / dt
s.upRate = float64(outBytes-s.lastOut) / dt
if s.downRate < 0 {
s.downRate = 0
}
if s.upRate < 0 {
s.upRate = 0
}
}
}
s.lastIn = inBytes
s.lastOut = outBytes
s.lastTime = now
}
// Rates returns the current download and upload rates in bytes/sec.
func (s *SpeedTracker) Rates() (down, up float64) {
s.mu.Lock()
defer s.mu.Unlock()
return s.downRate, s.upRate
}