fix: resolve stale waveform duplicates and throttle bypass for real-time streaming

This commit is contained in:
2026-07-15 15:02:17 +05:30
parent fbb9febda8
commit e417ada576
2 changed files with 21 additions and 0 deletions
+15
View File
@@ -177,6 +177,9 @@ async def process_vitals(vitals):
if device_id not in device_field_timestamps:
device_field_timestamps[device_id] = {}
# Check if incoming packet has fresh waveforms
has_fresh_waveforms = (vitals.ecg_wave is not None) or (vitals.pleth_wave is not None)
# 1. Merge new vitals into cached vitals to prevent fragmented entries
if device_id not in device_cache:
device_cache[device_id] = vitals
@@ -236,6 +239,7 @@ async def process_vitals(vitals):
logger.debug(f"WebSocket broadcast failed: {e}")
# 4. Throttled Database & REST forwarding (at most once every 5 seconds)
# Note: If we have fresh waveforms, we bypass throttle for REST forwarding to keep cloud graphs real-time
now = datetime.now(timezone.utc)
should_write_db = False
if device_id not in last_db_write or (now - last_db_write[device_id]).total_seconds() >= 5.0:
@@ -339,3 +343,14 @@ async def process_vitals(vitals):
db.rollback()
finally:
db.close()
elif has_fresh_waveforms:
# Bypass throttle for REST forwarding to keep cloud graphs real-time
try:
await forward_vitals_to_api(vitals)
except Exception as e:
logger.error(f"Real-time REST forwarding failed: {e}")
# Clear waveforms from cache to prevent repetition in subsequent non-waveform sub-packets
if device_id in device_cache:
device_cache[device_id].ecg_wave = None
device_cache[device_id].pleth_wave = None