Skip to content

Optimize Live dashboard#401

Open
ezio-melotti wants to merge 3 commits into
masterfrom
optimize-live-dashboard
Open

Optimize Live dashboard#401
ezio-melotti wants to merge 3 commits into
masterfrom
optimize-live-dashboard

Conversation

@ezio-melotti

Copy link
Copy Markdown
Collaborator

This PR optimizes the Live dashboard by introducing two major changes:

  • The list of sensors is fetched once at the beginning, and then once every 5 minutes (instead of being fetched every 5s together with the latest reading like before);
  • The SQL query used to retrieve the latest timestamp now assume that the timestamps are already sorted and retrieve the last value directly, instead of scanning the whole content of the tables finding the most recent timestamp.

@ezio-melotti
ezio-melotti requested a review from Copilot July 25, 2026 12:40
@ezio-melotti ezio-melotti self-assigned this Jul 25, 2026
@ezio-melotti ezio-melotti added the frontend Issues related to the frontend label Jul 25, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR targets Live dashboard performance by reducing how often sensor metadata is fetched and by changing the “latest reading” SQL lookups to avoid scanning sensor tables.

Changes:

  • Frontend: cache /api/sensors metadata and background-refresh it every 5 minutes instead of refetching on every live poll.
  • Backend: change “latest timestamp / latest reading” queries to fetch the last row via descending id order.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/simoc_sam/api.py Updates “latest” SQL queries to use ORDER BY id DESC LIMIT 1.
frontend/app.js Adds sensor-metadata TTL caching and background refresh behavior for the Live dashboard.
Comments suppressed due to low confidence (1)

src/simoc_sam/api.py:174

  • /api/latest orders by id instead of timestamp, which changes semantics from “most recent reading” to “most recently inserted row”. If any readings are inserted out of chronological order, the API will return a stale reading even though newer timestamps exist in the table.
            try:
                row = conn.execute(
                    f'SELECT sensor_id, timestamp, {", ".join(metrics)} '
                    f'FROM {sensor} ORDER BY id DESC LIMIT 1'
                ).fetchone()

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/simoc_sam/api.py
Comment on lines 80 to +85
for sensor in SENSOR_DATA:
try:
row = conn.execute(
f'SELECT MAX(timestamp) FROM {sensor}'
f'SELECT timestamp FROM {sensor} ORDER BY id DESC LIMIT 1'
).fetchone()
latest[sensor] = row[0]
latest[sensor] = row[0] if row else None
Comment thread frontend/app.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

frontend Issues related to the frontend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants