def test_add_decorator(self):
        from flask_monitoringdashboard.core.measurement import add_decorator

        with self.app.app_context():
            for i in range(4):
                self.assertEqual(
                    add_decorator(Endpoint(name=NAME, monitor_level=i)), None)
            self.assertRaises(ValueError, add_decorator,
                              Endpoint(name=NAME, monitor_level=-1))
    def test_start_thread_last_requested(self):
        num_threads = threading.active_count()
        start_thread_last_requested(Endpoint(id=1, name=NAME))
        self.wait_until_threads_finished(num_threads)
        from flask_monitoringdashboard.core.cache import memory_cache

        self.assertNotEqual(memory_cache.get(NAME).last_requested, None)
def add_fake_data():
    """ Adds data to the database for testing purposes. Module flask_monitoringdashboard must be imported locally. """
    from flask_monitoringdashboard.database import session_scope, Request, Endpoint, Outlier
    from flask_monitoringdashboard import config

    # Add requests
    with session_scope() as db_session:
        for i in range(len(REQUESTS)):
            call = Request(id=REQUEST_IDS[i],
                           endpoint_id=ENDPOINT_ID,
                           duration=REQUESTS[i],
                           version_requested=config.version,
                           time_requested=TIMES[i],
                           group_by=GROUP_BY,
                           ip=IP)
            db_session.add(call)

        # Add endpoint
        db_session.add(
            Endpoint(id=ENDPOINT_ID,
                     name=NAME,
                     monitor_level=1,
                     time_added=datetime.datetime.utcnow(),
                     version_added=config.version,
                     last_requested=TIMES[0]))

        # Add Outliers
        for i in range(OUTLIER_COUNT):
            db_session.add(
                Outlier(request_id=i + 1,
                        cpu_percent='[%d, %d, %d, %d]' %
                        (i, i + 1, i + 2, i + 3)))
Example #4
0
 def test_start_performance_thread(self):
     with self.app.test_request_context():
         from flask import request
         request.environ['REMOTE_ADDR'] = '127.0.0.1'
         num_threads = threading.active_count()
         start_performance_thread(Endpoint(id=1, name=NAME), 1234, 200)
         self.assertEqual(threading.active_count(), num_threads + 1)
         self.wait_until_threads_finished(num_threads)
Example #5
0
 def test_start_profiler_and_outlier_thread(self):
     with self.app.test_request_context():
         from flask import request
         request.environ['REMOTE_ADDR'] = '127.0.0.1'
         num_threads = threading.active_count()
         thread = start_profiler_and_outlier_thread(Endpoint(id=1, name=NAME))
         self.assertEqual(threading.active_count(), num_threads + 2)
         thread.stop(duration=1, status_code=200)
         self.wait_until_threads_finished(num_threads)
 def test_after_run(self):
     with self.app.test_request_context():
         from flask import request
         request.environ['REMOTE_ADDR'] = '127.0.0.1'
         current_thread = threading.current_thread().ident
         ip = request.environ['REMOTE_ADDR']
         thread = StacktraceProfiler(current_thread, Endpoint(id=0, name=NAME), ip, group_by=None)
         thread._keeprunning = False
         thread.run()
Example #7
0
def move_rules(old_connection):
    rules = old_connection.execute("select * from {}".format(TABLES[0]))
    endpoints = []
    with session_scope() as db_session:
        for rule in rules:
            end = Endpoint(name=rule['endpoint'], monitor_level=rule['monitor'],
                           time_added=parse(rule['time_added']), version_added=rule['version_added'],
                           last_requested=parse(rule['last_accessed']))
            endpoints.append(end)
        db_session.bulk_save_objects(endpoints)
    def test_start_performance_thread(self):
        with self.app.test_request_context():
            from flask import request

            request.environ['REMOTE_ADDR'] = '127.0.0.1'
            num_threads = threading.active_count()
            start_performance_thread(Endpoint(id=1, name=NAME), 1234, 200)
            self.assertEqual(threading.active_count(), num_threads + 1)
            self.wait_until_threads_finished(num_threads)
            from flask_monitoringdashboard.core.cache import memory_cache

            self.assertGreater(memory_cache.get(NAME).average_duration, 0)
def get_endpoint_by_name(db_session, endpoint_name):
    """
    Returns the Endpoint object from a given endpoint_name.
    If the result doesn't exist in the database, a new row is added.
    :param db_session: session for the database
    :param endpoint_name: string with the endpoint name
    :return Endpoint object
    """
    try:
        result = db_session.query(Endpoint). \
            filter(Endpoint.name == endpoint_name).one()
        result.time_added = to_local_datetime(result.time_added)
        result.last_requested = to_local_datetime(result.last_requested)
    except NoResultFound:
        result = Endpoint(name=endpoint_name)
        db_session.add(result)
        db_session.flush()
    db_session.expunge(result)
    return result
Example #10
0
 def test_start_thread_last_requested(self):
     num_threads = threading.active_count()
     start_thread_last_requested(Endpoint(id=1, name=NAME))
     self.assertEqual(threading.active_count(), num_threads + 1)
     self.wait_until_threads_finished(num_threads)