11defmodule Plausible.Imported.CSVImporter do
22 @ moduledoc """
3- CSV importer stub.
3+ CSV importer from S3 that uses ClickHouse [s3 table function.](https://clickhouse.com/docs/en/sql-reference/table-functions/s3)
44 """
55
66 use Plausible.Imported.Importer
@@ -16,10 +16,99 @@ defmodule Plausible.Imported.CSVImporter do
1616 def email_template ( ) , do: "google_analytics_import.html"
1717
1818 @ impl true
19- def parse_args ( % { "s3_path " => s3_path } ) , do: [ s3_path: s3_path ]
19+ def parse_args ( % { "uploads " => uploads } ) , do: [ uploads: uploads ]
2020
2121 @ impl true
22- def import_data ( _site_import , _opts ) do
23- :ok
22+ def import_data ( site_import , opts ) do
23+ % { id: import_id , site_id: site_id } = site_import
24+ uploads = Keyword . fetch! ( opts , :uploads )
25+
26+ % { access_key_id: s3_access_key_id , secret_access_key: s3_secret_access_key } =
27+ Plausible.S3 . import_clickhouse_credentials ( )
28+
29+ { :ok , ch } =
30+ Plausible.IngestRepo . config ( )
31+ |> Keyword . replace! ( :pool_size , 1 )
32+ |> Ch . start_link ( )
33+
34+ ranges =
35+ Enum . map ( uploads , fn upload ->
36+ % { "filename" => filename , "s3_url" => s3_url } = upload
37+
38+ ".csv" = Path . extname ( filename )
39+ table = Path . rootname ( filename )
40+ ensure_importable_table! ( table )
41+
42+ s3_structure = input_structure! ( table )
43+
44+ statement =
45+ """
46+ INSERT INTO {table:Identifier} \
47+ SELECT {site_id:UInt64} AS site_id, *, {import_id:UInt64} AS import_id \
48+ FROM s3({s3_url:String},{s3_access_key_id:String},{s3_secret_access_key:String},{s3_format:String},{s3_structure:String})\
49+ """
50+
51+ params =
52+ % {
53+ "table" => table ,
54+ "site_id" => site_id ,
55+ "import_id" => import_id ,
56+ "s3_url" => s3_url ,
57+ "s3_access_key_id" => s3_access_key_id ,
58+ "s3_secret_access_key" => s3_secret_access_key ,
59+ "s3_format" => "CSVWithNames" ,
60+ "s3_structure" => s3_structure
61+ }
62+
63+ Ch . query! ( ch , statement , params , timeout: :infinity )
64+
65+ % Ch.Result { rows: [ [ min_date , max_date ] ] } =
66+ Ch . query! (
67+ ch ,
68+ "SELECT min(date), max(date) FROM {table:Identifier} WHERE site_id = {site_id:UInt64} AND import_id = {import_id:UInt64}" ,
69+ % { "table" => table , "site_id" => site_id , "import_id" => import_id }
70+ )
71+
72+ Date . range ( min_date , max_date )
73+ end )
74+
75+ { :ok ,
76+ % {
77+ start_date: Enum . min_by ( ranges , & & 1 . first , Date ) . first ,
78+ end_date: Enum . max_by ( ranges , & & 1 . last , Date ) . last
79+ } }
80+ rescue
81+ # we are cancelling on any argument or ClickHouse errors
82+ e in [ ArgumentError , Ch.Error ] ->
83+ { :error , Exception . message ( e ) }
84+ end
85+
86+ input_structures = % {
87+ "imported_browsers" =>
88+ "date Date, browser String, visitors UInt64, visits UInt64, visit_duration UInt64, bounces UInt32" ,
89+ "imported_devices" =>
90+ "date Date, device String, visitors UInt64, visits UInt64, visit_duration UInt64, bounces UInt32" ,
91+ "imported_entry_pages" =>
92+ "date Date, entry_page String, visitors UInt64, entrances UInt64, visit_duration UInt64, bounces UInt32" ,
93+ "imported_exit_pages" => "date Date, exit_page String, visitors UInt64, exits UInt64" ,
94+ "imported_locations" =>
95+ "date Date, country String, region String, city UInt64, visitors UInt64, visits UInt64, visit_duration UInt64, bounces UInt32" ,
96+ "imported_operating_systems" =>
97+ "date Date, operating_system String, visitors UInt64, visits UInt64, visit_duration UInt64, bounces UInt32" ,
98+ "imported_pages" =>
99+ "date Date, hostname String, page String, visitors UInt64, pageviews UInt64, exits UInt64, time_on_page UInt64" ,
100+ "imported_sources" =>
101+ "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" ,
102+ "imported_visitors" =>
103+ "date Date, visitors UInt64, pageviews UInt64, bounces UInt64, visits UInt64, visit_duration UInt64"
104+ }
105+
106+ for { table , input_structure } <- input_structures do
107+ defp input_structure! ( unquote ( table ) ) , do: unquote ( input_structure )
108+ defp ensure_importable_table! ( unquote ( table ) ) , do: :ok
109+ end
110+
111+ defp ensure_importable_table! ( table ) do
112+ raise ArgumentError , "table #{ table } is not supported for data import"
24113 end
25114end
0 commit comments