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.

refactor(exports): name csv export series from the query (#120255)

Thomas Obermüllere1c825cbe8ad
diff --git a/products/exports/backend/tasks/csv_exporter.py b/products/exports/backend/tasks/csv_exporter.py
index a0907f374a2d..fc121f7e130e 100644
--- a/products/exports/backend/tasks/csv_exporter.py
+++ b/products/exports/backend/tasks/csv_exporter.py
@@ -5,8 +5,9 @@ import datetime
import tempfile
import textwrap
from collections import OrderedDict
-from collections.abc import Generator, Iterator
+from collections.abc import Generator, Iterator, Mapping
from contextlib import contextmanager
+from copy import deepcopy
from dataclasses import dataclass
from typing import Any, NoReturn, Optional, Protocol
from urllib.parse import parse_qsl, quote, urlencode, urlparse, urlunparse
@@ -40,8 +41,15 @@ from posthog.hogql_queries.utils.breakdowns import (
BREAKDOWN_OTHER_DISPLAY,
BREAKDOWN_OTHER_STRING_LABEL,
)
+from posthog.hogql_queries.utils.result_series import (
+ get_action_names,
+ get_result_series,
+ get_series_custom_name,
+ get_series_key,
+)
from posthog.jwt import PosthogJwtAudience, encode_jwt
from posthog.query_creator_access import creator_access_revoked, report_creator_access_revoked
+from posthog.schema_migrations.upgrade import upgrade
from posthog.security.spreadsheet_safety import sanitize_formula_injection
from posthog.tasks.exporter import EXPORT_TIMER
from posthog.utils import absolute_uri
@@ -276,7 +284,32 @@ def _format_breakdown_value(breakdown_value: Any) -> str:
return "::".join(breakdown_value)
-def _convert_response_to_csv_data(data: Any, breakdown_filter: Optional[dict] = None) -> Generator[Any]:
+def _custom_name_column(
+ query: Mapping[str, Any], node: Mapping[str, Any], action_names: Optional[Mapping[int, str | None]]
+) -> str | None:
+ # Each runner names a rename by its own rule: trends counts a `name` other than the series key, stickiness and
+ # lifecycle count only `custom_name`, and the calendar heatmap reports no rename.
+ if query.get("kind") == "TrendsQuery":
+ if (query.get("trendsFilter") or {}).get("display") == "CalendarHeatmap":
+ return None
+ return get_series_custom_name(node, action_names)
+ custom_name = node.get("custom_name")
+ return custom_name if isinstance(custom_name, str) and custom_name else None
+
+
+def _series_query(query: dict) -> dict:
+ # The runner unwraps a visualization node and runs the upgraded query, which stamps `kind` on legacy series nodes.
+ upgraded = upgrade(deepcopy(query))
+ source = upgraded.get("source")
+ return source if upgraded.get("kind") == "InsightVizNode" and isinstance(source, dict) else upgraded
+
+
+def _convert_response_to_csv_data(
+ data: Any,
+ breakdown_filter: Optional[dict] = None,
+ series_query: Optional[Mapping[str, Any]] = None,
+ action_names: Optional[Mapping[int, str | None]] = None,
+) -> Generator[Any]:
if isinstance(data.get("results"), list):
results = data.get("results")
if len(results) > 0 and (isinstance(results[0], list) or isinstance(results[0], tuple)) and data.get("types"):
@@ -380,12 +413,13 @@ def _convert_response_to_csv_data(data: Any, breakdown_filter: Optional[dict] =
for index, item in enumerate(results):
label = item.get("label", f"Series #{index + 1}")
compare_label = item.get("compare_label", "")
- action = item.get("action")
+ node = get_result_series(series_query, item) if series_query else None
breakdown_values, breakdowns, has_breakdown_columns = _get_breakdown_info(item, breakdown_filter)
- if has_breakdown_columns and isinstance(action, dict) and action.get("name"):
- series_name = action["name"]
+ if has_breakdown_columns and node is not None:
+ # The breakdown columns carry the value, so the series takes the runner's series label without it.
+ series_name = get_series_key(node, action_names) or "All events"
else:
series_name = label
@@ -396,8 +430,11 @@ def _convert_response_to_csv_data(data: Any, breakdown_filter: Optional[dict] =
label_item = date_labels_item if is_comparison else item
- if isinstance(action, dict) and action.get("custom_name"):
- line["custom name"] = action.get("custom_name")
+ custom_name = (
+ _custom_name_column(series_query, node, action_names) if series_query and node is not None else None
+ )
+ if custom_name:
+ line["custom name"] = custom_name
for idx, val in enumerate(breakdown_values):
prop_name = breakdowns[idx].get("property") if idx < len(breakdowns) else None
@@ -538,7 +575,16 @@ def get_from_insights_api(exported_asset: ExportedAsset, limit: int, resource: d
raise unexpected_empty_json_response
- csv_rows = list(_convert_response_to_csv_data(data))
+ # A saved insight's results name their series only through the query that the insights API returns with them.
+ query = data.get("query")
+ series_query = _series_query(query) if isinstance(query, dict) else None
+ csv_rows = list(
+ _convert_response_to_csv_data(
+ data,
+ series_query=series_query,
+ action_names=get_action_names(exported_asset.team, series_query) if series_query else None,
+ )
+ )
total += len(csv_rows)
yield from csv_rows
@@ -565,6 +611,8 @@ def get_from_query(
assert query is not None
breakdown_filter = query.get("breakdownFilter") if query else None
+ series_query = _series_query(query)
+ action_names = get_action_names(exported_asset.team, series_query)
supports_limit = _query_supports_limit(query)
total = 0
@@ -608,7 +656,11 @@ def get_from_query(
if response_dict.get("error"):
raise Exception(f"Query failed: {response_dict['error']}")
- rows = list(_convert_response_to_csv_data(response_dict, breakdown_filter=breakdown_filter))
+ rows = list(
+ _convert_response_to_csv_data(
+ response_dict, breakdown_filter=breakdown_filter, series_query=series_query, action_names=action_names
+ )
+ )
rows = rows[: CSV_EXPORT_LIMIT - total]
total += len(rows)
yield from rows
diff --git a/products/exports/backend/tasks/test/csv_renders/trends.json b/products/exports/backend/tasks/test/csv_renders/trends.json
index 15b98a16012b..6b195d2bb20e 100644
--- a/products/exports/backend/tasks/test/csv_renders/trends.json
+++ b/products/exports/backend/tasks/test/csv_renders/trends.json
@@ -41,29 +41,16 @@
""
],
"response": {
+ "query": {
+ "kind": "TrendsQuery",
+ "series": [
+ { "kind": "EventsNode", "event": "pineapple_on_pizza_survey", "math": "dau", "custom_name": "Yes" },
+ { "kind": "EventsNode", "event": "pineapple_on_pizza_survey", "math": "dau", "custom_name": "No" }
+ ]
+ },
"result": [
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 0,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "Yes",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["true"]
- }
- ]
- }
- },
+ "order": 0,
"label": "pineapple_on_pizza_survey - Belarus",
"count": 1,
"data": [0, 0, 0, 1],
@@ -72,27 +59,7 @@
"breakdown_value": "Belarus"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 0,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "Yes",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["true"]
- }
- ]
- }
- },
+ "order": 0,
"label": "pineapple_on_pizza_survey - Canada",
"count": 2,
"data": [1, 1, 0, 0],
@@ -101,27 +68,7 @@
"breakdown_value": "Canada"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 0,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "Yes",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["true"]
- }
- ]
- }
- },
+ "order": 0,
"label": "pineapple_on_pizza_survey - Denmark",
"count": 2,
"data": [0, 2, 0, 0],
@@ -130,27 +77,7 @@
"breakdown_value": "Denmark"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 0,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "Yes",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["true"]
- }
- ]
- }
- },
+ "order": 0,
"label": "pineapple_on_pizza_survey - Georgia",
"count": 1,
"data": [0, 0, 1, 0],
@@ -159,27 +86,7 @@
"breakdown_value": "Georgia"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 0,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "Yes",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["true"]
- }
- ]
- }
- },
+ "order": 0,
"label": "pineapple_on_pizza_survey - Germany",
"count": 3,
"data": [3, 0, 0, 0],
@@ -188,27 +95,7 @@
"breakdown_value": "Germany"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 0,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "Yes",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["true"]
- }
- ]
- }
- },
+ "order": 0,
"label": "pineapple_on_pizza_survey - India",
"count": 9,
"data": [1, 4, 3, 1],
@@ -217,27 +104,7 @@
"breakdown_value": "India"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 0,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "Yes",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["true"]
- }
- ]
- }
- },
+ "order": 0,
"label": "pineapple_on_pizza_survey - Ireland",
"count": 2,
"data": [0, 1, 1, 0],
@@ -246,27 +113,7 @@
"breakdown_value": "Ireland"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 0,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "Yes",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["true"]
- }
- ]
- }
- },
+ "order": 0,
"label": "pineapple_on_pizza_survey - Luxembourg",
"count": 1,
"data": [0, 0, 1, 0],
@@ -275,27 +122,7 @@
"breakdown_value": "Luxembourg"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 0,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "Yes",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["true"]
- }
- ]
- }
- },
+ "order": 0,
"label": "pineapple_on_pizza_survey - Netherlands",
"count": 1,
"data": [0, 0, 1, 0],
@@ -304,27 +131,7 @@
"breakdown_value": "Netherlands"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 0,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "Yes",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["true"]
- }
- ]
- }
- },
+ "order": 0,
"label": "pineapple_on_pizza_survey - Norway",
"count": 1,
"data": [0, 0, 1, 0],
@@ -333,27 +140,7 @@
"breakdown_value": "Norway"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 0,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "Yes",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["true"]
- }
- ]
- }
- },
+ "order": 0,
"label": "pineapple_on_pizza_survey - Poland",
"count": 1,
"data": [0, 1, 0, 0],
@@ -362,27 +149,7 @@
"breakdown_value": "Poland"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 0,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "Yes",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["true"]
- }
- ]
- }
- },
+ "order": 0,
"label": "pineapple_on_pizza_survey - Russia",
"count": 3,
"data": [1, 2, 0, 0],
@@ -391,27 +158,7 @@
"breakdown_value": "Russia"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 0,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "Yes",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["true"]
- }
- ]
- }
- },
+ "order": 0,
"label": "pineapple_on_pizza_survey - Slovenia",
"count": 2,
"data": [1, 1, 0, 0],
@@ -420,27 +167,7 @@
"breakdown_value": "Slovenia"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 0,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "Yes",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["true"]
- }
- ]
- }
- },
+ "order": 0,
"label": "pineapple_on_pizza_survey - Switzerland",
"count": 1,
"data": [0, 1, 0, 0],
@@ -449,27 +176,7 @@
"breakdown_value": "Switzerland"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 0,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "Yes",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["true"]
- }
- ]
- }
- },
+ "order": 0,
"label": "pineapple_on_pizza_survey - United Kingdom",
"count": 6,
"data": [1, 2, 1, 2],
@@ -478,27 +185,7 @@
"breakdown_value": "United Kingdom"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 0,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "Yes",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["true"]
- }
- ]
- }
- },
+ "order": 0,
"label": "pineapple_on_pizza_survey - United States",
"count": 14,
"data": [3, 6, 4, 1],
@@ -507,27 +194,7 @@
"breakdown_value": "United States"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - Australia",
"count": 2,
"data": [0, 2, 0, 0],
@@ -536,27 +203,7 @@
"breakdown_value": "Australia"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - Canada",
"count": 1,
"data": [1, 0, 0, 0],
@@ -565,27 +212,7 @@
"breakdown_value": "Canada"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - Denmark",
"count": 3,
"data": [1, 1, 1, 0],
@@ -594,27 +221,7 @@
"breakdown_value": "Denmark"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - Estonia",
"count": 1,
"data": [0, 0, 1, 0],
@@ -623,27 +230,7 @@
"breakdown_value": "Estonia"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - France",
"count": 2,
"data": [2, 0, 0, 0],
@@ -652,27 +239,7 @@
"breakdown_value": "France"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - Germany",
"count": 4,
"data": [2, 1, 1, 0],
@@ -681,27 +248,7 @@
"breakdown_value": "Germany"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - India",
"count": 5,
"data": [0, 2, 1, 2],
@@ -710,27 +257,7 @@
"breakdown_value": "India"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - Ireland",
"count": 1,
"data": [0, 0, 1, 0],
@@ -739,27 +266,7 @@
"breakdown_value": "Ireland"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - Italy",
"count": 2,
"data": [1, 1, 0, 0],
@@ -768,27 +275,7 @@
"breakdown_value": "Italy"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - Netherlands",
"count": 2,
"data": [0, 0, 2, 0],
@@ -797,27 +284,7 @@
"breakdown_value": "Netherlands"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - New Zealand",
"count": 1,
"data": [0, 0, 1, 0],
@@ -826,27 +293,7 @@
"breakdown_value": "New Zealand"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - Nigeria",
"count": 2,
"data": [0, 0, 2, 0],
@@ -855,27 +302,7 @@
"breakdown_value": "Nigeria"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - North Macedonia",
"count": 1,
"data": [1, 0, 0, 0],
@@ -884,27 +311,7 @@
"breakdown_value": "North Macedonia"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - Norway",
"count": 1,
"data": [0, 0, 0, 1],
@@ -913,27 +320,7 @@
"breakdown_value": "Norway"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - Romania",
"count": 1,
"data": [0, 0, 0, 1],
@@ -942,27 +329,7 @@
"breakdown_value": "Romania"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - Spain",
"count": 2,
"data": [0, 1, 1, 0],
@@ -971,27 +338,7 @@
"breakdown_value": "Spain"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - Sweden",
"count": 1,
"data": [1, 0, 0, 0],
@@ -1000,27 +347,7 @@
"breakdown_value": "Sweden"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - Switzerland",
"count": 2,
"data": [1, 1, 0, 0],
@@ -1029,27 +356,7 @@
"breakdown_value": "Switzerland"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - Tunisia",
"count": 1,
"data": [0, 1, 0, 0],
@@ -1058,27 +365,7 @@
"breakdown_value": "Tunisia"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - United Kingdom",
"count": 5,
"data": [1, 2, 1, 1],
@@ -1087,27 +374,7 @@
"breakdown_value": "United Kingdom"
},
{
- "action": {
- "id": "pineapple_on_pizza_survey",
- "type": "events",
- "order": 1,
- "name": "pineapple_on_pizza_survey",
- "custom_name": "No",
- "math": "dau",
- "math_property": null,
- "math_group_type_index": null,
- "properties": {
- "type": "AND",
- "values": [
- {
- "key": "does_pineapple_go_on_pizza",
- "operator": "exact",
- "type": "event",
- "value": ["false"]
- }
- ]
- }
- },
+ "order": 1,
"label": "pineapple_on_pizza_survey - United States",
"count": 4,
"data": [0, 2, 1, 1],
diff --git a/products/exports/backend/tasks/test/test_csv_exporter.py b/products/exports/backend/tasks/test/test_csv_exporter.py
index de8fd6aeaae2..ce3438f4b617 100644
--- a/products/exports/backend/tasks/test/test_csv_exporter.py
+++ b/products/exports/backend/tasks/test/test_csv_exporter.py
@@ -1,5 +1,5 @@
import os
-from collections.abc import Generator
+from collections.abc import Callable, Generator
from datetime import datetime, timedelta
from io import BytesIO
from typing import Any, Optional
@@ -18,6 +18,7 @@ from boto3 import resource
from botocore.client import Config
from dateutil.relativedelta import relativedelta
from openpyxl import load_workbook
+from parameterized import parameterized
from requests.exceptions import HTTPError
from posthog.hogql.constants import CSV_EXPORT_BREAKDOWN_LIMIT_INITIAL
@@ -1350,8 +1351,40 @@ class TestCSVExporter(APIBaseTest):
],
)
+ @parameterized.expand(
+ [
+ (
+ "a series node",
+ "test_event",
+ lambda query: query,
+ ["series,$browser,Total Sum", "test_event,Chrome,1", "test_event,Firefox,1"],
+ ),
+ (
+ "a saved series without a kind",
+ "legacy_event",
+ lambda query: {**query, "series": [{"event": "legacy_event", "name": "legacy_event", "math": "total"}]},
+ ["series,$browser,Total Sum", "legacy_event,Chrome,1", "legacy_event,Firefox,1"],
+ ),
+ (
+ "a renamed series wrapped in its visualization node",
+ "wrapped_event",
+ lambda query: {
+ "kind": "InsightVizNode",
+ "source": {**query, "series": [{**query["series"][0], "custom_name": "Tests"}]},
+ },
+ ["series,custom name,Total Sum", "Chrome,Tests,1", "Firefox,Tests,1"],
+ ),
+ ]
+ )
@patch("products.exports.backend.models.exported_asset.UUIDT")
- def test_csv_exporter_trends_with_breakdown(self, mocked_uuidt: Any) -> None:
+ def test_csv_exporter_trends_with_breakdown(
+ self,
+ _name: str,
+ event: str,
+ shape_source: Callable[[dict[str, Any]], dict[str, Any]],
+ expected_lines: list[str],
+ mocked_uuidt: Any,
+ ) -> None:
with time_machine.travel("2025-05-22T12:00:00.000Z", tick=False):
_create_person(distinct_ids=["user_1"], team=self.team)
_create_person(distinct_ids=["user_2"], team=self.team)
@@ -1359,14 +1392,14 @@ class TestCSVExporter(APIBaseTest):
events_by_person = {
"user_1": [
{
- "event": "test_event",
+ "event": event,
"timestamp": datetime(2025, 5, 22, 13, 0),
"properties": {"$browser": "Chrome"},
},
],
"user_2": [
{
- "event": "test_event",
+ "event": event,
"timestamp": datetime(2025, 5, 22, 13, 1),
"properties": {"$browser": "Firefox"},
},
@@ -1379,21 +1412,23 @@ class TestCSVExporter(APIBaseTest):
team=self.team,
export_format=ExportedAsset.ExportFormat.CSV,
export_context={
- "source": {
- "kind": "TrendsQuery",
- "dateRange": {"date_to": "2025-05-22", "date_from": "2025-05-22"},
- "series": [
- {"kind": "EventsNode", "event": "test_event", "name": "test_event", "math": "total"},
- ],
- "interval": "day",
- "trendsFilter": {
- "display": "ActionsTable",
- },
- "breakdownFilter": {
- "breakdown": "$browser",
- "breakdown_type": "event",
- },
- }
+ "source": shape_source(
+ {
+ "kind": "TrendsQuery",
+ "dateRange": {"date_to": "2025-05-22", "date_from": "2025-05-22"},
+ "series": [
+ {"kind": "EventsNode", "event": event, "name": event, "math": "total"},
+ ],
+ "interval": "day",
+ "trendsFilter": {
+ "display": "ActionsTable",
+ },
+ "breakdownFilter": {
+ "breakdown": "$browser",
+ "breakdown_type": "event",
+ },
+ }
+ )
},
)
exported_asset.save()
@@ -1411,14 +1446,7 @@ class TestCSVExporter(APIBaseTest):
lines = (content or "").strip().split("\r\n")
data_lines = sorted(lines[1:])
- self.assertEqual(
- lines[0:1] + data_lines,
- [
- "series,$browser,Total Sum",
- "test_event,Chrome,1",
- "test_event,Firefox,1",
- ],
- )
+ self.assertEqual(lines[0:1] + data_lines, expected_lines)
@patch("products.exports.backend.models.exported_asset.UUIDT")
def test_csv_exporter_trends_with_breakdown_and_action(self, mocked_uuidt: Any) -> None:
diff --git a/products/exports/backend/tasks/test/test_csv_exporter_renders.py b/products/exports/backend/tasks/test/test_csv_exporter_renders.py
index 22acdd3d5ed1..30d722cba5c3 100644
--- a/products/exports/backend/tasks/test/test_csv_exporter_renders.py
+++ b/products/exports/backend/tasks/test/test_csv_exporter_renders.py
@@ -62,7 +62,7 @@ def test_csv_rendering(
assert csv_rows == fixture["csv_rows"]
if mode == "hogql":
- export_context["source"] = {"some": "query"}
+ export_context["source"] = fixture["response"].get("query", {"some": "query"})
asset.save()
if fixture.get("hogql_response"):
# If HogQL has a different response structure, add it to the fixture as `hogql_response`