CREATE VIEW orchestration_success_rate AS
SELECT
onature.nature AS orchestration_nature,
COUNT(*) AS total_sessions,
SUM(CASE WHEN oss.to_state = 'surveilr_orch_completed' THEN 1 ELSE 0 END) AS successful_sessions,
(CAST(SUM(CASE WHEN oss.to_state = 'surveilr_orch_completed' THEN 1 ELSE 0 END) AS FLOAT) / COUNT(*)) * 100 AS success_rate
FROM orchestration_session os
JOIN orchestration_nature onature ON os.orchestration_nature_id = onature.orchestration_nature_id
JOIN orchestration_session_state oss ON os.orchestration_session_id = oss.session_id
WHERE oss.to_state IN ('surveilr_orch_completed', 'surveilr_orch_failed') -- Consider other terminal states if applicable
GROUP BY onature.nature