forked from cbitstech/Purple-Robot-Django
/
performance.py
105 lines (63 loc) · 2.52 KB
/
performance.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# pylint: disable=line-too-long, bare-except
import datetime
import os
import cPickle as pickle # nosec
import arrow
from django.utils import timezone
def append_performance_sample(user, item, detail_date=None, value=''):
from django.conf import settings
if detail_date is None:
detail_date = timezone.now()
os.umask(000)
today = datetime.date.today()
folder = settings.MEDIA_ROOT + '/purple_robot_analytics/' + user + '/' + today.isoformat()
if not os.path.exists(folder):
os.makedirs(folder)
item_path = folder + '/' + item + '.pickle'
content = {}
if os.path.exists(item_path):
pickle_file = open(item_path, 'rb')
try:
content = pickle.load(pickle_file) # nosec
pickle_file.close()
except EOFError:
content = {}
except ValueError:
content = {}
content[detail_date.isoformat()] = value
pickle.dump(content, open(item_path, 'wb'))
def fetch_performance_samples(user, item, start=None, end=None):
if end is None:
end = timezone.now()
if start is None:
today = datetime.date.today()
start = datetime.datetime(today.year, today.month, today.day, 0, 0, 0, 0, tzinfo=end.tzinfo)
start_date = start.date()
end_date = end.date()
samples = []
while start_date <= end_date:
from django.conf import settings
item_path = settings.MEDIA_ROOT + '/purple_robot_analytics/' + user + '/' + start_date.isoformat() + '/' + item + '.pickle'
if os.path.exists(item_path):
pickle_file = open(item_path, 'rb')
content = pickle.load(pickle_file) # nosec
for key, value in content.iteritems():
sample_date = arrow.get(key).datetime
if sample_date >= start and sample_date <= end:
value['sample_date'] = key
samples.append(value)
pickle_file.close()
start_date += datetime.timedelta(days=1)
samples.sort(key=lambda sample: sample['sample_date'])
return samples
def fetch_performance_users():
from django.conf import settings
items_path = settings.MEDIA_ROOT + '/purple_robot_analytics/'
users = {}
for item in os.listdir(items_path):
if item != 'system':
dates = os.listdir(items_path + '/' + item)
last = sorted(dates, reverse=True)[0]
toks = last.split('-')
users[item] = datetime.date(int(toks[0]), int(toks[1]), int(toks[2]))
return users