-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain_live.ex
More file actions
277 lines (235 loc) · 8.56 KB
/
main_live.ex
File metadata and controls
277 lines (235 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
defmodule DashboardWeb.Live.MainLive do
use DashboardWeb, :live_view
import Dashboard.Helpers
alias DashBoard.Config
alias DashBoard.DbAttribute
alias Csv2sql.Database.ConnectionTest
alias DashboardWeb.Live.ConfigLive
alias DashboardWeb.Live.StartLive
@debounce_time 1000
@impl true
def mount(_params, _session, socket) do
local_storage_config = (get_connect_params(socket) || %{}) |> Map.get("localConfig", %{})
local_storage_config =
for {key, val} <- local_storage_config, into: %{}, do: {String.to_existing_atom(key), val}
# Check for DB connection on config load from local storage
timer_ref = Process.send_after(self(), :check_db_connection, @debounce_time)
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
def handle_event("page-change", ~m{page}, socket) do
{:noreply, assign(socket, ~M{page})}
end
@impl true
def handle_event("open-modal", ~m{modal}, socket) do
{:noreply, assign(socket, :modal, modal)}
end
@impl true
def handle_event("close-modal", _attrs, socket) do
{:noreply, assign(socket, :modal, false)}
end
@impl true
def handle_event("validate-and-save", attrs, socket) do
args = Map.get(attrs, "config", %{})
socket =
socket
|> assign(
page: "config",
changeset: Config.changeset(args)
)
# DB connection checker is expensive and returns result to caller process with delay
# so we don't do this validation on changeset level
|> db_connection_checker(args)
|> update_matching_date_time(attrs)
{:noreply, socket |> push_event("save-config", %{config: socket.assigns.changeset})}
end
@impl true
def handle_event("add-new-" <> field, _attrs, ~M{assigns} = socket)
when field in ~w[db-attr date-pattern date-time-pattern] do
new_field =
field
|> String.replace("-", "_")
|> String.to_atom()
|> case do
:db_attr -> %DbAttribute{id: Nanoid.generate(), name: "", value: ""}
:date_pattern -> %DashBoard.DatePattern{id: Nanoid.generate()}
:date_time_pattern -> %DashBoard.DateTimePattern{id: Nanoid.generate()}
end
association = "#{field}s" |> String.replace("-", "_") |> String.to_atom()
updated_association =
assigns.changeset
|> Ecto.Changeset.get_field(association, [])
|> Enum.concat([new_field])
updated_changeset =
Ecto.Changeset.put_embed(assigns.changeset, association, updated_association)
{:noreply,
socket
|> assign(changeset: updated_changeset)
|> push_event("scroll-to-bottom", %{id: "#{field}s-container"})}
end
@impl true
def handle_event("remove-" <> field, ~m{attrid}, ~M{assigns} = socket)
when field in ~w[db-attr date-pattern date-time-pattern] do
association = "#{field}s" |> String.replace("-", "_") |> String.to_atom()
updated_association =
assigns.changeset
# For relations get_change/2 return the original changeset data with changes applied, fetch_change!/2 returns raw db_config changesets
|> Ecto.Changeset.fetch_change!(association)
|> Enum.reject(fn embed_changeset ->
Ecto.Changeset.get_field(embed_changeset, :id) == attrid
end)
updated_changeset =
Ecto.Changeset.put_embed(assigns.changeset, association, updated_association)
{:noreply,
socket
|> assign(changeset: updated_changeset)
|> 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(
db_url = create_db_url(assigns.changeset.changes, false),
true <- not ("NA" == db_url),
db_type <- Ecto.Changeset.get_field(assigns.changeset, :db_type),
false <- is_nil(db_type),
args = %{db_type: db_type, db_url: db_url},
resp = ConnectionTest.check_db_connection(self(), args),
:ok <- resp
) do
socket
else
{:error, :on_going} ->
Process.send_after(self(), :check_db_connection, @debounce_time)
socket
_ ->
assign(socket, db_connection_established: false)
end
{:noreply, socket}
end
# DB connection callbacks
@impl true
def handle_info({:connected, _}, socket),
do: {:noreply, assign(socket, db_connection_established: true)}
@impl true
def handle_info({:error, _}, ~M{assigns} = socket) do
{:noreply,
assign(
socket,
changeset:
Ecto.Changeset.add_error(assigns.changeset, :db_url, "Could not connect to database"),
db_connection_established: false
)}
end
defp render_page(assigns) do
case assigns.page do
"config" ->
ConfigLive.config_page(assigns)
"start" ->
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"""
About
"""
end
end
defp db_connection_checker(~M{assigns} = socket, args) do
if db_config_updated?(assigns, args) do
if assigns.db_connection_debouncer,
do: Process.cancel_timer(assigns.db_connection_debouncer)
timer_ref = Process.send_after(self(), :check_db_connection, @debounce_time)
assign(socket, :db_connection_debouncer, timer_ref)
else
socket
end
end
defp db_config_updated?(~M{changeset}, args) do
# TODO: Take into account custom db params
Ecto.Changeset.get_field(changeset, :db_type) != Map.get(args, "db_type") ||
Ecto.Changeset.get_field(changeset, :db_username) != Map.get(args, "db_username") ||
Ecto.Changeset.get_field(changeset, :db_password) != Map.get(args, "db_password") ||
Ecto.Changeset.get_field(changeset, :db_host) != Map.get(args, "db_host") ||
Ecto.Changeset.get_field(changeset, :db_name) != Map.get(args, "db_name")
end
defp update_matching_date_time(~M{assigns} = socket, attrs \\ %{}) do
date_time_sample =
get_in(attrs, ["config", "date_time_trial"]) ||
Ecto.Changeset.get_field(assigns.changeset, :date_time_trial)
case match_date_time(assigns.changeset, date_time_sample) do
{type, index} ->
socket
|> assign(matching_date_time: {type, index})
|> push_event("scroll-into-view", %{
id: "config_#{type}_patterns_#{index}_pattern"
})
false ->
assign(socket, matching_date_time: nil)
end
end
end