Ejemplo n.º 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
Ejemplo n.º 2
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()
Ejemplo n.º 3
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
Ejemplo n.º 4
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
Ejemplo n.º 5
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
Ejemplo n.º 6
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()
Ejemplo n.º 7
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()
Ejemplo n.º 8
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()
Ejemplo n.º 9
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()
Ejemplo n.º 10
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
Ejemplo n.º 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()
Ejemplo n.º 12
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