Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deal with clock drifts and use DateTime UTC for server data #2787

Merged
merged 5 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/livebook/hubs/team_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ defmodule Livebook.Hubs.TeamClient do
deployment_group_id: app_deployment.deployment_group_id,
file: nil,
deployed_by: app_deployment.deployed_by,
deployed_at: NaiveDateTime.from_gregorian_seconds(app_deployment.deployed_at)
deployed_at: DateTime.from_gregorian_seconds(app_deployment.deployed_at)
}
end

Expand Down
5 changes: 2 additions & 3 deletions lib/livebook/teams/app_deployment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Livebook.Teams.AppDeployment do
deployment_group_id: String.t() | nil,
file: binary() | nil,
deployed_by: String.t() | nil,
deployed_at: NaiveDateTime.t() | nil
deployed_at: DateTime.t() | nil
}

@access_types Livebook.Notebook.AppSettings.access_types()
Expand All @@ -33,8 +33,7 @@ defmodule Livebook.Teams.AppDeployment do
field :deployment_group_id, :string
field :file, :string
field :deployed_by, :string

timestamps(updated_at: nil, inserted_at: :deployed_at)
field :deployed_at, :utc_datetime
end

@doc """
Expand Down
23 changes: 15 additions & 8 deletions lib/livebook/utils/time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ defmodule Livebook.Utils.Time do

@doc """
Formats the given point in time relatively to present.

## Examples

To deal with clock-drifts when receiving timestamps from the server, we accept future times:

iex> Livebook.Utils.Time.time_ago_in_words(~N[2100-06-20 18:15:00])
"less than 5 seconds"

"""
@spec time_ago_in_words(NaiveDateTime.t()) :: String.t()
def time_ago_in_words(naive_date_time) when is_struct(naive_date_time, NaiveDateTime) do
now = NaiveDateTime.utc_now()

if NaiveDateTime.compare(naive_date_time, now) == :gt do
raise ArgumentError, "expected a datetime in the past, got: #{inspect(naive_date_time)}"
end

distance_of_time_in_words(naive_date_time, now)
distance_of_time_in_words(naive_date_time, NaiveDateTime.utc_now())
end

@doc """
Expand Down Expand Up @@ -75,6 +77,11 @@ defmodule Livebook.Utils.Time do
iex> Livebook.Utils.Time.distance_of_time_in_words(~N[2020-06-20 18:15:00], ~N[2021-08-22 18:15:00])
"about 14 months"

To deal with clock-drifts when receiving timestamps from another machine, we accept future times:

iex> Livebook.Utils.Time.distance_of_time_in_words(~N[2020-06-20 18:15:06], ~N[2020-06-20 18:15:04])
"less than 5 seconds"

"""
@spec distance_of_time_in_words(NaiveDateTime.t(), NaiveDateTime.t()) :: String.t()
def distance_of_time_in_words(from_ndt, to_ndt)
Expand All @@ -92,7 +99,7 @@ defmodule Livebook.Utils.Time do

defp maybe_convert_to_minutes(duration), do: duration

defp duration_in_words({:seconds, seconds}) when seconds in 0..4 do
defp duration_in_words({:seconds, seconds}) when seconds <= 4 do
"less than 5 seconds"
end

Expand Down
2 changes: 1 addition & 1 deletion lib/livebook_web/helpers/html_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ defmodule LivebookWeb.HTMLHelpers do
Formats the given UTC datetime relatively to present.
"""
@spec format_datetime_relatively(DateTime.t() | NaiveDateTime.t()) :: String.t()
def format_datetime_relatively(%DateTime{} = date) do
def format_datetime_relatively(%DateTime{time_zone: "Etc/UTC"} = date) do
date |> DateTime.to_naive() |> Livebook.Utils.Time.time_ago_in_words()
end

Expand Down
4 changes: 2 additions & 2 deletions test/livebook_teams/hubs/team_client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -663,10 +663,10 @@ defmodule Livebook.Hubs.TeamClientTest do
version: Livebook.Utils.random_id(),
file: nil,
deployed_by: teams_app_deployment.app_revision.created_by.name,
deployed_at: teams_app_deployment.updated_at
deployed_at: DateTime.from_naive!(teams_app_deployment.updated_at, "Etc/UTC")
}

{seconds, 0} = NaiveDateTime.to_gregorian_seconds(app_deployment.deployed_at)
{seconds, 0} = DateTime.to_gregorian_seconds(app_deployment.deployed_at)

livebook_proto_app_deployment =
%LivebookProto.AppDeployment{
Expand Down
6 changes: 3 additions & 3 deletions test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ defmodule Livebook.Factory do
shasum = Base.encode16(md5_hash, case: :lower)

deployed_at =
NaiveDateTime.utc_now()
|> NaiveDateTime.truncate(:second)
DateTime.utc_now()
|> DateTime.truncate(:second)

{seconds, 0} = NaiveDateTime.to_gregorian_seconds(deployed_at)
{seconds, 0} = DateTime.to_gregorian_seconds(deployed_at)

%Livebook.Teams.AppDeployment{
id: "1",
Expand Down
Loading