Skip to content

Commit

Permalink
🐛 fix: update database connection string format and date parsing in a…
Browse files Browse the repository at this point in the history
…nalytics

The commit includes:
- Correct PostgreSQL DSN parameter order
- Fix date comparison in analytics using RFC3339 format
- Add one day to end date for inclusive range
  • Loading branch information
watzon committed Nov 17, 2024
1 parent aa327c9 commit ade5d7d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
17 changes: 6 additions & 11 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
# Database configuration
database:
# Database driver: "sqlite" or "postgres"
driver: sqlite

# SQLite configuration
name: paste69.db # Database file path for SQLite

# # PostgreSQL configuration (ignored for SQLite)
# host: localhost
# port: 5432
# user: paste69
# password: paste69
# sslmode: disable # Options: disable, require, verify-ca, verify-full
driver: postgres
host: localhost
port: 5432
user: postgres
name: paste69
sslmode: disable # Options: disable, require, verify-ca, verify-full

# Storage configuration
storage:
Expand Down
4 changes: 2 additions & 2 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func New(config *config.Config, gormConfig *gorm.Config) (*Database, error) {
switch config.Database.Driver {
case "postgres":
dsn := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
"host=%s user=%s password=%s port=%d dbname=%s sslmode=%s",
config.Database.Host,
config.Database.Port,
config.Database.User,
config.Database.Password,
config.Database.Port,
config.Database.Name,
config.Database.SSLMode,
)
Expand Down
14 changes: 9 additions & 5 deletions internal/server/services/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (s *AnalyticsService) GetStatsHistory(days int) (*StatsHistory, error) {
}

// Calculate date range
endDate := time.Now()
endDate := time.Now().AddDate(0, 0, 1)
startDate := endDate.AddDate(0, 0, -days)

// Get paste counts by day
Expand Down Expand Up @@ -232,21 +232,24 @@ func (s *AnalyticsService) GetStatsHistory(days int) (*StatsHistory, error) {

// Update with actual values if available
for _, pc := range pasteCounts {
if pc.DateStr == dateStr {
pcTime, err := time.Parse(time.RFC3339, pc.DateStr)
if err == nil && pcTime.Format("2006-01-02") == dateStr {
history.Pastes[i].Value = pc.Count
break
}
}

for _, uc := range urlCounts {
if uc.DateStr == dateStr {
ucTime, err := time.Parse(time.RFC3339, uc.DateStr)
if err == nil && ucTime.Format("2006-01-02") == dateStr {
history.URLs[i].Value = uc.Count
break
}
}

for _, sc := range storageCounts {
if sc.DateStr == dateStr {
scTime, err := time.Parse(time.RFC3339, sc.DateStr)
if err == nil && scTime.Format("2006-01-02") == dateStr {
history.Storage[i].Value = sc.Size
if sc.Count > 0 {
history.AvgSize[i].Value = float64(sc.Size) / float64(sc.Count)
Expand All @@ -256,7 +259,8 @@ func (s *AnalyticsService) GetStatsHistory(days int) (*StatsHistory, error) {
}

for _, ac := range apiKeyCounts {
if ac.DateStr == dateStr {
acTime, err := time.Parse(time.RFC3339, ac.DateStr)
if err == nil && acTime.Format("2006-01-02") == dateStr {
history.APIKeys[i].Value = ac.Count
break
}
Expand Down

0 comments on commit ade5d7d

Please sign in to comment.