mirror of
https://github.com/Retropex/custom-ocean.xyz-dashboard.git
synced 2025-05-12 19:20:45 +02:00
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:
parent
f166126525
commit
6d07060b7e
24
App.py
24
App.py
@ -821,6 +821,30 @@ class RobustMiddleware:
|
|||||||
start_response("500 Internal Server Error", [("Content-Type", "text/html")])
|
start_response("500 Internal Server Error", [("Content-Type", "text/html")])
|
||||||
return [b"<h1>Internal Server Error</h1>"]
|
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
|
# Add the middleware
|
||||||
app.wsgi_app = RobustMiddleware(app.wsgi_app)
|
app.wsgi_app = RobustMiddleware(app.wsgi_app)
|
||||||
|
|
||||||
|
@ -1412,43 +1412,34 @@ function initNotificationBadge() {
|
|||||||
setInterval(updateNotificationBadge, 60000);
|
setInterval(updateNotificationBadge, 60000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to reset dashboard chart data
|
// Modify the resetDashboardChart function
|
||||||
function resetDashboardChart() {
|
function resetDashboardChart() {
|
||||||
console.log("Resetting dashboard chart data");
|
console.log("Resetting dashboard chart data");
|
||||||
|
|
||||||
if (trendChart) {
|
if (trendChart) {
|
||||||
// Reset chart data arrays
|
// Reset chart data arrays first (always succeeds)
|
||||||
trendChart.data.labels = [];
|
trendChart.data.labels = [];
|
||||||
trendChart.data.datasets[0].data = [];
|
trendChart.data.datasets[0].data = [];
|
||||||
|
|
||||||
// Update the chart with empty data
|
|
||||||
trendChart.update('none');
|
trendChart.update('none');
|
||||||
|
|
||||||
// Show feedback to the user
|
// Show immediate feedback
|
||||||
showConnectionIssue("Chart data reset");
|
showConnectionIssue("Resetting chart data...");
|
||||||
setTimeout(hideConnectionIssue, 2000);
|
|
||||||
|
|
||||||
// If we have latest metrics, add the current point back
|
// Then call the API to clear underlying data
|
||||||
if (latestMetrics && latestMetrics.hashrate_60sec) {
|
$.ajax({
|
||||||
// Get current time
|
url: '/api/reset-chart-data',
|
||||||
const now = new Date();
|
method: 'POST',
|
||||||
let currentTime = now.toLocaleTimeString('en-US', {
|
success: function (response) {
|
||||||
timeZone: dashboardTimezone,
|
console.log("Server data reset:", response);
|
||||||
hour: '2-digit',
|
showConnectionIssue("Chart data reset successfully");
|
||||||
minute: '2-digit',
|
setTimeout(hideConnectionIssue, 3000);
|
||||||
hour12: true
|
},
|
||||||
}).replace(/\s[AP]M$/i, '');
|
error: function (xhr, status, error) {
|
||||||
|
console.error("Error resetting chart data:", error);
|
||||||
// Get current hashrate
|
showConnectionIssue("Chart display reset (backend error: " + error + ")");
|
||||||
const currentUnit = latestMetrics.hashrate_60sec_unit ?
|
setTimeout(hideConnectionIssue, 5000);
|
||||||
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');
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user