Ejemplo n.º 1
0
def test_detector_should_calculate_probability_for_each_metric(monkeypatch):
    event = helpers.get_fake_event()
    list_of_keys = ['cpu', 'mem']

    def fake_calculate_probability_by_metric(key, event):
        list_of_keys.remove(key)
        return 1

    detector = Detector()
    monkeypatch.setattr(detector, 'calculate_probability_by_metric', fake_calculate_probability_by_metric)
    detector.detect_anomaly(event)

    assert not list_of_keys
Ejemplo n.º 2
0
class AddMetricsHandler(RequestHandler):

    API_ERROR_CODES = 400,

    def initialize(self):
        self.detector = Detector()

    def post(self, bucket, target, timestamp=None):
        event = self.create_event(bucket, target, self.request.arguments, timestamp)
        if self.detector.detect_anomaly(event):
            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))

    def write_error(self, status_code, **kwargs):
        if status_code in self.API_ERROR_CODES and 'exc_info' in kwargs:
            exc_info = kwargs.pop('exc_info')
            self.write({'error_message': exc_info[1].log_message})
            self.finish()
        else:
            super(AddMetricsHandler, self).write_error(status_code, **kwargs)
Ejemplo n.º 3
0
def test_detector_should_be_able_to_detect_anomaly():
    detector = Detector()
    event = helpers.get_fake_event()
    assert False == detector.detect_anomaly(event)