Skip to content

Commit

Permalink
add prerender for index
Browse files Browse the repository at this point in the history
  • Loading branch information
fliepeltje committed Apr 11, 2024
1 parent c857258 commit 5badf20
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.scribble/
__pycache__/
static/out*
static/index_prerender.html
.pytest_cache/
data/
.hypothesis/
Expand Down
23 changes: 13 additions & 10 deletions humitifier/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,78 +37,81 @@ class Filter:
fn: Callable[[Host], bool]


def _host_filters(request: Request, all_hosts: list[Host]) -> list[Filter]:
def host_filters(request: Request | None, all_hosts: list[Host]) -> list[Filter]:

return [
Filter(
typ="search",
id="fqdn",
label="Search Hosts",
options={str(h.fqdn) for h in all_hosts},
value=request.query_params.get("fqdn"),
value=request.query_params.get("fqdn") if request else None,
fn=lambda h, p: not p.get("fqdn") or p.get("fqdn") in h.fqdn,
),
Filter(
typ="select",
id="os",
label="Operating System",
options={str(h.os) for h in all_hosts},
value=request.query_params.get("os"),
value=request.query_params.get("os") if request else None,
fn=lambda h, p: not p.get("os") or p.get("os") == h.os,
),
Filter(
typ="select",
id="alert",
label="Alert",
options={a.__name__ for a in ALERTS},
value=request.query_params.get("alert"),
value=request.query_params.get("alert") if request else None,
fn=lambda h, p: not p.get("alert") or p.get("alert") in h.alert_codes,
),
Filter(
typ="select",
id="department",
label="Department",
options={str(h.department) for h in all_hosts},
value=request.query_params.get("department"),
value=request.query_params.get("department") if request else None,
fn=lambda h, p: not p.get("department") or p.get("department") == h.department,
),
Filter(
typ="select",
id="contact",
label="Contact",
options={str(h.contact) for h in all_hosts},
value=request.query_params.get("contact"),
value=request.query_params.get("contact") if request else None,
fn=lambda h, p: not p.get("contact") or p.get("contact") == h.contact,
),
Filter(
typ="search",
id="package",
label="Package",
options={pkg.name for h in all_hosts for pkg in h.packages or []},
value=request.query_params.get("package"),
value=request.query_params.get("package") if request else None,
fn=lambda h, p: not p.get("package") or p.get("package") in {pkg.name for pkg in h.packages},
),
Filter(
typ="hidden",
id="severity",
label="Severity",
options={"info", "warning", "critical"},
value=request.query_params.get("severity"),
value=request.query_params.get("severity") if request else None,
fn=lambda h, p: not p.get("severity") or p.get("severity") == h.severity,
),
]


def _current_hosts(request: Request, all_hosts: list[Host]) -> list[Host]:
filters = _host_filters(request, all_hosts)
filters = host_filters(request, all_hosts)
return [h for h in all_hosts if all(f.fn(h, request.query_params) for f in filters)]


@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
if not {k: v for k, v in request.query_params.items() if v} and os.path.exists("static/index_prerender.html"):
with open("static/index_prerender.html") as f:
return HTMLResponse(f.read())
hosts = await get_hosts()
current_hosts = _current_hosts(request, hosts)
filters = _host_filters(request, hosts)
filters = host_filters(request, hosts)
template = template_env.get_template("page_index.jinja2")
return HTMLResponse(
template.render(
Expand Down
22 changes: 20 additions & 2 deletions humitifier/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
from rocketry.conds import after_success, hourly
from pssh.clients import ParallelSSHClient
from pssh.output import HostOutput
from ssh2.exceptions import SocketRecvError
from humitifier import facts
from humitifier.config import CONFIG, PSSH_CLIENT, Config
from humitifier.config import CONFIG, Config
from humitifier.dashboard import template_env, host_filters
from humitifier.logging import logging
from humitifier.utils import FactError
from humitifier.models import get_hosts

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -107,3 +108,20 @@ async def parse_facts():
)
await conn.execute("DELETE FROM host_outputs")
await conn.close()


@app.task(after_success(parse_facts))
async def pre_render_index():
logger.info("Pre-rendering index")
template = template_env.get_template("page_index.jinja2")
hosts = await get_hosts()
filters = host_filters(None, hosts)
html = template.render(
current_hosts=hosts,
critical_count=len([h for h in hosts if h.severity == "critical"]),
warning_count=len([h for h in hosts if h.severity == "warning"]),
info_count=len([h for h in hosts if h.severity == "info"]),
filters=filters,
)
with open("static/index_prerender.html", "w") as out:
out.write(html)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "humitifier"
version = "2.0.6"
version = "2.1.0"
description = "Tools and interfaces for displaying server resources"
authors = ["Donatas Rasiukevicius <[email protected]>"]

Expand Down

0 comments on commit 5badf20

Please sign in to comment.