diff --git a/Makefile b/Makefile index 9531ba721..6f60a3c28 100644 --- a/Makefile +++ b/Makefile @@ -24,5 +24,5 @@ dummy_event: -H 'X-Forwarded-For: 127.0.0.1' \ -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.284' \ -H 'Content-Type: text/plain' \ - --data-binary '{"n":"pageview","u":"http://dummy.site/very-long-link-this-is-going-to-take-a-while-to-read-very-long-indeed-yes","d":"plausible.io","r":null,"w":1666}' \ + --data-binary '{"n":"pageview","u":"http://dummy.site/very-long-link-this-is-going-to-take-a-while-to-read-very-long-indeed-yes","d":"new-site.com","r":null,"w":1666}' \ --compressed diff --git a/assets/js/dashboard/datepicker.js b/assets/js/dashboard/datepicker.js index 88d640426..4bcd8c188 100644 --- a/assets/js/dashboard/datepicker.js +++ b/assets/js/dashboard/datepicker.js @@ -340,8 +340,8 @@ class DatePicker extends React.Component { { this.renderLink('month', 'Last month', {date: lastMonth(this.props.site)}) }
- {this.renderLink("12mo", "Last 12 months")} {this.renderLink("year", "Year to Date")} + {this.renderLink("12mo", "Last 12 months")}
{this.renderLink("all", "All time")} diff --git a/assets/js/dashboard/util/date.js b/assets/js/dashboard/util/date.js index bb913f0cf..17771009b 100644 --- a/assets/js/dashboard/util/date.js +++ b/assets/js/dashboard/util/date.js @@ -41,7 +41,7 @@ export function formatYear(date) { } export function formatDay(date) { - var weekday = DAYS_ABBREV[date.getUTCDay()]; + var weekday = DAYS_ABBREV[date.getDay()]; if (date.getFullYear() !== (new Date()).getFullYear()) { return `${weekday}, ${date.getDate()} ${formatMonthShort(date)} ${date.getFullYear()}`; } else { diff --git a/lib/plausible/stats/query.ex b/lib/plausible/stats/query.ex index 67f1b2fb8..3cf2efabe 100644 --- a/lib/plausible/stats/query.ex +++ b/lib/plausible/stats/query.ex @@ -187,24 +187,43 @@ defmodule Plausible.Stats.Query do end def from(site, %{"period" => "all"} = params) do - end_date = - today(site.timezone) - |> Timex.end_of_month() - start_date = site.inserted_at |> Timex.Timezone.convert("UTC") |> Timex.Timezone.convert(site.timezone) - |> Timex.beginning_of_month() + |> Timex.to_date() - %__MODULE__{ - period: "all", - date_range: Date.range(start_date, end_date), - interval: Map.get(params, "interval", "month"), - filters: parse_filters(params), - sample_threshold: Map.get(params, "sample_threshold", @default_sample_threshold) - } - |> maybe_include_imported(site, params) + now = today(site.timezone) + + cond do + Timex.diff(now, start_date, :months) > 0 -> + from( + site, + Map.merge(params, %{ + "period" => "custom", + "from" => Date.to_iso8601(start_date), + "to" => Date.to_iso8601(now), + "interval" => "month" + }) + ) + |> Map.put(:period, "all") + + Timex.diff(now, start_date, :days) > 0 -> + from( + site, + Map.merge(params, %{ + "period" => "custom", + "from" => Date.to_iso8601(start_date), + "to" => Date.to_iso8601(now), + "interval" => "date" + }) + ) + |> Map.put(:period, "all") + + true -> + from(site, Map.merge(params, %{"period" => "day", "date" => "today"})) + |> Map.put(:period, "all") + end end def from(site, %{"period" => "custom", "from" => from, "to" => to} = params) do diff --git a/test/plausible/stats/query_test.exs b/test/plausible/stats/query_test.exs index df13edf0b..fe259dbf8 100644 --- a/test/plausible/stats/query_test.exs +++ b/test/plausible/stats/query_test.exs @@ -85,8 +85,38 @@ defmodule Plausible.Stats.QueryTest do site = Map.put(@site, :timezone, "America/Cancun") q = Query.from(site, %{"period" => "all"}) - assert q.date_range.first == ~D[2019-12-01] - assert q.date_range.last == Timex.today("America/Cancun") |> Timex.end_of_month() + assert q.date_range.first == ~D[2019-12-31] + assert q.date_range.last == Timex.today("America/Cancun") + end + + test "all time shows hourly if site is completely new" do + site = Map.put(@site, :inserted_at, Timex.now()) + q = Query.from(site, %{"period" => "all"}) + + assert q.date_range.first == Timex.today() + assert q.date_range.last == Timex.today() + assert q.period == "all" + assert q.interval == "hour" + end + + test "all time shows daily if site is more than a day old" do + site = Map.put(@site, :inserted_at, Timex.now() |> Timex.shift(days: -1)) + q = Query.from(site, %{"period" => "all"}) + + assert q.date_range.first == Timex.today() |> Timex.shift(days: -1) + assert q.date_range.last == Timex.today() + assert q.period == "all" + assert q.interval == "date" + end + + test "all time shows monthly if site is more than a month old" do + site = Map.put(@site, :inserted_at, Timex.now() |> Timex.shift(months: -1)) + q = Query.from(site, %{"period" => "all"}) + + assert q.date_range.first == Timex.today() |> Timex.shift(months: -1) + assert q.date_range.last == Timex.today() + assert q.period == "all" + assert q.interval == "month" end test "defaults to 30 days format" do