Commit b54e43a4 authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

dashboard recent activity retrieval optimization

parent aa74c771
...@@ -41,10 +41,10 @@ def get_user_data_count_per_hour(cur, username, hour): ...@@ -41,10 +41,10 @@ def get_user_data_count_per_hour(cur, username, hour):
i = 0 i = 0
for a in apps: for a in apps:
for d in devs[i]: for d in devs[i]:
query += 'SELECT COUNT(*) FROM dev_{}_{} UNION ALL '.format(a[1], d[1]) query += 'SELECT utc FROM dev_{}_{} UNION ALL '.format(a[1], d[1])
i += 1 i += 1
query = query[0:-10] query = query[0:-10]
query += ') SELECT SUM(count) FROM t WHERE utc > {} AND utc < {}'.format(utc_hour, utc_hour+60*60) query += ') SELECT COUNT(*) FROM t WHERE utc > {} AND utc < {}'.format(utc_hour, utc_hour+60*60)
#print(query) #print(query)
cur.execute(query, ()) cur.execute(query, ())
...@@ -52,6 +52,34 @@ def get_user_data_count_per_hour(cur, username, hour): ...@@ -52,6 +52,34 @@ def get_user_data_count_per_hour(cur, username, hour):
return (True,cur.fetchone()) return (True,cur.fetchone())
@with_psql
def get_user_data_count_per_hour_period(cur, username, period):
apps = ad.get_list(username)[1]
devs = []
for a in apps:
devs.append(dd.get_list(a[1])[1])
utc_hour = [utc_roundhour(x) for x in range(period,-1,-1)]
query = 'WITH t AS ('
i = 0
for a in apps:
for d in devs[i]:
query += 'SELECT utc FROM dev_{}_{} UNION ALL '.format(a[1], d[1])
i += 1
query = query[0:-10]
query += ') SELECT * FROM ('
for uh in utc_hour:
query +=' SELECT COUNT(*) FROM t WHERE utc > {} AND utc < {} UNION ALL'.format(uh, uh+60*60)
query = query[0:-9]
query += ') w'
cur.execute(query, ())
return (True,cur.fetchall())
@with_psql @with_psql
def get_user_data_count_per_day(cur, username, day=0): def get_user_data_count_per_day(cur, username, day=0):
apps = ad.get_list(username)[1] apps = ad.get_list(username)[1]
...@@ -76,6 +104,31 @@ def get_user_data_count_per_day(cur, username, day=0): ...@@ -76,6 +104,31 @@ def get_user_data_count_per_day(cur, username, day=0):
return (True,cur.fetchone()) return (True,cur.fetchone())
@with_psql
def get_user_data_count_per_day_period(cur, username, period):
apps = ad.get_list(username)[1]
devs = []
for a in apps:
devs.append(dd.get_list(a[1])[1])
utc_day = utc_roundday(day)
query = 'WITH t AS ('
i = 0
for a in apps:
for d in devs[i]:
query += 'SELECT utc FROM dev_{}_{} UNION ALL '.format(a[1], d[1])
i += 1
query = query[0:-10]
query += ') SELECT COUNT(*) FROM t WHERE utc > {} AND utc < {}'.format(utc_day, utc_day+24*60*60)
cur.execute(query, ())
return (True,cur.fetchone())
@with_psql @with_psql
def get_recent_activity(cur, username, n=5): def get_recent_activity(cur, username, n=5):
apps = ad.get_list(username)[1] apps = ad.get_list(username)[1]
...@@ -96,7 +149,6 @@ def get_recent_activity(cur, username, n=5): ...@@ -96,7 +149,6 @@ def get_recent_activity(cur, username, n=5):
UNION ALL""".format(a[1],d[1], a[0],d[0]) UNION ALL""".format(a[1],d[1], a[0],d[0])
query = query[0:-9] query = query[0:-9]
query += ' ORDER BY utc DESC LIMIT {}'.format(n) query += ' ORDER BY utc DESC LIMIT {}'.format(n)
print(query)
cur.execute(query, ()) cur.execute(query, ())
......
...@@ -33,17 +33,22 @@ def index(): ...@@ -33,17 +33,22 @@ def index():
active_devices = dd.get_count_by_user(session['name']) active_devices = dd.get_count_by_user(session['name'])
total_activity = md.get_user_data_count(session['name'])[1][0] total_activity = md.get_user_data_count(session['name'])[1][0]
last_activity = md.get_user_data_count_per_day(session['name'])[1][0] last_activity = md.get_user_data_count_per_day(session['name'])[1][0]
recent_activity = md.get_recent_activity(session['name'])[1] recent_activity = md.get_recent_activity(session['name'])[1]
#print('created_apps', created_apps) #print('created_apps', created_apps)
#print('active_devices', active_devices) #print('active_devices', active_devices)
#print('total_activity', total_activity) #print('total_activity', total_activity)
#print('last_activity', last_activity) #print('last_activity', last_activity)
info = [created_apps, active_devices, total_activity, last_activity] info = [created_apps, active_devices, total_activity, last_activity]
return render_template('new/public/dashboard.html', info=info, recent_activity=recent_activity) r = md.get_user_data_count_per_hour_period(session['name'], 12)[1]
r = [x[0] for x in r]
day_chart = {}
for i in range(12,-1,-1):
day_chart[misc.utc_hour(i)] = r[i]
return render_template('new/public/dashboard.html', info=info, recent_activity=recent_activity, day_chart=day_chart)
#apps = ad.get_list(session['name']) #apps = ad.get_list(session['name'])
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment