Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -294,25 +294,55 @@ async def get_asset_detail(asset_id: str, db: Session = Depends(get_db)):
|
|
| 294 |
raise HTTPException(status_code=404, detail=f"Asset {asset_id} not found. Visit /api/debug/seed to populate demo data.")
|
| 295 |
return asset.to_dict()
|
| 296 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
@app.get("/api/debug/seed")
|
| 298 |
async def seed_database(db: Session = Depends(get_db)):
|
| 299 |
-
"""Seed cloud database with NASA demo assets."""
|
| 300 |
try:
|
| 301 |
-
|
| 302 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
|
| 304 |
-
demo_id = str(uuid.uuid4())
|
| 305 |
-
new_asset = Asset(
|
| 306 |
-
id=demo_id, owner_id="demo_user", project_name="NASA PCoE ESS-01",
|
| 307 |
-
asset_id="BESS_01", location="Mountain View, CA", capacity=100.0,
|
| 308 |
-
bms_provider="ZenVolt Native", category=json.dumps(["Grid-Scale", "Lithium-Ion"]),
|
| 309 |
-
project_stage="Certified", health_score=94.2, remaining_life=1.5,
|
| 310 |
-
degradation_rate=2.4, confidence=0.94, is_online=1
|
| 311 |
-
)
|
| 312 |
-
db.add(new_asset)
|
| 313 |
db.commit()
|
| 314 |
-
return {"message": "Success!
|
| 315 |
except Exception as e:
|
|
|
|
| 316 |
return {"error": str(e)}
|
| 317 |
|
| 318 |
if __name__ == "__main__":
|
|
|
|
| 294 |
raise HTTPException(status_code=404, detail=f"Asset {asset_id} not found. Visit /api/debug/seed to populate demo data.")
|
| 295 |
return asset.to_dict()
|
| 296 |
|
| 297 |
+
@app.get("/api/assets/{asset_id}/history")
|
| 298 |
+
async def get_asset_history(asset_id: str, db: Session = Depends(get_db)):
|
| 299 |
+
"""Get historical health predictions for an asset."""
|
| 300 |
+
asset = db.query(Asset).filter((Asset.id == asset_id) | (Asset.asset_id == asset_id)).first()
|
| 301 |
+
if not asset:
|
| 302 |
+
raise HTTPException(status_code=404, detail="Asset not found")
|
| 303 |
+
|
| 304 |
+
predictions = db.query(HealthPrediction)\
|
| 305 |
+
.filter(HealthPrediction.asset_id == asset.id)\
|
| 306 |
+
.order_by(HealthPrediction.cycle_index)\
|
| 307 |
+
.all()
|
| 308 |
+
return [p.to_dict() for p in predictions]
|
| 309 |
+
|
| 310 |
@app.get("/api/debug/seed")
|
| 311 |
async def seed_database(db: Session = Depends(get_db)):
|
| 312 |
+
"""Seed cloud database with a full fleet of NASA demo assets and history."""
|
| 313 |
try:
|
| 314 |
+
db.query(HealthPrediction).delete()
|
| 315 |
+
db.query(Asset).delete()
|
| 316 |
+
|
| 317 |
+
assets_to_create = [
|
| 318 |
+
{"asset_id": "BESS_01", "name": "NASA PCoE ESS-01", "stage": "Certified", "health": 94.2, "life": 1.5, "loc": "Mountain View, CA"},
|
| 319 |
+
{"asset_id": "BESS_02", "name": "Stanford R&D Unit B2", "stage": "Monitoring", "health": 88.7, "life": 4.2, "loc": "Stanford, CA"},
|
| 320 |
+
{"asset_id": "BESS_03", "name": "Legacy Storage Cell C4", "stage": "Monitoring", "health": 68.5, "life": 0.8, "loc": "Fremont, CA"}
|
| 321 |
+
]
|
| 322 |
+
|
| 323 |
+
for a in assets_to_create:
|
| 324 |
+
uid = str(uuid.uuid4())
|
| 325 |
+
db.add(Asset(
|
| 326 |
+
id=uid, owner_id="demo_user", project_name=a["name"],
|
| 327 |
+
asset_id=a["asset_id"], location=a["loc"], capacity=100.0,
|
| 328 |
+
bms_provider="ZenVolt Native", category=json.dumps(["Grid-Scale"]),
|
| 329 |
+
project_stage=a["stage"], health_score=a["health"],
|
| 330 |
+
remaining_life=a["life"], degradation_rate=2.4,
|
| 331 |
+
confidence=0.94, is_online=1
|
| 332 |
+
))
|
| 333 |
+
|
| 334 |
+
for i in range(1, 11):
|
| 335 |
+
cycle = i * 10
|
| 336 |
+
h = a["health"] + (11-i)*0.4
|
| 337 |
+
db.add(HealthPrediction(
|
| 338 |
+
asset_id=uid, cycle_index=cycle, health_score=round(h, 2),
|
| 339 |
+
current_soh=round(h/100, 4), prediction_date=datetime.utcnow()
|
| 340 |
+
))
|
| 341 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
db.commit()
|
| 343 |
+
return {"message": "Success! Full fleet + history seeded.", "status": "201 Created"}
|
| 344 |
except Exception as e:
|
| 345 |
+
db.rollback()
|
| 346 |
return {"error": str(e)}
|
| 347 |
|
| 348 |
if __name__ == "__main__":
|