-- Query for Enabled vs Disabled pie chart (percentage only)
-- Total lanes in system: minimum 201, or actual count if more lanes are added
WITH latest_records AS (
    SELECT
        Name,
        Disabled,
        ROW_NUMBER() OVER (PARTITION BY Name ORDER BY t_stamp DESC) AS rn
    FROM lane_data
),
totals AS (
    SELECT
        GREATEST(COUNT(*), 201) AS total_lanes,
        SUM(CASE WHEN Disabled = 1 THEN 1 ELSE 0 END) AS disabled_count
    FROM latest_records
    WHERE rn = 1
)
SELECT
    'Enabled' AS `Status`,
    ROUND((total_lanes - COALESCE(disabled_count, 0)) * 100.0 / total_lanes, 1) AS `Percentage (%)`
FROM totals

UNION ALL

SELECT
    'Disabled' AS `Status`,
    ROUND(COALESCE(disabled_count, 0) * 100.0 / total_lanes, 1) AS `Percentage (%)`
FROM totals;

