feat: automatically forward merged non-null vitals (HR, SpO2, Temp, BP) during cloud sync
This commit is contained in:
@@ -122,6 +122,44 @@ async def sync_cloud_state():
|
|||||||
"patient_type": getattr(p, "patient_type", None)
|
"patient_type": getattr(p, "patient_type", None)
|
||||||
}
|
}
|
||||||
await sync_patient_assignment_to_cloud(p_data, p.device_id)
|
await sync_patient_assignment_to_cloud(p_data, p.device_id)
|
||||||
|
|
||||||
|
# 2. Forward latest non-null 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
|
||||||
|
)
|
||||||
|
hr_r = db.query(VitalReading.heart_rate).filter(patient_filters, VitalReading.heart_rate != None).order_by(VitalReading.timestamp.desc()).first()
|
||||||
|
spo2_r = db.query(VitalReading.spo2).filter(patient_filters, VitalReading.spo2 != 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()
|
||||||
|
|
||||||
|
if hr_r or spo2_r or temp_r or bp_r:
|
||||||
|
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=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:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ async def sync_cloud_state():
|
|||||||
}
|
}
|
||||||
await sync_patient_assignment_to_cloud(p_data, p.device_id)
|
await sync_patient_assignment_to_cloud(p_data, p.device_id)
|
||||||
|
|
||||||
# 2. Backfill and forward latest complete vitals to Render (Option B)
|
# 2. Forward latest non-null vitals to Render (Option B)
|
||||||
from sqlalchemy import or_
|
from sqlalchemy import or_
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
patient_filters = or_(
|
patient_filters = or_(
|
||||||
@@ -131,16 +131,15 @@ async def sync_cloud_state():
|
|||||||
VitalReading.patient_id == p.patient_id,
|
VitalReading.patient_id == p.patient_id,
|
||||||
VitalReading.device_id == p.device_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()
|
hr_r = db.query(VitalReading.heart_rate).filter(patient_filters, VitalReading.heart_rate != None).order_by(VitalReading.timestamp.desc()).first()
|
||||||
|
spo2_r = db.query(VitalReading.spo2).filter(patient_filters, VitalReading.spo2 != 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()
|
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()
|
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()
|
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()
|
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()
|
rr_r = db.query(VitalReading.respiratory_rate).filter(patient_filters, VitalReading.respiratory_rate != None).order_by(VitalReading.timestamp.desc()).first()
|
||||||
|
|
||||||
|
if hr_r or spo2_r or temp_r or bp_r:
|
||||||
merged = NormalizedVitals(
|
merged = NormalizedVitals(
|
||||||
device_id=p.device_id,
|
device_id=p.device_id,
|
||||||
patient_id=p.mrn or p.patient_id,
|
patient_id=p.mrn or p.patient_id,
|
||||||
@@ -149,7 +148,7 @@ async def sync_cloud_state():
|
|||||||
patient_dob=p.dob,
|
patient_dob=p.dob,
|
||||||
bed_id=p.bed_id,
|
bed_id=p.bed_id,
|
||||||
patient_type=getattr(p, "patient_type", None),
|
patient_type=getattr(p, "patient_type", None),
|
||||||
timestamp=recent_r.timestamp or datetime.now(timezone.utc),
|
timestamp=datetime.now(timezone.utc),
|
||||||
heart_rate=hr_r[0] if hr_r else None,
|
heart_rate=hr_r[0] if hr_r else None,
|
||||||
spo2=spo2_r[0] if spo2_r else None,
|
spo2=spo2_r[0] if spo2_r else None,
|
||||||
systolic_bp=bp_r[0] if bp_r else None,
|
systolic_bp=bp_r[0] if bp_r else None,
|
||||||
|
|||||||
Reference in New Issue
Block a user