|
|
|
|
@ -4,27 +4,32 @@ defmodule Plausible.Stats.TableDecider do
|
|
|
|
|
alias Plausible.Stats.Query
|
|
|
|
|
|
|
|
|
|
def partition_metrics(metrics, query) do
|
|
|
|
|
%{event: event_metrics, session: session_metrics, both: both_metrics, other: other_metrics} =
|
|
|
|
|
%{
|
|
|
|
|
event: event_metrics,
|
|
|
|
|
session: session_metrics,
|
|
|
|
|
either: either_metrics,
|
|
|
|
|
other: other_metrics
|
|
|
|
|
} =
|
|
|
|
|
partition(metrics, query, &metric_partitioner/2)
|
|
|
|
|
|
|
|
|
|
%{event: event_filters, session: session_filters, both: _both_filters} =
|
|
|
|
|
%{event: event_filters, session: session_filters, either: _either_filters} =
|
|
|
|
|
partition(query.filters, query, &filters_partitioner/2)
|
|
|
|
|
|
|
|
|
|
cond do
|
|
|
|
|
# Only one table needs to be queried
|
|
|
|
|
empty?(session_metrics) && empty?(session_filters) ->
|
|
|
|
|
{event_metrics ++ both_metrics, [], other_metrics}
|
|
|
|
|
{event_metrics ++ either_metrics, [], other_metrics}
|
|
|
|
|
|
|
|
|
|
empty?(event_metrics) && empty?(event_filters) ->
|
|
|
|
|
{[], session_metrics ++ both_metrics, other_metrics}
|
|
|
|
|
{[], session_metrics ++ either_metrics, other_metrics}
|
|
|
|
|
|
|
|
|
|
# Filters from both, but only one kind of metric
|
|
|
|
|
# Filters from either, but only one kind of metric
|
|
|
|
|
empty?(session_metrics) ->
|
|
|
|
|
{event_metrics ++ both_metrics, [], other_metrics}
|
|
|
|
|
{event_metrics ++ either_metrics, [], other_metrics}
|
|
|
|
|
|
|
|
|
|
# Default: prefer sessions
|
|
|
|
|
true ->
|
|
|
|
|
{event_metrics, session_metrics ++ both_metrics, other_metrics}
|
|
|
|
|
{event_metrics, session_metrics ++ either_metrics, other_metrics}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@ -49,7 +54,7 @@ defmodule Plausible.Stats.TableDecider do
|
|
|
|
|
raise ArgumentError, "Metric #{unknown} not supported without experimental_reduced_joins?"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp metric_partitioner(_, _), do: :both
|
|
|
|
|
defp metric_partitioner(_, _), do: :either
|
|
|
|
|
|
|
|
|
|
defp filters_partitioner(_, {"event:name", _}), do: :event
|
|
|
|
|
defp filters_partitioner(_, {"event:page", _}), do: :event
|
|
|
|
|
@ -65,9 +70,9 @@ defmodule Plausible.Stats.TableDecider do
|
|
|
|
|
raise ArgumentError, "Filter #{unknown} not supported without experimental_reduced_joins?"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp filters_partitioner(_, _), do: :both
|
|
|
|
|
defp filters_partitioner(_, _), do: :either
|
|
|
|
|
|
|
|
|
|
@default %{event: [], session: [], both: [], other: []}
|
|
|
|
|
@default %{event: [], session: [], either: [], other: []}
|
|
|
|
|
defp partition(values, query, partitioner) do
|
|
|
|
|
Enum.reduce(values, @default, fn value, acc ->
|
|
|
|
|
key = partitioner.(query, value)
|
|
|
|
|
|