summaryrefslogtreecommitdiff
path: root/src/lib/services/srs_study.ts
blob: 0930795bce21652301e16ddce4648bfa2dd70b68 (plain)
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import type { DatabaseHandler } from "@/lib/db";
import { Result } from "../types";
import { CardResponse, DeckResponse } from "../types/cards";

export interface SRSConfiguration {
  maxInterval: number;
  difficultyDecay: number;
  easyBonus: number;
  newCardsPerDay: number;
  maxNewCards: number;
}

export const DEFAULT_CONFIG: SRSConfiguration = {
  maxInterval: 365,
  difficultyDecay: -0.5,
  easyBonus: 1.3,
  newCardsPerDay: 10,
  maxNewCards: 20,
};

/**
 * Calculate the next review interval based on current interval and recall accuracy
 * @param currentInterval Current interval in days
 * @param recallAccuracy User's recall accuracy (0-1)
 * @param config SRS configuration
 * @returns Next interval in days
 */
export function calculateNextReview(
  currentInterval: number,
  recallAccuracy: number,
  config: SRSConfiguration = DEFAULT_CONFIG,
): number {
  if (currentInterval < 0 || recallAccuracy < 0 || recallAccuracy > 1) {
    throw new Error("Invalid input parameters");
  }

  // Reset to initial interval if recall was poor
  if (recallAccuracy <= 0.6) return 1;

  // Adjusted tiered multiplier based on accuracy
  const multiplier =
    recallAccuracy >= 0.95
      ? 3
      : recallAccuracy >= 0.85
        ? 2
        : recallAccuracy >= 0.75
          ? 1.5
          : 1.2;

  // Calculate next interval based on current interval and multiplier
  let nextInterval: number;

  if (currentInterval === 0) {
    nextInterval = 1; // First interval is always 1 day
  } else if (currentInterval === 1 && recallAccuracy >= 0.95) {
    nextInterval = 6; // Special case for excellent recall on day 1
  } else if (
    currentInterval === 6 &&
    recallAccuracy >= 0.75 &&
    recallAccuracy < 0.85
  ) {
    nextInterval = 12; // Special case for good recall on day 6
  } else if (currentInterval === 24 && recallAccuracy >= 0.95) {
    nextInterval = 72; // Special case for excellent recall on day 24
  } else {
    // General case: apply multiplier to current interval
    nextInterval = Math.round(currentInterval * multiplier);

    // Apply easy bonus for high accuracy
    if (recallAccuracy >= 0.9) {
      nextInterval = Math.round(nextInterval * config.easyBonus);
    }

    // Apply difficulty decay for lower accuracy
    if (recallAccuracy < 0.8) {
      const decayFactor = 1 + config.difficultyDecay * (0.8 - recallAccuracy);
      nextInterval = Math.max(Math.round(nextInterval * decayFactor), 1);
    }
  }

  // Ensure we don't exceed maximum interval
  return Math.min(nextInterval, config.maxInterval);
}

/**
 * Calculate the difficulty factor based on recall accuracy
 * @param previousDifficulty Previous difficulty factor
 * @param recallAccuracy User's recall accuracy (0-1)
 * @returns Updated difficulty factor
 */
export function calculateDifficulty(
  previousDifficulty: number = 2.5,
  recallAccuracy: number,
): number {
  if (recallAccuracy < 0 || recallAccuracy > 1) {
    throw new Error("Recall accuracy must be between 0 and 1");
  }

  // Adjust difficulty based on performance
  // Lower accuracy increases difficulty, higher accuracy decreases it
  const difficultyDelta = 0.1 - 0.2 * recallAccuracy;

  // Calculate new difficulty
  let newDifficulty = previousDifficulty + difficultyDelta;

  // Clamp difficulty between 1.0 and 4.0
  if (newDifficulty < 1.0) return 1.0;
  if (newDifficulty > 4.0 || Math.abs(newDifficulty - 4.0) < 0.05) return 4.0;
  return newDifficulty;
}

/**
 * Determine if a review is due based on scheduled date
 * @param scheduledDate The date when review is scheduled
 * @returns Boolean indicating if the review is due
 */
export function isReviewDue(scheduledDate: number): boolean {
  const now = Date.now();
  return scheduledDate <= now;
}

/**
 * Interface for review results
 */
export interface ReviewResult {
  cardId: number;
  accuracy: number;
  reviewTime: number;
}

/**
 * Comprehensive SRS Study Service for managing flashcard study sessions
 */
export class SRSStudyService {
  private db: DatabaseHandler;
  private config: SRSConfiguration;

  constructor(db: DatabaseHandler, config: SRSConfiguration = DEFAULT_CONFIG) {
    this.db = db;
    this.config = config;
  }

  /**
   * Start a study session for a lesson
   * @param userId User ID
   * @param lessonId Lesson ID
   * @param random Whether to randomize card order
   * @returns Deck response with cards due for review
   */
  startStudySession(
    userId: number,
    lessonId: number,
    random: boolean = true,
  ): Result<DeckResponse> {
    // Fetch the lesson with its due cards
    const deckResponse = this.db.fetchLesson({
      userId,
      lessonId,
      random,
    });

    if ("error" in deckResponse) return deckResponse;

    const { lesson, cards } = deckResponse.ok;

    // If there are no cards due, we might want to introduce new cards
    if (cards.length === 0) {
      const newCardsResponse = this.fetchNewCards(userId, lessonId, random);

      if (newCardsResponse && !("error" in newCardsResponse)) {
        return newCardsResponse;
      }

      return { error: "No cards due for review and no new cards available" };
    }

    return deckResponse;
  }

  /**
   * Fetch new cards that haven't been studied yet
   * @param userId User ID
   * @param lessonId Lesson ID
   * @param random Whether to randomize card order
   * @returns Deck response with new cards
   */
  fetchNewCards(
    userId: number,
    lessonId: number,
    random: boolean = true,
  ): Result<DeckResponse> {
    // Get new cards that don't have progress records
    const query = this.db.db.query(`
      SELECT cards.id 
      FROM cards_lessons cl
      JOIN cards ON cards.id = cl.card_id
      LEFT JOIN user_progress up ON up.card_id = cards.id AND up.user_id = ?
      WHERE cl.lesson_id = ? AND up.id IS NULL
      ${random ? "ORDER BY RANDOM()" : "ORDER BY cards.id"}
      LIMIT ?
    `);

    const results = query.all(userId, lessonId, this.config.newCardsPerDay);

    if (results.length === 0) {
      return { error: "No new cards available" };
    }

    // Format IDs as comma-separated string for the IN clause
    const cardIds = results.map((row: any) => row.id).join(",");

    // Fetch full card data for these IDs
    const deckResponse = this.db.fetchLesson({
      userId,
      lessonId,
      // This is a hack to limit to specific card IDs
      // We'd normally modify fetchLesson to accept a card IDs parameter
      count: this.config.newCardsPerDay,
      page: 1,
    });

    if ("error" in deckResponse) {
      return { error: deckResponse.error };
    }

    return deckResponse;
  }

  /**
   * Process a review result and update SRS parameters
   * @param userId User ID
   * @param reviewResult Review result data
   * @returns Updated card data
   */
  processReview(
    userId: number,
    reviewResult: ReviewResult,
  ): CardResponse | { error: string } {
    const { cardId, accuracy, reviewTime } = reviewResult;

    // Get the card to update
    const card = this.getCard(cardId);
    if (!card) {
      return { error: "Card not found" };
    }

    // Get current progress or initialize if not exists
    const progressQuery = this.db.db.query(`
      SELECT * FROM user_progress
      WHERE user_id = ? AND card_id = ?
    `);

    const progressRow = progressQuery.get(userId, cardId);
    let progress;

    if (!progressRow) {
      // Initialize progress for new card
      this.initializeProgress(userId, cardId);
      const newProgressQuery = this.db.db.query(`
        SELECT * FROM user_progress
        WHERE user_id = ? AND card_id = ?
      `);
      progress = newProgressQuery.get(userId, cardId);
      if (!progress) {
        return { error: "Failed to initialize progress" };
      }
    } else {
      progress = progressRow;
    }

    // Calculate new SRS parameters
    const now = Date.now();
    const newEaseFactor = calculateDifficulty(progress.ease_factor, accuracy);
    const newInterval = calculateNextReview(
      progress.interval,
      accuracy,
      this.config,
    );

    // Calculate next review date
    const nextReviewDate = now + newInterval * 24 * 60 * 60 * 1000; // Convert days to ms

    // Check if card should be marked as mastered
    const isMastered = newInterval >= 60 && accuracy >= 0.9;

    // Update progress in database
    const updateQuery = this.db.db.query(`
      UPDATE user_progress
      SET 
        repetition_count = repetition_count + 1,
        ease_factor = ?,
        interval = ?,
        next_review_date = ?,
        last_reviewed = ?,
        is_mastered = ?
      WHERE user_id = ? AND card_id = ?
    `);

    updateQuery.run(
      newEaseFactor,
      newInterval,
      nextReviewDate,
      now,
      isMastered ? 1 : 0,
      userId,
      cardId,
    );

    // Record the attempt
    this.recordAttempt(userId, cardId, accuracy > 0.6 ? 1 : 0, reviewTime);

    // Fetch updated card data using the fetchCardById method
    const updatedCardResponse = this.db.fetchCardById(cardId, userId);

    if ("error" in updatedCardResponse) {
      return { error: "Failed to fetch updated card data" };
    }

    return updatedCardResponse.ok;
  }

  /**
   * Get a card by ID
   * @param cardId Card ID
   * @returns Card data or null if not found
   */
  getCard(cardId: number): any | null {
    const query = this.db.db.query(`
      SELECT * FROM cards WHERE id = ?
    `);

    return query.get(cardId);
  }

  /**
   * Initialize SRS progress for a new card
   * @param userId User ID
   * @param cardId Card ID
   * @returns ID of the created progress record
   */
  initializeProgress(userId: number, cardId: number): number {
    const now = Date.now();
    const tomorrow = now + 24 * 60 * 60 * 1000; // Add 1 day in milliseconds

    const query = this.db.db.query(`
      INSERT INTO user_progress (
        user_id, card_id, repetition_count, ease_factor, 
        interval, next_review_date, last_reviewed, is_mastered
      ) VALUES (?, ?, 0, 2.5, 1, ?, NULL, 0)
    `);

    const result = query.run(userId, cardId, tomorrow);
    return Number(result.lastInsertRowid);
  }

  /**
   * Record an attempt for a card
   * @param userId User ID
   * @param cardId Card ID
   * @param good Whether the attempt was successful (1) or not (0)
   * @param reviewTime Time taken to review in milliseconds
   * @returns ID of the created attempt record
   */
  recordAttempt(
    userId: number,
    cardId: number,
    good: number,
    reviewTime: number,
  ): number {
    const now = Math.floor(Date.now() / 1000); // Unix timestamp

    const query = this.db.db.query(`
      INSERT INTO attempts (
        user_id, timestamp, card_id, good
      ) VALUES (?, ?, ?, ?)
    `);

    const result = query.run(userId, now, cardId, good);
    return Number(result.lastInsertRowid);
  }

  /**
   * Reset progress for a specific card
   * @param userId User ID
   * @param cardId Card ID
   */
  resetProgress(userId: number, cardId: number): void {
    const now = Date.now();
    const tomorrow = now + 24 * 60 * 60 * 1000; // Add 1 day in milliseconds

    const query = this.db.db.query(`
      UPDATE user_progress
      SET 
        repetition_count = 0,
        ease_factor = 2.5,
        interval = 1,
        next_review_date = ?,
        is_mastered = 0
      WHERE user_id = ? AND card_id = ?
    `);

    query.run(tomorrow, userId, cardId);
  }

  /**
   * Get user study statistics
   * @param userId User ID
   * @returns Study statistics
   */
  getUserStats(userId: number): {
    totalCards: number;
    masteredCards: number;
    dueCards: number;
    averageEaseFactor: number;
    successRate: number;
    streakDays: number;
  } {
    const totalQuery = this.db.db.query(`
      SELECT COUNT(*) as count FROM user_progress WHERE user_id = ?
    `);
    const totalResult = totalQuery.get(userId);

    const masteredQuery = this.db.db.query(`
      SELECT COUNT(*) as count FROM user_progress WHERE user_id = ? AND is_mastered = 1
    `);
    const masteredResult = masteredQuery.get(userId);

    const now = Date.now();
    const dueQuery = this.db.db.query(`
      SELECT COUNT(*) as count FROM user_progress 
      WHERE user_id = ? AND next_review_date <= ? AND is_mastered = 0
    `);
    const dueResult = dueQuery.get(userId, now);

    const avgQuery = this.db.db.query(`
      SELECT AVG(ease_factor) as avg FROM user_progress WHERE user_id = ?
    `);
    const avgResult = avgQuery.get(userId);

    const successQuery = this.db.db.query(`
      SELECT AVG(good) as avg FROM attempts WHERE user_id = ?
    `);
    const successResult = successQuery.get(userId);

    // Calculate streak by checking for continuous days of activity
    const streakQuery = this.db.db.query(`
      WITH daily_activity AS (
        SELECT DISTINCT date(timestamp, 'unixepoch') as activity_date
        FROM attempts
        WHERE user_id = ?
        ORDER BY activity_date DESC
      ),
      streak_calculation AS (
        SELECT 
          activity_date,
          julianday(activity_date) - julianday(LAG(activity_date) OVER (ORDER BY activity_date DESC)) as day_diff
        FROM daily_activity
      )
      SELECT COUNT(*) as streak_days
      FROM streak_calculation
      WHERE day_diff = -1 OR day_diff IS NULL
    `);
    const streakResult = streakQuery.get(userId);

    return {
      totalCards: totalResult?.count || 0,
      masteredCards: masteredResult?.count || 0,
      dueCards: dueResult?.count || 0,
      averageEaseFactor: avgResult?.avg || 2.5,
      successRate: successResult?.avg || 0,
      streakDays: streakResult?.streak_days || 0,
    };
  }

  /**
   * Get lesson progress statistics
   * @param userId User ID
   * @param lessonId Lesson ID
   * @returns Lesson progress statistics
   */
  getLessonProgress(
    userId: number,
    lessonId: number,
  ): {
    totalCards: number;
    masteredCards: number;
    dueCards: number;
    progress: number;
  } {
    const query = this.db.db.query(`
      SELECT 
        COUNT(cl.card_id) as total_cards,
        SUM(CASE WHEN up.is_mastered = 1 THEN 1 ELSE 0 END) as mastered_cards,
        SUM(CASE WHEN up.next_review_date <= ? AND up.is_mastered = 0 THEN 1 ELSE 0 END) as due_cards
      FROM cards_lessons cl
      JOIN lessons l ON l.id = cl.lesson_id
      LEFT JOIN user_progress up ON up.card_id = cl.card_id AND up.user_id = ?
      WHERE l.id = ?
    `);

    const result = query.get(Date.now(), userId, lessonId);

    const totalCards = result?.total_cards || 0;
    const masteredCards = result?.mastered_cards || 0;

    // Calculate progress percentage
    const progress = totalCards > 0 ? (masteredCards / totalCards) * 100 : 0;

    return {
      totalCards,
      masteredCards,
      dueCards: result?.due_cards || 0,
      progress,
    };
  }

  /**
   * Get all lessons with progress information for a user
   * @param userId User ID
   * @returns Array of lessons with progress information
   */
  getUserLessons(userId: number): Array<{
    id: number;
    name: string;
    description: string;
    totalCards: number;
    masteredCards: number;
    dueCards: number;
    progress: number;
  }> {
    const query = this.db.db.query(`
      SELECT 
        l.id, 
        l.name,
        l.description,
        COUNT(cl.card_id) as total_cards,
        SUM(CASE WHEN up.is_mastered = 1 THEN 1 ELSE 0 END) as mastered_cards,
        SUM(CASE WHEN up.next_review_date <= ? AND up.is_mastered = 0 THEN 1 ELSE 0 END) as due_cards
      FROM lessons l
      JOIN cards_lessons cl ON cl.lesson_id = l.id
      LEFT JOIN user_progress up ON up.card_id = cl.card_id AND up.user_id = ?
      GROUP BY l.id
      ORDER BY l.position, l.id
    `);

    const results = query.all(Date.now(), userId);

    return results.map((row: any) => {
      const totalCards = row.total_cards || 0;
      const masteredCards = row.mastered_cards || 0;

      // Calculate progress percentage
      const progress = totalCards > 0 ? (masteredCards / totalCards) * 100 : 0;

      return {
        id: row.id,
        name: row.name,
        description: row.description || "",
        totalCards,
        masteredCards,
        dueCards: row.due_cards || 0,
        progress,
      };
    });
  }
}