Compare commits

..

3 commits

Author SHA1 Message Date
Luc Didry
5b91c6a482
🩹 — Fix tasks_tmp table truncating 2025-04-14 16:58:29 +02:00
Luc Didry
2e6c2e205f
— Allow to customize the name of the argos agent 2025-04-14 16:11:45 +02:00
Luc Didry
4c378e94ac
— Add waiting tasks count on index page and app API 2025-04-14 11:50:56 +02:00
7 changed files with 54 additions and 8 deletions

View file

@ -7,10 +7,13 @@
- 🔊 — Improve check agent log
- 🔒️ — Logging out now invalidate tokens
- 📝 — Improve OpenAPI doc
- 🤕 — Fix order of tasks sent to agent
- 🩹 — Fix order of tasks sent to agent
- ✨ — Add application API (fix #86)
- ⚡️ — Faster websites configuration reloading (#85)
- 💄 — Better mobile display
- ✨ — Add waiting tasks count on index page and app API
- ✨ — Allow to customize the name of the argos agent
- 🩹 — Fix tasks_tmp table truncating
## 0.9.0

View file

@ -38,7 +38,13 @@ class ArgosAgent: # pylint: disable-msg=too-many-instance-attributes
"""The Argos agent is responsible for running the checks and reporting the results."""
def __init__( # pylint: disable-msg=too-many-positional-arguments
self, server: str, auth: str, max_tasks: int, wait_time: int, user_agent: str
self,
server: str,
auth: str,
max_tasks: int,
wait_time: int,
user_agent: str,
name: str,
):
self.server = server
self.max_tasks = max_tasks
@ -53,7 +59,10 @@ class ArgosAgent: # pylint: disable-msg=too-many-instance-attributes
self._http_client_v6: httpx.AsyncClient | None = None
self._res_cache: dict[str, httpx.Response] = {}
self.agent_id = socket.gethostname()
if name == "":
self.agent_id = socket.gethostname()
else:
self.agent_id = name
@retry(after=log_failure, wait=wait_random(min=1, max=2))
async def run(self):

View file

@ -92,12 +92,13 @@ def version():
default="INFO",
type=click.Choice(logging.LOG_LEVELS, case_sensitive=False),
)
@click.option("--name", default="", help="The name of the agent sent to the server")
@click.option(
"--user-agent",
default="",
help="A custom string to append to the User-Agent header",
)
def agent(server_url, auth, max_tasks, wait_time, log_level, user_agent): # pylint: disable-msg=too-many-positional-arguments
def agent(server_url, auth, max_tasks, wait_time, log_level, user_agent, name): # pylint: disable-msg=too-many-positional-arguments
"""Get and run tasks for the provided server. Will wait for new tasks.
Usage: argos agent https://argos.example.org "auth-token-here"
@ -113,7 +114,14 @@ def agent(server_url, auth, max_tasks, wait_time, log_level, user_agent): # pyl
from argos.logging import logger
logger.setLevel(log_level)
agent_ = ArgosAgent(server_url, auth, max_tasks, wait_time, user_agent)
agent_ = ArgosAgent(
server=server_url,
auth=auth,
max_tasks=max_tasks,
wait_time=wait_time,
user_agent=user_agent,
name=name,
)
asyncio.run(agent_.run())

View file

@ -568,9 +568,8 @@ async def update_from_config(db: Session, config: schemas.Config):
db.commit()
else:
logger.debug("Truncating tasks_tmp table")
db.execute(
sa_text("TRUNCATE TABLE tasks_tmp;").execution_options(autocommit=True)
)
db.execute(sa_text("TRUNCATE TABLE tasks_tmp RESTART IDENTITY;"))
db.commit()
return {
"added": added_tasks,
@ -594,6 +593,19 @@ async def get_severity_counts(db: Session) -> dict:
return counts_dict
async def get_waiting_tasks_count(db: Session) -> int:
"""Get the count of tasks waiting to be processed"""
waiting_tasks = (
db.query(Task.id)
.filter(
Task.selected_by == None,
((Task.next_run <= datetime.now()) | (Task.next_run == None)),
)
.count()
)
return waiting_tasks
async def reschedule_all(db: Session):
"""Reschedule checks of all non OK tasks ASAP"""
db.query(Task).filter(Task.severity != "ok").update(

View file

@ -170,6 +170,7 @@ async def logout(
class SeverityCounts(BaseModel):
severities: Dict[Literal["ok", "warning", "critical", "unknown"], int]
agents: int
waiting_tasks: int
model_config = {
"json_schema_extra": {
@ -177,6 +178,7 @@ class SeverityCounts(BaseModel):
{
"severities": {"ok": 10, "warning": 0, "critical": 2, "unknown": 0},
"agents": 1,
"waiting_tasks": 3,
}
]
}
@ -194,9 +196,12 @@ async def get_severity_counts(
agents = db.query(Result.agent_id).distinct().all()
waiting_tasks = await queries.get_waiting_tasks_count(db)
return {
"severities": counts_dict,
"agents": len(agents),
"waiting_tasks": waiting_tasks,
}

View file

@ -158,12 +158,15 @@ async def get_severity_counts_view(
agents = db.query(Result.agent_id).distinct().all()
waiting_tasks = await queries.get_waiting_tasks_count(db)
return templates.TemplateResponse(
"index.html",
{
"request": request,
"counts_dict": counts_dict,
"agents": agents,
"waiting_tasks": waiting_tasks,
"auto_refresh_enabled": auto_refresh_enabled,
"auto_refresh_seconds": auto_refresh_seconds,
"user": user,

View file

@ -11,6 +11,9 @@
{{ agents | length }} agent{{ 's' if agents | length > 1 }}
</a>
</li>
<li>
{{ waiting_tasks }} task{{ 's' if waiting_tasks > 1 }} awaiting to be processed
</li>
</ul>
<ul>
<li>
@ -51,6 +54,9 @@
{{ agents | length }} agent{{ 's' if agents | length > 1 }}
</a>
</li>
<li>
{{ waiting_tasks }} task{{ 's' if waiting_tasks > 1 }} awaiting to be processed
</li>
</ul>
<div>
<ul>