You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
analytics/lib/plausible/stats/interval.ex

107 lines
3.2 KiB
Elixir

defmodule Plausible.Stats.Interval do
@moduledoc """
Collection of functions to work with intervals.
The interval of a query defines the granularity of the data. You can think of
it as a `GROUP BY` clause. Possible values are `minute`, `hour`, `date`,
`week`, and `month`.
"""
@type t() :: String.t()
@type(opt() :: {:site, Plausible.Site.t()} | {:from, Date.t()}, {:to, Date.t()})
@type opts :: list(opt())
@typep period() :: String.t()
@intervals ~w(minute hour date week month)
@spec list() :: [t()]
def list, do: @intervals
@spec valid?(term()) :: boolean()
def valid?(interval) do
interval in @intervals
end
@spec default_for_period(period()) :: t()
@doc """
Returns the suggested interval for the given time period.
"""
def default_for_period(period) do
case period do
"realtime" -> "minute"
"day" -> "hour"
period when period in ["custom", "7d", "30d", "month"] -> "date"
period when period in ["6mo", "12mo", "year"] -> "month"
end
end
@spec default_for_date_range(Date.Range.t()) :: t()
@doc """
Returns the suggested interval for the given `Date.Range` struct.
"""
def default_for_date_range(%Date.Range{first: first, last: last}) do
cond do
Timex.diff(last, first, :months) > 0 ->
"month"
Timex.diff(last, first, :days) > 0 ->
"date"
true ->
"hour"
end
end
@valid_by_period %{
"realtime" => ["minute"],
"day" => ["minute", "hour"],
"7d" => ["hour", "date"],
"month" => ["date", "week"],
"30d" => ["date", "week"],
"6mo" => ["date", "week", "month"],
"12mo" => ["date", "week", "month"],
"year" => ["date", "week", "month"],
"custom" => ["date", "week", "month"],
"all" => ["date", "week", "month"]
}
@spec valid_by_period(opts()) :: map()
def valid_by_period(opts \\ []) do
site = Keyword.fetch!(opts, :site)
table =
with %Date{} = from <- Keyword.get(opts, :from),
%Date{} = to <- Keyword.get(opts, :to),
true <- abs(Timex.diff(from, to, :months)) > 12 do
Map.replace(@valid_by_period, "custom", ["week", "month"])
else
_ ->
@valid_by_period
end
Add multiple imports per site (#3724) * Clean up references to no longer active `google_analytics_imports` Oban queue * Stub CSV importer * Add SiteImport schema * Rename `Plausible.Imported` module file to match module name * Add `import_id` column to `Imported.*` CH schemas * Implement Importer behavior and manage imports state using new entities * Implement importer callbacks and maintain site.imported_data for UA * Keep imports in sync when forgetting all imports * Scope imported data queries to completed import IDs * Mark newly imported data with respective import ID * Clean up Importer implementation a bit * Test querying legacy and new imported data * Send Oban notifications on import worker failure too * Fix checking for forgettable imports and remove redundant function * Fix UA integration test * Change site import source to atom enum and add source label * Add typespecs and reduce repetition in `Plausible.Imported` * Improve documentation and typespecs * Add test for purging particular import * Switch email notification templates depending on import source * Document running import synchronously * Fix UA importer args parsing and ensure it's covered by tests * Clear `site.stats_start_date` on complete import to force recalculation * Test Oban notifications (h/t @ruslandoga) * Purge stats on import failure right away to reduce a chance of leaving debris behind * Fix typos Co-authored-by: hq1 <hq@mtod.org> * Fix another typo * Refactor fetching earliest import and earliest stats start date * Use `Date.after?` instead of `Timex.after?` * Cache import data in site virtual fields and limit queried imports to 5 * Ensure always current `stats_start_date` is used * Work around broken typespec in Timex * Make `SiteController.forget_imported` action idempotent * Discard irrecoverably failed import tasks * Use macros for site import statuses There's also a fix ensuring only complete imports are considered where relevant - couldn't isolate it as it was in a common hunk * Use `import_id` as worker job uniqueness criterion * Do not load imported stats data in plugins API context --------- Co-authored-by: hq1 <hq@mtod.org>
3 years ago
with %Date{} = stats_start <- Plausible.Sites.stats_start_date(site),
true <- abs(Timex.diff(Date.utc_today(), stats_start, :months)) > 12 do
Map.replace(table, "all", ["week", "month"])
else
_ ->
table
end
end
@spec valid_for_period?(period(), t(), opts()) :: boolean()
@doc """
Returns whether the given interval is valid for a time period.
Intervals longer than periods are not supported, e.g. current month stats with
a month interval, or today stats with a week interval.
There are two dynamic states:
* `custom` period is only applicable with `month` or `week` intervals,
if the `opts[:from]` and `opts[:to]` range difference exceeds 12 months
* `all` period's interval options depend on particular site's `stats_start_date`
- daily interval is excluded if the all-time range exceeds 12 months
"""
def valid_for_period?(period, interval, opts \\ []) do
interval in Map.get(valid_by_period(opts), period, [])
end
end