Ejemplo n.º 1
0
    def post(self):

        try:
            #request.get
            postData = request.get_json(force=True, silent=False)

        except Exception as e:

            raise ErrorResponse(
                f"Error parsing json: { sys.exc_info()[0] }  {e}",
                status_code=HTTPStatus.INTERNAL_SERVER_ERROR)

        if not postData:
            raise ErrorResponse(f"Could not parse json.",
                                status_code=HTTPStatus.INTERNAL_SERVER_ERROR)

        listData = None
        if isinstance(postData, dict):
            listData = [postData]
            print("Putting postData into array ", flush=True)
        else:
            listData = postData
            print("Use postData as is ", flush=True)

        if not isinstance(listData, list):
            raise ErrorResponse("list expected",
                                status_code=HTTPStatus.INTERNAL_SERVER_ERROR)

        bee_db = None
        try:
            bee_db = BeekeeperDB()
        except Exception as e:
            raise ErrorResponse(f"Could not create BeekeeperDB: {e}",
                                status_code=HTTPStatus.INTERNAL_SERVER_ERROR)

        logData = []
        default_effective_time = datetime.datetime.now(
            datetime.timezone.utc
        )  # this way all operations in this submission have the exact same time
        for op in listData:

            for f in [
                    "node_id", "operation", "field_name", "field_value",
                    "source"
            ]:
                if f not in op:
                    raise Exception(
                        f'Field {f} missing. Got: {json.dumps(op)}')

            try:
                newLogDataEntry = {
                    "node_id":
                    op["node_id"],
                    "table_name":
                    "nodes_log",
                    "operation":
                    op["operation"],
                    "field_name":
                    op["field_name"],
                    "new_value":
                    op["field_value"],
                    "source":
                    op["source"],
                    "effective_time":
                    op.get("effective_time",
                           default_effective_time.isoformat())
                }

            except Exception as ex:
                raise ErrorResponse(
                    f"Unexpected error in creating newLogDataEntry : {ex}",
                    status_code=HTTPStatus.INTERNAL_SERVER_ERROR)

            logData.append(newLogDataEntry)
            #print("success", flush=True)

        try:
            bee_db.nodes_log_add(logData)  #  effective_time=effective_time)
        except Exception as ex:
            raise ErrorResponse(f"nodes_log_add failed: {ex}",
                                status_code=HTTPStatus.INTERNAL_SERVER_ERROR)

        response = {"success": 1}
        return response
Ejemplo n.º 2
0
def insert_log(postData, lock_tables=True, force=False, lock_requested_by="", replay=True):
    listData = None
    if isinstance( postData, dict ):
        listData = [ postData ]
        #print("Putting postData into array ", flush=True)
    else:
        listData = postData
        #print("Use postData as is ", flush=True)

    if not isinstance( listData, list ):
        raise Exception("list expected")





    logData = []
    default_effective_time = datetime.datetime.now(datetime.timezone.utc) # this way all operations in this submission have the exact same time
    for op in listData:

        for f in ["node_id", "operation", "field_name", "field_value", "source"]:
            if f not in op:
                raise Exception(f'Field {f} missing. Got: {json.dumps(op)}')

        if not force:
            if op["field_name"] == "beehive":
                raise Exception("Field \"beehive\" cannot be set via the /log resource")


        try:
            newLogDataEntry = {
                "node_id": op["node_id"],
                "table_name": "nodes_history" ,
                "operation": op["operation"],
                "field_name": op["field_name"],
                "new_value": op["field_value"],
                "source": op["source"],
                "effective_time" : op.get("effective_time", default_effective_time.isoformat()) }

        except Exception as ex:
            raise Exception(f"Unexpected error in creating newLogDataEntry : {ex}")

        logData.append(newLogDataEntry)
        #print("success", flush=True)

    bee_db = None
    try:
        bee_db = BeekeeperDB()
    except Exception as e:
        raise Exception(f"Could not create BeekeeperDB: {e}" )

    try:
        bee_db.nodes_log_add(logData, lock_tables=lock_tables, lock_requested_by=lock_requested_by, replay=replay)  #  effective_time=effective_time)
    except Exception as ex:
        raise Exception(f"nodes_log_add failed: {ex}" )




    bee_db.close()

    return