fix: SQL errors in learning engine for best model selection

This commit is contained in:
Rene Fichtmueller 2026-04-26 20:42:40 +02:00
parent 9808184384
commit d614795545

View File

@ -90,7 +90,7 @@ async function detectImprovements(
const routingResult = await pool.query(
`SELECT routing_model, task_type, COUNT(*) as total, SUM(CASE WHEN was_fallback THEN 1 ELSE 0 END) as fallback_count
FROM routing_decisions
WHERE created_at > NOW() - INTERVAL '1 day' * $1
WHERE created_at > NOW() - MAKE_INTERVAL(days => $1)
GROUP BY routing_model, task_type
HAVING SUM(CASE WHEN was_fallback THEN 1 ELSE 0 END)::float / COUNT(*) > 0.3
ORDER BY fallback_count DESC
@ -152,11 +152,15 @@ async function analyzeAndImprove(pool: any, duration: string): Promise<number> {
// 2. Identify best-performing models per task type and insert recommendations
const bestModels = await pool.query(
`SELECT DISTINCT ON (task_type) task_type, routing_model, success_rate, avg_latency_ms
`SELECT DISTINCT ON (task_type)
task_type,
routing_model,
ROUND((SUM(CASE WHEN success THEN 1 ELSE 0 END)::float / COUNT(*) * 100)::numeric, 2) as success_rate,
AVG(latency_ms)::int as avg_latency_ms
FROM routing_decisions rd
WHERE created_at > NOW() - INTERVAL '1 day'
ORDER BY task_type, (CASE WHEN success THEN 1 ELSE 0 END) DESC, latency_ms ASC
LIMIT 50`,
GROUP BY task_type, routing_model
ORDER BY task_type, (SUM(CASE WHEN success THEN 1 ELSE 0 END)::float / COUNT(*)) DESC, AVG(latency_ms) ASC`,
);
for (const row of bestModels.rows) {