@kne/fastify-task 3.0.0-alpha.4 → 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/libs/services/statistics.js +45 -25
- package/package.json +2 -1
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
|
+
const dayjs = require('dayjs');
|
|
3
|
+
const utc = require('dayjs/plugin/utc');
|
|
4
|
+
const timezone = require('dayjs/plugin/timezone');
|
|
5
|
+
|
|
6
|
+
dayjs.extend(utc);
|
|
7
|
+
dayjs.extend(timezone);
|
|
2
8
|
|
|
3
9
|
const RANGE_MAP = {
|
|
4
10
|
'7d': { value: 7, unit: 'day', label: '近7天' },
|
|
@@ -10,22 +16,33 @@ const RANGE_MAP = {
|
|
|
10
16
|
const RUNNER_TYPES = ['system', 'manual'];
|
|
11
17
|
const STAT_ATTRS = ['total', 'success', 'failed', 'canceled', 'waitingTime', 'executionTime', 'totalTime'];
|
|
12
18
|
|
|
13
|
-
|
|
19
|
+
// 查询用客户端时间:parseRange 使用客户端时区计算日期范围
|
|
20
|
+
const parseRange = (range = '7d', tz) => {
|
|
14
21
|
const config = RANGE_MAP[range];
|
|
15
22
|
if (!config) {
|
|
16
23
|
throw new Error(`不支持的时间范围: ${range}, 支持: ${Object.keys(RANGE_MAP).join(',')}`);
|
|
17
24
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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 };
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// 查询用客户端时间:formatDate 使用客户端时区格式化日期
|
|
37
|
+
const formatDate = (date, tz) => {
|
|
38
|
+
const d = tz ? dayjs(date).tz(tz) : dayjs(date);
|
|
39
|
+
return d.format('YYYY-MM-DD');
|
|
24
40
|
};
|
|
25
41
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
42
|
+
// 查询用客户端时间:提取客户端时区下的小时
|
|
43
|
+
const getHour = (date, tz) => {
|
|
44
|
+
const d = tz ? dayjs(date).tz(tz) : dayjs(date);
|
|
45
|
+
return d.hour();
|
|
29
46
|
};
|
|
30
47
|
|
|
31
48
|
const safeNum = v => {
|
|
@@ -91,14 +108,15 @@ const createDailyDurationEntry = () => ({
|
|
|
91
108
|
});
|
|
92
109
|
|
|
93
110
|
// 查询 statistics 并解析结果
|
|
94
|
-
|
|
111
|
+
// 写入数据全使用服务器时间(DB 中存储的是服务器时间),查询用客户端时间转换展示
|
|
112
|
+
const queryAndParse = async (statisticsServices, { channels, startTime, endTime, timezone: tz, runnerType: runnerTypeFilter }) => {
|
|
95
113
|
if (channels.length === 0) return null;
|
|
96
114
|
|
|
97
115
|
const statResult = await statisticsServices.query({
|
|
98
116
|
channels, startTime, endTime,
|
|
99
117
|
attributeNames: STAT_ATTRS,
|
|
100
118
|
aggregates: ['sum'],
|
|
101
|
-
timezone:
|
|
119
|
+
timezone: tz || undefined
|
|
102
120
|
});
|
|
103
121
|
|
|
104
122
|
const result = {
|
|
@@ -128,8 +146,9 @@ const queryAndParse = async (statisticsServices, { channels, startTime, endTime,
|
|
|
128
146
|
const sum = getSumData(item);
|
|
129
147
|
const count = safeNum(sum.total);
|
|
130
148
|
const isHourly = item.period === 'h';
|
|
131
|
-
|
|
132
|
-
const
|
|
149
|
+
// 查询用客户端时间:日期和小时使用客户端时区转换
|
|
150
|
+
const date = formatDate(item.time, tz);
|
|
151
|
+
const hour = isHourly ? getHour(item.time, tz) : null;
|
|
133
152
|
|
|
134
153
|
// ─── 1-segment channel: 全局 + 按日 + 时长 聚合 ───
|
|
135
154
|
if (!runnerTypeName) {
|
|
@@ -144,7 +163,7 @@ const queryAndParse = async (statisticsServices, { channels, startTime, endTime,
|
|
|
144
163
|
result.duration.completedCount += count;
|
|
145
164
|
result.duration.successCount += safeNum(sum.success);
|
|
146
165
|
result.duration.failedCount += safeNum(sum.failed);
|
|
147
|
-
result.duration.
|
|
166
|
+
result.duration.canceled += safeNum(sum.canceled);
|
|
148
167
|
result.duration.sumWaiting += safeNum(sum.waitingTime);
|
|
149
168
|
result.duration.sumExecution += safeNum(sum.executionTime);
|
|
150
169
|
result.duration.sumTotal += safeNum(sum.totalTime);
|
|
@@ -348,7 +367,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
348
367
|
|
|
349
368
|
// ─── queryStatistics (history 接口) ───
|
|
350
369
|
const queryStatistics = async ({ range = '7d', timezone, type, runnerType }) => {
|
|
351
|
-
|
|
370
|
+
// 查询用客户端时间:parseRange 传入 timezone
|
|
371
|
+
const { startTime, endTime, label, range: rangeKey } = parseRange(range, timezone);
|
|
352
372
|
const emptyResult = {
|
|
353
373
|
range: rangeKey, rangeLabel: label,
|
|
354
374
|
totalTasks: 0, byStatus: {}, byType: {}, byRunnerType: {},
|
|
@@ -380,11 +400,12 @@ module.exports = fp(async (fastify, options) => {
|
|
|
380
400
|
};
|
|
381
401
|
|
|
382
402
|
// ─── buildSseData (SSE 接口) ───
|
|
383
|
-
const buildSseData = async ({ timezone, type, runnerType }) => {
|
|
384
|
-
|
|
385
|
-
const
|
|
386
|
-
const
|
|
387
|
-
const
|
|
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');
|
|
388
409
|
|
|
389
410
|
// 统计查询:正在执行和等待执行的数量 + DB 查询
|
|
390
411
|
const [runnerTypeStatusRows, waitingTasks] = await Promise.all([
|
|
@@ -462,7 +483,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
462
483
|
|
|
463
484
|
try {
|
|
464
485
|
const channels = await buildChannels(statisticsServices, type, runnerType);
|
|
465
|
-
const parsed = await queryAndParse(statisticsServices, { channels, startTime: todayStart, endTime: todayEnd, timezone, runnerType });
|
|
486
|
+
const parsed = await queryAndParse(statisticsServices, { channels, startTime: todayStart, endTime: todayEnd, timezone: tz, runnerType });
|
|
466
487
|
if (parsed) {
|
|
467
488
|
totalTasks = parsed.totalTasks + dbStatusCount.running + dbWaitingTotal;
|
|
468
489
|
byStatus = { ...parsed.byStatus, running: dbStatusCount.running, waiting: dbWaitingTotal, pending: 0 };
|
|
@@ -500,9 +521,8 @@ module.exports = fp(async (fastify, options) => {
|
|
|
500
521
|
}
|
|
501
522
|
|
|
502
523
|
// 补充当前小时的 running/waiting 瞬时状态(来自 DB 实时查询,不受 statistics 服务影响)
|
|
503
|
-
//
|
|
504
|
-
|
|
505
|
-
const currentHour = now.getHours();
|
|
524
|
+
// 查询用客户端时间:当前小时使用客户端时区
|
|
525
|
+
const currentHour = now.hour();
|
|
506
526
|
if (dbStatusCount.running > 0) hourlyTrendByStatus.push({ hour: currentHour, status: 'running', count: dbStatusCount.running });
|
|
507
527
|
if (dbWaitingTotal > 0) hourlyTrendByStatus.push({ hour: currentHour, status: 'waiting', count: dbWaitingTotal });
|
|
508
528
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kne/fastify-task",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.5",
|
|
4
4
|
"description": "一个 Fastify 插件,用于任务编排、追踪任务执行状态和结果。支持系统自动执行和手动执行两种模式,提供完整的任务生命周期管理",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -57,6 +57,7 @@
|
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
59
|
"@kne/fastify-statistics": "^0.1.0-alpha.10",
|
|
60
|
+
"dayjs": "^1.11.20",
|
|
60
61
|
"fastify-cron": "^1.4.0",
|
|
61
62
|
"fs-extra": "^11.3.0"
|
|
62
63
|
}
|