Collapse history sections to latest+modal; add charts and dashboard stats

This commit is contained in:
2026-04-14 09:06:10 -05:00
parent 080768bf92
commit cd3eb046d7
3 changed files with 367 additions and 90 deletions
+26 -3
View File
@@ -2,7 +2,8 @@ from flask import Flask, render_template, redirect, url_for, request, flash, abo
from sqlalchemy import create_engine, func
from sqlalchemy.orm import scoped_session, sessionmaker
from datetime import datetime
from datetime import datetime, date, timedelta
import json
from models import Base, Battery, BatteryPctLog, Device, CapacityTest, ChargeLog
@@ -61,10 +62,17 @@ def create_app(config_object="config"):
]
devices = db.query(Device).order_by(Device.name).all()
devices_with_slots = [d for d in devices if d.installed_count() < d.battery_slots]
one_year_ago = (date.today() - timedelta(days=365)).isoformat()
total_charges = db.query(func.count(ChargeLog.id)).scalar() or 0
charges_last_year = (db.query(func.count(ChargeLog.id))
.filter(ChargeLog.charged_date >= one_year_ago)
.scalar()) or 0
return render_template("dashboard.html", batteries=batteries,
storage_locations=storage_locations, devices=devices,
devices_with_slots=devices_with_slots,
ha_enabled=ha_client.enabled)
ha_enabled=ha_client.enabled,
total_charges=total_charges,
charges_last_year=charges_last_year)
# ------------------------------------------------------------------ #
# Battery — add
@@ -152,11 +160,26 @@ def create_app(config_object="config"):
.filter_by(battery_id=battery_id)
.order_by(BatteryPctLog.recorded_at.desc())
.all())
charge_logs_json = json.dumps([
{"id": l.id, "date": l.charged_date, "cycles": l.increment_cycles, "notes": l.notes or ""}
for l in charge_logs
])
capacity_tests_json = json.dumps([
{"id": t.id, "date": t.tested_date, "mah": t.tested_capacity_mah, "notes": t.notes or ""}
for t in sorted(capacity_tests, key=lambda t: (t.tested_date, t.id), reverse=True)
])
pct_logs_json = json.dumps([
{"recorded_at": str(l.recorded_at), "pct": l.percentage, "source": l.source or ""}
for l in pct_logs
])
return render_template("battery_detail.html", battery=battery,
storage_locations=storage_locations,
capacity_tests=capacity_tests,
charge_logs=charge_logs,
pct_logs=pct_logs)
pct_logs=pct_logs,
charge_logs_json=charge_logs_json,
capacity_tests_json=capacity_tests_json,
pct_logs_json=pct_logs_json)
# ------------------------------------------------------------------ #
# Battery — edit notes