Skip to content

Commit 174c006

Browse files
authored
Speed and performance improvements
Add compression support, optimize SQL queries, add support for sample resolution to reduce fetched data.
1 parent 2429d81 commit 174c006

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

mqttdblg/config.ini

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ ROOT_TOPIC = solpiplog/pip
1414
# Subtopic(s) to subscribe to. For all subtopics use "+" or "#". Example: "+".
1515
SUB_TOPIC = +
1616
# QOS to use for subscription. 0, 1 or 2.
17-
QOS = 1
17+
QOS = 2
1818
# Days to retain data in database. Use with caution, database may become very large.
1919
AGE_LIMIT = 30
20+
# Sample resolution for graph. Increase this to speed up rendering. Load and siplay only every "n"th sample, where n = value below.
21+
SAMPLE_RESOLUTION = 6
2022
# Database file name to use for logging data. Database will be created automatically.
2123
DATABASE_FILE = /etc/mqttdblg/database/mqttdblogger.db
2224
# Run a vacuum on the database on startup. Good for maintenance but will slow down startup on a large DB.
@@ -30,4 +32,4 @@ DEFAULT_GRAPH = acin
3032
# Topics to graph, used in the dropdown selection box. JSON formatted, topic first, pretty label after.
3133
GRAPHED_TOPICS = {"load":"Load (%)", "acoutw":"Load (W)", "heatsinktemp":"Temp (°C)", "acin":"AC In (V)", "acinhz":"AC In (Hz)", "acout":"AC Out (V)", "acouthz":"AC Out (Hz)", "battv":"Battery (V)", "battcappa":"Capacity (%)", "battdischrg":"Discharge (A)"}
3234
# Page title
33-
PAGE_TITLE = Historical Data
35+
PAGE_TITLE = Synapse 3.0V+ History

mqttdblg/mqttdbgrapher.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import configparser
22
from flask import Flask, render_template, request
3+
from flask_compress import Compress
34
import sqlite3
45
import json
56

@@ -12,16 +13,19 @@
1213
default_range = int(config.get('config', 'DEFAULT_RANGE'))
1314
default_graph = config.get('config', 'DEFAULT_GRAPH')
1415
graphed_topics = json.loads(config.get('config', 'GRAPHED_TOPICS'))
16+
sample_resolution = int(config.get('config', 'SAMPLE_RESOLUTION'))
1517

1618
app = Flask(__name__)
19+
compress = Compress(app)
1720

1821
@app.route("/<datasource>.json")
1922
def load(datasource):
2023
connection = sqlite3.connect(database_file)
2124
cursor = connection.cursor()
22-
cursor.execute(f"SELECT (strftime('%s', DATETIME(timestamp, 'unixepoch', 'localtime')))*1000, CAST(payload AS DECIMAL) FROM mqtt_log WHERE topic = '{root_topic}/{datasource}' ORDER BY timestamp ASC")
25+
cursor.execute(f"SELECT (strftime('%s', DATETIME(timestamp, 'unixepoch', 'localtime')))*1000 as localtimemillis, CAST(payload AS DECIMAL) FROM mqtt_log WHERE topic = '{root_topic}/{datasource}' AND (rowid % {sample_resolution}) = 0 ORDER BY localtimemillis ASC")
2326
results = cursor.fetchall()
2427
return json.dumps(results)
28+
cursor.execute("PRAGMA OPTIMIZE")
2529

2630
@app.route("/graph")
2731
def graph():

mqttdblg/mqttdblogger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def main():
6363
topic TEXT NOT NULL,
6464
payload TEXT NOT NULL,
6565
PRIMARY KEY (timestamp, topic));
66-
CREATE INDEX IF NOT EXISTS mqtt_log_idx ON mqtt_log(topic, timestamp, payload);
66+
CREATE INDEX IF NOT EXISTS mqtt_log_idx ON mqtt_log(topic, timestamp ASC, payload);
6767
'''
6868

6969
cursor = db_conn.cursor()

0 commit comments

Comments
 (0)