@kne/fastify-task 3.0.0-alpha.3 → 3.0.0-alpha.5

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.
@@ -13,405 +13,559 @@ const RANGE_MAP = {
13
13
  '1y': { value: 1, unit: 'year', label: '近1年' }
14
14
  };
15
15
 
16
- const parseRange = (range = '7d') => {
16
+ const RUNNER_TYPES = ['system', 'manual'];
17
+ const STAT_ATTRS = ['total', 'success', 'failed', 'canceled', 'waitingTime', 'executionTime', 'totalTime'];
18
+
19
+ // 查询用客户端时间:parseRange 使用客户端时区计算日期范围
20
+ const parseRange = (range = '7d', tz) => {
17
21
  const config = RANGE_MAP[range];
18
22
  if (!config) {
19
23
  throw new Error(`不支持的时间范围: ${range}, 支持: ${Object.keys(RANGE_MAP).join(',')}`);
20
24
  }
21
- const now = new Date();
22
- const startTime = new Date(now);
23
- if (config.unit === 'day') {
24
- startTime.setDate(startTime.getDate() - config.value);
25
- } else if (config.unit === 'month') {
26
- startTime.setMonth(startTime.getMonth() - config.value);
27
- } else if (config.unit === 'year') {
28
- startTime.setFullYear(startTime.getFullYear() - config.value);
29
- }
30
- return { startTime, endTime: now, label: config.label, range };
25
+ // 写入数据全使用服务器时间,查询用客户端时间
26
+ // 使用客户端时区计算"现在"和"起始时间",确保日期边界对齐客户端视角
27
+ const now = tz ? dayjs().tz(tz) : dayjs();
28
+ const unitStartMap = { day: 'day', month: 'month', year: 'year' };
29
+ const startUnit = unitStartMap[config.unit];
30
+ const startTime = startUnit
31
+ ? now.subtract(config.value, startUnit).startOf(startUnit)
32
+ : now.subtract(config.value, config.unit);
33
+ return { startTime: startTime.toDate(), endTime: now.toDate(), label: config.label, range };
31
34
  };
32
35
 
33
- const COUNT_ATTRIBUTES = ['total', 'success', 'failed', 'canceled', 'pending', 'waiting', 'running'];
34
- const TIMING_ATTRIBUTES = ['waitingTime', 'executionTime', 'totalTime'];
35
- const RUNNER_TYPES = ['manual', 'system'];
36
-
37
- /** 解析通道: type:runnerType:hour */
38
- const parseChannel = channel => {
39
- const parts = channel.split(':');
40
- return {
41
- type: parts[0] || null,
42
- runnerType: parts[1] || null,
43
- hour: parts[2] !== undefined ? parseInt(parts[2], 10) : null
44
- };
36
+ // 查询用客户端时间:formatDate 使用客户端时区格式化日期
37
+ const formatDate = (date, tz) => {
38
+ const d = tz ? dayjs(date).tz(tz) : dayjs(date);
39
+ return d.format('YYYY-MM-DD');
45
40
  };
46
41
 
47
- /** 构建不同层级的通道列表 */
48
- const buildChannels = {
49
- type: types => [...types],
50
- runner: (types, rts = RUNNER_TYPES) => types.flatMap(t => rts.map(rt => `${t}:${rt}`)),
51
- hour: (types, rts = RUNNER_TYPES) => types.flatMap(t =>
52
- rts.flatMap(rt => Array.from({ length: 24 }, (_, h) => `${t}:${rt}:${h}`))
53
- )
42
+ // 查询用客户端时间:提取客户端时区下的小时
43
+ const getHour = (date, tz) => {
44
+ const d = tz ? dayjs(date).tz(tz) : dayjs(date);
45
+ return d.hour();
54
46
  };
55
47
 
56
- /** 按 period 过滤查询结果 */
57
- const filterByPeriod = (list, period) => (list || []).filter(item => item.period === period);
58
-
59
- /** 从 item 中提取 sum 数据(兼容单聚合/多聚合格式) */
60
- const getSumData = item => {
61
- const data = item.data || {};
62
- return data.sum || data;
48
+ const safeNum = v => {
49
+ const n = Number(v);
50
+ return (typeof n === 'number' && !isNaN(n) ? n : 0);
63
51
  };
64
52
 
65
- /** 空的实时统计结果 */
66
- const emptyRealtimeResult = todayDate => ({
67
- date: todayDate, totalTasks: 0, byStatus: {}, byType: {}, byRunnerType: {},
68
- hourlyTrend: [], hourlyTrendByType: [], hourlyTrendByStatus: [],
69
- todayDuration: null, waitingByRunnerType: {}, pendingByRunnerType: {},
70
- completedToday: {}, runnerTypeStats: {},
71
- waitingQueueMaxWaitMsByRunnerType: {}, completedTodayTotalDurationMsByRunnerType: {}
72
- });
53
+ const getSumData = item => (item.data || {}).sum || item.data || {};
73
54
 
74
- /** 空的历史统计结果 */
75
- const emptyHistoryResult = () => ({
76
- totalTasks: 0, byStatus: {}, byType: {},
77
- recentTrend: [], recentTrendByType: [], durationTrend: [], hourlyCompletionTrend: []
78
- });
55
+ const computeAvg = (sum, count) => (count > 0 ? Math.round(sum / count) : 0);
79
56
 
80
- module.exports = fp(async (fastify, options) => {
81
- const statisticsServices = fastify[`${options.name}Statistics`].services;
82
-
83
- /** 获取所有任务类型(通过 channelMeta 根通道,无冒号) */
84
- const getTaskTypes = async () => {
85
- const metas = await statisticsServices.channelMeta.list();
86
- return metas.map(m => m.channel).filter(ch => ch && !ch.includes(':'));
87
- };
57
+ // 将原始时长累加器 {count, sum*} 转为输出格式 {count, avg*}
58
+ const toAvgDuration = ({ count, sumWaiting, sumExecution, sumTotal }) => ({
59
+ count,
60
+ avgWaitingTime: computeAvg(sumWaiting, count),
61
+ avgExecutionTime: computeAvg(sumExecution, count),
62
+ avgTotalTime: computeAvg(sumTotal, count)
63
+ });
88
64
 
89
- /**
90
- * 实时统计:通过统计模块查询今日数据
91
- */
92
- const queryRealtimeData = async ({ timezone }) => {
93
- const tz = timezone || 'UTC';
94
- const now = dayjs().tz(tz);
95
- const todayStart = now.startOf('day').toDate();
96
- const todayDate = now.format('YYYY-MM-DD');
65
+ const parseChannel = channel => {
66
+ const parts = channel.split(':');
67
+ return parts.length >= 2
68
+ ? { type: parts[0], runnerType: parts[1] }
69
+ : { type: channel, runnerType: null };
70
+ };
97
71
 
98
- const taskTypes = await getTaskTypes();
99
- if (taskTypes.length === 0) return emptyRealtimeResult(todayDate);
72
+ const buildChannels = async (statisticsServices, type, runnerType) => {
73
+ const metaResult = await statisticsServices.channelMeta.list();
74
+ const list = Array.isArray(metaResult) ? metaResult : metaResult.list || [];
75
+ const rootChannels = list.map(meta => meta.channel).filter(Boolean);
76
+ const types = type ? rootChannels.filter(ch => ch === type) : rootChannels;
77
+ const rts = runnerType ? [runnerType] : RUNNER_TYPES;
78
+ const channels = [];
79
+ for (const t of types) {
80
+ channels.push(t);
81
+ for (const rt of rts) channels.push(`${t}:${rt}`);
82
+ }
83
+ return channels;
84
+ };
100
85
 
101
- const typeChannels = buildChannels.type(taskTypes);
102
- const runnerChannels = buildChannels.runner(taskTypes);
103
- const hourChannels = buildChannels.hour(taskTypes);
86
+ // 确保时长累加器存在于 map 中
87
+ const ensureDurationAcc = (map, key) => {
88
+ if (!map[key]) map[key] = { count: 0, sumWaiting: 0, sumExecution: 0, sumTotal: 0 };
89
+ return map[key];
90
+ };
104
91
 
105
- // 1. 按类型查询计数汇总
106
- const typeCountResult = await statisticsServices.query({
107
- channels: typeChannels, startTime: todayStart, endTime: now.toDate(),
108
- attributeNames: COUNT_ATTRIBUTES, aggregates: ['sum'], timezone: tz
109
- });
92
+ // 累加时长到累加器
93
+ const accumDuration = (acc, count, sum) => {
94
+ acc.count += count;
95
+ acc.sumWaiting += safeNum(sum.waitingTime);
96
+ acc.sumExecution += safeNum(sum.executionTime);
97
+ acc.sumTotal += safeNum(sum.totalTime);
98
+ };
110
99
 
111
- // 2. 按执行方式查询计数
112
- const runnerCountResult = await statisticsServices.query({
113
- channels: runnerChannels, startTime: todayStart, endTime: now.toDate(),
114
- attributeNames: COUNT_ATTRIBUTES, aggregates: ['sum'], timezone: tz
115
- });
100
+ // 创建空的 dailyMap 条目
101
+ const createDailyMapEntry = () => ({ total: 0, byStatus: { success: 0, failed: 0, canceled: 0 }, byType: {} });
116
102
 
117
- // 3. 按小时通道查询计数趋势
118
- const hourCountResult = await statisticsServices.query({
119
- channels: hourChannels, startTime: todayStart, endTime: now.toDate(),
120
- attributeNames: COUNT_ATTRIBUTES, aggregates: ['sum'], timezone: tz
121
- });
103
+ // 创建空的 dailyDurationMap 条目
104
+ const createDailyDurationEntry = () => ({
105
+ completedCount: 0, successCount: 0, failedCount: 0, canceledCount: 0,
106
+ sumWaiting: 0, sumExecution: 0, sumTotal: 0, countWithTiming: 0,
107
+ byType: {}, byRunnerType: {}
108
+ });
122
109
 
123
- // 汇总 byStatus / byType / totalTasks
124
- const byStatus = {};
125
- const byType = {};
126
- let totalTasks = 0;
127
- for (const item of typeCountResult.list || []) {
128
- const sumData = getSumData(item);
129
- if (sumData.total !== undefined) {
130
- byType[item.channel] = Math.round(sumData.total);
131
- totalTasks += Math.round(sumData.total);
132
- }
133
- for (const status of COUNT_ATTRIBUTES) {
134
- if (status !== 'total' && sumData[status] !== undefined) {
135
- byStatus[status] = (byStatus[status] || 0) + Math.round(sumData[status]);
110
+ // 查询 statistics 并解析结果
111
+ // 写入数据全使用服务器时间(DB 中存储的是服务器时间),查询用客户端时间转换展示
112
+ const queryAndParse = async (statisticsServices, { channels, startTime, endTime, timezone: tz, runnerType: runnerTypeFilter }) => {
113
+ if (channels.length === 0) return null;
114
+
115
+ const statResult = await statisticsServices.query({
116
+ channels, startTime, endTime,
117
+ attributeNames: STAT_ATTRS,
118
+ aggregates: ['sum'],
119
+ timezone: tz || undefined
120
+ });
121
+
122
+ const result = {
123
+ totalTasks: 0,
124
+ byStatus: { success: 0, failed: 0, canceled: 0 },
125
+ byType: {},
126
+ byRunnerType: {},
127
+ dailyMap: {},
128
+ hourlyMap: {},
129
+ hourlyStatusMap: {},
130
+ hourlyTypeMap: {},
131
+ hourlyDetailMap: {},
132
+ duration: {
133
+ completedCount: 0, successCount: 0, failedCount: 0, canceledCount: 0,
134
+ sumWaiting: 0, sumExecution: 0, sumTotal: 0, countWithTiming: 0,
135
+ byType: {}, byRunnerType: {}, byTypeByRunnerType: {}
136
+ },
137
+ durationTrend: []
138
+ };
139
+ const dailyDurationMap = {};
140
+
141
+ for (const item of statResult.list || []) {
142
+ const { type: typeName, runnerType: runnerTypeName } = parseChannel(item.channel);
143
+ if (runnerTypeFilter && runnerTypeName && runnerTypeName !== runnerTypeFilter) continue;
144
+ if (runnerTypeFilter && !runnerTypeName) continue;
145
+
146
+ const sum = getSumData(item);
147
+ const count = safeNum(sum.total);
148
+ const isHourly = item.period === 'h';
149
+ // 查询用客户端时间:日期和小时使用客户端时区转换
150
+ const date = formatDate(item.time, tz);
151
+ const hour = isHourly ? getHour(item.time, tz) : null;
152
+
153
+ // ─── 1-segment channel: 全局 + 按日 + 时长 聚合 ───
154
+ if (!runnerTypeName) {
155
+ // 全局汇总
156
+ result.totalTasks += count;
157
+ result.byStatus.success += safeNum(sum.success);
158
+ result.byStatus.failed += safeNum(sum.failed);
159
+ result.byStatus.canceled += safeNum(sum.canceled);
160
+ result.byType[typeName] = (result.byType[typeName] || 0) + count;
161
+
162
+ // 全局时长
163
+ result.duration.completedCount += count;
164
+ result.duration.successCount += safeNum(sum.success);
165
+ result.duration.failedCount += safeNum(sum.failed);
166
+ result.duration.canceled += safeNum(sum.canceled);
167
+ result.duration.sumWaiting += safeNum(sum.waitingTime);
168
+ result.duration.sumExecution += safeNum(sum.executionTime);
169
+ result.duration.sumTotal += safeNum(sum.totalTime);
170
+ result.duration.countWithTiming += count;
171
+ if (count > 0) accumDuration(ensureDurationAcc(result.duration.byType, typeName), count, sum);
172
+
173
+ // 按日汇总
174
+ if (!result.dailyMap[date]) result.dailyMap[date] = createDailyMapEntry();
175
+ const dm = result.dailyMap[date];
176
+ dm.total += count;
177
+ dm.byStatus.success += safeNum(sum.success);
178
+ dm.byStatus.failed += safeNum(sum.failed);
179
+ dm.byStatus.canceled += safeNum(sum.canceled);
180
+ dm.byType[typeName] = (dm.byType[typeName] || 0) + count;
181
+
182
+ // 按日时长
183
+ if (!dailyDurationMap[date]) dailyDurationMap[date] = createDailyDurationEntry();
184
+ const dd = dailyDurationMap[date];
185
+ dd.completedCount += count;
186
+ dd.successCount += safeNum(sum.success);
187
+ dd.failedCount += safeNum(sum.failed);
188
+ dd.canceledCount += safeNum(sum.canceled);
189
+ dd.sumWaiting += safeNum(sum.waitingTime);
190
+ dd.sumExecution += safeNum(sum.executionTime);
191
+ dd.sumTotal += safeNum(sum.totalTime);
192
+ dd.countWithTiming += count;
193
+ if (count > 0) accumDuration(ensureDurationAcc(dd.byType, typeName), count, sum);
194
+
195
+ // 小时级趋势(仅 period='h')
196
+ if (isHourly) {
197
+ if (!result.hourlyMap[date]) result.hourlyMap[date] = {};
198
+ result.hourlyMap[date][hour] = (result.hourlyMap[date][hour] || 0) + count;
199
+
200
+ if (!result.hourlyStatusMap[date]) result.hourlyStatusMap[date] = {};
201
+ if (!result.hourlyStatusMap[date][hour]) result.hourlyStatusMap[date][hour] = {};
202
+ for (const status of ['success', 'failed', 'canceled']) {
203
+ const sc = safeNum(sum[status]);
204
+ if (sc > 0) result.hourlyStatusMap[date][hour][status] = (result.hourlyStatusMap[date][hour][status] || 0) + sc;
136
205
  }
206
+
207
+ if (!result.hourlyTypeMap[date]) result.hourlyTypeMap[date] = {};
208
+ if (!result.hourlyTypeMap[date][hour]) result.hourlyTypeMap[date][hour] = {};
209
+ if (count > 0) result.hourlyTypeMap[date][hour][typeName] = (result.hourlyTypeMap[date][hour][typeName] || 0) + count;
137
210
  }
138
211
  }
139
212
 
140
- // 汇总 byRunnerType / waiting / pending / completed / runnerTypeStats
141
- const byRunnerType = {};
142
- const waitingByRunnerType = {};
143
- const pendingByRunnerType = {};
144
- const completedToday = {};
145
- const runnerTypeStats = {};
146
- for (const item of runnerCountResult.list || []) {
147
- const { runnerType } = parseChannel(item.channel);
148
- if (!runnerType) continue;
149
- const sumData = getSumData(item);
150
- if (sumData.total !== undefined) {
151
- byRunnerType[runnerType] = (byRunnerType[runnerType] || 0) + Math.round(sumData.total);
152
- }
153
- if (!runnerTypeStats[runnerType]) runnerTypeStats[runnerType] = { total: 0, waiting: 0, waitingCount: 0 };
154
- if (sumData.total !== undefined) runnerTypeStats[runnerType].total += Math.round(sumData.total);
155
- if (sumData.waiting !== undefined) {
156
- const w = Math.round(sumData.waiting);
157
- waitingByRunnerType[runnerType] = w;
158
- runnerTypeStats[runnerType].waiting += w;
159
- runnerTypeStats[runnerType].waitingCount += w;
213
+ // ─── 2-segment channel: byRunnerType 聚合 ───
214
+ if (runnerTypeName) {
215
+ result.byRunnerType[runnerTypeName] = (result.byRunnerType[runnerTypeName] || 0) + count;
216
+
217
+ if (count > 0) {
218
+ accumDuration(ensureDurationAcc(result.duration.byRunnerType, runnerTypeName), count, sum);
219
+
220
+ // byTypeByRunnerType(覆盖写入,非累加)
221
+ if (!result.duration.byTypeByRunnerType[runnerTypeName]) result.duration.byTypeByRunnerType[runnerTypeName] = {};
222
+ result.duration.byTypeByRunnerType[runnerTypeName][typeName] = {
223
+ count,
224
+ sumWaiting: safeNum(sum.waitingTime),
225
+ sumExecution: safeNum(sum.executionTime),
226
+ sumTotal: safeNum(sum.totalTime)
227
+ };
228
+
229
+ // 按日 byRunnerType 时长
230
+ if (dailyDurationMap[date]) {
231
+ accumDuration(ensureDurationAcc(dailyDurationMap[date].byRunnerType, runnerTypeName), count, sum);
232
+ }
160
233
  }
161
- if (sumData.pending !== undefined) pendingByRunnerType[runnerType] = Math.round(sumData.pending);
162
- if (sumData.success !== undefined) completedToday[runnerType] = Math.round(sumData.success);
163
- }
164
234
 
165
- // 按小时趋势(直接从 channel 中的小时数分组,用 time 字段做时区转换)
166
- const hourBuckets = {};
167
- for (const item of hourCountResult.list || []) {
168
- const { hour: channelHour, type: taskType } = parseChannel(item.channel);
169
- if (channelHour === null || isNaN(channelHour)) continue;
170
- // 使用 time 字段获取本地小时(更精确的时区处理)
171
- const localHour = item.time ? dayjs(item.time).tz(tz).hour() : channelHour;
172
- const sumData = getSumData(item);
173
- const count = sumData.total !== undefined ? Math.round(sumData.total) : 0;
174
-
175
- if (!hourBuckets[localHour]) hourBuckets[localHour] = { total: 0, byType: {}, byStatus: {} };
176
- const bucket = hourBuckets[localHour];
177
- bucket.total += count;
178
- if (taskType) bucket.byType[taskType] = (bucket.byType[taskType] || 0) + count;
179
- for (const status of COUNT_ATTRIBUTES) {
180
- if (status !== 'total' && sumData[status] !== undefined) {
181
- bucket.byStatus[status] = (bucket.byStatus[status] || 0) + Math.round(sumData[status]);
235
+ // hourlyDetailMap(仅 period='h')
236
+ if (isHourly) {
237
+ if (!result.hourlyDetailMap[date]) result.hourlyDetailMap[date] = {};
238
+ if (!result.hourlyDetailMap[date][hour]) result.hourlyDetailMap[date][hour] = {};
239
+ if (!result.hourlyDetailMap[date][hour][typeName]) result.hourlyDetailMap[date][hour][typeName] = {};
240
+ if (!result.hourlyDetailMap[date][hour][typeName][runnerTypeName]) {
241
+ result.hourlyDetailMap[date][hour][typeName][runnerTypeName] = { total: 0, success: 0, failed: 0, canceled: 0 };
182
242
  }
243
+ const detail = result.hourlyDetailMap[date][hour][typeName][runnerTypeName];
244
+ detail.total += count;
245
+ detail.success += safeNum(sum.success);
246
+ detail.failed += safeNum(sum.failed);
247
+ detail.canceled += safeNum(sum.canceled);
183
248
  }
184
249
  }
250
+ }
185
251
 
186
- const hourlyTrend = [], hourlyTrendByType = [], hourlyTrendByStatus = [];
187
- for (const [h, bucket] of Object.entries(hourBuckets)) {
188
- const hour = parseInt(h, 10);
189
- if (bucket.total > 0) hourlyTrend.push({ hour, count: bucket.total });
190
- for (const [type, count] of Object.entries(bucket.byType)) hourlyTrendByType.push({ hour, type, count });
191
- for (const [status, count] of Object.entries(bucket.byStatus)) hourlyTrendByStatus.push({ hour, status, count });
192
- }
252
+ // 构建按日时长趋势
253
+ result.durationTrend = Object.entries(dailyDurationMap)
254
+ .sort(([a], [b]) => a.localeCompare(b))
255
+ .map(([date, dd]) => ({
256
+ date,
257
+ completedCount: dd.completedCount,
258
+ successCount: dd.successCount,
259
+ failedCount: dd.failedCount,
260
+ canceledCount: dd.canceledCount,
261
+ avgWaitingTime: computeAvg(dd.sumWaiting, dd.countWithTiming),
262
+ avgExecutionTime: computeAvg(dd.sumExecution, dd.countWithTiming),
263
+ avgTotalTime: computeAvg(dd.sumTotal, dd.countWithTiming),
264
+ byType: Object.fromEntries(Object.entries(dd.byType).map(([n, td]) => [n, toAvgDuration(td)])),
265
+ byRunnerType: Object.fromEntries(Object.entries(dd.byRunnerType).map(([n, rt]) => [n, toAvgDuration(rt)]))
266
+ }));
193
267
 
194
- // 时长统计(avg 聚合)
195
- const durationResult = await statisticsServices.query({
196
- channels: runnerChannels, startTime: todayStart, endTime: now.toDate(),
197
- attributeNames: TIMING_ATTRIBUTES, aggregates: ['avg'], timezone: tz
198
- });
199
- let totalAvgExec = 0, totalAvgWait = 0, totalAvgTotal = 0, durationCount = 0;
200
- for (const item of durationResult.list || []) {
201
- const avgData = item.data?.avg || item.data || {};
202
- if (avgData.totalTime !== undefined) {
203
- totalAvgExec += avgData.executionTime || 0;
204
- totalAvgWait += avgData.waitingTime || 0;
205
- totalAvgTotal += avgData.totalTime || 0;
206
- durationCount++;
268
+ return result;
269
+ };
270
+
271
+ // 构建 todayDuration 结果
272
+ const buildDurationResult = (d) => ({
273
+ completedCount: d.completedCount,
274
+ successCount: d.successCount,
275
+ failedCount: d.failedCount,
276
+ canceledCount: d.canceledCount,
277
+ avgWaitingTime: computeAvg(d.sumWaiting, d.countWithTiming),
278
+ avgExecutionTime: computeAvg(d.sumExecution, d.countWithTiming),
279
+ avgTotalTime: computeAvg(d.sumTotal, d.countWithTiming),
280
+ byType: Object.fromEntries(Object.entries(d.byType).map(([n, td]) => [n, toAvgDuration(td)])),
281
+ byRunnerType: Object.fromEntries(Object.entries(d.byRunnerType).map(([n, rt]) => [n, toAvgDuration(rt)])),
282
+ byTypeByRunnerType: Object.fromEntries(
283
+ Object.entries(d.byTypeByRunnerType).map(([rtName, types]) => [
284
+ rtName,
285
+ Object.fromEntries(Object.entries(types).map(([typeName, td]) => [typeName, toAvgDuration(td)]))
286
+ ])
287
+ )
288
+ });
289
+
290
+ // 构建小时趋势数组(完整格式,供 history 接口使用)
291
+ const buildHourlyTrendArrays = (parsed) => {
292
+ const hourlyTrend = [];
293
+ const hourlyTrendByStatus = [];
294
+ const hourlyTrendByType = [];
295
+ const hourlyCompletionTrend = [];
296
+
297
+ const dates = Object.keys(parsed.hourlyMap).sort();
298
+ for (const date of dates) {
299
+ const hoursMap = parsed.hourlyMap[date] || {};
300
+ const statusMap = parsed.hourlyStatusMap[date] || {};
301
+ const typeMap = parsed.hourlyTypeMap[date] || {};
302
+ const detailMap = parsed.hourlyDetailMap[date] || {};
303
+
304
+ const allHours = [...new Set([
305
+ ...Object.keys(hoursMap).map(Number),
306
+ ...Object.keys(statusMap).map(Number),
307
+ ...Object.keys(typeMap).map(Number),
308
+ ...Object.keys(detailMap).map(Number)
309
+ ])].sort((a, b) => a - b);
310
+
311
+ for (const hour of allHours) {
312
+ const statuses = statusMap[hour] || {};
313
+ hourlyTrend.push({
314
+ date, hour,
315
+ total: hoursMap[hour] || 0,
316
+ success: statuses.success || 0,
317
+ failed: statuses.failed || 0,
318
+ canceled: statuses.canceled || 0
319
+ });
320
+ for (const [status, count] of Object.entries(statuses)) {
321
+ if (count > 0) hourlyTrendByStatus.push({ date, hour, status, count });
207
322
  }
208
- }
209
- const todayDuration = durationCount > 0 ? {
210
- avgExecutionTime: Math.round(totalAvgExec / durationCount),
211
- avgWaitingTime: Math.round(totalAvgWait / durationCount),
212
- avgTotalTime: Math.round(totalAvgTotal / durationCount)
213
- } : null;
214
-
215
- // 等待队列最长等待时间(max 聚合)
216
- const maxWaitResult = await statisticsServices.query({
217
- channels: runnerChannels, startTime: todayStart, endTime: now.toDate(),
218
- attributeNames: ['waitingTime'], aggregates: ['max'], timezone: tz
219
- });
220
- const waitingQueueMaxWaitMsByRunnerType = {};
221
- for (const item of maxWaitResult.list || []) {
222
- const { runnerType } = parseChannel(item.channel);
223
- if (!runnerType) continue;
224
- const maxData = item.data?.max || item.data || {};
225
- if (maxData.waitingTime !== undefined) {
226
- const v = Math.round(maxData.waitingTime);
227
- if (!waitingQueueMaxWaitMsByRunnerType[runnerType] || v > waitingQueueMaxWaitMsByRunnerType[runnerType]) {
228
- waitingQueueMaxWaitMsByRunnerType[runnerType] = v;
229
- }
323
+ const types = typeMap[hour] || {};
324
+ for (const [typeName, count] of Object.entries(types)) {
325
+ if (count > 0) hourlyTrendByType.push({ date, hour, type: typeName, count });
230
326
  }
231
327
  }
232
328
 
233
- // 当日完成任务的创建→完成总耗时(sum 聚合)
234
- const totalDurationResult = await statisticsServices.query({
235
- channels: runnerChannels, startTime: todayStart, endTime: now.toDate(),
236
- attributeNames: ['totalTime'], aggregates: ['sum'], timezone: tz
237
- });
238
- const completedTodayTotalDurationMsByRunnerType = {};
239
- for (const item of totalDurationResult.list || []) {
240
- const { runnerType } = parseChannel(item.channel);
241
- if (!runnerType) continue;
242
- const sumData = getSumData(item);
243
- if (sumData.totalTime !== undefined) {
244
- completedTodayTotalDurationMsByRunnerType[runnerType] =
245
- (completedTodayTotalDurationMsByRunnerType[runnerType] || 0) + Math.round(sumData.totalTime);
329
+ for (const hour of Object.keys(detailMap).map(Number).sort((a, b) => a - b)) {
330
+ const types = detailMap[hour] || {};
331
+ for (const [typeName, runnerTypes] of Object.entries(types)) {
332
+ for (const [runnerTypeName, detail] of Object.entries(runnerTypes)) {
333
+ if (detail.total > 0) {
334
+ hourlyCompletionTrend.push({
335
+ date, hour, type: typeName, runnerType: runnerTypeName,
336
+ totalCompleted: detail.total,
337
+ successCount: detail.success,
338
+ failedCount: detail.failed,
339
+ canceledCount: detail.canceled
340
+ });
341
+ }
342
+ }
246
343
  }
247
344
  }
345
+ }
248
346
 
249
- return {
250
- date: todayDate, totalTasks, byStatus, byType, byRunnerType,
251
- hourlyTrend, hourlyTrendByType, hourlyTrendByStatus, todayDuration,
252
- waitingByRunnerType, pendingByRunnerType, completedToday, runnerTypeStats,
253
- waitingQueueMaxWaitMsByRunnerType, completedTodayTotalDurationMsByRunnerType
254
- };
255
- };
256
-
257
- /**
258
- * 历史统计:通过统计模块查询
259
- */
260
- const queryHistoryData = async ({ range = '7d', timezone, type, runnerType }) => {
261
- const { startTime, endTime } = parseRange(range);
262
- const tz = timezone || undefined;
263
-
264
- const taskTypes = type ? [type] : await getTaskTypes();
265
- const runnerTypes = runnerType ? [runnerType] : RUNNER_TYPES;
266
-
267
- if (taskTypes.length === 0) return emptyHistoryResult();
268
-
269
- const typeChannels = buildChannels.type(taskTypes);
270
- const runnerChannels = buildChannels.runner(taskTypes, runnerTypes);
271
- const hourChannels = buildChannels.hour(taskTypes, runnerTypes);
347
+ return { hourlyTrend, hourlyTrendByStatus, hourlyTrendByType, hourlyCompletionTrend };
348
+ };
272
349
 
273
- // 按类型查询计数汇总
274
- const typeCountResult = await statisticsServices.query({
275
- channels: typeChannels, startTime, endTime,
276
- attributeNames: COUNT_ATTRIBUTES, aggregates: ['sum'], timezone: tz
277
- });
350
+ // 构建历史日趋势数组
351
+ const buildDailyTrendArrays = (parsed) => {
352
+ const sorted = Object.entries(parsed.dailyMap).sort(([a], [b]) => a.localeCompare(b));
353
+ const recentTrend = sorted.map(([date, d]) => ({ date, count: d.total }));
354
+ const recentTrendByStatus = [];
355
+ const recentTrendByType = [];
356
+ sorted.forEach(([date, d]) => {
357
+ Object.entries(d.byStatus).forEach(([status, count]) => recentTrendByStatus.push({ date, status, count }));
358
+ Object.entries(d.byType).forEach(([type, count]) => recentTrendByType.push({ date, type, count }));
359
+ });
360
+ return { recentTrend, recentTrendByStatus, recentTrendByType };
361
+ };
278
362
 
279
- // 按小时通道查询计数趋势
280
- const hourCountResult = await statisticsServices.query({
281
- channels: hourChannels, startTime, endTime,
282
- attributeNames: COUNT_ATTRIBUTES, aggregates: ['sum'], timezone: tz
283
- });
363
+ module.exports = fp(async (fastify, options) => {
364
+ const statisticsServices = fastify[`${options.name}Statistics`].services;
365
+ const { task: TaskModel } = fastify[options.name].models;
366
+ const { Op, fn, col } = fastify.sequelize.Sequelize;
284
367
 
285
- // 汇总
286
- const byStatus = {};
287
- const byType = {};
288
- let totalTasks = 0;
289
- for (const item of typeCountResult.list || []) {
290
- const sumData = getSumData(item);
291
- if (sumData.total !== undefined) {
292
- byType[item.channel] = Math.round(sumData.total);
293
- totalTasks += Math.round(sumData.total);
294
- }
295
- for (const status of COUNT_ATTRIBUTES) {
296
- if (status !== 'total' && sumData[status] !== undefined) {
297
- byStatus[status] = (byStatus[status] || 0) + Math.round(sumData[status]);
298
- }
299
- }
368
+ // ─── queryStatistics (history 接口) ───
369
+ const queryStatistics = async ({ range = '7d', timezone, type, runnerType }) => {
370
+ // 查询用客户端时间:parseRange 传入 timezone
371
+ const { startTime, endTime, label, range: rangeKey } = parseRange(range, timezone);
372
+ const emptyResult = {
373
+ range: rangeKey, rangeLabel: label,
374
+ totalTasks: 0, byStatus: {}, byType: {}, byRunnerType: {},
375
+ recentTrend: [], recentTrendByStatus: [], recentTrendByType: [],
376
+ durationTrend: [], hourlyCompletionTrend: []
377
+ };
378
+ try {
379
+ const channels = await buildChannels(statisticsServices, type, runnerType);
380
+ const parsed = await queryAndParse(statisticsServices, { channels, startTime, endTime, timezone, runnerType });
381
+ if (!parsed) return emptyResult;
382
+
383
+ const { recentTrend, recentTrendByStatus, recentTrendByType } = buildDailyTrendArrays(parsed);
384
+ const { hourlyTrend, hourlyCompletionTrend } = buildHourlyTrendArrays(parsed);
385
+
386
+ return {
387
+ range: rangeKey, rangeLabel: label,
388
+ totalTasks: parsed.totalTasks,
389
+ byStatus: parsed.byStatus,
390
+ byType: parsed.byType,
391
+ byRunnerType: parsed.byRunnerType,
392
+ recentTrend, recentTrendByStatus, recentTrendByType,
393
+ durationTrend: parsed.durationTrend,
394
+ hourlyTrend, hourlyCompletionTrend
395
+ };
396
+ } catch (e) {
397
+ fastify.log.error(`查询统计数据失败: ${e.message}`);
398
+ return emptyResult;
300
399
  }
400
+ };
301
401
 
302
- // 按日聚合趋势(从小时通道数据中按 time 日期分组)
303
- const dateMap = {};
304
- for (const item of hourCountResult.list || []) {
305
- if (!item.time) continue;
306
- const dateStr = tz ? dayjs(item.time).tz(tz).format('YYYY-MM-DD') : dayjs(item.time).format('YYYY-MM-DD');
307
- const sumData = getSumData(item);
308
- const { type: taskType } = parseChannel(item.channel);
309
- const count = sumData.total !== undefined ? Math.round(sumData.total) : 0;
310
-
311
- if (!dateMap[dateStr]) dateMap[dateStr] = { total: 0, byType: {} };
312
- dateMap[dateStr].total += count;
313
- if (taskType) dateMap[dateStr].byType[taskType] = (dateMap[dateStr].byType[taskType] || 0) + count;
314
- }
402
+ // ─── buildSseData (SSE 接口) ───
403
+ const buildSseData = async ({ timezone: tz, type, runnerType }) => {
404
+ // 查询用客户端时间:"今天"的边界使用客户端时区
405
+ const now = tz ? dayjs().tz(tz) : dayjs();
406
+ const todayStart = now.startOf('day').toDate();
407
+ const todayEnd = now.endOf('day').toDate();
408
+ const todayStr = now.format('YYYY-MM-DD');
409
+
410
+ // 统计查询:正在执行和等待执行的数量 + DB 查询
411
+ const [runnerTypeStatusRows, waitingTasks] = await Promise.all([
412
+ TaskModel.findAll({
413
+ attributes: ['runnerType', 'status', [fn('COUNT', col('id')), 'count']],
414
+ where: { ...(runnerType ? { runnerType } : {}) },
415
+ group: ['runnerType', 'status'], raw: true
416
+ }),
417
+ TaskModel.findAll({
418
+ attributes: ['runnerType', 'createdAt'],
419
+ where: { status: { [Op.in]: ['pending', 'waiting'] }, ...(runnerType ? { runnerType } : {}) },
420
+ raw: true
421
+ })
422
+ ]);
315
423
 
316
- const recentTrend = Object.entries(dateMap)
317
- .map(([date, data]) => ({ date, count: data.total }))
318
- .sort((a, b) => a.date.localeCompare(b.date));
424
+ const pendingByRunnerType = {};
425
+ const waitingByRunnerType = {};
426
+ const runningByRunnerType = {};
319
427
 
320
- const recentTrendByType = [];
321
- for (const [date, data] of Object.entries(dateMap)) {
322
- for (const [t, count] of Object.entries(data.byType)) {
323
- recentTrendByType.push({ date, type: t, count });
428
+ const nowMs = Date.now();
429
+ const waitingQueueMaxWaitMsByRunnerType = {};
430
+ waitingTasks.forEach(r => {
431
+ const rt = r.runnerType || 'manual';
432
+ const waitMs = nowMs - new Date(r.createdAt).getTime();
433
+ if (!waitingQueueMaxWaitMsByRunnerType[rt] || waitMs > waitingQueueMaxWaitMsByRunnerType[rt]) {
434
+ waitingQueueMaxWaitMsByRunnerType[rt] = waitMs;
324
435
  }
325
- }
326
- recentTrendByType.sort((a, b) => a.date.localeCompare(b.date) || a.type.localeCompare(b.type));
327
-
328
- // hourlyCompletionTrend(按日期+小时聚合,小时从 channel 解析)
329
- const hourDayMap = {};
330
- for (const item of hourCountResult.list || []) {
331
- if (!item.time) continue;
332
- const dj = tz ? dayjs(item.time).tz(tz) : dayjs(item.time);
333
- const date = dj.format('YYYY-MM-DD');
334
- const { hour } = parseChannel(item.channel);
335
- if (hour === null || isNaN(hour)) continue;
336
- const key = `${date}|${hour}`;
337
- if (!hourDayMap[key]) hourDayMap[key] = { date, hour, byStatus: {}, byType: {}, total: 0 };
338
-
339
- const bucket = hourDayMap[key];
340
- const sumData = getSumData(item);
341
- const { type: taskType } = parseChannel(item.channel);
342
- if (sumData.total !== undefined) bucket.total += Math.round(sumData.total);
343
- for (const status of COUNT_ATTRIBUTES) {
344
- if (status !== 'total' && sumData[status] !== undefined) {
345
- bucket.byStatus[status] = (bucket.byStatus[status] || 0) + Math.round(sumData[status]);
346
- }
436
+ });
437
+
438
+ const runnerTypeStats = {};
439
+ const dbStatusCount = { pending: 0, waiting: 0, running: 0, success: 0, failed: 0, canceled: 0 };
440
+ runnerTypeStatusRows.forEach(r => {
441
+ if (!runnerTypeStats[r.runnerType]) runnerTypeStats[r.runnerType] = { total: 0, pending: 0, waiting: 0, waitingCount: 0, executed: 0 };
442
+ const count = Number(r.count);
443
+ const rt = r.runnerType || 'manual';
444
+ runnerTypeStats[r.runnerType].total += count;
445
+ if (r.status === 'pending') runnerTypeStats[r.runnerType].pending += count;
446
+ if (['success', 'failed', 'canceled', 'running'].includes(r.status)) runnerTypeStats[r.runnerType].executed += count;
447
+ if (dbStatusCount[r.status] !== undefined) dbStatusCount[r.status] += count;
448
+ // 按 runnerType 汇总 running/waiting/pending(来自 DB 实时状态,避免 delta 脏数据)
449
+ if (r.status === 'running') runningByRunnerType[rt] = (runningByRunnerType[rt] || 0) + count;
450
+ if (r.status === 'waiting') waitingByRunnerType[rt] = (waitingByRunnerType[rt] || 0) + count;
451
+ if (r.status === 'pending') pendingByRunnerType[rt] = (pendingByRunnerType[rt] || 0) + count;
452
+ // pending 状态等同于"等待操作",计入 waitingByRunnerType 和 runnerTypeStats.waiting/waitingCount
453
+ if (r.status === 'pending') {
454
+ waitingByRunnerType[rt] = (waitingByRunnerType[rt] || 0) + count;
455
+ runnerTypeStats[r.runnerType].waiting += count;
456
+ runnerTypeStats[r.runnerType].waitingCount += count;
347
457
  }
348
- if (taskType && sumData.total !== undefined) {
349
- bucket.byType[taskType] = (bucket.byType[taskType] || 0) + Math.round(sumData.total);
458
+ if (r.status === 'waiting') {
459
+ runnerTypeStats[r.runnerType].waiting += count;
460
+ runnerTypeStats[r.runnerType].waitingCount += count;
350
461
  }
351
- }
352
-
353
- const hourlyCompletionTrend = Object.values(hourDayMap).map(b => ({
354
- date: b.date, hour: b.hour, totalCompleted: b.total,
355
- ...b.byStatus,
356
- ...Object.fromEntries(Object.entries(b.byType).map(([t, c]) => [`${t}Count`, c]))
357
- }));
358
-
359
- // 时长趋势(按日 + runnerType 聚合平均执行/等待时长)
360
- const durationResult = await statisticsServices.query({
361
- channels: runnerChannels, startTime, endTime,
362
- attributeNames: TIMING_ATTRIBUTES, aggregates: ['avg', 'sum', 'count'], timezone: tz
363
462
  });
364
463
 
365
- const durationByDate = {};
366
- for (const item of durationResult.list || []) {
367
- if (!item.time) continue;
368
- const dateStr = tz ? dayjs(item.time).tz(tz).format('YYYY-MM-DD') : dayjs(item.time).format('YYYY-MM-DD');
369
- const { runnerType: rt } = parseChannel(item.channel);
370
- if (!rt) continue;
371
-
372
- const data = item.data || {};
373
- const sumData = data.sum || {};
374
- const countData = data.count || {};
375
- const avgData = data.avg || {};
376
-
377
- let avgExecutionTime = 0, avgWaitingTime = 0;
378
- if (countData.totalTime > 0 && sumData.executionTime !== undefined) {
379
- avgExecutionTime = Math.round(sumData.executionTime / countData.totalTime);
380
- avgWaitingTime = Math.round((sumData.waitingTime || 0) / countData.totalTime);
381
- } else if (avgData.executionTime !== undefined) {
382
- avgExecutionTime = Math.round(avgData.executionTime);
383
- avgWaitingTime = Math.round(avgData.waitingTime || 0);
384
- }
464
+ // Statistics 查询:已完成数据
465
+ // 默认值:DB 实时数据(即使 statistics 服务不可用也能展示 running/waiting/pending)
466
+ // pending 全部合并到 waiting:前端"等待操作"只有 waiting 类别,不展示独立的 pending
467
+ const dbWaitingTotal = dbStatusCount.waiting + dbStatusCount.pending;
468
+ let totalTasks = dbStatusCount.running + dbWaitingTotal;
469
+ let byStatus = { success: 0, failed: 0, canceled: 0, running: dbStatusCount.running, waiting: dbWaitingTotal, pending: 0 };
470
+ let byType = {};
471
+ let byRunnerType = {};
472
+ let completedToday = {};
473
+ let completedTodayTotalDurationMsByRunnerType = {};
474
+ let hourlyTrend = [];
475
+ let hourlyTrendByStatus = [];
476
+ let hourlyTrendByType = [];
477
+ let intervalTrend = [];
478
+ let todayDuration = {
479
+ completedCount: 0, successCount: 0, failedCount: 0, canceledCount: 0,
480
+ avgWaitingTime: 0, avgExecutionTime: 0, avgTotalTime: 0,
481
+ byType: {}, byRunnerType: {}, byTypeByRunnerType: {}
482
+ };
385
483
 
386
- if (avgExecutionTime > 0 || avgWaitingTime > 0) {
387
- if (!durationByDate[dateStr]) durationByDate[dateStr] = {};
388
- durationByDate[dateStr][rt] = { avgExecutionTime, avgWaitingTime };
484
+ try {
485
+ const channels = await buildChannels(statisticsServices, type, runnerType);
486
+ const parsed = await queryAndParse(statisticsServices, { channels, startTime: todayStart, endTime: todayEnd, timezone: tz, runnerType });
487
+ if (parsed) {
488
+ totalTasks = parsed.totalTasks + dbStatusCount.running + dbWaitingTotal;
489
+ byStatus = { ...parsed.byStatus, running: dbStatusCount.running, waiting: dbWaitingTotal, pending: 0 };
490
+ byType = parsed.byType;
491
+ byRunnerType = parsed.byRunnerType;
492
+ completedToday = { ...parsed.byRunnerType };
493
+ todayDuration = buildDurationResult(parsed.duration);
494
+
495
+ const hourlyArrays = buildHourlyTrendArrays(parsed);
496
+
497
+ // SSE 格式:简化为 {hour, count},只取当天
498
+ hourlyTrend = hourlyArrays.hourlyTrend
499
+ .filter(h => h.date === todayStr)
500
+ .map(({ hour, total }) => ({ hour, count: total }));
501
+
502
+ hourlyTrendByStatus = hourlyArrays.hourlyTrendByStatus
503
+ .filter(h => h.date === todayStr)
504
+ .map(({ hour, status, count }) => ({ hour, status, count }));
505
+
506
+ hourlyTrendByType = hourlyArrays.hourlyTrendByType
507
+ .filter(h => h.date === todayStr)
508
+ .map(({ hour, type, count }) => ({ hour, type, count }));
509
+
510
+ intervalTrend = hourlyTrend.map(({ hour, count }) => ({
511
+ interval: `${String(hour).padStart(2, '0')}:00`,
512
+ count
513
+ }));
514
+
515
+ for (const [rtName, rt] of Object.entries(parsed.duration.byRunnerType)) {
516
+ completedTodayTotalDurationMsByRunnerType[rtName] = rt.sumTotal;
517
+ }
389
518
  }
519
+ } catch (e) {
520
+ fastify.log.error(`SSE统计查询失败: ${e.message}`);
390
521
  }
391
522
 
392
- const durationTrend = Object.entries(durationByDate)
393
- .map(([date, byRunnerType]) => ({ date, byRunnerType }))
394
- .sort((a, b) => a.date.localeCompare(b.date));
523
+ // 补充当前小时的 running/waiting 瞬时状态(来自 DB 实时查询,不受 statistics 服务影响)
524
+ // 查询用客户端时间:当前小时使用客户端时区
525
+ const currentHour = now.hour();
526
+ if (dbStatusCount.running > 0) hourlyTrendByStatus.push({ hour: currentHour, status: 'running', count: dbStatusCount.running });
527
+ if (dbWaitingTotal > 0) hourlyTrendByStatus.push({ hour: currentHour, status: 'waiting', count: dbWaitingTotal });
395
528
 
396
529
  return {
397
- totalTasks, byStatus, byType,
398
- recentTrend, recentTrendByType, durationTrend, hourlyCompletionTrend
530
+ date: todayStr,
531
+ totalTasks,
532
+ byStatus,
533
+ byType,
534
+ byRunnerType,
535
+ pendingByRunnerType,
536
+ waitingByRunnerType,
537
+ runningByRunnerType,
538
+ completedToday,
539
+ waitingQueueMaxWaitMsByRunnerType,
540
+ completedTodayTotalDurationMsByRunnerType,
541
+ runnerTypeStats,
542
+ hourlyTrend,
543
+ hourlyTrendByStatus,
544
+ hourlyTrendByType,
545
+ intervalTrend,
546
+ todayDuration
399
547
  };
400
548
  };
401
549
 
402
- /** HTTP 接口:历史统计概览 */
403
- const queryStatistics = async ({ range = '7d', timezone, type, runnerType }) => {
404
- return await queryHistoryData({ range, timezone, type, runnerType });
405
- };
406
-
407
- /** SSE 接口:实时统计推送 */
408
- const sseStatistics = async ({ timezone, interval }, reply) => {
550
+ // ─── sseStatistics ───
551
+ const sseStatistics = async ({ range = '7d', timezone, type, runnerType, interval }, reply) => {
409
552
  await statisticsServices.sseStream.send(reply, {
410
- name: 'query', params: { timezone },
411
- fetchData: async () => await queryRealtimeData({ timezone }),
553
+ name: 'taskStatistics',
554
+ params: { type, runnerType, timezone },
555
+ fetchData: async ({ type, runnerType, timezone } = {}) => {
556
+ try {
557
+ return await buildSseData({ timezone, type, runnerType });
558
+ } catch (e) {
559
+ fastify.log.error(`SSE获取数据失败: ${e.message}`);
560
+ return {};
561
+ }
562
+ },
412
563
  interval: interval || 5
413
564
  });
414
565
  };
415
566
 
416
- Object.assign(fastify[options.name].services, { queryStatistics, sseStatistics });
567
+ Object.assign(fastify[options.name].services, {
568
+ queryStatistics,
569
+ sseStatistics
570
+ });
417
571
  });