Ejemplo n.º 1
0
    def test_fetch_queries(self):
        """Should correctly fill host_results."""
        mocked_return_val = ("""[{"target": "target_1", "datapoints": [[0.1, 10], [0.2, 20]]},
            {"target": "target_2", "datapoints": [[0.1, 60], [0.2, 70]]}]""")
        with mock.patch('urllib2.urlopen') as urlopen:
            urlopen.return_value.read.return_value = mocked_return_val

            host_result = clusters_diff.fetch_queries(
                "host", "auth_key", ["query"], "-24hours", "-2minutes",
                "5.0", "0.01", lambda x: x)

            self.assertEquals(len(host_result.diffable_queries), 1)
            self.assertEquals(len(host_result.query_to_error), 0)
            self.assertEquals(len(host_result.query_to_time_s), 1)
            self.assertEquals(len(host_result.diffable_queries[0].diffable_targets), 2)
            self.assertEquals(host_result.name, "host")
            self.assertEquals(host_result.diffable_queries[0].name, "query")

            predicted_dt_1 = clusters_diff.DiffableTarget("target_1", {10: 0.1, 20: 0.2})
            predicted_dt_2 = clusters_diff.DiffableTarget("target_2", {60: 0.1, 70: 0.2})

            diffable_targets = host_result.diffable_queries[0].diffable_targets
            self.assertEquals(predicted_dt_1.name, diffable_targets[0].name)
            self.assertEquals(predicted_dt_1.ts_to_val, diffable_targets[0].ts_to_val)
            self.assertEquals(predicted_dt_2.name, diffable_targets[1].name)
            self.assertEquals(predicted_dt_2.ts_to_val, diffable_targets[1].ts_to_val)
Ejemplo n.º 2
0
    def test_measure_disymmetry(self):
        """Should correctly mesure dissymmetries with an other instance."""
        diffable_target_1 = clusters_diff.DiffableTarget("target_1", {10: 0.1, 20: 0.2})
        diffable_target_2 = clusters_diff.DiffableTarget("target_2", {10: 0.3, 20: 0.6})

        dissymmetry = diffable_target_1.measure_dissymmetry(diffable_target_2)
        measures = dissymmetry.measures
        rounded_measures = [round(i, 1) for i in measures]

        self.assertEquals(rounded_measures, [0.5, 0.5])
Ejemplo n.º 3
0
    def test_compute_dissymmetries_pctls(self):
        """Sould correctly compute dissymmetries pctls."""
        diffable_target_1 = clusters_diff.DiffableTarget("target_1", {10: 0.1, 20: 0.2})
        diffable_target_2 = clusters_diff.DiffableTarget("target_2", {60: 0.1, 70: 0.2})

        args1 = [(None, None), (None, []), ([], None), ([], []), ]
        for diffable_target_a, diffable_target_b in args1:
            dissymmetries = clusters_diff.compute_dissymmetries(diffable_target_a,
                                                                diffable_target_b)
            self.assertEquals(len(dissymmetries), 0)

        args2 = [([diffable_target_1], [diffable_target_1])]
        for diffable_target_a, diffable_target_b in args2:
            dissymmetries = clusters_diff.compute_dissymmetries(diffable_target_a,
                                                                diffable_target_b)
            self.assertEquals(len(dissymmetries), 1)
            self.assertEquals(dissymmetries[0].name, "target_1")
            self.assertEquals(dissymmetries[0].measures, [0, 0])
            self.assertEquals(dissymmetries[0].pctls,
                              collections.OrderedDict(
                                [(50, 0.0), (75, 0.0), (90, 0.0), (99, 0.0), (99.9, 0.0)]))

        args3 = [([diffable_target_1], [diffable_target_2])]
        for diffable_target_a, diffable_target_b in args3:
            dissymmetries = clusters_diff.compute_dissymmetries(diffable_target_a,
                                                                diffable_target_b)
            self.assertEquals(len(dissymmetries), 2)
            assert "target_1" in [dissymmetries[0].name, dissymmetries[1].name]
            self.assertEquals(dissymmetries[0].measures, [1, 1])
            self.assertEquals(dissymmetries[0].pctls,
                              collections.OrderedDict(
                                [(50, 1.0), (75, 1.0), (90, 1.0), (99, 1.0), (99.9, 1.0)]))
            assert "target_2" in [dissymmetries[0].name, dissymmetries[1].name]
            self.assertEquals(dissymmetries[1].measures, [1, 1])
            self.assertEquals(dissymmetries[1].pctls,
                              collections.OrderedDict(
                                [(50, 1.0), (75, 1.0), (90, 1.0), (99, 1.0), (99.9, 1.0)]))

        args4 = [([diffable_target_1], []), ([], [diffable_target_1])]
        for diffable_target_a, diffable_target_b in args4:
            dissymmetries = clusters_diff.compute_dissymmetries(diffable_target_a,
                                                                diffable_target_b)
            self.assertEquals(len(dissymmetries), 1)
            self.assertEquals(dissymmetries[0].name, "target_1")
            self.assertEquals(dissymmetries[0].measures, [1, 1])
            self.assertEquals(dissymmetries[0].pctls,
                              collections.OrderedDict(
                                [(50, 1.0), (75, 1.0), (90, 1.0), (99, 1.0), (99.9, 1.0)]))
Ejemplo n.º 4
0
    def test_parse_request_result(self):
        """Should correctly parse a jsonObject into a list of DiffableTarget."""
        data = [{"target": "target_1", "datapoints": [[0.1, 10], [0.2, 20]]},
                {"target": "target_2", "datapoints": [[0.1, 60], [0.2, 70]]}]
        data_json = json.dumps(data)

        diffable_target_1 = clusters_diff.DiffableTarget("target_1", {10: 0.1, 20: 0.2})
        diffable_target_2 = clusters_diff.DiffableTarget("target_2", {60: 0.1, 70: 0.2})

        request = clusters_diff.Request('http://fakeurl.com', 'auth_key', 5.0)
        self.assertEquals(len(request._parse_request_result(data_json)), 2)
        self.assertEquals(request._parse_request_result(data_json)[0].name,
                          diffable_target_1.name)
        self.assertEquals(request._parse_request_result(data_json)[0].ts_to_val,
                          diffable_target_1.ts_to_val)
        self.assertEquals(request._parse_request_result(data_json)[1].name,
                          diffable_target_2.name)
        self.assertEquals(request._parse_request_result(data_json)[1].ts_to_val,
                          diffable_target_2.ts_to_val)
Ejemplo n.º 5
0
    def test_outer_join_diffables(self):
        """Should correctly compute outer join on diffable name."""
        diffables_a = [
            clusters_diff.DiffableTarget("1", {1: 101}),
            clusters_diff.DiffableTarget("2", {1: 102}),
            clusters_diff.DiffableTarget("6", {1: 106}),
            clusters_diff.DiffableTarget("7", {1: 107}),
            clusters_diff.DiffableTarget("9", {1: 109}),
            clusters_diff.DiffableTarget("11", {1: 111}),
            clusters_diff.DiffableTarget("14", {1: 114}),
        ]

        diffables_b = [
            clusters_diff.DiffableTarget("2", {1: 202}),
            clusters_diff.DiffableTarget("4", {1: 204}),
            clusters_diff.DiffableTarget("5", {1: 205}),
            clusters_diff.DiffableTarget("9", {1: 209}),
            clusters_diff.DiffableTarget("12", {1: 212}),
            clusters_diff.DiffableTarget("14", {1: 214}),
        ]

        result = clusters_diff._outer_join_diffables(diffables_a, diffables_b)

        formated_result = []
        for (y, z) in result:
            assert not y or not z or y.name == z.name
            val1 = y.ts_to_val if y else None
            val2 = z.ts_to_val if z else None
            formated_result.append((val1, val2))

        predicted_result = [
            ({
                1: 111
            }, None),
            (None, {
                1: 212
            }),
            ({
                1: 114
            }, {
                1: 214
            }),
            ({
                1: 101
            }, None),
            ({
                1: 102
            }, {
                1: 202
            }),
            (None, {
                1: 205
            }),
            (None, {
                1: 204
            }),
            ({
                1: 107
            }, None),
            ({
                1: 106
            }, None),
            ({
                1: 109
            }, {
                1: 209
            }),
        ]

        def _key(v):
            a, b = v
            a = 0 if a is None else a[1]
            b = 0 if b is None else b[1]
            return max(a, b)

        self.assertEqual(sorted(formated_result, key=_key),
                         sorted(predicted_result, key=_key))