Skip to content

Commit 2e554bd

Browse files
chore: reverted system loggin due to memeory issue
1 parent 987920d commit 2e554bd

File tree

6 files changed

+96
-75
lines changed

6 files changed

+96
-75
lines changed

app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from src.thread_process import monitor_settings, start_website_monitoring
44

55
# background thread to monitor system settings changes
6-
monitor_settings() # Starts monitoring for system logging changes
6+
# monitor_settings() # Starts monitoring for system logging changes
77
start_website_monitoring() # Starts pinging active websites
88

99
if __name__ == "__main__":

src/logger.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
logger.addHandler(file_handler)
2929

3030
# Optionally, add console output for real-time debugging (optional)
31-
console_handler = logging.StreamHandler()
32-
console_handler.setLevel(logging.INFO)
33-
console_handler.setFormatter(formatter)
34-
logger.addHandler(console_handler)
31+
# console_handler = logging.StreamHandler()
32+
# console_handler.setLevel(logging.INFO)
33+
# console_handler.setFormatter(formatter)
34+
# logger.addHandler(console_handler)
3535

src/routes/api.py

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from src.config import app, db
44
from src.models import SystemInformation, UserDashboardSettings
55
from src.utils import _get_system_info
6+
import gc
67

78
api_bp = blueprints.Blueprint("api", __name__)
89

@@ -20,66 +21,62 @@ def cpu_percent_api():
2021
from flask import request
2122
from datetime import datetime, timedelta
2223

23-
@app.route('/api/graphs_data')
24+
@app.route('/api/graphs_data', methods=['GET'])
2425
@login_required
2526
def graph_data_api():
2627
try:
27-
curent_time = datetime.now()
28+
current_time = datetime.now()
2829
# Get the time filter from query parameters
2930
time_filter = request.args.get('filter', default='1 day')
30-
31+
3132
# Determine the start time based on the filter
3233
now = datetime.now()
33-
# 5 minutes, 15 minutes, 30 minutes, 1 hour, 3 hours, 6 hours, 12 hours, 1 day, 2 days, 3 days, 1 week, 1 month
34-
if time_filter == '5 minutes':
35-
start_time = now - timedelta(minutes=5)
36-
elif time_filter == '15 minutes':
37-
start_time = now - timedelta(minutes=15)
38-
elif time_filter == '30 minutes':
39-
start_time = now - timedelta(minutes=30)
40-
elif time_filter == '1 hour':
41-
start_time = now - timedelta(hours=1)
42-
elif time_filter == '3 hours':
43-
start_time = now - timedelta(hours=3)
44-
elif time_filter == '6 hours':
45-
start_time = now - timedelta(hours=6)
46-
elif time_filter == '12 hours':
47-
start_time = now - timedelta(hours=12)
48-
elif time_filter == '1 day':
49-
start_time = now - timedelta(days=1)
50-
elif time_filter == '2 days':
51-
start_time = now - timedelta(days=2)
52-
elif time_filter == '3 days':
53-
start_time = now - timedelta(days=3)
54-
elif time_filter == '1 week':
55-
start_time = now - timedelta(weeks=1)
56-
elif time_filter == '1 month':
57-
start_time = now - timedelta(weeks=4)
58-
else:
59-
start_time = now - timedelta(days=1) # Default to 1 day if filter is unknown
60-
34+
time_deltas = {
35+
'5 minutes': timedelta(minutes=5),
36+
'15 minutes': timedelta(minutes=15),
37+
'30 minutes': timedelta(minutes=30),
38+
'1 hour': timedelta(hours=1),
39+
'3 hours': timedelta(hours=3),
40+
'6 hours': timedelta(hours=6),
41+
'12 hours': timedelta(hours=12),
42+
'1 day': timedelta(days=1),
43+
'2 days': timedelta(days=2),
44+
'3 days': timedelta(days=3),
45+
'1 week': timedelta(weeks=1),
46+
'1 month': timedelta(weeks=4)
47+
}
48+
start_time = now - time_deltas.get(time_filter, timedelta(days=1))
6149

6250
# Fetch entries within the time range
63-
recent_system_info_entries = SystemInformation.query.filter(
64-
SystemInformation.timestamp >= start_time
65-
).all()
51+
query = SystemInformation.query.filter(SystemInformation.timestamp >= start_time)
52+
recent_system_info_entries = query.all()
6653

67-
# Use list comprehension to extract data fields if entries exist, else provide empty lists
54+
# Initialize lists for the data
55+
time_data = []
56+
cpu_data = []
57+
memory_data = []
58+
battery_data = []
59+
network_sent_data = []
60+
network_received_data = []
61+
dashboard_memory_usage = []
62+
cpu_frequency = []
63+
current_temp = []
64+
65+
# Extract data fields if entries exist
6866
if recent_system_info_entries:
69-
time_data, cpu_data, memory_data, battery_data, network_sent_data, network_received_data, \
70-
dashboard_memory_usage, cpu_frequency, current_temp = zip(*[
71-
(
72-
info.timestamp, info.cpu_percent, info.memory_percent, info.battery_percent,
73-
info.network_sent, info.network_received, info.dashboard_memory_usage,
74-
info.cpu_frequency, info.current_temp
75-
)
76-
for info in recent_system_info_entries
77-
])
78-
else:
79-
time_data = cpu_data = memory_data = battery_data = network_sent_data = network_received_data = []
80-
dashboard_memory_usage = cpu_frequency = current_temp = []
67+
for info in recent_system_info_entries:
68+
time_data.append(info.timestamp)
69+
cpu_data.append(info.cpu_percent)
70+
memory_data.append(info.memory_percent)
71+
battery_data.append(info.battery_percent)
72+
network_sent_data.append(info.network_sent)
73+
network_received_data.append(info.network_received)
74+
dashboard_memory_usage.append(info.dashboard_memory_usage)
75+
cpu_frequency.append(info.cpu_frequency)
76+
current_temp.append(info.current_temp)
77+
8178
# Return the data as JSON
82-
return jsonify({
79+
response = jsonify({
8380
"time": time_data,
8481
"cpu": cpu_data,
8582
"memory": memory_data,
@@ -89,8 +86,24 @@ def graph_data_api():
8986
"dashboard_memory_usage": dashboard_memory_usage,
9087
"cpu_frequency": cpu_frequency,
9188
"current_temp": current_temp,
92-
"current_time": curent_time
93-
}), 200
89+
"current_time": current_time
90+
})
91+
92+
# Clean up large data structures
93+
del recent_system_info_entries
94+
del time_data
95+
del cpu_data
96+
del memory_data
97+
del battery_data
98+
del network_sent_data
99+
del network_received_data
100+
del dashboard_memory_usage
101+
del cpu_frequency
102+
del current_temp
103+
104+
gc.collect()
105+
106+
return response, 200
94107
except Exception as e:
95108
# Handle and log the error for debugging purposes
96109
return jsonify({'error': 'An error occurred while fetching the graph data', 'details': str(e)}), 500

src/static/css/time_filter.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+
#timeFilter {
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+
#timeFilter:hover, #timeFilter: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+
}

src/static/js/graphs.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,26 +78,11 @@ function formatDate(dateString, currentTime) {
7878
}
7979

8080
function displayTimeAndTimeZone(currentTime, timeZoneName) {
81-
// Update current time and timezone
82-
function updateTime() {
83-
const options = { hour: '2-digit', minute: '2-digit', second: '2-digit', timeZone: timeZoneName };
84-
now = new Date();
85-
const timeString = now.toLocaleTimeString('en-US', options);
86-
87-
// Display the current time and timezone
88-
document.getElementById('currentTime').textContent = `Current Time: ${timeString}`;
89-
document.getElementById('timeZoneName').textContent = `Time Zone: ${timeZoneName}`;
90-
}
91-
92-
// Initial display
93-
updateTime();
94-
95-
// Update time every second
96-
setInterval(updateTime, 1000);
81+
// Display the current time and timezone
82+
document.getElementById('currentTime').textContent = `Current Time: ${currentTime}`;
83+
document.getElementById('timeZoneName').textContent = `Time Zone: ${timeZoneName}`;
9784
}
9885

99-
// Display current time and time zone
100-
10186

10287
// Function to create a chart with multiple datasets
10388
function createChart(ctx, labels, datasets, yLabel) {

src/templates/graphs.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
{% block content %}
1212
<div class="col">
1313
<div class="row selector-container">
14-
{% include 'card_comp/selector/refresh_button.html' %}
1514
<div class="time-filter-container">
1615
<label for="timeFilter">Select Time Filter:</label>
1716
<select id="timeFilter">
@@ -78,5 +77,4 @@ <h2><i class="fas fa-thermometer-half"></i> Current Temperature Over Time</h2>
7877
{% endblock %}
7978
{% block extra_scripts %}
8079
<script src="{{ url_for('static', filename='js/graphs.js')}}"></script>
81-
<script src="{{ url_for('static', filename='js/refresh_page.js')}}"></script>
8280
{% endblock %}

0 commit comments

Comments
 (0)