Fix realtime dashboard

This commit is contained in:
Uku Taht 2021-08-19 15:41:08 +03:00
parent bea26f0707
commit 0fe053e106
3 changed files with 22 additions and 5 deletions

View File

@ -41,7 +41,7 @@ defmodule Plausible.Stats.Query do
date = today(tz)
%__MODULE__{
period: "30m",
period: "realtime",
interval: "minute",
date_range: Date.range(date, date),
filters: parse_filters(params),

View File

@ -15,7 +15,14 @@ defmodule PlausibleWeb.Api.StatsController do
_ -> query
end
timeseries = Task.async(fn -> Stats.timeseries(site, query, ["visitors"]) end)
timeseries_query =
if query.period == "realtime" do
%Query{query | period: "30m"}
else
query
end
timeseries = Task.async(fn -> Stats.timeseries(site, timeseries_query, ["visitors"]) end)
{top_stats, sample_percent} = fetch_top_stats(site, query)
timeseries_result = Task.await(timeseries)
@ -62,11 +69,13 @@ defmodule PlausibleWeb.Api.StatsController do
end
end
defp fetch_top_stats(site, %Query{period: "30m"} = query) do
defp fetch_top_stats(site, %Query{period: "realtime"} = query) do
query_30m = %Query{query | period: "30m"}
%{
"visitors" => %{"value" => visitors},
"pageviews" => %{"value" => pageviews}
} = Stats.aggregate(site, query, ["visitors", "pageviews"])
} = Stats.aggregate(site, query_30m, ["visitors", "pageviews"])
stats = [
%{
@ -482,6 +491,14 @@ defmodule PlausibleWeb.Api.StatsController do
def conversions(conn, params) do
site = conn.assigns[:site]
query = Query.from(site.timezone, params) |> Filters.add_prefix()
query =
if query.period == "realtime" do
%Query{query | period: "30m"}
else
query
end
pagination = parse_pagination(params)
total_q = Query.remove_goal(query)

View File

@ -25,7 +25,7 @@ defmodule Plausible.Stats.QueryTest do
assert q.date_range.first == Timex.today()
assert q.date_range.last == Timex.today()
assert q.period == "30m"
assert q.period == "realtime"
end
test "parses month format" do