@kne/fastify-task 2.0.0 → 2.0.1
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/main.js +170 -41
- package/package.json +1 -1
package/libs/services/main.js
CHANGED
|
@@ -557,6 +557,28 @@ module.exports = fp(async (fastify, options) => {
|
|
|
557
557
|
|
|
558
558
|
const resolveTimezone = tz => tz || dayjs.tz.guess();
|
|
559
559
|
|
|
560
|
+
/** 与 updateDailyStatistics 一致的耗时计算(用于实时接口在无日统计时的回算) */
|
|
561
|
+
const computeTaskTiming = task => {
|
|
562
|
+
const completedAt = task.completedAt ? new Date(task.completedAt) : null;
|
|
563
|
+
const createdAt = task.createdAt ? new Date(task.createdAt) : null;
|
|
564
|
+
const startedAt = task.startedAt ? new Date(task.startedAt) : null;
|
|
565
|
+
let waitingTime = 0;
|
|
566
|
+
let executionTime = 0;
|
|
567
|
+
let totalTime = 0;
|
|
568
|
+
let hasTiming = false;
|
|
569
|
+
if (createdAt && completedAt && !Number.isNaN(createdAt.getTime()) && !Number.isNaN(completedAt.getTime())) {
|
|
570
|
+
totalTime = completedAt.getTime() - createdAt.getTime();
|
|
571
|
+
if (startedAt && !Number.isNaN(startedAt.getTime())) {
|
|
572
|
+
waitingTime = startedAt.getTime() - createdAt.getTime();
|
|
573
|
+
executionTime = completedAt.getTime() - startedAt.getTime();
|
|
574
|
+
} else {
|
|
575
|
+
executionTime = totalTime;
|
|
576
|
+
}
|
|
577
|
+
hasTiming = totalTime > 0;
|
|
578
|
+
}
|
|
579
|
+
return { waitingTime, executionTime, totalTime, hasTiming };
|
|
580
|
+
};
|
|
581
|
+
|
|
560
582
|
// 更新每日统计表(任务完成/取消时调用,异步不阻塞主流程)
|
|
561
583
|
const updateDailyStatistics = task => {
|
|
562
584
|
const completedAt = task.completedAt || new Date();
|
|
@@ -1009,51 +1031,158 @@ module.exports = fp(async (fastify, options) => {
|
|
|
1009
1031
|
})),
|
|
1010
1032
|
intervalTrend: intervalTrend.map(item => ({ interval: adjustInterval(item.interval), count: Number(item.count) })),
|
|
1011
1033
|
todayDuration: await (async () => {
|
|
1034
|
+
const statDateKey = formatDate(todayStart, effectiveTimezone);
|
|
1012
1035
|
const todayStat = await models.taskDailyStatistics.findOne({
|
|
1013
|
-
where: { date:
|
|
1036
|
+
where: { date: statDateKey },
|
|
1014
1037
|
raw: true
|
|
1015
1038
|
});
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1039
|
+
|
|
1040
|
+
const emptyDuration = () => ({
|
|
1041
|
+
completedCount: 0,
|
|
1042
|
+
successCount: 0,
|
|
1043
|
+
failedCount: 0,
|
|
1044
|
+
canceledCount: 0,
|
|
1045
|
+
avgWaitingTime: 0,
|
|
1046
|
+
avgExecutionTime: 0,
|
|
1047
|
+
avgTotalTime: 0,
|
|
1048
|
+
byType: {},
|
|
1049
|
+
byRunnerType: {}
|
|
1050
|
+
});
|
|
1051
|
+
|
|
1052
|
+
const mapStatToDuration = baseStat => {
|
|
1053
|
+
if (!baseStat) return emptyDuration();
|
|
1054
|
+
const typeData = type && baseStat.byType && baseStat.byType[type] ? baseStat.byType[type] : null;
|
|
1055
|
+
const runnerTypeData = runnerType && baseStat.byRunnerType && baseStat.byRunnerType[runnerType] ? baseStat.byRunnerType[runnerType] : null;
|
|
1056
|
+
const filterData = runnerTypeData || typeData;
|
|
1057
|
+
const mainTimedTaskCount = filterData ? filterData.timedTaskCount : baseStat.timedTaskCount;
|
|
1058
|
+
const mainTotalWaitingTime = filterData ? filterData.totalWaitingTime : baseStat.totalWaitingTime;
|
|
1059
|
+
const mainTotalExecutionTime = filterData ? filterData.totalExecutionTime : baseStat.totalExecutionTime;
|
|
1060
|
+
const mainTotalTime = filterData ? filterData.totalTime : baseStat.totalTime;
|
|
1061
|
+
|
|
1062
|
+
return {
|
|
1063
|
+
completedCount: filterData ? filterData.count : baseStat.totalCompleted,
|
|
1064
|
+
successCount: filterData ? filterData.successCount : baseStat.successCount,
|
|
1065
|
+
failedCount: filterData ? filterData.failedCount : baseStat.failedCount,
|
|
1066
|
+
canceledCount: filterData ? filterData.canceledCount : baseStat.canceledCount,
|
|
1067
|
+
avgWaitingTime: mainTimedTaskCount > 0 ? Math.round(mainTotalWaitingTime / mainTimedTaskCount) : 0,
|
|
1068
|
+
avgExecutionTime: mainTimedTaskCount > 0 ? Math.round(mainTotalExecutionTime / mainTimedTaskCount) : 0,
|
|
1069
|
+
avgTotalTime: mainTimedTaskCount > 0 ? Math.round(mainTotalTime / mainTimedTaskCount) : 0,
|
|
1070
|
+
byType: Object.entries(baseStat.byType || {}).reduce((acc, [key, val]) => {
|
|
1071
|
+
if (type && key !== type) return acc;
|
|
1072
|
+
acc[key] = {
|
|
1073
|
+
count: val.count,
|
|
1074
|
+
avgWaitingTime: val.timedTaskCount > 0 ? Math.round(val.totalWaitingTime / val.timedTaskCount) : 0,
|
|
1075
|
+
avgExecutionTime: val.timedTaskCount > 0 ? Math.round(val.totalExecutionTime / val.timedTaskCount) : 0,
|
|
1076
|
+
avgTotalTime: val.timedTaskCount > 0 ? Math.round(val.totalTime / val.timedTaskCount) : 0
|
|
1077
|
+
};
|
|
1078
|
+
return acc;
|
|
1079
|
+
}, {}),
|
|
1080
|
+
byRunnerType: Object.entries(baseStat.byRunnerType || {}).reduce((acc, [key, val]) => {
|
|
1081
|
+
if (runnerType && key !== runnerType) return acc;
|
|
1082
|
+
acc[key] = {
|
|
1083
|
+
count: val.count,
|
|
1084
|
+
avgWaitingTime: val.timedTaskCount > 0 ? Math.round(val.totalWaitingTime / val.timedTaskCount) : 0,
|
|
1085
|
+
avgExecutionTime: val.timedTaskCount > 0 ? Math.round(val.totalExecutionTime / val.timedTaskCount) : 0,
|
|
1086
|
+
avgTotalTime: val.timedTaskCount > 0 ? Math.round(val.totalTime / val.timedTaskCount) : 0
|
|
1087
|
+
};
|
|
1088
|
+
return acc;
|
|
1089
|
+
}, {})
|
|
1090
|
+
};
|
|
1091
|
+
};
|
|
1092
|
+
|
|
1093
|
+
const aggregateDurationFromTodayTasks = async () => {
|
|
1094
|
+
const terminalStatuses = ['success', 'failed', 'canceled'];
|
|
1095
|
+
const tasks = await taskModel.findAll({
|
|
1096
|
+
where: {
|
|
1097
|
+
...whereToday,
|
|
1098
|
+
status: { [Op.in]: terminalStatuses },
|
|
1099
|
+
completedAt: { [Op.ne]: null }
|
|
1100
|
+
},
|
|
1101
|
+
attributes: ['type', 'runnerType', 'status', 'createdAt', 'startedAt', 'completedAt'],
|
|
1102
|
+
raw: true
|
|
1103
|
+
});
|
|
1104
|
+
|
|
1105
|
+
const newBucket = () => ({
|
|
1106
|
+
count: 0,
|
|
1107
|
+
successCount: 0,
|
|
1108
|
+
failedCount: 0,
|
|
1109
|
+
canceledCount: 0,
|
|
1110
|
+
totalWaitingTime: 0,
|
|
1111
|
+
totalExecutionTime: 0,
|
|
1112
|
+
totalTime: 0,
|
|
1113
|
+
timedTaskCount: 0
|
|
1114
|
+
});
|
|
1115
|
+
|
|
1116
|
+
const bump = (bucket, task, timing) => {
|
|
1117
|
+
bucket.count += 1;
|
|
1118
|
+
if (task.status === 'success') bucket.successCount += 1;
|
|
1119
|
+
else if (task.status === 'failed') bucket.failedCount += 1;
|
|
1120
|
+
else if (task.status === 'canceled') bucket.canceledCount += 1;
|
|
1121
|
+
if (timing.hasTiming) {
|
|
1122
|
+
bucket.totalWaitingTime += timing.waitingTime;
|
|
1123
|
+
bucket.totalExecutionTime += timing.executionTime;
|
|
1124
|
+
bucket.totalTime += timing.totalTime;
|
|
1125
|
+
bucket.timedTaskCount += 1;
|
|
1126
|
+
}
|
|
1127
|
+
};
|
|
1128
|
+
|
|
1129
|
+
const byTypeAcc = {};
|
|
1130
|
+
const byRunnerAcc = {};
|
|
1131
|
+
const main = newBucket();
|
|
1132
|
+
|
|
1133
|
+
for (const task of tasks) {
|
|
1134
|
+
const timing = computeTaskTiming(task);
|
|
1135
|
+
bump(main, task, timing);
|
|
1136
|
+
if (!byTypeAcc[task.type]) byTypeAcc[task.type] = newBucket();
|
|
1137
|
+
bump(byTypeAcc[task.type], task, timing);
|
|
1138
|
+
const rt = task.runnerType || 'system';
|
|
1139
|
+
if (!byRunnerAcc[rt]) byRunnerAcc[rt] = newBucket();
|
|
1140
|
+
bump(byRunnerAcc[rt], task, timing);
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
const finalizeBuckets = acc =>
|
|
1144
|
+
Object.entries(acc).reduce((out, [key, b]) => {
|
|
1145
|
+
out[key] = {
|
|
1146
|
+
count: b.count,
|
|
1147
|
+
avgWaitingTime: b.timedTaskCount > 0 ? Math.round(b.totalWaitingTime / b.timedTaskCount) : 0,
|
|
1148
|
+
avgExecutionTime: b.timedTaskCount > 0 ? Math.round(b.totalExecutionTime / b.timedTaskCount) : 0,
|
|
1149
|
+
avgTotalTime: b.timedTaskCount > 0 ? Math.round(b.totalTime / b.timedTaskCount) : 0
|
|
1150
|
+
};
|
|
1151
|
+
return out;
|
|
1152
|
+
}, {});
|
|
1153
|
+
|
|
1154
|
+
return {
|
|
1155
|
+
completedCount: main.count,
|
|
1156
|
+
successCount: main.successCount,
|
|
1157
|
+
failedCount: main.failedCount,
|
|
1158
|
+
canceledCount: main.canceledCount,
|
|
1159
|
+
avgWaitingTime: main.timedTaskCount > 0 ? Math.round(main.totalWaitingTime / main.timedTaskCount) : 0,
|
|
1160
|
+
avgExecutionTime: main.timedTaskCount > 0 ? Math.round(main.totalExecutionTime / main.timedTaskCount) : 0,
|
|
1161
|
+
avgTotalTime: main.timedTaskCount > 0 ? Math.round(main.totalTime / main.timedTaskCount) : 0,
|
|
1162
|
+
byType: finalizeBuckets(byTypeAcc),
|
|
1163
|
+
byRunnerType: finalizeBuckets(byRunnerAcc)
|
|
1164
|
+
};
|
|
1056
1165
|
};
|
|
1166
|
+
|
|
1167
|
+
const fromTasks = await aggregateDurationFromTodayTasks();
|
|
1168
|
+
|
|
1169
|
+
const statHasAggregate = ts =>
|
|
1170
|
+
ts &&
|
|
1171
|
+
(Number(ts.totalCompleted) > 0 ||
|
|
1172
|
+
Number(ts.timedTaskCount) > 0 ||
|
|
1173
|
+
(ts.byType && Object.keys(ts.byType).length > 0) ||
|
|
1174
|
+
(ts.byRunnerType && Object.keys(ts.byRunnerType).length > 0));
|
|
1175
|
+
|
|
1176
|
+
if (todayStat && statHasAggregate(todayStat)) {
|
|
1177
|
+
return mapStatToDuration(todayStat);
|
|
1178
|
+
}
|
|
1179
|
+
if (fromTasks.completedCount > 0) {
|
|
1180
|
+
return fromTasks;
|
|
1181
|
+
}
|
|
1182
|
+
if (todayStat) {
|
|
1183
|
+
return mapStatToDuration(todayStat);
|
|
1184
|
+
}
|
|
1185
|
+
return emptyDuration();
|
|
1057
1186
|
})()
|
|
1058
1187
|
};
|
|
1059
1188
|
}
|