From b105004f3b8afc6694eec9c1b713743ee682da08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Tue, 24 Oct 2023 18:01:13 +0200 Subject: [PATCH] Add the number of agents in the index template --- README.md | 1 - argos/schemas/config.py | 19 ++++++++++++------- argos/server/routes/views.py | 24 +++++++++++++++++++++++- argos/server/templates/agents.html | 21 +++++++++++++++++++++ argos/server/templates/index.html | 7 ++++++- 5 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 argos/server/templates/agents.html diff --git a/README.md b/README.md index 76a98dc..2c8b67d 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,6 @@ Internally, a HTTP API is exposed, and a job queue is used to distribute the che ## Todo: - [ ] Do not return empty list on / when no results from agents. -- [ ] Last seen agents - [ ] donner un aperçu rapide de l’état de la supervision. - [ ] Allow passing a dict to check - [ ] Rename error in unexpected error diff --git a/argos/schemas/config.py b/argos/schemas/config.py index 4d78cf1..418a320 100644 --- a/argos/schemas/config.py +++ b/argos/schemas/config.py @@ -52,13 +52,18 @@ def parse_checks(value): available_names = get_registered_checks().keys() - for name, expected in value.items(): - if name not in available_names: - msg = f"Check should be one of f{available_names}. ({name} given)" - raise ValueError(msg) - if isinstance(expected, int): - expected = str(expected) - return (name, expected) + try: + for name, expected in value.items(): + if name not in available_names: + msg = f"Check should be one of f{available_names}. ({name} given)" + raise ValueError(msg) + if isinstance(expected, int): + expected = str(expected) + return (name, expected) + except: + from ipdb import set_trace + + set_trace() # noqa class WebsitePath(BaseModel): diff --git a/argos/server/routes/views.py b/argos/server/routes/views.py index d6cbd4b..cc1cc5e 100644 --- a/argos/server/routes/views.py +++ b/argos/server/routes/views.py @@ -4,7 +4,7 @@ from urllib.parse import urlparse from fastapi import APIRouter, Depends, Request from fastapi.templating import Jinja2Templates from sqlalchemy import desc -from sqlalchemy.orm import Session +from sqlalchemy.orm import Session, aliased from argos.schemas import Config from argos.server.models import Result, Task @@ -43,6 +43,8 @@ async def read_tasks(request: Request, db: Session = Depends(get_db)): last_checks = {key: max(value) for key, value in domains_last_checks.items()} domains.sort(key=lambda x: x[1]) + agents = db.query(Result.agent_id).distinct().all() + return templates.TemplateResponse( "index.html", { @@ -50,6 +52,7 @@ async def read_tasks(request: Request, db: Session = Depends(get_db)): "domains": domains, "last_checks": last_checks, "total_task_count": len(tasks), + "agents": agents, }, ) @@ -96,3 +99,22 @@ async def get_task_results( "description": description, }, ) + + +@route.get("/agents") +async def get_agents(request: Request, db: Session = Depends(get_db)): + t1 = aliased(Result, name="t1") + t2 = aliased(Result, name="t2") + + last_seen = ( + db.query(t1) + .outerjoin( + t2, (t1.agent_id == t2.agent_id) & (t1.submitted_at < t2.submitted_at) + ) + .filter(t2.agent_id.is_(None)) + .all() + ) + + return templates.TemplateResponse( + "agents.html", {"request": request, "last_seen": last_seen} + ) diff --git a/argos/server/templates/agents.html b/argos/server/templates/agents.html new file mode 100644 index 0000000..9cd6b1a --- /dev/null +++ b/argos/server/templates/agents.html @@ -0,0 +1,21 @@ +{% extends "base.html" %} +{% block title %}

Agents

{% endblock %} +{% block content %} + + + + + + + + + + {% for result in last_seen %} + + + + + {% endfor %} + +
AgentLast seen
{{ result.agent_id }}{{ result.submitted_at }}
+{% endblock %} \ No newline at end of file diff --git a/argos/server/templates/index.html b/argos/server/templates/index.html index d6b8ac3..64a2d36 100644 --- a/argos/server/templates/index.html +++ b/argos/server/templates/index.html @@ -1,7 +1,12 @@ {% extends "base.html" %} {% block content %}
-

{{domains | length}} domains, {{ total_task_count }} tasks

+

+ {{domains | length}} domains, + {{ total_task_count }} tasks, + {{ agents |length }} agent{% if agents|length > 1 %}s{% endif %} + +