More datepicker improvements

pull/1811/head
Uku Taht 5 years ago
parent f9ab9ef980
commit d40faf6ec7

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

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

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

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

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

Loading…
Cancel
Save