-
Notifications
You must be signed in to change notification settings - Fork 2
/
get_user_stat.py
50 lines (47 loc) · 1.8 KB
/
get_user_stat.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
#!/usr/bin/env python
from core.sg2_users import user as u
from core.database.sg2_database_utils import image_database
import numpy as np
import datetime
import json
import sys
import get_config as gc
cf = gc.get_config('config.dat')
db = image_database(**cf['sg2'])
def get_statistics(user_name, time_unit):
now = datetime.datetime.now()
if time_unit == 'day':
x = range(-23,1)
hour_begin = datetime.datetime(now.year, now.month, now.day, now.hour)
times = [hour_begin+datetime.timedelta(hours=float(h)) for h in x]
xlabels = [t.strftime('%I %p') for t in times]
times.append(now)
elif time_unit == 'week':
x = range(-6,1)
day_begin = datetime.datetime(now.year, now.month, now.day)
times = [day_begin+datetime.timedelta(days=float(d)) for d in x]
xlabels = [t.strftime('%a %d %b %Y') for t in times]
times.append(now)
elif time_unit == 'year':
x = range(-51,1)
day_week = now.weekday()
monday = now+datetime.timedelta(days=-float(day_week))
week_start = datetime.datetime(monday.year, monday.month, monday.day)
times = [week_start+datetime.timedelta(days=float(w)*7) for w in x]
times.append(now)
xlabels = [''] * (len(times)-1)
current_month = times[0].month
for ii, t in enumerate(times):
if t.month != current_month:
xlabels[ii] = t.strftime('%b %Y')
current_month = t.month
user = u.USER(user_name)
y = []
for ii in range(len(times)-1):
result = db.get_rate_time('sg2_image_rate', user_name, times[ii], times[ii+1] )
y.append(len(result))
return json.dumps((y, xlabels))
if __name__== "__main__":
username = sys.argv[1]
timeu = sys.argv[2]
print get_statistics(username,timeu)