fix: auto-detect working directory, install venv, configure ufw firewall, and sync Option B vitals in setup_service.sh
This commit is contained in:
@@ -122,6 +122,45 @@ async def sync_cloud_state():
|
||||
"patient_type": getattr(p, "patient_type", None)
|
||||
}
|
||||
await sync_patient_assignment_to_cloud(p_data, p.device_id)
|
||||
|
||||
# 2. Backfill and forward latest complete vitals to Render (Option B)
|
||||
from sqlalchemy import or_
|
||||
from datetime import datetime, timezone
|
||||
patient_filters = or_(
|
||||
VitalReading.patient_id == p.mrn,
|
||||
VitalReading.patient_id == p.patient_id,
|
||||
VitalReading.device_id == p.device_id
|
||||
)
|
||||
recent_r = db.query(VitalReading).filter(patient_filters).order_by(VitalReading.timestamp.desc()).first()
|
||||
if recent_r:
|
||||
spo2_r = db.query(VitalReading.spo2).filter(patient_filters, VitalReading.spo2 != None).order_by(VitalReading.timestamp.desc()).first()
|
||||
hr_r = db.query(VitalReading.heart_rate).filter(patient_filters, VitalReading.heart_rate != None).order_by(VitalReading.timestamp.desc()).first()
|
||||
bp_r = db.query(VitalReading.systolic_bp, VitalReading.diastolic_bp, VitalReading.map_bp).filter(patient_filters, VitalReading.systolic_bp != None).order_by(VitalReading.timestamp.desc()).first()
|
||||
temp_r = db.query(VitalReading.temperature).filter(patient_filters, VitalReading.temperature != None).order_by(VitalReading.timestamp.desc()).first()
|
||||
temp2_r = db.query(VitalReading.temperature2).filter(patient_filters, VitalReading.temperature2 != None).order_by(VitalReading.timestamp.desc()).first()
|
||||
temp_diff_r = db.query(VitalReading.temperature_diff).filter(patient_filters, VitalReading.temperature_diff != None).order_by(VitalReading.timestamp.desc()).first()
|
||||
rr_r = db.query(VitalReading.respiratory_rate).filter(patient_filters, VitalReading.respiratory_rate != None).order_by(VitalReading.timestamp.desc()).first()
|
||||
|
||||
merged = NormalizedVitals(
|
||||
device_id=p.device_id,
|
||||
patient_id=p.mrn or p.patient_id,
|
||||
patient_name=p.name,
|
||||
patient_gender=p.gender,
|
||||
patient_dob=p.dob,
|
||||
bed_id=p.bed_id,
|
||||
patient_type=getattr(p, "patient_type", None),
|
||||
timestamp=recent_r.timestamp or datetime.now(timezone.utc),
|
||||
heart_rate=hr_r[0] if hr_r else None,
|
||||
spo2=spo2_r[0] if spo2_r else None,
|
||||
systolic_bp=bp_r[0] if bp_r else None,
|
||||
diastolic_bp=bp_r[1] if bp_r else None,
|
||||
map_bp=bp_r[2] if bp_r else None,
|
||||
temperature=temp_r[0] if temp_r else None,
|
||||
temperature2=temp2_r[0] if temp2_r else None,
|
||||
temperature_diff=temp_diff_r[0] if temp_diff_r else None,
|
||||
respiratory_rate=rr_r[0] if rr_r else None
|
||||
)
|
||||
await forward_vitals_to_api(merged)
|
||||
finally:
|
||||
db.close()
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user