View Source Plausible.Exports (Plausible v0.0.1)

Contains functions to export data for events and sessions as Zip archives.

Summary

Functions

Renders export archive filename.

Safely renders content disposition for an arbitrary export filename.

Returns the date range for the site's events data in site's timezone or nil if there is no data

Deletes local export for a site

Deletes S3 export for a site. Raises if object storage is unavailable.

Builds Ecto queries to export data from events_v2 and sessions_v2 tables into the format of imported_* tables for a website.

Gets last CSV export job for a site

Gets local export for a site

Gets S3 export for a site. Raises if object storage is unavailable.

Subscribes to CSV export job notifications

Schedules CSV export job to local storage

Schedules CSV export job to S3 storage

Creates a streamable Zip archive from the provided (named) Ecto queries.

Types

@type export() :: %{
  path: Path.t(),
  name: String.t(),
  expires_at: DateTime.t() | nil,
  size: pos_integer()
}

Functions

Link to this function

archive_filename(domain, created_on)

View Source

Renders export archive filename.

Examples:

iex> archive_filename("plausible.io", _created_on = ~D[2024-12-31])
"plausible_io_20241231.zip"
Link to this function

content_disposition(filename)

View Source

Safely renders content disposition for an arbitrary export filename.

Examples:

iex> content_disposition("plausible_io_20241231.zip")
"attachment; filename=\"plausible_io_20241231.zip\""

iex> content_disposition("📊.zip")
"attachment; filename=\"plausible-export.zip\"; filename*=utf-8''%F0%9F%93%8A.zip"
Link to this function

date_range(site_id, timezone)

View Source
@spec date_range(pos_integer(), String.t()) :: Date.Range.t() | nil

Returns the date range for the site's events data in site's timezone or nil if there is no data

Link to this function

delete_local_export(site_id)

View Source
@spec delete_local_export(pos_integer()) :: :ok

Deletes local export for a site

Link to this function

delete_s3_export!(site_id)

View Source
@spec delete_s3_export!(pos_integer()) :: :ok

Deletes S3 export for a site. Raises if object storage is unavailable.

Link to this function

export_queries(site_id, opts \\ [])

View Source
@spec export_queries(pos_integer(),
  extname: String.t(),
  date_range: Date.Range.t(),
  timezone: String.t()
) :: %{required(String.t()) => Ecto.Query.t()}

Builds Ecto queries to export data from events_v2 and sessions_v2 tables into the format of imported_* tables for a website.

Link to this function

get_last_export_job(site_id)

View Source
@spec get_last_export_job(pos_integer()) :: Oban.Job.t() | nil

Gets last CSV export job for a site

Link to this function

get_local_export(site_id, domain, timezone)

View Source
@spec get_local_export(pos_integer(), String.t(), String.t()) :: export() | nil

Gets local export for a site

Link to this function

get_s3_export!(site_id, retries \\ 0)

View Source
@spec get_s3_export!(pos_integer(), non_neg_integer()) :: export() | nil

Gets S3 export for a site. Raises if object storage is unavailable.

Subscribes to CSV export job notifications

Link to this function

schedule_local_export(site_id, email_to)

View Source
@spec schedule_local_export(pos_integer(), String.t()) ::
  {:ok, Oban.Job.t()} | {:error, :no_data}

Schedules CSV export job to local storage

Link to this function

schedule_s3_export(site_id, email_to)

View Source
@spec schedule_s3_export(pos_integer(), String.t()) ::
  {:ok, Oban.Job.t()} | {:error, :no_data}

Schedules CSV export job to S3 storage

Link to this function

stream_archive(conn, named_queries, opts \\ [])

View Source
@spec stream_archive(DBConnection.t(), %{required(String.t()) => Ecto.Query.t()}, [
  Ch.query_option()
]) ::
  Enumerable.t()

Creates a streamable Zip archive from the provided (named) Ecto queries.

Example usage:

{:ok, pool} = Ch.start_link(pool_size: 1)

DBConnection.run(pool, fn conn ->
  conn
  |> stream_archive(export_queries(_site_id = 1), format: "CSVWithNames")
  |> Stream.into(File.stream!("export.zip"))
  |> Stream.run()
end)