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

1018 data dictionary refactor #1654

Merged
merged 13 commits into from
Apr 12, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,13 @@ defmodule AndiWeb.IngestionLiveView.DataDictionaryForm do
{:ok, assign(socket, loading_schema: false, current_data_dictionary_item: :no_dictionary)}

false ->
{:ok, assign(socket, loading_schema: false, pending_schema: decoded_json, overwrite_schema_visible?: true)}
{:ok,
assign(socket,
loading_schema: false,
pending_schema: decoded_json,
pending_file_type: "application/json",
overwrite_schema_visible?: true
)}
end
end
end
Expand All @@ -357,7 +363,7 @@ defmodule AndiWeb.IngestionLiveView.DataDictionaryForm do
{:ok, assign(socket, schema_sample_errors: "There was a problem interpreting this file")}

{:ok, csv_file} ->
decoded_csv = parse_sv_file(csv_file, file_type)
decoded_file = parse_sv_file(csv_file, file_type)

is_schema_empty? =
case Changeset.fetch_field(socket.assigns.changeset, :schema) do
Expand All @@ -369,7 +375,7 @@ defmodule AndiWeb.IngestionLiveView.DataDictionaryForm do
case is_schema_empty? do
true ->
changeset =
decoded_csv
decoded_file
|> List.wrap()
|> DataDictionaryFormSchema.changeset_from_tuple_list(socket.assigns.ingestion_id)

Expand All @@ -378,7 +384,13 @@ defmodule AndiWeb.IngestionLiveView.DataDictionaryForm do
{:ok, assign(socket, loading_schema: false, current_data_dictionary_item: :no_dictionary)}

false ->
{:ok, assign(socket, loading_schema: false, pending_schema: decoded_csv, overwrite_schema_visible?: true)}
{:ok,
assign(socket,
loading_schema: false,
pending_schema: decoded_file,
pending_file_type: file_type,
overwrite_schema_visible?: true
)}
end
end
end
Expand All @@ -392,10 +404,15 @@ defmodule AndiWeb.IngestionLiveView.DataDictionaryForm do
end

def update(%{action: :overwrite_schema}, socket) do
schema_changesets =
schema_list =
socket.assigns.pending_schema
|> List.wrap()
|> DataDictionaryFormSchema.changeset_from_file(socket.assigns.ingestion_id)

schema_changesets =
case socket.assigns.pending_file_type do
"application/json" -> DataDictionaryFormSchema.changeset_from_file(schema_list, socket.assigns.ingestion_id)
_ -> DataDictionaryFormSchema.changeset_from_tuple_list(schema_list, socket.assigns.ingestion_id)
end

send(self(), {:update_data_dictionary, schema_changesets})

Expand Down Expand Up @@ -553,17 +570,17 @@ defmodule AndiWeb.IngestionLiveView.DataDictionaryForm do
end

defp parse_sv_file(file_string, file_type) do
regex =
{regex, split_text} =
case file_type do
"text/csv" -> ~r/[^[:alnum:] _,]/
"text/plain" -> ~r/[^[:alnum:] _\t]/
"text/csv" -> {~r/[^[:alnum:] _,]/, ","}
_ -> {~r/[^[:alnum:] _\t]/, "\t"}
end

file_string
|> String.split("\n")
|> Enum.take(2)
|> List.update_at(0, &String.replace(&1, regex, "", global: true))
|> Enum.map(fn row -> String.split(row, "\t") end)
|> Enum.map(fn row -> String.split(row, split_text) end)
|> Enum.zip()
|> Enum.map(fn {k, v} -> {k, convert_value(v)} end)
end
Expand Down
2 changes: 1 addition & 1 deletion apps/andi/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Andi.MixProject do
def project do
[
app: :andi,
version: "2.6.56",
version: "2.6.57",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
Expand Down