mirror of
https://github.com/nprasad2077/NBA_Go.git
synced 2026-09-22 05:55:13 +00:00
clean up
This commit is contained in:
@@ -172,62 +172,4 @@ func FetchAndStorePlayerAdvancedScrapedStats(db *gorm.DB, season int, isPlayoff
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// // 3. map into your model
|
||||
// stat := models.PlayerAdvancedStat{
|
||||
// ExternalID: mustAtoi(data["rk"]),
|
||||
// PlayerID: playerID,
|
||||
// PlayerName: data["player"],
|
||||
// Position: data["pos"],
|
||||
// Age: mustAtoi(data["age"]),
|
||||
// Games: mustAtoi(data["g"]),
|
||||
// MinutesPlayed: mustAtoi(data["mp"]),
|
||||
// PER: mustParseFloat(data["per"]),
|
||||
// TSPercent: mustParseFloat(data["ts_pct"]),
|
||||
// ThreePAR: mustParseFloat(data["three_par"]),
|
||||
// FTR: mustParseFloat(data["ftr"]),
|
||||
// OffensiveRBPercent: mustParseFloat(data["orb_pct"]),
|
||||
// DefensiveRBPercent: mustParseFloat(data["drb_pct"]),
|
||||
// TotalRBPercent: mustParseFloat(data["trb_pct"]),
|
||||
// AssistPercent: mustParseFloat(data["ast_pct"]),
|
||||
// StealPercent: mustParseFloat(data["stl_pct"]),
|
||||
// BlockPercent: mustParseFloat(data["blk_pct"]),
|
||||
// TurnoverPercent: mustParseFloat(data["tov_pct"]),
|
||||
// UsagePercent: mustParseFloat(data["usg_pct"]),
|
||||
// OffensiveWS: mustParseFloat(data["ows"]),
|
||||
// DefensiveWS: mustParseFloat(data["dws"]),
|
||||
// WinShares: mustParseFloat(data["ws"]),
|
||||
// WinSharesPer: mustParseFloat(data["ws_per_48"]),
|
||||
// OffensiveBox: mustParseFloat(data["obpm"]),
|
||||
// DefensiveBox: mustParseFloat(data["dbpm"]),
|
||||
// Box: mustParseFloat(data["bpm"]),
|
||||
// VORP: mustParseFloat(data["vorp"]),
|
||||
// Team: data["team_id"],
|
||||
// Season: season,
|
||||
// IsPlayoff: isPlayoff,
|
||||
// }
|
||||
|
||||
// // 4. upsert
|
||||
// if err := db.Clauses(clause.OnConflict{
|
||||
// Columns: []clause.Column{
|
||||
// {Name: "player_id"},
|
||||
// {Name: "season"},
|
||||
// {Name: "team"},
|
||||
// {Name: "is_playoff"},
|
||||
// },
|
||||
// DoUpdates: clause.AssignmentColumns([]string{
|
||||
// "external_id", "player_name", "position", "age", "games", "minutes_played",
|
||||
// "per", "ts_percent", "three_par", "ftr",
|
||||
// "offensive_rb_percent", "defensive_rb_percent", "total_rb_percent",
|
||||
// "assist_percent", "steal_percent", "block_percent", "turnover_percent",
|
||||
// "usage_percent", "offensive_ws", "defensive_ws", "win_shares",
|
||||
// "win_shares_per", "offensive_box", "defensive_box", "box", "vorp",
|
||||
// }),
|
||||
// }).Create(&stat).Error; err != nil {
|
||||
// log.Printf("Failed upsert advanced for %s: %v", stat.PlayerID, err)
|
||||
// }
|
||||
// })
|
||||
|
||||
// return nil
|
||||
// }
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"github.com/nprasad2077/NBA_Go/models"
|
||||
"github.com/nprasad2077/NBA_Go/utils"
|
||||
"github.com/nprasad2077/NBA_Go/utils/metrics"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
func FetchAndStorePlayerAdvancedPlayoffsStats(db *gorm.DB, season int, isPlayoff bool) error {
|
||||
metrics.DBOperationsTotal.WithLabelValues("fetch", "player_advanced").Inc()
|
||||
url := fmt.Sprintf("http://rest.nbaapi.com/api/PlayerDataAdvancedPlayoffs/query?season=%d&sortBy=PlayerName&ascending=true&pageNumber=1&pageSize=1000", season)
|
||||
|
||||
body, err := utils.GetJSON(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var stats []models.PlayerAdvancedStat
|
||||
if err := json.Unmarshal(body, &stats); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, stat := range stats {
|
||||
// Ensure the season is included from the query param
|
||||
stat.Season = season
|
||||
stat.IsPlayoff = isPlayoff
|
||||
|
||||
err := db.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "player_id"}, {Name: "season"}, {Name: "team"}, {Name: "is_playoff"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{
|
||||
"external_id", "player_name", "position", "age", "games",
|
||||
"minutes_played", "per", "ts_percent", "three_par", "ftr",
|
||||
"offensive_rb_percent", "defensive_rb_percent", "total_rb_percent",
|
||||
"assist_percent", "steal_percent", "block_percent", "turnover_percent",
|
||||
"usage_percent", "offensive_ws", "defensive_ws", "win_shares",
|
||||
"win_shares_per", "offensive_box", "defensive_box", "box", "vorp",
|
||||
}),
|
||||
}).Create(&stat).Error
|
||||
|
||||
metrics.DBOperationsTotal.WithLabelValues("store", "player_advanced").Inc()
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Failed to upsert stat for playerId %s (%s): %v", stat.PlayerID, stat.Team, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"github.com/nprasad2077/NBA_Go/models"
|
||||
"github.com/nprasad2077/NBA_Go/utils"
|
||||
"github.com/nprasad2077/NBA_Go/utils/metrics"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
func FetchAndStorePlayerAdvancedStats(db *gorm.DB, season int, isPlayoff bool) error {
|
||||
metrics.DBOperationsTotal.WithLabelValues("fetch", "player_advanced").Inc()
|
||||
url := fmt.Sprintf("http://rest.nbaapi.com/api/PlayerDataAdvanced/query?season=%d&sortBy=Points&ascending=false&pageNumber=1&pageSize=1000", season)
|
||||
|
||||
body, err := utils.GetJSON(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var stats []models.PlayerAdvancedStat
|
||||
if err := json.Unmarshal(body, &stats); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, stat := range stats {
|
||||
// Ensure the season is included from the query param
|
||||
stat.Season = season
|
||||
stat.IsPlayoff = isPlayoff
|
||||
|
||||
err := db.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "player_id"}, {Name: "season"}, {Name: "team"}, {Name: "is_playoff"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{
|
||||
"external_id", "player_name", "position", "age", "games",
|
||||
"minutes_played", "per", "ts_percent", "three_par", "ftr",
|
||||
"offensive_rb_percent", "defensive_rb_percent", "total_rb_percent",
|
||||
"assist_percent", "steal_percent", "block_percent", "turnover_percent",
|
||||
"usage_percent", "offensive_ws", "defensive_ws", "win_shares",
|
||||
"win_shares_per", "offensive_box", "defensive_box", "box", "vorp",
|
||||
}),
|
||||
}).Create(&stat).Error
|
||||
|
||||
metrics.DBOperationsTotal.WithLabelValues("store", "player_advanced").Inc()
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Failed to upsert stat for playerId %s (%s): %v", stat.PlayerID, stat.Team, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
// File: services/player_shot_chart_fetch_service.go
|
||||
package services
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/nprasad2077/NBA_Go/models"
|
||||
"github.com/nprasad2077/NBA_Go/utils"
|
||||
"github.com/nprasad2077/NBA_Go/utils/metrics"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// FetchAndStoreShotChartForPlayer pulls the public NBA‑API JSON for a single
|
||||
// player, derives the season from the shot date, and upserts into SQLite.
|
||||
// Dupes are prevented by the unique index:
|
||||
//
|
||||
// (player_id, season, date, qtr, time_remaining, top, left)
|
||||
func FetchAndStoreShotChartForPlayer(db *gorm.DB, playerID string) error {
|
||||
metrics.DBOperationsTotal.WithLabelValues("fetch", "player_shot_chart").Inc()
|
||||
|
||||
url := fmt.Sprintf("http://rest.nbaapi.com/api/ShotChartData/playerid/%s", playerID)
|
||||
body, err := utils.GetJSON(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var shots []models.PlayerShotChart
|
||||
if err := json.Unmarshal(body, &shots); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, shot := range shots {
|
||||
// "Feb 11, 2023" → season 2023
|
||||
if t, err := time.Parse("Jan 2, 2006", shot.Date); err == nil {
|
||||
shot.Season = t.Year()
|
||||
}
|
||||
|
||||
err := db.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{
|
||||
{Name: "player_id"},
|
||||
{Name: "season"},
|
||||
{Name: "date"},
|
||||
{Name: "qtr"},
|
||||
{Name: "time_remaining"},
|
||||
{Name: "top"},
|
||||
{Name: "left"},
|
||||
},
|
||||
// update mutable fields; leave the identity columns untouched
|
||||
DoUpdates: clause.AssignmentColumns([]string{
|
||||
"player_name", "result", "shot_type", "distance_ft",
|
||||
"lead", "team_score", "opponent_team_score",
|
||||
"opponent", "team",
|
||||
}),
|
||||
}).Create(&shot).Error
|
||||
|
||||
metrics.DBOperationsTotal.WithLabelValues("store", "player_shot_chart").Inc()
|
||||
|
||||
if err != nil {
|
||||
log.Printf("❌ upsert failed for %s on %s (%s %s): %v",
|
||||
shot.PlayerID, shot.Date, shot.Quarter, shot.TimeRemaining, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FetchAndStoreAllPlayerShotCharts enumerates every distinct player_id in your
|
||||
// totals table, then calls the per‑player loader. It sleeps ~1.1 s between
|
||||
// requests to stay well under public‑API rate limits.
|
||||
func FetchAndStoreAllPlayerShotCharts(db *gorm.DB) error {
|
||||
var playerIDs []string
|
||||
|
||||
// collect IDs (add other stats tables if needed)
|
||||
db.Model(&models.PlayerTotalStat{}).
|
||||
Distinct("player_id").
|
||||
Pluck("player_id", &playerIDs)
|
||||
|
||||
for _, pid := range playerIDs {
|
||||
if err := FetchAndStoreShotChartForPlayer(db, pid); err != nil {
|
||||
log.Printf("Error importing shot chart for %s: %v", pid, err)
|
||||
}
|
||||
time.Sleep(1100 * time.Millisecond) // throttle
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/nprasad2077/NBA_Go/models"
|
||||
"github.com/nprasad2077/NBA_Go/utils"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
func FetchAndStorePlayerTotalPlayoffsStats(db *gorm.DB, season int, isPlayoff bool) error {
|
||||
url := fmt.Sprintf(
|
||||
"http://rest.nbaapi.com/api/PlayerDataTotalsPlayoffs/query?season=%d&sortBy=PlayerName&ascending=true&pageNumber=1&pageSize=1000",
|
||||
season,
|
||||
)
|
||||
|
||||
body, err := utils.GetJSON(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var stats []models.PlayerTotalStat
|
||||
if err := json.Unmarshal(body, &stats); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, stat := range stats {
|
||||
stat.Season = season
|
||||
stat.IsPlayoff = isPlayoff
|
||||
|
||||
err := db.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "player_id"}, {Name: "season"}, {Name: "team"}, {Name: "is_playoff"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{
|
||||
"player_name", "position", "age", "games", "games_started",
|
||||
"minutes_pg", "field_goals", "field_attempts", "field_percent",
|
||||
"three_fg", "three_attempts", "three_percent", "two_fg", "two_attempts",
|
||||
"two_percent", "effect_fg_percent", "ft", "ft_attempts", "ft_percent",
|
||||
"offensive_rb", "defensive_rb", "total_rb", "assists", "steals",
|
||||
"blocks", "turnovers", "personal_fouls", "points",
|
||||
}),
|
||||
}).Create(&stat).Error
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Failed to upsert PlayerTotalPlayoffStat for playerId %s: %v", stat.PlayerID, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -170,43 +170,6 @@ func FetchAndStorePlayerTotalScrapedStats(db *gorm.DB, season int, isPlayoff boo
|
||||
IsPlayoff: isPlayoff,
|
||||
}
|
||||
|
||||
// // 3) map into GORM model
|
||||
// stat := models.PlayerTotalStat{
|
||||
// ExternalID: mustAtoi(data["rk"]),
|
||||
// PlayerID: playerID,
|
||||
// PlayerName: data["player"],
|
||||
// Position: data["pos"],
|
||||
// Age: mustAtoi(data["age"]),
|
||||
// Games: mustAtoi(data["g"]),
|
||||
// GamesStarted: mustAtoi(data["gs"]),
|
||||
// MinutesPG: mustParseFloat(data["mp"]), // TOTAL minutes as float
|
||||
// FieldGoals: mustAtoi(data["fg"]),
|
||||
// FieldAttempts: mustAtoi(data["fga"]),
|
||||
// FieldPercent: mustParseFloat(data["fg_pct"]),
|
||||
// ThreeFG: mustAtoi(data["fg3"]),
|
||||
// ThreeAttempts: mustAtoi(data["fg3a"]),
|
||||
// ThreePercent: mustParseFloat(data["fg3_pct"]),
|
||||
// TwoFG: mustAtoi(data["fg2"]),
|
||||
// TwoAttempts: mustAtoi(data["fg2a"]),
|
||||
// TwoPercent: mustParseFloat(data["fg2_pct"]),
|
||||
// EffectFGPercent: mustParseFloat(data["efg_pct"]),
|
||||
// FT: mustAtoi(data["ft"]),
|
||||
// FTAttempts: mustAtoi(data["fta"]),
|
||||
// FTPercent: mustParseFloat(data["ft_pct"]),
|
||||
// OffensiveRB: mustAtoi(data["orb"]),
|
||||
// DefensiveRB: mustAtoi(data["drb"]),
|
||||
// TotalRB: mustAtoi(data["trb"]),
|
||||
// Assists: mustAtoi(data["ast"]),
|
||||
// Steals: mustAtoi(data["stl"]),
|
||||
// Blocks: mustAtoi(data["blk"]),
|
||||
// Turnovers: mustAtoi(data["tov"]),
|
||||
// PersonalFouls: mustAtoi(data["pf"]),
|
||||
// Points: mustAtoi(data["pts"]),
|
||||
// Team: data["team_id"],
|
||||
// Season: season,
|
||||
// IsPlayoff: isPlayoff,
|
||||
// }
|
||||
|
||||
// 4) upsert on (player_id, season, team, is_playoff)
|
||||
if err := db.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/nprasad2077/NBA_Go/models"
|
||||
"github.com/nprasad2077/NBA_Go/utils"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
func FetchAndStorePlayerTotalStats(db *gorm.DB, season int, isPlayoff bool) error {
|
||||
url := fmt.Sprintf(
|
||||
"http://rest.nbaapi.com/api/PlayerDataTotals/query?season=%d&sortBy=PlayerName&ascending=true&pageNumber=1&pageSize=1000",
|
||||
season,
|
||||
)
|
||||
|
||||
body, err := utils.GetJSON(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var stats []models.PlayerTotalStat
|
||||
if err := json.Unmarshal(body, &stats); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, stat := range stats {
|
||||
stat.Season = season
|
||||
stat.IsPlayoff = isPlayoff
|
||||
|
||||
err := db.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "player_id"}, {Name: "season"}, {Name: "team"}, {Name: "is_playoff"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{
|
||||
"player_name", "position", "age", "games", "games_started",
|
||||
"minutes_pg", "field_goals", "field_attempts", "field_percent",
|
||||
"three_fg", "three_attempts", "three_percent", "two_fg", "two_attempts",
|
||||
"two_percent", "effect_fg_percent", "ft", "ft_attempts", "ft_percent",
|
||||
"offensive_rb", "defensive_rb", "total_rb", "assists", "steals",
|
||||
"blocks", "turnovers", "personal_fouls", "points",
|
||||
}),
|
||||
}).Create(&stat).Error
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Failed to upsert PlayerTotalStat for playerId %s: %v", stat.PlayerID, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user