Exemple #1
0
def load_messages_to_db(msg_list):
    """
    Load list of messages into DB Status_Message table

    Args:
        msg_list ([Message]): Message list
        purge (boolean): Indicates when the fucntion should purge old messages.

    Returns:
        success (bool): True if successful, False elsewhere
        result  (str): Error message (if any)
    """
    success = True
    result = ""
    try:
        if msg_list:
            db.session.begin()

            for msg in msg_list:
                db_msg = Status_Message(
                    id=get_bytes_from_uuid(msg.id),
                    level=Status_Message.get_level_integer_from_string(
                        msg.level),
                    type=msg.type,
                    message_role=msg.message_role,
                    action_role=msg.action_role,
                    title=msg.title,
                    description=msg.description,
                    actions=msg.actions,
                    alternative_actions=msg.alternative_actions,
                    source=msg.source)
                db.session.merge(db_msg)  # insert or update
            # remove those messages that have been dissapeared
            messsages_ids_str = ','.join(
                ["'%s'" % msg.id.replace('-', '').upper() for msg in msg_list])
            cmd = "delete from status_message  where  hex(id) not in (%s) and source='monitor'" % (
                messsages_ids_str)
            db.session.connection(mapper=Status_Message).execute(cmd)
            #cmd = "delete current_status from current_status left join status_message on current_status.message_id=status_message.id where status_message.id is null;"
            #db.session.connection(mapper=Current_Status).execute(cmd)
            #success, result = purge_current_status_messages()
            db.session.commit()
    except Exception, e:
        success = False
        result = "[load_messages_to_db] Error: %s" % str(e)
        db.session.rollback()
Exemple #2
0
def load_mcserver_messages(message_list):
    """Adds or updates messages coming from the mcserver

    Args:
        message_list[Status_Message]

    Returns:
        success (bool): True if successful, False elsewhere
        result  (str): Error message (if any)
    """
    result = ""
    success = True
    try:
        db.session.begin()
        for msg in message_list:
            msg_id_str = str(msg['msg_id'])
            msg_id_binary = get_bytes_from_uuid(msg_id_str)
            additional_info_json = ""
            if msg['additional_info'] is not None and msg[
                    'additional_info'] != "":
                try:
                    additional_info_json = json.dumps(msg['additional_info'])
                except Exception as e:
                    api_log.warning(
                        "Message with an invalid additional_info %s -  %s" %
                        (msg_id_str, str(e)))
                    additional_info_json = ""
            success, status_message = get_status_message_from_id(
                message_id=msg_id_binary, is_admin=True, serialize=False)
            if success:
                #update values:
                status_message.level = Status_Message.get_level_integer_from_string(
                    str(msg['level']))
                status_message.title = msg['title']
                status_message.description = msg['description']
                status_message.type = msg['type']
                success, current_status_message = get_current_status_from_message_id(
                    msg_id_str)
                if not success or len(current_status_message) != 1:
                    api_log.error(
                        "Invalid external message %s. Current_Status: %s, tuples(%s)"
                        % (msg_id_str, success, len(current_status_message)))
                    continue
                current_status_message[
                    0].additional_info = additional_info_json
                db.session.merge(current_status_message[0])
                db.session.merge(status_message)
            else:
                new_msg = Status_Message()
                new_msg.id = msg_id_binary
                new_msg.level = Status_Message.get_level_integer_from_string(
                    str(msg['level']))
                new_msg.title = msg['title']
                new_msg.description = msg['description']
                new_msg.type = msg['type']
                new_msg.expire = datetime.strptime(msg['valid_to'],
                                                   "%Y-%m-%dT%H:%M:%S")
                new_msg.actions = ""
                new_msg.alternative_actions = ""
                new_msg.source = "external"
                current_status_message = Current_Status()
                current_status_message.id = uuid4().bytes
                current_status_message.component_type = 'external'
                current_status_message.creation_time = datetime.strptime(
                    msg['valid_from'], "%Y-%m-%dT%H:%M:%S")
                current_status_message.message_id = new_msg.id
                current_status_message.additional_info = ""
                current_status_message.suppressed = 0
                current_status_message.viewed = 0
                current_status_message.additional_info = additional_info_json
                db.session.add(new_msg)
                db.session.add(current_status_message)
        db.session.commit()
    except Exception, e:
        success = False
        result = "[load_mcserver_messages(] Error: %s" % str(e)
        db.session.rollback()