Skip to content

Commit

Permalink
Apply location filters from API
Browse files Browse the repository at this point in the history
This makes it consistent with the sources etc which filter out 'Direct /
None' on the API side. These filters are used by both the native and
imported data handling code, which would otherwise both duplicate the
filters in their `where` clauses.
  • Loading branch information
m-col committed Jan 7, 2022
1 parent ce83d67 commit cfd27b2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
3 changes: 0 additions & 3 deletions lib/plausible/stats/breakdown.ex
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ defmodule Plausible.Stats.Breakdown do
from(
s in q,
group_by: s.country_code,
where: s.country_code != "\0\0",
select_merge: %{country: s.country_code}
)
end
Expand All @@ -402,7 +401,6 @@ defmodule Plausible.Stats.Breakdown do
from(
s in q,
group_by: s.subdivision1_code,
where: s.subdivision1_code != "",
select_merge: %{region: s.subdivision1_code}
)
end
Expand All @@ -411,7 +409,6 @@ defmodule Plausible.Stats.Breakdown do
from(
s in q,
group_by: s.city_geoname_id,
where: s.city_geoname_id != 0,
select_merge: %{city: s.city_geoname_id}
)
end
Expand Down
8 changes: 2 additions & 6 deletions lib/plausible/stats/imported.ex
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,10 @@ defmodule Plausible.Stats.Imported do
imported_q |> select_merge([i], %{country: i.country})

:region ->
imported_q
|> select_merge([i], %{region: i.region})
|> where([i], i.region != "")
imported_q |> select_merge([i], %{region: i.region})

:city ->
imported_q
|> select_merge([i], %{city: i.city})
|> where([i], i.city != 0)
imported_q |> select_merge([i], %{city: i.city})

:device ->
imported_q |> select_merge([i], %{device: i.device})
Expand Down
21 changes: 18 additions & 3 deletions lib/plausible_web/controllers/api/stats_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,12 @@ defmodule PlausibleWeb.Api.StatsController do

def countries(conn, params) do
site = conn.assigns[:site]
query = Query.from(site.timezone, params) |> Filters.add_prefix()

query =
Query.from(site.timezone, params)
|> Filters.add_prefix()
|> Query.put_filter("visit:country", {:is_not, "\0\0"})

pagination = parse_pagination(params)

countries =
Expand Down Expand Up @@ -614,7 +619,12 @@ defmodule PlausibleWeb.Api.StatsController do

def regions(conn, params) do
site = conn.assigns[:site]
query = Query.from(site.timezone, params) |> Filters.add_prefix()

query =
Query.from(site.timezone, params)
|> Filters.add_prefix()
|> Query.put_filter("visit:region", {:is_not, ""})

pagination = parse_pagination(params)

regions =
Expand Down Expand Up @@ -646,7 +656,12 @@ defmodule PlausibleWeb.Api.StatsController do

def cities(conn, params) do
site = conn.assigns[:site]
query = Query.from(site.timezone, params) |> Filters.add_prefix()

query =
Query.from(site.timezone, params)
|> Filters.add_prefix()
|> Query.put_filter("visit:city", {:is_not, 0})

pagination = parse_pagination(params)

cities =
Expand Down

0 comments on commit cfd27b2

Please sign in to comment.