telemetría: instalaciones únicas con id anónimo de instalación
Nueva tabla telemetry_active(install_id, day) (create_all la crea). add_telemetry hace upsert (install_id, day) si viene install_id. El summary añade installsTotal (únicas de siempre), installsActive (en el periodo) y dauToday. Tests: dos ids únicos + uno duplicado → 2 únicas. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
30
main.py
30
main.py
@@ -21,7 +21,7 @@ def clamp_limit(limit: int) -> int:
|
|||||||
|
|
||||||
import off
|
import off
|
||||||
from db import Base, engine, get_db
|
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
|
# 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):
|
class TelemetryIn(BaseModel):
|
||||||
day: str
|
day: str
|
||||||
app_version: str | None = None
|
app_version: str | None = None
|
||||||
|
install_id: str | None = None
|
||||||
active_modules: list[str] = []
|
active_modules: list[str] = []
|
||||||
opens_by_module: dict[str, int] = {}
|
opens_by_module: dict[str, int] = {}
|
||||||
seconds_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.active = (row.active or 0) + delta["active"]
|
||||||
row.opens = (row.opens or 0) + delta["opens"]
|
row.opens = (row.opens or 0) + delta["opens"]
|
||||||
row.seconds = (row.seconds or 0) + delta["seconds"]
|
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()
|
db.commit()
|
||||||
return {"ok": True}
|
return {"ok": True}
|
||||||
|
|
||||||
@@ -372,7 +379,26 @@ def telemetry_summary(days: int = 30, db: Session = Depends(get_db)):
|
|||||||
"seconds": int(seconds or 0),
|
"seconds": int(seconds or 0),
|
||||||
})
|
})
|
||||||
modules.sort(key=lambda x: x["opens"], reverse=True)
|
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")
|
@app.post("/recipes")
|
||||||
|
|||||||
10
models.py
10
models.py
@@ -85,3 +85,13 @@ class TelemetryDaily(Base):
|
|||||||
active = Column(Integer, default=0)
|
active = Column(Integer, default=0)
|
||||||
opens = Column(Integer, default=0)
|
opens = Column(Integer, default=0)
|
||||||
seconds = Column(Integer, default=0) # tiempo en pantalla acumulado
|
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'
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ def test_telemetria_agrega_y_resume():
|
|||||||
r1 = client.post("/telemetry", json={
|
r1 = client.post("/telemetry", json={
|
||||||
"day": day,
|
"day": day,
|
||||||
"app_version": "2.27.0",
|
"app_version": "2.27.0",
|
||||||
|
"install_id": "inst-A",
|
||||||
"active_modules": ["fasting", "weight", "alcohol"],
|
"active_modules": ["fasting", "weight", "alcohol"],
|
||||||
"opens_by_module": {"alcohol": 3, "weight": 1},
|
"opens_by_module": {"alcohol": 3, "weight": 1},
|
||||||
"seconds_by_module": {"alcohol": 120, "aprende:sueno": 45},
|
"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
|
assert r1.status_code < 300, r1.text
|
||||||
r2 = client.post("/telemetry", json={
|
r2 = client.post("/telemetry", json={
|
||||||
"day": day,
|
"day": day,
|
||||||
|
"install_id": "inst-B",
|
||||||
"active_modules": ["fasting", "alcohol"],
|
"active_modules": ["fasting", "alcohol"],
|
||||||
"opens_by_module": {"alcohol": 2},
|
"opens_by_module": {"alcohol": 2},
|
||||||
"seconds_by_module": {"alcohol": 60},
|
"seconds_by_module": {"alcohol": 60},
|
||||||
})
|
})
|
||||||
assert r2.status_code < 300, r2.text
|
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()
|
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"]}
|
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.
|
# alcohol: activo en 2 envíos, abierto 3+2 = 5 veces, 120+60 = 180 s.
|
||||||
assert by_module["alcohol"]["activeInstallDays"] == 2
|
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
|
# Versión de la app (fragmentación): el primer envío fue 2.27.0, el segundo
|
||||||
# sin versión → 'unknown'.
|
# sin versión → 'unknown'.
|
||||||
assert by_module["ver:2.27.0"]["activeInstallDays"] == 1
|
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.
|
# fasting: activo en 2 envíos, 0 aperturas.
|
||||||
assert by_module["fasting"]["activeInstallDays"] == 2
|
assert by_module["fasting"]["activeInstallDays"] == 2
|
||||||
assert by_module["fasting"]["opens"] == 0
|
assert by_module["fasting"]["opens"] == 0
|
||||||
|
|||||||
Reference in New Issue
Block a user