Browse Source

做题数据上报增加耗时

shmily1213 1 month ago
parent
commit
58fe221b27
2 changed files with 77 additions and 13 deletions
  1. 3 0
      src/common/report.ts
  2. 74 13
      src/pagesStudy/pages/exam-start/exam-start.vue

+ 3 - 0
src/common/report.ts

@@ -2,6 +2,9 @@ export const events = {
   examStartEnter: 'exam-start-enter',
   examStartLoadSuccess: 'exam-start-load-success',
   examStartLoadError: 'exam-start-load-error',
+  examStartGetPaperSuccess: 'exam-start-get-paper-success',
+  examStartGetPaperError: 'exam-start-get-paper-error',
+  ExamStartLoadSlow: 'exam-start-load-slow',
   examStartSubmitSuccess: 'exam-start-submit-success',
   examStartSubmitError: 'exam-start-submit-error',
   examStartExit: 'exam-start-exit',

+ 74 - 13
src/pagesStudy/pages/exam-start/exam-start.vue

@@ -204,7 +204,7 @@ const handleSubmit = (tempSave: boolean = false) => {
     };
     console.log('提交试卷参数', params)
     await commitExamineePaper(params);
-    uni.report(events.examStartSubmitSuccess, getReportData(params));
+    uni.report(events.examStartSubmitSuccess, getReportData());
     if (isSimulationExam.value || isTestExam.value) {
       if (!tempSave) {
         setTimeout(async () => {
@@ -288,6 +288,52 @@ const restoreQuestion = (savedQuestion: Study.ExamineeQuestion[], fullQuestion:
   }
   return fullQuestion;
 }
+/**
+ * 带超时检测的 getOpenExaminee 请求
+ * 在 3s, 5s, 10s, 15s, 20s 这 5 个时间点检测超时并上报
+ * 每个时间点如果请求还未返回,都会上报一次
+ */
+const getOpenExamineeWithTimeoutCheck = async (params: Study.OpenExamineeRequestDTO) => {
+  // 超时时间档次(单位:毫秒)
+  const timeoutLevels = [3000, 5000, 10000, 15000, 20000];
+  // 存储所有定时器ID,用于清理
+  const timers: number[] = [];
+  // 请求是否已完成
+  let isCompleted = false;
+
+  // 创建超时检测定时器
+  timeoutLevels.forEach((timeout) => {
+    const timer = setTimeout(() => {
+      // 如果请求已完成,不进行上报
+      if (isCompleted) {
+        return;
+      }
+      // 上报超时数据,超时时间单位为秒
+      // 每个时间点如果请求还未返回,都会上报一次
+      uni.report(events.ExamStartLoadSlow, getReportData({
+        timeout: timeout / 1000, // 转换为秒
+        timeoutLevel: timeout,
+      }));
+    }, timeout) as unknown as number;
+    timers.push(timer);
+  });
+
+  try {
+    // 执行请求
+    const result = await getOpenExaminee(params);
+    // 标记请求已完成
+    isCompleted = true;
+    // 清除所有定时器
+    timers.forEach((timer) => clearTimeout(timer));
+    return result;
+  } catch (error) {
+    // 请求失败时也要清除定时器
+    isCompleted = true;
+    timers.forEach((timer) => clearTimeout(timer));
+    throw error;
+  }
+};
+
 // 1、加载知识点练习数据
 const loadPracticeData = async () => {
   const { paperType, readonly, practiceInfo } = prevData.value;
@@ -298,17 +344,29 @@ const loadPracticeData = async () => {
       data = res.data;
     }
   } else {
-    const params = {
-      paperType: paperType,
-      relateId: practiceInfo?.relateId,
-    } as Study.OpenExamineeRequestDTO;
-    if (userStore.isVHS) {
-      params.questionType = practiceInfo?.questionType;
-    } else {
-      params.directed = practiceInfo?.directed || false;
+    try {
+      const params = {
+        paperType: paperType,
+        relateId: practiceInfo?.relateId,
+      } as Study.OpenExamineeRequestDTO;
+      if (userStore.isVHS) {
+        params.questionType = practiceInfo?.questionType;
+      } else {
+        params.directed = practiceInfo?.directed || false;
+      }
+      const startTime = Date.now();
+      const res = await getOpenExamineeWithTimeoutCheck(params);
+      data = res.data || {};
+      const endTime = Date.now() - startTime;
+      console.log('开卷用时:', endTime);
+      uni.report(events.examStartLoadSuccess, getReportData({
+        time: Date.now() - startTime
+      }));
+    } catch (error) {
+      uni.report(events.examStartLoadError, getReportData({
+        error: error
+      }));
     }
-    const res = await getOpenExaminee(params);
-    data = res.data || {};
   }
 
   if (!data) {
@@ -378,6 +436,7 @@ const combinePaperData = async (examinee: Study.Examinee, paperType: EnumPaperTy
     console.log('初始化数据', paperData.value.questions)
     setQuestionList(paperData.value.questions);
     setDuration(examinee.duration || 0);
+    uni.report(events.examStartGetPaperSuccess, getReportData());
     await nextTick();
     const targetQuestion = flatQuestionList.value.find(item => item.id === prevData.value.questionId);
     if (targetQuestion) {
@@ -409,9 +468,11 @@ const combinePaperData = async (examinee: Study.Examinee, paperType: EnumPaperTy
         startTime();
       }
     }
-    uni.report(events.examStartLoadSuccess, getReportData());
+    uni.report(events.examStartGetPaperSuccess, getReportData());
   } else {
-    uni.report(events.examStartLoadError, getReportData());
+    uni.report(events.examStartGetPaperError, getReportData({
+      error: '获取试卷数据失败,没有paperId'
+    }));
   }
 }
 const handleSwiperTipNext = () => {