This commit is contained in:
qaq
2025-08-15 12:57:56 +08:00
parent 6f80e26a8f
commit 23980578d7
5 changed files with 42 additions and 51 deletions
-3
View File
@@ -836,9 +836,6 @@ UPDATE \`statistics_flow\`
SET \`created_time\` = UNIX_TIMESTAMP() * 1000 SET \`created_time\` = UNIX_TIMESTAMP() * 1000
WHERE \`created_time\` = 0 OR \`created_time\` IS NULL; WHERE \`created_time\` = 0 OR \`created_time\` IS NULL;
EOF EOF
# 检查数据库容器 # 检查数据库容器
@@ -37,11 +37,11 @@ public class StatisticsFlowAsync {
long time = new Date().getTime(); long time = new Date().getTime();
// 删除48小时前的数据 // 删除48小时前的数据
long nowMs = System.currentTimeMillis(); long nowMs = new Date().getTime();
long cutoffMs = nowMs - 48L * 60 * 60 * 1000; long cutoffMs = nowMs - 48L * 60 * 60 * 1000;
statisticsFlowService.remove( statisticsFlowService.remove(
new LambdaQueryWrapper<StatisticsFlow>() new LambdaQueryWrapper<StatisticsFlow>()
.lt(StatisticsFlow::getCreateTime, cutoffMs) .lt(StatisticsFlow::getCreatedTime, cutoffMs)
); );
@@ -79,7 +79,7 @@ public class StatisticsFlowAsync {
statisticsFlow.setFlow(incrementFlow); statisticsFlow.setFlow(incrementFlow);
statisticsFlow.setTotalFlow(currentTotalFlow); statisticsFlow.setTotalFlow(currentTotalFlow);
statisticsFlow.setTime(hourString); statisticsFlow.setTime(hourString);
statisticsFlow.setCreateTime(time); statisticsFlow.setCreatedTime(time);
statisticsFlowList.add(statisticsFlow); statisticsFlowList.add(statisticsFlow);
} }
@@ -28,7 +28,7 @@ public class StatisticsFlow {
private String time; private String time;
private Long createTime; private Long createdTime;
} }
@@ -727,50 +727,44 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
* @return 最近24小时流量统计列表 * @return 最近24小时流量统计列表
*/ */
private List<StatisticsFlow> getLast24HoursFlowStatistics(Long userId) { private List<StatisticsFlow> getLast24HoursFlowStatistics(Long userId) {
try { // 按ID倒序查最近24条记录(ID越大越新,时间就是23:00, 22:00, 21:00...这样倒序)
// 按ID倒序查最近24条记录(ID越大越新,时间就是23:00, 22:00, 21:00...这样倒序) List<StatisticsFlow> recentFlows = statisticsFlowService.list(
List<StatisticsFlow> recentFlows = statisticsFlowService.list( new QueryWrapper<StatisticsFlow>()
new QueryWrapper<StatisticsFlow>() .eq("user_id", userId)
.eq("user_id", userId) .orderByDesc("id")
.orderByDesc("id") .last("LIMIT 24")
.last("LIMIT 24") );
);
List<StatisticsFlow> result = new ArrayList<>(recentFlows); List<StatisticsFlow> result = new ArrayList<>(recentFlows);
// 如果查出来的记录不足24条,需要补0和对应的时间 // 如果查出来的记录不足24条,需要补0和对应的时间
if (result.size() < 24) { if (result.size() < 24) {
// 获取最早记录的时间,继续往前推 // 获取最早记录的时间,继续往前推
int startHour = getCurrentHour(); int startHour = getCurrentHour();
if (!result.isEmpty()) { if (!result.isEmpty()) {
// 从最后一条记录的时间继续往前推 // 从最后一条记录的时间继续往前推
String lastTime = result.get(result.size() - 1).getTime(); String lastTime = result.get(result.size() - 1).getTime();
startHour = parseHour(lastTime) - 1; startHour = parseHour(lastTime) - 1;
}
// 补0到24条
while (result.size() < 24) {
if (startHour < 0) startHour = 23; // 跨天处理
StatisticsFlow emptyFlow = new StatisticsFlow();
emptyFlow.setUserId(userId);
emptyFlow.setFlow(0L);
emptyFlow.setTotalFlow(0L);
emptyFlow.setTime(String.format("%02d:00", startHour));
result.add(emptyFlow);
startHour--;
}
} }
log.debug("用户 {} 获取到 {} 条实际记录,补齐为 {} 条24小时记录", userId, recentFlows.size(), result.size()); // 补0到24条
return result; while (result.size() < 24) {
if (startHour < 0) startHour = 23; // 跨天处理
} catch (Exception e) { StatisticsFlow emptyFlow = new StatisticsFlow();
log.error("获取用户 {} 最近24小时流量统计失败", userId, e); emptyFlow.setUserId(userId);
// 返回24条全0数据,时间从当前小时往前推 emptyFlow.setFlow(0L);
return generateEmpty24HourData(userId); emptyFlow.setTotalFlow(0L);
emptyFlow.setTime(String.format("%02d:00", startHour));
result.add(emptyFlow);
startHour--;
}
} }
log.debug("用户 {} 获取到 {} 条实际记录,补齐为 {} 条24小时记录", userId, recentFlows.size(), result.size());
return result;
} }
/** /**
+2 -2
View File
@@ -215,7 +215,7 @@ export default function IndexPage() {
const checkResponse = await checkCaptcha(); const checkResponse = await checkCaptcha();
if (checkResponse.code !== 0) { if (checkResponse.code !== 0) {
toast.error("检查验证码状态失败,请重试"); toast.error("检查验证码状态失败,请重试" + checkResponse.msg);
setLoading(false); setLoading(false);
return; return;
} }
@@ -234,7 +234,7 @@ export default function IndexPage() {
} }
} catch (error) { } catch (error) {
console.error('检查验证码状态错误:', error); console.error('检查验证码状态错误:', error);
toast.error("网络错误,请稍后重试"); toast.error("网络错误,请稍后重试" + error);
setLoading(false); setLoading(false);
} }
}; };