Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
refactor(core): move the refactoring summary chips into core (#2519)
Raghav Chamadiya328322e27e6b
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring_summary.py b/packages/core/src/repowise/core/analysis/health/refactoring_summary.pynew file mode 100644index 000000000..f41490ea9--- /dev/null+++ b/packages/core/src/repowise/core/analysis/health/refactoring_summary.py@@ -0,0 +1,57 @@+"""The refactoring board's summary chips, over plain plan rows.++Kept outside the ``refactoring`` package on purpose: importing that package+registers every detector, which a reader that only counts plans should not pay.+"""++from __future__ import annotations++from collections.abc import Iterable+from typing import Any++from repowise.core.analysis.health.rows import field++#: The types the board groups under its "Structural" lens.+STRUCTURAL_TYPES = frozenset({"split_file", "break_cycle", "extract_class", "move_method"})++#: A plan recovers health at or above this ``impact_delta``.+HEALTH_RECOVERY_MIN = 0.1++#: A plan's health gain is negligible below this ``impact_delta``. Overlaps the+#: recovery band by design: the two chips answer different questions.+NEGLIGIBLE_HEALTH_BELOW = 0.5+++def summarize_plans(plans: Iterable[Any]) -> dict[str, Any]:+ """Chip counts for *plans*, each with ``refactoring_type``, ``file_path``,+ ``effort_bucket`` and ``impact_delta``. Shaped like ``RefactoringSummary``."""+ plans = list(plans)+ by_type: dict[str, int] = {}+ for plan in plans:+ kind = field(plan, "refactoring_type")+ by_type[kind] = by_type.get(kind, 0) + 1+ impacts = [field(plan, "impact_delta") for plan in plans]+ return {+ "total": len(plans),+ "by_type": [+ {"type": kind, "count": count}+ for kind, count in sorted(by_type.items(), key=lambda item: (-item[1], item[0]))+ ],+ "files_total": len({field(plan, "file_path") for plan in plans}),+ "structural_total": sum(+ field(plan, "refactoring_type") in STRUCTURAL_TYPES for plan in plans+ ),+ "performance_total": by_type.get("performance_fix", 0),+ "small_effort_total": sum(field(plan, "effort_bucket") == "S" for plan in plans),+ "health_recovery_total": sum(impact >= HEALTH_RECOVERY_MIN for impact in impacts),+ "negligible_health_total": sum(impact < NEGLIGIBLE_HEALTH_BELOW for impact in impacts),+ "best_health_gain": round(max((float(impact) for impact in impacts), default=0.0), 3),+ }+++__all__ = [+ "HEALTH_RECOVERY_MIN",+ "NEGLIGIBLE_HEALTH_BELOW",+ "STRUCTURAL_TYPES",+ "summarize_plans",+]diff --git a/packages/server/src/repowise/server/routers/refactoring.py b/packages/server/src/repowise/server/routers/refactoring.pyindex 79e03b298..81f5e6a2d 100644--- a/packages/server/src/repowise/server/routers/refactoring.py+++ b/packages/server/src/repowise/server/routers/refactoring.py@@ -24,6 +24,7 @@ from repowise.core.analysis.health.refactoring.recommendations import (blast_size,hydrate_recommendations,)+from repowise.core.analysis.health.refactoring_summary import STRUCTURAL_TYPES, summarize_plansfrom repowise.core.persistence import crudfrom repowise.core.persistence.crud.analysis.refactoring import ALLOWED_STATUSESfrom repowise.server.deps import get_db_session, verify_api_key@@ -140,34 +141,12 @@ def _to_response(data: dict[str, Any]) -> RefactoringPlanResponse:return RefactoringPlanResponse(**data)-_STRUCTURAL_TYPES = {"split_file", "break_cycle", "extract_class", "move_method"}+_STRUCTURAL_TYPES = STRUCTURAL_TYPES_EFFORT_ORDER = {"S": 0, "M": 1, "L": 2, "XL": 3}def _summary(recommendations: list[Any]) -> RefactoringSummary:- by_type: dict[str, int] = {}- for recommendation in recommendations:- suggestion = recommendation.suggestion- by_type[suggestion.refactoring_type] = by_type.get(suggestion.refactoring_type, 0) + 1- return RefactoringSummary(- total=len(recommendations),- by_type=[- RefactoringTypeCount(type=kind, count=count)- for kind, count in sorted(by_type.items(), key=lambda item: (-item[1], item[0]))- ],- files_total=len({item.suggestion.file_path for item in recommendations}),- structural_total=sum(- item.suggestion.refactoring_type in _STRUCTURAL_TYPES for item in recommendations- ),- performance_total=by_type.get("performance_fix", 0),- small_effort_total=sum(item.suggestion.effort_bucket == "S" for item in recommendations),- health_recovery_total=sum(item.suggestion.impact_delta >= 0.1 for item in recommendations),- negligible_health_total=sum(item.suggestion.impact_delta < 0.5 for item in recommendations),- best_health_gain=round(- max((float(item.suggestion.impact_delta) for item in recommendations), default=0.0),- 3,- ),- )+ return RefactoringSummary(**summarize_plans(item.suggestion for item in recommendations))def _csv_values(value: str | None) -> set[str]:diff --git a/tests/unit/health/test_lightweight_health_imports.py b/tests/unit/health/test_lightweight_health_imports.pyindex 741cc1001..a6b3b6f6d 100644--- a/tests/unit/health/test_lightweight_health_imports.py+++ b/tests/unit/health/test_lightweight_health_imports.py@@ -24,6 +24,7 @@ import pytest_LIGHTWEIGHT = ("repowise.core.analysis.health.rows",+ "repowise.core.analysis.health.refactoring_summary","repowise.core.analysis.health.ranking","repowise.core.analysis.health.aggregation","repowise.core.analysis.health.trends",diff --git a/tests/unit/server/test_refactoring_summary_golden.py b/tests/unit/server/test_refactoring_summary_golden.pynew file mode 100644index 000000000..cdc559697--- /dev/null+++ b/tests/unit/server/test_refactoring_summary_golden.py@@ -0,0 +1,76 @@+"""Characterization of the refactoring board's summary chips.++Pins every count, the by-type order, both threshold edges and the rounding of+the best gain that the board and ``/refactoring/plans`` report.+"""++from __future__ import annotations++from types import SimpleNamespace++from repowise.server.routers import refactoring+++def _rec(kind: str, path: str, effort: str, impact: float) -> SimpleNamespace:+ return SimpleNamespace(+ suggestion=SimpleNamespace(+ refactoring_type=kind, file_path=path, effort_bucket=effort, impact_delta=impact+ )+ )+++_RECS = [+ _rec("extract_method", "a.py", "S", 0.0999),+ _rec("extract_method", "a.py", "M", 0.1),+ _rec("split_file", "b.py", "L", 0.4999),+ _rec("break_cycle", "c.py", "S", 0.5),+ _rec("performance_fix", "c.py", "XL", 1.23456),+ _rec("move_method", "d.py", "", 0.0),+ _rec("extract_class", "e.py", "S", 2.34567),+ _rec("extract_helper", "e.py", "M", 0.25),+]+++def test_summary_counts_and_threshold_edges() -> None:+ assert refactoring._summary(_RECS).model_dump() == {+ "total": 8,+ "by_type": [+ {"type": "extract_method", "count": 2},+ {"type": "break_cycle", "count": 1},+ {"type": "extract_class", "count": 1},+ {"type": "extract_helper", "count": 1},+ {"type": "move_method", "count": 1},+ {"type": "performance_fix", "count": 1},+ {"type": "split_file", "count": 1},+ ],+ "files_total": 5,+ "structural_total": 4,+ "performance_total": 1,+ "small_effort_total": 3,+ "health_recovery_total": 6,+ "negligible_health_total": 5,+ "best_health_gain": 2.346,+ }+++def test_summary_of_nothing() -> None:+ assert refactoring._summary([]).model_dump() == {+ "total": 0,+ "by_type": [],+ "files_total": 0,+ "structural_total": 0,+ "performance_total": 0,+ "small_effort_total": 0,+ "health_recovery_total": 0,+ "negligible_health_total": 0,+ "best_health_gain": 0.0,+ }+++def test_structural_lens() -> None:+ assert set(refactoring._STRUCTURAL_TYPES) == {+ "split_file",+ "break_cycle",+ "extract_class",+ "move_method",+ }