Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(metrics): close the row hole that a dropped panel leaves in a generated dashboard (#122158)

Frank Hamandf0e9cfd95721
diff --git a/products/metrics/backend/suggested_dashboards/dashboards.py b/products/metrics/backend/suggested_dashboards/dashboards.py
index b3653e267717..8fb6f23138ad 100644
--- a/products/metrics/backend/suggested_dashboards/dashboards.py
+++ b/products/metrics/backend/suggested_dashboards/dashboards.py
@@ -87,7 +87,7 @@ def _with_team_metric_types(query: dict[str, Any], catalog: MetricCatalog) -> di
return resolved
-def _fill_rows(boxes: list[tuple[int, GridLayout]]) -> dict[int, GridLayout]:
+def fill_rows(boxes: list[tuple[int, GridLayout]]) -> dict[int, GridLayout]:
"""Spread the boxes of each row across the full width again, after some panels of the row were dropped."""
rows: dict[int, list[tuple[int, GridLayout]]] = defaultdict(list)
for index, layout in boxes:
@@ -128,7 +128,7 @@ def build_tiles(panels: Sequence[TemplatePanel], catalog: MetricCatalog, *, refi
dropped.append(panel)
else:
kept.append((panel, query))
- layouts = _fill_rows([(index, panel.layout) for index, (panel, _) in enumerate(kept)]) if dropped and refill else {}
+ layouts = fill_rows([(index, panel.layout) for index, (panel, _) in enumerate(kept)]) if dropped and refill else {}
tiles: list[NewInsightTile | NewTextTile] = []
for index, (panel, query) in enumerate(kept):
layout = layouts.get(index, panel.layout).clamped(
diff --git a/products/metrics/backend/suggested_dashboards/generation.py b/products/metrics/backend/suggested_dashboards/generation.py
index b66f459b8ba1..b156afa2b6b8 100644
--- a/products/metrics/backend/suggested_dashboards/generation.py
+++ b/products/metrics/backend/suggested_dashboards/generation.py
@@ -47,6 +47,7 @@ from products.metrics.backend.suggested_dashboards.dashboards import (
attribute_variable,
create_dashboard,
drop_preview,
+ fill_rows,
)
from products.metrics.backend.suggested_dashboards.llm import MODEL, ask_model, image_block, text_block
from products.metrics.backend.suggested_dashboards.matching import query_metric_names, template_metric_names
@@ -350,9 +351,11 @@ def _convert(
boxes = place_screenshot_boxes(
[RequestedBox(key=draft.key, layout=draft.layout, min_w=2, min_h=2) for draft in accepted]
)
+ # A dropped panel leaves a hole in its row, so the rest of the row spreads across the full width again.
+ layouts = fill_rows([(index, boxes[draft.key]) for index, draft in enumerate(accepted)])
panels = tuple(
- TemplatePanel(key=draft.key, title=draft.title[:200], query=queries_by_key[draft.key], layout=boxes[draft.key])
- for draft in accepted
+ TemplatePanel(key=draft.key, title=draft.title[:200], query=queries_by_key[draft.key], layout=layouts[index])
+ for index, draft in enumerate(accepted)
)
definitions = _variable_definitions(valid_variables, accepted, queries_by_key)
kept_variables = tuple(variable for variable in valid_variables if variable.name in definitions)
diff --git a/products/metrics/backend/tests/test_suggested_dashboards.py b/products/metrics/backend/tests/test_suggested_dashboards.py
index b2563fa54d3b..d3ca8629d11a 100644
--- a/products/metrics/backend/tests/test_suggested_dashboards.py
+++ b/products/metrics/backend/tests/test_suggested_dashboards.py
@@ -398,6 +398,19 @@ class TestSuggestedDashboards(SimpleTestCase):
assert [panel.key for panel in converted.panels] == ["orders"]
assert converted.dropped == ("queue: Bad regex",)
+ def test_convert_closes_the_hole_that_a_dropped_panel_leaves_in_its_row(self) -> None:
+ drafts = [
+ self._draft(key, "orders_total", x).model_copy(update={"layout": GridLayout(x=x, y=0, w=3, h=2)})
+ for key, x in [("a", 0), ("b", 3), ("c", 6), ("d", 9)]
+ ]
+ converted = _convert(drafts, Team(id=1), CATALOG, checks=_Checks({}, errors={"b": "Bad regex"}))
+
+ assert [(panel.key, panel.layout.x, panel.layout.w) for panel in converted.panels] == [
+ ("a", 0, 4),
+ ("c", 4, 4),
+ ("d", 8, 4),
+ ]
+
def test_convert_lists_only_the_values_of_the_metrics_of_the_panels_that_refer_to_a_variable(self) -> None:
scoped = [BuilderFilter(key="instance", op="eq", value="$instance")]
converted = _convert(