Spaces:
Sleeping
Sleeping
Faham commited on
Commit ·
031a6a1
1
Parent(s): 79476b6
UPDATE: resource monitor clean temp files
Browse files- pages/System_Monitor.py +0 -5
- resource_monitor.py +52 -0
pages/System_Monitor.py
CHANGED
|
@@ -150,11 +150,6 @@ def main():
|
|
| 150 |
st.write("• Volatility Indicators (Bollinger Bands)")
|
| 151 |
st.write("• Volume Analysis & Market Sentiment")
|
| 152 |
|
| 153 |
-
# Export data button
|
| 154 |
-
if st.button("📥 Export Resource Data"):
|
| 155 |
-
filename = export_resource_data()
|
| 156 |
-
st.success(f"Resource data exported to: {filename}")
|
| 157 |
-
|
| 158 |
else:
|
| 159 |
st.error(f"Error getting resource stats: {current_stats['error']}")
|
| 160 |
else:
|
|
|
|
| 150 |
st.write("• Volatility Indicators (Bollinger Bands)")
|
| 151 |
st.write("• Volume Analysis & Market Sentiment")
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
else:
|
| 154 |
st.error(f"Error getting resource stats: {current_stats['error']}")
|
| 155 |
else:
|
resource_monitor.py
CHANGED
|
@@ -4,6 +4,8 @@ import threading
|
|
| 4 |
import plotly.graph_objects as go
|
| 5 |
from datetime import datetime
|
| 6 |
import json
|
|
|
|
|
|
|
| 7 |
from typing import Dict
|
| 8 |
|
| 9 |
|
|
@@ -36,6 +38,10 @@ class ResourceMonitor:
|
|
| 36 |
if not self.monitoring:
|
| 37 |
self.monitoring = True
|
| 38 |
self.start_time = datetime.now()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
self.monitor_thread = threading.Thread(
|
| 40 |
target=self._monitor_loop, daemon=True
|
| 41 |
)
|
|
@@ -297,8 +303,54 @@ class ResourceMonitor:
|
|
| 297 |
with open(filename, "w") as f:
|
| 298 |
json.dump(export_data, f, indent=2)
|
| 299 |
|
|
|
|
|
|
|
|
|
|
| 300 |
return filename
|
| 301 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
# Global monitor instance
|
| 304 |
resource_monitor = ResourceMonitor()
|
|
|
|
| 4 |
import plotly.graph_objects as go
|
| 5 |
from datetime import datetime
|
| 6 |
import json
|
| 7 |
+
import os
|
| 8 |
+
import glob
|
| 9 |
from typing import Dict
|
| 10 |
|
| 11 |
|
|
|
|
| 38 |
if not self.monitoring:
|
| 39 |
self.monitoring = True
|
| 40 |
self.start_time = datetime.now()
|
| 41 |
+
|
| 42 |
+
# Clean up old files when starting monitoring
|
| 43 |
+
self.cleanup_old_files()
|
| 44 |
+
|
| 45 |
self.monitor_thread = threading.Thread(
|
| 46 |
target=self._monitor_loop, daemon=True
|
| 47 |
)
|
|
|
|
| 303 |
with open(filename, "w") as f:
|
| 304 |
json.dump(export_data, f, indent=2)
|
| 305 |
|
| 306 |
+
# Clean up old files after creating a new one
|
| 307 |
+
self.cleanup_old_files()
|
| 308 |
+
|
| 309 |
return filename
|
| 310 |
|
| 311 |
+
def cleanup_old_files(self, max_files: int = 10, max_age_days: int = 7):
|
| 312 |
+
"""Clean up old JSON files to prevent disk space issues."""
|
| 313 |
+
try:
|
| 314 |
+
# Get all resource monitor JSON files
|
| 315 |
+
pattern = "resource_monitor_*.json"
|
| 316 |
+
files = glob.glob(pattern)
|
| 317 |
+
|
| 318 |
+
if len(files) <= max_files:
|
| 319 |
+
return # No cleanup needed
|
| 320 |
+
|
| 321 |
+
# Sort files by modification time (oldest first)
|
| 322 |
+
files.sort(key=lambda x: os.path.getmtime(x))
|
| 323 |
+
|
| 324 |
+
# Calculate cutoff time for age-based cleanup
|
| 325 |
+
cutoff_time = time.time() - (max_age_days * 24 * 60 * 60)
|
| 326 |
+
|
| 327 |
+
files_to_delete = []
|
| 328 |
+
|
| 329 |
+
# Add files that are too old
|
| 330 |
+
for file in files:
|
| 331 |
+
if os.path.getmtime(file) < cutoff_time:
|
| 332 |
+
files_to_delete.append(file)
|
| 333 |
+
|
| 334 |
+
# Add oldest files if we still have too many
|
| 335 |
+
remaining_files = [f for f in files if f not in files_to_delete]
|
| 336 |
+
if len(remaining_files) > max_files:
|
| 337 |
+
files_to_delete.extend(remaining_files[:-max_files])
|
| 338 |
+
|
| 339 |
+
# Delete the files
|
| 340 |
+
deleted_count = 0
|
| 341 |
+
for file in files_to_delete:
|
| 342 |
+
try:
|
| 343 |
+
os.remove(file)
|
| 344 |
+
deleted_count += 1
|
| 345 |
+
except Exception as e:
|
| 346 |
+
print(f"Error deleting file {file}: {e}")
|
| 347 |
+
|
| 348 |
+
if deleted_count > 0:
|
| 349 |
+
print(f"Cleaned up {deleted_count} old resource monitor files")
|
| 350 |
+
|
| 351 |
+
except Exception as e:
|
| 352 |
+
print(f"Error during file cleanup: {e}")
|
| 353 |
+
|
| 354 |
|
| 355 |
# Global monitor instance
|
| 356 |
resource_monitor = ResourceMonitor()
|