-
Notifications
You must be signed in to change notification settings - Fork 4
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
Feature/59 subjects csv export #60
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4660127
Add export subjects on csv file
emaraschio ddef595
Add export csv button on subjects list
emaraschio 2ef0e20
Add calculated data to Subjects CSV export
matiasgarciaisaia 203f80f
[skip ci] - WIP - Showing Subject stats on index
matiasgarciaisaia de87545
Format rows on subjects list
emaraschio db3d221
Add DB indices to help Subject stats queries
matiasgarciaisaia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 8 additions & 0 deletions
8
priv/repo/migrations/20171221180410_add_indices_for_subjects_stats.exs
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,8 @@ | ||
defmodule ActiveMonitoring.Repo.Migrations.AddIndicesForSubjectsStats do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create index(:calls, [:campaign_id, :subject_id], name: :calls_campaign_id_subject_id_index) | ||
create index(:calls, [:campaign_id, :subject_id, :current_step], name: :calls_campaign_id_subject_id_current_step_index) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ defmodule ActiveMonitoring.SubjectsControllerTest do | |
|
||
setup %{conn: conn} do | ||
user = build(:user, email: "[email protected]") |> Repo.insert! | ||
campaign = build(:campaign, user: user) |> Repo.insert! | ||
campaign = build(:campaign, user: user, monitor_duration: 30) |> Repo.insert! | ||
|
||
other_user = build(:user, email: "[email protected]") |> Repo.insert! | ||
other_campaign = build(:campaign, user: other_user) |> Repo.insert! | ||
|
@@ -54,6 +54,16 @@ defmodule ActiveMonitoring.SubjectsControllerTest do | |
assert subj["phoneNumber"] == subject.phone_number | ||
end | ||
|
||
test "subjects csv export", %{conn: conn, campaign: campaign, subject: subject} do | ||
conn = conn |> get(campaigns_subjects_export_csv_path(conn, :export_csv, campaign)) | ||
csv = conn |> response(200) | ||
assert get_resp_header(conn, "content-disposition") == ["attachment; filename=\"export_#{campaign.name}_subjects.csv\""] | ||
assert get_resp_header(conn, "content-type") == ["text/csv; charset=utf-8"] | ||
[header, line1 | _] = csv |> String.split("\r\n") | ||
assert header == "ID,Phone Number,Enroll date,First Call Date,Last Call Date,Last Successful Call,Active Case" | ||
assert line1 == "#{subject.registration_identifier},#{subject.phone_number},#{subject.inserted_at},,,,true" | ||
end | ||
|
||
test "lists subjects by page size", %{conn: conn, campaign: campaign} do | ||
response = conn |> get(campaigns_subjects_path(conn, :index, campaign, limit: 2)) |> json_response(200) | ||
assert length(response["data"]["subjects"]) == 2 | ||
|
@@ -152,6 +162,12 @@ defmodule ActiveMonitoring.SubjectsControllerTest do | |
end | ||
end | ||
|
||
test "doesn't allow to export another user's campaign subjects", %{conn: conn, other_campaign: other_campaign} do | ||
assert_error_sent 403, fn -> | ||
conn |> get(campaigns_subjects_export_csv_path(conn, :export_csv, other_campaign)) | ||
end | ||
end | ||
|
||
test "not found when trying to list a campaign that doesn't exist subjects", %{conn: conn} do | ||
assert_error_sent 404, fn -> | ||
conn |> get(campaigns_subjects_path(conn, :index, -1)) | ||
|
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,19 @@ | ||
defmodule CSV.Helper do | ||
import Plug.Conn | ||
@chunk_lines 100 | ||
|
||
def csv_stream(conn, rows, filename) do | ||
conn = conn | ||
|> put_resp_content_type("text/csv") | ||
|> put_resp_header("content-disposition", "attachment; filename=\"#{filename}\"") | ||
|> send_chunked(200) | ||
|
||
rows | ||
|> CSV.encode | ||
|> Stream.chunk(@chunk_lines, @chunk_lines, []) | ||
|> Enum.reduce(conn, fn (lines, conn) -> | ||
{:ok, conn} = chunk(conn, lines) | ||
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mverzilli reworking these queries is a big issue. We're going to merge this as-is, and have filled #61 to not forget about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've added indices to help the queries we're currently executing, BTW.