@kne/fastify-task 3.0.0-alpha.9 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/libs/controllers/main.js +124 -62
- package/libs/controllers/statistics.js +11 -2
- package/libs/helpers/task-service-context.js +13 -1
- package/libs/schema/response.js +100 -0
- package/libs/services/scheduler.js +6 -1
- package/libs/services/statistics.js +292 -129
- package/libs/services/task.js +77 -2
- package/package.json +1 -1
package/libs/controllers/main.js
CHANGED
|
@@ -1,4 +1,23 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
|
+
const { any, success, empty, TASK_STATUS, RUNNER_TYPE, task, taskList } = require('../schema/response');
|
|
3
|
+
|
|
4
|
+
const TAG = '任务-任务管理';
|
|
5
|
+
const CALLBACK_TAG = '任务-外部回调';
|
|
6
|
+
const SIGNATURE_HINT = '任务 context.secret 存在时校验 HMAC-SHA256 签名(密钥为 secret,内容为 `${id}|${JSON 数据}`),不通过报错;未设置 secret 时不校验';
|
|
7
|
+
const timeRange = name => ({
|
|
8
|
+
type: 'object',
|
|
9
|
+
properties: {
|
|
10
|
+
startTime: {
|
|
11
|
+
type: 'string',
|
|
12
|
+
description: `${name}起始时间(含),可被 Date 解析的时间字符串`
|
|
13
|
+
},
|
|
14
|
+
endTime: {
|
|
15
|
+
type: 'string',
|
|
16
|
+
description: `${name}结束时间(含),可被 Date 解析的时间字符串`
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
description: `${name}区间查找`
|
|
20
|
+
});
|
|
2
21
|
|
|
3
22
|
module.exports = fp(async (fastify, options) => {
|
|
4
23
|
const { services } = fastify[options.name];
|
|
@@ -8,7 +27,9 @@ module.exports = fp(async (fastify, options) => {
|
|
|
8
27
|
{
|
|
9
28
|
onRequest: options.getAuthenticate('write'),
|
|
10
29
|
schema: {
|
|
30
|
+
tags: [TAG],
|
|
11
31
|
summary: '创建任务',
|
|
32
|
+
description: '创建一条 pending 状态的任务,到达开始时间后由定时器按优先级调度执行(system)或等待人工完成(manual);type 必须是已注册的任务类型,返回新任务 ID',
|
|
12
33
|
body: {
|
|
13
34
|
type: 'object',
|
|
14
35
|
properties: {
|
|
@@ -24,6 +45,12 @@ module.exports = fp(async (fastify, options) => {
|
|
|
24
45
|
type: 'string',
|
|
25
46
|
description: '目标对象类型'
|
|
26
47
|
},
|
|
48
|
+
context: {
|
|
49
|
+
description: '执行上下文,任务执行时可读取;其中 secret 用于外部回调签名校验'
|
|
50
|
+
},
|
|
51
|
+
options: {
|
|
52
|
+
description: '任务扩展选项'
|
|
53
|
+
},
|
|
27
54
|
input: {
|
|
28
55
|
type: 'object',
|
|
29
56
|
description: '输入数据'
|
|
@@ -31,15 +58,15 @@ module.exports = fp(async (fastify, options) => {
|
|
|
31
58
|
runnerType: {
|
|
32
59
|
type: 'string',
|
|
33
60
|
enum: ['manual', 'system'],
|
|
34
|
-
description:
|
|
61
|
+
description: `执行者类型,${RUNNER_TYPE},默认 manual`
|
|
35
62
|
},
|
|
36
63
|
delay: {
|
|
37
64
|
type: 'number',
|
|
38
|
-
description: '
|
|
65
|
+
description: '延迟执行秒数,必须为非负数,默认 0 立即可执行'
|
|
39
66
|
},
|
|
40
67
|
scriptName: {
|
|
41
68
|
type: 'string',
|
|
42
|
-
description: '
|
|
69
|
+
description: '任务脚本名称(不含 .js),只能包含字母、数字、下划线或中划线;不传使用任务类型声明的脚本,默认 index'
|
|
43
70
|
},
|
|
44
71
|
priority: {
|
|
45
72
|
type: 'number',
|
|
@@ -59,7 +86,12 @@ module.exports = fp(async (fastify, options) => {
|
|
|
59
86
|
}
|
|
60
87
|
},
|
|
61
88
|
required: ['type', 'targetId', 'targetType']
|
|
62
|
-
}
|
|
89
|
+
},
|
|
90
|
+
response: success('创建成功', {
|
|
91
|
+
type: 'object',
|
|
92
|
+
additionalProperties: true,
|
|
93
|
+
properties: { id: any('新任务 ID') }
|
|
94
|
+
})
|
|
63
95
|
}
|
|
64
96
|
},
|
|
65
97
|
async request => {
|
|
@@ -73,68 +105,62 @@ module.exports = fp(async (fastify, options) => {
|
|
|
73
105
|
{
|
|
74
106
|
onRequest: options.getAuthenticate('read'),
|
|
75
107
|
schema: {
|
|
108
|
+
tags: [TAG],
|
|
76
109
|
summary: '获取任务列表',
|
|
110
|
+
description: '分页查询任务,默认按创建时间倒序;filter / sort 也支持 filter[key]=value 形式的扁平 query 参数',
|
|
77
111
|
query: {
|
|
78
112
|
type: 'object',
|
|
79
113
|
properties: {
|
|
80
114
|
perPage: {
|
|
81
115
|
type: 'number',
|
|
82
|
-
default: 20
|
|
116
|
+
default: 20,
|
|
117
|
+
description: '每页条数'
|
|
83
118
|
},
|
|
84
119
|
currentPage: {
|
|
85
120
|
type: 'number',
|
|
86
|
-
default: 1
|
|
121
|
+
default: 1,
|
|
122
|
+
description: '页码,从 1 开始'
|
|
87
123
|
},
|
|
88
124
|
filter: {
|
|
89
125
|
type: 'object',
|
|
126
|
+
description: '筛选条件',
|
|
90
127
|
properties: {
|
|
91
128
|
id: {
|
|
92
|
-
type: 'string'
|
|
129
|
+
type: 'string',
|
|
130
|
+
description: '任务ID'
|
|
93
131
|
},
|
|
94
132
|
targetId: {
|
|
95
|
-
type: 'string'
|
|
133
|
+
type: 'string',
|
|
134
|
+
description: '目标对象ID'
|
|
135
|
+
},
|
|
136
|
+
targetName: {
|
|
137
|
+
type: 'string',
|
|
138
|
+
description: '目标对象名称,对应任务 input.name'
|
|
96
139
|
},
|
|
97
140
|
type: {
|
|
98
|
-
type: 'string'
|
|
141
|
+
type: 'string',
|
|
142
|
+
description: '任务类型'
|
|
99
143
|
},
|
|
100
144
|
status: {
|
|
101
|
-
type: 'string'
|
|
145
|
+
type: 'string',
|
|
146
|
+
description: TASK_STATUS
|
|
102
147
|
},
|
|
103
148
|
runnerType: {
|
|
104
|
-
type: 'string'
|
|
149
|
+
type: 'string',
|
|
150
|
+
description: RUNNER_TYPE
|
|
105
151
|
},
|
|
106
|
-
createdAt:
|
|
107
|
-
|
|
108
|
-
properties: {
|
|
109
|
-
startTime: {
|
|
110
|
-
type: 'string'
|
|
111
|
-
},
|
|
112
|
-
endTime: {
|
|
113
|
-
type: 'string'
|
|
114
|
-
}
|
|
115
|
-
},
|
|
116
|
-
description: 'createdAt区间查找'
|
|
117
|
-
},
|
|
118
|
-
completedAt: {
|
|
119
|
-
type: 'object',
|
|
120
|
-
properties: {
|
|
121
|
-
startTime: {
|
|
122
|
-
type: 'string'
|
|
123
|
-
},
|
|
124
|
-
endTime: {
|
|
125
|
-
type: 'string'
|
|
126
|
-
}
|
|
127
|
-
},
|
|
128
|
-
description: 'completedAt区间查找'
|
|
129
|
-
}
|
|
152
|
+
createdAt: timeRange('createdAt'),
|
|
153
|
+
completedAt: timeRange('completedAt')
|
|
130
154
|
}
|
|
131
155
|
},
|
|
132
156
|
sort: {
|
|
133
157
|
type: 'object',
|
|
134
|
-
description:
|
|
158
|
+
description:
|
|
159
|
+
"排序,键为字段名、值为 ASC 升序 / DESC 降序,如 { completedAt: 'DESC' };可排序字段:id、targetId、targetType、type、status、runnerType、priority、createdAt、updatedAt、startTime、startedAt、completedAt,其他字段报错"
|
|
135
160
|
}
|
|
136
161
|
}
|
|
137
|
-
}
|
|
162
|
+
},
|
|
163
|
+
response: success('任务分页结果', taskList)
|
|
138
164
|
}
|
|
139
165
|
},
|
|
140
166
|
async request => {
|
|
@@ -147,33 +173,43 @@ module.exports = fp(async (fastify, options) => {
|
|
|
147
173
|
{
|
|
148
174
|
onRequest: options.getAuthenticate('write'),
|
|
149
175
|
schema: {
|
|
176
|
+
tags: [TAG],
|
|
150
177
|
summary: '手动完成任务',
|
|
178
|
+
description: '人工把任务标记为成功或失败并记录完成人;成功时执行任务类型的完成回调,回调出错则转为失败;只有未结束的任务会被更新;只接受下列声明的字段,其它字段(如 context、userId)会被忽略',
|
|
151
179
|
body: {
|
|
152
180
|
type: 'object',
|
|
153
181
|
properties: {
|
|
154
182
|
id: {
|
|
155
|
-
type: 'string'
|
|
183
|
+
type: 'string',
|
|
184
|
+
description: '任务ID'
|
|
156
185
|
},
|
|
157
186
|
status: {
|
|
158
187
|
type: 'string',
|
|
159
|
-
enum: ['success', 'failed']
|
|
188
|
+
enum: ['success', 'failed'],
|
|
189
|
+
description: '完成结果:success 成功,failed 失败'
|
|
160
190
|
},
|
|
161
191
|
error: {
|
|
162
|
-
type: 'string'
|
|
192
|
+
type: 'string',
|
|
193
|
+
description: '失败原因,status 为 failed 时记录,不传记为「任务失败」'
|
|
163
194
|
},
|
|
164
195
|
msg: {
|
|
165
|
-
type: 'string'
|
|
196
|
+
type: 'string',
|
|
197
|
+
description: '任务消息,status 为 failed 时写入任务'
|
|
166
198
|
},
|
|
167
199
|
output: {
|
|
168
|
-
type: 'object'
|
|
200
|
+
type: 'object',
|
|
201
|
+
description: '任务输出数据'
|
|
169
202
|
}
|
|
170
203
|
},
|
|
171
204
|
required: ['id', 'status']
|
|
172
|
-
}
|
|
205
|
+
},
|
|
206
|
+
response: empty('操作成功,返回空对象')
|
|
173
207
|
}
|
|
174
208
|
},
|
|
175
209
|
async request => {
|
|
176
|
-
|
|
210
|
+
// 只透传 schema 声明的字段,避免客户端借失败分支覆盖 context.secret 等任意列
|
|
211
|
+
const { id, status, error, msg, output } = request.body;
|
|
212
|
+
await services.complete({ id, status, error, msg, output, userId: request.userInfo?.id });
|
|
177
213
|
return {};
|
|
178
214
|
}
|
|
179
215
|
);
|
|
@@ -183,7 +219,9 @@ module.exports = fp(async (fastify, options) => {
|
|
|
183
219
|
{
|
|
184
220
|
onRequest: options.getAuthenticate('write'),
|
|
185
221
|
schema: {
|
|
222
|
+
tags: [TAG],
|
|
186
223
|
summary: '取消任务',
|
|
224
|
+
description: '把 pending / running 状态的任务置为 canceled:传 id 取消单个任务;同时传 targetId、targetType、type 时批量取消该目标下该类型的任务(优先于 id);两种都没传报错',
|
|
187
225
|
body: {
|
|
188
226
|
type: 'object',
|
|
189
227
|
properties: {
|
|
@@ -204,7 +242,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
204
242
|
description: '任务类型(批量取消时使用)'
|
|
205
243
|
}
|
|
206
244
|
}
|
|
207
|
-
}
|
|
245
|
+
},
|
|
246
|
+
response: empty('操作成功,返回空对象')
|
|
208
247
|
}
|
|
209
248
|
},
|
|
210
249
|
async request => {
|
|
@@ -218,21 +257,26 @@ module.exports = fp(async (fastify, options) => {
|
|
|
218
257
|
{
|
|
219
258
|
onRequest: options.getAuthenticate('write'),
|
|
220
259
|
schema: {
|
|
260
|
+
tags: [TAG],
|
|
221
261
|
summary: '重试任务',
|
|
262
|
+
description: '把 failed / canceled 状态的任务重置为 pending 并清空完成信息、错误和重试计数,等待重新调度;其他状态报错,批量时遇到错误即中止',
|
|
222
263
|
body: {
|
|
223
264
|
type: 'object',
|
|
224
265
|
properties: {
|
|
225
266
|
id: {
|
|
226
|
-
type: 'string'
|
|
267
|
+
type: 'string',
|
|
268
|
+
description: '任务ID(单个重试)'
|
|
227
269
|
},
|
|
228
270
|
taskIds: {
|
|
229
271
|
type: 'array',
|
|
230
272
|
items: {
|
|
231
273
|
type: 'string'
|
|
232
|
-
}
|
|
274
|
+
},
|
|
275
|
+
description: '任务ID列表(批量重试),可与 id 同时传'
|
|
233
276
|
}
|
|
234
277
|
}
|
|
235
|
-
}
|
|
278
|
+
},
|
|
279
|
+
response: empty('操作成功,返回空对象')
|
|
236
280
|
}
|
|
237
281
|
},
|
|
238
282
|
async request => {
|
|
@@ -245,7 +289,9 @@ module.exports = fp(async (fastify, options) => {
|
|
|
245
289
|
`${options.prefix}/next`,
|
|
246
290
|
{
|
|
247
291
|
schema: {
|
|
292
|
+
tags: [CALLBACK_TAG],
|
|
248
293
|
summary: '处理任务next',
|
|
294
|
+
description: `外部服务回传 waiting 状态任务的执行结果:result 为 JSON 字符串,code 为 0 时任务成功(data 交给任务完成回调),否则任务失败;${SIGNATURE_HINT}。该接口不鉴权`,
|
|
249
295
|
body: {
|
|
250
296
|
type: 'object',
|
|
251
297
|
properties: {
|
|
@@ -255,14 +301,15 @@ module.exports = fp(async (fastify, options) => {
|
|
|
255
301
|
},
|
|
256
302
|
signature: {
|
|
257
303
|
type: 'string',
|
|
258
|
-
description: '
|
|
304
|
+
description: '签名:以任务 context.secret 为密钥,对 `${id}|${result}` 做 HMAC-SHA256 的十六进制摘要'
|
|
259
305
|
},
|
|
260
306
|
result: {
|
|
261
307
|
type: 'string',
|
|
262
|
-
description: '结果'
|
|
308
|
+
description: '结果 JSON 字符串,形如 {"code":0,"data":{}};解析失败任务置为失败'
|
|
263
309
|
}
|
|
264
310
|
}
|
|
265
|
-
}
|
|
311
|
+
},
|
|
312
|
+
response: { 200: { description: '处理完成,响应体为空' } }
|
|
266
313
|
}
|
|
267
314
|
},
|
|
268
315
|
async request => {
|
|
@@ -274,18 +321,23 @@ module.exports = fp(async (fastify, options) => {
|
|
|
274
321
|
`${options.prefix}/log`,
|
|
275
322
|
{
|
|
276
323
|
schema: {
|
|
324
|
+
tags: [CALLBACK_TAG],
|
|
277
325
|
summary: '记录任务日志',
|
|
326
|
+
description: `外部服务为任务追加一条日志,保存在任务 options.logs(新的在前,最多 100 条);${SIGNATURE_HINT}。该接口不鉴权`,
|
|
278
327
|
body: {
|
|
279
328
|
type: 'object',
|
|
280
329
|
properties: {
|
|
281
330
|
id: {
|
|
282
|
-
type: 'string'
|
|
331
|
+
type: 'string',
|
|
332
|
+
description: '任务ID,与 query.taskId 二选一,优先使用 id'
|
|
283
333
|
},
|
|
284
334
|
data: {
|
|
285
|
-
type: 'object'
|
|
335
|
+
type: 'object',
|
|
336
|
+
description: '日志数据'
|
|
286
337
|
},
|
|
287
338
|
message: {
|
|
288
|
-
type: 'string'
|
|
339
|
+
type: 'string',
|
|
340
|
+
description: '日志内容'
|
|
289
341
|
},
|
|
290
342
|
signature: {
|
|
291
343
|
type: 'string',
|
|
@@ -297,10 +349,12 @@ module.exports = fp(async (fastify, options) => {
|
|
|
297
349
|
type: 'object',
|
|
298
350
|
properties: {
|
|
299
351
|
taskId: {
|
|
300
|
-
type: 'string'
|
|
352
|
+
type: 'string',
|
|
353
|
+
description: '任务ID,body 未传 id 时使用'
|
|
301
354
|
}
|
|
302
355
|
}
|
|
303
|
-
}
|
|
356
|
+
},
|
|
357
|
+
response: success('更新后的任务记录', task)
|
|
304
358
|
}
|
|
305
359
|
},
|
|
306
360
|
async request => {
|
|
@@ -312,21 +366,27 @@ module.exports = fp(async (fastify, options) => {
|
|
|
312
366
|
`${options.prefix}/callback`,
|
|
313
367
|
{
|
|
314
368
|
schema: {
|
|
369
|
+
tags: [CALLBACK_TAG],
|
|
315
370
|
summary: '任务回调',
|
|
371
|
+
description: `外部服务回传任务最终结果:先把回调内容记入任务日志,code 为 0 时任务成功(data 作为输出),否则任务失败(message 作为错误);${SIGNATURE_HINT}。该接口不鉴权`,
|
|
316
372
|
body: {
|
|
317
373
|
type: 'object',
|
|
318
374
|
properties: {
|
|
319
375
|
id: {
|
|
320
|
-
type: 'string'
|
|
376
|
+
type: 'string',
|
|
377
|
+
description: '任务ID,与 query.taskId 二选一,优先使用 id'
|
|
321
378
|
},
|
|
322
379
|
code: {
|
|
323
|
-
type: 'number'
|
|
380
|
+
type: 'number',
|
|
381
|
+
description: '结果码,0 表示成功,其他表示失败'
|
|
324
382
|
},
|
|
325
383
|
data: {
|
|
326
|
-
type: 'object'
|
|
384
|
+
type: 'object',
|
|
385
|
+
description: '结果数据,作为任务输出'
|
|
327
386
|
},
|
|
328
387
|
message: {
|
|
329
|
-
type: 'string'
|
|
388
|
+
type: 'string',
|
|
389
|
+
description: '结果说明,失败时作为错误信息'
|
|
330
390
|
},
|
|
331
391
|
signature: {
|
|
332
392
|
type: 'string',
|
|
@@ -338,10 +398,12 @@ module.exports = fp(async (fastify, options) => {
|
|
|
338
398
|
type: 'object',
|
|
339
399
|
properties: {
|
|
340
400
|
taskId: {
|
|
341
|
-
type: 'string'
|
|
401
|
+
type: 'string',
|
|
402
|
+
description: '任务ID,body 未传 id 时使用'
|
|
342
403
|
}
|
|
343
404
|
}
|
|
344
|
-
}
|
|
405
|
+
},
|
|
406
|
+
response: { 200: { description: '处理完成,响应体为空' } }
|
|
345
407
|
}
|
|
346
408
|
},
|
|
347
409
|
async request => {
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
|
+
const { success, statistics } = require('../schema/response');
|
|
3
|
+
|
|
4
|
+
const TAG = '任务-统计';
|
|
2
5
|
|
|
3
6
|
module.exports = fp(async (fastify, options) => {
|
|
4
7
|
const { services } = fastify[options.name];
|
|
@@ -8,7 +11,9 @@ module.exports = fp(async (fastify, options) => {
|
|
|
8
11
|
{
|
|
9
12
|
onRequest: options.getAuthenticate('statistics'),
|
|
10
13
|
schema: {
|
|
14
|
+
tags: [TAG],
|
|
11
15
|
summary: '获取任务统计概览',
|
|
16
|
+
description: '按时间范围汇总已结束任务的数量、状态/类型/执行方式分布、每日与按小时趋势及耗时;不支持的 range 报错,其他查询异常时返回空统计',
|
|
12
17
|
query: {
|
|
13
18
|
type: 'object',
|
|
14
19
|
properties: {
|
|
@@ -17,7 +22,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
17
22
|
type: { type: 'string', description: '按任务类型筛选' },
|
|
18
23
|
runnerType: { type: 'string', description: '按执行方式筛选 manual / system' }
|
|
19
24
|
}
|
|
20
|
-
}
|
|
25
|
+
},
|
|
26
|
+
response: success('任务统计数据', statistics)
|
|
21
27
|
}
|
|
22
28
|
},
|
|
23
29
|
async request => {
|
|
@@ -32,7 +38,9 @@ module.exports = fp(async (fastify, options) => {
|
|
|
32
38
|
{
|
|
33
39
|
onRequest: options.getAuthenticate('statistics'),
|
|
34
40
|
schema: {
|
|
41
|
+
tags: [TAG],
|
|
35
42
|
summary: 'SSE实时推送任务统计数据',
|
|
43
|
+
description: '以 text/event-stream 长连接按 interval 推送实时统计(当天各状态数量、执行中/等待中任务数等),客户端断开后停止',
|
|
36
44
|
query: {
|
|
37
45
|
type: 'object',
|
|
38
46
|
properties: {
|
|
@@ -42,7 +50,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
42
50
|
runnerType: { type: 'string', description: '按执行方式筛选 manual / system' },
|
|
43
51
|
interval: { type: 'integer', minimum: 1, default: 5, description: '推送间隔时间(秒),最小1秒,默认5秒' }
|
|
44
52
|
}
|
|
45
|
-
}
|
|
53
|
+
},
|
|
54
|
+
response: { 200: { description: 'text/event-stream 事件流,每条 data 为实时统计 JSON' } }
|
|
46
55
|
}
|
|
47
56
|
},
|
|
48
57
|
async (request, reply) => {
|
|
@@ -17,6 +17,8 @@ const createContext = (fastify, options) => {
|
|
|
17
17
|
if (task.reload && typeof task.reload === 'function') {
|
|
18
18
|
await task.reload();
|
|
19
19
|
}
|
|
20
|
+
// errorHandler 重试会把任务改回 pending,此时任务未结束,等最终结束时再采集
|
|
21
|
+
if (!TERMINAL_STATUSES.includes(task.status)) return;
|
|
20
22
|
const completedAt = task.completedAt || new Date();
|
|
21
23
|
const createdAt = task.createdAt;
|
|
22
24
|
const startedAt = task.startedAt;
|
|
@@ -212,7 +214,17 @@ const createContext = (fastify, options) => {
|
|
|
212
214
|
const getTaskDeclaration = type => {
|
|
213
215
|
const declaration = resolveTaskDeclaration(type);
|
|
214
216
|
if (!declaration) {
|
|
215
|
-
|
|
217
|
+
const { taskName } = splitTaskName(type);
|
|
218
|
+
return registerTaskDeclaration({
|
|
219
|
+
namespace: mainNamespace,
|
|
220
|
+
taskName,
|
|
221
|
+
dirs: options.dirs,
|
|
222
|
+
scriptName: options.scriptName,
|
|
223
|
+
config: {},
|
|
224
|
+
override: false,
|
|
225
|
+
syncLegacyTask: false,
|
|
226
|
+
useGlobalDirs: true
|
|
227
|
+
}).declaration;
|
|
216
228
|
}
|
|
217
229
|
return declaration;
|
|
218
230
|
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// 响应 schema 只用于 OpenAPI 文档:字段可空、对象允许额外字段,避免序列化改变实际输出
|
|
2
|
+
const str = description => ({ type: 'string', nullable: true, description });
|
|
3
|
+
const num = description => ({ type: 'number', nullable: true, description });
|
|
4
|
+
// ID、日期、JSON 列可能是 number / string / Date / 任意结构,不声明类型,原样输出
|
|
5
|
+
const any = description => ({ description });
|
|
6
|
+
const obj = (description, properties = {}) => ({ type: 'object', nullable: true, additionalProperties: true, description, properties });
|
|
7
|
+
const list = (description, items) => ({ type: 'array', nullable: true, description, items });
|
|
8
|
+
|
|
9
|
+
const success = (description, schema) => ({ 200: Object.assign({}, schema || { type: 'object', additionalProperties: true }, { description }) });
|
|
10
|
+
const empty = description => success(description || '操作成功,返回空对象');
|
|
11
|
+
|
|
12
|
+
const TASK_STATUS = '任务状态:pending 待执行,running 执行中,waiting 等待外部回调/轮询,success 成功,failed 失败,canceled 已取消';
|
|
13
|
+
const RUNNER_TYPE = '执行方式:manual 手动执行,system 系统自动执行';
|
|
14
|
+
|
|
15
|
+
const task = obj('任务', {
|
|
16
|
+
id: any('任务 ID'),
|
|
17
|
+
type: str('任务类型'),
|
|
18
|
+
scriptName: str('任务脚本名称'),
|
|
19
|
+
targetId: any('任务目标对象 ID'),
|
|
20
|
+
targetType: str('任务目标对象类型'),
|
|
21
|
+
runnerType: str(RUNNER_TYPE),
|
|
22
|
+
priority: num('优先级,数值越大越优先'),
|
|
23
|
+
parentTaskId: any('父任务 ID'),
|
|
24
|
+
retryCount: num('已自动重试次数'),
|
|
25
|
+
maxRetries: num('最大自动重试次数,0 表示不自动重试'),
|
|
26
|
+
timeout: num('任务超时时间(毫秒),0 表示不超时'),
|
|
27
|
+
startTime: any('任务最早执行时间'),
|
|
28
|
+
startedAt: any('实际开始执行时间'),
|
|
29
|
+
completedAt: any('完成时间'),
|
|
30
|
+
userId: any('创建人用户 ID,系统创建为空'),
|
|
31
|
+
completedUserId: any('完成任务的用户 ID'),
|
|
32
|
+
input: any('输入数据'),
|
|
33
|
+
output: any('输出数据'),
|
|
34
|
+
error: any('错误信息'),
|
|
35
|
+
status: str(TASK_STATUS),
|
|
36
|
+
context: any('执行上下文,如回调签名密钥等'),
|
|
37
|
+
pollResults: any('轮询执行结果列表'),
|
|
38
|
+
pollCount: num('已轮询次数'),
|
|
39
|
+
progress: num('任务进度(0-100)'),
|
|
40
|
+
msg: str('任务消息或错误信息'),
|
|
41
|
+
options: any('扩展选项,logs 为最近 100 条任务日志(新的在前)'),
|
|
42
|
+
createdAt: any('创建时间'),
|
|
43
|
+
updatedAt: any('更新时间')
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const taskList = {
|
|
47
|
+
type: 'object',
|
|
48
|
+
additionalProperties: true,
|
|
49
|
+
properties: {
|
|
50
|
+
pageData: list('当前页任务列表,默认按创建时间倒序', task),
|
|
51
|
+
totalCount: num('符合条件的任务总数')
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const DURATION_ITEM = '每项为 { count, avgWaitingTime, avgExecutionTime, avgTotalTime }';
|
|
56
|
+
|
|
57
|
+
const bucket = obj('趋势桶', {
|
|
58
|
+
key: str('桶 key:YYYY-MM-DD(日 / 周起始日)或 YYYY-MM(月)'),
|
|
59
|
+
start: any('桶起点(含)'),
|
|
60
|
+
end: any('桶终点(不含);最后一个桶为当前时间')
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const duration = obj('范围内耗时汇总;查询异常时为 null', {
|
|
64
|
+
completedCount: num('已结束任务数'),
|
|
65
|
+
successCount: num('成功任务数'),
|
|
66
|
+
failedCount: num('失败任务数'),
|
|
67
|
+
canceledCount: num('取消任务数'),
|
|
68
|
+
avgWaitingTime: num('平均等待时间(毫秒),无耗时数据时为 0'),
|
|
69
|
+
avgExecutionTime: num('平均执行时间(毫秒),无耗时数据时为 0'),
|
|
70
|
+
avgTotalTime: num('平均总耗时(毫秒),无耗时数据时为 0'),
|
|
71
|
+
byType: obj(`按任务类型拆分,键为任务类型,${DURATION_ITEM}`),
|
|
72
|
+
byRunnerType: obj(`按执行方式拆分,键为 manual / system,${DURATION_ITEM}`),
|
|
73
|
+
byTypeByRunnerType: obj(`先按执行方式再按任务类型拆分,结构为 { [runnerType]: { [type]: 耗时项 } },${DURATION_ITEM}`)
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const statistics = {
|
|
77
|
+
type: 'object',
|
|
78
|
+
additionalProperties: true,
|
|
79
|
+
properties: {
|
|
80
|
+
range: str('时间范围:7d / 1m / 3m / 1y'),
|
|
81
|
+
rangeLabel: str('时间范围名称,如「近7天」'),
|
|
82
|
+
granularity: str('趋势粒度:7d / 1m 为 day,3m 为 week(周一起),1y 为 month'),
|
|
83
|
+
startTime: any('统计起始时间(第一个桶的起点)'),
|
|
84
|
+
endTime: any('统计截止时间(当前时间)'),
|
|
85
|
+
buckets: list('趋势桶列表,按时间正序', bucket),
|
|
86
|
+
totalTasks: num('范围内已结束(成功、失败、取消)的任务总数'),
|
|
87
|
+
byStatus: any('按状态统计的任务数,键为 success / failed / canceled'),
|
|
88
|
+
byType: any('按任务类型统计的任务数,键为任务类型'),
|
|
89
|
+
byRunnerType: any('按执行方式统计的任务数,键为 manual / system'),
|
|
90
|
+
recentTrend: any('按桶趋势,每项为 { date, count },date 为桶 key,无数据的桶补 0'),
|
|
91
|
+
recentTrendByStatus: any('按桶按状态趋势,每项为 { date, status, count },每个桶都含 success / failed / canceled'),
|
|
92
|
+
recentTrendByType: any('按桶按类型趋势,每项为 { date, type, count }'),
|
|
93
|
+
durationTrend: any('按桶耗时趋势,每项含 date(桶 key)、完成/成功/失败数及平均等待、执行、总耗时(毫秒)'),
|
|
94
|
+
duration,
|
|
95
|
+
hourlyTrend: any('按小时趋势,每项为 { date, hour, total, success, failed, canceled };查询失败或无数据时不返回'),
|
|
96
|
+
hourlyCompletionTrend: any('按小时、类型、执行方式的完成明细,每项含 date、hour、type、runnerType 及完成/成功/失败/取消数')
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
module.exports = { str, num, any, obj, list, success, empty, TASK_STATUS, RUNNER_TYPE, task, taskList, statistics };
|
|
@@ -63,12 +63,17 @@ module.exports = fp(async (fastify, options) => {
|
|
|
63
63
|
return fastify[options.name].services.log(Object.assign({}, props, { taskId: task.id }));
|
|
64
64
|
};
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
let timeoutTimer;
|
|
67
|
+
const createTimeout = ms =>
|
|
68
|
+
new Promise((_, reject) => {
|
|
69
|
+
timeoutTimer = setTimeout(() => reject(new Error(`任务执行超时(${ms}ms)`)), ms);
|
|
70
|
+
});
|
|
67
71
|
|
|
68
72
|
const executorPromise = context.executor({ type: task.type, scriptName: task.scriptName, task, log: addLog });
|
|
69
73
|
const racePromise = options.taskTimeout > 0 ? Promise.race([executorPromise, createTimeout(options.taskTimeout)]) : executorPromise;
|
|
70
74
|
|
|
71
75
|
return racePromise
|
|
76
|
+
.finally(() => clearTimeout(timeoutTimer))
|
|
72
77
|
.then(async result => {
|
|
73
78
|
if (result === false) {
|
|
74
79
|
return;
|
|
@@ -6,29 +6,56 @@ const timezone = require('dayjs/plugin/timezone');
|
|
|
6
6
|
dayjs.extend(utc);
|
|
7
7
|
dayjs.extend(timezone);
|
|
8
8
|
|
|
9
|
+
// granularity × count 决定趋势桶:period-stat 的 d/w 只保留当年,m 永久保留,所以 1y 必须按月分桶
|
|
9
10
|
const RANGE_MAP = {
|
|
10
|
-
'7d': {
|
|
11
|
-
'1m': {
|
|
12
|
-
'3m': {
|
|
13
|
-
'1y': {
|
|
11
|
+
'7d': { granularity: 'day', count: 7, label: '近7天' },
|
|
12
|
+
'1m': { granularity: 'day', count: 30, label: '近1个月' },
|
|
13
|
+
'3m': { granularity: 'week', count: 13, label: '近3个月' },
|
|
14
|
+
'1y': { granularity: 'month', count: 12, label: '近1年' }
|
|
14
15
|
};
|
|
15
16
|
|
|
17
|
+
const BUCKET_KEY_FORMAT = { day: 'YYYY-MM-DD', week: 'YYYY-MM-DD', month: 'YYYY-MM' };
|
|
18
|
+
const BUCKET_QUERY_CONCURRENCY = 6;
|
|
19
|
+
|
|
16
20
|
const RUNNER_TYPES = ['system', 'manual'];
|
|
17
21
|
const STAT_ATTRS = ['total', 'success', 'failed', 'canceled', 'waitingTime', 'executionTime', 'totalTime'];
|
|
22
|
+
const COMPLETED_STATUSES = ['success', 'failed', 'canceled'];
|
|
23
|
+
|
|
24
|
+
// 周以周一为起点,与 fastify-statistics 的 w 周期对齐
|
|
25
|
+
const startOfBucket = (d, granularity) => {
|
|
26
|
+
if (granularity === 'month') return d.startOf('month');
|
|
27
|
+
if (granularity === 'week') return d.subtract((d.day() + 6) % 7, 'day').startOf('day');
|
|
28
|
+
return d.startOf('day');
|
|
29
|
+
};
|
|
18
30
|
|
|
19
|
-
//
|
|
20
|
-
const
|
|
31
|
+
// 查询用客户端时间:桶边界按客户端时区计算,最后一个桶截止到当前时间
|
|
32
|
+
const buildRangeBuckets = (range = '7d', tz) => {
|
|
21
33
|
const config = RANGE_MAP[range];
|
|
22
34
|
if (!config) {
|
|
23
35
|
throw new Error(`不支持的时间范围: ${range}, 支持: ${Object.keys(RANGE_MAP).join(',')}`);
|
|
24
36
|
}
|
|
25
|
-
|
|
26
|
-
// 使用客户端时区计算"现在"和"起始时间",确保日期边界对齐客户端视角
|
|
37
|
+
const { granularity, count, label } = config;
|
|
27
38
|
const now = tz ? dayjs().tz(tz) : dayjs();
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
39
|
+
const first = startOfBucket(now, granularity).subtract(count - 1, granularity);
|
|
40
|
+
const buckets = Array.from({ length: count }, (_, i) => {
|
|
41
|
+
const start = first.add(i, granularity);
|
|
42
|
+
const end = i === count - 1 ? now : start.add(1, granularity);
|
|
43
|
+
return { key: start.format(BUCKET_KEY_FORMAT[granularity]), start: start.toDate(), end: end.toDate() };
|
|
44
|
+
});
|
|
45
|
+
return { range, label, granularity, buckets, startTime: buckets[0].start, endTime: now.toDate() };
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const mapWithConcurrency = async (items, limit, fn) => {
|
|
49
|
+
const results = new Array(items.length);
|
|
50
|
+
let cursor = 0;
|
|
51
|
+
const worker = async () => {
|
|
52
|
+
while (cursor < items.length) {
|
|
53
|
+
const index = cursor++;
|
|
54
|
+
results[index] = await fn(items[index], index);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
|
|
58
|
+
return results;
|
|
32
59
|
};
|
|
33
60
|
|
|
34
61
|
// 查询用客户端时间:formatDate 使用客户端时区格式化日期
|
|
@@ -43,9 +70,16 @@ const getHour = (date, tz) => {
|
|
|
43
70
|
return d.hour();
|
|
44
71
|
};
|
|
45
72
|
|
|
73
|
+
const getZonedChannelHour = (periodTime, serverHour, tz) => {
|
|
74
|
+
if (!tz) return { date: formatDate(periodTime), hour: serverHour };
|
|
75
|
+
const localPeriodTime = dayjs(periodTime).hour(serverHour).minute(0).second(0).millisecond(0);
|
|
76
|
+
const zonedTime = localPeriodTime.tz(tz);
|
|
77
|
+
return { date: zonedTime.format('YYYY-MM-DD'), hour: zonedTime.hour() };
|
|
78
|
+
};
|
|
79
|
+
|
|
46
80
|
const safeNum = v => {
|
|
47
81
|
const n = Number(v);
|
|
48
|
-
return
|
|
82
|
+
return typeof n === 'number' && !isNaN(n) ? n : 0;
|
|
49
83
|
};
|
|
50
84
|
|
|
51
85
|
const getSumData = item => (item.data || {}).sum || item.data || {};
|
|
@@ -62,9 +96,12 @@ const toAvgDuration = ({ count, sumWaiting, sumExecution, sumTotal }) => ({
|
|
|
62
96
|
|
|
63
97
|
const parseChannel = channel => {
|
|
64
98
|
const parts = channel.split(':');
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
99
|
+
const hour = Number(parts[2]);
|
|
100
|
+
return {
|
|
101
|
+
type: parts[0],
|
|
102
|
+
runnerType: parts.length >= 2 ? parts[1] : null,
|
|
103
|
+
completedHour: parts.length >= 3 && Number.isInteger(hour) && hour >= 0 && hour < 24 ? hour : null
|
|
104
|
+
};
|
|
68
105
|
};
|
|
69
106
|
|
|
70
107
|
const buildChannels = async (statisticsServices, type, runnerType) => {
|
|
@@ -81,6 +118,20 @@ const buildChannels = async (statisticsServices, type, runnerType) => {
|
|
|
81
118
|
return channels;
|
|
82
119
|
};
|
|
83
120
|
|
|
121
|
+
const buildHourlyChannels = (channels, runnerType) => {
|
|
122
|
+
const types = [...new Set(channels.map(ch => ch.split(':')[0]).filter(Boolean))];
|
|
123
|
+
const runnerTypes = runnerType ? [runnerType] : RUNNER_TYPES;
|
|
124
|
+
const hourlyChannels = [];
|
|
125
|
+
for (const type of types) {
|
|
126
|
+
for (const rt of runnerTypes) {
|
|
127
|
+
for (let hour = 0; hour < 24; hour += 1) {
|
|
128
|
+
hourlyChannels.push(`${type}:${rt}:${hour}`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return hourlyChannels;
|
|
133
|
+
};
|
|
134
|
+
|
|
84
135
|
// 确保时长累加器存在于 map 中
|
|
85
136
|
const ensureDurationAcc = (map, key) => {
|
|
86
137
|
if (!map[key]) map[key] = { count: 0, sumWaiting: 0, sumExecution: 0, sumTotal: 0 };
|
|
@@ -100,23 +151,61 @@ const createDailyMapEntry = () => ({ total: 0, byStatus: { success: 0, failed: 0
|
|
|
100
151
|
|
|
101
152
|
// 创建空的 dailyDurationMap 条目
|
|
102
153
|
const createDailyDurationEntry = () => ({
|
|
103
|
-
completedCount: 0,
|
|
104
|
-
|
|
105
|
-
|
|
154
|
+
completedCount: 0,
|
|
155
|
+
successCount: 0,
|
|
156
|
+
failedCount: 0,
|
|
157
|
+
canceledCount: 0,
|
|
158
|
+
sumWaiting: 0,
|
|
159
|
+
sumExecution: 0,
|
|
160
|
+
sumTotal: 0,
|
|
161
|
+
countWithTiming: 0,
|
|
162
|
+
byType: {},
|
|
163
|
+
byRunnerType: {},
|
|
164
|
+
byTypeByRunnerType: {}
|
|
106
165
|
});
|
|
107
166
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
167
|
+
const createHourlyAccumulator = () => ({
|
|
168
|
+
hourlyMap: {},
|
|
169
|
+
hourlyStatusMap: {},
|
|
170
|
+
hourlyTypeMap: {},
|
|
171
|
+
hourlyDetailMap: {}
|
|
172
|
+
});
|
|
112
173
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
174
|
+
const mergeHourlyItem = (result, { date, hour, typeName, runnerTypeName, count, sum, includeSummary = true }) => {
|
|
175
|
+
if (includeSummary) {
|
|
176
|
+
if (!result.hourlyMap[date]) result.hourlyMap[date] = {};
|
|
177
|
+
result.hourlyMap[date][hour] = (result.hourlyMap[date][hour] || 0) + count;
|
|
178
|
+
|
|
179
|
+
if (!result.hourlyStatusMap[date]) result.hourlyStatusMap[date] = {};
|
|
180
|
+
if (!result.hourlyStatusMap[date][hour]) result.hourlyStatusMap[date][hour] = {};
|
|
181
|
+
for (const status of ['success', 'failed', 'canceled']) {
|
|
182
|
+
const sc = safeNum(sum[status]);
|
|
183
|
+
if (sc > 0) result.hourlyStatusMap[date][hour][status] = (result.hourlyStatusMap[date][hour][status] || 0) + sc;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (!result.hourlyTypeMap[date]) result.hourlyTypeMap[date] = {};
|
|
187
|
+
if (!result.hourlyTypeMap[date][hour]) result.hourlyTypeMap[date][hour] = {};
|
|
188
|
+
if (count > 0) result.hourlyTypeMap[date][hour][typeName] = (result.hourlyTypeMap[date][hour][typeName] || 0) + count;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (runnerTypeName) {
|
|
192
|
+
if (!result.hourlyDetailMap[date]) result.hourlyDetailMap[date] = {};
|
|
193
|
+
if (!result.hourlyDetailMap[date][hour]) result.hourlyDetailMap[date][hour] = {};
|
|
194
|
+
if (!result.hourlyDetailMap[date][hour][typeName]) result.hourlyDetailMap[date][hour][typeName] = {};
|
|
195
|
+
if (!result.hourlyDetailMap[date][hour][typeName][runnerTypeName]) {
|
|
196
|
+
result.hourlyDetailMap[date][hour][typeName][runnerTypeName] = { total: 0, success: 0, failed: 0, canceled: 0 };
|
|
197
|
+
}
|
|
198
|
+
const detail = result.hourlyDetailMap[date][hour][typeName][runnerTypeName];
|
|
199
|
+
detail.total += count;
|
|
200
|
+
detail.success += safeNum(sum.success);
|
|
201
|
+
detail.failed += safeNum(sum.failed);
|
|
202
|
+
detail.canceled += safeNum(sum.canceled);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
119
205
|
|
|
206
|
+
// 解析 statistics 结果;resolveDate 决定条目归入哪个日期/桶,默认按客户端时区取条目日期
|
|
207
|
+
// 写入数据全使用服务器时间(DB 中存储的是服务器时间),查询用客户端时间转换展示
|
|
208
|
+
const parseStatList = (list, { timezone: tz, runnerType: runnerTypeFilter, resolveDate }) => {
|
|
120
209
|
const result = {
|
|
121
210
|
totalTasks: 0,
|
|
122
211
|
byStatus: { success: 0, failed: 0, canceled: 0 },
|
|
@@ -128,28 +217,39 @@ const queryAndParse = async (statisticsServices, { channels, startTime, endTime,
|
|
|
128
217
|
hourlyTypeMap: {},
|
|
129
218
|
hourlyDetailMap: {},
|
|
130
219
|
duration: {
|
|
131
|
-
completedCount: 0,
|
|
132
|
-
|
|
133
|
-
|
|
220
|
+
completedCount: 0,
|
|
221
|
+
successCount: 0,
|
|
222
|
+
failedCount: 0,
|
|
223
|
+
canceledCount: 0,
|
|
224
|
+
sumWaiting: 0,
|
|
225
|
+
sumExecution: 0,
|
|
226
|
+
sumTotal: 0,
|
|
227
|
+
countWithTiming: 0,
|
|
228
|
+
byType: {},
|
|
229
|
+
byRunnerType: {},
|
|
230
|
+
byTypeByRunnerType: {}
|
|
134
231
|
},
|
|
135
232
|
durationTrend: []
|
|
136
233
|
};
|
|
137
234
|
const dailyDurationMap = {};
|
|
138
235
|
|
|
139
|
-
for (const item of
|
|
236
|
+
for (const item of list || []) {
|
|
140
237
|
const { type: typeName, runnerType: runnerTypeName } = parseChannel(item.channel);
|
|
141
|
-
if (runnerTypeFilter && runnerTypeName
|
|
142
|
-
if (runnerTypeFilter && !runnerTypeName) continue;
|
|
238
|
+
if (runnerTypeFilter && runnerTypeName !== runnerTypeFilter) continue;
|
|
143
239
|
|
|
144
240
|
const sum = getSumData(item);
|
|
145
|
-
|
|
241
|
+
// 任务数只认终态:旧版本在失败重试(任务回到 pending)时也会采集一条,total 里混有这类非终态记录
|
|
242
|
+
const count = COMPLETED_STATUSES.reduce((n, status) => n + safeNum(sum[status]), 0);
|
|
243
|
+
// 耗时累计值包含上述记录,均值分母须用同口径的 total
|
|
244
|
+
const timingCount = safeNum(sum.total);
|
|
146
245
|
const isHourly = item.period === 'h';
|
|
147
246
|
// 查询用客户端时间:日期和小时使用客户端时区转换
|
|
148
|
-
const date = formatDate(item.time, tz);
|
|
247
|
+
const date = resolveDate ? resolveDate(item) : formatDate(item.time, tz);
|
|
149
248
|
const hour = isHourly ? getHour(item.time, tz) : null;
|
|
150
249
|
|
|
151
250
|
// ─── 1-segment channel: 全局 + 按日 + 时长 聚合 ───
|
|
152
|
-
|
|
251
|
+
// 按 runnerType 筛选时只查询了 2 段 channel,汇总改由匹配的 2 段 channel 提供
|
|
252
|
+
if (runnerTypeFilter || !runnerTypeName) {
|
|
153
253
|
// 全局汇总
|
|
154
254
|
result.totalTasks += count;
|
|
155
255
|
result.byStatus.success += safeNum(sum.success);
|
|
@@ -161,12 +261,12 @@ const queryAndParse = async (statisticsServices, { channels, startTime, endTime,
|
|
|
161
261
|
result.duration.completedCount += count;
|
|
162
262
|
result.duration.successCount += safeNum(sum.success);
|
|
163
263
|
result.duration.failedCount += safeNum(sum.failed);
|
|
164
|
-
result.duration.
|
|
264
|
+
result.duration.canceledCount += safeNum(sum.canceled);
|
|
165
265
|
result.duration.sumWaiting += safeNum(sum.waitingTime);
|
|
166
266
|
result.duration.sumExecution += safeNum(sum.executionTime);
|
|
167
267
|
result.duration.sumTotal += safeNum(sum.totalTime);
|
|
168
|
-
result.duration.countWithTiming +=
|
|
169
|
-
if (
|
|
268
|
+
result.duration.countWithTiming += timingCount;
|
|
269
|
+
if (timingCount > 0) accumDuration(ensureDurationAcc(result.duration.byType, typeName), timingCount, sum);
|
|
170
270
|
|
|
171
271
|
// 按日汇总
|
|
172
272
|
if (!result.dailyMap[date]) result.dailyMap[date] = createDailyMapEntry();
|
|
@@ -187,24 +287,12 @@ const queryAndParse = async (statisticsServices, { channels, startTime, endTime,
|
|
|
187
287
|
dd.sumWaiting += safeNum(sum.waitingTime);
|
|
188
288
|
dd.sumExecution += safeNum(sum.executionTime);
|
|
189
289
|
dd.sumTotal += safeNum(sum.totalTime);
|
|
190
|
-
dd.countWithTiming +=
|
|
191
|
-
if (
|
|
290
|
+
dd.countWithTiming += timingCount;
|
|
291
|
+
if (timingCount > 0) accumDuration(ensureDurationAcc(dd.byType, typeName), timingCount, sum);
|
|
192
292
|
|
|
193
293
|
// 小时级趋势(仅 period='h')
|
|
194
294
|
if (isHourly) {
|
|
195
|
-
|
|
196
|
-
result.hourlyMap[date][hour] = (result.hourlyMap[date][hour] || 0) + count;
|
|
197
|
-
|
|
198
|
-
if (!result.hourlyStatusMap[date]) result.hourlyStatusMap[date] = {};
|
|
199
|
-
if (!result.hourlyStatusMap[date][hour]) result.hourlyStatusMap[date][hour] = {};
|
|
200
|
-
for (const status of ['success', 'failed', 'canceled']) {
|
|
201
|
-
const sc = safeNum(sum[status]);
|
|
202
|
-
if (sc > 0) result.hourlyStatusMap[date][hour][status] = (result.hourlyStatusMap[date][hour][status] || 0) + sc;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
if (!result.hourlyTypeMap[date]) result.hourlyTypeMap[date] = {};
|
|
206
|
-
if (!result.hourlyTypeMap[date][hour]) result.hourlyTypeMap[date][hour] = {};
|
|
207
|
-
if (count > 0) result.hourlyTypeMap[date][hour][typeName] = (result.hourlyTypeMap[date][hour][typeName] || 0) + count;
|
|
295
|
+
mergeHourlyItem(result, { date, hour, typeName, runnerTypeName: null, count, sum });
|
|
208
296
|
}
|
|
209
297
|
}
|
|
210
298
|
|
|
@@ -212,37 +300,26 @@ const queryAndParse = async (statisticsServices, { channels, startTime, endTime,
|
|
|
212
300
|
if (runnerTypeName) {
|
|
213
301
|
result.byRunnerType[runnerTypeName] = (result.byRunnerType[runnerTypeName] || 0) + count;
|
|
214
302
|
|
|
215
|
-
if (
|
|
216
|
-
accumDuration(ensureDurationAcc(result.duration.byRunnerType, runnerTypeName),
|
|
303
|
+
if (timingCount > 0) {
|
|
304
|
+
accumDuration(ensureDurationAcc(result.duration.byRunnerType, runnerTypeName), timingCount, sum);
|
|
217
305
|
|
|
218
|
-
// byTypeByRunnerType(覆盖写入,非累加)
|
|
219
306
|
if (!result.duration.byTypeByRunnerType[runnerTypeName]) result.duration.byTypeByRunnerType[runnerTypeName] = {};
|
|
220
|
-
result.duration.byTypeByRunnerType[runnerTypeName]
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
accumDuration(ensureDurationAcc(dailyDurationMap[date].
|
|
307
|
+
accumDuration(ensureDurationAcc(result.duration.byTypeByRunnerType[runnerTypeName], typeName), timingCount, sum);
|
|
308
|
+
|
|
309
|
+
// 按日 byRunnerType 时长(同一时间点的 1 段 / 2 段 channel 顺序不固定,不能依赖前者先建条目)
|
|
310
|
+
if (!dailyDurationMap[date]) dailyDurationMap[date] = createDailyDurationEntry();
|
|
311
|
+
{
|
|
312
|
+
accumDuration(ensureDurationAcc(dailyDurationMap[date].byRunnerType, runnerTypeName), timingCount, sum);
|
|
313
|
+
if (!dailyDurationMap[date].byTypeByRunnerType[runnerTypeName]) {
|
|
314
|
+
dailyDurationMap[date].byTypeByRunnerType[runnerTypeName] = {};
|
|
315
|
+
}
|
|
316
|
+
accumDuration(ensureDurationAcc(dailyDurationMap[date].byTypeByRunnerType[runnerTypeName], typeName), timingCount, sum);
|
|
230
317
|
}
|
|
231
318
|
}
|
|
232
319
|
|
|
233
320
|
// hourlyDetailMap(仅 period='h')
|
|
234
321
|
if (isHourly) {
|
|
235
|
-
|
|
236
|
-
if (!result.hourlyDetailMap[date][hour]) result.hourlyDetailMap[date][hour] = {};
|
|
237
|
-
if (!result.hourlyDetailMap[date][hour][typeName]) result.hourlyDetailMap[date][hour][typeName] = {};
|
|
238
|
-
if (!result.hourlyDetailMap[date][hour][typeName][runnerTypeName]) {
|
|
239
|
-
result.hourlyDetailMap[date][hour][typeName][runnerTypeName] = { total: 0, success: 0, failed: 0, canceled: 0 };
|
|
240
|
-
}
|
|
241
|
-
const detail = result.hourlyDetailMap[date][hour][typeName][runnerTypeName];
|
|
242
|
-
detail.total += count;
|
|
243
|
-
detail.success += safeNum(sum.success);
|
|
244
|
-
detail.failed += safeNum(sum.failed);
|
|
245
|
-
detail.canceled += safeNum(sum.canceled);
|
|
322
|
+
mergeHourlyItem(result, { date, hour, typeName, runnerTypeName, count, sum, includeSummary: false });
|
|
246
323
|
}
|
|
247
324
|
}
|
|
248
325
|
}
|
|
@@ -260,14 +337,81 @@ const queryAndParse = async (statisticsServices, { channels, startTime, endTime,
|
|
|
260
337
|
avgExecutionTime: computeAvg(dd.sumExecution, dd.countWithTiming),
|
|
261
338
|
avgTotalTime: computeAvg(dd.sumTotal, dd.countWithTiming),
|
|
262
339
|
byType: Object.fromEntries(Object.entries(dd.byType).map(([n, td]) => [n, toAvgDuration(td)])),
|
|
263
|
-
byRunnerType: Object.fromEntries(Object.entries(dd.byRunnerType).map(([n, rt]) => [n, toAvgDuration(rt)]))
|
|
340
|
+
byRunnerType: Object.fromEntries(Object.entries(dd.byRunnerType).map(([n, rt]) => [n, toAvgDuration(rt)])),
|
|
341
|
+
byTypeByRunnerType: Object.fromEntries(Object.entries(dd.byTypeByRunnerType).map(([rtName, types]) => [rtName, Object.fromEntries(Object.entries(types).map(([typeName, td]) => [typeName, toAvgDuration(td)]))]))
|
|
264
342
|
}));
|
|
265
343
|
|
|
266
344
|
return result;
|
|
267
345
|
};
|
|
268
346
|
|
|
347
|
+
const queryAndParse = async (statisticsServices, { channels, startTime, endTime, timezone: tz, runnerType }) => {
|
|
348
|
+
if (channels.length === 0) return null;
|
|
349
|
+
const statResult = await statisticsServices.query({
|
|
350
|
+
channels,
|
|
351
|
+
startTime,
|
|
352
|
+
endTime,
|
|
353
|
+
attributeNames: STAT_ATTRS,
|
|
354
|
+
aggregates: ['sum'],
|
|
355
|
+
timezone: tz || undefined
|
|
356
|
+
});
|
|
357
|
+
return parseStatList(statResult.list, { timezone: tz, runnerType });
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
// fastify-statistics 会把区间拆成能对齐的最大周期(y/q/m/w/d/h),单次查询无法按日/周/月拆分,
|
|
361
|
+
// 因此逐桶查询,并把条目归到所属桶的 key 上
|
|
362
|
+
const queryBucketedAndParse = async (statisticsServices, { channels, buckets, timezone: tz, runnerType }) => {
|
|
363
|
+
if (channels.length === 0) return null;
|
|
364
|
+
const lists = await mapWithConcurrency(buckets, BUCKET_QUERY_CONCURRENCY, async bucket => {
|
|
365
|
+
const statResult = await statisticsServices.query({
|
|
366
|
+
channels,
|
|
367
|
+
startTime: bucket.start,
|
|
368
|
+
endTime: bucket.end,
|
|
369
|
+
attributeNames: STAT_ATTRS,
|
|
370
|
+
aggregates: ['sum'],
|
|
371
|
+
timezone: tz || undefined
|
|
372
|
+
});
|
|
373
|
+
return (statResult.list || []).map(item => ({ ...item, bucketKey: bucket.key }));
|
|
374
|
+
});
|
|
375
|
+
return parseStatList(lists.flat(), { timezone: tz, runnerType, resolveDate: item => item.bucketKey });
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
const queryHourlyChildChannels = async (statisticsServices, { channels, startTime, endTime, timezone: tz, runnerType: runnerTypeFilter }) => {
|
|
379
|
+
const hourlyChannels = buildHourlyChannels(channels, runnerTypeFilter);
|
|
380
|
+
if (hourlyChannels.length === 0) return null;
|
|
381
|
+
|
|
382
|
+
const statResult = await statisticsServices.query({
|
|
383
|
+
channels: hourlyChannels,
|
|
384
|
+
startTime,
|
|
385
|
+
endTime,
|
|
386
|
+
attributeNames: STAT_ATTRS,
|
|
387
|
+
aggregates: ['sum'],
|
|
388
|
+
timezone: tz || undefined
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
const result = createHourlyAccumulator();
|
|
392
|
+
for (const item of statResult.list || []) {
|
|
393
|
+
const { type: typeName, runnerType: runnerTypeName, completedHour } = parseChannel(item.channel);
|
|
394
|
+
if (completedHour == null) continue;
|
|
395
|
+
if (runnerTypeFilter && runnerTypeName !== runnerTypeFilter) continue;
|
|
396
|
+
|
|
397
|
+
const sum = getSumData(item);
|
|
398
|
+
const count = safeNum(sum.total);
|
|
399
|
+
const { date, hour } = getZonedChannelHour(item.time, completedHour, tz);
|
|
400
|
+
mergeHourlyItem(result, {
|
|
401
|
+
date,
|
|
402
|
+
hour,
|
|
403
|
+
typeName,
|
|
404
|
+
runnerTypeName,
|
|
405
|
+
count,
|
|
406
|
+
sum
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
return result;
|
|
411
|
+
};
|
|
412
|
+
|
|
269
413
|
// 构建 todayDuration 结果
|
|
270
|
-
const buildDurationResult =
|
|
414
|
+
const buildDurationResult = d => ({
|
|
271
415
|
completedCount: d.completedCount,
|
|
272
416
|
successCount: d.successCount,
|
|
273
417
|
failedCount: d.failedCount,
|
|
@@ -277,16 +421,11 @@ const buildDurationResult = (d) => ({
|
|
|
277
421
|
avgTotalTime: computeAvg(d.sumTotal, d.countWithTiming),
|
|
278
422
|
byType: Object.fromEntries(Object.entries(d.byType).map(([n, td]) => [n, toAvgDuration(td)])),
|
|
279
423
|
byRunnerType: Object.fromEntries(Object.entries(d.byRunnerType).map(([n, rt]) => [n, toAvgDuration(rt)])),
|
|
280
|
-
byTypeByRunnerType: Object.fromEntries(
|
|
281
|
-
Object.entries(d.byTypeByRunnerType).map(([rtName, types]) => [
|
|
282
|
-
rtName,
|
|
283
|
-
Object.fromEntries(Object.entries(types).map(([typeName, td]) => [typeName, toAvgDuration(td)]))
|
|
284
|
-
])
|
|
285
|
-
)
|
|
424
|
+
byTypeByRunnerType: Object.fromEntries(Object.entries(d.byTypeByRunnerType).map(([rtName, types]) => [rtName, Object.fromEntries(Object.entries(types).map(([typeName, td]) => [typeName, toAvgDuration(td)]))]))
|
|
286
425
|
});
|
|
287
426
|
|
|
288
427
|
// 构建小时趋势数组(完整格式,供 history 接口使用)
|
|
289
|
-
const buildHourlyTrendArrays =
|
|
428
|
+
const buildHourlyTrendArrays = parsed => {
|
|
290
429
|
const hourlyTrend = [];
|
|
291
430
|
const hourlyTrendByStatus = [];
|
|
292
431
|
const hourlyTrendByType = [];
|
|
@@ -299,17 +438,13 @@ const buildHourlyTrendArrays = (parsed) => {
|
|
|
299
438
|
const typeMap = parsed.hourlyTypeMap[date] || {};
|
|
300
439
|
const detailMap = parsed.hourlyDetailMap[date] || {};
|
|
301
440
|
|
|
302
|
-
const allHours = [...new Set([
|
|
303
|
-
...Object.keys(hoursMap).map(Number),
|
|
304
|
-
...Object.keys(statusMap).map(Number),
|
|
305
|
-
...Object.keys(typeMap).map(Number),
|
|
306
|
-
...Object.keys(detailMap).map(Number)
|
|
307
|
-
])].sort((a, b) => a - b);
|
|
441
|
+
const allHours = [...new Set([...Object.keys(hoursMap).map(Number), ...Object.keys(statusMap).map(Number), ...Object.keys(typeMap).map(Number), ...Object.keys(detailMap).map(Number)])].sort((a, b) => a - b);
|
|
308
442
|
|
|
309
443
|
for (const hour of allHours) {
|
|
310
444
|
const statuses = statusMap[hour] || {};
|
|
311
445
|
hourlyTrend.push({
|
|
312
|
-
date,
|
|
446
|
+
date,
|
|
447
|
+
hour,
|
|
313
448
|
total: hoursMap[hour] || 0,
|
|
314
449
|
success: statuses.success || 0,
|
|
315
450
|
failed: statuses.failed || 0,
|
|
@@ -324,13 +459,18 @@ const buildHourlyTrendArrays = (parsed) => {
|
|
|
324
459
|
}
|
|
325
460
|
}
|
|
326
461
|
|
|
327
|
-
for (const hour of Object.keys(detailMap)
|
|
462
|
+
for (const hour of Object.keys(detailMap)
|
|
463
|
+
.map(Number)
|
|
464
|
+
.sort((a, b) => a - b)) {
|
|
328
465
|
const types = detailMap[hour] || {};
|
|
329
466
|
for (const [typeName, runnerTypes] of Object.entries(types)) {
|
|
330
467
|
for (const [runnerTypeName, detail] of Object.entries(runnerTypes)) {
|
|
331
468
|
if (detail.total > 0) {
|
|
332
469
|
hourlyCompletionTrend.push({
|
|
333
|
-
date,
|
|
470
|
+
date,
|
|
471
|
+
hour,
|
|
472
|
+
type: typeName,
|
|
473
|
+
runnerType: runnerTypeName,
|
|
334
474
|
totalCompleted: detail.total,
|
|
335
475
|
successCount: detail.success,
|
|
336
476
|
failedCount: detail.failed,
|
|
@@ -345,14 +485,16 @@ const buildHourlyTrendArrays = (parsed) => {
|
|
|
345
485
|
return { hourlyTrend, hourlyTrendByStatus, hourlyTrendByType, hourlyCompletionTrend };
|
|
346
486
|
};
|
|
347
487
|
|
|
348
|
-
//
|
|
349
|
-
const buildDailyTrendArrays = (parsed) => {
|
|
350
|
-
const
|
|
351
|
-
const recentTrend =
|
|
488
|
+
// 构建历史趋势数组:按桶 key 输出,无数据的桶补 0,保证时间轴连续
|
|
489
|
+
const buildDailyTrendArrays = (parsed, bucketKeys) => {
|
|
490
|
+
const keys = bucketKeys || Object.keys(parsed.dailyMap).sort();
|
|
491
|
+
const recentTrend = [];
|
|
352
492
|
const recentTrendByStatus = [];
|
|
353
493
|
const recentTrendByType = [];
|
|
354
|
-
|
|
355
|
-
|
|
494
|
+
keys.forEach(date => {
|
|
495
|
+
const d = parsed.dailyMap[date] || createDailyMapEntry();
|
|
496
|
+
recentTrend.push({ date, count: d.total });
|
|
497
|
+
COMPLETED_STATUSES.forEach(status => recentTrendByStatus.push({ date, status, count: d.byStatus[status] || 0 }));
|
|
356
498
|
Object.entries(d.byType).forEach(([type, count]) => recentTrendByType.push({ date, type, count }));
|
|
357
499
|
});
|
|
358
500
|
return { recentTrend, recentTrendByStatus, recentTrendByType };
|
|
@@ -365,31 +507,50 @@ module.exports = fp(async (fastify, options) => {
|
|
|
365
507
|
|
|
366
508
|
// ─── queryStatistics (history 接口) ───
|
|
367
509
|
const queryStatistics = async ({ range = '7d', timezone, type, runnerType }) => {
|
|
368
|
-
//
|
|
369
|
-
const { startTime, endTime, label, range: rangeKey } =
|
|
510
|
+
// 查询用客户端时间:桶边界按 timezone 计算
|
|
511
|
+
const { startTime, endTime, label, range: rangeKey, granularity, buckets } = buildRangeBuckets(range, timezone);
|
|
512
|
+
const bucketKeys = buckets.map(b => b.key);
|
|
513
|
+
const rangeMeta = {
|
|
514
|
+
range: rangeKey,
|
|
515
|
+
rangeLabel: label,
|
|
516
|
+
granularity,
|
|
517
|
+
startTime,
|
|
518
|
+
endTime,
|
|
519
|
+
buckets: buckets.map(({ key, start, end }) => ({ key, start, end }))
|
|
520
|
+
};
|
|
370
521
|
const emptyResult = {
|
|
371
|
-
|
|
372
|
-
totalTasks: 0,
|
|
373
|
-
|
|
374
|
-
|
|
522
|
+
...rangeMeta,
|
|
523
|
+
totalTasks: 0,
|
|
524
|
+
byStatus: {},
|
|
525
|
+
byType: {},
|
|
526
|
+
byRunnerType: {},
|
|
527
|
+
...buildDailyTrendArrays({ dailyMap: {} }, bucketKeys),
|
|
528
|
+
durationTrend: [],
|
|
529
|
+
duration: null,
|
|
530
|
+
hourlyCompletionTrend: []
|
|
375
531
|
};
|
|
376
532
|
try {
|
|
377
533
|
const channels = await buildChannels(statisticsServices, type, runnerType);
|
|
378
|
-
const parsed = await
|
|
534
|
+
const parsed = await queryBucketedAndParse(statisticsServices, { channels, buckets, timezone, runnerType });
|
|
379
535
|
if (!parsed) return emptyResult;
|
|
536
|
+
const hourlyParsed = await queryHourlyChildChannels(statisticsServices, { channels, startTime, endTime, timezone, runnerType });
|
|
380
537
|
|
|
381
|
-
const { recentTrend, recentTrendByStatus, recentTrendByType } = buildDailyTrendArrays(parsed);
|
|
382
|
-
const { hourlyTrend, hourlyCompletionTrend } = buildHourlyTrendArrays(
|
|
538
|
+
const { recentTrend, recentTrendByStatus, recentTrendByType } = buildDailyTrendArrays(parsed, bucketKeys);
|
|
539
|
+
const { hourlyTrend, hourlyCompletionTrend } = buildHourlyTrendArrays(hourlyParsed || createHourlyAccumulator());
|
|
383
540
|
|
|
384
541
|
return {
|
|
385
|
-
|
|
542
|
+
...rangeMeta,
|
|
386
543
|
totalTasks: parsed.totalTasks,
|
|
387
544
|
byStatus: parsed.byStatus,
|
|
388
545
|
byType: parsed.byType,
|
|
389
546
|
byRunnerType: parsed.byRunnerType,
|
|
390
|
-
recentTrend,
|
|
547
|
+
recentTrend,
|
|
548
|
+
recentTrendByStatus,
|
|
549
|
+
recentTrendByType,
|
|
391
550
|
durationTrend: parsed.durationTrend,
|
|
392
|
-
|
|
551
|
+
duration: buildDurationResult(parsed.duration),
|
|
552
|
+
hourlyTrend,
|
|
553
|
+
hourlyCompletionTrend
|
|
393
554
|
};
|
|
394
555
|
} catch (e) {
|
|
395
556
|
fastify.log.error(`查询统计数据失败: ${e.message}`);
|
|
@@ -410,7 +571,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
410
571
|
TaskModel.findAll({
|
|
411
572
|
attributes: ['runnerType', 'status', [fn('COUNT', col('id')), 'count']],
|
|
412
573
|
where: { ...(runnerType ? { runnerType } : {}) },
|
|
413
|
-
group: ['runnerType', 'status'],
|
|
574
|
+
group: ['runnerType', 'status'],
|
|
575
|
+
raw: true
|
|
414
576
|
}),
|
|
415
577
|
TaskModel.findAll({
|
|
416
578
|
attributes: ['runnerType', 'createdAt'],
|
|
@@ -474,9 +636,16 @@ module.exports = fp(async (fastify, options) => {
|
|
|
474
636
|
let hourlyTrendByType = [];
|
|
475
637
|
let intervalTrend = [];
|
|
476
638
|
let todayDuration = {
|
|
477
|
-
completedCount: 0,
|
|
478
|
-
|
|
479
|
-
|
|
639
|
+
completedCount: 0,
|
|
640
|
+
successCount: 0,
|
|
641
|
+
failedCount: 0,
|
|
642
|
+
canceledCount: 0,
|
|
643
|
+
avgWaitingTime: 0,
|
|
644
|
+
avgExecutionTime: 0,
|
|
645
|
+
avgTotalTime: 0,
|
|
646
|
+
byType: {},
|
|
647
|
+
byRunnerType: {},
|
|
648
|
+
byTypeByRunnerType: {}
|
|
480
649
|
};
|
|
481
650
|
|
|
482
651
|
try {
|
|
@@ -493,17 +662,11 @@ module.exports = fp(async (fastify, options) => {
|
|
|
493
662
|
const hourlyArrays = buildHourlyTrendArrays(parsed);
|
|
494
663
|
|
|
495
664
|
// SSE 格式:简化为 {hour, count},只取当天
|
|
496
|
-
hourlyTrend = hourlyArrays.hourlyTrend
|
|
497
|
-
.filter(h => h.date === todayStr)
|
|
498
|
-
.map(({ hour, total }) => ({ hour, count: total }));
|
|
665
|
+
hourlyTrend = hourlyArrays.hourlyTrend.filter(h => h.date === todayStr).map(({ hour, total }) => ({ hour, count: total }));
|
|
499
666
|
|
|
500
|
-
hourlyTrendByStatus = hourlyArrays.hourlyTrendByStatus
|
|
501
|
-
.filter(h => h.date === todayStr)
|
|
502
|
-
.map(({ hour, status, count }) => ({ hour, status, count }));
|
|
667
|
+
hourlyTrendByStatus = hourlyArrays.hourlyTrendByStatus.filter(h => h.date === todayStr).map(({ hour, status, count }) => ({ hour, status, count }));
|
|
503
668
|
|
|
504
|
-
hourlyTrendByType = hourlyArrays.hourlyTrendByType
|
|
505
|
-
.filter(h => h.date === todayStr)
|
|
506
|
-
.map(({ hour, type, count }) => ({ hour, type, count }));
|
|
669
|
+
hourlyTrendByType = hourlyArrays.hourlyTrendByType.filter(h => h.date === todayStr).map(({ hour, type, count }) => ({ hour, type, count }));
|
|
507
670
|
|
|
508
671
|
intervalTrend = hourlyTrend.map(({ hour, count }) => ({
|
|
509
672
|
interval: `${String(hour).padStart(2, '0')}:00`,
|
package/libs/services/task.js
CHANGED
|
@@ -4,7 +4,9 @@ const getTaskServiceContext = require('../helpers/task-service-context');
|
|
|
4
4
|
module.exports = fp(async (fastify, options) => {
|
|
5
5
|
const context = getTaskServiceContext(fastify, options);
|
|
6
6
|
const { models, Op } = context;
|
|
7
|
+
const Sequelize = fastify.sequelize.Sequelize;
|
|
7
8
|
const SORTABLE_FIELDS = new Set(['id', 'targetId', 'targetType', 'type', 'status', 'runnerType', 'priority', 'createdAt', 'updatedAt', 'startTime', 'startedAt', 'completedAt']);
|
|
9
|
+
const INTEGER_PRIORITY_MAX = 2147483647;
|
|
8
10
|
|
|
9
11
|
const cancel = async ({ id, targetId, targetType, type }) => {
|
|
10
12
|
if (!id && !(targetId && targetType && type)) {
|
|
@@ -73,7 +75,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
73
75
|
await context.updateTaskByStatus({
|
|
74
76
|
task,
|
|
75
77
|
allowedStatuses: ['pending'],
|
|
76
|
-
updateData: { priority:
|
|
78
|
+
updateData: { priority: INTEGER_PRIORITY_MAX }
|
|
77
79
|
});
|
|
78
80
|
const claimedTask = await fastify[options.name].services.claimTask(task);
|
|
79
81
|
if (claimedTask) {
|
|
@@ -142,7 +144,76 @@ module.exports = fp(async (fastify, options) => {
|
|
|
142
144
|
}
|
|
143
145
|
};
|
|
144
146
|
|
|
145
|
-
const
|
|
147
|
+
const normalizeListQuery = (query = {}) => {
|
|
148
|
+
const normalized = Object.assign({}, query);
|
|
149
|
+
|
|
150
|
+
Object.entries(query || {}).forEach(([rawKey, value]) => {
|
|
151
|
+
const match = rawKey.match(/^(filter|sort)\[([^\]]+)\](?:\[([^\]]+)\])?$/);
|
|
152
|
+
if (!match) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const [, root, key, childKey] = match;
|
|
157
|
+
if (!normalized[root] || typeof normalized[root] !== 'object' || Array.isArray(normalized[root])) {
|
|
158
|
+
normalized[root] = {};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (childKey) {
|
|
162
|
+
if (!normalized[root][key] || typeof normalized[root][key] !== 'object' || Array.isArray(normalized[root][key])) {
|
|
163
|
+
normalized[root][key] = {};
|
|
164
|
+
}
|
|
165
|
+
normalized[root][key][childKey] = value;
|
|
166
|
+
} else {
|
|
167
|
+
normalized[root][key] = value;
|
|
168
|
+
}
|
|
169
|
+
delete normalized[rawKey];
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
return normalized;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const quoteIdentifier = identifier => `"${String(identifier).replaceAll('"', '""')}"`;
|
|
176
|
+
|
|
177
|
+
const getJsonTextExpression = fieldPath => {
|
|
178
|
+
if (typeof Sequelize.literal !== 'function') {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const [field, key] = String(fieldPath).split('.');
|
|
183
|
+
const column = quoteIdentifier(models.task.rawAttributes?.[field]?.field || field);
|
|
184
|
+
const dialect = models.task.sequelize?.getDialect?.();
|
|
185
|
+
|
|
186
|
+
if (dialect === 'postgres') {
|
|
187
|
+
return Sequelize.literal(`${column}->>'${key}'`);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (dialect === 'sqlite') {
|
|
191
|
+
return Sequelize.literal(`json_extract(${column}, '$.${key}')`);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return typeof Sequelize.json === 'function' ? Sequelize.json(fieldPath) : null;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const appendJsonFieldLikeFilter = (whereQuery, fieldPath, value) => {
|
|
198
|
+
const keyword = String(value).toLowerCase();
|
|
199
|
+
const condition = {
|
|
200
|
+
[Op.like]: `%${keyword}%`
|
|
201
|
+
};
|
|
202
|
+
const fallbackCondition = {
|
|
203
|
+
[Op.like]: `%${value}%`
|
|
204
|
+
};
|
|
205
|
+
const jsonTextExpression = getJsonTextExpression(fieldPath);
|
|
206
|
+
|
|
207
|
+
if (typeof Sequelize.where === 'function' && typeof Sequelize.fn === 'function' && jsonTextExpression && Op.and) {
|
|
208
|
+
whereQuery[Op.and] = [...(whereQuery[Op.and] || []), Sequelize.where(Sequelize.fn('lower', jsonTextExpression), condition)];
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
whereQuery[fieldPath] = fallbackCondition;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const list = async query => {
|
|
216
|
+
const { filter, perPage = 20, currentPage = 1, sort } = normalizeListQuery(query);
|
|
146
217
|
const whereQuery = {};
|
|
147
218
|
|
|
148
219
|
['id', 'targetId', 'type', 'status', 'runnerType'].forEach(key => {
|
|
@@ -151,6 +222,10 @@ module.exports = fp(async (fastify, options) => {
|
|
|
151
222
|
}
|
|
152
223
|
});
|
|
153
224
|
|
|
225
|
+
if (filter && filter.targetName) {
|
|
226
|
+
appendJsonFieldLikeFilter(whereQuery, 'input.name', filter.targetName);
|
|
227
|
+
}
|
|
228
|
+
|
|
154
229
|
if (filter && filter.createdAt) {
|
|
155
230
|
whereQuery.createdAt = getTimeQuery(filter.createdAt);
|
|
156
231
|
}
|