From 5631367aefa934345b7341e31d3a7d3607c54647 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Wed, 8 Jan 2020 11:23:03 +0200 Subject: [PATCH] Add 'last year' instead of '6 months' --- assets/js/dashboard/datepicker.js | 9 ++++++--- assets/js/dashboard/query.js | 8 +++++--- assets/js/dashboard/stats/visitor-graph.js | 6 ++++-- lib/plausible/stats/query.ex | 20 ++++++++++++++++---- test/plausible/stats/query_test.exs | 16 ++++++++++++---- 5 files changed, 43 insertions(+), 16 deletions(-) diff --git a/assets/js/dashboard/datepicker.js b/assets/js/dashboard/datepicker.js index 38957dc20..133a0c49e 100644 --- a/assets/js/dashboard/datepicker.js +++ b/assets/js/dashboard/datepicker.js @@ -65,12 +65,14 @@ class DatePicker extends React.Component { return 'Last 7 days' } else if (query.period === '30d') { return 'Last 30 days' + } else if (query.period === '60d') { + return 'Last 60 days' } else if (query.period === 'month') { return formatMonthYYYY(query.date) } else if (query.period === '3mo') { return 'Last 3 months' - } else if (query.period === '6mo') { - return 'Last 6 months' + } else if (query.period === '12mo') { + return 'Last year' } } @@ -121,8 +123,9 @@ class DatePicker extends React.Component { Today Last 7 days Last 30 days + Last 60 days Last 3 months - Last 6 months + Last year ) diff --git a/assets/js/dashboard/query.js b/assets/js/dashboard/query.js index 7a07d3df2..0b0ac2a40 100644 --- a/assets/js/dashboard/query.js +++ b/assets/js/dashboard/query.js @@ -1,6 +1,6 @@ import {formatDay, formatMonthYYYY, nowInOffset, parseUTCDate} from './date' -const PERIODS = ['day', 'month', '7d', '30d', '3mo', '6mo'] +const PERIODS = ['day', 'month', '7d', '30d', '60d', '3mo', '12mo'] export function parseQuery(querystring, site) { const q = new URLSearchParams(querystring) @@ -35,10 +35,12 @@ export function toHuman(query) { return 'in the last 7 days' } else if (query.period === '30d') { return 'in the last 30 days' + } else if (query.period === '60d') { + return 'in the last 60 days' } else if (query.period === '3mo') { return 'in the last 3 months' - } else if (query.period === '6mo') { - return 'in the last 6 months' + } else if (query.period === '12mo') { + return 'in the last year' } } diff --git a/assets/js/dashboard/stats/visitor-graph.js b/assets/js/dashboard/stats/visitor-graph.js index a18578a5c..729076727 100644 --- a/assets/js/dashboard/stats/visitor-graph.js +++ b/assets/js/dashboard/stats/visitor-graph.js @@ -247,10 +247,12 @@ class LineGraph extends React.Component { return 'last week' } else if (query.period === '30d') { return 'last month' + } else if (query.period === '60d') { + return 'prev 60 days' } else if (query.period === '3mo') { return 'prev 3 months' - } else if (query.period === '6mo') { - return 'prev 6 months' + } else if (query.period === '12mo') { + return 'last year' } } diff --git a/lib/plausible/stats/query.ex b/lib/plausible/stats/query.ex index a1a4b094e..2e9deb6b0 100644 --- a/lib/plausible/stats/query.ex +++ b/lib/plausible/stats/query.ex @@ -59,6 +59,18 @@ defmodule Plausible.Stats.Query do } end + def from(tz, %{"period" => "60d"} = params) do + end_date = today(tz) + start_date = end_date |> Timex.shift(days: -60) + + %__MODULE__{ + period: "60d", + date_range: Date.range(start_date, end_date), + step_type: "date", + filters: parse_filters(params) + } + end + def from(_tz, %{"period" => "month", "date" => date} = params) do start_date = Date.from_iso8601!(date) |> Timex.beginning_of_month end_date = Timex.end_of_month(start_date) @@ -85,15 +97,15 @@ defmodule Plausible.Stats.Query do } end - def from(tz, %{"period" => "6mo"} = params) do - start_date = Timex.shift(today(tz), months: -5) + def from(tz, %{"period" => "12mo"} = params) do + start_date = Timex.shift(today(tz), months: -11) |> Timex.beginning_of_month() %__MODULE__{ - period: "6mo", + period: "12mo", date_range: Date.range(start_date, today(tz)), step_type: "month", - steps: 6, + steps: 12, filters: parse_filters(params) } end diff --git a/test/plausible/stats/query_test.exs b/test/plausible/stats/query_test.exs index 96dc2b42f..fbd662709 100644 --- a/test/plausible/stats/query_test.exs +++ b/test/plausible/stats/query_test.exs @@ -20,7 +20,6 @@ defmodule Plausible.Stats.QueryTest do assert q.step_type == "hour" end - test "parses month format" do q = Query.from(@tz, %{"period" => "month", "date" => "2019-01-01"}) @@ -37,10 +36,10 @@ defmodule Plausible.Stats.QueryTest do assert q.step_type == "month" end - test "parses 6 month format" do - q = Query.from(@tz, %{"period" => "6mo"}) + test "parses 12 month format" do + q = Query.from(@tz, %{"period" => "12mo"}) - assert q.date_range.first == Timex.shift(Timex.today(), months: -5) |> Timex.beginning_of_month() + assert q.date_range.first == Timex.shift(Timex.today(), months: -11) |> Timex.beginning_of_month() assert q.date_range.last == Timex.today() assert q.step_type == "month" end @@ -49,6 +48,15 @@ defmodule Plausible.Stats.QueryTest do assert Query.from(@tz, %{}) == Query.from(@tz, %{"period" => "30d"}) end + test "parses 60d format" do + q = Query.from(@tz, %{"period" => "60d"}) + + assert q.date_range.last == Timex.today() + assert Timex.diff(q.date_range.last, q.date_range.first, :days) == 60 + assert q.step_type == "date" + end + + describe "filters" do test "parses goal filter" do filters = Jason.encode!(%{"goal" => "Signup"})