-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand segments in queries #4982
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a6390d7
Refactor segments model
apata ec5ae0a
Fix inconsistent code
apata 8876a2a
Remove superfluous error case
apata 8676123
Beautify Plausible.Segments module
apata 39998c9
Expand segments in filters
apata 56df03f
Add tests
apata fb2fbaf
Generate types
apata 0027bcc
Remove extraneous newlines
apata d25d088
Move Segment filters logic away from QueryParser
apata 85a98d6
Add moduledoc
apata 9b8abb2
Add tests for /v2/query-internal-test
apata 363b4b7
Refactor max segment filters count to module attribute
apata a7d61d6
Add more parser tests and unify asserts in query
apata 2cd949b
Merge branch 'master' into saved-segments/expand-segments-in-query
apata File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| defmodule Plausible.Segments.Filters do | ||
| @moduledoc """ | ||
| This module contains functions that enable resolving segments in filters. | ||
| """ | ||
| alias Plausible.Segments | ||
| alias Plausible.Stats.Filters | ||
|
|
||
| @max_segment_filters_count 10 | ||
|
|
||
| @doc """ | ||
| Finds unique segment IDs used in query filters. | ||
|
|
||
| ## Examples | ||
| iex> get_segment_ids([[:not, [:is, "segment", [10, 20]]], [:contains, "visit:entry_page", ["blog"]]]) | ||
| {:ok, [10, 20]} | ||
|
|
||
| iex> get_segment_ids([[:and, [[:is, "segment", Enum.to_list(1..6)], [:is, "segment", Enum.to_list(1..6)]]]]) | ||
| {:error, "Invalid filters. You can only use up to 10 segment filters in a query."} | ||
| """ | ||
| def get_segment_ids(filters) do | ||
| ids = | ||
| filters | ||
| |> Filters.traverse() | ||
| |> Enum.flat_map(fn | ||
| {[_operation, "segment", clauses], _depth} -> clauses | ||
| _ -> [] | ||
| end) | ||
|
|
||
| if length(ids) > @max_segment_filters_count do | ||
| {:error, | ||
| "Invalid filters. You can only use up to #{@max_segment_filters_count} segment filters in a query."} | ||
| else | ||
| {:ok, Enum.uniq(ids)} | ||
| end | ||
| end | ||
|
|
||
| def preload_needed_segments(%Plausible.Site{} = site, filters) do | ||
| with {:ok, segment_ids} <- get_segment_ids(filters), | ||
| {:ok, segments} <- | ||
| Segments.get_many( | ||
| site, | ||
| segment_ids, | ||
| fields: [:id, :segment_data] | ||
| ), | ||
| {:ok, segments_by_id} <- | ||
| {:ok, | ||
| Enum.into( | ||
| segments, | ||
| %{}, | ||
| fn %Segments.Segment{id: id, segment_data: segment_data} -> | ||
| case Filters.QueryParser.parse_filters(segment_data["filters"]) do | ||
| {:ok, filters} -> {id, filters} | ||
| _ -> {id, nil} | ||
| end | ||
| end | ||
| )}, | ||
| :ok <- | ||
| if(Enum.any?(segment_ids, fn id -> is_nil(Map.get(segments_by_id, id)) end), | ||
| do: {:error, "Invalid filters. Some segments don't exist or aren't accessible."}, | ||
| else: :ok | ||
| ) do | ||
| {:ok, segments_by_id} | ||
| end | ||
| end | ||
|
|
||
| defp replace_segment_with_filter_tree([_, "segment", clauses], preloaded_segments) do | ||
| if length(clauses) === 1 do | ||
| [[:and, Map.get(preloaded_segments, Enum.at(clauses, 0))]] | ||
| else | ||
| [[:or, Enum.map(clauses, fn id -> [:and, Map.get(preloaded_segments, id)] end)]] | ||
| end | ||
| end | ||
|
|
||
| defp replace_segment_with_filter_tree(_filter, _preloaded_segments) do | ||
| nil | ||
| end | ||
|
|
||
| @doc """ | ||
| ## Examples | ||
|
|
||
| iex> resolve_segments([[:is, "visit:entry_page", ["/home"]]], %{}) | ||
| {:ok, [[:is, "visit:entry_page", ["/home"]]]} | ||
|
|
||
| iex> resolve_segments([[:is, "visit:entry_page", ["/home"]], [:is, "segment", [1]]], %{1 => [[:contains, "visit:entry_page", ["blog"]], [:is, "visit:country", ["PL"]]]}) | ||
| {:ok, [ | ||
| [:is, "visit:entry_page", ["/home"]], | ||
| [:and, [[:contains, "visit:entry_page", ["blog"]], [:is, "visit:country", ["PL"]]]] | ||
| ]} | ||
|
|
||
| iex> resolve_segments([[:is, "segment", [1, 2]]], %{1 => [[:contains, "event:goal", ["Singup"]], [:is, "visit:country", ["PL"]]], 2 => [[:contains, "event:goal", ["Sauna"]], [:is, "visit:country", ["EE"]]]}) | ||
| {:ok, [ | ||
| [:or, [ | ||
| [:and, [[:contains, "event:goal", ["Singup"]], [:is, "visit:country", ["PL"]]]], | ||
| [:and, [[:contains, "event:goal", ["Sauna"]], [:is, "visit:country", ["EE"]]]]] | ||
| ] | ||
| ]} | ||
| """ | ||
| def resolve_segments(original_filters, preloaded_segments) do | ||
| if map_size(preloaded_segments) > 0 do | ||
| {:ok, | ||
| Filters.transform_filters(original_filters, fn f -> | ||
| replace_segment_with_filter_tree(f, preloaded_segments) | ||
| end)} | ||
| else | ||
| {:ok, original_filters} | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| defmodule Plausible.Segments.FiltersTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| doctest Plausible.Segments.Filters, import: true | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Annoyingly, usages of this as-is is going to cause annoying conflicts with #4980.
To reduce headaches depending on the order of the merge I suggest copying the implementation from that PR and maybe adding a default to state/state_transformer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we cross that bridge when we come to it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup - just wanted to point out an annoyance you might run into trying to get this merged.