def test_align_time_window(self): retention = self._TEST self.assertNotEqual( retention.align_time_window(0, 0, 0), (0, 0, bg_accessor.Stage(precision=60, points=60)) ) stage0 = bg_accessor.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_accessor.Stage(precision=3600, points=24)) )
def _read_points(self, path): """Return a list of (timestamp, value).""" info = whisper.info(path) res = [] if not info: return [] archives = info["archives"] with open(path) as f: buf = f.read() stage0 = True for archive in archives: offset = archive["offset"] stage = bg_accessor.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
def test_stage0(self): s1 = bg_accessor.Stage(points=24, precision=3600, stage0=True) s2 = bg_accessor.Stage.from_string("24*3600s_0") s3 = bg_accessor.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)
def test_operators(self): # Doesn't use assertEqual to make == and != are called s1 = bg_accessor.Stage(points=24, precision=3600) self.assertTrue(s1 != object()) self.assertFalse(s1 == object()) s2 = bg_accessor.Stage.from_string("24*3600s") self.assertTrue(s1 == s2) self.assertFalse(s1 != s2) s3 = bg_accessor.Stage.from_string("12*3600s") self.assertFalse(s1 == s3) self.assertTrue(s1 != s3)
def test_fetch_points_should_be_called_on_both_accessors(self): time_start = 0 time_end = 1 stage = bg_accessor.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)
def _read_metadata(metric_name, path): info = whisper.info(path) if not info: return None retentions = bg_accessor.Retention([ bg_accessor.Stage(precision=a["secondsPerPoint"], points=a["points"]) for a in info["archives"] ]) aggregator = bg_accessor.Aggregator.from_carbon_name( info["aggregationMethod"]) return bg_accessor.MetricMetadata( aggregator=aggregator, retention=retentions, carbon_xfilesfactor=info["xFilesFactor"], )
def test_create_async(self): metric_name = "a.b.c" metric = self.make_metric(metric_name) self._plugin._createAsyncOrig(metric) self.assertFalse(self._plugin.exists(metric_name)) self._plugin._createOneMetric() self.assertTrue(self._plugin.exists(metric_name)) # See if we can update. metric = self.make_metric(metric_name) metric.metadata.retention = bg_accessor.Retention( [bg_accessor.Stage(1, 1)]) self._plugin._createAsyncOrig(metric) self._plugin._createOneMetric() retention = self._plugin.getMetadata(metric_name, "retention") self.assertEquals(retention, metric.metadata.retention)
def run(metrics): end = int(now / 60) * 60 start = end - 3600 for m in metrics: ac.fetch_points(m, start, end, bg_accessor.Stage(86400, 1)) ac.fetch_points(m, start, end, bg_accessor.Stage(10080, 60))