Skip to content

Commit

Permalink
Run mix format.
Browse files Browse the repository at this point in the history
  • Loading branch information
errantsky committed Nov 25, 2024
1 parent 8f3849a commit 30acadf
Show file tree
Hide file tree
Showing 18 changed files with 57 additions and 68 deletions.
1 change: 0 additions & 1 deletion config/prod.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Config


config :phoenix_vault,
:archiver_enabled,
true
Expand Down
3 changes: 2 additions & 1 deletion lib/embedding_search.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule EmbeddingSearch do
where: s.user_id == ^user_id,
order_by: cosine_distance(s.embedding, ^snapshot_embedding),
limit: ^limit
) |> Repo.preload(:tags)
)
|> Repo.preload(:tags)
end
end
24 changes: 15 additions & 9 deletions lib/helper_macros/func_logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ defmodule HelperMacros.FuncLogger do
require Logger

defmacro log_func({func, _, [args]} = call) do

quote do
func_name = unquote(Atom.to_string(func))

arg_vals =
Enum.map(unquote(args), fn
{name, val} = arg -> """
#{Atom.to_string(name)}:\n\t#{inspect(val, pretty: true)}
"""
arg -> "#{inspect(arg, pretty: true)}"
{name, val} = arg ->
"""
#{Atom.to_string(name)}:\n\t#{inspect(val, pretty: true)}
"""

arg ->
"#{inspect(arg, pretty: true)}"
end)
|> Enum.join("\n" <> String.duplicate("-", 20) <> "\n")

Expand All @@ -25,10 +27,14 @@ defmodule HelperMacros.FuncLogger do
result = unquote(call)
end_time = :os.system_time(:millisecond)

Logger.debug("log_func #{func_name} Execution time was: #{:io_lib.format("~B", [end_time - start_time]) |> IO.iodata_to_binary()} ms")
Logger.debug(
"log_func #{func_name} Execution time was: #{:io_lib.format("~B", [end_time - start_time]) |> IO.iodata_to_binary()} ms"
)

Logger.debug("""
log_func #{func_name} The result was:\n#{inspect(result, pretty: true)}
""")
log_func #{func_name} The result was:\n#{inspect(result, pretty: true)}
""")

Logger.debug("log_func #{func_name} finished.")

result
Expand Down
6 changes: 3 additions & 3 deletions lib/open_ai_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ defmodule OpenAIClient do
{"Content-Type", "application/json"},
{"Authorization", "Bearer #{openai_api_key}"}
]
{:ok, encoded_list} = Tiktoken.CL100K.encode(text)

{:ok, encoded_list} = Tiktoken.CL100K.encode(text)
truncated_token_list = Enum.take(encoded_list, @embedding_max_tokens)

request_body =
%{
input: truncated_token_list,
Expand Down
2 changes: 0 additions & 2 deletions lib/phoenix_vault/archivers/html_archiver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ defmodule PhoenixVault.Archivers.HtmlArchiver do

@impl Worker
def perform(%Job{args: %{"snapshot_id" => snapshot_id, "snapshot_url" => snapshot_url}}) do

# todo handle error
{:ok, body} = archive_as_html(snapshot_id, snapshot_url)

Expand All @@ -23,7 +22,6 @@ defmodule PhoenixVault.Archivers.HtmlArchiver do
archive_command =
"wget --no-parent --adjust-extension --page-requisites --reject=*.js #{snapshot_url} -P #{ArchiverConfig.snapshot_dir(snapshot_id)}"


{_output, _exit_status} = System.cmd("sh", ["-c", archive_command])

# Now continue with the rest of the logic
Expand Down
1 change: 0 additions & 1 deletion lib/phoenix_vault/archivers/pdf_archiver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ defmodule PhoenixVault.Archivers.PdfArchiver do

@impl Worker
def perform(%Job{args: %{"snapshot_id" => snapshot_id, "snapshot_url" => snapshot_url}}) do

# todo add error handling
print_pdf_for_url(snapshot_id, snapshot_url)

Expand Down
3 changes: 0 additions & 3 deletions lib/phoenix_vault/archivers/screenshot_archiver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ defmodule PhoenixVault.Archivers.ScreenshotArchiver do

@impl Worker
def perform(%Job{args: %{"snapshot_id" => snapshot_id, "snapshot_url" => snapshot_url}}) do

save_screenshot(snapshot_id, snapshot_url)



PhoenixVaultWeb.Endpoint.broadcast!("snapshots", "archiver_update", %{
snapshot_id: snapshot_id,
updated_columns: %{is_screenshot_saved: true}
Expand Down
14 changes: 6 additions & 8 deletions lib/phoenix_vault/archivers/single_file_archiver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ defmodule PhoenixVault.Archivers.SingleFileArchiver do

command = "#{select_binary()} #{snapshot_url} #{index_html_path}"



{_output, _exit_status} = System.cmd("sh", ["-c", command])
# todo add error handling
File.read!(index_html_path) |> extract_body()
Expand All @@ -34,12 +32,12 @@ defmodule PhoenixVault.Archivers.SingleFileArchiver do
body = Floki.find(document, "body")
{:ok, Floki.raw_html(body)}
end

defp select_binary do
case :os.type() do
{:unix, :darwin} -> "./bin/single-file-aarch64-apple-darwin"
{:unix, _} -> "./bin/single-file-x86_64-linux"
_ -> raise "Unsupported operating system"
end
case :os.type() do
{:unix, :darwin} -> "./bin/single-file-aarch64-apple-darwin"
{:unix, _} -> "./bin/single-file-x86_64-linux"
_ -> raise "Unsupported operating system"
end
end
end
12 changes: 10 additions & 2 deletions lib/phoenix_vault/schemas/snapshot.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule PhoenixVault.Schemas.Snapshot do
field :is_single_file_saved, :boolean, default: false
field :user_id, :integer
field :embedding, Pgvector.Ecto.Vector

many_to_many :tags, PhoenixVault.Schemas.Tag,
join_through: PhoenixVault.Schemas.SnapshotTag,
on_replace: :delete
Expand All @@ -25,7 +25,15 @@ defmodule PhoenixVault.Schemas.Snapshot do
# TODO validate url
# TODO validate tags
snapshot
|> cast(attrs, [:title, :url, :user_id, :is_pdf_saved, :is_screenshot_saved, :is_single_file_saved, :embedding])
|> cast(attrs, [
:title,
:url,
:user_id,
:is_pdf_saved,
:is_screenshot_saved,
:is_single_file_saved,
:embedding
])
|> validate_required([:title, :url, :user_id])
|> unique_constraint(:url)
|> put_assoc(:tags, Map.get(attrs, "tags", []))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ defmodule PhoenixVaultWeb.SnapshotLive.BulkSnapshotComponent do
<:subtitle>Use this form to bulk create snapshot records in your database.</:subtitle>
</.header>
<.simple_form
for={@form}
id="bulk-snapshot-form"
phx-target={@myself}
phx-submit="bulk_save"
>
<.simple_form for={@form} id="bulk-snapshot-form" phx-target={@myself} phx-submit="bulk_save">
<.input field={@form[:urls]} label="URLs" type="textarea" required />
<:actions>
<.button phx-disable-with="Saving...">Save Snapshots</.button>
Expand Down
14 changes: 4 additions & 10 deletions lib/phoenix_vault_web/live/snapshot_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ defmodule PhoenixVaultWeb.SnapshotLive.FormComponent do
<:subtitle>Use this form to manage snapshot records in your database.</:subtitle>
</.header>
<.simple_form
for={@form}
id="snapshot-form"
phx-target={@myself}
phx-submit="save"
>
<.simple_form for={@form} id="snapshot-form" phx-target={@myself} phx-submit="save">
<.input field={@form[:title]} label="Title" type="text" required />
<.input field={@form[:url]} label="URL" type="text" required />
<.input field={@form[:tags]} label="Tags" type="text" value={@tag_names} />
Expand Down Expand Up @@ -80,21 +75,20 @@ defmodule PhoenixVaultWeb.SnapshotLive.FormComponent do
case {action, Archive.create_snapshot(snapshot_params, socket.assigns[:current_user])} do
{:new, {:ok, snapshot}} ->
notify_parent({:saved, snapshot})

{:noreply,
socket
|> put_flash(:info, "Snapshot created successfully")
|> push_patch(to: socket.assigns.patch)}

{:new, {:error, changeset}} ->
{:noreply, assign_form(socket, changeset)}

{_, _} ->
{:noreply, socket}
end
end


defp assign_form(socket, %Ecto.Changeset{} = changeset) do
assign(socket, :form, to_form(changeset))
end
Expand Down
2 changes: 0 additions & 2 deletions lib/phoenix_vault_web/live/snapshot_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ defmodule PhoenixVaultWeb.SnapshotLive.Index do
{:noreply, stream_insert(socket, :snapshots, updated_snapshot, limit: @per_page)}

{:error, %Ecto.Changeset{} = _changeset} ->


{:noreply, socket}
end
end
Expand Down
8 changes: 2 additions & 6 deletions lib/phoenix_vault_web/live/snapshot_viewer_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ defmodule PhoenixVaultWeb.SnapshotViewerLive do

@impl true
def handle_event("next", _unsigned_params, socket) do


with next_snapshot when not is_nil(next_snapshot) <-
Archive.get_next_snapshot(socket.assigns.current_snapshot) do
query_params =
Expand All @@ -126,8 +124,6 @@ defmodule PhoenixVaultWeb.SnapshotViewerLive do

@impl true
def handle_event("prev", _unsigned_params, socket) do


with prev_snapshot when not is_nil(prev_snapshot) <-
Archive.get_prev_snapshot(socket.assigns.current_snapshot) do
query_params =
Expand All @@ -144,7 +140,7 @@ defmodule PhoenixVaultWeb.SnapshotViewerLive do
_ -> {:noreply, socket}
end
end

@impl true
def handle_event("navigate-snapshot", %{"key" => key} = params, socket) do
case key do
Expand All @@ -153,7 +149,7 @@ defmodule PhoenixVaultWeb.SnapshotViewerLive do
_ -> {:noreply, socket}
end
end

@impl true
def handle_event("navigate-snapshot", _, socket) do
{:noreply, socket}
Expand Down
4 changes: 2 additions & 2 deletions lib/phoenix_vault_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ defmodule PhoenixVaultWeb.Router do

live "/snapshots/:id", SnapshotLive.Show, :show
live "/snapshots/:id/show/edit", SnapshotLive.Show, :edit

# viewer routes
live "/snapshots/view/:id/", SnapshotViewerLive, :show
end
end

# Enable LiveDashboard and Swoosh mailbox preview in development
if Application.compile_env(:phoenix_vault, :dev_routes) do
# If you want to use the LiveDashboard in production, you should put
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule PhoenixVault.Repo.Migrations.AddObanJobsTable do
def up() do
Oban.Migration.up(version: 12)
end

def down() do
Oban.Migration.down(version: 1)
end
Expand Down
5 changes: 2 additions & 3 deletions test/phoenix_vault_web/live/snapshot_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ defmodule PhoenixVaultWeb.SnapshotLiveTest do
snapshot = snapshot_fixture(nil, user)
%{snapshot: snapshot}
end

defp register_and_login(%{conn: conn}) do
%{conn: _conn, user: _user} = register_and_log_in_user(%{conn: conn})
end

describe "Index" do
setup [:register_and_login, :create_snapshot]

test "lists all snapshots", %{conn: conn} do

{:ok, _index_live, html} = live(conn, ~p"/snapshots")

assert html =~ "Listing Snapshots"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ defmodule PhoenixVaultWeb.UserConfirmationLiveTest do

test "does not confirm email with invalid token", %{conn: conn, user: user} do
conn = log_in_user(conn, user)

{:ok, lv, _html} = live(conn, ~p"/users/confirm/invalid-token")

{:ok, conn} =
Expand Down
15 changes: 8 additions & 7 deletions test/support/fixtures/archive_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ defmodule PhoenixVault.ArchiveFixtures do
else
user
end

attrs = if is_nil(attrs) do
%{
"title" => "sample",
"url" => "https://example.com"
}
end

attrs =
if is_nil(attrs) do
%{
"title" => "sample",
"url" => "https://example.com"
}
end

{:ok, snapshot} =
attrs
Expand Down

0 comments on commit 30acadf

Please sign in to comment.