Skip to content

fix: elements api fixes #31067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 10, 2025
Merged

fix: elements api fixes #31067

merged 16 commits into from
Apr 10, 2025

Conversation

pauldambra
Copy link
Member

@pauldambra pauldambra commented Apr 10, 2025

consider me nerd-sniped

has a set of changes from revisiting this after a number of years

2025-04-10 16 46 23

@@ -141,7 +141,10 @@ export const heatmapToolbarMenuLogic = kea<heatmapToolbarMenuLogicType>([
date_to: values.commonFilters.date_to,
}

defaultUrl = `/api/element/stats/${encodeParams({ ...params, paginate_response: true }, '?')}`
defaultUrl = `/api/element/stats/${encodeParams(
{ ...params, paginate_response: true, sampling_factor: 0.1 },
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sampling was still in beta at posthog when we added sampling to elements API, let's allow the client to control it (but default to on)

checking in housewatch, switch to sampling factor isn't significantly faster or slower and is at least a consistent approach

raise ValidationError("offset must be an integer")

events_filter = self._events_filter(request)
timer = ServerTimingsGathered()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i "know" this is slow
let's prove it

date_params.update(date_to_params)

try:
limit = int(request.query_params.get("limit", 10_000))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raising the page limit doesn't seem to slow the query down massively so 🤷

# when multiple includes are sent expects them as separate parameters
# e.g. ?include=a&include=b
events_to_include = request.query_params.getlist("include", [])
def _events_filter(self, request) -> tuple[Literal["$autocapture", "$rageclick", "$dead_click"], ...]:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simplify and add dead click support

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

This PR enhances the elements API with performance optimizations and improved timing metrics.

  • Added configurable sampling factor (0.1) to element stats API calls in heatmapToolbarMenuLogic to reduce backend processing load
  • Added support for $dead_click event type alongside existing $autocapture and $rageclick events
  • Implemented a reusable ServerTimingsGathered context manager for tracking and reporting server-side timing metrics
  • Modified SQL query to correctly extrapolate element counts when sampling is used (count() * %(sampling_factor)s)
  • Increased default element limit from 250 to 10,000 for more comprehensive data retrieval

5 file(s) reviewed, 3 comment(s)
Edit PR Review Bot Settings | Greptile

@@ -1,8 +1,8 @@
GET_ELEMENTS = """
SELECT
elements_chain, count() as count, event as event_type
elements_chain, count() * %(sampling_factor)s as count, event as event_type
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should apparently always have been adjusting this count 😱

https://clickhouse.com/docs/sql-reference/statements/select/sample#sample-k

@@ -48,7 +48,6 @@ const EDITABLE_INSTANCE_SETTINGS = [
'RATE_LIMITING_ALLOW_LIST_TEAMS',
'SENTRY_AUTH_TOKEN',
'SENTRY_ORGANIZATION',
'HEATMAP_SAMPLE_N',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to support self hosted config here any more

pauldambra and others added 2 commits April 10, 2025 16:10
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Copy link
Contributor

github-actions bot commented Apr 10, 2025

Size Change: +1.82 kB (+0.01%)

Total Size: 13.2 MB

Filename Size Change
frontend/dist/toolbar.js 13.2 MB +1.82 kB (+0.01%)

compressed-size-action

@pauldambra pauldambra requested review from a team, Copilot, sortafreel and veryayskiy and removed request for a team April 10, 2025 15:41
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 9 out of 10 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • .cursor/rules/react-typescript.mdc: Language not supported

@pauldambra pauldambra merged commit 677e623 into master Apr 10, 2025
110 checks passed
@pauldambra pauldambra deleted the fix/element_stats_fangling branch April 10, 2025 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants