Skip to content

Commit bfdcd7f

Browse files
committed
Latest learnings from running the migration logic
1 parent 3ef6e12 commit bfdcd7f

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

migrate_collections.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,26 @@
77
# Access the database
88
db = get_open_sensor_db()
99

10-
collections_to_migrate = ["Temperature", "Humidity", "Pressure", "Lux", "CO2", "PH", "Moisture"]
10+
collections_to_migrate = ["Temperature", "Humidity", "Pressure", "Lux", "CO2", "pH", "Moisture"]
1111

1212
migration = db.Migration.find_one({"migration_name": "FreeTier"})
1313
if not migration:
1414
db["Migration"].insert_one({"migration_name": "FreeTier", "migration_complete": False})
1515

1616
earliest_timestamp = datetime(2023, 1, 1)
1717
start_date = earliest_timestamp
18-
one_day = timedelta(days=1) # Change to one day
18+
one_day = timedelta(days=1)
19+
20+
21+
# Function to create a composite key
22+
def create_key(timestamp, metadata):
23+
return f"{timestamp}_{metadata['device_id']}_{metadata.get('name', 'NA')}_{metadata.get('user_id', 'NA')}"
24+
1925

2026
while start_date <= datetime(2023, 11, 10):
21-
end_date = start_date + one_day # Use one day
27+
end_date = start_date + one_day
2228
buffer = {}
23-
timestamps_set = set() # For faster timestamp lookups
29+
2430
print(start_date, end_date)
2531

2632
for collection_name in collections_to_migrate:
@@ -39,28 +45,24 @@
3945
if unit:
4046
new_document[f"{new_collections[collection_name]}_unit"] = unit
4147

42-
found = False
43-
for existing_timestamp in timestamps_set:
44-
if abs(existing_timestamp - document["timestamp"]) <= timedelta(seconds=3):
45-
buffer[existing_timestamp][new_collections[collection_name]] = document.get(
46-
old_collections[collection_name]
47-
)
48-
if unit:
49-
buffer[existing_timestamp][
50-
f"{new_collections[collection_name]}_unit"
51-
] = unit
52-
found = True
53-
break
54-
55-
if not found:
56-
buffer[document["timestamp"]] = new_document
57-
timestamps_set.add(document["timestamp"])
48+
key = create_key(document["timestamp"], document["metadata"])
49+
50+
if key in buffer:
51+
buffer[key][new_collections[collection_name]] = document.get(
52+
old_collections[collection_name]
53+
)
54+
if unit:
55+
buffer[key][f"{new_collections[collection_name]}_unit"] = unit
56+
else:
57+
buffer[key] = new_document
5858

5959
all_documents = sorted(buffer.values(), key=itemgetter("timestamp"))
6060

61-
if all_documents: # Only insert if there are documents
61+
# Insert the batch of documents for the current day
62+
if all_documents:
6263
db["FreeTier"].insert_many(all_documents)
6364

65+
# Move to the next day
6466
start_date = end_date
6567

6668
db["Migration"].update_one({"migration_name": "FreeTier"}, {"$set": {"migration_complete": True}})

0 commit comments

Comments
 (0)