fix: wire metrics service to existing methods#193
Open
Yurzi wants to merge 2 commits into
Open
Conversation
The big architecture refactor (e46b634) left MetricsService calling methods that no longer exist: - intelligence_metrics_service.calculate_metrics() - database_manager.get_diversity_metrics() - database_manager.get_affection_metrics() Each call raised AttributeError on every request, spamming error logs and making the three WebUI metrics endpoints always return zeroed fallback data. Rewire the three methods to the real APIs: - intelligence: gather inputs via get_detailed_metrics / get_all_user_affections, then call calculate_learning_efficiency and map LearningEfficiencyMetrics to {overall_score, dimensions, trends} - affection: compute from get_all_user_affections (average, user count, high/low counts, distribution buckets) - diversity: best-effort estimate from style patterns, no error spam Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Update the stale conftest mocks that mirrored the removed methods (get_diversity_metrics / get_affection_metrics / calculate_metrics) to the real API now used by MetricsService (get_detailed_metrics / get_all_user_affections / calculate_learning_efficiency). Add integration tests for /api/intelligence_metrics, /api/affection_metrics and /api/diversity_metrics asserting they return real computed data with no error key, guarding against the AttributeError regression. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
Reviewer's GuideWires the WebUI metrics service to the new post-refactor metrics/data APIs, replacing calls to removed methods with the correct implementations and adding integration tests to ensure the three metrics endpoints return real computed data instead of zeroed fallbacks. Sequence diagram for updated get_intelligence_metrics flowsequenceDiagram
actor WebUI
participant MetricsService
participant DatabaseManager
participant IntelligenceMetricsService
WebUI->>MetricsService: get_intelligence_metrics(group_id)
activate MetricsService
MetricsService->>DatabaseManager: get_detailed_metrics(group_id)
DatabaseManager-->>MetricsService: detailed_metrics
MetricsService->>DatabaseManager: get_all_user_affections(group_id)
DatabaseManager-->>MetricsService: affections
MetricsService->>IntelligenceMetricsService: calculate_learning_efficiency(total_messages, filtered_messages, style_patterns_learned, persona_updates_count, affection_users_count)
IntelligenceMetricsService-->>MetricsService: LearningEfficiencyMetrics
MetricsService-->>WebUI: {overall_score, dimensions, trends}
deactivate MetricsService
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- In
get_diversity_metrics, you callself.database_manager.get_detailed_metricswithout checking ifself.database_managerisNone, unlikeget_intelligence_metricswhere you guard ondb, which can lead to an AttributeError in cases where the DB manager is not wired. - The data collection logic in
get_intelligence_metrics(pulling and normalizing metrics fromget_detailed_metricsandget_all_user_affections) is getting fairly dense; consider extracting it into a small helper method to keep the endpoint method focused on orchestration. - The affection buckets (0–20, 21–40, etc.) and thresholds (≥70, ≤30) are currently hard-coded; if these ranges are expected to evolve or be reused, consider centralizing them as configuration or module-level constants to make future changes safer.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `get_diversity_metrics`, you call `self.database_manager.get_detailed_metrics` without checking if `self.database_manager` is `None`, unlike `get_intelligence_metrics` where you guard on `db`, which can lead to an AttributeError in cases where the DB manager is not wired.
- The data collection logic in `get_intelligence_metrics` (pulling and normalizing metrics from `get_detailed_metrics` and `get_all_user_affections`) is getting fairly dense; consider extracting it into a small helper method to keep the endpoint method focused on orchestration.
- The affection buckets (0–20, 21–40, etc.) and thresholds (≥70, ≤30) are currently hard-coded; if these ranges are expected to evolve or be reused, consider centralizing them as configuration or module-level constants to make future changes safer.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
重构整个项目为新架构(e46b634)之后,
webui/services/metrics_service.py里残留了对已不存在方法的调用,导致三个 WebUI 指标接口实际上一直失效:intelligence_metrics_service.calculate_metrics()calculate_learning_efficiency(...)database_manager.get_diversity_metrics()database_manager.get_affection_metrics()get_all_user_affections()由于调用都包在
try/except内,运行时不会崩溃,但每次请求都抛AttributeError、刷错误日志,并返回全 0 的兜底数据。对应日志:修改内容
webui/services/metrics_service.pyget_detailed_metrics/get_all_user_affections收集输入,再调用真实的calculate_learning_efficiency(...),并将LearningEfficiencyMetrics映射为约定的{overall_score, dimensions, trends}。get_all_user_affections(group_id),真实计算平均好感度、用户数、高/低好感度人数(≥70 / ≤30)及五档分布。测试
tests/conftest.py中仍指向旧方法名的 mock,改为真实 API。tests/integration/test_metrics_service_endpoints.py,覆盖/api/intelligence_metrics、/api/affection_metrics、/api/diversity_metrics,断言返回真实计算数据且不含error字段。验证
本地(含 astrbot 运行环境)整套集成测试通过。
🤖 Generated with Claude Code
Summary by Sourcery
Wire the WebUI metrics service to the current metrics and database APIs so the three metrics endpoints return real computed data instead of zeroed fallbacks.
New Features:
Tests: