Skip to content

Commit 4c1a8b3

Browse files
feat: button added for refresh dashbaord page
1 parent bafd882 commit 4c1a8b3

10 files changed

Lines changed: 134 additions & 32 deletions

File tree

app.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import datetime
23
from src.config import app
34
from src import routes
@@ -18,31 +19,9 @@ def register_routes():
1819

1920
app.config['is_server_up_email_sent'] = False
2021

21-
def server_up_email():
22-
with app.app_context():
23-
admin_emails = [user.email for user in UserProfile.query.filter_by(user_level="admin", receive_email_alerts=True).all()]
24-
if admin_emails:
25-
subject = "SystemGuard Server Started"
26-
context = {
27-
"current_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
28-
"cpu_usage": cpu_usage_percent(),
29-
"memory_usage": get_memory_percent(),
30-
"app_memory_usage": get_flask_memory_usage(),
31-
32-
}
33-
server_up_template = os.path.join(ROOT_DIR, "src/templates/email_templates/server_up.html")
34-
html_body = render_template_from_file(server_up_template, **context)
35-
# send_smpt_email(admin_emails, subject, html_body, is_html=True)
36-
print("Server up email sent to", admin_emails)
37-
3822

3923
if __name__ == "__main__":
4024
register_routes()
41-
# TODO: fix this email alert sent twice
42-
# if not app.config['is_server_up_email_sent']:
43-
# app.config['is_server_up_email_sent'] = True
44-
# server_up_email()
45-
4625
# # Start the memory-consuming program in a separate thread
4726
# memory_thread = threading.Thread(target=memory_consuming_program, daemon=True)
4827
# memory_thread.start()

src/models/application_general_settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ class ApplicationGeneralSettings(db.Model):
77
enable_alerts = db.Column(db.Boolean, default=True)
88
timezone = db.Column(db.String(50), default='UTC')
99
enable_cache = db.Column(db.Boolean, default=True)
10-
10+

src/models/feature_toggle_settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
from src.config import db
23

34
class FeatureToggleSettings(db.Model):
@@ -12,3 +13,5 @@ class FeatureToggleSettings(db.Model):
1213
is_disk_info_enabled = db.Column(db.Boolean, default=True)
1314
is_network_info_enabled = db.Column(db.Boolean, default=True)
1415
is_process_info_enabled = db.Column(db.Boolean, default=True)
16+
refresh_interval = db.Column(db.Integer, default=10)
17+

src/routes/other.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import subprocess
22
from flask import render_template, request, jsonify, flash, blueprints
33
from flask_login import login_required, current_user
4-
5-
from src.config import app
4+
from src.models import FeatureToggleSettings
5+
from src.config import app, db
66

77
other_bp = blueprints.Blueprint('other', __name__)
88

@@ -24,3 +24,31 @@ def terminal():
2424
return jsonify(output=output)
2525
return render_template('terminal.html')
2626

27+
28+
@app.route('/update-refresh-interval', methods=['POST'])
29+
def update_refresh_interval():
30+
# Retrieve user ID from session or other authentication methods
31+
user_id = current_user.id
32+
33+
# Get the new refresh interval from the request
34+
new_interval = request.json.get('refresh_interval')
35+
36+
# Validate the new interval (must be a positive integer)
37+
if not isinstance(new_interval, int) or new_interval <= 0:
38+
return jsonify({'error': 'Invalid refresh interval value'}), 400
39+
40+
# Query the settings for the current user
41+
settings = FeatureToggleSettings.query.filter_by(user_id=user_id).first()
42+
43+
# If settings do not exist for the user, create them
44+
if not settings:
45+
settings = FeatureToggleSettings(user_id=user_id, refresh_interval=new_interval)
46+
db.session.add(settings)
47+
else:
48+
# Update the refresh interval
49+
settings.refresh_interval = new_interval
50+
51+
# Commit changes to the database
52+
db.session.commit()
53+
54+
return jsonify({'success': 'Refresh interval updated successfully', 'refresh_interval': new_interval})

src/routes/settings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def general_settings():
3030
general_settings.timezone = request.form.get('timezone')
3131
general_settings.enable_cache = 'enable_cache' in request.form
3232
general_settings.enable_alerts = 'enable_alerts' in request.form
33-
admin_emails = [user.email for user in User.query.filter_by(user_level="admin", receive_email_alerts=True).all()]
33+
admin_emails = [user.email for user in UserProfile.query.filter_by(user_level="admin", receive_email_alerts=True).all()]
3434
if admin_emails:
3535
subject = "SystemGuard Server Started"
3636
context = {
@@ -56,6 +56,8 @@ def feature_toggles():
5656
feature_toggles_settings.is_disk_info_enabled = 'is_disk_info_enabled' in request.form
5757
feature_toggles_settings.is_network_info_enabled = 'is_network_info_enabled' in request.form
5858
feature_toggles_settings.is_process_info_enabled = 'is_process_info_enabled' in request.form
59+
feature_toggles_settings.refresh_interval = request.form["refresh_interval"]
60+
print("refresh_interval", feature_toggles_settings.refresh_interval)
5961
db.session.commit()
6062
flash('Feature toggles updated successfully!', 'success')
6163
return redirect(url_for('feature_toggles'))

src/scripts/email_me.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def send_smpt_email(receiver_email, subject, body, attachment_path=None, is_html
3131
flash("SMTP email credentials not found. Please set EMAIL_ADDRESS and EMAIL_PASSWORD environment variables.", "danger")
3232
return redirect(url_for('update_smpt_email_password'))
3333

34-
EMAIL_ADDRESS = "email_password.email"
35-
EMAIL_PASSWORD = "email_password.password"
34+
EMAIL_ADDRESS = email_password.email
35+
EMAIL_PASSWORD = email_password.password
3636

3737
print(f"Sending email to {receiver_email}")
3838

@@ -85,6 +85,7 @@ def send_smpt_email(receiver_email, subject, body, attachment_path=None, is_html
8585
}
8686

8787
except Exception as e:
88+
print(f"Failed to send email to {email}. Error: {str(e)}")
8889
return {
8990
"message": f"Failed to send email to {email}. Error: {str(e)}",
9091
"status": "failed",

src/static/css/dashbaord.css

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* Container for the select input */
2+
.selector-container {
3+
display: flex; /* Use flexbox for alignment */
4+
justify-content: flex-end; /* Align items to the right */
5+
padding: 10px; /* Add some padding for spacing */
6+
}
7+
8+
/* Styling the select input */
9+
#refresh-interval {
10+
padding: 5px 8px; /* Adjust padding for a smaller look */
11+
font-size: 14px; /* Smaller font size */
12+
border-radius: 4px; /* Rounded corners */
13+
border: 1px solid #ccc; /* Light border */
14+
outline: none; /* Remove default focus outline */
15+
background-color: #fff; /* White background */
16+
cursor: pointer; /* Pointer cursor for better UX */
17+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Subtle shadow */
18+
transition: border-color 0.3s ease, box-shadow 0.3s ease; /* Smooth transition */
19+
}
20+
21+
/* Hover and focus styles for better interactivity */
22+
#refresh-interval:hover, #refresh-interval:focus {
23+
border-color: #007bff; /* Border color change on hover */
24+
box-shadow: 0 2px 8px rgba(0, 123, 255, 0.2); /* More pronounced shadow */
25+
}
Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,70 @@
1-
{% extends 'base.html' %}{% block title %}Server Dashboard{% endblock %}{% block content %}<div class="container mt-4">
2-
<div class="row">{% include 'dasbhboard_comp/other/username.html' %}{% include
1+
2+
{% extends 'base.html' %}{% block title %}Server Dashboard{% endblock %}{% block content %}
3+
{% block extra_head %}
4+
<link rel="stylesheet" href="{{ url_for('static', filename='css/dashbaord.css') }}">
5+
{% endblock %}
6+
<div class="selector-container">
7+
<div class="row">
8+
<label for="refresh-interval">Select Refresh Interval (seconds):</label>
9+
<select id="refresh-interval">
10+
<option value="5" {% if feature_toggles_settings.refresh_interval == 5 %}selected{% endif %}>5 seconds</option>
11+
<option value="10" {% if feature_toggles_settings.refresh_interval == 10 %}selected{% endif %}>10 seconds</option>
12+
<option value="15" {% if feature_toggles_settings.refresh_interval == 15 %}selected{% endif %}>15 seconds</option>
13+
<option value="30" {% if feature_toggles_settings.refresh_interval == 30 %}selected{% endif %}>30 seconds</option>
14+
<option value="60" {% if feature_toggles_settings.refresh_interval == 60 %}selected{% endif %}>60 seconds</option>
15+
</select>
16+
</div>
17+
</div>
18+
<div class="row">
19+
<!-- Container for positioning the select input on the right -->
20+
{% include 'dasbhboard_comp/other/username.html' %}{% include
321
'dasbhboard_comp/other/boot_time.html' %}{% include 'dasbhboard_comp/battery/percentage.html' %}{% include
422
'dasbhboard_comp/cpu/core.html' %}{% include 'dasbhboard_comp/cpu/usages.html' %}{% include
523
'dasbhboard_comp/cpu/current_temp.html' %}{% include 'dasbhboard_comp/memory/dashboard_memory.html' %}{% include
624
'dasbhboard_comp/memory/usage.html' %}{% include 'dasbhboard_comp/disk/usage.html' %}{% include
725
'dasbhboard_comp/other/uptime.html' %}{% include 'dasbhboard_comp/network/stats.html' %}{% include
826
'dasbhboard_comp/other/speedtest.html' %} </div>
9-
</div>{% endblock %}
27+
</div>
28+
<script>
29+
let refreshInterval = {{ feature_toggles_settings.refresh_interval *1000 }};
30+
let refreshTimeout;
31+
32+
function startRefresh() {
33+
refreshTimeout = setTimeout(function () {
34+
location.reload();
35+
}, refreshInterval);
36+
}
37+
38+
// Start the refresh process
39+
startRefresh();
40+
41+
// Event listener for select input change
42+
document.getElementById('refresh-interval').addEventListener('change', function() {
43+
// Clear the existing timeout
44+
clearTimeout(refreshTimeout);
45+
46+
// Update the interval based on the selected value
47+
refreshInterval = parseInt(this.value) * 1000;
48+
49+
// Restart the refresh process with the new interval
50+
startRefresh();
51+
52+
// Send the updated refresh interval to the server
53+
fetch('/update-refresh-interval', {
54+
method: 'POST',
55+
headers: {
56+
'Content-Type': 'application/json'
57+
},
58+
body: JSON.stringify({ refresh_interval: parseInt(this.value) })
59+
})
60+
.then(response => response.json())
61+
.then(data => {
62+
if (data.success) {
63+
console.log('Refresh interval updated successfully:', data.refresh_interval);
64+
} else {
65+
console.error('Failed to update refresh interval:', data.error);
66+
}
67+
})
68+
.catch(error => console.error('Error:', error));
69+
});
70+
</script>{% endblock %}

src/templates/feature_toggles.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ <h1 class="settings-title">Feature Toggles</h1>
3434
<label for="is_process_info_enabled">Enable Process Information:</label>
3535
<input type="checkbox" id="is_process_info_enabled" name="is_process_info_enabled" {% if feature_toggles_settings.is_process_info_enabled %}checked{% endif %}>
3636
</div>
37-
37+
<div class="form-group">
38+
<label for="refresh_interval">Refresh Interval(sec):</label>
39+
<input type="number" id="refresh_interval" name="refresh_interval" value="{{ feature_toggles_settings.refresh_interval }}">
40+
</div>
3841
<button type="submit" class="btn btn-primary">Save Settings</button>
3942
</form>
4043
</div>

tmp.py

Whitespace-only changes.

0 commit comments

Comments
 (0)