From 7766c7b50330507f5ce8c2bd6edc5f9a7d2b8f57 Mon Sep 17 00:00:00 2001 From: deepak-linux Date: Sun, 1 Sep 2024 12:45:14 +0530 Subject: [PATCH 1/3] feat: :sparkles: caching implement to load web page faster --- README.md | 8 +- src/routes/cpu_info.py | 5 +- src/routes/disk_info.py | 10 +- src/routes/homepage.py | 2 +- src/routes/memory_info.py | 22 +-- .../dasbhboard_comp/other/boot_time.html | 13 +- src/utils.py | 127 +++++++++++++----- 7 files changed, 128 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 624765f..6da4e75 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ It will install the SystemGuard app and its dependencies in the crontab and it w ```bash # install miniconda3 if not installed already wget https://raw.githubusercontent.com/codeperfectplus/HackScripts/main/setup/install_miniconda.sh -chmod +x install_miniconda.sh && ./install_miniconda.sh +chmod +x install_miniconda.sh && sudo ./install_miniconda.sh ``` ## Features 🚀 @@ -84,6 +84,12 @@ chmod +x install_miniconda.sh && ./install_miniconda.sh A Docker image has not been created for this project because it requires access to the host machine in order to retrieve server stats. Therefore, it is not possible to obtain server stats from within a Docker container. +## Upcoming Features 📅 + +- Threshold notifications +- Customizable dashboards +- Plugin support to make SystemGuard even more powerful. + ## Acknowledgments | Project | License | Repository | diff --git a/src/routes/cpu_info.py b/src/routes/cpu_info.py index dde796d..68307f8 100644 --- a/src/routes/cpu_info.py +++ b/src/routes/cpu_info.py @@ -2,15 +2,16 @@ from flask import render_template, blueprints from src.config import app -from src.utils import get_cpu_core_count, get_cpu_frequency, cpu_usage_percent, get_cpu_temp +from src.utils import get_cpu_core_count, get_cpu_frequency, cpu_usage_percent, get_cpu_temp, get_cached_value cpu_info_bp = blueprints.Blueprint("cpu_usage", __name__) @app.route("/cpu_usage") def cpu_usage(): current_temp, high_temp, critical_temp = get_cpu_temp() + cpu_core = get_cached_value("cpu_core", get_cpu_core_count) system_info = { - "cpu_core": get_cpu_core_count(), + "cpu_core": cpu_core, "cpu_frequency": get_cpu_frequency(), "cpu_percent": cpu_usage_percent(), "current_temp": current_temp, diff --git a/src/routes/disk_info.py b/src/routes/disk_info.py index 39bce50..4b82c6f 100644 --- a/src/routes/disk_info.py +++ b/src/routes/disk_info.py @@ -3,16 +3,18 @@ from flask import render_template, blueprints from src.config import app +from src.utils import get_cached_value, get_disk_free, get_disk_total, get_disk_used, get_disk_usage_percent disk_info_bp = blueprints.Blueprint("disk_usage", __name__) @app.route("/disk_usage") def disk_usage(): + disk_total = get_cached_value("disk_total", get_disk_total) system_info = { - "disk_usage": psutil.disk_usage("/").percent, - "disk_total": round(psutil.disk_usage("/").total / (1024**3), 2), # In GB - "disk_used": round(psutil.disk_usage("/").used / (1024**3), 2), # In GB - "disk_free": round(psutil.disk_usage("/").free / (1024**3), 2), # In GB + "disk_usage": get_disk_usage_percent(), + "disk_total": disk_total, + "disk_used": get_disk_used(), + "disk_free": get_disk_free(), } return render_template("disk_info.html", system_info=system_info) diff --git a/src/routes/homepage.py b/src/routes/homepage.py index 811ee25..2933e2d 100644 --- a/src/routes/homepage.py +++ b/src/routes/homepage.py @@ -1,5 +1,5 @@ import datetime -from flask import render_template, blueprints +from flask import render_template, blueprints, session from src.config import app from src.models import SpeedTestResult, DashboardSettings, SystemInfo diff --git a/src/routes/memory_info.py b/src/routes/memory_info.py index bd58f1c..ba0ca32 100644 --- a/src/routes/memory_info.py +++ b/src/routes/memory_info.py @@ -1,23 +1,23 @@ import psutil from flask import render_template, blueprints -from src.utils import swap_memory_info - from src.config import app +from src.utils import get_cached_value, get_memory_percent, get_memory_available, get_memory_used, get_swap_memory_info memory_info_bp = blueprints.Blueprint("memory_usage", __name__) + @app.route("/memory_usage") def memory_usage(): - total_swap, used_swap, free_swap = swap_memory_info() + + memory_available = get_cached_value("memory_available", get_memory_available) system_info = { - "memory_percent": psutil.virtual_memory().percent, - "memory_available": round( - psutil.virtual_memory().available / (1024**3), 2 - ), # In GB - "memory_used": round(psutil.virtual_memory().used / (1024**3), 2), # In GB, - "total_swap": round(total_swap / (1024**3), 2), # In GB - "used_swap": round(used_swap / (1024**3), 2), # In GB - "free_swap": round(free_swap / (1024**3), 2), # In GB + "memory_percent": get_memory_percent(), + "memory_available": memory_available, + "memory_used": get_memory_used(), } + + swap_info = get_swap_memory_info() + system_info.update(swap_info) + return render_template("memory_info.html", system_info=system_info) diff --git a/src/templates/dasbhboard_comp/other/boot_time.html b/src/templates/dasbhboard_comp/other/boot_time.html index 03eb341..957ff2d 100644 --- a/src/templates/dasbhboard_comp/other/boot_time.html +++ b/src/templates/dasbhboard_comp/other/boot_time.html @@ -10,11 +10,16 @@
Server Time