Skip to content

Commit 3ef6e12

Browse files
committed
check in migration changes
1 parent de73934 commit 3ef6e12

File tree

1 file changed

+16
-35
lines changed

1 file changed

+16
-35
lines changed

migrate_collections.py

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
from datetime import datetime, timedelta
22
from operator import itemgetter
33

4-
from pymongo import ASCENDING
5-
64
from opensensor.collection_apis import new_collections, old_collections
7-
8-
# Create a MongoDB client
95
from opensensor.db import get_open_sensor_db
106

117
# Access the database
@@ -17,24 +13,15 @@
1713
if not migration:
1814
db["Migration"].insert_one({"migration_name": "FreeTier", "migration_complete": False})
1915

20-
earliest_timestamp = datetime.now()
21-
latest_timestamp = datetime.min
22-
23-
for collection_name in collections_to_migrate:
24-
collection = db[collection_name]
25-
earliest_document = collection.find_one(sort=[("timestamp", ASCENDING)])
26-
latest_document = collection.find_one(sort=[("timestamp", -1)])
27-
if earliest_document and earliest_document["timestamp"] < earliest_timestamp:
28-
earliest_timestamp = earliest_document["timestamp"]
29-
if latest_document and latest_document["timestamp"] > latest_timestamp:
30-
latest_timestamp = latest_document["timestamp"]
31-
16+
earliest_timestamp = datetime(2023, 1, 1)
3217
start_date = earliest_timestamp
33-
one_week = timedelta(weeks=1)
18+
one_day = timedelta(days=1) # Change to one day
3419

35-
while start_date <= latest_timestamp:
36-
end_date = start_date + one_week
20+
while start_date <= datetime(2023, 11, 10):
21+
end_date = start_date + one_day # Use one day
3722
buffer = {}
23+
timestamps_set = set() # For faster timestamp lookups
24+
print(start_date, end_date)
3825

3926
for collection_name in collections_to_migrate:
4027
collection = db[collection_name]
@@ -44,15 +31,16 @@
4431
"metadata": {
4532
"device_id": document["metadata"]["device_id"],
4633
"name": document["metadata"].get("name"),
47-
"user_id": document.get("user_id"),
34+
"user_id": document["metadata"].get("user_id"),
4835
},
4936
new_collections[collection_name]: document.get(old_collections[collection_name]),
5037
"timestamp": document["timestamp"],
5138
}
5239
if unit:
5340
new_document[f"{new_collections[collection_name]}_unit"] = unit
5441

55-
for existing_timestamp in buffer.keys():
42+
found = False
43+
for existing_timestamp in timestamps_set:
5644
if abs(existing_timestamp - document["timestamp"]) <= timedelta(seconds=3):
5745
buffer[existing_timestamp][new_collections[collection_name]] = document.get(
5846
old_collections[collection_name]
@@ -61,25 +49,18 @@
6149
buffer[existing_timestamp][
6250
f"{new_collections[collection_name]}_unit"
6351
] = unit
52+
found = True
6453
break
65-
else:
54+
55+
if not found:
6656
buffer[document["timestamp"]] = new_document
57+
timestamps_set.add(document["timestamp"])
6758

6859
all_documents = sorted(buffer.values(), key=itemgetter("timestamp"))
69-
free_tier_collection = db["FreeTier"]
70-
for document in all_documents:
71-
free_tier_collection.insert_one(document)
7260

73-
# Update the latest_timestamp after processing this chunk, to check if new data has been added.
74-
new_latest_timestamp = datetime.min
75-
for collection_name in collections_to_migrate:
76-
collection = db[collection_name]
77-
latest_document = collection.find_one(sort=[("timestamp", -1)])
78-
if latest_document and latest_document["timestamp"] > new_latest_timestamp:
79-
new_latest_timestamp = latest_document["timestamp"]
61+
if all_documents: # Only insert if there are documents
62+
db["FreeTier"].insert_many(all_documents)
8063

81-
# If there are new records added, the while loop will continue until there are no more records.
82-
latest_timestamp = new_latest_timestamp
8364
start_date = end_date
8465

85-
# db["Migration"].update_one({"migration_name": "FreeTier"}, {"$set": {"migration_complete": True}})
66+
db["Migration"].update_one({"migration_name": "FreeTier"}, {"$set": {"migration_complete": True}})

0 commit comments

Comments
 (0)