|
7 | 7 | # Access the database
|
8 | 8 | db = get_open_sensor_db()
|
9 | 9 |
|
10 |
| -collections_to_migrate = ["Temperature", "Humidity", "Pressure", "Lux", "CO2", "PH", "Moisture"] |
| 10 | +collections_to_migrate = ["Temperature", "Humidity", "Pressure", "Lux", "CO2", "pH", "Moisture"] |
11 | 11 |
|
12 | 12 | migration = db.Migration.find_one({"migration_name": "FreeTier"})
|
13 | 13 | if not migration:
|
14 | 14 | db["Migration"].insert_one({"migration_name": "FreeTier", "migration_complete": False})
|
15 | 15 |
|
16 | 16 | earliest_timestamp = datetime(2023, 1, 1)
|
17 | 17 | 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 | + |
19 | 25 |
|
20 | 26 | while start_date <= datetime(2023, 11, 10):
|
21 |
| - end_date = start_date + one_day # Use one day |
| 27 | + end_date = start_date + one_day |
22 | 28 | buffer = {}
|
23 |
| - timestamps_set = set() # For faster timestamp lookups |
| 29 | + |
24 | 30 | print(start_date, end_date)
|
25 | 31 |
|
26 | 32 | for collection_name in collections_to_migrate:
|
|
39 | 45 | if unit:
|
40 | 46 | new_document[f"{new_collections[collection_name]}_unit"] = unit
|
41 | 47 |
|
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 |
58 | 58 |
|
59 | 59 | all_documents = sorted(buffer.values(), key=itemgetter("timestamp"))
|
60 | 60 |
|
61 |
| - if all_documents: # Only insert if there are documents |
| 61 | + # Insert the batch of documents for the current day |
| 62 | + if all_documents: |
62 | 63 | db["FreeTier"].insert_many(all_documents)
|
63 | 64 |
|
| 65 | + # Move to the next day |
64 | 66 | start_date = end_date
|
65 | 67 |
|
66 | 68 | db["Migration"].update_one({"migration_name": "FreeTier"}, {"$set": {"migration_complete": True}})
|
0 commit comments