@@ -353,14 +353,8 @@ defmodule Ask.SurveyResults do
353353 end
354354
355355 def file_path ( survey , file_type , target_dir \\ @ target_dir ) do
356- # FIXME: as a first iteration, generate a stable name and have a single file per type
357- # but we should probably include the date and respondent results filter in the name
358- prefix = file_prefix ( file_type )
359- # name = survey.name || "survey_id_#{survey.id}"
360- # name = Regex.replace(~r/[^a-zA-Z0-9_]/, name, "_")
361- # current_time = Timex.format!(DateTime.utc_now(), "%Y-%m-%d-%H-%M-%S", :strftime)
362- # "#{target_dir}/#{name}_#{survey.state}-#{prefix}_#{current_time}.csv"
363- "#{ target_dir } /survey_#{ survey . id } -#{ survey . state } -#{ prefix } .csv"
356+ suffix = file_suffix ( file_type )
357+ "#{ target_dir } /survey_#{ survey . id } -#{ survey . state } -#{ suffix } .csv"
364358 end
365359
366360 defp write_to_file ( file_type , survey , rows ) do
@@ -370,20 +364,20 @@ defmodule Ask.SurveyResults do
370364
371365 # Poor man's mktemp. We only want to avoid having the file living at the stable
372366 # path while it's still being written to avoid partial downloads
373- temporal_file = for _ <- 1 .. 10 , into: "#{ target_file } .tmp." , do: << Enum . random ( ?a .. ?z ) >>
374- file = File . open! ( temporal_file , [ :write , :utf8 ] )
367+ temporal_file_name = for _ <- 1 .. 10 , into: "#{ target_file } .tmp." , do: << Enum . random ( ?a .. ?z ) >>
368+ temporal_file = File . open! ( temporal_file_name , [ :write , :utf8 ] )
375369 initial_datetime = Timex . now ( )
376370
377371 rows
378372 |> CSV . encode ( )
379- |> Enum . each ( & IO . write ( file , & 1 ) )
373+ |> Enum . each ( & IO . write ( temporal_file , & 1 ) )
380374
381- File . rename! ( temporal_file , target_file )
375+ File . rename! ( temporal_file_name , target_file )
382376
383377 seconds_to_process_file = Timex . diff ( Timex . now ( ) , initial_datetime , :seconds )
384378
385379 Logger . info (
386- "Generation of #{ file_prefix ( file_type ) } file (survey_id: #{ survey . id } ) took #{ seconds_to_process_file } seconds"
380+ "Generation of #{ file_suffix ( file_type ) } file (survey_id: #{ survey . id } ) took #{ seconds_to_process_file } seconds"
387381 )
388382 end
389383
@@ -433,14 +427,17 @@ defmodule Ask.SurveyResults do
433427 |> to_string
434428 end
435429
436- defp file_prefix ( :interactions ) , do: "respondents_interactions"
437- defp file_prefix ( :incentives ) , do: "respondents_incentives"
438- defp file_prefix ( :disposition_history ) , do: "disposition_history"
439- defp file_prefix ( :respondents_results ) , do: "respondents"
440- defp file_prefix ( { :respondents_results , filter } ) do
430+ defp file_suffix ( :interactions ) , do: "respondents_interactions"
431+ defp file_suffix ( :incentives ) , do: "respondents_incentives"
432+ defp file_suffix ( :disposition_history ) , do: "disposition_history"
433+ defp file_suffix ( :respondents_results ) , do: "respondents"
434+ defp file_suffix ( { :respondents_results , filter } ) do
441435 if RespondentsFilter . empty? ( filter ) do
442436 "respondents"
443437 else
438+ # Hashing the filter object avoids dealing with the order of the params in the filter
439+ # (ie, "disposition:queued mode:sms" vs "mode:sms disposition:queued"), and avoids `:`
440+ # and ` ` characters in the file name
444441 "respondents_filtered_#{ filter_hash ( filter ) } "
445442 end
446443 end
0 commit comments