mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-10-09 21:16:26 +08:00
Display creation date of a session in home (#593)
* add created_at to Session struct * test + doc enhcncement * change format creation date function name * use helper function to format created_at * use mix format * apply review suggested changes and 2 more tests * Store creation time in session state and sort by date by default Co-authored-by: Jonatan Kłosko <jonatanklosko@gmail.com>
This commit is contained in:
parent
bb2fccc525
commit
d2cdf0a2cb
4 changed files with 30 additions and 5 deletions
|
@ -46,7 +46,7 @@ defmodule Livebook.Session do
|
|||
# The struct holds the basic session information that we track
|
||||
# and pass around. The notebook and evaluation state is kept
|
||||
# within the process state.
|
||||
defstruct [:id, :pid, :origin, :notebook_name, :file, :images_dir]
|
||||
defstruct [:id, :pid, :origin, :notebook_name, :file, :images_dir, :created_at]
|
||||
|
||||
use GenServer, restart: :temporary
|
||||
|
||||
|
@ -61,12 +61,14 @@ defmodule Livebook.Session do
|
|||
origin: {:file, FileSystem.File.t()} | {:url, String.t()} | nil,
|
||||
notebook_name: String.t(),
|
||||
file: FileSystem.File.t() | nil,
|
||||
images_dir: FileSystem.File.t()
|
||||
images_dir: FileSystem.File.t(),
|
||||
created_at: DateTime.t()
|
||||
}
|
||||
|
||||
@type state :: %{
|
||||
session_id: id(),
|
||||
data: Data.t(),
|
||||
created_at: DateTime.t(),
|
||||
runtime_monitor_ref: reference() | nil,
|
||||
autosave_timer_ref: reference() | nil,
|
||||
save_task_pid: pid() | nil
|
||||
|
@ -378,6 +380,7 @@ defmodule Livebook.Session do
|
|||
state = %{
|
||||
session_id: id,
|
||||
data: data,
|
||||
created_at: DateTime.utc_now(),
|
||||
runtime_monitor_ref: nil,
|
||||
autosave_timer_ref: nil,
|
||||
save_task_pid: nil
|
||||
|
@ -691,7 +694,8 @@ defmodule Livebook.Session do
|
|||
origin: state.data.origin,
|
||||
notebook_name: state.data.notebook.name,
|
||||
file: state.data.file,
|
||||
images_dir: images_dir_from_state(state)
|
||||
images_dir: images_dir_from_state(state),
|
||||
created_at: state.created_at
|
||||
}
|
||||
end
|
||||
|
||||
|
|
|
@ -180,6 +180,9 @@ defmodule LivebookWeb.HomeLive do
|
|||
<div class="text-gray-600 text-sm">
|
||||
<%= if session.file, do: session.file.path, else: "No file" %>
|
||||
</div>
|
||||
<div class="text-gray-600 text-sm">
|
||||
Created <%= format_creation_date(session.created_at) %>
|
||||
</div>
|
||||
</div>
|
||||
<div class="relative" id={"session-#{session.id}-menu"} phx-hook="Menu" data-element="menu">
|
||||
<button class="icon-button" data-toggle>
|
||||
|
@ -344,7 +347,7 @@ defmodule LivebookWeb.HomeLive do
|
|||
def handle_info(_message, socket), do: {:noreply, socket}
|
||||
|
||||
defp sort_sessions(sessions) do
|
||||
Enum.sort_by(sessions, & &1.notebook_name)
|
||||
Enum.sort_by(sessions, & &1.created_at, {:desc, DateTime})
|
||||
end
|
||||
|
||||
defp files(sessions) do
|
||||
|
@ -383,4 +386,9 @@ defmodule LivebookWeb.HomeLive do
|
|||
session = Enum.find(sessions, &(&1.file == file))
|
||||
session.id
|
||||
end
|
||||
|
||||
def format_creation_date(created_at) do
|
||||
time_words = created_at |> DateTime.to_naive() |> Livebook.Utils.Time.time_ago_in_words()
|
||||
time_words <> " ago"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1149,7 +1149,8 @@ defmodule LivebookWeb.SessionLive do
|
|||
|> Enum.map(fn {client_pid, user_id} -> {client_pid, data.users_map[user_id]} end)
|
||||
|> Enum.sort_by(fn {_client_pid, user} -> user.name end),
|
||||
section_views: section_views(data.notebook.sections, data),
|
||||
bin_entries: data.bin_entries
|
||||
bin_entries: data.bin_entries,
|
||||
created_at: DateTime.now!("Etc/UTC")
|
||||
}
|
||||
end
|
||||
|
||||
|
|
|
@ -614,6 +614,18 @@ defmodule Livebook.SessionTest do
|
|||
end
|
||||
end
|
||||
|
||||
test "session has created_at attribute when it is created", %{session: session} do
|
||||
assert Map.has_key?(session, :created_at)
|
||||
end
|
||||
|
||||
test "session created_at attribute is a date time", %{session: session} do
|
||||
assert %DateTime{} = session.created_at
|
||||
end
|
||||
|
||||
test "session created_at is before now", %{session: session} do
|
||||
assert session.created_at < DateTime.utc_now()
|
||||
end
|
||||
|
||||
defp start_session(opts \\ []) do
|
||||
session_id = Utils.random_id()
|
||||
{:ok, pid} = Session.start_link(Keyword.merge([id: session_id], opts))
|
||||
|
|
Loading…
Add table
Reference in a new issue