telemetría anónima: POST /telemetry (contadores agregados) + GET /telemetry/summary

Tabla telemetry_daily (day, module, active, opens) sin identificador de usuario
ni contenido: solo cuenta cuántas instalaciones tienen cada módulo activo y
cuántas veces lo abren (fila __submissions__ = envíos del día). El POST agrega
por módulo antes de tocar la BD (evita PK duplicada intra-request). create_all
crea la tabla nueva; no necesita migración ALTER. Tests en SQLite en memoria.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Carlos Narro
2026-07-03 21:27:39 +02:00
parent 9157d12f67
commit 3092e19b73
3 changed files with 154 additions and 1 deletions

52
test_telemetry.py Normal file
View File

@@ -0,0 +1,52 @@
"""Tests de telemetría anónima: el POST agrega contadores por día/módulo y el
summary los devuelve. SQLite en memoria. Sin identificador de usuario."""
import os
os.environ["DATABASE_URL"] = "sqlite://"
from datetime import date # noqa: E402
from fastapi.testclient import TestClient # noqa: E402
from main import app # noqa: E402
client = TestClient(app)
def _today() -> str:
return date.today().isoformat()
def test_telemetria_agrega_y_resume():
day = _today()
# Dos envíos de "instalaciones" distintas el mismo día.
r1 = client.post("/telemetry", json={
"day": day,
"app_version": "2.27.0",
"active_modules": ["fasting", "weight", "alcohol"],
"opens_by_module": {"alcohol": 3, "weight": 1},
})
assert r1.status_code < 300, r1.text
r2 = client.post("/telemetry", json={
"day": day,
"active_modules": ["fasting", "alcohol"],
"opens_by_module": {"alcohol": 2},
})
assert r2.status_code < 300, r2.text
summary = client.get("/telemetry/summary").json()
assert summary["submissions"] == 2 # dos envíos ese día
by_module = {m["module"]: m for m in summary["modules"]}
# alcohol: activo en 2 envíos, abierto 3+2 = 5 veces.
assert by_module["alcohol"]["activeInstallDays"] == 2
assert by_module["alcohol"]["opens"] == 5
# fasting: activo en 2 envíos, 0 aperturas.
assert by_module["fasting"]["activeInstallDays"] == 2
assert by_module["fasting"]["opens"] == 0
# No se filtra ningún identificador ni contenido: solo módulos.
assert "__submissions__" not in by_module
def test_day_invalido_rechazado():
r = client.post("/telemetry", json={"day": "hoy"})
assert r.status_code == 400