From f3985903b3239dd9d93fd6ad1e8f9418daaf7602 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Tue, 3 Sep 2019 11:56:42 +0100 Subject: [PATCH] Add 7 days as an option --- lib/plausible/stats/query.ex | 11 +++++++++++ lib/plausible_web/controllers/stats_controller.ex | 2 +- lib/plausible_web/templates/stats/stats.html.eex | 7 ++++--- lib/plausible_web/views/stats_view.ex | 6 ++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/plausible/stats/query.ex b/lib/plausible/stats/query.ex index d9d8b80ac..5d596b8d8 100644 --- a/lib/plausible/stats/query.ex +++ b/lib/plausible/stats/query.ex @@ -51,6 +51,17 @@ defmodule Plausible.Stats.Query do } end + def from(tz, %{"period" => "7d"}) do + end_date = today(tz) + start_date = end_date |> Timex.shift(days: -7) + + %__MODULE__{ + period: "7d", + date_range: Date.range(start_date, end_date), + step_type: "date" + } + end + def from(_tz, %{"period" => "month", "date" => month_start}) do start_date = Date.from_iso8601!(month_start) |> Timex.beginning_of_month end_date = Timex.end_of_month(start_date) diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.ex index 907a0f802..aeb90ea3a 100644 --- a/lib/plausible_web/controllers/stats_controller.ex +++ b/lib/plausible_web/controllers/stats_controller.ex @@ -308,7 +308,7 @@ defmodule PlausibleWeb.StatsController do defp fetch_period(conn, site) do case conn.params["period"] do - p when p in ["day", "month", "3mo", "6mo"] -> + p when p in ["day", "month", "7d", "3mo", "6mo"] -> saved_periods = get_session(conn, :saved_periods) || %{} {put_session(conn, :saved_periods, Map.merge(saved_periods, %{site.domain => p})), conn.params} _ -> diff --git a/lib/plausible_web/templates/stats/stats.html.eex b/lib/plausible_web/templates/stats/stats.html.eex index 653b7d8b8..9c519d6d0 100644 --- a/lib/plausible_web/templates/stats/stats.html.eex +++ b/lib/plausible_web/templates/stats/stats.html.eex @@ -46,9 +46,10 @@ diff --git a/lib/plausible_web/views/stats_view.ex b/lib/plausible_web/views/stats_view.ex index d9544ba5d..36e89294d 100644 --- a/lib/plausible_web/views/stats_view.ex +++ b/lib/plausible_web/views/stats_view.ex @@ -36,6 +36,8 @@ defmodule PlausibleWeb.StatsView do "on #{Timex.format!(query.date_range.first, "{Mfull} {D}")}" "month" -> "in #{Timex.format!(query.date_range.first, "{Mfull} {YYYY}")}" + "7d" -> + "in the last 7 days" "3mo" -> "in the last 3 months" "6mo" -> @@ -51,6 +53,8 @@ defmodule PlausibleWeb.StatsView do "month" -> date = Date.to_iso8601(query.date_range.first) "?period=month&date=#{date}" + "7d" -> + "?period=7d" "3mo" -> "?period=3mo" "6mo" -> @@ -86,6 +90,8 @@ defmodule PlausibleWeb.StatsView do else Timex.format!(query.date_range.first, "{Mfull} {YYYY}") end + "7d" -> + "Last 7 days" "day" -> if query.date_range.first == today(site) do "Today"