|
1 | | -from flask import render_template, request, flash |
2 | | -import os |
3 | | -import psutil |
4 | | -import datetime |
5 | | -import subprocess |
6 | 1 | from src.config import app, db |
7 | | -from src.models import SpeedTestResult, DashoardSettings, SystemInfo |
8 | | -from src.utils import ( |
9 | | - datetimeformat, |
10 | | - run_speedtest, |
11 | | - get_system_info |
12 | | -) |
| 2 | +from src import routes |
13 | 3 |
|
14 | | -# initialize the database |
15 | | -with app.app_context(): |
16 | | - db.create_all() |
17 | | - settings = DashoardSettings.query.first() |
18 | | - if not settings: |
19 | | - db.session.add(DashoardSettings()) |
20 | | - db.session.commit() |
| 4 | +def register_routes(): |
| 5 | + app.register_blueprint(routes.homepage_bp) |
| 6 | + app.register_blueprint(routes.settings_bp) |
| 7 | + app.register_blueprint(routes.system_health_bp) |
| 8 | + app.register_blueprint(routes.cpu_usage_bp) |
| 9 | + app.register_blueprint(routes.disk_usage_bp) |
| 10 | + app.register_blueprint(routes.memory_usage_bp) |
| 11 | + app.register_blueprint(routes.network_stats_bp) |
| 12 | + app.register_blueprint(routes.speedtest_bp) |
21 | 13 |
|
22 | | -@app.route('/') |
23 | | -def dashboard(): |
24 | | - settings = DashoardSettings.query.first() |
25 | | - SPEEDTEST_COOLDOWN_IN_HOURS = settings.speedtest_cooldown |
26 | | - system_info = get_system_info(SystemInfo=SystemInfo) |
27 | | - |
28 | | - # Fetch the last speedtest result |
29 | | - n_hour_ago = datetime.datetime.now() - datetime.timedelta(hours=SPEEDTEST_COOLDOWN_IN_HOURS) |
30 | | - recent_results = SpeedTestResult.query.filter(SpeedTestResult.timestamp > n_hour_ago).all() |
31 | | - last_timestamp = datetimeformat(recent_results[-1].timestamp) if recent_results else None |
32 | 14 |
|
33 | | - if recent_results: |
34 | | - # Display the most recent result from the database |
35 | | - latest_result = recent_results[-1] |
36 | | - speedtest_result = { |
37 | | - 'download_speed': latest_result.download_speed, |
38 | | - 'upload_speed': latest_result.upload_speed, |
39 | | - 'ping': latest_result.ping |
40 | | - } |
41 | | - source = "Database" |
42 | | - next_test_time = latest_result.timestamp + datetime.timedelta(hours=SPEEDTEST_COOLDOWN_IN_HOURS) |
43 | | - show_prompt = False |
44 | | - remaining_time_for_next_test = round((next_test_time - datetime.datetime.now()).total_seconds() / 60) |
45 | | - else: |
46 | | - # No recent results, prompt to perform a test |
47 | | - speedtest_result = None |
48 | | - source = None |
49 | | - show_prompt = True |
50 | | - remaining_time_for_next_test = None |
51 | | - |
52 | | - return render_template('dashboard.html', system_info=system_info, |
53 | | - speedtest_result=speedtest_result, |
54 | | - source=source, |
55 | | - last_timestamp=last_timestamp, |
56 | | - next_test_time=remaining_time_for_next_test, |
57 | | - show_prompt=show_prompt) |
58 | 15 |
|
59 | | -@app.route('/settings', methods=['GET', 'POST']) |
60 | | -def settings(): |
61 | | - # Fetch the settings from the database and update them |
62 | | - settings = DashoardSettings.query.first() |
63 | | - if settings: |
64 | | - if request.method == 'POST': |
65 | | - settings.speedtest_cooldown = int(request.form['speedtest_cooldown']) |
66 | | - settings.number_of_speedtests = int(request.form['number_of_speedtests']) |
67 | | - settings.timezone = request.form['timezone'] |
68 | | - db.session.commit() |
69 | | - flash('Settings updated successfully!', 'success') |
70 | | - return render_template('settings.html', settings=settings) |
71 | 16 |
|
72 | | -@app.route('/speedtest') |
73 | | -def speedtest(): |
74 | | - settings = DashoardSettings.query.first() |
75 | | - SPEEDTEST_COOLDOWN_IN_HOURS = settings.speedtest_cooldown |
76 | | - NUMBER_OF_SPEEDTESTS = settings.number_of_speedtests |
77 | | - n_hour_ago = datetime.datetime.now() - datetime.timedelta(hours=SPEEDTEST_COOLDOWN_IN_HOURS) |
78 | | - recent_results = SpeedTestResult.query.filter(SpeedTestResult.timestamp > n_hour_ago).all() |
79 | | - |
80 | | - if len(recent_results) < NUMBER_OF_SPEEDTESTS: |
81 | | - speedtest_result = run_speedtest() |
82 | | - if speedtest_result['status'] == "Error": |
83 | | - return render_template('error/speedtest_error.html', error=speedtest_result['message']) |
84 | | - |
85 | | - if speedtest_result: |
86 | | - new_result = SpeedTestResult( |
87 | | - download_speed=speedtest_result['download_speed'], |
88 | | - upload_speed=speedtest_result['upload_speed'], |
89 | | - ping=speedtest_result['ping'] |
90 | | - ) |
91 | | - db.session.add(new_result) |
92 | | - db.session.commit() |
93 | | - return render_template('speedtest_result.html', speedtest_result=speedtest_result, source="Actual Test") |
94 | | - else: |
95 | | - latest_result = recent_results[-1] |
96 | | - next_test_time = latest_result.timestamp + datetime.timedelta(hours=SPEEDTEST_COOLDOWN_IN_HOURS) |
97 | | - remaining_time_for_next_test = round((next_test_time - datetime.datetime.now()).total_seconds() / 60) |
98 | | - return render_template('speedtest_result.html', |
99 | | - speedtest_result=latest_result, |
100 | | - source="Database", |
101 | | - next_test_time=next_test_time, |
102 | | - remaining_time_for_next_test=remaining_time_for_next_test) |
103 | | - |
104 | | -@app.route('/cpu_usage') |
105 | | -def cpu_usage(): |
106 | | - cpu_usage = psutil.cpu_percent(interval=1, percpu=True) |
107 | | - return render_template('cpu_usage.html', cpu_usage=cpu_usage) |
108 | | - |
109 | | -@app.route('/memory_usage') |
110 | | -def memory_usage(): |
111 | | - memory_info = { |
112 | | - 'memory_percent': psutil.virtual_memory().percent, |
113 | | - 'memory_available': round(psutil.virtual_memory().available / (1024 ** 3), 2), # In GB |
114 | | - 'memory_used': round(psutil.virtual_memory().used / (1024 ** 3), 2) # In GB |
115 | | - } |
116 | | - return render_template('memory_usage.html', memory_info=memory_info) |
117 | | - |
118 | | -@app.route('/disk_usage') |
119 | | -def disk_usage(): |
120 | | - disk_info = { |
121 | | - 'disk_usage': psutil.disk_usage('/').percent, |
122 | | - 'disk_total': round(psutil.disk_usage('/').total / (1024 ** 3), 2), # In GB |
123 | | - 'disk_used': round(psutil.disk_usage('/').used / (1024 ** 3), 2), # In GB |
124 | | - 'disk_free': round(psutil.disk_usage('/').free / (1024 ** 3), 2) # In GB |
125 | | - } |
126 | | - return render_template('disk_usage.html', disk_info=disk_info) |
127 | | - |
128 | | -@app.route('/network_stats') |
129 | | -def network_stats(): |
130 | | - net_io = psutil.net_io_counters() |
131 | | - network_info = { |
132 | | - 'network_sent': round(net_io.bytes_sent / (1024 ** 2), 2), # In MB |
133 | | - 'network_received': round(net_io.bytes_recv / (1024 ** 2), 2) # In MB |
134 | | - } |
135 | | - return render_template('network_stats.html', network_info=network_info) |
136 | | - |
137 | | -@app.route('/system_health') |
138 | | -def system_health(): |
139 | | - system_info = get_system_info(SystemInfo=SystemInfo) |
140 | | - return render_template('system_health.html', system_info=system_info) |
141 | | - |
142 | | -if __name__ == '__main__': |
143 | | - with app.app_context(): |
144 | | - db.create_all() |
145 | | - app.run(host='0.0.0.0', port=5000, debug=True) |
| 17 | +if __name__ == "__main__": |
| 18 | + app.run(host="0.0.0.0", port=5001, debug=True) |
0 commit comments