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.

perf(metrics): open suggested dashboards on the last hour (#122333)

Frank Hamand85c6dfb011ac
diff --git a/products/metrics/backend/suggested_dashboards/dashboards.py b/products/metrics/backend/suggested_dashboards/dashboards.py
index 8fb6f23138ad..baa193e1357f 100644
--- a/products/metrics/backend/suggested_dashboards/dashboards.py
+++ b/products/metrics/backend/suggested_dashboards/dashboards.py
@@ -46,6 +46,8 @@ MAX_TEXT_LENGTH = 4000
SERVICE_ATTRIBUTES = frozenset({"service_name", "service.name"})
# The name and attribute tables store hourly buckets, so the bucket that holds the start of the range counts too.
OPTIONS_RANGE = "time_bucket >= toStartOfHour($__from) AND time_bucket < $__to"
+# A dashboard opens on the last hour, as the metrics viewer does. Without it, each tile reads the last 24 hours.
+DASHBOARD_DATE_FROM = "-1h"
_OTEL_TYPES = frozenset(member.value for member in MetricsOtelType)
@@ -226,7 +228,7 @@ def create_dashboard(
) -> tuple[CreatedDashboard | None, BuiltTiles]:
"""Create the dashboard as the user. Returns no dashboard when no panel has data in this team.
- The dashboard gets only the variables that its tiles refer to.
+ The dashboard gets only the variables that its tiles refer to, and opens on the last hour.
"""
built = build_tiles(panels, catalog, refill=refill)
if not any(isinstance(tile, NewInsightTile) for tile in built.tiles):
@@ -244,7 +246,10 @@ def create_dashboard(
tiles=built.tiles,
idempotency_key=idempotency_key,
unlisted=unlisted,
- filters={"metricVariables": kept_variables} if kept_variables else None,
+ filters={
+ "date_from": DASHBOARD_DATE_FROM,
+ **({"metricVariables": kept_variables} if kept_variables else {}),
+ },
),
)
return created, built
diff --git a/products/metrics/backend/tests/test_suggested_dashboards_api.py b/products/metrics/backend/tests/test_suggested_dashboards_api.py
index 62964552a73c..8e5a73b3782e 100644
--- a/products/metrics/backend/tests/test_suggested_dashboards_api.py
+++ b/products/metrics/backend/tests/test_suggested_dashboards_api.py
@@ -167,6 +167,7 @@ class TestSuggestedDashboardsAPI(APIBaseTest):
dashboard = Dashboard.objects.get(id=response.json()["dashboard_id"])
assert dashboard.filters["metricVariables"] == [template.variables[0]]
assert dashboard.filters["metricVariables"][0]["current"] == {"values": [], "all": True}
+ assert dashboard.filters["date_from"] == "-1h"
def test_suggestions_need_the_feature_flag(self) -> None:
self.flags.discard("metrics-suggested-dashboards")