@kne/fastify-task 3.0.0-alpha.1 → 3.0.0-alpha.3
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/libs/controllers/statistics.js +2 -5
- package/libs/services/main.js +15 -10
- package/libs/services/statistics.js +375 -33
- package/package.json +2 -2
|
@@ -36,18 +36,15 @@ module.exports = fp(async (fastify, options) => {
|
|
|
36
36
|
query: {
|
|
37
37
|
type: 'object',
|
|
38
38
|
properties: {
|
|
39
|
-
range: { type: 'string', default: '7d', description: '时间范围: 7d=近7天, 1m=近1个月, 3m=近3个月, 1y=近1年' },
|
|
40
39
|
timezone: { type: 'string', description: '时区,如 Asia/Shanghai,默认服务器时区' },
|
|
41
|
-
type: { type: 'string', description: '按任务类型筛选' },
|
|
42
|
-
runnerType: { type: 'string', description: '按执行方式筛选 manual / system' },
|
|
43
40
|
interval: { type: 'integer', minimum: 1, default: 5, description: '推送间隔时间(秒),最小1秒,默认5秒' }
|
|
44
41
|
}
|
|
45
42
|
}
|
|
46
43
|
}
|
|
47
44
|
},
|
|
48
45
|
async (request, reply) => {
|
|
49
|
-
const {
|
|
50
|
-
await services.sseStatistics({
|
|
46
|
+
const { timezone, interval } = request.query;
|
|
47
|
+
await services.sseStatistics({ timezone, interval }, reply);
|
|
51
48
|
}
|
|
52
49
|
);
|
|
53
50
|
});
|
package/libs/services/main.js
CHANGED
|
@@ -8,7 +8,11 @@ module.exports = fp(async (fastify, options) => {
|
|
|
8
8
|
const { Op } = fastify.sequelize.Sequelize;
|
|
9
9
|
|
|
10
10
|
/** 通过 @kne/fastify-statistics 采集任务完成数据 */
|
|
11
|
-
const collectTaskStatistics = task => {
|
|
11
|
+
const collectTaskStatistics = async task => {
|
|
12
|
+
// 确保 task 数据是最新的(Sequelize update 后实例可能未完全同步)
|
|
13
|
+
if (task.reload && typeof task.reload === 'function') {
|
|
14
|
+
await task.reload();
|
|
15
|
+
}
|
|
12
16
|
const completedAt = task.completedAt || new Date();
|
|
13
17
|
const createdAt = task.createdAt;
|
|
14
18
|
const startedAt = task.startedAt;
|
|
@@ -28,20 +32,21 @@ module.exports = fp(async (fastify, options) => {
|
|
|
28
32
|
hasTiming = totalTime > 0;
|
|
29
33
|
}
|
|
30
34
|
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
+
const hour = new Date(completedAt).getUTCHours();
|
|
36
|
+
const channel = `${task.type}:${task.runnerType || 'manual'}:${hour}`;
|
|
37
|
+
const status = task.status || 'unknown';
|
|
38
|
+
|
|
39
|
+
const data = { total: 1, [status]: 1 };
|
|
40
|
+
const unit = { total: 'count', [status]: 'count' };
|
|
35
41
|
if (hasTiming) {
|
|
36
|
-
data
|
|
37
|
-
|
|
38
|
-
data.totalTime = totalTime;
|
|
42
|
+
Object.assign(data, { waitingTime, executionTime, totalTime });
|
|
43
|
+
Object.assign(unit, { waitingTime: 'ms', executionTime: 'ms', totalTime: 'ms' });
|
|
39
44
|
}
|
|
40
|
-
|
|
41
45
|
fastify[`${options.name}Statistics`].services
|
|
42
46
|
.collect({
|
|
43
|
-
channel
|
|
47
|
+
channel,
|
|
44
48
|
data,
|
|
49
|
+
unit,
|
|
45
50
|
time: completedAt
|
|
46
51
|
})
|
|
47
52
|
.catch(e => {
|
|
@@ -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天' },
|
|
@@ -24,52 +30,388 @@ const parseRange = (range = '7d') => {
|
|
|
24
30
|
return { startTime, endTime: now, label: config.label, range };
|
|
25
31
|
};
|
|
26
32
|
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
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
|
+
)
|
|
54
|
+
};
|
|
55
|
+
|
|
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;
|
|
31
63
|
};
|
|
32
64
|
|
|
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
|
+
});
|
|
73
|
+
|
|
74
|
+
/** 空的历史统计结果 */
|
|
75
|
+
const emptyHistoryResult = () => ({
|
|
76
|
+
totalTasks: 0, byStatus: {}, byType: {},
|
|
77
|
+
recentTrend: [], recentTrendByType: [], durationTrend: [], hourlyCompletionTrend: []
|
|
78
|
+
});
|
|
79
|
+
|
|
33
80
|
module.exports = fp(async (fastify, options) => {
|
|
34
81
|
const statisticsServices = fastify[`${options.name}Statistics`].services;
|
|
35
82
|
|
|
36
|
-
|
|
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
|
+
};
|
|
88
|
+
|
|
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');
|
|
97
|
+
|
|
98
|
+
const taskTypes = await getTaskTypes();
|
|
99
|
+
if (taskTypes.length === 0) return emptyRealtimeResult(todayDate);
|
|
100
|
+
|
|
101
|
+
const typeChannels = buildChannels.type(taskTypes);
|
|
102
|
+
const runnerChannels = buildChannels.runner(taskTypes);
|
|
103
|
+
const hourChannels = buildChannels.hour(taskTypes);
|
|
104
|
+
|
|
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
|
+
});
|
|
110
|
+
|
|
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
|
+
});
|
|
116
|
+
|
|
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
|
+
});
|
|
122
|
+
|
|
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]);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
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;
|
|
160
|
+
}
|
|
161
|
+
if (sumData.pending !== undefined) pendingByRunnerType[runnerType] = Math.round(sumData.pending);
|
|
162
|
+
if (sumData.success !== undefined) completedToday[runnerType] = Math.round(sumData.success);
|
|
163
|
+
}
|
|
164
|
+
|
|
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]);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
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
|
+
}
|
|
193
|
+
|
|
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++;
|
|
207
|
+
}
|
|
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
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
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);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
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 }) => {
|
|
37
261
|
const { startTime, endTime } = parseRange(range);
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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);
|
|
272
|
+
|
|
273
|
+
// 按类型查询计数汇总
|
|
274
|
+
const typeCountResult = await statisticsServices.query({
|
|
275
|
+
channels: typeChannels, startTime, endTime,
|
|
276
|
+
attributeNames: COUNT_ATTRIBUTES, aggregates: ['sum'], timezone: tz
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
// 按小时通道查询计数趋势
|
|
280
|
+
const hourCountResult = await statisticsServices.query({
|
|
281
|
+
channels: hourChannels, startTime, endTime,
|
|
282
|
+
attributeNames: COUNT_ATTRIBUTES, aggregates: ['sum'], timezone: tz
|
|
283
|
+
});
|
|
284
|
+
|
|
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
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
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
|
+
}
|
|
315
|
+
|
|
316
|
+
const recentTrend = Object.entries(dateMap)
|
|
317
|
+
.map(([date, data]) => ({ date, count: data.total }))
|
|
318
|
+
.sort((a, b) => a.date.localeCompare(b.date));
|
|
319
|
+
|
|
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 });
|
|
324
|
+
}
|
|
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
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (taskType && sumData.total !== undefined) {
|
|
349
|
+
bucket.byType[taskType] = (bucket.byType[taskType] || 0) + Math.round(sumData.total);
|
|
350
|
+
}
|
|
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
|
|
47
363
|
});
|
|
364
|
+
|
|
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
|
+
}
|
|
385
|
+
|
|
386
|
+
if (avgExecutionTime > 0 || avgWaitingTime > 0) {
|
|
387
|
+
if (!durationByDate[dateStr]) durationByDate[dateStr] = {};
|
|
388
|
+
durationByDate[dateStr][rt] = { avgExecutionTime, avgWaitingTime };
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const durationTrend = Object.entries(durationByDate)
|
|
393
|
+
.map(([date, byRunnerType]) => ({ date, byRunnerType }))
|
|
394
|
+
.sort((a, b) => a.date.localeCompare(b.date));
|
|
395
|
+
|
|
396
|
+
return {
|
|
397
|
+
totalTasks, byStatus, byType,
|
|
398
|
+
recentTrend, recentTrendByType, durationTrend, hourlyCompletionTrend
|
|
399
|
+
};
|
|
48
400
|
};
|
|
49
401
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
402
|
+
/** HTTP 接口:历史统计概览 */
|
|
403
|
+
const queryStatistics = async ({ range = '7d', timezone, type, runnerType }) => {
|
|
404
|
+
return await queryHistoryData({ range, timezone, type, runnerType });
|
|
405
|
+
};
|
|
53
406
|
|
|
407
|
+
/** SSE 接口:实时统计推送 */
|
|
408
|
+
const sseStatistics = async ({ timezone, interval }, reply) => {
|
|
54
409
|
await statisticsServices.sseStream.send(reply, {
|
|
55
|
-
name: 'query',
|
|
56
|
-
|
|
57
|
-
fetchData: async () => {
|
|
58
|
-
const { startTime, endTime } = parseRange(range);
|
|
59
|
-
return statisticsServices.query({
|
|
60
|
-
channels: [channel],
|
|
61
|
-
startTime,
|
|
62
|
-
endTime,
|
|
63
|
-
timezone: timezone || undefined,
|
|
64
|
-
includeChildren
|
|
65
|
-
});
|
|
66
|
-
},
|
|
410
|
+
name: 'query', params: { timezone },
|
|
411
|
+
fetchData: async () => await queryRealtimeData({ timezone }),
|
|
67
412
|
interval: interval || 5
|
|
68
413
|
});
|
|
69
414
|
};
|
|
70
415
|
|
|
71
|
-
Object.assign(fastify[options.name].services, {
|
|
72
|
-
queryStatistics,
|
|
73
|
-
sseStatistics
|
|
74
|
-
});
|
|
416
|
+
Object.assign(fastify[options.name].services, { queryStatistics, sseStatistics });
|
|
75
417
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kne/fastify-task",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.3",
|
|
4
4
|
"description": "一个 Fastify 插件,用于任务编排、追踪任务执行状态和结果。支持系统自动执行和手动执行两种模式,提供完整的任务生命周期管理",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"sqlite3": "^5.1.7"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@kne/fastify-statistics": "^0.1.0-alpha.
|
|
59
|
+
"@kne/fastify-statistics": "^0.1.0-alpha.6",
|
|
60
60
|
"fastify-cron": "^1.4.0",
|
|
61
61
|
"fs-extra": "^11.3.0"
|
|
62
62
|
}
|