updated best icon selection logic

This commit is contained in:
Joe Lothan 2026-05-20 01:15:08 -04:00
parent 03e343a136
commit 902928235c
2 changed files with 35 additions and 23 deletions

View file

@ -1,13 +1,14 @@
-- Best Icon Selection
-- Picks the best completed icon for each host and stores its s3_key in hosts.best_icon_s3_key.
--
-- Target: 32x32 source icon (displayed at 16x16 CSS, crisp on 2x Retina).
--
-- Priority:
-- 1. Standard square sizes (64 > 48 > 32 > 16) — ideal for tab display
-- 2. Other square sizes ≤64
-- 3. Non-square sizes ≤64 on both axes
-- 4. Anything larger (downloaded because rel_sizes was undeclared)
-- 5. Among equal priority: prefer PNG/GIF/ICO over WebP (SVGs excluded — not supported in bundle generation)
-- 6. Tiebreaker: smaller file size (less bandwidth in bundles)
-- 1. Icons ≥32px: prefer smallest first (closest to 32 — a 32x32 beats a 48x48 beats a 180x180)
-- 2. Icons <32px: prefer largest first (16x16 beats 8x8)
-- 3. Within same size: prefer PNG > ICO > GIF/JPEG/BMP > WebP
-- 4. Tiebreaker: smaller file size
-- SVGs excluded (not supported in bundle generation). Icons ≤2x2 excluded (tracking pixels).
--
-- Usage: psql $DATABASE_URL -f pipeline/04_best_icon/select.sql
@ -17,22 +18,30 @@ FROM (
FROM icons i
WHERE i.scan_state = 'completed'
AND i.s3_key IS NOT NULL
AND i.content_type NOT IN ('image/svg+xml')
AND i.content_type != 'image/svg+xml'
AND (i.width IS NULL OR i.width > 2)
AND (i.height IS NULL OR i.height > 2)
ORDER BY i.host_id,
-- Tier: ≥32 preferred over <32. NULL dimensions go last.
CASE
WHEN i.width = i.height AND i.width IN (64, 48, 32, 16) THEN 0
WHEN i.width = i.height AND i.width <= 64 THEN 1
WHEN i.width IS NOT NULL AND i.width <= 64 AND i.height IS NOT NULL AND i.height <= 64 THEN 2
ELSE 3
END,
COALESCE(i.width, 0) DESC,
CASE
WHEN i.content_type IN ('image/png', 'image/gif', 'image/x-icon', 'image/vnd.microsoft.icon') THEN 0
WHEN i.content_type = 'image/webp' THEN 1
WHEN LEAST(COALESCE(i.width, 0), COALESCE(i.height, 0)) >= 32 THEN 0
WHEN COALESCE(i.width, 0) > 0 THEN 1
ELSE 2
END,
-- Within ≥32: smallest first (closest to 32). Within <32: largest first.
CASE
WHEN LEAST(COALESCE(i.width, 0), COALESCE(i.height, 0)) >= 32
THEN GREATEST(COALESCE(i.width, 0), COALESCE(i.height, 0))
ELSE -GREATEST(COALESCE(i.width, 0), COALESCE(i.height, 0))
END,
-- Format preference
CASE
WHEN i.content_type = 'image/png' THEN 0
WHEN i.content_type IN ('image/x-icon', 'image/vnd.microsoft.icon') THEN 1
WHEN i.content_type IN ('image/gif', 'image/jpeg', 'image/bmp') THEN 2
WHEN i.content_type = 'image/webp' THEN 3
ELSE 4
END,
i.file_size ASC
) sub
WHERE h.id = sub.host_id;