Skip to content

Commit

Permalink
feat(hogql-queries): Lifecycle upgrades (#18574)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra authored Nov 14, 2023
1 parent 550bb8d commit 20e7921
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
6 changes: 1 addition & 5 deletions frontend/src/scenes/debug/DebugScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ function QueryDebug({ query, setQuery, queryKey }: QueryDebugProps): JSX.Element
query={query}
setQuery={(query) => setQuery(JSON.stringify(query, null, 2))}
context={{
showQueryEditor:
parsed &&
parsed.kind == 'DataTableNode' &&
parsed.source.kind == 'HogQLQuery' &&
(parsed.full || parsed.showHogQLEditor),
showQueryEditor: true,
}}
/>
)}
Expand Down
22 changes: 18 additions & 4 deletions posthog/hogql_queries/insights/lifecycle_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,29 @@ def __init__(
super().__init__(query, team, timings, in_export_context)

def to_query(self) -> ast.SelectQuery | ast.SelectUnionQuery:
if self.query.samplingFactor == 0:
counts_with_sampling = ast.Constant(value=0)
elif self.query.samplingFactor is not None and self.query.samplingFactor != 1:
counts_with_sampling = parse_expr(
"round(counts * (1 / {sampling_factor}))",
{
"sampling_factor": ast.Constant(value=self.query.samplingFactor),
},
)
else:
counts_with_sampling = parse_expr("counts")

placeholders = {
**self.query_date_range.to_placeholders(),
"events_query": self.events_query,
"periods_query": self.periods_query,
"counts_with_sampling": counts_with_sampling,
}
with self.timings.measure("lifecycle_query"):
lifecycle_query = parse_select(
"""
SELECT groupArray(start_of_period) AS date,
groupArray(counts) AS total,
groupArray({counts_with_sampling}) AS total,
status
FROM (
SELECT
Expand Down Expand Up @@ -161,10 +174,11 @@ def calculate(self) -> LifecycleQueryResponse:
"math": "total",
}
elif isinstance(self.query.series[0], EventsNode):
label = "{} - {}".format(self.query.series[0].event, val[2])
event = self.query.series[0].event
label = "{} - {}".format("All events" if event is None else event, val[2])
action_object = {
"id": self.query.series[0].event,
"name": self.query.series[0].event,
"id": event,
"name": "All events" if event is None else event,
"type": "events",
"order": 0,
"math": "total",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# name: TestLifecycleQueryRunner.test_sampling
'
SELECT groupArray(start_of_period) AS date,
groupArray(counts) AS total,
groupArray(round(multiply(counts, divide(1, 0.1)))) AS total,
status
FROM
(SELECT if(ifNull(equals(status, 'dormant'), 0), negate(sum(counts)), negate(negate(sum(counts)))) AS counts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ def test_lifecycle_trend(self):
.calculate()
.results
)
assert result[0]["label"] == "$pageview - new"

assertLifecycleResults(
result,
Expand All @@ -437,7 +438,7 @@ def test_lifecycle_trend(self):
],
)

def test_lifecycle_trend_any_event(self):
def test_lifecycle_trend_all_events(self):
self._create_events(
event="$pageview",
data=[
Expand Down Expand Up @@ -476,6 +477,8 @@ def test_lifecycle_trend_any_event(self):
.results
)

assert result[0]["label"] == "All events - new"

assertLifecycleResults(
result,
[
Expand Down

0 comments on commit 20e7921

Please sign in to comment.