Commit 81d4c41f authored by suraksha.pv's avatar suraksha.pv

updated script

parent 8a1aeb46
import json
from pymongo import MongoClient
import pandas as pd
MONGO_URI = "mongodb://ilens:ilens4321@192.168.0.220:2717/"
database_ilens_assistant = "ilens_assistant"
database_ilens_configuration = "ilens_configuration"
# PERIODIC sheet---step_id,old_property_name,new_property_name,step_type
# NON-PERIODIC sheet-----logbook_id,project_id,step_id,old_property_name,new_property_name,step_type
input_json_file_name = 'input_json.json' # input_json_file
input_project_id = "project_099"
input_logbook_id = ["logbook_131"]
input_step_id = ['step_182']
step_type = "NON-PERIODIC" # NON-PERIODIC,PERIODIC
# give input file,sheet name,input details here
CONNECTION_STRING = MONGO_URI
client = MongoClient(CONNECTION_STRING)
input_file_name = 'sterlite.xlsx'
sheet_name = 'PERIODIC'
CONNECTION_STRING = "mongodb://ilens:ilens4321@192.168.0.220:2717/"
# --------------------------------main code -------------------------------------------
ilens_assistant = client[database_ilens_assistant]
ilens_configuration = client[database_ilens_configuration]
client = MongoClient(CONNECTION_STRING)
ilens_assistant = client["project_384__ilens_assistant"]
ilens_configuration = client["project_384__ilens_configuration"]
task_instances = ilens_assistant['task_instances']
task_instance_data = ilens_assistant['task_instance_data']
periodic_data = ilens_configuration['periodic_data']
# ---------------------------code execution---------------------------------------#
with open(input_json_file_name) as user_file:
input_json = user_file.read()
input_dict = json.loads(input_json)
def replace_keys(data, replacements):
if not isinstance(data, dict):
......@@ -40,15 +33,43 @@ def replace_keys(data, replacements):
return new_data
if step_type == 'NON-PERIODIC':
file_path = input_file_name
json_values = pd.read_excel(file_path, sheet_name=sheet_name)
for logbook_id in input_logbook_id:
query = {"logbook_id": input_logbook_id, "project_id": input_project_id}
json_values.columns = json_values.columns.str.strip()
for index, row in json_values.iterrows():
step_id = row['step_id']
input_project_id = row.get('project_id', None)
input_dict = {row['old_property_name']: row['new_property_name']}
step_type = row['step_type']
logbook_id = row.get('logbook_id', None)
print(f"Processing row for step_id: {step_id}, step_type: {step_type}")
if step_type == "PERIODIC":
query = {"step_id": step_id}
periodic_step_data = list(ilens_assistant.periodic_data.find(query))
updated_y = []
if periodic_step_data:
for each in periodic_step_data:
if 'manual_data' in each:
each['manual_data'] = replace_keys(each['manual_data'], input_dict)
updated_y.append(each)
for item in updated_y:
update_query = {"step_id": item['step_id'], "_id": item["_id"]}
ilens_assistant.periodic_data.update_one(update_query, {"$set": item})
print(f"Executed the script for step_id: {step_id}, step_type: {step_type}")
elif step_type == 'NON-PERIODIC':
query = {"logbook_id": logbook_id, "project_id": input_project_id}
data = []
if logbook_data := list(ilens_assistant.task_instances.find(query)):
data.extend(each['task_id'] for each in logbook_data)
task_details = list(ilens_assistant.task_instance_data.find(
{"step_id": input_step_id, "project_id": input_project_id, "task_id": {"$in": data}}))
{"step_id": step_id, "project_id": input_project_id, "task_id": {"$in": data}}))
updated_y = []
if task_details:
for item in task_details:
......@@ -57,20 +78,8 @@ if step_type == 'NON-PERIODIC':
updated_y.append(item)
for item in updated_y:
if item['task_id'] in data:
update_query = {"task_id": item['task_id'], "step_id": input_step_id,
update_query = {"task_id": item['task_id'], "step_id": step_id,
"project_id": input_project_id}
ilens_assistant.task_instance_data.update_one(update_query, {"$set": item})
elif step_type == "PERIODIC":
for step_id in input_step_id:
query = {"step_id": step_id}
periodic_step_data = list(ilens_assistant.periodic_data.find(query))
updated_y = []
if periodic_step_data:
for each in periodic_step_data:
if 'manual_data' in each:
each['manual_data'] = replace_keys(each['manual_data'], input_dict)
updated_y.append(each)
for item in updated_y:
update_query = {"step_id": item['step_id'], "_id": item["_id"]}
ilens_assistant.periodic_data.update_one(update_query, {"$set": item})
print(f"Executed the script for step_id: {step_id}, step_type: {step_type}")
File added
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