feat: drill-down filters on overview and outdated agent version highlight
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -62,3 +62,39 @@ def count_stale_hosts(db: Session, stale_minutes: int) -> int:
|
||||
for host_id in host_ids
|
||||
if agent_status(hb_map.get(host_id), stale_minutes=stale_minutes) == "stale"
|
||||
)
|
||||
|
||||
|
||||
def apply_agent_status_host_filter(
|
||||
stmt,
|
||||
count_stmt,
|
||||
agent_status_filter: str,
|
||||
*,
|
||||
stale_minutes: int,
|
||||
):
|
||||
"""Ограничить выборку Host по online / stale / unknown (как в agent_status)."""
|
||||
from datetime import timedelta
|
||||
|
||||
hb_subq = (
|
||||
select(Event.host_id, func.max(Event.received_at).label("last_hb"))
|
||||
.where(Event.type == HEARTBEAT_TYPE)
|
||||
.group_by(Event.host_id)
|
||||
.subquery()
|
||||
)
|
||||
cutoff = datetime.now(timezone.utc) - timedelta(minutes=stale_minutes)
|
||||
|
||||
if agent_status_filter == "online":
|
||||
join = stmt.join(hb_subq, Host.id == hb_subq.c.host_id)
|
||||
count_join = count_stmt.join(hb_subq, Host.id == hb_subq.c.host_id)
|
||||
cond = hb_subq.c.last_hb >= cutoff
|
||||
return join.where(cond), count_join.where(cond)
|
||||
if agent_status_filter == "stale":
|
||||
join = stmt.join(hb_subq, Host.id == hb_subq.c.host_id)
|
||||
count_join = count_stmt.join(hb_subq, Host.id == hb_subq.c.host_id)
|
||||
cond = hb_subq.c.last_hb < cutoff
|
||||
return join.where(cond), count_join.where(cond)
|
||||
if agent_status_filter == "unknown":
|
||||
join = stmt.outerjoin(hb_subq, Host.id == hb_subq.c.host_id)
|
||||
count_join = count_stmt.outerjoin(hb_subq, Host.id == hb_subq.c.host_id)
|
||||
cond = hb_subq.c.last_hb.is_(None)
|
||||
return join.where(cond), count_join.where(cond)
|
||||
return stmt, count_stmt
|
||||
|
||||
Reference in New Issue
Block a user