Skip to content

Commit

Permalink
chore: process page improved and load testing scritp added
Browse files Browse the repository at this point in the history
  • Loading branch information
codeperfectplus committed Aug 30, 2024
1 parent eb693e6 commit e742c57
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 75 deletions.
17 changes: 11 additions & 6 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ def register_routes():
app.register_blueprint(routes.homepage_bp)
app.register_blueprint(routes.settings_bp)
app.register_blueprint(routes.system_health_bp)
app.register_blueprint(routes.cpu_usage_bp)
app.register_blueprint(routes.disk_usage_bp)
app.register_blueprint(routes.memory_usage_bp)
app.register_blueprint(routes.network_stats_bp)
app.register_blueprint(routes.cpu_info_bp)
app.register_blueprint(routes.disk_info_bp)
app.register_blueprint(routes.memory_info_bp)
app.register_blueprint(routes.network_info_bp)
app.register_blueprint(routes.speedtest_bp)
app.register_blueprint(routes.process_bp)




if __name__ == "__main__":
register_routes()

# # Start the memory-consuming program in a separate thread
# memory_thread = threading.Thread(target=memory_consuming_program, daemon=True)
# memory_thread.start()

# Run the Flask application
app.run(host="0.0.0.0", port=5000, debug=True)
48 changes: 48 additions & 0 deletions locustfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from locust import HttpUser, TaskSet, task, between
from locust.exception import StopUser

class UserBehavior(TaskSet):

def on_start(self):
""" Called when a Locust user starts running """
self.login()

def login(self):
""" Simulate a user logging in """
response = self.client.post("/login", {
"username": "admin",
"password": "adminpassword"
})
if response.status_code == 200 and "Invalid username or password" not in response.text:
print("Login successful")
else:
print("Login failed")
raise StopUser("Login failed")

@task(1)
def view_dashboard(self):
""" Simulate viewing the dashboard """
self.client.get("/")

@task(2)
def view_cpu_usage(self):
""" Simulate viewing CPU usage """
self.client.get("/cpu_usage")

@task(1)
def view_disk_usage(self):
""" Simulate viewing disk usage """
self.client.get("/disk_usage")

@task(1)
def view_memory_usage(self):
""" Simulate viewing memory usage """
self.client.get("/memory_usage")

class WebsiteUser(HttpUser):
tasks = [UserBehavior]
wait_time = between(1, 5) # Time to wait between tasks

if __name__ == "__main__":
import os
os.system("locust -f locustfile.py --host=http://localhost:5000")
2 changes: 2 additions & 0 deletions src/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class SystemInfo(db.Model):
ipv4_connections = db.Column(db.String(50))
dashboard_memory_usage = db.Column(db.String(50))
timestamp = db.Column(db.DateTime, default=datetime.datetime.now())
cpu_frequency = db.Column(db.String(50))
current_temp = db.Column(db.String(50))

def __repr__(self):
return f"<SystemInfo {self.username}, {self.cpu_percent}, {self.memory_percent}, {self.disk_usage}, {self.battery_percent}, {self.cpu_core}, {self.boot_time}, {self.network_sent}, {self.network_received}, {self.process_count}, {self.swap_memory}, {self.uptime}, {self.ipv4_connections}, {self.ipv6_connections}, {self.dashboard_memory_usage}>"
Expand Down
2 changes: 1 addition & 1 deletion src/routes/homepage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask import render_template, blueprints

from src.config import app
from src.models import SpeedTestResult, DashboardSettings
from src.models import SpeedTestResult, DashboardSettings, SystemInfo
from src.utils import datetimeformat, get_system_info

homepage_bp = blueprints.Blueprint("homepage", __name__)
Expand Down
25 changes: 19 additions & 6 deletions src/routes/process.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
from flask import request, render_template, blueprints, redirect, url_for, flash
from flask import request, render_template, redirect, url_for, flash, session, blueprints
from flask_login import login_required, current_user

from src.config import app
from src.utils import get_top_processes

Expand All @@ -14,11 +13,14 @@ def process():
flash("You do not have permission to view this page.", "danger")
return redirect(url_for("dashboard"))

number_of_processes = 5 # Default number
# Retrieve number of processes from session or set default
number_of_processes = session.get('number_of_processes', 50)
sort_by = request.args.get('sort', 'cpu') # Default to sort by CPU usage
order = request.args.get('order', 'asc') # Default to ascending order
toggle_order = 'desc' if order == 'asc' else 'asc' # Toggle order for next click

if request.method == "POST":
if "kill_pid" in request.form:
# Handle killing the process
pid_to_kill = request.form.get("kill_pid")
process_name = request.form.get("process_name")
try:
Expand All @@ -29,7 +31,18 @@ def process():
return redirect(url_for("process")) # Refresh the page after killing process

# Handle the number of processes to display
number_of_processes = int(request.form.get("number", 5))
number_of_processes = int(request.form.get("number", 50))
session['number_of_processes'] = number_of_processes # Store the number in session

# Retrieve processes
top_processes = get_top_processes(number_of_processes)
return render_template("process.html", processes=top_processes, number=number_of_processes)

# Sorting logic based on selected criteria
if sort_by == 'cpu':
top_processes.sort(key=lambda x: x[1], reverse=(order == 'desc'))
elif sort_by == 'memory':
top_processes.sort(key=lambda x: x[2], reverse=(order == 'desc'))
elif sort_by == 'name':
top_processes.sort(key=lambda x: x[0], reverse=(order == 'desc'))

return render_template("process.html", processes=top_processes, number=number_of_processes, toggle_order=toggle_order)
84 changes: 59 additions & 25 deletions src/static/css/process.css
Original file line number Diff line number Diff line change
@@ -1,71 +1,105 @@
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
font-family: 'Roboto', sans-serif; /* Use a modern font */
background-color: #f8f9fa; /* Light gray background for contrast */
margin: 0;
padding: 0;
color: #212529; /* Dark gray text for readability */
}

.container {
width: 80%;
margin: 20px auto;
padding: 20px;
background-color: white;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
max-width: 1200px; /* Limit the maximum width */
margin: 40px auto; /* Increased margin for spacing */
padding: 30px; /* More padding for content */
background-color: #ffffff; /* White background for content */
border-radius: 10px; /* Slightly rounded corners */
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1); /* Subtle shadow for depth */
}

h1 {
color: #333;
color: #343a40; /* Darker shade for the heading */
text-align: center;
margin-bottom: 20px;
font-weight: 700; /* Bolder font */
}

.form-control {
margin-bottom: 20px;
margin-bottom: 25px; /* Consistent spacing */
display: flex;
justify-content: center;
align-items: center;
gap: 10px; /* Modern spacing with gap */
}

label {
margin-right: 10px;
font-weight: 500; /* Medium font weight for labels */
}

input[type="number"] {
width: 60px;
padding: 5px;
margin-right: 10px;
width: 80px; /* Slightly wider input */
padding: 8px;
border: 1px solid #ced4da; /* Light border */
border-radius: 5px;
transition: border-color 0.3s; /* Smooth transition on focus */
}

input[type="number"]:focus {
border-color: #007bff; /* Blue border on focus */
outline: none;
}

button {
padding: 5px 15px;
background-color: #007bff;
color: white;
padding: 8px 20px;
background-color: #007bff; /* Primary color */
color: #ffffff; /* White text */
border: none;
border-radius: 4px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s, transform 0.2s; /* Smooth transitions */
}

button:hover {
background-color: #0056b3;
background-color: #0056b3; /* Darker blue on hover */
transform: translateY(-2px); /* Lift effect */
}

table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
font-size: 0.9rem;
}

th, td {
padding: 10px;
border-bottom: 1px solid #ddd;
padding: 12px 15px;
border-bottom: 1px solid #dee2e6;
text-align: left;
}

th {
background-color: #007bff;
color: white;
background-color: #0056b3; /* Darker blue for better contrast */
color: #ffffff; /* White text for visibility */
font-weight: 600; /* Slightly bolder text */
text-transform: uppercase; /* Uppercase for a modern touch */
position: sticky; /* Sticky header for better navigation */
top: 0;
padding: 15px 10px; /* More padding for a balanced look */
letter-spacing: 0.5px; /* Slight letter spacing for readability */
}

tr:hover {
background-color: #f1f1f1;
background-color: #f1f3f5; /* Light gray on row hover */
}

td button {
background-color: #dc3545;
border-radius: 5px;
transition: background-color 0.3s, transform 0.2s;
}

td button:hover {
background-color: #c82333;
transform: translateY(-2px);
}

.name {
color: #ffffff; /* White text for visibility */
}
8 changes: 7 additions & 1 deletion src/templates/cpu_info.html
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
{% extends 'base.html' %}{% block title %}CPU Usage Details{% endblock %}{% block content %}<h2 class="my-4">CPU Details</h2><div class="container mt-4"><div class="row">{% include 'dasbhboard_comp/cpu/core.html' %} {% include 'dasbhboard_comp/cpu/frequency.html' %} {% include 'dasbhboard_comp/cpu/usages.html' %} {% include 'dasbhboard_comp/cpu/current_temp.html' %} {% include 'dasbhboard_comp/cpu/highest_temp.html' %} {% include 'dasbhboard_comp/cpu/critical_temp.html' %} </div></div>{% endblock %}
{% extends 'base.html' %}{% block title %}CPU Usage Details{% endblock %}{% block content %}<h2 class="my-4">CPU Details
</h2>
<div class="container mt-4">
<div class="row">{% include 'dasbhboard_comp/cpu/core.html' %} {% include 'dasbhboard_comp/cpu/frequency.html' %} {%
include 'dasbhboard_comp/cpu/usages.html' %} {% include 'dasbhboard_comp/cpu/current_temp.html' %} {% include
'dasbhboard_comp/cpu/highest_temp.html' %} {% include 'dasbhboard_comp/cpu/critical_temp.html' %} </div>
</div>{% endblock %}
5 changes: 5 additions & 0 deletions src/templates/ext/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
<i class="fas fa-sign-out-alt mr-1"></i>Logout
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('dashboard') }}">
<i class="fas fa-user-alt mr-1"></i>{{current_user.username}}
</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link {% if request.endpoint == 'login' %}active{% endif %}"
Expand Down
16 changes: 9 additions & 7 deletions src/templates/process.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@
{% block content %}
<div class="container">
<h1>Top Processes by Memory Usage</h1>

<!-- Display flash messages -->
{% include 'ext/message.html' %}

<form method="post" class="form-control">
<label for="number">Number of Processes:</label>
<input type="number" id="number" name="number" min="1" value="{{ number }}">
<button type="submit">Show</button>
</form>
<table>

<table class="table">
<thead>
<tr>
<th>Process Name</th>
<th>CPU Usage (%)</th>
<th>Memory Usage (%)</th>
<th><a href="{{ url_for('process', sort='name', order=toggle_order) }}">Process Name</a></th>
<th><a href="{{ url_for('process', sort='cpu', order=toggle_order) }}">CPU Usage (%)</a></th>
<th><a href="{{ url_for('process', sort='memory', order=toggle_order) }}">Memory Usage (%)</a></th>
<th>Process ID</th>
<th>Action</th>
</tr>
</thead>
Expand All @@ -31,6 +32,7 @@ <h1>Top Processes by Memory Usage</h1>
<td>{{ process[0] }}</td>
<td>{{ process[1] }}</td>
<td>{{ process[2] }}</td>
<td>{{ process[3] }}</td>
<td>
<form method="post">
<input type="hidden" name="kill_pid" value="{{ process[3] }}">
Expand Down
3 changes: 1 addition & 2 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def get_cpu_core_count():
return psutil.cpu_count(logical=True)

def cpu_usage_percent():
return round(psutil.cpu_percent(interval=1), 2)
return psutil.cpu_percent(interval=1)

def get_cpu_temp():
temp = psutil.sensors_temperatures().get('coretemp', [{'current': 'N/A'}])[0]
Expand All @@ -99,7 +99,6 @@ def get_cpu_temp():
critical_temp = temp.critical
return current_temp, high_temp, critical_temp

import psutil

def get_top_processes(number=5):
"""Get the top processes by memory usage."""
Expand Down
27 changes: 0 additions & 27 deletions tmp.py

This file was deleted.

0 comments on commit e742c57

Please sign in to comment.