diff --git a/main.py b/main.py index fda39aa..81755b9 100644 --- a/main.py +++ b/main.py @@ -21,7 +21,7 @@ def clamp_limit(limit: int) -> int: import off from db import Base, engine, get_db -from models import Food, Recipe, TelemetryDaily +from models import Food, Recipe, TelemetryDaily, TelemetryActive # Columnas añadidas a `recipes` después de la v1 (id/name/servings/items). En @@ -278,6 +278,7 @@ def delete_cookbook(author_id: str, db: Session = Depends(get_db)): class TelemetryIn(BaseModel): day: str app_version: str | None = None + install_id: str | None = None active_modules: list[str] = [] opens_by_module: dict[str, int] = {} seconds_by_module: dict[str, int] = {} @@ -337,6 +338,12 @@ def add_telemetry(body: TelemetryIn, db: Session = Depends(get_db)): row.active = (row.active or 0) + delta["active"] row.opens = (row.opens or 0) + delta["opens"] row.seconds = (row.seconds or 0) + delta["seconds"] + + # Presencia (instalación, día) para contar instalaciones únicas. + install_id = (body.install_id or "").strip()[:64] + if install_id and db.get(TelemetryActive, (install_id, day)) is None: + db.add(TelemetryActive(install_id=install_id, day=day)) + db.commit() return {"ok": True} @@ -372,7 +379,26 @@ def telemetry_summary(days: int = 30, db: Session = Depends(get_db)): "seconds": int(seconds or 0), }) modules.sort(key=lambda x: x["opens"], reverse=True) - return {"sinceDays": days, "submissions": submissions, "modules": modules} + + # Instalaciones únicas (por install_id): total, activas en el periodo y hoy. + today = date.today().isoformat() + installs_total = db.query( + func.count(func.distinct(TelemetryActive.install_id))).scalar() or 0 + installs_active = db.query( + func.count(func.distinct(TelemetryActive.install_id)) + ).filter(TelemetryActive.day >= cutoff).scalar() or 0 + dau_today = db.query( + func.count(func.distinct(TelemetryActive.install_id)) + ).filter(TelemetryActive.day == today).scalar() or 0 + + return { + "sinceDays": days, + "submissions": submissions, + "installsTotal": int(installs_total), + "installsActive": int(installs_active), + "dauToday": int(dau_today), + "modules": modules, + } @app.post("/recipes") diff --git a/models.py b/models.py index 86638e8..764a133 100644 --- a/models.py +++ b/models.py @@ -85,3 +85,13 @@ class TelemetryDaily(Base): active = Column(Integer, default=0) opens = Column(Integer, default=0) seconds = Column(Integer, default=0) # tiempo en pantalla acumulado + + +class TelemetryActive(Base): + """Presencia (instalación, día) para contar instalaciones ÚNICAS, DAU/MAU y + retención. El install_id es un identificador anónimo de la instalación (un + número aleatorio, sin nada personal).""" + __tablename__ = "telemetry_active" + + install_id = Column(String, primary_key=True) + day = Column(String, primary_key=True) # 'YYYY-MM-DD' diff --git a/test_telemetry.py b/test_telemetry.py index 9769a05..76f8ea3 100644 --- a/test_telemetry.py +++ b/test_telemetry.py @@ -23,6 +23,7 @@ def test_telemetria_agrega_y_resume(): r1 = client.post("/telemetry", json={ "day": day, "app_version": "2.27.0", + "install_id": "inst-A", "active_modules": ["fasting", "weight", "alcohol"], "opens_by_module": {"alcohol": 3, "weight": 1}, "seconds_by_module": {"alcohol": 120, "aprende:sueno": 45}, @@ -30,14 +31,22 @@ def test_telemetria_agrega_y_resume(): assert r1.status_code < 300, r1.text r2 = client.post("/telemetry", json={ "day": day, + "install_id": "inst-B", "active_modules": ["fasting", "alcohol"], "opens_by_module": {"alcohol": 2}, "seconds_by_module": {"alcohol": 60}, }) assert r2.status_code < 300, r2.text + # Tercer envío de una instalación ya vista (inst-A): no suma instalación única. + r3 = client.post("/telemetry", json={"day": day, "install_id": "inst-A"}) + assert r3.status_code < 300, r3.text summary = client.get("/telemetry/summary").json() - assert summary["submissions"] == 2 # dos envíos ese día + assert summary["submissions"] == 3 # tres envíos ese día + # Dos instalaciones únicas (inst-A duplicada no cuenta doble), ambas hoy. + assert summary["installsTotal"] == 2 + assert summary["installsActive"] == 2 + assert summary["dauToday"] == 2 by_module = {m["module"]: m for m in summary["modules"]} # alcohol: activo en 2 envíos, abierto 3+2 = 5 veces, 120+60 = 180 s. assert by_module["alcohol"]["activeInstallDays"] == 2 @@ -49,7 +58,8 @@ def test_telemetria_agrega_y_resume(): # Versión de la app (fragmentación): el primer envío fue 2.27.0, el segundo # sin versión → 'unknown'. assert by_module["ver:2.27.0"]["activeInstallDays"] == 1 - assert by_module["ver:unknown"]["activeInstallDays"] == 1 + # r2 y r3 sin app_version → ver:unknown cuenta 2. + assert by_module["ver:unknown"]["activeInstallDays"] == 2 # fasting: activo en 2 envíos, 0 aperturas. assert by_module["fasting"]["activeInstallDays"] == 2 assert by_module["fasting"]["opens"] == 0