Skip to content

Commit 28e55e9

Browse files
committed
refactor: move analytics to run every minute
1 parent ae59b50 commit 28e55e9

3 files changed

Lines changed: 73 additions & 21 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@ __debug_bin*
3131

3232
# I promise i'll fix the name later
3333
MastodonTwitterAPI*
34-
TwitterAPIBridge*
34+
TwitterAPIBridge*
35+
36+
# golang profiling
37+
profile*.pdf

db_controller/analytics.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package db_controller
2+
3+
import (
4+
"log"
5+
"time"
6+
)
7+
8+
var (
9+
inRamAnalytics []AnalyticData
10+
analyticsTicker *time.Ticker
11+
)
12+
13+
// Stores analytic data (if enabled)
14+
// -- TYPES --
15+
// 1. "login"
16+
// 2. "tweets viewed"
17+
// 3. "tweets posted"
18+
func StoreAnalyticData(data AnalyticData) {
19+
if !cfg.TrackAnalytics {
20+
return
21+
}
22+
23+
inRamAnalytics = append(inRamAnalytics, data)
24+
}
25+
26+
func WriteAnalyticsToDB() {
27+
if !cfg.TrackAnalytics {
28+
return
29+
}
30+
31+
if len(inRamAnalytics) == 0 {
32+
return
33+
}
34+
35+
tx := db.Begin()
36+
37+
if err := tx.CreateInBatches(inRamAnalytics, 100).Error; err != nil {
38+
tx.Rollback()
39+
log.Printf("Error writing analytics data: %v", err)
40+
return
41+
}
42+
43+
if err := tx.Commit().Error; err != nil {
44+
log.Printf("Error committing analytics transaction: %v", err)
45+
return
46+
}
47+
48+
log.Printf("Successfully wrote %d analytics records to database", len(inRamAnalytics))
49+
inRamAnalytics = inRamAnalytics[:0]
50+
}
51+
52+
func StartPeriodicAnalyticsWriter(interval time.Duration) {
53+
if !cfg.TrackAnalytics {
54+
return
55+
}
56+
57+
analyticsTicker = time.NewTicker(interval)
58+
59+
go func() {
60+
for range analyticsTicker.C {
61+
WriteAnalyticsToDB()
62+
}
63+
}()
64+
}

db_controller/db_controller.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,8 @@ type ShortLink struct {
9595
}
9696

9797
var (
98-
db *gorm.DB
99-
cfg config.Config
100-
base62Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
98+
db *gorm.DB
99+
cfg config.Config
101100
)
102101

103102
func InitDB(_cfg config.Config) {
@@ -132,7 +131,9 @@ func InitDB(_cfg config.Config) {
132131
db.AutoMigrate(&MessageContext{})
133132
db.AutoMigrate(&TwitterIDs{})
134133
db.AutoMigrate(&AnalyticData{})
135-
db.AutoMigrate(&ShortLink{}) // Add this line
134+
db.AutoMigrate(&ShortLink{})
135+
136+
StartPeriodicAnalyticsWriter(time.Minute)
136137
}
137138

138139
// StoreToken stores an encrypted access token and refresh token in the database.
@@ -409,22 +410,6 @@ func GetTwitterIDFromDatabase(twitterID *int64) (*string, *time.Time, *string, e
409410
return &blueskyID.BlueskyID, blueskyID.DateCreated, blueskyID.ReposterDid, nil
410411
}
411412

412-
// Stores analytic data (if enabled)
413-
// -- TYPES --
414-
// 1. "login"
415-
// 2. "tweets viewed"
416-
// 3. "tweets posted"
417-
func StoreAnalyticData(data AnalyticData) {
418-
if !cfg.TrackAnalytics {
419-
return
420-
}
421-
422-
result := db.Create(&data)
423-
if result.Error != nil {
424-
fmt.Println("Failed to store analytic data:", result.Error)
425-
}
426-
}
427-
428413
// StoreShortLink stores a short link in the database with optimized collision handling
429414
func StoreShortLink(shortCode string, originalURL string) error {
430415
shortLink := ShortLink{

0 commit comments

Comments
 (0)