From ade5d7d8454fa6d65fc7c17f72f12bf6ee29a9d0 Mon Sep 17 00:00:00 2001 From: Chris Watson Date: Sat, 16 Nov 2024 22:03:02 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20update=20database=20conne?= =?UTF-8?q?ction=20string=20format=20and=20date=20parsing=20in=20analytics?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- config/config.yaml | 17 ++++++----------- internal/database/database.go | 4 ++-- internal/server/services/analytics.go | 14 +++++++++----- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index bd76c32..11ba045 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -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: diff --git a/internal/database/database.go b/internal/database/database.go index 1aac5f4..c7ece16 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -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, ) diff --git a/internal/server/services/analytics.go b/internal/server/services/analytics.go index 1702401..01bcecd 100644 --- a/internal/server/services/analytics.go +++ b/internal/server/services/analytics.go @@ -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 @@ -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) @@ -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 }