Skip to content
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

APIv2: Replace breakdown module with QueryBuilder #4283

Open
wants to merge 45 commits into
base: master
Choose a base branch
from

Conversation

macobo
Copy link
Contributor

@macobo macobo commented Jun 27, 2024

Changes

This PR starts using the new QueryBuilder in breakdown module with a compatibility layer for the old response schema.

The following changes also happen as a side-effect:

  • bounce_rate is reported as 0 not nil when site is not entry page
  • revenue metrics are handled in breakdown module. When added to apiv2 it would be wise to include currency in response and add validation errors when site does not have access to revenue
  • time_on_page is a special case within the breakdown module.
  • imports.ex gets significantly changed (to remove old compatibility hacks)
  • QueryOptimizer also changes event:page to visit:entry_page filter when doing a breakdown by event:page and querying session metrics. Required for imports.
  • Garbage filters (which pass validation) cause query to return false
  • Breakdown module default ordering subtly changes, causing changes in some tests.
  • Simplify query building around order_by

Depends on #4251

@macobo macobo force-pushed the apiv2-breakdown-module-remove branch from 2638495 to 41bf62e Compare June 27, 2024 08:06
@macobo macobo force-pushed the apiv2-breakdown-module-remove branch from 4724dfe to 763d449 Compare June 27, 2024 08:12
@macobo macobo marked this pull request as ready for review June 27, 2024 08:26
@macobo macobo requested a review from ukutaht June 27, 2024 08:26
@macobo macobo force-pushed the apiv2-breakdown-module-remove branch 2 times, most recently from 5137a74 to f49ccbd Compare June 27, 2024 09:04
@macobo macobo changed the title APIv2: Use QueryBuilder in breakdown module. APIv2: Replace breakdown module with QueryBuilder Jun 27, 2024
lib/plausible/stats/breakdown.ex Outdated Show resolved Hide resolved
defp result_key("event:props:" <> custom_property), do: custom_property
defp result_key("event:" <> key), do: key |> String.to_atom()
defp result_key("visit:" <> key), do: key |> String.to_atom()
defp result_key(dimension), do: dimension

defp maybe_add_time_on_page(event_results, site, query, metrics) do
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this means we won't be able to order by time_on_page?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. If needed, I think we can restore this but given we have a time_on_page project in plans I can live with this limitation.

@@ -150,6 +152,15 @@ defmodule Plausible.Stats.Filters.WhereBuilder do
true
end

defp add_filter(table, _query, filter) do
Logger.info("Unable to process garbage filter. No results are returned",
Copy link
Contributor

Choose a reason for hiding this comment

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

At first glance I'm not a fan of silently accepting/ignoring garbage. Would it make sense to error out the whole query here due to invalid filter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not a fan either but there's an existing test that tested against sentry spam when security scanners are run against our API: test/plausible_web/controllers/api/stats_controller/conversions_test.exs:211.

Not sure if this is indeed the best solution - do you recommend keeping this, removing the test or something else.

Copy link
Contributor

Choose a reason for hiding this comment

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

do you recommend keeping this, removing the test or something else.

Can we make it a 400 Bad Request rather than 200 OK?

lib/plausible/stats/query_result.ex Outdated Show resolved Hide resolved
Enum.reduce(query.order_by || [], q, &build_order_by(&2, query, &1, mode))
q
|> select_merge(^%{key => Expression.dimension(dimension, query, key)})
|> group_by([], selected_as(^key))
Copy link
Contributor

Choose a reason for hiding this comment

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

nice 👍

@macobo macobo added the deploy-to-staging Special label that triggers a deploy to a staging environment label Jun 27, 2024
Base automatically changed from apiv2-aggregates-tests to master June 28, 2024 05:59
@macobo macobo force-pushed the apiv2-breakdown-module-remove branch from 139e3ee to eac23c8 Compare June 28, 2024 06:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deploy-to-staging Special label that triggers a deploy to a staging environment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants