Example #1
0
    def test_align_time_window(self):
        retention = self._TEST

        self.assertNotEqual(
            retention.align_time_window(0, 0, 0),
            (0, 0, bg_metric.Stage(precision=60, points=60)),
        )
        stage0 = bg_metric.Stage(precision=60, points=60, stage0=True)
        self.assertEqual(retention.align_time_window(0, 0, 0), (0, 0, stage0))
        self.assertEqual(retention.align_time_window(60, 120, 1200), (60, 120, stage0))
        self.assertEqual(retention.align_time_window(61, 119, 1200), (60, 120, stage0))
        self.assertEqual(retention.align_time_window(59, 121, 1200), (0, 180, stage0))
        self.assertEqual(
            retention.align_time_window(59, 3601, 8000),
            (0, 7200, bg_metric.Stage(precision=3600, points=24)),
        )
Example #2
0
    def _read_points(self, path):
        """Return a list of (timestamp, value)."""
        info = whisper.info(path)
        res = []
        if not info:
            return []

        archives = info["archives"]
        with io.open(path, "rb") as f:
            buf = f.read()

        stage0 = True
        for archive in archives:
            offset = archive["offset"]
            stage = bg_metric.Stage(
                precision=archive["secondsPerPoint"],
                points=archive["points"],
                stage0=stage0,
            )
            stage0 = False
            if stage in self._opts.ignored_stages:
                continue

            for _ in range(archive["points"]):
                timestamp, value = _POINT_STRUCT.unpack_from(buf, offset)
                offset += whisper.pointSize

                if timestamp == 0:
                    continue
                elif timestamp >= self.time_start and timestamp <= self.time_end:
                    res.append((timestamp, value, 1, stage))

        return res
Example #3
0
    def test_stage0(self):
        s1 = bg_metric.Stage(points=24, precision=3600, stage0=True)
        s2 = bg_metric.Stage.from_string("24*3600s_0")
        s3 = bg_metric.Stage.from_string("24*3600s_aggr")

        self.assertTrue(s1.stage0)
        self.assertTrue(s2.stage0)
        self.assertFalse(s3.stage0)
        self.assertTrue(s1 == s2)
        self.assertFalse(s1 != s2)
Example #4
0
    def test_operators(self):
        # Doesn't use assertEqual to make == and != are called
        s1 = bg_metric.Stage(points=24, precision=3600)
        self.assertTrue(s1 != object())
        self.assertFalse(s1 == object())

        s2 = bg_metric.Stage.from_string("24*3600s")
        self.assertTrue(s1 == s2)
        self.assertFalse(s1 != s2)

        s3 = bg_metric.Stage.from_string("12*3600s")
        self.assertFalse(s1 == s3)
        self.assertTrue(s1 != s3)
Example #5
0
    def test_fetch_points_should_be_called_on_both_accessors(self):
        time_start = 0
        time_end = 1
        stage = bg_metric.Stage("0", "1")
        aggregated = False

        self._accessor.connect()
        self._accessor.fetch_points(DEFAULT_METRIC, time_start, time_end,
                                    stage, aggregated)

        self._metadata_accessor.fetch_points.assert_called_with(
            DEFAULT_METRIC, time_start, time_end, stage, aggregated)
        self._data_accessor.fetch_points.assert_called_with(
            DEFAULT_METRIC, time_start, time_end, stage, aggregated)
Example #6
0
    def _read_metadata(metric_name, path):
        info = whisper.info(path)
        if not info:
            return None

        retentions = bg_metric.Retention([
            bg_metric.Stage(precision=a["secondsPerPoint"], points=a["points"])
            for a in info["archives"]
        ])
        aggregator = bg_metric.Aggregator.from_carbon_name(
            info["aggregationMethod"])
        return bg_metric.MetricMetadata.create(
            aggregator=aggregator,
            retention=retentions,
            carbon_xfilesfactor=info["xFilesFactor"],
        )
Example #7
0
    def test_create_async(self):
        metric_name = "a.b.c"
        metric = bg_test_utils.make_metric(metric_name)

        self._plugin._createAsyncOrig(metric, metric_name)
        self.assertFalse(self._plugin.exists(metric_name))
        self._plugin._createOneMetric()
        self.assertTrue(self._plugin.exists(metric_name))

        # See if we can update.
        metric = bg_test_utils.make_metric(
            metric_name, retention=bg_metric.Retention([bg_metric.Stage(1, 1)])
        )
        self._plugin._createAsyncOrig(metric, metric_name)
        self._plugin._createOneMetric()
        retention = self._plugin.getMetadata(metric_name, "retention")
        self.assertEqual(retention, metric.metadata.retention)