Add 7 days as an option

This commit is contained in:
Uku Taht 2019-09-03 11:56:42 +01:00
parent dc201f0b57
commit f3985903b3
4 changed files with 22 additions and 4 deletions

View File

@ -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)

View File

@ -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}
_ ->

View File

@ -46,9 +46,10 @@
</div>
<div data-dropdown style="top: 42px; right: 0px; width: 225px;" class="dropdown-content hidden absolute pin-r bg-white border border-grey-light rounded shadow z-10 font-bold text-sm text-grey-darker">
<%= link("Today", to: "/#{@site.domain}?period=day&date=#{Timex.now(@site.timezone) |> Timex.format!("{ISOdate}")}", class: "block px-2 py-3 hover:bg-grey-lighter") %>
<%= link("This month (#{this_month(@site) |> Timex.format!("{Mfull}")})", to: "/#{@site.domain}?period=month&date=#{this_month(@site) |> Timex.format!("{ISOdate}")}", class: "block p-2 hover:bg-grey-lighter") %>
<%= link("Last month (#{last_month(@site) |> Timex.format!("{Mfull}")})", to: "/#{@site.domain}?period=month&date=#{last_month(@site) |> Timex.format!("{ISOdate}")}", class: "block p-2 hover:bg-grey-lighter") %>
<%= link("Today", to: "/#{@site.domain}?period=day", class: "block p-2 hover:bg-grey-lighter") %>
<%= link("Last 7 days", to: "/#{@site.domain}?period=7d", class: "block px-2 py-3 hover:bg-grey-lighter") %>
<%= link("This month", to: "/#{@site.domain}?period=month", class: "block p-2 hover:bg-grey-lighter") %>
<%= link("Last month", to: "/#{@site.domain}?period=month&date=#{last_month(@site) |> Timex.format!("{ISOdate}")}", class: "block p-2 hover:bg-grey-lighter") %>
<%= link("Last 3 months", to: "/#{@site.domain}?period=3mo", class: "block p-2 hover:bg-grey-lighter") %>
<%= link("Last 6 months", to: "/#{@site.domain}?period=6mo", class: "block p-2 hover:bg-grey-lighter") %>
</div>

View File

@ -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"