-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #238 from katafrakt/csv-export
Add contacts CSV export capability
- Loading branch information
Showing
10 changed files
with
180 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
defmodule KeilaWeb.ContactsCsvExport do | ||
@moduledoc """ | ||
Shared logic of exporting list of contacts to CSV file with support of streaming. | ||
""" | ||
alias Keila.Contacts | ||
import Plug.Conn | ||
|
||
@chunk_size Application.compile_env!(:keila, KeilaWeb.ContactsCsvExport)[:chunk_size] | ||
|
||
def stream_csv_response(conn, filename, project_id, stream_opts \\ []) do | ||
stream_opts = Keyword.merge(stream_opts, max_rown: @chunk_size) | ||
|
||
conn = | ||
conn | ||
|> put_resp_header("content-disposition", "attachment; filename=\"#{filename}\"") | ||
|> put_resp_header("content-type", "text/csv") | ||
|> send_chunked(200) | ||
|
||
header = | ||
[["Email", "First name", "Last name", "Data", "Status"]] | ||
|> NimbleCSV.RFC4180.dump_to_iodata() | ||
|> IO.iodata_to_binary() | ||
|
||
{:ok, conn} = chunk(conn, header) | ||
|
||
Keila.Repo.transaction(fn -> | ||
Contacts.stream_project_contacts(project_id, stream_opts) | ||
|> Stream.map(fn contact -> | ||
data = if is_nil(contact.data), do: nil, else: Jason.encode!(contact.data) | ||
|
||
[[contact.email, contact.first_name, contact.last_name, data, contact.status]] | ||
|> NimbleCSV.RFC4180.dump_to_iodata() | ||
|> IO.iodata_to_binary() | ||
end) | ||
|> Stream.chunk_every(@chunk_size) | ||
|> Enum.reduce_while(conn, fn chunk, conn -> | ||
case Plug.Conn.chunk(conn, chunk) do | ||
{:ok, conn} -> {:cont, conn} | ||
{:error, :closed} -> {:halt, conn} | ||
end | ||
end) | ||
end) | ||
|> then(fn {:ok, conn} -> conn end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,4 +83,42 @@ defmodule KeilaWeb.SegmentControllerTest do | |
assert segment == Contacts.get_segment(segment.id) | ||
end | ||
end | ||
|
||
@tag :segment_controller | ||
test "GET /projects/:p_id/export CSV export contacts in multiple chunks", %{conn: conn} do | ||
{conn, project} = with_login_and_project(conn) | ||
|
||
segment = | ||
insert!(:contacts_segment, | ||
project_id: project.id, | ||
filter: %{"email" => %{"$like" => "%keila.io"}} | ||
) | ||
|
||
insert!(:contact, project_id: project.id, email: "[email protected]") | ||
|
||
contact = | ||
insert!(:contact, project_id: project.id, status: :unreachable, email: "[email protected]") | ||
|
||
insert!(:contact, project_id: project.id, email: "[email protected]") | ||
insert!(:contact, project_id: project.id, email: "[email protected]") | ||
insert!(:contact, project_id: project.id, email: "[email protected]") | ||
conn = get(conn, Routes.segment_path(conn, :contacts_export, project.id, segment.id)) | ||
|
||
assert conn.state == :chunked | ||
rows = String.split(response(conn, 200), "\r\n") | ||
assert length(rows) == 6 | ||
assert Enum.at(rows, 1) =~ ~r/,unreachable/ | ||
|
||
{_, disposition} = List.keyfind(conn.resp_headers, "content-disposition", 0) | ||
|
||
assert disposition == | ||
"attachment; filename=\"contacts_#{project.id}_segment_#{segment.id}.csv\"" | ||
|
||
assert [ | ||
"Email,First name,Last name,Data,Status", | ||
contact_row | _ | ||
] = rows | ||
|
||
assert contact_row == "[email protected],#{contact.first_name},#{contact.last_name},,unreachable" | ||
end | ||
end |