mirror of
https://github.com/This-weiyuqing/mi-fitness-health.git
synced 2026-10-18 11:13:48 +08:00
feat: sync spo2 stress and abnormal heart beat
This commit is contained in:
1 parent
b532ee652d
commit
0ff3dd1c82
8 files changed
+596
-3
No files matched your search
@@ -301,6 +301,62 @@ class QueryService:
|
||||
return samples[:limit]
|
||||
return samples
|
||||
|
||||
|
||||
def get_spo2_samples(
|
||||
self,
|
||||
start_date: str,
|
||||
end_date: str,
|
||||
limit: int | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
records = self.db.query_spo2_samples(self.user_id, start_date, end_date)
|
||||
samples = [
|
||||
{
|
||||
"timestamp": record["timestamp"],
|
||||
"spo2_pct": record["spo2_pct"],
|
||||
}
|
||||
for record in records
|
||||
]
|
||||
return samples[:limit] if limit is not None else samples
|
||||
|
||||
def get_stress_samples(
|
||||
self,
|
||||
start_date: str,
|
||||
end_date: str,
|
||||
level: str | None = None,
|
||||
limit: int | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
records = self.db.query_stress_samples(self.user_id, start_date, end_date)
|
||||
samples = []
|
||||
for record in records:
|
||||
if level and record.get("level") != level:
|
||||
continue
|
||||
samples.append(
|
||||
{
|
||||
"timestamp": record["timestamp"],
|
||||
"stress_score": record["stress_score"],
|
||||
"level": record["level"],
|
||||
}
|
||||
)
|
||||
return samples[:limit] if limit is not None else samples
|
||||
|
||||
def get_abnormal_heart_beat_events(
|
||||
self,
|
||||
start_date: str,
|
||||
end_date: str,
|
||||
limit: int | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
records = self.db.query_abnormal_heart_beat_events(self.user_id, start_date, end_date)
|
||||
events = [
|
||||
{
|
||||
"event_id": record["event_id"],
|
||||
"start_at": record["start_at"],
|
||||
"end_at": record["end_at"],
|
||||
"duration_seconds": record["duration_seconds"],
|
||||
}
|
||||
for record in records
|
||||
]
|
||||
return events[:limit] if limit is not None else events
|
||||
|
||||
def get_data_coverage(self, data_types: list[str] | None = None) -> list[dict[str, Any]]:
|
||||
"""Get data coverage information."""
|
||||
coverage = self.db.get_data_coverage(self.user_id)
|
||||
|
||||
@@ -123,6 +123,36 @@ class SyncService:
|
||||
if last_ts is None or sample.timestamp > last_ts:
|
||||
last_ts = sample.timestamp
|
||||
|
||||
elif data_type == "spo2":
|
||||
records = self.adapter.iter_spo2(start_date, end_date)
|
||||
async for sample in self._iterate_records(records):
|
||||
if self.db.insert_spo2_sample(sample):
|
||||
added += 1
|
||||
else:
|
||||
updated += 1
|
||||
if last_ts is None or sample.timestamp > last_ts:
|
||||
last_ts = sample.timestamp
|
||||
|
||||
elif data_type == "stress":
|
||||
records = self.adapter.iter_stress(start_date, end_date)
|
||||
async for sample in self._iterate_records(records):
|
||||
if self.db.insert_stress_sample(sample):
|
||||
added += 1
|
||||
else:
|
||||
updated += 1
|
||||
if last_ts is None or sample.timestamp > last_ts:
|
||||
last_ts = sample.timestamp
|
||||
|
||||
elif data_type == "abnormal_heart_beat":
|
||||
records = self.adapter.iter_abnormal_heart_beat(start_date, end_date)
|
||||
async for event in self._iterate_records(records):
|
||||
if self.db.insert_abnormal_heart_beat_event(event):
|
||||
added += 1
|
||||
else:
|
||||
updated += 1
|
||||
if last_ts is None or event.start_at > last_ts:
|
||||
last_ts = event.start_at
|
||||
|
||||
else:
|
||||
raise ValueError(f"Unknown data type: {data_type}")
|
||||
|
||||
|
||||
Reference in new issue
Block a user