pygui: implemented cpu usage monitor to status bar

This commit is contained in:
Blake Harnden 2020-07-22 21:57:05 -07:00
parent f8d862a296
commit 3544d00431
2 changed files with 54 additions and 13 deletions

View file

@ -590,8 +590,7 @@ class CanvasGraph(tk.Canvas):
) )
logging.debug("ratio: %s", self.ratio) logging.debug("ratio: %s", self.ratio)
logging.debug("offset: %s", self.offset) logging.debug("offset: %s", self.offset)
zoom_label = f"{self.ratio * 100:.0f}%" self.app.statusbar.set_zoom(self.ratio)
self.app.statusbar.zoom.config(text=zoom_label)
if self.wallpaper: if self.wallpaper:
self.redraw_wallpaper() self.redraw_wallpaper()

View file

@ -1,7 +1,10 @@
""" """
status bar status bar
""" """
import sched
import tkinter as tk import tkinter as tk
from pathlib import Path
from threading import Thread
from tkinter import ttk from tkinter import ttk
from typing import TYPE_CHECKING, List, Optional from typing import TYPE_CHECKING, List, Optional
@ -13,6 +16,41 @@ if TYPE_CHECKING:
from core.gui.app import Application from core.gui.app import Application
class CpuUsage:
def __init__(self, statusbar: "StatusBar") -> None:
self.scheduler: sched.scheduler = sched.scheduler()
self.running: bool = False
self.thread: Optional[Thread] = None
self.prev_idle: int = 0
self.prev_total: int = 0
self.stat_file: Path = Path("/proc/stat")
self.statusbar: "StatusBar" = statusbar
def start(self) -> None:
self.running = True
self.thread = Thread(target=self._start, daemon=True)
self.thread.start()
def _start(self):
self.scheduler.enter(0, 0, self.run)
self.scheduler.run()
def run(self) -> None:
lines = self.stat_file.read_text().splitlines()[0]
values = [int(x) for x in lines.split()[1:]]
idle = sum(values[3:5])
non_idle = sum(values[:3] + values[5:8])
total = idle + non_idle
total_diff = total - self.prev_total
idle_diff = idle - self.prev_idle
cpu_percent = (total_diff - idle_diff) / total_diff
self.statusbar.after(0, self.statusbar.set_cpu, cpu_percent)
self.prev_idle = idle
self.prev_total = total
if self.running:
self.scheduler.enter(3, 0, self.run)
class StatusBar(ttk.Frame): class StatusBar(ttk.Frame):
def __init__(self, master: tk.Widget, app: "Application") -> None: def __init__(self, master: tk.Widget, app: "Application") -> None:
super().__init__(master) super().__init__(master)
@ -20,12 +58,14 @@ class StatusBar(ttk.Frame):
self.status: Optional[ttk.Label] = None self.status: Optional[ttk.Label] = None
self.statusvar: tk.StringVar = tk.StringVar() self.statusvar: tk.StringVar = tk.StringVar()
self.zoom: Optional[ttk.Label] = None self.zoom: Optional[ttk.Label] = None
self.cpu_usage: Optional[ttk.Label] = None self.cpu_label: Optional[ttk.Label] = None
self.alerts_button: Optional[ttk.Button] = None self.alerts_button: Optional[ttk.Button] = None
self.alert_style = Styles.no_alert self.alert_style = Styles.no_alert
self.running: bool = False self.running: bool = False
self.core_alarms: List[ExceptionEvent] = [] self.core_alarms: List[ExceptionEvent] = []
self.draw() self.draw()
self.cpu_usage: CpuUsage = CpuUsage(self)
self.cpu_usage.start()
def draw(self) -> None: def draw(self) -> None:
self.columnconfigure(0, weight=7) self.columnconfigure(0, weight=7)
@ -46,25 +86,27 @@ class StatusBar(ttk.Frame):
) )
self.status.grid(row=0, column=0, sticky="ew") self.status.grid(row=0, column=0, sticky="ew")
self.zoom = ttk.Label( self.zoom = ttk.Label(self, anchor=tk.CENTER, borderwidth=1, relief=tk.RIDGE)
self,
text="%s" % (int(self.app.canvas.ratio * 100)) + "%",
anchor=tk.CENTER,
borderwidth=1,
relief=tk.RIDGE,
)
self.zoom.grid(row=0, column=1, sticky="ew") self.zoom.grid(row=0, column=1, sticky="ew")
self.set_zoom(self.app.canvas.ratio)
self.cpu_usage = ttk.Label( self.cpu_label = ttk.Label(
self, text="CPU TBD", anchor=tk.CENTER, borderwidth=1, relief=tk.RIDGE self, anchor=tk.CENTER, borderwidth=1, relief=tk.RIDGE
) )
self.cpu_usage.grid(row=0, column=2, sticky="ew") self.cpu_label.grid(row=0, column=2, sticky="ew")
self.set_cpu(0.0)
self.alerts_button = ttk.Button( self.alerts_button = ttk.Button(
self, text="Alerts", command=self.click_alerts, style=self.alert_style self, text="Alerts", command=self.click_alerts, style=self.alert_style
) )
self.alerts_button.grid(row=0, column=3, sticky="ew") self.alerts_button.grid(row=0, column=3, sticky="ew")
def set_cpu(self, usage: float) -> None:
self.cpu_label.config(text=f"CPU {usage * 100:.2f}%")
def set_zoom(self, zoom: float) -> None:
self.zoom.config(text=f"ZOOM {zoom * 100:.0f}%")
def add_alert(self, event: ExceptionEvent) -> None: def add_alert(self, event: ExceptionEvent) -> None:
self.core_alarms.append(event) self.core_alarms.append(event)
level = event.exception_event.level level = event.exception_event.level