From 6fc8721830c5046df81af1c7a911c7855d7182ee Mon Sep 17 00:00:00 2001 From: serversdwn Date: Tue, 9 Dec 2025 02:08:00 +0000 Subject: [PATCH 1/2] settings overhaul, many QOL improvements --- backend/main.py | 2 +- backend/migrate_add_user_preferences.py | 80 ++ backend/models.py | 24 +- backend/routers/settings.py | 88 +- templates/base.html | 110 +++ templates/dashboard.html | 47 +- templates/partials/active_table.html | 8 +- templates/partials/benched_table.html | 8 +- templates/roster.html | 40 + templates/settings.html | 1072 ++++++++++++++++------- templates/unit_detail.html | 57 +- 11 files changed, 1188 insertions(+), 348 deletions(-) create mode 100644 backend/migrate_add_user_preferences.py diff --git a/backend/main.py b/backend/main.py index 4b5a155..4ca8828 100644 --- a/backend/main.py +++ b/backend/main.py @@ -18,7 +18,7 @@ Base.metadata.create_all(bind=engine) ENVIRONMENT = os.getenv("ENVIRONMENT", "production") # Initialize FastAPI app -VERSION = "0.2.2" +VERSION = "0.2.3" app = FastAPI( title="Seismo Fleet Manager", description="Backend API for managing seismograph fleet status", diff --git a/backend/migrate_add_user_preferences.py b/backend/migrate_add_user_preferences.py new file mode 100644 index 0000000..73a4e9c --- /dev/null +++ b/backend/migrate_add_user_preferences.py @@ -0,0 +1,80 @@ +""" +Migration script to add user_preferences table. + +This creates a new table for storing persistent user preferences: +- Display settings (timezone, theme, date format) +- Auto-refresh configuration +- Calibration defaults +- Status threshold customization + +Run this script once to migrate an existing database. +""" + +import sqlite3 +import os + +# Database path +DB_PATH = "./data/seismo_fleet.db" + +def migrate_database(): + """Create user_preferences table""" + + if not os.path.exists(DB_PATH): + print(f"Database not found at {DB_PATH}") + print("The database will be created automatically when you run the application.") + return + + print(f"Migrating database: {DB_PATH}") + + conn = sqlite3.connect(DB_PATH) + cursor = conn.cursor() + + # Check if user_preferences table already exists + cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='user_preferences'") + table_exists = cursor.fetchone() + + if table_exists: + print("Migration already applied - user_preferences table exists") + conn.close() + return + + print("Creating user_preferences table...") + + try: + cursor.execute(""" + CREATE TABLE user_preferences ( + id INTEGER PRIMARY KEY DEFAULT 1, + timezone TEXT DEFAULT 'America/New_York', + theme TEXT DEFAULT 'auto', + auto_refresh_interval INTEGER DEFAULT 10, + date_format TEXT DEFAULT 'MM/DD/YYYY', + table_rows_per_page INTEGER DEFAULT 25, + calibration_interval_days INTEGER DEFAULT 365, + calibration_warning_days INTEGER DEFAULT 30, + status_ok_threshold_hours INTEGER DEFAULT 12, + status_pending_threshold_hours INTEGER DEFAULT 24, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ) + """) + print(" ✓ Created user_preferences table") + + # Insert default preferences + cursor.execute(""" + INSERT INTO user_preferences (id) VALUES (1) + """) + print(" ✓ Inserted default preferences") + + conn.commit() + print("\nMigration completed successfully!") + + except sqlite3.Error as e: + print(f"\nError during migration: {e}") + conn.rollback() + raise + + finally: + conn.close() + + +if __name__ == "__main__": + migrate_database() diff --git a/backend/models.py b/backend/models.py index d941264..4b36061 100644 --- a/backend/models.py +++ b/backend/models.py @@ -1,4 +1,4 @@ -from sqlalchemy import Column, String, DateTime, Boolean, Text, Date +from sqlalchemy import Column, String, DateTime, Boolean, Text, Date, Integer from datetime import datetime from backend.database import Base @@ -56,4 +56,24 @@ class IgnoredUnit(Base): id = Column(String, primary_key=True, index=True) reason = Column(String, nullable=True) - ignored_at = Column(DateTime, default=datetime.utcnow) \ No newline at end of file + ignored_at = Column(DateTime, default=datetime.utcnow) + + +class UserPreferences(Base): + """ + User preferences: persistent storage for application settings. + Single-row table (id=1) to store global user preferences. + """ + __tablename__ = "user_preferences" + + id = Column(Integer, primary_key=True, default=1) + timezone = Column(String, default="America/New_York") + theme = Column(String, default="auto") # auto, light, dark + auto_refresh_interval = Column(Integer, default=10) # seconds + date_format = Column(String, default="MM/DD/YYYY") + table_rows_per_page = Column(Integer, default=25) + calibration_interval_days = Column(Integer, default=365) + calibration_warning_days = Column(Integer, default=30) + status_ok_threshold_hours = Column(Integer, default=12) + status_pending_threshold_hours = Column(Integer, default=24) + updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) \ No newline at end of file diff --git a/backend/routers/settings.py b/backend/routers/settings.py index 7063209..1af0547 100644 --- a/backend/routers/settings.py +++ b/backend/routers/settings.py @@ -2,11 +2,13 @@ from fastapi import APIRouter, Depends, HTTPException, UploadFile, File from fastapi.responses import StreamingResponse from sqlalchemy.orm import Session from datetime import datetime, date +from pydantic import BaseModel +from typing import Optional import csv import io from backend.database import get_db -from backend.models import RosterUnit, Emitter, IgnoredUnit +from backend.models import RosterUnit, Emitter, IgnoredUnit, UserPreferences router = APIRouter(prefix="/api/settings", tags=["settings"]) @@ -239,3 +241,87 @@ def clear_ignored(db: Session = Depends(get_db)): except Exception as e: db.rollback() raise HTTPException(status_code=500, detail=f"Clear failed: {str(e)}") + + +# User Preferences Endpoints + +class PreferencesUpdate(BaseModel): + """Schema for updating user preferences (all fields optional)""" + timezone: Optional[str] = None + theme: Optional[str] = None + auto_refresh_interval: Optional[int] = None + date_format: Optional[str] = None + table_rows_per_page: Optional[int] = None + calibration_interval_days: Optional[int] = None + calibration_warning_days: Optional[int] = None + status_ok_threshold_hours: Optional[int] = None + status_pending_threshold_hours: Optional[int] = None + + +@router.get("/preferences") +def get_preferences(db: Session = Depends(get_db)): + """ + Get user preferences. Creates default preferences if none exist. + """ + prefs = db.query(UserPreferences).filter(UserPreferences.id == 1).first() + + if not prefs: + # Create default preferences + prefs = UserPreferences(id=1) + db.add(prefs) + db.commit() + db.refresh(prefs) + + return { + "timezone": prefs.timezone, + "theme": prefs.theme, + "auto_refresh_interval": prefs.auto_refresh_interval, + "date_format": prefs.date_format, + "table_rows_per_page": prefs.table_rows_per_page, + "calibration_interval_days": prefs.calibration_interval_days, + "calibration_warning_days": prefs.calibration_warning_days, + "status_ok_threshold_hours": prefs.status_ok_threshold_hours, + "status_pending_threshold_hours": prefs.status_pending_threshold_hours, + "updated_at": prefs.updated_at.isoformat() if prefs.updated_at else None + } + + +@router.put("/preferences") +def update_preferences( + updates: PreferencesUpdate, + db: Session = Depends(get_db) +): + """ + Update user preferences. Accepts partial updates. + Creates default preferences if none exist. + """ + prefs = db.query(UserPreferences).filter(UserPreferences.id == 1).first() + + if not prefs: + # Create default preferences + prefs = UserPreferences(id=1) + db.add(prefs) + + # Update only provided fields + update_data = updates.dict(exclude_unset=True) + for field, value in update_data.items(): + setattr(prefs, field, value) + + prefs.updated_at = datetime.utcnow() + + db.commit() + db.refresh(prefs) + + return { + "message": "Preferences updated successfully", + "timezone": prefs.timezone, + "theme": prefs.theme, + "auto_refresh_interval": prefs.auto_refresh_interval, + "date_format": prefs.date_format, + "table_rows_per_page": prefs.table_rows_per_page, + "calibration_interval_days": prefs.calibration_interval_days, + "calibration_warning_days": prefs.calibration_warning_days, + "status_ok_threshold_hours": prefs.status_ok_threshold_hours, + "status_pending_threshold_hours": prefs.status_pending_threshold_hours, + "updated_at": prefs.updated_at.isoformat() if prefs.updated_at else None + } diff --git a/templates/base.html b/templates/base.html index 8814760..b16ba60 100644 --- a/templates/base.html +++ b/templates/base.html @@ -149,6 +149,116 @@ } else if (localStorage.getItem('theme') === 'dark') { document.documentElement.classList.add('dark'); } + + // Helper function: Convert timestamp to relative time + function timeAgo(dateString) { + if (!dateString) return 'Never'; + + const date = new Date(dateString); + const now = new Date(); + const seconds = Math.floor((now - date) / 1000); + + if (seconds < 60) return `${seconds}s ago`; + + const minutes = Math.floor(seconds / 60); + if (minutes < 60) return `${minutes}m ago`; + + const hours = Math.floor(minutes / 60); + if (hours < 24) { + const remainingMins = minutes % 60; + return remainingMins > 0 ? `${hours}h ${remainingMins}m ago` : `${hours}h ago`; + } + + const days = Math.floor(hours / 24); + if (days < 7) { + const remainingHours = hours % 24; + return remainingHours > 0 ? `${days}d ${remainingHours}h ago` : `${days}d ago`; + } + + const weeks = Math.floor(days / 7); + if (weeks < 4) { + const remainingDays = days % 7; + return remainingDays > 0 ? `${weeks}w ${remainingDays}d ago` : `${weeks}w ago`; + } + + const months = Math.floor(days / 30); + return `${months}mo ago`; + } + + // Helper function: Get user's selected timezone + function getTimezone() { + return localStorage.getItem('timezone') || 'America/New_York'; + } + + // Helper function: Format timestamp with tooltip (timezone-aware) + function formatTimestamp(dateString) { + if (!dateString) return 'Never'; + + const date = new Date(dateString); + const timezone = getTimezone(); + + const fullDate = date.toLocaleString('en-US', { + weekday: 'short', + year: 'numeric', + month: 'short', + day: 'numeric', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + timeZone: timezone, + timeZoneName: 'short' + }); + + return `${timeAgo(dateString)}`; + } + + // Helper function: Format timestamp as full date/time (no relative time) + // Format: "9/10/2020 8:00 AM EST" + function formatFullTimestamp(dateString) { + if (!dateString) return 'Never'; + + const date = new Date(dateString); + const timezone = getTimezone(); + + return date.toLocaleString('en-US', { + month: 'numeric', + day: 'numeric', + year: 'numeric', + hour: 'numeric', + minute: '2-digit', + timeZone: timezone, + timeZoneName: 'short' + }); + } + + // Update all timestamps on page load and periodically + function updateAllTimestamps() { + document.querySelectorAll('[data-timestamp]').forEach(el => { + const timestamp = el.getAttribute('data-timestamp'); + el.innerHTML = formatTimestamp(timestamp); + }); + } + + // Run on load and every minute + updateAllTimestamps(); + setInterval(updateAllTimestamps, 60000); + + // Copy to clipboard helper + function copyToClipboard(text, button) { + navigator.clipboard.writeText(text).then(() => { + // Visual feedback + const originalHTML = button.innerHTML; + button.innerHTML = ''; + button.classList.add('text-green-500'); + + setTimeout(() => { + button.innerHTML = originalHTML; + button.classList.remove('text-green-500'); + }, 1500); + }).catch(err => { + console.error('Failed to copy:', err); + }); + } {% block extra_scripts %}{% endblock %} diff --git a/templates/dashboard.html b/templates/dashboard.html index cdbd881..851c1ca 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -10,9 +10,15 @@ {% endif %} -
-

Dashboard

-

Fleet overview and recent activity

+
+
+

Dashboard

+

Fleet overview and recent activity

+
+
+

Last updated

+

--

+
@@ -48,23 +54,35 @@

Deployed Status:

-
+
- + + + + + OK
--
-
+
- + + + + + Pending
--
-
+
- + + + + + Missing
-- @@ -185,6 +203,17 @@ function updateDashboard(event) { try { const data = JSON.parse(event.detail.xhr.response); + // Update "Last updated" timestamp with timezone + const now = new Date(); + const timezone = localStorage.getItem('timezone') || 'America/New_York'; + document.getElementById('last-refresh').textContent = now.toLocaleTimeString('en-US', { + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + timeZone: timezone, + timeZoneName: 'short' + }); + // ===== Fleet summary numbers ===== document.getElementById('total-units').textContent = data.summary?.total ?? 0; document.getElementById('deployed-units').textContent = data.summary?.active ?? 0; diff --git a/templates/partials/active_table.html b/templates/partials/active_table.html index d9f4b72..bd62e74 100644 --- a/templates/partials/active_table.html +++ b/templates/partials/active_table.html @@ -46,5 +46,11 @@ {% endfor %}
{% else %} -

No active units

+
+ + + +

No active units

+

Get started by adding a new unit to the fleet.

+
{% endif %} diff --git a/templates/partials/benched_table.html b/templates/partials/benched_table.html index b109259..20164bf 100644 --- a/templates/partials/benched_table.html +++ b/templates/partials/benched_table.html @@ -46,5 +46,11 @@ {% endfor %}
{% else %} -

No benched units

+
+ + + +

No benched units

+

Units awaiting deployment will appear here.

+
{% endif %} diff --git a/templates/roster.html b/templates/roster.html index 527a6d8..b870668 100644 --- a/templates/roster.html +++ b/templates/roster.html @@ -34,6 +34,21 @@
+ +
+
+ + + + +
+
+
+