-- ================================================================================================ -- Migration 027: Create get_top_models_24h() PostgreSQL function -- Utility function for retrieving top performing models in last 24 hours -- ================================================================================================ -- Function: Get top performing models in last 24 hours CREATE OR REPLACE FUNCTION get_top_models_24h( p_limit INT, p_min_predictions INT ) RETURNS TABLE ( model_id VARCHAR, total_predictions BIGINT, accuracy FLOAT, sharpe_ratio FLOAT, total_pnl FLOAT, avg_weight FLOAT ) AS $$ BEGIN RETURN QUERY SELECT COALESCE(mpa.model_id, 'UNKNOWN')::VARCHAR as model_id, COALESCE(mpa.total_predictions, 0)::BIGINT as total_predictions, COALESCE(mpa.accuracy, 0.0)::FLOAT as accuracy, COALESCE(mpa.sharpe_ratio, 0.0)::FLOAT as sharpe_ratio, COALESCE(mpa.total_pnl::FLOAT, 0.0) as total_pnl, COALESCE(mpa.avg_weight, 0.0)::FLOAT as avg_weight FROM model_performance_attribution mpa WHERE mpa.timestamp >= NOW() - INTERVAL '24 hours' AND mpa.total_predictions >= p_min_predictions ORDER BY mpa.sharpe_ratio DESC NULLS LAST LIMIT p_limit; END; $$ LANGUAGE plpgsql; COMMENT ON FUNCTION get_top_models_24h IS 'Get top N performing models in last 24 hours by Sharpe ratio (requires minimum prediction count)'; -- ================================================================================================ -- END MIGRATION 027 -- ================================================================================================