diff --git a/Makefile b/Makefile index 9531ba721d61ef8196897ca00352f7d8230e154d..6f60a3c2854b82962b71983f77220a67424403b3 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 88d64042678029092722bb8fd5c7213de82d97da..4bcd8c18861d3e75fca7378f8b47444f59e73621 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)}) } </div> <div className="py-1 border-b border-gray-200 dark:border-gray-500 date-option-group"> - {this.renderLink("12mo", "Last 12 months")} {this.renderLink("year", "Year to Date")} + {this.renderLink("12mo", "Last 12 months")} </div> <div className="py-1 date-option-group"> {this.renderLink("all", "All time")} diff --git a/assets/js/dashboard/util/date.js b/assets/js/dashboard/util/date.js index bb913f0cf7545bb6f523f70142e43ecf30a31615..17771009b7c69b4837906d50fdeec6d3459f023f 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 67f1b2fb89864e75ef8fc068d23039ab9642ac34..3cf2efabeb3947314e71d840d026de439e1147a4 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 df13edf0b3ac04c3fd61cf2bd4baa0d0769b861c..fe259dbf831597d8575b50b8bcc2ed470fb66bf1 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