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
fix(chat): draw one column per field when a chart field plays two roles (#3865)
Raghav Chamadiyad0adcd41d265
diff --git a/packages/server/src/repowise/server/chat_spec/fit.py b/packages/server/src/repowise/server/chat_spec/fit.pyindex bcf8f20cd..3bea996f2 100644--- a/packages/server/src/repowise/server/chat_spec/fit.py+++ b/packages/server/src/repowise/server/chat_spec/fit.py@@ -207,6 +207,9 @@ def fit("""meta = meta or {}spec = _coerce_filters(_repair_names(spec, types), rows)+ # parse_spec drops these too; a repaired name can make the repeat only now.+ if spec.series and (spec.series == spec.x or spec.series in spec.y):+ spec = spec.noted(f"series {spec.series!r} repeats x or y, so it was dropped", series=None)# "x" and "value" name the axis and the plotted value when no field has that name.if spec.sort and spec.sort[0] not in types:name, order = spec.sort@@ -252,9 +255,20 @@ def fit(aggregate="sum" if size else "count",color=spec.color or measure,)+ # A group that is also the x, a part of it or a plotted field would be its column+ # twice. Dropped before the hierarchy fill, so a treemap left with none still nests.+ taken = {spec.x, *spec.x_parts, *spec.y, *([spec.series] if spec.series else [])}+ if spec.kind == "treemap" and (repeats := [g for g in spec.groups if g in taken]):+ kept = tuple(g for g in spec.groups if g not in taken)+ spec = spec.noted(+ f"groups {repeats} are already the x or a plotted field, so they were left out"+ + (f"; the treemap nests by {', '.join(kept)}" if kept else ""),+ groups=kept,+ )if spec.kind == "treemap" and not spec.groups and hierarchy:leaf = spec.x_parts[-1] if spec.x_parts else spec.x- above = tuple(hierarchy[: hierarchy.index(leaf)]) if leaf in hierarchy else ()+ levels = hierarchy[: hierarchy.index(leaf)] if leaf in hierarchy else ()+ above = tuple(h for h in levels if h not in taken)if above:spec = spec.noted(f"the treemap nests by {', '.join(above)}", groups=above)if spec.kind != "treemap" and (spec.groups or spec.color):diff --git a/packages/server/src/repowise/server/chat_spec/transform.py b/packages/server/src/repowise/server/chat_spec/transform.pyindex ae0af0f92..76fa0ab3f 100644--- a/packages/server/src/repowise/server/chat_spec/transform.py+++ b/packages/server/src/repowise/server/chat_spec/transform.py@@ -314,11 +314,16 @@ def _chart_rows(place = found.places.get(row_out[spec.x], {})for g in spec.groups:row_out[g] = place.get(g)- if spec.color:+ if _own_color_column(spec):row_out[_color_key(spec, keys)] = found.colors.get(row_out[spec.x])return out+def _own_color_column(spec: ChartSpec) -> bool:+ """A colour by a group level reads that level's column, so it adds none."""+ return spec.color is not None and spec.color not in spec.groups++def _color_key(spec: ChartSpec, keys: list[str]) -> str:key = spec.color or "color"return f"{key}_color" if key in keys or key == spec.x else key@@ -389,7 +394,9 @@ def transform((kfor k in _LABEL_FIELDS- if k in types and k != spec.x and k not in spec.y and k not in spec.x_parts+ if k in types+ and k not in (spec.x, spec.color)+ and k not in (*spec.y, *spec.x_parts, *spec.groups)),None,)@@ -404,7 +411,7 @@ def transform(columns.insert(0, (label_key, label(label_key)))if spec.groups:columns[:0] = [(g, label(g)) for g in spec.groups]- if spec.color:+ if spec.color and _own_color_column(spec):color_label = label(spec.color)if spec.aggregate and types.get(spec.color) == "number":color_label = f"Average {color_label.lower()}"@@ -444,7 +451,7 @@ def transform(bucket[k] = _round(sum(values)) if values else Nonefor g in spec.groups:bucket[g] = OTHER_LABEL- if spec.color:+ if _own_color_column(spec):bucket[_color_key(spec, keys)] = Noneif label_key:bucket[label_key] = Nonediff --git a/packages/types/fixtures/views.conformance.json b/packages/types/fixtures/views.conformance.jsonindex f9212892f..5ba2b7c6d 100644--- a/packages/types/fixtures/views.conformance.json+++ b/packages/types/fixtures/views.conformance.json@@ -319,6 +319,7 @@{ "name": "treemap group naming a missing column", "from": "c6", "set": { "groups": ["nope"] }, "error": "view.groups[0]: not a column of data" },{ "name": "treemap group that is the x column", "from": "c6", "set": { "groups": ["file"] }, "error": "is the x or a series column" },{ "name": "treemap coloured by its x", "from": "c6", "set": { "color": "file" }, "error": "view.color: not a column of data other than x" },+ { "name": "treemap coloured by a missing column", "from": "c6", "set": { "color": "nope" }, "error": "view.color: not a column of data other than x" },{ "name": "treemap with two series", "from": "c6", "set": { "series": ["nloc", "health_band"] }, "error": "a treemap chart plots 1 series" },{ "name": "negative treemap cell", "from": "c6", "set": { "data.rows.0.nloc": -1 }, "error": "cannot be negative" },{ "name": "string in a heatmap cell", "from": "c7", "set": { "data.rows.0.s1": "4" }, "error": "plotted values must be numbers" },@@ -340,6 +341,7 @@],"valid_variants": [{ "name": "a part at the current schema version", "from": "c6", "set": { "schema_version": 2 } },+ { "name": "treemap coloured by one of its group levels", "from": "c6", "set": { "color": "package" } },{ "name": "non-BMP title and summary", "from": "t1", "set": { "title": "😀", "summary": "Payments 💳 leads" } },{ "name": "negative zero in a line", "from": "c3", "set": { "data.rows.0.commits": -0 } },{ "name": "negative zero donut part", "from": "c4", "set": { "data.rows.2.files": -0.0 } },diff --git a/tests/unit/server/test_chat_spec.py b/tests/unit/server/test_chat_spec.pyindex 8d5351d4e..88438c828 100644--- a/tests/unit/server/test_chat_spec.py+++ b/tests/unit/server/test_chat_spec.py@@ -442,6 +442,81 @@ def test_a_treemap_nests_by_the_hierarchy_and_colours_by_a_field():assert ("score", "Average score") in result.columns+def test_a_treemap_coloured_by_a_group_level_reads_that_levels_column():+ rows = [+ {"package": "core", "directory": "core/a", "file": "x.py", "nloc": 10},+ {"package": "core", "directory": "core/b", "file": "y.py", "nloc": 30},+ {"package": "web", "directory": "web/src", "file": "z.ts", "nloc": 5},+ ]+ options = {"x": "directory", "y": "nloc", "aggregate": "sum", "kind": "treemap"}+ result = _run(rows, **options, groups=["package", "directory"], color="package")+ keys = [k for k, _ in result.columns]+ assert keys == ["package", "directory", "nloc"]+ assert result.groups == ["package"] and result.color == "package"+ assert result.rows[0] == {"package": "core", "directory": "core/b", "nloc": 30}+ assert result.notes[0] == (+ "groups ['directory'] are already the x or a plotted field, so they were left out; "+ "the treemap nests by package"+ )+++_TREE = [+ {"package": "core", "directory": "core/a", "file": "x.py", "nloc": 10, "score": 9.0},+ {"package": "core", "directory": "core/b", "file": "y.py", "nloc": 30, "score": 5.0},+ {"package": "web", "directory": "web/src", "file": "z.ts", "nloc": 5, "score": 7.0},+]+++def _tree(**options: Any):+ types = field_types(_TREE)+ spec = fit(parse_spec(options), _TREE, types, hierarchy=("package", "directory", "file"))+ return transform(spec, _TREE, types)+++def test_a_treemap_whose_only_group_repeats_x_still_nests_by_the_hierarchy():+ result = _tree(x="directory", y="nloc", aggregate="sum", kind="treemap", groups=["directory"])+ assert result.groups == ["package"]+ assert result.notes[-1] == "the treemap nests by package"+++def test_a_group_that_is_part_of_a_composite_x_is_left_out():+ result = _tree(x=["package", "directory"], y="nloc", aggregate="sum", kind="treemap", groups=["package"])+ assert result.groups == []+ keys = [k for k, _ in result.columns]+ assert len(keys) == len(set(keys))+ assert "groups ['package'] are already the x" in result.notes[0]+++ ("options", "color_key"),+ [+ ({"x": "directory", "y": "nloc", "aggregate": "sum", "color": "nloc"}, "nloc_color"),+ ({"x": "directory", "y": "nloc", "aggregate": "sum", "color": "directory"}, "directory_color"),+ ({"x": "directory", "y": "score", "aggregate": "avg", "weight": "nloc", "color": "nloc"}, "nloc_color"),+ ],+)+def test_a_treemap_colour_that_is_another_role_gets_its_own_column(options, color_key):+ result = _tree(kind="treemap", **options)+ keys = [k for k, _ in result.columns]+ assert result.color == color_key and keys.count(color_key) == 1+ assert len(keys) == len(set(keys))+++def test_the_other_bucket_of_a_treemap_coloured_by_a_group_keeps_one_column():+ result = _tree(+ x="directory", y="nloc", aggregate="sum", kind="treemap", color="package", top=1, other=True+ )+ assert result.color == "package" and result.other+ assert result.rows[-1]["package"] == OTHER_LABEL+ assert all(set(row) == {k for k, _ in result.columns} for row in result.rows)+++def test_a_repaired_series_name_that_repeats_x_is_dropped():+ result = _run(x="kind", series="Kind")+ assert len(result.series) == 1+ assert any("series 'kind' repeats x or y" in n for n in result.notes)++def test_figures_carry_totals_and_shares_so_nobody_divides():result = _run(x="kind")assert result.figures["total"] == 5diff --git a/tests/unit/server/test_chat_views.py b/tests/unit/server/test_chat_views.pyindex 482dc5ba6..60668dd3b 100644--- a/tests/unit/server/test_chat_views.py+++ b/tests/unit/server/test_chat_views.py@@ -790,6 +790,54 @@ def test_a_suggestion_says_what_it_gave_up():assert result["adjustments"] == ["filter dropped, so every row is charted"]+_PACKAGES = {+ "files": [+ {"package": "core", "directory": "core/a", "file_path": "x.py", "nloc": 10, "score": 9.0},+ {"package": "core", "directory": "core/b", "file_path": "y.py", "nloc": 30, "score": 5.0},+ {"package": "web", "directory": "web/src", "file_path": "z.ts", "nloc": 5, "score": 7.0},+ ]+}+++ "options",+ [+ {"kind": "treemap", "x": "directory", "y": "nloc", "aggregate": "sum",+ "groups": ["package", "directory"], "color": "package"},+ {"kind": "treemap", "x": "file_path", "y": "nloc", "groups": ["package", "directory"],+ "color": "directory"},+ {"kind": "treemap", "x": "directory", "y": "nloc", "aggregate": "sum", "groups": ["nloc"]},+ {"kind": "bar", "x": "directory", "y": "nloc", "aggregate": "sum", "series": "directory"},+ {"kind": "scatter", "x": "nloc", "y": "score", "series": "nloc"},+ {"kind": "heatmap", "x": "directory", "y": "nloc", "aggregate": "sum",+ "series": "package", "color": "package"},+ {"kind": "heatmap", "x": "package", "y": "nloc", "aggregate": "sum", "series": "directory",+ "groups": ["package"]},+ {"kind": "treemap", "x": "directory", "y": "nloc", "aggregate": "sum", "color": "nloc"},+ {"kind": "treemap", "x": "directory", "y": "nloc", "aggregate": "sum", "color": "directory"},+ {"kind": "treemap", "x": "directory", "y": "score", "aggregate": "avg", "weight": "nloc",+ "color": "nloc"},+ {"kind": "treemap", "x": "directory", "y": "nloc", "aggregate": "sum", "groups": ["package"],+ "color": "package", "top": 1, "other": True},+ ],+)+def test_one_field_in_two_roles_draws_one_column(options):+ turn, receipt = _show_chart(_PACKAGES, rows="files", **options)+ assert "error" not in receipt+ ViewPartList.model_validate(turn.parts)+ keys = [c["key"] for c in turn.parts[0]["data"]["columns"]]+ assert len(keys) == len(set(keys))+++def test_a_repeated_person_field_is_refused_not_dropped():+ data = {"files": [{**row, "owner": "ana"} for row in _PACKAGES["files"]]}+ _, result = _show_chart(+ data, rows="files", kind="treemap", x="directory", y="nloc", aggregate="sum",+ groups=["owner", "directory"],+ )+ assert result["error_code"] == "person_grouping"++def test_a_risk_score_is_refused_before_any_suggestion():_, result = _show_chart(RISK, rows="global_hotspots", x="file_path", y="hotspot_scroe")assert result["error_code"] == "risk_score_series"