Skip to content

Commit

Permalink
updating process count logic
Browse files Browse the repository at this point in the history
  • Loading branch information
davidslusser committed Feb 18, 2024
1 parent fb172e3 commit 57e1726
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
24 changes: 15 additions & 9 deletions src/djangoaddicts/hostutils/views/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,22 @@ def get(self, request, *args, **kwargs):
context["title"] = self.title
context["now"] = datetime.datetime.now()
context["subtitle"] = psutil.os.uname()[1]
process_list = list(psutil.process_iter())
counts = {"running": 0, "sleeping": 0, "idle": 0, "stopped": 0, "zombie": 0, "dead": 0, "disk-sleep": 0}
process_list = []
for process in psutil.process_iter():
try:
counts[process.status()] += 1
process_list.append(
{
"pid": process.pid,
"name": process.name(),
"status": process.status(),
"create_time": process.create_time(),
}
)
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
context["process_list"] = process_list
counts = {
"running": len([i for i in process_list if i.status() == "running"]),
"sleeping": len([i for i in process_list if i.status() == "sleeping"]),
"idle": len([i for i in process_list if i.status() == "idle"]),
"stopped": len([i for i in process_list if i.status() == "stopped"]),
"zombie": len([i for i in process_list if i.status() == "zombie"]),
"dead": len([i for i in process_list if i.status() == "dead"]),
}
context["counts"] = counts
filter_form = {}
filter_form["form"] = HostProcessFilterForm(request.GET or None)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def test_get(self):
def test_get_with_invalid(self):
"""verify page is redered if psutil.NoSuchProcess is raised"""
url = reverse("hostutils:host_process")
process_list = psutil.process_iter()
with patch("psutil.process_iter") as mocked_process_list:
process_list = psutil.process_iter()
p = subprocess.Popen("ls", stdout=subprocess.PIPE)
mp = iter((psutil.Process(p.pid), psutil.Process(p.pid)))
p.communicate()
Expand Down

0 comments on commit 57e1726

Please sign in to comment.