@kne/fastify-task 2.0.0 → 2.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.
Files changed (2) hide show
  1. package/libs/services/main.js +212 -46
  2. package/package.json +1 -1
@@ -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();
@@ -903,7 +925,7 @@ module.exports = fp(async (fastify, options) => {
903
925
  return Sequelize.literal(`CONCAT(DATE_FORMAT(CONVERT_TZ("${createdAtCol}", '+00:00', '${effectiveTimezone}'), '%H:'), LPAD(FLOOR(EXTRACT(MINUTE FROM CONVERT_TZ("${createdAtCol}", '+00:00', '${effectiveTimezone}')) / 15) * 15, 2, '0'))`);
904
926
  })();
905
927
 
906
- const [totalTasks, byStatus, byType, byRunnerType, hourlyTrend, hourlyTrendByStatus, hourlyTrendByType, intervalTrend] = await Promise.all([
928
+ const [totalTasks, byStatus, byType, byRunnerType, pendingByRunnerType, nonPendingByRunnerType, hourlyTrend, hourlyTrendByStatus, hourlyTrendByType, intervalTrend] = await Promise.all([
907
929
  taskModel.count({ where: whereToday }),
908
930
  taskModel.findAll({
909
931
  attributes: ['status', [Sequelize.fn('COUNT', Sequelize.col('id')), 'count']],
@@ -923,6 +945,18 @@ module.exports = fp(async (fastify, options) => {
923
945
  group: ['runnerType'],
924
946
  raw: true
925
947
  }),
948
+ taskModel.findAll({
949
+ attributes: ['runnerType', [Sequelize.fn('COUNT', Sequelize.col('id')), 'count']],
950
+ where: { ...whereToday, status: 'pending' },
951
+ group: ['runnerType'],
952
+ raw: true
953
+ }),
954
+ taskModel.findAll({
955
+ attributes: ['runnerType', [Sequelize.fn('COUNT', Sequelize.col('id')), 'count']],
956
+ where: { ...whereToday, status: { [Sequelize.Op.ne]: 'pending' } },
957
+ group: ['runnerType'],
958
+ raw: true
959
+ }),
926
960
  taskModel.findAll({
927
961
  attributes: [
928
962
  [hourExtract, 'hour'],
@@ -981,6 +1015,32 @@ module.exports = fp(async (fastify, options) => {
981
1015
  return utcTime.tz(effectiveTimezone).format('HH:mm');
982
1016
  };
983
1017
 
1018
+ const byRunnerTypeMap = byRunnerType.reduce((acc, item) => {
1019
+ acc[item.runnerType] = Number(item.count);
1020
+ return acc;
1021
+ }, {});
1022
+ const pendingByRunnerTypeMap = pendingByRunnerType.reduce((acc, item) => {
1023
+ acc[item.runnerType] = Number(item.count);
1024
+ return acc;
1025
+ }, {});
1026
+ const nonPendingByRunnerTypeMap = nonPendingByRunnerType.reduce((acc, item) => {
1027
+ acc[item.runnerType] = Number(item.count);
1028
+ return acc;
1029
+ }, {});
1030
+ const runnerTypeStatsKeys = new Set([
1031
+ ...Object.keys(byRunnerTypeMap),
1032
+ ...Object.keys(pendingByRunnerTypeMap),
1033
+ ...Object.keys(nonPendingByRunnerTypeMap)
1034
+ ]);
1035
+ const runnerTypeStats = {};
1036
+ for (const k of runnerTypeStatsKeys) {
1037
+ runnerTypeStats[k] = {
1038
+ total: Number(byRunnerTypeMap[k]) || 0,
1039
+ pending: Number(pendingByRunnerTypeMap[k]) || 0,
1040
+ executed: Number(nonPendingByRunnerTypeMap[k]) || 0
1041
+ };
1042
+ }
1043
+
984
1044
  return {
985
1045
  date: formatDate(todayStart, effectiveTimezone),
986
1046
  totalTasks,
@@ -992,10 +1052,9 @@ module.exports = fp(async (fastify, options) => {
992
1052
  acc[item.type] = Number(item.count);
993
1053
  return acc;
994
1054
  }, {}),
995
- byRunnerType: byRunnerType.reduce((acc, item) => {
996
- acc[item.runnerType] = Number(item.count);
997
- return acc;
998
- }, {}),
1055
+ byRunnerType: byRunnerTypeMap,
1056
+ pendingByRunnerType: pendingByRunnerTypeMap,
1057
+ runnerTypeStats,
999
1058
  hourlyTrend: hourlyTrend.map(item => ({ hour: adjustHour(item.hour), count: Number(item.count) })),
1000
1059
  hourlyTrendByStatus: hourlyTrendByStatus.map(item => ({
1001
1060
  hour: adjustHour(item.hour),
@@ -1009,51 +1068,158 @@ module.exports = fp(async (fastify, options) => {
1009
1068
  })),
1010
1069
  intervalTrend: intervalTrend.map(item => ({ interval: adjustInterval(item.interval), count: Number(item.count) })),
1011
1070
  todayDuration: await (async () => {
1071
+ const statDateKey = formatDate(todayStart, effectiveTimezone);
1012
1072
  const todayStat = await models.taskDailyStatistics.findOne({
1013
- where: { date: dayjs(todayStart).format('YYYY-MM-DD') },
1073
+ where: { date: statDateKey },
1014
1074
  raw: true
1015
1075
  });
1016
- if (!todayStat) {
1017
- return { completedCount: 0, avgWaitingTime: 0, avgExecutionTime: 0, avgTotalTime: 0, byType: {}, byRunnerType: {} };
1018
- }
1019
- // 如果指定了type/runnerType筛选,从对应子维度中提取数据作为主数据
1020
- const typeData = type && todayStat.byType && todayStat.byType[type] ? todayStat.byType[type] : null;
1021
- const runnerTypeData = runnerType && todayStat.byRunnerType && todayStat.byRunnerType[runnerType] ? todayStat.byRunnerType[runnerType] : null;
1022
- const filterData = runnerTypeData || typeData;
1023
- const mainTimedTaskCount = filterData ? filterData.timedTaskCount : todayStat.timedTaskCount;
1024
- const mainTotalWaitingTime = filterData ? filterData.totalWaitingTime : todayStat.totalWaitingTime;
1025
- const mainTotalExecutionTime = filterData ? filterData.totalExecutionTime : todayStat.totalExecutionTime;
1026
- const mainTotalTime = filterData ? filterData.totalTime : todayStat.totalTime;
1027
-
1028
- return {
1029
- completedCount: filterData ? filterData.count : todayStat.totalCompleted,
1030
- successCount: filterData ? filterData.successCount : todayStat.successCount,
1031
- failedCount: filterData ? filterData.failedCount : todayStat.failedCount,
1032
- canceledCount: filterData ? filterData.canceledCount : todayStat.canceledCount,
1033
- avgWaitingTime: mainTimedTaskCount > 0 ? Math.round(mainTotalWaitingTime / mainTimedTaskCount) : 0,
1034
- avgExecutionTime: mainTimedTaskCount > 0 ? Math.round(mainTotalExecutionTime / mainTimedTaskCount) : 0,
1035
- avgTotalTime: mainTimedTaskCount > 0 ? Math.round(mainTotalTime / mainTimedTaskCount) : 0,
1036
- byType: Object.entries(todayStat.byType || {}).reduce((acc, [key, val]) => {
1037
- if (type && key !== type) return acc;
1038
- acc[key] = {
1039
- count: val.count,
1040
- avgWaitingTime: val.timedTaskCount > 0 ? Math.round(val.totalWaitingTime / val.timedTaskCount) : 0,
1041
- avgExecutionTime: val.timedTaskCount > 0 ? Math.round(val.totalExecutionTime / val.timedTaskCount) : 0,
1042
- avgTotalTime: val.timedTaskCount > 0 ? Math.round(val.totalTime / val.timedTaskCount) : 0
1043
- };
1044
- return acc;
1045
- }, {}),
1046
- byRunnerType: Object.entries(todayStat.byRunnerType || {}).reduce((acc, [key, val]) => {
1047
- if (runnerType && key !== runnerType) return acc;
1048
- acc[key] = {
1049
- count: val.count,
1050
- avgWaitingTime: val.timedTaskCount > 0 ? Math.round(val.totalWaitingTime / val.timedTaskCount) : 0,
1051
- avgExecutionTime: val.timedTaskCount > 0 ? Math.round(val.totalExecutionTime / val.timedTaskCount) : 0,
1052
- avgTotalTime: val.timedTaskCount > 0 ? Math.round(val.totalTime / val.timedTaskCount) : 0
1053
- };
1054
- return acc;
1055
- }, {})
1076
+
1077
+ const emptyDuration = () => ({
1078
+ completedCount: 0,
1079
+ successCount: 0,
1080
+ failedCount: 0,
1081
+ canceledCount: 0,
1082
+ avgWaitingTime: 0,
1083
+ avgExecutionTime: 0,
1084
+ avgTotalTime: 0,
1085
+ byType: {},
1086
+ byRunnerType: {}
1087
+ });
1088
+
1089
+ const mapStatToDuration = baseStat => {
1090
+ if (!baseStat) return emptyDuration();
1091
+ const typeData = type && baseStat.byType && baseStat.byType[type] ? baseStat.byType[type] : null;
1092
+ const runnerTypeData = runnerType && baseStat.byRunnerType && baseStat.byRunnerType[runnerType] ? baseStat.byRunnerType[runnerType] : null;
1093
+ const filterData = runnerTypeData || typeData;
1094
+ const mainTimedTaskCount = filterData ? filterData.timedTaskCount : baseStat.timedTaskCount;
1095
+ const mainTotalWaitingTime = filterData ? filterData.totalWaitingTime : baseStat.totalWaitingTime;
1096
+ const mainTotalExecutionTime = filterData ? filterData.totalExecutionTime : baseStat.totalExecutionTime;
1097
+ const mainTotalTime = filterData ? filterData.totalTime : baseStat.totalTime;
1098
+
1099
+ return {
1100
+ completedCount: filterData ? filterData.count : baseStat.totalCompleted,
1101
+ successCount: filterData ? filterData.successCount : baseStat.successCount,
1102
+ failedCount: filterData ? filterData.failedCount : baseStat.failedCount,
1103
+ canceledCount: filterData ? filterData.canceledCount : baseStat.canceledCount,
1104
+ avgWaitingTime: mainTimedTaskCount > 0 ? Math.round(mainTotalWaitingTime / mainTimedTaskCount) : 0,
1105
+ avgExecutionTime: mainTimedTaskCount > 0 ? Math.round(mainTotalExecutionTime / mainTimedTaskCount) : 0,
1106
+ avgTotalTime: mainTimedTaskCount > 0 ? Math.round(mainTotalTime / mainTimedTaskCount) : 0,
1107
+ byType: Object.entries(baseStat.byType || {}).reduce((acc, [key, val]) => {
1108
+ if (type && key !== type) return acc;
1109
+ acc[key] = {
1110
+ count: val.count,
1111
+ avgWaitingTime: val.timedTaskCount > 0 ? Math.round(val.totalWaitingTime / val.timedTaskCount) : 0,
1112
+ avgExecutionTime: val.timedTaskCount > 0 ? Math.round(val.totalExecutionTime / val.timedTaskCount) : 0,
1113
+ avgTotalTime: val.timedTaskCount > 0 ? Math.round(val.totalTime / val.timedTaskCount) : 0
1114
+ };
1115
+ return acc;
1116
+ }, {}),
1117
+ byRunnerType: Object.entries(baseStat.byRunnerType || {}).reduce((acc, [key, val]) => {
1118
+ if (runnerType && key !== runnerType) return acc;
1119
+ acc[key] = {
1120
+ count: val.count,
1121
+ avgWaitingTime: val.timedTaskCount > 0 ? Math.round(val.totalWaitingTime / val.timedTaskCount) : 0,
1122
+ avgExecutionTime: val.timedTaskCount > 0 ? Math.round(val.totalExecutionTime / val.timedTaskCount) : 0,
1123
+ avgTotalTime: val.timedTaskCount > 0 ? Math.round(val.totalTime / val.timedTaskCount) : 0
1124
+ };
1125
+ return acc;
1126
+ }, {})
1127
+ };
1128
+ };
1129
+
1130
+ const aggregateDurationFromTodayTasks = async () => {
1131
+ const terminalStatuses = ['success', 'failed', 'canceled'];
1132
+ const tasks = await taskModel.findAll({
1133
+ where: {
1134
+ ...whereToday,
1135
+ status: { [Op.in]: terminalStatuses },
1136
+ completedAt: { [Op.ne]: null }
1137
+ },
1138
+ attributes: ['type', 'runnerType', 'status', 'createdAt', 'startedAt', 'completedAt'],
1139
+ raw: true
1140
+ });
1141
+
1142
+ const newBucket = () => ({
1143
+ count: 0,
1144
+ successCount: 0,
1145
+ failedCount: 0,
1146
+ canceledCount: 0,
1147
+ totalWaitingTime: 0,
1148
+ totalExecutionTime: 0,
1149
+ totalTime: 0,
1150
+ timedTaskCount: 0
1151
+ });
1152
+
1153
+ const bump = (bucket, task, timing) => {
1154
+ bucket.count += 1;
1155
+ if (task.status === 'success') bucket.successCount += 1;
1156
+ else if (task.status === 'failed') bucket.failedCount += 1;
1157
+ else if (task.status === 'canceled') bucket.canceledCount += 1;
1158
+ if (timing.hasTiming) {
1159
+ bucket.totalWaitingTime += timing.waitingTime;
1160
+ bucket.totalExecutionTime += timing.executionTime;
1161
+ bucket.totalTime += timing.totalTime;
1162
+ bucket.timedTaskCount += 1;
1163
+ }
1164
+ };
1165
+
1166
+ const byTypeAcc = {};
1167
+ const byRunnerAcc = {};
1168
+ const main = newBucket();
1169
+
1170
+ for (const task of tasks) {
1171
+ const timing = computeTaskTiming(task);
1172
+ bump(main, task, timing);
1173
+ if (!byTypeAcc[task.type]) byTypeAcc[task.type] = newBucket();
1174
+ bump(byTypeAcc[task.type], task, timing);
1175
+ const rt = task.runnerType || 'system';
1176
+ if (!byRunnerAcc[rt]) byRunnerAcc[rt] = newBucket();
1177
+ bump(byRunnerAcc[rt], task, timing);
1178
+ }
1179
+
1180
+ const finalizeBuckets = acc =>
1181
+ Object.entries(acc).reduce((out, [key, b]) => {
1182
+ out[key] = {
1183
+ count: b.count,
1184
+ avgWaitingTime: b.timedTaskCount > 0 ? Math.round(b.totalWaitingTime / b.timedTaskCount) : 0,
1185
+ avgExecutionTime: b.timedTaskCount > 0 ? Math.round(b.totalExecutionTime / b.timedTaskCount) : 0,
1186
+ avgTotalTime: b.timedTaskCount > 0 ? Math.round(b.totalTime / b.timedTaskCount) : 0
1187
+ };
1188
+ return out;
1189
+ }, {});
1190
+
1191
+ return {
1192
+ completedCount: main.count,
1193
+ successCount: main.successCount,
1194
+ failedCount: main.failedCount,
1195
+ canceledCount: main.canceledCount,
1196
+ avgWaitingTime: main.timedTaskCount > 0 ? Math.round(main.totalWaitingTime / main.timedTaskCount) : 0,
1197
+ avgExecutionTime: main.timedTaskCount > 0 ? Math.round(main.totalExecutionTime / main.timedTaskCount) : 0,
1198
+ avgTotalTime: main.timedTaskCount > 0 ? Math.round(main.totalTime / main.timedTaskCount) : 0,
1199
+ byType: finalizeBuckets(byTypeAcc),
1200
+ byRunnerType: finalizeBuckets(byRunnerAcc)
1201
+ };
1056
1202
  };
1203
+
1204
+ const fromTasks = await aggregateDurationFromTodayTasks();
1205
+
1206
+ const statHasAggregate = ts =>
1207
+ ts &&
1208
+ (Number(ts.totalCompleted) > 0 ||
1209
+ Number(ts.timedTaskCount) > 0 ||
1210
+ (ts.byType && Object.keys(ts.byType).length > 0) ||
1211
+ (ts.byRunnerType && Object.keys(ts.byRunnerType).length > 0));
1212
+
1213
+ if (todayStat && statHasAggregate(todayStat)) {
1214
+ return mapStatToDuration(todayStat);
1215
+ }
1216
+ if (fromTasks.completedCount > 0) {
1217
+ return fromTasks;
1218
+ }
1219
+ if (todayStat) {
1220
+ return mapStatToDuration(todayStat);
1221
+ }
1222
+ return emptyDuration();
1057
1223
  })()
1058
1224
  };
1059
1225
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-task",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "一个 Fastify 插件,用于任务编排、追踪任务执行状态和结果。支持系统自动执行和手动执行两种模式,提供完整的任务生命周期管理",
5
5
  "main": "index.js",
6
6
  "scripts": {