Skip to content

Commit

Permalink
Take live stream snapshot periodically
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed Apr 21, 2024
1 parent 5aff18c commit 138b3de
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/asciinema/streaming/live_stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule Asciinema.Streaming.LiveStream do
field :peak_viewer_count, :integer
field :buffer_time, :float
field :parser, :string
field :snapshot, Asciinema.Ecto.Type.JsonArray

timestamps()

Expand Down
12 changes: 11 additions & 1 deletion lib/asciinema/streaming/live_stream_server.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Asciinema.Streaming.LiveStreamServer do
use GenServer, restart: :transient
alias Asciinema.Recordings.Snapshot
alias Asciinema.Streaming.ViewerTracker
alias Asciinema.{Colors, PubSub, Streaming, Vt}
require Logger
Expand Down Expand Up @@ -169,7 +170,8 @@ defmodule Asciinema.Streaming.LiveStreamServer do
Streaming.update_live_stream(state.stream,
current_viewer_count: state.viewer_count,
cols: cols,
rows: rows
rows: rows,
snapshot: generate_snapshot(state.vt)
)

nil ->
Expand Down Expand Up @@ -265,4 +267,12 @@ defmodule Asciinema.Streaming.LiveStreamServer do
theme_palette: Enum.join(Enum.slice(colors, 2..-1), ":")
]
end

defp generate_snapshot(vt) do
{:ok, {lines, cursor}} = Vt.dump_screen(vt)

{lines, cursor}
|> Snapshot.new()
|> Snapshot.unwrap()
end
end
2 changes: 0 additions & 2 deletions lib/asciinema_web/controllers/recording_svg.ex
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,4 @@ defmodule AsciinemaWeb.RecordingSVG do
|> Snapshot.new()
|> Snapshot.crop(cols, rows)
end

defp snapshot(_, _), do: Snapshot.new([])
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Asciinema.Repo.Migrations.AddSnapshotToLiveStreams do
use Ecto.Migration

def change do
alter table(:live_streams) do
add :snapshot, :text
end
end
end

0 comments on commit 138b3de

Please sign in to comment.