diff --git a/app/routers/audit_full.py b/app/routers/audit_full.py index a3aef5e..b7c6e84 100644 --- a/app/routers/audit_full.py +++ b/app/routers/audit_full.py @@ -287,16 +287,19 @@ async def audit_full_patching(request: Request, db=Depends(get_db)): patch_weekly = [] if year == 2026: - patch_weekly = db.execute(text( - "SELECT pw.week, SUM(pw.patched) as patched, SUM(pw.cancelled) as cancelled FROM (" - " SELECT unnest(string_to_array(patch_weeks_2026, ',')) as week," - " CASE WHEN patch_status_2026 = 'patched' THEN 1 ELSE 0 END as patched," - " CASE WHEN patch_status_2026 IN ('cancelled','reported') THEN 1 ELSE 0 END as cancelled" - " FROM server_audit_full" - " WHERE status IN ('ok','partial') AND patch_weeks_2026 IS NOT NULL AND patch_weeks_2026 != ''" - " AND id IN (SELECT DISTINCT ON (hostname) id FROM server_audit_full WHERE status IN ('ok','partial') ORDER BY hostname, audit_date DESC)" - ") pw WHERE pw.week != '' GROUP BY pw.week ORDER BY pw.week" - )).fetchall() + patch_weekly = db.execute(text(""" + SELECT week, SUM(patched)::int as patched, SUM(cancelled)::int as cancelled FROM ( + SELECT unnest(string_to_array(patch_weeks_2026, ',')) as week, 1 as patched, 0 as cancelled + FROM server_audit_full + WHERE status IN ('ok','partial') AND patch_weeks_2026 IS NOT NULL AND patch_weeks_2026 != '' + AND id IN (SELECT DISTINCT ON (hostname) id FROM server_audit_full WHERE status IN ('ok','partial') ORDER BY hostname, audit_date DESC) + UNION ALL + SELECT unnest(string_to_array(cancelled_weeks_2026, ',')) as week, 0 as patched, 1 as cancelled + FROM server_audit_full + WHERE status IN ('ok','partial') AND cancelled_weeks_2026 IS NOT NULL AND cancelled_weeks_2026 != '' + AND id IN (SELECT DISTINCT ON (hostname) id FROM server_audit_full WHERE status IN ('ok','partial') ORDER BY hostname, audit_date DESC) + ) combined WHERE week != '' GROUP BY week ORDER BY week + """)).fetchall() all_domains = db.execute(text("SELECT code, name, 'domain' as type FROM domains ORDER BY name")).fetchall() all_zones = db.execute(text("SELECT name as code, name, 'zone' as type FROM zones ORDER BY name")).fetchall()