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

Updated start page to run application #37

Open
wants to merge 3 commits into
base: version-4.0
Choose a base branch
from
Open
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
14 changes: 9 additions & 5 deletions apps/csv2sql/lib/csv2sql/config/loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,16 @@ defmodule Csv2sql.Config.Loader do
end

defp load_db_config(args) do
db_type = get_db_type(args)
db_type = args[:db_type]
db_url = args[:db_url]
# db_type = get_db_type(args)

db_url =
if args[:db_url],
do: "ecto://#{args[:db_url]}",
else: raise("Please provide a valid database url")
# db_type = args[:db_type]

# db_url = args[:db_url]
# if args[:db_url],
# do: "ecto://#{args[:db_url]}",
# else: raise("Please provide a valid database url")

varchar_limit = get_varchar_limit(args)
insertion_chunk_size = get_insertion_chunk_size(args)
Expand Down
92 changes: 78 additions & 14 deletions apps/dashboard/lib/dashboard_web/live/main_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule DashboardWeb.Live.MainLive do
alias DashBoard.DbAttribute
alias Csv2sql.Database.ConnectionTest
alias DashboardWeb.Live.ConfigLive
alias DashboardWeb.Live.StartLive

@debounce_time 1000

Expand All @@ -18,17 +19,50 @@ defmodule DashboardWeb.Live.MainLive do
# Check for DB connection on config load from local storage
timer_ref = Process.send_after(self(), :check_db_connection, @debounce_time)

{:ok,
assign(socket,
page: "config",
modal: false,
path_validator_debouncer: nil,
db_connection_debouncer: timer_ref,
db_connection_established: false,
changeset: Config.get_defaults() |> Map.merge(local_storage_config) |> Config.changeset(),
matching_date_time: nil,
constraints: Csv2sql.Config.Loader.get_constraints()
)}
case Csv2sql.ProgressTracker.get_state().status do
{:error, %{message: message}} ->
{:ok,
assign(socket,
page: "start",
modal: false,
path_validator_debouncer: nil,
db_connection_debouncer: timer_ref,
db_connection_established: false,
changeset:
Config.get_defaults() |> Map.merge(local_storage_config) |> Config.changeset(),
matching_date_time: nil,
constraints: Csv2sql.Config.Loader.get_constraints(),
error: true,
reason: String.split(message, "\n") |> Enum.filter(fn s -> s not in ["", nil] end),
state: Csv2sql.ProgressTracker.get_state()
)}

_ ->
{:ok,
assign(socket,
page: "start",
modal: false,
path_validator_debouncer: nil,
db_connection_debouncer: timer_ref,
db_connection_established: false,
changeset:
Config.get_defaults() |> Map.merge(local_storage_config) |> Config.changeset(),
matching_date_time: nil,
constraints: Csv2sql.Config.Loader.get_constraints(),
state: Csv2sql.ProgressTracker.get_state()
)}
end
end

def handle_event("csv-parse", _attrs, socket) do
try do
Csv2sql.Stages.Analyze.analyze_files()
Process.send_after(self(), :get_status, 2000)
{:noreply, assign(socket, page: "start", state: Csv2sql.ProgressTracker.get_state())}
catch
err ->
{:noreply, assign(socket, page: "start", error: true)}
end
end

@impl true
Expand Down Expand Up @@ -115,6 +149,28 @@ defmodule DashboardWeb.Live.MainLive do
|> update_matching_date_time()}
end

@impl true
def handle_info(:get_status, socket) do
IO.inspect(Csv2sql.ProgressTracker.get_state().status)

case Csv2sql.ProgressTracker.get_state().status do
:finish ->
{:noreply, assign(socket, page: "start", state: Csv2sql.ProgressTracker.get_state())}

{:error, %{message: message}} ->
{:ok,
assign(socket,
page: "start",
error: true,
reason: String.split(message, "\n") |> Enum.filter(fn s -> s not in ["", nil] end),
state: Csv2sql.ProgressTracker.get_state()
)}

_ ->
Process.send_after(self(), :get_status, 2000)
end
end

@impl true
def handle_info(:check_db_connection, ~M{assigns} = socket) do
with(
Expand Down Expand Up @@ -161,9 +217,17 @@ defmodule DashboardWeb.Live.MainLive do
ConfigLive.config_page(assigns)

"start" ->
~H"""
Placeholder
"""
Map.put(assigns.changeset.changes, :dashboard, true)

Csv2sql.Config.Loader.load(
Map.put(
assigns.changeset.changes,
:db_url,
create_db_url(assigns.changeset.changes, false)
)
)

StartLive.start_parse(assigns)

"about" ->
~H"""
Expand Down
43 changes: 43 additions & 0 deletions apps/dashboard/lib/dashboard_web/live/start_live.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule DashboardWeb.Live.StartLive do
use DashboardWeb, :component

def start_parse(assigns) do
~H"""
<h2 class="text-center mb-4 mt-2">Start Parse</h2>
<div class="w-100 d-flex justify-content-center align-items-center">
<button class = "btn btn-primary px-4 text-center" phx-click="csv-parse">Start</button>
</div>
<%= case Map.get(assigns, :state).status do %>
<% :init -> %>
<p class="mt-2 text-center text-info"> Click on Start to parse CSVs </p>
<% :working -> %>
<p class="mt-2 text-center text-info"> Analyzing files it may take some time </p>
<% :finish -> %>
<p class="mt-2 text-center text-success"> The csv sucessfully parsed </p>
<% _ -> %>
<p class="alert alert-danger text-center mt-2">There was a problem while parsing</p>
<ul>
<%= Enum.map(Map.get(assigns, :reason,[]), fn s -> %>
<li class="list-group-item"><%=s%></li>
<% end) %>
</ul>
<% end %>
<%= if Map.get(assigns, :state).status != :init do%>
<table class = "table w-75 m-2 table-bordered border-dark">
<tr>
<th> File name </th>
<th> rows_processed </th>
<th> row_count </th>
</tr>
<%= Enum.map(Map.get(assigns, :state).files, fn {_path, file} -> %>
<tr>
<td> <%= file.name %> </td>
<td> <%= file.rows_processed %> </td>
<td> <%= file.row_count %> </td>
</tr>
<% end) %>
</table>
<% end %>
"""
end
end