working version
This commit is contained in:
parent
0666a58162
commit
51d7b6c239
6 changed files with 23 additions and 10 deletions
4
a5/db.py
4
a5/db.py
|
|
@ -79,12 +79,12 @@ CREATE TABLE IF NOT EXISTS measurements
|
||||||
return stats
|
return stats
|
||||||
|
|
||||||
def get_page(self, page: int, page_size: int = 20) -> List[sqlite3.Row]:
|
def get_page(self, page: int, page_size: int = 20) -> List[sqlite3.Row]:
|
||||||
"""Gets a paged list of all measurements. Page is 0 indexed."""
|
"""Gets a paged list of all measurements. Page is 1 indexed."""
|
||||||
with closing(self._conn.cursor()) as cursor:
|
with closing(self._conn.cursor()) as cursor:
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
# sql
|
# sql
|
||||||
"SELECT timestamp, tvoc, co2 FROM measurements ORDER BY timestamp DESC LIMIT ?, ?",
|
"SELECT timestamp, tvoc, co2 FROM measurements ORDER BY timestamp DESC LIMIT ?, ?",
|
||||||
(page_size, (page - 1) * page_size),
|
((page - 1) * page_size, page_size),
|
||||||
)
|
)
|
||||||
return cursor.fetchall()
|
return cursor.fetchall()
|
||||||
|
|
||||||
|
|
|
||||||
BIN
a5/greetings.db
BIN
a5/greetings.db
Binary file not shown.
1
a5/main.m5f
Normal file
1
a5/main.m5f
Normal file
File diff suppressed because one or more lines are too long
|
|
@ -1,3 +1,4 @@
|
||||||
|
from datetime import datetime
|
||||||
from time import time
|
from time import time
|
||||||
|
|
||||||
from flask import Flask, g, render_template, request
|
from flask import Flask, g, render_template, request
|
||||||
|
|
@ -10,6 +11,11 @@ app = Flask(__name__)
|
||||||
htmx = HTMX(app)
|
htmx = HTMX(app)
|
||||||
|
|
||||||
|
|
||||||
|
@app.template_filter()
|
||||||
|
def format_datetime(value):
|
||||||
|
return datetime.fromtimestamp(value).isoformat()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/report", methods=["POST"])
|
@app.route("/api/report", methods=["POST"])
|
||||||
def report() -> ResponseReturnValue:
|
def report() -> ResponseReturnValue:
|
||||||
"""Adds a measurement"""
|
"""Adds a measurement"""
|
||||||
|
|
@ -33,7 +39,7 @@ def stats() -> ResponseReturnValue:
|
||||||
"""Gets overall statistics as well as few recent measurements"""
|
"""Gets overall statistics as well as few recent measurements"""
|
||||||
db = get_db()
|
db = get_db()
|
||||||
stats = db.get_stats()
|
stats = db.get_stats()
|
||||||
recents = db.get_page(0, 10)
|
recents = db.get_page(1, 10)
|
||||||
template = "page/index.html.j2"
|
template = "page/index.html.j2"
|
||||||
if htmx:
|
if htmx:
|
||||||
template = "block/index.html.j2"
|
template = "block/index.html.j2"
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,14 @@
|
||||||
<div hx-get="/" hx-trigger="every 2s" hx-target="main"></div>
|
<div hx-get="/?htmx" hx-trigger="every 2s" hx-target="main"></div>
|
||||||
|
|
||||||
<h1>Overview</h1>
|
<h1>Overview</h1>
|
||||||
|
|
||||||
<article class="card">
|
<article class="card">
|
||||||
<div class="contents">
|
<div class="contents">
|
||||||
Count {{ stats.count }}
|
<div>
|
||||||
|
<b>Count {{ stats.count }}</b> <br>
|
||||||
|
<b>TVOC min {{ stats.tvoc_min.tvoc }}</b> @ {{ stats.tvoc_min.timestamp|format_datetime }} <b>max {{ stats.tvoc_max.tvoc }}</b> @ {{ stats.tvoc_max.timestamp|format_datetime }} <br>
|
||||||
|
<b>CO2 min {{ stats.co2_min.co2 }}</b> @ {{ stats.co2_min.timestamp|format_datetime }} <b>max {{ stats.co2_max.co2 }}</b> @ {{ stats.co2_max.timestamp|format_datetime }}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</article>
|
</article>
|
||||||
|
|
||||||
|
|
@ -14,8 +18,10 @@
|
||||||
<article class="card">
|
<article class="card">
|
||||||
<div class="contents">
|
<div class="contents">
|
||||||
<header>@ {{row['timestamp']}}</header>
|
<header>@ {{row['timestamp']}}</header>
|
||||||
CO2: {{row['co2']}}
|
<div>
|
||||||
TVOC: {{row['tvoc']}}
|
CO2: <b>{{row['co2']}}</b>
|
||||||
|
TVOC: <b>{{row['tvoc']}}</b>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</article>
|
</article>
|
||||||
{% else %}
|
{% else %}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
<div hx-get="/measurements" hx-trigger="every 2s" hx-target="main"></div>
|
<div hx-get="/measurements?htmx" hx-trigger="every 2s" hx-target="main"></div>
|
||||||
|
|
||||||
<h1>Measurements</h1>
|
<h1>Measurements</h1>
|
||||||
<p>Page {{page}}</p>
|
<p>Page {{page}}</p>
|
||||||
|
|
@ -21,10 +21,10 @@
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
{% if page > 1 %}
|
{% if page > 1 %}
|
||||||
<a href="/measurements?page={{page+1}}" class="btn btn-link">Previous page</a>
|
<a href="/measurements?page={{page+1}}" hx-get="/measurements?page={{page+1}}&htmx" hx-target="main" class="btn btn-link">Previous page</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if has_next_page %}
|
{% if has_next_page %}
|
||||||
<a href="/measurements?page={{page-1}}" class="btn btn-link">Next page</a>
|
<a href="/measurements?page={{page-1}}" hx-get="/measurements?page={{page-1}}&htmx" hx-target="main" class="btn btn-link">Next page</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<a href="/" class="btn btn-link">View statistics</a>
|
<a href="/" class="btn btn-link">View statistics</a>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue