-
Notifications
You must be signed in to change notification settings - Fork 168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Application usage statistics #2387
Open
alex-odysseus
wants to merge
11
commits into
master
Choose a base branch
from
application-usage-statistics
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e9bbb31
add execution and trends statistics
ssuvorov-fls c8df50c
Feature readiness
alex-odysseus 49b51a8
Fixing statistics endpoint permission method GET -> POST
alex-odysseus 30493ce
ATL-8: Atlas Usage Statistics
hernaldourbina 1678925
ATL-8: Addressing userId info
hernaldourbina 83662bd
ATL-8: Updating username logic
hernaldourbina c80b8f4
ATL-8: Adding accesstrends regexp update
hernaldourbina 88893ce
ATL-8: Adding userInformation to the response
hernaldourbina fe86641
Minor formatting
alex-odysseus cd0f48a
Merge remote-tracking branch 'remotes/origin/execution_and_trend_stat…
alex-odysseus 831e3e7
Log entry added, non-closed resources are correctly handled, file hea…
alex-odysseus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
250 changes: 250 additions & 0 deletions
250
src/main/java/org/ohdsi/webapi/statistic/controller/StatisticController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,250 @@ | ||
package org.ohdsi.webapi.statistic.controller; | ||
|
||
import com.opencsv.CSVWriter; | ||
|
||
import org.ohdsi.webapi.statistic.dto.AccessTrendDto; | ||
import org.ohdsi.webapi.statistic.dto.AccessTrendsDto; | ||
import org.ohdsi.webapi.statistic.dto.EndpointDto; | ||
import org.ohdsi.webapi.statistic.dto.SourceExecutionDto; | ||
import org.ohdsi.webapi.statistic.dto.SourceExecutionsDto; | ||
import org.ohdsi.webapi.statistic.service.StatisticService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Controller; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.StringWriter; | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Controller | ||
@Path("/statistic/") | ||
@ConditionalOnProperty(value = "audit.trail.enabled", havingValue = "true") | ||
public class StatisticController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(StatisticController.class); | ||
|
||
private StatisticService service; | ||
|
||
public enum ResponseFormat { | ||
CSV, JSON | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both CSV and JSON output formats should be supported |
||
} | ||
|
||
private static final List<String[]> EXECUTION_STATISTICS_CSV_RESULT_HEADER = new ArrayList<String[]>() {{ | ||
add(new String[]{"Date", "Source", "Execution Type"}); | ||
}}; | ||
|
||
private static final List<String[]> EXECUTION_STATISTICS_CSV_RESULT_HEADER_WITH_USER_ID = new ArrayList<String[]>() {{ | ||
add(new String[]{"Date", "Source", "Execution Type", "User ID"}); | ||
}}; | ||
|
||
private static final List<String[]> ACCESS_TRENDS_CSV_RESULT_HEADER = new ArrayList<String[]>() {{ | ||
add(new String[]{"Date", "Endpoint"}); | ||
}}; | ||
|
||
private static final List<String[]> ACCESS_TRENDS_CSV_RESULT_HEADER_WITH_USER_ID = new ArrayList<String[]>() {{ | ||
add(new String[]{"Date", "Endpoint", "User ID"}); | ||
}}; | ||
|
||
public StatisticController(StatisticService service) { | ||
this.service = service; | ||
} | ||
|
||
/** | ||
* Returns execution statistics | ||
* @param executionStatisticsRequest - filter settings for statistics | ||
*/ | ||
@POST | ||
@Path("/executions") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
public Response executionStatistics(ExecutionStatisticsRequest executionStatisticsRequest) { | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); | ||
boolean showUserInformation = executionStatisticsRequest.isShowUserInformation(); | ||
|
||
SourceExecutionsDto sourceExecutions = service.getSourceExecutions(LocalDate.parse(executionStatisticsRequest.getStartDate(), formatter), | ||
LocalDate.parse(executionStatisticsRequest.getEndDate(), formatter), executionStatisticsRequest.getSourceKey(), showUserInformation); | ||
|
||
if (ResponseFormat.CSV.equals(executionStatisticsRequest.getResponseFormat())) { | ||
return prepareExecutionResultResponse(sourceExecutions.getExecutions(), "execution_statistics.zip", showUserInformation); | ||
} else { | ||
return Response.ok(sourceExecutions).build(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns access trends statistics | ||
* @param accessTrendsStatisticsRequest - filter settings for statistics | ||
*/ | ||
@POST | ||
@Path("/accesstrends") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
public Response accessStatistics(AccessTrendsStatisticsRequest accessTrendsStatisticsRequest) { | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); | ||
boolean showUserInformation = accessTrendsStatisticsRequest.isShowUserInformation(); | ||
|
||
AccessTrendsDto trends = service.getAccessTrends(LocalDate.parse(accessTrendsStatisticsRequest.getStartDate(), formatter), | ||
LocalDate.parse(accessTrendsStatisticsRequest.getEndDate(), formatter), accessTrendsStatisticsRequest.getEndpoints(), showUserInformation); | ||
|
||
if (ResponseFormat.CSV.equals(accessTrendsStatisticsRequest.getResponseFormat())) { | ||
return prepareAccessTrendsResponse(trends.getTrends(), "execution_trends.zip", showUserInformation); | ||
} else { | ||
return Response.ok(trends).build(); | ||
} | ||
} | ||
|
||
private Response prepareExecutionResultResponse(List<SourceExecutionDto> executions, String filename, boolean showUserInformation) { | ||
List<String[]> data = executions.stream() | ||
.map(execution -> showUserInformation | ||
? new String[]{execution.getExecutionDate(), execution.getSourceName(), execution.getExecutionName(), execution.getUserId()} | ||
: new String[]{execution.getExecutionDate(), execution.getSourceName(), execution.getExecutionName()} | ||
) | ||
.collect(Collectors.toList()); | ||
return prepareResponse(data, filename, showUserInformation ? EXECUTION_STATISTICS_CSV_RESULT_HEADER_WITH_USER_ID : EXECUTION_STATISTICS_CSV_RESULT_HEADER); | ||
} | ||
|
||
private Response prepareAccessTrendsResponse(List<AccessTrendDto> trends, String filename, boolean showUserInformation) { | ||
List<String[]> data = trends.stream() | ||
.map(trend -> showUserInformation | ||
? new String[]{trend.getExecutionDate().toString(), trend.getEndpointName(), trend.getUserID()} | ||
: new String[]{trend.getExecutionDate().toString(), trend.getEndpointName()} | ||
) | ||
.collect(Collectors.toList()); | ||
return prepareResponse(data, filename, showUserInformation ? ACCESS_TRENDS_CSV_RESULT_HEADER_WITH_USER_ID : ACCESS_TRENDS_CSV_RESULT_HEADER); | ||
} | ||
|
||
private Response prepareResponse(List<String[]> data, String filename, List<String[]> header) { | ||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
StringWriter sw = new StringWriter(); | ||
CSVWriter csvWriter = new CSVWriter(sw, ',', CSVWriter.DEFAULT_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER)) { | ||
|
||
csvWriter.writeAll(header); | ||
csvWriter.writeAll(data); | ||
csvWriter.flush(); | ||
baos.write(sw.getBuffer().toString().getBytes()); | ||
|
||
return Response | ||
.ok(baos) | ||
.type(MediaType.APPLICATION_OCTET_STREAM) | ||
.header("Content-Disposition", String.format("attachment; filename=\"%s\"", filename)) | ||
.build(); | ||
} catch (Exception ex) { | ||
log.error("An error occurred while building a response"); | ||
throw new RuntimeException(ex); | ||
alex-odysseus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
public static final class ExecutionStatisticsRequest { | ||
// Format - yyyy-MM-dd | ||
String startDate; | ||
// Format - yyyy-MM-dd | ||
String endDate; | ||
String sourceKey; | ||
ResponseFormat responseFormat; | ||
boolean showUserInformation; | ||
|
||
public String getStartDate() { | ||
return startDate; | ||
} | ||
|
||
public void setStartDate(String startDate) { | ||
this.startDate = startDate; | ||
} | ||
|
||
public String getEndDate() { | ||
return endDate; | ||
} | ||
|
||
public void setEndDate(String endDate) { | ||
this.endDate = endDate; | ||
} | ||
|
||
public String getSourceKey() { | ||
return sourceKey; | ||
} | ||
|
||
public void setSourceKey(String sourceKey) { | ||
this.sourceKey = sourceKey; | ||
} | ||
|
||
public ResponseFormat getResponseFormat() { | ||
return responseFormat; | ||
} | ||
|
||
public void setResponseFormat(ResponseFormat responseFormat) { | ||
this.responseFormat = responseFormat; | ||
} | ||
|
||
public boolean isShowUserInformation() { | ||
return showUserInformation; | ||
} | ||
|
||
public void setShowUserInformation(boolean showUserInformation) { | ||
this.showUserInformation = showUserInformation; | ||
} | ||
} | ||
|
||
public static final class AccessTrendsStatisticsRequest { | ||
// Format - yyyy-MM-dd | ||
String startDate; | ||
// Format - yyyy-MM-dd | ||
String endDate; | ||
// Key - method (POST, GET) | ||
// Value - endpoint ("{}" can be used as a placeholder, will be converted to ".*" in regular expression) | ||
List<EndpointDto> endpoints; | ||
ResponseFormat responseFormat; | ||
boolean showUserInformation; | ||
|
||
public String getStartDate() { | ||
return startDate; | ||
} | ||
|
||
public void setStartDate(String startDate) { | ||
this.startDate = startDate; | ||
} | ||
|
||
public String getEndDate() { | ||
return endDate; | ||
} | ||
|
||
public void setEndDate(String endDate) { | ||
this.endDate = endDate; | ||
} | ||
|
||
public List<EndpointDto> getEndpoints() { | ||
return endpoints; | ||
} | ||
|
||
public void setEndpoints(List<EndpointDto> endpoints) { | ||
this.endpoints = endpoints; | ||
} | ||
|
||
public ResponseFormat getResponseFormat() { | ||
return responseFormat; | ||
} | ||
|
||
public void setResponseFormat(ResponseFormat responseFormat) { | ||
this.responseFormat = responseFormat; | ||
} | ||
|
||
public boolean isShowUserInformation() { | ||
return showUserInformation; | ||
} | ||
|
||
public void setShowUserInformation(boolean showUserInformation) { | ||
this.showUserInformation = showUserInformation; | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/org/ohdsi/webapi/statistic/dto/AccessTrendDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.ohdsi.webapi.statistic.dto; | ||
|
||
public class AccessTrendDto { | ||
private String endpointName; | ||
private String executionDate; | ||
private String userID; | ||
|
||
public AccessTrendDto(String endpointName, String executionDate, String userID) { | ||
this.endpointName = endpointName; | ||
this.executionDate = executionDate; | ||
this.userID = userID; | ||
} | ||
|
||
public String getEndpointName() { | ||
return endpointName; | ||
} | ||
|
||
public void setEndpointName(String endpointName) { | ||
this.endpointName = endpointName; | ||
} | ||
|
||
public String getExecutionDate() { | ||
return executionDate; | ||
} | ||
|
||
public void setExecutionDate(String executionDate) { | ||
this.executionDate = executionDate; | ||
} | ||
|
||
public String getUserID() { | ||
return userID; | ||
} | ||
|
||
public void setUserID(String userID) { | ||
this.userID = userID; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/org/ohdsi/webapi/statistic/dto/AccessTrendsDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.ohdsi.webapi.statistic.dto; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class AccessTrendsDto { | ||
private List<AccessTrendDto> trends = new ArrayList<>(); | ||
|
||
public AccessTrendsDto(List<AccessTrendDto> trends) { | ||
this.trends = trends; | ||
} | ||
|
||
public List<AccessTrendDto> getTrends() { | ||
return trends; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/org/ohdsi/webapi/statistic/dto/EndpointDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.ohdsi.webapi.statistic.dto; | ||
|
||
public class EndpointDto { | ||
String method; | ||
String urlPattern; | ||
String userId; | ||
|
||
public String getMethod() { | ||
return method; | ||
} | ||
|
||
public void setMethod(String method) { | ||
this.method = method; | ||
} | ||
|
||
public String getUrlPattern() { | ||
return urlPattern; | ||
} | ||
|
||
public void setUrlPattern(String urlPattern) { | ||
this.urlPattern = urlPattern; | ||
} | ||
|
||
public String getUserId() { | ||
return userId; | ||
} | ||
|
||
public void setUserId(String userId) { | ||
this.userId = userId; | ||
} | ||
} | ||
|
47 changes: 47 additions & 0 deletions
47
src/main/java/org/ohdsi/webapi/statistic/dto/SourceExecutionDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.ohdsi.webapi.statistic.dto; | ||
|
||
public class SourceExecutionDto { | ||
private String sourceName; | ||
private String executionName; | ||
private String executionDate; | ||
private String userId; | ||
|
||
public SourceExecutionDto(String sourceName, String executionName, String executionDate, String userId) { | ||
this.sourceName = sourceName; | ||
this.executionName = executionName; | ||
this.executionDate = executionDate; | ||
this.userId = userId; | ||
} | ||
|
||
public String getSourceName() { | ||
return sourceName; | ||
} | ||
|
||
public void setSourceName(String sourceName) { | ||
this.sourceName = sourceName; | ||
} | ||
|
||
public String getExecutionName() { | ||
return executionName; | ||
} | ||
|
||
public void setExecutionName(String executionName) { | ||
this.executionName = executionName; | ||
} | ||
|
||
public String getExecutionDate() { | ||
return executionDate; | ||
} | ||
|
||
public void setExecutionDate(String executionDate) { | ||
this.executionDate = executionDate; | ||
} | ||
|
||
public String getUserId() { | ||
return userId; | ||
} | ||
|
||
public void setUserId(String userId) { | ||
this.userId = userId; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If Audit Trail feature is not enabled the Controller is not efficient
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the intent here to make the
/statistic/
end-point available based on theaudit.trail.enabled
property? Seems a little confusing to make an endpoint conditional vs. having it always available and to have it return based on the status of the configuration of the application.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally see no problem that some of the endpoints are not available if an appropriate configuration is not at place