()
| 21 | const STREAK_BREAK_PROBABILITY = 0.15; // 15% chance of breaking a streak |
| 22 | |
| 23 | async function generateTestData() { |
| 24 | try { |
| 25 | console.log('Connecting to database...'); |
| 26 | await connectToDatabase(); |
| 27 | const db = getDb(); |
| 28 | |
| 29 | // Parse command line arguments |
| 30 | const args = process.argv.slice(2); |
| 31 | if (args.length < 2) { |
| 32 | console.error('Usage: node generateStudyTestData.js <userId> <deckId> [daysOfHistory]'); |
| 33 | process.exit(1); |
| 34 | } |
| 35 | |
| 36 | const userId = parseInt(args[0]); |
| 37 | const deckId = parseInt(args[1]); |
| 38 | const daysOfHistory = args[2] ? parseInt(args[2]) : 30; |
| 39 | |
| 40 | // Verify user and deck exist |
| 41 | const user = await db.collection('users').findOne({ userId }); |
| 42 | if (!user) { |
| 43 | console.error(`User with ID ${userId} not found.`); |
| 44 | process.exit(1); |
| 45 | } |
| 46 | |
| 47 | const deck = await db.collection('flashcard_decks').findOne({ deckId }); |
| 48 | if (!deck) { |
| 49 | console.error(`Deck with ID ${deckId} not found.`); |
| 50 | process.exit(1); |
| 51 | } |
| 52 | |
| 53 | console.log(`Generating ${daysOfHistory} days of study data for user ${userId} (${user.name}) and deck ${deckId} (${deck.name}).`); |
| 54 | |
| 55 | // Clear existing data for this user and deck |
| 56 | await db.collection('study_progress').deleteMany({ userId, deckId }); |
| 57 | await db.collection('study_streaks').deleteMany({ userId, deckId }); |
| 58 | |
| 59 | const today = new Date(); |
| 60 | today.setHours(0, 0, 0, 0); |
| 61 | |
| 62 | // Initialize variables for tracking streak data |
| 63 | let currentStreak = 0; |
| 64 | let maxStreak = 0; |
| 65 | let maxStreakStartDate = null; |
| 66 | let maxStreakEndDate = null; |
| 67 | let currentStreakStartDate = null; |
| 68 | let lastStudyDate = null; |
| 69 | const studyDates = []; |
| 70 | |
| 71 | // Generate daily progress entries |
| 72 | for (let i = 0; i < daysOfHistory; i++) { |
| 73 | const date = new Date(today); |
| 74 | date.setDate(date.getDate() - (daysOfHistory - i - 1)); |
| 75 | |
| 76 | // Decide if user studied on this day (with higher probability for recent days) |
| 77 | const recencyBoost = i / daysOfHistory; // 0 to 1 factor increasing for more recent days |
| 78 | const didStudy = Math.random() < (STUDY_PROBABILITY + (recencyBoost * 0.25)); |
| 79 | |
| 80 | // Add a chance to break a streak for more realistic data |
no test coverage detected