File size: 1,164 Bytes
8da2481
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
defmodule Plausible.Stats.Interval do
  @moduledoc """
  [DEPRECATED] Stats API v2 handles "intervals" as time dimensions.
  See `Plausible.Stats.ApiQueryParser.parse_dimensions/1`.
  """

  alias Plausible.Stats.{DateTimeRange, Query}

  @intervals ["minute", "hour", "day", "week", "month"]

  def valid?(interval) do
    interval in @intervals
  end

  @doc """
  Returns the suggested interval for a given Stats API v1 (legacy) query.
  """
  def default_for_query(query)

  def default_for_query(%Query{
        input_date_range: :all,
        utc_time_range: %DateTimeRange{first: first, last: last}
      }) do
    cond do
      Plausible.Times.diff(last, first, :month) > 0 ->
        "month"

      DateTime.diff(last, first, :day) > 0 ->
        "day"

      true ->
        "hour"
    end
  end

  def default_for_query(%Query{} = query) do
    case query.input_date_range do
      period when period in [:realtime, :realtime_30m] -> "minute"
      :day -> "hour"
      :"24h" -> "hour"
      {:last_n_days, _} -> "day"
      period when period in [:custom, :month] -> "day"
      {:last_n_months, _} -> "month"
      :year -> "month"
    end
  end
end