Skip to content

Commit

Permalink
add sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Mar 12, 2024
1 parent 487064c commit 565d4a9
Showing 1 changed file with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions lib/plausible/exports.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Plausible.Exports do

# NOTE: needs tests

require Plausible
import Ecto.Query

@doc """
Expand All @@ -29,6 +30,16 @@ defmodule Plausible.Exports do
}
end

Plausible.on_full_build do
defp sampled(table) do
Plausible.Stats.Sampling.add_query_hint(from(table))
end
else
defp sampled(table) do
table
end
end

defmacrop date(timestamp) do
quote do
selected_as(fragment("toDate(?)", unquote(timestamp)), :date)
Expand Down Expand Up @@ -71,9 +82,7 @@ defmodule Plausible.Exports do
@spec export_visitors_q(pos_integer) :: Ecto.Query.t()
def export_visitors_q(site_id) do
visitors_sessions_q =
from s in "sessions_v2",
# NOTE: no smapling is used right now
# hints: ["SAMPLE", unsafe_fragment(^@sample)],
from s in sampled("sessions_v2"),
where: s.site_id == ^site_id,
group_by: selected_as(:date),
select: %{
Expand All @@ -86,9 +95,7 @@ defmodule Plausible.Exports do
}

visitors_events_q =
from e in "events_v2",
# NOTE: no smapling is used right now
# hints: ["SAMPLE", unsafe_fragment(^@sample)],
from e in sampled("events_v2"),
where: e.site_id == ^site_id,
group_by: selected_as(:date),
select: %{
Expand Down Expand Up @@ -122,9 +129,7 @@ defmodule Plausible.Exports do

@spec export_sources_q(pos_integer) :: Ecto.Query.t()
def export_sources_q(site_id) do
from s in "sessions_v2",
# NOTE: no smapling is used right now
# hints: ["SAMPLE", unsafe_fragment(^@sample)],
from s in sampled("sessions_v2"),
where: s.site_id == ^site_id,
group_by: [
selected_as(:date),
Expand Down Expand Up @@ -152,9 +157,7 @@ defmodule Plausible.Exports do
@spec export_pages_q(pos_integer) :: Ecto.Query.t()
def export_pages_q(site_id) do
window_q =
from e in "events_v2",
# NOTE: no smapling is used right now
# hints: ["SAMPLE", unsafe_fragment(^@sample)],
from e in sampled("events_v2"),
where: e.site_id == ^site_id,
select: %{
timestamp: e.timestamp,
Expand Down Expand Up @@ -197,9 +200,7 @@ defmodule Plausible.Exports do

@spec export_entry_pages_q(pos_integer) :: Ecto.Query.t()
def export_entry_pages_q(site_id) do
from s in "sessions_v2",
# NOTE: no smapling is used right now
# hints: ["SAMPLE", unsafe_fragment(^@sample)],
from s in sampled("sessions_v2"),
where: s.site_id == ^site_id,
group_by: [selected_as(:date), s.entry_page],
order_by: selected_as(:date),
Expand All @@ -218,9 +219,7 @@ defmodule Plausible.Exports do

@spec export_exit_pages_q(pos_integer) :: Ecto.Query.t()
def export_exit_pages_q(site_id) do
from s in "sessions_v2",
# NOTE: no smapling is used right now
# hints: ["SAMPLE", unsafe_fragment(^@sample)],
from s in sampled("sessions_v2"),
where: s.site_id == ^site_id,
group_by: [selected_as(:date), s.exit_page],
order_by: selected_as(:date),
Expand All @@ -237,9 +236,7 @@ defmodule Plausible.Exports do

@spec export_locations_q(pos_integer) :: Ecto.Query.t()
def export_locations_q(site_id) do
from s in "sessions_v2",
# NOTE: no smapling is used right now
# hints: ["SAMPLE", unsafe_fragment(^@sample)],
from s in sampled("sessions_v2"),
where: s.site_id == ^site_id,
where: s.city_geoname_id != 0 and s.country_code != "\0\0" and s.country_code != "ZZ",
group_by: [selected_as(:date), s.country_code, selected_as(:region), s.city_geoname_id],
Expand All @@ -258,9 +255,7 @@ defmodule Plausible.Exports do

@spec export_devices_q(pos_integer) :: Ecto.Query.t()
def export_devices_q(site_id) do
from s in "sessions_v2",
# NOTE: no smapling is used right now
# hints: ["SAMPLE", unsafe_fragment(^@sample)],
from s in sampled("sessions_v2"),
where: s.site_id == ^site_id,
group_by: [selected_as(:date), s.screen_size],
order_by: selected_as(:date),
Expand All @@ -276,9 +271,7 @@ defmodule Plausible.Exports do

@spec export_browsers_q(pos_integer) :: Ecto.Query.t()
def export_browsers_q(site_id) do
from s in "sessions_v2",
# NOTE: no smapling is used right now
# hints: ["SAMPLE", unsafe_fragment(^@sample)],
from s in sampled("sessions_v2"),
where: s.site_id == ^site_id,
group_by: [selected_as(:date), s.browser],
order_by: selected_as(:date),
Expand All @@ -294,9 +287,7 @@ defmodule Plausible.Exports do

@spec export_operating_systems_q(pos_integer) :: Ecto.Query.t()
def export_operating_systems_q(site_id) do
from s in "sessions_v2",
# NOTE: no smapling is used right now
# hints: ["SAMPLE", unsafe_fragment(^@sample)],
from s in sampled("sessions_v2"),
where: s.site_id == ^site_id,
group_by: [selected_as(:date), s.operating_system],
order_by: selected_as(:date),
Expand Down

0 comments on commit 565d4a9

Please sign in to comment.