Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ postgres-stop: ## Stop and remove the postgres container
docker stop plausible_db && docker rm plausible_db

minio: ## Start a transient container with a recent version of minio (s3)
docker run -d --rm -p 6000:6000 -p 6001:6001 --name plausible_minio minio/minio server /data --address ":6000" --console-address ":6001"
docker run -d --rm -p 10000:10000 -p 10001:10001 --name plausible_minio minio/minio server /data --address ":10000" --console-address ":10001"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More info on switching from 6000 to 10000 #3845 (comment)

while ! docker exec plausible_minio mc alias set local http://localhost:10000 minioadmin minioadmin; do sleep 1; done
docker exec plausible_minio mc mb local/dev-exports
docker exec plausible_minio mc mb local/dev-imports

minio-stop: ## Stop and remove the minio container
docker stop plausible_minio
3 changes: 2 additions & 1 deletion config/.env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ S3_DISABLED=false
S3_ACCESS_KEY_ID=minioadmin
S3_SECRET_ACCESS_KEY=minioadmin
S3_REGION=us-east-1
S3_ENDPOINT=http://localhost:6000
S3_ENDPOINT=http://localhost:10000
S3_EXPORTS_BUCKET=dev-exports
3 changes: 2 additions & 1 deletion config/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ S3_DISABLED=false
S3_ACCESS_KEY_ID=minioadmin
S3_SECRET_ACCESS_KEY=minioadmin
S3_REGION=us-east-1
S3_ENDPOINT=http://localhost:6000
S3_ENDPOINT=http://localhost:10000
S3_EXPORTS_BUCKET=test-exports
13 changes: 10 additions & 3 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -547,15 +547,16 @@ base_queues = [
clean_invitations: 1,
analytics_imports: 1,
domain_change_transition: 1,
check_accept_traffic_until: 1
check_accept_traffic_until: 1,
# NOTE: maybe move s3_csv_export to cloud_queues?
s3_csv_export: 1
]

cloud_queues = [
trial_notification_emails: 1,
check_usage: 1,
notify_annual_renewal: 1,
lock_sites: 1,
s3_csv_export: 1
lock_sites: 1
]

queues = if(is_selfhost, do: base_queues, else: base_queues ++ cloud_queues)
Expand Down Expand Up @@ -740,6 +741,10 @@ unless s3_disabled? do
%{
name: "S3_ENDPOINT",
example: "https://<ACCOUNT_ID>.r2.cloudflarestorage.com"
},
%{
name: "S3_EXPORTS_BUCKET",
example: "my-csv-exports-bucket"
}
]

Expand Down Expand Up @@ -774,4 +779,6 @@ unless s3_disabled? do
scheme: s3_scheme <> "://",
host: s3_host,
port: s3_port

config :plausible, Plausible.S3, exports_bucket: s3_env_value.("S3_EXPORTS_BUCKET")
end
18 changes: 18 additions & 0 deletions lib/plausible/s3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@ defmodule Plausible.S3 do
Helper functions for S3 exports/imports.
"""

@doc """
Returns the pre-configured S3 bucket for CSV exports.

config :plausible, Plausible.S3,
exports_bucket: System.fetch_env!("S3_EXPORTS_BUCKET")

Example:

iex> exports_bucket()
"test-exports"

"""
@spec exports_bucket :: String.t()
def exports_bucket, do: config(:exports_bucket)

defp config, do: Application.fetch_env!(:plausible, __MODULE__)
defp config(key), do: Keyword.fetch!(config(), key)

@doc """
Chunks and uploads Zip archive to the provided S3 destination.

Expand Down
17 changes: 17 additions & 0 deletions lib/plausible_web/controllers/site_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,23 @@ defmodule PlausibleWeb.SiteController do
|> redirect(external: Routes.site_path(conn, :settings_integrations, site.domain))
end

def export(conn, _params) do
%{site: site, current_user: user} = conn.assigns

Oban.insert!(
Plausible.Workers.ExportCSV.new(%{

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now it exports CSVs without date range in the name like imported_browsers.csv

I'll update it in #3845, or rather, I'll split that PR into two as well, one for business logic and tests and one for UI to make reviewing easier.

@ruslandoga ruslandoga Mar 12, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason I'm having problems with running this job. It stays in available state for longer than 60 seconds (is this required for job unique-ness?) For the manual test I ran it with

iex> Plausible.Repo.all(Oban.Job) |> List.last |> Plausible.Workers.ExportCSV.perform

Ah, seems like cloud queues are not started in dev env.

Oban.config.queues
[
  rotate_salts: [limit: 1],
  schedule_email_reports: [limit: 1],
  send_email_reports: [limit: 1],
  spike_notifications: [limit: 1],
  check_stats_emails: [limit: 1],
  site_setup_emails: [limit: 1],
  clean_invitations: [limit: 1],
  analytics_imports: [limit: 1],
  domain_change_transition: [limit: 1],
  check_accept_traffic_until: [limit: 1]
]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick-fixed in 17c163c

"site_id" => site.id,
"email_to" => user.email,
"s3_bucket" => Plausible.S3.exports_bucket(),
"s3_path" => "Plausible-#{site.id}.zip"
})
)

conn
|> put_flash(:success, "SCHEDULED. WAIT FOR MAIL")
|> redirect(to: Routes.site_path(conn, :settings_imports_exports, site.domain))
end

def change_domain(conn, _params) do
changeset = Plausible.Site.update_changeset(conn.assigns.site)

Expand Down
1 change: 1 addition & 0 deletions lib/plausible_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ defmodule PlausibleWeb.Router do

delete "/:website/settings/forget-imported", SiteController, :forget_imported
delete "/:website/settings/forget-import/:import_id", SiteController, :forget_import
post "/:website/settings/export", SiteController, :export

get "/:domain/export", StatsController, :csv_export
get "/:domain/*path", StatsController, :stats
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@
</header>

<div class="mt-4">
<PlausibleWeb.Components.Generic.button_link disabled href="">
<PlausibleWeb.Components.Generic.button
data-method="post"
data-to={"/#{URI.encode_www_form(@site.domain)}/settings/export"}
data-csrf={Plug.CSRFProtection.get_csrf_token()}
>
Export to CSV
</PlausibleWeb.Components.Generic.button_link>
</PlausibleWeb.Components.Generic.button>
</div>
</div>
16 changes: 12 additions & 4 deletions test/plausible/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ defmodule Plausible.ConfigTest do
{"S3_ACCESS_KEY_ID", nil},
{"S3_SECRET_ACCESS_KEY", nil},
{"S3_REGION", nil},
{"S3_ENDPOINT", nil}
{"S3_ENDPOINT", nil},
{"S3_EXPORTS_BUCKET", nil}
]

result =
Expand All @@ -210,12 +211,13 @@ defmodule Plausible.ConfigTest do
assert %ArgumentError{} = result

assert Exception.message(result) == """
Missing S3 configuration. Please set S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, S3_REGION, S3_ENDPOINT environment variable(s):
Missing S3 configuration. Please set S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, S3_REGION, S3_ENDPOINT, S3_EXPORTS_BUCKET environment variable(s):

\tS3_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
\tS3_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
\tS3_REGION=us-east-1
\tS3_ENDPOINT=https://<ACCOUNT_ID>.r2.cloudflarestorage.com
\tS3_EXPORTS_BUCKET=my-csv-exports-bucket
"""
end

Expand All @@ -224,7 +226,8 @@ defmodule Plausible.ConfigTest do
{"S3_ACCESS_KEY_ID", "AKIAIOSFODNN7EXAMPLE"},
{"S3_SECRET_ACCESS_KEY", nil},
{"S3_REGION", "eu-north-1"},
{"S3_ENDPOINT", nil}
{"S3_ENDPOINT", nil},
{"S3_EXPORTS_BUCKET", "my-exports"}
]

result =
Expand All @@ -249,7 +252,8 @@ defmodule Plausible.ConfigTest do
{"S3_ACCESS_KEY_ID", "minioadmin"},
{"S3_SECRET_ACCESS_KEY", "minioadmin"},
{"S3_REGION", "us-east-1"},
{"S3_ENDPOINT", "http://localhost:6000"}
{"S3_ENDPOINT", "http://localhost:6000"},
{"S3_EXPORTS_BUCKET", "my-exports"}
]

config = runtime_config(env)
Expand All @@ -261,6 +265,10 @@ defmodule Plausible.ConfigTest do
region: "us-east-1",
s3: [scheme: "http://", host: "localhost", port: 6000]
]

assert get_in(runtime_config(env), [:plausible, Plausible.S3]) == [
exports_bucket: "my-exports"
]
end
end

Expand Down