Skip to content

Commit

Permalink
Rework datetimes handlings
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanIvanoff committed May 27, 2020
1 parent 3ea1248 commit 58c5152
Show file tree
Hide file tree
Showing 42 changed files with 315 additions and 314 deletions.
46 changes: 34 additions & 12 deletions lib/sanbase/auth/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,17 @@ defmodule Sanbase.Auth.User do
timestamps()
end

def generate_salt do
def generate_salt() do
:crypto.strong_rand_bytes(@salt_length) |> Base.url_encode64() |> binary_part(0, @salt_length)
end

def generate_email_token do
def generate_email_token() do
:crypto.strong_rand_bytes(@email_token_length) |> Base.url_encode64()
end

def changeset(%User{} = user, attrs \\ %{}) do
attrs = Sanbase.DateTimeUtils.truncate_datetimes(attrs)

user
|> cast(attrs, [
:email,
Expand Down Expand Up @@ -189,15 +191,20 @@ defmodule Sanbase.Auth.User do
def san_balance_cache_stale?(%User{san_balance_updated_at: nil}), do: true

def san_balance_cache_stale?(%User{san_balance_updated_at: san_balance_updated_at}) do
Timex.diff(Timex.now(), san_balance_updated_at, :seconds) > @san_balance_cache_seconds
naive_now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
Timex.diff(naive_now, san_balance_updated_at, :seconds) > @san_balance_cache_seconds
end

def update_san_balance_changeset(user) do
user = Repo.preload(user, :eth_accounts)
san_balance = san_balance_for_eth_accounts(user)
naive_now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)

user
|> change(san_balance: san_balance, san_balance_updated_at: Timex.now())
|> change(
san_balance_updated_at: naive_now,
san_balance: san_balance
)
end

@spec san_balance(%User{}) :: {:ok, float()} | {:ok, nil} | {:error, String.t()}
Expand Down Expand Up @@ -274,7 +281,7 @@ defmodule Sanbase.Auth.User do
user
|> change(
email_token: generate_email_token(),
email_token_generated_at: Timex.now(),
email_token_generated_at: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second),
email_token_validated_at: nil,
consent_id: consent
)
Expand All @@ -286,44 +293,57 @@ defmodule Sanbase.Auth.User do
|> changeset(%{
email_candidate: email_candidate,
email_candidate_token: generate_email_token(),
email_candidate_token_generated_at: Timex.now(),
email_candidate_token_generated_at:
NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second),
email_candidate_token_validated_at: nil
})
|> Repo.update()
end

def mark_email_token_as_validated(user) do
validated_at =
(user.email_token_validated_at || Timex.now())
|> Timex.to_naive_datetime()
|> NaiveDateTime.truncate(:second)

user
|> change(
email_token_validated_at: user.email_token_validated_at || Timex.now(),
email_token_validated_at: validated_at,
is_registered: true
)
|> Repo.update()
end

def update_email_from_email_candidate(user) do
validated_at =
(user.email_candidate_token_validated_at || Timex.now())
|> Timex.to_naive_datetime()
|> NaiveDateTime.truncate(:second)

user
|> changeset(%{
email: user.email_candidate,
email_candidate: nil,
email_candidate_token_validated_at: user.email_candidate_token_validated_at || Timex.now()
email_candidate_token_validated_at: validated_at
})
|> Repo.update()
end

def email_token_valid?(user, token) do
naive_now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)

cond do
user.email_token != token ->
false

Timex.diff(Timex.now(), user.email_token_generated_at, :minutes) >
Timex.diff(naive_now, user.email_token_generated_at, :minutes) >
@login_email_valid_minutes ->
false

user.email_token_validated_at == nil ->
true

Timex.diff(Timex.now(), user.email_token_validated_at, :minutes) >
Timex.diff(naive_now, user.email_token_validated_at, :minutes) >
@login_email_valid_after_validation_minutes ->
false

Expand All @@ -333,18 +353,20 @@ defmodule Sanbase.Auth.User do
end

def email_candidate_token_valid?(user, email_candidate_token) do
naive_now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)

cond do
user.email_candidate_token != email_candidate_token ->
false

Timex.diff(Timex.now(), user.email_candidate_token_generated_at, :minutes) >
Timex.diff(naive_now, user.email_candidate_token_generated_at, :minutes) >
@login_email_valid_minutes ->
false

user.email_candidate_token_validated_at == nil ->
true

Timex.diff(Timex.now(), user.email_candidate_token_validated_at, :minutes) >
Timex.diff(naive_now, user.email_candidate_token_validated_at, :minutes) >
@login_email_valid_after_validation_minutes ->
false

Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase/clickhouse/erc20_transfers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule Sanbase.Clickhouse.Erc20Transfers do
@table "erc20_transfers"

@primary_key false
@timestamps_opts updated_at: false
@timestamps_opts [updated_at: false]
schema @table do
field(:datetime, :utc_datetime, source: :dt)
field(:contract, :string, primary_key: true)
Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase/clickhouse/eth_transfers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule Sanbase.Clickhouse.EthTransfers do
@eth_decimals 1_000_000_000_000_000_000

@primary_key false
@timestamps_opts updated_at: false
@timestamps_opts [updated_at: false]
schema @table do
field(:datetime, :utc_datetime, source: :dt)
field(:from_address, :string, primary_key: true, source: :from)
Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase/clickhouse/github/github.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule Sanbase.Clickhouse.Github do
@table "github"

@primary_key false
@timestamps_opts updated_at: false
@timestamps_opts [updated_at: false]
schema @table do
field(:datetime, :utc_datetime, source: :dt, primary_key: true)
field(:repo, :string, primary_key: true)
Expand Down
2 changes: 2 additions & 0 deletions lib/sanbase/comments/comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ defmodule Sanbase.Comment do
end

def changeset(%__MODULE__{} = comment, attrs \\ %{}) do
attrs = Sanbase.DateTimeUtils.truncate_datetimes(attrs)

comment
|> cast(attrs, [:user_id, :parent_id, :root_parent_id, :content, :edited_at])
|> validate_required([:user_id, :content])
Expand Down
4 changes: 2 additions & 2 deletions lib/sanbase/comments/entity_comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ defmodule Sanbase.Comments.EntityComment do
end

defp apply_cursor(query, %{type: :before, datetime: datetime}) do
from(c in query, where: c.inserted_at < ^datetime)
from(c in query, where: c.inserted_at <= ^(datetime |> DateTime.to_naive()))
end

defp apply_cursor(query, %{type: :after, datetime: datetime}) do
from(c in query, where: c.inserted_at >= ^datetime)
from(c in query, where: c.inserted_at >= ^(datetime |> DateTime.to_naive()))
end

defp apply_cursor(query, nil), do: query
Expand Down
4 changes: 2 additions & 2 deletions lib/sanbase/comments/notification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ defmodule Sanbase.Comments.Notification do
where:
p.post_id == ^post_comment.post.id and
p.comment_id != ^post_comment.comment.id and
p.inserted_at < ^post_comment.inserted_at,
p.inserted_at <= ^post_comment.inserted_at,
preload: [comment: :user]
)
end
Expand All @@ -270,7 +270,7 @@ defmodule Sanbase.Comments.Notification do
where:
t.timeline_event_id == ^timeline_event_comment.timeline_event.id and
t.comment_id != ^timeline_event_comment.comment.id and
t.inserted_at < ^timeline_event_comment.inserted_at,
t.inserted_at <= ^timeline_event_comment.inserted_at,
preload: [comment: :user]
)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase/insight/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule Sanbase.Insight.Post do
on_delete: :delete_all
)

field(:published_at, :naive_datetime)
field(:published_at, :naive_datetime_usec)
timestamps()
end

Expand Down
8 changes: 4 additions & 4 deletions lib/sanbase/model/project/roi.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ defmodule Sanbase.Model.Project.Roi do
)
end

defp calc_token_usd_ico_price(nil, _currency_from, _ico_start_date, _current_datetime), do: nil
defp calc_token_usd_ico_price(_price_from, _currency_from, nil, _current_datetime), do: nil
defp calc_token_usd_ico_price(nil, _currency_from, _ico_start_date, _update_naive_dt), do: nil
defp calc_token_usd_ico_price(_price_from, _currency_from, nil, _update_naive_dt), do: nil
defp calc_token_usd_ico_price(_price_from, _currency_from, _ico_start_date, nil), do: nil

defp calc_token_usd_ico_price(price_from, currency_from, ico_start_date, current_datetime) do
with :gt <- Date.compare(current_datetime |> DateTime.to_date(), ico_start_date),
defp calc_token_usd_ico_price(price_from, currency_from, ico_start_date, update_naive_dt) do
with :gt <- Date.compare(update_naive_dt |> NaiveDateTime.to_date(), ico_start_date),
datetime <- Sanbase.DateTimeUtils.date_to_datetime(ico_start_date),
price_usd when not is_nil(price_usd) <-
Sanbase.Price.Utils.fetch_last_price_before(currency_from, "USD", datetime) do
Expand Down
4 changes: 2 additions & 2 deletions lib/sanbase/timeline/cursor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ defmodule Sanbase.Timeline.Cursor do
def filter_by_cursor(query, :before, datetime) do
from(
event in query,
where: event.inserted_at < ^datetime
where: event.inserted_at < ^DateTime.to_naive(datetime)
)
end

def filter_by_cursor(query, :after, datetime) do
from(
event in query,
where: event.inserted_at > ^datetime
where: event.inserted_at > ^DateTime.to_naive(datetime)
)
end

Expand Down
4 changes: 3 additions & 1 deletion lib/sanbase/timeline/timeline_event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule Sanbase.Timeline.TimelineEvent do

@max_events_returned 100

@timestamps_opts [updated_at: false, type: :utc_datetime]
@timestamps_opts [updated_at: false]
@table "timeline_events"
schema @table do
field(:event_type, :string)
Expand All @@ -59,6 +59,8 @@ defmodule Sanbase.Timeline.TimelineEvent do
def trigger_fired(), do: @trigger_fired

def create_changeset(%__MODULE__{} = timeline_events, attrs \\ %{}) do
attrs = Sanbase.DateTimeUtils.truncate_datetimes(attrs)

timeline_events
|> cast(attrs, [
:event_type,
Expand Down
8 changes: 8 additions & 0 deletions lib/sanbase/utils/datetime/utils.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
defmodule Sanbase.DateTimeUtils do
def truncate_datetimes(%{} = map, precision \\ :second) do
Enum.into(map, %{}, fn
{k, %DateTime{} = dt} -> {k, DateTime.truncate(dt, precision)}
{k, %NaiveDateTime{} = dt} -> {k, NaiveDateTime.truncate(dt, precision)}
{k, v} -> {k, v}
end)
end

def time_in_range?(%Time{} = time, %Time{} = from, %Time{} = to) do
case Time.compare(from, to) do
:eq ->
Expand Down
1 change: 0 additions & 1 deletion lib/sanbase_web/graphql/middlewares/access_control.ex
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ defmodule SanbaseWeb.Graphql.Middlewares.AccessControl do
middleware_args
) do
if Plan.AccessChecker.is_restricted?(query) do
IO.inspect("#{inspect(query)} IS RESTRICTED????")
restricted_query(resolution, middleware_args, query)
else
not_restricted_query(resolution, middleware_args)
Expand Down
4 changes: 3 additions & 1 deletion lib/sanbase_web/graphql/schema/custom_types/datetime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ defmodule SanbaseWeb.Graphql.CustomTypes.DateTime do
|> DateTime.to_iso8601()

%DateTime{} = dt ->
DateTime.truncate(dt, :second) |> DateTime.to_iso8601()
dt
|> DateTime.truncate(:second)
|> DateTime.to_iso8601()
end)

parse(&parse_datetime/1)
Expand Down
8 changes: 4 additions & 4 deletions test/sanbase/auth/user_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ defmodule Sanbase.Auth.UserTest do

user =
insert(:user,
san_balance: 0,
eth_accounts: [%EthAccount{address: "0x000000000001"}],
privacy_policy_accepted: true
san_balance: 100,
san_balance_updated_at: ~N[2020-01-01 00:00:00]
)

changeset = User.update_san_balance_changeset(user)

assert changeset.changes[:san_balance] == 5.0
#

assert Sanbase.TestUtils.datetime_close_to(
Timex.now(),
NaiveDateTime.utc_now(),
changeset.changes[:san_balance_updated_at],
2,
:seconds
Expand Down
1 change: 0 additions & 1 deletion test/sanbase/clickhouse/api_call_data_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule Sanbase.Clickhouse.ApiCallDataTest do
use Sanbase.DataCase

import Mock
import Sanbase.Factory
import Sanbase.DateTimeUtils, only: [from_iso8601_to_unix!: 1, from_iso8601!: 1]

Expand Down
1 change: 0 additions & 1 deletion test/sanbase/clickhouse/gas_used_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule Sanbase.Clickhouse.GasUsedTest do
use Sanbase.DataCase
import Mock
import Sanbase.DateTimeUtils, only: [from_iso8601_to_unix!: 1, from_iso8601!: 1]

alias Sanbase.Clickhouse.GasUsed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule Sanbase.Clickhouse.HistoricalBalance.Erc20AssetsHeldByAdderssTest do
use Sanbase.DataCase

import Mock
import Sanbase.Factory

alias Sanbase.Clickhouse.HistoricalBalance.Erc20Balance
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule Sanbase.Clickhouse.HistoricalBalance.EthAssetsHeldByAdderssTest do
use Sanbase.DataCase

import Mock
import Sanbase.Factory

alias Sanbase.Clickhouse.HistoricalBalance.EthBalance
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
defmodule Sanbase.Clickhouse.HistoricalBalance.MinersBalanceTest do
use Sanbase.DataCase
import Mock

import Sanbase.DateTimeUtils, only: [from_iso8601_to_unix!: 1, from_iso8601!: 1]

alias Sanbase.Clickhouse.HistoricalBalance.MinersBalance

require Sanbase.ClickhouseRepo

setup do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule Sanbase.Clickhouse.Metric.HelperTest do
use Sanbase.DataCase

import Mock

test "metric id to name map" do
rows = [
[1, "daily_active_addresses"],
Expand Down
4 changes: 2 additions & 2 deletions test/sanbase/clickhouse/mining_pools_distribution_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule Sanbase.Clickhouse.MiningPoolsDistributionTest do
use Sanbase.DataCase
import Mock
import Sanbase.DateTimeUtils, only: [from_iso8601_to_unix!: 1, from_iso8601!: 1]

alias Sanbase.Clickhouse.MiningPoolsDistribution

require Sanbase.ClickhouseRepo

setup do
Expand Down
4 changes: 3 additions & 1 deletion test/sanbase/clickhouse/top_holders_test.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
defmodule Sanbase.Clickhouse.TopHoldersTest do
use Sanbase.DataCase
import Mock

import Sanbase.Factory
import Sanbase.DateTimeUtils, only: [from_iso8601_to_unix!: 1, from_iso8601!: 1]

alias Sanbase.Clickhouse.TopHolders

require Sanbase.ClickhouseRepo

setup do
Expand Down
Loading

0 comments on commit 58c5152

Please sign in to comment.