Skip to content

Commit 831e3e7

Browse files
committed
Log entry added, non-closed resources are correctly handled, file header collections are stateless, hard-coded constants deleted relying on the application configuration file
Migration scripts combined
1 parent cd0f48a commit 831e3e7

5 files changed

+37
-45
lines changed

src/main/java/org/ohdsi/webapi/statistic/controller/StatisticController.java

+21-26
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.ohdsi.webapi.statistic.dto.SourceExecutionDto;
99
import org.ohdsi.webapi.statistic.dto.SourceExecutionsDto;
1010
import org.ohdsi.webapi.statistic.service.StatisticService;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
1113
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
1214
import org.springframework.stereotype.Controller;
1315

@@ -30,6 +32,9 @@
3032
@Path("/statistic/")
3133
@ConditionalOnProperty(value = "audit.trail.enabled", havingValue = "true")
3234
public 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;

src/main/java/org/ohdsi/webapi/statistic/dto/SourceExecutionDto.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
package org.ohdsi.webapi.statistic.dto;
22

3-
import java.time.Instant;
4-
import java.time.LocalDate;
5-
63
public class SourceExecutionDto {
74
private String sourceName;
85
private String executionName;
96
private String executionDate;
10-
private String userID;
7+
private String userId;
118

12-
public SourceExecutionDto(String sourceName, String executionName, String executionDate, String userID) {
9+
public SourceExecutionDto(String sourceName, String executionName, String executionDate, String userId) {
1310
this.sourceName = sourceName;
1411
this.executionName = executionName;
1512
this.executionDate = executionDate;
16-
this.userID = userID;
13+
this.userId = userId;
1714
}
1815

1916
public String getSourceName() {
@@ -40,11 +37,11 @@ public void setExecutionDate(String executionDate) {
4037
this.executionDate = executionDate;
4138
}
4239

43-
public String getUserID() {
44-
return userID;
40+
public String getUserId() {
41+
return userId;
4542
}
4643

47-
public void setUserID(String userID) {
48-
this.userID = userID;
44+
public void setUserId(String userId) {
45+
this.userId = userId;
4946
}
5047
}

src/main/java/org/ohdsi/webapi/statistic/service/StatisticService.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,12 @@ public class StatisticService {
3737
protected final Logger LOG = LoggerFactory.getLogger(getClass());
3838

3939
@Value("${audit.trail.log.file}")
40-
// TODO remove value
41-
private String absoluteLogFileName = "/tmp/atlas/audit/audit.log";
40+
private String absoluteLogFileName;
4241

4342
private String logFileName;
4443

4544
@Value("${audit.trail.log.file.pattern}")
46-
// TODO remove value
47-
private String absoluteLogFileNamePattern = "/tmp/atlas/audit/audit-%d{yyyy-MM-dd}-%i.log";
45+
private String absoluteLogFileNamePattern;
4846

4947
private String logFileNamePattern;
5048

@@ -102,6 +100,10 @@ public class StatisticService {
102100
}
103101

104102
public StatisticService() {
103+
if (absoluteLogFileName == null || absoluteLogFileNamePattern == null) {
104+
throw new RuntimeException("Application statistics can't operate because of missing configuration values for the audit trail log file or its pattern");
105+
}
106+
105107
logFileName = new File(absoluteLogFileName).getName();
106108
logFileNamePattern = new File(absoluteLogFileNamePattern).getName();
107109

src/main/resources/db/migration/postgresql/V2.14.0.20231206190600__fixing_statistics_enpoint_permission_method.sql

-2
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
INSERT INTO ${ohdsiSchema}.sec_permission(id, value, description) VALUES
2-
(nextval('${ohdsiSchema}.sec_permission_id_seq'), 'statistic:executions:get', 'Source execution statistics permission');
2+
(nextval('${ohdsiSchema}.sec_permission_id_seq'), 'statistic:executions:post', 'Source execution statistics permission');
33

44
INSERT INTO ${ohdsiSchema}.sec_permission(id, value, description) VALUES
55
(nextval('${ohdsiSchema}.sec_permission_id_seq'), 'statistic:accesstrends:post', 'Access trends statistics permission');
66

77
INSERT INTO ${ohdsiSchema}.sec_role_permission(id, role_id, permission_id)
88
SELECT nextval('${ohdsiSchema}.sec_role_permission_sequence'), sr.id, sp.id
99
FROM ${ohdsiSchema}.sec_permission SP, ${ohdsiSchema}.sec_role sr
10-
WHERE sp.value IN ('statistic:executions:get') AND sr.name IN ('admin');
10+
WHERE sp.value IN ('statistic:executions:post') AND sr.name IN ('admin');
1111

1212
INSERT INTO ${ohdsiSchema}.sec_role_permission(id, role_id, permission_id)
1313
SELECT nextval('${ohdsiSchema}.sec_role_permission_sequence'), sr.id, sp.id
1414
FROM ${ohdsiSchema}.sec_permission SP, ${ohdsiSchema}.sec_role sr
15-
WHERE sp.value IN ('statistic:accesstrends:post') AND sr.name IN ('admin');
15+
WHERE sp.value IN ('statistic:accesstrends:post') AND sr.name IN ('admin');

0 commit comments

Comments
 (0)