From e0a1841b1c71d0156b4b58d6e2707f6a728d38b3 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 26 Dec 2013 09:01:46 -0500 Subject: [PATCH] Extract middleware.is_slow_query() and add tests. (imported from commit 60902244a420800f558fdf2f1c38b4ed736c1286) --- zerver/middleware.py | 13 ++++++++----- zerver/tests.py | 11 +++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/zerver/middleware.py b/zerver/middleware.py index 17e648b7ed..f2c6dcd5d7 100644 --- a/zerver/middleware.py +++ b/zerver/middleware.py @@ -65,6 +65,13 @@ def format_timedelta(timedelta): return "%.1fs" % (timedelta) return "%.0fms" % (timedelta_ms(timedelta),) +def is_slow_query(time_delta, path): + return time_delta >= 1 \ + and path not in ["/activity", "/json/report_error", + "/api/v1/deployments/report_error"] \ + and not path.startswith("/realm_activity/") \ + and not path.startswith("/user_activity/") + def write_log_line(log_data, path, method, remote_ip, email, client_name, status_code=200, error_content=''): # For statsd timer name @@ -160,11 +167,7 @@ def write_log_line(log_data, path, method, remote_ip, email, client_name, logger_timing, extra_request_data, logger_client) logger.info(logger_line) - if (time_delta >= 1 - and path not in ["/activity", "/json/report_error", - "/api/v1/deployments/report_error"] - and not path.startswith("/realm_activity/") - and not path.startswith("/user_activity/")): + if (is_slow_query(time_delta, path)): queue_json_publish("slow_queries", "%s (%s)" % (logger_timing, email), lambda e: None) if settings.PROFILE_ALL_REQUESTS: diff --git a/zerver/tests.py b/zerver/tests.py index 843a93c9f4..136eb0f7f0 100644 --- a/zerver/tests.py +++ b/zerver/tests.py @@ -34,6 +34,7 @@ from zerver.lib.digest import send_digest_email from zerver.forms import not_mit_mailing_list from zerver.lib.validator import check_string, check_list, check_dict, \ check_bool, check_int +from zerver.middleware import is_slow_query from zerver.worker import queue_processors @@ -201,6 +202,16 @@ def is_known_slow_test(test_method): API_KEYS = {} +class SlowQueryTest(TestCase): + def test_is_slow_query(self): + self.assertFalse(is_slow_query(0.9, '/some/random/url')) + self.assertTrue(is_slow_query(2, '/some/random/url')) + self.assertFalse(is_slow_query(2, '/activity')) + self.assertFalse(is_slow_query(2, '/json/report_error')) + self.assertFalse(is_slow_query(2, '/api/v1/deployments/report_error')) + self.assertFalse(is_slow_query(2, '/realm_activity/whatever')) + self.assertFalse(is_slow_query(2, '/user_activity/whatever')) + class DecoratorTestCase(TestCase): def test_REQ_converter(self):