Ejemplo n.º 1
0
def validate_evaluate(rule_condition):
    word_list = rule_condition.split(" ")
    rules = ""
    list_length = len(word_list)
    i = 0
    while i < list_length:
        try:
            operator_handler = operator_handlers.operator_dictionary.get(word_list[i])
        except Exception as e:
            raise e
        if operator_handler is not None:
            try:
                result = operator_handler(name=word_list[i]).evaluate(metric_name=word_list[i + 1], begin_time_given=word_list[i + 2])
                i = i + 3
                rules += result
            except Exception as e:
                raise exceptions.InvalidException("Rule syntax is not valid")
        else:
            rules += word_list[i]
            i = i+1
        rules += " "
    try:
        print("RULES " + str(rules))
        result_value = eval(rules)
        if result_value is True:
            return result_value
        else:
            return False
    except:
        raise exceptions.InvalidException("Rule syntax is not valid")
Ejemplo n.º 2
0
def create_rule(name_space, rule_condition, rule_name, action_value_map, freq):
    try:
        is_valid_rule = validate_evaluate(rule_condition)
    except Exception as e:
        raise e

    if is_valid_rule is None:
        raise exceptions.InvalidException("not_valid_rule_error")
        return false

    try:
        action_value_map = modify_action_value_map(action_value_map)
    except Exception as e:
        raise e

    if action_value_map is None:
        raise exceptions.InvalidException("empty_action_value_map_error")
        return false
    rule_id = Rule.rule_existence(rule_name, name_space)
    if rule_id is not None:
        Rule.update(rule_id, rule_condition, freq)
    else:
        try:
            rule_id = Rule.insert_into_rule_table(name_space, rule_condition,
                                                  rule_name, freq)
        except Exception as e:
            raise e

    for action_value in action_value_map:
        action_id = action_value[0]
        value = action_value[1]
        try:
            RuleAction.insert_into_rule_action_table(rule_id, action_id, value)
        except Exception as e:
            raise e
Ejemplo n.º 3
0
def modify_action_value_map(action_value_map):
    action_value_list = []
    if len(action_value_map) == 0:
        return None
    for i in range(len(action_value_map)):
        print(action_value_map)
        action_id = Action.get_id_from_name(action_value_map[i][0])

        if(action_id is not None and is_valid_value(action_value_map[i][1])):
            action_value_list.append([action_id, action_value_map[i][1]])
        else:
            if action_id is None:
                raise exceptions.InvalidException("action_id_null_error")
            else:
                raise exceptions.InvalidException("Value is not valid")
    return action_value_list
Ejemplo n.º 4
0
 def create_action(action_name):
     action_object = Action.objects.create(name=action_name)
     if action_object is None:
         raise exceptions.InvalidException("Error in creating " +
                                           action_name)
     else:
         return  #action_object.pk
Ejemplo n.º 5
0
 def insert_into_rule_action_table(rule_id, action_id, value):
     rule_action = RuleAction.objects.create(rule_id=rule_id,
                                             action_id=action_id,
                                             value=value)
     if rule_action is None:
         raise exceptions.InvalidException("Error in creating row for " +
                                           value)
Ejemplo n.º 6
0
 def create_entry(namespace, metric_name, metric_value, date_time):
     response_obj = MetricData.objects.create(namespace=namespace,
                                              index=metric_name,
                                              metric_value=metric_value,
                                              created_date=date_time)
     if response_obj is None:
         raise exceptions.InvalidException("Error in creating row for " +
                                           str(namespace))
Ejemplo n.º 7
0
    def get_id_from_name(action_name):
        print("ENTERED ")
        action_id = list(
            Action.objects.filter(name=action_name).values_list("id",
                                                                flat=True))

        if action_id is None:
            raise exceptions.InvalidException(action_name + " not found")
        return action_id[0]
Ejemplo n.º 8
0
 def insert_into_rule_table(namespace_name, rule_condition, rule_name,
                            frequency):
     namespace_id = list(
         Namespace.objects.filter(name=namespace_name).values_list(
             'id', flat=True))
     if len(namespace_id) == 0:
         raise exceptions.InvalidException("No entry found for " +
                                           str(namespace_name) +
                                           " in Namespace table")
     rule_object = Rule.objects.create(name=rule_name,
                                       namespace_id=namespace_id[0],
                                       frequency=frequency,
                                       rule_condition=rule_condition)
     if rule_object is None:
         raise exceptions.InvalidException("Error in creating " +
                                           rule_name + " " +
                                           str(namespace_name))
     else:
         return rule_object.pk
Ejemplo n.º 9
0
def total_seconds(frequency):
    freq_split = frequency.split('_')
    if len(freq_split) is not 4:
        raise exceptions.InvalidException("Invalid Format")
    days = int(freq_split[0][:-1])
    hours = int(freq_split[1][:-1])
    minutes = int(freq_split[2][:-1])
    seconds = int(freq_split[3][:-1])
    total_seconds = seconds + (minutes * 60) + (hours * 60 * 60) + (days * 24 *
                                                                    60 * 60)
    return total_seconds
Ejemplo n.º 10
0
def delete_rule(rule_name):
    rule_list = Rule.objects.filter(name=rule_name)
    if len(rule_list) == 0:
        raise exceptions.InvalidException(rule_name + " doesn't exist")
    else:
        for rule in rule_list:
            rule.is_active = False
            rule.save()
            action_rules = RuleAction.objects.filter(id=rule.id)
            for action_rule in action_rules:
                action_rule.is_active = False
                action_rule.save()
    return
Ejemplo n.º 11
0
def modify_date_time(before_time):
    word_list = before_time.split('_')
    if len(word_list) is not 4:
        raise exceptions.InvalidException("Invalid data_time format")
    day = word_list[0][:-1]
    hour = word_list[1][:-1]
    minute = word_list[2][:-1]
    second = word_list[3][:-1]
    query_date_time = datetime.today() - timedelta(days=int(day),
                                                   hours=int(hour),
                                                   minutes=int(minute),
                                                   seconds=int(second))
    return query_date_time