Skip to main content

Run Scripts Across All 3 Environments (Local, Dev, Stg)

For Claude: REQUIRED SUB-SKILL: Use superpowers-extended-cc:executing-plans to implement this plan task-by-task.

Goal: Execute all necessary batch scripts against all 3 MongoDB databases (local, dev, stg) to ensure consistent data across environments.

Architecture: Scripts are run via yarn script-dev which reads script/config.json for the script sequence and script/.env for the DB connection. We switch environments by uncommenting the appropriate MONGO_CONNECTION_STRING in script/.env, run the required scripts, then move to the next environment.

Tech Stack: Node.js, Yarn 4 PnP, MongoDB Atlas, Mongoose


Environment Connection Strings

All stored in script/.env (toggle by commenting/uncommenting):

EnvConnection String
Localmongodb+srv://demo:GhKYzU37veEYEmrc@energyaudit.0l6duna.mongodb.net/
Devmongodb+srv://dgi-ai-widgets-demo-dev:FEQ8AKdlea4rmkHM@dev.e45p6au.mongodb.net/
Stgmongodb+srv://dgi-ai-widgets-demo-stg:tiuYWzxKOWnvNXwj@stg.ivmjjjb.mongodb.net/

DB name is dtcea-mumbai-demo for all 3.


Scripts Inventory & Execution Order

Scripts have dependencies — they must run in this order:

#ScriptEntityWhat it doesDependencies
1POST /seed/ai-audit-test-dataseed (API)Seeds demo TCs, installations, users, AI audit results for section S01None (standalone seed)
2transformer_backfillAuditStatustransformerSets auditStatus = AUDITED/AUDIT_FAILED on all TCs based on lossPercentageRequires TCs to exist (after seeds + migration data)
3transformer_fixInvalidAuditedTCstransformerFixes TCs that have lossPercentage but missing tcConsumption/installationConsumptionAfter backfillAuditStatus
4installation_backfillAvgConsumptioninstallationComputes 6-month avg consumption for remarked installationsRequires installations to exist
5transformer_backfillHealthScoretransformerComputes healthScore + healthGrade on all TCsAfter audit status is finalized
6auditSummary_computeAuditSummaryauditSummaryComputes section→MESCOM level audit summariesMust run LAST — depends on final TC audit states

Execution Strategy

For each environment, we run all 6 scripts in order. The approach:

  1. Update script/.env to point to the target environment
  2. Update script/config.json with the script(s) to run
  3. Run yarn script-dev
  4. Verify output logs
  5. Repeat for next script or next environment

Order: Local → Dev → Stg (local first so we can verify before touching shared envs)


Task 1: Run all scripts on LOCAL environment

Files:

  • Modify: script/.env (ensure Local connection string is active)
  • Modify: script/config.json (update scriptRunArray for each script)

Step 1: Verify script/.env points to Local

Ensure the active (uncommented) connection string is:

MONGO_CONNECTION_STRING=mongodb+srv://demo:GhKYzU37veEYEmrc@energyaudit.0l6duna.mongodb.net/

Step 2: Seed via API

POST /dtcea-mumbai-demo/v1/seed/ai-audit-test-data
Body: { "clearExisting": true }

Expected: Response showing seed data inserted for TCs, installations, users, AI audit results

Step 3: Backfill audit status

Update script/config.json:

{
"scriptRunArray": ["transformer_backfillAuditStatus"],
"input": {
"transformer": {
"backfillAuditStatus": {}
}
},
"printTagsToScriptMapping": false
}

Run: yarn script-dev Expected: Logs with Marked AUDITED: N and Marked AUDIT_FAILED: N

Step 4: Fix invalid audited TCs

Update script/config.json:

{
"scriptRunArray": ["transformer_fixInvalidAuditedTCs"],
"input": {
"transformer": {
"fixInvalidAuditedTCs": {}
}
},
"printTagsToScriptMapping": false
}

Run: yarn script-dev Expected: Either "Nothing to fix" or logs showing affected TCs corrected

Step 5: Backfill avg consumption

Update script/config.json:

{
"scriptRunArray": ["installation_backfillAvgConsumption"],
"input": {
"installation": {
"backfillAvgConsumption": {}
}
},
"printTagsToScriptMapping": false
}

Run: yarn script-dev Expected: Per-month logs showing averages computed and merged

Step 6: Backfill health scores

Update script/config.json:

{
"scriptRunArray": ["transformer_backfillHealthScore"],
"input": {
"transformer": {
"backfillHealthScore": {}
}
},
"printTagsToScriptMapping": false
}

Run: yarn script-dev Expected: Per-month logs showing TCs updated with healthScore + healthGrade

Step 7: Compute audit summaries (MUST BE LAST)

Update script/config.json:

{
"scriptRunArray": ["auditSummary_computeAuditSummary"],
"input": {
"auditSummary": {
"computeAuditSummary": {}
}
},
"printTagsToScriptMapping": false
}

Run: yarn script-dev Expected: Per-month SECTION/SUB_DIVISION/DIVISION/CIRCLE/ZONE/MESCOM summaries inserted, invariant checks all "OK"

Step 8: Commit checkpoint

git add script/config.json
git commit -m "chore: run all scripts on local environment"

Task 2: Run all scripts on DEV environment

Files:

  • Modify: script/.env (switch to Dev connection string)

Step 1: Switch script/.env to Dev

# Local
# MONGO_CONNECTION_STRING=mongodb+srv://demo:GhKYzU37veEYEmrc@energyaudit.0l6duna.mongodb.net/
# Dev
MONGO_CONNECTION_STRING=mongodb+srv://dgi-ai-widgets-demo-dev:FEQ8AKdlea4rmkHM@dev.e45p6au.mongodb.net/
# Stg
# MONGO_CONNECTION_STRING=mongodb+srv://dgi-ai-widgets-demo-stg:tiuYWzxKOWnvNXwj@stg.ivmjjjb.mongodb.net/

Steps 2-8: Run all 7 scripts in the same order as Task 1

Same config.json updates and yarn script-dev commands. Same expected outputs.

Step 9: Verify Dev data

Quick sanity check — connect to Dev DB and confirm:

  • transformers collection has docs with auditStatus set
  • audit-summaries collection has SECTION and MESCOM level docs
  • installations collection has avgConsumption6Months on remarked docs

Task 3: Run all scripts on STG environment

Files:

  • Modify: script/.env (switch to Stg connection string)

Step 1: Switch script/.env to Stg

# Local
# MONGO_CONNECTION_STRING=mongodb+srv://demo:GhKYzU37veEYEmrc@energyaudit.0l6duna.mongodb.net/
# Dev
# MONGO_CONNECTION_STRING=mongodb+srv://dgi-ai-widgets-demo-dev:FEQ8AKdlea4rmkHM@dev.e45p6au.mongodb.net/
# Stg
MONGO_CONNECTION_STRING=mongodb+srv://dgi-ai-widgets-demo-stg:tiuYWzxKOWnvNXwj@stg.ivmjjjb.mongodb.net/

Steps 2-8: Run all 7 scripts in the same order as Task 1

Same config.json updates and yarn script-dev commands. Same expected outputs.

Step 9: Verify Stg data

Same sanity checks as Dev.


Task 4: Restore Local config and clean up

Step 1: Switch script/.env back to Local

# Local
MONGO_CONNECTION_STRING=mongodb+srv://demo:GhKYzU37veEYEmrc@energyaudit.0l6duna.mongodb.net/
# Dev
# MONGO_CONNECTION_STRING=mongodb+srv://dgi-ai-widgets-demo-dev:FEQ8AKdlea4rmkHM@dev.e45p6au.mongodb.net/
# Stg
# MONGO_CONNECTION_STRING=mongodb+srv://dgi-ai-widgets-demo-stg:tiuYWzxKOWnvNXwj@stg.ivmjjjb.mongodb.net/

Step 2: Reset script/config.json to default

{
"scriptRunArray": ["auditSummary_computeAuditSummary"],
"input": {
"auditSummary": {
"computeAuditSummary": {}
}
},
"printTagsToScriptMapping": false
}

Step 3: Verify local API still works

Run: yarn api-dev Expected: API starts successfully, connects to local MongoDB


Important Notes

  • computeAuditSummary DELETES all existing audit summaries before recomputing — always run it last
  • POST /seed/ai-audit-test-data is idempotent — pass clearExisting: true to clean up before inserting
  • backfillAuditStatus only touches TCs with auditStatus: null — safe to re-run
  • fixInvalidAuditedTCs is idempotent — only fixes TCs matching the bad state
  • backfillAvgConsumption creates a temp collection and drops it after — safe to re-run
  • backfillHealthScore overwrites healthScore/healthGrade — safe to re-run

Estimated Scale

  • ~150K TCs × 7 months = ~1M transformer docs
  • ~3.3M installation docs
  • backfillAvgConsumption is the heaviest (aggregation + $merge on millions of docs) — expect 5-15 min per environment
  • backfillHealthScore processes ~1M docs in batches of 500 — expect 3-8 min per environment
  • computeAuditSummary aggregates ~1M TCs — expect 2-5 min per environment