@kne/fastify-task 3.0.0 → 3.0.2

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.
@@ -17,6 +17,8 @@ const createContext = (fastify, options) => {
17
17
  if (task.reload && typeof task.reload === 'function') {
18
18
  await task.reload();
19
19
  }
20
+ // errorHandler 重试会把任务改回 pending,此时任务未结束,等最终结束时再采集
21
+ if (!TERMINAL_STATUSES.includes(task.status)) return;
20
22
  const completedAt = task.completedAt || new Date();
21
23
  const createdAt = task.createdAt;
22
24
  const startedAt = task.startedAt;
@@ -52,20 +52,46 @@ const taskList = {
52
52
  }
53
53
  };
54
54
 
55
+ const DURATION_ITEM = '每项为 { count, avgWaitingTime, avgExecutionTime, avgTotalTime }';
56
+
57
+ const bucket = obj('趋势桶', {
58
+ key: str('桶 key:YYYY-MM-DD(日 / 周起始日)或 YYYY-MM(月)'),
59
+ start: any('桶起点(含)'),
60
+ end: any('桶终点(不含);最后一个桶为当前时间')
61
+ });
62
+
63
+ const duration = obj('范围内耗时汇总;查询异常时为 null', {
64
+ completedCount: num('已结束任务数'),
65
+ successCount: num('成功任务数'),
66
+ failedCount: num('失败任务数'),
67
+ canceledCount: num('取消任务数'),
68
+ avgWaitingTime: num('平均等待时间(毫秒),无耗时数据时为 0'),
69
+ avgExecutionTime: num('平均执行时间(毫秒),无耗时数据时为 0'),
70
+ avgTotalTime: num('平均总耗时(毫秒),无耗时数据时为 0'),
71
+ byType: obj(`按任务类型拆分,键为任务类型,${DURATION_ITEM}`),
72
+ byRunnerType: obj(`按执行方式拆分,键为 manual / system,${DURATION_ITEM}`),
73
+ byTypeByRunnerType: obj(`先按执行方式再按任务类型拆分,结构为 { [runnerType]: { [type]: 耗时项 } },${DURATION_ITEM}`)
74
+ });
75
+
55
76
  const statistics = {
56
77
  type: 'object',
57
78
  additionalProperties: true,
58
79
  properties: {
59
80
  range: str('时间范围:7d / 1m / 3m / 1y'),
60
81
  rangeLabel: str('时间范围名称,如「近7天」'),
82
+ granularity: str('趋势粒度:7d / 1m 为 day,3m 为 week(周一起),1y 为 month'),
83
+ startTime: any('统计起始时间(第一个桶的起点)'),
84
+ endTime: any('统计截止时间(当前时间)'),
85
+ buckets: list('趋势桶列表,按时间正序', bucket),
61
86
  totalTasks: num('范围内已结束(成功、失败、取消)的任务总数'),
62
87
  byStatus: any('按状态统计的任务数,键为 success / failed / canceled'),
63
88
  byType: any('按任务类型统计的任务数,键为任务类型'),
64
89
  byRunnerType: any('按执行方式统计的任务数,键为 manual / system'),
65
- recentTrend: any('每日趋势,每项为 { date, count }'),
66
- recentTrendByStatus: any('每日按状态趋势,每项为 { date, status, count }'),
67
- recentTrendByType: any('每日按类型趋势,每项为 { date, type, count }'),
68
- durationTrend: any('每日耗时趋势,每项含 date、完成/成功/失败数及平均等待、执行、总耗时(毫秒)'),
90
+ recentTrend: any('按桶趋势,每项为 { date, count },date 为桶 key,无数据的桶补 0'),
91
+ recentTrendByStatus: any('按桶按状态趋势,每项为 { date, status, count },每个桶都含 success / failed / canceled'),
92
+ recentTrendByType: any('按桶按类型趋势,每项为 { date, type, count }'),
93
+ durationTrend: any('按桶耗时趋势,每项含 date(桶 key)、完成/成功/失败数及平均等待、执行、总耗时(毫秒)'),
94
+ duration,
69
95
  hourlyTrend: any('按小时趋势,每项为 { date, hour, total, success, failed, canceled };查询失败或无数据时不返回'),
70
96
  hourlyCompletionTrend: any('按小时、类型、执行方式的完成明细,每项含 date、hour、type、runnerType 及完成/成功/失败/取消数')
71
97
  }
@@ -63,12 +63,17 @@ module.exports = fp(async (fastify, options) => {
63
63
  return fastify[options.name].services.log(Object.assign({}, props, { taskId: task.id }));
64
64
  };
65
65
 
66
- const createTimeout = ms => new Promise((_, reject) => setTimeout(() => reject(new Error(`任务执行超时(${ms}ms)`)), ms));
66
+ let timeoutTimer;
67
+ const createTimeout = ms =>
68
+ new Promise((_, reject) => {
69
+ timeoutTimer = setTimeout(() => reject(new Error(`任务执行超时(${ms}ms)`)), ms);
70
+ });
67
71
 
68
72
  const executorPromise = context.executor({ type: task.type, scriptName: task.scriptName, task, log: addLog });
69
73
  const racePromise = options.taskTimeout > 0 ? Promise.race([executorPromise, createTimeout(options.taskTimeout)]) : executorPromise;
70
74
 
71
75
  return racePromise
76
+ .finally(() => clearTimeout(timeoutTimer))
72
77
  .then(async result => {
73
78
  if (result === false) {
74
79
  return;
@@ -6,29 +6,56 @@ const timezone = require('dayjs/plugin/timezone');
6
6
  dayjs.extend(utc);
7
7
  dayjs.extend(timezone);
8
8
 
9
+ // granularity × count 决定趋势桶:period-stat 的 d/w 只保留当年,m 永久保留,所以 1y 必须按月分桶
9
10
  const RANGE_MAP = {
10
- '7d': { value: 7, unit: 'day', label: '近7天' },
11
- '1m': { value: 1, unit: 'month', label: '近1个月' },
12
- '3m': { value: 3, unit: 'month', label: '近3个月' },
13
- '1y': { value: 1, unit: 'year', label: '近1年' }
11
+ '7d': { granularity: 'day', count: 7, label: '近7天' },
12
+ '1m': { granularity: 'day', count: 30, label: '近1个月' },
13
+ '3m': { granularity: 'week', count: 13, label: '近3个月' },
14
+ '1y': { granularity: 'month', count: 12, label: '近1年' }
14
15
  };
15
16
 
17
+ const BUCKET_KEY_FORMAT = { day: 'YYYY-MM-DD', week: 'YYYY-MM-DD', month: 'YYYY-MM' };
18
+ const BUCKET_QUERY_CONCURRENCY = 6;
19
+
16
20
  const RUNNER_TYPES = ['system', 'manual'];
17
21
  const STAT_ATTRS = ['total', 'success', 'failed', 'canceled', 'waitingTime', 'executionTime', 'totalTime'];
22
+ const COMPLETED_STATUSES = ['success', 'failed', 'canceled'];
23
+
24
+ // 周以周一为起点,与 fastify-statistics 的 w 周期对齐
25
+ const startOfBucket = (d, granularity) => {
26
+ if (granularity === 'month') return d.startOf('month');
27
+ if (granularity === 'week') return d.subtract((d.day() + 6) % 7, 'day').startOf('day');
28
+ return d.startOf('day');
29
+ };
18
30
 
19
- // 查询用客户端时间:parseRange 使用客户端时区计算日期范围
20
- const parseRange = (range = '7d', tz) => {
31
+ // 查询用客户端时间:桶边界按客户端时区计算,最后一个桶截止到当前时间
32
+ const buildRangeBuckets = (range = '7d', tz) => {
21
33
  const config = RANGE_MAP[range];
22
34
  if (!config) {
23
35
  throw new Error(`不支持的时间范围: ${range}, 支持: ${Object.keys(RANGE_MAP).join(',')}`);
24
36
  }
25
- // 写入数据全使用服务器时间,查询用客户端时间
26
- // 使用客户端时区计算"现在"和"起始时间",确保日期边界对齐客户端视角
37
+ const { granularity, count, label } = config;
27
38
  const now = tz ? dayjs().tz(tz) : dayjs();
28
- const startTime = config.unit === 'day'
29
- ? now.subtract(config.value, config.unit).startOf('day')
30
- : now.subtract(config.value, config.unit);
31
- return { startTime: startTime.toDate(), endTime: now.toDate(), label: config.label, range };
39
+ const first = startOfBucket(now, granularity).subtract(count - 1, granularity);
40
+ const buckets = Array.from({ length: count }, (_, i) => {
41
+ const start = first.add(i, granularity);
42
+ const end = i === count - 1 ? now : start.add(1, granularity);
43
+ return { key: start.format(BUCKET_KEY_FORMAT[granularity]), start: start.toDate(), end: end.toDate() };
44
+ });
45
+ return { range, label, granularity, buckets, startTime: buckets[0].start, endTime: now.toDate() };
46
+ };
47
+
48
+ const mapWithConcurrency = async (items, limit, fn) => {
49
+ const results = new Array(items.length);
50
+ let cursor = 0;
51
+ const worker = async () => {
52
+ while (cursor < items.length) {
53
+ const index = cursor++;
54
+ results[index] = await fn(items[index], index);
55
+ }
56
+ };
57
+ await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
58
+ return results;
32
59
  };
33
60
 
34
61
  // 查询用客户端时间:formatDate 使用客户端时区格式化日期
@@ -52,7 +79,7 @@ const getZonedChannelHour = (periodTime, serverHour, tz) => {
52
79
 
53
80
  const safeNum = v => {
54
81
  const n = Number(v);
55
- return (typeof n === 'number' && !isNaN(n) ? n : 0);
82
+ return typeof n === 'number' && !isNaN(n) ? n : 0;
56
83
  };
57
84
 
58
85
  const getSumData = item => (item.data || {}).sum || item.data || {};
@@ -124,9 +151,17 @@ const createDailyMapEntry = () => ({ total: 0, byStatus: { success: 0, failed: 0
124
151
 
125
152
  // 创建空的 dailyDurationMap 条目
126
153
  const createDailyDurationEntry = () => ({
127
- completedCount: 0, successCount: 0, failedCount: 0, canceledCount: 0,
128
- sumWaiting: 0, sumExecution: 0, sumTotal: 0, countWithTiming: 0,
129
- byType: {}, byRunnerType: {}, byTypeByRunnerType: {}
154
+ completedCount: 0,
155
+ successCount: 0,
156
+ failedCount: 0,
157
+ canceledCount: 0,
158
+ sumWaiting: 0,
159
+ sumExecution: 0,
160
+ sumTotal: 0,
161
+ countWithTiming: 0,
162
+ byType: {},
163
+ byRunnerType: {},
164
+ byTypeByRunnerType: {}
130
165
  });
131
166
 
132
167
  const createHourlyAccumulator = () => ({
@@ -168,18 +203,9 @@ const mergeHourlyItem = (result, { date, hour, typeName, runnerTypeName, count,
168
203
  }
169
204
  };
170
205
 
171
- // 查询 statistics 并解析结果
206
+ // 解析 statistics 结果;resolveDate 决定条目归入哪个日期/桶,默认按客户端时区取条目日期
172
207
  // 写入数据全使用服务器时间(DB 中存储的是服务器时间),查询用客户端时间转换展示
173
- const queryAndParse = async (statisticsServices, { channels, startTime, endTime, timezone: tz, runnerType: runnerTypeFilter }) => {
174
- if (channels.length === 0) return null;
175
-
176
- const statResult = await statisticsServices.query({
177
- channels, startTime, endTime,
178
- attributeNames: STAT_ATTRS,
179
- aggregates: ['sum'],
180
- timezone: tz || undefined
181
- });
182
-
208
+ const parseStatList = (list, { timezone: tz, runnerType: runnerTypeFilter, resolveDate }) => {
183
209
  const result = {
184
210
  totalTasks: 0,
185
211
  byStatus: { success: 0, failed: 0, canceled: 0 },
@@ -191,28 +217,39 @@ const queryAndParse = async (statisticsServices, { channels, startTime, endTime,
191
217
  hourlyTypeMap: {},
192
218
  hourlyDetailMap: {},
193
219
  duration: {
194
- completedCount: 0, successCount: 0, failedCount: 0, canceledCount: 0,
195
- sumWaiting: 0, sumExecution: 0, sumTotal: 0, countWithTiming: 0,
196
- byType: {}, byRunnerType: {}, byTypeByRunnerType: {}
220
+ completedCount: 0,
221
+ successCount: 0,
222
+ failedCount: 0,
223
+ canceledCount: 0,
224
+ sumWaiting: 0,
225
+ sumExecution: 0,
226
+ sumTotal: 0,
227
+ countWithTiming: 0,
228
+ byType: {},
229
+ byRunnerType: {},
230
+ byTypeByRunnerType: {}
197
231
  },
198
232
  durationTrend: []
199
233
  };
200
234
  const dailyDurationMap = {};
201
235
 
202
- for (const item of statResult.list || []) {
236
+ for (const item of list || []) {
203
237
  const { type: typeName, runnerType: runnerTypeName } = parseChannel(item.channel);
204
- if (runnerTypeFilter && runnerTypeName && runnerTypeName !== runnerTypeFilter) continue;
205
- if (runnerTypeFilter && !runnerTypeName) continue;
238
+ if (runnerTypeFilter && runnerTypeName !== runnerTypeFilter) continue;
206
239
 
207
240
  const sum = getSumData(item);
208
- const count = safeNum(sum.total);
241
+ // 任务数只认终态:旧版本在失败重试(任务回到 pending)时也会采集一条,total 里混有这类非终态记录
242
+ const count = COMPLETED_STATUSES.reduce((n, status) => n + safeNum(sum[status]), 0);
243
+ // 耗时累计值包含上述记录,均值分母须用同口径的 total
244
+ const timingCount = safeNum(sum.total);
209
245
  const isHourly = item.period === 'h';
210
246
  // 查询用客户端时间:日期和小时使用客户端时区转换
211
- const date = formatDate(item.time, tz);
247
+ const date = resolveDate ? resolveDate(item) : formatDate(item.time, tz);
212
248
  const hour = isHourly ? getHour(item.time, tz) : null;
213
249
 
214
250
  // ─── 1-segment channel: 全局 + 按日 + 时长 聚合 ───
215
- if (!runnerTypeName) {
251
+ // 按 runnerType 筛选时只查询了 2 段 channel,汇总改由匹配的 2 段 channel 提供
252
+ if (runnerTypeFilter || !runnerTypeName) {
216
253
  // 全局汇总
217
254
  result.totalTasks += count;
218
255
  result.byStatus.success += safeNum(sum.success);
@@ -224,12 +261,12 @@ const queryAndParse = async (statisticsServices, { channels, startTime, endTime,
224
261
  result.duration.completedCount += count;
225
262
  result.duration.successCount += safeNum(sum.success);
226
263
  result.duration.failedCount += safeNum(sum.failed);
227
- result.duration.canceled += safeNum(sum.canceled);
264
+ result.duration.canceledCount += safeNum(sum.canceled);
228
265
  result.duration.sumWaiting += safeNum(sum.waitingTime);
229
266
  result.duration.sumExecution += safeNum(sum.executionTime);
230
267
  result.duration.sumTotal += safeNum(sum.totalTime);
231
- result.duration.countWithTiming += count;
232
- if (count > 0) accumDuration(ensureDurationAcc(result.duration.byType, typeName), count, sum);
268
+ result.duration.countWithTiming += timingCount;
269
+ if (timingCount > 0) accumDuration(ensureDurationAcc(result.duration.byType, typeName), timingCount, sum);
233
270
 
234
271
  // 按日汇总
235
272
  if (!result.dailyMap[date]) result.dailyMap[date] = createDailyMapEntry();
@@ -250,8 +287,8 @@ const queryAndParse = async (statisticsServices, { channels, startTime, endTime,
250
287
  dd.sumWaiting += safeNum(sum.waitingTime);
251
288
  dd.sumExecution += safeNum(sum.executionTime);
252
289
  dd.sumTotal += safeNum(sum.totalTime);
253
- dd.countWithTiming += count;
254
- if (count > 0) accumDuration(ensureDurationAcc(dd.byType, typeName), count, sum);
290
+ dd.countWithTiming += timingCount;
291
+ if (timingCount > 0) accumDuration(ensureDurationAcc(dd.byType, typeName), timingCount, sum);
255
292
 
256
293
  // 小时级趋势(仅 period='h')
257
294
  if (isHourly) {
@@ -263,29 +300,20 @@ const queryAndParse = async (statisticsServices, { channels, startTime, endTime,
263
300
  if (runnerTypeName) {
264
301
  result.byRunnerType[runnerTypeName] = (result.byRunnerType[runnerTypeName] || 0) + count;
265
302
 
266
- if (count > 0) {
267
- accumDuration(ensureDurationAcc(result.duration.byRunnerType, runnerTypeName), count, sum);
303
+ if (timingCount > 0) {
304
+ accumDuration(ensureDurationAcc(result.duration.byRunnerType, runnerTypeName), timingCount, sum);
268
305
 
269
- // byTypeByRunnerType(覆盖写入,非累加)
270
306
  if (!result.duration.byTypeByRunnerType[runnerTypeName]) result.duration.byTypeByRunnerType[runnerTypeName] = {};
271
- result.duration.byTypeByRunnerType[runnerTypeName][typeName] = {
272
- count,
273
- sumWaiting: safeNum(sum.waitingTime),
274
- sumExecution: safeNum(sum.executionTime),
275
- sumTotal: safeNum(sum.totalTime)
276
- };
277
-
278
- // 按日 byRunnerType 时长
279
- if (dailyDurationMap[date]) {
280
- accumDuration(ensureDurationAcc(dailyDurationMap[date].byRunnerType, runnerTypeName), count, sum);
307
+ accumDuration(ensureDurationAcc(result.duration.byTypeByRunnerType[runnerTypeName], typeName), timingCount, sum);
308
+
309
+ // 按日 byRunnerType 时长(同一时间点的 1 段 / 2 段 channel 顺序不固定,不能依赖前者先建条目)
310
+ if (!dailyDurationMap[date]) dailyDurationMap[date] = createDailyDurationEntry();
311
+ {
312
+ accumDuration(ensureDurationAcc(dailyDurationMap[date].byRunnerType, runnerTypeName), timingCount, sum);
281
313
  if (!dailyDurationMap[date].byTypeByRunnerType[runnerTypeName]) {
282
314
  dailyDurationMap[date].byTypeByRunnerType[runnerTypeName] = {};
283
315
  }
284
- accumDuration(
285
- ensureDurationAcc(dailyDurationMap[date].byTypeByRunnerType[runnerTypeName], typeName),
286
- count,
287
- sum
288
- );
316
+ accumDuration(ensureDurationAcc(dailyDurationMap[date].byTypeByRunnerType[runnerTypeName], typeName), timingCount, sum);
289
317
  }
290
318
  }
291
319
 
@@ -310,23 +338,51 @@ const queryAndParse = async (statisticsServices, { channels, startTime, endTime,
310
338
  avgTotalTime: computeAvg(dd.sumTotal, dd.countWithTiming),
311
339
  byType: Object.fromEntries(Object.entries(dd.byType).map(([n, td]) => [n, toAvgDuration(td)])),
312
340
  byRunnerType: Object.fromEntries(Object.entries(dd.byRunnerType).map(([n, rt]) => [n, toAvgDuration(rt)])),
313
- byTypeByRunnerType: Object.fromEntries(
314
- Object.entries(dd.byTypeByRunnerType).map(([rtName, types]) => [
315
- rtName,
316
- Object.fromEntries(Object.entries(types).map(([typeName, td]) => [typeName, toAvgDuration(td)]))
317
- ])
318
- )
341
+ byTypeByRunnerType: Object.fromEntries(Object.entries(dd.byTypeByRunnerType).map(([rtName, types]) => [rtName, Object.fromEntries(Object.entries(types).map(([typeName, td]) => [typeName, toAvgDuration(td)]))]))
319
342
  }));
320
343
 
321
344
  return result;
322
345
  };
323
346
 
347
+ const queryAndParse = async (statisticsServices, { channels, startTime, endTime, timezone: tz, runnerType }) => {
348
+ if (channels.length === 0) return null;
349
+ const statResult = await statisticsServices.query({
350
+ channels,
351
+ startTime,
352
+ endTime,
353
+ attributeNames: STAT_ATTRS,
354
+ aggregates: ['sum'],
355
+ timezone: tz || undefined
356
+ });
357
+ return parseStatList(statResult.list, { timezone: tz, runnerType });
358
+ };
359
+
360
+ // fastify-statistics 会把区间拆成能对齐的最大周期(y/q/m/w/d/h),单次查询无法按日/周/月拆分,
361
+ // 因此逐桶查询,并把条目归到所属桶的 key 上
362
+ const queryBucketedAndParse = async (statisticsServices, { channels, buckets, timezone: tz, runnerType }) => {
363
+ if (channels.length === 0) return null;
364
+ const lists = await mapWithConcurrency(buckets, BUCKET_QUERY_CONCURRENCY, async bucket => {
365
+ const statResult = await statisticsServices.query({
366
+ channels,
367
+ startTime: bucket.start,
368
+ endTime: bucket.end,
369
+ attributeNames: STAT_ATTRS,
370
+ aggregates: ['sum'],
371
+ timezone: tz || undefined
372
+ });
373
+ return (statResult.list || []).map(item => ({ ...item, bucketKey: bucket.key }));
374
+ });
375
+ return parseStatList(lists.flat(), { timezone: tz, runnerType, resolveDate: item => item.bucketKey });
376
+ };
377
+
324
378
  const queryHourlyChildChannels = async (statisticsServices, { channels, startTime, endTime, timezone: tz, runnerType: runnerTypeFilter }) => {
325
379
  const hourlyChannels = buildHourlyChannels(channels, runnerTypeFilter);
326
380
  if (hourlyChannels.length === 0) return null;
327
381
 
328
382
  const statResult = await statisticsServices.query({
329
- channels: hourlyChannels, startTime, endTime,
383
+ channels: hourlyChannels,
384
+ startTime,
385
+ endTime,
330
386
  attributeNames: STAT_ATTRS,
331
387
  aggregates: ['sum'],
332
388
  timezone: tz || undefined
@@ -355,7 +411,7 @@ const queryHourlyChildChannels = async (statisticsServices, { channels, startTim
355
411
  };
356
412
 
357
413
  // 构建 todayDuration 结果
358
- const buildDurationResult = (d) => ({
414
+ const buildDurationResult = d => ({
359
415
  completedCount: d.completedCount,
360
416
  successCount: d.successCount,
361
417
  failedCount: d.failedCount,
@@ -365,16 +421,11 @@ const buildDurationResult = (d) => ({
365
421
  avgTotalTime: computeAvg(d.sumTotal, d.countWithTiming),
366
422
  byType: Object.fromEntries(Object.entries(d.byType).map(([n, td]) => [n, toAvgDuration(td)])),
367
423
  byRunnerType: Object.fromEntries(Object.entries(d.byRunnerType).map(([n, rt]) => [n, toAvgDuration(rt)])),
368
- byTypeByRunnerType: Object.fromEntries(
369
- Object.entries(d.byTypeByRunnerType).map(([rtName, types]) => [
370
- rtName,
371
- Object.fromEntries(Object.entries(types).map(([typeName, td]) => [typeName, toAvgDuration(td)]))
372
- ])
373
- )
424
+ byTypeByRunnerType: Object.fromEntries(Object.entries(d.byTypeByRunnerType).map(([rtName, types]) => [rtName, Object.fromEntries(Object.entries(types).map(([typeName, td]) => [typeName, toAvgDuration(td)]))]))
374
425
  });
375
426
 
376
427
  // 构建小时趋势数组(完整格式,供 history 接口使用)
377
- const buildHourlyTrendArrays = (parsed) => {
428
+ const buildHourlyTrendArrays = parsed => {
378
429
  const hourlyTrend = [];
379
430
  const hourlyTrendByStatus = [];
380
431
  const hourlyTrendByType = [];
@@ -387,17 +438,13 @@ const buildHourlyTrendArrays = (parsed) => {
387
438
  const typeMap = parsed.hourlyTypeMap[date] || {};
388
439
  const detailMap = parsed.hourlyDetailMap[date] || {};
389
440
 
390
- const allHours = [...new Set([
391
- ...Object.keys(hoursMap).map(Number),
392
- ...Object.keys(statusMap).map(Number),
393
- ...Object.keys(typeMap).map(Number),
394
- ...Object.keys(detailMap).map(Number)
395
- ])].sort((a, b) => a - b);
441
+ const allHours = [...new Set([...Object.keys(hoursMap).map(Number), ...Object.keys(statusMap).map(Number), ...Object.keys(typeMap).map(Number), ...Object.keys(detailMap).map(Number)])].sort((a, b) => a - b);
396
442
 
397
443
  for (const hour of allHours) {
398
444
  const statuses = statusMap[hour] || {};
399
445
  hourlyTrend.push({
400
- date, hour,
446
+ date,
447
+ hour,
401
448
  total: hoursMap[hour] || 0,
402
449
  success: statuses.success || 0,
403
450
  failed: statuses.failed || 0,
@@ -412,13 +459,18 @@ const buildHourlyTrendArrays = (parsed) => {
412
459
  }
413
460
  }
414
461
 
415
- for (const hour of Object.keys(detailMap).map(Number).sort((a, b) => a - b)) {
462
+ for (const hour of Object.keys(detailMap)
463
+ .map(Number)
464
+ .sort((a, b) => a - b)) {
416
465
  const types = detailMap[hour] || {};
417
466
  for (const [typeName, runnerTypes] of Object.entries(types)) {
418
467
  for (const [runnerTypeName, detail] of Object.entries(runnerTypes)) {
419
468
  if (detail.total > 0) {
420
469
  hourlyCompletionTrend.push({
421
- date, hour, type: typeName, runnerType: runnerTypeName,
470
+ date,
471
+ hour,
472
+ type: typeName,
473
+ runnerType: runnerTypeName,
422
474
  totalCompleted: detail.total,
423
475
  successCount: detail.success,
424
476
  failedCount: detail.failed,
@@ -433,14 +485,16 @@ const buildHourlyTrendArrays = (parsed) => {
433
485
  return { hourlyTrend, hourlyTrendByStatus, hourlyTrendByType, hourlyCompletionTrend };
434
486
  };
435
487
 
436
- // 构建历史日趋势数组
437
- const buildDailyTrendArrays = (parsed) => {
438
- const sorted = Object.entries(parsed.dailyMap).sort(([a], [b]) => a.localeCompare(b));
439
- const recentTrend = sorted.map(([date, d]) => ({ date, count: d.total }));
488
+ // 构建历史趋势数组:按桶 key 输出,无数据的桶补 0,保证时间轴连续
489
+ const buildDailyTrendArrays = (parsed, bucketKeys) => {
490
+ const keys = bucketKeys || Object.keys(parsed.dailyMap).sort();
491
+ const recentTrend = [];
440
492
  const recentTrendByStatus = [];
441
493
  const recentTrendByType = [];
442
- sorted.forEach(([date, d]) => {
443
- Object.entries(d.byStatus).forEach(([status, count]) => recentTrendByStatus.push({ date, status, count }));
494
+ keys.forEach(date => {
495
+ const d = parsed.dailyMap[date] || createDailyMapEntry();
496
+ recentTrend.push({ date, count: d.total });
497
+ COMPLETED_STATUSES.forEach(status => recentTrendByStatus.push({ date, status, count: d.byStatus[status] || 0 }));
444
498
  Object.entries(d.byType).forEach(([type, count]) => recentTrendByType.push({ date, type, count }));
445
499
  });
446
500
  return { recentTrend, recentTrendByStatus, recentTrendByType };
@@ -453,32 +507,50 @@ module.exports = fp(async (fastify, options) => {
453
507
 
454
508
  // ─── queryStatistics (history 接口) ───
455
509
  const queryStatistics = async ({ range = '7d', timezone, type, runnerType }) => {
456
- // 查询用客户端时间:parseRange 传入 timezone
457
- const { startTime, endTime, label, range: rangeKey } = parseRange(range, timezone);
510
+ // 查询用客户端时间:桶边界按 timezone 计算
511
+ const { startTime, endTime, label, range: rangeKey, granularity, buckets } = buildRangeBuckets(range, timezone);
512
+ const bucketKeys = buckets.map(b => b.key);
513
+ const rangeMeta = {
514
+ range: rangeKey,
515
+ rangeLabel: label,
516
+ granularity,
517
+ startTime,
518
+ endTime,
519
+ buckets: buckets.map(({ key, start, end }) => ({ key, start, end }))
520
+ };
458
521
  const emptyResult = {
459
- range: rangeKey, rangeLabel: label,
460
- totalTasks: 0, byStatus: {}, byType: {}, byRunnerType: {},
461
- recentTrend: [], recentTrendByStatus: [], recentTrendByType: [],
462
- durationTrend: [], hourlyCompletionTrend: []
522
+ ...rangeMeta,
523
+ totalTasks: 0,
524
+ byStatus: {},
525
+ byType: {},
526
+ byRunnerType: {},
527
+ ...buildDailyTrendArrays({ dailyMap: {} }, bucketKeys),
528
+ durationTrend: [],
529
+ duration: null,
530
+ hourlyCompletionTrend: []
463
531
  };
464
532
  try {
465
533
  const channels = await buildChannels(statisticsServices, type, runnerType);
466
- const parsed = await queryAndParse(statisticsServices, { channels, startTime, endTime, timezone, runnerType });
534
+ const parsed = await queryBucketedAndParse(statisticsServices, { channels, buckets, timezone, runnerType });
467
535
  if (!parsed) return emptyResult;
468
536
  const hourlyParsed = await queryHourlyChildChannels(statisticsServices, { channels, startTime, endTime, timezone, runnerType });
469
537
 
470
- const { recentTrend, recentTrendByStatus, recentTrendByType } = buildDailyTrendArrays(parsed);
538
+ const { recentTrend, recentTrendByStatus, recentTrendByType } = buildDailyTrendArrays(parsed, bucketKeys);
471
539
  const { hourlyTrend, hourlyCompletionTrend } = buildHourlyTrendArrays(hourlyParsed || createHourlyAccumulator());
472
540
 
473
541
  return {
474
- range: rangeKey, rangeLabel: label,
542
+ ...rangeMeta,
475
543
  totalTasks: parsed.totalTasks,
476
544
  byStatus: parsed.byStatus,
477
545
  byType: parsed.byType,
478
546
  byRunnerType: parsed.byRunnerType,
479
- recentTrend, recentTrendByStatus, recentTrendByType,
547
+ recentTrend,
548
+ recentTrendByStatus,
549
+ recentTrendByType,
480
550
  durationTrend: parsed.durationTrend,
481
- hourlyTrend, hourlyCompletionTrend
551
+ duration: buildDurationResult(parsed.duration),
552
+ hourlyTrend,
553
+ hourlyCompletionTrend
482
554
  };
483
555
  } catch (e) {
484
556
  fastify.log.error(`查询统计数据失败: ${e.message}`);
@@ -499,7 +571,8 @@ module.exports = fp(async (fastify, options) => {
499
571
  TaskModel.findAll({
500
572
  attributes: ['runnerType', 'status', [fn('COUNT', col('id')), 'count']],
501
573
  where: { ...(runnerType ? { runnerType } : {}) },
502
- group: ['runnerType', 'status'], raw: true
574
+ group: ['runnerType', 'status'],
575
+ raw: true
503
576
  }),
504
577
  TaskModel.findAll({
505
578
  attributes: ['runnerType', 'createdAt'],
@@ -563,9 +636,16 @@ module.exports = fp(async (fastify, options) => {
563
636
  let hourlyTrendByType = [];
564
637
  let intervalTrend = [];
565
638
  let todayDuration = {
566
- completedCount: 0, successCount: 0, failedCount: 0, canceledCount: 0,
567
- avgWaitingTime: 0, avgExecutionTime: 0, avgTotalTime: 0,
568
- byType: {}, byRunnerType: {}, byTypeByRunnerType: {}
639
+ completedCount: 0,
640
+ successCount: 0,
641
+ failedCount: 0,
642
+ canceledCount: 0,
643
+ avgWaitingTime: 0,
644
+ avgExecutionTime: 0,
645
+ avgTotalTime: 0,
646
+ byType: {},
647
+ byRunnerType: {},
648
+ byTypeByRunnerType: {}
569
649
  };
570
650
 
571
651
  try {
@@ -582,17 +662,11 @@ module.exports = fp(async (fastify, options) => {
582
662
  const hourlyArrays = buildHourlyTrendArrays(parsed);
583
663
 
584
664
  // SSE 格式:简化为 {hour, count},只取当天
585
- hourlyTrend = hourlyArrays.hourlyTrend
586
- .filter(h => h.date === todayStr)
587
- .map(({ hour, total }) => ({ hour, count: total }));
665
+ hourlyTrend = hourlyArrays.hourlyTrend.filter(h => h.date === todayStr).map(({ hour, total }) => ({ hour, count: total }));
588
666
 
589
- hourlyTrendByStatus = hourlyArrays.hourlyTrendByStatus
590
- .filter(h => h.date === todayStr)
591
- .map(({ hour, status, count }) => ({ hour, status, count }));
667
+ hourlyTrendByStatus = hourlyArrays.hourlyTrendByStatus.filter(h => h.date === todayStr).map(({ hour, status, count }) => ({ hour, status, count }));
592
668
 
593
- hourlyTrendByType = hourlyArrays.hourlyTrendByType
594
- .filter(h => h.date === todayStr)
595
- .map(({ hour, type, count }) => ({ hour, type, count }));
669
+ hourlyTrendByType = hourlyArrays.hourlyTrendByType.filter(h => h.date === todayStr).map(({ hour, type, count }) => ({ hour, type, count }));
596
670
 
597
671
  intervalTrend = hourlyTrend.map(({ hour, count }) => ({
598
672
  interval: `${String(hour).padStart(2, '0')}:00`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-task",
3
- "version": "3.0.0",
3
+ "version": "3.0.2",
4
4
  "description": "一个 Fastify 插件,用于任务编排、追踪任务执行状态和结果。支持系统自动执行和手动执行两种模式,提供完整的任务生命周期管理",
5
5
  "main": "index.js",
6
6
  "scripts": {