This commit is contained in:
Admin9705
2025-05-03 19:38:25 -04:00
parent f99dd5c845
commit ccf9ae2a9a
5 changed files with 132 additions and 15 deletions

View File

@@ -131,13 +131,21 @@ def save_stats(stats: Dict[str, Dict[str, int]]) -> bool:
try:
logger.debug(f"Saving stats to: {STATS_FILE}")
with open(STATS_FILE, 'w') as f:
# First write to a temp file, then move it to avoid partial writes
temp_file = f"{STATS_FILE}.tmp"
with open(temp_file, 'w') as f:
json.dump(stats, f, indent=2)
f.flush()
os.fsync(f.fileno())
# Move the temp file to the actual file
os.replace(temp_file, STATS_FILE)
logger.info(f"===> Successfully wrote stats to file: {STATS_FILE}")
logger.debug(f"Stats saved successfully: {stats}")
return True
except Exception as e:
logger.error(f"Error saving stats to {STATS_FILE}: {e}")
logger.error(f"Error saving stats to {STATS_FILE}: {e}", exc_info=True)
return False
def increment_stat(app_type: str, stat_type: str, count: int = 1) -> bool:

View File

@@ -671,22 +671,56 @@ def api_reset_stats():
# Get logger for logging the reset action
web_logger = get_logger("web_server")
# Import the reset_stats function
from src.primary.stats_manager import reset_stats
if app_type:
web_logger.info(f"Resetting statistics for app: {app_type}")
# In a real implementation, you would reset stats just for this app
reset_success = reset_stats(app_type)
else:
web_logger.info("Resetting all media statistics")
# In a real implementation, you would reset all app stats
reset_success = reset_stats(None)
# Return immediate success since this is a visual-only feature for now
# In a production environment, this would actually reset stored statistics
return jsonify({"success": True, "message": "Statistics reset successfully"})
if reset_success:
return jsonify({"success": True, "message": "Statistics reset successfully"})
else:
return jsonify({"success": False, "error": "Failed to reset statistics"}), 500
except Exception as e:
web_logger = get_logger("web_server")
web_logger.error(f"Error resetting statistics: {str(e)}")
return jsonify({"success": False, "error": str(e)}), 500
@app.route('/api/stats/reset_public', methods=['POST'])
def api_reset_stats_public():
"""Reset the media statistics for all apps or a specific app - public endpoint without auth"""
try:
data = request.json or {}
app_type = data.get('app_type')
# Get logger for logging the reset action
web_logger = get_logger("web_server")
# Import the reset_stats function
from src.primary.stats_manager import reset_stats
if app_type:
web_logger.info(f"Resetting statistics for app (public): {app_type}")
reset_success = reset_stats(app_type)
else:
web_logger.info("Resetting all media statistics (public)")
reset_success = reset_stats(None)
if reset_success:
return jsonify({"success": True, "message": "Statistics reset successfully"}), 200
else:
return jsonify({"success": False, "error": "Failed to reset statistics"}), 500
except Exception as e:
web_logger = get_logger("web_server")
web_logger.error(f"Error resetting statistics (public): {str(e)}")
return jsonify({"success": False, "error": str(e)}), 500
@app.route('/version.txt')
def version_txt():
"""Serve version.txt file directly"""