Skip to content

Commit

Permalink
Remove Timex.shift in app (#4338)
Browse files Browse the repository at this point in the history
* rm Timex.shift in app

* update current_visitors_12h/1 as well

* type and fix
  • Loading branch information
ruslandoga committed Jul 15, 2024
1 parent 42d2a6f commit f09db60
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 50 deletions.
2 changes: 1 addition & 1 deletion lib/mix/tasks/download_country_database.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Mix.Tasks.DownloadCountryDatabase do
Application.ensure_all_started(:httpoison)
Application.ensure_all_started(:timex)
this_month = Timex.today()
last_month = Timex.shift(this_month, months: -1)
last_month = Date.shift(this_month, month: -1)
this_month = this_month |> Date.to_iso8601() |> binary_part(0, 7)
last_month = last_month |> Date.to_iso8601() |> binary_part(0, 7)
this_month_url = "https://download.db-ip.com/free/dbip-country-lite-#{this_month}.mmdb.gz"
Expand Down
2 changes: 1 addition & 1 deletion lib/plausible/auth/email_verification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ defmodule Plausible.Auth.EmailVerification do

@spec expired?(EmailActivationCode.t()) :: boolean()
def expired?(verification) do
expiration_time = Timex.shift(NaiveDateTime.utc_now(), hours: -1 * @expiration_hours)
expiration_time = NaiveDateTime.shift(NaiveDateTime.utc_now(), hour: -1 * @expiration_hours)
Timex.before?(verification.issued_at, expiration_time)
end
end
2 changes: 1 addition & 1 deletion lib/plausible/auth/grace_period.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Plausible.Auth.GracePeriod do
"""
def start_changeset(%User{} = user) do
grace_period = %__MODULE__{
end_date: Timex.shift(Timex.today(), days: 7),
end_date: Date.shift(Timex.today(), day: 7),
is_over: false,
manual_lock: false
}
Expand Down
6 changes: 3 additions & 3 deletions lib/plausible/auth/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ defmodule Plausible.Auth.User do
end

def end_trial(user) do
change(user, trial_expiry_date: Timex.today() |> Timex.shift(days: -1))
change(user, trial_expiry_date: Timex.today() |> Date.shift(day: -1))
end

def password_strength(changeset) do
Expand Down Expand Up @@ -253,9 +253,9 @@ defmodule Plausible.Auth.User do

defp trial_expiry() do
on_ee do
Timex.today() |> Timex.shift(days: 30)
Timex.today() |> Date.shift(day: 30)
else
Timex.today() |> Timex.shift(years: 100)
Timex.today() |> Date.shift(year: 100)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/plausible/billing/paddle_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ defmodule Plausible.Billing.PaddleApi do
vendor_auth_code: config[:vendor_auth_code],
subscription_id: subscription.paddle_subscription_id,
is_paid: 1,
from: Timex.shift(Timex.today(), years: -5) |> Timex.format!("{YYYY}-{0M}-{0D}"),
to: Timex.shift(Timex.today(), days: 1) |> Timex.format!("{YYYY}-{0M}-{0D}")
from: Date.shift(Timex.today(), year: -5) |> Timex.format!("{YYYY}-{0M}-{0D}"),
to: Date.shift(Timex.today(), day: 1) |> Timex.format!("{YYYY}-{0M}-{0D}")
}

with {:ok, %{body: body}} <- HTTPClient.post(invoices_url(), @headers, params),
Expand Down
14 changes: 7 additions & 7 deletions lib/plausible/billing/qouta/usage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ defmodule Plausible.Billing.Quota.Usage do
end

def usage_cycle(_user, :last_30_days, owned_site_ids, today) do
date_range = Date.range(Timex.shift(today, days: -30), today)
date_range = Date.range(Date.shift(today, day: -30), today)

{pageviews, custom_events} =
Plausible.Stats.Clickhouse.usage_breakdown(owned_site_ids, date_range)
Expand All @@ -136,26 +136,26 @@ defmodule Plausible.Billing.Quota.Usage do
last_bill_date = user.subscription.last_bill_date

normalized_last_bill_date =
Timex.shift(last_bill_date, months: Timex.diff(today, last_bill_date, :months))
Date.shift(last_bill_date, month: Timex.diff(today, last_bill_date, :months))

date_range =
case cycle do
:current_cycle ->
Date.range(
normalized_last_bill_date,
Timex.shift(normalized_last_bill_date, months: 1, days: -1)
Date.shift(normalized_last_bill_date, month: 1, day: -1)
)

:last_cycle ->
Date.range(
Timex.shift(normalized_last_bill_date, months: -1),
Timex.shift(normalized_last_bill_date, days: -1)
Date.shift(normalized_last_bill_date, month: -1),
Date.shift(normalized_last_bill_date, day: -1)
)

:penultimate_cycle ->
Date.range(
Timex.shift(normalized_last_bill_date, months: -2),
Timex.shift(normalized_last_bill_date, days: -1, months: -1)
Date.shift(normalized_last_bill_date, month: -2),
Date.shift(normalized_last_bill_date, day: -1, month: -1)
)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/plausible/google/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ defmodule Plausible.Google.API do
end

defp needs_to_refresh_token?(%NaiveDateTime{} = expires_at) do
thirty_seconds_ago = Timex.shift(Timex.now(), seconds: 30)
thirty_seconds_ago = DateTime.shift(Timex.now(), second: 30)
Timex.before?(expires_at, thirty_seconds_ago)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/plausible/stats.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ defmodule Plausible.Stats do
Timeseries.timeseries(site, query, metrics)
end

def current_visitors(site, shift \\ [minutes: -5]) do
def current_visitors(site, duration \\ Duration.new!(minute: -5)) do
include_sentry_replay_info()
CurrentVisitors.current_visitors(site, shift)
CurrentVisitors.current_visitors(site, duration)
end

on_ee do
Expand Down
8 changes: 4 additions & 4 deletions lib/plausible/stats/clickhouse.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ defmodule Plausible.Stats.Clickhouse do
end

def current_visitors_12h(site) do
Plausible.Stats.current_visitors(site, hours: -12)
Plausible.Stats.current_visitors(site, Duration.new!(hour: -12))
end

def has_pageviews?(site) do
Expand Down Expand Up @@ -239,7 +239,7 @@ defmodule Plausible.Stats.Clickhouse do

first_datetime =
last_datetime
|> Timex.shift(minutes: -30)
|> NaiveDateTime.shift(minute: -30)
|> beginning_of_time(site.native_stats_start_at)
|> NaiveDateTime.truncate(:second)

Expand All @@ -251,7 +251,7 @@ defmodule Plausible.Stats.Clickhouse do

first_datetime =
last_datetime
|> Timex.shift(minutes: -5)
|> NaiveDateTime.shift(minute: -5)
|> beginning_of_time(site.native_stats_start_at)
|> NaiveDateTime.truncate(:second)

Expand All @@ -266,7 +266,7 @@ defmodule Plausible.Stats.Clickhouse do
|> Timezones.to_utc_datetime(site.timezone)
|> beginning_of_time(site.native_stats_start_at)

{:ok, last} = NaiveDateTime.new(date_range.last |> Timex.shift(days: 1), ~T[00:00:00])
{:ok, last} = NaiveDateTime.new(date_range.last |> Date.shift(day: 1), ~T[00:00:00])

last_datetime =
Timezones.to_utc_datetime(last, site.timezone)
Expand Down
5 changes: 3 additions & 2 deletions lib/plausible/stats/current_visitors.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ defmodule Plausible.Stats.CurrentVisitors do
use Plausible.ClickhouseRepo
use Plausible.Stats.SQL.Fragments

def current_visitors(site, shift \\ [minutes: -5]) do
@spec current_visitors(Plausible.Site.t(), Duration.duration()) :: non_neg_integer
def current_visitors(site, duration \\ Duration.new!(minute: -5)) do
first_datetime =
NaiveDateTime.utc_now()
|> Timex.shift(shift)
|> NaiveDateTime.shift(duration)
|> NaiveDateTime.truncate(:second)

ClickhouseRepo.one(
Expand Down
4 changes: 2 additions & 2 deletions lib/plausible/stats/filters/query_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ defmodule Plausible.Stats.Filters.QueryParser do

first =
last
|> Timex.shift(months: -5)
|> Date.shift(month: -5)
|> Date.beginning_of_month()

{:ok, Date.range(first, last)}
Expand All @@ -155,7 +155,7 @@ defmodule Plausible.Stats.Filters.QueryParser do

first =
last
|> Timex.shift(months: -11)
|> Date.shift(month: -11)
|> Date.beginning_of_month()

{:ok, Date.range(first, last)}
Expand Down
8 changes: 4 additions & 4 deletions lib/plausible/stats/legacy/legacy_query_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule Plausible.Stats.Legacy.QueryBuilder do

defp put_period(query, site, %{"period" => "7d"} = params) do
end_date = parse_single_date(site.timezone, params)
start_date = end_date |> Timex.shift(days: -6)
start_date = end_date |> Date.shift(day: -6)

struct!(
query,
Expand All @@ -50,7 +50,7 @@ defmodule Plausible.Stats.Legacy.QueryBuilder do

defp put_period(query, site, %{"period" => "30d"} = params) do
end_date = parse_single_date(site.timezone, params)
start_date = end_date |> Timex.shift(days: -30)
start_date = end_date |> Date.shift(day: -30)

struct!(query, period: "30d", date_range: Date.range(start_date, end_date))
end
Expand All @@ -73,7 +73,7 @@ defmodule Plausible.Stats.Legacy.QueryBuilder do
|> Timex.end_of_month()

start_date =
Timex.shift(end_date, months: -5)
Date.shift(end_date, month: -5)
|> Timex.beginning_of_month()

struct!(query,
Expand All @@ -88,7 +88,7 @@ defmodule Plausible.Stats.Legacy.QueryBuilder do
|> Timex.end_of_month()

start_date =
Timex.shift(end_date, months: -11)
Date.shift(end_date, month: -11)
|> Timex.beginning_of_month()

struct!(query,
Expand Down
18 changes: 9 additions & 9 deletions lib/plausible/stats/time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ defmodule Plausible.Stats.Time do
def utc_boundaries(%Query{period: "realtime", now: now}, site) do
last_datetime =
now
|> Timex.shift(seconds: 5)
|> NaiveDateTime.shift(second: 5)
|> beginning_of_time(site.native_stats_start_at)
|> NaiveDateTime.truncate(:second)

first_datetime =
now |> Timex.shift(minutes: -5) |> NaiveDateTime.truncate(:second)
now |> NaiveDateTime.shift(minute: -5) |> NaiveDateTime.truncate(:second)

{first_datetime, last_datetime}
end

def utc_boundaries(%Query{period: "30m", now: now}, site) do
last_datetime =
now
|> Timex.shift(seconds: 5)
|> NaiveDateTime.shift(second: 5)
|> beginning_of_time(site.native_stats_start_at)
|> NaiveDateTime.truncate(:second)

first_datetime =
now |> Timex.shift(minutes: -30) |> NaiveDateTime.truncate(:second)
now |> NaiveDateTime.shift(minute: -30) |> NaiveDateTime.truncate(:second)

{first_datetime, last_datetime}
end
Expand All @@ -40,7 +40,7 @@ defmodule Plausible.Stats.Time do
|> Timezones.to_utc_datetime(site.timezone)
|> beginning_of_time(site.native_stats_start_at)

{:ok, last} = NaiveDateTime.new(date_range.last |> Timex.shift(days: 1), ~T[00:00:00])
{:ok, last} = NaiveDateTime.new(date_range.last |> Date.shift(day: 1), ~T[00:00:00])

last_datetime = Timezones.to_utc_datetime(last, site.timezone)

Expand Down Expand Up @@ -85,7 +85,7 @@ defmodule Plausible.Stats.Time do
Enum.map(n_buckets..0, fn shift ->
query.date_range.last
|> Date.beginning_of_month()
|> Timex.shift(months: -shift)
|> Date.shift(month: -shift)
|> format_datetime()
end)
end
Expand All @@ -100,7 +100,7 @@ defmodule Plausible.Stats.Time do

Enum.map(0..n_buckets, fn shift ->
query.date_range.first
|> Timex.shift(weeks: shift)
|> Date.shift(week: shift)
|> date_or_weekstart(query)
|> format_datetime()
end)
Expand Down Expand Up @@ -129,7 +129,7 @@ defmodule Plausible.Stats.Time do
Enum.map(0..n_buckets, fn step ->
query.date_range.first
|> Timex.to_datetime()
|> Timex.shift(hours: step)
|> DateTime.shift(hour: step)
|> DateTime.truncate(:second)
|> format_datetime()
end)
Expand All @@ -152,7 +152,7 @@ defmodule Plausible.Stats.Time do
Enum.map(0..n_buckets, fn step ->
query.date_range.first
|> Timex.to_datetime()
|> Timex.shift(minutes: step)
|> DateTime.shift(minute: step)
|> format_datetime()
end)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/plausible/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ defmodule Plausible.Users do

cond do
Plausible.Users.on_trial?(user) ->
Timex.shift(user.trial_expiry_date,
days: Auth.User.trial_accept_traffic_until_offset_days()
Date.shift(user.trial_expiry_date,
day: Auth.User.trial_accept_traffic_until_offset_days()
)

user.subscription && user.subscription.paddle_plan_id == "free_10k" ->
@accept_traffic_until_free

user.subscription && user.subscription.next_bill_date ->
Timex.shift(user.subscription.next_bill_date,
days: Auth.User.subscription_accept_traffic_until_offset_days()
Date.shift(user.subscription.next_bill_date,
day: Auth.User.subscription_accept_traffic_until_offset_days()
)

true ->
Expand Down
2 changes: 1 addition & 1 deletion lib/workers/check_usage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Plausible.Workers.CheckUsage do

@impl Oban.Worker
def perform(_job, usage_mod \\ Quota.Usage, today \\ Timex.today()) do
yesterday = today |> Timex.shift(days: -1)
yesterday = today |> Date.shift(day: -1)

active_subscribers =
Repo.all(
Expand Down
8 changes: 4 additions & 4 deletions lib/workers/schedule_email_reports.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ defmodule Plausible.Workers.ScheduleEmailReports do

def monday_9am(timezone) do
Timex.now(timezone)
|> Timex.shift(weeks: 1)
|> DateTime.shift(week: 1)
|> Timex.beginning_of_week()
|> Timex.shift(hours: 9)
|> DateTime.shift(hour: 9)
end

defp schedule_monthly_emails() do
Expand Down Expand Up @@ -90,8 +90,8 @@ defmodule Plausible.Workers.ScheduleEmailReports do

def first_of_month_9am(timezone) do
Timex.now(timezone)
|> Timex.shift(months: 1)
|> DateTime.shift(month: 1)
|> Timex.beginning_of_month()
|> Timex.shift(hours: 9)
|> DateTime.shift(hour: 9)
end
end
4 changes: 2 additions & 2 deletions lib/workers/send_email_report.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ defmodule Plausible.Workers.SendEmailReport do
defp put_last_month_query(%{site: site} = assigns) do
last_month =
Timex.now(site.timezone)
|> Timex.shift(months: -1)
|> DateTime.shift(month: -1)
|> Timex.beginning_of_month()
|> Timex.format!("{ISOdate}")

Expand All @@ -70,7 +70,7 @@ defmodule Plausible.Workers.SendEmailReport do

defp put_last_week_query(%{site: site} = assigns) do
today = Timex.now(site.timezone) |> DateTime.to_date()
date = Timex.shift(today, weeks: -1) |> Timex.end_of_week() |> Date.to_iso8601()
date = Date.shift(today, week: -1) |> Timex.end_of_week() |> Date.to_iso8601()
query = Query.from(site, %{"period" => "7d", "date" => date})

Map.put(assigns, :query, query)
Expand Down

0 comments on commit f09db60

Please sign in to comment.