Skip to content

Commit

Permalink
feat: add logging pause/resume functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jackccrawford committed Nov 21, 2024
1 parent c2a01d7 commit 91bda1c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
29 changes: 23 additions & 6 deletions backend/src/service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
# In-memory state
temperature_history = {}
peak_temperatures = {}
logging_enabled = True # New state variable

def get_nvidia_info() -> NvidiaInfo:
try:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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")
Expand Down
47 changes: 47 additions & 0 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -343,6 +371,25 @@ function App() {
)}
{darkMode ? 'Light Mode' : 'Dark Mode'}
</button>
<button
onClick={toggleLogging}
style={{
padding: '8px 12px',
borderRadius: '8px',
border: `1px solid ${theme.border}`,
backgroundColor: theme.cardBackground,
color: loggingEnabled ? getMetricColor(90, theme) : theme.subtext,
cursor: 'pointer',
display: 'flex',
alignItems: 'center',
gap: '5px'
}}
>
<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
<path d="M14 12c0-1.1-.9-2-2-2s-2 .9-2 2 .9 2 2 2 2-.9 2-2zm-2-9c-4.97 0-9 4.03-9 9H0l4 4 4-4H5c0-3.87 3.13-7 7-7s7 3.13 7 7-3.13 7-7 7c-1.51 0-2.91-.49-4.06-1.3l-1.42 1.44C8.04 20.3 9.94 21 12 21c4.97 0 9-4.03 9-9s-4.03-9-9-9z"/>
</svg>
{loggingEnabled ? 'Pause Logging' : 'Resume Logging'}
</button>
</div>
</div>

Expand Down

0 comments on commit 91bda1c

Please sign in to comment.