From 75fb3a583cf4e8a7ca34bedb3322d309bb170ed4 Mon Sep 17 00:00:00 2001 From: bndw Date: Sat, 14 Feb 2026 12:53:50 -0800 Subject: feat: add storage stats and average latency metrics Track and display storage and performance metrics that were previously showing zeros. Storage metrics: - Add GetStats() method to storage returning event count and DB size - Store database file path for size calculation - Update metrics every 30 seconds via goroutine in main.go - Display event count and DB size (MB) in dashboard Performance metrics: - Calculate average latency from histogram sum/count metrics - Display as milliseconds in dashboard - Formula: (duration_sum / duration_count) * 1000 Missing metrics (deferred): - Connections: requires connection lifecycle tracking (gRPC + WebSocket) - Deletions: count would need API change to ProcessDeletion Dashboard now shows accurate storage stats and request latency! --- internal/storage/storage.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'internal/storage') diff --git a/internal/storage/storage.go b/internal/storage/storage.go index 9ef9956..ca9b264 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "fmt" + "os" _ "modernc.org/sqlite" // Pure Go SQLite driver ) @@ -11,7 +12,8 @@ import ( // Storage provides event persistence using SQLite. // Consumers should define their own interface based on their needs. type Storage struct { - db *sql.DB + db *sql.DB + dbPath string } // New creates a new Storage instance and initializes the database schema. @@ -48,7 +50,10 @@ func New(dbPath string) (*Storage, error) { } } - s := &Storage{db: db} + s := &Storage{ + db: db, + dbPath: dbPath, + } // Initialize schema if err := s.initSchema(context.Background()); err != nil { @@ -142,3 +147,30 @@ func (s *Storage) initSchema(ctx context.Context) error { func (s *Storage) DB() *sql.DB { return s.db } + +type Stats struct { + EventCount int64 + DBSizeBytes int64 +} + +func (s *Storage) GetStats(ctx context.Context) (*Stats, error) { + var eventCount int64 + err := s.db.QueryRowContext(ctx, "SELECT COUNT(*) FROM events WHERE deleted = 0").Scan(&eventCount) + if err != nil { + return nil, fmt.Errorf("failed to count events: %w", err) + } + + var dbSize int64 + if s.dbPath != ":memory:" { + info, err := os.Stat(s.dbPath) + if err != nil { + return nil, fmt.Errorf("failed to stat database file: %w", err) + } + dbSize = info.Size() + } + + return &Stats{ + EventCount: eventCount, + DBSizeBytes: dbSize, + }, nil +} -- cgit v1.2.3