8
8
import org .ohdsi .webapi .statistic .dto .SourceExecutionDto ;
9
9
import org .ohdsi .webapi .statistic .dto .SourceExecutionsDto ;
10
10
import org .ohdsi .webapi .statistic .service .StatisticService ;
11
+ import org .slf4j .Logger ;
12
+ import org .slf4j .LoggerFactory ;
11
13
import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
12
14
import org .springframework .stereotype .Controller ;
13
15
30
32
@ Path ("/statistic/" )
31
33
@ ConditionalOnProperty (value = "audit.trail.enabled" , havingValue = "true" )
32
34
public class StatisticController {
35
+
36
+ private static final Logger log = LoggerFactory .getLogger (StatisticController .class );
37
+
33
38
private StatisticService service ;
34
39
35
40
public enum ResponseFormat {
@@ -40,8 +45,16 @@ public enum ResponseFormat {
40
45
add (new String []{"Date" , "Source" , "Execution Type" });
41
46
}};
42
47
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
+
43
52
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" });
45
58
}};
46
59
47
60
public StatisticController (StatisticService service ) {
@@ -93,31 +106,30 @@ public Response accessStatistics(AccessTrendsStatisticsRequest accessTrendsStati
93
106
}
94
107
95
108
private Response prepareExecutionResultResponse (List <SourceExecutionDto > executions , String filename , boolean showUserInformation ) {
96
- updateExecutionStatisticsHeader (showUserInformation );
97
109
List <String []> data = executions .stream ()
98
110
.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 ()}
100
112
: new String []{execution .getExecutionDate (), execution .getSourceName (), execution .getExecutionName ()}
101
113
)
102
114
.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 );
104
116
}
105
117
106
118
private Response prepareAccessTrendsResponse (List <AccessTrendDto > trends , String filename , boolean showUserInformation ) {
107
- updateAccessTrendsHeader (showUserInformation );
108
119
List <String []> data = trends .stream ()
109
120
.map (trend -> showUserInformation
110
121
? new String []{trend .getExecutionDate ().toString (), trend .getEndpointName (), trend .getUserID ()}
111
122
: new String []{trend .getExecutionDate ().toString (), trend .getEndpointName ()}
112
123
)
113
124
.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 );
115
126
}
116
127
117
128
private Response prepareResponse (List <String []> data , String filename , List <String []> header ) {
118
- try (ByteArrayOutputStream baos = new ByteArrayOutputStream ()) {
129
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream ();
119
130
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
+
121
133
csvWriter .writeAll (header );
122
134
csvWriter .writeAll (data );
123
135
csvWriter .flush ();
@@ -129,28 +141,11 @@ private Response prepareResponse(List<String[]> data, String filename, List<Stri
129
141
.header ("Content-Disposition" , String .format ("attachment; filename=\" %s\" " , filename ))
130
142
.build ();
131
143
} catch (Exception ex ) {
144
+ log .error ("An error occurred while building a response" );
132
145
throw new RuntimeException (ex );
133
146
}
134
147
}
135
148
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
-
154
149
public static final class ExecutionStatisticsRequest {
155
150
// Format - yyyy-MM-dd
156
151
String startDate ;
0 commit comments