diff --git a/backend/src/service/app.py b/backend/src/service/app.py index 0d9a310..a265e3f 100755 --- a/backend/src/service/app.py +++ b/backend/src/service/app.py @@ -37,6 +37,7 @@ # In-memory state temperature_history = {} peak_temperatures = {} +logging_enabled = True # New state variable def get_nvidia_info() -> NvidiaInfo: try: @@ -123,12 +124,15 @@ def get_gpu_metrics() -> GpuMetricsRecord: # Check for alerts alert_system.check_metrics(metrics) - # Store in database - try: - db.insert_gpu_metrics(metrics) - logger.info("Metrics stored in database") - except Exception as e: - logger.error(f"Failed to store metrics: {e}") + # Store in database only if logging is enabled + if logging_enabled: + try: + db.insert_gpu_metrics(metrics) + logger.info("Metrics stored in database") + except Exception as e: + logger.error(f"Failed to store metrics: {e}") + else: + logger.debug("Metrics logging is disabled, skipping database insert") return metrics except Exception as e: @@ -201,6 +205,19 @@ async def get_alerts(hours: int = 24): """Get recent alerts""" return alert_system.get_recent_alerts(hours) +@app.post("/api/logging/toggle") +async def toggle_logging(): + """Toggle metrics logging to database""" + global logging_enabled + logging_enabled = not logging_enabled + logger.info(f"Metrics logging {'enabled' if logging_enabled else 'disabled'}") + return {"logging_enabled": logging_enabled} + +@app.get("/api/logging/status") +async def get_logging_status(): + """Get current logging status""" + return {"logging_enabled": logging_enabled} + if __name__ == "__main__": import uvicorn logger.info("Starting GPU Metrics Service") diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index fc8d80f..65326c2 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -107,6 +107,34 @@ function App() { const saved = localStorage.getItem('pollingInterval') return saved ? parseInt(saved) : 1000 }) + const [loggingEnabled, setLoggingEnabled] = useState(true) + + useEffect(() => { + const fetchLoggingStatus = async () => { + try { + const response = await fetch(`${API_URL}/api/logging/status`) + if (!response.ok) throw new Error('Failed to fetch logging status') + const data = await response.json() + setLoggingEnabled(data.logging_enabled) + } catch (error) { + console.error('Error fetching logging status:', error) + } + } + fetchLoggingStatus() + }, []) + + const toggleLogging = async () => { + try { + const response = await fetch(`${API_URL}/api/logging/toggle`, { + method: 'POST' + }) + if (!response.ok) throw new Error('Failed to toggle logging') + const data = await response.json() + setLoggingEnabled(data.logging_enabled) + } catch (error) { + console.error('Error toggling logging:', error) + } + } const theme: ThemeColors = darkMode ? { background: '#1a1a1a', @@ -343,6 +371,25 @@ function App() { )} {darkMode ? 'Light Mode' : 'Dark Mode'} +