Spaces:
Running

File size: 7,193 Bytes
5655786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""
Upload Synthetic Trajectory Data to MongoDB Atlas (with Vector Embeddings)
===========================================================================
This script uploads the synthetic_trajectories.json dataset to MongoDB Atlas
AND generates Gemini embedding vectors for each trajectory record.

The embeddings enable Atlas Vector Search β€” converting your MongoDB database
from a plain JSON store into a semantic similarity search engine.

What this script does:
1. Reads synthetic_trajectories.json (1,000 episodes, ~3,000 trajectory records)
2. For each trajectory record, generates a 768-dim embedding via Gemini API
3. Inserts all records WITH their embedding vectors into MongoDB
4. Creates the 'trajectories' flat collection (unnested from episodes)
5. Also stores the full episode documents in 'episodes' collection

Collections created:
- libratio.trajectories  β€” flat trajectory records with embedding vectors (for Vector Search)
- libratio.episodes      β€” full episode documents with all trajectories nested
- libratio.runs          β€” live inference run telemetry (written by fleet_inference.py)

Usage:
    python upload_to_mongodb.py

Note: Embedding 3,000 trajectories takes ~5 minutes (Gemini API rate limits).
      A progress bar is shown as embeddings are generated.
"""

import os
import json
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from pymongo import MongoClient
import certifi
from dotenv import load_dotenv
from mongodb_vector import embed_trajectory, trajectory_to_text, EMBEDDING_DIM, _GEMINI_AVAILABLE

load_dotenv()

MONGO_URI = os.getenv("MONGO_URI")
DB_NAME = "libratio"
TRAJECTORIES_COLLECTION = "trajectories"
EPISODES_COLLECTION = "episodes"
BATCH_SIZE = 50       # Insert this many documents at a time
EMBED_BATCH_DELAY = 0.1  # Seconds between batches to avoid API rate limits


def upload_data():
    print("=" * 60)
    print("  MongoDB Atlas Upload β€” With Vector Embeddings")
    print("=" * 60)

    if not MONGO_URI:
        print("[ERROR] MONGO_URI not set in .env")
        return

    # ── Connect ──
    print("\n[1/5] Connecting to MongoDB Atlas...")
    client = MongoClient(MONGO_URI, tlsCAFile=certifi.where(), serverSelectionTimeoutMS=10000)
    try:
        client.admin.command("ping")
        print("  [OK] Connected")
    except Exception as e:
        print(f"  [ERROR] Connection failed: {e}")
        return

    db = client[DB_NAME]
    trajectories_col = db[TRAJECTORIES_COLLECTION]
    episodes_col = db[EPISODES_COLLECTION]

    # ── Read dataset ──
    print("\n[2/5] Reading synthetic dataset...")
    with open("synthetic_trajectories.json", "r") as f:
        dataset = json.load(f)

    total_episodes = len(dataset)
    total_trajs = sum(len(ep.get("trajectories", [])) for ep in dataset)
    print(f"  Episodes: {total_episodes}")
    print(f"  Trajectory records: {total_trajs}")
    print(f"  Embedding model: {'Gemini text-embedding-004 (semantic)' if _GEMINI_AVAILABLE else 'Fallback hash (no Gemini key)'}")
    print(f"  Embedding dimensions: {EMBEDDING_DIM}")

    # ── Prepare flat trajectory documents with embeddings ──
    print(f"\n[3/5] Generating embeddings for {total_trajs} trajectory records in parallel...")
    print("  Accelerated using ThreadPoolExecutor for concurrent Gemini API requests.")

    flat_trajectories = []
    for ep in dataset:
        episode_id = ep.get("episode_id", 0)
        cluster_capacity = ep.get("cluster_capacity_gb", 0)
        episode_memory_overflow = ep.get("memory_overflow", False)

        for traj in ep.get("trajectories", []):
            enriched = {
                **traj,
                "episode_id": episode_id,
                "cluster_capacity_gb": cluster_capacity,
                "episode_memory_overflow": episode_memory_overflow,
            }
            flat_trajectories.append(enriched)

    start_time = time.time()
    processed = 0

    def process_record(index, record):
        record["embedding"] = embed_trajectory(record)
        record["embedding_source_text"] = trajectory_to_text(record)
        return index, record

    # Process records concurrently in a thread pool (max 20 workers to balance speed & rate limits)
    with ThreadPoolExecutor(max_workers=20) as executor:
        futures = {executor.submit(process_record, i, rec): i for i, rec in enumerate(flat_trajectories)}
        
        for future in as_completed(futures):
            index, record = future.result()
            flat_trajectories[index] = record
            processed += 1

            # Progress update every 100 records
            if processed % 100 == 0:
                elapsed = time.time() - start_time
                rate = processed / elapsed
                remaining = (total_trajs - processed) / rate
                print(f"  [{processed}/{total_trajs}] "
                      f"{processed/total_trajs*100:.0f}% β€” "
                      f"{rate:.1f} records/sec β€” "
                      f"~{remaining:.0f}s remaining")

    elapsed_total = time.time() - start_time
    print(f"\n  [OK] Generated {len(flat_trajectories)} embeddings in {elapsed_total:.0f}s")

    # ── Upload trajectories (with embeddings) ──
    print(f"\n[4/5] Uploading to MongoDB Atlas...")

    # Drop and recreate collections for a clean state
    # (safe to do β€” this is synthetic training data, not production data)
    print(f"  Clearing existing 'trajectories' collection...")
    trajectories_col.drop()

    print(f"  Inserting {len(flat_trajectories)} trajectory documents with embeddings...")
    inserted_count = 0
    for i in range(0, len(flat_trajectories), BATCH_SIZE):
        batch = flat_trajectories[i:i + BATCH_SIZE]
        result = trajectories_col.insert_many(batch)
        inserted_count += len(result.inserted_ids)

    print(f"  [OK] Inserted {inserted_count} trajectory records into 'trajectories'")

    # ── Upload episodes ──
    print(f"\n  Uploading {total_episodes} full episode documents to 'episodes'...")
    episodes_col.drop()
    result = episodes_col.insert_many(dataset)
    print(f"  [OK] Inserted {len(result.inserted_ids)} episodes into 'episodes'")

    # ── Print stats ──
    print(f"\n[5/5] Upload Summary")
    print("-" * 40)
    total_crashes = sum(
        1 for t in flat_trajectories if t.get("outcome") != "SUCCESS"
    )
    total_success = len(flat_trajectories) - total_crashes
    has_embeddings = sum(1 for t in flat_trajectories if t.get("embedding"))
    thermal_high = sum(1 for t in flat_trajectories if t.get("thermal_risk") == "HIGH")

    print(f"  Trajectories uploaded : {inserted_count}")
    print(f"  With embeddings       : {has_embeddings}")
    print(f"  Successful outcomes   : {total_success}")
    print(f"  Crash outcomes        : {total_crashes}")
    print(f"  High thermal risk     : {thermal_high}")
    print(f"  Episodes uploaded     : {len(result.inserted_ids)}")
    print()
    print("  [OK] MongoDB is now ready for Atlas Vector Search!")
    print("  Next step: python setup_vector_index.py")
    print("=" * 60)

    client.close()


if __name__ == "__main__":
    upload_data()