All files / web/src/lib/curriculum time-estimation.ts

99.53% Statements 428/430
97.72% Branches 43/44
100% Functions 11/11
99.53% Lines 428/430

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 4312x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 9x 9x 20x 20x 20x 170x 170x 170x 20x 20x 20x 29x 9x 9x 9x 9x 9x 9x 9x 20x 29x     20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 439x 439x 439x 439x 439x 439x 439x 439x 439x 439x 439x 439x 439x 439x 439x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5x 5x 5x 5x 5x 5x 5x 5x 5x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 14x 14x 14x 14x 14x 14x 14x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 7x 7x 7x 7x 7x 7x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 98x 96x 96x 98x 98x 98x 94x 94x 94x 4x 4x 4x 4x 4x 4x 4x 98x 2x 2x 2x 2x 2x 98x 96x 96x 96x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 12x 12x 12x 12x 12x 6x 6x 6x 6x 82x 82x 82x 6x 6x 6x 6x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5x 5x 5x 5x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x  
/**
 * Time Estimation Utilities
 *
 * Provides sophisticated time estimation for practice sessions based on:
 * - Historical response times from completed problems
 * - Problem complexity (number of terms, digits, skill difficulty)
 * - Part type modifiers (abacus vs visualization vs linear)
 *
 * ## Key Concepts
 *
 * ### Seconds per Term (SPT)
 * The average time it takes to process one term in a problem.
 * A problem like "45 + 27 + 13" has 3 terms, so:
 * - Total time = SPT × 3 + base overhead
 *
 * ### Complexity Units
 * A more advanced metric that accounts for skill difficulty:
 * - Simple direct operations: 1 unit
 * - Five complement: 1.5 units
 * - Ten complement: 2 units
 * - Multi-digit carries: additional units per digit
 *
 * ### Part Type Modifiers
 * Different part types have different time characteristics:
 * - Abacus: 1.0x (baseline - student uses physical abacus)
 * - Visualization: 1.3x (mental imagery takes longer)
 * - Linear: 0.8x (no visual processing, just mental math)
 *
 * ## Usage
 *
 * ```typescript
 * import {
 *   calculateSecondsPerTerm,
 *   estimateProblemTime,
 *   estimateSessionProblemCount,
 * } from '@/lib/curriculum/time-estimation'
 *
 * // Get avg seconds per term from historical data
 * const spt = calculateSecondsPerTerm(sessionResults)
 *
 * // Estimate time for a specific problem
 * const timeMs = estimateProblemTime(problem, spt)
 *
 * // Estimate how many problems fit in a duration
 * const count = estimateSessionProblemCount(durationMinutes, avgTermsPerProblem, spt)
 * ```
 */
 
import type { SessionPart, SlotResult } from '@/db/schema/session-plans'
 
// ============================================================================
// Constants and Configuration
// ============================================================================
 
/**
 * Default time estimation values (in seconds)
 */
export const TIME_ESTIMATION_DEFAULTS = {
  /** Default seconds per term when no historical data is available */
  secondsPerTerm: 8,
 
  /** Minimum reasonable seconds per term (prevents unrealistic estimates) */
  minSecondsPerTerm: 3,
 
  /** Maximum reasonable seconds per term (caps outliers) */
  maxSecondsPerTerm: 30,
 
  /** Base overhead per problem (thinking time, reading, etc.) in seconds */
  problemOverheadSeconds: 2,
 
  /** Minimum problems per part to ensure meaningful practice */
  minProblemsPerPart: 2,
} as const
 
/**
 * Part type time multipliers
 *
 * These adjust time estimates based on the cognitive load of each part type:
 * - Abacus: Physical manipulation is relatively fast once learned
 * - Visualization: Mental imagery requires more processing time
 * - Linear: No visual processing, fastest for practiced students
 */
export const PART_TYPE_MULTIPLIERS: Record<SessionPart['type'], number> = {
  abacus: 1.0,
  visualization: 1.3,
  linear: 0.85,
}
 
/**
 * Skill complexity weights
 *
 * Used to weight time estimates based on which skills a problem exercises.
 * Higher weights = more complex = takes longer.
 */
export const SKILL_COMPLEXITY_WEIGHTS: Record<string, number> = {
  // Direct operations (simplest)
  'add.direct': 1.0,
  'sub.direct': 1.0,
 
  // Five complement operations
  'add.five': 1.5,
  'sub.five': 1.5,
 
  // Ten complement operations (most complex single-digit)
  'add.ten': 2.0,
  'sub.ten': 2.0,
 
  // Multi-digit operations add complexity
  carry: 1.5,
  borrow: 1.5,
}
 
// ============================================================================
// Core Calculation Functions
// ============================================================================
 
/**
 * Calculate average seconds per term from historical session results
 *
 * This is the core metric for time estimation. By measuring time per term
 * rather than per problem, we can more accurately estimate time for problems
 * of varying complexity.
 *
 * @param results - Array of slot results with timing and problem data
 * @param options - Configuration options
 * @returns Seconds per term, or null if insufficient data
 */
export function calculateSecondsPerTerm(
  results: SlotResult[],
  options: {
    /** Minimum number of results needed for reliable estimate */
    minResults?: number
    /** Whether to exclude outliers (>3 std dev from mean) */
    excludeOutliers?: boolean
  } = {}
): number | null {
  const { minResults = 5, excludeOutliers = true } = options
 
  // Filter to results with valid timing and problem data
  const validResults = results.filter((r) => r.responseTimeMs > 0 && r.problem?.terms?.length > 0)
 
  if (validResults.length < minResults) {
    return null
  }
 
  // Calculate seconds per term for each result
  let sptsRaw = validResults.map((r) => {
    const termCount = r.problem.terms.length
    const seconds = r.responseTimeMs / 1000
    return seconds / termCount
  })
 
  // Optionally exclude outliers
  if (excludeOutliers && sptsRaw.length >= 10) {
    const mean = sptsRaw.reduce((a, b) => a + b, 0) / sptsRaw.length
    const stdDev = Math.sqrt(
      sptsRaw.reduce((sum, spt) => sum + (spt - mean) ** 2, 0) / sptsRaw.length
    )
    const threshold = 3 * stdDev
    sptsRaw = sptsRaw.filter((spt) => Math.abs(spt - mean) <= threshold)
  }
 
  if (sptsRaw.length === 0) {
    return null
  }
 
  // Return weighted average (more recent results weighted slightly higher)
  // For simplicity, we just use arithmetic mean for now
  const avgSpt = sptsRaw.reduce((a, b) => a + b, 0) / sptsRaw.length
 
  // Clamp to reasonable bounds
  return Math.max(
    TIME_ESTIMATION_DEFAULTS.minSecondsPerTerm,
    Math.min(TIME_ESTIMATION_DEFAULTS.maxSecondsPerTerm, avgSpt)
  )
}
 
/**
 * Calculate average seconds per term from session plan data
 *
 * Convenience function that extracts results from session plans.
 *
 * @param sessions - Array of session plans with results
 * @returns Seconds per term, or null if insufficient data
 */
export function calculateSecondsPerTermFromSessions(
  sessions: Array<{ results: SlotResult[] }>
): number | null {
  const allResults = sessions.flatMap((s) => s.results)
  return calculateSecondsPerTerm(allResults)
}
 
/**
 * Estimate time in milliseconds for a specific problem
 *
 * @param problem - Problem with terms array
 * @param secondsPerTerm - Average seconds per term
 * @param partType - Part type for modifier (optional)
 * @returns Estimated time in milliseconds
 */
export function estimateProblemTimeMs(
  problem: { terms: number[] },
  secondsPerTerm: number,
  partType?: SessionPart['type']
): number {
  const termCount = problem.terms.length
  const baseSeconds = termCount * secondsPerTerm + TIME_ESTIMATION_DEFAULTS.problemOverheadSeconds
 
  const modifier = partType ? PART_TYPE_MULTIPLIERS[partType] : 1.0
 
  return Math.round(baseSeconds * modifier * 1000)
}
 
/**
 * Estimate time in seconds for a specific problem
 *
 * @param problem - Problem with terms array
 * @param secondsPerTerm - Average seconds per term
 * @param partType - Part type for modifier (optional)
 * @returns Estimated time in seconds
 */
export function estimateProblemTimeSeconds(
  problem: { terms: number[] },
  secondsPerTerm: number,
  partType?: SessionPart['type']
): number {
  return estimateProblemTimeMs(problem, secondsPerTerm, partType) / 1000
}
 
// ============================================================================
// Session Planning Functions
// ============================================================================
 
/**
 * Estimate the number of problems that can fit in a given duration
 *
 * @param durationMinutes - Target session duration in minutes
 * @param avgTermsPerProblem - Average number of terms per problem (default: 3)
 * @param secondsPerTerm - Seconds per term (default: from constants)
 * @param partType - Part type for modifier (optional)
 * @returns Estimated problem count
 */
export function estimateSessionProblemCount(
  durationMinutes: number,
  avgTermsPerProblem: number = 3,
  secondsPerTerm: number = TIME_ESTIMATION_DEFAULTS.secondsPerTerm,
  partType?: SessionPart['type']
): number {
  const totalSeconds = durationMinutes * 60
  const modifier = partType ? PART_TYPE_MULTIPLIERS[partType] : 1.0
 
  const secondsPerProblem =
    (avgTermsPerProblem * secondsPerTerm + TIME_ESTIMATION_DEFAULTS.problemOverheadSeconds) *
    modifier
 
  const count = Math.floor(totalSeconds / secondsPerProblem)
  return Math.max(TIME_ESTIMATION_DEFAULTS.minProblemsPerPart, count)
}
 
/**
 * Estimate total session duration given problem count and complexity
 *
 * @param problemCount - Number of problems
 * @param avgTermsPerProblem - Average terms per problem
 * @param secondsPerTerm - Seconds per term
 * @returns Estimated duration in minutes
 */
export function estimateSessionDurationMinutes(
  problemCount: number,
  avgTermsPerProblem: number = 3,
  secondsPerTerm: number = TIME_ESTIMATION_DEFAULTS.secondsPerTerm
): number {
  const secondsPerProblem =
    avgTermsPerProblem * secondsPerTerm + TIME_ESTIMATION_DEFAULTS.problemOverheadSeconds
 
  return Math.round((problemCount * secondsPerProblem) / 60)
}
 
/**
 * Convert seconds per term to approximate seconds per problem
 *
 * Useful for UI display and backwards compatibility with existing code
 * that expects "seconds per problem" as input.
 *
 * @param secondsPerTerm - Seconds per term
 * @param avgTermsPerProblem - Average terms per problem (default: 3)
 * @returns Approximate seconds per problem
 */
export function convertSptToSecondsPerProblem(
  secondsPerTerm: number,
  avgTermsPerProblem = 3
): number {
  return Math.round(
    avgTermsPerProblem * secondsPerTerm + TIME_ESTIMATION_DEFAULTS.problemOverheadSeconds
  )
}
 
/**
 * Convert seconds per problem to approximate seconds per term
 *
 * Useful for converting legacy data or user input to the new model.
 *
 * @param secondsPerProblem - Seconds per problem
 * @param avgTermsPerProblem - Average terms per problem (default: 3)
 * @returns Approximate seconds per term
 */
export function convertSecondsPerProblemToSpt(
  secondsPerProblem: number,
  avgTermsPerProblem = 3
): number {
  const netTime = Math.max(0, secondsPerProblem - TIME_ESTIMATION_DEFAULTS.problemOverheadSeconds)
  return netTime / avgTermsPerProblem
}
 
// ============================================================================
// Advanced: Complexity-Weighted Estimation
// ============================================================================
 
/**
 * Calculate complexity units for a problem based on skills required
 *
 * This provides a more nuanced estimate than just counting terms.
 *
 * @param skillsRequired - Array of skill IDs the problem exercises
 * @returns Total complexity units
 */
export function calculateProblemComplexityUnits(skillsRequired: string[]): number {
  if (skillsRequired.length === 0) return 1 // Minimum complexity
 
  let totalComplexity = 0
  for (const skill of skillsRequired) {
    // Check for exact match first
    if (SKILL_COMPLEXITY_WEIGHTS[skill]) {
      totalComplexity += SKILL_COMPLEXITY_WEIGHTS[skill]
      continue
    }
 
    // Check for partial match (e.g., "add.+5.direct" matches "add.direct")
    const parts = skill.split('.')
    const operation = parts[0] // add, sub
    const technique = parts[parts.length - 1] // direct, five, ten
 
    const partialKey = `${operation}.${technique}`
    if (SKILL_COMPLEXITY_WEIGHTS[partialKey]) {
      totalComplexity += SKILL_COMPLEXITY_WEIGHTS[partialKey]
    } else {
      // Default complexity for unknown skills
      totalComplexity += 1.0
    }
  }
 
  return totalComplexity
}
 
/**
 * Calculate seconds per complexity unit from historical results
 *
 * This is the most sophisticated estimation method, accounting for
 * both problem structure and skill difficulty.
 *
 * @param results - Array of slot results
 * @returns Seconds per complexity unit, or null if insufficient data
 */
export function calculateSecondsPerComplexityUnit(results: SlotResult[]): number | null {
  const validResults = results.filter(
    (r) => r.responseTimeMs > 0 && r.problem?.skillsRequired?.length > 0
  )
 
  if (validResults.length < 10) {
    return null
  }
 
  const ratios = validResults.map((r) => {
    const complexity = calculateProblemComplexityUnits(r.problem.skillsRequired)
    const seconds = r.responseTimeMs / 1000
    return seconds / complexity
  })
 
  const avg = ratios.reduce((a, b) => a + b, 0) / ratios.length
  return Math.max(2, Math.min(15, avg)) // Clamp to reasonable bounds
}
 
// ============================================================================
// Utility Types
// ============================================================================
 
/**
 * Time estimation summary for a student
 */
export interface TimeEstimationProfile {
  /** Seconds per term (primary metric) */
  secondsPerTerm: number
  /** Seconds per complexity unit (advanced metric, if available) */
  secondsPerComplexityUnit: number | null
  /** Equivalent seconds per problem for UI display */
  secondsPerProblem: number
  /** Number of results used to calculate these estimates */
  sampleSize: number
  /** Whether these are default values (no historical data) */
  isDefault: boolean
}
 
/**
 * Get a complete time estimation profile for a student
 *
 * @param results - Historical slot results
 * @returns Time estimation profile
 */
export function getTimeEstimationProfile(results: SlotResult[]): TimeEstimationProfile {
  const spt = calculateSecondsPerTerm(results)
  const spcu = calculateSecondsPerComplexityUnit(results)
 
  if (spt === null) {
    return {
      secondsPerTerm: TIME_ESTIMATION_DEFAULTS.secondsPerTerm,
      secondsPerComplexityUnit: null,
      secondsPerProblem: convertSptToSecondsPerProblem(TIME_ESTIMATION_DEFAULTS.secondsPerTerm),
      sampleSize: 0,
      isDefault: true,
    }
  }
 
  return {
    secondsPerTerm: spt,
    secondsPerComplexityUnit: spcu,
    secondsPerProblem: convertSptToSecondsPerProblem(spt),
    sampleSize: results.length,
    isDefault: false,
  }
}