@kne/fastify-task 1.1.1 → 2.0.0

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.
package/README.md CHANGED
@@ -41,6 +41,55 @@ npm i --save @kne/fastify-task
41
41
  | failed | 执行失败 |
42
42
  | canceled | 已取消 |
43
43
 
44
+ ### 签名验证
45
+
46
+ 当任务的 `context.secret` 设置了密钥时,外部调用需要提供 HMAC-SHA256 签名进行验证。
47
+
48
+ #### 签名生成方法
49
+
50
+ ```javascript
51
+ const crypto = require('node:crypto');
52
+
53
+ function generateSignature({ secret, id, data }) {
54
+ // data 可以是字符串或对象
55
+ const dataStr = typeof data === 'string' ? data : JSON.stringify(data);
56
+ const dataToSign = `${id}|${dataStr}`;
57
+ const hmac = crypto.createHmac('sha256', secret);
58
+ hmac.update(dataToSign);
59
+ return hmac.digest('hex');
60
+ }
61
+ ```
62
+
63
+ #### 各接口签名数据格式
64
+
65
+ | 接口 | data 格式 |
66
+ |----------------------|----------------------------------|
67
+ | processNext | `result` 字符串(JSON格式结果) |
68
+ | logWithSignature | `{ data, message }` 对象 |
69
+ | callbackWithSignature| `{ code, data, message }` 对象 |
70
+
71
+ #### 示例
72
+
73
+ ```javascript
74
+ // processNext 签名
75
+ const result = JSON.stringify({ code: 0, data: { output: 'done' } });
76
+ const signature = generateSignature({ secret: 'your-secret', id: 'task-1', data: result });
77
+
78
+ // logWithSignature 签名
79
+ const signature = generateSignature({
80
+ secret: 'your-secret',
81
+ id: 'task-1',
82
+ data: { data: { key: 'value' }, message: '日志消息' }
83
+ });
84
+
85
+ // callbackWithSignature 签名
86
+ const signature = generateSignature({
87
+ secret: 'your-secret',
88
+ id: 'task-1',
89
+ data: { code: 0, data: { result: 'done' }, message: '成功' }
90
+ });
91
+ ```
92
+
44
93
  ### 示例
45
94
 
46
95
  ### API
@@ -211,7 +260,20 @@ npm i --save @kne/fastify-task
211
260
  | id | string | 否 | 单个任务ID |
212
261
  | taskIds | string[] | 否 | 任务ID数组 |
213
262
 
214
- #### log - 记录日志
263
+ #### log - 记录日志(内部调用)
264
+
265
+ > 内部调用方法,不需要签名验证
266
+
267
+ | 参数名 | 类型 | 必填 | 说明 |
268
+ |-----------|--------|----|-----------------|
269
+ | id | string | 是 | 任务ID |
270
+ | taskId | string | 是 | 任务ID(id的别名) |
271
+ | data | object | 否 | 日志数据 |
272
+ | message | string | 否 | 日志消息 |
273
+
274
+ #### logWithSignature - 记录日志(外部调用)
275
+
276
+ > 外部调用方法,需要签名验证(HTTP API使用)
215
277
 
216
278
  | 参数名 | 类型 | 必填 | 说明 |
217
279
  |-----------|--------|----|----------------------------------------|
@@ -221,7 +283,20 @@ npm i --save @kne/fastify-task
221
283
  | message | string | 否 | 日志消息 |
222
284
  | signature | string | 否 | HMAC-SHA256签名(当任务设置了context.secret时必填) |
223
285
 
224
- #### callback - 任务回调
286
+ #### callback - 任务回调(内部调用)
287
+
288
+ > 内部调用方法,不需要签名验证
289
+
290
+ | 参数名 | 类型 | 必填 | 说明 |
291
+ |-----------|--------|----|-----------|
292
+ | id | string | 是 | 任务ID |
293
+ | code | number | 是 | 状态码,0为成功 |
294
+ | data | object | 否 | 回调数据 |
295
+ | message | string | 否 | 回调消息 |
296
+
297
+ #### callbackWithSignature - 任务回调(外部调用)
298
+
299
+ > 外部调用方法,需要签名验证(HTTP API使用)
225
300
 
226
301
  | 参数名 | 类型 | 必填 | 说明 |
227
302
  |-----------|--------|----|----------------------------------------|
@@ -228,7 +228,7 @@ module.exports = fp(async (fastify, options) => {
228
228
  }
229
229
  },
230
230
  async request => {
231
- return await services.log(Object.assign({}, request.body, { taskId: request.query.taskId }));
231
+ return await services.logWithSignature(Object.assign({}, request.body, { taskId: request.query.taskId }));
232
232
  }
233
233
  );
234
234
 
@@ -269,7 +269,176 @@ module.exports = fp(async (fastify, options) => {
269
269
  }
270
270
  },
271
271
  async request => {
272
- return await services.callback(Object.assign({}, request.body, { taskId: request.query.taskId }));
272
+ return await services.callbackWithSignature(Object.assign({}, request.body, { taskId: request.query.taskId }));
273
+ }
274
+ );
275
+
276
+ // 历史统计数据
277
+ fastify.get(
278
+ `${options.prefix}/statistics`,
279
+ {
280
+ onRequest: options.getAuthenticate('statistics'),
281
+ schema: {
282
+ summary: '获取任务统计概览',
283
+ query: {
284
+ type: 'object',
285
+ properties: {
286
+ range: { type: 'string', default: '7d', description: '时间范围: 7d=近7天, 1m=近1个月, 1y=近1年' },
287
+ timezone: { type: 'string', description: '时区,如 Asia/Shanghai,默认服务器时区' },
288
+ type: { type: 'string', description: '按任务类型筛选' }
289
+ }
290
+ },
291
+ response: {
292
+ 200: {
293
+ type: 'object',
294
+ properties: {
295
+ range: { type: 'string', description: '当前时间范围' },
296
+ rangeLabel: { type: 'string', description: '时间范围描述' },
297
+ totalTasks: { type: 'integer', description: '时间范围内任务总数' },
298
+ byStatus: {
299
+ type: 'object',
300
+ description: '按任务状态统计',
301
+ additionalProperties: { type: 'integer' }
302
+ },
303
+ byType: {
304
+ type: 'object',
305
+ description: '按任务类型统计',
306
+ additionalProperties: { type: 'integer' }
307
+ },
308
+ byRunnerType: {
309
+ type: 'object',
310
+ description: '按执行者类型统计(manual/system)',
311
+ additionalProperties: { type: 'integer' }
312
+ },
313
+ byTargetType: {
314
+ type: 'object',
315
+ description: '按目标对象类型统计',
316
+ additionalProperties: { type: 'integer' }
317
+ },
318
+ recentTrend: {
319
+ type: 'array',
320
+ description: '按日任务创建趋势',
321
+ items: {
322
+ type: 'object',
323
+ properties: {
324
+ date: { type: 'string', description: '日期' },
325
+ count: { type: 'integer', description: '任务数量' }
326
+ }
327
+ }
328
+ },
329
+ recentTrendByStatus: {
330
+ type: 'array',
331
+ description: '按日+状态任务创建趋势',
332
+ items: {
333
+ type: 'object',
334
+ properties: {
335
+ date: { type: 'string', description: '日期' },
336
+ status: { type: 'string', description: '任务状态' },
337
+ count: { type: 'integer', description: '任务数量' }
338
+ }
339
+ }
340
+ },
341
+ recentTrendByType: {
342
+ type: 'array',
343
+ description: '按日+类型任务创建趋势',
344
+ items: {
345
+ type: 'object',
346
+ properties: {
347
+ date: { type: 'string', description: '日期' },
348
+ type: { type: 'string', description: '任务类型' },
349
+ count: { type: 'integer', description: '任务数量' }
350
+ }
351
+ }
352
+ },
353
+ durationTrend: {
354
+ type: 'array',
355
+ description: '按日平均耗时趋势(来自预聚合统计表)',
356
+ items: {
357
+ type: 'object',
358
+ properties: {
359
+ date: { type: 'string', description: '完成日期' },
360
+ completedCount: { type: 'integer', description: '完成任务数' },
361
+ successCount: { type: 'integer', description: '成功数' },
362
+ failedCount: { type: 'integer', description: '失败数' },
363
+ canceledCount: { type: 'integer', description: '取消数' },
364
+ avgWaitingTime: { type: 'integer', description: '平均等待时间(毫秒)' },
365
+ avgExecutionTime: { type: 'integer', description: '平均执行时间(毫秒)' },
366
+ avgTotalTime: { type: 'integer', description: '平均总耗时(毫秒)' },
367
+ byType: {
368
+ type: 'object',
369
+ description: '按类型分组的耗时统计',
370
+ additionalProperties: {
371
+ type: 'object',
372
+ properties: {
373
+ count: { type: 'integer' },
374
+ avgWaitingTime: { type: 'integer' },
375
+ avgExecutionTime: { type: 'integer' },
376
+ avgTotalTime: { type: 'integer' }
377
+ }
378
+ }
379
+ },
380
+ byRunnerType: {
381
+ type: 'object',
382
+ description: '按执行者类型分组的耗时统计',
383
+ additionalProperties: {
384
+ type: 'object',
385
+ properties: {
386
+ count: { type: 'integer' },
387
+ avgWaitingTime: { type: 'integer' },
388
+ avgExecutionTime: { type: 'integer' },
389
+ avgTotalTime: { type: 'integer' }
390
+ }
391
+ }
392
+ }
393
+ }
394
+ }
395
+ }
396
+ }
397
+ }
398
+ }
399
+ }
400
+ },
401
+ async request => {
402
+ const { range = '7d', timezone, type } = request.query;
403
+ return await services.statistics.getOverview({ range, timezone, type });
404
+ }
405
+ );
406
+
407
+ // SSE 实时统计数据推送
408
+ fastify.get(
409
+ `${options.prefix}/statistics/sse`,
410
+ {
411
+ sse: true,
412
+ onRequest: options.getAuthenticate('statistics'),
413
+ schema: {
414
+ summary: 'SSE实时推送当天任务统计数据',
415
+ query: {
416
+ type: 'object',
417
+ properties: {
418
+ interval: { type: 'integer', minimum: 1, default: 5, description: '推送间隔时间(秒),最小1秒,默认5秒' },
419
+ timezone: { type: 'string', description: '时区,如 Asia/Shanghai,默认服务器时区' },
420
+ type: { type: 'string', description: '按任务类型筛选' }
421
+ }
422
+ }
423
+ }
424
+ },
425
+ async function (request, reply) {
426
+ const intervalSeconds = request.query.interval;
427
+ const { timezone, type } = request.query;
428
+ reply.sse.keepAlive();
429
+
430
+ async function* eventStream() {
431
+ while (reply.sse.isConnected) {
432
+ try {
433
+ yield { data: JSON.stringify(await services.statistics.getRealtime({ timezone, type })) };
434
+ } catch (err) {
435
+ yield { event: 'error', data: JSON.stringify({ message: err.message }) };
436
+ }
437
+ await new Promise(resolve => setTimeout(resolve, intervalSeconds * 1000));
438
+ }
439
+ }
440
+
441
+ await reply.sse.send(eventStream());
273
442
  }
274
443
  );
275
444
  });
@@ -0,0 +1,66 @@
1
+ module.exports = ({ DataTypes, definePrimaryType, options }) => {
2
+ return {
3
+ model: {
4
+ date: {
5
+ type: DataTypes.DATEONLY,
6
+ comment: '统计日期(按完成日期)'
7
+ },
8
+ totalCompleted: {
9
+ type: DataTypes.INTEGER,
10
+ defaultValue: 0,
11
+ comment: '当天完成任务总数'
12
+ },
13
+ successCount: {
14
+ type: DataTypes.INTEGER,
15
+ defaultValue: 0,
16
+ comment: '成功数'
17
+ },
18
+ failedCount: {
19
+ type: DataTypes.INTEGER,
20
+ defaultValue: 0,
21
+ comment: '失败数'
22
+ },
23
+ canceledCount: {
24
+ type: DataTypes.INTEGER,
25
+ defaultValue: 0,
26
+ comment: '取消数'
27
+ },
28
+ totalWaitingTime: {
29
+ type: DataTypes.BIGINT,
30
+ defaultValue: 0,
31
+ comment: '总等待时间(毫秒)'
32
+ },
33
+ totalExecutionTime: {
34
+ type: DataTypes.BIGINT,
35
+ defaultValue: 0,
36
+ comment: '总执行时间(毫秒)'
37
+ },
38
+ totalTime: {
39
+ type: DataTypes.BIGINT,
40
+ defaultValue: 0,
41
+ comment: '总耗时(毫秒, completedAt-createdAt)'
42
+ },
43
+ timedTaskCount: {
44
+ type: DataTypes.INTEGER,
45
+ defaultValue: 0,
46
+ comment: '有耗时数据的任务数'
47
+ },
48
+ byType: {
49
+ type: DataTypes.JSON,
50
+ defaultValue: {},
51
+ comment: '按任务类型分组统计'
52
+ },
53
+ byRunnerType: {
54
+ type: DataTypes.JSON,
55
+ defaultValue: {},
56
+ comment: '按执行者类型分组统计'
57
+ }
58
+ },
59
+ options: {
60
+ comment: '任务每日统计',
61
+ indexes: [
62
+ { fields: ['date'], unique: true }
63
+ ]
64
+ }
65
+ };
66
+ };
@@ -29,6 +29,10 @@ module.exports = ({ DataTypes, definePrimaryType, options }) => {
29
29
  comment: '任务最早执行时间',
30
30
  defaultValue: new Date()
31
31
  },
32
+ startedAt: {
33
+ type: DataTypes.DATE,
34
+ comment: '任务实际开始执行时间'
35
+ },
32
36
  completedAt: {
33
37
  type: DataTypes.DATE,
34
38
  comment: '任务完成时间'
@@ -92,7 +96,16 @@ module.exports = ({ DataTypes, definePrimaryType, options }) => {
92
96
  });
93
97
  },
94
98
  options: {
95
- comment: '任务'
99
+ comment: '任务',
100
+ indexes: [
101
+ { fields: ['created_at'] },
102
+ { fields: ['status'] },
103
+ { fields: ['type'] },
104
+ { fields: ['runner_type'] },
105
+ { fields: ['type', 'status'] },
106
+ { fields: ['runner_type', 'status'] },
107
+ { fields: ['target_id', 'target_type', 'type'] }
108
+ ]
96
109
  }
97
110
  };
98
111
  };
@@ -2,6 +2,12 @@ const fp = require('fastify-plugin');
2
2
  const path = require('node:path');
3
3
  const fs = require('fs-extra');
4
4
  const crypto = require('node:crypto');
5
+ const dayjs = require('dayjs');
6
+ const utc = require('dayjs/plugin/utc');
7
+ const timezone = require('dayjs/plugin/timezone');
8
+
9
+ dayjs.extend(utc);
10
+ dayjs.extend(timezone);
5
11
 
6
12
  module.exports = fp(async (fastify, options) => {
7
13
  const { models } = fastify[options.name];
@@ -137,6 +143,7 @@ module.exports = fp(async (fastify, options) => {
137
143
  error: result,
138
144
  completedAt: new Date()
139
145
  });
146
+ updateDailyStatistics(task);
140
147
  return;
141
148
  }
142
149
  await options.task[task.type]({ task, result: result.data, context: task.context });
@@ -146,12 +153,14 @@ module.exports = fp(async (fastify, options) => {
146
153
  progress: 100,
147
154
  completedAt: new Date()
148
155
  });
156
+ updateDailyStatistics(task);
149
157
  };
150
158
 
151
159
  const processSystemTask = async task => {
152
160
  try {
153
161
  await task.update({
154
162
  status: 'running',
163
+ startedAt: new Date(),
155
164
  progress: 0,
156
165
  error: null,
157
166
  output: null,
@@ -175,13 +184,15 @@ module.exports = fp(async (fastify, options) => {
175
184
  progress: 100,
176
185
  completedAt: new Date()
177
186
  });
187
+ updateDailyStatistics(task);
178
188
  })
179
- .catch(e => {
180
- return task.update({
189
+ .catch(async e => {
190
+ await task.update({
181
191
  status: 'failed',
182
192
  error: (e.stack || '').replaceAll(process.cwd(), '/server'),
183
193
  completedAt: new Date()
184
194
  });
195
+ updateDailyStatistics(task);
185
196
  });
186
197
  } catch (e) {
187
198
  await task.update({
@@ -189,6 +200,7 @@ module.exports = fp(async (fastify, options) => {
189
200
  error: (e.stack || '').replaceAll(process.cwd(), '/server'),
190
201
  completedAt: new Date()
191
202
  });
203
+ updateDailyStatistics(task);
192
204
  }
193
205
  };
194
206
 
@@ -238,7 +250,7 @@ module.exports = fp(async (fastify, options) => {
238
250
 
239
251
  const cancel = async ({ id, targetId, targetType, type }) => {
240
252
  if (targetId && targetType && type) {
241
- return await models.task.update(
253
+ const [affectedCount] = await models.task.update(
242
254
  {
243
255
  status: 'canceled'
244
256
  },
@@ -253,15 +265,18 @@ module.exports = fp(async (fastify, options) => {
253
265
  }
254
266
  }
255
267
  );
268
+ return affectedCount;
256
269
  }
257
270
  if (id) {
258
271
  const task = await detail({ id });
259
272
  if (!['pending', 'running'].includes(task.status)) {
260
273
  return;
261
274
  }
262
- return await task.update({
263
- status: 'canceled'
275
+ await task.update({
276
+ status: 'canceled',
277
+ completedAt: new Date()
264
278
  });
279
+ updateDailyStatistics(task);
265
280
  }
266
281
  };
267
282
 
@@ -278,17 +293,21 @@ module.exports = fp(async (fastify, options) => {
278
293
  output,
279
294
  progress: 100,
280
295
  completedAt: new Date(),
296
+ startedAt: task.startedAt || task.createdAt,
281
297
  completedUserId: userId
282
298
  });
299
+ updateDailyStatistics(task);
283
300
  } catch (e) {
284
301
  await task.update(
285
302
  Object.assign({}, props, {
286
303
  status: 'failed',
287
304
  output,
288
305
  error: (e.stack || '').replaceAll(process.cwd(), '/server'),
289
- completedAt: new Date()
306
+ completedAt: new Date(),
307
+ startedAt: task.startedAt || task.createdAt
290
308
  })
291
309
  );
310
+ updateDailyStatistics(task);
292
311
  throw e;
293
312
  }
294
313
  } else {
@@ -296,9 +315,11 @@ module.exports = fp(async (fastify, options) => {
296
315
  Object.assign({}, props, {
297
316
  status: 'failed',
298
317
  completedAt: new Date(),
318
+ startedAt: task.startedAt || task.createdAt,
299
319
  completedUserId: userId
300
320
  })
301
321
  );
322
+ updateDailyStatistics(task);
302
323
  }
303
324
  };
304
325
 
@@ -439,17 +460,13 @@ module.exports = fp(async (fastify, options) => {
439
460
  }
440
461
  };
441
462
 
442
- const log = async ({ id, taskId, data, message = '', signature }) => {
463
+ const log = async ({ id, taskId, data, message = '' }) => {
443
464
  const targetId = id || taskId;
444
465
  const task = await detail({ id: targetId });
445
466
  if (!task) {
446
467
  throw new Error('任务不存在');
447
468
  }
448
469
 
449
- if (task.context?.secret && !verifySignature({ secret: task.context.secret, id: targetId, data: { data, message }, signature })) {
450
- throw new Error('签名验证失败');
451
- }
452
-
453
470
  const currentOptions = task.options || {};
454
471
  const logs = (currentOptions.logs || []).slice(0);
455
472
  logs.splice(0, 0, {
@@ -470,21 +487,28 @@ module.exports = fp(async (fastify, options) => {
470
487
  return task;
471
488
  };
472
489
 
473
- const callback = async ({ id, code, data, message, signature }) => {
474
- const task = await detail({ id });
490
+ const logWithSignature = async ({ id, taskId, data, message = '', signature }) => {
491
+ const targetId = id || taskId;
492
+ const task = await detail({ id: targetId });
475
493
  if (!task) {
476
494
  throw new Error('任务不存在');
477
495
  }
478
496
 
479
- if (task.context?.secret && !verifySignature({ secret: task.context.secret, id, data: { code, data, message }, signature })) {
497
+ if (task.context?.secret && !verifySignature({ secret: task.context.secret, id: targetId, data: { data, message }, signature })) {
480
498
  throw new Error('签名验证失败');
481
499
  }
482
500
 
501
+ return log({ id: targetId, data, message });
502
+ };
503
+
504
+ const callback = async ({ id, code, data, message }) => {
505
+ const task = await detail({ id });
506
+ if (!task) {
507
+ throw new Error('任务不存在');
508
+ }
509
+
483
510
  const result = { code, data, message };
484
- const logData = JSON.stringify(result);
485
- const logMessage = '回调结果';
486
- const logSignature = task.context?.secret ? generateSignature({ secret: task.context.secret, id, data: { data: logData, message: logMessage } }) : undefined;
487
- await log({ taskId: id, message: logMessage, data: logData, signature: logSignature });
511
+ await log({ id, message: '回调结果', data: JSON.stringify(result) });
488
512
  const input = Object.assign(
489
513
  {},
490
514
  { id },
@@ -502,6 +526,539 @@ module.exports = fp(async (fastify, options) => {
502
526
  await complete(input);
503
527
  };
504
528
 
529
+ const callbackWithSignature = async ({ id, code, data, message, signature }) => {
530
+ const task = await detail({ id });
531
+ if (!task) {
532
+ throw new Error('任务不存在');
533
+ }
534
+
535
+ if (task.context?.secret && !verifySignature({ secret: task.context.secret, id, data: { code, data, message }, signature })) {
536
+ throw new Error('签名验证失败');
537
+ }
538
+
539
+ return callback({ id, code, data, message });
540
+ };
541
+
542
+ // 按指定时区获取"今天0点"的UTC Date对象
543
+ const getTodayStart = tz => {
544
+ if (!tz) {
545
+ return dayjs().startOf('day').toDate();
546
+ }
547
+ return dayjs().tz(tz).startOf('day').toDate();
548
+ };
549
+
550
+ // 按指定时区格式化日期
551
+ const formatDate = (date, tz) => {
552
+ if (!tz) {
553
+ return dayjs(date).format('YYYY-MM-DD');
554
+ }
555
+ return dayjs(date).tz(tz).format('YYYY-MM-DD');
556
+ };
557
+
558
+ const resolveTimezone = tz => tz || dayjs.tz.guess();
559
+
560
+ // 更新每日统计表(任务完成/取消时调用,异步不阻塞主流程)
561
+ const updateDailyStatistics = task => {
562
+ const completedAt = task.completedAt || new Date();
563
+ const statDate = dayjs(completedAt).format('YYYY-MM-DD');
564
+ const status = task.status;
565
+
566
+ // 计算耗时
567
+ const createdAt = task.createdAt;
568
+ const startedAt = task.startedAt;
569
+ let waitingTime = 0;
570
+ let executionTime = 0;
571
+ let totalTime = 0;
572
+ let hasTiming = false;
573
+
574
+ if (createdAt && completedAt) {
575
+ totalTime = new Date(completedAt).getTime() - new Date(createdAt).getTime();
576
+ if (startedAt) {
577
+ waitingTime = new Date(startedAt).getTime() - new Date(createdAt).getTime();
578
+ executionTime = new Date(completedAt).getTime() - new Date(startedAt).getTime();
579
+ } else {
580
+ // 无startedAt时(手动任务直接完成),等待时间=0,执行时间=总耗时
581
+ executionTime = totalTime;
582
+ }
583
+ hasTiming = totalTime > 0;
584
+ }
585
+
586
+ // 异步更新,不阻塞主流程
587
+ (async () => {
588
+ try {
589
+ const [stat, created] = await models.taskDailyStatistics.findOrCreate({
590
+ where: { date: statDate },
591
+ defaults: {
592
+ date: statDate,
593
+ totalCompleted: 1,
594
+ successCount: status === 'success' ? 1 : 0,
595
+ failedCount: status === 'failed' ? 1 : 0,
596
+ canceledCount: status === 'canceled' ? 1 : 0,
597
+ totalWaitingTime: hasTiming ? waitingTime : 0,
598
+ totalExecutionTime: hasTiming ? executionTime : 0,
599
+ totalTime: hasTiming ? totalTime : 0,
600
+ timedTaskCount: hasTiming ? 1 : 0,
601
+ byType: {
602
+ [task.type]: {
603
+ count: 1,
604
+ successCount: status === 'success' ? 1 : 0,
605
+ failedCount: status === 'failed' ? 1 : 0,
606
+ canceledCount: status === 'canceled' ? 1 : 0,
607
+ totalWaitingTime: hasTiming ? waitingTime : 0,
608
+ totalExecutionTime: hasTiming ? executionTime : 0,
609
+ totalTime: hasTiming ? totalTime : 0,
610
+ timedTaskCount: hasTiming ? 1 : 0
611
+ }
612
+ },
613
+ byRunnerType: {
614
+ [task.runnerType]: {
615
+ count: 1,
616
+ successCount: status === 'success' ? 1 : 0,
617
+ failedCount: status === 'failed' ? 1 : 0,
618
+ canceledCount: status === 'canceled' ? 1 : 0,
619
+ totalWaitingTime: hasTiming ? waitingTime : 0,
620
+ totalExecutionTime: hasTiming ? executionTime : 0,
621
+ totalTime: hasTiming ? totalTime : 0,
622
+ timedTaskCount: hasTiming ? 1 : 0
623
+ }
624
+ }
625
+ }
626
+ });
627
+
628
+ if (!created) {
629
+ const mergeKey = (obj, key, increment) => {
630
+ const current = obj[key] || {
631
+ count: 0, successCount: 0, failedCount: 0, canceledCount: 0,
632
+ totalWaitingTime: 0, totalExecutionTime: 0, totalTime: 0, timedTaskCount: 0
633
+ };
634
+ return Object.assign({}, obj, {
635
+ [key]: {
636
+ count: current.count + (increment.count || 0),
637
+ successCount: current.successCount + (increment.successCount || 0),
638
+ failedCount: current.failedCount + (increment.failedCount || 0),
639
+ canceledCount: current.canceledCount + (increment.canceledCount || 0),
640
+ totalWaitingTime: current.totalWaitingTime + (increment.totalWaitingTime || 0),
641
+ totalExecutionTime: current.totalExecutionTime + (increment.totalExecutionTime || 0),
642
+ totalTime: current.totalTime + (increment.totalTime || 0),
643
+ timedTaskCount: current.timedTaskCount + (increment.timedTaskCount || 0)
644
+ }
645
+ });
646
+ };
647
+
648
+ const typeIncrement = {
649
+ count: 1,
650
+ successCount: status === 'success' ? 1 : 0,
651
+ failedCount: status === 'failed' ? 1 : 0,
652
+ canceledCount: status === 'canceled' ? 1 : 0,
653
+ totalWaitingTime: hasTiming ? waitingTime : 0,
654
+ totalExecutionTime: hasTiming ? executionTime : 0,
655
+ totalTime: hasTiming ? totalTime : 0,
656
+ timedTaskCount: hasTiming ? 1 : 0
657
+ };
658
+
659
+ await stat.update({
660
+ totalCompleted: stat.totalCompleted + 1,
661
+ successCount: stat.successCount + (status === 'success' ? 1 : 0),
662
+ failedCount: stat.failedCount + (status === 'failed' ? 1 : 0),
663
+ canceledCount: stat.canceledCount + (status === 'canceled' ? 1 : 0),
664
+ totalWaitingTime: stat.totalWaitingTime + (hasTiming ? waitingTime : 0),
665
+ totalExecutionTime: stat.totalExecutionTime + (hasTiming ? executionTime : 0),
666
+ totalTime: stat.totalTime + (hasTiming ? totalTime : 0),
667
+ timedTaskCount: stat.timedTaskCount + (hasTiming ? 1 : 0),
668
+ byType: mergeKey(stat.byType || {}, task.type, typeIncrement),
669
+ byRunnerType: mergeKey(stat.byRunnerType || {}, task.runnerType, typeIncrement)
670
+ });
671
+ }
672
+ } catch (e) {
673
+ fastify.log.error(`更新每日统计失败: ${e.message}`);
674
+ }
675
+ })();
676
+ };
677
+
678
+ const statistics = {
679
+ getOverview: async ({ range = '7d', timezone, type, runnerType } = {}) => {
680
+ const { Sequelize } = models.task.sequelize;
681
+ const taskModel = models.task;
682
+ const createdAtCol = taskModel.rawAttributes.createdAt.field;
683
+ const dialect = taskModel.sequelize.getDialect();
684
+ const effectiveTimezone = resolveTimezone(timezone);
685
+
686
+ const rangeMap = {
687
+ '7d': { days: 7, label: '近7天' },
688
+ '1m': { days: 30, label: '近1个月' },
689
+ '1y': { days: 365, label: '近1年' }
690
+ };
691
+ const normalizedRange = rangeMap[range] ? range : '7d';
692
+ const rangeConfig = rangeMap[normalizedRange];
693
+ const todayStart = getTodayStart(effectiveTimezone);
694
+ const startDate = new Date(todayStart);
695
+ startDate.setDate(startDate.getDate() - rangeConfig.days);
696
+
697
+ const whereRange = { createdAt: { [Sequelize.Op.gte]: startDate } };
698
+ if (type) {
699
+ whereRange.type = type;
700
+ }
701
+ if (runnerType) {
702
+ whereRange.runnerType = runnerType;
703
+ }
704
+
705
+ // 按时区格式化日期的SQL
706
+ const dateFn = (() => {
707
+ if (!effectiveTimezone || dialect === 'sqlite') {
708
+ return col => Sequelize.fn('DATE', Sequelize.col(col));
709
+ }
710
+ if (dialect === 'postgres') {
711
+ return col => Sequelize.fn('TO_CHAR', Sequelize.literal(`"${col}" AT TIME ZONE '${effectiveTimezone}'`), 'YYYY-MM-DD');
712
+ }
713
+ return col => Sequelize.fn('DATE_FORMAT', Sequelize.fn('CONVERT_TZ', Sequelize.col(col), '+00:00', Sequelize.literal(`'${effectiveTimezone}'`)), '%Y-%m-%d');
714
+ })();
715
+
716
+ // 并行执行所有查询
717
+ const [totalTasks, byStatus, byType, byRunnerType, byTargetType, recentTrend, recentTrendByStatus, recentTrendByType] = await Promise.all([
718
+ taskModel.count({ where: whereRange }),
719
+ taskModel.findAll({
720
+ attributes: ['status', [Sequelize.fn('COUNT', Sequelize.col('id')), 'count']],
721
+ where: whereRange,
722
+ group: ['status'],
723
+ raw: true
724
+ }),
725
+ taskModel.findAll({
726
+ attributes: ['type', [Sequelize.fn('COUNT', Sequelize.col('id')), 'count']],
727
+ where: whereRange,
728
+ group: ['type'],
729
+ raw: true
730
+ }),
731
+ taskModel.findAll({
732
+ attributes: ['runnerType', [Sequelize.fn('COUNT', Sequelize.col('id')), 'count']],
733
+ where: whereRange,
734
+ group: ['runnerType'],
735
+ raw: true
736
+ }),
737
+ taskModel.findAll({
738
+ attributes: ['targetType', [Sequelize.fn('COUNT', Sequelize.col('id')), 'count']],
739
+ where: whereRange,
740
+ group: ['targetType'],
741
+ raw: true
742
+ }),
743
+ taskModel.findAll({
744
+ attributes: [
745
+ [dateFn(createdAtCol), 'date'],
746
+ [Sequelize.fn('COUNT', Sequelize.col('id')), 'count']
747
+ ],
748
+ where: whereRange,
749
+ group: [dateFn(createdAtCol)],
750
+ order: [[dateFn(createdAtCol), 'ASC']],
751
+ raw: true
752
+ }),
753
+ taskModel.findAll({
754
+ attributes: [
755
+ [dateFn(createdAtCol), 'date'],
756
+ 'status',
757
+ [Sequelize.fn('COUNT', Sequelize.col('id')), 'count']
758
+ ],
759
+ where: whereRange,
760
+ group: [dateFn(createdAtCol), 'status'],
761
+ order: [[dateFn(createdAtCol), 'ASC']],
762
+ raw: true
763
+ }),
764
+ taskModel.findAll({
765
+ attributes: [
766
+ [dateFn(createdAtCol), 'date'],
767
+ 'type',
768
+ [Sequelize.fn('COUNT', Sequelize.col('id')), 'count']
769
+ ],
770
+ where: whereRange,
771
+ group: [dateFn(createdAtCol), 'type'],
772
+ order: [[dateFn(createdAtCol), 'ASC']],
773
+ raw: true
774
+ })
775
+ ]);
776
+
777
+ // SQLite 需要在JS层做时区偏移修正DATE结果
778
+ const adjustDate = dateStr => {
779
+ if (!effectiveTimezone || dialect !== 'sqlite') return dateStr;
780
+ return dayjs.utc(dateStr).tz(effectiveTimezone).format('YYYY-MM-DD');
781
+ };
782
+
783
+ // 从预聚合表读取每日耗时统计
784
+ const dailyStats = await models.taskDailyStatistics.findAll({
785
+ where: { date: { [Sequelize.Op.gte]: dayjs(startDate).format('YYYY-MM-DD') } },
786
+ order: [['date', 'ASC']],
787
+ raw: true
788
+ });
789
+
790
+ const durationTrend = dailyStats.map(item => {
791
+ // 如果指定了type/runnerType筛选,从对应子维度中提取数据作为主数据
792
+ const typeData = type && item.byType && item.byType[type] ? item.byType[type] : null;
793
+ const runnerTypeData = runnerType && item.byRunnerType && item.byRunnerType[runnerType] ? item.byRunnerType[runnerType] : null;
794
+ // runnerType优先级高于type(两者同时指定时取交集,但预聚合表无交叉维度,取runnerType)
795
+ const filterData = runnerTypeData || typeData;
796
+ const mainTimedTaskCount = filterData ? filterData.timedTaskCount : item.timedTaskCount;
797
+ const mainTotalWaitingTime = filterData ? filterData.totalWaitingTime : item.totalWaitingTime;
798
+ const mainTotalExecutionTime = filterData ? filterData.totalExecutionTime : item.totalExecutionTime;
799
+ const mainTotalTime = filterData ? filterData.totalTime : item.totalTime;
800
+
801
+ return {
802
+ date: item.date,
803
+ completedCount: filterData ? filterData.count : item.totalCompleted,
804
+ successCount: filterData ? filterData.successCount : item.successCount,
805
+ failedCount: filterData ? filterData.failedCount : item.failedCount,
806
+ canceledCount: filterData ? filterData.canceledCount : item.canceledCount,
807
+ avgWaitingTime: mainTimedTaskCount > 0 ? Math.round(mainTotalWaitingTime / mainTimedTaskCount) : 0,
808
+ avgExecutionTime: mainTimedTaskCount > 0 ? Math.round(mainTotalExecutionTime / mainTimedTaskCount) : 0,
809
+ avgTotalTime: mainTimedTaskCount > 0 ? Math.round(mainTotalTime / mainTimedTaskCount) : 0,
810
+ byType: Object.entries(item.byType || {}).reduce((acc, [key, val]) => {
811
+ if (type && key !== type) return acc;
812
+ acc[key] = {
813
+ count: val.count,
814
+ avgWaitingTime: val.timedTaskCount > 0 ? Math.round(val.totalWaitingTime / val.timedTaskCount) : 0,
815
+ avgExecutionTime: val.timedTaskCount > 0 ? Math.round(val.totalExecutionTime / val.timedTaskCount) : 0,
816
+ avgTotalTime: val.timedTaskCount > 0 ? Math.round(val.totalTime / val.timedTaskCount) : 0
817
+ };
818
+ return acc;
819
+ }, {}),
820
+ byRunnerType: Object.entries(item.byRunnerType || {}).reduce((acc, [key, val]) => {
821
+ if (runnerType && key !== runnerType) return acc;
822
+ acc[key] = {
823
+ count: val.count,
824
+ avgWaitingTime: val.timedTaskCount > 0 ? Math.round(val.totalWaitingTime / val.timedTaskCount) : 0,
825
+ avgExecutionTime: val.timedTaskCount > 0 ? Math.round(val.totalExecutionTime / val.timedTaskCount) : 0,
826
+ avgTotalTime: val.timedTaskCount > 0 ? Math.round(val.totalTime / val.timedTaskCount) : 0
827
+ };
828
+ return acc;
829
+ }, {})
830
+ };
831
+ });
832
+
833
+ return {
834
+ range: normalizedRange,
835
+ rangeLabel: rangeConfig.label,
836
+ totalTasks,
837
+ byStatus: byStatus.reduce((acc, item) => {
838
+ acc[item.status] = Number(item.count);
839
+ return acc;
840
+ }, {}),
841
+ byType: byType.reduce((acc, item) => {
842
+ acc[item.type] = Number(item.count);
843
+ return acc;
844
+ }, {}),
845
+ byRunnerType: byRunnerType.reduce((acc, item) => {
846
+ acc[item.runnerType] = Number(item.count);
847
+ return acc;
848
+ }, {}),
849
+ byTargetType: byTargetType.reduce((acc, item) => {
850
+ acc[item.targetType] = Number(item.count);
851
+ return acc;
852
+ }, {}),
853
+ recentTrend: recentTrend.map(item => ({ date: adjustDate(item.date), count: Number(item.count) })),
854
+ recentTrendByStatus: recentTrendByStatus.map(item => ({ date: adjustDate(item.date), status: item.status, count: Number(item.count) })),
855
+ recentTrendByType: recentTrendByType.map(item => ({ date: adjustDate(item.date), type: item.type, count: Number(item.count) })),
856
+ durationTrend
857
+ };
858
+ },
859
+
860
+ getRealtime: async ({ timezone, type, runnerType } = {}) => {
861
+ const { Sequelize } = models.task.sequelize;
862
+ const taskModel = models.task;
863
+ const createdAtCol = taskModel.rawAttributes.createdAt.field;
864
+ const dialect = taskModel.sequelize.getDialect();
865
+ const effectiveTimezone = resolveTimezone(timezone);
866
+
867
+ const todayStart = getTodayStart(effectiveTimezone);
868
+ const whereToday = { createdAt: { [Sequelize.Op.gte]: todayStart } };
869
+ if (type) {
870
+ whereToday.type = type;
871
+ }
872
+ if (runnerType) {
873
+ whereToday.runnerType = runnerType;
874
+ }
875
+
876
+ // 按小时提取
877
+ const hourExtract = (() => {
878
+ if (!effectiveTimezone || dialect === 'sqlite') {
879
+ return dialect === 'sqlite'
880
+ ? Sequelize.fn('strftime', '%H', Sequelize.col(createdAtCol))
881
+ : Sequelize.fn('EXTRACT', Sequelize.literal(`HOUR FROM "${createdAtCol}"`));
882
+ }
883
+ if (dialect === 'postgres') {
884
+ return Sequelize.fn('EXTRACT', Sequelize.literal(`HOUR FROM "${createdAtCol}" AT TIME ZONE '${effectiveTimezone}'`));
885
+ }
886
+ return Sequelize.fn('EXTRACT', Sequelize.literal(`HOUR FROM CONVERT_TZ("${createdAtCol}", '+00:00', '${effectiveTimezone}')`));
887
+ })();
888
+
889
+ // 15分钟间隔提取
890
+ const intervalExtract = (() => {
891
+ if (dialect === 'sqlite') {
892
+ return Sequelize.literal(`strftime('%H', "${createdAtCol}") || ':' || printf('%02d', CAST(strftime('%M', "${createdAtCol}") AS INTEGER) / 15 * 15)`);
893
+ }
894
+ if (!effectiveTimezone) {
895
+ if (dialect === 'postgres') {
896
+ return Sequelize.literal(`TO_CHAR("${createdAtCol}", 'HH24:') || LPAD((FLOOR(EXTRACT(MINUTE FROM "${createdAtCol}") / 15) * 15)::TEXT, 2, '0')`);
897
+ }
898
+ return Sequelize.literal(`CONCAT(DATE_FORMAT("${createdAtCol}", '%H:'), LPAD(FLOOR(EXTRACT(MINUTE FROM "${createdAtCol}") / 15) * 15, 2, '0'))`);
899
+ }
900
+ if (dialect === 'postgres') {
901
+ return Sequelize.literal(`TO_CHAR("${createdAtCol}" AT TIME ZONE '${effectiveTimezone}', 'HH24:') || LPAD((FLOOR(EXTRACT(MINUTE FROM "${createdAtCol}" AT TIME ZONE '${effectiveTimezone}') / 15) * 15)::TEXT, 2, '0')`);
902
+ }
903
+ return Sequelize.literal(`CONCAT(DATE_FORMAT(CONVERT_TZ("${createdAtCol}", '+00:00', '${effectiveTimezone}'), '%H:'), LPAD(FLOOR(EXTRACT(MINUTE FROM CONVERT_TZ("${createdAtCol}", '+00:00', '${effectiveTimezone}')) / 15) * 15, 2, '0'))`);
904
+ })();
905
+
906
+ const [totalTasks, byStatus, byType, byRunnerType, hourlyTrend, hourlyTrendByStatus, hourlyTrendByType, intervalTrend] = await Promise.all([
907
+ taskModel.count({ where: whereToday }),
908
+ taskModel.findAll({
909
+ attributes: ['status', [Sequelize.fn('COUNT', Sequelize.col('id')), 'count']],
910
+ where: whereToday,
911
+ group: ['status'],
912
+ raw: true
913
+ }),
914
+ taskModel.findAll({
915
+ attributes: ['type', [Sequelize.fn('COUNT', Sequelize.col('id')), 'count']],
916
+ where: whereToday,
917
+ group: ['type'],
918
+ raw: true
919
+ }),
920
+ taskModel.findAll({
921
+ attributes: ['runnerType', [Sequelize.fn('COUNT', Sequelize.col('id')), 'count']],
922
+ where: whereToday,
923
+ group: ['runnerType'],
924
+ raw: true
925
+ }),
926
+ taskModel.findAll({
927
+ attributes: [
928
+ [hourExtract, 'hour'],
929
+ [Sequelize.fn('COUNT', Sequelize.col('id')), 'count']
930
+ ],
931
+ where: whereToday,
932
+ group: [hourExtract],
933
+ order: [[hourExtract, 'ASC']],
934
+ raw: true
935
+ }),
936
+ taskModel.findAll({
937
+ attributes: [
938
+ [hourExtract, 'hour'],
939
+ 'status',
940
+ [Sequelize.fn('COUNT', Sequelize.col('id')), 'count']
941
+ ],
942
+ where: whereToday,
943
+ group: [hourExtract, 'status'],
944
+ order: [[hourExtract, 'ASC'], ['status', 'ASC']],
945
+ raw: true
946
+ }),
947
+ taskModel.findAll({
948
+ attributes: [
949
+ [hourExtract, 'hour'],
950
+ 'type',
951
+ [Sequelize.fn('COUNT', Sequelize.col('id')), 'count']
952
+ ],
953
+ where: whereToday,
954
+ group: [hourExtract, 'type'],
955
+ order: [[hourExtract, 'ASC'], ['type', 'ASC']],
956
+ raw: true
957
+ }),
958
+ taskModel.findAll({
959
+ attributes: [
960
+ [intervalExtract, 'interval'],
961
+ [Sequelize.fn('COUNT', Sequelize.col('id')), 'count']
962
+ ],
963
+ where: whereToday,
964
+ group: [intervalExtract],
965
+ order: [[intervalExtract, 'ASC']],
966
+ raw: true
967
+ })
968
+ ]);
969
+
970
+ // SQLite的hour/interval是UTC,需按timezone偏移修正
971
+ const adjustHour = hour => {
972
+ if (!effectiveTimezone || dialect !== 'sqlite') return Number(hour);
973
+ const utcTime = dayjs.utc().startOf('day').hour(Number(hour));
974
+ return utcTime.tz(effectiveTimezone).hour();
975
+ };
976
+
977
+ const adjustInterval = intervalStr => {
978
+ if (!effectiveTimezone || dialect !== 'sqlite') return intervalStr;
979
+ const [h, m] = intervalStr.split(':').map(Number);
980
+ const utcTime = dayjs.utc().startOf('day').hour(h).minute(m);
981
+ return utcTime.tz(effectiveTimezone).format('HH:mm');
982
+ };
983
+
984
+ return {
985
+ date: formatDate(todayStart, effectiveTimezone),
986
+ totalTasks,
987
+ byStatus: byStatus.reduce((acc, item) => {
988
+ acc[item.status] = Number(item.count);
989
+ return acc;
990
+ }, {}),
991
+ byType: byType.reduce((acc, item) => {
992
+ acc[item.type] = Number(item.count);
993
+ return acc;
994
+ }, {}),
995
+ byRunnerType: byRunnerType.reduce((acc, item) => {
996
+ acc[item.runnerType] = Number(item.count);
997
+ return acc;
998
+ }, {}),
999
+ hourlyTrend: hourlyTrend.map(item => ({ hour: adjustHour(item.hour), count: Number(item.count) })),
1000
+ hourlyTrendByStatus: hourlyTrendByStatus.map(item => ({
1001
+ hour: adjustHour(item.hour),
1002
+ status: item.status,
1003
+ count: Number(item.count)
1004
+ })),
1005
+ hourlyTrendByType: hourlyTrendByType.map(item => ({
1006
+ hour: adjustHour(item.hour),
1007
+ type: item.type,
1008
+ count: Number(item.count)
1009
+ })),
1010
+ intervalTrend: intervalTrend.map(item => ({ interval: adjustInterval(item.interval), count: Number(item.count) })),
1011
+ todayDuration: await (async () => {
1012
+ const todayStat = await models.taskDailyStatistics.findOne({
1013
+ where: { date: dayjs(todayStart).format('YYYY-MM-DD') },
1014
+ raw: true
1015
+ });
1016
+ if (!todayStat) {
1017
+ return { completedCount: 0, avgWaitingTime: 0, avgExecutionTime: 0, avgTotalTime: 0, byType: {}, byRunnerType: {} };
1018
+ }
1019
+ // 如果指定了type/runnerType筛选,从对应子维度中提取数据作为主数据
1020
+ const typeData = type && todayStat.byType && todayStat.byType[type] ? todayStat.byType[type] : null;
1021
+ const runnerTypeData = runnerType && todayStat.byRunnerType && todayStat.byRunnerType[runnerType] ? todayStat.byRunnerType[runnerType] : null;
1022
+ const filterData = runnerTypeData || typeData;
1023
+ const mainTimedTaskCount = filterData ? filterData.timedTaskCount : todayStat.timedTaskCount;
1024
+ const mainTotalWaitingTime = filterData ? filterData.totalWaitingTime : todayStat.totalWaitingTime;
1025
+ const mainTotalExecutionTime = filterData ? filterData.totalExecutionTime : todayStat.totalExecutionTime;
1026
+ const mainTotalTime = filterData ? filterData.totalTime : todayStat.totalTime;
1027
+
1028
+ return {
1029
+ completedCount: filterData ? filterData.count : todayStat.totalCompleted,
1030
+ successCount: filterData ? filterData.successCount : todayStat.successCount,
1031
+ failedCount: filterData ? filterData.failedCount : todayStat.failedCount,
1032
+ canceledCount: filterData ? filterData.canceledCount : todayStat.canceledCount,
1033
+ avgWaitingTime: mainTimedTaskCount > 0 ? Math.round(mainTotalWaitingTime / mainTimedTaskCount) : 0,
1034
+ avgExecutionTime: mainTimedTaskCount > 0 ? Math.round(mainTotalExecutionTime / mainTimedTaskCount) : 0,
1035
+ avgTotalTime: mainTimedTaskCount > 0 ? Math.round(mainTotalTime / mainTimedTaskCount) : 0,
1036
+ byType: Object.entries(todayStat.byType || {}).reduce((acc, [key, val]) => {
1037
+ if (type && key !== type) return acc;
1038
+ acc[key] = {
1039
+ count: val.count,
1040
+ avgWaitingTime: val.timedTaskCount > 0 ? Math.round(val.totalWaitingTime / val.timedTaskCount) : 0,
1041
+ avgExecutionTime: val.timedTaskCount > 0 ? Math.round(val.totalExecutionTime / val.timedTaskCount) : 0,
1042
+ avgTotalTime: val.timedTaskCount > 0 ? Math.round(val.totalTime / val.timedTaskCount) : 0
1043
+ };
1044
+ return acc;
1045
+ }, {}),
1046
+ byRunnerType: Object.entries(todayStat.byRunnerType || {}).reduce((acc, [key, val]) => {
1047
+ if (runnerType && key !== runnerType) return acc;
1048
+ acc[key] = {
1049
+ count: val.count,
1050
+ avgWaitingTime: val.timedTaskCount > 0 ? Math.round(val.totalWaitingTime / val.timedTaskCount) : 0,
1051
+ avgExecutionTime: val.timedTaskCount > 0 ? Math.round(val.totalExecutionTime / val.timedTaskCount) : 0,
1052
+ avgTotalTime: val.timedTaskCount > 0 ? Math.round(val.totalTime / val.timedTaskCount) : 0
1053
+ };
1054
+ return acc;
1055
+ }, {})
1056
+ };
1057
+ })()
1058
+ };
1059
+ }
1060
+ };
1061
+
505
1062
  Object.assign(fastify[options.name].services, {
506
1063
  create,
507
1064
  detail,
@@ -512,10 +1069,13 @@ module.exports = fp(async (fastify, options) => {
512
1069
  resetAll,
513
1070
  retry,
514
1071
  log,
1072
+ logWithSignature,
515
1073
  callback,
1074
+ callbackWithSignature,
516
1075
  executor,
517
1076
  processNext,
518
1077
  processSystemTask,
519
- waitingComplete
1078
+ waitingComplete,
1079
+ statistics
520
1080
  });
521
1081
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-task",
3
- "version": "1.1.1",
3
+ "version": "2.0.0",
4
4
  "description": "一个 Fastify 插件,用于任务编排、追踪任务执行状态和结果。支持系统自动执行和手动执行两种模式,提供完整的任务生命周期管理",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -56,6 +56,7 @@
56
56
  "sqlite3": "^5.1.7"
57
57
  },
58
58
  "dependencies": {
59
+ "dayjs": "^1.11.13",
59
60
  "fastify-cron": "^1.4.0",
60
61
  "fs-extra": "^11.3.0"
61
62
  }