Skip to content

✨ 首次启动时提供使用web ui方式完全配置#1870

Merged
HibiKier merged 35 commits into
mainfrom
feature/webui-config
Jun 16, 2025
Merged

✨ 首次启动时提供使用web ui方式完全配置#1870
HibiKier merged 35 commits into
mainfrom
feature/webui-config

Conversation

@HibiKier

Copy link
Copy Markdown
Collaborator

No description provided.

@sourcery-ai

sourcery-ai Bot commented Mar 10, 2025

Copy link
Copy Markdown
Contributor

Sourcery 审查者指南

此 Pull Request 引入了一个 Web UI 配置,用于在数据库 URL 未配置时的初始设置。它还实现了一个新的 HookPriorityManager,用于管理基于优先级的启动和关闭钩子的执行顺序。此外,它还包括一些小的更改,例如修复了一个拼写错误和修改了 MessageManager.get

启动时 Hook 优先级管理的时序图

sequenceDiagram
    participant Driver
    participant HookPriorityManager
    Driver->>HookPriorityManager: on_startup()
    activate HookPriorityManager
    HookPriorityManager->>HookPriorityManager: Get priority data for STARTUP
    HookPriorityManager->>HookPriorityManager: Sort priorities
    loop For each priority
        loop For each function in priority
            HookPriorityManager->>func: Call function
            activate func
            func-->>HookPriorityManager: Return
            deactivate func
        end
    end
    deactivate HookPriorityManager
Loading

HookPriorityManager 的更新类图

classDiagram
    class HookPriorityManager {
        _data: dict[HookPriorityType, dict[int, list[Callable]]]
        +add(hook_type: HookPriorityType, func: Callable, priority: int = 5)
        +on_startup(priority: int = 5)
        +on_shutdown(priority: int = 5)
    }
    class HookPriorityType {
        STARTUP = \"STARTUP\"
        SHUTDOWN = \"SHUTDOWN\"
    }
    HookPriorityManager -- HookPriorityType : Uses
Loading

文件级别变更

变更 详情 文件
引入一个新的 HookPriorityManager 来管理基于优先级的启动和关闭钩子的执行顺序。
  • 添加了 HookPriorityManager 类来管理钩子执行顺序。
  • 实现了 on_startupon_shutdown 装饰器来注册具有特定优先级的函数。
  • 修改了现有的启动函数以使用 HookPriorityManager
  • 添加了 HookPriorityException 以允许中断启动过程。
  • 添加了 HookPriorityType 枚举来定义钩子类型。
  • 启动过程现在将根据优先级迭代注册的函数并执行它们。
  • 如果在启动期间引发 HookPriorityException,则该过程将被中断,并记录一条错误消息。
zhenxun/services/db_context.py
zhenxun/utils/exception.py
zhenxun/builtin_plugins/scripts.py
zhenxun/builtin_plugins/admin/welcome_message/data_source.py
zhenxun/builtin_plugins/init/init_config.py
zhenxun/builtin_plugins/web_ui/__init__.py
zhenxun/services/plugin_init.py
zhenxun/builtin_plugins/__init__.py
zhenxun/builtin_plugins/init/init_plugin.py
zhenxun/builtin_plugins/init/init_task.py
zhenxun/builtin_plugins/sign_in/utils.py
zhenxun/builtin_plugins/web_ui/api/tabs/dashboard/data_source.py
zhenxun/builtin_plugins/web_ui/api/tabs/database/__init__.py
zhenxun/utils/enum.py
zhenxun/utils/manager/priority_manager.py
实现了一个 Web UI 配置,用于在数据库 URL 未配置时的初始设置。
  • 当数据库 URL 丢失时,会引发一个 DbUrlIsNode 异常,并显示一条消息,提示用户使用 Web UI 进行配置。
  • 添加了一个新的 API 端点 /configure 来处理配置设置。
  • 配置设置包括超级用户、数据库 URL、主机、端口、用户名和密码。
  • API 端点使用提供的配置更新 .env.dev 文件。
  • 添加了一个测试数据库连接 API 来验证数据库 URL。
  • 现在可以通过 http://{driver.config.host}:{driver.config.port}/#/configure 访问 Web UI。
zhenxun/services/db_context.py
zhenxun/builtin_plugins/web_ui/api/__init__.py
zhenxun/builtin_plugins/web_ui/api/configure/__init__.py
zhenxun/builtin_plugins/web_ui/api/configure/data_source.py
zhenxun/builtin_plugins/web_ui/api/configure/model.py
少量更改。
  • 修复了 sign_in/__init__.py 中的一个拼写错误。
  • 修改了 MessageManager.get,如果找不到 UID,则返回一个空列表。
zhenxun/builtin_plugins/sign_in/__init__.py
zhenxun/utils/manager/message_manager.py

提示和命令

与 Sourcery 互动

  • 触发新的审查: 在 Pull Request 上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审查评论。
  • 从审查评论生成 GitHub issue: 要求 Sourcery 从审查评论创建一个 issue,方法是回复它。您也可以回复审查评论并使用 @sourcery-ai issue 从中创建一个 issue。
  • 生成 Pull Request 标题: 在 Pull Request 标题中的任何位置写入 @sourcery-ai 以随时生成标题。您也可以在 Pull Request 上评论 @sourcery-ai title 以随时(重新)生成标题。
  • 生成 Pull Request 摘要: 在 Pull Request 正文中的任何位置写入 @sourcery-ai summary 以随时在您想要的位置生成 PR 摘要。您也可以在 Pull Request 上评论 @sourcery-ai summary 以随时(重新)生成摘要。
  • 生成审查者指南: 在 Pull Request 上评论 @sourcery-ai guide 以随时(重新)生成审查者指南。
  • 解决所有 Sourcery 评论: 在 Pull Request 上评论 @sourcery-ai resolve 以解决所有 Sourcery 评论。如果您已经解决了所有评论并且不想再看到它们,这将非常有用。
  • 驳回所有 Sourcery 审查: 在 Pull Request 上评论 @sourcery-ai dismiss 以驳回所有现有的 Sourcery 审查。如果您想重新开始新的审查,这将特别有用 - 不要忘记评论 @sourcery-ai review 以触发新的审查!
  • 为 issue 生成行动计划: 在 issue 上评论 @sourcery-ai plan 以为其生成行动计划。

自定义您的体验

访问您的 仪表板 以:

  • 启用或禁用审查功能,例如 Sourcery 生成的 Pull Request 摘要、审查者指南等。
  • 更改审查语言。
  • 添加、删除或编辑自定义审查说明。
  • 调整其他审查设置。

获得帮助

Original review guide in English

Reviewer's Guide by Sourcery

This pull request introduces a web UI configuration for initial setup when the database URL is not configured. It also implements a new HookPriorityManager to manage the execution order of startup and shutdown hooks based on priority. Additionally, it includes minor changes such as fixing a typo and modifying MessageManager.get.

Sequence diagram for Hook Priority Management on Startup

sequenceDiagram
    participant Driver
    participant HookPriorityManager
    Driver->>HookPriorityManager: on_startup()
    activate HookPriorityManager
    HookPriorityManager->>HookPriorityManager: Get priority data for STARTUP
    HookPriorityManager->>HookPriorityManager: Sort priorities
    loop For each priority
        loop For each function in priority
            HookPriorityManager->>func: Call function
            activate func
            func-->>HookPriorityManager: Return
            deactivate func
        end
    end
    deactivate HookPriorityManager
Loading

Updated class diagram for HookPriorityManager

classDiagram
    class HookPriorityManager {
        _data: dict[HookPriorityType, dict[int, list[Callable]]]
        +add(hook_type: HookPriorityType, func: Callable, priority: int = 5)
        +on_startup(priority: int = 5)
        +on_shutdown(priority: int = 5)
    }
    class HookPriorityType {
        STARTUP = \"STARTUP\"
        SHUTDOWN = \"SHUTDOWN\"
    }
    HookPriorityManager -- HookPriorityType : Uses
Loading

File-Level Changes

Change Details Files
Introduces a new HookPriorityManager to manage the execution order of startup and shutdown hooks based on priority.
  • Added HookPriorityManager class for managing hook execution order.
  • Implemented on_startup and on_shutdown decorators to register functions with specific priorities.
  • Modified existing startup functions to use the HookPriorityManager.
  • Added HookPriorityException to allow interrupting the startup process.
  • Added HookPriorityType enum to define hook types.
  • The startup process will now iterate through registered functions based on priority and execute them.
  • If a HookPriorityException is raised during startup, the process will be interrupted, and an error message will be logged.
zhenxun/services/db_context.py
zhenxun/utils/exception.py
zhenxun/builtin_plugins/scripts.py
zhenxun/builtin_plugins/admin/welcome_message/data_source.py
zhenxun/builtin_plugins/init/init_config.py
zhenxun/builtin_plugins/web_ui/__init__.py
zhenxun/services/plugin_init.py
zhenxun/builtin_plugins/__init__.py
zhenxun/builtin_plugins/init/init_plugin.py
zhenxun/builtin_plugins/init/init_task.py
zhenxun/builtin_plugins/sign_in/utils.py
zhenxun/builtin_plugins/web_ui/api/tabs/dashboard/data_source.py
zhenxun/builtin_plugins/web_ui/api/tabs/database/__init__.py
zhenxun/utils/enum.py
zhenxun/utils/manager/priority_manager.py
Implements a web UI configuration for initial setup when the database URL is not configured.
  • When the database URL is missing, a DbUrlIsNode exception is raised with a message prompting the user to use the web UI for configuration.
  • A new API endpoint /configure is added to handle the configuration settings.
  • The configuration settings include superusers, database URL, host, port, username, and password.
  • The API endpoint updates the .env.dev file with the provided configuration.
  • A test database connection API is added to verify the database URL.
  • The web UI is now accessible at http://{driver.config.host}:{driver.config.port}/#/configure.
zhenxun/services/db_context.py
zhenxun/builtin_plugins/web_ui/api/__init__.py
zhenxun/builtin_plugins/web_ui/api/configure/__init__.py
zhenxun/builtin_plugins/web_ui/api/configure/data_source.py
zhenxun/builtin_plugins/web_ui/api/configure/model.py
Minor changes.
  • Fixed a typo in sign_in/__init__.py.
  • Modified MessageManager.get to return an empty list if the UID is not found.
zhenxun/builtin_plugins/sign_in/__init__.py
zhenxun/utils/manager/message_manager.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@AkashiCoin AkashiCoin added the enhancement New feature or request label Mar 10, 2025
Comment thread zhenxun/builtin_plugins/web_ui/api/configure/__init__.py Fixed

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey @HibiKier - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a more descriptive name than _ for functions decorated with @HookPriorityManager.on_startup.
  • The HookPriorityException seems like it's being used for control flow; consider if there's a better way to handle the initial configuration.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

driver = nonebot.get_driver()


class HookPriorityManager:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider using a list of (priority, callable) tuples instead of a nested dictionary for hook management to simplify registration and execution if the prioritization feature is used in a few places only .

If the prioritization feature is only used in a few places, you might reduce the complexity by switching from a nested dictionary to a simple per–hook-type list of (priority, callable) tuples. This simplifies both registration and execution. For example:

Before:
```python
class HookPriorityManager:
    _data: ClassVar[dict[HookPriorityType, dict[int, list[Callable]]]] = {}

    @classmethod
    def add(cls, hook_type: HookPriorityType, func: Callable, priority: int = 5):
        if hook_type not in cls._data:
            cls._data[hook_type] = {}
        if priority not in cls._data[hook_type]:
            cls._data[hook_type][priority] = []
        cls._data[hook_type][priority].append(func)

After:

class HookPriorityManager:
    _data: ClassVar[dict[HookPriorityType, list[tuple[int, Callable]]]] = {}

    @classmethod
    def add(cls, hook_type: HookPriorityType, func: Callable, priority: int = 5):
        cls._data.setdefault(hook_type, []).append((priority, func))

    @classmethod
    async def run_hooks(cls, hook_type: HookPriorityType):
        hooks = cls._data.get(hook_type, [])
        for prio, func in sorted(hooks, key=lambda x: x[0]):
            if is_coroutine_callable(func):
                await func()
            else:
                func()

Then update the startup handler to call this method:

@driver.on_startup
async def _():
    try:
        await HookPriorityManager.run_hooks(HookPriorityType.STARTUP)
    except HookPriorityException as e:
        logger.error(f"HookExecution interrupted: {e}")

This keeps all functionality intact while reducing the layers of nested dictionaries and making registration/execution logic simpler to follow.

@AkashiCoin AkashiCoin added the documentation Improvements or additions to documentation label Mar 13, 2025
Comment thread zhenxun/builtin_plugins/web_ui/api/configure/__init__.py Fixed
Comment thread zhenxun/builtin_plugins/web_ui/api/configure/__init__.py Fixed
Comment thread zhenxun/utils/manager/priority_manager.py
Comment thread zhenxun/utils/manager/priority_manager.py
Comment thread zhenxun/utils/manager/priority_manager.py
Comment thread zhenxun/utils/enum.py Outdated
@AkashiCoin AkashiCoin added the dependencies Tracks issues related to external dependencies or library updates. label Apr 3, 2025
@HibiKier HibiKier force-pushed the feature/webui-config branch from 4ff0540 to e84fc89 Compare April 3, 2025 21:04
Comment thread zhenxun/builtin_plugins/web_ui/api/configure/__init__.py Dismissed
Comment thread zhenxun/builtin_plugins/web_ui/api/configure/__init__.py Dismissed
Comment thread zhenxun/builtin_plugins/web_ui/api/configure/__init__.py Dismissed
Comment thread zhenxun/builtin_plugins/web_ui/api/tabs/system/__init__.py Fixed
Comment thread zhenxun/builtin_plugins/web_ui/api/tabs/system/__init__.py Fixed
Comment thread zhenxun/builtin_plugins/web_ui/api/tabs/system/__init__.py Fixed
Comment thread zhenxun/builtin_plugins/web_ui/api/tabs/system/__init__.py Fixed
Comment thread zhenxun/builtin_plugins/web_ui/api/tabs/system/__init__.py Fixed
Comment thread zhenxun/builtin_plugins/web_ui/api/tabs/system/__init__.py Fixed
Comment thread zhenxun/builtin_plugins/web_ui/api/tabs/system/__init__.py Fixed
Comment thread zhenxun/builtin_plugins/web_ui/api/tabs/system/__init__.py Fixed
Comment thread zhenxun/builtin_plugins/web_ui/api/tabs/system/__init__.py Fixed
Comment thread zhenxun/builtin_plugins/web_ui/api/tabs/system/__init__.py Fixed
Comment thread zhenxun/builtin_plugins/web_ui/utils.py Fixed
Comment thread zhenxun/builtin_plugins/web_ui/utils.py Fixed
Comment thread zhenxun/builtin_plugins/web_ui/api/tabs/system/__init__.py Dismissed
Comment thread zhenxun/builtin_plugins/web_ui/api/tabs/system/__init__.py Dismissed
Comment thread zhenxun/builtin_plugins/web_ui/api/tabs/system/__init__.py Dismissed
Comment thread zhenxun/builtin_plugins/web_ui/api/tabs/system/__init__.py Dismissed
Comment thread zhenxun/services/db_context.py
@HibiKier HibiKier merged commit 99f1388 into main Jun 16, 2025
@HibiKier HibiKier deleted the feature/webui-config branch June 16, 2025 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Tracks issues related to external dependencies or library updates. documentation Improvements or additions to documentation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants