From 5dc2b866f40e61929a91f00fe6f9b58ab73ef5a9 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Tue, 24 Jan 2023 20:16:12 +1100 Subject: [PATCH] Display devices and there status --- app.py | 6 ++++++ db.py | 14 ++++++++++++++ graph.py | 4 ++++ templates/base.html | 2 ++ templates/devices.html | 26 ++++++++++++++++++++++++++ 5 files changed, 52 insertions(+) create mode 100644 templates/devices.html diff --git a/app.py b/app.py index b44827c..0f57c57 100644 --- a/app.py +++ b/app.py @@ -50,6 +50,12 @@ def home(): return render_template('base.html') +@ app.route("/devices") +def devices(): + devices = db.get_devices() + return render_template('devices.html', devices=devices) + + @app.route("/overview") def overview(): cadences = db.get_all_cadences() diff --git a/db.py b/db.py index 0b7ae9d..9ca250a 100644 --- a/db.py +++ b/db.py @@ -42,3 +42,17 @@ class DataBase(): def get_all_cadences(self): return self.execute('SELECT * FROM cadence') + + def get_devices(self): + return self.execute(""" + SELECT DEVICE.ID, + DEVICE.NAME, + MAX(CADENCE.LOGGED_AT) AS LAST_LOGGED_AT, + CASE WHEN(NOW() < (MAX(CADENCE.LOGGED_AT) + (INTERVAL '5 min'))) THEN 'yes' + ELSE 'no' + END AS IS_ACTIVE + FROM DEVICE + LEFT JOIN CADENCE ON DEVICE.ID = CADENCE.DEVICE_ID + GROUP BY DEVICE.ID + ORDER BY LAST_LOGGED_AT + """) diff --git a/graph.py b/graph.py index 5d54430..63d9f99 100644 --- a/graph.py +++ b/graph.py @@ -14,6 +14,10 @@ custom_style = Style( def generate_graph(x_labels, data, style=custom_style): + """ + Generate SVG line graph using pygal + example usage: generate_graph([1,2,3], [("RPM", [1,2,3]),("Power", [2,4,6])]) + """ graph = pygal.Line(show_y_guides=False, show_legend=False, style=style) graph.x_labels = x_labels diff --git a/templates/base.html b/templates/base.html index 19a998f..809079f 100644 --- a/templates/base.html +++ b/templates/base.html @@ -15,6 +15,8 @@
+
+
diff --git a/templates/devices.html b/templates/devices.html new file mode 100644 index 0000000..8a3dbf9 --- /dev/null +++ b/templates/devices.html @@ -0,0 +1,26 @@ +
+ + + + + + + + + {% for d in devices %} + + + + + {% endfor %} + +
+ Name + + Status +
+ {{d['name']}} + + {{d['is_active']}} +
+
\ No newline at end of file