Backfill push and feedback behavior logs for one user.
(user_id: str, *, dry_run: bool = False)
| 37 | |
| 38 | |
| 39 | def backfill_user(user_id: str, *, dry_run: bool = False) -> Dict[str, Any]: |
| 40 | """Backfill push and feedback behavior logs for one user.""" |
| 41 | wiki_db.init_wiki_schema() |
| 42 | counts: Dict[str, Any] = {"pushed": 0, "feedback": 0, "topics": 0, "embedded": 0} |
| 43 | missing_tables = [ |
| 44 | table for table in ("behavior_logs", "papers") if not wiki_db.table_exists(table) |
| 45 | ] |
| 46 | if missing_tables: |
| 47 | counts["missing_tables"] = missing_tables |
| 48 | return counts |
| 49 | |
| 50 | conn = db_ops.get_connection() |
| 51 | rows = conn.execute( |
| 52 | """ |
| 53 | SELECT bl.id AS behavior_log_id, bl.user_id, bl.push_id, bl.paper_id, |
| 54 | bl.action, bl.action_type, bl.category, bl.timestamp, |
| 55 | bl.metadata AS behavior_metadata, |
| 56 | p.* |
| 57 | FROM behavior_logs bl |
| 58 | LEFT JOIN papers p ON p.id = bl.paper_id |
| 59 | WHERE bl.user_id = ? |
| 60 | ORDER BY bl.id ASC |
| 61 | """, |
| 62 | (user_id,), |
| 63 | ).fetchall() |
| 64 | conn.close() |
| 65 | |
| 66 | for row in rows: |
| 67 | metadata = _load_json(row["behavior_metadata"]) |
| 68 | paper = _paper_from_row(row) |
| 69 | action = str(row["action"] or "") |
| 70 | action_type = str(row["action_type"] or "") |
| 71 | if dry_run: |
| 72 | if action == "pushed": |
| 73 | counts["pushed"] += 1 |
| 74 | elif action in {"selected", "skipped", "created_report"}: |
| 75 | counts["feedback"] += 1 |
| 76 | continue |
| 77 | if action == "pushed": |
| 78 | daily_ingest.ingest_pushed_paper( |
| 79 | user_id=user_id, |
| 80 | push_id=row["push_id"], |
| 81 | paper=paper, |
| 82 | category=row["category"], |
| 83 | metadata={**metadata, "timestamp": row["timestamp"]}, |
| 84 | behavior_log_id=row["behavior_log_id"], |
| 85 | ) |
| 86 | counts["pushed"] += 1 |
| 87 | elif action in {"selected", "skipped", "created_report"}: |
| 88 | feedback_ingest.ingest_feedback_event( |
| 89 | user_id=user_id, |
| 90 | push_id=row["push_id"], |
| 91 | paper=paper, |
| 92 | action=action, |
| 93 | action_type=action_type, |
| 94 | category=row["category"], |
| 95 | metadata=metadata, |
| 96 | behavior_log_id=row["behavior_log_id"], |
no test coverage detected