base_panel: titlebar: turn red if high cpu or memory is in use, show usage

base_panel: rate limit usage notification
This commit is contained in:
alfrix 2024-03-15 23:07:59 -03:00
parent 16f967a86c
commit 489bd495ea

View File

@ -21,6 +21,7 @@ class BasePanel(ScreenPanel):
self.titlebar_items = []
self.titlebar_name_type = None
self.current_extruder = None
self.last_usage_report = datetime.now()
# Action bar buttons
abscale = self.bts * 1.1
self.control['back'] = self._gtk.Button('back', scale=abscale)
@ -206,6 +207,25 @@ class BasePanel(ScreenPanel):
self._screen._menu_go_back()
def process_update(self, action, data):
if action == "notify_proc_stat_update":
cpu = (max(data["system_cpu_usage"][core] for core in data["system_cpu_usage"] if core.startswith("cpu")))
memory = (data["system_memory"]["used"] / data["system_memory"]["total"]) * 100
error = "message_popup_error"
ctx = self.titlebar.get_style_context()
if cpu > 80 or memory > 85:
self.last_usage_report = datetime.now()
if not ctx.has_class(error):
ctx.add_class(error)
msg = f"CPU: {cpu:2.0f}% RAM: {memory:2.0f}%"
self._screen.log_notification(msg, 3)
self.titlelbl.set_label(msg)
elif ctx.has_class(error):
if (datetime.now() - self.last_usage_report).seconds < 3:
return
ctx.remove_class(error)
self.titlelbl.set_label(f"{self._screen.connecting_to_printer}")
return
if action == "notify_update_response":
if self.update_dialog is None:
self.show_update_dialog()