Skip to content

Commit

Permalink
Stub downloading previously generated result files
Browse files Browse the repository at this point in the history
See #2350
  • Loading branch information
matiasgarciaisaia committed Sep 24, 2024
1 parent ed988f8 commit 607b382
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 16 deletions.
19 changes: 18 additions & 1 deletion assets/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,25 @@ export const refreshDispositionHistoryLink = (projectId, surveyId) => {

export const triggerRespondentsResultFile = (projectId, surveyId, q) =>
apiPostJSON(`projects/${projectId}/surveys/${surveyId}/respondents/results?${
const downloadGeneratedFile = (sourceUrl) =>
apiFetch(sourceUrl, null, null).then((response) => {
response.json().then((data) => {
window.open(data.url)
})
}
)

export const downloadRespondentsResultsFile = (projectId, surveyId, q) =>
downloadGeneratedFile(`projects/${projectId}/surveys/${surveyId}/respondents/results_csv?${
(q && `&q=${encodeURIComponent(q)}`) || ""
}`, null, null)
}`)
export const downloadRespondentsDispositionHistoryFile = (projectId, surveyId) =>
downloadGeneratedFile(`projects/${projectId}/surveys/${surveyId}/respondents/disposition_history`)
export const downloadRespondentsIncentivesFile = (projectId, surveyId) =>
downloadGeneratedFile(`projects/${projectId}/surveys/${surveyId}/respondents/incentives`)
export const downloadRespondentsInteractionsFile = (projectId, surveyId) =>
downloadGeneratedFile(`projects/${projectId}/surveys/${surveyId}/respondents/interactions`)

export const triggerRespondentsDispositionHistoryCSV = (projectId, surveyId) =>
apiPostJSON(`projects/${projectId}/surveys/${surveyId}/respondents/disposition_history`, null, null)
export const triggerRespondentsIncentivesCSV = (projectId, surveyId) =>
Expand Down
18 changes: 9 additions & 9 deletions assets/js/components/respondents/RespondentIndex.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,25 @@ class RespondentIndex extends Component<Props, State> {
this.fetchRespondents(pageNumber - 1)
}

downloadCSV(applyUserFilter = false) {
downloadResultsCSV(applyUserFilter = false) {
const { projectId, surveyId, filter } = this.props
const q = (applyUserFilter && filter) || null
api.triggerRespondentsResultFile(projectId, surveyId, q)
api.downloadRespondentsResultsFile(projectId, surveyId, q)
}

downloadDispositionHistoryCSV() {
const { projectId, surveyId } = this.props
api.triggerRespondentsDispositionHistoryCSV(projectId, surveyId)
api.downloadRespondentsDispositionHistoryFile(projectId, surveyId)
}

downloadIncentivesCSV() {
const { projectId, surveyId } = this.props
api.triggerRespondentsIncentivesCSV(projectId, surveyId)
api.downloadRespondentsIncentivesFile(projectId, surveyId)
}

downloadInteractionsCSV() {
const { projectId, surveyId } = this.props
api.triggerRespondentsInteractionsCSV(projectId, surveyId)
api.downloadRespondentsInteractionsFile(projectId, surveyId)
}

sortBy(name) {
Expand Down Expand Up @@ -343,7 +343,7 @@ class RespondentIndex extends Component<Props, State> {
{ totalCount, filter }
),
downloadLink: null,
onDownload: () => this.downloadCSV(true),
onDownload: () => this.downloadResultsCSV(true),
}
break
case "results":
Expand All @@ -358,7 +358,7 @@ class RespondentIndex extends Component<Props, State> {
this.refreshResultsLink,
"resultsLink"
),
onDownload: () => this.downloadCSV(),
onDownload: () => this.downloadResultsCSV(),
}
break
case "disposition-history":
Expand Down Expand Up @@ -418,7 +418,7 @@ class RespondentIndex extends Component<Props, State> {
const downloadButton = (
<div className="file-download">
<Tooltip text={t("Download file")}>
<a className="black-text">
<a className="black-text" onClick={item.onDownload}>
<i className="material-icons">get_app</i>
</a>
</Tooltip>
Expand Down Expand Up @@ -481,7 +481,7 @@ class RespondentIndex extends Component<Props, State> {
const ownerOrAdmin = userLevel == "owner" || userLevel == "admin"

return (
<Modal id={`downloadCSV`} confirmationText="Download CSV" card>
<Modal id="downloadCSV" confirmationText="Download CSV" card>
<div className="card-title header">
<h5>{t("Download CSV")}</h5>
<p>{t("Choose the data you want to download")}</p>
Expand Down
65 changes: 63 additions & 2 deletions lib/ask_web/controllers/respondent_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,30 @@ defmodule AskWeb.RespondentController do
conn
end

# FIXME: this function should return the proper file URL/path
defp generated_file_url(%{id: survey_id}, {:results, _filter}), do: "/?some-thingy"
defp generated_file_url(%{id: survey_id}, :disposition_history), do: "#dispositon_history"
defp generated_file_url(%{id: survey_id}, :incentives), do: "#incentives-#{survey_id}"
defp generated_file_url(%{id: survey_id}, :interactions), do: "#interactions-#{survey_id}"

defp file_redirection(conn, survey, file_type) do
file_url = generated_file_url(survey, file_type)

render(conn, "file-redirect.json",
file_url: file_url
)
end

def results_csv(conn, %{"project_id" => project_id, "survey_id" => survey_id} = params) do
project = load_project(conn, project_id)
survey = load_survey(project, survey_id)

filter = RespondentsFilter.parse(Map.get(params, "q", ""))
filter = add_params_to_filter(filter, params)

file_redirection(conn, survey, {:results, filter})
end

def trigger_results(conn, %{"project_id" => project_id, "survey_id" => survey_id} = params) do
project = load_project(conn, project_id)
survey = load_survey(project, survey_id)
Expand Down Expand Up @@ -766,6 +790,15 @@ defmodule AskWeb.RespondentController do
filter
end

def generate_disposition_history(conn, %{"project_id" => project_id, "survey_id" => survey_id}) do
project = load_project(conn, project_id)
survey = load_survey(project, survey_id)

SurveyResults.generate_disposition_history_file(survey.id)

conn |> render("ok.json")
end

def disposition_history(conn, %{"project_id" => project_id, "survey_id" => survey_id}) do
project = load_project(conn, project_id)
survey = load_survey(project, survey_id)
Expand All @@ -774,8 +807,7 @@ defmodule AskWeb.RespondentController do
# and add another log when actually downloading?
ActivityLog.download(project, conn, survey, "disposition_history") |> Repo.insert()

SurveyResults.generate_disposition_history_file(survey_id)
conn |> send_resp(200, "OK")
file_redirection(conn, survey, :disposition_history)
end

def incentives(conn, %{"project_id" => project_id, "survey_id" => survey_id}) do
Expand All @@ -793,6 +825,24 @@ defmodule AskWeb.RespondentController do
# and add another log when actually downloading?
ActivityLog.download(project, conn, survey, "incentives") |> Repo.insert()

file_redirection(conn, survey, :incentives)
end

def generate_incentives(conn, %{"project_id" => project_id, "survey_id" => survey_id}) do
project =
conn
|> load_project_for_owner(project_id)

survey =
project
|> assoc(:surveys)
|> where([s], s.incentives_enabled)
|> Repo.get!(survey_id)

# TODO: We just change this for "trigger generation"
# and add another log when actually downloading?
ActivityLog.download(project, conn, survey, "incentives") |> Repo.insert()

SurveyResults.generate_incentives_file(survey_id)
conn |> send_resp(200, "OK")
end
Expand All @@ -805,6 +855,17 @@ defmodule AskWeb.RespondentController do
# and add another log when actually downloading?
ActivityLog.download(project, conn, survey, "interactions") |> Repo.insert()

file_redirection(conn, survey, :interactions)
end

def generate_interactions(conn, %{"project_id" => project_id, "survey_id" => survey_id}) do
project = load_project_for_owner(conn, project_id)
survey = load_survey(project, survey_id)

# TODO: We just change this for "trigger generation"
# and add another log when actually downloading?
ActivityLog.download(project, conn, survey, "interactions") |> Repo.insert()

SurveyResults.generate_interactions_file(survey_id)
conn |> send_resp(200, "OK")
end
Expand Down
11 changes: 7 additions & 4 deletions lib/ask_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,19 @@ defmodule AskWeb.Router do
pipe_through :api

get "/results", RespondentController, :results, as: :get_respondents_results

get "/results_csv", RespondentController, :results_csv
post "/results", RespondentController, :generate_results, as: :respondents_results

get "/disposition_history", RespondentController, :disposition_history, as: :respondents_disposition_history
post "/disposition_history", RespondentController, :generate_disposition_history,
as: :respondents_disposition_history
as: :generate_disposition_history

post "/incentives", RespondentController, :generate_incentives, as: :respondents_incentives
get "/incentives", RespondentController, :incentives, as: :respondents_incentives
post "/incentives", RespondentController, :generate_incentives, as: :generate_respondents_incentives

get "/interactions", RespondentController, :interactions, as: :respondents_interactions
post "/interactions", RespondentController, :generate_interactions,
as: :respondents_interactions
as: :generate_respondents_interactions
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/ask_web/views/respondent_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ defmodule AskWeb.RespondentView do
%{data: %{}}
end

def render("file-redirect.json", %{file_url: url}) do
%{url: url}
end

def render("respondent.json", %{
respondent: respondent,
partial_relevant_enabled: partial_relevant_enabled
Expand Down Expand Up @@ -269,6 +273,8 @@ defmodule AskWeb.RespondentView do
percent: percentage
}
end

def render("ok.json", _), do: %{status: :ok}

defp render_index_meta(%{respondents_count: respondents_count, index_fields: index_fields}),
do: %{
Expand Down

0 comments on commit 607b382

Please sign in to comment.