Commit 2b5a9a5f authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

dashboard added

parent f86d4de4
......@@ -68,4 +68,12 @@ def update(cur, appkey, name, desc):
return (True,)
@with_psql
def get_count(cur):
query = """
SELECT COUNT(*) FROM
applications
"""
cur.execute(query, ())
return (True, cur.fetchone())
......@@ -82,7 +82,7 @@ def get_all(cur, appkey, devid):
def get_count(cur, appkey, devid):
tn = 'dev_' +str(appkey)+ '_' +str(devid)
query = """
SELECT COUNT(utc) FROM
SELECT COUNT(*) FROM
{}
"""
cur.execute(
......
......@@ -105,3 +105,42 @@ def get_list(cur, appkey):
sql.SQL(query).format(sql.Identifier(tn)))
return (True, cur.fetchall())
@with_psql
def get_count(cur, appkey):
tn = 'devices_' +str(appkey)
query = """
SELECT COUNT(*) FROM
{}
"""
cur.execute(
sql.SQL(query).format(sql.Identifier(tn)), [appkey])
return (True, cur.fetchone())
@with_psql
def get_device_table_names(cur):
query = """
SELECT table_name FROM
information_schema.tables
WHERE
table_name ~ '^devices'
"""
cur.execute(query, ())
return(True, cur.fetchall())
def get_count_all():
count = 0
tns = get_device_table_names()
print(tns)
if tns[0] and len(tns[1]) > 0:
for tn in tns[1]:
sp = tn[0].split('_')
print(sp)
r = get_count(sp[1])
print(r)
if r[0]:
count += r[1][0]
return count
......@@ -62,3 +62,24 @@ def get(cur, name, password):
else:
return (False, 'Password or username do not match')
@with_psql
def get_count(cur):
query = """
SELECT COUNT(*) FROM
users
"""
cur.execute(query, ())
return (True, cur.fetchone())
@with_psql
def get_range(cur, rng):
query = """
SELECT * FROM
users
ORDER BY
name ASC
LIMIT %s OFFSET %s
"""
cur.execute(query, (rng[0],rng[1]))
return (True, cur.fetchall())
......@@ -74,3 +74,33 @@ def clean_data_folder():
os.remove(app.config['DATA_DOWNLOAD_DIR_OS']+'/'+f)
except OSError:
pass
def paging(cur_pg, ent_cnt, max_ent, max_pg):
npg = int(ent_cnt/max_ent)+1
ps = int(max_pg/2)
# next and previous pages
pp = False
np = False
pr = None # current pages range
if npg < max_pg:
# 1, ... , npg
pr = [1, npg+1]
else:
if cur_pg - ps <= 1:
# 1, 2, ..., MAX_PG >>
pr = [1, max_pg+1]
np = cur_pg + max_pg + ps
else:
if cur_pg + ps >= npg:
# << npg-MAX_PG-1, ..., npg
pp = cur_pg - max_pg
pr = [npg-max_pg-1, npg+1]
else:
# << cur_pg-ps, ... , cur_pg + ps >>
pp = cur_pg - max_pg
np = cur_pg + max_pg
pr = [cur_pg-ps, cur_pg+ps+1]
return [pp, pr, np]
{% extends 'layout.html' %}
{% block title %} Dashboard: {% endblock %}
{% block content %}
<div class="row">
<div class="col-md-3">
<div class="clickback">
<span class="glyphicon glyphicon-arrow-left"></span>
<p><a class="backlink" onclick="history.back(-1)"></a></p>
</div>
</div>
<div class="col-md-6">
<h2> General Server Statistics: </h2>
<br><br>
<table class="table">
<tbody>
<tr>
<th> Users: </th>
<th> {{ users_cnt }} </th>
</tr>
<tr>
<th> Applications: </th>
<th> {{ apps_cnt }} </th>
</tr>
<tr>
<th> Devices: </th>
<th> {{ dev_cnt }} </th>
</tr>
</tbody>
</table>
<br>
{% if users %}
<center><h3> List of users </h3>
<div>
<form action="dashboard" method="post" class="form-inline">
<div class="form-group">
<input type="text" maxlength="30" class="form-control" id="username" name="username" placeholder="Type username..."/>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Filter</button>
</div>
</form>
</div>
<br>
<div>
<ol class="list-group" start="{{ usn }}">
{% for u in users %}
<a href="users?name={{ u[0] }}">
<li class="list-group-item">
<strong> {{ u[0] }} </strong>
</li>
</a>
{% endfor %}
</ol>
</div>
<div>
<nav aria-label="Page navigation">
<ul class="pagination">
{% if pp %}
<li>
{% else %}
<li class="disabled">
{% endif %}
<a href="/dev-data-pg?p={{ pp }}" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
{% for i in range(pr[0],pr[1]) %}
{% if i == cp %}
<li class="active">
<a href="/dev-data-pg?p={{ i }}">
{{ i }}
<span class="sr-only">(current)</span>
</a>
</li>
{% else %}
<li>
<a href="/dev-data-pg?p={{ i }}"> {{ i }} </a>
</li>
{% endif %}
{% endfor %}
{% if np %}
<li>
{% else %}
<li class="disabled">
{% endif %}
<a href="/dev-data-pg?p={{ np }}" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
{% else %}
<p> No registered users. </p>
{% endif %}
<br><br>
<a href="signup"><button class="btn btn-primary">Add New User</button></a>
</center>
</div>
</div>
{% endblock %}
......@@ -303,40 +303,19 @@ def dev_data_pg():
if cur_pg < 1:
cur_pg = 1
ent_cnt = data.get_count(session['appkey'], session['devid'])
npg = int(ent_cnt[1][0]/MAX_PG_ENTRIES_DATA)+1
ps = int(MAX_PG/2)
last = data.get_last_range(session['appkey'], session['devid'], [MAX_PG_ENTRIES_DATA, (cur_pg-1)*MAX_PG_ENTRIES_DATA])
# next and previous pages
np = False
pp = False
pr = None # current pages range
if npg < MAX_PG:
# 1, ... , npg
pr = [1, npg+1]
else:
if cur_pg - ps <= 1:
# 1, 2, ..., MAX_PG >>
pr = [1, MAX_PG+1]
np = cur_pg + MAX_PG + ps
else:
if cur_pg + ps >= npg:
# << npg-MAX_PG-1, ..., npg
pp = cur_pg - MAX_PG
pr = [npg-MAX_PG-1, npg+1]
else:
# << cur_pg-ps, ... , cur_pg + ps >>
pp = cur_pg - MAX_PG
np = cur_pg + MAX_PG
pr = [cur_pg-ps, cur_pg+ps+1]
ent_cnt = data.get_count(session['appkey'], session['devid'])
if ent_cnt[0]:
# range data
rd = misc.paging(cur_pg, ent_cnt[1][0], MAX_PG_ENTRIES_DATA, MAX_PG)
if ent_cnt[1][0] > 0:
return render_template('public/dev-data-pg.html', data=last[1], total=ent_cnt[1][0], cp=cur_pg, np=np, pp=pp, pr=pr, devname=session['devname'])
if ent_cnt[1][0] > 0:
return render_template('public/dev-data-pg.html', data=last[1], total=ent_cnt[1][0], cp=cur_pg, np=rd[2], pp=rd[0], pr=rd[1], devname=session['devname'])
else:
return render_template('public/dev-data-pg.html', devname=session['devname'])
else:
flash('Error: {}'.format(ent_cnt[1]), 'danger')
return render_template('public/dev-data-pg.html', devname=session['devname'])
else:
return redirect(utl_for('index'))
......@@ -369,5 +348,32 @@ def data_csv():
return redirect(utl_for('index'))
@app.route('/dashboard', methods=['GET', 'POST'])
def dashboard():
if 'role' in session and session['role'] == 'admin':
user_cnt = ud.get_count()
apps_cnt = ad.get_count()
devs_cnt = dd.get_count_all()
print(devs_cnt)
cur_pg = 1
if request.args.get('p'):
cur_pg = int(request.args.get('p'))
if cur_pg < 1:
cur_pg = 1
users = ud.get_range([MAX_PG_ENTRIES_USERS, (cur_pg-1)*MAX_PG_ENTRIES_USERS])
if request.method == 'GET':
users = ud.get_range([MAX_PG_ENTRIES_USERS, (cur_pg-1)*MAX_PG_ENTRIES_USERS])
rd = misc.paging(cur_pg, user_cnt[1][0], MAX_PG_ENTRIES_USERS, MAX_PG)
return render_template('admin/dashboard.html', users_cnt=user_cnt[1][0], apps_cnt=apps_cnt[1][0], dev_cnt=devs_cnt, users=users[1], pp=rd[0], pr=rd[1], np=rd[2], cp=cur_pg, usn=(cur_pg-1)*MAX_PG_ENTRIES_USERS+1)
else:
pass
else:
return redirect(url_for('index'))
def pend_delete_all_ack():
pend.delete_all_ack()
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