Skip to content

Commit

Permalink
Improve disk report util and set it specific to Linux machines
Browse files Browse the repository at this point in the history
  • Loading branch information
dormant-user committed Nov 25, 2024
1 parent 9c16d1b commit f0386d7
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docs/README.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.19: https://docutils.sourceforge.io/" />

<title>PyNinja &#8212; PyNinja documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
Expand Down
5 changes: 3 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.19: https://docutils.sourceforge.io/" />

<title>Welcome to PyNinja’s documentation! &#8212; PyNinja documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
Expand Down Expand Up @@ -2284,7 +2284,7 @@ <h1>PyNinja - Monitor<a class="headerlink" href="#pyninja-monitor" title="Permal

<dl class="py function">
<dt class="sig sig-object py" id="pyninja.monitor.resources.container_cpu_limit">
<span class="sig-prename descclassname"><span class="pre">pyninja.monitor.resources.</span></span><span class="sig-name descname"><span class="pre">container_cpu_limit</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">container_name</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><span class="pre">int</span><span class="w"> </span><span class="p"><span class="pre">|</span></span><span class="w"> </span><span class="pre">None</span></span></span><a class="headerlink" href="#pyninja.monitor.resources.container_cpu_limit" title="Permalink to this definition"></a></dt>
<span class="sig-prename descclassname"><span class="pre">pyninja.monitor.resources.</span></span><span class="sig-name descname"><span class="pre">container_cpu_limit</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">container_name</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><span class="pre">int</span><span class="w"> </span><span class="p"><span class="pre">|</span></span><span class="w"> </span><span class="pre">float</span><span class="w"> </span><span class="p"><span class="pre">|</span></span><span class="w"> </span><span class="pre">None</span></span></span><a class="headerlink" href="#pyninja.monitor.resources.container_cpu_limit" title="Permalink to this definition"></a></dt>
<dd><p>Get CPU cores configured for a particular container using NanoCpus.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
Expand Down Expand Up @@ -2441,6 +2441,7 @@ <h1>PyNinja - Monitor<a class="headerlink" href="#pyninja-monitor" title="Permal
<dd class="field-odd"><ul class="simple">
<li><p><strong>request</strong> – Reference to the FastAPI request object.</p></li>
<li><p><strong>session_token</strong> – Session token set after verifying username and password.</p></li>
<li><p><strong>render</strong> – Render option set by the UI.</p></li>
</ul>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
Expand Down
2 changes: 1 addition & 1 deletion pyninja/monitor/authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ async def validate_session(host: str, cookie_string: str, log: bool = True) -> N
if models.env.no_auth:
if log:
LOGGER.info("No auth set! Bypassing auth filters!")
return True
return
try:
decoded_payload = base64.b64decode(cookie_string)
decoded_str = decoded_payload.decode("ascii")
Expand Down
23 changes: 12 additions & 11 deletions pyninja/monitor/drive.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import os
import logging

from fastapi.responses import HTMLResponse

LOGGER = logging.getLogger("uvicorn.default")

def report():
"""Generated disk utility report and returns an HTMLResponse."""
try:
import pyudisk

report_file = os.path.join(os.getcwd(), "disk-report.html")
return HTMLResponse(
content=pyudisk.generate_report(filepath=report_file, raw=True)
)
except Exception:
return None
async def report() -> HTMLResponse:
"""Generates a disk report using pyudisk.
Returns:
HTMLResponse:
Returns an HTML response with the disk report.
"""
import pyudisk

return HTMLResponse(content=pyudisk.generate_report(raw=True))
2 changes: 1 addition & 1 deletion pyninja/monitor/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def landing_page() -> Dict[str, Any]:
)


def container_cpu_limit(container_name: str) -> int | None:
def container_cpu_limit(container_name: str) -> int | float | None:
"""Get CPU cores configured for a particular container using NanoCpus.
Args:
Expand Down
14 changes: 12 additions & 2 deletions pyninja/monitor/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ async def monitor_endpoint(
Args:
request: Reference to the FastAPI request object.
session_token: Session token set after verifying username and password.
render: Render option set by the UI.
Returns:
HTMLResponse:
Expand Down Expand Up @@ -132,8 +133,16 @@ async def monitor_endpoint(
)
elif render == "drive":
LOGGER.info("Rendering disk report!")
response = monitor.drive.report()
# If drive option is chosen during login page, the cookie is deleted once logged in!
try:
response = await monitor.drive.report()
# If drive option is chosen during login page, the cookie is deleted once logged in!
except Exception as error:
LOGGER.error(error)
response = await monitor.config.clear_session(
response=HTMLResponse(
content="Failed to generate disk report", status_code=500
)
)
response.delete_cookie(key="render")
return response
# todo: Implement a better way to handle this
Expand All @@ -145,6 +154,7 @@ async def monitor_endpoint(
"request": request,
"signin": "/login",
"version": f"v{version.__version__}",
"linux": models.OPERATING_SYSTEM == "linux",
},
)

Expand Down
8 changes: 5 additions & 3 deletions pyninja/monitor/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,11 @@ <h2 style="margin-top:5%">This page requires JavaScript
<i class="fa-regular fa-eye" id="eye"></i>
</div>
<button type="submit" onclick="submitToAPI(event, 'monitor')">Sign In</button>
<br>
<!-- todo: this option should ONLY be available for Linux servers -->
<button type="submit" onclick="submitToAPI(event, 'drive')">Disk Report</button>
{% if linux %}
<br>
<!-- todo: this option should ONLY be available for Linux servers -->
<button type="submit" onclick="submitToAPI(event, 'drive')">Disk Report</button>
{% endif %}
</form>
</div>
</div>
Expand Down

0 comments on commit f0386d7

Please sign in to comment.