1
|
|
|
# coding: utf8 |
2
|
|
|
|
3
|
|
|
""" |
4
|
|
|
This software is licensed under the Apache 2 license, quoted below. |
5
|
|
|
|
6
|
|
|
Copyright 2014 Crystalnix Limited |
7
|
|
|
|
8
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not |
9
|
|
|
use this file except in compliance with the License. You may obtain a copy of |
10
|
|
|
the License at |
11
|
|
|
|
12
|
|
|
http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
|
14
|
|
|
Unless required by applicable law or agreed to in writing, software |
15
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
16
|
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
17
|
|
|
License for the specific language governing permissions and limitations under |
18
|
|
|
the License. |
19
|
|
|
""" |
20
|
|
|
|
21
|
|
|
from functools import wraps |
22
|
|
|
import datetime |
23
|
|
|
import calendar |
24
|
|
|
|
25
|
|
|
from singledispatch import singledispatch |
26
|
|
|
from django_redis import get_redis_connection |
27
|
|
|
from redis.exceptions import WatchError |
28
|
|
|
from django.utils import timezone |
29
|
|
|
|
30
|
|
|
from omaha.settings import KEY_PREFIX, KEY_LAST_ID |
31
|
|
|
|
32
|
|
|
__all__ = ['get_sec_since_midnight', 'get_id'] |
33
|
|
|
|
34
|
|
|
redis = get_redis_connection('statistics') |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
def get_sec_since_midnight(date): |
38
|
|
|
""" |
39
|
|
|
Return seconds since midnight |
40
|
|
|
|
41
|
|
|
>>> from datetime import datetime |
42
|
|
|
>>> get_sec_since_midnight(datetime(year=2014, month=1, day=1, second=42)) |
43
|
|
|
42 |
44
|
|
|
""" |
45
|
|
|
midnight = date.replace(hour=0, minute=0, second=0, microsecond=0) |
46
|
|
|
delta = date - midnight |
47
|
|
|
return delta.seconds |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
def get_days_since_20070101(date): |
51
|
|
|
""" |
52
|
|
|
Return days since 2007-01-01 |
53
|
|
|
|
54
|
|
|
>>> from datetime import datetime |
55
|
|
|
>>> get_days_since_20070101(datetime(year=2016, month=3, day=4)) |
56
|
|
|
3350 |
57
|
|
|
""" |
58
|
|
|
date_20070101 = datetime.datetime(year=2007, month=1, day=1, tzinfo=date.tzinfo) |
59
|
|
|
delta = date - date_20070101 |
60
|
|
|
return delta.days |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
def get_id(uuid): |
64
|
|
|
""" |
65
|
|
|
>>> get_id('{8C65E04C-0383-4AE2-893F-4EC7C58F70DC}') |
66
|
|
|
1 |
67
|
|
|
>>> get_id('{8C65E04C-0383-4AE2-893F-4EC7C58F70DC}') |
68
|
|
|
1 |
69
|
|
|
""" |
70
|
|
|
id = redis.get('{}:{}'.format(KEY_PREFIX, uuid)) |
71
|
|
|
if id is None: |
72
|
|
|
id = create_id(uuid) |
73
|
|
|
return int(id) |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def create_id(uuid): |
77
|
|
|
""" |
78
|
|
|
>>> create_id('{8C65E04C-0383-4AE2-893F-4EC7C58F70DC}') |
79
|
|
|
1 |
80
|
|
|
""" |
81
|
|
|
with redis.pipeline() as pipe: |
82
|
|
|
while True: |
83
|
|
|
try: |
84
|
|
|
pipe.watch(KEY_LAST_ID) |
85
|
|
|
current_id = pipe.get(KEY_LAST_ID) or 0 |
86
|
|
|
next_id = int(current_id) + 1 |
87
|
|
|
pipe.multi() |
88
|
|
|
pipe.set(KEY_LAST_ID, next_id) |
89
|
|
|
pipe.execute() |
90
|
|
|
|
91
|
|
|
redis.set('{}:{}'.format(KEY_PREFIX, uuid), next_id) |
92
|
|
|
return next_id |
93
|
|
|
except WatchError: |
94
|
|
|
continue |
95
|
|
|
except: |
96
|
|
|
raise |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
def valuedispatch(func): |
100
|
|
|
_func = singledispatch(func) |
101
|
|
|
|
102
|
|
|
@wraps(func) |
103
|
|
|
def wrapper(*args, **kwargs): |
104
|
|
|
return _func.registry.get(args[0], _func)(*args, **kwargs) |
105
|
|
|
|
106
|
|
|
wrapper.register = _func.register |
107
|
|
|
wrapper.dispatch = _func.dispatch |
108
|
|
|
wrapper.registry = _func.registry |
109
|
|
|
return wrapper |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
def make_piechart(id, data, unit='users'): |
113
|
|
|
xdata = [i[0] for i in data] |
114
|
|
|
ydata = [i[1] for i in data] |
115
|
|
|
|
116
|
|
|
extra_serie = { |
117
|
|
|
"tooltip": {"y_start": "", "y_end": " " + unit}, |
118
|
|
|
} |
119
|
|
|
chartdata = {'x': xdata, 'y1': ydata, 'extra1': extra_serie} |
120
|
|
|
charttype = "pieChart" |
121
|
|
|
chartcontainer = 'chart_container_%s' % id # container name |
122
|
|
|
|
123
|
|
|
data = { |
124
|
|
|
'charttype': charttype, |
125
|
|
|
'chartdata': chartdata, |
126
|
|
|
'chartcontainer': chartcontainer, |
127
|
|
|
'extra': { |
128
|
|
|
'x_is_date': False, |
129
|
|
|
'x_axis_format': '', |
130
|
|
|
'tag_script_js': True, |
131
|
|
|
'jquery_on_ready': False, |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
return data |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
def make_discrete_bar_chart(id, data): |
138
|
|
|
xdata = [i[0] for i in data] |
139
|
|
|
ydata = [i[1] for i in data] |
140
|
|
|
|
141
|
|
|
extra_serie1 = {"tooltip": {"y_start": "", "y_end": " cal"}} |
142
|
|
|
chartdata = { |
143
|
|
|
'x': xdata, 'name1': '', 'y1': ydata, 'extra1': extra_serie1, |
144
|
|
|
} |
145
|
|
|
charttype = "discreteBarChart" |
146
|
|
|
chartcontainer = 'chart_container_%s' % id # container name |
147
|
|
|
data = { |
148
|
|
|
'charttype': charttype, |
149
|
|
|
'chartdata': chartdata, |
150
|
|
|
'chartcontainer': chartcontainer, |
151
|
|
|
'extra': { |
152
|
|
|
'x_is_date': False, |
153
|
|
|
'x_axis_format': '', |
154
|
|
|
'tag_script_js': True, |
155
|
|
|
'jquery_on_ready': True, |
156
|
|
|
}, |
157
|
|
|
} |
158
|
|
|
return data |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
def get_month_range_from_dict(source): |
162
|
|
|
""" |
163
|
|
|
:param request: dictionary with keys 'start' and 'end |
164
|
|
|
:return: a tuple of datatime objects in the form (start, end) |
165
|
|
|
""" |
166
|
|
|
now = timezone.now() |
167
|
|
|
start = source.get('start') |
168
|
|
|
if not start: |
169
|
|
|
start = datetime.datetime(now.year-1, now.month+1, 1) if now.month != 12 else datetime.datetime(now.year, now.month, 1) |
170
|
|
|
|
171
|
|
|
end = source.get('end', datetime.datetime(now.year, now.month, calendar.monthrange(now.year, now.month)[1])) |
172
|
|
|
return start, end |
173
|
|
|
|