Add SQL insight dashboard widgets

This commit is contained in:
Peter Stockings
2026-09-06 13:37:46 +10:00
parent ff6a921550
commit d1100ece80
11 changed files with 273 additions and 28 deletions
+51 -4
View File
@@ -1,9 +1,56 @@
from utils import calculate_estimated_1rm, get_exercise_graph_model
from utils import calculate_estimated_1rm, get_exercise_graph_model, prepare_svg_plot_data
class Dashboard:
def __init__(self, db_connection_method):
self.execute = db_connection_method
class Dashboard:
def __init__(self, db_connection_method):
self.execute = db_connection_method
def get_pinned_widgets(self, person_id):
"""Execute and prepare the current user's pinned SQL dashboard widgets."""
widgets = self.execute("""
SELECT
w.widget_id,
w.saved_query_id AS query_id,
w.visualization_type,
q.title,
q.query
FROM dashboard_widget w
INNER JOIN saved_query q ON q.id = w.saved_query_id
WHERE w.person_id = %s AND w.is_visible = TRUE
ORDER BY w.position, w.widget_id
""", [person_id]) or []
prepared_widgets = []
for widget in widgets:
prepared = dict(widget)
prepared['results'] = []
prepared['columns'] = []
prepared['row_count'] = 0
prepared['plot_data'] = None
prepared['error'] = None
try:
results = self.execute(widget['query']) or []
columns = list(results[0].keys()) if results else []
prepared['row_count'] = len(results)
prepared['results'] = results[:100]
prepared['columns'] = columns
if widget['visualization_type'] == 'graph' and results:
prepared['plot_data'] = prepare_svg_plot_data(
results[:100], columns, widget['title']
)
except Exception as error:
# A broken saved query should not take down the Home page.
try:
self.execute('ROLLBACK')
except Exception:
pass
prepared['error'] = str(error)
prepared_widgets.append(prepared)
return prepared_widgets
def get_people_ids(self):
query = """