echoapi-cron-scheduler-batch 1.0.20 → 1.0.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +34 -6
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -149,7 +149,8 @@ class CronScheduler {
149
149
  try {
150
150
  const runners = (typeof job.runners === 'string') ? JSON5.parse(job.runners || '[]') : (job.runners || []);
151
151
  const executionId = `${job.job_id}_${Date.now()}`;
152
- this.resultCollector.set(executionId, { total: runners.length, received_count: 0, job_info: job });
152
+ const actualStartTime = Date.now();
153
+ this.resultCollector.set(executionId, { total: runners.length, received_count: 0, job_info: { ...job, actualStartTime } });
153
154
 
154
155
  const workers = Object.values(cluster.workers).filter(w => w.isConnected());
155
156
  runners.forEach((runner, index) => {
@@ -187,7 +188,12 @@ class CronScheduler {
187
188
  try { cleanPayload.runners = JSON5.parse(cleanPayload.runners); } catch (e) { }
188
189
  }
189
190
 
190
- const jobData = { ...cleanPayload, is_cancel: 0, create_dtime: Date.now() };
191
+ const jobData = {
192
+ ...cleanPayload, is_cancel: 0,
193
+ hook_ids: payload.hook_ids || [], // 新增:钩子ID数组
194
+ case_hook_switch: payload.case_hook_switch || -1, // 新增:钩子开关,默认开启(1)
195
+ create_dtime: Date.now()
196
+ };
191
197
  fs.writeFileSync(jobPath, JSON.stringify(jobData, null, 2), 'utf8');
192
198
  this.upsertTimer(jobData.job_id, jobData.frequency, jobData);
193
199
  return true;
@@ -428,7 +434,10 @@ class CronScheduler {
428
434
  let s = {
429
435
  http_total: 0, http_success: 0,
430
436
  assert_total: 0, assert_success: 0,
431
- total_resp_time: 0
437
+ total_resp_time: 0,
438
+ // --- 新增计数器 ---
439
+ total_case: 0,
440
+ success_case: 0
432
441
  };
433
442
 
434
443
  // 1. 遍历并处理每个结果单元
@@ -440,6 +449,18 @@ class CronScheduler {
440
449
  s.assert_success += (item.assert?.success || 0);
441
450
  s.total_resp_time += (item.total_response_time || 0);
442
451
 
452
+ // --- 逻辑:计算 total_case ---
453
+ s.total_case++;
454
+
455
+ // --- 逻辑:判定 success_case ---
456
+ // 标准:http 全部成功 且 断言全部通过
457
+ const isHttpOk = (item.http?.total === item.http?.success);
458
+ const isAssertOk = (item.assert?.total === item.assert?.success);
459
+
460
+ if (isHttpOk && isAssertOk) {
461
+ s.success_case++;
462
+ }
463
+
443
464
  // 还原 item 内部字段
444
465
  return _.assign(item, {
445
466
  source: 'scheduled',
@@ -453,6 +474,7 @@ class CronScheduler {
453
474
  const assertRate = s.assert_total > 0 ? ((s.assert_success / s.assert_total) * 100).toFixed(2) : "0.00";
454
475
  const avgResp = processedResults.length > 0 ? (s.total_resp_time / processedResults.length).toFixed(0) : 0;
455
476
 
477
+ const currentTime = Date.now();
456
478
  // 3. 封装与之前 DB 版本完全一致的 Payload
457
479
  return {
458
480
  info: {
@@ -461,9 +483,15 @@ class CronScheduler {
461
483
  project_id: record.job_info.project_id,
462
484
  execution_id: executionId,
463
485
  // 使用任务开始时间和当前结束时间
464
-
465
- start_at: this.formatTimeToISO(record.job_info.start_time || Date.now()),
466
- end_at: this.formatTimeToISO(Date.now()),
486
+ // ⭐ 新增:将任务中的这两个字段塞入 info 中上报
487
+ hook_ids: record.job_info.hook_ids || [],
488
+ case_hook_switch: record.job_info.case_hook_switch ?? -1,
489
+
490
+ // 2. ⭐ 新增:用例级别统计
491
+ total_case: s.total_case,
492
+ success_case: s.success_case,
493
+ start_at: this.formatTimeToISO(record.job_info.actualStartTime),
494
+ end_at: this.formatTimeToISO(currentTime),
467
495
  http_pass_rate: `${httpRate}%`,
468
496
  assert_pass_rate: `${assertRate}%`,
469
497
  avg_response_time: `${avgResp}ms`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "echoapi-cron-scheduler-batch",
3
- "version": "1.0.20",
3
+ "version": "1.0.22",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {