88import org .ohdsi .webapi .statistic .dto .SourceExecutionDto ;
99import org .ohdsi .webapi .statistic .dto .SourceExecutionsDto ;
1010import org .ohdsi .webapi .statistic .service .StatisticService ;
11+ import org .slf4j .Logger ;
12+ import org .slf4j .LoggerFactory ;
1113import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
1214import org .springframework .stereotype .Controller ;
1315
3032@ Path ("/statistic/" )
3133@ ConditionalOnProperty (value = "audit.trail.enabled" , havingValue = "true" )
3234public class StatisticController {
35+
36+ private static final Logger log = LoggerFactory .getLogger (StatisticController .class );
37+
3338 private StatisticService service ;
3439
3540 public enum ResponseFormat {
@@ -40,8 +45,16 @@ public enum ResponseFormat {
4045 add (new String []{"Date" , "Source" , "Execution Type" });
4146 }};
4247
48+ private static final List <String []> EXECUTION_STATISTICS_CSV_RESULT_HEADER_WITH_USER_ID = new ArrayList <String []>() {{
49+ add (new String []{"Date" , "Source" , "Execution Type" , "User ID" });
50+ }};
51+
4352 private static final List <String []> ACCESS_TRENDS_CSV_RESULT_HEADER = new ArrayList <String []>() {{
44- add (new String []{"Date" , "Endpoint" , "UserID" });
53+ add (new String []{"Date" , "Endpoint" });
54+ }};
55+
56+ private static final List <String []> ACCESS_TRENDS_CSV_RESULT_HEADER_WITH_USER_ID = new ArrayList <String []>() {{
57+ add (new String []{"Date" , "Endpoint" , "User ID" });
4558 }};
4659
4760 public StatisticController (StatisticService service ) {
@@ -93,31 +106,30 @@ public Response accessStatistics(AccessTrendsStatisticsRequest accessTrendsStati
93106 }
94107
95108 private Response prepareExecutionResultResponse (List <SourceExecutionDto > executions , String filename , boolean showUserInformation ) {
96- updateExecutionStatisticsHeader (showUserInformation );
97109 List <String []> data = executions .stream ()
98110 .map (execution -> showUserInformation
99- ? new String []{execution .getExecutionDate (), execution .getSourceName (), execution .getExecutionName (), execution .getUserID ()}
111+ ? new String []{execution .getExecutionDate (), execution .getSourceName (), execution .getExecutionName (), execution .getUserId ()}
100112 : new String []{execution .getExecutionDate (), execution .getSourceName (), execution .getExecutionName ()}
101113 )
102114 .collect (Collectors .toList ());
103- return prepareResponse (data , filename , EXECUTION_STATISTICS_CSV_RESULT_HEADER );
115+ return prepareResponse (data , filename , showUserInformation ? EXECUTION_STATISTICS_CSV_RESULT_HEADER_WITH_USER_ID : EXECUTION_STATISTICS_CSV_RESULT_HEADER );
104116 }
105117
106118 private Response prepareAccessTrendsResponse (List <AccessTrendDto > trends , String filename , boolean showUserInformation ) {
107- updateAccessTrendsHeader (showUserInformation );
108119 List <String []> data = trends .stream ()
109120 .map (trend -> showUserInformation
110121 ? new String []{trend .getExecutionDate ().toString (), trend .getEndpointName (), trend .getUserID ()}
111122 : new String []{trend .getExecutionDate ().toString (), trend .getEndpointName ()}
112123 )
113124 .collect (Collectors .toList ());
114- return prepareResponse (data , filename , ACCESS_TRENDS_CSV_RESULT_HEADER );
125+ return prepareResponse (data , filename , showUserInformation ? ACCESS_TRENDS_CSV_RESULT_HEADER_WITH_USER_ID : ACCESS_TRENDS_CSV_RESULT_HEADER );
115126 }
116127
117128 private Response prepareResponse (List <String []> data , String filename , List <String []> header ) {
118- try (ByteArrayOutputStream baos = new ByteArrayOutputStream ()) {
129+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream ();
119130 StringWriter sw = new StringWriter ();
120- CSVWriter csvWriter = new CSVWriter (sw , ',' , CSVWriter .DEFAULT_QUOTE_CHARACTER , CSVWriter .DEFAULT_ESCAPE_CHARACTER );
131+ CSVWriter csvWriter = new CSVWriter (sw , ',' , CSVWriter .DEFAULT_QUOTE_CHARACTER , CSVWriter .DEFAULT_ESCAPE_CHARACTER )) {
132+
121133 csvWriter .writeAll (header );
122134 csvWriter .writeAll (data );
123135 csvWriter .flush ();
@@ -129,28 +141,11 @@ private Response prepareResponse(List<String[]> data, String filename, List<Stri
129141 .header ("Content-Disposition" , String .format ("attachment; filename=\" %s\" " , filename ))
130142 .build ();
131143 } catch (Exception ex ) {
144+ log .error ("An error occurred while building a response" );
132145 throw new RuntimeException (ex );
133146 }
134147 }
135148
136- private void updateExecutionStatisticsHeader (boolean showUserInformation ) {
137- EXECUTION_STATISTICS_CSV_RESULT_HEADER .clear ();
138- if (showUserInformation ) {
139- EXECUTION_STATISTICS_CSV_RESULT_HEADER .add (new String []{"Date" , "Source" , "Execution Type" , "User ID" });
140- } else {
141- EXECUTION_STATISTICS_CSV_RESULT_HEADER .add (new String []{"Date" , "Source" , "Execution Type" });
142- }
143- }
144-
145- private void updateAccessTrendsHeader (boolean showUserInformation ) {
146- ACCESS_TRENDS_CSV_RESULT_HEADER .clear ();
147- if (showUserInformation ) {
148- ACCESS_TRENDS_CSV_RESULT_HEADER .add (new String []{"Date" , "Endpoint" , "UserID" });
149- } else {
150- ACCESS_TRENDS_CSV_RESULT_HEADER .add (new String []{"Date" , "Endpoint" });
151- }
152- }
153-
154149 public static final class ExecutionStatisticsRequest {
155150 // Format - yyyy-MM-dd
156151 String startDate ;
0 commit comments