🗃 — Make simpler DB query in get_agents_view

This commit is contained in:
Luc Didry 2024-02-28 16:59:14 +01:00
parent 67d92499bb
commit 9ff2814789
No known key found for this signature in database
GPG key ID: EA868E12D0257E3C

View file

@ -5,7 +5,8 @@ from urllib.parse import urlparse
from fastapi import APIRouter, Depends, Request from fastapi import APIRouter, Depends, Request
from fastapi.templating import Jinja2Templates from fastapi.templating import Jinja2Templates
from sqlalchemy.orm import Session, aliased from sqlalchemy import func
from sqlalchemy.orm import Session
from argos.schemas import Config from argos.schemas import Config
from argos.server import queries from argos.server import queries
@ -144,15 +145,9 @@ async def get_task_results_view(
@route.get("/agents") @route.get("/agents")
async def get_agents_view(request: Request, db: Session = Depends(get_db)): async def get_agents_view(request: Request, db: Session = Depends(get_db)):
"""Show argos agents and the last time the server saw them""" """Show argos agents and the last time the server saw them"""
t1 = aliased(Result, name="t1")
t2 = aliased(Result, name="t2")
last_seen = ( last_seen = (
db.query(t1) db.query(Result.agent_id, func.max(Result.submitted_at).label('submitted_at'))
.outerjoin( .group_by(Result.agent_id)
t2, (t1.agent_id == t2.agent_id) & (t1.submitted_at < t2.submitted_at)
)
.filter(t2.agent_id.is_(None))
.all() .all()
) )