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