games field fixes

This commit is contained in:
Ravi Prasad
2025-06-03 20:28:46 -05:00
parent c32da76997
commit 5a027f6636
14 changed files with 38366 additions and 68 deletions
Vendored
BIN
View File
Binary file not shown.
+10 -10
View File
@@ -22,18 +22,18 @@ type AdvancedStatsResponse struct {
// FetchPlayerAdvancedStats returns a handler that imports advanced stats for a season
// Note: not exposed in Swagger docs, only internal use.
func FetchPlayerAdvancedStats(db *gorm.DB) fiber.Handler {
return func(c *fiber.Ctx) error {
season := c.QueryInt("season", 2025)
isPlayoff := c.QueryBool("isPlayoff", false)
// func FetchPlayerAdvancedStats(db *gorm.DB) fiber.Handler {
// return func(c *fiber.Ctx) error {
// season := c.QueryInt("season", 2025)
// isPlayoff := c.QueryBool("isPlayoff", false)
if err := services.FetchAndStorePlayerAdvancedStats(db, season, isPlayoff); err != nil {
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
}
// if err := services.FetchAndStorePlayerAdvancedStats(db, season, isPlayoff); err != nil {
// return c.Status(500).JSON(fiber.Map{"error": err.Error()})
// }
return c.JSON(fiber.Map{"message": "Player stats fetched and saved."})
}
}
// return c.JSON(fiber.Map{"message": "Player stats fetched and saved."})
// }
// }
// ScrapePlayerAdvancedStats godoc
// @Summary Scrape player advanced stats from BR website
+12 -12
View File
@@ -16,18 +16,18 @@ import (
// @Success 200 {object} map[string]string
// @Failure 400,500 {object} map[string]string
// @Router /api/playershotchart/fetch [get]
func FetchPlayerShotChartAPI(db *gorm.DB) fiber.Handler {
return func(c *fiber.Ctx) error {
pid := c.Query("playerId")
if pid == "" {
return c.Status(400).JSON(fiber.Map{"error": "playerId query parameter is required"})
}
if err := services.FetchAndStoreShotChartForPlayer(db, pid); err != nil {
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
}
return c.JSON(fiber.Map{"message": "Shot chart for " + pid + " fetched and saved."})
}
}
// func FetchPlayerShotChartAPI(db *gorm.DB) fiber.Handler {
// return func(c *fiber.Ctx) error {
// pid := c.Query("playerId")
// if pid == "" {
// return c.Status(400).JSON(fiber.Map{"error": "playerId query parameter is required"})
// }
// if err := services.FetchAndStoreShotChartForPlayer(db, pid); err != nil {
// return c.Status(500).JSON(fiber.Map{"error": err.Error()})
// }
// return c.JSON(fiber.Map{"message": "Shot chart for " + pid + " fetched and saved."})
// }
// }
// ScrapePlayerShotChart godoc
// @Summary Scrape a player's shot-chart from BR website
+11 -11
View File
@@ -19,19 +19,19 @@ import (
"github.com/nprasad2077/NBA_Go/models"
)
func FetchPlayerTotalStats(db *gorm.DB) fiber.Handler {
return func(c *fiber.Ctx) error {
season := c.QueryInt("season", 2025)
isPlayoff := c.QueryBool("isPlayoff", false)
// func FetchPlayerTotalStats(db *gorm.DB) fiber.Handler {
// return func(c *fiber.Ctx) error {
// season := c.QueryInt("season", 2025)
// isPlayoff := c.QueryBool("isPlayoff", false)
err := services.FetchAndStorePlayerTotalStats(db, season, isPlayoff)
if err != nil {
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
}
// err := services.FetchAndStorePlayerTotalStats(db, season, isPlayoff)
// if err != nil {
// return c.Status(500).JSON(fiber.Map{"error": err.Error()})
// }
return c.JSON(fiber.Map{"message": "Player total stats fetched and saved."})
}
}
// return c.JSON(fiber.Map{"message": "Player total stats fetched and saved."})
// }
// }
// ScrapePlayerTotalStats godoc
// @Summary Scrape player total stats from BR website
BIN
View File
Binary file not shown.
@@ -0,0 +1,253 @@
{
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": {
"type": "grafana",
"uid": "-- Grafana --"
},
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"type": "dashboard"
}
]
},
"editable": true,
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
"id": 1,
"links": [],
"liveNow": true,
"panels": [
{
"datasource": {
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "thresholds"
},
"fieldMinMax": false,
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green"
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 10,
"w": 24,
"x": 0,
"y": 0
},
"id": 3,
"options": {
"displayMode": "lcd",
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom",
"showLegend": false
},
"maxVizHeight": 300,
"minVizHeight": 16,
"minVizWidth": 8,
"namePlacement": "auto",
"orientation": "horizontal",
"reduceOptions": {
"calcs": [
"delta"
],
"fields": "",
"values": false
},
"showUnfilled": true,
"sizing": "auto",
"text": {
"titleSize": 14
},
"valueMode": "color"
},
"pluginVersion": "11.6.1",
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"disableTextWrap": false,
"editorMode": "builder",
"expr": "nba_http_requests_total{endpoint=\"/api/playeradvancedstats/\", status=\"200\"}",
"fullMetaSearch": false,
"hide": false,
"includeNullMetadata": false,
"instant": false,
"legendFormat": "__auto",
"range": true,
"refId": "Advanced Stats Endpoint Sum",
"useBackend": false
}
],
"title": "Stats",
"transparent": true,
"type": "bargauge"
},
{
"datasource": {
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisBorderShow": false,
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"barWidthFactor": 0.6,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"insertNulls": false,
"lineInterpolation": "linear",
"lineStyle": {
"fill": "solid"
},
"lineWidth": 1,
"pointSize": 7,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "always",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "normal"
},
"thresholdsStyle": {
"mode": "off"
}
},
"fieldMinMax": false,
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green"
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 12,
"w": 24,
"x": 0,
"y": 10
},
"id": 2,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom",
"showLegend": true
},
"timezone": [
"browser"
],
"tooltip": {
"hideZeros": false,
"mode": "single",
"sort": "none"
}
},
"pluginVersion": "11.6.1",
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"disableTextWrap": false,
"editorMode": "builder",
"expr": "sum by(endpoint) (nba_http_requests_total{endpoint=\"/api/playeradvancedstats/\"})",
"fullMetaSearch": false,
"hide": false,
"includeNullMetadata": false,
"instant": false,
"legendFormat": "__auto",
"range": true,
"refId": "Advanced Stats Endpoint Sum",
"useBackend": false
},
{
"datasource": {
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"disableTextWrap": false,
"editorMode": "builder",
"expr": "nba_http_requests_total{endpoint=\"/api/playeradvancedstats/\"}",
"fullMetaSearch": false,
"hide": false,
"includeNullMetadata": false,
"instant": false,
"legendFormat": "__auto",
"range": true,
"refId": "Adv Stats individual endpoints",
"useBackend": false
}
],
"title": "Panel Title",
"type": "timeseries"
}
],
"preload": true,
"refresh": "5s",
"schemaVersion": 41,
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "now-5m",
"to": "now"
},
"timepicker": {},
"timezone": "browser",
"title": "NBA dashboard",
"uid": "bent2fb718074d",
"version": 9
}
+8 -8
View File
@@ -34,14 +34,14 @@ func importPlayerAdvancedPlayoffs(db *gorm.DB) {
}
// importPlayerShotChart fetches shot-charts for every known player
func importPlayerShotChart(db *gorm.DB) {
const firstID = "hardeja01"
log.Printf("▶️ importing shot chart for player %s…", firstID)
if err := services.FetchAndStoreShotChartForPlayer(db, firstID); err != nil {
log.Printf("shot chart import failed for %s: %v", firstID, err)
}
// you can add more IDs here or just rely on the API endpoint after
}
// func importPlayerShotChart(db *gorm.DB) {
// const firstID = "hardeja01"
// log.Printf("▶️ importing shot chart for player %s…", firstID)
// if err := services.FetchAndStoreShotChartForPlayer(db, firstID); err != nil {
// log.Printf("shot chart import failed for %s: %v", firstID, err)
// }
// // you can add more IDs here or just rely on the API endpoint after
// }
// importPlayerTotalsScrape fetches & stores scraped regular-season total stats
func importPlayerTotalsScrape(db *gorm.DB) {
-1
View File
@@ -14,7 +14,6 @@ type PlayerAdvancedStat struct {
Position string `json:"position"`
Age int `json:"age"`
Games int `json:"games"`
GamesStarted int `json:"gamesStarted"`
MinutesPlayed int `json:"minutesPlayed"`
PER float64 `json:"per"`
TSPercent float64 `json:"tsPercent"`
+1 -1
View File
@@ -9,7 +9,7 @@ import (
func RegisterPlayerAdvancedRoutes(app *fiber.App, db *gorm.DB) {
api := app.Group("/api/playeradvancedstats")
api.Get("/fetch", controllers.FetchPlayerAdvancedStats(db))
// api.Get("/fetch", controllers.FetchPlayerAdvancedStats(db))
api.Get("/scrape", controllers.ScrapePlayerAdvancedStats(db))
api.Get("/", controllers.GetAllAdvancedPlayerStats(db))
}
+1 -1
View File
@@ -9,7 +9,7 @@ import (
// RegisterPlayerShotChartRoutes sets up the shot-chart endpoints
func RegisterPlayerShotChartRoutes(app *fiber.App, db *gorm.DB) {
api := app.Group("/api/playershotchart")
api.Get("/fetch", controllers.FetchPlayerShotChartAPI(db))
// api.Get("/fetch", controllers.FetchPlayerShotChartAPI(db))
api.Get("/scrape", controllers.ScrapePlayerShotChart(db))
api.Get("/", controllers.GetPlayerShotChart(db))
}
+1 -1
View File
@@ -9,7 +9,7 @@ import (
func RegisterPlayerTotalRoutes(app *fiber.App, db *gorm.DB) {
api := app.Group("/api/playertotals")
api.Get("/fetch", controllers.FetchPlayerTotalStats(db))
// api.Get("/fetch", controllers.FetchPlayerTotalStats(db))
api.Get("/scrape", controllers.ScrapePlayerTotalStats(db))
api.Get("/", controllers.GetPlayerTotalStats(db))
}
@@ -54,31 +54,59 @@ func FetchAndStorePlayerAdvancedScrapedStats(db *gorm.DB, season int, isPlayoff
return err
}
// pick the right table selector
var table *goquery.Selection
// 1) Determine parent wrapper and table selector
// - Regular season: <div id="all_advanced"><!-- <table id="advanced">…</table> --></div>
// - Playoffs: <div id="all_advanced_stats"><!-- <table id="advanced_stats">…</table> --></div>
var parentDivSelector, tableSelector string
if isPlayoff {
table = doc.Find("#div_advanced_stats table#advanced_stats")
parentDivSelector = "#all_advanced_stats"
tableSelector = "table#advanced_stats"
} else {
table = doc.Find("#div_advanced table#advanced")
}
if table.Length() == 0 {
return fmt.Errorf("could not find advanced stats table")
parentDivSelector = "#all_advanced"
tableSelector = "table#advanced"
}
// 1. collect the data-stat keys in header order
// Attempt to find the table node directly (it will be inside a comment for regular season)
table := doc.Find(parentDivSelector + " " + tableSelector)
if table.Length() == 0 {
// Look for the commentedout HTML inside parentDivSelector
commentSel := doc.
Find(parentDivSelector).
Contents().
FilterFunction(func(i int, s *goquery.Selection) bool {
return goquery.NodeName(s) == "#comment"
})
if commentSel.Length() == 0 {
return fmt.Errorf("could not find advanced stats table (even inside comment)")
}
// Extract the raw HTML string from the comment, then reparse
commentedHTML := commentSel.Nodes[0].FirstChild.Data
innerDoc, err := goquery.NewDocumentFromReader(strings.NewReader(commentedHTML))
if err != nil {
return fmt.Errorf("failed to parse commented advanced HTML: %w", err)
}
table = innerDoc.Find(tableSelector)
if table.Length() == 0 {
return fmt.Errorf("could not find advanced stats table after uncommenting")
}
}
// 2) Collect the data-stat keys in header order
var headers []string
table.Find("thead tr th").Each(func(i int, th *goquery.Selection) {
if stat, ok := th.Attr("data-stat"); ok && stat != "" {
headers = append(headers, stat)
}
})
// add our appended-player column
// Add our appendedplayer column
headers = append(headers, "player-additional")
// 2. iterate each row
// 3) Iterate each row
table.Find("tbody tr").Each(func(_ int, tr *goquery.Selection) {
if tr.HasClass("thead") {
return // skip repeated headers
return // skip repeated header rows
}
cells := tr.Find("th, td")
data := make(map[string]string, len(headers))
@@ -92,13 +120,14 @@ func FetchAndStorePlayerAdvancedScrapedStats(db *gorm.DB, season int, isPlayoff
}
})
if playerID == "" {
return // not a data row
// not a real data row
return
}
data["player-additional"] = playerID
// 4) pick the correct keys for ExternalID, PlayerName, Team, depending on season vs. playoff
// - playoffs tables still use "rk", "player", "team_id"
// - regularseason advanced uses "ranker", "name_display", "team_name_abbr"
// 4) Determine ExternalID, PlayerName, Team
// • Playoff pages use "rk", "player", "team_id"
// • Regularseason advanced uses "ranker", "name_display", "team_name_abbr"
extID := mustAtoi(data["rk"])
if extID == 0 {
extID = mustAtoi(data["ranker"])
@@ -114,15 +143,20 @@ func FetchAndStorePlayerAdvancedScrapedStats(db *gorm.DB, season int, isPlayoff
teamID = data["team_name_abbr"]
}
// 5) map into your GORM model
// 5) Games column is always "g" in advanced (playoffs or season)
g := mustAtoi(data["games"])
if g == 0 {
g = mustAtoi(data["g"])
}
// 6) Map into your GORM model
stat := models.PlayerAdvancedStat{
ExternalID: extID,
PlayerID: playerID,
PlayerName: playerName,
Position: data["pos"],
Age: mustAtoi(data["age"]),
Games: mustAtoi(data["g"]),
GamesStarted: mustAtoi(data["games_started"]),
Games: g,
MinutesPlayed: mustAtoi(data["mp"]),
PER: mustParseFloat(data["per"]),
TSPercent: mustParseFloat(data["ts_pct"]),
@@ -149,7 +183,7 @@ func FetchAndStorePlayerAdvancedScrapedStats(db *gorm.DB, season int, isPlayoff
IsPlayoff: isPlayoff,
}
// 6) upsert on (player_id, season, team, is_playoff)
// 7) Upsert on (player_id, season, team, is_playoff)
if err := db.Clauses(clause.OnConflict{
Columns: []clause.Column{
{Name: "player_id"},
@@ -158,9 +192,9 @@ func FetchAndStorePlayerAdvancedScrapedStats(db *gorm.DB, season int, isPlayoff
{Name: "is_playoff"},
},
DoUpdates: clause.AssignmentColumns([]string{
"external_id", "player_name", "position", "age", "games", "games_started",
"external_id", "player_name", "position", "age", "games",
"minutes_played",
"per", "ts_percent", "three_par", "ftr",
"per", "ts_percent", "three_par", "ftr", // ← use 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",
+14 -2
View File
@@ -134,14 +134,26 @@ func FetchAndStorePlayerTotalScrapedStats(db *gorm.DB, season int, isPlayoff boo
teamID = data["team_name_abbr"]
}
// pick “games” → fallback to “g” if empty
g := mustAtoi(data["games"])
if g == 0 {
g = mustAtoi(data["g"])
}
// pick “games_started” → fallback to “gs” if empty
gs := mustAtoi(data["games_started"])
if gs == 0 {
gs = mustAtoi(data["gs"])
}
stat := models.PlayerTotalStat{
ExternalID: extID,
PlayerID: playerID,
PlayerName: playerName,
Position: data["pos"],
Age: mustAtoi(data["age"]),
Games: mustAtoi(data["games"]),
GamesStarted: mustAtoi(data["games_started"]),
Games: g,
GamesStarted: gs,
MinutesPG: mustParseFloat(data["mp"]),
FieldGoals: mustAtoi(data["fg"]),
FieldAttempts: mustAtoi(data["fga"]),
+38000
View File
File diff suppressed because it is too large Load Diff