{
    "timestamp": "2026-09-14 19:18:24",
    "level": "INFO",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Starting sync for tenant: lms_tenant_icc",
    "memory_usage": "36.5 MB",
    "peak_memory": "36.5 MB"
}
{
    "timestamp": "2026-09-14 19:18:24",
    "level": "INFO",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Batch size: 1000",
    "memory_usage": "36.5 MB",
    "peak_memory": "36.5 MB"
}
{
    "timestamp": "2026-09-14 19:18:24",
    "level": "INFO",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Tenant connection established: lms_tenant_icc",
    "memory_usage": "36.5 MB",
    "peak_memory": "36.5 MB"
}
{
    "timestamp": "2026-09-14 19:18:24",
    "level": "INFO",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Using Moodle prefix: omo_",
    "memory_usage": "36.5 MB",
    "peak_memory": "36.5 MB"
}
{
    "timestamp": "2026-09-14 19:18:24",
    "level": "INFO",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Fetching data from Moodle...",
    "memory_usage": "36.5 MB",
    "peak_memory": "36.5 MB"
}
{
    "timestamp": "2026-09-14 19:18:24",
    "level": "ERROR",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Sync failed: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'english_classroom.omo_question' doesn't exist (Connection: moodle, SQL: \n                SELECT\n                    u.id          AS user_id,\n                    u.username,\n                    u.email,\n\n                    q.id          AS quiz_id,\n                    q.name        AS quiz_name,\n                    cs.name       AS section_name,\n\n                    cm.id         AS cmid,\n                    cm.course     AS course_id,\n\n                    gg.finalgrade AS final_grade,\n\n                    best_qa.max_sumgrades AS raw_score_db,\n                    q.sumgrades           AS max_score_db,\n\n                    COALESCE(valid_q.valid_maxmark, q.sumgrades) AS valid_maxmark,\n\n                    -- Điểm UI quy đổi (Fallback về gg.finalgrade nếu bài thi không có attempt)\n                    ROUND(\n                        LEAST(\n                            COALESCE(\n                                (best_qa.max_sumgrades \/ NULLIF(COALESCE(valid_q.valid_maxmark, q.sumgrades), 0)) * 10,\n                                gg.finalgrade\n                            ),\n                            10.00\n                        ),\n                    2) AS score\n\n                FROM omo_grade_items gi\n\n                JOIN omo_grade_grades gg ON gg.itemid = gi.id AND gg.finalgrade IS NOT NULL\n                JOIN omo_quiz q ON q.id = gi.iteminstance\n                JOIN omo_user u ON u.id = gg.userid\n\n                JOIN omo_course_modules cm ON cm.instance = q.id AND cm.course = q.course\n                JOIN omo_modules m ON m.id = cm.module AND m.name = 'quiz'\n                JOIN omo_course_sections cs ON cs.id = cm.section\n\n                -- 1. SUBQUERY LẤY MAX SCORE\n                LEFT JOIN (\n                    SELECT\n                        qa.quiz,\n                        qa.userid,\n                        MAX(qa.sumgrades) AS max_sumgrades\n                    FROM omo_quiz_attempts qa\n                    WHERE qa.state IN ('finished', 'overdue')\n                      AND qa.quiz IN (SELECT id FROM omo_quiz WHERE course IN (158,159,160,161,167,168,169,170))\n                    GROUP BY qa.quiz, qa.userid\n                ) best_qa\n                    ON best_qa.quiz = q.id\n                   AND best_qa.userid = u.id\n\n                -- 2. SUBQUERY TÍNH VALID MAXMARK\n                LEFT JOIN (\n                    SELECT\n                        qs.quizid,\n                        SUM(qs.maxmark) AS valid_maxmark\n                    FROM omo_quiz_slots qs\n                    JOIN omo_question qst ON qst.id = qs.questionid\n                    WHERE qs.quizid IN (SELECT id FROM omo_quiz WHERE course IN (158,159,160,161,167,168,169,170))\n                      AND (qst.name IS NOT NULL AND qst.name != '')\n                      AND qst.qtype != 'random'\n                      AND qst.name NOT LIKE '%default%'\n                    GROUP BY qs.quizid\n                ) valid_q\n                    ON valid_q.quizid = q.id\n\n                WHERE gi.itemmodule = 'quiz'\n                  AND gi.courseid IN (158,159,160,161,167,168,169,170)\n                  AND cm.course IN (158,159,160,161,167,168,169,170)\n                  AND cm.deletioninprogress = 0\n            )",
    "memory_usage": "38.5 MB",
    "peak_memory": "38.5 MB"
}
{
    "timestamp": "2026-09-14 19:18:24",
    "level": "ERROR",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Stack trace: #0 \/var\/www\/html\/lms_hocmai\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connection.php(779): Illuminate\\Database\\Connection->runQueryCallback()\n#1 \/var\/www\/html\/lms_hocmai\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connection.php(398): Illuminate\\Database\\Connection->run()\n#2 \/var\/www\/html\/lms_hocmai\/app\/Console\/Commands\/SyncEnglishClassroomUserQuizGradeCommand.php(333): Illuminate\\Database\\Connection->select()\n#3 \/var\/www\/html\/lms_hocmai\/vendor\/laravel\/framework\/src\/Illuminate\/Container\/BoundMethod.php(36): App\\Console\\Commands\\SyncEnglishClassroomUserQuizGradeCommand->handle()\n#4 \/var\/www\/html\/lms_hocmai\/vendor\/laravel\/framework\/src\/Illuminate\/Container\/Util.php(43): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()\n#5 \/var\/www\/html\/lms_hocmai\/vendor\/laravel\/framework\/src\/Illuminate\/Container\/BoundMethod.php(95): Illuminate\\Container\\Util::unwrapIfClosure()\n#6 \/var\/www\/html\/lms_hocmai\/vendor\/laravel\/framework\/src\/Illuminate\/Container\/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod()\n#7 \/var\/www\/html\/lms_hocmai\/vendor\/laravel\/framework\/src\/Illuminate\/Container\/Container.php(696): Illuminate\\Container\\BoundMethod::call()\n#8 \/var\/www\/html\/lms_hocmai\/vendor\/laravel\/framework\/src\/Illuminate\/Console\/Command.php(213): Illuminate\\Container\\Container->call()\n#9 \/var\/www\/html\/lms_hocmai\/vendor\/symfony\/console\/Command\/Command.php(341): Illuminate\\Console\\Command->execute()\n#10 \/var\/www\/html\/lms_hocmai\/vendor\/laravel\/framework\/src\/Illuminate\/Console\/Command.php(182): Symfony\\Component\\Console\\Command\\Command->run()\n#11 \/var\/www\/html\/lms_hocmai\/vendor\/symfony\/console\/Application.php(1117): Illuminate\\Console\\Command->run()\n#12 \/var\/www\/html\/lms_hocmai\/vendor\/symfony\/console\/Application.php(356): Symfony\\Component\\Console\\Application->doRunCommand()\n#13 \/var\/www\/html\/lms_hocmai\/vendor\/symfony\/console\/Application.php(195): Symfony\\Component\\Console\\Application->doRun()\n#14 \/var\/www\/html\/lms_hocmai\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Console\/Kernel.php(198): Symfony\\Component\\Console\\Application->run()\n#15 \/var\/www\/html\/lms_hocmai\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Application.php(1235): Illuminate\\Foundation\\Console\\Kernel->handle()\n#16 \/var\/www\/html\/lms_hocmai\/artisan(13): Illuminate\\Foundation\\Application->handleCommand()\n#17 {main}",
    "memory_usage": "38.5 MB",
    "peak_memory": "38.5 MB"
}
{
    "timestamp": "2026-09-14 20:38:23",
    "level": "INFO",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Starting sync for tenant: lms_tenant_icc",
    "memory_usage": "36.5 MB",
    "peak_memory": "36.5 MB"
}
{
    "timestamp": "2026-09-14 20:38:23",
    "level": "INFO",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Batch size: 1000",
    "memory_usage": "36.5 MB",
    "peak_memory": "36.5 MB"
}
{
    "timestamp": "2026-09-14 20:38:23",
    "level": "INFO",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Tenant connection established: lms_tenant_icc",
    "memory_usage": "36.5 MB",
    "peak_memory": "36.5 MB"
}
{
    "timestamp": "2026-09-14 20:38:23",
    "level": "INFO",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Using Moodle prefix: omo_",
    "memory_usage": "36.5 MB",
    "peak_memory": "36.5 MB"
}
{
    "timestamp": "2026-09-14 20:38:23",
    "level": "INFO",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Fetching data from Moodle...",
    "memory_usage": "36.5 MB",
    "peak_memory": "36.5 MB"
}
{
    "timestamp": "2026-09-14 20:52:57",
    "level": "INFO",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Found 1744090 records in Moodle",
    "memory_usage": "1.93 GB",
    "peak_memory": "2.19 GB"
}
{
    "timestamp": "2026-09-14 20:52:57",
    "level": "INFO",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Processing 1745 batches...",
    "memory_usage": "1.93 GB",
    "peak_memory": "2.19 GB"
}
{
    "timestamp": "2026-09-14 20:58:45",
    "level": "INFO",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Sync completed!",
    "memory_usage": "2.85 GB",
    "peak_memory": "2.86 GB"
}
{
    "timestamp": "2026-09-14 20:58:45",
    "level": "INFO",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Total records: 1744090",
    "memory_usage": "2.85 GB",
    "peak_memory": "2.86 GB"
}
{
    "timestamp": "2026-09-14 20:58:45",
    "level": "INFO",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Inserted: 1744090",
    "memory_usage": "2.85 GB",
    "peak_memory": "2.86 GB"
}
{
    "timestamp": "2026-09-14 20:58:45",
    "level": "INFO",
    "command": "SyncEnglishClassroomUserQuizGradeCommand",
    "message": "Errors: 0",
    "memory_usage": "2.85 GB",
    "peak_memory": "2.86 GB"
}
