2020-06-09 02:49:45 +00:00
|
|
|
defmodule FrenzyWeb.EditPipelineLive do
|
|
|
|
use FrenzyWeb, :live_view
|
|
|
|
use Phoenix.HTML
|
|
|
|
alias Frenzy.{Repo, Pipeline}
|
|
|
|
|
2021-08-28 15:58:22 +00:00
|
|
|
def title(%{pipeline: %Pipeline{name: name}}) do
|
|
|
|
"Edit #{name}"
|
|
|
|
end
|
|
|
|
|
2020-06-10 23:05:37 +00:00
|
|
|
@stages [
|
|
|
|
{"Filter Stage", "Frenzy.Pipeline.FilterStage"},
|
|
|
|
{"Scrape Stage", "Frenzy.Pipeline.ScrapeStage"},
|
2020-07-18 23:50:41 +00:00
|
|
|
{"Conditional Stage", "Frenzy.Pipeline.ConditionalStage"},
|
2020-07-19 03:13:42 +00:00
|
|
|
{"Gemini Scrape Stage", "Frenzy.Pipeline.GeminiScrapeStage"},
|
|
|
|
{"Render Gemini Stage", "Frenzy.Pipeline.RenderGeminiStage"}
|
2020-06-10 23:05:37 +00:00
|
|
|
]
|
|
|
|
|
2020-07-18 14:25:43 +00:00
|
|
|
def stages, do: @stages
|
|
|
|
|
2020-06-09 02:49:45 +00:00
|
|
|
@impl true
|
2020-07-18 14:44:14 +00:00
|
|
|
def mount(_, %{"pipeline_id" => pipeline_id}, socket) do
|
2020-06-09 02:49:45 +00:00
|
|
|
pipeline = Repo.get(Pipeline, pipeline_id)
|
|
|
|
|
|
|
|
{:ok,
|
|
|
|
assign(socket,
|
|
|
|
pipeline: pipeline,
|
2020-06-10 23:05:37 +00:00
|
|
|
stages: @stages
|
2020-06-09 02:49:45 +00:00
|
|
|
)}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("add_stage", %{"stage" => %{"module_name" => module}}, socket) do
|
|
|
|
pipeline = socket.assigns[:pipeline]
|
|
|
|
default_options = apply(String.to_existing_atom("Elixir." <> module), :default_opts, [])
|
|
|
|
|
|
|
|
changeset =
|
|
|
|
Pipeline.changeset(pipeline, %{
|
|
|
|
stages:
|
|
|
|
pipeline.stages ++
|
|
|
|
[%{"module_name" => module, "options" => default_options}]
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, pipeline} = Repo.update(changeset)
|
|
|
|
{:noreply, assign(socket, pipeline: pipeline)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("delete_stage", %{"index" => index}, socket) do
|
|
|
|
pipeline = socket.assigns.pipeline
|
|
|
|
index = String.to_integer(index)
|
|
|
|
|
|
|
|
changeset =
|
|
|
|
Pipeline.changeset(pipeline, %{
|
|
|
|
stages: List.delete_at(pipeline.stages, index)
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, pipeline} = Repo.update(changeset)
|
|
|
|
{:noreply, assign(socket, pipeline: pipeline)}
|
|
|
|
end
|
|
|
|
|
2020-06-09 03:27:40 +00:00
|
|
|
def handle_event(event, %{"index" => index}, socket) when event in ["move_up", "move_down"] do
|
|
|
|
index = String.to_integer(index)
|
|
|
|
|
|
|
|
offset =
|
|
|
|
case event do
|
|
|
|
"move_up" -> -1
|
|
|
|
"move_down" -> 1
|
|
|
|
end
|
|
|
|
|
|
|
|
pipeline = socket.assigns.pipeline
|
|
|
|
stage = Enum.at(pipeline.stages, index)
|
|
|
|
|
|
|
|
changeset =
|
|
|
|
Pipeline.changeset(pipeline, %{
|
|
|
|
stages:
|
|
|
|
pipeline.stages
|
|
|
|
|> List.delete_at(index)
|
|
|
|
|> List.insert_at(index + offset, stage)
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, pipeline} = Repo.update(changeset)
|
|
|
|
{:noreply, assign(socket, pipeline: pipeline)}
|
|
|
|
end
|
|
|
|
|
2020-06-09 02:49:45 +00:00
|
|
|
@impl true
|
2020-06-10 23:05:37 +00:00
|
|
|
def handle_info({:update_stage, index, new_stage}, socket) do
|
2020-06-09 02:49:45 +00:00
|
|
|
pipeline = socket.assigns.pipeline
|
2020-06-10 23:05:37 +00:00
|
|
|
new_stages = List.replace_at(pipeline.stages, index, new_stage)
|
2020-06-09 02:49:45 +00:00
|
|
|
changeset = Pipeline.changeset(pipeline, %{stages: new_stages})
|
|
|
|
{:ok, pipeline} = Repo.update(changeset)
|
|
|
|
{:noreply, assign(socket, pipeline: pipeline)}
|
|
|
|
end
|
|
|
|
|
2020-07-18 16:22:16 +00:00
|
|
|
def component_module(module_name) do
|
|
|
|
case module_name do
|
|
|
|
"Frenzy.Pipeline.ScrapeStage" ->
|
|
|
|
FrenzyWeb.ConfigureStage.ScrapeStageLive
|
2020-06-09 02:49:45 +00:00
|
|
|
|
2020-07-18 16:22:16 +00:00
|
|
|
"Frenzy.Pipeline.FilterStage" ->
|
|
|
|
FrenzyWeb.ConfigureStage.FilterStageLive
|
2020-07-18 14:25:43 +00:00
|
|
|
|
2020-07-18 16:22:16 +00:00
|
|
|
"Frenzy.Pipeline.ConditionalStage" ->
|
|
|
|
FrenzyWeb.ConfigureStage.ConditionalStageLive
|
2020-06-10 23:05:37 +00:00
|
|
|
|
2020-07-18 16:22:16 +00:00
|
|
|
_ ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2020-06-09 02:49:45 +00:00
|
|
|
|
2020-07-18 16:22:16 +00:00
|
|
|
def component_for(socket, %{"module_name" => module} = stage, index) do
|
|
|
|
case component_module(module) do
|
2020-06-09 02:49:45 +00:00
|
|
|
nil ->
|
|
|
|
nil
|
|
|
|
|
|
|
|
component ->
|
2020-06-10 23:05:37 +00:00
|
|
|
live_component(socket, component,
|
|
|
|
index: index,
|
|
|
|
id: "stage-#{index}",
|
|
|
|
stage: stage,
|
|
|
|
keypath: ["options"]
|
|
|
|
)
|
2020-06-09 02:49:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|