-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.py
More file actions
135 lines (98 loc) · 3.59 KB
/
dashboard.py
File metadata and controls
135 lines (98 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
Web Dashboard for System Monitoring
Displays real-time metrics and AI predictions in browser
"""
from flask import Flask, render_template, jsonify
import psutil
from datetime import datetime
from collections import deque
from predictor import SystemHealthPredictor
import threading
import time
app = Flask(__name__)
metrics_history = deque(maxlen=50)
predictor = SystemHealthPredictor()
current_prediction = {'status': 'Initializing...', 'at_risk': False}
predictor.load_model()
def get_current_metrics():
net_io = psutil.net_io_counters()
return {
'timestamp': datetime.now().strftime("%H:%M:%S"),
'cpu_percent': round(psutil.cpu_percent(interval=1), 1),
'memory_percent': round(psutil.virtual_memory().percent, 1),
'disk_percent': round(psutil.disk_usage('/').percent, 1),
'network_sent_mb': round(net_io.bytes_sent / (1024 * 1024), 2),
'network_recv_mb': round(net_io.bytes_recv / (1024 * 1024), 2)
}
def background_monitor():
global current_prediction
while True:
try:
metrics = get_current_metrics()
metrics_history.append(metrics)
if len(metrics_history) >= 5:
recent = list(metrics_history)[-5:]
prediction = predictor.predict(metrics, recent)
if prediction and not prediction.get('error'):
current_prediction = prediction
else:
current_prediction = {
'status': 'COLLECTING DATA',
'at_risk': False,
'risk_probability': 0,
'confidence': 0
}
else:
current_prediction = {
'status': 'COLLECTING DATA',
'at_risk': False,
'risk_probability': 0,
'confidence': 0
}
time.sleep(3)
except Exception as e:
print(f"Error in background monitor: {e}")
current_prediction = {
'status': 'ERROR',
'at_risk': False,
'risk_probability': 0,
'confidence': 0
}
time.sleep(5)
@app.route('/')
def index():
return render_template('dashboard.html')
@app.route('/api/metrics')
def api_metrics():
if len(metrics_history) == 0:
return jsonify({'error': 'No data yet'})
current = metrics_history[-1]
return jsonify({
'current': current,
'prediction': current_prediction,
'history': list(metrics_history)
})
@app.route('/api/stats')
def api_stats():
cpu_freq = psutil.cpu_freq()
mem = psutil.virtual_memory()
disk = psutil.disk_usage('/')
return jsonify({
'cpu_count': psutil.cpu_count(),
'cpu_freq': round(cpu_freq.current, 1) if cpu_freq else 'N/A',
'memory_total_gb': round(mem.total / (1024 ** 3), 1),
'disk_total_gb': round(disk.total / (1024 ** 3), 1),
'uptime_hours': round((time.time() - psutil.boot_time()) / 3600, 1)
})
if __name__ == '__main__':
monitor_thread = threading.Thread(target=background_monitor, daemon=True)
monitor_thread.start()
print("\n" + "=" * 60)
print("🚀 Starting Web Dashboard...")
print("=" * 60)
print("\n📊 Dashboard will be available at:")
print(" http://localhost:5000")
print("\n⚠️ Make sure you've trained the model first:")
print(" python predictor.py")
print("\n Press Ctrl+C to stop\n")
app.run(debug=True, host='0.0.0.0', port=5000, use_reloader=False)