diff options
| author | bndw <ben@bdw.to> | 2026-02-14 12:53:50 -0800 |
|---|---|---|
| committer | bndw <ben@bdw.to> | 2026-02-14 12:53:50 -0800 |
| commit | 75fb3a583cf4e8a7ca34bedb3322d309bb170ed4 (patch) | |
| tree | 175a9cd032779ee4f448a2fa931eeaef0c0f283a /internal/storage | |
| parent | 40df56985402a31695a9a3bb13319bd2a3276305 (diff) | |
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!
Diffstat (limited to 'internal/storage')
| -rw-r--r-- | internal/storage/storage.go | 36 |
1 files changed, 34 insertions, 2 deletions
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 ( | |||
| 4 | "context" | 4 | "context" |
| 5 | "database/sql" | 5 | "database/sql" |
| 6 | "fmt" | 6 | "fmt" |
| 7 | "os" | ||
| 7 | 8 | ||
| 8 | _ "modernc.org/sqlite" // Pure Go SQLite driver | 9 | _ "modernc.org/sqlite" // Pure Go SQLite driver |
| 9 | ) | 10 | ) |
| @@ -11,7 +12,8 @@ import ( | |||
| 11 | // Storage provides event persistence using SQLite. | 12 | // Storage provides event persistence using SQLite. |
| 12 | // Consumers should define their own interface based on their needs. | 13 | // Consumers should define their own interface based on their needs. |
| 13 | type Storage struct { | 14 | type Storage struct { |
| 14 | db *sql.DB | 15 | db *sql.DB |
| 16 | dbPath string | ||
| 15 | } | 17 | } |
| 16 | 18 | ||
| 17 | // New creates a new Storage instance and initializes the database schema. | 19 | // New creates a new Storage instance and initializes the database schema. |
| @@ -48,7 +50,10 @@ func New(dbPath string) (*Storage, error) { | |||
| 48 | } | 50 | } |
| 49 | } | 51 | } |
| 50 | 52 | ||
| 51 | s := &Storage{db: db} | 53 | s := &Storage{ |
| 54 | db: db, | ||
| 55 | dbPath: dbPath, | ||
| 56 | } | ||
| 52 | 57 | ||
| 53 | // Initialize schema | 58 | // Initialize schema |
| 54 | if err := s.initSchema(context.Background()); err != nil { | 59 | if err := s.initSchema(context.Background()); err != nil { |
| @@ -142,3 +147,30 @@ func (s *Storage) initSchema(ctx context.Context) error { | |||
| 142 | func (s *Storage) DB() *sql.DB { | 147 | func (s *Storage) DB() *sql.DB { |
| 143 | return s.db | 148 | return s.db |
| 144 | } | 149 | } |
| 150 | |||
| 151 | type Stats struct { | ||
| 152 | EventCount int64 | ||
| 153 | DBSizeBytes int64 | ||
| 154 | } | ||
| 155 | |||
| 156 | func (s *Storage) GetStats(ctx context.Context) (*Stats, error) { | ||
| 157 | var eventCount int64 | ||
| 158 | err := s.db.QueryRowContext(ctx, "SELECT COUNT(*) FROM events WHERE deleted = 0").Scan(&eventCount) | ||
| 159 | if err != nil { | ||
| 160 | return nil, fmt.Errorf("failed to count events: %w", err) | ||
| 161 | } | ||
| 162 | |||
| 163 | var dbSize int64 | ||
| 164 | if s.dbPath != ":memory:" { | ||
| 165 | info, err := os.Stat(s.dbPath) | ||
| 166 | if err != nil { | ||
| 167 | return nil, fmt.Errorf("failed to stat database file: %w", err) | ||
| 168 | } | ||
| 169 | dbSize = info.Size() | ||
| 170 | } | ||
| 171 | |||
| 172 | return &Stats{ | ||
| 173 | EventCount: eventCount, | ||
| 174 | DBSizeBytes: dbSize, | ||
| 175 | }, nil | ||
| 176 | } | ||
