Example #1
0
def vector_to_scalar_equal_operation(op, v):
    """
    Passed Vector values

    [ {(name, tag) : value}, {(name, tag) : value} ...

    Return boolean scalar result True if all values are equal
    """

    if not op or not v or not isinstance(v, list):
        raise HealthException("Insufficient input for Equal operation ")

    i0 = v[0]
    k1, v1 = get_kv(i0)
    v1 = get_value_from_health_internal_tuple(v1)
    if v1 and isinstance(v1, list):
        v1 = sorted(v1)

    for i in v[1:]:
        k2, v2 = get_kv(i)
        v2 = get_value_from_health_internal_tuple(v2)
        if v2 and isinstance(v2, list):
            v2 = sorted(v2)

        if not op(v1, v2):
            return False

    return True
Example #2
0
    def _operate_each_key(self, arg1, arg2, save_param=None):
        if isinstance(arg1, dict) and isinstance(arg2, dict):
            return None

        if not isinstance(arg1, dict) and not isinstance(arg2, dict):

            try:
                raw_arg1 = get_value_from_health_internal_tuple(arg1)
                raw_arg2 = get_value_from_health_internal_tuple(arg2)
                if self.op == operator.div and raw_arg2 == 0:
                    val_to_save = create_value_list_to_save(save_param,
                                                            value=0,
                                                            op1=arg1,
                                                            op2=arg2)
                    return (0, val_to_save)

                # if any of the arg is type float or operation is division
                # cast all argument to float
                if self.op == operator.div or isinstance(
                        raw_arg1, float) or isinstance(raw_arg2, float):
                    raw_arg1 = float(raw_arg1)
                    raw_arg2 = float(raw_arg2)

                result = self.op(raw_arg1, raw_arg2)
                val_to_save = create_value_list_to_save(save_param,
                                                        value=result,
                                                        op1=arg1,
                                                        op2=arg2)
                return create_health_internal_tuple(result, val_to_save)

            except Exception:
                return create_health_internal_tuple(None, [])

        dict_first = True
        if isinstance(arg1, dict):
            d = arg1
            v = arg2
        elif isinstance(arg2, dict):
            d = arg2
            v = arg1
            dict_first = False

        res_dict = {}
        for _k in d:
            if dict_first:
                res_dict[_k] = self._operate_each_key(d[_k],
                                                      v,
                                                      save_param=save_param)
            else:
                res_dict[_k] = self._operate_each_key(v,
                                                      d[_k],
                                                      save_param=save_param)

        return res_dict
Example #3
0
    def operate(self,
                data={},
                check_val=create_health_internal_tuple(True, []),
                error=None,
                category=None,
                level=None,
                description=None,
                success_msg=None):
        if not data:
            raise HealthException("Wrong Input Data for ASSERT operation.")

        res = {}
        res[AssertResultKey.FAIL_MSG] = str(error)
        res[AssertResultKey.DESCRIPTION] = description
        res[AssertResultKey.SUCCESS_MSG] = success_msg
        res[AssertResultKey.KEYS] = []
        try:
            res[AssertResultKey.CATEGORY] = category.upper().split(".")
        except Exception:
            res[AssertResultKey.CATEGORY] = None

        res[AssertResultKey.LEVEL] = level

        if not isinstance(data, dict):
            if not self.op(get_value_from_health_internal_tuple(data),
                           get_value_from_health_internal_tuple(check_val)):
                return (ParserResultType.ASSERT, res)
            return None

        kv = find_kv_vector(NOKEY, data, recurse=True, update_saved_list=False)

        if not kv:
            return (ParserResultType.ASSERT, res)

        fail = False

        for i in kv:
            k, v = get_kv(i)
            kv_tuple = (k, None)
            value_to_check = get_value_from_health_internal_tuple(v)
            if v[1]:
                kv_tuple = (k, v[1])

            if not self.op(value_to_check,
                           get_value_from_health_internal_tuple(check_val)):
                res[AssertResultKey.SUCCESS] = False
                fail = True
                res[AssertResultKey.KEYS].append(kv_tuple)

        if not fail:
            res[AssertResultKey.SUCCESS] = True

        return (ParserResultType.ASSERT, res)
Example #4
0
 def test_get_value_from_health_internal_tuple(self):
     self.assertEqual(
         util.get_value_from_health_internal_tuple(
             (1, [('conf2', 100, True), ('conf1', 6.0, True)])), 1,
         "get_value_from_health_internal_tuple did not return the expected result"
     )
     self.assertEqual(
         util.get_value_from_health_internal_tuple(9), 9,
         "get_value_from_health_internal_tuple did not return the expected result"
     )
     self.assertEqual(
         util.get_value_from_health_internal_tuple(None), None,
         "get_value_from_health_internal_tuple did not return the expected result"
     )
Example #5
0
    def _operate_each_key(self, arg1, arg2, save_param=None):
        if isinstance(arg1, dict):
            return None

        try:
            raw_arg1 = get_value_from_health_internal_tuple(arg1)
            raw_arg2 = get_value_from_health_internal_tuple(arg2)
            result = self.op(raw_arg1, raw_arg2)
            val_to_save = create_value_list_to_save(save_param,
                                                    value=result,
                                                    op1=arg1,
                                                    op2=arg2)
            return create_health_internal_tuple(result, val_to_save)

        except Exception:
            return create_health_internal_tuple(None, [])
Example #6
0
def vector_to_scalar_value_uniform_operation(op, v):
    """
    Passed Vector values

    [ {(name, tag) : value}, {(name, tag) : value} ...

    Return boolean scalar result True if all values are uniformly distributed
    """

    if not v or not isinstance(v, list):
        raise HealthException(
            "Insufficient input for Value Uniform operation ")

    d = {}

    for i in v:
        k2, v2 = get_kv(i)
        v2 = get_value_from_health_internal_tuple(v2)
        if v2 and isinstance(v2, list):
            v2 = sorted(v2)

        if v2 not in d:
            d[v2] = 1
        else:
            d[v2] = d[v2] + 1

    minv = min(d.values())
    maxv = max(d.values())
    if (maxv - minv) > 1:
        return False

    return True
Example #7
0
def vector_to_vector_no_match_operation(kv, op, a, save_param):
    """
    Passed Vector values
    [ {(name, tag) : value}, {(name, tag) : value} ...

    Return health internal tuple

    (True/False , [(key, value, formatting), (key, value, formatting), ...])
    """
    res = {}
    operand = get_value_from_health_internal_tuple(a)
    if not kv:
        raise HealthException("Insufficient input for NO_MATCH operation ")

    try:
        values = [
            get_value_from_health_internal_tuple(get_kv(m)[1]) for m in kv
        ]
        match_operand = _find_match_operand_value(operand, values)

        result = False
        val_to_save = []
        for x in kv:
            k, v = get_kv(x)
            _val = get_value_from_health_internal_tuple(v)

            if not op(_val, match_operand):
                result |= True
                val_to_save += create_value_list_to_save(save_param=None,
                                                         value=result,
                                                         op1=v)

        if operand and operand == MAJORITY:
            key = "Majority Value"
        else:
            key = "Expected Value"

        val_to_save += create_value_list_to_save(save_param=save_param,
                                                 key=key,
                                                 value=match_operand)
        res = create_health_internal_tuple(result, val_to_save)

    except Exception:
        res = create_health_internal_tuple(False, None)

    return res
Example #8
0
    def _operate_each_key(self, arg1, arg2, comp_op=None):
        if isinstance(arg1, dict):
            return None

        if not isinstance(arg2, dict):

            try:
                raw_arg1 = get_value_from_health_internal_tuple(arg1)
                raw_arg2 = get_value_from_health_internal_tuple(arg2)

                return comp_op(raw_arg1, raw_arg2)

            except Exception:
                return False

        res_list = []
        for _k in arg2:
            res_list.append(
                self._operate_each_key(arg1, arg2[_k], comp_op=comp_op))

        return self.op(res_list)
Example #9
0
def vector_to_scalar_first_operation(op, v):
    """
    Passed Vector values

    [ {(name, tag) : value}, {(name, tag) : value} ...

    Returns first value from values
    """

    if not v or not isinstance(v, list):
        raise HealthException("Insufficient input for Random operation ")

    try:
        return get_value_from_health_internal_tuple(get_kv(v[0])[1])
    except Exception:
        return None
Example #10
0
def basic_vector_to_scalar_operation(op, kv, typecast=int, initial_value=None):
    """
    Passed Vector values and type of value

    [ {(name, tag) : value}, {(name, tag) : value} ...

    Return aggregated result along with count of element processed
    """

    if not op or not kv or not isinstance(kv, list):
        raise HealthException("Insufficient input for vector operation ")

    if initial_value:
        found_first = True
        res = initial_value
    else:
        found_first = False
        res = None

    cnt = 0

    for i in kv:
        k1, v1 = get_kv(i)
        v1 = get_value_from_health_internal_tuple(v1)
        try:
            if not found_first:
                res = typecast(v1)
                found_first = True
                cnt = cnt + 1
                continue
        except Exception:
            continue

        try:
            res = op(res, v1)
            cnt = cnt + 1
        except Exception:
            continue

    return res, cnt
Example #11
0
def vector_to_vector_sd_anomaly_operation(kv, op, a, save_param):
    """
    Passed Vector values
    [ {(name, tag) : value}, {(name, tag) : value} ...

    Return health internal tuple

    (True/False , [(key, value, formatting), (key, value, formatting), ...])
    """
    res = {}
    sd_multiplier = get_value_from_health_internal_tuple(a)
    if not kv or not sd_multiplier:
        raise HealthException("Insufficient input for SD_ANOMALY operation ")

    try:
        n = len(kv)
        if n < 3:
            no_anomaly = True
            range_start = 0
            range_end = 0
        else:
            values = [
                get_value_from_health_internal_tuple(get_kv(m)[1]) for m in kv
            ]
            no_anomaly = False

            try:
                # We should consider int and floats only
                s = sum(values)
            except Exception:
                no_anomaly = True

            if not no_anomaly:
                mean = float(s) / float(n)
                variance = 0
                for v in values:
                    variance += pow((v - mean), 2)
                variance = float(variance) / float(n)
                sd = sqrt(variance)
                range_start = mean - (sd_multiplier * sd)
                range_end = mean + (sd_multiplier * sd)

        result = False
        val_to_save = []
        for x in kv:
            k, v = get_kv(x)
            _val = get_value_from_health_internal_tuple(v)

            if not no_anomaly and (float(_val) < float(range_start)
                                   or float(_val) > float(range_end)):
                result |= True
                val_to_save += create_value_list_to_save(save_param=None,
                                                         value=result,
                                                         op1=v)

        val_to_save += create_value_list_to_save(save_param=save_param,
                                                 value=result)
        res = create_health_internal_tuple(result, val_to_save)

    except Exception:
        res = create_health_internal_tuple(False, None)

    return res
Example #12
0
def vector_to_vector_diff_operation(kv, op, a, save_param):
    """
    Passed Vector values
    [ {(name, tag) : value}, {(name, tag) : value} ...

    Return boolean dictionary result

    { (name, tag) : True/False , (name, tag) : True/False, ... }
    """

    res = {}
    temp_res = {}
    if not kv or not a:
        raise HealthException("Insufficient input for Diff operation ")

    exception_found = False
    try:
        for x, y in itertools.combinations(kv, 2):
            k1, v1 = get_kv(x)
            k2, v2 = get_kv(y)

            _v1 = get_value_from_health_internal_tuple(v1)
            _v2 = get_value_from_health_internal_tuple(v2)

            if op(abs(_v1 - _v2), a):
                try:
                    temp_res[make_key(k1)] |= True

                except Exception:
                    temp_res[make_key(k1)] = True

                try:
                    temp_res[make_key(k2)] |= True
                except Exception:
                    temp_res[make_key(k2)] = True

            else:
                try:
                    temp_res[make_key(k1)] |= False
                except Exception:
                    temp_res[make_key(k1)] = False

                try:
                    temp_res[make_key(k2)] |= False
                except Exception:
                    temp_res[make_key(k2)] = False

        for i in kv:
            k, v = get_kv(i)
            val_to_save = create_value_list_to_save(
                save_param, value=temp_res[make_key(k)], op1=v)
            res[make_key(k)] = create_health_internal_tuple(
                temp_res[make_key(k)], val_to_save)

    except Exception:
        exception_found = True

    if exception_found:
        for x in kv:
            k, v = get_kv(x)
            res[make_key(k)] = create_health_internal_tuple(None, None)

    return res