🎨 — Better domains sorting on domain view

This commit is contained in:
Luc Didry 2023-12-12 05:58:05 +01:00
parent 9316aa9a79
commit 9df5af7cb4
No known key found for this signature in database
GPG key ID: EA868E12D0257E3C

View file

@ -1,5 +1,6 @@
"""Web interface for humans"""
from collections import defaultdict
from functools import cmp_to_key
from urllib.parse import urlparse
from fastapi import APIRouter, Depends, Request
@ -50,7 +51,7 @@ async def get_severity_counts(
@route.get("/details")
async def read_tasks(request: Request, db: Session = Depends(get_db)):
"""Show all tasks and their current state"""
tasks = db.query(Task).order_by(Task.domain.desc()).all()
tasks = db.query(Task).all()
results = (
db.query(Task, Result)
@ -71,9 +72,20 @@ async def read_tasks(request: Request, db: Session = Depends(get_db)):
def _max_severity(severities):
return max(severities, key=SEVERITY_LEVELS.get)
def _cmp_domains(a, b):
if SEVERITY_LEVELS[a[1]] < SEVERITY_LEVELS[b[1]]:
return 1
if SEVERITY_LEVELS[a[1]] > SEVERITY_LEVELS[b[1]]:
return -1
if a[0] > b[0]:
return 1
if a[0] < b[0]:
return -1
return 0
domains = [(key, _max_severity(value)) for key, value in domains_severities.items()]
last_checks = {key: max(value) for key, value in domains_last_checks.items()}
domains.sort(key=lambda x: SEVERITY_LEVELS[x[1]], reverse=True)
domains.sort(key=cmp_to_key(_cmp_domains))
agents = db.query(Result.agent_id).distinct().all()