Compare commits

...

7 Commits

@ -45,7 +45,13 @@ export function serializeQuery(query, extraQuery=[]) {
if (query.filters) { queryObj.filters = serializeFilters(query.filters) }
if (query.with_imported) { queryObj.with_imported = query.with_imported }
if (SHARED_LINK_AUTH) { queryObj.auth = SHARED_LINK_AUTH }
if (query.comparison) { queryObj.comparison = query.comparison }
if (query.comparison) {
queryObj.comparison = query.comparison
queryObj.compare_from = query.compare_from
queryObj.compare_to = query.compare_to
}
Object.assign(queryObj, ...extraQuery)
return '?' + serialize(queryObj)

@ -1,15 +1,18 @@
import React, { Fragment } from 'react'
import { withRouter } from "react-router-dom";
import { withRouter } from 'react-router-dom'
import { navigateToQuery } from './query'
import { Menu, Transition } from '@headlessui/react'
import { ChevronDownIcon } from '@heroicons/react/20/solid'
import classNames from 'classnames'
import * as storage from './util/storage'
import Flatpickr from 'react-flatpickr'
import { formatISO, parseUTCDate, formatDayShort } from './util/date.js'
const COMPARISON_MODES = {
'off': 'Disable comparison',
'previous_period': 'Previous period',
'year_over_year': 'Year over year',
'custom': 'Custom period',
}
const DEFAULT_COMPARISON_MODE = 'previous_period'
@ -26,6 +29,7 @@ export const getStoredComparisonMode = function(domain) {
}
const storeComparisonMode = function(domain, mode) {
if (mode == "custom") return
storage.setItem(`comparison_mode__${domain}`, mode)
}
@ -49,17 +53,30 @@ export const toggleComparisons = function(history, query, site) {
}
}
function DropdownItem({ label, value, isCurrentlySelected, updateMode }) {
function DropdownItem({ label, value, isCurrentlySelected, updateMode, calendar }) {
const click = () => {
if (value == "custom") {
// https://github.com/flatpickr/flatpickr/issues/399#issuecomment-260007013
setTimeout(() => calendar.current.flatpickr.open(), 100)
} else {
updateMode(value)
}
}
const render = ({ active }) => {
const buttonClass = classNames("px-4 py-2 w-full text-left font-medium text-sm dark:text-white cursor-pointer", {
"bg-gray-100 text-gray-900 dark:bg-gray-900 dark:text-gray-100": active,
"font-bold": isCurrentlySelected,
})
return <button className={buttonClass}>{ label }</button>
}
const disabled = isCurrentlySelected && value !== "custom"
return (
<Menu.Item
key={value}
onClick={() => updateMode(value)}
disabled={isCurrentlySelected}>
{({ active }) => (
<button className={classNames("px-4 py-2 w-full text-left font-medium text-sm dark:text-white cursor-pointer", { "bg-gray-100 text-gray-900 dark:bg-gray-900 dark:text-gray-100": active, "font-bold": isCurrentlySelected })}>
{ label }
</button>
)}
<Menu.Item key={value} onClick={click} disabled={disabled}>
{ render }
</Menu.Item>
)
}
@ -69,19 +86,42 @@ const ComparisonInput = function({ site, query, history }) {
if (COMPARISON_DISABLED_PERIODS.includes(query.period)) return null
if (!isComparisonEnabled(query.comparison)) return null
const updateMode = (key) => {
storeComparisonMode(site.domain, key)
navigateToQuery(history, query, { comparison: key })
const updateMode = (mode, from = null, to = null) => {
storeComparisonMode(site.domain, mode)
navigateToQuery(history, query, { comparison: mode, compare_from: from, compare_to: to })
}
const buildLabel = (query) => {
if (query.comparison == "custom") {
const from = parseUTCDate(query.compare_from)
const to = parseUTCDate(query.compare_to)
return `${formatDayShort(from, false)} - ${formatDayShort(to, false)}`
} else {
return COMPARISON_MODES[query.comparison]
}
}
const calendar = React.useRef(null)
const flatpickrOptions = {
mode: 'range',
showMonths: 1,
maxDate: 'today',
animate: true,
static: true,
onChange: ([from, to]) => updateMode("custom", formatISO(from), formatISO(to))
}
return (
<>
<span className="h-0 w-0 invisible">
<Flatpickr ref={calendar} options={flatpickrOptions} />
</span>
<span className="pl-2 text-sm font-medium text-gray-800 dark:text-gray-200">vs.</span>
<div className="flex">
<div className="min-w-32 md:w-52 md:relative">
<div className="min-w-32 md:w-48 md:relative">
<Menu as="div" className="relative inline-block pl-2 w-full">
<Menu.Button className="bg-white text-gray-800 text-xs md:text-sm font-medium dark:bg-gray-800 dark:hover:bg-gray-900 dark:text-gray-200 hover:bg-gray-200 flex md:px-3 px-2 py-2 items-center justify-between leading-tight rounded shadow cursor-pointer w-full truncate">
<span className="truncate">{ COMPARISON_MODES[query.comparison] || 'Compare to' }</span>
<span className="truncate">{ buildLabel(query) }</span>
<ChevronDownIcon className="hidden sm:inline-block h-4 w-4 md:h-5 md:w-5 text-gray-500 ml-2" aria-hidden="true" />
</Menu.Button>
<Transition
@ -93,7 +133,7 @@ const ComparisonInput = function({ site, query, history }) {
leaveFrom="transform opacity-100 scale-100"
leaveTo="transform opacity-0 scale-95">
<Menu.Items className="py-1 text-left origin-top-right absolute right-0 mt-2 w-56 rounded-md shadow-lg bg-white dark:bg-gray-800 ring-1 ring-black ring-opacity-5 focus:outline-none z-10" static>
{ Object.keys(COMPARISON_MODES).map((key) => DropdownItem({ label: COMPARISON_MODES[key], value: key, isCurrentlySelected: key == query.comparison, updateMode })) }
{ Object.keys(COMPARISON_MODES).map((key) => DropdownItem({ label: COMPARISON_MODES[key], value: key, isCurrentlySelected: key == query.comparison, updateMode, calendar })) }
</Menu.Items>
</Transition>
</Menu>

@ -25,6 +25,8 @@ export function parseQuery(querystring, site) {
return {
period,
comparison,
compare_from: q.get('compare_from'),
compare_to: q.get('compare_to'),
date: q.get('date') ? parseUTCDate(q.get('date')) : nowForSite(site),
from: q.get('from') ? parseUTCDate(q.get('from')) : undefined,
to: q.get('to') ? parseUTCDate(q.get('to')) : undefined,

@ -40,14 +40,14 @@ export const LoadingState = {
isLoadedOrRefreshing: function (state) { return [this.loaded, this.refreshing].includes(state) }
}
const buildComparisonDataset = function(comparisonPlot, presentIndex) {
const buildComparisonDataset = function(comparisonPlot, _presentIndex) {
if (!comparisonPlot) return []
let data = [...comparisonPlot]
if (presentIndex) {
const dashedPartIncludedIndex = presentIndex + 1
data = data.slice(0, dashedPartIncludedIndex)
}
// if (presentIndex) {
// const dashedPartIncludedIndex = presentIndex + 1
// data = data.slice(0, dashedPartIncludedIndex)
// }
return [{
data: data,

@ -40,6 +40,10 @@ export function formatYear(date) {
return `Year of ${date.getFullYear()}`;
}
export function formatYearShort(date) {
return date.getUTCFullYear().toString().substring(2)
}
export function formatDay(date) {
var weekday = DAYS_ABBREV[date.getDay()];
if (date.getFullYear() !== (new Date()).getFullYear()) {
@ -49,8 +53,13 @@ export function formatDay(date) {
}
}
export function formatDayShort(date) {
return `${date.getDate()} ${formatMonthShort(date)}`;
export function formatDayShort(date, includeYear = false) {
let formatted = `${date.getDate()} ${formatMonthShort(date)}`
if (includeYear) {
formatted += ` ${formatYearShort(date)}`
}
return formatted
}
export function parseUTCDate(dateString) {

@ -9,32 +9,54 @@ defmodule Plausible.Stats.Comparisons do
alias Plausible.Stats
@modes ~w(previous_period year_over_year)
@modes ~w(previous_period year_over_year custom)
@disallowed_periods ~w(realtime all)
@type mode() :: String.t() | nil
@typep option() :: {:from, String.t()} | {:to, String.t()} | {:now, NaiveDateTime.t()}
@spec compare(
Plausible.Site.t(),
Stats.Query.t(),
mode(),
NaiveDateTime.t() | nil
) :: {:ok, Stats.Query.t()} | {:error, :not_supported}
def compare(
%Plausible.Site{} = site,
%Stats.Query{} = source_query,
mode,
now \\ nil
) do
@spec compare(Plausible.Site.t(), Stats.Query.t(), mode(), [option()]) ::
{:ok, Stats.Query.t()} | {:error, :not_supported}
@doc """
Generates a comparison query based on the source query and comparison mode.
The mode parameter specifies the type of comparison and can be one of the
following:
* `"previous_period"` - shifts back the query by the same number of days the
source query has.
* `"year_over_year"` - shifts back the query by 1 year.
* `"custom"` - compares the query using a custom start date defined in
`opts`. The end date of the comparison query is inferred using the same
numbers of days the original query had. For example, if the graph displays
data for seven days, and the user selects a custom comparison starting on
May 7th, the end date would be automatically set to May 13th.
The comparison query returned by the function has its end date restricted to
the current day. This can be overriden by the `now` option, described below.
## Options
* `:now` - a `NaiveDateTime` struct with the current date and time. This is
optional and used for testing purposes.
* `:from` - a ISO-8601 date string used when mode is `"custom"`.
"""
def compare(%Plausible.Site{} = site, %Stats.Query{} = source_query, mode, opts \\ []) do
if valid_mode?(source_query, mode) do
now = now || Timex.now(site.timezone)
{:ok, do_compare(source_query, mode, now)}
opts = Keyword.put_new(opts, :now, Timex.now(site.timezone))
{:ok, do_compare(source_query, mode, opts)}
else
{:error, :not_supported}
end
end
defp do_compare(source_query, "year_over_year", now) do
defp do_compare(source_query, "year_over_year", opts) do
now = Keyword.fetch!(opts, :now)
start_date = Date.add(source_query.date_range.first, -365)
end_date = earliest(source_query.date_range.last, now) |> Date.add(-365)
@ -42,7 +64,9 @@ defmodule Plausible.Stats.Comparisons do
%Stats.Query{source_query | date_range: range}
end
defp do_compare(source_query, "previous_period", now) do
defp do_compare(source_query, "previous_period", opts) do
now = Keyword.fetch!(opts, :now)
last = earliest(source_query.date_range.last, now)
diff_in_days = Date.diff(source_query.date_range.first, last) - 1
@ -53,6 +77,13 @@ defmodule Plausible.Stats.Comparisons do
%Stats.Query{source_query | date_range: range}
end
defp do_compare(source_query, "custom", opts) do
from = opts |> Keyword.fetch!(:from) |> Date.from_iso8601!()
to = opts |> Keyword.fetch!(:to) |> Date.from_iso8601!()
%Stats.Query{source_query | date_range: Date.range(from, to)}
end
defp earliest(a, b) do
if Date.compare(a, b) in [:eq, :lt], do: a, else: b
end

@ -110,21 +110,25 @@ defmodule PlausibleWeb.Api.StatsController do
end
timeseries_result = Stats.timeseries(site, timeseries_query, [selected_metric])
labels = label_timeseries(timeseries_result)
present_index = present_index_for(site, query, labels)
full_intervals = build_full_intervals(query, labels)
comparison_result =
case Comparisons.compare(site, query, params["comparison"]) do
case Comparisons.compare(site, query, params["comparison"],
from: params["compare_from"],
to: params["compare_to"]
) do
{:ok, comparison_query} -> Stats.timeseries(site, comparison_query, [selected_metric])
{:error, :not_supported} -> nil
end
labels = label_timeseries(query, timeseries_result, comparison_result)
present_index = present_index_for(site, query, labels)
full_intervals = build_full_intervals(query, labels)
json(conn, %{
plot: plot_timeseries(timeseries_result, selected_metric),
labels: labels,
comparison_plot: comparison_result && plot_timeseries(comparison_result, selected_metric),
comparison_labels: comparison_result && label_timeseries(comparison_result),
comparison_labels: comparison_result && label_timeseries(query, comparison_result, nil),
present_index: present_index,
interval: query.interval,
with_imported: query.include_imported,
@ -140,8 +144,40 @@ defmodule PlausibleWeb.Api.StatsController do
Enum.map(timeseries, fn row -> row[metric] || 0 end)
end
defp label_timeseries(timeseries) do
Enum.map(timeseries, & &1.date)
defp label_timeseries(_query, main_result, nil) do
Enum.map(main_result, & &1.date)
end
defp label_timeseries(query, main_result, comparison_result) do
blanks_to_fill = Enum.count(comparison_result) - Enum.count(main_result)
if blanks_to_fill > 0 do
last_date = List.last(main_result).date
blanks = generate_blanks(query, last_date, blanks_to_fill)
Enum.map(main_result, & &1.date) ++ blanks
else
Enum.map(main_result, & &1.date)
end
end
defp generate_blanks(%Query{interval: "month"}, last_date, blanks_to_fill) do
Enum.map(1..blanks_to_fill, fn index ->
Timex.shift(last_date, months: index)
end)
end
defp generate_blanks(%Query{interval: "week"}, last_date, blanks_to_fill) do
Enum.map(1..blanks_to_fill, fn index ->
Timex.shift(last_date, weeks: index)
end)
end
defp generate_blanks(query, last_date, blanks_to_fill) do
Enum.map(1..blanks_to_fill, fn index ->
Timex.shift(last_date, days: index)
end)
end
defp build_full_intervals(%{interval: "week", date_range: range}, labels) do
@ -175,9 +211,11 @@ defmodule PlausibleWeb.Api.StatsController do
with :ok <- validate_params(params) do
comparison_mode = params["comparison"] || "previous_period"
comparison_opts = [from: params["compare_from"], to: params["compare_to"]]
query = Query.from(site, params) |> Filters.add_prefix()
{top_stats, sample_percent} = fetch_top_stats(site, query, comparison_mode)
{top_stats, sample_percent} = fetch_top_stats(site, query, comparison_mode, comparison_opts)
json(conn, %{
top_stats: top_stats,
@ -245,7 +283,8 @@ defmodule PlausibleWeb.Api.StatsController do
defp fetch_top_stats(
site,
%Query{period: "realtime", filters: %{"event:goal" => _goal}} = query,
_comparison_mode
_comparison_mode,
_comparison_opts
) do
query_30m = %Query{query | period: "30m"}
@ -272,7 +311,12 @@ defmodule PlausibleWeb.Api.StatsController do
{stats, 100}
end
defp fetch_top_stats(site, %Query{period: "realtime"} = query, _comparison_mode) do
defp fetch_top_stats(
site,
%Query{period: "realtime"} = query,
_comparison_mode,
_comparison_opts
) do
query_30m = %Query{query | period: "30m"}
%{
@ -298,11 +342,16 @@ defmodule PlausibleWeb.Api.StatsController do
{stats, 100}
end
defp fetch_top_stats(site, %Query{filters: %{"event:goal" => _goal}} = query, comparison_mode) do
defp fetch_top_stats(
site,
%Query{filters: %{"event:goal" => _goal}} = query,
comparison_mode,
comparison_opts
) do
total_q = Query.remove_event_filters(query, [:goal, :props])
{prev_converted_visitors, prev_completions} =
case Stats.Comparisons.compare(site, query, comparison_mode) do
case Stats.Comparisons.compare(site, query, comparison_mode, comparison_opts) do
{:ok, prev_query} ->
%{visitors: %{value: prev_converted_visitors}, events: %{value: prev_completions}} =
Stats.aggregate(site, prev_query, [:visitors, :events])
@ -362,7 +411,7 @@ defmodule PlausibleWeb.Api.StatsController do
{stats, 100}
end
defp fetch_top_stats(site, query, comparison_mode) do
defp fetch_top_stats(site, query, comparison_mode, comparison_opts) do
metrics =
if query.filters["event:page"] do
[
@ -389,7 +438,7 @@ defmodule PlausibleWeb.Api.StatsController do
current_results = Stats.aggregate(site, query, metrics)
prev_results =
case Stats.Comparisons.compare(site, query, comparison_mode) do
case Stats.Comparisons.compare(site, query, comparison_mode, comparison_opts) do
{:ok, prev_results_query} -> Stats.aggregate(site, prev_results_query, metrics)
{:error, :not_supported} -> nil
end

@ -2,91 +2,112 @@ defmodule Plausible.Stats.ComparisonsTest do
use Plausible.DataCase
alias Plausible.Stats.{Query, Comparisons}
describe "this month" do
test "shifts back this month period" do
describe "with period set to this month" do
test "shifts back this month period when mode is previous_period" do
site = build(:site)
query = Query.from(site, %{"period" => "month", "date" => "2023-03-02"})
now = ~N[2023-03-02 14:00:00]
{:ok, comparison} = Comparisons.compare(site, query, "previous_period", now)
{:ok, comparison} = Comparisons.compare(site, query, "previous_period", now: now)
assert comparison.date_range.first == ~D[2023-02-27]
assert comparison.date_range.last == ~D[2023-02-28]
end
test "shifts back this month period when it's the first day of the month" do
test "shifts back this month period when it's the first day of the month and mode is previous_period" do
site = build(:site)
query = Query.from(site, %{"period" => "month", "date" => "2023-03-01"})
now = ~N[2023-03-01 14:00:00]
{:ok, comparison} = Comparisons.compare(site, query, "previous_period", now)
{:ok, comparison} = Comparisons.compare(site, query, "previous_period", now: now)
assert comparison.date_range.first == ~D[2023-02-28]
assert comparison.date_range.last == ~D[2023-02-28]
end
test "limits the comparison query to current day when mode is custom" do
site = build(:site)
query = Query.from(site, %{"period" => "month", "date" => "2023-03-25"})
now = ~N[2023-03-25 14:00:00]
{:ok, comparison} = Comparisons.compare(site, query, "custom", from: "2023-03-23", now: now)
assert comparison.date_range.first == ~D[2023-03-23]
assert comparison.date_range.last == ~D[2023-03-25]
end
end
describe "previous month" do
test "shifts back using the same number of days when previous_period" do
describe "with period set to previous month" do
test "shifts back using the same number of days when mode is previous_period" do
site = build(:site)
query = Query.from(site, %{"period" => "month", "date" => "2023-02-01"})
now = ~N[2023-03-01 14:00:00]
{:ok, comparison} = Comparisons.compare(site, query, "previous_period", now)
{:ok, comparison} = Comparisons.compare(site, query, "previous_period", now: now)
assert comparison.date_range.first == ~D[2023-01-04]
assert comparison.date_range.last == ~D[2023-01-31]
end
test "shifts back the full month when year_over_year" do
test "shifts back the full month when mode is year_over_year" do
site = build(:site)
query = Query.from(site, %{"period" => "month", "date" => "2023-02-01"})
now = ~N[2023-03-01 14:00:00]
{:ok, comparison} = Comparisons.compare(site, query, "year_over_year", now)
{:ok, comparison} = Comparisons.compare(site, query, "year_over_year", now: now)
assert comparison.date_range.first == ~D[2022-02-01]
assert comparison.date_range.last == ~D[2022-02-28]
end
test "shifts back whole month plus one day when year_over_year and a leap year" do
test "shifts back whole month plus one day when mode is year_over_year and a leap year" do
site = build(:site)
query = Query.from(site, %{"period" => "month", "date" => "2020-02-01"})
now = ~N[2023-03-01 14:00:00]
{:ok, comparison} = Comparisons.compare(site, query, "year_over_year", now)
{:ok, comparison} = Comparisons.compare(site, query, "year_over_year", now: now)
assert comparison.date_range.first == ~D[2019-02-01]
assert comparison.date_range.last == ~D[2019-03-01]
end
test "infers the end date from source query length when mode is custom" do
site = build(:site)
query = Query.from(site, %{"period" => "month", "date" => "2023-03-01"})
{:ok, comparison} = Comparisons.compare(site, query, "custom", from: "2023-04-02")
assert comparison.date_range.first == ~D[2023-04-02]
assert comparison.date_range.last == ~D[2023-05-01]
end
end
describe "year to date" do
test "shifts back by the same number of days when previous_period" do
describe "with period set to year to date" do
test "shifts back by the same number of days when mode is previous_period" do
site = build(:site)
query = Query.from(site, %{"period" => "year", "date" => "2023-03-01"})
now = ~N[2023-03-01 14:00:00]
{:ok, comparison} = Comparisons.compare(site, query, "previous_period", now)
{:ok, comparison} = Comparisons.compare(site, query, "previous_period", now: now)
assert comparison.date_range.first == ~D[2022-11-02]
assert comparison.date_range.last == ~D[2022-12-31]
end
test "shifts back by the same number of days when year_over_year" do
test "shifts back by the same number of days when mode is year_over_year" do
site = build(:site)
query = Query.from(site, %{"period" => "year", "date" => "2023-03-01"})
now = ~N[2023-03-01 14:00:00]
{:ok, comparison} = Comparisons.compare(site, query, "year_over_year", now)
{:ok, comparison} = Comparisons.compare(site, query, "year_over_year", now: now)
assert comparison.date_range.first == ~D[2022-01-01]
assert comparison.date_range.last == ~D[2022-03-01]
end
end
describe "previous year" do
test "shifts back a whole year when year_over_year" do
describe "with period set to previous year" do
test "shifts back a whole year when mode is year_over_year" do
site = build(:site)
query = Query.from(site, %{"period" => "year", "date" => "2022-03-02"})
@ -96,7 +117,7 @@ defmodule Plausible.Stats.ComparisonsTest do
assert comparison.date_range.last == ~D[2021-12-31]
end
test "shifts back a whole year when previous_period" do
test "shifts back a whole year when mode is previous_period" do
site = build(:site)
query = Query.from(site, %{"period" => "year", "date" => "2022-03-02"})
@ -105,10 +126,20 @@ defmodule Plausible.Stats.ComparisonsTest do
assert comparison.date_range.first == ~D[2021-01-01]
assert comparison.date_range.last == ~D[2021-12-31]
end
test "infers the end date from source query length when mode is custom" do
site = build(:site)
query = Query.from(site, %{"period" => "year", "date" => "2022-03-02"})
{:ok, comparison} = Comparisons.compare(site, query, "custom", from: "2019-01-01")
assert comparison.date_range.first == ~D[2019-01-01]
assert comparison.date_range.last == ~D[2019-12-30]
end
end
describe "custom" do
test "shifts back by the same number of days when previous_period" do
describe "with period set to custom" do
test "shifts back by the same number of days when mode is previous_period" do
site = build(:site)
query = Query.from(site, %{"period" => "custom", "date" => "2023-01-01,2023-01-07"})
@ -118,7 +149,7 @@ defmodule Plausible.Stats.ComparisonsTest do
assert comparison.date_range.last == ~D[2022-12-31]
end
test "shifts back to last year when year_over_year" do
test "shifts back to last year when mode is year_over_year" do
site = build(:site)
query = Query.from(site, %{"period" => "custom", "date" => "2023-01-01,2023-01-07"})

Loading…
Cancel
Save