Skip to content

Commit e9fba18

Browse files
committed
Sort a site's dynamic scans by date, newest first
The scan list on a site's page sorted by row id, on the assumption that id order and time order agree. They do not for a scan recorded by hand or imported from another server, which gets a new id but keeps its own older date, so it jumped to the top. The list now sorts by the scan's own date, newest first, and falls back to when it started and then the id for a scan that has no date yet.
1 parent 70b73e9 commit e9fba18

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

‎app/web/routes_external.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,13 +674,19 @@ def detail_page(asset_id: int, request: Request, scans: int = 1,
674674
links = external.Mapper(db, user).links(asset)
675675
names = [l["full_name"] for l in links]
676676
counts = external.sast_counts(db, names, user)
677-
# Newest first, a page at a time. Started, then finished, time
678-
# order and id order agree, so the id is the cheap way to sort.
677+
# Newest first, by the scan's own date, not its id: a scan recorded by
678+
# hand or imported from another server (InsightAppSec history) gets a
679+
# high id with an older date, so id order would put it in the wrong
680+
# place. Fall back to when it started, then the id, for ties and for a
681+
# scan still running (no date yet).
682+
from sqlalchemy import func as _func
683+
684+
recent = _func.coalesce(ExternalDastScan.scanned_at, ExternalDastScan.started_at)
679685
scan_query = db.query(ExternalDastScan).filter(ExternalDastScan.asset_id == asset.id)
680686
scan_total = scan_query.count()
681687
scan_pages = max(1, -(-scan_total // SCANS_PER_PAGE))
682688
scan_page = min(max(1, scans), scan_pages)
683-
page_scans = (scan_query.order_by(ExternalDastScan.id.desc())
689+
page_scans = (scan_query.order_by(recent.desc(), ExternalDastScan.id.desc())
684690
.offset((scan_page - 1) * SCANS_PER_PAGE).limit(SCANS_PER_PAGE).all())
685691
running = external.running_scans(db, [asset.id])
686692
linked = {l["full_name"]: counts.get(l["full_name"], {}) for l in links if l["linked"]}

0 commit comments

Comments
 (0)