Repository navigation
add csv importer #3795
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
add csv importer #3795
Changes from all commits
a06c7ff
d7ebb18
2925e89
54de248
30f3ca6
b30069a
6eab42f
c83de0a
558dd11
ab98201
3393ec5
3db1ea5
3a171d5
f912209
0c191cd
ab4f9c0
723b31f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| defmodule Plausible.Imported.CSVImporter do | ||
| @moduledoc """ | ||
| CSV importer stub. | ||
| CSV importer from S3 that uses ClickHouse [s3 table function.](https://clickhouse.com/docs/en/sql-reference/table-functions/s3) | ||
| """ | ||
|
|
||
| use Plausible.Imported.Importer | ||
|
|
@@ -16,10 +16,99 @@ defmodule Plausible.Imported.CSVImporter do | |
| def email_template(), do: "google_analytics_import.html" | ||
|
|
||
| @impl true | ||
| def parse_args(%{"s3_path" => s3_path}), do: [s3_path: s3_path] | ||
| def parse_args(%{"uploads" => uploads}), do: [uploads: uploads] | ||
|
|
||
| @impl true | ||
| def import_data(_site_import, _opts) do | ||
| :ok | ||
| def import_data(site_import, opts) do | ||
| %{id: import_id, site_id: site_id} = site_import | ||
| uploads = Keyword.fetch!(opts, :uploads) | ||
|
|
||
| %{access_key_id: s3_access_key_id, secret_access_key: s3_secret_access_key} = | ||
| Plausible.S3.import_clickhouse_credentials() | ||
|
|
||
| {:ok, ch} = | ||
| Plausible.IngestRepo.config() | ||
| |> Keyword.replace!(:pool_size, 1) | ||
| |> Ch.start_link() | ||
|
|
||
| ranges = | ||
| Enum.map(uploads, fn upload -> | ||
| %{"filename" => filename, "s3_url" => s3_url} = upload | ||
|
|
||
| ".csv" = Path.extname(filename) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll revisit this after implementing imports. I think we'd do this sort of check there. If not, I'll make it into an ArgumentError to cancel the job early. |
||
| table = Path.rootname(filename) | ||
| ensure_importable_table!(table) | ||
|
|
||
| s3_structure = input_structure!(table) | ||
|
|
||
| statement = | ||
| """ | ||
| INSERT INTO {table:Identifier} \ | ||
| SELECT {site_id:UInt64} AS site_id, *, {import_id:UInt64} AS import_id \ | ||
| FROM s3({s3_url:String},{s3_access_key_id:String},{s3_secret_access_key:String},{s3_format:String},{s3_structure:String})\ | ||
| """ | ||
|
|
||
| params = | ||
| %{ | ||
| "table" => table, | ||
| "site_id" => site_id, | ||
| "import_id" => import_id, | ||
| "s3_url" => s3_url, | ||
| "s3_access_key_id" => s3_access_key_id, | ||
| "s3_secret_access_key" => s3_secret_access_key, | ||
| "s3_format" => "CSVWithNames", | ||
| "s3_structure" => s3_structure | ||
| } | ||
|
|
||
| Ch.query!(ch, statement, params, timeout: :infinity) | ||
|
|
||
| %Ch.Result{rows: [[min_date, max_date]]} = | ||
| Ch.query!( | ||
| ch, | ||
| "SELECT min(date), max(date) FROM {table:Identifier} WHERE site_id = {site_id:UInt64} AND import_id = {import_id:UInt64}", | ||
| %{"table" => table, "site_id" => site_id, "import_id" => import_id} | ||
| ) | ||
|
|
||
| Date.range(min_date, max_date) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is something that can be tackled in a follow-up, but eventually we'll need to always know import date range before executing it. We have to determine the boundaries up front so that imported stats do not overlap with native stats. We might also provide an option to let user adjust the import date boundaries. |
||
| end) | ||
|
|
||
| {:ok, | ||
| %{ | ||
| start_date: Enum.min_by(ranges, & &1.first, Date).first, | ||
| end_date: Enum.max_by(ranges, & &1.last, Date).last | ||
| }} | ||
| rescue | ||
| # we are cancelling on any argument or ClickHouse errors | ||
| e in [ArgumentError, Ch.Error] -> | ||
|
ruslandoga marked this conversation as resolved.
Resolved
|
||
| {:error, Exception.message(e)} | ||
| end | ||
|
|
||
| input_structures = %{ | ||
| "imported_browsers" => | ||
| "date Date, browser String, visitors UInt64, visits UInt64, visit_duration UInt64, bounces UInt32", | ||
| "imported_devices" => | ||
| "date Date, device String, visitors UInt64, visits UInt64, visit_duration UInt64, bounces UInt32", | ||
| "imported_entry_pages" => | ||
| "date Date, entry_page String, visitors UInt64, entrances UInt64, visit_duration UInt64, bounces UInt32", | ||
| "imported_exit_pages" => "date Date, exit_page String, visitors UInt64, exits UInt64", | ||
| "imported_locations" => | ||
| "date Date, country String, region String, city UInt64, visitors UInt64, visits UInt64, visit_duration UInt64, bounces UInt32", | ||
| "imported_operating_systems" => | ||
| "date Date, operating_system String, visitors UInt64, visits UInt64, visit_duration UInt64, bounces UInt32", | ||
| "imported_pages" => | ||
| "date Date, hostname String, page String, visitors UInt64, pageviews UInt64, exits UInt64, time_on_page UInt64", | ||
| "imported_sources" => | ||
| "date Date, source String, utm_medium String, utm_campaign String, utm_content String, utm_term String, visitors UInt64, visits UInt64, visit_duration UInt64, bounces UInt32", | ||
| "imported_visitors" => | ||
| "date Date, visitors UInt64, pageviews UInt64, bounces UInt64, visits UInt64, visit_duration UInt64" | ||
| } | ||
|
|
||
| for {table, input_structure} <- input_structures do | ||
| defp input_structure!(unquote(table)), do: unquote(input_structure) | ||
| defp ensure_importable_table!(unquote(table)), do: :ok | ||
| end | ||
|
|
||
| defp ensure_importable_table!(table) do | ||
| raise ArgumentError, "table #{table} is not supported for data import" | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| defmodule Plausible.S3 do | ||
| @moduledoc """ | ||
| Helper functions for S3 exports/imports. | ||
| """ | ||
|
|
||
| @doc """ | ||
| Returns `access_key_id` and `secret_access_key` to be used by ClickHouse during imports from S3. | ||
| """ | ||
| @spec import_clickhouse_credentials :: | ||
| %{access_key_id: String.t(), secret_access_key: String.t()} | ||
| def import_clickhouse_credentials do | ||
| %{access_key_id: access_key_id, secret_access_key: secret_access_key} = ExAws.Config.new(:s3) | ||
| %{access_key_id: access_key_id, secret_access_key: secret_access_key} | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| defmodule Plausible.S3.Client do | ||
| @moduledoc false | ||
| @behaviour ExAws.Request.HttpClient | ||
|
|
||
| @impl true | ||
| def request(method, url, body, headers, opts) do | ||
| req = Finch.build(method, url, headers, body) | ||
|
|
||
| case Finch.request(req, Plausible.Finch, opts) do | ||
| {:ok, %Finch.Response{status: status, headers: headers, body: body}} -> | ||
| {:ok, %{status_code: status, headers: headers, body: body}} | ||
|
|
||
| {:error, reason} -> | ||
| {:error, %{reason: reason}} | ||
| end | ||
| end | ||
| end |
Uh oh!
There was an error while loading. Please reload this page.