@kne/fastify-task 3.0.0-alpha.1 → 3.0.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1052 -255
- package/index.js +14 -4
- package/libs/controllers/main.js +4 -3
- package/libs/helpers/task-service-context.js +556 -0
- package/libs/models/task.js +6 -1
- package/libs/services/append.js +45 -0
- package/libs/services/callback.js +109 -0
- package/libs/services/executor.js +10 -0
- package/libs/services/main.js +7 -674
- package/libs/services/scheduler.js +230 -0
- package/libs/services/statistics.js +614 -38
- package/libs/services/task.js +224 -0
- package/package.json +3 -2
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
|
+
const dayjs = require('dayjs');
|
|
3
|
+
const utc = require('dayjs/plugin/utc');
|
|
4
|
+
const timezone = require('dayjs/plugin/timezone');
|
|
5
|
+
|
|
6
|
+
dayjs.extend(utc);
|
|
7
|
+
dayjs.extend(timezone);
|
|
2
8
|
|
|
3
9
|
const RANGE_MAP = {
|
|
4
10
|
'7d': { value: 7, unit: 'day', label: '近7天' },
|
|
@@ -7,62 +13,632 @@ const RANGE_MAP = {
|
|
|
7
13
|
'1y': { value: 1, unit: 'year', label: '近1年' }
|
|
8
14
|
};
|
|
9
15
|
|
|
10
|
-
const
|
|
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) => {
|
|
11
21
|
const config = RANGE_MAP[range];
|
|
12
22
|
if (!config) {
|
|
13
23
|
throw new Error(`不支持的时间范围: ${range}, 支持: ${Object.keys(RANGE_MAP).join(',')}`);
|
|
14
24
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
25
|
+
// 写入数据全使用服务器时间,查询用客户端时间
|
|
26
|
+
// 使用客户端时区计算"现在"和"起始时间",确保日期边界对齐客户端视角
|
|
27
|
+
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 };
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// 查询用客户端时间:formatDate 使用客户端时区格式化日期
|
|
35
|
+
const formatDate = (date, tz) => {
|
|
36
|
+
const d = tz ? dayjs(date).tz(tz) : dayjs(date);
|
|
37
|
+
return d.format('YYYY-MM-DD');
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// 查询用客户端时间:提取客户端时区下的小时
|
|
41
|
+
const getHour = (date, tz) => {
|
|
42
|
+
const d = tz ? dayjs(date).tz(tz) : dayjs(date);
|
|
43
|
+
return d.hour();
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const safeNum = v => {
|
|
47
|
+
const n = Number(v);
|
|
48
|
+
return (typeof n === 'number' && !isNaN(n) ? n : 0);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const getSumData = item => (item.data || {}).sum || item.data || {};
|
|
52
|
+
|
|
53
|
+
const computeAvg = (sum, count) => (count > 0 ? Math.round(sum / count) : 0);
|
|
54
|
+
|
|
55
|
+
// 将原始时长累加器 {count, sum*} 转为输出格式 {count, avg*}
|
|
56
|
+
const toAvgDuration = ({ count, sumWaiting, sumExecution, sumTotal }) => ({
|
|
57
|
+
count,
|
|
58
|
+
avgWaitingTime: computeAvg(sumWaiting, count),
|
|
59
|
+
avgExecutionTime: computeAvg(sumExecution, count),
|
|
60
|
+
avgTotalTime: computeAvg(sumTotal, count)
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const parseChannel = channel => {
|
|
64
|
+
const parts = channel.split(':');
|
|
65
|
+
const hour = Number(parts[2]);
|
|
66
|
+
return {
|
|
67
|
+
type: parts[0],
|
|
68
|
+
runnerType: parts.length >= 2 ? parts[1] : null,
|
|
69
|
+
completedHour: parts.length >= 3 && Number.isInteger(hour) && hour >= 0 && hour < 24 ? hour : null
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const buildChannels = async (statisticsServices, type, runnerType) => {
|
|
74
|
+
const metaResult = await statisticsServices.channelMeta.list();
|
|
75
|
+
const list = Array.isArray(metaResult) ? metaResult : metaResult.list || [];
|
|
76
|
+
const rootChannels = list.map(meta => meta.channel).filter(Boolean);
|
|
77
|
+
const types = type ? rootChannels.filter(ch => ch === type) : rootChannels;
|
|
78
|
+
const rts = runnerType ? [runnerType] : RUNNER_TYPES;
|
|
79
|
+
const channels = [];
|
|
80
|
+
for (const t of types) {
|
|
81
|
+
channels.push(t);
|
|
82
|
+
for (const rt of rts) channels.push(`${t}:${rt}`);
|
|
83
|
+
}
|
|
84
|
+
return channels;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const buildHourlyChannels = (channels, runnerType) => {
|
|
88
|
+
const types = [...new Set(channels.map(ch => ch.split(':')[0]).filter(Boolean))];
|
|
89
|
+
const runnerTypes = runnerType ? [runnerType] : RUNNER_TYPES;
|
|
90
|
+
const hourlyChannels = [];
|
|
91
|
+
for (const type of types) {
|
|
92
|
+
for (const rt of runnerTypes) {
|
|
93
|
+
for (let hour = 0; hour < 24; hour += 1) {
|
|
94
|
+
hourlyChannels.push(`${type}:${rt}:${hour}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
23
97
|
}
|
|
24
|
-
return
|
|
98
|
+
return hourlyChannels;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// 确保时长累加器存在于 map 中
|
|
102
|
+
const ensureDurationAcc = (map, key) => {
|
|
103
|
+
if (!map[key]) map[key] = { count: 0, sumWaiting: 0, sumExecution: 0, sumTotal: 0 };
|
|
104
|
+
return map[key];
|
|
25
105
|
};
|
|
26
106
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
107
|
+
// 累加时长到累加器
|
|
108
|
+
const accumDuration = (acc, count, sum) => {
|
|
109
|
+
acc.count += count;
|
|
110
|
+
acc.sumWaiting += safeNum(sum.waitingTime);
|
|
111
|
+
acc.sumExecution += safeNum(sum.executionTime);
|
|
112
|
+
acc.sumTotal += safeNum(sum.totalTime);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// 创建空的 dailyMap 条目
|
|
116
|
+
const createDailyMapEntry = () => ({ total: 0, byStatus: { success: 0, failed: 0, canceled: 0 }, byType: {} });
|
|
117
|
+
|
|
118
|
+
// 创建空的 dailyDurationMap 条目
|
|
119
|
+
const createDailyDurationEntry = () => ({
|
|
120
|
+
completedCount: 0, successCount: 0, failedCount: 0, canceledCount: 0,
|
|
121
|
+
sumWaiting: 0, sumExecution: 0, sumTotal: 0, countWithTiming: 0,
|
|
122
|
+
byType: {}, byRunnerType: {}, byTypeByRunnerType: {}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const createHourlyAccumulator = () => ({
|
|
126
|
+
hourlyMap: {},
|
|
127
|
+
hourlyStatusMap: {},
|
|
128
|
+
hourlyTypeMap: {},
|
|
129
|
+
hourlyDetailMap: {}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
const mergeHourlyItem = (result, { date, hour, typeName, runnerTypeName, count, sum, includeSummary = true }) => {
|
|
133
|
+
if (includeSummary) {
|
|
134
|
+
if (!result.hourlyMap[date]) result.hourlyMap[date] = {};
|
|
135
|
+
result.hourlyMap[date][hour] = (result.hourlyMap[date][hour] || 0) + count;
|
|
136
|
+
|
|
137
|
+
if (!result.hourlyStatusMap[date]) result.hourlyStatusMap[date] = {};
|
|
138
|
+
if (!result.hourlyStatusMap[date][hour]) result.hourlyStatusMap[date][hour] = {};
|
|
139
|
+
for (const status of ['success', 'failed', 'canceled']) {
|
|
140
|
+
const sc = safeNum(sum[status]);
|
|
141
|
+
if (sc > 0) result.hourlyStatusMap[date][hour][status] = (result.hourlyStatusMap[date][hour][status] || 0) + sc;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (!result.hourlyTypeMap[date]) result.hourlyTypeMap[date] = {};
|
|
145
|
+
if (!result.hourlyTypeMap[date][hour]) result.hourlyTypeMap[date][hour] = {};
|
|
146
|
+
if (count > 0) result.hourlyTypeMap[date][hour][typeName] = (result.hourlyTypeMap[date][hour][typeName] || 0) + count;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (runnerTypeName) {
|
|
150
|
+
if (!result.hourlyDetailMap[date]) result.hourlyDetailMap[date] = {};
|
|
151
|
+
if (!result.hourlyDetailMap[date][hour]) result.hourlyDetailMap[date][hour] = {};
|
|
152
|
+
if (!result.hourlyDetailMap[date][hour][typeName]) result.hourlyDetailMap[date][hour][typeName] = {};
|
|
153
|
+
if (!result.hourlyDetailMap[date][hour][typeName][runnerTypeName]) {
|
|
154
|
+
result.hourlyDetailMap[date][hour][typeName][runnerTypeName] = { total: 0, success: 0, failed: 0, canceled: 0 };
|
|
155
|
+
}
|
|
156
|
+
const detail = result.hourlyDetailMap[date][hour][typeName][runnerTypeName];
|
|
157
|
+
detail.total += count;
|
|
158
|
+
detail.success += safeNum(sum.success);
|
|
159
|
+
detail.failed += safeNum(sum.failed);
|
|
160
|
+
detail.canceled += safeNum(sum.canceled);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
// 查询 statistics 并解析结果
|
|
165
|
+
// 写入数据全使用服务器时间(DB 中存储的是服务器时间),查询用客户端时间转换展示
|
|
166
|
+
const queryAndParse = async (statisticsServices, { channels, startTime, endTime, timezone: tz, runnerType: runnerTypeFilter }) => {
|
|
167
|
+
if (channels.length === 0) return null;
|
|
168
|
+
|
|
169
|
+
const statResult = await statisticsServices.query({
|
|
170
|
+
channels, startTime, endTime,
|
|
171
|
+
attributeNames: STAT_ATTRS,
|
|
172
|
+
aggregates: ['sum'],
|
|
173
|
+
timezone: tz || undefined
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
const result = {
|
|
177
|
+
totalTasks: 0,
|
|
178
|
+
byStatus: { success: 0, failed: 0, canceled: 0 },
|
|
179
|
+
byType: {},
|
|
180
|
+
byRunnerType: {},
|
|
181
|
+
dailyMap: {},
|
|
182
|
+
hourlyMap: {},
|
|
183
|
+
hourlyStatusMap: {},
|
|
184
|
+
hourlyTypeMap: {},
|
|
185
|
+
hourlyDetailMap: {},
|
|
186
|
+
duration: {
|
|
187
|
+
completedCount: 0, successCount: 0, failedCount: 0, canceledCount: 0,
|
|
188
|
+
sumWaiting: 0, sumExecution: 0, sumTotal: 0, countWithTiming: 0,
|
|
189
|
+
byType: {}, byRunnerType: {}, byTypeByRunnerType: {}
|
|
190
|
+
},
|
|
191
|
+
durationTrend: []
|
|
192
|
+
};
|
|
193
|
+
const dailyDurationMap = {};
|
|
194
|
+
|
|
195
|
+
for (const item of statResult.list || []) {
|
|
196
|
+
const { type: typeName, runnerType: runnerTypeName } = parseChannel(item.channel);
|
|
197
|
+
if (runnerTypeFilter && runnerTypeName && runnerTypeName !== runnerTypeFilter) continue;
|
|
198
|
+
if (runnerTypeFilter && !runnerTypeName) continue;
|
|
199
|
+
|
|
200
|
+
const sum = getSumData(item);
|
|
201
|
+
const count = safeNum(sum.total);
|
|
202
|
+
const isHourly = item.period === 'h';
|
|
203
|
+
// 查询用客户端时间:日期和小时使用客户端时区转换
|
|
204
|
+
const date = formatDate(item.time, tz);
|
|
205
|
+
const hour = isHourly ? getHour(item.time, tz) : null;
|
|
206
|
+
|
|
207
|
+
// ─── 1-segment channel: 全局 + 按日 + 时长 聚合 ───
|
|
208
|
+
if (!runnerTypeName) {
|
|
209
|
+
// 全局汇总
|
|
210
|
+
result.totalTasks += count;
|
|
211
|
+
result.byStatus.success += safeNum(sum.success);
|
|
212
|
+
result.byStatus.failed += safeNum(sum.failed);
|
|
213
|
+
result.byStatus.canceled += safeNum(sum.canceled);
|
|
214
|
+
result.byType[typeName] = (result.byType[typeName] || 0) + count;
|
|
215
|
+
|
|
216
|
+
// 全局时长
|
|
217
|
+
result.duration.completedCount += count;
|
|
218
|
+
result.duration.successCount += safeNum(sum.success);
|
|
219
|
+
result.duration.failedCount += safeNum(sum.failed);
|
|
220
|
+
result.duration.canceled += safeNum(sum.canceled);
|
|
221
|
+
result.duration.sumWaiting += safeNum(sum.waitingTime);
|
|
222
|
+
result.duration.sumExecution += safeNum(sum.executionTime);
|
|
223
|
+
result.duration.sumTotal += safeNum(sum.totalTime);
|
|
224
|
+
result.duration.countWithTiming += count;
|
|
225
|
+
if (count > 0) accumDuration(ensureDurationAcc(result.duration.byType, typeName), count, sum);
|
|
226
|
+
|
|
227
|
+
// 按日汇总
|
|
228
|
+
if (!result.dailyMap[date]) result.dailyMap[date] = createDailyMapEntry();
|
|
229
|
+
const dm = result.dailyMap[date];
|
|
230
|
+
dm.total += count;
|
|
231
|
+
dm.byStatus.success += safeNum(sum.success);
|
|
232
|
+
dm.byStatus.failed += safeNum(sum.failed);
|
|
233
|
+
dm.byStatus.canceled += safeNum(sum.canceled);
|
|
234
|
+
dm.byType[typeName] = (dm.byType[typeName] || 0) + count;
|
|
235
|
+
|
|
236
|
+
// 按日时长
|
|
237
|
+
if (!dailyDurationMap[date]) dailyDurationMap[date] = createDailyDurationEntry();
|
|
238
|
+
const dd = dailyDurationMap[date];
|
|
239
|
+
dd.completedCount += count;
|
|
240
|
+
dd.successCount += safeNum(sum.success);
|
|
241
|
+
dd.failedCount += safeNum(sum.failed);
|
|
242
|
+
dd.canceledCount += safeNum(sum.canceled);
|
|
243
|
+
dd.sumWaiting += safeNum(sum.waitingTime);
|
|
244
|
+
dd.sumExecution += safeNum(sum.executionTime);
|
|
245
|
+
dd.sumTotal += safeNum(sum.totalTime);
|
|
246
|
+
dd.countWithTiming += count;
|
|
247
|
+
if (count > 0) accumDuration(ensureDurationAcc(dd.byType, typeName), count, sum);
|
|
248
|
+
|
|
249
|
+
// 小时级趋势(仅 period='h')
|
|
250
|
+
if (isHourly) {
|
|
251
|
+
mergeHourlyItem(result, { date, hour, typeName, runnerTypeName: null, count, sum });
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// ─── 2-segment channel: byRunnerType 聚合 ───
|
|
256
|
+
if (runnerTypeName) {
|
|
257
|
+
result.byRunnerType[runnerTypeName] = (result.byRunnerType[runnerTypeName] || 0) + count;
|
|
258
|
+
|
|
259
|
+
if (count > 0) {
|
|
260
|
+
accumDuration(ensureDurationAcc(result.duration.byRunnerType, runnerTypeName), count, sum);
|
|
261
|
+
|
|
262
|
+
// byTypeByRunnerType(覆盖写入,非累加)
|
|
263
|
+
if (!result.duration.byTypeByRunnerType[runnerTypeName]) result.duration.byTypeByRunnerType[runnerTypeName] = {};
|
|
264
|
+
result.duration.byTypeByRunnerType[runnerTypeName][typeName] = {
|
|
265
|
+
count,
|
|
266
|
+
sumWaiting: safeNum(sum.waitingTime),
|
|
267
|
+
sumExecution: safeNum(sum.executionTime),
|
|
268
|
+
sumTotal: safeNum(sum.totalTime)
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
// 按日 byRunnerType 时长
|
|
272
|
+
if (dailyDurationMap[date]) {
|
|
273
|
+
accumDuration(ensureDurationAcc(dailyDurationMap[date].byRunnerType, runnerTypeName), count, sum);
|
|
274
|
+
if (!dailyDurationMap[date].byTypeByRunnerType[runnerTypeName]) {
|
|
275
|
+
dailyDurationMap[date].byTypeByRunnerType[runnerTypeName] = {};
|
|
276
|
+
}
|
|
277
|
+
accumDuration(
|
|
278
|
+
ensureDurationAcc(dailyDurationMap[date].byTypeByRunnerType[runnerTypeName], typeName),
|
|
279
|
+
count,
|
|
280
|
+
sum
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// hourlyDetailMap(仅 period='h')
|
|
286
|
+
if (isHourly) {
|
|
287
|
+
mergeHourlyItem(result, { date, hour, typeName, runnerTypeName, count, sum, includeSummary: false });
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// 构建按日时长趋势
|
|
293
|
+
result.durationTrend = Object.entries(dailyDurationMap)
|
|
294
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
295
|
+
.map(([date, dd]) => ({
|
|
296
|
+
date,
|
|
297
|
+
completedCount: dd.completedCount,
|
|
298
|
+
successCount: dd.successCount,
|
|
299
|
+
failedCount: dd.failedCount,
|
|
300
|
+
canceledCount: dd.canceledCount,
|
|
301
|
+
avgWaitingTime: computeAvg(dd.sumWaiting, dd.countWithTiming),
|
|
302
|
+
avgExecutionTime: computeAvg(dd.sumExecution, dd.countWithTiming),
|
|
303
|
+
avgTotalTime: computeAvg(dd.sumTotal, dd.countWithTiming),
|
|
304
|
+
byType: Object.fromEntries(Object.entries(dd.byType).map(([n, td]) => [n, toAvgDuration(td)])),
|
|
305
|
+
byRunnerType: Object.fromEntries(Object.entries(dd.byRunnerType).map(([n, rt]) => [n, toAvgDuration(rt)])),
|
|
306
|
+
byTypeByRunnerType: Object.fromEntries(
|
|
307
|
+
Object.entries(dd.byTypeByRunnerType).map(([rtName, types]) => [
|
|
308
|
+
rtName,
|
|
309
|
+
Object.fromEntries(Object.entries(types).map(([typeName, td]) => [typeName, toAvgDuration(td)]))
|
|
310
|
+
])
|
|
311
|
+
)
|
|
312
|
+
}));
|
|
313
|
+
|
|
314
|
+
return result;
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const queryHourlyChildChannels = async (statisticsServices, { channels, startTime, endTime, timezone: tz, runnerType: runnerTypeFilter }) => {
|
|
318
|
+
const hourlyChannels = buildHourlyChannels(channels, runnerTypeFilter);
|
|
319
|
+
if (hourlyChannels.length === 0) return null;
|
|
320
|
+
|
|
321
|
+
const statResult = await statisticsServices.query({
|
|
322
|
+
channels: hourlyChannels, startTime, endTime,
|
|
323
|
+
attributeNames: STAT_ATTRS,
|
|
324
|
+
aggregates: ['sum'],
|
|
325
|
+
timezone: tz || undefined
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
const result = createHourlyAccumulator();
|
|
329
|
+
for (const item of statResult.list || []) {
|
|
330
|
+
const { type: typeName, runnerType: runnerTypeName, completedHour } = parseChannel(item.channel);
|
|
331
|
+
if (completedHour == null) continue;
|
|
332
|
+
if (runnerTypeFilter && runnerTypeName !== runnerTypeFilter) continue;
|
|
333
|
+
|
|
334
|
+
const sum = getSumData(item);
|
|
335
|
+
const count = safeNum(sum.total);
|
|
336
|
+
const date = formatDate(item.time, tz);
|
|
337
|
+
mergeHourlyItem(result, {
|
|
338
|
+
date,
|
|
339
|
+
hour: completedHour,
|
|
340
|
+
typeName,
|
|
341
|
+
runnerTypeName,
|
|
342
|
+
count,
|
|
343
|
+
sum
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return result;
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
// 构建 todayDuration 结果
|
|
351
|
+
const buildDurationResult = (d) => ({
|
|
352
|
+
completedCount: d.completedCount,
|
|
353
|
+
successCount: d.successCount,
|
|
354
|
+
failedCount: d.failedCount,
|
|
355
|
+
canceledCount: d.canceledCount,
|
|
356
|
+
avgWaitingTime: computeAvg(d.sumWaiting, d.countWithTiming),
|
|
357
|
+
avgExecutionTime: computeAvg(d.sumExecution, d.countWithTiming),
|
|
358
|
+
avgTotalTime: computeAvg(d.sumTotal, d.countWithTiming),
|
|
359
|
+
byType: Object.fromEntries(Object.entries(d.byType).map(([n, td]) => [n, toAvgDuration(td)])),
|
|
360
|
+
byRunnerType: Object.fromEntries(Object.entries(d.byRunnerType).map(([n, rt]) => [n, toAvgDuration(rt)])),
|
|
361
|
+
byTypeByRunnerType: Object.fromEntries(
|
|
362
|
+
Object.entries(d.byTypeByRunnerType).map(([rtName, types]) => [
|
|
363
|
+
rtName,
|
|
364
|
+
Object.fromEntries(Object.entries(types).map(([typeName, td]) => [typeName, toAvgDuration(td)]))
|
|
365
|
+
])
|
|
366
|
+
)
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
// 构建小时趋势数组(完整格式,供 history 接口使用)
|
|
370
|
+
const buildHourlyTrendArrays = (parsed) => {
|
|
371
|
+
const hourlyTrend = [];
|
|
372
|
+
const hourlyTrendByStatus = [];
|
|
373
|
+
const hourlyTrendByType = [];
|
|
374
|
+
const hourlyCompletionTrend = [];
|
|
375
|
+
|
|
376
|
+
const dates = Object.keys(parsed.hourlyMap).sort();
|
|
377
|
+
for (const date of dates) {
|
|
378
|
+
const hoursMap = parsed.hourlyMap[date] || {};
|
|
379
|
+
const statusMap = parsed.hourlyStatusMap[date] || {};
|
|
380
|
+
const typeMap = parsed.hourlyTypeMap[date] || {};
|
|
381
|
+
const detailMap = parsed.hourlyDetailMap[date] || {};
|
|
382
|
+
|
|
383
|
+
const allHours = [...new Set([
|
|
384
|
+
...Object.keys(hoursMap).map(Number),
|
|
385
|
+
...Object.keys(statusMap).map(Number),
|
|
386
|
+
...Object.keys(typeMap).map(Number),
|
|
387
|
+
...Object.keys(detailMap).map(Number)
|
|
388
|
+
])].sort((a, b) => a - b);
|
|
389
|
+
|
|
390
|
+
for (const hour of allHours) {
|
|
391
|
+
const statuses = statusMap[hour] || {};
|
|
392
|
+
hourlyTrend.push({
|
|
393
|
+
date, hour,
|
|
394
|
+
total: hoursMap[hour] || 0,
|
|
395
|
+
success: statuses.success || 0,
|
|
396
|
+
failed: statuses.failed || 0,
|
|
397
|
+
canceled: statuses.canceled || 0
|
|
398
|
+
});
|
|
399
|
+
for (const [status, count] of Object.entries(statuses)) {
|
|
400
|
+
if (count > 0) hourlyTrendByStatus.push({ date, hour, status, count });
|
|
401
|
+
}
|
|
402
|
+
const types = typeMap[hour] || {};
|
|
403
|
+
for (const [typeName, count] of Object.entries(types)) {
|
|
404
|
+
if (count > 0) hourlyTrendByType.push({ date, hour, type: typeName, count });
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
for (const hour of Object.keys(detailMap).map(Number).sort((a, b) => a - b)) {
|
|
409
|
+
const types = detailMap[hour] || {};
|
|
410
|
+
for (const [typeName, runnerTypes] of Object.entries(types)) {
|
|
411
|
+
for (const [runnerTypeName, detail] of Object.entries(runnerTypes)) {
|
|
412
|
+
if (detail.total > 0) {
|
|
413
|
+
hourlyCompletionTrend.push({
|
|
414
|
+
date, hour, type: typeName, runnerType: runnerTypeName,
|
|
415
|
+
totalCompleted: detail.total,
|
|
416
|
+
successCount: detail.success,
|
|
417
|
+
failedCount: detail.failed,
|
|
418
|
+
canceledCount: detail.canceled
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
return { hourlyTrend, hourlyTrendByStatus, hourlyTrendByType, hourlyCompletionTrend };
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
// 构建历史日趋势数组
|
|
430
|
+
const buildDailyTrendArrays = (parsed) => {
|
|
431
|
+
const sorted = Object.entries(parsed.dailyMap).sort(([a], [b]) => a.localeCompare(b));
|
|
432
|
+
const recentTrend = sorted.map(([date, d]) => ({ date, count: d.total }));
|
|
433
|
+
const recentTrendByStatus = [];
|
|
434
|
+
const recentTrendByType = [];
|
|
435
|
+
sorted.forEach(([date, d]) => {
|
|
436
|
+
Object.entries(d.byStatus).forEach(([status, count]) => recentTrendByStatus.push({ date, status, count }));
|
|
437
|
+
Object.entries(d.byType).forEach(([type, count]) => recentTrendByType.push({ date, type, count }));
|
|
438
|
+
});
|
|
439
|
+
return { recentTrend, recentTrendByStatus, recentTrendByType };
|
|
31
440
|
};
|
|
32
441
|
|
|
33
442
|
module.exports = fp(async (fastify, options) => {
|
|
34
443
|
const statisticsServices = fastify[`${options.name}Statistics`].services;
|
|
444
|
+
const { task: TaskModel } = fastify[options.name].models;
|
|
445
|
+
const { Op, fn, col } = fastify.sequelize.Sequelize;
|
|
35
446
|
|
|
447
|
+
// ─── queryStatistics (history 接口) ───
|
|
36
448
|
const queryStatistics = async ({ range = '7d', timezone, type, runnerType }) => {
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
449
|
+
// 查询用客户端时间:parseRange 传入 timezone
|
|
450
|
+
const { startTime, endTime, label, range: rangeKey } = parseRange(range, timezone);
|
|
451
|
+
const emptyResult = {
|
|
452
|
+
range: rangeKey, rangeLabel: label,
|
|
453
|
+
totalTasks: 0, byStatus: {}, byType: {}, byRunnerType: {},
|
|
454
|
+
recentTrend: [], recentTrendByStatus: [], recentTrendByType: [],
|
|
455
|
+
durationTrend: [], hourlyCompletionTrend: []
|
|
456
|
+
};
|
|
457
|
+
try {
|
|
458
|
+
const channels = await buildChannels(statisticsServices, type, runnerType);
|
|
459
|
+
const parsed = await queryAndParse(statisticsServices, { channels, startTime, endTime, timezone, runnerType });
|
|
460
|
+
if (!parsed) return emptyResult;
|
|
461
|
+
const hourlyParsed = await queryHourlyChildChannels(statisticsServices, { channels, startTime, endTime, timezone, runnerType });
|
|
462
|
+
|
|
463
|
+
const { recentTrend, recentTrendByStatus, recentTrendByType } = buildDailyTrendArrays(parsed);
|
|
464
|
+
const { hourlyTrend, hourlyCompletionTrend } = buildHourlyTrendArrays(hourlyParsed || createHourlyAccumulator());
|
|
465
|
+
|
|
466
|
+
return {
|
|
467
|
+
range: rangeKey, rangeLabel: label,
|
|
468
|
+
totalTasks: parsed.totalTasks,
|
|
469
|
+
byStatus: parsed.byStatus,
|
|
470
|
+
byType: parsed.byType,
|
|
471
|
+
byRunnerType: parsed.byRunnerType,
|
|
472
|
+
recentTrend, recentTrendByStatus, recentTrendByType,
|
|
473
|
+
durationTrend: parsed.durationTrend,
|
|
474
|
+
hourlyTrend, hourlyCompletionTrend
|
|
475
|
+
};
|
|
476
|
+
} catch (e) {
|
|
477
|
+
fastify.log.error(`查询统计数据失败: ${e.message}`);
|
|
478
|
+
return emptyResult;
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
// ─── buildSseData (SSE 接口) ───
|
|
483
|
+
const buildSseData = async ({ timezone: tz, type, runnerType }) => {
|
|
484
|
+
// 查询用客户端时间:"今天"的边界使用客户端时区
|
|
485
|
+
const now = tz ? dayjs().tz(tz) : dayjs();
|
|
486
|
+
const todayStart = now.startOf('day').toDate();
|
|
487
|
+
const todayEnd = now.endOf('day').toDate();
|
|
488
|
+
const todayStr = now.format('YYYY-MM-DD');
|
|
489
|
+
|
|
490
|
+
// 统计查询:正在执行和等待执行的数量 + DB 查询
|
|
491
|
+
const [runnerTypeStatusRows, waitingTasks] = await Promise.all([
|
|
492
|
+
TaskModel.findAll({
|
|
493
|
+
attributes: ['runnerType', 'status', [fn('COUNT', col('id')), 'count']],
|
|
494
|
+
where: { ...(runnerType ? { runnerType } : {}) },
|
|
495
|
+
group: ['runnerType', 'status'], raw: true
|
|
496
|
+
}),
|
|
497
|
+
TaskModel.findAll({
|
|
498
|
+
attributes: ['runnerType', 'createdAt'],
|
|
499
|
+
where: { status: { [Op.in]: ['pending', 'waiting'] }, ...(runnerType ? { runnerType } : {}) },
|
|
500
|
+
raw: true
|
|
501
|
+
})
|
|
502
|
+
]);
|
|
503
|
+
|
|
504
|
+
const pendingByRunnerType = {};
|
|
505
|
+
const waitingByRunnerType = {};
|
|
506
|
+
const runningByRunnerType = {};
|
|
507
|
+
|
|
508
|
+
const nowMs = Date.now();
|
|
509
|
+
const waitingQueueMaxWaitMsByRunnerType = {};
|
|
510
|
+
waitingTasks.forEach(r => {
|
|
511
|
+
const rt = r.runnerType || 'manual';
|
|
512
|
+
const waitMs = nowMs - new Date(r.createdAt).getTime();
|
|
513
|
+
if (!waitingQueueMaxWaitMsByRunnerType[rt] || waitMs > waitingQueueMaxWaitMsByRunnerType[rt]) {
|
|
514
|
+
waitingQueueMaxWaitMsByRunnerType[rt] = waitMs;
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
const runnerTypeStats = {};
|
|
519
|
+
const dbStatusCount = { pending: 0, waiting: 0, running: 0, success: 0, failed: 0, canceled: 0 };
|
|
520
|
+
runnerTypeStatusRows.forEach(r => {
|
|
521
|
+
if (!runnerTypeStats[r.runnerType]) runnerTypeStats[r.runnerType] = { total: 0, pending: 0, waiting: 0, waitingCount: 0, executed: 0 };
|
|
522
|
+
const count = Number(r.count);
|
|
523
|
+
const rt = r.runnerType || 'manual';
|
|
524
|
+
runnerTypeStats[r.runnerType].total += count;
|
|
525
|
+
if (r.status === 'pending') runnerTypeStats[r.runnerType].pending += count;
|
|
526
|
+
if (['success', 'failed', 'canceled', 'running'].includes(r.status)) runnerTypeStats[r.runnerType].executed += count;
|
|
527
|
+
if (dbStatusCount[r.status] !== undefined) dbStatusCount[r.status] += count;
|
|
528
|
+
// 按 runnerType 汇总 running/waiting/pending(来自 DB 实时状态,避免 delta 脏数据)
|
|
529
|
+
if (r.status === 'running') runningByRunnerType[rt] = (runningByRunnerType[rt] || 0) + count;
|
|
530
|
+
if (r.status === 'waiting') waitingByRunnerType[rt] = (waitingByRunnerType[rt] || 0) + count;
|
|
531
|
+
if (r.status === 'pending') pendingByRunnerType[rt] = (pendingByRunnerType[rt] || 0) + count;
|
|
532
|
+
// pending 状态等同于"等待操作",计入 waitingByRunnerType 和 runnerTypeStats.waiting/waitingCount
|
|
533
|
+
if (r.status === 'pending') {
|
|
534
|
+
waitingByRunnerType[rt] = (waitingByRunnerType[rt] || 0) + count;
|
|
535
|
+
runnerTypeStats[r.runnerType].waiting += count;
|
|
536
|
+
runnerTypeStats[r.runnerType].waitingCount += count;
|
|
537
|
+
}
|
|
538
|
+
if (r.status === 'waiting') {
|
|
539
|
+
runnerTypeStats[r.runnerType].waiting += count;
|
|
540
|
+
runnerTypeStats[r.runnerType].waitingCount += count;
|
|
541
|
+
}
|
|
47
542
|
});
|
|
543
|
+
|
|
544
|
+
// Statistics 查询:已完成数据
|
|
545
|
+
// 默认值:DB 实时数据(即使 statistics 服务不可用也能展示 running/waiting/pending)
|
|
546
|
+
// pending 全部合并到 waiting:前端"等待操作"只有 waiting 类别,不展示独立的 pending
|
|
547
|
+
const dbWaitingTotal = dbStatusCount.waiting + dbStatusCount.pending;
|
|
548
|
+
let totalTasks = dbStatusCount.running + dbWaitingTotal;
|
|
549
|
+
let byStatus = { success: 0, failed: 0, canceled: 0, running: dbStatusCount.running, waiting: dbWaitingTotal, pending: 0 };
|
|
550
|
+
let byType = {};
|
|
551
|
+
let byRunnerType = {};
|
|
552
|
+
let completedToday = {};
|
|
553
|
+
let completedTodayTotalDurationMsByRunnerType = {};
|
|
554
|
+
let hourlyTrend = [];
|
|
555
|
+
let hourlyTrendByStatus = [];
|
|
556
|
+
let hourlyTrendByType = [];
|
|
557
|
+
let intervalTrend = [];
|
|
558
|
+
let todayDuration = {
|
|
559
|
+
completedCount: 0, successCount: 0, failedCount: 0, canceledCount: 0,
|
|
560
|
+
avgWaitingTime: 0, avgExecutionTime: 0, avgTotalTime: 0,
|
|
561
|
+
byType: {}, byRunnerType: {}, byTypeByRunnerType: {}
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
try {
|
|
565
|
+
const channels = await buildChannels(statisticsServices, type, runnerType);
|
|
566
|
+
const parsed = await queryAndParse(statisticsServices, { channels, startTime: todayStart, endTime: todayEnd, timezone: tz, runnerType });
|
|
567
|
+
if (parsed) {
|
|
568
|
+
totalTasks = parsed.totalTasks + dbStatusCount.running + dbWaitingTotal;
|
|
569
|
+
byStatus = { ...parsed.byStatus, running: dbStatusCount.running, waiting: dbWaitingTotal, pending: 0 };
|
|
570
|
+
byType = parsed.byType;
|
|
571
|
+
byRunnerType = parsed.byRunnerType;
|
|
572
|
+
completedToday = { ...parsed.byRunnerType };
|
|
573
|
+
todayDuration = buildDurationResult(parsed.duration);
|
|
574
|
+
|
|
575
|
+
const hourlyArrays = buildHourlyTrendArrays(parsed);
|
|
576
|
+
|
|
577
|
+
// SSE 格式:简化为 {hour, count},只取当天
|
|
578
|
+
hourlyTrend = hourlyArrays.hourlyTrend
|
|
579
|
+
.filter(h => h.date === todayStr)
|
|
580
|
+
.map(({ hour, total }) => ({ hour, count: total }));
|
|
581
|
+
|
|
582
|
+
hourlyTrendByStatus = hourlyArrays.hourlyTrendByStatus
|
|
583
|
+
.filter(h => h.date === todayStr)
|
|
584
|
+
.map(({ hour, status, count }) => ({ hour, status, count }));
|
|
585
|
+
|
|
586
|
+
hourlyTrendByType = hourlyArrays.hourlyTrendByType
|
|
587
|
+
.filter(h => h.date === todayStr)
|
|
588
|
+
.map(({ hour, type, count }) => ({ hour, type, count }));
|
|
589
|
+
|
|
590
|
+
intervalTrend = hourlyTrend.map(({ hour, count }) => ({
|
|
591
|
+
interval: `${String(hour).padStart(2, '0')}:00`,
|
|
592
|
+
count
|
|
593
|
+
}));
|
|
594
|
+
|
|
595
|
+
for (const [rtName, rt] of Object.entries(parsed.duration.byRunnerType)) {
|
|
596
|
+
completedTodayTotalDurationMsByRunnerType[rtName] = rt.sumTotal;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
} catch (e) {
|
|
600
|
+
fastify.log.error(`SSE统计查询失败: ${e.message}`);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
// 补充当前小时的 running/waiting 瞬时状态(来自 DB 实时查询,不受 statistics 服务影响)
|
|
604
|
+
// 查询用客户端时间:当前小时使用客户端时区
|
|
605
|
+
const currentHour = now.hour();
|
|
606
|
+
if (dbStatusCount.running > 0) hourlyTrendByStatus.push({ hour: currentHour, status: 'running', count: dbStatusCount.running });
|
|
607
|
+
if (dbWaitingTotal > 0) hourlyTrendByStatus.push({ hour: currentHour, status: 'waiting', count: dbWaitingTotal });
|
|
608
|
+
|
|
609
|
+
return {
|
|
610
|
+
date: todayStr,
|
|
611
|
+
totalTasks,
|
|
612
|
+
byStatus,
|
|
613
|
+
byType,
|
|
614
|
+
byRunnerType,
|
|
615
|
+
pendingByRunnerType,
|
|
616
|
+
waitingByRunnerType,
|
|
617
|
+
runningByRunnerType,
|
|
618
|
+
completedToday,
|
|
619
|
+
waitingQueueMaxWaitMsByRunnerType,
|
|
620
|
+
completedTodayTotalDurationMsByRunnerType,
|
|
621
|
+
runnerTypeStats,
|
|
622
|
+
hourlyTrend,
|
|
623
|
+
hourlyTrendByStatus,
|
|
624
|
+
hourlyTrendByType,
|
|
625
|
+
intervalTrend,
|
|
626
|
+
todayDuration
|
|
627
|
+
};
|
|
48
628
|
};
|
|
49
629
|
|
|
630
|
+
// ─── sseStatistics ───
|
|
50
631
|
const sseStatistics = async ({ range = '7d', timezone, type, runnerType, interval }, reply) => {
|
|
51
|
-
const channel = buildChannel({ type, runnerType });
|
|
52
|
-
const includeChildren = !type && !runnerType;
|
|
53
|
-
|
|
54
632
|
await statisticsServices.sseStream.send(reply, {
|
|
55
|
-
name: '
|
|
56
|
-
params: {
|
|
57
|
-
fetchData: async () => {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
includeChildren
|
|
65
|
-
});
|
|
633
|
+
name: 'taskStatistics',
|
|
634
|
+
params: { type, runnerType, timezone },
|
|
635
|
+
fetchData: async ({ type, runnerType, timezone } = {}) => {
|
|
636
|
+
try {
|
|
637
|
+
return await buildSseData({ timezone, type, runnerType });
|
|
638
|
+
} catch (e) {
|
|
639
|
+
fastify.log.error(`SSE获取数据失败: ${e.message}`);
|
|
640
|
+
return {};
|
|
641
|
+
}
|
|
66
642
|
},
|
|
67
643
|
interval: interval || 5
|
|
68
644
|
});
|