diff --git a/import.go b/import.go index 39235af..e9e087b 100644 --- a/import.go +++ b/import.go @@ -6,6 +6,7 @@ import ( "gorm.io/gorm" "github.com/nprasad2077/NBA_Go/services" + "github.com/nprasad2077/NBA_Go/utils" ) // importPlayerAdvanced fetches and stores advanced stats for seasons 2023–2025 @@ -14,7 +15,9 @@ func importPlayerAdvanced(db *gorm.DB) { if err := services.FetchAndStorePlayerAdvancedScrapedStats(db, season, false); err != nil { log.Printf("advanced import failed for %d: %v", season, err) } + log.Printf("Advanced import for season: %d", season) time.Sleep(1100 * time.Millisecond) + utils.SleepWithJitter(1500 * time.Millisecond) } } @@ -25,7 +28,9 @@ func importPlayerAdvancedPlayoffs(db *gorm.DB) { if err := services.FetchAndStorePlayerAdvancedScrapedStats(db, season, true); err != nil { log.Printf("advanced import failed for %d: %v", season, err) } + log.Printf("Advanced Playoffs import for season: %d", season) time.Sleep(1100 * time.Millisecond) + utils.SleepWithJitter(1500 * time.Millisecond) } } @@ -65,7 +70,9 @@ func importPlayerTotalsScrape(db *gorm.DB) { if err := services.FetchAndStorePlayerTotalScrapedStats(db, season, false); err != nil { log.Printf("scraped totals import failed for %d: %v", season, err) } + log.Printf("Player Totals import for season: %d", season) time.Sleep(1100 * time.Millisecond) + utils.SleepWithJitter(1500 * time.Millisecond) } } @@ -75,6 +82,8 @@ func importPlayerTotalsPlayoffsScrape(db *gorm.DB) { if err := services.FetchAndStorePlayerTotalScrapedStats(db, season, true); err != nil { log.Printf("scraped playoffs import failed for %d: %v", season, err) } + log.Printf("Player Playoffs Totals import for season: %d", season) time.Sleep(1500 * time.Millisecond) + utils.SleepWithJitter(1500 * time.Millisecond) } } \ No newline at end of file diff --git a/main.go b/main.go index 336e96f..d31e325 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,6 @@ import ( fiberswagger "github.com/swaggo/fiber-swagger" "github.com/nprasad2077/NBA_Go/config" - "github.com/nprasad2077/NBA_Go/utils" "github.com/nprasad2077/NBA_Go/controllers" "github.com/nprasad2077/NBA_Go/routes" "github.com/nprasad2077/NBA_Go/utils/middleware" @@ -40,7 +39,6 @@ func main() { importPlayerAdvanced(db) log.Println("🎉 Player Advanced Import completed successfully") - utils.SleepWithJitter(1100 * time.Millisecond) importPlayerAdvancedPlayoffs(db) log.Println("🎉 Player Advanced Playoffs Import completed successfully") @@ -52,6 +50,7 @@ func main() { importPlayerTotalsScrape(db) log.Println("🎉 Player Totals (scraped) Import completed successfully") + importPlayerTotalsPlayoffsScrape(db) log.Println("🎉 Player Playoffs (scraped) Import completed successfully") diff --git a/models/player_advanced_stat.go b/models/player_advanced_stat.go index a4478f5..bb79a7e 100644 --- a/models/player_advanced_stat.go +++ b/models/player_advanced_stat.go @@ -1,9 +1,13 @@ package models -import "gorm.io/gorm" +import ( + "time" + + "gorm.io/gorm" +) type PlayerAdvancedStat struct { - gorm.Model `swaggerignore:"true"` + ID uint `gorm:"primaryKey" swaggerignore:"true"` ExternalID int `json:"id"` PlayerID string `gorm:"not null;index:idx_player_season_team,unique" json:"playerId"` PlayerName string `json:"playerName"` @@ -34,4 +38,8 @@ type PlayerAdvancedStat struct { Team string `gorm:"not null;index:idx_player_season_team,unique" json:"team"` Season int `gorm:"not null;index:idx_player_season_team,unique" json:"season"` IsPlayoff bool `gorm:"not null;default:false;index:idx_player_season_team,unique" json:"isPlayoff"` + + CreatedAt time.Time `swaggerignore:"true"` + UpdatedAt time.Time `swaggerignore:"true"` + DeletedAt gorm.DeletedAt `gorm:"index" swaggerignore:"true"` } \ No newline at end of file diff --git a/models/player_total_stat.go b/models/player_total_stat.go index 29f15aa..f1158e6 100644 --- a/models/player_total_stat.go +++ b/models/player_total_stat.go @@ -1,9 +1,14 @@ package models -import "gorm.io/gorm" +import ( + "time" + + "gorm.io/gorm" +) type PlayerTotalStat struct { - gorm.Model + ID uint `gorm:"primaryKey" swaggerignore:"true"` + ExternalID int `json:"id"` PlayerID string `gorm:"not null;uniqueIndex:idx_total_player_season_team" json:"playerId"` PlayerName string `json:"playerName"` @@ -37,4 +42,8 @@ type PlayerTotalStat struct { Team string `gorm:"not null;uniqueIndex:idx_total_player_season_team" json:"team"` Season int `gorm:"not null;uniqueIndex:idx_total_player_season_team" json:"season"` IsPlayoff bool `gorm:"not null;default:false;uniqueIndex:idx_total_player_season_team" json:"isPlayoff"` + + CreatedAt time.Time `swaggerignore:"true"` + UpdatedAt time.Time `swaggerignore:"true"` + DeletedAt gorm.DeletedAt `gorm:"index" swaggerignore:"true"` } \ No newline at end of file diff --git a/utils/sleep.go b/utils/sleep.go index 1eced58..277bd15 100644 --- a/utils/sleep.go +++ b/utils/sleep.go @@ -2,6 +2,7 @@ package utils import ( + "log" "math/rand" "time" ) @@ -12,12 +13,14 @@ func init() { } // SleepWithJitter sleeps for the given base duration plus or minus 25% random jitter. -// This helps to avoid perfectly uniform request intervals. +// It also logs the actual sleep duration so you can verify it fired correctly. func SleepWithJitter(base time.Duration) { half := base / 2 // Random value in [0, half) randOffset := time.Duration(rand.Int63n(int64(half))) // Shift to center jitter around zero: [-half/2, +half/2) delta := randOffset - half/2 - time.Sleep(base + delta) -} \ No newline at end of file + actual := base + delta + log.Printf("⏱️ Sleeping for %v (base=%v, jitter=%v)", actual, base, delta) + time.Sleep(actual) +}