Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修正 size 为文件大小,添加已下载大小 #1

Merged
merged 4 commits into from
Nov 17, 2024
Merged

Conversation

paulzzh
Copy link
Contributor

@paulzzh paulzzh commented Nov 16, 2024

作为 PBH-BTN/PeerBanHelper#748 的一部分
关联:PBH-BTN/PeerBanHelper#700
修复:PBH-BTN/PeerBanHelper#746

Summary by CodeRabbit

  • 新功能

    • 引入 completed_size 属性,以更好地跟踪下载进度。
    • 更新了处理 torrent 信息的逻辑,确保更准确地表示 torrent 大小。
  • 错误修复

    • 增强了错误处理,确保在解码客户端名称时不会影响数据处理流程。

@paulzzh paulzzh requested a review from azicen November 16, 2024 19:34
Copy link

coderabbitai bot commented Nov 16, 2024

Walkthrough

该拉取请求对 deluge_peerbanhelperadapter/core.pydeluge_peerbanhelperadapter/model/torrent.py 文件进行了修改。主要更改包括在 Core 类中重命名 size 属性为 completed_size,并更新了与种子信息和封锁列表管理相关的方法。此外,增强了错误处理,特别是在获取活动种子信息时。Torrent 类中新增了 completed_size 属性,用于表示已下载数据的大小。

Changes

文件路径 更改摘要
deluge_peerbanhelperadapter/core.py 更新 Core 类中的 get_active_torrents_infoget_torrents_info 方法,重命名 sizecompleted_size,增强错误处理以捕获 UnicodeDecodeError
deluge_peerbanhelperadapter/model/torrent.py Torrent 类中新增 completed_size 属性,默认值为 0,用于表示已下载的数据大小。

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Core
    participant Torrent

    User->>Core: 请求活动种子信息
    Core->>Torrent: 获取种子信息
    Torrent-->>Core: 返回种子信息(包括completed_size)
    Core-->>User: 返回活动种子信息
Loading

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 79722d8 and ee4e253.

📒 Files selected for processing (2)
  • deluge_peerbanhelperadapter/core.py (2 hunks)
  • deluge_peerbanhelperadapter/model/torrent.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • deluge_peerbanhelperadapter/core.py
  • deluge_peerbanhelperadapter/model/torrent.py

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
deluge_peerbanhelperadapter/model/torrent.py (1)

83-85: 建议添加字段说明注释

completed_size 字段的实现正确,但建议补充更详细的注释说明,包括:

  • 字段的具体含义和单位(例如:字节)
  • size 字段的关系(已下载大小不应超过总大小)

建议修改注释如下:

-    # 已下载的大小
+    # 已下载的大小(字节)
+    # 注意:completed_size 始终小于等于 size
     completed_size: int = 0
deluge_peerbanhelperadapter/core.py (1)

136-142: 建议添加属性文档说明

建议为 completed_sizesize 属性添加文档字符串,说明它们的具体含义和单位,这样可以帮助其他开发者更好地理解和使用这些属性。

示例文档:

 def get_active_torrents_info(self):
-    """返回活跃种子的列表"""
+    """返回活跃种子的列表
+    
+    Returns:
+        list: 包含以下属性的种子信息列表:
+            - completed_size: 已下载的数据大小(字节)
+            - size: 种子总大小(字节)
+            ...
+    """

Also applies to: 212-216

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 28a6d01 and 0953f04.

📒 Files selected for processing (2)
  • deluge_peerbanhelperadapter/core.py (2 hunks)
  • deluge_peerbanhelperadapter/model/torrent.py (1 hunks)
🔇 Additional comments (4)
deluge_peerbanhelperadapter/model/torrent.py (1)

Line range hint 77-84: 验证 size 和 completed_size 的一致性

需要确保在运行时 completed_size 不会超过 size

✅ Verification successful

sizecompleted_size 的赋值逻辑是安全的

根据代码分析,completed_sizesize 的赋值来源是安全的:

  • size 通过 torrent_info.total_size() 获取,这是种子的总大小
  • completed_size 通过 deluge_torrent.status.total_wanted_done 获取,这是已下载的大小

由于这些值都是从 Deluge 客户端直接获取的,Deluge 本身会确保下载进度的正确性,因此不需要额外的验证逻辑。这是一个标准的 Deluge API 使用方式,可以保证 completed_size 不会超过 size

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 描述:检查是否有相关的验证逻辑

# 搜索与 size 和 completed_size 相关的验证代码
rg -A 5 "def.*size|completed_size.*size|size.*completed_size"

Length of output: 63


Script:

#!/bin/bash
# 搜索与 size 和 completed_size 相关的代码,使用更宽松的模式
rg -A 5 "size.*=|completed_size.*=|progress.*="

# 搜索可能的验证逻辑
ast-grep --pattern 'if $_ > $_: $$$'

Length of output: 6236


Script:

#!/bin/bash
# 搜索相关的方法和属性定义
ast-grep --pattern 'def set_$_($$$): $$$'

# 搜索可能的赋值操作
ast-grep --pattern '$_.size = $_'
ast-grep --pattern '$_.completed_size = $_'

Length of output: 545

deluge_peerbanhelperadapter/core.py (3)

136-142: 修改正确地区分了总大小和已完成大小!

代码变更符合以下要求:

  • 使用 total_wanted_done 正确表示已下载的数据大小
  • 使用 total_size() 准确反映种子的总大小

212-216: 实现与 get_active_torrents_info 保持一致

代码变更保持了与活动种子信息获取方法相同的实现逻辑,确保了整个代码库的一致性。


Line range hint 151-154: 优秀的错误处理实现!

通过添加 UnicodeDecodeError 异常处理,防止了客户端名称解码失败导致的程序崩溃,并提供了合理的默认值 "unknown"。这种防御性编程的做法值得肯定。

@azicen
Copy link
Collaborator

azicen commented Nov 17, 2024

total_donetotal_wanted_done 都不是实际做种大小,而是已选中文件大小,区别是前者计算所有已选择文件大小,后者只计算选择文件已完成的大小。

考虑用其他方法?

@azicen azicen merged commit fef216f into main Nov 17, 2024
1 check passed
@paulzzh paulzzh deleted the fixTorrentSize branch November 17, 2024 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants