Skip to content

Commit 1fe7db0

Browse files
hernaldourbinaoleg-odysseus
authored andcommitted
ATL-8: Addressing userId info
1 parent 8e28b98 commit 1fe7db0

File tree

3 files changed

+25
-33
lines changed

3 files changed

+25
-33
lines changed

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

+4-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.opencsv.CSVWriter;
44

5-
import org.ohdsi.webapi.shiro.TokenManager;
65
import org.ohdsi.webapi.statistic.dto.AccessTrendDto;
76
import org.ohdsi.webapi.statistic.dto.AccessTrendsDto;
87
import org.ohdsi.webapi.statistic.dto.EndpointDto;
@@ -11,8 +10,6 @@
1110
import org.ohdsi.webapi.statistic.service.StatisticService;
1211
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
1312
import org.springframework.stereotype.Controller;
14-
import org.springframework.web.context.request.RequestContextHolder;
15-
import org.springframework.web.context.request.ServletRequestAttributes;
1613

1714
import javax.ws.rs.Consumes;
1815
import javax.ws.rs.POST;
@@ -27,8 +24,6 @@
2724
import java.time.format.DateTimeFormatter;
2825
import java.util.ArrayList;
2926
import java.util.List;
30-
import java.util.Objects;
31-
import java.util.Optional;
3227
import java.util.stream.Collectors;
3328

3429
@Controller
@@ -63,10 +58,10 @@ public StatisticController(StatisticService service) {
6358
@Consumes(MediaType.APPLICATION_JSON)
6459
public Response executionStatistics(ExecutionStatisticsRequest executionStatisticsRequest) {
6560
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
66-
String userID = executionStatisticsRequest.isShowUserInformation() ? extractUserID() : null;
61+
boolean showUserInformation = executionStatisticsRequest.isShowUserInformation();
6762

6863
SourceExecutionsDto sourceExecutions = service.getSourceExecutions(LocalDate.parse(executionStatisticsRequest.getStartDate(), formatter),
69-
LocalDate.parse(executionStatisticsRequest.getEndDate(), formatter), executionStatisticsRequest.getSourceKey(), userID);
64+
LocalDate.parse(executionStatisticsRequest.getEndDate(), formatter), executionStatisticsRequest.getSourceKey(), showUserInformation);
7065

7166
if (ResponseFormat.CSV.equals(executionStatisticsRequest.getResponseFormat())) {
7267
return prepareExecutionResultResponse(sourceExecutions.getExecutions(), "execution_statistics.zip");
@@ -85,10 +80,10 @@ public Response executionStatistics(ExecutionStatisticsRequest executionStatisti
8580
@Consumes(MediaType.APPLICATION_JSON)
8681
public Response accessStatistics(AccessTrendsStatisticsRequest accessTrendsStatisticsRequest) {
8782
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
88-
String userID = accessTrendsStatisticsRequest.isShowUserInformation() ? extractUserID() : null;
83+
boolean showUserInformation = accessTrendsStatisticsRequest.isShowUserInformation();
8984

9085
AccessTrendsDto trends = service.getAccessTrends(LocalDate.parse(accessTrendsStatisticsRequest.getStartDate(), formatter),
91-
LocalDate.parse(accessTrendsStatisticsRequest.getEndDate(), formatter), accessTrendsStatisticsRequest.getEndpoints(), userID);
86+
LocalDate.parse(accessTrendsStatisticsRequest.getEndDate(), formatter), accessTrendsStatisticsRequest.getEndpoints(), showUserInformation);
9287

9388
if (ResponseFormat.CSV.equals(accessTrendsStatisticsRequest.getResponseFormat())) {
9489
return prepareAccessTrendsResponse(trends.getTrends(), "execution_trends.zip");
@@ -138,15 +133,6 @@ private Response prepareResponse(List<String[]> data, String filename, List<Stri
138133
}
139134
}
140135

141-
private String extractUserID() {
142-
return Optional.ofNullable(RequestContextHolder.getRequestAttributes())
143-
.map(ServletRequestAttributes.class::cast)
144-
.map(ServletRequestAttributes::getRequest)
145-
.map(TokenManager::extractToken)
146-
.map(TokenManager::getSubject)
147-
.orElse(null);
148-
}
149-
150136
public static final class ExecutionStatisticsRequest {
151137
// Format - yyyy-MM-dd
152138
String startDate;

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public class EndpointDto {
44
String method;
55
String urlPattern;
6+
String userId;
67

78
public String getMethod() {
89
return method;
@@ -18,6 +19,14 @@ public String getUrlPattern() {
1819

1920
public void setUrlPattern(String urlPattern) {
2021
this.urlPattern = urlPattern;
21-
}
22+
}
23+
24+
public String getUserId() {
25+
return userId;
26+
}
27+
28+
public void setUserId(String userId) {
29+
this.userId = userId;
30+
}
2231
}
2332

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

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.ohdsi.webapi.statistic.service;
22

33
import org.apache.commons.lang3.tuple.ImmutablePair;
4-
import org.apache.commons.lang3.tuple.Pair;
54
import org.ohdsi.webapi.statistic.dto.AccessTrendDto;
65
import org.ohdsi.webapi.statistic.dto.AccessTrendsDto;
76
import org.ohdsi.webapi.statistic.dto.EndpointDto;
@@ -22,14 +21,11 @@
2221
import java.text.SimpleDateFormat;
2322
import java.time.LocalDate;
2423
import java.time.ZoneId;
25-
import java.util.ArrayList;
2624
import java.util.HashMap;
2725
import java.util.List;
2826
import java.util.Map;
29-
import java.util.Objects;
3027
import java.util.Optional;
3128
import java.util.Set;
32-
import java.util.regex.Matcher;
3329
import java.util.regex.Pattern;
3430
import java.util.stream.Collectors;
3531
import java.util.stream.Stream;
@@ -117,26 +113,26 @@ public StatisticService() {
117113
logFileDateFormat = new SimpleDateFormat(dateString);
118114
}
119115

120-
public SourceExecutionsDto getSourceExecutions(LocalDate startDate, LocalDate endDate, String sourceKey, String userID) {
116+
public SourceExecutionsDto getSourceExecutions(LocalDate startDate, LocalDate endDate, String sourceKey, boolean showUserInformation) {
121117
Set<Path> paths = getLogPaths(startDate, endDate);
122118
List<SourceExecutionDto> executions = paths.stream()
123-
.flatMap(path -> extractSourceExecutions(path, sourceKey, userID).stream())
119+
.flatMap(path -> extractSourceExecutions(path, sourceKey, showUserInformation).stream())
124120
.collect(Collectors.toList());
125121
return new SourceExecutionsDto(executions);
126122
}
127123

128-
public AccessTrendsDto getAccessTrends(LocalDate startDate, LocalDate endDate, List<EndpointDto> endpoints, String userID) {
124+
public AccessTrendsDto getAccessTrends(LocalDate startDate, LocalDate endDate, List<EndpointDto> endpoints, boolean showUserInformation) {
129125
Set<Path> paths = getLogPaths(startDate, endDate);
130126
List<AccessTrendDto> trends = paths.stream()
131-
.flatMap(path -> extractAccessTrends(path, endpoints, userID).stream())
127+
.flatMap(path -> extractAccessTrends(path, endpoints, showUserInformation).stream())
132128
.collect(Collectors.toList());
133129
return new AccessTrendsDto(trends);
134130
}
135131

136-
private List<SourceExecutionDto> extractSourceExecutions(Path path, String sourceKey, String userID) {
132+
private List<SourceExecutionDto> extractSourceExecutions(Path path, String sourceKey, boolean showUserInformation) {
137133
try (Stream<String> stream = Files.lines(path)) {
138134
return stream
139-
.map(str -> getMatchedExecution(str, sourceKey, userID))
135+
.map(str -> getMatchedExecution(str, sourceKey, showUserInformation))
140136
.filter(Optional::isPresent)
141137
.map(Optional::get)
142138
.collect(Collectors.toList());
@@ -146,14 +142,15 @@ private List<SourceExecutionDto> extractSourceExecutions(Path path, String sourc
146142
}
147143
}
148144

149-
private List<AccessTrendDto> extractAccessTrends(Path path, List<EndpointDto> endpoints, String userID) {
145+
private List<AccessTrendDto> extractAccessTrends(Path path, List<EndpointDto> endpoints, boolean showUserInformation) {
150146
List<Pattern> patterns = endpoints.stream()
151147
.map(endpointPair -> {
152148
String method = endpointPair.getMethod();
153149
String endpoint = endpointPair.getUrlPattern().replaceAll("\\{\\}", ".*");
150+
String userId = endpointPair.getUserId();
154151
String regexpStr = ENDPOINT_REGEXP.replace("{METHOD_PLACEHOLDER}", method);
155152
regexpStr = regexpStr.replace("{ENDPOINT_PLACEHOLDER}", endpoint);
156-
regexpStr = regexpStr.replace("{USERID_PLACEHOLDER}", userID);
153+
regexpStr = regexpStr.replace("{USERID_PLACEHOLDER}", userId);
157154

158155
return Pattern.compile(regexpStr);
159156
})
@@ -176,12 +173,12 @@ private List<AccessTrendDto> extractAccessTrends(Path path, List<EndpointDto> en
176173
}
177174
}
178175

179-
private Optional<SourceExecutionDto> getMatchedExecution(String str, String sourceKey, String userID) {
176+
private Optional<SourceExecutionDto> getMatchedExecution(String str, String sourceKey, boolean showUserInformation) {
180177
return patternMap.entrySet().stream()
181178
.map(entry -> new ImmutablePair<>(entry.getKey(), entry.getValue().matcher(str)))
182179
.filter(pair -> pair.getValue().matches())
183180
.filter(pair -> sourceKey == null || (sourceKey != null && sourceKey.equals(pair.getValue().group(2))))
184-
.map(pair -> new SourceExecutionDto(pair.getValue().group(2), pair.getKey(), LocalDate.parse(pair.getValue().group(1)), userID))
181+
.map(pair -> new SourceExecutionDto(pair.getValue().group(2), pair.getKey(), LocalDate.parse(pair.getValue().group(1)), pair.getValue().group(3)))
185182
.findFirst();
186183
}
187184

0 commit comments

Comments
 (0)