def test_detector_should_be_able_to_calculate_total_probability(): detector = Detector() models.add_event('edge', 'edge_01', {u'cpu': u'2', u'mem': u'10'}, time.time()) event = helpers.get_fake_event() cpu_prob = detector.calculate_probability_by_metric("cpu", event) mem_prob = detector.calculate_probability_by_metric("mem", event) assert cpu_prob * mem_prob == detector.calculate_total_probability(event)
def test_detector_should_fetch_calculus_base_of_bucket_and_key(): models.add_event('edge', 'edge_01', {u'cpu': u'2', u'mem': u'10'}, time.time()) models.add_event('edge', 'edge_02', {u'cpu': u'3', u'mem': u'20'}, time.time()) detector = Detector() expected = (5, 2, 13) assert expected == detector.fetch_data_from_calculus_base("edge", "cpu")
def test_should_update_calculus_base_when_event_created(): metrics = {u'load': u'2', u'mem': u'150'} models.add_event('bk1', 'mach1', metrics, time.time()) assert_calculus_base_updated('bk1', {u'load': {'total': 2.0, 'count': 1, 'squared_total': 4.0}, u'mem': {'total': 150.0, 'count': 1, 'squared_total': 22500.0} })
def test_detector_should_be_able_to_calculate_the_number_of_standard_deviations_of_metric(): # adding this two events will generate a standard deviation of: cpu: 0.5, mem: 5 models.add_event('edge', 'edge_01', {u'cpu': u'2', u'mem': u'10'}, time.time()) models.add_event('edge', 'edge_02', {u'cpu': u'3', u'mem': u'20'}, time.time()) detector = Detector() event = { 'target' : 'edge_01', 'bucket' : 'edge', 'metrics' : {'cpu': 4.0, 'mem': 30}, 'timestamp': 8192819082, } assert 3 == detector.calculate_the_number_of_standard_deviations(event, "cpu") assert 3 == detector.calculate_the_number_of_standard_deviations(event, "mem")
def test_should_update_calculus_base_on_different_buckets(): models.add_event('bk1', 'mach1', {u'load': u'2', u'mem': u'10'}, time.time()) models.add_event('bk2', 'mach2', {u'load': u'3', u'mem': u'20'}, time.time()) assert_calculus_base_updated('bk1', {u'load': {'total': 2.0, 'count': 1, 'squared_total': 4.0}, u'mem': {'total': 10.0, 'count': 1, 'squared_total': 100.0} }) assert_calculus_base_updated('bk2', {u'load': {'total': 3.0, 'count': 1, 'squared_total': 9.0}, u'mem': {'total': 20.0, 'count': 1, 'squared_total': 400.0} })
def test_should_not_duplicate_calculus_base_per_bucket(): models.add_event('bk1', 'mach1', {u'load': u'2', u'mem': u'10'}, time.time()) models.add_event('bk1', 'mach2', {u'load': u'3', u'mem': u'20'}, time.time()) models.add_event('bk1', 'mach3', {u'load': u'4', u'mem': u'30'}, time.time()) assert_calculus_base_updated('bk1', {u'load': {'total': 9.0, 'count': 3, 'squared_total': 29.0}, u'mem': {'total': 60.0, 'count': 3, 'squared_total': 1400.0} })
def create_anomalous_event(bucket, target, metrics, timestamp): event = models.add_event(bucket, target, metrics, timestamp) models.mark_event_as_anomalous(event)
def create_event(self, *args, **kwargs): try: return models.add_event(*args, **kwargs) except models.ValidationError as ex: raise HTTPError(400, str(ex))