Commit df4cbc69 authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

added data dao

parent eaffc178
import psycopg2
from psycopg2 import sql
# decorator implementation
def with_psql(f):
def _with_psql(*args, **kwargs):
conn = psycopg2.connect('dbname=gateway')
cur = conn.cursor()
try:
res = f(cur, *args, **kwargs)
except (Exception, psycopg2.DatabaseError) as error:
conn.rollback()
res = (False, error)
else:
conn.commit()
finally:
cur.close()
conn.close()
return res
return _with_psql
@with_psql
def create_table(cur, appkey, dev_id):
tn = 'dev_' +str(appkey)+ '_' +str(dev_id)
cur.execute(
sql.SQL(
"""CREATE TABLE {} (
utc NUMERIC(10) NOT NULL,
timedate VARCHAR(100) NOT NULL,
data json NOT NULL
)"""
).format(sql.Identifier(tn)))
return (True,)
@with_psql
def delete_table(cur, appkey, dev_id):
tn = 'dev_' +str(appkey)+ '_' +str(dev_id)
cur.execute(
psycopg2.sql.SQL(
"DROP TABLE {}"
).format(sql.Identifier(tn)))
return (True,)
@with_psql
def get_last_n(cur, appkey, dev_id, n):
tn = 'dev_' +str(appkey)+ '_' +str(dev_id)
query = """
SELECT * FROM
{}
ORDER BY
utc DESC
LIMIT %s
"""
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)
@with_psql
def get_all(cur, appkey, devid):
tn = 'dev_' +str(appkey)+ '_' +str(dev_id)
query = """
SELECT * FROM
{}
"""
cur.execute(
sql.SQL(query).format(sql.Identifier(tn)))
return (True, cur.fetchall())
......@@ -36,6 +36,7 @@ class DeviceDao:
sql.SQL(
"""CREATE TABLE {} (
utc NUMERIC(10) NOT NULL,
timedate VARCHAR(100) NOT NULL,
data json NOT NULL
)"""
).format(sql.Identifier(tn)))
......
No preview for this file type
......@@ -175,7 +175,7 @@ def dev():
dh.delete(session['appkey'], request.form['devid'])
return render_template('add-dev.html', feedback=res[1])
else:
return redirect(url_for('app'))
return redirect(url_for('app', appkey=session['appkey']))
@server.route('/dev-conf', methods=['GET', 'POST'])
......@@ -206,6 +206,7 @@ def dev_conf():
return redirect(url_for('dev', id=session['devid']))
@server.route('/delete-dev')
def delete_dev():
dh = dd.DeviceDao()
......@@ -215,6 +216,10 @@ def delete_dev():
return redirect(url_for('app', appkey=session['appkey']))
@server.route('/dev-data')
def dev_data():
pass
if __name__ == '__main__':
server.secret_key = 'sdjfklsjf^$654sd^#sPH'
server.run(debug = True, host='0.0.0.0')
......
{% extends 'layout.html' %}
{% block title %} Device Data: {% endblock %}
{% block content %}
<div class="row">
<div class="col-md-6 col-md-offset-3">
<p>Last 5 messages:</p>
<table>
{% for data %}
<tr>
<th> {{ data[1] }} </th>
<th> {{ data[2] }} </th>
</tr>
{% endfor %}
</table>
<p>Total {{ total }} messages.</p>
<a href="/data-csv"><button type="submit" class="btn btn-primary">Download CSV</button></a>
</div>
</div>
<script type="text/javascrypt">
function conf() {
return confirm("Are you sure? It will remove all device data.")
}
</script>
{% endblock %}
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