|
| 1 | +import type { QueryPlan } from "./types" |
| 2 | + |
| 3 | +// Database schema mapping |
| 4 | +const SCHEMA_CONFIG = { |
| 5 | + // Map institution codes to database names |
| 6 | + institutionDbMap: { |
| 7 | + kctcs: "Kentucky_Community_and_Technical_College_System", |
| 8 | + akron: "University_of_Akron", |
| 9 | + }, |
| 10 | + // Primary table for student-level analytics |
| 11 | + mainTable: "cohort", |
| 12 | + // Map metric names to actual column names |
| 13 | + metricColumnMap: { |
| 14 | + retention_rate: "Retention", |
| 15 | + completion_rate: "Persistence", |
| 16 | + gpa: "GPA_Group_Year_1", |
| 17 | + credits_earned: "Number_of_Credits_Earned_Year_1", |
| 18 | + count: "COUNT(*)", |
| 19 | + }, |
| 20 | + // Map groupBy fields to actual column names |
| 21 | + groupByColumnMap: { |
| 22 | + cohort: "Cohort", |
| 23 | + term: "Cohort_Term", |
| 24 | + program: "Program_of_Study_Year_1", |
| 25 | + course_code: "Course_Prefix", |
| 26 | + enrollment_status: "Enrollment_Type", |
| 27 | + }, |
| 28 | +} |
| 29 | + |
| 30 | +export function analyzePrompt(prompt: string, institutionCode: string): QueryPlan { |
| 31 | + const lowerPrompt = prompt.toLowerCase() |
| 32 | + |
| 33 | + let metric: string | undefined |
| 34 | + if (lowerPrompt.includes("retention")) { |
| 35 | + metric = "retention_rate" |
| 36 | + } else if (lowerPrompt.includes("completion") || lowerPrompt.includes("persistence")) { |
| 37 | + metric = "completion_rate" |
| 38 | + } else if (lowerPrompt.includes("gpa")) { |
| 39 | + metric = "gpa" |
| 40 | + } else if (lowerPrompt.includes("credit")) { |
| 41 | + metric = "credits_earned" |
| 42 | + } else if (lowerPrompt.includes("enrollment") || lowerPrompt.includes("count")) { |
| 43 | + metric = "count" |
| 44 | + } |
| 45 | + |
| 46 | + let groupBy: string | undefined |
| 47 | + if (lowerPrompt.includes("by cohort") || lowerPrompt.includes("cohort")) { |
| 48 | + groupBy = "cohort" |
| 49 | + } else if (lowerPrompt.includes("by term") || lowerPrompt.includes("term")) { |
| 50 | + groupBy = "term" |
| 51 | + } else if (lowerPrompt.includes("by program") || lowerPrompt.includes("program")) { |
| 52 | + groupBy = "program" |
| 53 | + } else if (lowerPrompt.includes("by course") || lowerPrompt.includes("course")) { |
| 54 | + groupBy = "course_code" |
| 55 | + } else if (lowerPrompt.includes("by status") || lowerPrompt.includes("status")) { |
| 56 | + groupBy = "enrollment_status" |
| 57 | + } |
| 58 | + |
| 59 | + // Determine filters |
| 60 | + const filters: Record<string, any> = {} |
| 61 | + |
| 62 | + if (lowerPrompt.includes("last two terms") || lowerPrompt.includes("last 2 terms")) { |
| 63 | + if (groupBy === "cohort") { |
| 64 | + filters.cohort = ["2024-Fall", "2025-Spring"] |
| 65 | + } else { |
| 66 | + filters.term = ["Fall 2024", "Spring 2025"] |
| 67 | + } |
| 68 | + } else if (lowerPrompt.includes("2024")) { |
| 69 | + filters.term = ["Spring 2024", "Fall 2024"] |
| 70 | + } else if (lowerPrompt.includes("2025")) { |
| 71 | + filters.term = ["Spring 2025", "Fall 2025"] |
| 72 | + } |
| 73 | + |
| 74 | + // Status filters |
| 75 | + if (lowerPrompt.includes("enrolled")) { |
| 76 | + filters.enrollment_status = "enrolled" |
| 77 | + } else if (lowerPrompt.includes("completed")) { |
| 78 | + filters.enrollment_status = "completed" |
| 79 | + } |
| 80 | + |
| 81 | + // Time hint |
| 82 | + let timeHint: string | undefined |
| 83 | + if (lowerPrompt.includes("last two terms") || lowerPrompt.includes("last 2 terms")) { |
| 84 | + timeHint = "Last two terms" |
| 85 | + } else if (lowerPrompt.includes("current")) { |
| 86 | + timeHint = "Current term" |
| 87 | + } |
| 88 | + |
| 89 | + let vizType: QueryPlan["vizType"] = "bar" |
| 90 | + |
| 91 | + if (groupBy === "term" || groupBy === "cohort") { |
| 92 | + vizType = "line" |
| 93 | + } else if (lowerPrompt.includes("share") || lowerPrompt.includes("percentage") || lowerPrompt.includes("breakdown")) { |
| 94 | + vizType = "pie" |
| 95 | + } else if (!groupBy) { |
| 96 | + vizType = "kpi" |
| 97 | + } else if (groupBy === "course_code") { |
| 98 | + vizType = "table" |
| 99 | + } |
| 100 | + |
| 101 | + // Map to actual database columns |
| 102 | + const actualMetricColumn = metric ? SCHEMA_CONFIG.metricColumnMap[metric as keyof typeof SCHEMA_CONFIG.metricColumnMap] : undefined |
| 103 | + const actualGroupByColumn = groupBy ? SCHEMA_CONFIG.groupByColumnMap[groupBy as keyof typeof SCHEMA_CONFIG.groupByColumnMap] : undefined |
| 104 | + |
| 105 | + // Generate SQL with actual schema |
| 106 | + const selectClause = actualGroupByColumn |
| 107 | + ? actualMetricColumn && actualMetricColumn !== "COUNT(*)" |
| 108 | + ? `${actualGroupByColumn}, AVG(${actualMetricColumn}) as ${metric}` |
| 109 | + : `${actualGroupByColumn}, COUNT(*) as count` |
| 110 | + : actualMetricColumn |
| 111 | + ? actualMetricColumn === "COUNT(*)" |
| 112 | + ? "COUNT(*) as count" |
| 113 | + : `AVG(${actualMetricColumn}) as ${metric}` |
| 114 | + : "COUNT(*) as count" |
| 115 | + |
| 116 | + // Map filter keys to actual column names |
| 117 | + const actualFilters: Record<string, any> = {} |
| 118 | + Object.entries(filters).forEach(([key, value]) => { |
| 119 | + const actualKey = SCHEMA_CONFIG.groupByColumnMap[key as keyof typeof SCHEMA_CONFIG.groupByColumnMap] || key |
| 120 | + actualFilters[actualKey] = value |
| 121 | + }) |
| 122 | + |
| 123 | + const whereClause = Object.entries(actualFilters) |
| 124 | + .map(([key, value]) => { |
| 125 | + if (Array.isArray(value)) { |
| 126 | + return `${key} IN (${value.map((v) => `'${v}'`).join(", ")})` |
| 127 | + } |
| 128 | + return `${key} = '${value}'` |
| 129 | + }) |
| 130 | + .join(" AND ") |
| 131 | + |
| 132 | + const groupByClause = actualGroupByColumn ? `GROUP BY ${actualGroupByColumn}` : "" |
| 133 | + const orderByColumn = actualGroupByColumn || (actualMetricColumn && actualMetricColumn !== "COUNT(*)" ? actualMetricColumn : "count") |
| 134 | + |
| 135 | + // Get database name for institution |
| 136 | + const dbName = SCHEMA_CONFIG.institutionDbMap[institutionCode as keyof typeof SCHEMA_CONFIG.institutionDbMap] || institutionCode |
| 137 | + const tableName = SCHEMA_CONFIG.mainTable |
| 138 | + |
| 139 | + const sql = `SELECT ${selectClause} |
| 140 | +FROM \`${dbName}\`.${tableName} |
| 141 | +${whereClause ? `WHERE ${whereClause}` : ""} |
| 142 | +${groupByClause} |
| 143 | +ORDER BY ${orderByColumn}`.trim() |
| 144 | + |
| 145 | + const queryParams = new URLSearchParams() |
| 146 | + |
| 147 | + // Add limit parameter |
| 148 | + queryParams.append("limit", "1000") |
| 149 | + queryParams.append("offset", "0") |
| 150 | + |
| 151 | + // Convert filters to query parameters |
| 152 | + if (filters && Object.keys(filters).length > 0) { |
| 153 | + Object.entries(filters).forEach(([key, value]) => { |
| 154 | + if (Array.isArray(value)) { |
| 155 | + // For array values, add multiple parameters with the same key |
| 156 | + value.forEach((v) => queryParams.append(key, String(v))) |
| 157 | + } else { |
| 158 | + queryParams.append(key, String(value)) |
| 159 | + } |
| 160 | + }) |
| 161 | + } |
| 162 | + |
| 163 | + const queryString = `https://schools.syntex-ai.com/${institutionCode}/analysis-ready?${queryParams.toString()}` |
| 164 | + |
| 165 | + return { |
| 166 | + metric, |
| 167 | + groupBy, |
| 168 | + filters: Object.keys(filters).length > 0 ? filters : undefined, |
| 169 | + timeHint, |
| 170 | + vizType, |
| 171 | + sql, |
| 172 | + queryString, |
| 173 | + } |
| 174 | +} |
0 commit comments