Sort screen sizes by count (#269)

* Sort screen sizes by count

Sorting by a fixed list does make quick referencing slightly easier, but having them ordered looks much nicer, as the other tabs are also ordered.

* Add missing comma
This commit is contained in:
Jake Howard 2020-08-05 13:44:04 +01:00 committed by GitHub
parent c812cafcfc
commit 5e7044fe2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -422,20 +422,14 @@ defmodule Plausible.Stats.Clickhouse do
end)
end
@available_screen_sizes ["Desktop", "Laptop", "Tablet", "Mobile"]
def top_screen_sizes(site, query) do
Clickhouse.all(
from e in base_query(site, query),
select: {fragment("? as name", e.screen_size), fragment("uniq(user_id) as count")},
group_by: e.screen_size,
where: e.screen_size != ""
where: e.screen_size != "",
order_by: [desc: fragment("count")]
)
|> Enum.sort(fn %{"name" => screen_size1}, %{"name" => screen_size2} ->
index1 = Enum.find_index(@available_screen_sizes, fn s -> s == screen_size1 end)
index2 = Enum.find_index(@available_screen_sizes, fn s -> s == screen_size2 end)
index2 > index1
end)
|> add_percentages
end