mirror of
https://github.com/nprasad2077/NBA_Go.git
synced 2026-09-22 14:05:13 +00:00
updated models
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"github.com/nprasad2077/NBA_Go/services"
|
"github.com/nprasad2077/NBA_Go/services"
|
||||||
|
"github.com/nprasad2077/NBA_Go/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// importPlayerAdvanced fetches and stores advanced stats for seasons 2023–2025
|
// 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 {
|
if err := services.FetchAndStorePlayerAdvancedScrapedStats(db, season, false); err != nil {
|
||||||
log.Printf("advanced import failed for %d: %v", season, err)
|
log.Printf("advanced import failed for %d: %v", season, err)
|
||||||
}
|
}
|
||||||
|
log.Printf("Advanced import for season: %d", season)
|
||||||
time.Sleep(1100 * time.Millisecond)
|
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 {
|
if err := services.FetchAndStorePlayerAdvancedScrapedStats(db, season, true); err != nil {
|
||||||
log.Printf("advanced import failed for %d: %v", season, err)
|
log.Printf("advanced import failed for %d: %v", season, err)
|
||||||
}
|
}
|
||||||
|
log.Printf("Advanced Playoffs import for season: %d", season)
|
||||||
time.Sleep(1100 * time.Millisecond)
|
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 {
|
if err := services.FetchAndStorePlayerTotalScrapedStats(db, season, false); err != nil {
|
||||||
log.Printf("scraped totals import failed for %d: %v", season, err)
|
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)
|
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 {
|
if err := services.FetchAndStorePlayerTotalScrapedStats(db, season, true); err != nil {
|
||||||
log.Printf("scraped playoffs import failed for %d: %v", season, err)
|
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)
|
time.Sleep(1500 * time.Millisecond)
|
||||||
|
utils.SleepWithJitter(1500 * time.Millisecond)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -26,7 +26,6 @@ import (
|
|||||||
fiberswagger "github.com/swaggo/fiber-swagger"
|
fiberswagger "github.com/swaggo/fiber-swagger"
|
||||||
|
|
||||||
"github.com/nprasad2077/NBA_Go/config"
|
"github.com/nprasad2077/NBA_Go/config"
|
||||||
"github.com/nprasad2077/NBA_Go/utils"
|
|
||||||
"github.com/nprasad2077/NBA_Go/controllers"
|
"github.com/nprasad2077/NBA_Go/controllers"
|
||||||
"github.com/nprasad2077/NBA_Go/routes"
|
"github.com/nprasad2077/NBA_Go/routes"
|
||||||
"github.com/nprasad2077/NBA_Go/utils/middleware"
|
"github.com/nprasad2077/NBA_Go/utils/middleware"
|
||||||
@@ -40,7 +39,6 @@ func main() {
|
|||||||
|
|
||||||
importPlayerAdvanced(db)
|
importPlayerAdvanced(db)
|
||||||
log.Println("🎉 Player Advanced Import completed successfully")
|
log.Println("🎉 Player Advanced Import completed successfully")
|
||||||
utils.SleepWithJitter(1100 * time.Millisecond)
|
|
||||||
|
|
||||||
importPlayerAdvancedPlayoffs(db)
|
importPlayerAdvancedPlayoffs(db)
|
||||||
log.Println("🎉 Player Advanced Playoffs Import completed successfully")
|
log.Println("🎉 Player Advanced Playoffs Import completed successfully")
|
||||||
@@ -52,6 +50,7 @@ func main() {
|
|||||||
|
|
||||||
importPlayerTotalsScrape(db)
|
importPlayerTotalsScrape(db)
|
||||||
log.Println("🎉 Player Totals (scraped) Import completed successfully")
|
log.Println("🎉 Player Totals (scraped) Import completed successfully")
|
||||||
|
|
||||||
importPlayerTotalsPlayoffsScrape(db)
|
importPlayerTotalsPlayoffsScrape(db)
|
||||||
log.Println("🎉 Player Playoffs (scraped) Import completed successfully")
|
log.Println("🎉 Player Playoffs (scraped) Import completed successfully")
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import "gorm.io/gorm"
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
type PlayerAdvancedStat struct {
|
type PlayerAdvancedStat struct {
|
||||||
gorm.Model `swaggerignore:"true"`
|
ID uint `gorm:"primaryKey" swaggerignore:"true"`
|
||||||
ExternalID int `json:"id"`
|
ExternalID int `json:"id"`
|
||||||
PlayerID string `gorm:"not null;index:idx_player_season_team,unique" json:"playerId"`
|
PlayerID string `gorm:"not null;index:idx_player_season_team,unique" json:"playerId"`
|
||||||
PlayerName string `json:"playerName"`
|
PlayerName string `json:"playerName"`
|
||||||
@@ -34,4 +38,8 @@ type PlayerAdvancedStat struct {
|
|||||||
Team string `gorm:"not null;index:idx_player_season_team,unique" json:"team"`
|
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"`
|
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"`
|
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"`
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,14 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import "gorm.io/gorm"
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
type PlayerTotalStat struct {
|
type PlayerTotalStat struct {
|
||||||
gorm.Model
|
ID uint `gorm:"primaryKey" swaggerignore:"true"`
|
||||||
|
|
||||||
ExternalID int `json:"id"`
|
ExternalID int `json:"id"`
|
||||||
PlayerID string `gorm:"not null;uniqueIndex:idx_total_player_season_team" json:"playerId"`
|
PlayerID string `gorm:"not null;uniqueIndex:idx_total_player_season_team" json:"playerId"`
|
||||||
PlayerName string `json:"playerName"`
|
PlayerName string `json:"playerName"`
|
||||||
@@ -37,4 +42,8 @@ type PlayerTotalStat struct {
|
|||||||
Team string `gorm:"not null;uniqueIndex:idx_total_player_season_team" json:"team"`
|
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"`
|
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"`
|
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"`
|
||||||
}
|
}
|
||||||
+5
-2
@@ -2,6 +2,7 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -12,12 +13,14 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SleepWithJitter sleeps for the given base duration plus or minus 25% random jitter.
|
// 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) {
|
func SleepWithJitter(base time.Duration) {
|
||||||
half := base / 2
|
half := base / 2
|
||||||
// Random value in [0, half)
|
// Random value in [0, half)
|
||||||
randOffset := time.Duration(rand.Int63n(int64(half)))
|
randOffset := time.Duration(rand.Int63n(int64(half)))
|
||||||
// Shift to center jitter around zero: [-half/2, +half/2)
|
// Shift to center jitter around zero: [-half/2, +half/2)
|
||||||
delta := randOffset - half/2
|
delta := randOffset - half/2
|
||||||
time.Sleep(base + delta)
|
actual := base + delta
|
||||||
|
log.Printf("⏱️ Sleeping for %v (base=%v, jitter=%v)", actual, base, delta)
|
||||||
|
time.Sleep(actual)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user