Просмотр исходного кода

perf: reduce analysis report polling overhead

yemeishu 1 неделя назад
Родитель
Сommit
583b606cad

+ 2 - 8
app/Http/Controllers/Api/ExamAnalysisApiController.php

@@ -66,6 +66,7 @@ class ExamAnalysisApiController extends Controller
                         'recordId' => $recordId,
                     ]),
                     'pdf_url' => null,  // 稍后生成
+                    'pdf_poll_interval_seconds' => 2,
                     'created_at' => now()->toISOString(),
                 ],
             ];
@@ -133,11 +134,6 @@ class ExamAnalysisApiController extends Controller
                 ->first();
 
             if ($report && !empty($report->analysis_pdf_url)) {
-                Log::info('学情报告PDF URL查询成功(从数据库)', [
-                    'paper_id' => $paperId,
-                    'pdf_url' => $report->analysis_pdf_url,
-                ]);
-
                 return response()->json([
                     'success' => true,
                     'data' => [
@@ -150,15 +146,13 @@ class ExamAnalysisApiController extends Controller
                 ], 200, [], JSON_UNESCAPED_SLASHES);
             }
 
-            // 如果数据库中没有找到报告
-            Log::info('未找到学情报告', ['paper_id' => $paperId]);
-
             return response()->json([
                 'success' => true,
                 'data' => [
                     'paper_id' => $paperId,
                     'status' => 'not_found',
                     'pdf_url' => null,
+                    'retry_after_seconds' => 2,
                     'message' => '报告尚未生成,请先提交试卷进行分析',
                 ]
             ], 200, [], JSON_UNESCAPED_SLASHES);

+ 1 - 115
app/Services/ExamPdfExportService.php

@@ -345,16 +345,6 @@ class ExamPdfExportService
                 $lastMark = $now;
             };
 
-            Log::info('ExamPdfExportService: 开始生成学情分析PDF', [
-                'paper_id' => $paperId,
-                'student_id' => $studentId,
-                'record_id' => $recordId,
-            ]);
-            Log::warning('ExamPdfExportService: ANALYSIS_PDF_START', [
-                'paper_id' => $paperId,
-                'student_id' => $studentId,
-            ]);
-
             // 构建分析数据
             $analysisData = $this->buildAnalysisData($paperId, $studentId);
             $mark('build_analysis_data_ms');
@@ -367,15 +357,6 @@ class ExamPdfExportService
                 return null;
             }
 
-            Log::info('ExamPdfExportService: buildAnalysisData返回摘要', [
-                'paper_id' => $paperId,
-                'student_id' => $studentId,
-                'analysisData_keys_count' => count(array_keys($analysisData)),
-                'analysis_question_count' => count($analysisData['analysis_data']['question_analysis'] ?? []),
-                'questions_count' => count($analysisData['questions'] ?? []),
-                'mastery_count' => count($analysisData['mastery']['items'] ?? []),
-            ]);
-
             // 创建DTO
             $dto = ExamAnalysisDataDto::fromArray($analysisData);
             $payloadDto = ReportPayloadDto::fromExamAnalysisDataDto($dto);
@@ -386,9 +367,6 @@ class ExamPdfExportService
             if (function_exists('gc_collect_cycles')) {
                 gc_collect_cycles();
             }
-
-            Log::info('ExamPdfExportService: 模板数据摘要', $this->summarizeTemplateDataForLog($templateData));
-
             // V3 学情报告不再渲染逐题错题卡,保留题目元数据用于统计即可。
             $mark('prepare_report_data_ms');
 
@@ -2312,13 +2290,6 @@ class ExamPdfExportService
      */
     private function buildAnalysisData(string $paperId, string $studentId): ?array
     {
-        // 【关键调试】确认方法被调用
-        Log::warning('ExamPdfExportService: buildAnalysisData方法被调用了!', [
-            'paper_id' => $paperId,
-            'student_id' => $studentId,
-            'timestamp' => now()->toISOString(),
-        ]);
-
         $paper = Paper::with(['questions' => function ($query) {
             $query->orderBy('question_number')->orderBy('id');
         }])->find($paperId);
@@ -2361,12 +2332,6 @@ class ExamPdfExportService
 
         // 首先尝试从paper->analysis_id获取
         if (! empty($paper->analysis_id)) {
-            Log::info('ExamPdfExportService: 从本地数据库获取试卷分析数据', [
-                'paper_id' => $paperId,
-                'student_id' => $studentId,
-                'analysis_id' => $paper->analysis_id,
-            ]);
-
             $analysisRecord = \DB::table('exam_analysis_results')
                 ->where('id', $paper->analysis_id)
                 ->where('student_id', $studentId)
@@ -2374,9 +2339,6 @@ class ExamPdfExportService
 
             if ($analysisRecord && ! empty($analysisRecord->analysis_data)) {
                 $analysisData = json_decode($analysisRecord->analysis_data, true);
-                Log::info('ExamPdfExportService: 成功获取本地分析数据(通过analysis_id)', [
-                    'data_size' => strlen($analysisRecord->analysis_data),
-                ]);
             } else {
                 Log::warning('ExamPdfExportService: 未找到本地分析数据,将尝试其他方式', [
                     'paper_id' => $paperId,
@@ -2388,11 +2350,6 @@ class ExamPdfExportService
 
         // 如果没有analysis_id或未找到数据,直接从exam_analysis_results表查询
         if (empty($analysisData)) {
-            Log::info('ExamPdfExportService: 直接从exam_analysis_results表查询分析数据', [
-                'paper_id' => $paperId,
-                'student_id' => $studentId,
-            ]);
-
             $analysisRecord = \DB::table('exam_analysis_results')
                 ->where('paper_id', $paperId)
                 ->where('student_id', $studentId)
@@ -2401,10 +2358,6 @@ class ExamPdfExportService
 
             if ($analysisRecord && ! empty($analysisRecord->analysis_data)) {
                 $analysisData = json_decode($analysisRecord->analysis_data, true);
-                Log::info('ExamPdfExportService: 成功获取本地分析数据(直接查询)', [
-                    'data_size' => strlen($analysisRecord->analysis_data),
-                    'question_count' => count($analysisData['question_analysis'] ?? []),
-                ]);
             } else {
                 Log::warning('ExamPdfExportService: 未找到任何分析数据,将使用空数据', [
                     'paper_id' => $paperId,
@@ -2416,11 +2369,6 @@ class ExamPdfExportService
         // 【修复】优先使用analysisData中的knowledge_point_analysis数据
         $masteryData = [];
         $parentMasteryLevels = []; // 新增:父节点掌握度数据
-        Log::info('ExamPdfExportService: 开始处理掌握度数据', [
-            'student_id' => $studentId,
-            'analysisData_keys' => array_keys($analysisData),
-            'has_knowledge_point_analysis' => ! empty($analysisData['knowledge_point_analysis']),
-        ]);
 
         $fullMasteryMap = [];
         $snapshotMasteryData = [];
@@ -2441,10 +2389,6 @@ class ExamPdfExportService
             try {
                 // 获取本次考试涉及的知识点代码列表
                 $examKpCodes = array_column($masteryData, 'kp_code');
-                Log::info('ExamPdfExportService: 本次考试涉及的知识点', [
-                    'count' => count($examKpCodes),
-                    'kp_codes' => $examKpCodes,
-                ]);
 
                 // 获取最新快照的数据(mastery_data 内已包含 current_mastery 和 previous_mastery)
                 $lastSnapshot = DB::connection('mysql')
@@ -2606,27 +2550,14 @@ class ExamPdfExportService
                     }
                 }
 
-                Log::info('ExamPdfExportService: 过滤后的父节点掌握度', [
-                    'all_parent_count' => count($allParentMasteryLevels),
-                    'filtered_parent_count' => count($parentMasteryLevels),
-                    'filtered_codes' => array_keys($parentMasteryLevels),
-                ]);
             } catch (\Exception $e) {
                 Log::warning('ExamPdfExportService: 获取父节点掌握度失败', [
                     'error' => $e->getMessage(),
                 ]);
             }
-
-            Log::info('ExamPdfExportService: 使用analysisData中的掌握度数据', [
-                'count' => count($masteryData),
-                'has_change_values' => collect($masteryData)->contains(fn ($item) => is_array($item) && array_key_exists('mastery_change', $item)),
-            ]);
         } else {
             // 如果没有knowledge_point_analysis,使用MasteryCalculator获取多层级掌握度概览
             try {
-                Log::info('ExamPdfExportService: 获取学生多层级掌握度概览', [
-                    'student_id' => $studentId,
-                ]);
                 $masteryOverview = $this->masteryCalculator->getStudentMasteryOverviewWithHierarchy($studentId);
                 $masteryData = $masteryOverview['details'] ?? [];
                 $parentMasteryLevels = $masteryOverview['parent_mastery_levels'] ?? []; // 获取父节点掌握度
@@ -2697,11 +2628,6 @@ class ExamPdfExportService
                         unset($item);
                     }
                 }
-
-                Log::info('ExamPdfExportService: 成功获取多层级掌握度数据', [
-                    'count' => count($masteryData),
-                    'parent_count' => count($parentMasteryLevels),
-                ]);
             } catch (\Exception $e) {
                 Log::error('ExamPdfExportService: 获取掌握度数据失败', [
                     'student_id' => $studentId,
@@ -2710,29 +2636,11 @@ class ExamPdfExportService
             }
         }
 
-        // 【修改】使用本地方法获取学习路径推荐(替代API调用)
+        // V3 PDF 会在 reduceTemplateDataForV3 中丢弃 recommendations,这里不再计算未展示的推荐数据。
         $recommendations = [];
-        try {
-            Log::info('ExamPdfExportService: 获取学习路径推荐', [
-                'student_id' => $studentId,
-            ]);
-            $learningPaths = $this->learningAnalyticsService->recommendLearningPaths($studentId, 3);
-            $recommendations = $learningPaths['recommendations'] ?? [];
-            Log::info('ExamPdfExportService: 成功获取学习路径推荐', [
-                'count' => count($recommendations),
-            ]);
-        } catch (\Exception $e) {
-            Log::error('ExamPdfExportService: 获取学习路径推荐失败', [
-                'student_id' => $studentId,
-                'error' => $e->getMessage(),
-            ]);
-        }
 
         // 获取知识点名称映射
         $kpNameMap = $this->buildKnowledgePointNameMap();
-        Log::info('ExamPdfExportService: 获取知识点名称映射', [
-            'kpNameMap_count' => count($kpNameMap),
-        ]);
 
         // 【修复】直接从MySQL数据库获取题目详情(不通过API)
         // 只有当 $paper 是 Paper 模型时才查询题目详情
@@ -2745,13 +2653,6 @@ class ExamPdfExportService
 
         // 【关键调试】查看buildMasterySummary的返回结果
         $masterySummary = $this->buildMasterySummary($masteryData, $kpNameMap);
-        Log::info('ExamPdfExportService: buildMasterySummary返回结果', [
-            'masteryData_count' => count($masteryData),
-            'kpNameMap_count' => count($kpNameMap),
-            'masterySummary_items_count' => count($masterySummary['items'] ?? []),
-            'masterySummary_avg' => $masterySummary['average'] ?? null,
-            'masterySummary_weak_count' => count($masterySummary['weak_list'] ?? []),
-        ]);
 
         // 构建当前学生掌握度映射,供父子影响分析展示使用
         // 与 PC 端保持一致:优先使用当前报告/学案对应快照的 current_mastery。
@@ -2781,11 +2682,6 @@ class ExamPdfExportService
             $snapshotMasteryData
         );
 
-        Log::info('ExamPdfExportService: 处理后的父节点掌握度', [
-            'raw_count' => count($parentMasteryLevels),
-            'processed_count' => count($processedParentMastery),
-        ]);
-
         return [
             'paper' => [
                 'id' => $paper->paper_id,
@@ -4375,11 +4271,6 @@ class ExamPdfExportService
      */
     private function buildMasterySummary(array $masteryData, array $kpNameMap): array
     {
-        Log::info('ExamPdfExportService: buildMasterySummary开始处理', [
-            'masteryData_count' => count($masteryData),
-            'kpNameMap_count' => count($kpNameMap),
-        ]);
-
         $items = [];
         $total = 0;
         $count = 0;
@@ -4415,11 +4306,6 @@ class ExamPdfExportService
             'weak_list' => array_slice($items, 0, 5),
         ];
 
-        Log::info('ExamPdfExportService: buildMasterySummary完成', [
-            'total_count' => $count,
-            'items_count' => count($items),
-        ]);
-
         return $result;
     }