|
| 1 | +"""Script to backfill policy versions for all checkpoints in the database.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import logging |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +from .database import Database |
| 8 | +from .services import ObservatoryService |
| 9 | + |
| 10 | +logging.basicConfig(level=logging.INFO) |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +async def backfill_checkpoint_versions(db_path: str = "skydeck.db"): |
| 15 | + """Backfill policy versions for all checkpoints missing them. |
| 16 | +
|
| 17 | + Args: |
| 18 | + db_path: Path to the SQLite database |
| 19 | + """ |
| 20 | + db = Database(db_path) |
| 21 | + await db.connect() |
| 22 | + |
| 23 | + try: |
| 24 | + # Get all experiments |
| 25 | + experiments = await db.get_all_experiments() |
| 26 | + logger.info(f"Found {len(experiments)} experiments") |
| 27 | + |
| 28 | + total_checkpoints = 0 |
| 29 | + updated_checkpoints = 0 |
| 30 | + |
| 31 | + for exp in experiments: |
| 32 | + logger.info(f"Processing experiment: {exp.id}") |
| 33 | + |
| 34 | + # Get all checkpoints for this experiment (no limit) |
| 35 | + cursor = await db._conn.execute( |
| 36 | + """ |
| 37 | + SELECT * FROM checkpoints |
| 38 | + WHERE experiment_id = ? |
| 39 | + ORDER BY epoch DESC |
| 40 | + """, |
| 41 | + (exp.id,), |
| 42 | + ) |
| 43 | + rows = await cursor.fetchall() |
| 44 | + checkpoints = [db._row_to_checkpoint(row) for row in rows] |
| 45 | + |
| 46 | + total_checkpoints += len(checkpoints) |
| 47 | + logger.info(f" Found {len(checkpoints)} checkpoints") |
| 48 | + |
| 49 | + # Filter checkpoints that need backfill |
| 50 | + needs_backfill = [cp for cp in checkpoints if not cp.policy_version] |
| 51 | + |
| 52 | + if not needs_backfill: |
| 53 | + logger.info(" All checkpoints have versions, skipping") |
| 54 | + continue |
| 55 | + |
| 56 | + logger.info(f" {len(needs_backfill)} checkpoints need version backfill") |
| 57 | + |
| 58 | + # Fetch policy version once per experiment |
| 59 | + policy_version = ObservatoryService.fetch_policy_version(exp.id) |
| 60 | + observatory_url = ObservatoryService.get_policy_api_url(exp.id, limit=500) |
| 61 | + |
| 62 | + if policy_version: |
| 63 | + logger.info(f" Found policy version: {policy_version}") |
| 64 | + else: |
| 65 | + logger.warning(f" Could not fetch policy version for {exp.id}") |
| 66 | + |
| 67 | + # Update all checkpoints that need backfill |
| 68 | + for cp in needs_backfill: |
| 69 | + # Update the checkpoint fields |
| 70 | + cp.policy_version = policy_version |
| 71 | + cp.observatory_url = observatory_url |
| 72 | + cp.synced_at = datetime.utcnow() |
| 73 | + |
| 74 | + # Save the updated checkpoint |
| 75 | + await db.save_checkpoint(cp) |
| 76 | + updated_checkpoints += 1 |
| 77 | + |
| 78 | + logger.info(f" Updated {len(needs_backfill)} checkpoints") |
| 79 | + |
| 80 | + logger.info("\nBackfill complete:") |
| 81 | + logger.info(f" Total checkpoints: {total_checkpoints}") |
| 82 | + logger.info(f" Updated checkpoints: {updated_checkpoints}") |
| 83 | + |
| 84 | + finally: |
| 85 | + await db.close() |
| 86 | + |
| 87 | + |
| 88 | +async def main(): |
| 89 | + """Main entry point.""" |
| 90 | + await backfill_checkpoint_versions() |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + asyncio.run(main()) |
0 commit comments