Skip to content

Commit

Permalink
Add limit to clickhouse sql events (#4740)
Browse files Browse the repository at this point in the history
* add limit to clickhouse sql event

* fix limit sql string

* added test for limit parameter

Co-authored-by: Buddy Williams <buddy@posthog.com>
  • Loading branch information
liyiy and buwilliams authored Jun 14, 2021
1 parent 8d8b868 commit 2aa1edc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
14 changes: 10 additions & 4 deletions ee/clickhouse/views/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def _get_people(self, query_result: List[Dict], team: Team) -> Dict[str, Any]:
def _query_events_list(
self, filter: Filter, team: Team, request: Request, long_date_from: bool = False, limit: int = 100
) -> List:
limit_sql = f"LIMIT {limit + 1}"
limit += 1
limit_sql = "LIMIT %(limit)s"
conditions, condition_params = determine_event_conditions(
team,
{
Expand All @@ -72,17 +73,22 @@ def _query_events_list(
if prop_filters != "":
return sync_execute(
SELECT_EVENT_WITH_PROP_SQL.format(conditions=conditions, limit=limit_sql, filters=prop_filters),
{"team_id": team.pk, **condition_params, **prop_filter_params},
{"team_id": team.pk, "limit": limit, **condition_params, **prop_filter_params},
)
else:
return sync_execute(
SELECT_EVENT_WITH_ARRAY_PROPS_SQL.format(conditions=conditions, limit=limit_sql),
{"team_id": team.pk, **condition_params},
{"team_id": team.pk, "limit": limit, **condition_params},
)

def list(self, request: Request, *args: Any, **kwargs: Any) -> Response:
is_csv_request = self.request.accepted_renderer.format == "csv"
limit = self.CSV_EXPORT_LIMIT if is_csv_request else 100
if is_csv_request:
limit = self.CSV_EXPORT_LIMIT
elif self.request.GET.get("limit", None):
limit = int(self.request.GET.get("limit")) # type: ignore
else:
limit = 100

team = self.team
filter = Filter(request=request)
Expand Down
28 changes: 28 additions & 0 deletions posthog/api/test/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,34 @@ def test_get_event_by_id(self):
response = self.client.get(f"/api/event/im_a_string_not_an_integer",)
self.assertIn(response.status_code, [status.HTTP_404_NOT_FOUND, status.HTTP_400_BAD_REQUEST])

def test_limit(self):
person_factory(
properties={"email": "tim@posthog.com"},
team=self.team,
distinct_ids=["2", "some-random-uid"],
is_identified=True,
)

event_factory(
event="$autocapture",
team=self.team,
distinct_id="2",
properties={"$ip": "8.8.8.8"},
elements=[Element(tag_name="button", text="something"), Element(tag_name="div")],
)
event_factory(
event="$pageview", team=self.team, distinct_id="some-random-uid", properties={"$ip": "8.8.8.8"}
)
event_factory(
event="$pageview", team=self.team, distinct_id="some-other-one", properties={"$ip": "8.8.8.8"}
)

response = self.client.get("/api/event/?limit=1").json()
self.assertEqual(1, len(response["results"]))

response = self.client.get("/api/event/?limit=2").json()
self.assertEqual(2, len(response["results"]))

return TestEvents


Expand Down

0 comments on commit 2aa1edc

Please sign in to comment.