# BHI Layer — Integration Plan Steps to merge the BHI layer into the base Economic Brain after the base build finishes. **Prereqs** (verified before step 1): - Base Brain is running: `psql -d brain -c '\dt'` shows core tables including `job_runs`. - `/home/ubuntu/economic-brain/` contains a working `jobs/` directory structure. - DATABASE_URL env var exported and pointing at the `brain` Postgres. --- ## 1. Apply the BHI schema ```bash cd /home/ubuntu/economic-brain-bhi psql "$DATABASE_URL" -f schemas/bhi_tables.sql psql "$DATABASE_URL" -c "\dt bhi_*" # Expect 9 tables: bhi_facilities, bhi_facility_quality, bhi_facility_financials, # bhi_demand_indicators, bhi_workforce, bhi_shortages, bhi_rtf_licensing, # bhi_policy_events, bhi_crisis_calls ``` ## 2. Copy ingestion jobs into the Brain's jobs tree ```bash mkdir -p /home/ubuntu/economic-brain/jobs/bhi cp /home/ubuntu/economic-brain-bhi/jobs/ingestion/*.py /home/ubuntu/economic-brain/jobs/bhi/ # _common.py is included; it reads DATABASE_URL from env already ``` Install Python deps if the base Brain doesn't already have them: ```bash pip install requests psycopg2-binary ``` ## 3. Smoke test every Phase A job (no DB writes) ```bash cd /home/ubuntu/economic-brain/jobs/bhi for f in cms_ipfqr.py cms_hospital_compare.py cms_nursing_home.py \ samhsa_locator.py hrsa_hpsa.py nppes.py cdc_brfss.py \ cdc_yrbss.py cdc_wonder_mortality.py bls_oes.py cms_pos.py \ samhsa_nssats_nmhss.py idea_part_b.py nsch.py; do echo "=== $f ===" python3 "$f" test || echo "FAIL: $f" done ``` Every job should print `OK:` and exit 0. If any fail, fix the endpoint/URL in the job file before proceeding. ## 4. Run jobs in dependency order ```bash # Facilities first (feed bhi_facilities.facility_id FK for quality/financials) python3 cms_ipfqr.py python3 cms_hospital_compare.py python3 cms_nursing_home.py python3 samhsa_locator.py python3 cms_pos.py python3 samhsa_nssats_nmhss.py python3 nppes.py # Shortages + demand (independent) python3 hrsa_hpsa.py python3 cdc_wonder_mortality.py python3 cdc_brfss.py python3 cdc_yrbss.py python3 idea_part_b.py python3 nsch.py # Workforce python3 bls_oes.py ``` Monitor `job_runs`: ```sql SELECT job_name, status, started_at, finished_at, error FROM job_runs WHERE job_name LIKE 'bhi_%' ORDER BY started_at DESC; ``` ## 5. Import n8n workflows (scheduled refresh) Create workflows in n8n (or add to existing scheduler): | Workflow | Cron | Script | |---|---|---| | BHI: CMS facilities refresh | `0 3 * * 1` (weekly Mon 3am) | `cms_ipfqr.py`, `cms_hospital_compare.py`, `cms_nursing_home.py` | | BHI: SAMHSA locator refresh | `0 4 1 * *` (monthly) | `samhsa_locator.py` | | BHI: HRSA HPSA refresh | `0 5 * * 2` (weekly Tue 5am) | `hrsa_hpsa.py` | | BHI: CDC demand refresh | `0 6 1 * *` (monthly) | `cdc_brfss.py`, `cdc_yrbss.py`, `cdc_wonder_mortality.py` | | BHI: Workforce refresh | `0 7 1 */3 *` (quarterly) | `bls_oes.py` | | BHI: CMS POS refresh | `0 8 1 */3 *` (quarterly) | `cms_pos.py` | Workflow template: Cron node -> Execute Command (`python3 /home/ubuntu/economic-brain/jobs/bhi/