Half-working attempt at allowing `to` date for comparisons

custom-comparison-mode-end-date-spike
Uku Taht 4 years ago
parent 9d64394d8d
commit b13a34c9a3

@ -49,6 +49,7 @@ export function serializeQuery(query, extraQuery=[]) {
if (query.comparison) {
queryObj.comparison = query.comparison
queryObj.compare_from = query.compare_from
queryObj.compare_to = query.compare_to
}
Object.assign(queryObj, ...extraQuery)

@ -57,7 +57,7 @@ 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(), 0)
setTimeout(() => calendar.current.flatpickr.open(), 100)
} else {
updateMode(value)
}
@ -86,15 +86,16 @@ const ComparisonInput = function({ site, query, history }) {
if (COMPARISON_DISABLED_PERIODS.includes(query.period)) return null
if (!isComparisonEnabled(query.comparison)) return null
const updateMode = (mode, from = null) => {
const updateMode = (mode, from = null, to = null) => {
storeComparisonMode(site.domain, mode)
navigateToQuery(history, query, { comparison: mode, compare_from: from })
navigateToQuery(history, query, { comparison: mode, compare_from: from, compare_to: to })
}
const buildLabel = (query) => {
if (query.comparison == "custom") {
const date = parseUTCDate(query.compare_from)
return formatDayShort(date, true)
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]
}
@ -102,10 +103,12 @@ const ComparisonInput = function({ site, query, history }) {
const calendar = React.useRef(null)
const flatpickrOptions = {
mode: 'range',
showMonths: 1,
maxDate: 'today',
animate: true,
static: true,
onChange: ([date]) => updateMode("custom", formatISO(date))
onChange: ([from, to]) => updateMode("custom", formatISO(from), formatISO(to))
}
return (

@ -26,6 +26,7 @@ export function parseQuery(querystring, site) {
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,

@ -79,9 +79,7 @@ defmodule Plausible.Stats.Comparisons do
defp do_compare(source_query, "custom", opts) do
from = opts |> Keyword.fetch!(:from) |> Date.from_iso8601!()
diff_in_days = Date.diff(source_query.date_range.last, source_query.date_range.first)
to = from |> Date.add(diff_in_days) |> earliest(opts[:now])
to = opts |> Keyword.fetch!(:to) |> Date.from_iso8601!()
%Stats.Query{source_query | date_range: Date.range(from, to)}
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"], from: params["compare_from"]) 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,7 +211,7 @@ defmodule PlausibleWeb.Api.StatsController do
with :ok <- validate_params(params) do
comparison_mode = params["comparison"] || "previous_period"
comparison_opts = [from: params["compare_from"]]
comparison_opts = [from: params["compare_from"], to: params["compare_to"]]
query = Query.from(site, params) |> Filters.add_prefix()

Loading…
Cancel
Save