Skip to content

fix: 修复 duration 显示错误并重构为公共函数#59

Merged
awsl233777 merged 3 commits intomainfrom
fix/duration-display-error
Jan 16, 2026
Merged

fix: 修复 duration 显示错误并重构为公共函数#59
awsl233777 merged 3 commits intomainfrom
fix/duration-display-error

Conversation

@solarhell
Copy link
Contributor

@solarhell solarhell commented Jan 16, 2026

Summary

修复了请求详情页面中 duration 显示为数万分钟的错误问题,并将重复的 formatDuration 函数提取为公共工具函数。

问题修复

问题原因

  • 后端 Go 使用 time.Duration 类型,以纳秒为单位存储
  • 前端 formatDuration 函数错误地将输入值当作毫秒处理
  • 导致显示值放大了 100 万倍(例如:8 秒显示成 90542 分钟)

修复方案

  • 添加纳秒到毫秒的转换逻辑:const ms = ns / 1_000_000
  • 将函数参数从 ms 重命名为 ns 以明确单位
  • 添加注释说明转换逻辑

代码重构

重构内容

  • formatDuration 函数提取到 lib/utils.ts 作为公共工具函数
  • 移除了 RequestHeader.tsxRequestSidebar.tsx 中的重复代码
  • 优化 index.tsx 中的实现,保留特殊的 pending 状态处理逻辑
  • 添加了完整的 JSDoc 注释

改进效果

  • ✅ 减少了 26 行重复代码
  • ✅ 提高了可维护性(只需在一处修改)
  • ✅ 增强了代码可读性和文档化

修改文件

  • web/src/lib/utils.ts - 添加公共 formatDuration 函数
  • web/src/pages/requests/detail/RequestHeader.tsx - 使用公共函数
  • web/src/pages/requests/detail/RequestSidebar.tsx - 使用公共函数
  • web/src/pages/requests/index.tsx - 优化并添加注释

Test plan

  • 检查请求详情页面的 duration 显示是否正确(8 秒左右)
  • 验证不同时长的格式化显示(毫秒、秒、分钟)
  • 确保列表页和详情页的 duration 显示一致

🤖 Generated with Claude Code

修复了请求详情页面中 duration 显示为数万分钟的问题。
后端 time.Duration 以纳秒为单位存储,但前端 formatDuration 函数错误地将其当作毫秒处理,导致显示值放大了 100 万倍。

更改:
- 在 RequestHeader.tsx 和 RequestSidebar.tsx 中添加纳秒到毫秒的转换
- 将 formatDuration 参数从 ms 重命名为 ns 以明确单位

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@github-actions
Copy link
Contributor

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Code Clarity

The conversion logic from nanoseconds to milliseconds is clear, but consider adding more detailed comments on the expected input range for ns to avoid confusion in future usage.

function formatDuration(ns: number): string {
  // Convert nanoseconds to milliseconds
  const ms = ns / 1_000_000
  if (ms < 1000) return `${ms.toFixed(0)}ms`
Code Clarity

Similar to the previous file, it would be beneficial to include comments explaining the conversion logic and the expected input for ns.

function formatDuration(ns: number): string {
  // Convert nanoseconds to milliseconds
  const ms = ns / 1_000_000
  if (ms < 1000) return `${ms.toFixed(0)}ms`

@github-actions
Copy link
Contributor

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Handle negative duration values

The condition for displaying milliseconds should also handle cases where ms is
negative. Consider adding a check to return an error message or a default value for
negative inputs to prevent misleading outputs.

web/src/pages/requests/detail/RequestHeader.tsx [28]

++  if (ms < 0) return 'Invalid duration'
 +  if (ms < 1000) return `${ms.toFixed(0)}ms`
Suggestion importance[1-10]: 5

__

Why: The suggestion addresses a potential issue with negative values for ms, which could lead to misleading outputs. However, the impact is moderate as it does not address a critical bug but rather improves input validation.

Low

将重复的 formatDuration 函数提取到 lib/utils.ts 中,减少代码重复。

更改:
- 在 lib/utils.ts 中添加 formatDuration 公共函数
- 更新 RequestHeader.tsx 和 RequestSidebar.tsx 使用公共函数
- 优化 index.tsx 中的 formatDuration,添加注释说明

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@solarhell solarhell changed the title fix: 修复 duration 显示错误问题 fix: 修复 duration 显示错误并重构为公共函数 Jan 16, 2026
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@awsl233777 awsl233777 merged commit c62556b into main Jan 16, 2026
1 check passed
@awsl233777 awsl233777 deleted the fix/duration-display-error branch January 16, 2026 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants