File size: 10,817 Bytes
88211d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
defmodule PlausibleWeb.Live.CSVImport do
  @moduledoc """
  LiveView allowing uploading CSVs for imported tables to S3 or local storage
  """

  use PlausibleWeb, :live_view

  alias Plausible.Imported.CSVImporter
  alias Plausible.Imported

  # :not_mounted_at_router ensures we have already done auth checks in the controller
  # if this liveview becomes available from the router, please make sure
  # to check that site_role is allowed to make site imports
  @impl true
  def mount(:not_mounted_at_router, session, socket) do
    %{"site_id" => site_id, "storage" => storage} = session

    upload_opts = [
      accept: [".csv", "text/csv"],
      auto_upload: true,
      max_entries: length(Imported.tables()),
      # 1GB
      max_file_size: 1_000_000_000,
      progress: &handle_progress/3
    ]

    upload_opts =
      case storage do
        "s3" -> [{:external, &presign_upload/2} | upload_opts]
        "local" -> upload_opts
      end

    upload_consumer =
      case storage do
        "s3" ->
          fn meta, entry ->
            {:ok, %{"s3_url" => meta.s3_url, "filename" => entry.client_name}}
          end

        "local" ->
          local_dir = CSVImporter.local_dir(site_id)
          File.mkdir_p!(local_dir)

          fn meta, entry ->
            local_path = Path.join(local_dir, Path.basename(meta.path))
            Plausible.File.mv!(meta.path, local_path)
            {:ok, %{"local_path" => local_path, "filename" => entry.client_name}}
          end
      end

    %{assigns: %{site: site}} =
      socket = assign_new(socket, :site, fn -> Plausible.Repo.get!(Plausible.Site, site_id) end)

    # we'll listen for new completed imports to know
    # when to reload the occupied ranges
    if connected?(socket), do: Imported.listen()

    occupied_ranges = Imported.get_occupied_date_ranges(site)
    native_stats_start_date = Plausible.Sites.native_stats_start_date(site)

    socket =
      socket
      |> assign(
        site_id: site_id,
        storage: storage,
        upload_consumer: upload_consumer,
        occupied_ranges: occupied_ranges,
        native_stats_start_date: native_stats_start_date
      )
      |> allow_upload(:import, upload_opts)
      |> process_imported_tables()

    {:ok, socket}
  end

  @impl true
  def render(assigns) do
    ~H"""
    <div>
      <form action="#" method="post" phx-change="validate-upload-form" phx-submit="submit-upload-form">
        <.csv_picker upload={@uploads.import} imported_tables={@imported_tables} />
        <.confirm_button date_range={@clamped_date_range} can_confirm?={@can_confirm?} />
        <.maybe_date_range_warning
          :if={@original_date_range}
          clamped={@clamped_date_range}
          original={@original_date_range}
        />
        <p :for={error <- upload_errors(@uploads.import)} class="text-red-400">
          {error_to_string(error)}
        </p>
      </form>
    </div>
    """
  end

  defp csv_picker(assigns) do
    ~H"""
    <label
      phx-drop-target={@upload.ref}
      class="block border border-gray-300 dark:border-gray-750 dark:bg-gray-750 rounded-md p-4 transition cursor-pointer"
    >
      <div class="hidden md:flex items-center text-gray-500 dark:text-gray-400">
        <Heroicons.document_plus class="size-5 transition" />
        <span class="ml-1.5 text-sm">
          (or drag-and-drop your unzipped CSVs here)
        </span>
        <.live_file_input upload={@upload} class="hidden" />
      </div>

      <ul id="imported-tables" class="truncate mt-3.5 mb-0.5 space-y-1.5">
        <.imported_table
          :for={{table, upload} <- @imported_tables}
          table={table}
          upload={upload}
          errors={if(upload, do: upload_errors(@upload, upload), else: [])}
        />
      </ul>
    </label>
    """
  end

  defp confirm_button(assigns) do
    ~H"""
    <.button type="submit" disabled={not @can_confirm?} class="w-full">
      <%= if @date_range do %>
        Confirm import <.dates range={@date_range} />
      <% else %>
        Confirm import
      <% end %>
    </.button>
    """
  end

  defp maybe_date_range_warning(assigns) do
    ~H"""
    <%= if @clamped do %>
      <.notice :if={@clamped != @original} title="Dates Adjusted" theme={:yellow} class="mt-4">
        The dates <.dates range={@original} />
        overlap with previous imports, so we'll use the next best period, <.dates range={@clamped} />
      </.notice>
    <% else %>
      <.notice title="Dates Conflict" theme={:red} class="mt-4">
        The dates <.dates range={@original} />
        overlap with existing site data and cannot be used for a new import.
      </.notice>
    <% end %>
    """
  end

  defp dates(assigns) do
    ~H"""
    <span class="whitespace-nowrap">
      <span class="font-medium">{@range.first}</span>
      to <span class="font-medium">{@range.last}</span>
    </span>
    """
  end

  defp imported_table(assigns) do
    status =
      cond do
        assigns.upload && assigns.upload.progress == 100 -> :success
        assigns.upload && assigns.upload.progress > 0 -> :in_progress
        not Enum.empty?(assigns.errors) -> :error
        true -> :empty
      end

    assigns = assign(assigns, status: status)

    ~H"""
    <li id={@table} class="ml-0.5">
      <div class="flex items-center space-x-2 text-gray-600 dark:text-gray-500">
        <Heroicons.document_check :if={@status == :success} class="w-4 h-4" />
        <.spinner :if={@status == :in_progress} class="w-4 h-4" />
        <Heroicons.document :if={@status == :empty} class="w-4 h-4 opacity-80" />
        <Heroicons.document :if={@status == :error} class="w-4 h-4 text-red-600 dark:text-red-700" />

        <span class={[
          "text-sm",
          if(@status == :empty, do: "opacity-80"),
          if(@status == :error, do: "text-red-600 dark:text-red-700")
        ]}>
          <%= if @upload do %>
            {@upload.client_name}
          <% else %>
            {@table}_YYYYMMDD_YYYYMMDD.csv
          <% end %>
        </span>
      </div>

      <p :for={error <- @errors} class="ml-6 text-sm text-red-600 dark:text-red-700">
        {error_to_string(error)}
      </p>
    </li>
    """
  end

  @impl true
  def handle_event("validate-upload-form", _params, socket) do
    {:noreply, process_imported_tables(socket)}
  end

  def handle_event("submit-upload-form", _params, socket) do
    %{
      storage: storage,
      site: site,
      current_user: current_user,
      clamped_date_range: clamped_date_range,
      upload_consumer: upload_consumer
    } =
      socket.assigns

    uploads = consume_uploaded_entries(socket, :import, upload_consumer)

    {:ok, _job} =
      CSVImporter.new_import(site, current_user,
        start_date: clamped_date_range.first,
        end_date: clamped_date_range.last,
        uploads: uploads,
        storage: storage
      )

    redirect_to =
      Routes.site_path(socket, :settings_imports_exports, site.domain)

    {:noreply, redirect(socket, to: redirect_to)}
  end

  @impl true
  def handle_info({:notification, :analytics_imports_jobs, details}, socket) do
    site = socket.assigns.site

    socket =
      if details["site_id"] == site.id and details["event"] == "complete" do
        occupied_ranges = Imported.get_occupied_date_ranges(site)
        socket |> assign(occupied_ranges: occupied_ranges) |> process_imported_tables()
      else
        socket
      end

    {:noreply, socket}
  end

  defp error_to_string(:too_large), do: "is too large (max size is 1 gigabyte)"
  defp error_to_string(:too_many_files), do: "too many files"
  defp error_to_string(:not_accepted), do: "unacceptable file types"
  defp error_to_string(:external_client_failure), do: "browser upload failed"

  defp presign_upload(entry, socket) do
    %{s3_url: s3_url, presigned_url: upload_url} =
      Plausible.S3.import_presign_upload(socket.assigns.site_id, random_suffix(entry.client_name))

    {:ok, %{uploader: "S3", s3_url: s3_url, url: upload_url}, socket}
  end

  defp random_suffix(filename) do
    # based on Plug.Upload.path/2
    # https://github.com/elixir-plug/plug/blob/eabf0b9d43060c10663a9105cb1baf984d272a6c/lib/plug/upload.ex#L154-L159
    sec = Integer.to_string(:os.system_time(:second))
    rand = Integer.to_string(:rand.uniform(999_999_999_999))
    scheduler_id = Integer.to_string(:erlang.system_info(:scheduler_id))
    filename <> "-" <> sec <> "-" <> rand <> "-" <> scheduler_id
  end

  defp handle_progress(:import, entry, socket) do
    if entry.done? do
      {:noreply, process_imported_tables(socket)}
    else
      {:noreply, socket}
    end
  end

  defp process_imported_tables(socket) do
    tables = Imported.tables()
    {completed, in_progress} = uploaded_entries(socket, :import)

    {valid_uploads, invalid_uploads} =
      Enum.split_with(completed ++ in_progress, &CSVImporter.valid_filename?(&1.client_name))

    imported_tables_all_uploads =
      Enum.map(tables, fn table ->
        uploads =
          Enum.filter(valid_uploads, fn upload ->
            CSVImporter.extract_table(upload.client_name) == table
          end)

        {upload, replaced_uploads} = List.pop_at(uploads, -1)
        {table, upload, replaced_uploads}
      end)

    imported_tables =
      Enum.map(imported_tables_all_uploads, fn {table, upload, _replaced_uploads} ->
        {table, upload}
      end)

    replaced_uploads =
      Enum.flat_map(imported_tables_all_uploads, fn {_table, _upload, replaced_uploads} ->
        replaced_uploads
      end)

    original_date_range = CSVImporter.date_range(Enum.map(valid_uploads, & &1.client_name))

    clamped_date_range =
      if original_date_range do
        %Date.Range{first: start_date, last: end_date} = original_date_range

        %{
          site: site,
          occupied_ranges: occupied_ranges,
          native_stats_start_date: native_stats_start_date
        } = socket.assigns

        cutoff_date = native_stats_start_date || Plausible.Times.today(site.timezone)

        case Imported.clamp_dates(occupied_ranges, cutoff_date, start_date, end_date) do
          {:ok, start_date, end_date} -> Date.range(start_date, end_date)
          {:error, :no_time_window} -> nil
        end
      end

    all_uploaded? = completed != [] and in_progress == []
    can_confirm? = all_uploaded? and not is_nil(clamped_date_range)

    socket
    |> cancel_uploads(invalid_uploads)
    |> cancel_uploads(replaced_uploads)
    |> assign(
      imported_tables: imported_tables,
      can_confirm?: can_confirm?,
      original_date_range: original_date_range,
      clamped_date_range: clamped_date_range
    )
  end

  defp cancel_uploads(socket, uploads) do
    Enum.reduce(uploads, socket, fn upload, socket ->
      cancel_upload(socket, :import, upload.ref)
    end)
  end
end