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