Commit ef7a808c authored by charankumar's avatar charankumar

Modified

parent d33b9882
......@@ -7,7 +7,8 @@ log:
back_up_count: 10
mongo_db:
URI: mongodb://192.168.0.220:2717/
URI: mongodb://ilens:iLens%231234@192.168.0.207:2117/ilens_configuration?authSource=admin
URI_: mongodb://192.168.0.220:2717
REDIS:
REDIS_HOST: 192.168.0.220
......
......@@ -5,9 +5,27 @@ from scripts.handler.rule_engine_update import RuleUpdate
if __name__ == '__main__':
try:
logger.debug("main block started")
status = RuleUpdate().rule_update()
if status:
print("Created successfully")
# status = RuleUpdate().rule_update(request_payload)
# status = RuleUpdate().fetch_hireares('MODBUS_DELTA_UPDATED')
lst = [["rule_errorcode_1", "tag_194", "tag_352", "rule_engine_131"],
["rule_errorcode_2", "tag_197", "tag_353", "rule_engine_132"],
["rule_errorcode_3", "tag_200", "tag_354", "rule_engine_133"],
["rule_errorcode_4", "tag_202", "tag_355", "rule_engine_134"],
["rule_errorcode_5", "tag_205", "tag_356", "rule_engine_135"],
["rule_errorcode_6", "tag_207", "tag_357", "rule_engine_136"],
["rule_errorcode_7", "tag_208", "tag_358", "rule_engine_137"],
["rule_errorcode_8", "tag_209", "tag_359", "rule_engine_139"],
["rule_errorcode_9", "tag_210", "tag_360", "rule_engine_140"]]
for each in lst:
status = RuleUpdate().fetch_hireares('MODBUS_DELTA_UPDATED',
each[0].split('rule_')[0],
input_tag=each[1],
output_tag=each[2],
rule_id=each[-1],
lookup_name=each[0])
# status = RuleUpdate().rule_update(request_payload, 'error_code_1')
if status.get('status') == 'success':
print(f"Created successfully: {each}")
else:
print("Some records are not created successfully, Please check the log file")
except RequiredFieldsMissing:
......@@ -16,5 +34,3 @@ if __name__ == '__main__':
except Exception as e:
logger.exception("Exception in the main block" + str(e))
print("Exception in the main block" + str(e))
......@@ -359,7 +359,7 @@ class RuleConfigurationHandler(object):
this will check whether that rule already exists while creations
"""
exists = self.rules_mongo.find_one_rule(query={"ruleName": input_data["ruleName"]})
if exists is not None:
if exists:
return True
else:
return False
......
......@@ -9,6 +9,8 @@ from scripts.db.mongo.ilens_configuration.aggregates.site_conf import SiteConfAg
from scripts.handler.rule_configuration_handler import RuleConfigurationHandler
from scripts.errors.module_exceptions import RequiredFieldsMissing
hirearchy_name_mapping = {}
class RuleUpdate:
def __init__(self):
......@@ -17,54 +19,133 @@ class RuleUpdate:
self.sites_mongo = SiteConf(mongo_client=mongo_client)
self.sites_aggregate = SiteConfAggregate
self.rule_configuration_handler = RuleConfigurationHandler()
self.assign_hirearchies = []
self.hirearchy_name_mapping = {}
def rule_update(self):
def fetch_hireares(self, model_id, errr_name, input_tag, output_tag, rule_id, lookup_name):
final_json = {}
try:
final_json["data"] = []
each_site = self.sites_mongo.find_site_by_site_id(site_id='site_101')
try:
dept_exists = False
dept_mapping = {}
line_mapping = {}
line_exists = False
hire_str_label = each_site['site_id']
hire_str_value = each_site['site_info']['site_name']
self.tag_update(each_site['site_info']['tags'], hire_str_label, hire_str_value)
if "dept" in each_site and each_site["dept"]:
dept_exists = True
for each_dept in each_site["dept"]:
temp_hire_str_value = f"{hire_str_value}>{each_dept['dept_name']}"
temp_hire_str_label = f"{hire_str_label}${each_dept['dept_id']}"
dept_mapping[each_dept['dept_id']] = each_dept['dept_name']
self.tag_update(each_dept['tags'], temp_hire_str_label, temp_hire_str_value)
if "line" in each_site and each_site['line']:
for each_line in each_site['line']:
line_exists = True
try:
line_mapping[each_line['line_id']] = each_line['line_name']
if dept_exists:
temp_hire_str_value = f"{hire_str_value}>{dept_mapping[each_line['dept_id']]}>{each_line['line_name']}"
temp_hire_str_label = f"{hire_str_label}${each_line['dept_id']}${each_line['line_id']}"
else:
temp_hire_str_value = f"{hire_str_value}>{each_line['line_name']}"
temp_hire_str_label = f"{hire_str_label}${each_line['line_id']}"
self.tag_update(each_line['tags'], temp_hire_str_label, temp_hire_str_value)
except Exception as e:
logger.exception(e)
if "equipment" in each_site and each_site['equipment']:
for each_equip in each_site['equipment']:
try:
request_payload = [{
"lookup_name": "Rule_lookup_one",
eq_hir_str = hire_str_value
eq_hir_id = hire_str_label
if dept_exists:
eq_hir_str = eq_hir_str + f">{dept_mapping[each_equip['dept_id']]}"
eq_hir_id = eq_hir_id + f"${each_equip['dept_id']}"
if line_exists:
eq_hir_str = eq_hir_str + f">{line_mapping[each_equip['line_id']]}"
eq_hir_id = eq_hir_id + f"${each_equip['line_id']}"
eq_hir_str = eq_hir_str + f">{each_equip['equipment_name']}"
eq_hir_id = eq_hir_id + f"${each_equip['equipment_id']}"
if each_equip['make_model'].lower() == model_id.lower(): # todo
self.assign_hirearchies.append([eq_hir_str, eq_hir_id])
self.tag_update(each_equip['tags'], eq_hir_id, eq_hir_str)
except Exception as e:
logger.exception(e)
except Exception as e:
logger.exception(str(e))
selected_list = []
for each in self.assign_hirearchies:
selected_list.append({
"id": each[1],
"itemName": each[0]
})
status = self.form_insert_json(errr_name, selected_list, input_tag, output_tag, rule_id, lookup_name)
except Exception as e:
logger.exception(e)
status = {'status': 'Failed'}
return status
def tag_update(self, tag_list, hire_id, hire_str):
self.hirearchy_name_mapping[hire_id] = hire_str
for each_tag in tag_list:
self.hirearchy_name_mapping[f"{hire_id}${each_tag['value']}"] = \
f"{hire_str}:{each_tag['label']}"
def form_insert_json(self, errr_name, selected_list, input_tag, output_tag, rule_id, lookup_name):
final_insert_list = []
c = 0
for each_hi in self.assign_hirearchies:
try:
final_insert_list.append({
"lookup_name": lookup_name,
"match_type": "exact",
"hierarchy_ste": ["[Kite:AC VOLTAGE-1]",
"[Kite:Temperature]"],
"complete_tag_id": "tag_36980",
"hierarchy_ste": [
self.hirearchy_name_mapping[f"{each_hi[1]}${input_tag}"]
],
"complete_tag_id": output_tag,
"selected_tags": [
"site_305$tag_125",
"site_305$tag_100"
f"{each_hi[1]}${input_tag}"
],
"output_device": ["site_305"]
},
{
"lookup_name": "Rule_lookup_two",
"match_type": "exact",
"hierarchy_ste": "[Kite:AC VOLTAGE-1]",
"complete_tag_id": "tag_36980",
"selected_tags": ["site_305$tag_125"],
"output_devices": [
"site_305"
"output_device": [
each_hi[1]
]
})
except Exception as e:
c += 1
logger.info(f"No Mapping: {e} - {c}")
logger.exception(e)
status = self.rule_update(final_insert_list, errr_name, selected_list, rule_id)
return status
}]
def rule_update(self, request_payload, rulename, selected_list, rule_id):
try:
project_id = "project_101"
user_id = "user_100"
static_json = {
"rule_engine_id": "",
"Selected_device": [],
"Selected_ruleType": "", # todo
"rule_engine_id": rule_id,
"Selected_device": selected_list,
"Selected_ruleType": "Real Time",
"calcFormulaList": [],
"deviceDescription": "", # todo
"disable_all": "", # todo --> bool
"execute_on": {}, # todo --> Optional
"processOn": "", # todo
"project_id": "project_099", # todo
"ruleName": "", # todo
"schedule": "", # todo --> bool --> Optional
"selected_device_meta": {}, # todo --> Optional
"transformation_type": "", # todo --> Optional
"user_id": "", # todo
"status": "" # todo --> Optional
"deviceDescription": f"Manually created rule from code for lookup - {rulename}", # todo
"disable_all": False,
"execute_on": {},
"processOn": "server",
"project_id": project_id, # todo
"ruleName": f"Lookup-{rulename}", # todo
"selected_device_meta": {},
"transformation_type": "validation_and_transformation",
"user_id": user_id,
"tz": "Asia/Kolkata",
"language": "en"
}
required_fields = ["Selected_ruleType", "deviceDescription", "disable_all",
"processOn", "project_id", "ruleName", "user_id"] # required fields
for each_rule_index, each_rule in enumerate(request_payload):
static_calc_form_json = {}
static_calc_form_json = {
"code": "",
"code": f"lookup({each_rule['hierarchy_ste']}, '{each_rule['lookup_name']}', 'exact')",
"parsedCode": {
"type": "Program",
"body": [
......@@ -76,7 +157,22 @@ class RuleUpdate:
"type": "Identifier",
"name": "lookup"
},
"arguments": []
"arguments": [
{
"type": "Identifier",
"name": each_rule['selected_tags'][0]
},
{
"type": "Literal",
"value": each_rule['lookup_name'],
"raw": f"'{each_rule['lookup_name']}'"
},
{
"type": "Literal",
"value": "match-type",
"raw": "'match-type'"
}
]
}
}
],
......@@ -87,10 +183,10 @@ class RuleUpdate:
"status": True,
"message": "Correct , Valid rule"
},
"completeTagId": "",
"selectedTags": [],
"previousTags": [],
"output_devices": [],
"completeTagId": each_rule['complete_tag_id'],
"selectedTags": each_rule['selected_tags'],
"previousTags": each_rule['selected_tags'],
"output_devices": each_rule['output_device'],
"output_type": [
"data_store"
],
......@@ -109,100 +205,15 @@ class RuleUpdate:
"output_tags": [],
"disable": False,
"execute_on_tag": [],
"block_id": "",
"rule_block_parent": [],
"execute_on": {},
"completeTagName": ""
}
identifier_dict = {
"type": "Identifier",
"name": ""
}
literal_dict = {
"type": "Literal",
"value": "",
"raw": ""
"execute_on": {}
}
selected_device_dict = {
"id": "",
"itemName": ""
}
code_name = ""
if isinstance(each_rule.get("hierarchy_ste"), list):
for each_hierarchy_ste in each_rule.get("hierarchy_ste", []):
if code_name == "":
code_name += each_hierarchy_ste
else:
code_name += ", " + each_hierarchy_ste
else:
code_name += each_rule.get("hierarchy_ste")
code_name += ", " + "'" + each_rule.get("lookup_name", "") + "'"
code_name += ", " + "'" + each_rule.get("match_type", "") + "'"
static_calc_form_json["code"] = "lookup" + "(" + code_name + ")"
if isinstance(each_rule.get("selected_tags"), list):
for each_hierarchy in each_rule.get("selected_tags", []):
identifier_dict["name"] = each_hierarchy
static_calc_form_json["parsedCode"]["body"][0]["expression"]["arguments"].append(
deepcopy(identifier_dict))
else:
identifier_dict["name"] = each_rule.get("selected_tags", "")
static_calc_form_json["parsedCode"]["body"][0]["expression"]["arguments"].append(
deepcopy(identifier_dict))
literal_dict["value"] = each_rule.get("lookup_name", "")
literal_dict["raw"] = "'" + each_rule.get("lookup_name", "") + "'"
static_calc_form_json["parsedCode"]["body"][0]["expression"]["arguments"].append(deepcopy(literal_dict))
literal_dict["value"] = each_rule.get("match_type", "")
literal_dict["raw"] = "'" + each_rule.get("match_type", "") + "'"
static_calc_form_json["parsedCode"]["body"][0]["expression"]["arguments"].append(deepcopy(literal_dict))
static_calc_form_json["completeTagId"] = each_rule.get("complete_tag_id")
static_calc_form_json["selectedTags"] = each_rule.get("selected_tags", [])
static_calc_form_json["previousTags"] = each_rule.get("selected_tags", [])
static_calc_form_json["output_devices"] = each_rule.get("output_devices", [])
if each_rule.get("complete_tag_name"):
static_calc_form_json["completeTagName"] = each_rule.get("complete_tag_name")
else:
complete_tag_name_query = self.tags_aggregate.tag_id_name_map
complete_tag_name = self.tags_mongo.find_by_aggregate(complete_tag_name_query)
if complete_tag_name:
complete_tag_name = complete_tag_name[0]
static_calc_form_json["completeTagName"] = complete_tag_name["tag_id_name"][
each_rule.get("complete_tag_id")]
block_id = "block_" + str((each_rule_index + 1))
static_calc_form_json["block_id"] = block_id
static_json["calcFormulaList"].append(static_calc_form_json)
for each_selected_tags in each_rule.get("selected_tags", []):
selected_device_array = each_selected_tags.split("$tag")
if selected_device_array:
selected_device = selected_device_array[0]
selected_device_dict["id"] = selected_device
device_exist = False
for each_selected_device in static_json.get("Selected_device", []):
if selected_device_dict["id"] == each_selected_device.get("id"):
device_exist = True
if not device_exist:
hierarchy_name_array = selected_device_dict["id"].split("$")
request_dict = {"project_id": static_json["project_id"]}
for each_level in hierarchy_name_array:
if 'site' in each_level:
request_dict.update({"site_id": each_level})
elif 'dept' in each_level:
request_dict.update({"dept_id": each_level})
elif 'line' in each_level:
request_dict.update({"line_id": each_level})
elif 'equipment' in each_level:
request_dict.update({"equipment_id": each_level})
name_hierarchy_query = self.sites_aggregate.find_hierarchy_name(request_dict)
name_hierarchy = self.sites_mongo.find_site_by_aggregate(name_hierarchy_query)
if name_hierarchy:
name_hierarchy = name_hierarchy[0]
selected_device_dict["itemName"] = name_hierarchy.get("itemName")
static_json["Selected_device"].append(deepcopy(selected_device_dict))
for each_required_field in required_fields:
if not static_json.get(each_required_field):
raise RequiredFieldsMissing # Raise error if required fields are not filled
static_json['calcFormulaList'].append(static_calc_form_json)
print(static_json)
return_json = self.rule_configuration_handler.create_rule_engine(static_json)
print(return_json)
# return_json = self.rule_configuration_handler.create_rule_engine(static_json)
# logger.info(return_json)
except Exception as e:
logger.exception(str(e))
raise
return {'status':'success'}
# return return_json
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