@kne/fastify-task 2.0.2 → 2.0.4
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/index.js +15 -0
- package/libs/controllers/main.js +0 -169
- package/libs/controllers/statistics.js +194 -0
- package/libs/models/task-hourly-statistics.js +48 -0
- package/libs/services/main.js +30 -712
- package/libs/services/statistics.js +978 -0
- package/package.json +2 -1
package/index.js
CHANGED
|
@@ -15,6 +15,8 @@ module.exports = fp(
|
|
|
15
15
|
scriptName: 'index',
|
|
16
16
|
maxPollTimes: 20,
|
|
17
17
|
pollInterval: 10000,
|
|
18
|
+
/** 每小时任务完成按 UTC 桶重算(默认每时第 5 分执行,聚合上一完整 UTC 小时);传 null/false 关闭 */
|
|
19
|
+
hourlyStatisticsCronTime: '5 * * * *',
|
|
18
20
|
getUserModel: () => {
|
|
19
21
|
return fastify.account.models.user;
|
|
20
22
|
},
|
|
@@ -26,6 +28,8 @@ module.exports = fp(
|
|
|
26
28
|
options
|
|
27
29
|
);
|
|
28
30
|
|
|
31
|
+
fastify.register(require('@fastify/sse'));
|
|
32
|
+
|
|
29
33
|
fastify.register(require('@kne/fastify-namespace'), {
|
|
30
34
|
options,
|
|
31
35
|
name: options.name,
|
|
@@ -57,6 +61,17 @@ module.exports = fp(
|
|
|
57
61
|
//启动时,将running状态的任务设置为pending
|
|
58
62
|
fastify.addHook('onReady', async () => {
|
|
59
63
|
await fastify[options.name].services.resetAll();
|
|
64
|
+
fastify[options.name].services.syncHourlyStatisticsFromTasks().catch(console.error);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (options.hourlyStatisticsCronTime) {
|
|
68
|
+
fastify.cron.createJob({
|
|
69
|
+
cronTime: options.hourlyStatisticsCronTime,
|
|
70
|
+
onTick: async () => {
|
|
71
|
+
const { syncHourlyStatisticsFromTasks } = fastify[options.name].services;
|
|
72
|
+
await syncHourlyStatisticsFromTasks();
|
|
73
|
+
},
|
|
74
|
+
start: true
|
|
60
75
|
});
|
|
61
76
|
}
|
|
62
77
|
})
|
package/libs/controllers/main.js
CHANGED
|
@@ -272,173 +272,4 @@ module.exports = fp(async (fastify, options) => {
|
|
|
272
272
|
return await services.callbackWithSignature(Object.assign({}, request.body, { taskId: request.query.taskId }));
|
|
273
273
|
}
|
|
274
274
|
);
|
|
275
|
-
|
|
276
|
-
// 历史统计数据
|
|
277
|
-
fastify.get(
|
|
278
|
-
`${options.prefix}/statistics`,
|
|
279
|
-
{
|
|
280
|
-
onRequest: options.getAuthenticate('statistics'),
|
|
281
|
-
schema: {
|
|
282
|
-
summary: '获取任务统计概览',
|
|
283
|
-
query: {
|
|
284
|
-
type: 'object',
|
|
285
|
-
properties: {
|
|
286
|
-
range: { type: 'string', default: '7d', description: '时间范围: 7d=近7天, 1m=近1个月, 1y=近1年' },
|
|
287
|
-
timezone: { type: 'string', description: '时区,如 Asia/Shanghai,默认服务器时区' },
|
|
288
|
-
type: { type: 'string', description: '按任务类型筛选' }
|
|
289
|
-
}
|
|
290
|
-
},
|
|
291
|
-
response: {
|
|
292
|
-
200: {
|
|
293
|
-
type: 'object',
|
|
294
|
-
properties: {
|
|
295
|
-
range: { type: 'string', description: '当前时间范围' },
|
|
296
|
-
rangeLabel: { type: 'string', description: '时间范围描述' },
|
|
297
|
-
totalTasks: { type: 'integer', description: '时间范围内任务总数' },
|
|
298
|
-
byStatus: {
|
|
299
|
-
type: 'object',
|
|
300
|
-
description: '按任务状态统计',
|
|
301
|
-
additionalProperties: { type: 'integer' }
|
|
302
|
-
},
|
|
303
|
-
byType: {
|
|
304
|
-
type: 'object',
|
|
305
|
-
description: '按任务类型统计',
|
|
306
|
-
additionalProperties: { type: 'integer' }
|
|
307
|
-
},
|
|
308
|
-
byRunnerType: {
|
|
309
|
-
type: 'object',
|
|
310
|
-
description: '按执行者类型统计(manual/system)',
|
|
311
|
-
additionalProperties: { type: 'integer' }
|
|
312
|
-
},
|
|
313
|
-
byTargetType: {
|
|
314
|
-
type: 'object',
|
|
315
|
-
description: '按目标对象类型统计',
|
|
316
|
-
additionalProperties: { type: 'integer' }
|
|
317
|
-
},
|
|
318
|
-
recentTrend: {
|
|
319
|
-
type: 'array',
|
|
320
|
-
description: '按日任务创建趋势',
|
|
321
|
-
items: {
|
|
322
|
-
type: 'object',
|
|
323
|
-
properties: {
|
|
324
|
-
date: { type: 'string', description: '日期' },
|
|
325
|
-
count: { type: 'integer', description: '任务数量' }
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
},
|
|
329
|
-
recentTrendByStatus: {
|
|
330
|
-
type: 'array',
|
|
331
|
-
description: '按日+状态任务创建趋势',
|
|
332
|
-
items: {
|
|
333
|
-
type: 'object',
|
|
334
|
-
properties: {
|
|
335
|
-
date: { type: 'string', description: '日期' },
|
|
336
|
-
status: { type: 'string', description: '任务状态' },
|
|
337
|
-
count: { type: 'integer', description: '任务数量' }
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
},
|
|
341
|
-
recentTrendByType: {
|
|
342
|
-
type: 'array',
|
|
343
|
-
description: '按日+类型任务创建趋势',
|
|
344
|
-
items: {
|
|
345
|
-
type: 'object',
|
|
346
|
-
properties: {
|
|
347
|
-
date: { type: 'string', description: '日期' },
|
|
348
|
-
type: { type: 'string', description: '任务类型' },
|
|
349
|
-
count: { type: 'integer', description: '任务数量' }
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
},
|
|
353
|
-
durationTrend: {
|
|
354
|
-
type: 'array',
|
|
355
|
-
description: '按日平均耗时趋势(来自预聚合统计表)',
|
|
356
|
-
items: {
|
|
357
|
-
type: 'object',
|
|
358
|
-
properties: {
|
|
359
|
-
date: { type: 'string', description: '完成日期' },
|
|
360
|
-
completedCount: { type: 'integer', description: '完成任务数' },
|
|
361
|
-
successCount: { type: 'integer', description: '成功数' },
|
|
362
|
-
failedCount: { type: 'integer', description: '失败数' },
|
|
363
|
-
canceledCount: { type: 'integer', description: '取消数' },
|
|
364
|
-
avgWaitingTime: { type: 'integer', description: '平均等待时间(毫秒)' },
|
|
365
|
-
avgExecutionTime: { type: 'integer', description: '平均执行时间(毫秒)' },
|
|
366
|
-
avgTotalTime: { type: 'integer', description: '平均总耗时(毫秒)' },
|
|
367
|
-
byType: {
|
|
368
|
-
type: 'object',
|
|
369
|
-
description: '按类型分组的耗时统计',
|
|
370
|
-
additionalProperties: {
|
|
371
|
-
type: 'object',
|
|
372
|
-
properties: {
|
|
373
|
-
count: { type: 'integer' },
|
|
374
|
-
avgWaitingTime: { type: 'integer' },
|
|
375
|
-
avgExecutionTime: { type: 'integer' },
|
|
376
|
-
avgTotalTime: { type: 'integer' }
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
},
|
|
380
|
-
byRunnerType: {
|
|
381
|
-
type: 'object',
|
|
382
|
-
description: '按执行者类型分组的耗时统计',
|
|
383
|
-
additionalProperties: {
|
|
384
|
-
type: 'object',
|
|
385
|
-
properties: {
|
|
386
|
-
count: { type: 'integer' },
|
|
387
|
-
avgWaitingTime: { type: 'integer' },
|
|
388
|
-
avgExecutionTime: { type: 'integer' },
|
|
389
|
-
avgTotalTime: { type: 'integer' }
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
},
|
|
401
|
-
async request => {
|
|
402
|
-
const { range = '7d', timezone, type } = request.query;
|
|
403
|
-
return await services.statistics.getOverview({ range, timezone, type });
|
|
404
|
-
}
|
|
405
|
-
);
|
|
406
|
-
|
|
407
|
-
// SSE 实时统计数据推送
|
|
408
|
-
fastify.get(
|
|
409
|
-
`${options.prefix}/statistics/sse`,
|
|
410
|
-
{
|
|
411
|
-
sse: true,
|
|
412
|
-
onRequest: options.getAuthenticate('statistics'),
|
|
413
|
-
schema: {
|
|
414
|
-
summary: 'SSE实时推送当天任务统计数据',
|
|
415
|
-
query: {
|
|
416
|
-
type: 'object',
|
|
417
|
-
properties: {
|
|
418
|
-
interval: { type: 'integer', minimum: 1, default: 5, description: '推送间隔时间(秒),最小1秒,默认5秒' },
|
|
419
|
-
timezone: { type: 'string', description: '时区,如 Asia/Shanghai,默认服务器时区' },
|
|
420
|
-
type: { type: 'string', description: '按任务类型筛选' }
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
},
|
|
425
|
-
async function (request, reply) {
|
|
426
|
-
const intervalSeconds = request.query.interval;
|
|
427
|
-
const { timezone, type } = request.query;
|
|
428
|
-
reply.sse.keepAlive();
|
|
429
|
-
|
|
430
|
-
async function* eventStream() {
|
|
431
|
-
while (reply.sse.isConnected) {
|
|
432
|
-
try {
|
|
433
|
-
yield { data: JSON.stringify(await services.statistics.getRealtime({ timezone, type })) };
|
|
434
|
-
} catch (err) {
|
|
435
|
-
yield { event: 'error', data: JSON.stringify({ message: err.message }) };
|
|
436
|
-
}
|
|
437
|
-
await new Promise(resolve => setTimeout(resolve, intervalSeconds * 1000));
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
await reply.sse.send(eventStream());
|
|
442
|
-
}
|
|
443
|
-
);
|
|
444
275
|
});
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
const fp = require('fastify-plugin');
|
|
2
|
+
|
|
3
|
+
/** 看板统计:概览 GET /statistics 与 SSE /statistics/sse(独立 controller 插件,见业务插件开发指南「路由标准化」) */
|
|
4
|
+
module.exports = fp(
|
|
5
|
+
async (fastify, options) => {
|
|
6
|
+
const { services } = fastify[options.name];
|
|
7
|
+
|
|
8
|
+
fastify.get(
|
|
9
|
+
`${options.prefix}/statistics`,
|
|
10
|
+
{
|
|
11
|
+
onRequest: options.getAuthenticate('statistics'),
|
|
12
|
+
schema: {
|
|
13
|
+
summary: '获取任务统计概览',
|
|
14
|
+
query: {
|
|
15
|
+
type: 'object',
|
|
16
|
+
properties: {
|
|
17
|
+
range: { type: 'string', default: '7d', description: '时间范围: 7d=近7天, 1m=近1个月, 1y=近1年' },
|
|
18
|
+
timezone: { type: 'string', description: '时区,如 Asia/Shanghai,默认服务器时区' },
|
|
19
|
+
type: { type: 'string', description: '按任务类型筛选' },
|
|
20
|
+
runnerType: { type: 'string', description: '按执行方式筛选 manual / system' }
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
response: {
|
|
24
|
+
200: {
|
|
25
|
+
type: 'object',
|
|
26
|
+
properties: {
|
|
27
|
+
range: { type: 'string', description: '当前时间范围' },
|
|
28
|
+
rangeLabel: { type: 'string', description: '时间范围描述' },
|
|
29
|
+
totalTasks: { type: 'integer', description: '时间范围内任务总数' },
|
|
30
|
+
byStatus: {
|
|
31
|
+
type: 'object',
|
|
32
|
+
description: '按任务状态统计',
|
|
33
|
+
additionalProperties: { type: 'integer' }
|
|
34
|
+
},
|
|
35
|
+
byType: {
|
|
36
|
+
type: 'object',
|
|
37
|
+
description: '按任务类型统计',
|
|
38
|
+
additionalProperties: { type: 'integer' }
|
|
39
|
+
},
|
|
40
|
+
byRunnerType: {
|
|
41
|
+
type: 'object',
|
|
42
|
+
description: '按执行者类型统计(manual/system)',
|
|
43
|
+
additionalProperties: { type: 'integer' }
|
|
44
|
+
},
|
|
45
|
+
byTargetType: {
|
|
46
|
+
type: 'object',
|
|
47
|
+
description: '按目标对象类型统计',
|
|
48
|
+
additionalProperties: { type: 'integer' }
|
|
49
|
+
},
|
|
50
|
+
recentTrend: {
|
|
51
|
+
type: 'array',
|
|
52
|
+
description: '按日任务创建趋势',
|
|
53
|
+
items: {
|
|
54
|
+
type: 'object',
|
|
55
|
+
properties: {
|
|
56
|
+
date: { type: 'string', description: '日期' },
|
|
57
|
+
count: { type: 'integer', description: '任务数量' }
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
recentTrendByStatus: {
|
|
62
|
+
type: 'array',
|
|
63
|
+
description: '按日+状态任务创建趋势',
|
|
64
|
+
items: {
|
|
65
|
+
type: 'object',
|
|
66
|
+
properties: {
|
|
67
|
+
date: { type: 'string', description: '日期' },
|
|
68
|
+
status: { type: 'string', description: '任务状态' },
|
|
69
|
+
count: { type: 'integer', description: '任务数量' }
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
recentTrendByType: {
|
|
74
|
+
type: 'array',
|
|
75
|
+
description: '按日+类型任务创建趋势',
|
|
76
|
+
items: {
|
|
77
|
+
type: 'object',
|
|
78
|
+
properties: {
|
|
79
|
+
date: { type: 'string', description: '日期' },
|
|
80
|
+
type: { type: 'string', description: '任务类型' },
|
|
81
|
+
count: { type: 'integer', description: '任务数量' }
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
durationTrend: {
|
|
86
|
+
type: 'array',
|
|
87
|
+
description: '按日平均耗时趋势(来自预聚合统计表)',
|
|
88
|
+
items: {
|
|
89
|
+
type: 'object',
|
|
90
|
+
properties: {
|
|
91
|
+
date: { type: 'string', description: '完成日期' },
|
|
92
|
+
completedCount: { type: 'integer', description: '完成任务数' },
|
|
93
|
+
successCount: { type: 'integer', description: '成功数' },
|
|
94
|
+
failedCount: { type: 'integer', description: '失败数' },
|
|
95
|
+
canceledCount: { type: 'integer', description: '取消数' },
|
|
96
|
+
avgWaitingTime: { type: 'integer', description: '平均等待时间(毫秒)' },
|
|
97
|
+
avgExecutionTime: { type: 'integer', description: '平均执行时间(毫秒)' },
|
|
98
|
+
avgTotalTime: { type: 'integer', description: '平均总耗时(毫秒)' },
|
|
99
|
+
byType: {
|
|
100
|
+
type: 'object',
|
|
101
|
+
description: '按类型分组的耗时统计',
|
|
102
|
+
additionalProperties: {
|
|
103
|
+
type: 'object',
|
|
104
|
+
properties: {
|
|
105
|
+
count: { type: 'integer' },
|
|
106
|
+
avgWaitingTime: { type: 'integer' },
|
|
107
|
+
avgExecutionTime: { type: 'integer' },
|
|
108
|
+
avgTotalTime: { type: 'integer' }
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
byRunnerType: {
|
|
113
|
+
type: 'object',
|
|
114
|
+
description: '按执行者类型分组的耗时统计',
|
|
115
|
+
additionalProperties: {
|
|
116
|
+
type: 'object',
|
|
117
|
+
properties: {
|
|
118
|
+
count: { type: 'integer' },
|
|
119
|
+
avgWaitingTime: { type: 'integer' },
|
|
120
|
+
avgExecutionTime: { type: 'integer' },
|
|
121
|
+
avgTotalTime: { type: 'integer' }
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
hourlyCompletionTrend: {
|
|
129
|
+
type: 'array',
|
|
130
|
+
description: '按完成时刻归入 UTC 小时桶后,在请求 timezone 下展开的每小时+类型+执行方式完成数',
|
|
131
|
+
items: {
|
|
132
|
+
type: 'object',
|
|
133
|
+
properties: {
|
|
134
|
+
date: { type: 'string', description: '客户端时区下的日期 YYYY-MM-DD' },
|
|
135
|
+
hour: { type: 'integer', description: '客户端时区下的小时 0-23' },
|
|
136
|
+
type: { type: 'string' },
|
|
137
|
+
runnerType: { type: 'string' },
|
|
138
|
+
totalCompleted: { type: 'integer' },
|
|
139
|
+
successCount: { type: 'integer' },
|
|
140
|
+
failedCount: { type: 'integer' },
|
|
141
|
+
canceledCount: { type: 'integer' }
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
async request => {
|
|
151
|
+
const { range = '7d', timezone, type, runnerType } = request.query;
|
|
152
|
+
return await services.statistics.getOverview({ range, timezone, type, runnerType });
|
|
153
|
+
}
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
// SSE 实时统计数据推送
|
|
157
|
+
fastify.get(
|
|
158
|
+
`${options.prefix}/statistics/sse`,
|
|
159
|
+
{
|
|
160
|
+
sse: true,
|
|
161
|
+
onRequest: options.getAuthenticate('statistics'),
|
|
162
|
+
schema: {
|
|
163
|
+
summary: 'SSE实时推送当天任务统计数据',
|
|
164
|
+
query: {
|
|
165
|
+
type: 'object',
|
|
166
|
+
properties: {
|
|
167
|
+
interval: { type: 'integer', minimum: 1, default: 5, description: '推送间隔时间(秒),最小1秒,默认5秒' },
|
|
168
|
+
timezone: { type: 'string', description: '时区,如 Asia/Shanghai,默认服务器时区' },
|
|
169
|
+
type: { type: 'string', description: '按任务类型筛选' },
|
|
170
|
+
runnerType: { type: 'string', description: '按执行方式筛选 manual / system' }
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
async function (request, reply) {
|
|
176
|
+
const intervalSeconds = request.query.interval;
|
|
177
|
+
const { timezone, type, runnerType } = request.query;
|
|
178
|
+
reply.sse.keepAlive();
|
|
179
|
+
|
|
180
|
+
async function* eventStream() {
|
|
181
|
+
while (reply.sse.isConnected) {
|
|
182
|
+
try {
|
|
183
|
+
yield { data: JSON.stringify(await services.statistics.getRealtime({ timezone, type, runnerType })) };
|
|
184
|
+
} catch (err) {
|
|
185
|
+
yield { event: 'error', data: JSON.stringify({ message: err.message }) };
|
|
186
|
+
}
|
|
187
|
+
await new Promise(resolve => setTimeout(resolve, intervalSeconds * 1000));
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
await reply.sse.send(eventStream());
|
|
192
|
+
}
|
|
193
|
+
);
|
|
194
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
module.exports = ({ DataTypes }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
4
|
+
bucketStartUtc: {
|
|
5
|
+
type: DataTypes.DATE,
|
|
6
|
+
allowNull: false,
|
|
7
|
+
comment: 'UTC 整点桶起始时间(按任务完成时刻归入该小时,与客户端时区无关)'
|
|
8
|
+
},
|
|
9
|
+
type: {
|
|
10
|
+
type: DataTypes.STRING,
|
|
11
|
+
allowNull: false,
|
|
12
|
+
comment: '任务类型'
|
|
13
|
+
},
|
|
14
|
+
runnerType: {
|
|
15
|
+
type: DataTypes.STRING,
|
|
16
|
+
allowNull: false,
|
|
17
|
+
comment: '执行方式 manual / system'
|
|
18
|
+
},
|
|
19
|
+
totalCompleted: {
|
|
20
|
+
type: DataTypes.INTEGER,
|
|
21
|
+
defaultValue: 0,
|
|
22
|
+
comment: '该桶内完成任务数'
|
|
23
|
+
},
|
|
24
|
+
successCount: {
|
|
25
|
+
type: DataTypes.INTEGER,
|
|
26
|
+
defaultValue: 0,
|
|
27
|
+
comment: '成功数'
|
|
28
|
+
},
|
|
29
|
+
failedCount: {
|
|
30
|
+
type: DataTypes.INTEGER,
|
|
31
|
+
defaultValue: 0,
|
|
32
|
+
comment: '失败数'
|
|
33
|
+
},
|
|
34
|
+
canceledCount: {
|
|
35
|
+
type: DataTypes.INTEGER,
|
|
36
|
+
defaultValue: 0,
|
|
37
|
+
comment: '取消数'
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
options: {
|
|
41
|
+
comment: '任务按 UTC 小时 + 类型 + 执行方式的完成数预聚合(由定时任务从任务表重算,读接口按客户端时区换算展示)',
|
|
42
|
+
indexes: [
|
|
43
|
+
{ unique: true, fields: ['bucket_start_utc', 'type', 'runner_type'] },
|
|
44
|
+
{ fields: ['bucket_start_utc'] }
|
|
45
|
+
]
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
};
|