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

implemeted pagination of data

parent b5f9b93d
No preview for this file type
...@@ -46,6 +46,27 @@ def get_last_n(cur, appkey, devid, n): ...@@ -46,6 +46,27 @@ def get_last_n(cur, appkey, devid, n):
return (True, data) return (True, data)
@with_psql
def get_last_range(cur, appkey, devid, r):
tn = 'dev_' +str(appkey)+ '_' +str(devid)
query = """
SELECT * FROM
{}
ORDER BY
utc DESC
LIMIT %s OFFSET %s
"""
cur.execute(
sql.SQL(query).format(sql.Identifier(tn)), [r[0],r[1]])
data = cur.fetchall()
if (data == []):
return (False, 'There is no data for the device.')
else:
return (True, data)
@with_psql @with_psql
def get_all(cur, appkey, devid): def get_all(cur, appkey, devid):
tn = 'dev_' +str(appkey)+ '_' +str(devid) tn = 'dev_' +str(appkey)+ '_' +str(devid)
......
No preview for this file type
{% extends 'layout.html' %}
{% block title %} Device Data: {% 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">
<div class="panel panel-success">
<div class="panel-heading">
<h2><center> {{ devname }} data </center></h2>
</div>
<div class="panel-body">
{% if data %}
<table class="table">
<thead>
<th>Time</th>
<th>Content</th>
</thead>
<tbody>
{% for d in data %}
<tr>
<th> {{ d[1] }} </th>
<th>
{% for k in d[2] %}
{{ k }} : {{ d[2][k] }} <br>
{% endfor %}
</th>
</tr>
{% endfor %}
</tbody>
</table>
<p>Total: <strong>{{ total }}</strong> messages.</p>
<center>
<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>
</center>
<br>
<center>
<a href="/data-csv"><button type="submit" class="btn btn-primary">Download CSV</button></a>
</center>
{% else %}
<h3><center>Device have not sent any data yet.</center></h3>
{% endif %}
</div>
</div>
<script type="text/javascrypt">
function conf() {
return confirm("Are you sure? It will remove all device data.")
}
</script>
{% endblock %}
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
<br><br> <br><br>
<center> <center>
<a href="/dev-data"><button type="submit" class="btn btn-primary">Data</button></a> <a href="/dev-data-pg"><button type="submit" class="btn btn-primary">Data</button></a>
<a href="/dev-conf"><button type="submit" class="btn btn-primary">Configure</button></a> <a href="/dev-conf"><button type="submit" class="btn btn-primary">Configure</button></a>
<a href="/delete-dev"><button type="submit" class="btn btn-danger" onclick="return confirm('Are you sure? It will remove all device data.');">Delete Device</button></a> <a href="/delete-dev"><button type="submit" class="btn btn-danger" onclick="return confirm('Are you sure? It will remove all device data.');">Delete Device</button></a>
</center> </center>
......
...@@ -15,6 +15,11 @@ import binascii ...@@ -15,6 +15,11 @@ import binascii
import os import os
MAX_PG = 5
MAX_PG_ENTRIES_USERS = 10
MAX_PG_ENTRIES_DATA = 10
@app.route('/') @app.route('/')
def index(): def index():
if 'name' in session and len(session['name']) > 0: if 'name' in session and len(session['name']) > 0:
...@@ -242,11 +247,6 @@ def dev_conf(): ...@@ -242,11 +247,6 @@ def dev_conf():
pend.create(session['appkey'], session['devid'], base64_args) pend.create(session['appkey'], session['devid'], base64_args)
#print('msg = ', args)
#print('base64 = ', base64_args)
#print(type(request.form['arg'].encode('utf-8')))
#print(request.form['arg'].encode('utf-8'))
return redirect(url_for('dev', id=session['devid'])) return redirect(url_for('dev', id=session['devid']))
else: else:
return redirect(url_for('index')) return redirect(url_for('index'))
...@@ -286,9 +286,6 @@ def dev_data(): ...@@ -286,9 +286,6 @@ def dev_data():
if count[1][0] < 10: if count[1][0] < 10:
last_ctr = count[1][0] last_ctr = count[1][0]
#print(last[1][2][2])
#print(type(last[1][2][2]))
#print(count)
if count[1][0] > 0: if count[1][0] > 0:
return render_template('public/dev-data.html', data=last[1], total=count[1][0], lastctr=last_ctr, devname=session['devname']) return render_template('public/dev-data.html', data=last[1], total=count[1][0], lastctr=last_ctr, devname=session['devname'])
else: else:
...@@ -296,6 +293,55 @@ def dev_data(): ...@@ -296,6 +293,55 @@ def dev_data():
else: else:
return redirect(utl_for('index')) return redirect(utl_for('index'))
@app.route('/dev-data-pg')
def dev_data_pg():
if 'name' in session and 'devid' in session:
cur_pg = 1
if request.args.get('p'):
cur_pg = int(request.args.get('p'))
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]
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'])
else:
return render_template('public/dev-data-pg.html', devname=session['devname'])
else:
return redirect(utl_for('index'))
@app.route('/data-csv') @app.route('/data-csv')
def data_csv(): def data_csv():
if 'name' in session and 'devid' in session: if 'name' in session and 'devid' in session:
......
No preview for this file type
No preview for this file type
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