Make is not filter work

This commit is contained in:
Uku Taht 2021-08-16 10:39:13 +03:00
parent 62c5bfcbf8
commit 79f6756ba2
4 changed files with 69 additions and 54 deletions

View File

@ -29,7 +29,17 @@ function clearAllFilters(history, query) {
);
}
function filterText(key, value, query) {
function filterType(val) {
if (val.startsWith('!')) {
return ['is not', val.substr(1)]
}
return ['is', val]
}
function filterText(key, rawValue, query) {
const [type, value] = filterType(rawValue)
if (key === "goal") {
return <>Completed goal <b>{value}</b></>
}
@ -38,51 +48,24 @@ function filterText(key, value, query) {
const eventName = query.filters.goal ? query.filters.goal : 'event'
return <>{eventName}.{metaKey} is <b>{metaValue}</b></>
}
if (key === "source") {
return <>Source: <b>{value}</b></>
}
if (key === "utm_medium") {
return <>UTM medium: <b>{value}</b></>
}
if (key === "utm_source") {
return <>UTM source: <b>{value}</b></>
}
if (key === "utm_campaign") {
return <>UTM campaign: <b>{value}</b></>
}
if (key === "referrer") {
return <>Referrer: <b>{value}</b></>
}
if (key === "screen") {
return <>Screen size: <b>{value}</b></>
}
if (key === "browser") {
return <>Browser: <b>{value}</b></>
}
if (key === "browser_version") {
const browserName = query.filters.browser ? query.filters.browser : 'Browser'
return <>{browserName}.Version: <b>{value}</b></>
}
if (key === "os") {
return <>Operating System: <b>{value}</b></>
return <>{browserName}.Version {type} <b>{value}</b></>
}
if (key === "os_version") {
const osName = query.filters.os ? query.filters.os : 'OS'
return <>{osName}.Version: <b>{value}</b></>
return <>{osName}.Version {type} <b>{value}</b></>
}
if (key === "country") {
const allCountries = Datamap.prototype.worldTopo.objects.world.geometries;
const selectedCountry = allCountries.find((c) => c.id === value) || {properties: {name: value}};
return <>Country: <b>{selectedCountry.properties.name}</b></>
return <>Country {type} <b>{selectedCountry.properties.name}</b></>
}
if (key === "page") {
return <>Page: <b>{value}</b></>
}
if (key === "entry_page") {
return <>Entry Page: <b>{value}</b></>
}
if (key === "exit_page") {
return <>Exit Page: <b>{value}</b></>
const formattedFilter = formattedFilters[key]
if (formattedFilter) {
return <>{formattedFilter} {type} <b>{value}</b></>
}
throw new Error(`Unknown filter: ${key}`)

View File

@ -98,7 +98,10 @@ defmodule Plausible.Stats.Aggregate do
{where_clause, where_arg} =
case query.filters["event:page"] do
{:is, page} ->
{"p=?", page}
{"p = ?", page}
{:is_not, page} ->
{"p != ?", page}
{:matches, expr} ->
regex = page_regex(expr)

View File

@ -38,6 +38,9 @@ defmodule Plausible.Stats.Base do
{:is, page} ->
from(e in q, where: e.pathname == ^page)
{:is_not, page} ->
from(e in q, where: e.pathname != ^page)
{:matches, glob_expr} ->
regex = page_regex(glob_expr)
from(e in q, where: fragment("match(?, ?)", e.pathname, ^regex))
@ -45,15 +48,19 @@ defmodule Plausible.Stats.Base do
{:member, list} ->
from(e in q, where: e.pathname in ^list)
_ ->
nil ->
q
_ ->
raise "Unknown filter type"
end
q =
case query.filters["event:name"] do
{:is, name} -> from(e in q, where: e.name == ^name)
{:member, list} -> from(e in q, where: e.name in ^list)
_ -> q
nil -> q
_ -> raise "Unknown filter type"
end
q =
@ -64,8 +71,11 @@ defmodule Plausible.Stats.Base do
{:is, :event, event} ->
from(e in q, where: e.name == ^event)
_ ->
nil ->
q
_ ->
raise "Unknown goal type"
end
Enum.reduce(query.filters, q, fn {filter_key, filter_value}, query ->
@ -116,6 +126,9 @@ defmodule Plausible.Stats.Base do
{:is, page} ->
from(e in sessions_q, where: e.entry_page == ^page)
{:is_not, page} ->
from(e in sessions_q, where: e.entry_page != ^page)
{:matches, glob_expr} ->
regex = page_regex(glob_expr)
from(s in sessions_q, where: fragment("match(?, ?)", s.entry_page, ^regex))
@ -123,8 +136,11 @@ defmodule Plausible.Stats.Base do
{:member, list} ->
from(e in sessions_q, where: e.entry_page in ^list)
_ ->
nil ->
sessions_q
_ ->
raise "Unknown filter type"
end
Enum.reduce(Filters.visit_props(), sessions_q, fn prop_name, sessions_q ->
@ -148,8 +164,11 @@ defmodule Plausible.Stats.Base do
fragment_data = [{String.to_existing_atom(prop_name), {:in, list}}]
from(s in sessions_q, where: fragment(^fragment_data))
_ ->
nil ->
sessions_q
_ ->
raise "Unknown filter type"
end
end)
end

View File

@ -28,21 +28,15 @@ defmodule Plausible.Stats.Filters do
Enum.reduce(query.filters, %{}, fn {name, val}, new_filters ->
cond do
name == "country" ->
new_val = Plausible.Stats.CountryName.to_alpha2(val)
Map.put(new_filters, "visit:country", {:is, new_val})
name == "page" ->
if String.match?(val, ~r/\*/) do
Map.put(new_filters, "event:page", {:matches, val})
else
Map.put(new_filters, "event:page", {:is, val})
end
{filter_type, filter_val} = filter_value(name, val)
new_val = Plausible.Stats.CountryName.to_alpha2(filter_val)
Map.put(new_filters, "visit:country", {filter_type, new_val})
name in (@visit_props ++ ["goal"]) ->
Map.put(new_filters, "visit:" <> name, {:is, val})
Map.put(new_filters, "visit:" <> name, filter_value(name, val))
name in @event_props ->
Map.put(new_filters, "event:" <> name, {:is, val})
Map.put(new_filters, "event:" <> name, filter_value(name, val))
name == "props" ->
Enum.reduce(val, new_filters, fn {prop_key, prop_val}, new_filters ->
@ -50,10 +44,26 @@ defmodule Plausible.Stats.Filters do
end)
true ->
Map.put(new_filters, name, {:is, val})
raise "Unknown filter prop"
end
end)
%Plausible.Stats.Query{query | filters: new_filters}
end
defp filter_value(key, "!" <> val) do
if String.contains?(key, "page") && String.match?(val, ~r/\*/) do
{:does_not_match, val}
else
{:is_not, val}
end
end
defp filter_value(key, val) do
if String.contains?(key, "page") && String.match?(val, ~r/\*/) do
{:matches, val}
else
{:is, val}
end
end
end