Auto refresh
This commit is contained in:
+33
-1
@@ -22,6 +22,10 @@ def create_app() -> Flask:
|
||||
app.secret_key = config.flask_secret_key
|
||||
init_db()
|
||||
|
||||
@app.context_processor
|
||||
def inject_common_template_vars() -> dict[str, int]:
|
||||
return {"collect_interval_seconds": config.collect_interval_seconds}
|
||||
|
||||
@app.get("/")
|
||||
def dashboard() -> str:
|
||||
tz = ZoneInfo(config.app_timezone)
|
||||
@@ -43,10 +47,10 @@ def create_app() -> Flask:
|
||||
"dashboard.html",
|
||||
devices=devices,
|
||||
latest=latest,
|
||||
latest_relative=latest_ages_for_display(latest, tz),
|
||||
stats=stats_by_device(readings),
|
||||
chart_json=json.dumps(chart_payload(readings, tz)),
|
||||
timezone=config.app_timezone,
|
||||
collect_interval_seconds=config.collect_interval_seconds,
|
||||
)
|
||||
|
||||
@app.get("/devices/<device_id>")
|
||||
@@ -262,6 +266,34 @@ def readings_for_display(readings: list[Reading], tz: ZoneInfo) -> list[dict[str
|
||||
return rows
|
||||
|
||||
|
||||
def latest_ages_for_display(latest: dict[str, Reading], tz: ZoneInfo) -> dict[str, str]:
|
||||
now_local = datetime.now(tz)
|
||||
ages: dict[str, str] = {}
|
||||
|
||||
for device_id, reading in latest.items():
|
||||
collected_local = reading.recorded_at.replace(tzinfo=timezone.utc).astimezone(tz)
|
||||
delta_seconds = max(0, int((now_local - collected_local).total_seconds()))
|
||||
|
||||
if delta_seconds < 60:
|
||||
ages[device_id] = "just now"
|
||||
continue
|
||||
|
||||
minutes = delta_seconds // 60
|
||||
if minutes < 60:
|
||||
ages[device_id] = f"{minutes} min ago"
|
||||
continue
|
||||
|
||||
hours = minutes // 60
|
||||
if hours < 24:
|
||||
ages[device_id] = f"{hours} h ago"
|
||||
continue
|
||||
|
||||
days = hours // 24
|
||||
ages[device_id] = f"{days} d ago"
|
||||
|
||||
return ages
|
||||
|
||||
|
||||
def report_rows(
|
||||
session: Session,
|
||||
start_utc: datetime,
|
||||
|
||||
Reference in New Issue
Block a user