Skip to content

Commit

Permalink
Added unit test to usage metrics endpoints (#1133)
Browse files Browse the repository at this point in the history
* Added unit test to usage metrics endpoints
  • Loading branch information
jfkonecn authored Jul 24, 2024
1 parent db3de2d commit 43fab0d
Show file tree
Hide file tree
Showing 5 changed files with 1,034 additions and 28 deletions.
14 changes: 14 additions & 0 deletions src/main/java/org/mskcc/cbio/oncokb/config/ClockConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.mskcc.cbio.oncokb.config;

import java.time.Clock;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ClockConfig {

@Bean
public Clock clock() {
return Clock.systemUTC();
}
}
3 changes: 2 additions & 1 deletion src/main/java/org/mskcc/cbio/oncokb/config/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public final class Constants {

public static final String MONTH_USERS_USAGE_SUMMARY_FILE_PREFIX = "public-website/usage-analysis/month-user-summary_";
public static final String YEAR_USERS_USAGE_SUMMARY_FILE_PREFIX = "public-website/usage-analysis/year-user-summary_";
public static final String MONTH_RESOURCES_USAGE_SUMMARY_FILE_PREFIX = "public-website/usage-analysis/month-resource-summary_";
public static final String YEAR_RESOURCES_USAGE_SUMMARY_FILE_PREFIX = "public-website/usage-analysis/year-resource-summary_";
public static final String TOKEN_STATS_STORAGE_FILE_PREFIX = "public-website/token-usage/token-stats_";

Expand All @@ -43,7 +44,7 @@ public final class Constants {
public static final String TESTING_TOKEN = "faketoken";

public static final String ONCOKB_S3_BUCKET = "oncokb-v2";

private Constants() {
}

Expand Down
40 changes: 21 additions & 19 deletions src/main/java/org/mskcc/cbio/oncokb/util/TimeUtil.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
package org.mskcc.cbio.oncokb.util;

import org.mskcc.cbio.oncokb.config.Constants;

import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
import org.mskcc.cbio.oncokb.config.Constants;

/**
* Created by Hongxin Zhang on 4/6/21.
*/
public class TimeUtil {
public static String toSystemDefaultZoneTime(Instant time) {
DateTimeFormatter formatter =
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)
.withLocale(Locale.US)
.withZone(ZoneId.systemDefault());
return formatter.format(time);
}

public static String toNYZoneTime(Instant time) {
DateTimeFormatter formatter =
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)
.withLocale(Locale.US)
.withZone(ZoneId.of(Constants.NY_ZONE_ID));
return formatter.format(time);
}
public static String toSystemDefaultZoneTime(Instant time) {
DateTimeFormatter formatter = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.FULL)
.withLocale(Locale.US)
.withZone(ZoneId.systemDefault());
return formatter.format(time);
}

public static String toNYZoneTime(Instant time) {
DateTimeFormatter formatter = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.FULL)
.withLocale(Locale.US)
.withZone(ZoneId.of(Constants.NY_ZONE_ID));
return formatter.format(time);
}

public static ZonedDateTime getCurrentNYTime() {
return ZonedDateTime.now(ZoneId.of(Constants.NY_ZONE_ID));
}
// TODO: Convert TimeUtil into a service class so we don't have autowire a clock into every class
public static ZonedDateTime getCurrentNYTime(Clock clock) {
return ZonedDateTime.now(clock.withZone(ZoneId.of(Constants.NY_ZONE_ID)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.time.Clock;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
Expand Down Expand Up @@ -52,6 +53,9 @@ public class UsageAnalysisController {
@Autowired
private UserMapper userMapper;

@Autowired
private Clock clock;

private JSONObject requestData(String file)
throws UnsupportedEncodingException, IOException, ParseException {
Optional<ResponseInputStream<GetObjectResponse>> s3object = s3Service.getObject(Constants.ONCOKB_S3_BUCKET, file);
Expand All @@ -66,7 +70,7 @@ private JSONObject requestData(String file)
/**
* API to get the detail usage info for specific user
* @param userId
* @return user usage infomation of given user
* @return user usage information of given user
* @throws IOException
* @throws ParseException
*/
Expand All @@ -76,13 +80,13 @@ public ResponseEntity<UserUsage> userUsageGet(@PathVariable @NotNull Long userId
HttpStatus status = HttpStatus.OK;

if (userId != null) {
int year = TimeUtil.getCurrentNYTime().getYear();
int year = TimeUtil.getCurrentNYTime(clock).getYear();
JSONObject yearSummary = requestData(YEAR_USERS_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension.JSON_FILE.getExtension());
Map<String, JSONObject> monthSummaries = new HashMap<>();
int monthsBack = 0;
JSONObject monthSummary;
do {
String month = TimeUtil.getCurrentNYTime().minus(monthsBack, ChronoUnit.MONTHS).format(DateTimeFormatter.ofPattern("yyyy-MM"));
String month = TimeUtil.getCurrentNYTime(clock).minus(monthsBack, ChronoUnit.MONTHS).format(DateTimeFormatter.ofPattern("yyyy-MM"));
monthSummary = requestData(MONTH_USERS_USAGE_SUMMARY_FILE_PREFIX + month + FileExtension.JSON_FILE.getExtension());
if (monthSummary != null) {
monthSummaries.put(month, monthSummary);
Expand Down Expand Up @@ -143,13 +147,13 @@ public ResponseEntity<List<UserOverviewUsage>> userOverviewUsageGet(@RequestPara
throws IOException, ParseException {
HttpStatus status = HttpStatus.OK;

int year = TimeUtil.getCurrentNYTime().getYear();
int year = TimeUtil.getCurrentNYTime(clock).getYear();
JSONObject yearSummary = requestData(YEAR_USERS_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension.JSON_FILE.getExtension());
Map<String, JSONObject> monthSummaries = new HashMap<>();
int monthsBack = 0;
JSONObject monthSummary;
do {
String month = TimeUtil.getCurrentNYTime().minus(monthsBack, ChronoUnit.MONTHS).format(DateTimeFormatter.ofPattern("yyyy-MM"));
String month = TimeUtil.getCurrentNYTime(clock).minus(monthsBack, ChronoUnit.MONTHS).format(DateTimeFormatter.ofPattern("yyyy-MM"));
monthSummary = requestData(MONTH_USERS_USAGE_SUMMARY_FILE_PREFIX + month + FileExtension.JSON_FILE.getExtension());
if (monthSummary != null) {
monthSummaries.put(month, monthSummary);
Expand Down Expand Up @@ -251,7 +255,7 @@ public ResponseEntity<UsageSummary> resourceUsageGet()
throws IOException, ParseException {
HttpStatus status = HttpStatus.OK;

int year = TimeUtil.getCurrentNYTime().getYear();
int year = TimeUtil.getCurrentNYTime(clock).getYear();
JSONObject jsonObject = requestData(YEAR_RESOURCES_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension.JSON_FILE.getExtension());

Gson gson = new Gson();
Expand All @@ -263,7 +267,7 @@ public ResponseEntity<UsageSummary> resourceUsageGet()
}

/**
* API to get the usage of a sepcific resource
* API to get the usage of a specific resource
* @param endpoint
* @return usage of a specific endpoint
* @throws UnsupportedEncodingException
Expand All @@ -275,7 +279,7 @@ public ResponseEntity<UsageSummary> resourceDetailGet(@RequestParam String endpo
throws UnsupportedEncodingException, IOException, ParseException {
HttpStatus status = HttpStatus.OK;

int year = TimeUtil.getCurrentNYTime().getYear();
int year = TimeUtil.getCurrentNYTime(clock).getYear();
JSONObject resourceSummary = requestData(YEAR_RESOURCES_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension.JSON_FILE.getExtension());
JSONObject userSummary = requestData(YEAR_USERS_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension.JSON_FILE.getExtension());
if (resourceSummary != null && userSummary != null ){
Expand Down
Loading

0 comments on commit 43fab0d

Please sign in to comment.