Skip to content

Commit

Permalink
Handle permission error for IO counters
Browse files Browse the repository at this point in the history
Include an option for custom service names
Center collapsible details in the UI
  • Loading branch information
dormant-user committed Sep 29, 2024
1 parent 4038e87 commit 83d866c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
36 changes: 27 additions & 9 deletions pyninja/monitor/templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,6 @@
background-color: #e0e0e0;
}

.center-container {
width: 100%;
margin-left: 45%;
}

.center-container details {
text-align: left;
}

h1 {
width: 100%;
text-align: center;
Expand Down Expand Up @@ -219,9 +210,24 @@

</style>
<style>
/* This container will only take up the space needed */
.center-container {
display: flex;
flex-direction: column;
justify-content: center;
margin-left: 40%;
text-align: left;
}

.center-container details {
width: 40%;
text-align: center;
}

.collapsible-table {
display: table;
border-collapse: collapse;
width: 100%;
}

.collapsible-table-row {
Expand All @@ -237,6 +243,18 @@
.collapsible-table-cell {
font-weight: bold;
}

/* Ensures that the details are collapsed initially and expand on click
details[open] summary:before {
content: '-';
padding-right: 10px;
}

details summary:before {
content: '+';
padding-right: 10px;
}
*/
</style>
<noscript>
<style>
Expand Down
25 changes: 14 additions & 11 deletions pyninja/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@
def default(name: str):
"""Default values for processes and services."""
return {
"PID": 0000,
"Name": name,
"PID": 0,
"Memory": "N/A",
"CPU": "N/A",
"Memory": "N/A",
"Uptime": "N/A",
"Threads": "N/A",
"Read I/O": "N/A",
"Write I/O": "N/A",
"Write I/O": "N/A"
}


def get_process_info(proc: psutil.Process) -> Dict[str, str | int]:
def get_process_info(proc: psutil.Process, process_name: str = None) -> Dict[str, str | int]:
"""Get process information.
Args:
proc: Takes a ``psutil.Process`` object as an argument.
process_name: Takes a custom process name as an optional argument.
Returns:
Dict[str, str | int]:
Expand All @@ -41,12 +43,13 @@ def get_process_info(proc: psutil.Process) -> Dict[str, str | int]:
io_counters = proc.io_counters()
read_io = squire.size_converter(io_counters.read_bytes)
write_io = squire.size_converter(io_counters.write_bytes)
except AttributeError:
except (AttributeError, psutil.AccessDenied) as error:
LOGGER.debug(error)
read_io, write_io = "N/A", "N/A"
try:
return {
"PID": proc.pid,
"Name": proc.name(),
"Name": process_name or proc.name(),
"CPU": f"{proc.cpu_percent(models.MINIMUM_CPU_UPDATE_INTERVAL):.2f}%",
# Resident Set Size
"Memory": squire.size_converter(proc.memory_info().rss),
Expand All @@ -57,9 +60,9 @@ def get_process_info(proc: psutil.Process) -> Dict[str, str | int]:
"Read I/O": read_io,
"Write I/O": write_io,
}
except psutil.NoSuchProcess as error:
except psutil.Error as error:
LOGGER.debug(error)
return default(proc.name())
return default(process_name or proc.name())


async def process_monitor(executor: ThreadPoolExecutor) -> List[Dict[str, str]]:
Expand Down Expand Up @@ -113,11 +116,11 @@ async def service_monitor(executor: ThreadPoolExecutor) -> List[Dict[str, str]]:
continue
try:
proc = psutil.Process(pid)
except psutil.NoSuchProcess:
LOGGER.debug(f"Process with PID {pid} not found")
except psutil.Error as error:
LOGGER.debug(error)
usages.append(default(service_name))
continue
tasks.append(loop.run_in_executor(executor, get_process_info, proc))
tasks.append(loop.run_in_executor(executor, get_process_info, proc, service_name))
for task in asyncio.as_completed(tasks):
usages.append(await task)
return usages
Expand Down

0 comments on commit 83d866c

Please sign in to comment.