Commit 6db2e80d authored by ajil.k's avatar ajil.k

Update translator.py

parent 188d8616
import pymongo import pymongo
import pytz import pytz
from datetime import datetime
""" mongo dumping"""
import os
import subprocess
import zipfile
from datetime import datetime, timedelta
from schema import DeviceDetails, DeviceParameters from schema import DeviceDetails, DeviceParameters
# # Set the MongoDB URL to connect to
# mongo_url = 'mongodb://ilens:ilens%401234@192.168.0.220:4720/?authSource=admin'
# # Set the path to the mongodump executable
# mongodump_path = 'C:/Program Files/MongoDB/Server/6.0/bin/mongodump.exe'
# # Set the name of the database to be dumped
# db_name = 'Iot_manager(Test)'
# # Set the path where the dump will be saved
# dump_path = 'E:/Iot_manager(Test)/dumped_data'
# # Build the command to be executed
# command = [mongodump_path, '--uri', mongo_url, '--db', db_name, '--out', dump_path]
# # Execute the command
# subprocess.call(command)
# # mongo restore
# mongorestore_path = 'C:/Program Files/MongoDB/Server/6.0/bin/mongorestore.exe'
# now = datetime.now()
# date_str = now.strftime("%Y-%m-%d")
# new_db_name = f"Iot_manager(Test)_copy_{date_str}"
# restore_command = [mongorestore_path, '--uri', mongo_url, '--db', new_db_name, dump_path + "/" + db_name]
# subprocess.call(restore_command)
# def zip_folder(folder_path, zip_path):
# with zipfile.ZipFile(zip_path, 'w', compression=zipfile.ZIP_DEFLATED) as zip:
# for root, dirs, files in os.walk(folder_path):
# for file in files:
# file_path = os.path.join(root, file)
# zip.write(file_path, file_path[len(folder_path):])
# zip_folder(dump_path, 'E:/Iot_manager(Test)/dumped_data.zip')
# """ end of mongo dumping and backup"""
""" mongo migration""" """ mongo migration"""
# set up the connection to MongoDB # set up the connection to MongoDB
client1 = pymongo.MongoClient("mongodb://ilens:ilens%401234@192.168.0.220:4720/") mongo_client = pymongo.MongoClient("mongodb://ilens:ilens%401234@192.168.0.220:4720/")
# TODO: Change the Mongo Connectio String
col_list = ['mp201a', 'glens', 'ilens', 'ilens_ir4.0', 'dubai-munc', 'epda-rak', 'camw', 'ilens-avis', 'old_glens', col_list = ['mp201a', 'glens', 'ilens', 'ilens_ir4.0', 'dubai-munc', 'epda-rak', 'camw', 'ilens-avis', 'old_glens',
'old_calixto', "ilens-mc"] 'old_calixto', "ilens-mc"]
db1 = client1["Iot_manager(Test)"]
db2 = client1["Assets_manager"] # Databases
device_details = db2["Iot_devices_details"] old_db = mongo_client["Iot_manager(Test)"] # TODO: Change the Old DB name
parameters_details = db2["Iot_device_Parameters_details"] new_db = mongo_client["Assets_manager"]
# New Collections
device_details = new_db["Iot_devices_details"]
parameters_details = new_db["Iot_device_Parameters_details"]
counter = 1 counter = 1
for collection_name in col_list: for collection_name in col_list:
existing_collection = db1[collection_name] # Old Collection
existing_collection = old_db[collection_name]
# query the collection to fetch documents # query the collection to fetch documents
documents = existing_collection.find({}, {"_id": 0}) documents = existing_collection.find({}, {"_id": 0})
i = 0 i = 0
...@@ -302,7 +258,7 @@ for collection_name in col_list: ...@@ -302,7 +258,7 @@ for collection_name in col_list:
parameters_details.create_index("software_name") parameters_details.create_index("software_name")
else: else:
device_details.delete_one({"mac_address": mac_address}) device_details.delete_one({"mac_address": mac_address})
print(f"{collection_name} - {i}") print(f"{collection_name} - \"{mac_address}\" -{i}")
except Exception as e: except Exception as e:
print(e) print(f"{collection_name} - error while migrating -\"{mac_address}\"- device - {e}")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment