summaryrefslogtreecommitdiffstats
path: root/internal/storage
diff options
context:
space:
mode:
Diffstat (limited to 'internal/storage')
-rw-r--r--internal/storage/storage.go36
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.
13type Storage struct { 14type 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 {
142func (s *Storage) DB() *sql.DB { 147func (s *Storage) DB() *sql.DB {
143 return s.db 148 return s.db
144} 149}
150
151type Stats struct {
152 EventCount int64
153 DBSizeBytes int64
154}
155
156func (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}