Skip to content

Commit

Permalink
refactor: misc
Browse files Browse the repository at this point in the history
- fix styles
- hardcode perm check
- few more indicators
- cache directory size for 5 min (rapid refreshes should be fast enough)
  • Loading branch information
ankush committed Apr 22, 2024
1 parent c712780 commit c9a8cd6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,16 @@ frappe.ui.form.on("System Health Report", {
"queue_status.pending_jobs": (val) => val > 50,
"background_workers.utilization": (val) => val > 70,
"background_workers.failed_jobs": (val) => val > 50,
"top_errors.occurrences": (val) => val > 10,
"failing_scheduled_jobs.failure_rate": (val) => val > 10,
};

const style = document.createElement("style");
style.innerText =
".health-check-failed { border: 1px solid var(--error-border) !important; }";
style.innerText = `.health-check-failed {
font-weight: bold;
color: var(--text-colour);
background-color: var(--bg-red);
}`;
document.head.appendChild(style);

const update_fields = () => {
Expand Down
45 changes: 26 additions & 19 deletions frappe/desk/doctype/system_health_report/system_health_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import frappe
from frappe.model.document import Document
from frappe.utils.background_jobs import get_queue, get_queue_list
from frappe.utils.caching import redis_cache
from frappe.utils.data import add_to_date
from frappe.utils.scheduler import get_scheduler_status

Expand All @@ -41,7 +42,7 @@ def wrapper(*args, **kwargs):
return func(*args, **kwargs)
except Exception as e:
# nosemgrep
frappe.msgprint(f"System Health check step {step} failed: {e}", alert=True)
frappe.msgprint(f"System Health check step {frappe.bold(step)} failed: {e}", alert=True)

return wrapper

Expand Down Expand Up @@ -112,6 +113,7 @@ def db_insert(self, *args, **kwargs):

def load_from_db(self):
super(Document, self).__init__({})
frappe.only_for("System Manager")

# Each method loads a section of health report
# They should be written in a manner they are least likely to fail and if they do fail,
Expand Down Expand Up @@ -240,28 +242,13 @@ def fetch_cache_details(self):
self.cache_keys = len(frappe.cache.get_keys(""))
self.cache_memory_usage = frappe.cache.execute_command("INFO", "MEMORY").get("used_memory_human")

@classmethod
def get_directory_size(cls, *path):
folder = os.path.abspath(frappe.get_site_path(*path))
# Copied as is from agent
total_size = os.path.getsize(folder)
for item in os.listdir(folder):
itempath = os.path.join(folder, item)

if not os.path.islink(itempath):
if os.path.isfile(itempath):
total_size += os.path.getsize(itempath)
elif os.path.isdir(itempath):
total_size += cls.get_directory_size(itempath)
return total_size

@health_check("Storage")
def fetch_storage_details(self):
from frappe.desk.page.backups.backups import get_context

self.backups_size = self.get_directory_size("private", "backups") / (1024 * 1024)
self.private_files_size = self.get_directory_size("private", "files") / (1024 * 1024)
self.public_files_size = self.get_directory_size("public", "files") / (1024 * 1024)
self.backups_size = get_directory_size("private", "backups") / (1024 * 1024)
self.private_files_size = get_directory_size("private", "files") / (1024 * 1024)
self.public_files_size = get_directory_size("public", "files") / (1024 * 1024)
self.onsite_backups = len(get_context({}).get("files", []))

@health_check("Users")
Expand Down Expand Up @@ -315,3 +302,23 @@ def get_job_status(job_id: str | None = None):
return frappe.get_doc("RQ Job", job_id).status
except Exception:
frappe.clear_messages()


@redis_cache(ttl=5 * 60)
def get_directory_size(*path):
return _get_directory_size(*path)


def _get_directory_size(*path):
folder = os.path.abspath(frappe.get_site_path(*path))
# Copied as is from agent
total_size = os.path.getsize(folder)
for item in os.listdir(folder):
itempath = os.path.join(folder, item)

if not os.path.islink(itempath):
if os.path.isfile(itempath):
total_size += os.path.getsize(itempath)
elif os.path.isdir(itempath):
total_size += _get_directory_size(itempath)
return total_size

0 comments on commit c9a8cd6

Please sign in to comment.