Commit 9648ab54 authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

added functions for new dashboard

parent c707758f
from psycopg2 import sql
from app.helpers.misc import with_psql
# appkeys is a list of tuples [(app1), (app2), ..., (appn)]
# devids is a list of lists of tuples [[(dev1),...],[(dev1),...]]
@with_psql
def get_user_data_count(cur, apps, devs):
query = 'WITH t AS ('
i = 0
for a in apps:
for d in devs[i]:
query += """
SELECT COUNT(*) FROM dev_{}_{} UNION ALL
""".format(a[1], d[1])
i += 1
query += ') SELECT SUM(count) FROM t'
print(query)
cur.execute(query, ())
return (True,cur.fetchone())
@with_psql
def get_user_data_count_per_hour(cur, apps, devs, utc_hour):
query = 'WITH t AS ('
i = 0
for a in apps:
for d in devs[i]:
query += """
SELECT COUNT(*) FROM dev_{}_{} UNION ALL
""".format(a[1], d[1])
i += 1
query += ') SELECT SUM(count) FROM t WHERE utc > {} AND utc < {}'.format(utc_hour, utc_hour+60*60)
print(query)
cur.execute(query, ())
return (True,cur.fetchone())
@with_psql
def get_user_data_count_per_day(cur, apps, devs, utc_day):
query = 'WITH t AS ('
i = 0
for a in apps:
for d in devs[i]:
query += """
SELECT COUNT(*) FROM dev_{}_{} UNION ALL
""".format(a[1], d[1])
i += 1
query += ') SELECT SUM(count) FROM t WHERE utc > {} AND utc < {}'.format(utc_day, utc_day+24*60*60)
print(query)
cur.execute(query, ())
return (True,cur.fetchone())
@with_psql
def get_recent_activity(cur, apps, devs, n):
query = ''
i = 0
for a in apps:
for d in devs[i]:
query += """
(SELECT utc, appname, devname, data from
(SELECT utc, data from dev_{}_{} limit 5) AS utc,
(SELECT '{}' as appname) AS appname,
(SELECT {} as devname) AS devname)
UNION ALL
""".format( a[1],d[1],
a[0],d[0])
i += 1
query = query[0:-9]
query += 'ORDER BY utc DESC LIMIT 5'
print(query)
cur.execute(query, ())
return (True, cur.fetchall())
...@@ -3,6 +3,7 @@ from binascii import hexlify ...@@ -3,6 +3,7 @@ from binascii import hexlify
import os import os
import psycopg2 import psycopg2
import binascii import binascii
from datetime import datetime
def rand_str(length): def rand_str(length):
if length % 2 == 0: if length % 2 == 0:
...@@ -125,3 +126,20 @@ def pend_base64_encode(arg, confid): ...@@ -125,3 +126,20 @@ def pend_base64_encode(arg, confid):
i += 1 i += 1
return binascii.b2a_base64(args).decode('utf-8') return binascii.b2a_base64(args).decode('utf-8')
def utc_roundhour(hour_offset = 0):
n = datetime.utcnow()
return int((datetime(n.year, n.month, n.day, n.hour) - datetime(1970,1,1,hour_offset)).total_seconds())
def utc_roundday(day_offset = 0):
n = datetime.utcnow()
return int((datetime(n.year, n.month, n.day) - datetime(1970,1,1+day_offset)).total_seconds())
def utc_hour(hour_offset):
return ((datetime.utcnow().hour - hour_offset) % 24)
def utc_weekday(day_offset = 0):
d = {0:'Mo',1:'Tu',2:'We',3:'Th',4:'Fr',5:'Sa',6:'Su'}
return (d[((datetime.utcnow().weekday() - day_offset) % 7)])
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