Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into apiv2-timeseries-re…
Browse files Browse the repository at this point in the history
…moval
  • Loading branch information
macobo committed Jul 3, 2024
2 parents 7dd2eff + 05ac840 commit 00360a7
Show file tree
Hide file tree
Showing 12 changed files with 529 additions and 227 deletions.
2 changes: 1 addition & 1 deletion assets/js/dashboard/stats/behaviours/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default function Properties(props) {
BUILD_EXTRA && { name: 'total_revenue', label: 'Revenue', hiddenOnMobile: true },
BUILD_EXTRA && { name: 'average_revenue', label: 'Average', hiddenOnMobile: true }
]}
detailsLink={`/custom-prop-values/${propKey}`}
detailsLink={url.sitePath(`custom-prop-values/${propKey}`)}
maybeHideDetails={true}
query={query}
color="bg-red-50"
Expand Down
2 changes: 1 addition & 1 deletion assets/js/dashboard/util/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function apiPath(site, path = '') {
}

export function sitePath(path = '') {
return `/${path}` + window.location.search
return (path.startsWith('/') ? path : '/' + path) + window.location.search
}

export function setQuery(key, value) {
Expand Down
57 changes: 0 additions & 57 deletions extra/lib/plausible_web/authorize_sites_api.ex

This file was deleted.

68 changes: 34 additions & 34 deletions lib/plausible/stats/sql/expression.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Plausible.Stats.SQL.Expression do

defmacrop field_or_blank_value(key, expr, empty_value) do
quote do
wrap_expression([t], %{
wrap_alias([t], %{
unquote(key) =>
fragment("if(empty(?), ?, ?)", unquote(expr), unquote(empty_value), unquote(expr))
})
Expand All @@ -40,13 +40,13 @@ defmodule Plausible.Stats.SQL.Expression do
end

def dimension(key, "time:month", _table, query) do
wrap_expression([t], %{
wrap_alias([t], %{
key => fragment("toStartOfMonth(toTimeZone(?, ?))", t.timestamp, ^query.timezone)
})
end

def dimension(key, "time:week", _table, query) do
wrap_expression([t], %{
wrap_alias([t], %{
key =>
weekstart_not_before(
to_timezone(t.timestamp, ^query.timezone),
Expand All @@ -56,19 +56,19 @@ defmodule Plausible.Stats.SQL.Expression do
end

def dimension(key, "time:day", _table, query) do
wrap_expression([t], %{
wrap_alias([t], %{
key => fragment("toDate(toTimeZone(?, ?))", t.timestamp, ^query.timezone)
})
end

def dimension(key, "time:hour", :sessions, query) do
wrap_expression([s], %{
wrap_alias([s], %{
key => regular_time_slots(query, 3600)
})
end

def dimension(key, "time:hour", _table, query) do
wrap_expression([t], %{
wrap_alias([t], %{
key => fragment("toStartOfHour(toTimeZone(?, ?))", t.timestamp, ^query.timezone)
})
end
Expand All @@ -77,7 +77,7 @@ defmodule Plausible.Stats.SQL.Expression do
def dimension(key, "time:minute", :sessions, %Query{
period: "30m"
}) do
wrap_expression([s], %{
wrap_alias([s], %{
key =>
fragment(
"arrayJoin(range(dateDiff('minute', now(), ?), dateDiff('minute', now(), ?) + 1))",
Expand All @@ -89,36 +89,36 @@ defmodule Plausible.Stats.SQL.Expression do

# :NOTE: This is not exposed in Query APIv2
def dimension(key, "time:minute", _table, %Query{period: "30m"}) do
wrap_expression([t], %{
wrap_alias([t], %{
key => fragment("dateDiff('minute', now(), ?)", t.timestamp)
})
end

# :NOTE: This is not exposed in Query APIv2
def dimension(key, "time:minute", :sessions, query) do
wrap_expression([s], %{
wrap_alias([s], %{
key => regular_time_slots(query, 60)
})
end

# :NOTE: This is not exposed in Query APIv2
def dimension(key, "time:minute", _table, query) do
wrap_expression([t], %{
wrap_alias([t], %{
key => fragment("toStartOfMinute(toTimeZone(?, ?))", t.timestamp, ^query.timezone)
})
end

def dimension(key, "event:name", _table, _query),
do: wrap_expression([t], %{key => t.name})
do: wrap_alias([t], %{key => t.name})

def dimension(key, "event:page", _table, _query),
do: wrap_expression([t], %{key => t.pathname})
do: wrap_alias([t], %{key => t.pathname})

def dimension(key, "event:hostname", _table, _query),
do: wrap_expression([t], %{key => t.hostname})
do: wrap_alias([t], %{key => t.hostname})

def dimension(key, "event:props:" <> property_name, _table, _query) do
wrap_expression([t], %{
wrap_alias([t], %{
key =>
fragment(
"if(not empty(?), ?, '(none)')",
Expand All @@ -129,10 +129,10 @@ defmodule Plausible.Stats.SQL.Expression do
end

def dimension(key, "visit:entry_page", _table, _query),
do: wrap_expression([t], %{key => t.entry_page})
do: wrap_alias([t], %{key => t.entry_page})

def dimension(key, "visit:exit_page", _table, _query),
do: wrap_expression([t], %{key => t.exit_page})
do: wrap_alias([t], %{key => t.exit_page})

def dimension(key, "visit:utm_medium", _table, _query),
do: field_or_blank_value(key, t.utm_medium, @not_set)
Expand Down Expand Up @@ -171,42 +171,42 @@ defmodule Plausible.Stats.SQL.Expression do
do: field_or_blank_value(key, t.browser_version, @not_set)

def dimension(key, "visit:country", _table, _query),
do: wrap_expression([t], %{key => t.country})
do: wrap_alias([t], %{key => t.country})

def dimension(key, "visit:region", _table, _query),
do: wrap_expression([t], %{key => t.region})
do: wrap_alias([t], %{key => t.region})

def dimension(key, "visit:city", _table, _query),
do: wrap_expression([t], %{key => t.city})
do: wrap_alias([t], %{key => t.city})

def event_metric(:pageviews) do
wrap_expression([e], %{
wrap_alias([e], %{
pageviews:
fragment("toUInt64(round(countIf(? = 'pageview') * any(_sample_factor)))", e.name)
})
end

def event_metric(:events) do
wrap_expression([], %{
wrap_alias([], %{
events: fragment("toUInt64(round(count(*) * any(_sample_factor)))")
})
end

def event_metric(:visitors) do
wrap_expression([e], %{
wrap_alias([e], %{
visitors: fragment("toUInt64(round(uniq(?) * any(_sample_factor)))", e.user_id)
})
end

def event_metric(:visits) do
wrap_expression([e], %{
wrap_alias([e], %{
visits: fragment("toUInt64(round(uniq(?) * any(_sample_factor)))", e.session_id)
})
end

on_ee do
def event_metric(:total_revenue) do
wrap_expression(
wrap_alias(
[e],
%{
total_revenue:
Expand All @@ -216,7 +216,7 @@ defmodule Plausible.Stats.SQL.Expression do
end

def event_metric(:average_revenue) do
wrap_expression(
wrap_alias(
[e],
%{
average_revenue:
Expand All @@ -227,7 +227,7 @@ defmodule Plausible.Stats.SQL.Expression do
end

def event_metric(:sample_percent) do
wrap_expression([], %{
wrap_alias([], %{
sample_percent:
fragment("if(any(_sample_factor) > 1, round(100 / any(_sample_factor)), 100)")
})
Expand All @@ -245,7 +245,7 @@ defmodule Plausible.Stats.SQL.Expression do
event_page_filter = Query.get_filter(query, "event:page")
condition = SQL.WhereBuilder.build_condition(:entry_page, event_page_filter)

wrap_expression([], %{
wrap_alias([], %{
bounce_rate:
fragment(
"toUInt32(ifNotFinite(round(sumIf(is_bounce * sign, ?) / sumIf(sign, ?) * 100), 0))",
Expand All @@ -257,40 +257,40 @@ defmodule Plausible.Stats.SQL.Expression do
end

def session_metric(:visits, _query) do
wrap_expression([s], %{
wrap_alias([s], %{
visits: fragment("toUInt64(round(sum(?) * any(_sample_factor)))", s.sign)
})
end

def session_metric(:pageviews, _query) do
wrap_expression([s], %{
wrap_alias([s], %{
pageviews:
fragment("toUInt64(round(sum(? * ?) * any(_sample_factor)))", s.sign, s.pageviews)
})
end

def session_metric(:events, _query) do
wrap_expression([s], %{
wrap_alias([s], %{
events: fragment("toUInt64(round(sum(? * ?) * any(_sample_factor)))", s.sign, s.events)
})
end

def session_metric(:visitors, _query) do
wrap_expression([s], %{
wrap_alias([s], %{
visitors: fragment("toUInt64(round(uniq(?) * any(_sample_factor)))", s.user_id)
})
end

def session_metric(:visit_duration, _query) do
wrap_expression([], %{
wrap_alias([], %{
visit_duration:
fragment("toUInt32(ifNotFinite(round(sum(duration * sign) / sum(sign)), 0))"),
__internal_visits: fragment("toUInt32(sum(sign))")
})
end

def session_metric(:views_per_visit, _query) do
wrap_expression([s], %{
wrap_alias([s], %{
views_per_visit:
fragment(
"ifNotFinite(round(sum(? * ?) / sum(?), 2), 0)",
Expand All @@ -303,7 +303,7 @@ defmodule Plausible.Stats.SQL.Expression do
end

def session_metric(:sample_percent, _query) do
wrap_expression([], %{
wrap_alias([], %{
sample_percent:
fragment("if(any(_sample_factor) > 1, round(100 / any(_sample_factor)), 100)")
})
Expand Down
20 changes: 16 additions & 4 deletions lib/plausible/stats/sql/fragments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ defmodule Plausible.Stats.SQL.Fragments do
### Examples
iex> wrap_expression([t], %{ foo: t.column }) |> expand_macro_once
iex> wrap_alias([t], %{ foo: t.column }) |> expand_macro_once
"%{foo: dynamic([t], selected_as(t.column, :foo))}"
"""
defmacro wrap_expression(binding, map_literal) do
defmacro wrap_alias(binding, map_literal) do
update_literal_map_values(map_literal, fn {key, expr} ->
key_expr =
if Macro.quoted_literal?(key) do
Expand All @@ -171,11 +171,11 @@ defmodule Plausible.Stats.SQL.Fragments do
### Examples
iex> select_merge_as(q, [t], %{ foo: t.column }) |> expand_macro_once
"select_merge(q, [], ^wrap_expression([t], %{foo: t.column}))"
"select_merge(q, [], ^wrap_alias([t], %{foo: t.column}))"
"""
defmacro select_merge_as(q, binding, map_literal) do
quote do
select_merge(unquote(q), [], ^wrap_expression(unquote(binding), unquote(map_literal)))
select_merge(unquote(q), [], ^wrap_alias(unquote(binding), unquote(map_literal)))
end
end

Expand Down Expand Up @@ -205,4 +205,16 @@ defmodule Plausible.Stats.SQL.Fragments do
end)
end
end

defp update_literal_map_values({:%{}, ctx, keyword_list}, mapper_fn) do
{
:%{},
ctx,
Enum.map(keyword_list, fn {key, expr} ->
{key, mapper_fn.({key, expr})}
end)
}
end

defp update_literal_map_values(ast, _), do: ast
end
4 changes: 2 additions & 2 deletions lib/plausible/stats/sql/special_metrics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ defmodule Plausible.Stats.SQL.SpecialMetrics do
defp total_visitors_subquery(site, query, include_imported)

defp total_visitors_subquery(site, query, true = _include_imported) do
wrap_expression([], %{
wrap_alias([], %{
total_visitors:
subquery(total_visitors(site, query)) +
subquery(Plausible.Stats.Imported.total_imported_visitors(site, query))
})
end

defp total_visitors_subquery(site, query, false = _include_imported) do
wrap_expression([], %{
wrap_alias([], %{
total_visitors: subquery(total_visitors(site, query))
})
end
Expand Down
Loading

0 comments on commit 00360a7

Please sign in to comment.