From d8e3ff34b67f21d358ed5f9fe56e5aefe22ad81c Mon Sep 17 00:00:00 2001 From: Ravi Prasad Date: Thu, 19 Jun 2025 22:06:44 -0500 Subject: [PATCH] working remote db --- .gitignore | 1 + docker-compose.local.yml | 101 ++++++++++++++++++++++++++++++++++++ docker-compose.override.yml | 34 ++++++++++++ import.go | 8 +-- 4 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 docker-compose.local.yml create mode 100644 docker-compose.override.yml diff --git a/.gitignore b/.gitignore index fb763e3..c45b604 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,7 @@ go.work.sum # env file .env +.env.local data/nba.db diff --git a/docker-compose.local.yml b/docker-compose.local.yml new file mode 100644 index 0000000..5c32137 --- /dev/null +++ b/docker-compose.local.yml @@ -0,0 +1,101 @@ +# docker-compose.local.yml +# +# Use this file to run local services (API, Nginx, etc.) while +# connecting to a REMOTE database. +# +# This version includes the 'db-init' service. Note that this service +# will attempt to run its 'import-data' command every time you execute +# 'docker-compose up'. + +services: + # This service runs the 'import-data' command against the remote database + # specified in your '.env.local' file. It has no 'depends_on' since there + # is no local postgres container to wait for. + db-init: + build: . + env_file: .env.local + command: ["/nba_go", "import-data"] + networks: + - api.network + restart: "no" + + # API services now have no 'depends_on' section. + # They will start and immediately try to connect to the DB_HOST in your .env file. + api1: + build: . + env_file: .env.local + ports: + - '5001:5000' + networks: + - api.network + restart: always + + api2: + build: . + env_file: .env.local + ports: + - '5002:5000' + networks: + - api.network + restart: always + + api3: + build: . + env_file: .env.local + ports: + - '5003:5000' + networks: + - api.network + restart: always + + nginx: + image: 'nginx:stable' + volumes: + - './nginx/nginx.conf:/etc/nginx/nginx.conf:ro' + depends_on: + - api1 + - api2 + - api3 + ports: + - '8080:8080' + networks: + - api.network + + prometheus: + image: 'prom/prometheus:latest' + volumes: + - './prometheus/prometheus.yml:/etc/prometheus/prometheus.yml' + - 'prometheus_data:/prometheus' + command: + - '--config.file=/etc/prometheus/prometheus.yml' + - '--storage.tsdb.path=/prometheus' + - '--web.console.libraries=/etc/prometheus/console_libraries' + - '--web.console.templates=/etc/prometheus/consoles' + ports: + - '9090:9090' + networks: + - api.network + restart: unless-stopped + + grafana: + image: grafana/grafana:latest + ports: + - '3001:3000' + environment: + - GF_SECURITY_ADMIN_PASSWORD=testing + volumes: + - './grafana/provisioning/datasources:/etc/grafana/provisioning/datasources' + - './grafana/provisioning/dashboards:/etc/grafana/provisioning/dashboards' + - './grafana/dashboards:/var/lib/grafana/dashboards' + - 'grafana_data:/var/lib/grafana' + networks: + - api.network + restart: unless-stopped + +volumes: + # The 'postgres_data' volume is not needed in this configuration. + prometheus_data: + grafana_data: + +networks: + api.network: null \ No newline at end of file diff --git a/docker-compose.override.yml b/docker-compose.override.yml new file mode 100644 index 0000000..6e5917a --- /dev/null +++ b/docker-compose.override.yml @@ -0,0 +1,34 @@ +# docker-compose.override.yml +# +# This file is used for local development to connect to a REMOTE database. +# It overrides the main 'docker-compose.yml' by: +# 1. Disabling the local 'postgres' service by replacing it with a container that runs forever. +# 2. Removing the 'depends_on' conditions from other services that rely on the local database. + +services: + # This section effectively disables the local postgres service. + # Instead of `command: "true"`, which exits immediately, we use `tail -f /dev/null` + # to keep the container running indefinitely, satisfying Docker Compose. + postgres: + image: busybox + command: ["tail", "-f", "/dev/null"] + networks: [] + healthcheck: {} + volumes: [] + + # Remove the dependency on the local postgres container for the db-init service. + # It will now start immediately and use the DB_HOST from your .env.local file. + db-init: + depends_on: {} + + # Remove the dependency on the local postgres container for the api1 service. + api1: + depends_on: {} + + # Remove the dependency on the local postgres container for the api2 service. + api2: + depends_on: {} + + # Remove the dependency on the local postgres container for the api3 service. + api3: + depends_on: {} \ No newline at end of file diff --git a/import.go b/import.go index 5650edb..bfe2a4b 100644 --- a/import.go +++ b/import.go @@ -11,7 +11,7 @@ import ( // importPlayerAdvanced fetches and stores advanced stats for seasons 2017–2025 func importPlayerAdvanced(db *gorm.DB) { - for season := 2023; season <= 2025; season++ { + for season := 2015; season <= 2019; season++ { if err := services.FetchAndStorePlayerAdvancedScrapedStats(db, season, false); err != nil { log.Printf("advanced import failed for %d: %v", season, err) } @@ -23,7 +23,7 @@ func importPlayerAdvanced(db *gorm.DB) { // importPlayerAdvancedPlayoffs fetches and stores advanced stats for playoffs seasons 2023–2025 func importPlayerAdvancedPlayoffs(db *gorm.DB) { - for season := 2023; season <= 2025; season++ { + for season := 2015; season <= 2019; season++ { if err := services.FetchAndStorePlayerAdvancedScrapedStats(db, season, true); err != nil { log.Printf("advanced import failed for %d: %v", season, err) } @@ -35,7 +35,7 @@ func importPlayerAdvancedPlayoffs(db *gorm.DB) { // importPlayerTotalsScrape fetches & stores scraped regular-season total stats func importPlayerTotalsScrape(db *gorm.DB) { - for season := 2023; season <= 2025; season++ { + for season := 2015; season <= 2019; season++ { if err := services.FetchAndStorePlayerTotalScrapedStats(db, season, false); err != nil { log.Printf("scraped totals import failed for %d: %v", season, err) } @@ -47,7 +47,7 @@ func importPlayerTotalsScrape(db *gorm.DB) { // importPlayerPlayoffsScrape fetches & stores scraped playoff total stats func importPlayerTotalsPlayoffsScrape(db *gorm.DB) { - for season := 2023; season <= 2025; season++ { + for season := 2015; season <= 2019; season++ { if err := services.FetchAndStorePlayerTotalScrapedStats(db, season, true); err != nil { log.Printf("scraped playoffs import failed for %d: %v", season, err) }