Commit 20afa51b authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

dev-data view works

parent df4cbc69
...@@ -23,8 +23,8 @@ def with_psql(f): ...@@ -23,8 +23,8 @@ def with_psql(f):
@with_psql @with_psql
def create_table(cur, appkey, dev_id): def create_table(cur, appkey, devid):
tn = 'dev_' +str(appkey)+ '_' +str(dev_id) tn = 'dev_' +str(appkey)+ '_' +str(devid)
cur.execute( cur.execute(
sql.SQL( sql.SQL(
"""CREATE TABLE {} ( """CREATE TABLE {} (
...@@ -37,8 +37,8 @@ def create_table(cur, appkey, dev_id): ...@@ -37,8 +37,8 @@ def create_table(cur, appkey, dev_id):
@with_psql @with_psql
def delete_table(cur, appkey, dev_id): def delete_table(cur, appkey, devid):
tn = 'dev_' +str(appkey)+ '_' +str(dev_id) tn = 'dev_' +str(appkey)+ '_' +str(devid)
cur.execute( cur.execute(
psycopg2.sql.SQL( psycopg2.sql.SQL(
"DROP TABLE {}" "DROP TABLE {}"
...@@ -47,8 +47,8 @@ def delete_table(cur, appkey, dev_id): ...@@ -47,8 +47,8 @@ def delete_table(cur, appkey, dev_id):
@with_psql @with_psql
def get_last_n(cur, appkey, dev_id, n): def get_last_n(cur, appkey, devid, n):
tn = 'dev_' +str(appkey)+ '_' +str(dev_id) tn = 'dev_' +str(appkey)+ '_' +str(devid)
query = """ query = """
SELECT * FROM SELECT * FROM
{} {}
...@@ -56,24 +56,36 @@ def get_last_n(cur, appkey, dev_id, n): ...@@ -56,24 +56,36 @@ def get_last_n(cur, appkey, dev_id, n):
utc DESC utc DESC
LIMIT %s LIMIT %s
""" """
cur.execute( cur.execute(
sql.SQL(query).format(sql.Identifier(tn)), [n]) sql.SQL(query).format(sql.Identifier(tn)), [n])
data = cur.fetchall() data = cur.fetchall()
if (data == []): if (data == []):
return (False, 'There is no data for the device.') return (False, 'There is no data for the device.')
else: else:
return (True, data) return (True, data)
@with_psql @with_psql
def get_all(cur, appkey, devid): def get_all(cur, appkey, devid):
tn = 'dev_' +str(appkey)+ '_' +str(dev_id) tn = 'dev_' +str(appkey)+ '_' +str(devid)
query = """ query = """
SELECT * FROM SELECT * FROM
{} {}
""" """
cur.execute( cur.execute(
sql.SQL(query).format(sql.Identifier(tn))) sql.SQL(query).format(sql.Identifier(tn)))
return (True, cur.fetchall()) return (True, cur.fetchall())
@with_psql
def get_count(cur, appkey, devid):
tn = 'dev_' +str(appkey)+ '_' +str(devid)
query = """
SELECT COUNT(utc) FROM
{}
"""
cur.execute(
sql.SQL(query).format(sql.Identifier(tn)))
return (True, cur.fetchone())
...@@ -6,6 +6,7 @@ import dao.user.user as ud ...@@ -6,6 +6,7 @@ import dao.user.user as ud
import dao.application.application as ad import dao.application.application as ad
import dao.device.device as dd import dao.device.device as dd
import dao.pend.pend as pend import dao.pend.pend as pend
import dao.data.data as data
import binascii import binascii
...@@ -126,7 +127,7 @@ def delete_app(): ...@@ -126,7 +127,7 @@ def delete_app():
devs = dh.get_list(session['appkey']) devs = dh.get_list(session['appkey'])
for dev in devs[1]: for dev in devs[1]:
dh.delete_datatable(session['appkey'], dev[1]) data.delete_table(session['appkey'], dev[1])
dh.delete_table(session['appkey']) dh.delete_table(session['appkey'])
...@@ -169,7 +170,7 @@ def dev(): ...@@ -169,7 +170,7 @@ def dev():
if not res[0]: if not res[0]:
return render_template('add-dev.html', feedback=res[1]) return render_template('add-dev.html', feedback=res[1])
else: else:
res = dh.create_datatable(session['appkey'], request.form['devid']) res = data.create_table(session['appkey'], request.form['devid'])
if not res[0]: if not res[0]:
dh.delete(session['appkey'], request.form['devid']) dh.delete(session['appkey'], request.form['devid'])
...@@ -210,15 +211,21 @@ def dev_conf(): ...@@ -210,15 +211,21 @@ def dev_conf():
@server.route('/delete-dev') @server.route('/delete-dev')
def delete_dev(): def delete_dev():
dh = dd.DeviceDao() dh = dd.DeviceDao()
dh.delete_datatable(session['appkey'], request.args.get('id')) data.delete_table(session['appkey'], session['devid'])
res = dh.delete(session['appkey'], request.args.get('id')) res = dh.delete(session['appkey'], session['devid'])
return redirect(url_for('app', appkey=session['appkey'])) return redirect(url_for('app', appkey=session['appkey']))
@server.route('/dev-data') @server.route('/dev-data')
def dev_data(): def dev_data():
pass last = data.get_last_n(session['appkey'], session['devid'], 5)
count = data.get_count(session['appkey'], session['devid'])
print(last)
print(count)
return render_template('dev-data.html', data=last[1], total=count[1][0])
if __name__ == '__main__': if __name__ == '__main__':
server.secret_key = 'sdjfklsjf^$654sd^#sPH' server.secret_key = 'sdjfklsjf^$654sd^#sPH'
......
...@@ -8,16 +8,22 @@ ...@@ -8,16 +8,22 @@
<div class="col-md-6 col-md-offset-3"> <div class="col-md-6 col-md-offset-3">
<p>Last 5 messages:</p> <p>Last 5 messages:</p>
<table> <table class="table">
{% for data %} <thead>
<th>Time</th>
<th>Content</th>
</thead>
<tbody>
{% for d in data %}
<tr> <tr>
<th> {{ data[1] }} </th> <th> {{ d[1] }} </th>
<th> {{ data[2] }} </th> <th> {{ d[2] }} </th>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody>
</table> </table>
<p>Total {{ total }} messages.</p> <p>Total: {{ total }} messages.</p>
<a href="/data-csv"><button type="submit" class="btn btn-primary">Download CSV</button></a> <a href="/data-csv"><button type="submit" class="btn btn-primary">Download CSV</button></a>
</div> </div>
......
...@@ -12,9 +12,9 @@ ...@@ -12,9 +12,9 @@
<p>Description : {{ dev[2] }} </p> <p>Description : {{ dev[2] }} </p>
<p>Last time up : {{ ltup }} </p> <p>Last time up : {{ ltup }} </p>
<a href="/data?id={{ dev[1] }}"><button type="submit" class="btn btn-primary">Data</button></a> <a href="/dev-data"><button type="submit" class="btn btn-primary">Data</button></a>
<a href="/dev-conf?id={{ dev[1] }}"><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?id={{ dev[1] }}"><button type="submit" class="btn btn-danger" onclick="return conf();">Delete Device</button></a> <a href="/delete-dev"><button type="submit" class="btn btn-danger" onclick="return conf();">Delete Device</button></a>
</div> </div>
</div> </div>
......
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