Пример #1
0
def find_kv_vector(key, data, recurse=False, update_saved_list=False):
    """
    Function takes a arbitrary next dictionary and creates
    vector of based level key and value pair in form

    [ {(key, "KEY"): value1}, {(key1: "KEY") : value1} ... ] 

    If recurse is tree key is entire "/" path from root

    e.g ascluster/sfo-counteraero8.alcfd.com:3000/dlog_overwritten_error
    """

    v = []
    if data is None:
        return v

    if not isinstance(data, dict):
        k = merge_key(key, " ", recurse)
        v.append(make_map(k, data))
        return v

    for _key in sorted(data.keys()):
        k = merge_key(key, _key, recurse)
        if not isinstance(data[_key], dict):

            if _key[1] == "KEY":
                _k = key

            else:
                _k = k
            v.append(
                make_map(
                    k,
                    add_prefix_to_saved_keys(_k, data[_key])
                    if update_saved_list else data[_key]))
            # v.append(make_map(k, data[_key]))
        else:
            v.extend(
                find_kv_vector(k,
                               data[_key],
                               recurse=recurse,
                               update_saved_list=update_saved_list))

    return v
Пример #2
0
    def test_merge_key(self):
        expected = " "
        result = util.merge_key("key", " ")
        self.assertEqual(result, expected,
                         "merge_key did not return the expected result")

        expected = "abcd"
        result = util.merge_key("key", "abcd")
        self.assertEqual(result, expected,
                         "merge_key did not return the expected result")

        expected = "key/test"
        result = util.merge_key("key", ("test", "NAMESPACE"), recurse=True)
        self.assertEqual(result, expected,
                         "merge_key did not return the expected result")

        expected = "test"
        result = util.merge_key("", ("test", "NAMESPACE"), recurse=True)
        self.assertEqual(result, expected,
                         "merge_key did not return the expected result")
Пример #3
0
def apply_operator(data,
                   key,
                   op_fn,
                   group_by=None,
                   arg2=None,
                   recurse=False,
                   on_all_keys=True,
                   save_param=None,
                   update_saved_list=False):
    res_dict = {}
    if not data or not isinstance(data, dict):
        raise HealthException("Wrong Input Data ")

    if not group_by:
        raise HealthException("No Group Id ")

    for _key in data.keys():
        k = merge_key(key, _key, recurse)
        if _key[1] == group_by:
            # User merged key for aggregation result
            if on_all_keys:
                # Apply operation on all leaf values
                res_dict[k] = op_fn(
                    find_kv_vector(NOKEY,
                                   data[_key],
                                   recurse=True,
                                   update_saved_list=update_saved_list),
                    save_param)
            else:
                # Apply operation on next level only, no further
                if isinstance(data[_key], dict):
                    # Next level is dict, so apply operation on keys
                    res_dict[k] = op_fn(data[_key].keys(), save_param)
                else:
                    # Next level is not dict, so apply operation on value
                    res_dict[k] = op_fn([data[_key]], save_param)
        else:
            res_dict[_key] = apply_operator(
                data[_key],
                k,
                op_fn,
                group_by,
                arg2,
                recurse,
                on_all_keys=on_all_keys,
                save_param=save_param,
                update_saved_list=update_saved_list)

    return res_dict