-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.py
39 lines (30 loc) · 1.14 KB
/
middleware.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# coding=utf-8
"""Common middleware"""
import logging
import time
from djperf.utils import print_slow_queries
class MiddlewareMixin:
def __init__(self, get_response=None):
self.get_response = get_response
def __call__(self, request):
response = None
if hasattr(self, 'process_request'):
response = self.process_request(request)
response = response or self.get_response(request)
if hasattr(self, 'process_response'):
response = self.process_response(request, response)
return response
class SQLLog(MiddlewareMixin):
logger = logging.getLogger()
slow_response_time = 0.001
def process_request(self, request):
request.start_time = time.time()
def process_response(self, request, response):
"""Process request"""
from .utils import log
response_time = time.time() - request.start_time
# if response_time < self.slow_response_time:
# return response
print_slow_queries(str(request.path + str(request.GET.urlencode())))
log(f"Response Time: {round(response_time, 3)} (sec)")
return response