@kne/fastify-task 1.1.2 → 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.
@@ -272,4 +272,173 @@ module.exports = fp(async (fastify, options) => {
272
272
  return await services.callbackWithSignature(Object.assign({}, request.body, { taskId: request.query.taskId }));
273
273
  }
274
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());
442
+ }
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
 
@@ -518,6 +539,526 @@ module.exports = fp(async (fastify, options) => {
518
539
  return callback({ id, code, data, message });
519
540
  };
520
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
+
521
1062
  Object.assign(fastify[options.name].services, {
522
1063
  create,
523
1064
  detail,
@@ -534,6 +1075,7 @@ module.exports = fp(async (fastify, options) => {
534
1075
  executor,
535
1076
  processNext,
536
1077
  processSystemTask,
537
- waitingComplete
1078
+ waitingComplete,
1079
+ statistics
538
1080
  });
539
1081
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-task",
3
- "version": "1.1.2",
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
  }