Add API for resetting chart data and update frontend

Implemented a new API endpoint `/api/reset-chart-data` in `App.py` to clear chart data history and save state to Redis. Updated the `resetDashboardChart` function in `main.js` to make an AJAX call to this endpoint, providing immediate user feedback. Removed previous logic for handling latest metrics to streamline the reset process.
This commit is contained in:
DJObleezy 2025-04-18 12:27:20 -07:00
parent f166126525
commit 6d07060b7e
2 changed files with 43 additions and 28 deletions

24
App.py
View File

@ -821,6 +821,30 @@ class RobustMiddleware:
start_response("500 Internal Server Error", [("Content-Type", "text/html")])
return [b"<h1>Internal Server Error</h1>"]
@app.route("/api/reset-chart-data", methods=["POST"])
def reset_chart_data():
"""API endpoint to reset chart data history."""
try:
global arrow_history, state_manager
# Clear hashrate data from in-memory dictionary
hashrate_keys = ["hashrate_60sec", "hashrate_3hr", "hashrate_10min", "hashrate_24hr"]
for key in hashrate_keys:
if key in arrow_history:
arrow_history[key] = []
# Force an immediate save to Redis if available
if state_manager and hasattr(state_manager, 'redis_client') and state_manager.redis_client:
# Force save by overriding the time check
state_manager.last_save_time = 0
state_manager.save_graph_state()
logging.info("Chart data reset saved to Redis immediately")
return jsonify({"status": "success", "message": "Chart data reset successfully"})
except Exception as e:
logging.error(f"Error resetting chart data: {e}")
return jsonify({"status": "error", "message": str(e)}), 500
# Add the middleware
app.wsgi_app = RobustMiddleware(app.wsgi_app)

View File

@ -1412,43 +1412,34 @@ function initNotificationBadge() {
setInterval(updateNotificationBadge, 60000);
}
// Function to reset dashboard chart data
// Modify the resetDashboardChart function
function resetDashboardChart() {
console.log("Resetting dashboard chart data");
if (trendChart) {
// Reset chart data arrays
// Reset chart data arrays first (always succeeds)
trendChart.data.labels = [];
trendChart.data.datasets[0].data = [];
// Update the chart with empty data
trendChart.update('none');
// Show feedback to the user
showConnectionIssue("Chart data reset");
setTimeout(hideConnectionIssue, 2000);
// Show immediate feedback
showConnectionIssue("Resetting chart data...");
// If we have latest metrics, add the current point back
if (latestMetrics && latestMetrics.hashrate_60sec) {
// Get current time
const now = new Date();
let currentTime = now.toLocaleTimeString('en-US', {
timeZone: dashboardTimezone,
hour: '2-digit',
minute: '2-digit',
hour12: true
}).replace(/\s[AP]M$/i, '');
// Get current hashrate
const currentUnit = latestMetrics.hashrate_60sec_unit ?
latestMetrics.hashrate_60sec_unit.toLowerCase() : 'th/s';
const normalizedValue = normalizeHashrate(parseFloat(latestMetrics.hashrate_60sec) || 0, currentUnit);
// Add single point
trendChart.data.labels = [currentTime];
trendChart.data.datasets[0].data = [normalizedValue];
trendChart.update('none');
}
// Then call the API to clear underlying data
$.ajax({
url: '/api/reset-chart-data',
method: 'POST',
success: function (response) {
console.log("Server data reset:", response);
showConnectionIssue("Chart data reset successfully");
setTimeout(hideConnectionIssue, 3000);
},
error: function (xhr, status, error) {
console.error("Error resetting chart data:", error);
showConnectionIssue("Chart display reset (backend error: " + error + ")");
setTimeout(hideConnectionIssue, 5000);
}
});
}
}