예제 #1
0
def save_resource_file(payload,solution_id):
    result = dict(status="failure")
    payload["solution_id"] = solution_id
    context = tracer.get_context(request_id=str(uuid4()), log_level="INFO")
    context.start_span(component=__name__)
    try:
        data = deepcopy(payload)
        if "description" in data.keys():
            data["desc"] = data.pop("description")
        else:
            data["desc"] = ""
            payload["description"] = ""
        rules = RuleManager()
        response = rules.process(RULES_ENDPOINT["SAVE"],data)
        if response["status"]["success"]:
            result["status"] = "success"
            result["msg"] = "File uploaded successfully"
        else:
            result["msg"] = response["status"]["msg"]
    # TODO raise specific exception
    except Exception as e:
        context.log(message=str(e), obj={"tb": traceback.format_exc()})
        result["msg"] = str(e)
    context.end_span()
    return result
예제 #2
0
def delete_custom_rule(solution_id,data):
    try:
        data["solution_id"] = solution_id
        rules = RuleManager()
        result = rules.process("deleteCustomRules",data)
        status = result["status"]
        if status["success"]:
            response = {"status":"success"}
        else:
            response = {"status":"failure","msg":status["msg"]}
    except Exception as e:
        response = {"status": "failure","msg":str(e)}
    return response
예제 #3
0
def update_custom_rule(solution_id,data):
    context = tracer.get_context(request_id=str(uuid4()), log_level="INFO")
    context.start_span(component=__name__)
    try:
        rules = RuleManager()
        data["solution_id"] = solution_id
        result = rules.process("saveCustomRule",data)
        status = result["status"]
        if status["success"]:
            response = {"status":"success"}
        else:
            response = {"status":"failure","msg":status["msg"]}
    # TODO raise specific exception
    except Exception as e:
        response = {"status": "failure","msg":str(e)}
    context.end_span()
    return response
예제 #4
0
def save_data_changes(payload,type):
    result = dict(status="failure")
    context = tracer.get_context(request_id=str(uuid4()), log_level="INFO")
    context.start_span(component=__name__)
    try:
        rules = RuleManager()
        response = rules.process(RULES_ENDPOINT[type],payload)
        if response["status"]["success"]:
            result["status"] = "success"
            result["msg"] = "Data added successfully" if type == "ADD" else "Data deleted successfully"
        else:
            result["msg"] = response["status"]["msg"]
    # TODO raise specific exception
    except Exception as e:
        context.log(message=str(e), obj={"tb": traceback.format_exc()})
        result["msg"] = str(e)
    context.end_span()
    return result
예제 #5
0
def get_custom_rules(solution_id,type):
    context = tracer.get_context(request_id=str(uuid4()), log_level="INFO")
    context.start_span(component=__name__)
    try:
        data = {"solution_id" : solution_id,"type":type}
        rules = RuleManager()
        result = rules.process("getCustomRules",data)
        if result and result["status"]["success"]:
            rules = result["metadata"]["rules"]
            return {"status":"success","custom_rules":rules}
        else:
            return {"status": "success","msg":"error while retrieving custom rules"}
    # TODO raise specific exception
    except Exception as e:
        context.log(message=str(e), obj={"tb": traceback.format_exc()})
        return {"status":"failure","msg":str(e)}
    finally:
        context.end_span()
예제 #6
0
def get_config(request):
    context = tracer.get_context(request_id=str(uuid4()), log_level="INFO")
    context.start_span(component=__name__)
    try:
        solution_id = common.get_solution_from_session(request)
        rules = RuleManager()
        result = rules.process("getOperators",solution_id)
        if result and result["status"]["success"]:
            config = result["metadata"]["data"]
            return {"status":"success","config":config}
        else:
            return {"status": "success","msg":"error while retrieving config from rules"}
    # TODO raise specific exception
    except Exception as e:
        context.log(message=str(e), obj={"tb": traceback.format_exc()})
        return {"status":"failure","msg":str(e)}
    finally:
        context.end_span()
예제 #7
0
def execute_custom_rules(request):
    context = tracer.get_context(request_id=str(uuid4()), log_level="INFO")
    context.start_span(component=__name__)
    try:
        if request.method == "POST":
            payload = json.loads(request.body.decode())
            payload["solution_id"] = common.get_solution_from_session(request)
            rules = RuleManager()
            exec_result = rules.process("executeCustomCode",payload)
            if exec_result["status"]["success"]:
                output = exec_result["metadata"]["result"]
                return {"status":"success","result":output}
            else:
                return {"status": "failure","msg":exec_result["msg"]}
    # TODO raise specific exception
    except Exception as e:
        return {"status":"failure","error":str(e),"msg":"Internal Error occurred"}
    finally:
        context.end_span()
예제 #8
0
def delete_rule(solution_id,rule_id):
    context = tracer.get_context(request_id=str(uuid4()), log_level="INFO")
    context.start_span(component=__name__)
    try:
        payload = dict(solution_id=solution_id,rule_id=rule_id)
        rules = RuleManager()
        result = rules.process("deleteRule",payload)
        if result["status"]["success"] is True:
            MongoDbConn.remove(RULES_COLLECTION,dict(solution_id=solution_id,rule_id=rule_id))
            response = {"status":"success"}
        else:
            response = {"status":"failure","msg":result["status"]["msg"]}
        return response
    # TODO raise specific exception
    except Exception as e:
        context.log(message=str(e), obj={"tb": traceback.format_exc()})
        return {"status":"failure","msg":str(e)}
    finally:
        context.end_span()
예제 #9
0
def update_rule(solution_id,rule):
    context = tracer.get_context(request_id=str(uuid4()), log_level="INFO")
    context.start_span(component=__name__)
    try:
        rules = RuleManager()
        rule["solution_id"] = solution_id
        result = rules.process("saveRule",rule)
        status = result["status"]
        if status["success"]:
            rule_id = get_nested_value(result,"metadata.rule_id")
            where_clause = dict(solution_id=solution_id, rule_id=rule_id)
            MongoDbConn.update(RULES_COLLECTION,where_clause,rule)
            response = {"status":"success","rule_id":rule_id}
        else:
            response = {"status":"failure","msg":status["msg"]}
    # TODO raise specific exception
    except Exception as e:
        context.log(message=str(e), obj={"tb": traceback.format_exc()})
        response = {"status": "failure","msg":str(e)}
    context.end_span()
    return response
예제 #10
0
def get_rule(solution_id,rule_id=None):
    context = tracer.get_context(request_id=str(uuid4()), log_level="INFO")
    context.start_span(component=__name__)
    try:
        rules = RuleManager()
        if rule_id:
            payload = dict(solution_id=solution_id,rule_id=rule_id)
            result = rules.process("getRule", payload)
            key = "rule"
        else:
            payload = dict()
            error = 'Invalid response'
            payload["solution_id"] = solution_id
            result = rules.process("getRules", payload)
            key = "rules"
        if 'status' in result:
            if 'code' in result['status']:
                if result['status']['code'] == 200:
                    if 'metadata' in result and key in result['metadata']:
                        rules = result['metadata'][key]
                        if isinstance(rules,list) and len(rules) > 0:
                            for r in rules:
                                r.pop('_id', False)
                        if isinstance(rules, dict):
                            rules.pop('_id', False)
                        error = None
                else:
                    error = result['msg']

        res = {"status": "success", "data": rules}
        if error is not None:
            res['error'] = error

        return res
    # TODO raise specific exception
    except Exception as e:
        context.log(message=str(e), obj={"tb": traceback.format_exc()})
        return {"status" : "failure", "msg" : str(e)}
    finally:
        context.end_span()
예제 #11
0
def execute_rules(request):
    context = tracer.get_context(request_id=str(uuid4()), log_level="INFO")
    context.start_span(component=__name__)
    try:
        rules = RuleManager()
        data = json.loads(request.body.decode())
        data["solution_id"] = common.get_solution_from_session(request)
        rule = data["rule"]
        transform_rule = False
        if "rule_type" in rule.keys() and rule["rule_type"] == "T":
            transform_rule = True
            document_variables = [k for k in data["source"].keys()]
            data["source"].update(format_source_data(rule["rule"]))
        else:
            data["source"] = data["source"]["source"]
        result = rules.process("execute",data)
        if result["status"]["success"]:
            exec_result = result["metadata"]["result"]["facts"]
            if transform_rule:
                final_result = []
                formatted_result = reformat_attribute_dict(exec_result["result"],"",[])
                for item in formatted_result:
                    if list(item.keys())[0] not in document_variables:
                        final_result.append(item)
                exec_result["result"] = final_result
            if "src" in exec_result.keys():
                exec_result.pop("src")
            return {"status":"success","result":exec_result}
        else:
            return {"status":"failure","msg":result["status"]["msg"]}
    # TODO raise specific exception
    except Exception as e:
        context.log(message=str(e), obj={"tb": traceback.format_exc()})
        return {"status": "failure", "msg": str(e)}
    finally:
        context.end_span()
예제 #12
0
 def create_solution(soln):
     context = tracer.get_context(request_id=str(uuid4()), log_level="INFO")
     context.start_span(component=__name__)
     try:
         # verify solution name already exists
         soln_name = soln["solution_name"]
         if bool(re.match('^[a-zA-Z0-9\s]+$', soln_name)):
             soln_ref_name = str(soln_name).lower().replace(' ', '-')
         else:
             return {
                 "status": "failure",
                 "msg": "invalid characters in solution name"
             }
         soln_ref_id = soln_ref_name + "_" + str(uuid.uuid4())
         soln_exists = None
         try:
             query = {'solution_name': soln_name, 'is_deleted': False}
             projection = {'_id': 0}
             soln_exists = MongoDbConn.find_one(SOLUTION_COLLECTION,
                                                query,
                                                projection=projection)
         except Exception as e:
             context.log(message=str(e), obj={"tb": traceback.format_exc()})
             return {
                 'status': 'failure',
                 'msg': 'Error occurred while creating solution'
             }
         if soln_exists:
             return {
                 'status':
                 'failure',
                 'msg':
                 (soln['solution_name']) + ' - solution name already exists'
             }
         else:
             # Initialising Nifi
             if "is_pipeline" in soln and soln["is_pipeline"]:
                 create_nifi_pipeline_config(soln_ref_id, soln_ref_name)
             data_dict = {
                 'solution_id': soln_ref_id,
                 'solution_name': soln_name,
                 'solution_type': soln["solution_type"],
                 'description': soln["description"],
                 'is_deleted': False,
                 'created_ts': datetime.utcnow().isoformat(),
                 'updated_ts': datetime.utcnow().isoformat(),
                 'hocr_type': 'XPMS'
             }
             resp = SolutionService.create_default_caseflow(
                 soln_ref_id, context)
             if resp['status'] == 'success':
                 wf_msg = 'Default Workflow, BPMN and Queues has been created.'
             else:
                 wf_msg = 'Error while creating default Workflow, BPMN and Queues.'
             MongoDbConn.insert(SOLUTION_COLLECTION, data_dict)
             try:
                 RuleManager().process("saveDefaultRules",
                                       {"solution_id": soln_ref_id})
             except Exception as e:
                 context.log(message=str(e),
                             obj={"tb": traceback.format_exc()})
             # Initialising Services
             SolutionService().solution_trigger(soln_ref_id)
             return {
                 'status':
                 'success',
                 'msg': (soln['solution_name']) +
                 ' - solution has been created. ' + wf_msg
             }
     except Exception as e:
         context.log(message=str(e), obj={"tb": traceback.format_exc()})
         return {"status": "failure", "msg": str(e)}
     finally:
         context.end_span()
예제 #13
0
from xpms_common import trace
from xpms_common.storage_handler import StorageHandler
from services.entity import store_entity_definitions, delete_entity_definitions
from utilities import common
from utilities.common import get_solution_from_session, delete_files, get_file_contents, save_to_folder
from utilities.http import post_s3, post_to_ms, s3_delete, download_file,get, get_nested_value
from connections.mongodb import MongoDbConn
from config_vars import ROOT, TRAINING_SET_COLLECTION, TRAINING_SET_SERVICES_COLLECTION, \
    AMAZON_AWS_BUCKET, AMAZON_AWS_KEY_PATH, MOUNT_PATH, RESOURCES_COLLECTION, RULES_ENDPOINT, \
    API_GATEWAY_POST_JOB_URI, PLATFORM_SERVICE_SPECS, SERVICE_NAME, UPLOADED_FILE_COLLECTION, STATUS_CODES
from uuid import uuid4
from copy import deepcopy
from jrules_lib.rule_manager import RuleManager
#from xpms_common.sftp_handler import SFTPManager

rm_rules = RuleManager()


tracer = trace.Tracer.get_instance(SERVICE_NAME)


def process_tags(request):
    context = tracer.get_context(request_id=str(uuid4()), log_level="INFO")
    context.start_span(component=__name__)
    try:
        solution_id = common.get_solution_from_session(request)
        result = dict(status="failure")
        if request.method == "GET":
            data = dict(solution_id=solution_id)
            response = rm_rules.process(RULES_ENDPOINT["get_tags"],data)
            if response["status"]["success"]:
예제 #14
0
def get_rule_info(solution_id,rule_id):
    rule = RuleManager()
    payload = dict(solution_id=solution_id, rule_id=rule_id)
    rule_info = rule.process(RULES_ENDPOINT["get_rule"], payload)
    return rule_info