def get_threshold_count(self): if self.threshold: metric = ThresholdMetric(self.basekey, threshold=self.threshold, minutes=self.alarm_minutes) return metric.amount() return None
def check_threshold(self, doubled=False): if self.threshold: metric = ThresholdMetric(self.basekey, threshold=self.threshold, minutes=self.alarm_minutes) return metric.is_okay(doubled) return True
def test_threshold_doubles_appropriately(self): mt = ThresholdMetric(str(random()), 5, 1) with override_service('time', FakeTimeProvider): self.assertFalse(mt.is_okay()) mt.increment() mt.increment() Services.time.step(30) mt.increment() mt.increment() Services.time.step(50) mt.increment() mt.increment() self.assertFalse(mt.is_okay()) self.assertTrue(mt.is_okay(True))
def record(self, request_or_user, **metadata): from canvas import fact # A unique key per day. if hasattr(request_or_user, 'user'): request = request_or_user user = request.user else: request = None user = request_or_user def _record(timestamp_key): if request: RedisSet(timestamp_key + ":unique_ips").sadd( util.ip_to_int(request.META.get('REMOTE_ADDR'))) if user: RedisSet(timestamp_key + ":uniques").sadd(user.id) RedisKey(timestamp_key + ":count").incr(1) _record(self.basekey + ":" + Services.time.strftime("%Y.%m.%d")) _record(self.basekey + ":" + Services.time.strftime("%Y.%m.%d.%H")) self.timestamp_key.set(str(Services.time.time())) if self.threshold: ThresholdMetric(self.basekey, threshold=self.threshold, minutes=self.alarm_minutes).increment() if metadata.get('record_fact', True): fact.record('metric', request_or_user, dict(metadata, metric=self.name))
def test_negative_threshold_returns_to_success(self): mt = ThresholdMetric(str(random()), -5, 1) with override_service('time', FakeTimeProvider): self.assertTrue(mt.is_okay()) mt.increment() Services.time.step(30) mt.increment() mt.increment() mt.increment() mt.increment() self.assertFalse(mt.is_okay()) Services.time.step(50) self.assertTrue(mt.is_okay()) mt.increment() self.assertFalse(mt.is_okay())
def test_threshold_initial_failure(self): mt = ThresholdMetric(str(random()), 5, 1) with override_service('time', FakeTimeProvider): self.assertFalse(mt.is_okay())
def test_negative_threshold_doubles_appropriately(self): mt = ThresholdMetric(str(random()), -5, 1) with override_service('time', FakeTimeProvider): self.assertTrue(mt.is_okay()) mt.increment() mt.increment() self.assertTrue(mt.is_okay()) Services.time.step(30) mt.increment() mt.increment() self.assertTrue(mt.is_okay()) Services.time.step(50) mt.increment() mt.increment() self.assertTrue(mt.is_okay()) self.assertFalse(mt.is_okay(True))