Browse Source

🎈 perf: 优化改进部分细节

Pchen. 1 month ago
parent
commit
17bfca4a92
2 changed files with 22 additions and 3 deletions
  1. 1 1
      apis/Corn/StartAutoLepao.js
  2. 21 2
      plugin/jkes/runRecord.js

+ 1 - 1
apis/Corn/StartAutoLepao.js

@@ -57,7 +57,7 @@ class StartAutoLepao extends API {
 
                 const isSuccess = await Redis.get(jkesRedisKeys.lepaoSuccess(student_num))
                 if (isSuccess) {
-                    this.logger.info(`${name}(${student_num})当天已乐跑成功,不执行自动乐跑`)
+                    this.logger.info(`${name}(${student_num})当天已乐跑成功,不执行自动乐跑,如仍需跑步请手动发起`)
                     continue
                 }
 

+ 21 - 2
plugin/jkes/runRecord.js

@@ -225,6 +225,24 @@ function sleep(ms) {
     return new Promise((r) => setTimeout(r, ms))
 }
 
+/** 根据轨迹首尾 deviceTime 得到预计跑完全程的等待时长(与循环内 sleep 总和一致,不含网络请求) */
+function estimateRunWallClockMs(points) {
+    if (!Array.isArray(points) || points.length < 2) {
+        return 0
+    }
+    return Math.max(0, points[points.length - 1].deviceTime - points[0].deviceTime)
+}
+
+function formatMinutesSeconds(ms) {
+    const totalSec = Math.max(0, Math.round(ms / 1000))
+    const m = Math.floor(totalSec / 60)
+    const s = totalSec % 60
+    if (m <= 0) {
+        return `${s}秒`
+    }
+    return `${m}分${s}秒`
+}
+
 function buildAxiosConfig(headers) {
     const s = getJkesSettings()
     const agent = new https.Agent({
@@ -342,7 +360,8 @@ async function runJkesRecord(opts) {
     })
     const recordId = startJson.data.info.id
     const chunks = chunkPoints(points, firstBatchSize, batchSize)
-    log(`已开始跑步 recordId=${recordId} 预计耗时${(CALC_INTERVAL_MS * chunks.length / 1000).toFixed(2)}秒`)
+    const estMs = estimateRunWallClockMs(points)
+    log(`已开始跑步 recordId=${recordId} 预计耗时${formatMinutesSeconds(estMs)}`)
 
     const calcState = {
         runStartMs,
@@ -389,7 +408,7 @@ async function runJkesRecord(opts) {
 
     await postJson(`/health/runRecord/pause/${recordId}`, {})
     const endJson = await postJson(`/health/runRecord/end/${recordId}`, {})
-    log(`ID:{recordId} 跑步已结束`)
+    log(`ID:${recordId} 跑步已结束`)
     return { recordId, endJson, runStartMs, uploadedPayloadPoints }
 }