"""Realistic fake data for a newborn, pushed through the real sync code path (as if a Mamadachi had been in use for N days). For demos and testing only.""" import random from datetime import datetime, timedelta from sqlalchemy import select from sqlalchemy.orm import Session from .models import Event from .schemas import DeviceEventIn, SyncRequest from .services.catalog import DEFAULT_BUTTONS, get_profile from .services.ingest import create_device, ingest from .timeutil import safe_zone, unix, utcnow BUTTON = {slug: n for n, slug in enumerate(DEFAULT_BUTTONS, start=1)} def _newborn_presses(start: datetime, end: datetime, tz, rng: random.Random) -> list[tuple[datetime, int]]: # noqa: ANN001 presses: list[tuple[datetime, int]] = [] t = start side = "nurse_left" last_pump = start last_vitamin_day = None while t < end: hour = t.astimezone(tz).hour night = hour >= 22 or hour < 6 # -- feed if rng.random() < 0.85: presses.append((t, BUTTON[side])) other = "nurse_right" if side == "nurse_left" else "nurse_left" if rng.random() < 0.8: presses.append((t + timedelta(minutes=rng.randint(12, 22)), BUTTON[other])) side = other else: presses.append((t, BUTTON["bottle"])) # -- diaper change = t + timedelta(minutes=rng.randint(20, 35)) if rng.random() < 0.85: presses.append((change, BUTTON["wet"])) if rng.random() < (0.25 if night else 0.4): presses.append((change + timedelta(seconds=20), BUTTON["dirty"])) # -- vitamin D once a day after the first morning feed local = t.astimezone(tz) if local.hour >= 8 and last_vitamin_day != local.date(): presses.append((t + timedelta(minutes=40), BUTTON["medicine"])) last_vitamin_day = local.date() # -- pump a couple of times a day if not night and t - last_pump > timedelta(hours=rng.uniform(7, 10)): presses.append((t + timedelta(minutes=50), BUTTON["pump"])) last_pump = t # -- sleep until shortly before the next feed gap = timedelta(hours=rng.uniform(3.2, 4.4) if night else rng.uniform(2.2, 3.3)) nxt = t + gap sleep_start = t + timedelta(minutes=rng.randint(45, 70)) sleep_end = nxt - timedelta(minutes=rng.randint(4, 15)) if sleep_end - sleep_start > timedelta(minutes=20): presses.append((sleep_start, BUTTON["sleep"])) if sleep_end < end: presses.append((sleep_end, BUTTON["sleep"])) t = nxt presses.sort() return [(when, b) for when, b in presses if when < end] def seed_demo(db: Session, days: int = 21, seed: int = 7) -> int: """Create a "Demo Mamadachi" and replay `days` of presses through hourly syncs.""" rng = random.Random(seed) profile = get_profile(db) tz = safe_zone(profile.timezone) now = utcnow().replace(microsecond=0) start = now - timedelta(days=days) presses = _newborn_presses(start, now - timedelta(minutes=10), tz, rng) device, _token = create_device(db, "Demo Mamadachi") seq = 0 battery = 4150.0 i = 0 sync_at = start + timedelta(hours=1) while sync_at <= now: batch: list[DeviceEventIn] = [] while i < len(presses) and presses[i][0] <= sync_at: seq += 1 batch.append(DeviceEventIn(seq=seq, time=unix(presses[i][0]), button=presses[i][1])) i += 1 battery -= rng.uniform(0.3, 0.9) # ~15 mV a day charging = False if battery < 3720: # a recharge partway through battery, charging = 4180.0, True req = SyncRequest( device_time=unix(sync_at) - rng.randint(0, 2), firmware="0.1.0-demo", battery_mv=round(battery + rng.uniform(-6, 6)), charging=charging, rssi=rng.randint(-72, -52), boot_count=seq, queue_len=0, events=batch, ) ingest(db, device, req, received_at=sync_at + timedelta(seconds=rng.randint(1, 4))) sync_at += timedelta(minutes=profile.sync_interval_min) # A couple of notes, like a parent would add. for ev in db.scalars(select(Event).where(Event.kind_slug == "bottle").limit(6)): ev.note = f"{rng.choice([60, 75, 90, 90, 120])} ml" db.commit() return seq