/
middleware.py
68 lines (57 loc) · 2.2 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from django.conf import settings
from johnny import signals
import uuid
import time
import backend
import datetime
def get_unix_date():
#no reason to use int here other than that it's just as easy as storing a datetime...
#dates are not accepted in couch db
return int(datetime.datetime.now().date().strftime('%s'))
class CachePlotMiddleware(object):
def __init__(self):
signals.qc_hit.connect(self._hit)
signals.qc_miss.connect(self._miss)
try:
self.tables, self.requests = backend.get_buckets()
except:
pass
def _hit(self, sender, **kwargs):
tables = kwargs['tables']
for table in tables:
try:
self.tables.update({"table":table},
{"$inc":{"hits":1}}, True)
except Exception, e:
print "Error when logging row to tables: " %str(e)
self.log_row['hits'] += 1
def _miss(self, sender, **kwargs):
tables = kwargs['tables']
for table in tables:
try:
self.tables.update({"table":table}, {"$inc":{"misses":1}}, True)
#self.tables.insert({'result':'miss',
# 'table':table,
# 'time':time.time()})
except Exception, e:
print "Error when logging row to tables: " %str(e)
self.log_row['misses'] += 1
def process_request(self, request):
req_id = str(uuid.uuid4())
self.log_row = {}
self.log_row['time'] = time.time()
self.log_row['path'] = request.get_full_path()
self.log_row['hits'] = 0
self.log_row['misses'] = 0
def log(self):
try:
self.requests.update({"path":self.log_row['path']},
{"$inc":{"hits":self.log_row['hits'], "misses":self.log_row['misses'], 'visits':1}},
True)
except Exception, e:
print "Error when logging row to requests: " %str(e)
def process_response(self, request, response):
self.log()
return response
def process_exception(self, request, exception):
self.log()