All files / web/src/hooks useSessionTimeEstimate.ts

100% Statements 221/221
93.75% Branches 30/32
100% Functions 6/6
100% Lines 221/221

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 2221x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 45x 45x 45x 45x 45x 45x 45x 21x 21x 21x 45x 18x 18x 18x 18x 18x 18x 1x 1x 1x 1x 1x 1x 1x 1x 45x 45x 45x 45x 45x 45x 45x 3x 3x 3x 22x 22x 3x 3x 45x 42x 42x 42x 45x 45x 45x 45x 45x 45x 10x 35x 45x 45x 45x 45x 45x 45x 45x 1x 1x 1x 1x 1x 42x 42x 42x 6x 6x 1x 1x 1x 1x 1x 39x 39x 39x 39x 39x 39x 39x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
'use client'
 
import { useMemo } from 'react'
import type { SessionPart, SlotResult } from '@/db/schema/session-plans'
 
// ============================================================================
// Constants
// ============================================================================
 
/** Minimum samples needed for reliable statistical estimates */
export const MIN_SAMPLES_FOR_STATS = 5
 
/** Default time per problem in ms when not enough data (10 seconds) */
export const DEFAULT_TIME_PER_PROBLEM_MS = 10_000
 
// ============================================================================
// Types
// ============================================================================
 
export interface TimingStats {
  /** Mean response time in milliseconds */
  mean: number
  /** Standard deviation of response times */
  stdDev: number
  /** Number of samples used */
  count: number
  /** Whether we have enough data for reliable estimates */
  hasEnoughData: boolean
  /** Auto-pause threshold in milliseconds */
  threshold: number
}
 
export interface SessionTimeEstimate {
  /** Timing statistics from results */
  timingStats: TimingStats
  /** Number of problems remaining */
  problemsRemaining: number
  /** Total problems in session */
  totalProblems: number
  /** Number of completed problems */
  completedProblems: number
  /** Estimated time remaining in milliseconds */
  estimatedTimeRemainingMs: number
  /** Formatted estimated time remaining (e.g., "~5 min") */
  estimatedTimeRemainingFormatted: string
}
 
// ============================================================================
// Helper Functions
// ============================================================================
 
/**
 * Calculate mean and standard deviation from an array of numbers
 */
function calculateStats(times: number[]): {
  mean: number
  stdDev: number
  count: number
} {
  const count = times.length
  if (count === 0) return { mean: 0, stdDev: 0, count: 0 }
 
  const mean = times.reduce((sum, t) => sum + t, 0) / count
 
  if (count < 2) return { mean, stdDev: 0, count }
 
  const variance = times.reduce((sum, t) => sum + (t - mean) ** 2, 0) / (count - 1)
  const stdDev = Math.sqrt(variance)
 
  return { mean, stdDev, count }
}
 
/**
 * Calculate timing stats from session results
 *
 * Can optionally filter by part type for more accurate estimates
 * when the student is working on a specific part.
 */
export function calculateTimingStats(
  results: SlotResult[],
  parts?: SessionPart[],
  currentPartType?: SessionPart['type']
): TimingStats {
  let times: number[]
 
  if (currentPartType && parts) {
    // Filter results by part type for current-part-specific estimates
    times = results
      .filter((r) => {
        const partIndex = parts.findIndex((p) => p.partNumber === r.partNumber)
        return partIndex >= 0 && parts[partIndex].type === currentPartType
      })
      .map((r) => r.responseTimeMs)
  } else {
    // Use all results
    times = results.map((r) => r.responseTimeMs)
  }
 
  const stats = calculateStats(times)
  const hasEnoughData = stats.count >= MIN_SAMPLES_FOR_STATS
 
  // Calculate auto-pause threshold: mean + 2*stdDev, clamped between 30s and 5min
  const threshold = hasEnoughData
    ? Math.max(30_000, Math.min(stats.mean + 2 * stats.stdDev, 5 * 60 * 1000))
    : 60_000 // Default 1 minute when not enough data
 
  return {
    ...stats,
    hasEnoughData,
    threshold,
  }
}
 
/**
 * Format estimated time remaining as human-readable string
 */
export function formatEstimatedTimeRemaining(ms: number): string {
  const minutes = Math.round(ms / 60_000)
  if (minutes < 1) return '< 1 min'
  if (minutes === 1) return '~1 min'
  return `~${minutes} min`
}
 
/**
 * Calculate estimated time remaining in milliseconds
 */
export function calculateEstimatedTimeRemainingMs(
  timingStats: TimingStats,
  problemsRemaining: number
): number {
  const timePerProblem = timingStats.hasEnoughData ? timingStats.mean : DEFAULT_TIME_PER_PROBLEM_MS
 
  return timePerProblem * problemsRemaining
}
 
// ============================================================================
// Hook
// ============================================================================
 
export interface UseSessionTimeEstimateOptions {
  /** Session results array */
  results: SlotResult[]
  /** Session parts array */
  parts: SessionPart[]
  /** Optional: current part type to filter stats by (for more accurate current-part estimates) */
  currentPartType?: SessionPart['type']
}
 
/**
 * Hook to calculate session time estimates
 *
 * Provides timing statistics and estimated time remaining based on
 * the student's response times during the session.
 *
 * @example
 * ```tsx
 * const estimate = useSessionTimeEstimate({
 *   results: session.results,
 *   parts: session.parts,
 * })
 *
 * return <span>{estimate.estimatedTimeRemainingFormatted} left</span>
 * ```
 */
export function useSessionTimeEstimate({
  results,
  parts,
  currentPartType,
}: UseSessionTimeEstimateOptions): SessionTimeEstimate {
  return useMemo(() => {
    // Calculate total and completed problems
    const totalProblems = parts.reduce((sum, p) => sum + (p.slots?.length ?? 0), 0)
    const completedProblems = results.length
    const problemsRemaining = totalProblems - completedProblems
 
    // Calculate timing stats
    const timingStats = calculateTimingStats(results, parts, currentPartType)
 
    // Calculate estimated time remaining
    const estimatedTimeRemainingMs = calculateEstimatedTimeRemainingMs(
      timingStats,
      problemsRemaining
    )
 
    return {
      timingStats,
      problemsRemaining,
      totalProblems,
      completedProblems,
      estimatedTimeRemainingMs,
      estimatedTimeRemainingFormatted: formatEstimatedTimeRemaining(estimatedTimeRemainingMs),
    }
  }, [results, parts, currentPartType])
}
 
/**
 * Standalone function version for use outside React components
 *
 * Useful for computing time estimates from raw session data without hooks.
 */
export function getSessionTimeEstimate(
  results: SlotResult[],
  parts: SessionPart[],
  currentPartType?: SessionPart['type']
): SessionTimeEstimate {
  const totalProblems = parts.reduce((sum, p) => sum + (p.slots?.length ?? 0), 0)
  const completedProblems = results.length
  const problemsRemaining = totalProblems - completedProblems
 
  const timingStats = calculateTimingStats(results, parts, currentPartType)
  const estimatedTimeRemainingMs = calculateEstimatedTimeRemainingMs(timingStats, problemsRemaining)
 
  return {
    timingStats,
    problemsRemaining,
    totalProblems,
    completedProblems,
    estimatedTimeRemainingMs,
    estimatedTimeRemainingFormatted: formatEstimatedTimeRemaining(estimatedTimeRemainingMs),
  }
}