Replace MySQL with SQLite, fix Jinja2 template syntax, and add README

This commit is contained in:
j
2026-03-07 19:50:14 +13:00
parent bd907c8d40
commit d8754a9d92
9 changed files with 104 additions and 58 deletions

View File

@@ -7,8 +7,6 @@ from datetime import datetime, timezone
from concurrent.futures import ThreadPoolExecutor, as_completed
import paramiko
import pymysql
pymysql.install_as_MySQLdb()
from flask import Flask, render_template, jsonify
from flask_sqlalchemy import SQLAlchemy
@@ -16,12 +14,10 @@ from flask_sqlalchemy import SQLAlchemy
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
logger = logging.getLogger(__name__)
DB_PATH = '/app/data/infmap.db'
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = (
f"mysql+pymysql://{os.environ['MYSQL_USER']}:{os.environ['MYSQL_PASSWORD']}"
f"@{os.environ.get('MYSQL_HOST', 'db')}/{os.environ['MYSQL_DATABASE']}"
)
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {'pool_recycle': 280, 'pool_pre_ping': True}
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_PATH}'
db = SQLAlchemy(app)
COLLECTION_INTERVAL = int(os.environ.get('COLLECTION_INTERVAL', 300))
@@ -349,19 +345,10 @@ def usage_color(percent):
# --- Main ---
if __name__ == '__main__':
# Wait for database
for attempt in range(30):
try:
with app.app_context():
db.create_all()
logger.info("Database ready")
break
except Exception as e:
logger.info("Waiting for database... (%s)", e)
time.sleep(2)
else:
logger.error("Could not connect to database after 30 attempts")
exit(1)
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
with app.app_context():
db.create_all()
logger.info("Database ready at %s", DB_PATH)
# Start collector thread
collector_thread = threading.Thread(target=collector_loop, daemon=True)

View File

@@ -92,11 +92,11 @@ done
# GPUs
gpu_idx=0
lspci 2>/dev/null | grep -iE 'vga|3d|display' | while read -r line; do
while read -r line; do
echo "[gpu:$gpu_idx]"
echo "description=$line"
gpu_idx=$((gpu_idx + 1))
done
done < <(lspci 2>/dev/null | grep -iE 'vga|3d|display')
# Network interfaces
for iface in $(ls /sys/class/net/ 2>/dev/null); do

View File

@@ -1,5 +1,4 @@
flask==3.1.*
flask-sqlalchemy==3.1.*
pymysql==1.1.*
paramiko==3.5.*
cryptography>=43.0

View File

@@ -142,7 +142,7 @@
<td>{{ iface.get('ipv6', '-') or '-' }}</td>
<td>{{ iface.get('mac', '-') or '-' }}</td>
<td>{{ iface.get('state', '-') }}</td>
<td>{% if iface.get('speed_mbps') %}{{ iface.speed_mbps }} Mbps{% else %}-{% endif %}</td>
<td>{% if iface.get('speed_mbps') %}{{ iface.get('speed_mbps') }} Mbps{% else %}-{% endif %}</td>
<td>{{ iface.get('driver', '-') or '-' }}</td>
</tr>
{% endfor %}
@@ -203,7 +203,7 @@
{% if d.get('error') %}
<div class="detail-section wide">
<h4>Error</h4>
<p class="error-text">{{ d.error }}</p>
<p class="error-text">{{ d.get('error') }}</p>
</div>
{% endif %}
</div>